remove calloc calls
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Fri, 9 Dec 2022 12:16:38 +0000 (13:16 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Fri, 9 Dec 2022 12:16:38 +0000 (13:16 +0100)
code.c
code.h
compress.c
debug.h
fprintf.c
fprintf.h

diff --git a/code.c b/code.c
index 67220e05b02dd97a390f25a7d49409da3872d840..934bfe9f04dd15a431212307c89a3f2ce8900404 100644 (file)
--- a/code.c
+++ b/code.c
@@ -1,11 +1,12 @@
-#include <stddef.h>
-#include <unistd.h>
-
 #include "debug.h"
 #include "fprintf.h"
 
 #include "code.h"
 
+/* constants */
+
+#define NB_LEAFS ((NB_BYTES + 1) * NB_BYTES / 2)
+
 /* concatenate code */
 
 int codcat (char *dst, int n, char *src)
@@ -14,7 +15,7 @@ int codcat (char *dst, int n, char *src)
         n--;
         dst++;
     }
-    
+
     return (n > 0) ? codcpy (dst, n, src) : -1;
 }
 
@@ -46,7 +47,7 @@ int codcpy (char *dst, int n, char *src)
             return i;
         }
     }
-    VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "Buffer too short\n"));
+    VERBOSE (ERROR, PRINTERR ("Buffer too short\n"));
 
     return -1;
 }
@@ -64,4 +65,22 @@ int codlen (char *code)
     return i;
 }
 
-/* vim: set ts=4 sw=4 et */
+/* get leaf(s) */
+
+leaf_t *getleaf (int n)
+{
+    static leaf_t leafs[NB_LEAFS] = {0};
+    static int pos = 0;
+    leaf_t *pt = (void *)0;
+
+    if (pos + n < NB_LEAFS) {
+        pt = leafs + pos;
+        pos += n;
+    } else {
+        VERBOSE (ERROR, PRINTERR ("No more leaf avaliable\n"));
+    }
+
+    return pt;
+}
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/code.h b/code.h
index 684745bde13928e300572a57cc1154fc14ba2f08..06f78ebf234089aa6cc5f90f796c2262248fd48c 100644 (file)
--- a/code.h
+++ b/code.h
@@ -18,6 +18,18 @@ int codcmp (char *cod1, char *cod2);
 int codcpy (char *dst, int n, char *src);
 int codlen (char *code);
 
+/* leaf structure */
+
+typedef struct _leaf_t
+{
+    struct _leaf_t *left;
+    struct _leaf_t *right;
+    int occ;
+    byte_t c;
+} leaf_t;
+
+leaf_t *getleaf (int n);
+
 #endif /* __CODE_H__ */
 
 /* vim: set ts=4 sw=4 et */
index 539429c1927908162a3e084e6958296def57dd7a..17385d49a188d30a04c6a2f445216ed6765000b7 100644 (file)
@@ -3,8 +3,8 @@
 /* linker: atoi.o code.o debug.o fprintf.o */
 
 #include <fcntl.h>
-#include <malloc.h>
 #include <unistd.h>
+#include <stddef.h>
 #include "atoi.h"
 #include "code.h"
 #include "debug.h"
@@ -14,6 +14,9 @@
 
 #define BUFFER_SIZE 4096
 
+#define COMPRESS 1
+#define DECOMPRESS 2
+
 /* macros */
 
 /* gobal variables */
@@ -22,16 +25,17 @@ char *progname = NULL;
 
 /* help function */
 
-void usage (int ret)
+int usage (int ret)
 {
-    int fd = ret ? STDERR_FILENO : STDOUT_FILENO;
+    //int fd = ret ? STDERR_FILENO : STDOUT_FILENO;
+    int fd = ret ? _fderr : _fdout;
     fdprintf (fd, "usage: %s\n", progname);
     fdprintf (fd, " -h : help message\n");
     fdprintf (fd, " -i <file>: input file\n");
     fdprintf (fd, " -o <file>: output file\n");
     fdprintf (fd, " -v : verbose level (%d)\n", verbose);
 
-    exit (ret);
+    return ret;
 }
 
 void blkcpy (void *dst, const void *src, int len)
@@ -42,36 +46,26 @@ void blkcpy (void *dst, const void *src, int len)
 }
 
 /* create occurence table */
-
 int *create_table (char *filename)
 {
     byte_t buffer[BUFFER_SIZE] = {0};
     int nbread;
-    int *table = NULL;
+    static int table[NB_BYTES] = {0};
     int fid = 0;
 
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start creating occurence table\n"));
-
-    /* memory allocation */
-    table = (int *) calloc (NB_BYTES, sizeof (int));
-    if (table == NULL) {
-        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't allocate memory\n"));
-        return NULL;
-    }
-    VERBOSE (INFO, fdprintf (STDOUT_FILENO, "memory allocated\n"));
+    VERBOSE (DEBUG, PRINTOUT ("start creating occurence table\n"));
 
     /* open file */
     fid = open (filename, O_RDONLY|O_RAW);
     if (fid == -1) {
-        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't open file '%s'\n", filename));
-        free (table);
+        VERBOSE (ERROR, PRINTOUT ("can't open file '%s'\n", filename));
         return NULL;
     }
-    VERBOSE (INFO, fdprintf (STDOUT_FILENO, "file '%s' opened\n", filename));
+    VERBOSE (INFO, PRINTOUT ("file '%s' opened\n", filename));
 
     /* read file */
     while ((nbread = read (fid, buffer, BUFFER_SIZE)) > 0) {
-        VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nbread: %d\n", nbread));
+        VERBOSE (DEBUG, PRINTOUT ("nbread: %d\n", nbread));
         while (nbread--) {
             table[(int)buffer[nbread]]++;
         }
@@ -80,7 +74,7 @@ int *create_table (char *filename)
     /* close file */
     close (fid);
 
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end creating occurence table\n"));
+    VERBOSE (DEBUG, PRINTOUT ("end creating occurence table\n"));
 
     return table;
 }
@@ -91,33 +85,23 @@ void print_occ_table (int *table)
 {
     int i;
 
-    fdprintf (STDOUT_FILENO, "Occurence table\n");
+    PRINTOUT ("Occurence table\n");
     for (i = 0; i < NB_BYTES; i++) {
         if (table[i]) {
-            fdprintf (STDOUT_FILENO, "0x%02x '%c': %d\n", i, ((i < 32) || (i > 127)) ? '.' : i, table[i]);
+            PRINTOUT ("0x%02x '%c': %d\n", i, ((i < 32) || (i > 127)) ? '.' : i, table[i]);
         }
     }
 }
 
-/* leaf structure */
-
-typedef struct _leaf_t
-{
-    struct _leaf_t *left;
-    struct _leaf_t *right;
-    int occ;
-    byte_t c;
-} leaf_t;
-
 /* initialize forest */
 
 leaf_t **init_forest (int *table)
 {
-    leaf_t **leafs = NULL;
+    static leaf_t *leafs[NB_BYTES] = {0};
     int nb_leafs = 0;
     int i, l;
 
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start initiliazing forest\n"));
+    VERBOSE (DEBUG, PRINTOUT ("start initiliazing forest\n"));
 
     /* count number of leafs */
     for (i = 0; i < NB_BYTES; i++) {
@@ -126,19 +110,12 @@ leaf_t **init_forest (int *table)
         }
     }
 
-    /* allocate memory */
-    leafs = (leaf_t **) calloc (nb_leafs + 1, sizeof (leaf_t *));
-    if (leafs == NULL) {
-        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't allocate memory\n"));
-        return NULL;
-    }
-
     /* initialize leafs */
     for (i = 0, l = 0; i < NB_BYTES; i++) {
         if (table[i] > 0) {
-            leafs[l] = (leaf_t *) calloc (1, sizeof (leaf_t));
+            leafs[l] = getleaf (1);
             if (leafs[l] == NULL) {
-                VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't allocate memory\n"));
+                VERBOSE (ERROR, PRINTOUT ("can't allocate memory\n"));
                 return NULL;
             }
             leafs[l]->occ = table[i];
@@ -147,7 +124,7 @@ leaf_t **init_forest (int *table)
         }
     }
 
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end initiliazing forest\n"));
+    VERBOSE (DEBUG, PRINTOUT ("end initiliazing forest\n"));
 
     return leafs;
 }
@@ -162,7 +139,7 @@ leaf_t *create_tree (leaf_t **leafs)
     int ante;
     int i, j;
 
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start creating tree\n"));
+    VERBOSE (DEBUG, PRINTOUT ("start creating tree\n"));
 
     /* count number of leafs */
     while (leafs[nb_leafs] != NULL) {
@@ -196,12 +173,12 @@ leaf_t *create_tree (leaf_t **leafs)
 
         /* create branch */
         if ((last == -1) || (ante == -1)) {
-            VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "error during tree building\n"));
+            VERBOSE (ERROR, PRINTOUT ("error during tree building\n"));
             return NULL;
         }
-        branch = (leaf_t *) calloc (1, sizeof (leaf_t));
+        branch = getleaf (1);
         if (branch == NULL) {
-            VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't allocate memory\n"));
+            VERBOSE (ERROR, PRINTOUT ("can't allocate memory\n"));
             return NULL;
         }
         branch->left = leafs[last];
@@ -211,29 +188,18 @@ leaf_t *create_tree (leaf_t **leafs)
         leafs[ante] = NULL;
     }
 
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end creating tree\n"));
+    VERBOSE (DEBUG, PRINTOUT ("end creating tree\n"));
 
     return (last != -1) ? leafs[last] : NULL;
 }
 
-/* free tree */
-
-void free_tree (leaf_t *root) {
-    if (root) {
-        if (root->left) {
-            free_tree (root->left);
-        }
-        if (root->right) {
-            free_tree (root->right);
-        }
-        free (root);
-    }
-}
-
 /* explore tree */
 
 void explore_tree (code_t *table, leaf_t *root, char *code, int index)
 {
+
+    VERBOSE (DEBUG, PRINTOUT ("start exploring code tree\n"));
+
     if ((root->left == NULL) && (root->right == NULL)) {
         codcpy ((char *)(table + (int)(root->c)), sizeof (code_t), code);
     }
@@ -243,27 +209,21 @@ void explore_tree (code_t *table, leaf_t *root, char *code, int index)
         codcpy (code + index, sizeof (code_t), "0");
         explore_tree (table, root->right, code, index + 1);
     }
+
+    VERBOSE (DEBUG, PRINTOUT ("end exploring code tree\n"));
 }
 
 /* create code table */
-
 code_t *create_code (leaf_t *root)
 {
-    code_t *table = NULL;
+    static code_t table[NB_BYTES] = {0};
     code_t code = {0};
 
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start creating code table\n"));
-
-    /* allocate table */
-    table = (code_t *) calloc (NB_BYTES, sizeof (code_t));
-    if (table == NULL) {
-        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't allocate memory\n"));
-        return NULL;
-    }
+    VERBOSE (DEBUG, PRINTOUT ("start creating code table\n"));
 
     explore_tree (table, root, (char *)&code, 0);
 
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end creating code table\n"));
+    VERBOSE (DEBUG, PRINTOUT ("end creating code table\n"));
 
     return table;
 }
@@ -275,13 +235,13 @@ void print_code_table (code_t *codes)
     char *code;
     int i;
 
-    fdprintf (STDOUT_FILENO, "Code table\n");
+    PRINTOUT ("Code table\n");
     for (i = 0; i < NB_BYTES; i++) {
         code = (char *)(codes + i);
         if (codlen (code) == 0) {
             continue;
         }
-        fdprintf (STDOUT_FILENO, "0x%02x '%c': %s\n", i, ((i < 32) || (i > 127)) ? '.' : i, code);
+        PRINTOUT ("0x%02x '%c': %s\n", i, ((i < 32) || (i > 127)) ? '.' : i, code);
     }
 }
 
@@ -289,7 +249,7 @@ void print_code_table (code_t *codes)
 
 byte_t *encode_header_table (code_t *codes, int *occ)
 {
-    byte_t buffer[NB_BYTES * (NB_BYTES - 1) / 2 / 8 + NB_BYTES + 6] = {0};
+    static byte_t buffer[NB_BYTES * (NB_BYTES - 1) / 2 / 8 + NB_BYTES + 6] = {0};
     char bits[(NB_BYTES - 1) + 8 + 1] = {0};
     char *code;
     byte_t *header = buffer;
@@ -297,7 +257,7 @@ byte_t *encode_header_table (code_t *codes, int *occ)
     int nb = 0;
     int size = 0;
 
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start encoding header and code table\n"));
+    VERBOSE (DEBUG, PRINTOUT ("start encoding header and code table\n"));
 
     /* mode 1 or 2 */
     for (i = 0; i < NB_BYTES; i++) {
@@ -308,10 +268,10 @@ byte_t *encode_header_table (code_t *codes, int *occ)
         }
     }
     mode = (NB_BYTES < 2 * nb + 1) ? 1 : 2;
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nb chars: %d\n", nb));
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "mode: %d\n", mode));
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "size: %d\n", size));
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "rem: %d\n", size % 256));
+    VERBOSE (DEBUG, PRINTOUT ("nb chars: %d\n", nb));
+    VERBOSE (DEBUG, PRINTOUT ("mode: %d\n", mode));
+    VERBOSE (DEBUG, PRINTOUT ("size: %d\n", size));
+    VERBOSE (DEBUG, PRINTOUT ("rem: %d\n", size % 256));
 
     /* header */
     codcpy ((char *)header, sizeof (buffer), (mode == 1) ? "MZ1   " : "MZ2   ");
@@ -369,16 +329,13 @@ byte_t *encode_header_table (code_t *codes, int *occ)
 
     /* length */
     length = (int)(header - buffer - 6);
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "lengh: %d %02x %02x\n", length, length >> 8, length & 0xff));
+    VERBOSE (DEBUG, PRINTOUT ("lengh: %d %02x %02x\n", length, length >> 8, length & 0xff));
     buffer[3] = (byte_t)(length >> 8);
     buffer[4] = (byte_t)(length & 0xff);
     buffer[5] = (byte_t)(size % 256);
+    header = buffer;
 
-    /* allocation */
-    header = (byte_t *) calloc (length + 6, 1);
-    blkcpy (header, buffer, length + 6);
-
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end encoding header and code table\n"));
+    VERBOSE (DEBUG, PRINTOUT ("end encoding header and code table\n"));
 
     return header;
 }
@@ -390,11 +347,11 @@ void print_header (byte_t *header)
     int length, i;
 
     length = (header[3] << 8) + header[4];
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "lengh: %d\n", length));
+    VERBOSE (DEBUG, PRINTOUT ("lengh: %d\n", length));
     for (i = 0; i < length + 6; i++) {
-        fdprintf (STDOUT_FILENO, "%02x", header[i]);
+        PRINTOUT ("%02x", header[i]);
     }
-    fdprintf (STDOUT_FILENO, "\n");
+    PRINTOUT ("\n");
 }
 
 /* write crompressed file */
@@ -409,34 +366,34 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header)
     int i, j, nbread;
     byte_t *pt;
 
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start writting compressed file\n"));
+    VERBOSE (DEBUG, PRINTOUT ("start writting compressed file\n"));
 
     /* open input file */
     fin = open (input, O_RDONLY|O_RAW);
     if (fin == -1) {
-        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't open file '%s' for reading\n", input));
+        VERBOSE (ERROR, PRINTOUT ("can't open file '%s' for reading\n", input));
         return 1;
     }
-    VERBOSE (INFO, fdprintf (STDOUT_FILENO, "file '%s' opened\n", input));
+    VERBOSE (INFO, PRINTOUT ("file '%s' opened\n", input));
 
     /* open output file */
     fout = open (output, O_WRONLY|O_CREAT|O_RAW, 0700);
     if (fout == -1) {
-        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't open file '%s' for writing\n", output));
+        VERBOSE (ERROR, PRINTOUT ("can't open file '%s' for writing\n", output));
            close (fin);
         return 1;
     }
-    VERBOSE (INFO, fdprintf (STDOUT_FILENO, "file '%s' opened\n", output));
+    VERBOSE (INFO, PRINTOUT ("file '%s' opened\n", output));
 
     /* write header */
     length = (header[3] << 8) + header[4];
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "lengh: %d\n", length));
+    VERBOSE (DEBUG, PRINTOUT ("lengh: %d\n", length));
     write (fout, header, length + 6);
 
     /* write file */
     pt = bufout;
     while ((nbread = read (fin, bufin, BUFFER_SIZE)) > 0) {
-        VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nbread: %d\n", nbread));
+        VERBOSE (DEBUG, PRINTOUT ("nbread: %d\n", nbread));
         for (i = 0; i < nbread; i++) {
             codcat (bits, sizeof (code_t), (char *)(codes + bufin[i]));
             while (codlen (bits) > (8 - 1)) {
@@ -456,7 +413,7 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header)
             }
         }
     }
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "lastest bits : %d\n", codlen (bits)));
+    VERBOSE (DEBUG, PRINTOUT ("lastest bits : %d\n", codlen (bits)));
     if (codlen (bits) > 0) {
         for (j = 0; j < (int)codlen (bits); j++) {
             *pt <<= 1;
@@ -470,7 +427,7 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header)
         pt++;
     }
     if (pt != bufout) {
-        VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "last partial buffer written: %u\n", pt - bufout));
+        VERBOSE (DEBUG, PRINTOUT ("last partial buffer written: %u\n", pt - bufout));
         write (fout, bufout, pt - bufout);
     }
     
@@ -478,7 +435,7 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header)
     close (fin);
     close (fout);
 
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end writting compressed file\n"));
+    VERBOSE (DEBUG, PRINTOUT ("end writting compressed file\n"));
 
     return 0;
 }
@@ -486,8 +443,8 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header)
 /* read header */
 
 code_t *read_header (char *filename) {
+    static code_t table[NB_BYTES] = {0};
     byte_t buffer[NB_BYTES * (NB_BYTES - 1) / 2 / 8 + NB_BYTES + 6] = {0};
-    code_t *table = NULL;
     byte_t *codes = NULL;
     byte_t cur;
     int lengths[NB_BYTES] = {0};
@@ -495,28 +452,28 @@ code_t *read_header (char *filename) {
     int mode = 0;
     int i, j, l, nb, size;
 
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start reading header\n"));
+    VERBOSE (DEBUG, PRINTOUT ("start reading header\n"));
 
     /* open file */
     fid = open (filename, O_RDONLY|O_RAW);
     if (fid == -1) {
-        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't open file '%s'\n", filename));
+        VERBOSE (ERROR, PRINTOUT ("can't open file '%s'\n", filename));
         return NULL;
     }
-    VERBOSE (INFO, fdprintf (STDOUT_FILENO, "file '%s' opened\n", filename));
+    VERBOSE (INFO, PRINTOUT ("file '%s' opened\n", filename));
 
     /* read magic number */
     nb = read (fid, buffer, 6);
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nb, buffer: %d 0x%02x 0x%02x\n", nb, buffer[0], buffer[1]));
+    VERBOSE (DEBUG, PRINTOUT ("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;
         size = (buffer[3] << 8) + buffer[4];
-        VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "mode, size: %d %d\n", mode, size));
+        VERBOSE (DEBUG, PRINTOUT ("mode, size: %d %d\n", mode, size));
         if (size > NB_BYTES * (NB_BYTES - 1) / 2 / 8 + NB_BYTES) {
             mode = 0;
         } else {
             nb = read (fid, buffer, size);
-            VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nb read: %d/%d\n", nb, size));
+            VERBOSE (DEBUG, PRINTOUT ("nb read: %d/%d\n", nb, size));
             if (nb != size) {
                 mode = 0;
             }
@@ -524,7 +481,7 @@ code_t *read_header (char *filename) {
     }
     close (fid);
     if (mode == 0) {
-        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "incorrect file\n"));
+        VERBOSE (ERROR, PRINTOUT ("incorrect file\n"));
         return NULL;
     }
 
@@ -538,14 +495,14 @@ code_t *read_header (char *filename) {
         break;
     case 2:
         nb = *(codes++) + 1;
-        VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nb codes: %d\n", nb));
+        VERBOSE (DEBUG, PRINTOUT ("nb codes: %d\n", nb));
         for (i = 0; i < nb; i++) {
             j = *(codes++);
             lengths[j] = *(codes++);
         }
         break;
     }
-    VERBOSE (DEBUG, for (i = 0; i < NB_BYTES; i++) if (lengths[i]) fdprintf (STDOUT_FILENO, "%d: %d\n", i, lengths[i]));
+    VERBOSE (DEBUG, for (i = 0; i < NB_BYTES; i++) if (lengths[i]) PRINTOUT ("%d: %d\n", i, lengths[i]));
 
     /* check lengths */
     for (i = 0, l = 0; i < NB_BYTES; i++) {
@@ -553,14 +510,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, fdprintf (STDOUT_FILENO, "incorrect code table length: %d %d %d\n", size, nb, l));
-        return NULL;
-    }
-
-    /* allocate table */
-    table = (code_t *) calloc (NB_BYTES, sizeof (code_t));
-    if (table == NULL) {
-        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't allocate memory\n"));
+        VERBOSE (ERROR, PRINTOUT ("incorrect code table length: %d %d %d\n", size, nb, l));
         return NULL;
     }
 
@@ -582,7 +532,7 @@ code_t *read_header (char *filename) {
         }
     }
 
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end reading header\n"));
+    VERBOSE (DEBUG, PRINTOUT ("end reading header\n"));
 
     return table;
 }
@@ -601,30 +551,30 @@ int write_decompress (char *output, char *input, code_t *codes)
     int l = 0;
     byte_t *pt;
 
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start writing decompressed file\n"));
+    VERBOSE (DEBUG, PRINTOUT ("start writing decompressed file\n"));
 
     /* open file for reading */
     fin = open (input, O_RDONLY|O_RAW);
     if (fin == -1) {
-        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't open file '%s' for reading\n", input));
+        VERBOSE (ERROR, PRINTOUT ("can't open file '%s' for reading\n", input));
         return 1;
     }
-    VERBOSE (INFO, fdprintf (STDOUT_FILENO, "file '%s' opened\n", input));
+    VERBOSE (INFO, PRINTOUT ("file '%s' opened\n", input));
 
     /* read magic number */
     nb = read (fin, bufhea, 6);
     if (nb != 6) {
-        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't read file\n"));
+        VERBOSE (ERROR, PRINTOUT ("can't read file\n"));
         close (fin);
         return 1;
     }
     size = (bufhea[3] << 8) + bufhea[4];
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "table size: %d\n", size));
+    VERBOSE (DEBUG, PRINTOUT ("table size: %d\n", size));
     rem = bufhea[5];
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "remainder: %d\n", rem));
+    VERBOSE (DEBUG, PRINTOUT ("remainder: %d\n", rem));
     nb = read (fin, bufhea, size);
     if (nb != size) {
-        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't read file\n"));
+        VERBOSE (ERROR, PRINTOUT ("can't read file\n"));
         close (fin);
         return 1;
     }
@@ -632,33 +582,33 @@ int write_decompress (char *output, char *input, code_t *codes)
     /* open file for writing */
     fout = open (output, O_WRONLY|O_CREAT|O_RAW, 0700);
     if (fout == -1) {
-        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't open file '%s' for writing\n", output));
+        VERBOSE (ERROR, PRINTOUT ("can't open file '%s' for writing\n", output));
         close (fin);
        return 2;
     }
-    VERBOSE (INFO, fdprintf (STDOUT_FILENO, "file '%s' opened\n", output));
+    VERBOSE (INFO, PRINTOUT ("file '%s' opened\n", output));
 
     /* write file */
     pt = bufout;
     while ((nb = read (fin, bufin, BUFFER_SIZE)) > 0) {
-        VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nbread: %d\n", nb));
+        VERBOSE (DEBUG, PRINTOUT ("nbread: %d\n", nb));
         for (i = 0; i < nb; i++) {
             for (j = 0; j < 8; j++) {
                 codcat (bits, sizeof (bits), ((bufin[i] & 0x80) == 0) ? "0" : "1");
                 bufin[i] <<= 1;
                 l++;
-                VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "bits: %d - %s\n", codlen (bits), bits));
+                VERBOSE (DEBUG, PRINTOUT ("bits: %d - %s\n", codlen (bits), bits));
 
                 /* look for correct code */
                 is_found = 0;
                 for (k = 0; (k < NB_BYTES) && (!is_found); k++) {
                     if (codcmp ((char *)(codes + k), bits) == 0) {
                         is_found = 1;
-                        VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "found: %d\n", k));
+                        VERBOSE (DEBUG, PRINTOUT ("found: %d\n", k));
                         *pt= k;
                         bits[0] = 0;
                         if (pt - bufout == BUFFER_SIZE - 1) {
-                            VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nb buffer out: %u\n", (pt - bufout)));
+                            VERBOSE (DEBUG, PRINTOUT ("nb buffer out: %u\n", (pt - bufout)));
                             write (fout, bufout, BUFFER_SIZE);
                             pt = bufout;
                         } else {
@@ -667,14 +617,14 @@ int write_decompress (char *output, char *input, code_t *codes)
                     }
                 }
                 if ((i == nb - 1) && (l % 256 == rem) && (nb != BUFFER_SIZE)) {
-                    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "break\n"));
+                    VERBOSE (DEBUG, PRINTOUT ("break\n"));
                     break;
                 }
             }
         }
     }
     if (pt != bufout) {
-        VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nb buffer out: %u\n", (pt - bufout)));
+        VERBOSE (DEBUG, PRINTOUT ("nb buffer out: %u\n", (pt - bufout)));
         write (fout, bufout, pt - bufout);
     }
 
@@ -682,7 +632,7 @@ int write_decompress (char *output, char *input, code_t *codes)
     close (fin);
     close (fout);
 
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end writing decompressed file\n"));
+    VERBOSE (DEBUG, PRINTOUT ("end writing decompressed file\n"));
 
     return 0;
 }
@@ -705,15 +655,15 @@ int main (int argc, char *argv[])
 
     int c;
     char * arg;
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start processing arguments\n"));
+    VERBOSE (DEBUG, PRINTOUT ("start processing arguments\n"));
     while (argc-- > 1) {
         arg = *(++argv);
         if (arg[0] != '-') {
-            fdprintf (STDERR_FILENO, "%s: invalid option -- %s\n", progname, arg);
-            usage (1);
+            PRINTERR ("%s: invalid option -- %s\n", progname, arg);
+            return usage (1);
         }
         c = arg[1];
-        VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "option: %c\n", c));
+        VERBOSE (DEBUG, PRINTOUT ("option: %c\n", c));
         switch (c) {
         case 'c':
             mode = COMPRESS;
@@ -723,31 +673,31 @@ int main (int argc, char *argv[])
             break;
         case 'i':
             input = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL;
-            VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "input: %s\n", input));
+            VERBOSE (DEBUG, PRINTOUT ("input: %s\n", input));
             break;
         case 'o':
             output = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL;
-            VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "output: %s\n", output));
+            VERBOSE (DEBUG, PRINTOUT ("output: %s\n", output));
             break;
         case 'v':
             arg = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL;
             if (arg == NULL) {
-                fdprintf (STDERR_FILENO, "%s: missing verbose level\n", progname);
-                usage (1);
+                PRINTERR ("%s: missing verbose level\n", progname);
+                return usage (1);
             }
             verbose = myatoi (arg);
-            VERBOSE (INFO, fdprintf (STDOUT_FILENO, "verbose: %d\n", verbose));
+            VERBOSE (INFO, PRINTOUT ("verbose: %d\n", verbose));
             break;
         case 'h':
         default:
-            usage (c != 'h');
+            return usage (c != 'h');
         }
     }
     if ((input == NULL) || (output == NULL)) {
-        fdprintf (STDERR_FILENO, "%s: missing file\n", progname);
-        usage (1);
+        PRINTERR ("%s: missing file\n", progname);
+        return usage (1);
     }
-    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end processing arguments\n"));
+    VERBOSE (DEBUG, PRINTOUT ("end processing arguments\n"));
 
     switch (mode) {
     case COMPRESS:
@@ -775,13 +725,6 @@ int main (int argc, char *argv[])
         break;
     }
 
-    /* clean everything */
-    if (header) free (header);
-    if (codes) free (codes);
-    if (root) free_tree (root);
-    if (leafs) free (leafs);
-    if (table) free (table);
-
     return rc;
 }
 
diff --git a/debug.h b/debug.h
index 62fc38d71e5c713b10324456ed7493adc7c87226..3b8a56e3f160258ccecc491c537be1f87ded774a 100644 (file)
--- a/debug.h
+++ b/debug.h
@@ -3,11 +3,6 @@
 
 /* constants */
 
-#define COMPRESS 1
-#define DECOMPRESS 2
-
-#define NB_CHARS 256
-
 #define DEBUG 4
 #define INFO 3
 #define WARNING 2
index 7b3b097b753cdb4828d73676ff5105cda5b7d8f6..f74189c369b906be442261fbcfbe20ecc523614f 100644 (file)
--- a/fprintf.c
+++ b/fprintf.c
   - initial version
 */
 
-#include <fcntl.h>
 #include <stdarg.h>
 #include <unistd.h>
 
+#include "fprintf.h"
+
+int _fdout = STDOUT_FILENO;
+int _fderr = STDERR_FILENO;
+
 inline unsigned int nextpow (unsigned int x, int base) {
     unsigned int n = 0;
     while (x) {
index 8686b43507ab5cab6abb40115c6e913787fa0445..8c2bb0bb2f81ad971c921be5098ba0473685e205 100644 (file)
--- a/fprintf.h
+++ b/fprintf.h
@@ -3,6 +3,12 @@
 
 int fdprintf (int fd, const char *fmt, ...);
 
+extern int _fdout;
+extern int _fderr;
+
+#define PRINTOUT(fmt...) fdprintf (_fdout, fmt)
+#define PRINTERR(fmt...) fdprintf (_fderr, fmt)
+
 #endif /* __FPRINTF_H__ */
 
 /* vim: set ts=4 sw=4 et */