3 /* linker: code.o debug.o */
16 #define BUFFER_SIZE 4096
22 char *progname
= NULL
;
28 FILE *fd
= ret
? stderr
: stdout
;
29 fprintf (fd
, "usage: %s\n", progname
);
30 fprintf (fd
, " -h : help message\n");
31 fprintf (fd
, " -i <file>: input file\n");
32 fprintf (fd
, " -o <file>: output file\n");
33 fprintf (fd
, " -v : verbose level (%d)\n", verbose
);
38 /* create occurence table */
40 int *create_table (char *filename
)
42 byte_t buffer
[BUFFER_SIZE
] = {0};
47 VERBOSE (DEBUG
, PRINTF ("start creating occurence table\n"));
49 /* memory allocation */
50 table
= (int *) calloc (NB_BYTES
, sizeof (int));
52 VERBOSE (ERROR
, printf ("can't allocate memory\n"));
55 VERBOSE (INFO
, printf ("memory allocated\n"));
58 fid
= fopen (filename
, "rb");
60 VERBOSE (ERROR
, printf ("can't open file '%s'\n", filename
));
64 VERBOSE (INFO
, printf ("file '%s' opened\n", filename
));
68 nbread
= fread (buffer
, 1, BUFFER_SIZE
, fid
);
69 VERBOSE (DEBUG
, PRINTF ("nbread: %d\n", nbread
));
71 table
[(int)buffer
[nbread
]]++;
78 VERBOSE (DEBUG
, PRINTF ("end creating occurence table\n"));
83 /* print occurence table */
85 void print_occ_table (int *table
)
89 printf ("Occurence table\n");
90 for (i
= 0; i
< NB_BYTES
; i
++) {
92 printf ("0x%02x '%c': %d\n", i
, ((i
< 32) || (i
> 127)) ? '.' : i
, table
[i
]);
99 typedef struct _leaf_t
101 struct _leaf_t
*left
;
102 struct _leaf_t
*right
;
107 /* initialize forest */
109 leaf_t
**init_forest (int *table
)
111 leaf_t
**leafs
= NULL
;
115 VERBOSE (DEBUG
, PRINTF ("start initiliazing forest\n"));
117 /* count number of leafs */
118 for (i
= 0; i
< NB_BYTES
; i
++) {
124 /* allocate memory */
125 leafs
= (leaf_t
**) calloc (nb_leafs
+ 1, sizeof (leaf_t
*));
127 VERBOSE (ERROR
, printf ("can't allocate memory\n"));
131 /* initialize leafs */
132 for (i
= 0, l
= 0; i
< NB_BYTES
; i
++) {
134 leafs
[l
] = (leaf_t
*) calloc (1, sizeof (leaf_t
));
135 if (leafs
[l
] == NULL
) {
136 VERBOSE (ERROR
, printf ("can't allocate memory\n"));
139 leafs
[l
]->occ
= table
[i
];
145 VERBOSE (DEBUG
, PRINTF ("end initiliazing forest\n"));
152 leaf_t
*create_tree (leaf_t
**leafs
)
154 leaf_t
*branch
= NULL
;
160 VERBOSE (DEBUG
, PRINTF ("start creating tree\n"));
162 /* count number of leafs */
163 while (leafs
[nb_leafs
] != NULL
) {
168 for (j
= 0; j
< nb_leafs
- 1; j
++) {
170 /* look for leatest occurence */
172 for (i
= 0; i
< nb_leafs
; i
++) {
173 if (leafs
[i
] == NULL
) {
176 if ((last
== -1) || (leafs
[i
]->occ
< leafs
[last
]->occ
)) {
181 /* look for ante leatest occurence */
183 for (i
= 0; i
< nb_leafs
; i
++) {
184 if ((i
== last
) || (leafs
[i
] == NULL
)) {
187 if ((ante
== -1) || (leafs
[i
]->occ
< leafs
[ante
]->occ
)) {
193 if ((last
== -1) || (ante
== -1)) {
194 VERBOSE (ERROR
, printf ("error during tree building\n"));
197 branch
= (leaf_t
*) calloc (1, sizeof (leaf_t
));
198 if (branch
== NULL
) {
199 VERBOSE (ERROR
, printf ("can't allocate memory\n"));
202 branch
->left
= leafs
[last
];
203 branch
->right
= leafs
[ante
];
204 branch
->occ
= branch
->left
->occ
+ branch
->right
->occ
;
205 leafs
[last
] = branch
;
209 VERBOSE (DEBUG
, PRINTF ("end creating tree\n"));
211 return (last
!= -1) ? leafs
[last
] : NULL
;
216 void free_tree (leaf_t
*root
) {
219 free_tree (root
->left
);
222 free_tree (root
->right
);
230 void explore_tree (code_t
*table
, leaf_t
*root
, char *code
, int index
)
232 if ((root
->left
== NULL
) && (root
->right
== NULL
)) {
233 codcpy ((char *)(table
+ (int)(root
->c
)), sizeof (code_t
), code
);
236 codcpy (code
+ index
, sizeof (code_t
), "1");
237 explore_tree (table
, root
->left
, code
, index
+ 1);
238 codcpy (code
+ index
, sizeof (code_t
), "0");
239 explore_tree (table
, root
->right
, code
, index
+ 1);
243 /* create code table */
245 code_t
*create_code (leaf_t
*root
)
247 code_t
*table
= NULL
;
250 VERBOSE (DEBUG
, PRINTF ("start creating code table\n"));
253 table
= (code_t
*) calloc (NB_BYTES
, sizeof (code_t
));
255 VERBOSE (ERROR
, printf ("can't allocate memory\n"));
259 explore_tree (table
, root
, (char *)&code
, 0);
261 VERBOSE (DEBUG
, PRINTF ("end creating code table\n"));
266 /* print code table */
268 void print_code_table (code_t
*codes
)
273 printf ("Code table\n");
274 for (i
= 0; i
< NB_BYTES
; i
++) {
275 code
= (char *)(codes
+ i
);
276 if (codlen (code
) == 0) {
279 printf ("0x%02x '%c': %s\n", i
, ((i
< 32) || (i
> 127)) ? '.' : i
, code
);
283 /* encode header and code table */
285 byte_t
*encode_header_table (code_t
*codes
, int *occ
)
287 byte_t buffer
[NB_BYTES
* (NB_BYTES
- 1) / 2 / 8 + NB_BYTES
+ 6] = {0};
288 char bits
[(NB_BYTES
- 1) + 8 + 1] = {0};
290 byte_t
*header
= buffer
;
291 int i
, j
, length
, mode
;
295 VERBOSE (DEBUG
, PRINTF ("start encoding header and code table\n"));
298 for (i
= 0; i
< NB_BYTES
; i
++) {
299 code
= (char *)(codes
+ i
);
300 if (codlen (code
) > 0) {
302 size
+= codlen (code
) * occ
[i
];
305 mode
= (NB_BYTES
< 2 * nb
+ 1) ? 1 : 2;
306 VERBOSE (DEBUG
, PRINTF ("nb chars: %d\n", nb
));
307 VERBOSE (DEBUG
, PRINTF ("mode: %d\n", mode
));
308 VERBOSE (DEBUG
, PRINTF ("size: %d\n", size
));
309 VERBOSE (DEBUG
, PRINTF ("rem: %d\n", size
% 256));
312 codcpy ((char *)header
, sizeof (buffer
), (mode
== 1) ? "MZ1 " : "MZ2 ");
318 for (i
= 0; i
< NB_BYTES
; i
++) {
319 code
= (char *)(codes
+ i
);
320 *(header
++) = (byte_t
) codlen (code
);
324 *(header
++) = (byte_t
)(nb
- 1);
325 for (i
= 0; i
< NB_BYTES
; i
++) {
326 code
= (char *)(codes
+ i
);
327 if (codlen (code
) > 0) {
328 *(header
++) = (byte_t
) i
;
329 *(header
++) = (byte_t
) codlen (code
);
336 for (i
= 0; i
< NB_BYTES
; i
++) {
337 code
= (char *)(codes
+ i
);
338 if (codlen (code
) > 0) {
339 codcat (bits
, sizeof (code_t
), code
);
340 while (codlen (bits
) > (8 - 1)) {
341 for (j
= 0; j
< 8; j
++) {
343 if (bits
[j
] == '1') {
347 codcpy (bits
, sizeof (code_t
), bits
+ 8);
352 if (codlen (bits
) > 0) {
353 for (j
= 0; j
< (int)codlen (bits
); j
++) {
355 if (bits
[j
] == '1') {
359 for (j
= (int)codlen (bits
); j
< 8; j
++) {
366 length
= (int)(header
- buffer
- 6);
367 VERBOSE (DEBUG
, PRINTF ("lengh: %d %02x %02x\n", length
, length
>> 8, length
& 0xff));
368 buffer
[3] = (byte_t
)(length
>> 8);
369 buffer
[4] = (byte_t
)(length
& 0xff);
370 buffer
[5] = (byte_t
)(size
% 256);
373 header
= (byte_t
*) calloc (length
+ 6, 1);
374 memcpy (header
, buffer
, length
+ 6);
376 VERBOSE (DEBUG
, PRINTF ("end encoding header and code table\n"));
383 void print_header (byte_t
*header
)
387 length
= (header
[3] << 8) + header
[4];
388 VERBOSE (DEBUG
, PRINTF ("lengh: %d\n", length
));
389 for (i
= 0; i
< length
+ 6; i
++) {
390 printf ("%02x", header
[i
]);
395 /* write crompressed file */
397 int write_compress (char *output
, char *input
, code_t
*codes
, byte_t
*header
)
399 byte_t bufin
[BUFFER_SIZE
] = {0};
400 byte_t bufout
[BUFFER_SIZE
] = {0};
401 char bits
[(NB_BYTES
- 1) + 8 + 1] = {0};
407 VERBOSE (DEBUG
, PRINTF ("start writting compressed file\n"));
409 /* open input file */
410 fin
= fopen (input
, "rb");
412 VERBOSE (ERROR
, printf ("can't open file '%s' for reading\n", input
));
415 VERBOSE (INFO
, printf ("file '%s' opened\n", input
));
417 /* open output file */
418 fout
= fopen (output
, "wb");
420 VERBOSE (ERROR
, printf ("can't open file '%s' for writing\n", output
));
423 VERBOSE (INFO
, printf ("file '%s' opened\n", output
));
426 length
= (header
[3] << 8) + header
[4];
427 VERBOSE (DEBUG
, PRINTF ("lengh: %d\n", length
));
428 fwrite (header
, 1, length
+ 6, fout
);
432 while (!feof (fin
)) {
433 nbread
= fread (bufin
, 1, BUFFER_SIZE
, fin
);
434 VERBOSE (DEBUG
, PRINTF ("nbread: %d\n", nbread
));
435 for (i
= 0; i
< nbread
; i
++) {
436 codcat (bits
, sizeof (code_t
), (char *)(codes
+ bufin
[i
]));
437 while (codlen (bits
) > (8 - 1)) {
438 for (j
= 0; j
< 8; j
++) {
440 if (bits
[j
] == '1') {
444 codcpy (bits
, sizeof (code_t
), bits
+ 8);
445 if (pt
- bufout
== BUFFER_SIZE
- 1) {
446 fwrite (bufout
, 1, BUFFER_SIZE
, fout
);
454 VERBOSE (DEBUG
, PRINTF ("lastest bits : %d\n", codlen (bits
)));
455 if (codlen (bits
) > 0) {
456 for (j
= 0; j
< (int)codlen (bits
); j
++) {
458 if (bits
[j
] == '1') {
462 for (j
= (int)codlen (bits
); j
< 8; j
++) {
468 VERBOSE (DEBUG
, PRINTF ("last partial buffer written: %u\n", pt
- bufout
));
469 fwrite (bufout
, 1, pt
- bufout
, fout
);
476 VERBOSE (DEBUG
, PRINTF ("end writting compressed file\n"));
483 code_t
*read_header (char *filename
) {
484 byte_t buffer
[NB_BYTES
* (NB_BYTES
- 1) / 2 / 8 + NB_BYTES
+ 6] = {0};
485 code_t
*table
= NULL
;
486 byte_t
*codes
= NULL
;
488 int lengths
[NB_BYTES
] = {0};
491 size_t i
, j
, l
, nb
, size
;
493 VERBOSE (DEBUG
, PRINTF ("start reading header\n"));
496 fid
= fopen (filename
, "rb");
498 VERBOSE (ERROR
, printf ("can't open file '%s'\n", filename
));
501 VERBOSE (INFO
, printf ("file '%s' opened\n", filename
));
503 /* read magic number */
504 nb
= fread (buffer
, 1, 6, fid
);
505 VERBOSE (DEBUG
, PRINTF ("nb, buffer: %d 0x%02x 0x%02x\n", nb
, buffer
[0], buffer
[1]));
506 if ((nb
== 6) && (buffer
[0] == 'M') && (buffer
[1] == 'Z')) {
507 mode
= (buffer
[2] == '1') ? 1 : (buffer
[2] == '2') ? 2 : 0;
508 size
= (buffer
[3] << 8) + buffer
[4];
509 VERBOSE (DEBUG
, PRINTF ("mode, size: %d %d\n", mode
, size
));
510 if (size
> NB_BYTES
* (NB_BYTES
- 1) / 2 / 8 + NB_BYTES
) {
513 nb
= fread (buffer
, 1, size
, fid
);
514 VERBOSE (DEBUG
, PRINTF ("nb read: %d\n", nb
));
522 VERBOSE (ERROR
, printf ("incorrect file\n"));
530 for (i
= 0; i
< NB_BYTES
; i
++) {
531 lengths
[i
] = *(codes
++);
536 VERBOSE (DEBUG
, PRINTF ("nb codes: %d\n", nb
));
537 for (i
= 0; i
< nb
; i
++) {
539 lengths
[j
] = *(codes
++);
543 VERBOSE (DEBUG
, for (i
= 0; i
< NB_BYTES
; i
++) if (lengths
[i
]) PRINTF ("%d: %d\n", i
, lengths
[i
]));
546 for (i
= 0, l
= 0; i
< NB_BYTES
; i
++) {
549 if (((mode
== 1) && (size
- 256 != (l
+ 7) / 8)) ||
550 ((mode
== 2) && (size
- 2 * nb
- 1 != (l
+ 7) / 8))) {
551 VERBOSE (ERROR
, printf ("incorrect code table length\n"));
556 table
= (code_t
*) calloc (NB_BYTES
, sizeof (code_t
));
558 VERBOSE (ERROR
, printf ("can't allocate memory\n"));
565 for (i
= 0; i
< NB_BYTES
; i
++) {
566 if (lengths
[i
] == 0) {
569 while (lengths
[i
]--) {
570 codcat ((char *)(table
+ i
), sizeof (code_t
), ((cur
& 0x80) == 0) ? "0" : "1");
580 VERBOSE (DEBUG
, PRINTF ("end reading header\n"));
585 /* write decompressed file */
587 int write_decompress (char *output
, char *input
, code_t
*codes
)
589 byte_t bufin
[BUFFER_SIZE
] = {0};
590 byte_t bufout
[BUFFER_SIZE
] = {0};
591 byte_t bufhea
[MAX(NB_BYTES
* (NB_BYTES
- 1) / 2 / 8 + NB_BYTES
+ 6, BUFFER_SIZE
)] = {0};
592 char bits
[(NB_BYTES
- 1) + 1] = {0};
594 int i
, j
, k
, nb
, size
, rem
;
599 VERBOSE (DEBUG
, PRINTF ("start writing decompressed file\n"));
601 /* open file for reading */
602 fin
= fopen (input
, "rb");
604 VERBOSE (ERROR
, printf ("can't open file '%s' for reading\n", input
));
607 VERBOSE (INFO
, printf ("file '%s' opened\n", input
));
609 /* read magic number */
610 nb
= fread (bufhea
, 1, 6, fin
);
612 VERBOSE (ERROR
, printf ("can't read file\n"));
616 size
= (bufhea
[3] << 8) + bufhea
[4];
617 VERBOSE (DEBUG
, printf ("table size: %d\n", size
));
619 VERBOSE (DEBUG
, printf ("remainder: %d\n", rem
));
620 nb
= fread (bufhea
, 1, size
, fin
);
622 VERBOSE (ERROR
, printf ("can't read file\n"));
627 /* open file for writing */
628 fout
= fopen (output
, "wb");
630 VERBOSE (ERROR
, printf ("can't open file '%s' for writing\n", output
));
633 VERBOSE (INFO
, printf ("file '%s' opened\n", output
));
637 while (!feof (fin
)) {
638 nb
= fread (bufin
, 1, BUFFER_SIZE
, fin
);
639 VERBOSE (DEBUG
, PRINTF ("nbread: %d\n", nb
));
640 for (i
= 0; i
< nb
; i
++) {
641 for (j
= 0; j
< 8; j
++) {
642 codcat (bits
, sizeof (bits
), ((bufin
[i
] & 0x80) == 0) ? "0" : "1");
645 VERBOSE (DEBUG
, PRINTF ("bits: %d - %s\n", codlen (bits
), bits
));
647 /* look for correct code */
649 for (k
= 0; (k
< NB_BYTES
) && (!is_found
); k
++) {
650 if (codcmp ((char *)(codes
+ k
), bits
) == 0) {
652 VERBOSE (DEBUG
, PRINTF ("found: %d\n", k
));
655 if (pt
- bufout
== BUFFER_SIZE
- 1) {
656 VERBOSE (DEBUG
, PRINTF ("nb buffer out: %u\n", (pt
- bufout
)));
657 fwrite (bufout
, 1, BUFFER_SIZE
, fout
);
664 if ((i
== nb
- 1) && (l
% 256 == rem
) && (feof (fin
))) {
665 VERBOSE (DEBUG
, PRINTF ("break\n"));
672 VERBOSE (DEBUG
, PRINTF ("nb buffer out: %u\n", (pt
- bufout
)));
673 fwrite (bufout
, 1, pt
- bufout
, fout
);
680 VERBOSE (DEBUG
, PRINTF ("end writing decompressed file\n"));
687 int main (int argc
, char *argv
[])
692 leaf_t
**leafs
= NULL
;
694 code_t
*codes
= NULL
;
695 byte_t
*header
= NULL
;
702 VERBOSE (DEBUG
, PRINTF ("start processing arguments\n"));
703 while ((c
= getopt(argc
, argv
, "cdhi:o:v:")) != EOF
) {
704 VERBOSE (DEBUG
, PRINTF ("option: %c\n", c
));
714 VERBOSE (DEBUG
, PRINTF ("input: %s\n", input
));
718 VERBOSE (DEBUG
, PRINTF ("output: %s\n", output
));
721 verbose
= atoi (optarg
);
722 VERBOSE (INFO
, printf ("verbose: %d\n", verbose
));
729 if (argc
- optind
!= 0) {
730 fprintf (stderr
, "%s: invalid option -- %s\n", progname
, argv
[optind
]);
733 VERBOSE (DEBUG
, PRINTF ("end processing arguments\n"));
737 table
= create_table (input
);
738 if (table
== NULL
) break;
739 VERBOSE (INFO
, print_occ_table (table
));
741 leafs
= init_forest (table
);
742 if (leafs
== NULL
) break;
743 root
= create_tree (leafs
);
744 if (root
== NULL
) break;
745 codes
= create_code (root
);
746 if (codes
== NULL
) break;
747 VERBOSE (INFO
, print_code_table (codes
));
748 header
= encode_header_table (codes
, table
);
749 if (header
== NULL
) break;
750 VERBOSE (INFO
, print_header (header
));
751 rc
= write_compress (output
, input
, codes
, header
);
754 codes
= read_header (input
);
755 if (codes
== NULL
) break;
756 VERBOSE (INFO
, print_code_table (codes
));
757 rc
= write_decompress (output
, input
, codes
);
761 /* clean everything */
763 if (header
) free (header
);
764 if (codes
) free (codes
);
765 if (root
) free_tree (root
);
766 if (leafs
) free (leafs
);
767 if (table
) free (table
);
772 // test: compress.exe -h
773 // test: compress.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
774 // test: compress.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
775 // test: compress.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
776 // test: compress.exe -c -i compress.c -o compress.mz
777 // test: ls -sS1 compress.c compress.mz | tail -1 | grep compress.mz
778 // test: compress.exe -d -i compress.mz -o tmp.c
779 // test: cmp compress.c tmp.c
780 // test: rm compress.mz tmp.c