3 /* linker: code.o debug.o fprintf.o */
13 #define BUFFER_SIZE 4096
19 char *progname
= NULL
;
25 FILE *fd
= ret
? stderr
: stdout
;
26 _fprintf (fd
, "usage: %s\n", progname
);
27 _fprintf (fd
, " -h : help message\n");
28 _fprintf (fd
, " -i <file>: input file\n");
29 _fprintf (fd
, " -o <file>: output file\n");
30 _fprintf (fd
, " -v : verbose level (%d)\n", verbose
);
35 void blkcpy (void *dst
, const void *src
, int len
)
38 *((char *)dst
++) = *((char *)src
++);
42 /* create occurence table */
44 int *create_table (char *filename
)
46 byte_t buffer
[BUFFER_SIZE
] = {0};
51 VERBOSE (DEBUG
, PRINTF ("start creating occurence table\n"));
53 /* memory allocation */
54 table
= (int *) calloc (NB_BYTES
, sizeof (int));
56 VERBOSE (ERROR
, _fprintf (stdout
, "can't allocate memory\n"));
59 VERBOSE (INFO
, _fprintf (stdout
, "memory allocated\n"));
62 fid
= fopen (filename
, "rb");
64 VERBOSE (ERROR
, _fprintf (stdout
, "can't open file '%s'\n", filename
));
68 VERBOSE (INFO
, _fprintf (stdout
, "file '%s' opened\n", filename
));
72 nbread
= fread (buffer
, 1, BUFFER_SIZE
, fid
);
73 VERBOSE (DEBUG
, PRINTF ("nbread: %d\n", nbread
));
75 table
[(int)buffer
[nbread
]]++;
82 VERBOSE (DEBUG
, PRINTF ("end creating occurence table\n"));
87 /* print occurence table */
89 void print_occ_table (int *table
)
93 _fprintf (stdout
, "Occurence table\n");
94 for (i
= 0; i
< NB_BYTES
; i
++) {
96 _fprintf (stdout
, "0x%02x '%c': %d\n", i
, ((i
< 32) || (i
> 127)) ? '.' : i
, table
[i
]);
103 typedef struct _leaf_t
105 struct _leaf_t
*left
;
106 struct _leaf_t
*right
;
111 /* initialize forest */
113 leaf_t
**init_forest (int *table
)
115 leaf_t
**leafs
= NULL
;
119 VERBOSE (DEBUG
, PRINTF ("start initiliazing forest\n"));
121 /* count number of leafs */
122 for (i
= 0; i
< NB_BYTES
; i
++) {
128 /* allocate memory */
129 leafs
= (leaf_t
**) calloc (nb_leafs
+ 1, sizeof (leaf_t
*));
131 VERBOSE (ERROR
, _fprintf (stdout
, "can't allocate memory\n"));
135 /* initialize leafs */
136 for (i
= 0, l
= 0; i
< NB_BYTES
; i
++) {
138 leafs
[l
] = (leaf_t
*) calloc (1, sizeof (leaf_t
));
139 if (leafs
[l
] == NULL
) {
140 VERBOSE (ERROR
, _fprintf (stdout
, "can't allocate memory\n"));
143 leafs
[l
]->occ
= table
[i
];
149 VERBOSE (DEBUG
, PRINTF ("end initiliazing forest\n"));
156 leaf_t
*create_tree (leaf_t
**leafs
)
158 leaf_t
*branch
= NULL
;
164 VERBOSE (DEBUG
, PRINTF ("start creating tree\n"));
166 /* count number of leafs */
167 while (leafs
[nb_leafs
] != NULL
) {
172 for (j
= 0; j
< nb_leafs
- 1; j
++) {
174 /* look for leatest occurence */
176 for (i
= 0; i
< nb_leafs
; i
++) {
177 if (leafs
[i
] == NULL
) {
180 if ((last
== -1) || (leafs
[i
]->occ
< leafs
[last
]->occ
)) {
185 /* look for ante leatest occurence */
187 for (i
= 0; i
< nb_leafs
; i
++) {
188 if ((i
== last
) || (leafs
[i
] == NULL
)) {
191 if ((ante
== -1) || (leafs
[i
]->occ
< leafs
[ante
]->occ
)) {
197 if ((last
== -1) || (ante
== -1)) {
198 VERBOSE (ERROR
, _fprintf (stdout
, "error during tree building\n"));
201 branch
= (leaf_t
*) calloc (1, sizeof (leaf_t
));
202 if (branch
== NULL
) {
203 VERBOSE (ERROR
, _fprintf (stdout
, "can't allocate memory\n"));
206 branch
->left
= leafs
[last
];
207 branch
->right
= leafs
[ante
];
208 branch
->occ
= branch
->left
->occ
+ branch
->right
->occ
;
209 leafs
[last
] = branch
;
213 VERBOSE (DEBUG
, PRINTF ("end creating tree\n"));
215 return (last
!= -1) ? leafs
[last
] : NULL
;
220 void free_tree (leaf_t
*root
) {
223 free_tree (root
->left
);
226 free_tree (root
->right
);
234 void explore_tree (code_t
*table
, leaf_t
*root
, char *code
, int index
)
236 if ((root
->left
== NULL
) && (root
->right
== NULL
)) {
237 codcpy ((char *)(table
+ (int)(root
->c
)), sizeof (code_t
), code
);
240 codcpy (code
+ index
, sizeof (code_t
), "1");
241 explore_tree (table
, root
->left
, code
, index
+ 1);
242 codcpy (code
+ index
, sizeof (code_t
), "0");
243 explore_tree (table
, root
->right
, code
, index
+ 1);
247 /* create code table */
249 code_t
*create_code (leaf_t
*root
)
251 code_t
*table
= NULL
;
254 VERBOSE (DEBUG
, PRINTF ("start creating code table\n"));
257 table
= (code_t
*) calloc (NB_BYTES
, sizeof (code_t
));
259 VERBOSE (ERROR
, _fprintf (stdout
, "can't allocate memory\n"));
263 explore_tree (table
, root
, (char *)&code
, 0);
265 VERBOSE (DEBUG
, PRINTF ("end creating code table\n"));
270 /* print code table */
272 void print_code_table (code_t
*codes
)
277 _fprintf (stdout
, "Code table\n");
278 for (i
= 0; i
< NB_BYTES
; i
++) {
279 code
= (char *)(codes
+ i
);
280 if (codlen (code
) == 0) {
283 _fprintf (stdout
, "0x%02x '%c': %s\n", i
, ((i
< 32) || (i
> 127)) ? '.' : i
, code
);
287 /* encode header and code table */
289 byte_t
*encode_header_table (code_t
*codes
, int *occ
)
291 byte_t buffer
[NB_BYTES
* (NB_BYTES
- 1) / 2 / 8 + NB_BYTES
+ 6] = {0};
292 char bits
[(NB_BYTES
- 1) + 8 + 1] = {0};
294 byte_t
*header
= buffer
;
295 int i
, j
, length
, mode
;
299 VERBOSE (DEBUG
, PRINTF ("start encoding header and code table\n"));
302 for (i
= 0; i
< NB_BYTES
; i
++) {
303 code
= (char *)(codes
+ i
);
304 if (codlen (code
) > 0) {
306 size
+= codlen (code
) * occ
[i
];
309 mode
= (NB_BYTES
< 2 * nb
+ 1) ? 1 : 2;
310 VERBOSE (DEBUG
, PRINTF ("nb chars: %d\n", nb
));
311 VERBOSE (DEBUG
, PRINTF ("mode: %d\n", mode
));
312 VERBOSE (DEBUG
, PRINTF ("size: %d\n", size
));
313 VERBOSE (DEBUG
, PRINTF ("rem: %d\n", size
% 256));
316 codcpy ((char *)header
, sizeof (buffer
), (mode
== 1) ? "MZ1 " : "MZ2 ");
322 for (i
= 0; i
< NB_BYTES
; i
++) {
323 code
= (char *)(codes
+ i
);
324 *(header
++) = (byte_t
) codlen (code
);
328 *(header
++) = (byte_t
)(nb
- 1);
329 for (i
= 0; i
< NB_BYTES
; i
++) {
330 code
= (char *)(codes
+ i
);
331 if (codlen (code
) > 0) {
332 *(header
++) = (byte_t
) i
;
333 *(header
++) = (byte_t
) codlen (code
);
340 for (i
= 0; i
< NB_BYTES
; i
++) {
341 code
= (char *)(codes
+ i
);
342 if (codlen (code
) > 0) {
343 codcat (bits
, sizeof (code_t
), code
);
344 while (codlen (bits
) > (8 - 1)) {
345 for (j
= 0; j
< 8; j
++) {
347 if (bits
[j
] == '1') {
351 codcpy (bits
, sizeof (code_t
), bits
+ 8);
356 if (codlen (bits
) > 0) {
357 for (j
= 0; j
< (int)codlen (bits
); j
++) {
359 if (bits
[j
] == '1') {
363 for (j
= (int)codlen (bits
); j
< 8; j
++) {
370 length
= (int)(header
- buffer
- 6);
371 VERBOSE (DEBUG
, PRINTF ("lengh: %d %02x %02x\n", length
, length
>> 8, length
& 0xff));
372 buffer
[3] = (byte_t
)(length
>> 8);
373 buffer
[4] = (byte_t
)(length
& 0xff);
374 buffer
[5] = (byte_t
)(size
% 256);
377 header
= (byte_t
*) calloc (length
+ 6, 1);
378 blkcpy (header
, buffer
, length
+ 6);
380 VERBOSE (DEBUG
, PRINTF ("end encoding header and code table\n"));
387 void print_header (byte_t
*header
)
391 length
= (header
[3] << 8) + header
[4];
392 VERBOSE (DEBUG
, PRINTF ("lengh: %d\n", length
));
393 for (i
= 0; i
< length
+ 6; i
++) {
394 _fprintf (stdout
, "%02x", header
[i
]);
396 _fprintf (stdout
, "\n");
399 /* write crompressed file */
401 int write_compress (char *output
, char *input
, code_t
*codes
, byte_t
*header
)
403 byte_t bufin
[BUFFER_SIZE
] = {0};
404 byte_t bufout
[BUFFER_SIZE
] = {0};
405 char bits
[(NB_BYTES
- 1) + 8 + 1] = {0};
411 VERBOSE (DEBUG
, PRINTF ("start writting compressed file\n"));
413 /* open input file */
414 fin
= fopen (input
, "rb");
416 VERBOSE (ERROR
, _fprintf (stdout
, "can't open file '%s' for reading\n", input
));
419 VERBOSE (INFO
, _fprintf (stdout
, "file '%s' opened\n", input
));
421 /* open output file */
422 fout
= fopen (output
, "wb");
424 VERBOSE (ERROR
, _fprintf (stdout
, "can't open file '%s' for writing\n", output
));
427 VERBOSE (INFO
, _fprintf (stdout
, "file '%s' opened\n", output
));
430 length
= (header
[3] << 8) + header
[4];
431 VERBOSE (DEBUG
, PRINTF ("lengh: %d\n", length
));
432 fwrite (header
, 1, length
+ 6, fout
);
436 while (!feof (fin
)) {
437 nbread
= fread (bufin
, 1, BUFFER_SIZE
, fin
);
438 VERBOSE (DEBUG
, PRINTF ("nbread: %d\n", nbread
));
439 for (i
= 0; i
< nbread
; i
++) {
440 codcat (bits
, sizeof (code_t
), (char *)(codes
+ bufin
[i
]));
441 while (codlen (bits
) > (8 - 1)) {
442 for (j
= 0; j
< 8; j
++) {
444 if (bits
[j
] == '1') {
448 codcpy (bits
, sizeof (code_t
), bits
+ 8);
449 if (pt
- bufout
== BUFFER_SIZE
- 1) {
450 fwrite (bufout
, 1, BUFFER_SIZE
, fout
);
458 VERBOSE (DEBUG
, PRINTF ("lastest bits : %d\n", codlen (bits
)));
459 if (codlen (bits
) > 0) {
460 for (j
= 0; j
< (int)codlen (bits
); j
++) {
462 if (bits
[j
] == '1') {
466 for (j
= (int)codlen (bits
); j
< 8; j
++) {
472 VERBOSE (DEBUG
, PRINTF ("last partial buffer written: %u\n", pt
- bufout
));
473 fwrite (bufout
, 1, pt
- bufout
, fout
);
480 VERBOSE (DEBUG
, PRINTF ("end writting compressed file\n"));
487 code_t
*read_header (char *filename
) {
488 byte_t buffer
[NB_BYTES
* (NB_BYTES
- 1) / 2 / 8 + NB_BYTES
+ 6] = {0};
489 code_t
*table
= NULL
;
490 byte_t
*codes
= NULL
;
492 int lengths
[NB_BYTES
] = {0};
495 size_t i
, j
, l
, nb
, size
;
497 VERBOSE (DEBUG
, PRINTF ("start reading header\n"));
500 fid
= fopen (filename
, "rb");
502 VERBOSE (ERROR
, _fprintf (stdout
, "can't open file '%s'\n", filename
));
505 VERBOSE (INFO
, _fprintf (stdout
, "file '%s' opened\n", filename
));
507 /* read magic number */
508 nb
= fread (buffer
, 1, 6, fid
);
509 VERBOSE (DEBUG
, PRINTF ("nb, buffer: %d 0x%02x 0x%02x\n", nb
, buffer
[0], buffer
[1]));
510 if ((nb
== 6) && (buffer
[0] == 'M') && (buffer
[1] == 'Z')) {
511 mode
= (buffer
[2] == '1') ? 1 : (buffer
[2] == '2') ? 2 : 0;
512 size
= (buffer
[3] << 8) + buffer
[4];
513 VERBOSE (DEBUG
, PRINTF ("mode, size: %d %d\n", mode
, size
));
514 if (size
> NB_BYTES
* (NB_BYTES
- 1) / 2 / 8 + NB_BYTES
) {
517 nb
= fread (buffer
, 1, size
, fid
);
518 VERBOSE (DEBUG
, PRINTF ("nb read: %d\n", nb
));
526 VERBOSE (ERROR
, _fprintf (stdout
, "incorrect file\n"));
534 for (i
= 0; i
< NB_BYTES
; i
++) {
535 lengths
[i
] = *(codes
++);
540 VERBOSE (DEBUG
, PRINTF ("nb codes: %d\n", nb
));
541 for (i
= 0; i
< nb
; i
++) {
543 lengths
[j
] = *(codes
++);
547 VERBOSE (DEBUG
, for (i
= 0; i
< NB_BYTES
; i
++) if (lengths
[i
]) PRINTF ("%d: %d\n", i
, lengths
[i
]));
550 for (i
= 0, l
= 0; i
< NB_BYTES
; i
++) {
553 if (((mode
== 1) && (size
- 256 != (l
+ 7) / 8)) ||
554 ((mode
== 2) && (size
- 2 * nb
- 1 != (l
+ 7) / 8))) {
555 VERBOSE (ERROR
, _fprintf (stdout
, "incorrect code table length\n"));
560 table
= (code_t
*) calloc (NB_BYTES
, sizeof (code_t
));
562 VERBOSE (ERROR
, _fprintf (stdout
, "can't allocate memory\n"));
569 for (i
= 0; i
< NB_BYTES
; i
++) {
570 if (lengths
[i
] == 0) {
573 while (lengths
[i
]--) {
574 codcat ((char *)(table
+ i
), sizeof (code_t
), ((cur
& 0x80) == 0) ? "0" : "1");
584 VERBOSE (DEBUG
, PRINTF ("end reading header\n"));
589 /* write decompressed file */
591 int write_decompress (char *output
, char *input
, code_t
*codes
)
593 byte_t bufin
[BUFFER_SIZE
] = {0};
594 byte_t bufout
[BUFFER_SIZE
] = {0};
595 byte_t bufhea
[MAX(NB_BYTES
* (NB_BYTES
- 1) / 2 / 8 + NB_BYTES
+ 6, BUFFER_SIZE
)] = {0};
596 char bits
[(NB_BYTES
- 1) + 1] = {0};
598 int i
, j
, k
, nb
, size
, rem
;
603 VERBOSE (DEBUG
, PRINTF ("start writing decompressed file\n"));
605 /* open file for reading */
606 fin
= fopen (input
, "rb");
608 VERBOSE (ERROR
, _fprintf (stdout
, "can't open file '%s' for reading\n", input
));
611 VERBOSE (INFO
, _fprintf (stdout
, "file '%s' opened\n", input
));
613 /* read magic number */
614 nb
= fread (bufhea
, 1, 6, fin
);
616 VERBOSE (ERROR
, _fprintf (stdout
, "can't read file\n"));
620 size
= (bufhea
[3] << 8) + bufhea
[4];
621 VERBOSE (DEBUG
, _fprintf (stdout
, "table size: %d\n", size
));
623 VERBOSE (DEBUG
, _fprintf (stdout
, "remainder: %d\n", rem
));
624 nb
= fread (bufhea
, 1, size
, fin
);
626 VERBOSE (ERROR
, _fprintf (stdout
, "can't read file\n"));
631 /* open file for writing */
632 fout
= fopen (output
, "wb");
634 VERBOSE (ERROR
, _fprintf (stdout
, "can't open file '%s' for writing\n", output
));
637 VERBOSE (INFO
, _fprintf (stdout
, "file '%s' opened\n", output
));
641 while (!feof (fin
)) {
642 nb
= fread (bufin
, 1, BUFFER_SIZE
, fin
);
643 VERBOSE (DEBUG
, PRINTF ("nbread: %d\n", nb
));
644 for (i
= 0; i
< nb
; i
++) {
645 for (j
= 0; j
< 8; j
++) {
646 codcat (bits
, sizeof (bits
), ((bufin
[i
] & 0x80) == 0) ? "0" : "1");
649 VERBOSE (DEBUG
, PRINTF ("bits: %d - %s\n", codlen (bits
), bits
));
651 /* look for correct code */
653 for (k
= 0; (k
< NB_BYTES
) && (!is_found
); k
++) {
654 if (codcmp ((char *)(codes
+ k
), bits
) == 0) {
656 VERBOSE (DEBUG
, PRINTF ("found: %d\n", k
));
659 if (pt
- bufout
== BUFFER_SIZE
- 1) {
660 VERBOSE (DEBUG
, PRINTF ("nb buffer out: %u\n", (pt
- bufout
)));
661 fwrite (bufout
, 1, BUFFER_SIZE
, fout
);
668 if ((i
== nb
- 1) && (l
% 256 == rem
) && (feof (fin
))) {
669 VERBOSE (DEBUG
, PRINTF ("break\n"));
676 VERBOSE (DEBUG
, PRINTF ("nb buffer out: %u\n", (pt
- bufout
)));
677 fwrite (bufout
, 1, pt
- bufout
, fout
);
684 VERBOSE (DEBUG
, PRINTF ("end writing decompressed file\n"));
691 int main (int argc
, char *argv
[])
696 leaf_t
**leafs
= NULL
;
698 code_t
*codes
= NULL
;
699 byte_t
*header
= NULL
;
707 VERBOSE (DEBUG
, PRINTF ("start processing arguments\n"));
711 _fprintf (stderr
, "%s: invalid option -- %s\n", progname
, arg
);
715 VERBOSE (DEBUG
, PRINTF ("option: %c\n", c
));
724 input
= (arg
[2]) ? arg
+ 2 : (--argc
> 0 ) ? *(++argv
) : NULL
;
725 VERBOSE (DEBUG
, PRINTF ("input: %s\n", input
));
728 output
= (arg
[2]) ? arg
+ 2 : (--argc
> 0 ) ? *(++argv
) : NULL
;
729 VERBOSE (DEBUG
, PRINTF ("output: %s\n", output
));
732 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0 ) ? *(++argv
) : NULL
;
734 _fprintf (stderr
, "%s: missing verbose level\n", progname
);
737 verbose
= atoi (arg
);
738 VERBOSE (INFO
, _fprintf (stdout
, "verbose: %d\n", verbose
));
745 if ((input
== NULL
) || (output
== NULL
)) {
746 _fprintf (stderr
, "%s: missing file\n", progname
);
749 VERBOSE (DEBUG
, PRINTF ("end processing arguments\n"));
753 table
= create_table (input
);
754 if (table
== NULL
) break;
755 VERBOSE (INFO
, print_occ_table (table
));
757 leafs
= init_forest (table
);
758 if (leafs
== NULL
) break;
759 root
= create_tree (leafs
);
760 if (root
== NULL
) break;
761 codes
= create_code (root
);
762 if (codes
== NULL
) break;
763 VERBOSE (INFO
, print_code_table (codes
));
764 header
= encode_header_table (codes
, table
);
765 if (header
== NULL
) break;
766 VERBOSE (INFO
, print_header (header
));
767 rc
= write_compress (output
, input
, codes
, header
);
770 codes
= read_header (input
);
771 if (codes
== NULL
) break;
772 VERBOSE (INFO
, print_code_table (codes
));
773 rc
= write_decompress (output
, input
, codes
);
777 /* clean everything */
779 if (header
) free (header
);
780 if (codes
) free (codes
);
781 if (root
) free_tree (root
);
782 if (leafs
) free (leafs
);
783 if (table
) free (table
);
788 // test: compress.exe -h
789 // test: compress.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
790 // test: compress.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
791 // test: compress.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
792 // test: compress.exe -c -i compress.c -o compress.mz
793 // test: ls -sS1 compress.c compress.mz | tail -1 | grep compress.mz
794 // test: compress.exe -d -i compress.mz -o tmp.c
795 // test: cmp compress.c tmp.c
796 // test: rm compress.mz tmp.c
798 /* vim: set ts=4 sw=4 et */