From 6b687da36f3ddcf2635349d95d9e07b889b731eb Mon Sep 17 00:00:00 2001 From: Guillaume Cottenceau Date: Thu, 16 Nov 2000 19:44:12 +0000 Subject: - fix bug of not actually writing down the `0' char to end filetable - use stat to compute file lengths - test NULL pointers directly on their value --- mdk-stage1/mar/Makefile | 5 +- mdk-stage1/mar/mar-extract-only.c | 24 ++++--- mdk-stage1/mar/mar-frontend.c | 133 ++++++++------------------------------ mdk-stage1/mar/mar.h | 4 ++ 4 files changed, 47 insertions(+), 119 deletions(-) diff --git a/mdk-stage1/mar/Makefile b/mdk-stage1/mar/Makefile index 91579aac1..6f87681f7 100644 --- a/mdk-stage1/mar/Makefile +++ b/mdk-stage1/mar/Makefile @@ -3,7 +3,8 @@ all: mar clean: rm -f *.o mar -FLAGS = -Wall -Werror -O1 -fomit-frame-pointer -c +FLAGS = -Wall -Werror -Os -fomit-frame-pointer -c + mar: mar-frontend.o mar-extract-only.o gcc -o mar mar-frontend.o mar-extract-only.o -lz @@ -12,4 +13,4 @@ mar-frontend.o: mar-frontend.c mar.h gcc $(FLAGS) mar-frontend.c mar-extract-only.o: mar-extract-only.c mar-extract-only.h mar.h - gcc $(FLAGS) mar-extract-only.c \ No newline at end of file + gcc $(FLAGS) mar-extract-only.c diff --git a/mdk-stage1/mar/mar-extract-only.c b/mdk-stage1/mar/mar-extract-only.c index 013631ce2..097ac4cb0 100644 --- a/mdk-stage1/mar/mar-extract-only.c +++ b/mdk-stage1/mar/mar-extract-only.c @@ -27,12 +27,14 @@ #include "mar.h" -void gzerr(gzFile f) /* decrease code size */ +void +gzerr(gzFile f) /* decrease code size */ { fprintf(stderr, gzerror(f, &gz_errnum)); } -int calc_integrity(mar_stream *s) +int +calc_integrity(mar_stream *s) { char buf[4096]; int current_crc = 0; @@ -61,10 +63,11 @@ int calc_integrity(mar_stream *s) } -int extract_file(mar_stream *s, char *filename, char *dest_dir) +int +extract_file(mar_stream *s, char *filename, char *dest_dir) { mar_element * elem = s->first_element; - while (elem != NULL) + while (elem) { if (strcmp(elem->filename, filename) == 0) { @@ -81,7 +84,7 @@ int extract_file(mar_stream *s, char *filename, char *dest_dir) return -1; } buf = (char *) malloc(elem->file_length); - if (buf == NULL) + if (!buf) { perror(dest_file); return -1; @@ -110,7 +113,8 @@ int extract_file(mar_stream *s, char *filename, char *dest_dir) } -mar_stream * open_marfile(char *filename) +mar_stream * +open_marfile(char *filename) { int end_filetable = 0; mar_stream * s = (mar_stream *) malloc(sizeof(mar_stream)); @@ -118,7 +122,7 @@ mar_stream * open_marfile(char *filename) /* mar_gzfile */ s->mar_gzfile = gzopen(filename, "rb"); - if (s->mar_gzfile == NULL) + if (!s->mar_gzfile) { perror(filename); return NULL; @@ -178,10 +182,10 @@ mar_stream * open_marfile(char *filename) return NULL; } /* write down chaining */ - if (previous_element == NULL) - s->first_element = e; - else + if (previous_element) previous_element->next_element = e; + else + s->first_element = e; previous_element = e; } else diff --git a/mdk-stage1/mar/mar-frontend.c b/mdk-stage1/mar/mar-frontend.c index 0b5371d47..ae9338f91 100644 --- a/mdk-stage1/mar/mar-frontend.c +++ b/mdk-stage1/mar/mar-frontend.c @@ -27,45 +27,28 @@ #include "mar.h" #include "mar-extract-only.h" -void list_files(mar_stream *s) +void +list_files(mar_stream *s) { mar_element * elem = s->first_element; printf("%-20s%8s\n", "FILENAME", "LENGTH"); - while (elem != NULL) + while (elem) { printf("%-20s%8d\n", elem->filename, elem->file_length); elem = elem->next_element; } } - -int file_size(char *filename) +int +file_size(char *filename) { - FILE * f = fopen(filename, "rb"); - int size; - DEBUG_MAR(printf("D: mar::file_size filename %s\n", filename);); - if (f == NULL) - { - perror(filename); - return -1; - } - if (fseek(f, 0L, SEEK_END) == -1) - { - perror(filename); - return -1; - } - size = ftell(f); - if (size == -1) + struct stat buf; + if (stat(filename, &buf) != 0) { perror(filename); return -1; } - if (fclose(f) != 0) - { - perror(filename); - return -1; - } - return size; + return buf.st_size; } @@ -74,7 +57,8 @@ int file_size(char *filename) */ /* ``files'' is a NULL-terminated array of char* */ -int create_marfile(char *dest_file, char **files) +int +create_marfile(char *dest_file, char **files) { int filenum = 0; int current_offset_filetable; @@ -84,7 +68,7 @@ int create_marfile(char *dest_file, char **files) /* calculate offset of ``raw_files_data'' */ current_offset_rawdata = sizeof(int) + sizeof(char); /* crc32, ``char 0'' */ - while (files[filenum] != NULL) + while (files[filenum]) { current_offset_rawdata += 2*sizeof(int) /* file_length, data_offset */ + strlen(files[filenum]) + 1; filenum++; @@ -94,7 +78,7 @@ int create_marfile(char *dest_file, char **files) /* calculate length of final uncompressed marfile, for malloc */ total_length = current_offset_rawdata; /* first part of the marfile: the crc plus filetable */ filenum = 0; - while (files[filenum] != NULL) + while (files[filenum]) { int fsiz = file_size(files[filenum]); if (fsiz == -1) @@ -102,16 +86,16 @@ int create_marfile(char *dest_file, char **files) total_length += fsiz; filenum++; } - temp_marfile_buffer = (void *) malloc(total_length); /* create the whole file in-memory */ + temp_marfile_buffer = (char *) malloc(total_length); /* create the whole file in-memory */ DEBUG_MAR(printf("D: mar::create_marfile total-length %d\n", total_length);); current_offset_filetable = sizeof(int); /* first file is after the crc */ filenum = 0; - while (files[filenum] != NULL) + while (files[filenum]) { FILE * f = fopen(files[filenum], "rb"); int fsize; - if (f == NULL) + if (!f) { perror(files[filenum]); return -1; @@ -144,6 +128,9 @@ int create_marfile(char *dest_file, char **files) filenum++; } + /* write down ``char 0'' to terminate file table */ + memset(&temp_marfile_buffer[current_offset_filetable], 0, sizeof(char)); + /* calculate crc with all the data we now got */ { int current_crc = 0; @@ -157,7 +144,7 @@ int create_marfile(char *dest_file, char **files) /* ok, buffer is ready, let's write it on-disk */ { gzFile f = gzopen(dest_file, "wb"); - if (f == NULL) + if (!f) { perror(dest_file); return -1; @@ -170,47 +157,29 @@ int create_marfile(char *dest_file, char **files) gz_errnum = gzclose(f); } -// /* uncompressed write -// -// int fd = creat(dest_file, 00660); -// if (fd == -1) -// { -// perror("E: mar::create_marfile::creat"); -// return -1; -// } -// if (write(fd, temp_marfile_buffer, total_length) != total_length) -// { -// perror("E: mar::create_marfile::write"); -// return -1; -// } -// if (close(fd) != 0) -// { -// perror("E: mar::create_marfile::close"); -// return -1; -// } -// */ - return 0; } -void print_usage(char *progname) +void +print_usage(char *progname) { printf("Usage: %s [-lxc] [files..]\n", progname); exit(0); } -int main(int argc, char **argv) +int +main(int argc, char **argv) { if (argc <= 2) print_usage(argv[0]); - + if (argc >= 3) { if (strcmp(argv[1], "-l") == 0) { mar_stream *s = open_marfile(argv[2]); - if (s == NULL) + if (!s) exit(-1); list_files(s); if (s->crc32 == calc_integrity(s)) @@ -223,7 +192,7 @@ int main(int argc, char **argv) { mar_stream *s = open_marfile(argv[2]); int i = 3; - if (s == NULL) + if (!s) exit(-1); while (i < argc) { @@ -252,53 +221,3 @@ int main(int argc, char **argv) return 0; } - /* mar_stream *s; - int res; - char *files[4]; - - char * file1 = "Makefile"; - char * file2 = "mar.c"; - char * file3 = "mar.h"; - - files[0] = file1; - files[1] = file2; - files[2] = file3; - files[3] = NULL; - - create_marfile("test.mar", files); - - - s = open_marfile("test.mar"); - - if (s == NULL) - exit(-1); - - res = extract_file(s, "Makefile", "t/"); - printf("return-code %d\n", res); - - exit(0); -} - char plop[20]; - int i,j; - bzero(plop, 20); - j = 4096; - memcpy(&plop[2], &j, sizeof(j)); - for (i=0; i<20; i++) printf("offset %d, contains char %d\n", i, plop[i]); - exit(-1); - - s.first_element = &e1; -// s.crc32 = calc_integrity(&s); - - e1.filename = "bonjour"; - e1.file_length = 4; - e1.next_element = &e2; - e2.filename = "deuz"; - e2.file_length = 54; - e2.next_element = NULL; - - list_files(&s); -// if (verify_integrity(&s)) { printf("CRC OK\n"); } else { printf("CRC FAILS!\n"); } - return 0; -} - -*/ diff --git a/mdk-stage1/mar/mar.h b/mdk-stage1/mar/mar.h index 5d8d98f61..a5ea62aa0 100644 --- a/mdk-stage1/mar/mar.h +++ b/mdk-stage1/mar/mar.h @@ -27,6 +27,7 @@ #ifndef MAR_H #define MAR_H +#if 1 #include #include #include @@ -35,6 +36,9 @@ #include #include #include +#endif +//#include + #include /* -- cgit v1.2.1