From fe68b9a3f7a335bb0ee0d9bd5f07a121416f337d Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sat, 12 Mar 2011 11:17:11 +1100 Subject: [PATCH] Institute writing and reading of 0.6 file format for compress/decompress. --- lrzip.c | 17 ++++++++++------- lrzip_private.h | 3 ++- stream.c | 23 +++++++++++++++++++++-- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/lrzip.c b/lrzip.c index a6ad4a0..a1ee5a8 100644 --- a/lrzip.c +++ b/lrzip.c @@ -46,7 +46,7 @@ void write_magic(rzip_control *control, int fd_in, int fd_out) { struct timeval tv; struct stat st; - char magic[40]; + char magic[39]; int i; memset(magic, 0, sizeof(magic)); @@ -92,12 +92,13 @@ void write_magic(rzip_control *control, int fd_in, int fd_out) void read_magic(rzip_control *control, int fd_in, i64 *expected_size) { - char magic[40]; + char magic[39]; uint32_t v; int md5, i; - memset(magic, 0, 40); - if (unlikely(read(fd_in, magic, sizeof(magic)) != sizeof(magic))) + memset(magic, 0, sizeof(magic)); + /* Initially read only major_version == 0 && control->minor_version > 5) { + if (unlikely(read(fd_in, magic + 24, 15) != 15)) + fatal("Failed to read magic header\n"); if (magic[22] == 1) control->encrypt = 1; memcpy(&control->secs, &magic[23], 8); @@ -635,7 +638,7 @@ void compress_file(rzip_control *control) * Spares a compiler warning */ int fd_in, fd_out; - char header[24]; + char header[39]; memset(header, 0, sizeof(header)); @@ -708,13 +711,13 @@ void compress_file(rzip_control *control) preserve_perms(control, fd_in, fd_out); - /* write zeroes to 24 bytes at beginning of file */ + /* Write zeroes to header at beginning of file */ if (unlikely(write(fd_out, header, sizeof(header)) != sizeof(header))) fatal("Cannot write file header\n"); rzip_fd(control, fd_in, fd_out); - /* write magic at end b/c lzma does not tell us properties until it is done */ + /* Wwrite magic at end b/c lzma does not tell us properties until it is done */ write_magic(control, fd_in, fd_out); if (STDOUT) diff --git a/lrzip_private.h b/lrzip_private.h index bff5b2f..8fd9762 100644 --- a/lrzip_private.h +++ b/lrzip_private.h @@ -180,7 +180,7 @@ struct rzip_control { int encrypt; i64 secs; i64 usecs; - int eof; + unsigned char eof; md5_ctx ctx; i64 md5_read; // How far into the file the md5 has done so far }; @@ -206,6 +206,7 @@ struct stream_info { i64 initial_pos; i64 total_read; i64 ram_alloced; + i64 size; long thread_no; long next_thread; int chunks; diff --git a/stream.c b/stream.c index 2fc7cad..c07e177 100644 --- a/stream.c +++ b/stream.c @@ -818,7 +818,7 @@ void *open_stream_out(rzip_control *control, int f, unsigned int n, i64 chunk_li if (unlikely(!sinfo)) return NULL; - sinfo->bufsize = limit = chunk_limit; + sinfo->bufsize = sinfo->size = limit = chunk_limit; sinfo->chunk_bytes = cbytes; sinfo->num_streams = n; @@ -917,7 +917,6 @@ void *open_stream_in(rzip_control *control, int f, int n) sinfo->num_streams = n; sinfo->fd = f; - sinfo->initial_pos = lseek(f, 0, SEEK_CUR); sinfo->s = calloc(sizeof(struct stream), n); if (unlikely(!sinfo->s)) { @@ -928,6 +927,21 @@ void *open_stream_in(rzip_control *control, int f, int n) sinfo->s[0].total_threads = 1; sinfo->s[1].total_threads = total_threads - 1; + if (control->major_version == 0 && control->minor_version > 5) { + /* Read in flag that tells us if there are more chunks after + * this */ + if (unlikely(read_u8(f, &control->eof))) { + print_err("Failed to read eof flag in open_stream_in\n"); + goto failed; + } + /* Read in the expected chunk size */ + if (unlikely(read_i64(f, &sinfo->size))) { + print_err("Failed to read in chunk size in open_stream_in\n"); + goto failed; + } + } + sinfo->initial_pos = lseek(f, 0, SEEK_CUR); + for (i = 0; i < n; i++) { uchar c; i64 v1, v2; @@ -1060,6 +1074,11 @@ retry: /* Write chunk bytes of this block */ write_u8(ctis->fd, ctis->chunk_bytes); + /* Write whether this is the last chunk, followed by the size + * of this chunk */ + write_u8(ctis->fd, control->eof); + write_i64(ctis->fd, ctis->size); + /* First chunk of this stream, write headers */ ctis->initial_pos = lseek(ctis->fd, 0, SEEK_CUR);