Convert liblrzip.c into 1TBS style.

This commit is contained in:
Con Kolivas 2011-09-13 14:53:11 +10:00
parent c665665020
commit b1c5d5504a

View file

@ -26,7 +26,8 @@ static FILE *fake_fmemopen(void *buf, size_t buflen, const char *mode)
FILE *in; FILE *in;
in = tmpfile(); in = tmpfile();
if (!in) return NULL; if (!in)
return NULL;
if (fwrite(buf, buflen, 1, in) != 1) { if (fwrite(buf, buflen, 1, in) != 1) {
fclose(in); fclose(in);
return NULL; return NULL;
@ -45,7 +46,8 @@ static void liblrzip_index_update(size_t x, size_t *idx, void **queue)
static bool liblrzip_setup_flags(Lrzip *lr) static bool liblrzip_setup_flags(Lrzip *lr)
{ {
if (!lr) return false; if (!lr)
return false;
#define MODE_CHECK(X) \ #define MODE_CHECK(X) \
case LRZIP_MODE_COMPRESS_##X: \ case LRZIP_MODE_COMPRESS_##X: \
lr->control->flags ^= FLAG_NOT_LZMA; \ lr->control->flags ^= FLAG_NOT_LZMA; \
@ -132,7 +134,8 @@ void lrzip_free(Lrzip *lr)
{ {
size_t x; size_t x;
if ((!lr) || (!lr->infilename_buckets)) return; if ((!lr) || (!lr->infilename_buckets))
return;
rzip_control_free(lr->control); rzip_control_free(lr->control);
for (x = 0; x < lr->infilename_idx; x++) for (x = 0; x < lr->infilename_idx; x++)
free(lr->infilenames[x]); free(lr->infilenames[x]);
@ -146,10 +149,13 @@ Lrzip *lrzip_new(Lrzip_Mode mode)
Lrzip *lr; Lrzip *lr;
lr = calloc(1, sizeof(Lrzip)); lr = calloc(1, sizeof(Lrzip));
if (!lr) return NULL; if (!lr)
return NULL;
lr->control = calloc(1, sizeof(rzip_control)); lr->control = calloc(1, sizeof(rzip_control));
if (!lr->control) goto error; if (!lr->control)
if (!initialize_control(lr->control)) goto error; goto error;
if (!initialize_control(lr->control))
goto error;
lr->mode = mode; lr->mode = mode;
lr->control->library_mode = 1; lr->control->library_mode = 1;
return lr; return lr;
@ -160,106 +166,124 @@ error:
Lrzip_Mode lrzip_mode_get(Lrzip *lr) Lrzip_Mode lrzip_mode_get(Lrzip *lr)
{ {
if (!lr) return LRZIP_MODE_NONE; if (!lr)
return LRZIP_MODE_NONE;
return lr->mode; return lr->mode;
} }
bool lrzip_mode_set(Lrzip *lr, Lrzip_Mode mode) bool lrzip_mode_set(Lrzip *lr, Lrzip_Mode mode)
{ {
if ((!lr) || (mode > LRZIP_MODE_COMPRESS_ZPAQ)) return false; if ((!lr) || (mode > LRZIP_MODE_COMPRESS_ZPAQ))
return false;
lr->mode = mode; lr->mode = mode;
return true; return true;
} }
bool lrzip_compression_level_set(Lrzip *lr, unsigned int level) bool lrzip_compression_level_set(Lrzip *lr, unsigned int level)
{ {
if ((!lr) || (!level) || (level > 9)) return false; if ((!lr) || (!level) || (level > 9))
return false;
lr->control->compression_level = level; lr->control->compression_level = level;
return true; return true;
} }
unsigned int lrzip_compression_level_get(Lrzip *lr) unsigned int lrzip_compression_level_get(Lrzip *lr)
{ {
if (!lr) return 0; if (!lr)
return 0;
return lr->control->compression_level; return lr->control->compression_level;
} }
void lrzip_flags_set(Lrzip *lr, unsigned int flags) void lrzip_flags_set(Lrzip *lr, unsigned int flags)
{ {
if (!lr) return; if (!lr)
return;
lr->flags = flags; lr->flags = flags;
} }
unsigned int lrzip_flags_get(Lrzip *lr) unsigned int lrzip_flags_get(Lrzip *lr)
{ {
if (!lr) return 0; if (!lr)
return 0;
return lr->flags; return lr->flags;
} }
void lrzip_nice_set(Lrzip *lr, int nice) void lrzip_nice_set(Lrzip *lr, int nice)
{ {
if ((!lr) || (nice < -19) || (nice > 20)) return; if ((!lr) || (nice < -19) || (nice > 20))
return;
lr->control->nice_val = nice; lr->control->nice_val = nice;
} }
int lrzip_nice_get(Lrzip *lr) int lrzip_nice_get(Lrzip *lr)
{ {
if (!lr) return 0; if (!lr)
return 0;
return lr->control->nice_val; return lr->control->nice_val;
} }
void lrzip_threads_set(Lrzip *lr, unsigned int threads) void lrzip_threads_set(Lrzip *lr, unsigned int threads)
{ {
if ((!lr) || (!threads)) return; if ((!lr) || (!threads))
return;
lr->control->threads = threads; lr->control->threads = threads;
} }
unsigned int lrzip_threads_get(Lrzip *lr) unsigned int lrzip_threads_get(Lrzip *lr)
{ {
if (!lr) return 0; if (!lr)
return 0;
return lr->control->threads; return lr->control->threads;
} }
void lrzip_compression_window_max_set(Lrzip *lr, int64_t size) void lrzip_compression_window_max_set(Lrzip *lr, int64_t size)
{ {
if (!lr) return; if (!lr)
return;
lr->control->window = size; lr->control->window = size;
} }
int64_t lrzip_compression_window_max_get(Lrzip *lr) int64_t lrzip_compression_window_max_get(Lrzip *lr)
{ {
if (!lr) return -1; if (!lr)
return -1;
return lr->control->window; return lr->control->window;
} }
unsigned int lrzip_files_count(Lrzip *lr) unsigned int lrzip_files_count(Lrzip *lr)
{ {
if (!lr) return 0; if (!lr)
return 0;
return lr->infile_idx; return lr->infile_idx;
} }
unsigned int lrzip_filenames_count(Lrzip *lr) unsigned int lrzip_filenames_count(Lrzip *lr)
{ {
if (!lr) return 0; if (!lr)
return 0;
return lr->infilename_idx; return lr->infilename_idx;
} }
FILE **lrzip_files_get(Lrzip *lr) FILE **lrzip_files_get(Lrzip *lr)
{ {
if (!lr) return NULL; if (!lr)
return NULL;
return lr->infiles; return lr->infiles;
} }
char **lrzip_filenames_get(Lrzip *lr) char **lrzip_filenames_get(Lrzip *lr)
{ {
if (!lr) return NULL; if (!lr)
return NULL;
return lr->infilenames; return lr->infilenames;
} }
bool lrzip_file_add(Lrzip *lr, FILE *file) bool lrzip_file_add(Lrzip *lr, FILE *file)
{ {
if ((!lr) || (!file)) return false; if ((!lr) || (!file))
if (lr->infilenames) return false; return false;
if (lr->infilenames)
return false;
if (!lr->infile_buckets) { if (!lr->infile_buckets) {
/* no files added */ /* no files added */
lr->infiles = calloc(INFILE_BUCKET_SIZE + 1, sizeof(void*)); lr->infiles = calloc(INFILE_BUCKET_SIZE + 1, sizeof(void*));
@ -269,7 +293,8 @@ bool lrzip_file_add(Lrzip *lr, FILE *file)
FILE **tmp; FILE **tmp;
tmp = realloc(lr->infiles, (++lr->infile_buckets * INFILE_BUCKET_SIZE + 1) * sizeof(void*)); tmp = realloc(lr->infiles, (++lr->infile_buckets * INFILE_BUCKET_SIZE + 1) * sizeof(void*));
if (!tmp) return false; if (!tmp)
return false;
lr->infiles = tmp; lr->infiles = tmp;
} }
@ -281,12 +306,16 @@ bool lrzip_file_del(Lrzip *lr, FILE *file)
{ {
size_t x; size_t x;
if ((!lr) || (!file)) return false; if ((!lr) || (!file))
if (!lr->infile_buckets) return true; return false;
if (!lr->infile_buckets)
return true;
for (x = 0; x <= lr->infile_idx + 1; x++) { for (x = 0; x <= lr->infile_idx + 1; x++) {
if (!lr->infiles[x]) return true; /* not found */ if (!lr->infiles[x])
if (lr->infiles[x] != file) continue; /* not a match */ return true; /* not found */
if (lr->infiles[x] != file)
continue; /* not a match */
break; break;
} }
/* update index */ /* update index */
@ -297,7 +326,8 @@ bool lrzip_file_del(Lrzip *lr, FILE *file)
FILE *lrzip_file_pop(Lrzip *lr) FILE *lrzip_file_pop(Lrzip *lr)
{ {
FILE *ret; FILE *ret;
if ((!lr) || (!lr->infile_buckets)) return NULL; if ((!lr) || (!lr->infile_buckets))
return NULL;
ret = lr->infiles[0]; ret = lr->infiles[0];
lrzip_file_del(lr, ret); lrzip_file_del(lr, ret);
return ret; return ret;
@ -305,7 +335,8 @@ FILE *lrzip_file_pop(Lrzip *lr)
void lrzip_files_clear(Lrzip *lr) void lrzip_files_clear(Lrzip *lr)
{ {
if ((!lr) || (!lr->infile_buckets)) return; if ((!lr) || (!lr->infile_buckets))
return;
free(lr->infiles); free(lr->infiles);
lr->infiles = NULL; lr->infiles = NULL;
} }
@ -314,10 +345,14 @@ bool lrzip_filename_add(Lrzip *lr, const char *file)
{ {
struct stat st; struct stat st;
if ((!lr) || (!file) || (!file[0]) || (!strcmp(file, "-"))) return false; if ((!lr) || (!file) || (!file[0]) || (!strcmp(file, "-")))
if (lr->infiles) return false; return false;
if (stat(file, &st)) return false; if (lr->infiles)
if (S_ISDIR(st.st_mode)) return false; return false;
if (stat(file, &st))
return false;
if (S_ISDIR(st.st_mode))
return false;
if (!lr->infilename_buckets) { if (!lr->infilename_buckets) {
/* no files added */ /* no files added */
@ -328,7 +363,8 @@ bool lrzip_filename_add(Lrzip *lr, const char *file)
char **tmp; char **tmp;
tmp = realloc(lr->infilenames, (++lr->infilename_buckets * INFILE_BUCKET_SIZE + 1) * sizeof(void*)); tmp = realloc(lr->infilenames, (++lr->infilename_buckets * INFILE_BUCKET_SIZE + 1) * sizeof(void*));
if (!tmp) return false; if (!tmp)
return false;
lr->infilenames = tmp; lr->infilenames = tmp;
} }
@ -340,12 +376,16 @@ bool lrzip_filename_del(Lrzip *lr, const char *file)
{ {
size_t x; size_t x;
if ((!lr) || (!file) || (!file[0])) return false; if ((!lr) || (!file) || (!file[0]))
if (!lr->infilename_buckets) return true; return false;
if (!lr->infilename_buckets)
return true;
for (x = 0; x <= lr->infilename_idx + 1; x++) { for (x = 0; x <= lr->infilename_idx + 1; x++) {
if (!lr->infilenames[x]) return true; /* not found */ if (!lr->infilenames[x])
if (strcmp(lr->infilenames[x], file)) continue; /* not a match */ return true; /* not found */
if (strcmp(lr->infilenames[x], file))
continue; /* not a match */
free(lr->infilenames[x]); free(lr->infilenames[x]);
break; break;
} }
@ -357,7 +397,8 @@ bool lrzip_filename_del(Lrzip *lr, const char *file)
const char *lrzip_filename_pop(Lrzip *lr) const char *lrzip_filename_pop(Lrzip *lr)
{ {
static char buf[4096]; static char buf[4096];
if ((!lr) || (!lr->infilename_buckets)) return NULL; if ((!lr) || (!lr->infilename_buckets))
return NULL;
strcat(buf, lr->infilenames[0]); strcat(buf, lr->infilenames[0]);
lrzip_filename_del(lr, buf); lrzip_filename_del(lr, buf);
return &buf[0]; return &buf[0];
@ -366,7 +407,8 @@ const char *lrzip_filename_pop(Lrzip *lr)
void lrzip_filenames_clear(Lrzip *lr) void lrzip_filenames_clear(Lrzip *lr)
{ {
size_t x; size_t x;
if ((!lr) || (!lr->infilename_buckets)) return; if ((!lr) || (!lr->infilename_buckets))
return;
for (x = 0; x < lr->infilename_idx; x++) for (x = 0; x < lr->infilename_idx; x++)
free(lr->infilenames[x]); free(lr->infilenames[x]);
free(lr->infilenames); free(lr->infilenames);
@ -375,14 +417,16 @@ void lrzip_filenames_clear(Lrzip *lr)
void lrzip_suffix_set(Lrzip *lr, const char *suffix) void lrzip_suffix_set(Lrzip *lr, const char *suffix)
{ {
if ((!lr) || (!suffix) || (!suffix[0])) return; if ((!lr) || (!suffix) || (!suffix[0]))
return;
free(lr->control->suffix); free(lr->control->suffix);
lr->control->suffix = strdup(suffix); lr->control->suffix = strdup(suffix);
} }
const char *lrzip_suffix_get(Lrzip *lr) const char *lrzip_suffix_get(Lrzip *lr)
{ {
if (!lr) return NULL; if (!lr)
return NULL;
return lr->control->suffix; return lr->control->suffix;
} }
@ -391,7 +435,8 @@ void lrzip_outdir_set(Lrzip *lr, const char *dir)
const char *slash; const char *slash;
char *buf; char *buf;
size_t len; size_t len;
if ((!lr) || (!dir) || (!dir[0])) return; if ((!lr) || (!dir) || (!dir[0]))
return;
free(lr->control->outdir); free(lr->control->outdir);
slash = strrchr(dir, '/'); slash = strrchr(dir, '/');
if (slash && (slash[1] == 0)) { if (slash && (slash[1] == 0)) {
@ -400,7 +445,8 @@ void lrzip_outdir_set(Lrzip *lr, const char *dir)
} }
len = strlen(dir); len = strlen(dir);
buf = malloc(len + 2); buf = malloc(len + 2);
if (!buf) return; if (!buf)
return;
memcpy(buf, dir, len); memcpy(buf, dir, len);
buf[len] = '/'; buf[len] = '/';
buf[len + 1] = 0; buf[len + 1] = 0;
@ -409,41 +455,50 @@ void lrzip_outdir_set(Lrzip *lr, const char *dir)
const char *lrzip_outdir_get(Lrzip *lr) const char *lrzip_outdir_get(Lrzip *lr)
{ {
if (!lr) return NULL; if (!lr)
return NULL;
return lr->control->outdir; return lr->control->outdir;
} }
void lrzip_outfile_set(Lrzip *lr, FILE *file) void lrzip_outfile_set(Lrzip *lr, FILE *file)
{ {
if ((!lr) || (file && (file == stderr))) return; if ((!lr) || (file && (file == stderr)))
if (lr->control->outname) return; return;
if (lr->control->outname)
return;
lr->control->outFILE = file; lr->control->outFILE = file;
} }
FILE *lrzip_outfile_get(Lrzip *lr) FILE *lrzip_outfile_get(Lrzip *lr)
{ {
if (!lr) return NULL; if (!lr)
return NULL;
return lr->control->outFILE; return lr->control->outFILE;
} }
void lrzip_outfilename_set(Lrzip *lr, const char *file) void lrzip_outfilename_set(Lrzip *lr, const char *file)
{ {
if ((!lr) || (file && (!file[0]))) return; if ((!lr) || (file && (!file[0])))
if (lr->control->outFILE) return; return;
if (lr->control->outname && file && (!strcmp(lr->control->outname, file))) return; if (lr->control->outFILE)
return;
if (lr->control->outname && file && (!strcmp(lr->control->outname, file)))
return;
free(lr->control->outname); free(lr->control->outname);
lr->control->outname = file ? strdup(file) : NULL; lr->control->outname = file ? strdup(file) : NULL;
} }
const char *lrzip_outfilename_get(Lrzip *lr) const char *lrzip_outfilename_get(Lrzip *lr)
{ {
if (!lr) return NULL; if (!lr)
return NULL;
return lr->control->outname; return lr->control->outname;
} }
const unsigned char *lrzip_md5digest_get(Lrzip *lr) const unsigned char *lrzip_md5digest_get(Lrzip *lr)
{ {
if (!lr) return NULL; if (!lr)
return NULL;
return lr->control->md5_resblock; return lr->control->md5_resblock;
} }
@ -454,10 +509,12 @@ bool lrzip_run(Lrzip *lr)
double seconds,total_time; // for timers double seconds,total_time; // for timers
int hours,minutes; int hours,minutes;
if (!liblrzip_setup_flags(lr)) return false; if (!liblrzip_setup_flags(lr))
return false;
control = lr->control; control = lr->control;
if ((!lr->infile_idx) && (!lr->infilename_idx)) return false; if ((!lr->infile_idx) && (!lr->infilename_idx))
return false;
if (lr->control->outFILE) { if (lr->control->outFILE) {
if (lr->control->outFILE == lr->control->msgout) if (lr->control->outFILE == lr->control->msgout)
lr->control->msgout = stderr; lr->control->msgout = stderr;
@ -485,10 +542,13 @@ bool lrzip_run(Lrzip *lr)
} }
if (DECOMPRESS || TEST_ONLY) { if (DECOMPRESS || TEST_ONLY) {
if (!decompress_file(lr->control)) return false; if (!decompress_file(lr->control))
return false;
} else if (INFO) { } else if (INFO) {
if (!get_fileinfo(lr->control)) return false; if (!get_fileinfo(lr->control))
} else if (!compress_file(lr->control)) return false; return false;
} else if (!compress_file(lr->control))
return false;
/* compute total time */ /* compute total time */
gettimeofday(&end_time, NULL); gettimeofday(&end_time, NULL);
@ -505,57 +565,66 @@ bool lrzip_run(Lrzip *lr)
void lrzip_log_level_set(Lrzip *lr, int level) void lrzip_log_level_set(Lrzip *lr, int level)
{ {
if (!lr) return; if (!lr)
return;
lr->control->log_level = level; lr->control->log_level = level;
} }
int lrzip_log_level_get(Lrzip *lr) int lrzip_log_level_get(Lrzip *lr)
{ {
if (!lr) return 0; if (!lr)
return 0;
return lr->control->log_level; return lr->control->log_level;
} }
void lrzip_log_cb_set(Lrzip *lr, Lrzip_Log_Cb cb, void *log_data) void lrzip_log_cb_set(Lrzip *lr, Lrzip_Log_Cb cb, void *log_data)
{ {
if (!lr) return; if (!lr)
return;
lr->control->log_cb = (void*)cb; lr->control->log_cb = (void*)cb;
lr->control->log_data = log_data; lr->control->log_data = log_data;
} }
void lrzip_log_stdout_set(Lrzip *lr, FILE *out) void lrzip_log_stdout_set(Lrzip *lr, FILE *out)
{ {
if (!lr) return; if (!lr)
return;
lr->control->msgout = out; lr->control->msgout = out;
} }
FILE *lrzip_log_stdout_get(Lrzip *lr) FILE *lrzip_log_stdout_get(Lrzip *lr)
{ {
if (!lr) return NULL; if (!lr)
return NULL;
return lr->control->msgout; return lr->control->msgout;
} }
void lrzip_log_stderr_set(Lrzip *lr, FILE *err) void lrzip_log_stderr_set(Lrzip *lr, FILE *err)
{ {
if (!lr) return; if (!lr)
return;
lr->control->msgerr = err; lr->control->msgerr = err;
} }
FILE *lrzip_log_stderr_get(Lrzip *lr) FILE *lrzip_log_stderr_get(Lrzip *lr)
{ {
if (!lr) return NULL; if (!lr)
return NULL;
return lr->control->msgerr; return lr->control->msgerr;
} }
void lrzip_pass_cb_set(Lrzip *lr, Lrzip_Password_Cb cb, void *data) void lrzip_pass_cb_set(Lrzip *lr, Lrzip_Password_Cb cb, void *data)
{ {
if (!lr) return; if (!lr)
return;
lr->control->pass_cb = (void*)cb; lr->control->pass_cb = (void*)cb;
lr->control->pass_data = data; lr->control->pass_data = data;
} }
void lrzip_info_cb_set(Lrzip *lr, Lrzip_Info_Cb cb, void *data) void lrzip_info_cb_set(Lrzip *lr, Lrzip_Info_Cb cb, void *data)
{ {
if (!lr) return; if (!lr)
return;
lr->control->info_cb = (void*)cb; lr->control->info_cb = (void*)cb;
lr->control->info_data = data; lr->control->info_data = data;
} }
@ -567,25 +636,32 @@ bool lrzip_compress_full(void *dest, unsigned long *dest_len, const void *source
struct stat st; struct stat st;
int fd; int fd;
if ((!dest) || (!dest_len) || (!source) || (!source_len) || (mode < LRZIP_MODE_COMPRESS_NONE)) return false; if ((!dest) || (!dest_len) || (!source) || (!source_len) || (mode < LRZIP_MODE_COMPRESS_NONE))
return false;
lrzip_init(); lrzip_init();
if (!mode) mode = LRZIP_MODE_COMPRESS_LZMA; if (!mode) mode = LRZIP_MODE_COMPRESS_LZMA;
lr = lrzip_new(mode); lr = lrzip_new(mode);
if (!lr) return false; if (!lr)
return false;
lrzip_config_env(lr); lrzip_config_env(lr);
s = fmemopen((void*)source, source_len, "r"); s = fmemopen((void*)source, source_len, "r");
d = tmpfile(); d = tmpfile();
if ((!s) || (!d)) goto error; if ((!s) || (!d))
goto error;
if (!lrzip_file_add(lr, s)) goto error; if (!lrzip_file_add(lr, s))
goto error;
lrzip_outfile_set(lr, d); lrzip_outfile_set(lr, d);
if (!lrzip_compression_level_set(lr, compress_level)) goto error; if (!lrzip_compression_level_set(lr, compress_level))
if (!lrzip_run(lr)) goto error; goto error;
if (!lrzip_run(lr))
goto error;
fd = fileno(d); fd = fileno(d);
if (fstat(fd, &st)) goto error; if (fstat(fd, &st))
goto error;
*dest_len = st.st_size; *dest_len = st.st_size;
if (unlikely(fread(dest, sizeof(char), st.st_size, d) != st.st_size)) if (unlikely(fread(dest, sizeof(char), st.st_size, d) != st.st_size))
goto error; goto error;
@ -610,23 +686,29 @@ bool lrzip_decompress(void *dest, unsigned long *dest_len, const void *source, u
struct stat st; struct stat st;
int fd; int fd;
if ((!dest) || (!dest_len) || (!source) || (!source_len)) return false; if ((!dest) || (!dest_len) || (!source) || (!source_len))
return false;
lrzip_init(); lrzip_init();
lr = lrzip_new(LRZIP_MODE_DECOMPRESS); lr = lrzip_new(LRZIP_MODE_DECOMPRESS);
if (!lr) return false; if (!lr)
return false;
lrzip_config_env(lr); lrzip_config_env(lr);
s = fmemopen((void*)source, source_len, "r"); s = fmemopen((void*)source, source_len, "r");
d = tmpfile(); d = tmpfile();
if ((!s) || (!d)) goto error; if ((!s) || (!d))
goto error;
if (!lrzip_file_add(lr, s)) goto error; if (!lrzip_file_add(lr, s))
goto error;
lrzip_outfile_set(lr, d); lrzip_outfile_set(lr, d);
if (!lrzip_run(lr)) goto error; if (!lrzip_run(lr))
goto error;
fd = fileno(d); fd = fileno(d);
if (fstat(fd, &st)) goto error; if (fstat(fd, &st))
goto error;
*dest_len = st.st_size; *dest_len = st.st_size;
if (unlikely(fread(dest, sizeof(char), st.st_size, d) != st.st_size)) if (unlikely(fread(dest, sizeof(char), st.st_size, d) != st.st_size))
goto error; goto error;