From bf1d9554eacc69d63395de06a50d6b002e0d6b45 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Tue, 6 Dec 2022 13:51:59 +0100 Subject: [PATCH] remove FILE dependency --- compress.c | 92 +++++++++++++++++++++++++++--------------------------- debug.h | 1 - 2 files changed, 46 insertions(+), 47 deletions(-) diff --git a/compress.c b/compress.c index 26e922e..bda2587 100644 --- a/compress.c +++ b/compress.c @@ -2,8 +2,9 @@ /* cflags: */ /* linker: atoi.o code.o debug.o fprintf.o */ +#include #include -#include +#include #include "atoi.h" #include "code.h" #include "debug.h" @@ -47,7 +48,7 @@ int *create_table (char *filename) byte_t buffer[BUFFER_SIZE] = {0}; int nbread; int *table = NULL; - FILE *fid = NULL; + int fid = 0; VERBOSE (DEBUG, PRINTF ("start creating occurence table\n")); @@ -60,8 +61,8 @@ int *create_table (char *filename) VERBOSE (INFO, myfprintf (stdout, "memory allocated\n")); /* open file */ - fid = fopen (filename, "rb"); - if (fid == NULL) { + fid = open (filename, O_RDONLY|O_RAW); + if (fid == -1) { VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s'\n", filename)); free (table); return NULL; @@ -69,8 +70,7 @@ int *create_table (char *filename) VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", filename)); /* read file */ - while (!feof (fid)) { - nbread = fread (buffer, 1, BUFFER_SIZE, fid); + while ((nbread = read (fid, buffer, BUFFER_SIZE)) > 0) { VERBOSE (DEBUG, PRINTF ("nbread: %d\n", nbread)); while (nbread--) { table[(int)buffer[nbread]]++; @@ -78,7 +78,7 @@ int *create_table (char *filename) } /* close file */ - fclose (fid); + close (fid); VERBOSE (DEBUG, PRINTF ("end creating occurence table\n")); @@ -404,7 +404,7 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header) byte_t bufin[BUFFER_SIZE] = {0}; byte_t bufout[BUFFER_SIZE] = {0}; char bits[(NB_BYTES - 1) + 8 + 1] = {0}; - FILE *fin, *fout; + int fin, fout; int length = 0; int i, j, nbread; byte_t *pt; @@ -412,17 +412,18 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header) VERBOSE (DEBUG, PRINTF ("start writting compressed file\n")); /* open input file */ - fin = fopen (input, "rb"); - if (fin == NULL) { + fin = open (input, O_RDONLY|O_RAW); + if (fin == -1) { VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s' for reading\n", input)); return 1; } VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", input)); /* open output file */ - fout = fopen (output, "wb"); - if (fin == NULL) { + fout = open (output, O_WRONLY|O_CREAT|O_RAW, 0700); + if (fout == -1) { VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s' for writing\n", output)); + close (fin); return 1; } VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", output)); @@ -430,12 +431,11 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header) /* write header */ length = (header[3] << 8) + header[4]; VERBOSE (DEBUG, PRINTF ("lengh: %d\n", length)); - fwrite (header, 1, length + 6, fout); + write (fout, header, length + 6); /* write file */ pt = bufout; - while (!feof (fin)) { - nbread = fread (bufin, 1, BUFFER_SIZE, fin); + while ((nbread = read (fin, bufin, BUFFER_SIZE)) > 0) { VERBOSE (DEBUG, PRINTF ("nbread: %d\n", nbread)); for (i = 0; i < nbread; i++) { codcat (bits, sizeof (code_t), (char *)(codes + bufin[i])); @@ -448,7 +448,7 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header) } codcpy (bits, sizeof (code_t), bits + 8); if (pt - bufout == BUFFER_SIZE - 1) { - fwrite (bufout, 1, BUFFER_SIZE, fout); + write (fout, bufout, BUFFER_SIZE); pt = bufout; } else { pt++; @@ -471,12 +471,12 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header) } if (pt != bufout) { VERBOSE (DEBUG, PRINTF ("last partial buffer written: %u\n", pt - bufout)); - fwrite (bufout, 1, pt - bufout, fout); + write (fout, bufout, pt - bufout); } /* closing */ - fclose (fin); - fclose (fout); + close (fin); + close (fout); VERBOSE (DEBUG, PRINTF ("end writting compressed file\n")); @@ -491,22 +491,22 @@ code_t *read_header (char *filename) { byte_t *codes = NULL; byte_t cur; int lengths[NB_BYTES] = {0}; - FILE *fid = NULL; + int fid; int mode = 0; - size_t i, j, l, nb, size; + int i, j, l, nb, size; VERBOSE (DEBUG, PRINTF ("start reading header\n")); /* open file */ - fid = fopen (filename, "rb"); - if (fid == NULL) { + fid = open (filename, O_RDONLY|O_RAW); + if (fid == -1) { VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s'\n", filename)); return NULL; } VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", filename)); /* read magic number */ - nb = fread (buffer, 1, 6, fid); + nb = read (fid, buffer, 6); VERBOSE (DEBUG, PRINTF ("nb, buffer: %d 0x%02x 0x%02x\n", nb, buffer[0], buffer[1])); if ((nb == 6) && (buffer[0] == 'M') && (buffer[1] == 'Z')) { mode = (buffer[2] == '1') ? 1 : (buffer[2] == '2') ? 2 : 0; @@ -515,14 +515,14 @@ code_t *read_header (char *filename) { if (size > NB_BYTES * (NB_BYTES - 1) / 2 / 8 + NB_BYTES) { mode = 0; } else { - nb = fread (buffer, 1, size, fid); - VERBOSE (DEBUG, PRINTF ("nb read: %d\n", nb)); + nb = read (fid, buffer, size); + VERBOSE (DEBUG, PRINTF ("nb read: %d/%d\n", nb, size)); if (nb != size) { mode = 0; } } } - fclose (fid); + close (fid); if (mode == 0) { VERBOSE (ERROR, myfprintf (stdout, "incorrect file\n")); return NULL; @@ -553,7 +553,7 @@ code_t *read_header (char *filename) { } if (((mode == 1) && (size - 256 != (l + 7) / 8)) || ((mode == 2) && (size - 2 * nb - 1 != (l + 7) / 8))) { - VERBOSE (ERROR, myfprintf (stdout, "incorrect code table length\n")); + VERBOSE (ERROR, myfprintf (stdout, "incorrect code table length: %d %d %d\n", size, nb, l)); return NULL; } @@ -595,7 +595,7 @@ int write_decompress (char *output, char *input, code_t *codes) byte_t bufout[BUFFER_SIZE] = {0}; byte_t bufhea[MAX(NB_BYTES * (NB_BYTES - 1) / 2 / 8 + NB_BYTES + 6, BUFFER_SIZE)] = {0}; char bits[(NB_BYTES - 1) + 1] = {0}; - FILE *fin, *fout; + int fin, fout; int i, j, k, nb, size, rem; int is_found; int l = 0; @@ -604,43 +604,43 @@ int write_decompress (char *output, char *input, code_t *codes) VERBOSE (DEBUG, PRINTF ("start writing decompressed file\n")); /* open file for reading */ - fin = fopen (input, "rb"); - if (fin == NULL) { + fin = open (input, O_RDONLY|O_RAW); + if (fin == -1) { VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s' for reading\n", input)); return 1; } VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", input)); /* read magic number */ - nb = fread (bufhea, 1, 6, fin); + nb = read (fin, bufhea, 6); if (nb != 6) { VERBOSE (ERROR, myfprintf (stdout, "can't read file\n")); - fclose (fin); + close (fin); return 1; } size = (bufhea[3] << 8) + bufhea[4]; VERBOSE (DEBUG, myfprintf (stdout, "table size: %d\n", size)); rem = bufhea[5]; VERBOSE (DEBUG, myfprintf (stdout, "remainder: %d\n", rem)); - nb = fread (bufhea, 1, size, fin); + nb = read (fin, bufhea, size); if (nb != size) { VERBOSE (ERROR, myfprintf (stdout, "can't read file\n")); - fclose (fin); + close (fin); return 1; } /* open file for writing */ - fout = fopen (output, "wb"); - if (fout == NULL) { + fout = open (output, O_WRONLY|O_CREAT|O_RAW, 0700); + if (fout == -1) { VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s' for writing\n", output)); - return 2; + close (fin); + return 2; } VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", output)); /* write file */ pt = bufout; - while (!feof (fin)) { - nb = fread (bufin, 1, BUFFER_SIZE, fin); + while ((nb = read (fin, bufin, BUFFER_SIZE)) > 0) { VERBOSE (DEBUG, PRINTF ("nbread: %d\n", nb)); for (i = 0; i < nb; i++) { for (j = 0; j < 8; j++) { @@ -659,14 +659,14 @@ int write_decompress (char *output, char *input, code_t *codes) bits[0] = 0; if (pt - bufout == BUFFER_SIZE - 1) { VERBOSE (DEBUG, PRINTF ("nb buffer out: %u\n", (pt - bufout))); - fwrite (bufout, 1, BUFFER_SIZE, fout); + write (fout, bufout, BUFFER_SIZE); pt = bufout; } else { pt++; } } } - if ((i == nb - 1) && (l % 256 == rem) && (feof (fin))) { + if ((i == nb - 1) && (l % 256 == rem) && (nb != BUFFER_SIZE)) { VERBOSE (DEBUG, PRINTF ("break\n")); break; } @@ -675,12 +675,12 @@ int write_decompress (char *output, char *input, code_t *codes) } if (pt != bufout) { VERBOSE (DEBUG, PRINTF ("nb buffer out: %u\n", (pt - bufout))); - fwrite (bufout, 1, pt - bufout, fout); + write (fout, bufout, pt - bufout); } /* close files */ - fclose (fin); - fclose (fout); + close (fin); + close (fout); VERBOSE (DEBUG, PRINTF ("end writing decompressed file\n")); @@ -796,4 +796,4 @@ int main (int argc, char *argv[]) // test: cmp compress.c tmp.c // test: rm compress.mz tmp.c -/* vim: set ts=4 sw=4 et */ +/* vim: set ts=4 sw=4 et: */ diff --git a/debug.h b/debug.h index a0d26f7..b05fd06 100644 --- a/debug.h +++ b/debug.h @@ -10,7 +10,6 @@ #define DECOMPRESS 2 #define NB_CHARS 256 -#define BUFFER_SIZE 4096 #define DEBUG 4 #define INFO 3 -- 2.30.2