18 #define BUFFER_SIZE 4096
27 #define CEIL(x, y) (((x) + (y) - 1) / (y))
28 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
29 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
30 #define VERBOSE(level, statement...) do { if (level <= verbose) { statement; } } while(0)
31 #define PRINTF(args...) do { fprintf (stdout, args); fflush (stdout); } while (0)
35 char *progname
= NULL
;
42 FILE *fd
= ret
? stderr
: stdout
;
43 fprintf (fd
, "usage: %s\n", progname
);
44 fprintf (fd
, " -h : help message\n");
45 fprintf (fd
, " -i <file>: input file\n");
46 fprintf (fd
, " -o <file>: output file\n");
47 fprintf (fd
, " -v : verbose level (%d)\n", verbose
);
52 /* create occurence table */
54 int *create_table (char *filename
)
56 char buffer
[BUFFER_SIZE
] = {0};
61 VERBOSE (DEBUG
, PRINTF ("start create occurence table\n"));
63 /* memory allocation */
64 table
= (int *) calloc (NB_CHARS
, sizeof (int));
66 VERBOSE (ERROR
, printf ("can't allocate memory\n"));
69 VERBOSE (INFO
, printf ("memory allocated\n"));
72 fid
= fopen (filename
, "rb");
74 VERBOSE (ERROR
, printf ("can't open file '%s'\n", filename
));
78 VERBOSE (INFO
, printf ("file '%s' opened\n", filename
));
82 nbread
= fread (buffer
, 1, BUFFER_SIZE
, fid
);
83 VERBOSE (DEBUG
, PRINTF ("nbread: %d\n", nbread
));
85 table
[(int)buffer
[nbread
]]++;
92 VERBOSE (DEBUG
, PRINTF ("end create occurence table\n"));
97 /* print occurence table */
99 void print_occ_table (int *table
)
103 printf ("Occurence table\n");
104 for (i
= 0; i
< NB_CHARS
; i
++) {
106 printf ("0x%02x '%c': %d\n", i
, ((i
< 32) || (i
> 127)) ? '.' : i
, table
[i
]);
113 typedef struct _leaf_t
115 struct _leaf_t
*left
;
116 struct _leaf_t
*right
;
121 /* initialize forest */
123 leaf_t
**init_forest (int *table
)
125 leaf_t
**leafs
= NULL
;
129 VERBOSE (DEBUG
, PRINTF ("start initiliazing forest\n"));
131 /* count number of leafs */
132 for (i
= 0; i
< NB_CHARS
; i
++) {
138 /* allocate memory */
139 leafs
= (leaf_t
**) calloc (nb_leafs
+ 1, sizeof (leaf_t
*));
141 VERBOSE (ERROR
, printf ("can't allocate memory\n"));
145 /* initialize leafs */
146 for (i
= 0, l
= 0; i
< NB_CHARS
; i
++) {
148 leafs
[l
] = (leaf_t
*) calloc (1, sizeof (leaf_t
));
149 if (leafs
[l
] == NULL
) {
150 VERBOSE (ERROR
, printf ("can't allocate memory\n"));
153 leafs
[l
]->occ
= table
[i
];
159 VERBOSE (DEBUG
, PRINTF ("end initiliazing forest\n"));
166 leaf_t
*create_tree (leaf_t
**leafs
)
168 leaf_t
*branch
= NULL
;
173 VERBOSE (DEBUG
, PRINTF ("start creating tree\n"));
175 /* count number of leafs */
176 while (leafs
[nb_leafs
] != NULL
) {
181 for (j
= 0; j
< nb_leafs
- 1; j
++) {
183 /* look for leatest occurence */
185 for (i
= 0; i
< nb_leafs
; i
++) {
186 if (leafs
[i
] == NULL
) {
189 if ((last
== -1) || (leafs
[i
]->occ
< leafs
[last
]->occ
)) {
194 /* look for ante leatest occurence */
196 for (i
= 0; i
< nb_leafs
; i
++) {
197 if ((i
== last
) || (leafs
[i
] == NULL
)) {
200 if ((ante
== -1) || (leafs
[i
]->occ
< leafs
[ante
]->occ
)) {
206 if ((last
== -1) || (ante
== -1)) {
207 VERBOSE (ERROR
, printf ("error during tree building\n"));
210 branch
= (leaf_t
*) calloc (1, sizeof (leaf_t
));
211 if (branch
== NULL
) {
212 VERBOSE (ERROR
, printf ("can't allocate memory\n"));
215 branch
->left
= leafs
[last
];
216 branch
->right
= leafs
[ante
];
217 branch
->occ
= branch
->left
->occ
+ branch
->right
->occ
;
218 leafs
[last
] = branch
;
222 VERBOSE (DEBUG
, PRINTF ("end creating tree\n"));
229 void free_tree (leaf_t
*root
) {
232 free_tree (root
->left
);
235 free_tree (root
->right
);
244 char code
[NB_CHARS
- 1 + 1];
249 void explore_tree (code_t
*table
, leaf_t
*root
, char *code
, int index
)
251 if ((root
->left
== NULL
) && (root
->right
== NULL
)) {
252 strcpy ((char *)(table
+ (int)(root
->c
)), code
);
255 strcpy (code
+ index
, "1");
256 explore_tree (table
, root
->left
, code
, index
+ 1);
257 strcpy (code
+ index
, "0");
258 explore_tree (table
, root
->right
, code
, index
+ 1);
262 /* create code table */
264 code_t
*create_code (leaf_t
*root
)
266 code_t
*table
= NULL
;
269 VERBOSE (DEBUG
, PRINTF ("start creating code table\n"));
272 table
= (code_t
*) calloc (NB_CHARS
, sizeof (code_t
));
274 VERBOSE (ERROR
, printf ("can't allocate memory\n"));
278 explore_tree (table
, root
, (char *)&code
, 0);
280 VERBOSE (DEBUG
, PRINTF ("end creating code table\n"));
285 /* print code table */
287 void print_code_table (code_t
*codes
)
292 printf ("Code table\n");
293 for (i
= 0; i
< NB_CHARS
; i
++) {
294 code
= (char *)(codes
+ i
);
295 if (strlen (code
) == 0) {
298 printf ("0x%02x '%c': %s\n", i
, ((i
< 32) || (i
> 127)) ? '.' : i
, code
);
302 /* encode header and code table */
304 char *encode_header_table (code_t
*codes
, int *occ
)
306 unsigned char buffer
[NB_CHARS
* (NB_CHARS
- 1) / 2 / 8 + NB_CHARS
+ 2] = {0};
307 char bits
[(NB_CHARS
- 1) + 8 + 1] = {0};
309 unsigned char *header
= buffer
;
310 int i
, j
, length
, mode
;
314 VERBOSE (DEBUG
, PRINTF ("start encoding header and code table\n"));
317 for (i
= 0; i
< NB_CHARS
; i
++) {
318 code
= (char *)(codes
+ i
);
319 if (strlen (code
) > 0) {
321 size
+= strlen (code
) * occ
[i
];
324 mode
= (NB_CHARS
< 2 * nb
+ 1) ? 1 : 2;
325 VERBOSE (DEBUG
, PRINTF ("nb chars: %d\n", nb
));
326 VERBOSE (DEBUG
, PRINTF ("mode: %d\n", mode
));
327 VERBOSE (DEBUG
, PRINTF ("size: %d\n", size
));
330 strcpy ((char *)header
, (mode
== 1) ? "MZ1 " : "MZ2 ");
336 for (i
= 0; i
< NB_CHARS
; i
++) {
337 code
= (char *)(codes
+ i
);
338 *(header
++) = (unsigned char) strlen (code
);
342 *(header
++) = (unsigned char)(nb
- 1);
343 for (i
= 0; i
< NB_CHARS
; i
++) {
344 code
= (char *)(codes
+ i
);
345 if (strlen (code
) > 0) {
346 *(header
++) = (unsigned char)i
;
347 *(header
++) = (unsigned char) strlen (code
);
354 for (i
= 0; i
< NB_CHARS
; i
++) {
355 code
= (char *)(codes
+ i
);
356 if (strlen (code
) > 0) {
358 while (strlen (bits
) > (8 - 1)) {
359 for (j
= 0; j
< 8; j
++) {
361 if (bits
[j
] == '1') {
365 strcpy (bits
, bits
+ 8);
370 if (strlen (bits
) > 0) {
371 for (j
= 0; j
< (int)strlen (bits
); j
++) {
373 if (bits
[j
] == '1') {
381 length
= (int)(header
- buffer
- 6);
382 VERBOSE (DEBUG
, PRINTF ("lengh: %d %02x %02x\n", length
, length
>> 8, length
& 0xff));
383 buffer
[3] = (unsigned char)(length
>> 8);
384 buffer
[4] = (unsigned char)(length
& 0xff);
385 buffer
[5] = (unsigned char)(size
% 8);
388 header
= (unsigned char *) calloc (length
+ 6, 1);
389 memcpy (header
, buffer
, length
+ 6);
391 VERBOSE (DEBUG
, PRINTF ("end encoding header and code table\n"));
393 return (char *)header
;
398 void print_header (char *header
)
402 length
= ((unsigned char)(header
[3]) << 8) + (unsigned char)(header
[4]);
403 VERBOSE (DEBUG
, PRINTF ("lengh: %d\n", length
));
404 for (i
= 0; i
< length
+ 6; i
++) {
405 printf ("%02x", (unsigned char)header
[i
]);
410 /* write crompressed file */
412 int write_compress (char *output
, char *input
, code_t
*codes
, char *header
)
414 char bufin
[BUFFER_SIZE
] = {0};
415 char bufout
[BUFFER_SIZE
] = {0};
416 char bits
[(NB_CHARS
- 1) + 8 + 1] = {0};
422 VERBOSE (DEBUG
, PRINTF ("start writting compressed file\n"));
424 /* open input file */
425 fin
= fopen (input
, "rb");
427 VERBOSE (ERROR
, printf ("can't open file '%s'\n", input
));
430 VERBOSE (INFO
, printf ("file '%s' opened\n", input
));
432 /* open output file */
433 fout
= fopen (output
, "wb");
435 VERBOSE (ERROR
, printf ("can't open file '%s'\n", output
));
438 VERBOSE (INFO
, printf ("file '%s' opened\n", output
));
441 length
= ((unsigned char)(header
[3]) << 8) + (unsigned char)(header
[4]);
442 VERBOSE (DEBUG
, PRINTF ("lengh: %d\n", length
));
443 fwrite (header
, 1, length
+ 6, fout
);
447 while (!feof (fin
)) {
448 nbread
= fread (bufin
, 1, BUFFER_SIZE
, fin
);
449 VERBOSE (DEBUG
, PRINTF ("nbread: %d\n", nbread
));
450 for (i
= 0; i
< nbread
; i
++) {
451 strcat (bits
, (char *)(codes
+ bufin
[i
]));
452 while (strlen (bits
) > (8 - 1)) {
453 for (j
= 0; j
< 8; j
++) {
455 if (bits
[j
] == '1') {
459 strcpy (bits
, bits
+ 8);
460 if (pt
- bufout
< BUFFER_SIZE
) {
463 fwrite (bufout
, 1, BUFFER_SIZE
, fout
);
469 if (strlen (bits
) > 0) {
470 for (j
= 0; j
< (int)strlen (bits
); j
++) {
472 if (bits
[j
] == '1') {
476 if (pt
- bufout
< BUFFER_SIZE
) {
479 fwrite (bufout
, 1, BUFFER_SIZE
, fout
);
485 fwrite (bufout
, 1, pt
- bufout
, fout
);
492 VERBOSE (DEBUG
, PRINTF ("end writting compressed file\n"));
499 int main (int argc
, char *argv
[])
504 leaf_t
**leafs
= NULL
;
506 code_t
*codes
= NULL
;
514 VERBOSE (DEBUG
, PRINTF ("start argument processing\n"));
515 while ((c
= getopt(argc
, argv
, "cdhi:o:v:")) != EOF
) {
524 VERBOSE (DEBUG
, PRINTF ("-i\n"));
525 VERBOSE (DEBUG
, PRINTF ("optarg: %s\n", optarg
));
529 VERBOSE (DEBUG
, PRINTF ("-o\n"));
533 VERBOSE (DEBUG
, PRINTF ("-v\n"));
534 verbose
= atoi (optarg
);
535 VERBOSE (INFO
, printf ("verbose: %d\n", verbose
));
542 if (argc
- optind
!= 0) {
543 fprintf (stderr
, "%s: invalid option -- %s\n", progname
, argv
[optind
]);
546 VERBOSE (DEBUG
, PRINTF ("end argument processing\n"));
550 table
= create_table (input
);
551 if (table
== NULL
) break;
552 VERBOSE (INFO
, print_occ_table (table
));
554 leafs
= init_forest (table
);
555 if (leafs
== NULL
) break;
556 root
= create_tree (leafs
);
557 if (root
== NULL
) break;
558 codes
= create_code (root
);
559 if (codes
== NULL
) break;
560 VERBOSE (INFO
, print_code_table (codes
));
561 header
= encode_header_table (codes
, table
);
562 if (header
== NULL
) break;
563 VERBOSE (INFO
, print_header (header
));
564 rc
= write_compress (output
, input
, codes
, header
);
571 /* clean everything */
572 if (header
) free (header
);
573 if (codes
) free (codes
);
574 if (root
) free_tree (root
);
575 if (leafs
) free (leafs
);
576 if (table
) free (table
);
581 // test: compress.exe -h
582 // test: compress.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
583 // test: compress.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
584 // test: compress.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'