void usage (int ret)
{
- FILE *fd = ret ? stderr : stdout;
- myfprintf (fd, "usage: %s\n", progname);
- myfprintf (fd, " -h : help message\n");
- myfprintf (fd, " -i <file>: input file\n");
- myfprintf (fd, " -o <file>: output file\n");
- myfprintf (fd, " -v : verbose level (%d)\n", verbose);
+ int fd = ret ? STDERR_FILENO : STDOUT_FILENO;
+ 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);
}
int *table = NULL;
int fid = 0;
- VERBOSE (DEBUG, PRINTF ("start creating occurence table\n"));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start creating occurence table\n"));
/* memory allocation */
table = (int *) calloc (NB_BYTES, sizeof (int));
if (table == NULL) {
- VERBOSE (ERROR, myfprintf (stdout, "can't allocate memory\n"));
+ VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't allocate memory\n"));
return NULL;
}
- VERBOSE (INFO, myfprintf (stdout, "memory allocated\n"));
+ VERBOSE (INFO, fdprintf (STDOUT_FILENO, "memory allocated\n"));
/* open file */
fid = open (filename, O_RDONLY|O_RAW);
if (fid == -1) {
- VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s'\n", filename));
+ VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't open file '%s'\n", filename));
free (table);
return NULL;
}
- VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", filename));
+ VERBOSE (INFO, fdprintf (STDOUT_FILENO, "file '%s' opened\n", filename));
/* read file */
while ((nbread = read (fid, buffer, BUFFER_SIZE)) > 0) {
- VERBOSE (DEBUG, PRINTF ("nbread: %d\n", nbread));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nbread: %d\n", nbread));
while (nbread--) {
table[(int)buffer[nbread]]++;
}
/* close file */
close (fid);
- VERBOSE (DEBUG, PRINTF ("end creating occurence table\n"));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end creating occurence table\n"));
return table;
}
{
int i;
- myfprintf (stdout, "Occurence table\n");
+ fdprintf (STDOUT_FILENO, "Occurence table\n");
for (i = 0; i < NB_BYTES; i++) {
if (table[i]) {
- myfprintf (stdout, "0x%02x '%c': %d\n", i, ((i < 32) || (i > 127)) ? '.' : i, table[i]);
+ fdprintf (STDOUT_FILENO, "0x%02x '%c': %d\n", i, ((i < 32) || (i > 127)) ? '.' : i, table[i]);
}
}
}
int nb_leafs = 0;
int i, l;
- VERBOSE (DEBUG, PRINTF ("start initiliazing forest\n"));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start initiliazing forest\n"));
/* count number of leafs */
for (i = 0; i < NB_BYTES; i++) {
/* allocate memory */
leafs = (leaf_t **) calloc (nb_leafs + 1, sizeof (leaf_t *));
if (leafs == NULL) {
- VERBOSE (ERROR, myfprintf (stdout, "can't allocate memory\n"));
+ VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't allocate memory\n"));
return NULL;
}
if (table[i] > 0) {
leafs[l] = (leaf_t *) calloc (1, sizeof (leaf_t));
if (leafs[l] == NULL) {
- VERBOSE (ERROR, myfprintf (stdout, "can't allocate memory\n"));
+ VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't allocate memory\n"));
return NULL;
}
leafs[l]->occ = table[i];
}
}
- VERBOSE (DEBUG, PRINTF ("end initiliazing forest\n"));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end initiliazing forest\n"));
return leafs;
}
int ante;
int i, j;
- VERBOSE (DEBUG, PRINTF ("start creating tree\n"));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start creating tree\n"));
/* count number of leafs */
while (leafs[nb_leafs] != NULL) {
/* create branch */
if ((last == -1) || (ante == -1)) {
- VERBOSE (ERROR, myfprintf (stdout, "error during tree building\n"));
+ VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "error during tree building\n"));
return NULL;
}
branch = (leaf_t *) calloc (1, sizeof (leaf_t));
if (branch == NULL) {
- VERBOSE (ERROR, myfprintf (stdout, "can't allocate memory\n"));
+ VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't allocate memory\n"));
return NULL;
}
branch->left = leafs[last];
leafs[ante] = NULL;
}
- VERBOSE (DEBUG, PRINTF ("end creating tree\n"));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end creating tree\n"));
return (last != -1) ? leafs[last] : NULL;
}
code_t *table = NULL;
code_t code = {0};
- VERBOSE (DEBUG, PRINTF ("start creating code table\n"));
+ 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, myfprintf (stdout, "can't allocate memory\n"));
+ VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't allocate memory\n"));
return NULL;
}
explore_tree (table, root, (char *)&code, 0);
- VERBOSE (DEBUG, PRINTF ("end creating code table\n"));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end creating code table\n"));
return table;
}
char *code;
int i;
- myfprintf (stdout, "Code table\n");
+ fdprintf (STDOUT_FILENO, "Code table\n");
for (i = 0; i < NB_BYTES; i++) {
code = (char *)(codes + i);
if (codlen (code) == 0) {
continue;
}
- myfprintf (stdout, "0x%02x '%c': %s\n", i, ((i < 32) || (i > 127)) ? '.' : i, code);
+ fdprintf (STDOUT_FILENO, "0x%02x '%c': %s\n", i, ((i < 32) || (i > 127)) ? '.' : i, code);
}
}
int nb = 0;
int size = 0;
- VERBOSE (DEBUG, PRINTF ("start encoding header and code table\n"));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start encoding header and code table\n"));
/* mode 1 or 2 */
for (i = 0; i < NB_BYTES; i++) {
}
}
mode = (NB_BYTES < 2 * nb + 1) ? 1 : 2;
- VERBOSE (DEBUG, PRINTF ("nb chars: %d\n", nb));
- VERBOSE (DEBUG, PRINTF ("mode: %d\n", mode));
- VERBOSE (DEBUG, PRINTF ("size: %d\n", size));
- VERBOSE (DEBUG, PRINTF ("rem: %d\n", size % 256));
+ 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));
/* header */
codcpy ((char *)header, sizeof (buffer), (mode == 1) ? "MZ1 " : "MZ2 ");
/* length */
length = (int)(header - buffer - 6);
- VERBOSE (DEBUG, PRINTF ("lengh: %d %02x %02x\n", length, length >> 8, length & 0xff));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "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 = (byte_t *) calloc (length + 6, 1);
blkcpy (header, buffer, length + 6);
- VERBOSE (DEBUG, PRINTF ("end encoding header and code table\n"));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end encoding header and code table\n"));
return header;
}
int length, i;
length = (header[3] << 8) + header[4];
- VERBOSE (DEBUG, PRINTF ("lengh: %d\n", length));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "lengh: %d\n", length));
for (i = 0; i < length + 6; i++) {
- myfprintf (stdout, "%02x", header[i]);
+ fdprintf (STDOUT_FILENO, "%02x", header[i]);
}
- myfprintf (stdout, "\n");
+ fdprintf (STDOUT_FILENO, "\n");
}
/* write crompressed file */
int i, j, nbread;
byte_t *pt;
- VERBOSE (DEBUG, PRINTF ("start writting compressed file\n"));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start writting compressed file\n"));
/* open input file */
fin = open (input, O_RDONLY|O_RAW);
if (fin == -1) {
- VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s' for reading\n", input));
+ VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't open file '%s' for reading\n", input));
return 1;
}
- VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", input));
+ VERBOSE (INFO, fdprintf (STDOUT_FILENO, "file '%s' opened\n", input));
/* open output file */
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));
+ VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't open file '%s' for writing\n", output));
close (fin);
return 1;
}
- VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", output));
+ VERBOSE (INFO, fdprintf (STDOUT_FILENO, "file '%s' opened\n", output));
/* write header */
length = (header[3] << 8) + header[4];
- VERBOSE (DEBUG, PRINTF ("lengh: %d\n", length));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "lengh: %d\n", length));
write (fout, header, length + 6);
/* write file */
pt = bufout;
while ((nbread = read (fin, bufin, BUFFER_SIZE)) > 0) {
- VERBOSE (DEBUG, PRINTF ("nbread: %d\n", nbread));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nbread: %d\n", nbread));
for (i = 0; i < nbread; i++) {
codcat (bits, sizeof (code_t), (char *)(codes + bufin[i]));
while (codlen (bits) > (8 - 1)) {
}
}
}
- VERBOSE (DEBUG, PRINTF ("lastest bits : %d\n", codlen (bits)));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "lastest bits : %d\n", codlen (bits)));
if (codlen (bits) > 0) {
for (j = 0; j < (int)codlen (bits); j++) {
*pt <<= 1;
pt++;
}
if (pt != bufout) {
- VERBOSE (DEBUG, PRINTF ("last partial buffer written: %u\n", pt - bufout));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "last partial buffer written: %u\n", pt - bufout));
write (fout, bufout, pt - bufout);
}
close (fin);
close (fout);
- VERBOSE (DEBUG, PRINTF ("end writting compressed file\n"));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end writting compressed file\n"));
return 0;
}
int mode = 0;
int i, j, l, nb, size;
- VERBOSE (DEBUG, PRINTF ("start reading header\n"));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start reading header\n"));
/* open file */
fid = open (filename, O_RDONLY|O_RAW);
if (fid == -1) {
- VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s'\n", filename));
+ VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't open file '%s'\n", filename));
return NULL;
}
- VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", filename));
+ VERBOSE (INFO, fdprintf (STDOUT_FILENO, "file '%s' opened\n", filename));
/* read magic number */
nb = read (fid, buffer, 6);
- VERBOSE (DEBUG, PRINTF ("nb, buffer: %d 0x%02x 0x%02x\n", nb, buffer[0], buffer[1]));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "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, PRINTF ("mode, size: %d %d\n", mode, size));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "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, PRINTF ("nb read: %d/%d\n", nb, size));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nb read: %d/%d\n", nb, size));
if (nb != size) {
mode = 0;
}
}
close (fid);
if (mode == 0) {
- VERBOSE (ERROR, myfprintf (stdout, "incorrect file\n"));
+ VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "incorrect file\n"));
return NULL;
}
break;
case 2:
nb = *(codes++) + 1;
- VERBOSE (DEBUG, PRINTF ("nb codes: %d\n", nb));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "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]) PRINTF ("%d: %d\n", i, lengths[i]));
+ VERBOSE (DEBUG, for (i = 0; i < NB_BYTES; i++) if (lengths[i]) fdprintf (STDOUT_FILENO, "%d: %d\n", i, lengths[i]));
/* check lengths */
for (i = 0, l = 0; i < NB_BYTES; i++) {
}
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: %d %d %d\n", size, nb, l));
+ 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, myfprintf (stdout, "can't allocate memory\n"));
+ VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't allocate memory\n"));
return NULL;
}
}
}
- VERBOSE (DEBUG, PRINTF ("end reading header\n"));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end reading header\n"));
return table;
}
int l = 0;
byte_t *pt;
- VERBOSE (DEBUG, PRINTF ("start writing decompressed file\n"));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start writing decompressed file\n"));
/* open file for reading */
fin = open (input, O_RDONLY|O_RAW);
if (fin == -1) {
- VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s' for reading\n", input));
+ VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't open file '%s' for reading\n", input));
return 1;
}
- VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", input));
+ VERBOSE (INFO, fdprintf (STDOUT_FILENO, "file '%s' opened\n", input));
/* read magic number */
nb = read (fin, bufhea, 6);
if (nb != 6) {
- VERBOSE (ERROR, myfprintf (stdout, "can't read file\n"));
+ VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't read file\n"));
close (fin);
return 1;
}
size = (bufhea[3] << 8) + bufhea[4];
- VERBOSE (DEBUG, myfprintf (stdout, "table size: %d\n", size));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "table size: %d\n", size));
rem = bufhea[5];
- VERBOSE (DEBUG, myfprintf (stdout, "remainder: %d\n", rem));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "remainder: %d\n", rem));
nb = read (fin, bufhea, size);
if (nb != size) {
- VERBOSE (ERROR, myfprintf (stdout, "can't read file\n"));
+ VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't read file\n"));
close (fin);
return 1;
}
/* open file for writing */
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));
+ VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't open file '%s' for writing\n", output));
close (fin);
return 2;
}
- VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", output));
+ VERBOSE (INFO, fdprintf (STDOUT_FILENO, "file '%s' opened\n", output));
/* write file */
pt = bufout;
while ((nb = read (fin, bufin, BUFFER_SIZE)) > 0) {
- VERBOSE (DEBUG, PRINTF ("nbread: %d\n", nb));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "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, PRINTF ("bits: %d - %s\n", codlen (bits), bits));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "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, PRINTF ("found: %d\n", k));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "found: %d\n", k));
*pt= k;
bits[0] = 0;
if (pt - bufout == BUFFER_SIZE - 1) {
- VERBOSE (DEBUG, PRINTF ("nb buffer out: %u\n", (pt - bufout)));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nb buffer out: %u\n", (pt - bufout)));
write (fout, bufout, BUFFER_SIZE);
pt = bufout;
} else {
}
}
if ((i == nb - 1) && (l % 256 == rem) && (nb != BUFFER_SIZE)) {
- VERBOSE (DEBUG, PRINTF ("break\n"));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "break\n"));
break;
}
}
}
}
if (pt != bufout) {
- VERBOSE (DEBUG, PRINTF ("nb buffer out: %u\n", (pt - bufout)));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nb buffer out: %u\n", (pt - bufout)));
write (fout, bufout, pt - bufout);
}
close (fin);
close (fout);
- VERBOSE (DEBUG, PRINTF ("end writing decompressed file\n"));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end writing decompressed file\n"));
return 0;
}
int c;
char * arg;
- VERBOSE (DEBUG, PRINTF ("start processing arguments\n"));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start processing arguments\n"));
while (argc-- > 1) {
arg = *(++argv);
if (arg[0] != '-') {
- myfprintf (stderr, "%s: invalid option -- %s\n", progname, arg);
+ fdprintf (STDERR_FILENO, "%s: invalid option -- %s\n", progname, arg);
usage (1);
}
c = arg[1];
- VERBOSE (DEBUG, PRINTF ("option: %c\n", c));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "option: %c\n", c));
switch (c) {
case 'c':
mode = COMPRESS;
break;
case 'i':
input = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL;
- VERBOSE (DEBUG, PRINTF ("input: %s\n", input));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "input: %s\n", input));
break;
case 'o':
output = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL;
- VERBOSE (DEBUG, PRINTF ("output: %s\n", output));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "output: %s\n", output));
break;
case 'v':
arg = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL;
if (arg == NULL) {
- myfprintf (stderr, "%s: missing verbose level\n", progname);
+ fdprintf (STDERR_FILENO, "%s: missing verbose level\n", progname);
usage (1);
}
verbose = myatoi (arg);
- VERBOSE (INFO, myfprintf (stdout, "verbose: %d\n", verbose));
+ VERBOSE (INFO, fdprintf (STDOUT_FILENO, "verbose: %d\n", verbose));
break;
case 'h':
default:
}
}
if ((input == NULL) || (output == NULL)) {
- myfprintf (stderr, "%s: missing file\n", progname);
+ fdprintf (STDERR_FILENO, "%s: missing file\n", progname);
usage (1);
}
- VERBOSE (DEBUG, PRINTF ("end processing arguments\n"));
+ VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end processing arguments\n"));
switch (mode) {
case COMPRESS:
}
/* clean everything */
- fflush (stdout);
if (header) free (header);
if (codes) free (codes);
if (root) free_tree (root);