mirror of
https://github.com/ckolivas/lrzip.git
synced 2026-01-18 14:29:57 +01:00
move more code out of main.c, allocate outfile suffix
This commit is contained in:
parent
08d2294e5e
commit
133b201867
2
lrzip.c
2
lrzip.c
|
|
@ -1157,7 +1157,7 @@ void initialize_control(rzip_control *control)
|
|||
control->msgout = stderr;
|
||||
register_outputfile(control, control->msgout);
|
||||
control->flags = FLAG_SHOW_PROGRESS | FLAG_KEEP_FILES | FLAG_THRESHOLD;
|
||||
control->suffix = ".lrz";
|
||||
control->suffix = strdup(".lrz");
|
||||
control->compression_level = 7;
|
||||
control->ramsize = get_ram(control);
|
||||
/* for testing single CPU */
|
||||
|
|
|
|||
|
|
@ -289,7 +289,7 @@ struct rzip_control {
|
|||
i64 in_len;
|
||||
i64 in_maxlen;
|
||||
FILE *msgout; //stream for output messages
|
||||
const char *suffix;
|
||||
char *suffix;
|
||||
uchar compression_level;
|
||||
i64 overhead; // compressor overhead
|
||||
i64 usable_ram; // the most ram we'll try to use on one activity
|
||||
|
|
|
|||
166
main.c
166
main.c
|
|
@ -26,9 +26,6 @@
|
|||
#ifdef HAVE_UNISTD_H
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
#ifdef HAVE_CTYPE_H
|
||||
# include <ctype.h>
|
||||
#endif
|
||||
#ifdef HAVE_SYS_TIME_H
|
||||
# include <sys/time.h>
|
||||
#endif
|
||||
|
|
@ -61,10 +58,6 @@
|
|||
/* needed for CRC routines */
|
||||
#include "lzma/C/7zCrc.h"
|
||||
|
||||
/* Macros for testing parameters */
|
||||
#define isparameter( parmstring, value ) (!strcasecmp( parmstring, value ))
|
||||
#define iscaseparameter( parmvalue, value ) (!strcmp( parmvalue, value ))
|
||||
|
||||
static rzip_control controlstaticvariablehaha, *control;
|
||||
|
||||
static void usage(void)
|
||||
|
|
@ -192,165 +185,6 @@ static void show_summary(void)
|
|||
}
|
||||
}
|
||||
|
||||
static void read_config(rzip_control *control)
|
||||
{
|
||||
/* check for lrzip.conf in ., $HOME/.lrzip and /etc/lrzip */
|
||||
char *HOME, *homeconf;
|
||||
char *parametervalue;
|
||||
char *parameter;
|
||||
char *line;
|
||||
FILE *fp;
|
||||
|
||||
line = malloc(255);
|
||||
homeconf = malloc(255);
|
||||
if (line == NULL || homeconf == NULL)
|
||||
fatal(control, "Fatal Memory Error in read_config");
|
||||
|
||||
fp = fopen("lrzip.conf", "r");
|
||||
if (fp)
|
||||
fprintf(control->msgout, "Using configuration file ./lrzip.conf\n");
|
||||
if (fp == NULL) {
|
||||
fp = fopen("/etc/lrzip/lrzip.conf", "r");
|
||||
if (fp)
|
||||
fprintf(control->msgout, "Using configuration file /etc/lrzip/lrzip.conf\n");
|
||||
}
|
||||
if (fp == NULL) {
|
||||
HOME=getenv("HOME");
|
||||
if (HOME) {
|
||||
strcpy(homeconf, HOME);
|
||||
strcat(homeconf,"/.lrzip/lrzip.conf");
|
||||
fp = fopen(homeconf, "r");
|
||||
if (fp)
|
||||
fprintf(control->msgout, "Using configuration file %s\n", homeconf);
|
||||
}
|
||||
}
|
||||
if (fp == NULL)
|
||||
goto out;
|
||||
|
||||
/* if we get here, we have a file. read until no more. */
|
||||
|
||||
while ((fgets(line, 255, fp)) != NULL) {
|
||||
if (strlen(line))
|
||||
line[strlen(line) - 1] = '\0';
|
||||
parameter = strtok(line, " =");
|
||||
if (parameter == NULL)
|
||||
continue;
|
||||
/* skip if whitespace or # */
|
||||
if (isspace(*parameter))
|
||||
continue;
|
||||
if (*parameter == '#')
|
||||
continue;
|
||||
|
||||
parametervalue = strtok(NULL, " =");
|
||||
if (parametervalue == NULL)
|
||||
continue;
|
||||
|
||||
/* have valid parameter line, now assign to control */
|
||||
|
||||
if (isparameter(parameter, "window"))
|
||||
control->window = atoi(parametervalue);
|
||||
else if (isparameter(parameter, "unlimited")) {
|
||||
if (isparameter(parametervalue, "yes"))
|
||||
control->flags |= FLAG_UNLIMITED;
|
||||
} else if (isparameter(parameter, "compressionlevel")) {
|
||||
control->compression_level = atoi(parametervalue);
|
||||
if ( control->compression_level < 1 || control->compression_level > 9 )
|
||||
failure(control, "CONF.FILE error. Compression Level must between 1 and 9");
|
||||
} else if (isparameter(parameter, "compressionmethod")) {
|
||||
/* valid are rzip, gzip, bzip2, lzo, lzma (default), and zpaq */
|
||||
if (control->flags & FLAG_NOT_LZMA)
|
||||
failure(control, "CONF.FILE error. Can only specify one compression method");
|
||||
if (isparameter(parametervalue, "bzip2"))
|
||||
control->flags |= FLAG_BZIP2_COMPRESS;
|
||||
else if (isparameter(parametervalue, "gzip"))
|
||||
control->flags |= FLAG_ZLIB_COMPRESS;
|
||||
else if (isparameter(parametervalue, "lzo"))
|
||||
control->flags |= FLAG_LZO_COMPRESS;
|
||||
else if (isparameter(parametervalue, "rzip"))
|
||||
control->flags |= FLAG_NO_COMPRESS;
|
||||
else if (isparameter(parametervalue, "zpaq"))
|
||||
control->flags |= FLAG_ZPAQ_COMPRESS;
|
||||
else if (!isparameter(parametervalue, "lzma")) /* oops, not lzma! */
|
||||
failure(control, "CONF.FILE error. Invalid compression method %s specified\n",parametervalue);
|
||||
} else if (isparameter(parameter, "lzotest")) {
|
||||
/* default is yes */
|
||||
if (isparameter(parametervalue, "no"))
|
||||
control->flags &= ~FLAG_THRESHOLD;
|
||||
} else if (isparameter(parameter, "hashcheck")) {
|
||||
if (isparameter(parametervalue, "yes")) {
|
||||
control->flags |= FLAG_CHECK;
|
||||
control->flags |= FLAG_HASH;
|
||||
}
|
||||
} else if (isparameter(parameter, "showhash")) {
|
||||
if (isparameter(parametervalue, "yes"))
|
||||
control->flags |= FLAG_HASH;
|
||||
} else if (isparameter(parameter, "outputdirectory")) {
|
||||
control->outdir = malloc(strlen(parametervalue) + 2);
|
||||
if (!control->outdir)
|
||||
fatal(control, "Fatal Memory Error in read_config");
|
||||
strcpy(control->outdir, parametervalue);
|
||||
if (strcmp(parametervalue + strlen(parametervalue) - 1, "/"))
|
||||
strcat(control->outdir, "/");
|
||||
} else if (isparameter(parameter,"verbosity")) {
|
||||
if (control->flags & FLAG_VERBOSE)
|
||||
failure(control, "CONF.FILE error. Verbosity already defined.");
|
||||
if (isparameter(parametervalue, "yes"))
|
||||
control->flags |= FLAG_VERBOSITY;
|
||||
else if (isparameter(parametervalue,"max"))
|
||||
control->flags |= FLAG_VERBOSITY_MAX;
|
||||
else /* oops, unrecognized value */
|
||||
print_err("lrzip.conf: Unrecognized verbosity value %s. Ignored.\n", parametervalue);
|
||||
} else if (isparameter(parameter, "showprogress")) {
|
||||
/* Yes by default */
|
||||
if (isparameter(parametervalue, "NO"))
|
||||
control->flags &= ~FLAG_SHOW_PROGRESS;
|
||||
} else if (isparameter(parameter,"nice")) {
|
||||
control->nice_val = atoi(parametervalue);
|
||||
if (control->nice_val < -20 || control->nice_val > 19)
|
||||
failure(control, "CONF.FILE error. Nice must be between -20 and 19");
|
||||
} else if (isparameter(parameter, "keepbroken")) {
|
||||
if (isparameter(parametervalue, "yes" ))
|
||||
control->flags |= FLAG_KEEP_BROKEN;
|
||||
} else if (iscaseparameter(parameter, "DELETEFILES")) {
|
||||
/* delete files must be case sensitive */
|
||||
if (iscaseparameter(parametervalue, "YES"))
|
||||
control->flags &= ~FLAG_KEEP_FILES;
|
||||
} else if (iscaseparameter(parameter, "REPLACEFILE")) {
|
||||
/* replace lrzip file must be case sensitive */
|
||||
if (iscaseparameter(parametervalue, "YES"))
|
||||
control->flags |= FLAG_FORCE_REPLACE;
|
||||
} else if (isparameter(parameter, "tmpdir")) {
|
||||
control->tmpdir = realloc(NULL, strlen(parametervalue) + 2);
|
||||
if (!control->tmpdir)
|
||||
fatal(control, "Fatal Memory Error in read_config");
|
||||
strcpy(control->tmpdir, parametervalue);
|
||||
if (strcmp(parametervalue + strlen(parametervalue) - 1, "/"))
|
||||
strcat(control->tmpdir, "/");
|
||||
} else if (isparameter(parameter, "encrypt")) {
|
||||
if (isparameter(parameter, "YES"))
|
||||
control->flags |= FLAG_ENCRYPT;
|
||||
} else
|
||||
/* oops, we have an invalid parameter, display */
|
||||
print_err("lrzip.conf: Unrecognized parameter value, %s = %s. Continuing.\n",\
|
||||
parameter, parametervalue);
|
||||
}
|
||||
|
||||
if (unlikely(fclose(fp)))
|
||||
fatal(control, "Failed to fclose fp in read_config\n");
|
||||
out:
|
||||
/* clean up */
|
||||
free(line);
|
||||
free(homeconf);
|
||||
|
||||
/* fprintf(stderr, "\nWindow = %d \
|
||||
\nCompression Level = %d \
|
||||
\nThreshold = %1.2f \
|
||||
\nOutput Directory = %s \
|
||||
\nFlags = %d\n", control->window,control->compression_level, control->threshold, control->outdir, control->flags);
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct timeval start_time, end_time;
|
||||
|
|
|
|||
1
rzip.c
1
rzip.c
|
|
@ -1032,5 +1032,6 @@ void rzip_control_free(rzip_control *control)
|
|||
if (!control) return;
|
||||
|
||||
free(control->tmpdir);
|
||||
free(control->suffix);
|
||||
free(control);
|
||||
}
|
||||
|
|
|
|||
165
util.c
165
util.c
|
|
@ -56,6 +56,13 @@
|
|||
#include "util.h"
|
||||
#include "sha4.h"
|
||||
#include "aes.h"
|
||||
#ifdef HAVE_CTYPE_H
|
||||
# include <ctype.h>
|
||||
#endif
|
||||
|
||||
/* Macros for testing parameters */
|
||||
#define isparameter( parmstring, value ) (!strcasecmp( parmstring, value ))
|
||||
#define iscaseparameter( parmvalue, value ) (!strcmp( parmvalue, value ))
|
||||
|
||||
void register_infile(rzip_control *control, const char *name, char delete)
|
||||
{
|
||||
|
|
@ -187,6 +194,164 @@ void get_rand(rzip_control *control, uchar *buf, int len)
|
|||
}
|
||||
}
|
||||
|
||||
void read_config(rzip_control *control)
|
||||
{
|
||||
/* check for lrzip.conf in ., $HOME/.lrzip and /etc/lrzip */
|
||||
char *HOME, *homeconf;
|
||||
char *parametervalue;
|
||||
char *parameter;
|
||||
char *line;
|
||||
FILE *fp;
|
||||
|
||||
line = malloc(255);
|
||||
homeconf = malloc(255);
|
||||
if (line == NULL || homeconf == NULL)
|
||||
fatal(control, "Fatal Memory Error in read_config");
|
||||
|
||||
fp = fopen("lrzip.conf", "r");
|
||||
if (fp)
|
||||
fprintf(control->msgout, "Using configuration file ./lrzip.conf\n");
|
||||
if (fp == NULL) {
|
||||
fp = fopen("/etc/lrzip/lrzip.conf", "r");
|
||||
if (fp)
|
||||
fprintf(control->msgout, "Using configuration file /etc/lrzip/lrzip.conf\n");
|
||||
}
|
||||
if (fp == NULL) {
|
||||
HOME=getenv("HOME");
|
||||
if (HOME) {
|
||||
strcpy(homeconf, HOME);
|
||||
strcat(homeconf,"/.lrzip/lrzip.conf");
|
||||
fp = fopen(homeconf, "r");
|
||||
if (fp)
|
||||
fprintf(control->msgout, "Using configuration file %s\n", homeconf);
|
||||
}
|
||||
}
|
||||
if (fp == NULL)
|
||||
goto out;
|
||||
|
||||
/* if we get here, we have a file. read until no more. */
|
||||
|
||||
while ((fgets(line, 255, fp)) != NULL) {
|
||||
if (strlen(line))
|
||||
line[strlen(line) - 1] = '\0';
|
||||
parameter = strtok(line, " =");
|
||||
if (parameter == NULL)
|
||||
continue;
|
||||
/* skip if whitespace or # */
|
||||
if (isspace(*parameter))
|
||||
continue;
|
||||
if (*parameter == '#')
|
||||
continue;
|
||||
|
||||
parametervalue = strtok(NULL, " =");
|
||||
if (parametervalue == NULL)
|
||||
continue;
|
||||
|
||||
/* have valid parameter line, now assign to control */
|
||||
|
||||
if (isparameter(parameter, "window"))
|
||||
control->window = atoi(parametervalue);
|
||||
else if (isparameter(parameter, "unlimited")) {
|
||||
if (isparameter(parametervalue, "yes"))
|
||||
control->flags |= FLAG_UNLIMITED;
|
||||
} else if (isparameter(parameter, "compressionlevel")) {
|
||||
control->compression_level = atoi(parametervalue);
|
||||
if ( control->compression_level < 1 || control->compression_level > 9 )
|
||||
failure(control, "CONF.FILE error. Compression Level must between 1 and 9");
|
||||
} else if (isparameter(parameter, "compressionmethod")) {
|
||||
/* valid are rzip, gzip, bzip2, lzo, lzma (default), and zpaq */
|
||||
if (control->flags & FLAG_NOT_LZMA)
|
||||
failure(control, "CONF.FILE error. Can only specify one compression method");
|
||||
if (isparameter(parametervalue, "bzip2"))
|
||||
control->flags |= FLAG_BZIP2_COMPRESS;
|
||||
else if (isparameter(parametervalue, "gzip"))
|
||||
control->flags |= FLAG_ZLIB_COMPRESS;
|
||||
else if (isparameter(parametervalue, "lzo"))
|
||||
control->flags |= FLAG_LZO_COMPRESS;
|
||||
else if (isparameter(parametervalue, "rzip"))
|
||||
control->flags |= FLAG_NO_COMPRESS;
|
||||
else if (isparameter(parametervalue, "zpaq"))
|
||||
control->flags |= FLAG_ZPAQ_COMPRESS;
|
||||
else if (!isparameter(parametervalue, "lzma")) /* oops, not lzma! */
|
||||
failure(control, "CONF.FILE error. Invalid compression method %s specified\n",parametervalue);
|
||||
} else if (isparameter(parameter, "lzotest")) {
|
||||
/* default is yes */
|
||||
if (isparameter(parametervalue, "no"))
|
||||
control->flags &= ~FLAG_THRESHOLD;
|
||||
} else if (isparameter(parameter, "hashcheck")) {
|
||||
if (isparameter(parametervalue, "yes")) {
|
||||
control->flags |= FLAG_CHECK;
|
||||
control->flags |= FLAG_HASH;
|
||||
}
|
||||
} else if (isparameter(parameter, "showhash")) {
|
||||
if (isparameter(parametervalue, "yes"))
|
||||
control->flags |= FLAG_HASH;
|
||||
} else if (isparameter(parameter, "outputdirectory")) {
|
||||
control->outdir = malloc(strlen(parametervalue) + 2);
|
||||
if (!control->outdir)
|
||||
fatal(control, "Fatal Memory Error in read_config");
|
||||
strcpy(control->outdir, parametervalue);
|
||||
if (strcmp(parametervalue + strlen(parametervalue) - 1, "/"))
|
||||
strcat(control->outdir, "/");
|
||||
} else if (isparameter(parameter,"verbosity")) {
|
||||
if (control->flags & FLAG_VERBOSE)
|
||||
failure(control, "CONF.FILE error. Verbosity already defined.");
|
||||
if (isparameter(parametervalue, "yes"))
|
||||
control->flags |= FLAG_VERBOSITY;
|
||||
else if (isparameter(parametervalue,"max"))
|
||||
control->flags |= FLAG_VERBOSITY_MAX;
|
||||
else /* oops, unrecognized value */
|
||||
print_err("lrzip.conf: Unrecognized verbosity value %s. Ignored.\n", parametervalue);
|
||||
} else if (isparameter(parameter, "showprogress")) {
|
||||
/* Yes by default */
|
||||
if (isparameter(parametervalue, "NO"))
|
||||
control->flags &= ~FLAG_SHOW_PROGRESS;
|
||||
} else if (isparameter(parameter,"nice")) {
|
||||
control->nice_val = atoi(parametervalue);
|
||||
if (control->nice_val < -20 || control->nice_val > 19)
|
||||
failure(control, "CONF.FILE error. Nice must be between -20 and 19");
|
||||
} else if (isparameter(parameter, "keepbroken")) {
|
||||
if (isparameter(parametervalue, "yes" ))
|
||||
control->flags |= FLAG_KEEP_BROKEN;
|
||||
} else if (iscaseparameter(parameter, "DELETEFILES")) {
|
||||
/* delete files must be case sensitive */
|
||||
if (iscaseparameter(parametervalue, "YES"))
|
||||
control->flags &= ~FLAG_KEEP_FILES;
|
||||
} else if (iscaseparameter(parameter, "REPLACEFILE")) {
|
||||
/* replace lrzip file must be case sensitive */
|
||||
if (iscaseparameter(parametervalue, "YES"))
|
||||
control->flags |= FLAG_FORCE_REPLACE;
|
||||
} else if (isparameter(parameter, "tmpdir")) {
|
||||
control->tmpdir = realloc(NULL, strlen(parametervalue) + 2);
|
||||
if (!control->tmpdir)
|
||||
fatal(control, "Fatal Memory Error in read_config");
|
||||
strcpy(control->tmpdir, parametervalue);
|
||||
if (strcmp(parametervalue + strlen(parametervalue) - 1, "/"))
|
||||
strcat(control->tmpdir, "/");
|
||||
} else if (isparameter(parameter, "encrypt")) {
|
||||
if (isparameter(parameter, "YES"))
|
||||
control->flags |= FLAG_ENCRYPT;
|
||||
} else
|
||||
/* oops, we have an invalid parameter, display */
|
||||
print_err("lrzip.conf: Unrecognized parameter value, %s = %s. Continuing.\n",\
|
||||
parameter, parametervalue);
|
||||
}
|
||||
|
||||
if (unlikely(fclose(fp)))
|
||||
fatal(control, "Failed to fclose fp in read_config\n");
|
||||
out:
|
||||
/* clean up */
|
||||
free(line);
|
||||
free(homeconf);
|
||||
|
||||
/* fprintf(stderr, "\nWindow = %d \
|
||||
\nCompression Level = %d \
|
||||
\nThreshold = %1.2f \
|
||||
\nOutput Directory = %s \
|
||||
\nFlags = %d\n", control->window,control->compression_level, control->threshold, control->outdir, control->flags);
|
||||
*/
|
||||
}
|
||||
|
||||
static void xor128 (void *pa, const void *pb)
|
||||
{
|
||||
i64 *a = pa;
|
||||
|
|
|
|||
1
util.h
1
util.h
|
|
@ -31,6 +31,7 @@ void setup_overhead(rzip_control *control);
|
|||
void setup_ram(rzip_control *control);
|
||||
void round_to_page(i64 *size);
|
||||
void get_rand(rzip_control *control, uchar *buf, int len);
|
||||
void read_config(rzip_control *control);
|
||||
void lrz_stretch(rzip_control *control);
|
||||
void lrz_stretch2(rzip_control *control);
|
||||
void lrz_crypt(const rzip_control *control, uchar *buf, i64 len, const uchar *salt, int encrypt);
|
||||
|
|
|
|||
Loading…
Reference in a new issue