Institute writing and reading of 0.6 file format for compress/decompress.

This commit is contained in:
Con Kolivas 2011-03-12 11:17:11 +11:00
parent d87df4f44e
commit fe68b9a3f7
3 changed files with 33 additions and 10 deletions

17
lrzip.c
View file

@ -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 <v0.6x header */
if (unlikely(read(fd_in, magic, 24) != 24))
fatal("Failed to read magic header\n");
*expected_size = 0;
@ -117,6 +118,8 @@ void read_magic(rzip_control *control, int fd_in, i64 *expected_size)
} else {
memcpy(expected_size, &magic[6], 8);
if (control->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)

View file

@ -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;

View file

@ -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);