8cfce314cc13aba7e4e47c94da1c49ee52c7fa97
3 /* linker: code.o debug.o */
12 #define BUFFER_SIZE 4096
18 char *progname
= NULL
;
24 FILE *fd
= ret
? stderr
: stdout
;
25 fprintf (fd
, "usage: %s\n", progname
);
26 fprintf (fd
, " -h : help message\n");
27 fprintf (fd
, " -i <file>: input file\n");
28 fprintf (fd
, " -o <file>: output file\n");
29 fprintf (fd
, " -v : verbose level (%d)\n", verbose
);
34 void blkcpy (void *dst
, const void *src
, int len
)
37 *((char *)dst
++) = *((char *)src
++);
41 /* create occurence table */
43 int *create_table (char *filename
)
45 byte_t buffer
[BUFFER_SIZE
] = {0};
50 VERBOSE (DEBUG
, PRINTF ("start creating occurence table\n"));
52 /* memory allocation */
53 table
= (int *) calloc (NB_BYTES
, sizeof (int));
55 VERBOSE (ERROR
, printf ("can't allocate memory\n"));
58 VERBOSE (INFO
, printf ("memory allocated\n"));
61 fid
= fopen (filename
, "rb");
63 VERBOSE (ERROR
, printf ("can't open file '%s'\n", filename
));
67 VERBOSE (INFO
, printf ("file '%s' opened\n", filename
));
71 nbread
= fread (buffer
, 1, BUFFER_SIZE
, fid
);
72 VERBOSE (DEBUG
, PRINTF ("nbread: %d\n", nbread
));
74 table
[(int)buffer
[nbread
]]++;
81 VERBOSE (DEBUG
, PRINTF ("end creating occurence table\n"));
86 /* print occurence table */
88 void print_occ_table (int *table
)
92 printf ("Occurence table\n");
93 for (i
= 0; i
< NB_BYTES
; i
++) {
95 printf ("0x%02x '%c': %d\n", i
, ((i
< 32) || (i
> 127)) ? '.' : i
, table
[i
]);
102 typedef struct _leaf_t
104 struct _leaf_t
*left
;
105 struct _leaf_t
*right
;
110 /* initialize forest */
112 leaf_t
**init_forest (int *table
)
114 leaf_t
**leafs
= NULL
;
118 VERBOSE (DEBUG
, PRINTF ("start initiliazing forest\n"));
120 /* count number of leafs */
121 for (i
= 0; i
< NB_BYTES
; i
++) {
127 /* allocate memory */
128 leafs
= (leaf_t
**) calloc (nb_leafs
+ 1, sizeof (leaf_t
*));
130 VERBOSE (ERROR
, printf ("can't allocate memory\n"));
134 /* initialize leafs */
135 for (i
= 0, l
= 0; i
< NB_BYTES
; i
++) {
137 leafs
[l
] = (leaf_t
*) calloc (1, sizeof (leaf_t
));
138 if (leafs
[l
] == NULL
) {
139 VERBOSE (ERROR
, printf ("can't allocate memory\n"));
142 leafs
[l
]->occ
= table
[i
];
148 VERBOSE (DEBUG
, PRINTF ("end initiliazing forest\n"));
155 leaf_t
*create_tree (leaf_t
**leafs
)
157 leaf_t
*branch
= NULL
;
163 VERBOSE (DEBUG
, PRINTF ("start creating tree\n"));
165 /* count number of leafs */
166 while (leafs
[nb_leafs
] != NULL
) {
171 for (j
= 0; j
< nb_leafs
- 1; j
++) {
173 /* look for leatest occurence */
175 for (i
= 0; i
< nb_leafs
; i
++) {
176 if (leafs
[i
] == NULL
) {
179 if ((last
== -1) || (leafs
[i
]->occ
< leafs
[last
]->occ
)) {
184 /* look for ante leatest occurence */
186 for (i
= 0; i
< nb_leafs
; i
++) {
187 if ((i
== last
) || (leafs
[i
] == NULL
)) {
190 if ((ante
== -1) || (leafs
[i
]->occ
< leafs
[ante
]->occ
)) {
196 if ((last
== -1) || (ante
== -1)) {
197 VERBOSE (ERROR
, printf ("error during tree building\n"));
200 branch
= (leaf_t
*) calloc (1, sizeof (leaf_t
));
201 if (branch
== NULL
) {
202 VERBOSE (ERROR
, printf ("can't allocate memory\n"));
205 branch
->left
= leafs
[last
];
206 branch
->right
= leafs
[ante
];
207 branch
->occ
= branch
->left
->occ
+ branch
->right
->occ
;
208 leafs
[last
] = branch
;
212 VERBOSE (DEBUG
, PRINTF ("end creating tree\n"));
214 return (last
!= -1) ? leafs
[last
] : NULL
;
219 void free_tree (leaf_t
*root
) {
222 free_tree (root
->left
);
225 free_tree (root
->right
);
233 void explore_tree (code_t
*table
, leaf_t
*root
, char *code
, int index
)
235 if ((root
->left
== NULL
) && (root
->right
== NULL
)) {
236 codcpy ((char *)(table
+ (int)(root
->c
)), sizeof (code_t
), code
);
239 codcpy (code
+ index
, sizeof (code_t
), "1");
240 explore_tree (table
, root
->left
, code
, index
+ 1);
241 codcpy (code
+ index
, sizeof (code_t
), "0");
242 explore_tree (table
, root
->right
, code
, index
+ 1);
246 /* create code table */
248 code_t
*create_code (leaf_t
*root
)
250 code_t
*table
= NULL
;
253 VERBOSE (DEBUG
, PRINTF ("start creating code table\n"));
256 table
= (code_t
*) calloc (NB_BYTES
, sizeof (code_t
));
258 VERBOSE (ERROR
, printf ("can't allocate memory\n"));
262 explore_tree (table
, root
, (char *)&code
, 0);
264 VERBOSE (DEBUG
, PRINTF ("end creating code table\n"));
269 /* print code table */
271 void print_code_table (code_t
*codes
)
276 printf ("Code table\n");
277 for (i
= 0; i
< NB_BYTES
; i
++) {
278 code
= (char *)(codes
+ i
);
279 if (codlen (code
) == 0) {
282 printf ("0x%02x '%c': %s\n", i
, ((i
< 32) || (i
> 127)) ? '.' : i
, code
);
286 /* encode header and code table */
288 byte_t
*encode_header_table (code_t
*codes
, int *occ
)
290 byte_t buffer
[NB_BYTES
* (NB_BYTES
- 1) / 2 / 8 + NB_BYTES
+ 6] = {0};
291 char bits
[(NB_BYTES
- 1) + 8 + 1] = {0};
293 byte_t
*header
= buffer
;
294 int i
, j
, length
, mode
;
298 VERBOSE (DEBUG
, PRINTF ("start encoding header and code table\n"));
301 for (i
= 0; i
< NB_BYTES
; i
++) {
302 code
= (char *)(codes
+ i
);
303 if (codlen (code
) > 0) {
305 size
+= codlen (code
) * occ
[i
];
308 mode
= (NB_BYTES
< 2 * nb
+ 1) ? 1 : 2;
309 VERBOSE (DEBUG
, PRINTF ("nb chars: %d\n", nb
));
310 VERBOSE (DEBUG
, PRINTF ("mode: %d\n", mode
));
311 VERBOSE (DEBUG
, PRINTF ("size: %d\n", size
));
312 VERBOSE (DEBUG
, PRINTF ("rem: %d\n", size
% 256));
315 codcpy ((char *)header
, sizeof (buffer
), (mode
== 1) ? "MZ1 " : "MZ2 ");
321 for (i
= 0; i
< NB_BYTES
; i
++) {
322 code
= (char *)(codes
+ i
);
323 *(header
++) = (byte_t
) codlen (code
);
327 *(header
++) = (byte_t
)(nb
- 1);
328 for (i
= 0; i
< NB_BYTES
; i
++) {
329 code
= (char *)(codes
+ i
);
330 if (codlen (code
) > 0) {
331 *(header
++) = (byte_t
) i
;
332 *(header
++) = (byte_t
) codlen (code
);
339 for (i
= 0; i
< NB_BYTES
; i
++) {
340 code
= (char *)(codes
+ i
);
341 if (codlen (code
) > 0) {
342 codcat (bits
, sizeof (code_t
), code
);
343 while (codlen (bits
) > (8 - 1)) {
344 for (j
= 0; j
< 8; j
++) {
346 if (bits
[j
] == '1') {
350 codcpy (bits
, sizeof (code_t
), bits
+ 8);
355 if (codlen (bits
) > 0) {
356 for (j
= 0; j
< (int)codlen (bits
); j
++) {
358 if (bits
[j
] == '1') {
362 for (j
= (int)codlen (bits
); j
< 8; j
++) {
369 length
= (int)(header
- buffer
- 6);
370 VERBOSE (DEBUG
, PRINTF ("lengh: %d %02x %02x\n", length
, length
>> 8, length
& 0xff));
371 buffer
[3] = (byte_t
)(length
>> 8);
372 buffer
[4] = (byte_t
)(length
& 0xff);
373 buffer
[5] = (byte_t
)(size
% 256);
376 header
= (byte_t
*) calloc (length
+ 6, 1);
377 blkcpy (header
, buffer
, length
+ 6);
379 VERBOSE (DEBUG
, PRINTF ("end encoding header and code table\n"));
386 void print_header (byte_t
*header
)
390 length
= (header
[3] << 8) + header
[4];
391 VERBOSE (DEBUG
, PRINTF ("lengh: %d\n", length
));
392 for (i
= 0; i
< length
+ 6; i
++) {
393 printf ("%02x", header
[i
]);
398 /* write crompressed file */
400 int write_compress (char *output
, char *input
, code_t
*codes
, byte_t
*header
)
402 byte_t bufin
[BUFFER_SIZE
] = {0};
403 byte_t bufout
[BUFFER_SIZE
] = {0};
404 char bits
[(NB_BYTES
- 1) + 8 + 1] = {0};
410 VERBOSE (DEBUG
, PRINTF ("start writting compressed file\n"));
412 /* open input file */
413 fin
= fopen (input
, "rb");
415 VERBOSE (ERROR
, printf ("can't open file '%s' for reading\n", input
));
418 VERBOSE (INFO
, printf ("file '%s' opened\n", input
));
420 /* open output file */
421 fout
= fopen (output
, "wb");
423 VERBOSE (ERROR
, printf ("can't open file '%s' for writing\n", output
));
426 VERBOSE (INFO
, printf ("file '%s' opened\n", output
));
429 length
= (header
[3] << 8) + header
[4];
430 VERBOSE (DEBUG
, PRINTF ("lengh: %d\n", length
));
431 fwrite (header
, 1, length
+ 6, fout
);
435 while (!feof (fin
)) {
436 nbread
= fread (bufin
, 1, BUFFER_SIZE
, fin
);
437 VERBOSE (DEBUG
, PRINTF ("nbread: %d\n", nbread
));
438 for (i
= 0; i
< nbread
; i
++) {
439 codcat (bits
, sizeof (code_t
), (char *)(codes
+ bufin
[i
]));
440 while (codlen (bits
) > (8 - 1)) {
441 for (j
= 0; j
< 8; j
++) {
443 if (bits
[j
] == '1') {
447 codcpy (bits
, sizeof (code_t
), bits
+ 8);
448 if (pt
- bufout
== BUFFER_SIZE
- 1) {
449 fwrite (bufout
, 1, BUFFER_SIZE
, fout
);
457 VERBOSE (DEBUG
, PRINTF ("lastest bits : %d\n", codlen (bits
)));
458 if (codlen (bits
) > 0) {
459 for (j
= 0; j
< (int)codlen (bits
); j
++) {
461 if (bits
[j
] == '1') {
465 for (j
= (int)codlen (bits
); j
< 8; j
++) {
471 VERBOSE (DEBUG
, PRINTF ("last partial buffer written: %u\n", pt
- bufout
));
472 fwrite (bufout
, 1, pt
- bufout
, fout
);
479 VERBOSE (DEBUG
, PRINTF ("end writting compressed file\n"));
486 code_t
*read_header (char *filename
) {
487 byte_t buffer
[NB_BYTES
* (NB_BYTES
- 1) / 2 / 8 + NB_BYTES
+ 6] = {0};
488 code_t
*table
= NULL
;
489 byte_t
*codes
= NULL
;
491 int lengths
[NB_BYTES
] = {0};
494 size_t i
, j
, l
, nb
, size
;
496 VERBOSE (DEBUG
, PRINTF ("start reading header\n"));
499 fid
= fopen (filename
, "rb");
501 VERBOSE (ERROR
, printf ("can't open file '%s'\n", filename
));
504 VERBOSE (INFO
, printf ("file '%s' opened\n", filename
));
506 /* read magic number */
507 nb
= fread (buffer
, 1, 6, fid
);
508 VERBOSE (DEBUG
, PRINTF ("nb, buffer: %d 0x%02x 0x%02x\n", nb
, buffer
[0], buffer
[1]));
509 if ((nb
== 6) && (buffer
[0] == 'M') && (buffer
[1] == 'Z')) {
510 mode
= (buffer
[2] == '1') ? 1 : (buffer
[2] == '2') ? 2 : 0;
511 size
= (buffer
[3] << 8) + buffer
[4];
512 VERBOSE (DEBUG
, PRINTF ("mode, size: %d %d\n", mode
, size
));
513 if (size
> NB_BYTES
* (NB_BYTES
- 1) / 2 / 8 + NB_BYTES
) {
516 nb
= fread (buffer
, 1, size
, fid
);
517 VERBOSE (DEBUG
, PRINTF ("nb read: %d\n", nb
));
525 VERBOSE (ERROR
, printf ("incorrect file\n"));
533 for (i
= 0; i
< NB_BYTES
; i
++) {
534 lengths
[i
] = *(codes
++);
539 VERBOSE (DEBUG
, PRINTF ("nb codes: %d\n", nb
));
540 for (i
= 0; i
< nb
; i
++) {
542 lengths
[j
] = *(codes
++);
546 VERBOSE (DEBUG
, for (i
= 0; i
< NB_BYTES
; i
++) if (lengths
[i
]) PRINTF ("%d: %d\n", i
, lengths
[i
]));
549 for (i
= 0, l
= 0; i
< NB_BYTES
; i
++) {
552 if (((mode
== 1) && (size
- 256 != (l
+ 7) / 8)) ||
553 ((mode
== 2) && (size
- 2 * nb
- 1 != (l
+ 7) / 8))) {
554 VERBOSE (ERROR
, printf ("incorrect code table length\n"));
559 table
= (code_t
*) calloc (NB_BYTES
, sizeof (code_t
));
561 VERBOSE (ERROR
, printf ("can't allocate memory\n"));
568 for (i
= 0; i
< NB_BYTES
; i
++) {
569 if (lengths
[i
] == 0) {
572 while (lengths
[i
]--) {
573 codcat ((char *)(table
+ i
), sizeof (code_t
), ((cur
& 0x80) == 0) ? "0" : "1");
583 VERBOSE (DEBUG
, PRINTF ("end reading header\n"));
588 /* write decompressed file */
590 int write_decompress (char *output
, char *input
, code_t
*codes
)
592 byte_t bufin
[BUFFER_SIZE
] = {0};
593 byte_t bufout
[BUFFER_SIZE
] = {0};
594 byte_t bufhea
[MAX(NB_BYTES
* (NB_BYTES
- 1) / 2 / 8 + NB_BYTES
+ 6, BUFFER_SIZE
)] = {0};
595 char bits
[(NB_BYTES
- 1) + 1] = {0};
597 int i
, j
, k
, nb
, size
, rem
;
602 VERBOSE (DEBUG
, PRINTF ("start writing decompressed file\n"));
604 /* open file for reading */
605 fin
= fopen (input
, "rb");
607 VERBOSE (ERROR
, printf ("can't open file '%s' for reading\n", input
));
610 VERBOSE (INFO
, printf ("file '%s' opened\n", input
));
612 /* read magic number */
613 nb
= fread (bufhea
, 1, 6, fin
);
615 VERBOSE (ERROR
, printf ("can't read file\n"));
619 size
= (bufhea
[3] << 8) + bufhea
[4];
620 VERBOSE (DEBUG
, printf ("table size: %d\n", size
));
622 VERBOSE (DEBUG
, printf ("remainder: %d\n", rem
));
623 nb
= fread (bufhea
, 1, size
, fin
);
625 VERBOSE (ERROR
, printf ("can't read file\n"));
630 /* open file for writing */
631 fout
= fopen (output
, "wb");
633 VERBOSE (ERROR
, printf ("can't open file '%s' for writing\n", output
));
636 VERBOSE (INFO
, printf ("file '%s' opened\n", output
));
640 while (!feof (fin
)) {
641 nb
= fread (bufin
, 1, BUFFER_SIZE
, fin
);
642 VERBOSE (DEBUG
, PRINTF ("nbread: %d\n", nb
));
643 for (i
= 0; i
< nb
; i
++) {
644 for (j
= 0; j
< 8; j
++) {
645 codcat (bits
, sizeof (bits
), ((bufin
[i
] & 0x80) == 0) ? "0" : "1");
648 VERBOSE (DEBUG
, PRINTF ("bits: %d - %s\n", codlen (bits
), bits
));
650 /* look for correct code */
652 for (k
= 0; (k
< NB_BYTES
) && (!is_found
); k
++) {
653 if (codcmp ((char *)(codes
+ k
), bits
) == 0) {
655 VERBOSE (DEBUG
, PRINTF ("found: %d\n", k
));
658 if (pt
- bufout
== BUFFER_SIZE
- 1) {
659 VERBOSE (DEBUG
, PRINTF ("nb buffer out: %u\n", (pt
- bufout
)));
660 fwrite (bufout
, 1, BUFFER_SIZE
, fout
);
667 if ((i
== nb
- 1) && (l
% 256 == rem
) && (feof (fin
))) {
668 VERBOSE (DEBUG
, PRINTF ("break\n"));
675 VERBOSE (DEBUG
, PRINTF ("nb buffer out: %u\n", (pt
- bufout
)));
676 fwrite (bufout
, 1, pt
- bufout
, fout
);
683 VERBOSE (DEBUG
, PRINTF ("end writing decompressed file\n"));
690 int main (int argc
, char *argv
[])
695 leaf_t
**leafs
= NULL
;
697 code_t
*codes
= NULL
;
698 byte_t
*header
= NULL
;
706 VERBOSE (DEBUG
, PRINTF ("start processing arguments\n"));
710 fprintf (stderr
, "%s: invalid option -- %s\n", progname
, arg
);
714 VERBOSE (DEBUG
, PRINTF ("option: %c\n", c
));
723 input
= (arg
[2]) ? arg
+ 2 : (--argc
> 0 ) ? *(++argv
) : NULL
;
724 VERBOSE (DEBUG
, PRINTF ("input: %s\n", input
));
727 output
= (arg
[2]) ? arg
+ 2 : (--argc
> 0 ) ? *(++argv
) : NULL
;
728 VERBOSE (DEBUG
, PRINTF ("output: %s\n", output
));
731 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0 ) ? *(++argv
) : NULL
;
733 fprintf (stderr
, "%s: missing verbose level\n", progname
);
736 verbose
= atoi (arg
);
737 VERBOSE (INFO
, printf ("verbose: %d\n", verbose
));
744 if ((input
== NULL
) || (output
== NULL
)) {
745 fprintf (stderr
, "%s: missing file\n", progname
);
748 VERBOSE (DEBUG
, PRINTF ("end processing arguments\n"));
752 table
= create_table (input
);
753 if (table
== NULL
) break;
754 VERBOSE (INFO
, print_occ_table (table
));
756 leafs
= init_forest (table
);
757 if (leafs
== NULL
) break;
758 root
= create_tree (leafs
);
759 if (root
== NULL
) break;
760 codes
= create_code (root
);
761 if (codes
== NULL
) break;
762 VERBOSE (INFO
, print_code_table (codes
));
763 header
= encode_header_table (codes
, table
);
764 if (header
== NULL
) break;
765 VERBOSE (INFO
, print_header (header
));
766 rc
= write_compress (output
, input
, codes
, header
);
769 codes
= read_header (input
);
770 if (codes
== NULL
) break;
771 VERBOSE (INFO
, print_code_table (codes
));
772 rc
= write_decompress (output
, input
, codes
);
776 /* clean everything */
778 if (header
) free (header
);
779 if (codes
) free (codes
);
780 if (root
) free_tree (root
);
781 if (leafs
) free (leafs
);
782 if (table
) free (table
);
787 // test: compress.exe -h
788 // test: compress.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
789 // test: compress.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
790 // test: compress.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
791 // test: compress.exe -c -i compress.c -o compress.mz
792 // test: ls -sS1 compress.c compress.mz | tail -1 | grep compress.mz
793 // test: compress.exe -d -i compress.mz -o tmp.c
794 // test: cmp compress.c tmp.c
795 // test: rm compress.mz tmp.c