Skip to content

Commit

Permalink
another semi-clusterfuck commit: *new logging macros *allow compressi…
Browse files Browse the repository at this point in the history
…on to and from any FILE, not just stdin/stdout *update main.c to work with the above
  • Loading branch information
discomfitor authored and ckolivas committed Aug 17, 2011
1 parent b6dc92a commit 949c6d7
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 62 deletions.
13 changes: 7 additions & 6 deletions lrzip.c
Expand Up @@ -52,7 +52,6 @@
#include "runzip.h"
#include "util.h"
#include "stream.h"
#include "liblrzip.h" /* flag defines */

#define MAGIC_LEN (24)

Expand Down Expand Up @@ -319,14 +318,14 @@ static bool fwrite_stdout(rzip_control *control, void *buf, i64 len)
ret = one_g;
else
ret = len;
ret = fwrite(offset_buf, 1, ret, stdout);
ret = fwrite(offset_buf, 1, ret, control->outFILE);
if (unlikely(ret <= 0))
fatal_return(("Failed to fwrite in fwrite_stdout\n"), false);
len -= ret;
offset_buf += ret;
total += ret;
}
fflush(stdout);
fflush(control->outFILE);
return true;
}

Expand Down Expand Up @@ -375,10 +374,10 @@ bool dump_tmpoutfile(rzip_control *control, int fd_out)
rewind(tmpoutfp);

if (!TEST_ONLY) {
print_verbose("Dumping temporary file to stdout.\n");
print_verbose("Dumping temporary file to control->outFILE.\n");
while ((tmpchar = fgetc(tmpoutfp)) != EOF)
putchar(tmpchar);
fflush(stdout);
fflush(control->outFILE);
rewind(tmpoutfp);
}

Expand Down Expand Up @@ -564,7 +563,7 @@ static bool get_hash(rzip_control *control, int make_hash)
mlock(control->hash, HASH_LEN);

if (control->pass_cb) {
control->pass_cb(control->pass_data, passphrase, PASS_LEN);
control->pass_cb(control->pass_data, passphrase, PASS_LEN - SALT_LEN);
if (!passphrase[0]) {
fatal("Supplied password was null!");
munlock(passphrase, PASS_LEN);
Expand All @@ -574,6 +573,7 @@ static bool get_hash(rzip_control *control, int make_hash)
release_hashes(control);
return false;
}
control->salt_pass_len = strlen(passphrase) + SALT_LEN;
} else {
/* Disable stdin echo to screen */
tcgetattr(fileno(stdin), &termios_p);
Expand Down Expand Up @@ -1194,6 +1194,7 @@ bool initialize_control(rzip_control *control)

memset(control, 0, sizeof(rzip_control));
control->msgout = stderr;
control->msgerr = stderr;
register_outputfile(control, control->msgout);
control->flags = FLAG_SHOW_PROGRESS | FLAG_KEEP_FILES | FLAG_THRESHOLD;
control->suffix = strdup(".lrz");
Expand Down
93 changes: 67 additions & 26 deletions lrzip_private.h
Expand Up @@ -27,6 +27,7 @@
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdarg.h>
#ifdef HAVE_STRING_H
# include <string.h>
#endif
Expand All @@ -52,6 +53,10 @@ extern "C"
void *alloca (size_t);
#endif

#ifndef MD5_DIGEST_SIZE
# define MD5_DIGEST_SIZE 16
#endif

#define free(X) do { free((X)); (X) = NULL; } while (0)

#ifndef strdupa
Expand Down Expand Up @@ -262,31 +267,6 @@ typedef struct md5_ctx md5_ctx;
#define TMP_INBUF (control->flags & FLAG_TMP_INBUF)
#define ENCRYPT (control->flags & FLAG_ENCRYPT)

#define print_output(format, args...) do {\
fprintf(control->msgout, format, ##args); \
fflush(control->msgout); \
} while (0)

#define print_progress(format, args...) do {\
if (SHOW_PROGRESS) \
print_output(format, ##args); \
} while (0)

#define print_verbose(format, args...) do {\
if (VERBOSE) \
print_output(format, ##args); \
} while (0)

#define print_maxverbose(format, args...) do {\
if (MAX_VERBOSE) \
print_output(format, ##args); \
} while (0)


#define print_err(format, args...) do {\
fprintf(stderr, format, ##args); \
} while (0)


/* Structure to save state of computation between the single steps. */
struct md5_ctx
Expand All @@ -303,8 +283,10 @@ struct md5_ctx

struct rzip_control {
char *infile;
FILE *inFILE; // if a FILE is being read from
char *outname;
char *outfile;
FILE *outFILE; // if a FILE is being written to
char *outdir;
char *tmpdir; // when stdin, stdout, or test used
uchar *tmp_outbuf; // Temporary file storage for stdout
Expand All @@ -318,6 +300,7 @@ struct rzip_control {
i64 in_len;
i64 in_maxlen;
FILE *msgout; //stream for output messages
FILE *msgerr; //stream for output errors
char *suffix;
uchar compression_level;
i64 overhead; // compressor overhead
Expand Down Expand Up @@ -349,14 +332,16 @@ struct rzip_control {
unsigned char eof;
unsigned char magic_written;
md5_ctx ctx;
uchar md5_resblock[MD5_DIGEST_SIZE];
i64 md5_read; // How far into the file the md5 has done so far
const char *util_infile;
char delete_infile;
const char *util_outfile;
char delete_outfile;
FILE *outputfile;
char library_mode : 1;
void (*log_cb)(void *, unsigned int, const char *, const char *, va_list);
int log_level;
void (*log_cb)(void *data, unsigned int level, unsigned int line, const char *file, const char *func, const char *format, va_list);
void *log_data;
};

Expand Down Expand Up @@ -387,4 +372,60 @@ struct stream_info {
int chunks;
char chunk_bytes;
};

static inline void print_stuff(const rzip_control *control, int level, unsigned int line, const char *file, const char *func, const char *format, ...)
{
va_list ap;
if (control->library_mode && control->log_cb && (control->log_level >= level)) {
va_start(ap, format);
control->log_cb(control->log_data, level, line, file, func, format, ap);
va_end(ap);
} else if (control->msgout) {
va_start(ap, format);
vfprintf(control->msgout, format, ap);
fflush(control->msgout);
va_end(ap);
}
}

static inline void print_err(const rzip_control *control, unsigned int line, const char *file, const char *func, const char *format, ...)
{
va_list ap;
if (control->library_mode && control->log_cb && (control->log_level >= 0)) {
va_start(ap, format);
control->log_cb(control->log_data, 0, line, file, func, format, ap);
va_end(ap);
} else if (control->msgerr) {
va_start(ap, format);
vfprintf(control->msgerr, format, ap);
va_end(ap);
}
}

#define print_stuff(level, format, args...) do {\
print_stuff(control, level, __LINE__, __FILE__, __func__, format, ##args); \
} while (0)

#define print_output(format, args...) do {\
print_stuff(1, format, ##args); \
} while (0)

#define print_progress(format, args...) do {\
if (SHOW_PROGRESS) \
print_stuff(2, format, ##args); \
} while (0)

#define print_verbose(format, args...) do {\
if (VERBOSE) \
print_stuff(3, format, ##args); \
} while (0)

#define print_maxverbose(format, args...) do {\
if (MAX_VERBOSE) \
print_stuff(4, format, ##args); \
} while (0)

#define print_err(format, args...) do {\
print_err(control, __LINE__, __FILE__, __func__, format, ##args); \
} while (0)
#endif
6 changes: 5 additions & 1 deletion main.c
Expand Up @@ -49,7 +49,6 @@
# include <arpa/inet.h>
#endif

#include "liblrzip.h"
#include "rzip.h"
#include "lrzip.h"
#include "util.h"
Expand Down Expand Up @@ -284,6 +283,7 @@ int main(int argc, char *argv[])
if (unlikely(STDOUT))
failure("Cannot specify an output filename when outputting to stdout\n");
control->outname = optarg;
free(control->suffix);
control->suffix = "";
break;
case 'O':
Expand Down Expand Up @@ -311,6 +311,7 @@ int main(int argc, char *argv[])
failure("Specified output filename already, can't specify an extension.\n");
if (unlikely(STDOUT))
failure("Cannot specify a filename suffix when outputting to stdout\n");
free(control->suffix);
control->suffix = optarg;
break;
case 't':
Expand Down Expand Up @@ -410,6 +411,7 @@ int main(int argc, char *argv[])

if (control->outname && (strcmp(control->outname, "-") == 0)) {
control->flags |= FLAG_STDOUT;
control->outFILE = stdout;
control->msgout = stderr;
register_outputfile(control, control->msgout);
}
Expand All @@ -419,13 +421,15 @@ int main(int argc, char *argv[])
if (!control->outname && STDIN) {
control->flags |= FLAG_STDOUT;
control->msgout = stderr;
control->outFILE = stdout;
register_outputfile(control, control->msgout);
}

if (!STDOUT) {
control->msgout = stdout;
register_outputfile(control, control->msgout);
}
if (STDIN) control->inFILE = stdin;
/* Implement signal handler only once flags are set */
handler.sa_handler = &sighandler;
sigaction(SIGTERM, &handler, 0);
Expand Down
18 changes: 8 additions & 10 deletions runzip.c
Expand Up @@ -45,7 +45,6 @@
#include "stream.h"
#include "util.h"
#include "lrzip.h"
#include "liblrzip.h"
/* needed for CRC routines */
#include "lzma/C/7zCrc.h"

Expand Down Expand Up @@ -364,7 +363,6 @@ static i64 runzip_chunk(rzip_control *control, int fd_in, i64 expected_size, i64
*/
i64 runzip_fd(rzip_control *control, int fd_in, int fd_out, int fd_hist, i64 expected_size)
{
uchar md5_resblock[MD5_DIGEST_SIZE];
uchar md5_stored[MD5_DIGEST_SIZE];
struct timeval start,end;
i64 total = 0, u;
Expand Down Expand Up @@ -395,7 +393,7 @@ i64 runzip_fd(rzip_control *control, int fd_in, int fd_out, int fd_hist, i64 exp
if (!NO_MD5) {
int i,j;

md5_finish_ctx (&control->ctx, md5_resblock);
md5_finish_ctx (&control->ctx, control->md5_resblock);
if (HAS_MD5) {
i64 fdinend = seekto_fdinend(control);

Expand All @@ -409,21 +407,21 @@ i64 runzip_fd(rzip_control *control, int fd_in, int fd_out, int fd_hist, i64 exp
if (ENCRYPT)
if (unlikely(!lrz_decrypt(control, md5_stored, MD5_DIGEST_SIZE, control->salt_pass))) return -1;
for (i = 0; i < MD5_DIGEST_SIZE; i++)
if (md5_stored[i] != md5_resblock[i]) {
if (md5_stored[i] != control->md5_resblock[i]) {
print_output("MD5 CHECK FAILED.\nStored:");
for (j = 0; j < MD5_DIGEST_SIZE; j++)
print_output("%02x", md5_stored[j] & 0xFF);
print_output("\nOutput file:");
for (j = 0; j < MD5_DIGEST_SIZE; j++)
print_output("%02x", md5_resblock[j] & 0xFF);
print_output("%02x", control->md5_resblock[j] & 0xFF);
failure_return(("\n"), -1);
}
}

if (HASH_CHECK || MAX_VERBOSE) {
print_output("MD5: ");
for (i = 0; i < MD5_DIGEST_SIZE; i++)
print_output("%02x", md5_resblock[i] & 0xFF);
print_output("%02x", control->md5_resblock[i] & 0xFF);
print_output("\n");
}

Expand All @@ -433,22 +431,22 @@ i64 runzip_fd(rzip_control *control, int fd_in, int fd_out, int fd_hist, i64 exp

if (TMP_OUTBUF)
close_tmpoutbuf(control);
memcpy(md5_stored, md5_resblock, MD5_DIGEST_SIZE);
memcpy(md5_stored, control->md5_resblock, MD5_DIGEST_SIZE);
if (unlikely(seekto_fdhist(control, 0) == -1))
fatal_return(("Failed to seekto_fdhist in runzip_fd\n"), -1);
if (unlikely((md5_fstream = fdopen(fd_hist, "r")) == NULL))
fatal_return(("Failed to fdopen fd_hist in runzip_fd\n"), -1);
if (unlikely(md5_stream(md5_fstream, md5_resblock)))
if (unlikely(md5_stream(md5_fstream, control->md5_resblock)))
fatal_return(("Failed to md5_stream in runzip_fd\n"), -1);
/* We don't close the file here as it's closed in main */
for (i = 0; i < MD5_DIGEST_SIZE; i++)
if (md5_stored[i] != md5_resblock[i]) {
if (md5_stored[i] != control->md5_resblock[i]) {
print_output("MD5 CHECK FAILED.\nStored:");
for (j = 0; j < MD5_DIGEST_SIZE; j++)
print_output("%02x", md5_stored[j] & 0xFF);
print_output("\nOutput file:");
for (j = 0; j < MD5_DIGEST_SIZE; j++)
print_output("%02x", md5_resblock[j] & 0xFF);
print_output("%02x", control->md5_resblock[j] & 0xFF);
failure_return(("\n"), -1);
}
print_output("MD5 integrity of written file matches archive\n");
Expand Down
14 changes: 6 additions & 8 deletions rzip.c
Expand Up @@ -54,7 +54,6 @@
#include "md5.h"
#include "stream.h"
#include "util.h"
#include "liblrzip.h"
#include "lrzip.h"
/* needed for CRC routines */
#include "lzma/C/7zCrc.h"
Expand Down Expand Up @@ -712,7 +711,7 @@ static bool mmap_stdin(rzip_control *control, uchar *buf, struct rzip_state *st)
total = 0;
while (len > 0) {
ret = MIN(len, one_g);
ret = read(0, offset_buf, (size_t)ret);
ret = read(fileno(control->inFILE), offset_buf, (size_t)ret);
if (unlikely(ret < 0))
fatal_return(("Failed to read in mmap_stdin\n"), false);
total += ret;
Expand Down Expand Up @@ -806,7 +805,6 @@ bool rzip_fd(rzip_control *control, int fd_in, int fd_out)
* If file size < compression window, can't do
*/
struct timeval current, start, last;
uchar md5_resblock[MD5_DIGEST_SIZE];
i64 len = 0, last_chunk = 0;
int pass = 0, passes, j;
struct rzip_state *st;
Expand Down Expand Up @@ -1066,20 +1064,20 @@ bool rzip_fd(rzip_control *control, int fd_in, int fd_out)

if (!NO_MD5) {
/* Temporary workaround till someone fixes apple md5 */
md5_finish_ctx(&control->ctx, md5_resblock);
md5_finish_ctx(&control->ctx, control->md5_resblock);
if (HASH_CHECK || MAX_VERBOSE) {
print_output("MD5: ");
for (j = 0; j < MD5_DIGEST_SIZE; j++)
print_output("%02x", md5_resblock[j] & 0xFF);
print_output("%02x", control->md5_resblock[j] & 0xFF);
print_output("\n");
}
/* When encrypting data, we encrypt the MD5 value as well */
if (ENCRYPT)
if (unlikely(!lrz_encrypt(control, md5_resblock, MD5_DIGEST_SIZE, control->salt_pass))) {
if (unlikely(!lrz_encrypt(control, control->md5_resblock, MD5_DIGEST_SIZE, control->salt_pass))) {
free(st);
return false;
}
if (unlikely(write_1g(control, md5_resblock, MD5_DIGEST_SIZE) != MD5_DIGEST_SIZE)) {
if (unlikely(write_1g(control, control->md5_resblock, MD5_DIGEST_SIZE) != MD5_DIGEST_SIZE)) {
free(st);
fatal_return(("Failed to write md5 in rzip_fd\n"), false);
}
Expand Down Expand Up @@ -1122,6 +1120,6 @@ void rzip_control_free(rzip_control *control)
if (!control) return;

free(control->tmpdir);
free(control->suffix);
if (control->suffix && control->suffix[0]) free(control->suffix);
free(control);
}

0 comments on commit 949c6d7

Please sign in to comment.