From e40f378821be25ae4667d311cf3bf3374fe63e0d Mon Sep 17 00:00:00 2001 From: Guillaume Cottenceau Date: Fri, 26 Jan 2001 17:46:26 +0000 Subject: mar now uses bzlib, no more zlib --- mdk-stage1/mar/mar-extract-only.c | 223 +++++++++++++++++--------------------- 1 file changed, 100 insertions(+), 123 deletions(-) (limited to 'mdk-stage1/mar/mar-extract-only.c') diff --git a/mdk-stage1/mar/mar-extract-only.c b/mdk-stage1/mar/mar-extract-only.c index ec99da114..2d3898cfc 100644 --- a/mdk-stage1/mar/mar-extract-only.c +++ b/mdk-stage1/mar/mar-extract-only.c @@ -24,13 +24,14 @@ * (and it DOES perform tests and return values, blaaaah..) */ +#include "mar-extract-only.h" #include "mar.h" #ifdef _STANDALONE_ void -gzerr(gzFile f) /* decrease code size */ +zerr(BZFILE * f) /* decrease code size */ { - fprintf(stderr, gzerror(f, &gz_errnum)); + fprintf(stderr, BZ2_bzerror(f, &z_errnum)); } void log_perror(char *msg) @@ -45,20 +46,89 @@ log_message(char *msg) #else /* _STANDALONE_ */ #include "../log.h" void -gzerr(gzFile f) /* decrease code size */ +zerr(BZFILE * f) /* decrease code size */ { - log_message(gzerror(f, &gz_errnum)); + log_message(BZ2_bzerror(f, &z_errnum)); } #endif /* _STANDALONE_ */ +static int +mar_open_file(char *filename, struct mar_stream *s) +{ + int end_filetable = 0; + struct mar_element * previous_element = NULL; + + /* mar_zfile */ + s->mar_zfile = BZ2_bzopen(filename, "rb"); + if (!s->mar_zfile) + { + log_perror(filename); + return -1; + } + + while (end_filetable == 0) + { + char buf[512]; + int ptr = 0; + /* read filename */ + do + { + if (BZ2_bzread(s->mar_zfile, &(buf[ptr]), sizeof(char)) != sizeof(char)) + { + zerr(s->mar_zfile); + return -1; + } + ptr++; + } while ((buf[ptr-1] != 0) && (ptr < 512)); + /* ptr == 1 when we arrive on the "char 0" of the end of the filetable */ + if (ptr > 1) + { + struct mar_element * e = (struct mar_element *) malloc(sizeof(struct mar_element)); + e->filename = strdup(buf); + /* read file_length */ + if (BZ2_bzread(s->mar_zfile, &(e->file_length), sizeof(int)) != sizeof(int)) + { + zerr(s->mar_zfile); + return -1; + } + /* read data_offset */ + if (BZ2_bzread(s->mar_zfile, &(e->data_offset), sizeof(int)) != sizeof(int)) + { + zerr(s->mar_zfile); + return -1; + } + /* write down chaining */ + if (previous_element) + previous_element->next_element = e; + else + s->first_element = e; + previous_element = e; + } + else + end_filetable = 1; + + } + /* chaining for last element */ + previous_element->next_element = NULL; + + return 0; +} + + char ** -mar_list_contents(struct mar_stream *s) +mar_list_contents(char * mar_filename) { - struct mar_element * elem = s->first_element; + struct mar_stream s; + struct mar_element * elem; char * tmp_contents[500]; char ** answ; int i = 0; + + if (mar_open_file(mar_filename, &s)) + return NULL; + + elem = s.first_element; while (elem) { tmp_contents[i++] = strdup(elem->filename); @@ -70,48 +140,28 @@ mar_list_contents(struct mar_stream *s) return answ; } + int -mar_calc_integrity(struct mar_stream *s) +mar_extract_file(char *mar_filename, char *filename_to_extract, char *dest_dir) { - char buf[4096]; - int current_crc = 0; - if (gzseek(s->mar_gzfile, sizeof(int), SEEK_SET) != sizeof(int)) - { - gzerr(s->mar_gzfile); - return -1; - } - while (!gzeof(s->mar_gzfile)) - { - int bytes = gzread(s->mar_gzfile, buf, sizeof(buf)); - if (bytes == -1) - { - gzerr(s->mar_gzfile); - return -1; - } - while (bytes > 0) - { - bytes--; - current_crc += buf[bytes]; - } - } - return current_crc; -} + struct mar_stream s; + struct mar_element * elem; + if (mar_open_file(mar_filename, &s)) + return -1; -int -mar_extract_file(struct mar_stream *s, char *filename, char *dest_dir) -{ - struct mar_element * elem = s->first_element; + elem = s.first_element; while (elem) { - if (strcmp(elem->filename, filename) == 0) + if (strcmp(elem->filename, filename_to_extract) == 0) { + char garb_buf[4096]; char *buf; char *dest_file; - int fd; - dest_file = (char *) alloca(strlen(dest_dir) + strlen(filename) + 1); + int fd, i; + dest_file = (char *) alloca(strlen(dest_dir) + strlen(filename_to_extract) + 1); strcpy(dest_file, dest_dir); - strcat(dest_file, filename); + strcat(dest_file, filename_to_extract); fd = creat(dest_file, 00660); if (fd == -1) { @@ -124,14 +174,18 @@ mar_extract_file(struct mar_stream *s, char *filename, char *dest_dir) log_perror(dest_file); return -1; } - if (gzseek(s->mar_gzfile, elem->data_offset, SEEK_SET) != elem->data_offset) - { - gzerr(s->mar_gzfile); - return -1; + i = elem->data_offset; + while (i > 0) { + int to_read = i > sizeof(garb_buf) ? sizeof(garb_buf) : i; + if (BZ2_bzread(s.mar_zfile, garb_buf, to_read) != to_read) { + log_message("MAR: unexpected EOF in stream"); + return -1; + } + i -= to_read; } - if (gzread(s->mar_gzfile, buf, elem->file_length) != elem->file_length) + if (BZ2_bzread(s.mar_zfile, buf, elem->file_length) != elem->file_length) { - gzerr(s->mar_gzfile); + zerr(s.mar_zfile); return -1; } if (write(fd, buf, elem->file_length) != elem->file_length) @@ -140,90 +194,13 @@ mar_extract_file(struct mar_stream *s, char *filename, char *dest_dir) return -1; } close(fd); /* do not check return value for code size */ + BZ2_bzclose(s.mar_zfile); return 0; } elem = elem->next_element; } + BZ2_bzclose(s.mar_zfile); return 1; /* 1 for file_not_found_in_archive */ } -int -mar_open_file(char *filename, struct mar_stream *s) -{ - int end_filetable = 0; - struct mar_element * previous_element = NULL; - - /* mar_gzfile */ - s->mar_gzfile = gzopen(filename, "rb"); - if (!s->mar_gzfile) - { - log_perror(filename); - return -1; - } - - /* crc32 */ - if (gzread(s->mar_gzfile, &(s->crc32), sizeof(int)) != sizeof(int)) - { - gzerr(s->mar_gzfile); - return -1; - } - - /* verify integrity */ - if (s->crc32 != mar_calc_integrity(s)) - log_message("ERROR! mar_open_file: CRC check failed (trying to continue)"); - - if (gzseek(s->mar_gzfile, sizeof(int), SEEK_SET) != sizeof(int)) - { - gzerr(s->mar_gzfile); - return -1; - } - - while (end_filetable == 0) - { - char buf[512]; - int ptr = 0; - /* read filename */ - do - { - if (gzread(s->mar_gzfile, &(buf[ptr]), sizeof(char)) != sizeof(char)) - { - gzerr(s->mar_gzfile); - return -1; - } - ptr++; - } while ((buf[ptr-1] != 0) && (ptr < 512)); - /* ptr == 1 when we arrive on the "char 0" of the end of the filetable */ - if (ptr > 1) - { - struct mar_element * e = (struct mar_element *) malloc(sizeof(struct mar_element)); - e->filename = strdup(buf); - /* read file_length */ - if (gzread(s->mar_gzfile, &(e->file_length), sizeof(int)) != sizeof(int)) - { - gzerr(s->mar_gzfile); - return -1; - } - /* read data_offset */ - if (gzread(s->mar_gzfile, &(e->data_offset), sizeof(int)) != sizeof(int)) - { - gzerr(s->mar_gzfile); - return -1; - } - /* write down chaining */ - if (previous_element) - previous_element->next_element = e; - else - s->first_element = e; - previous_element = e; - } - else - end_filetable = 1; - - } - /* chaining for last element */ - previous_element->next_element = NULL; - - return 0; -} - -- cgit v1.2.1