/* cflags: */
/* linker: atoi.o code.o debug.o fprintf.o */
+#include <fcntl.h>
#include <malloc.h>
-#include <stdio.h>
+#include <unistd.h>
#include "atoi.h"
#include "code.h"
#include "debug.h"
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"));
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;
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]]++;
}
/* close file */
- fclose (fid);
+ close (fid);
VERBOSE (DEBUG, PRINTF ("end creating occurence table\n"));
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;
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));
/* 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]));
}
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++;
}
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"));
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;
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;
}
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;
}
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;
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++) {
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;
}
}
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"));
// 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: */