Debian Codegraph @

compression/huffman/hzip.c

raw symbols SCIP
1 /* hzip.c */cxx . . $ `<file>/compression/huffman/hzip.c`/
2 /*
3 | Routines designed to be used as a pilot experiment for compression
4 | of tablebases. Not really optimized, but they are supposed to work
5 | --Miguel A. Ballicora
6 */
7
8 /*
9 This Software is distributed with the following X11 License,
10 sometimes also known as MIT license.
11
12 Copyright (c) 2010 Miguel A. Ballicora
13
14 Permission is hereby granted, free of charge, to any person
15 obtaining a copy of this software and associated documentation
16 files (the "Software"), to deal in the Software without
17 restriction, including without limitation the rights to use,
18 copy, modify, merge, publish, distribute, sublicense, and/or sell
19 copies of the Software, and to permit persons to whom the
20 Software is furnished to do so, subject to the following
21 conditions:
22
23 The above copyright notice and this permission notice shall be
24 included in all copies or substantial portions of the Software.
25
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
28 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
30 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
31 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 OTHER DEALINGS IN THE SOFTWARE.
34 */
35
36 #include "hzip.h"cxx . . $ `<file>/compression/huffman/hzip.h`/
37
38 /*-------------------------------------------------------------------*\
39 |
40 | Huffman coding compression
41 |
42 \*-------------------------------------------------------------------*/
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <assert.h>
48
49 #define MAXDIVERSITYcxx . . $ `compression/huffman/hzip.c:49:9`! (256)
50 #define MAXHEAPcxx . . $ `compression/huffman/hzip.c:50:9`! (MAXDIVERSITYcxx . . $ `compression/huffman/hzip.c:49:9`!+1)
51 #define MAXSTREAMcxx . . $ `compression/huffman/hzip.c:51:9`! (1<<18)
52 #define MAXHUFFcxx . . $ `compression/huffman/hzip.c:52:9`! (2*MAXDIVERSITYcxx . . $ `compression/huffman/hzip.c:49:9`!)
53
54 typedef int bool_tcxx . . $ bool_t#;
55
56 #define TRUEcxx . . $ `compression/huffman/hzip.c:56:9`! 1
57 #define FALSEcxx . . $ `compression/huffman/hzip.c:57:9`! 0;
58
59 /* huffman tree */
60 struct huffcxx . . $ huff# {
61 int freqcxx . . $ huff#freq.;
62 int valuecxx . . $ huff#value.;
63 int pleftcxx . . $ huff#pleft.;
64 int prightcxx . . $ huff#pright.;
65 bool_tcxx . . $ bool_t# isleafcxx . . $ huff#isleaf.;
66 };
67
68 static int huff_endcxx . . $ huff_end.;
69 static struct huffcxx . . $ huff# hufftreecxx . . $ hufftree.[MAXHUFFcxx . . $ `compression/huffman/hzip.c:52:9`!];
70
71 /* heap */
72 struct elementcxx . . $ element# {
73 int freqcxx . . $ element#freq.;
74 int huffidxcxx . . $ element#huffidx.;
75 };
76
77 static int heap_endcxx . . $ heap_end.;
78 static struct elementcxx . . $ element# heapcxx . . $ heap.[MAXHEAPcxx . . $ `compression/huffman/hzip.c:50:9`!];
79
80 unsigned char streambuffercxx . . $ streambuffer.[MAXSTREAMcxx . . $ `compression/huffman/hzip.c:51:9`!];
81
82 /* stream */
83 struct STREAMcxx . . $ STREAM# {
84 unsigned long pbitcxx . . $ STREAM#pbit.;
85 unsigned char *xcxx . . $ STREAM#x.;
86 };
87
88 typedef struct STREAMcxx . . $ STREAM# stream_tcxx . . $ stream_t#;
89
90 /* read only */
91 struct RO_STREAMcxx . . $ RO_STREAM# {
92 unsigned long pbitcxx . . $ RO_STREAM#pbit.;
93 const unsigned char *xcxx . . $ RO_STREAM#x.;
94 };
95
96 typedef struct RO_STREAMcxx . . $ RO_STREAM# ro_stream_tcxx . . $ ro_stream_t#;
97
98 /*
99 |
100 | VARIABLES
101 |
102 \*---------------------------*/
103
104 static int freqcxx . . $ freq.[MAXDIVERSITYcxx . . $ `compression/huffman/hzip.c:49:9`!];
105 static unsigned code_tablecxx . . $ code_table.[MAXDIVERSITYcxx . . $ `compression/huffman/hzip.c:49:9`!];
106 static unsigned size_tablecxx . . $ size_table.[MAXDIVERSITYcxx . . $ `compression/huffman/hzip.c:49:9`!];
107 static stream_tcxx . . $ stream_t# Streamcxx . . $ Stream. = {0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!};
108 static ro_stream_tcxx . . $ ro_stream_t# RO_Streamcxx . . $ RO_Stream. = {0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!};
109 static const unsigned int VALUEBITScxx . . $ VALUEBITS. = 8u;
110
111 /*==== PROTOTYPES======================================*/
112
113
114 /* heap */
115
116 static void freq_initcxx . . $ freq_init(1cebb3dd0b1c9c51). (const unsigned char *in, size_tcxx . . $ size_t# max);
117 static void heap_initcxx . . $ heap_init(49f6e7a06ebc5aa8). (void);
118 static void heap_appendcxx . . $ heap_append(4bfb29fe608e0bf2). (struct elementcxx . . $ element# e);
119 static void heap_sift_upcxx . . $ heap_sift_up(d4f767463ce0a6b3). (int x);
120 static void heap_adjust_downcxx . . $ heap_adjust_down(3e454b8ccf442868). (int top, int last);
121
122
123 /* hufftree */
124
125 static int hufftree_from_freqcxx . . $ hufftree_from_freq(b126dc7c1de90089). (void);
126 static int hufftree_from_heapcxx . . $ hufftree_from_heap(b126dc7c1de90089). (void);
127 static void hufftree_to_codescxx . . $ hufftree_to_codes(f59d17f8b9f2d3e1). (int start, int n, unsigned code);
128 static void hufftree_resetcxx . . $ hufftree_reset(49f6e7a06ebc5aa8). (void);
129 static int hufftree_frombitscxx . . $ hufftree_frombits(4ab50908037b7de2). (ro_stream_tcxx . . $ ro_stream_t# *stream, bool_tcxx . . $ bool_t# *pok);
130 static void hufftree_tobitscxx . . $ hufftree_tobits(abff3b7bf7cc5fdc). (int thisnode, stream_tcxx . . $ stream_t# *stream);
131 static unsigned int hufftree_readstreamcxx . . $ hufftree_readstream(94d630985062c180). (int root, ro_stream_tcxx . . $ ro_stream_t# *s);
132
133 /* stream */
134
135 /* read only */
136 static void ro_stream_rewindcxx . . $ ro_stream_rewind(47b426539a174f77). (ro_stream_tcxx . . $ ro_stream_t# *s);
137 static void ro_stream_initcxx . . $ ro_stream_init(bee1ab8ae1fbdce7). (ro_stream_tcxx . . $ ro_stream_t# *s, const unsigned char *buffer);
138 static void ro_stream_donecxx . . $ ro_stream_done(47b426539a174f77). (ro_stream_tcxx . . $ ro_stream_t# *s);
139
140 /* read and write */
141 static void stream_clearcxx . . $ stream_clear(f9c7c5c9afde3300). (stream_tcxx . . $ stream_t# *s);
142 static void stream_initcxx . . $ stream_init(d92cfaed751b77b7). (stream_tcxx . . $ stream_t# *s, unsigned char *buffer);
143 static void stream_donecxx . . $ stream_done(f9c7c5c9afde3300). (stream_tcxx . . $ stream_t# *s);
144 static size_tcxx . . $ size_t# stream_lencxx . . $ stream_len(22cbbdb37de59cc0). (stream_tcxx . . $ stream_t# *s);
145
146 static void stream_rewindcxx . . $ stream_rewind(f9c7c5c9afde3300). (stream_tcxx . . $ stream_t# *s);
147 static unsigned int stream_nextbitcxx . . $ stream_nextbit(2d47deecc3be6973). (ro_stream_tcxx . . $ ro_stream_t# *s);
148 static unsigned int stream_nextbit_ncxx . . $ stream_nextbit_n(59e1778e378c3f1b). (ro_stream_tcxx . . $ ro_stream_t# *s, unsigned int width);
149 static void stream_writebitcxx . . $ stream_writebit(6a28a5ef0f848c65). (stream_tcxx . . $ stream_t# *s, unsigned z);
150 static void stream_write_ncxx . . $ stream_write_n(ed67174fd7e844eb). (unsigned code, unsigned width, stream_tcxx . . $ stream_t# *s);
151
152
153 static bool_tcxx . . $ bool_t# decode_from_streamcxx . . $ decode_from_stream(b6c4ebb398656412). (ro_stream_tcxx . . $ ro_stream_t# *stream, size_tcxx . . $ size_t# n, unsigned char *out);
154 static void encode_to_streamcxx . . $ encode_to_stream(d7d712f6ba46c16e). (const unsigned char *in, size_tcxx . . $ size_t# inlen, stream_tcxx . . $ stream_t# *stream);
155
156 /*static unsigned int stream_next8 (stream_t *s);*/
157 /*static void stream_write8 (stream_t *s, unsigned z);*/
158
159 /* supporting functions */
160
161 /*
162 static void heap_plot (void);
163 static int fill_block(unsigned char *out);
164 static char *binstream(unsigned int x, int n);
165 static void stream_print(stream_t *s, int n);
166 static void stream_printnext (stream_t *s, int n);
167 static void stream_dump (stream_t *s, int ori, int n);
168 static void freq_report (void);
169 */
170
171 /*=== ENCODE/DECODE=================================================*/
172
173 size_tcxx . . $ size_t# TB_hzip_unusedcxx . . $ TB_hzip_unused.;
174
175 static int
176 huffman_decodecxx . . $ huffman_decode(c7d822ce9856131c). (size_tcxx . . $ size_t# z, const unsigned char *bz, size_tcxx . . $ size_t# n, unsigned char *bp)
177 /* bz:buffer huffman zipped to bp:buffer decoded */
178 {
179 bool_tcxx . . $ bool_t# ok;
180 TB_hzip_unusedcxx . . $ TB_hzip_unused. = z; /* to silence compiler */
181 ro_stream_initcxx . . $ ro_stream_init(bee1ab8ae1fbdce7). (&RO_Streamcxx . . $ RO_Stream., bz);
182 ok = decode_from_streamcxx . . $ decode_from_stream(b6c4ebb398656412). (&RO_Streamcxx . . $ RO_Stream., n, bp);
183 ro_stream_donecxx . . $ ro_stream_done(47b426539a174f77). (&RO_Streamcxx . . $ RO_Stream.);
184 return ok;
185 }
186
187 static int
188 huffman_encodecxx . . $ huffman_encode(8390a9752bc51574). (size_tcxx . . $ size_t# n, const unsigned char *bp, size_tcxx . . $ size_t# *z, unsigned char *bz)
189 /* bz:buffer huffman zipped to bp:buffer decoded */
190 {
191 size_tcxx . . $ size_t# i, zz;
192 stream_initcxx . . $ stream_init(d92cfaed751b77b7). (&Streamcxx . . $ Stream., streambuffercxx . . $ streambuffer.);
193 encode_to_streamcxx . . $ encode_to_stream(d7d712f6ba46c16e). (bp, n, &Streamcxx . . $ Stream.);
194 zz = stream_lencxx . . $ stream_len(22cbbdb37de59cc0). (&Streamcxx . . $ Stream.);
195 for (i = 0; i < zz; i++) {
196 bz[i] = Streamcxx . . $ Stream..xcxx . . $ STREAM#x.[i];
197 }
198 *z = zz;
199 stream_donecxx . . $ stream_done(f9c7c5c9afde3300). (&Streamcxx . . $ Stream.);
200 return TRUEcxx . . $ `compression/huffman/hzip.c:56:9`!;
201 }
202
203 extern int
204 huff_decodecxx . . $ huff_decode(8df7f9bf94206c40).
205 (const unsigned char *in_start, size_tcxx . . $ size_t# in_len, unsigned char *out_start, size_tcxx . . $ size_t# *pout_len, size_tcxx . . $ size_t# out_max)
206 {
207 size_tcxx . . $ size_t# n = (size_tcxx . . $ size_t#)in_start[0]
208 | ((size_tcxx . . $ size_t#)in_start[1] << 8)
209 | ((size_tcxx . . $ size_t#)in_start[2] << 16)
210 | ((size_tcxx . . $ size_t#)in_start[3] << 24);
211 TB_hzip_unusedcxx . . $ TB_hzip_unused. = out_max;
212 *pout_len = n;
213 return huffman_decodecxx . . $ huffman_decode(c7d822ce9856131c). (in_len-4, in_start+4, n, out_start);
214 }
215
216
217 extern int
218 huff_encodecxx . . $ huff_encode(8df7f9bf94206c40).
219 (const unsigned char *in_start, size_tcxx . . $ size_t# in_len, unsigned char *out_start, size_tcxx . . $ size_t# *pout_len, size_tcxx . . $ size_t# out_max)
220 {
221 bool_tcxx . . $ bool_t# ok;
222 size_tcxx . . $ size_t# hlen = 0;
223 TB_hzip_unusedcxx . . $ TB_hzip_unused. = out_max;
224 out_start[0] = (unsigned char) ((in_len ) & 0xffu);
225 out_start[1] = (unsigned char) ((in_len >> 8) & 0xffu);
226 out_start[2] = (unsigned char) ((in_len >> 16) & 0xffu);
227 out_start[3] = (unsigned char) ((in_len >> 24) & 0xffu);
228 ok = huffman_encodecxx . . $ huffman_encode(8390a9752bc51574). (in_len, in_start, &hlen, out_start+4);
229 *pout_len = hlen + 4;
230 return ok;
231 }
232
233
234 static bool_tcxx . . $ bool_t#
235 decode_from_streamcxx . . $ decode_from_stream(b6c4ebb398656412). (ro_stream_tcxx . . $ ro_stream_t# *s, size_tcxx . . $ size_t# n, unsigned char *out)
236 {
237 int root;
238 bool_tcxx . . $ bool_t# ok = TRUEcxx . . $ `compression/huffman/hzip.c:56:9`!; /* default */
239
240 hufftree_resetcxx . . $ hufftree_reset(49f6e7a06ebc5aa8). ();
241 ro_stream_rewindcxx . . $ ro_stream_rewind(47b426539a174f77). (s);
242 root = hufftree_frombitscxx . . $ hufftree_frombits(4ab50908037b7de2). (s, &ok);
243
244 if (ok) {
245 while (n-->0) {
246 *out++ = (unsigned char) hufftree_readstreamcxx . . $ hufftree_readstream(94d630985062c180). (root, s); /* cast to silence compiler */
247 }
248 }
249 return ok;
250 }
251
252
253 static void
254 encode_to_streamcxx . . $ encode_to_stream(d7d712f6ba46c16e). (const unsigned char *in, size_tcxx . . $ size_t# inlen, stream_tcxx . . $ stream_t# *stream)
255 {
256 size_tcxx . . $ size_t# i;
257 unsigned x, c, s;
258 int root;
259
260 stream_clearcxx . . $ stream_clear(f9c7c5c9afde3300). (&Streamcxx . . $ Stream.);
261 stream_rewindcxx . . $ stream_rewind(f9c7c5c9afde3300).(&Streamcxx . . $ Stream.);
262
263 /* pass to collect frequencies */
264 freq_initcxx . . $ freq_init(1cebb3dd0b1c9c51). (in, inlen);
265
266 /* frequency --> heap --> hufftrees */
267 root = hufftree_from_freqcxx . . $ hufftree_from_freq(b126dc7c1de90089).();
268
269 /* hufftree --> codes */
270 hufftree_to_codescxx . . $ hufftree_to_codes(f59d17f8b9f2d3e1). (root, 0, 0);
271
272 /* hufftrees --> stored in bits (stream) */
273 hufftree_tobitscxx . . $ hufftree_tobits(abff3b7bf7cc5fdc). (root, stream) ;
274
275 /* input + codes --> stored in bits (stream) */
276 for (i = 0; i < inlen; i++) {
277 x = in[i];
278 c = code_tablecxx . . $ code_table.[x];
279 s = size_tablecxx . . $ size_table.[x];
280 stream_write_ncxx . . $ stream_write_n(ed67174fd7e844eb). (c, s, stream);
281 }
282 return;
283 }
284
285 /*=== STREAM =================================================*/
286 /*
287 static char buffer[256];
288 */
289 /*
290 static char *
291 binstream(unsigned int x, int n)
292 {
293 char *s = buffer;
294 int i;
295
296 for (i = 0; i < n; i++) {
297 if (0!=(x&(1<<i))) {
298 s[i] = '1';
299 } else {
300 s[i] = '0';
301 }
302 }
303 s[i] = '\0';
304 return buffer;
305 }
306 */
307
308 /* READ ONLY */
309 static void ro_stream_rewindcxx . . $ ro_stream_rewind(47b426539a174f77). (ro_stream_tcxx . . $ ro_stream_t# *s) {s->pbitcxx . . $ RO_STREAM#pbit. = 0; return;}
310
311 static void
312 ro_stream_initcxx . . $ ro_stream_init(bee1ab8ae1fbdce7). (ro_stream_tcxx . . $ ro_stream_t# *s, const unsigned char *buffer)
313 {
314 s->xcxx . . $ RO_STREAM#x. = buffer;
315 s->pbitcxx . . $ RO_STREAM#pbit. = 0;
316 return;
317 }
318
319 static void
320 ro_stream_donecxx . . $ ro_stream_done(47b426539a174f77). (ro_stream_tcxx . . $ ro_stream_t# *s)
321 {
322 s->xcxx . . $ RO_STREAM#x. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
323 s->pbitcxx . . $ RO_STREAM#pbit. = 0;
324 return;
325 }
326
327
328 /* READ AND WRITE */
329 static void stream_rewindcxx . . $ stream_rewind(f9c7c5c9afde3300). (stream_tcxx . . $ stream_t# *s) {s->pbitcxx . . $ STREAM#pbit. = 0; return;}
330
331 static void
332 stream_initcxx . . $ stream_init(d92cfaed751b77b7). (stream_tcxx . . $ stream_t# *s, unsigned char *buffer)
333 {
334 s->xcxx . . $ STREAM#x. = buffer;
335 s->pbitcxx . . $ STREAM#pbit. = 0;
336 return;
337 }
338
339 static void
340 stream_donecxx . . $ stream_done(f9c7c5c9afde3300). (stream_tcxx . . $ stream_t# *s)
341 {
342 s->xcxx . . $ STREAM#x. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
343 s->pbitcxx . . $ STREAM#pbit. = 0;
344 return;
345 }
346
347 static void
348 stream_clearcxx . . $ stream_clear(f9c7c5c9afde3300). (stream_tcxx . . $ stream_t# *s)
349 { int i;
350 for (i = 0; i < MAXSTREAMcxx . . $ `compression/huffman/hzip.c:51:9`!; i++) {
351 s->xcxx . . $ STREAM#x.[i] = 0;
352 }
353 s->pbitcxx . . $ STREAM#pbit. = 0;
354 return;
355 }
356
357 static size_tcxx . . $ size_t#
358 stream_lencxx . . $ stream_len(22cbbdb37de59cc0). (stream_tcxx . . $ stream_t# *s)
359 {
360 return 1 + s->pbitcxx . . $ STREAM#pbit./8;
361 }
362
363 static unsigned int
364 stream_nextbitcxx . . $ stream_nextbit(2d47deecc3be6973). (ro_stream_tcxx . . $ ro_stream_t# *s)
365 {
366 unsigned long y, byte, bit;
367 y = s->pbitcxx . . $ RO_STREAM#pbit.++;
368 byte = y / 8;
369 bit = y & 7;
370 return 1u & (((unsigned)s->xcxx . . $ RO_STREAM#x.[byte]) >> bit);
371 }
372
373 static unsigned int
374 stream_nextbit_ncxx . . $ stream_nextbit_n(59e1778e378c3f1b). (ro_stream_tcxx . . $ ro_stream_t# *s, unsigned int width)
375 {
376 unsigned i;
377 unsigned x;
378 unsigned r = 0;
379 for (i = 0; i < width; i++) {
380 x = stream_nextbitcxx . . $ stream_nextbit(2d47deecc3be6973). (s);
381 r |= (x << i);
382 }
383 return r;
384 }
385
386 /*
387 static unsigned int
388 stream_next8 (stream_t *s)
389 {
390 unsigned a,b,y,byte,bit;
391 y = s->pbit;
392 s->pbit += 8;
393 byte = y / 8;
394 bit = y & 7;
395 a = 0xff & s->x[byte];
396 b = 0xff & s->x[byte+1];
397 return 0xff & ((a >> bit) | (b << (8-bit)));
398 }
399 */
400
401 #if 1
402 static void
403 stream_writebitcxx . . $ stream_writebit(6a28a5ef0f848c65). (stream_tcxx . . $ stream_t# *s, unsigned z)
404 {
405 unsigned long y,byte,bit;
406 y = s->pbitcxx . . $ STREAM#pbit.++;
407
408 byte = y / 8;
409 bit = y & 7;
410
411 /* s->x[byte] &= ~(1u << bit);*/
412 s->xcxx . . $ STREAM#x.[byte] = (unsigned char) (s->xcxx . . $ STREAM#x.[byte] | ((z&1u) << bit)); /* cast to silence compiler */
413 return;
414 }
415
416 #else
417 static void
418 stream_writebit (stream_t *s, unsigned z)
419 {
420 /* This function will write the next bit, 0 or 1 depending on z, and will clear
421 | the following bits (when bit == 0) or some future bytes
422 | Do not use for writing after random access
423 | It is only useful when this function is use for sequential writing on a
424 | empty buffer.
425 */
426 unsigned long y, byte, bit;
427 unsigned char *p;
428 y = s->pbit++;
429
430 byte = y / 8;
431 bit = y & 7;
432
433 p = &(s->x[byte]);
434
435 /* hack to clear the byte only when bit == 0, otherwise, it clears future bytes
436 | This will avoid clearing the whole buffer beforehand or doing
437 | *p &= (unsigned char)(~(1u << bit));
438 */
439 p[bit] = 0;
440
441 *p |= (unsigned char)(z&1u) << bit);
442
443 return;
444 }
445 #endif
446
447
448 /*
449 static void
450 stream_write8 (stream_t *s, unsigned z)
451 {
452 unsigned a,b,c,y,byte,bit;
453 y = s->pbit;
454 s->pbit += 8;
455 byte = y / 8;
456 bit = y & 7;
457 a = 0xff & s->x[byte];
458 b = 0xff & s->x[byte+1];
459 c = a | (b << 8);
460
461 c &= ~(0xff << bit);
462 c |= z << bit;
463
464 s->x[byte] = c & 0xff;
465 s->x[byte+1] = 0xff & (c >> 8);
466
467 return;
468 }
469 */
470
471 static void
472 stream_write_ncxx . . $ stream_write_n(ed67174fd7e844eb). (unsigned code, unsigned width, stream_tcxx . . $ stream_t# *s)
473 {
474 unsigned i;
475 for (i = 0; i < width; i++) {
476 stream_writebitcxx . . $ stream_writebit(6a28a5ef0f848c65). (s, 1u & (code >> i));
477 }
478 return;
479 }
480
481 /*
482 static void
483 stream_printnext (stream_t *s, int n)
484 {
485 int i; unsigned int x;
486 unsigned long int oldpos = s->pbit;
487
488 printf("\n");
489 for (i = 0; i < n; i++) {
490 if ((i & 7) == 0)
491 printf ("\n");
492 x = stream_next8 (s);
493 printf ("%s ", binstream(x,8) );
494 }
495 printf("\n");
496
497 s->pbit = oldpos;
498 return;
499 }
500 */
501
502 /*
503 static void
504 stream_dump (stream_t *s, int ori, int n)
505 {
506 int i; unsigned int x;
507 unsigned long int oldpos = s->pbit;
508
509 s->pbit = ori;
510
511 printf("\n");
512 for (i = 0; i < n; i++) {
513 if ((i & 7) == 0)
514 printf ("\n");
515 x = stream_next8 (s);
516 printf ("%s ", binstream(x,8) );
517 }
518 printf("\n");
519
520 s->pbit = oldpos;
521 return;
522 }
523 */
524
525 /*=== HUFFTREE=================================================*/
526
527 #define LEFTCODEcxx . . $ `compression/huffman/hzip.c:527:9`! 0u
528 #define RIGHTCODEcxx . . $ `compression/huffman/hzip.c:528:9`! 1u
529 #define BITLEAFcxx . . $ `compression/huffman/hzip.c:529:9`! 1
530 #define BITNODEcxx . . $ `compression/huffman/hzip.c:530:9`! 0
531
532 static void
533 hufftree_resetcxx . . $ hufftree_reset(49f6e7a06ebc5aa8). (void)
534 {
535 struct huffcxx . . $ huff# h;
536 int i;
537 for (i = 0; i < 2*MAXDIVERSITYcxx . . $ `compression/huffman/hzip.c:49:9`!; i++) {
538 h.isleafcxx . . $ huff#isleaf. = FALSEcxx . . $ `compression/huffman/hzip.c:57:9`!;
539 h.valuecxx . . $ huff#value. = 0;
540 h.freqcxx . . $ huff#freq. = 0;
541 h.pleftcxx . . $ huff#pleft. = 0;
542 h.prightcxx . . $ huff#pright. = 0;
543 hufftreecxx . . $ hufftree.[i] = h;
544 }
545 huff_endcxx . . $ huff_end. = 0;
546 }
547
548 static int
549 hufftree_from_heapcxx . . $ hufftree_from_heap(b126dc7c1de90089). (void)
550 {
551 int top, newidx /*, left, right, lesser */ ;
552 struct huffcxx . . $ huff# h;
553
554 for (;;)
555 {
556 if (heap_endcxx . . $ heap_end. == 2) { /* at least top element */
557 /* done */
558 break;
559 }
560
561 /* work at the top */
562 top = 1;
563 /*
564 left = 2*top;
565 right = left + 1;
566
567 lesser = left;
568 if (right < heap_end && (heap[right].freq < heap[left].freq))
569 lesser = right;
570 */
571 /* new huff node */
572 newidx = huff_endcxx . . $ huff_end.++;
573
574 h.isleafcxx . . $ huff#isleaf. = FALSEcxx . . $ `compression/huffman/hzip.c:57:9`!;
575 h.valuecxx . . $ huff#value. = -1;
576 h.freqcxx . . $ huff#freq. = heapcxx . . $ heap.[top].freqcxx . . $ element#freq.; /* will be incremented later when in 'combine' */
577 h.pleftcxx . . $ huff#pleft. = heapcxx . . $ heap.[top].huffidxcxx . . $ element#huffidx.;
578 h.prightcxx . . $ huff#pright. = -1; /* will be attached the next element */
579
580
581 #ifdef TRACE
582 printf ("\n\nBefore Eliminate Top\n");
583 heap_plot();
584 #endif
585
586 /* eliminate top */
587 heapcxx . . $ heap.[top] = heapcxx . . $ heap.[--heap_endcxx . . $ heap_end.];
588
589 /* next 'lesser' element at 'top' */
590 heap_adjust_downcxx . . $ heap_adjust_down(3e454b8ccf442868). (1, heap_endcxx . . $ heap_end.-1);
591
592 #ifdef TRACE
593 printf ("\n\nEliminate Top\n");
594 heap_plot ();
595 #endif
596
597 /* combine */
598 h.prightcxx . . $ huff#pright. = heapcxx . . $ heap.[1].huffidxcxx . . $ element#huffidx.;
599 h.freqcxx . . $ huff#freq. += heapcxx . . $ heap.[1].freqcxx . . $ element#freq.; /* combine frequencies */
600 hufftreecxx . . $ hufftree.[newidx] = h;
601
602 heapcxx . . $ heap.[1].freqcxx . . $ element#freq. = h.freqcxx . . $ huff#freq.;
603 heapcxx . . $ heap.[1].huffidxcxx . . $ element#huffidx. = newidx;
604
605 /* adjust the combined elements */
606 heap_adjust_downcxx . . $ heap_adjust_down(3e454b8ccf442868). (1, heap_endcxx . . $ heap_end.-1);
607
608 #ifdef TRACE
609 printf ("\n\nAfter Combine\n");
610 heap_plot ();
611 #endif
612
613 }
614
615 return heapcxx . . $ heap.[1].huffidxcxx . . $ element#huffidx.;
616 }
617
618
619 static void
620 hufftree_to_codescxx . . $ hufftree_to_codes(f59d17f8b9f2d3e1). (int start, int n, unsigned code)
621 {
622 int x, m;
623 unsigned c;
624 int value;
625
626 #ifdef TRACK
627 if (n == 0)
628 printf ("\nHufftree to codes\n");
629 #endif
630
631 assertcxx . . $ `<external>/4398592474888995393/assert.h:169:12`! (n >= 0);
632
633 x = hufftreecxx . . $ hufftree.[start].pleftcxx . . $ huff#pleft.;
634 c = code | (LEFTCODEcxx . . $ `compression/huffman/hzip.c:527:9`! << n);
635 m = n + 1;
636
637 /* LEFT */
638 if (hufftreecxx . . $ hufftree.[x].isleafcxx . . $ huff#isleaf.) {
639 value = hufftreecxx . . $ hufftree.[x].valuecxx . . $ huff#value.;
640 code_tablecxx . . $ code_table.[value] = c;
641 size_tablecxx . . $ size_table.[value] = (unsigned)m;
642
643 #ifdef TRACK
644 printf ("value=%c:%d, code=%d \"%s\", size=%d\n", value,value, c, binstream(c,m), m);
645 #endif
646
647 } else {
648 hufftree_to_codescxx . . $ hufftree_to_codes(f59d17f8b9f2d3e1).(x, m, c);
649 }
650
651
652 /* RIGHT */
653 x = hufftreecxx . . $ hufftree.[start].prightcxx . . $ huff#pright.;
654 c = code | (RIGHTCODEcxx . . $ `compression/huffman/hzip.c:528:9`! << n);
655 m = n + 1;
656
657 if (hufftreecxx . . $ hufftree.[x].isleafcxx . . $ huff#isleaf.) {
658 value = hufftreecxx . . $ hufftree.[x].valuecxx . . $ huff#value.;
659 code_tablecxx . . $ code_table.[value] = c;
660 size_tablecxx . . $ size_table.[value] = (unsigned)m;
661
662 #ifdef TRACK
663 printf ("value=%c:%d, code=%d \"%s\", size=%d\n", value,value, c, binstream(c,m), m);
664 #endif
665
666 } else {
667 hufftree_to_codescxx . . $ hufftree_to_codes(f59d17f8b9f2d3e1).(x, m, c);
668 }
669
670 return;
671 }
672
673
674 static int
675 hufftree_frombitscxx . . $ hufftree_frombits(4ab50908037b7de2). (ro_stream_tcxx . . $ ro_stream_t# *stream, bool_tcxx . . $ bool_t# *pok)
676 {
677 unsigned bit;
678 unsigned value;
679 int thisnode;
680 struct huffcxx . . $ huff# h;
681
682 if (!*pok)
683 return 0;
684
685 bit = stream_nextbitcxx . . $ stream_nextbit(2d47deecc3be6973).(stream);
686 if (bit == BITLEAFcxx . . $ `compression/huffman/hzip.c:529:9`!) {
687 /* leaf */
688 value = stream_nextbit_ncxx . . $ stream_nextbit_n(59e1778e378c3f1b). (stream, VALUEBITScxx . . $ VALUEBITS.);
689 thisnode = huff_endcxx . . $ huff_end.++;
690 h.isleafcxx . . $ huff#isleaf. = TRUEcxx . . $ `compression/huffman/hzip.c:56:9`!;
691 h.valuecxx . . $ huff#value. = (int)value;
692 h.freqcxx . . $ huff#freq. = 0;
693 h.pleftcxx . . $ huff#pleft. = 0;
694 h.prightcxx . . $ huff#pright. = 0;
695
696 if (thisnode >= MAXHUFFcxx . . $ `compression/huffman/hzip.c:52:9`!) {
697 *pok = FALSEcxx . . $ `compression/huffman/hzip.c:57:9`!;
698 return 0;
699 }
700
701 hufftreecxx . . $ hufftree.[thisnode] = h;
702
703 #ifdef TRACK
704 printf ("Huff leaf, %d=%c\n", value, value);
705 #endif
706
707 return thisnode;
708
709 } else {
710 /* node */
711 thisnode = huff_endcxx . . $ huff_end.++;
712
713 if (thisnode >= MAXHUFFcxx . . $ `compression/huffman/hzip.c:52:9`!) {
714 *pok = FALSEcxx . . $ `compression/huffman/hzip.c:57:9`!;
715 return 0;
716 }
717
718 h.isleafcxx . . $ huff#isleaf. = FALSEcxx . . $ `compression/huffman/hzip.c:57:9`!;
719 h.valuecxx . . $ huff#value. = -1;
720 h.freqcxx . . $ huff#freq. = 0;
721 h.pleftcxx . . $ huff#pleft. = hufftree_frombitscxx . . $ hufftree_frombits(4ab50908037b7de2). (stream, pok);
722 h.prightcxx . . $ huff#pright. = hufftree_frombitscxx . . $ hufftree_frombits(4ab50908037b7de2). (stream, pok);
723 hufftreecxx . . $ hufftree.[thisnode] = h;
724 return thisnode;
725 }
726 }
727
728
729 static void
730 hufftree_tobitscxx . . $ hufftree_tobits(abff3b7bf7cc5fdc). (int thisnode, stream_tcxx . . $ stream_t# *stream)
731 {
732
733 if (hufftreecxx . . $ hufftree.[thisnode].isleafcxx . . $ huff#isleaf.) {
734
735 #ifdef TRACK
736 {int c = hufftree[thisnode].value; printf ("[leaf=1][%c:%d=%s]", c, c, binstream(c,8));}
737 #endif
738
739 assertcxx . . $ `<external>/4398592474888995393/assert.h:169:12`! (0 <= hufftree[thisnode].value);
740
741 stream_writebitcxx . . $ stream_writebit(6a28a5ef0f848c65). (stream, BITLEAFcxx . . $ `compression/huffman/hzip.c:529:9`!);
742 stream_write_ncxx . . $ stream_write_n(ed67174fd7e844eb). ((unsigned)hufftreecxx . . $ hufftree.[thisnode].valuecxx . . $ huff#value., VALUEBITScxx . . $ VALUEBITS., stream);
743
744 } else {
745 stream_writebitcxx . . $ stream_writebit(6a28a5ef0f848c65). (stream, BITNODEcxx . . $ `compression/huffman/hzip.c:530:9`!);
746
747 #ifdef TRACK
748 printf ("[node=0]");
749 #endif
750
751 hufftree_tobitscxx . . $ hufftree_tobits(abff3b7bf7cc5fdc). (hufftreecxx . . $ hufftree.[thisnode].pleftcxx . . $ huff#pleft., stream);
752 hufftree_tobitscxx . . $ hufftree_tobits(abff3b7bf7cc5fdc). (hufftreecxx . . $ hufftree.[thisnode].prightcxx . . $ huff#pright., stream);
753
754 }
755 return;
756 }
757
758
759 static unsigned int
760 hufftree_readstreamcxx . . $ hufftree_readstream(94d630985062c180). (int root, ro_stream_tcxx . . $ ro_stream_t# *s)
761 {
762 unsigned bit;
763 int next;
764
765 bit = stream_nextbitcxx . . $ stream_nextbit(2d47deecc3be6973).(s);
766 if (bit == RIGHTCODEcxx . . $ `compression/huffman/hzip.c:528:9`!) {
767 /* right */
768 next = hufftreecxx . . $ hufftree.[root].prightcxx . . $ huff#pright.;
769 } else {
770 /*ASSERT (bit == LEFTCODE */
771 /* left */
772 next = hufftreecxx . . $ hufftree.[root].pleftcxx . . $ huff#pleft.;
773 }
774
775 if (hufftreecxx . . $ hufftree.[next].isleafcxx . . $ huff#isleaf.) {
776 assertcxx . . $ `<external>/4398592474888995393/assert.h:169:12`! (0 <= hufftree[next].value);
777 return (unsigned)hufftreecxx . . $ hufftree.[next].valuecxx . . $ huff#value.;
778 } else {
779 return hufftree_readstreamcxx . . $ hufftree_readstream(94d630985062c180). (next, s);
780 }
781 }
782
783 /*==== HEAP ==========================================*/
784
785 static void
786 heap_initcxx . . $ heap_init(49f6e7a06ebc5aa8). (void)
787 {
788 heap_endcxx . . $ heap_end. = 1;
789 return;
790 }
791
792 static void
793 heap_appendcxx . . $ heap_append(4bfb29fe608e0bf2). (struct elementcxx . . $ element# e)
794 {
795 /*ASSERT (heap_end < MAXHEAP);*/
796 heapcxx . . $ heap.[heap_endcxx . . $ heap_end.++] = e;
797 return;
798 }
799
800 static void
801 heap_sift_upcxx . . $ heap_sift_up(d4f767463ce0a6b3). (int x)
802 {
803 struct elementcxx . . $ element# t;
804 int p;
805 int c = x;
806 while (c > 1) {
807 p = c / 2;
808 if (heapcxx . . $ heap.[c].freqcxx . . $ element#freq. < heapcxx . . $ heap.[p].freqcxx . . $ element#freq.) {
809 t = heapcxx . . $ heap.[c]; heapcxx . . $ heap.[c] = heapcxx . . $ heap.[p]; heapcxx . . $ heap.[p] = t;
810 } else {
811 break;
812 }
813 c = p;
814 }
815 return;
816 }
817
818
819
820 static void
821 heap_adjust_downcxx . . $ heap_adjust_down(3e454b8ccf442868). (int top, int last)
822 {
823 struct elementcxx . . $ element# t;
824 int p;
825 int c;
826 int left, right;
827
828 if (last == top) { /* at least top element */
829 /* done */
830 return;
831 }
832
833 /* starts at the top */
834 p = top;
835
836 while (p <= last)
837 {
838 left = 2*p;
839 right = left + 1;
840
841 if (left > last)
842 break;
843
844 if (right <= last && (heapcxx . . $ heap.[right].freqcxx . . $ element#freq. < heapcxx . . $ heap.[left].freqcxx . . $ element#freq.))
845 c = right;
846 else
847 c = left;
848
849 if (c > last)
850 break;
851
852 if (heapcxx . . $ heap.[c].freqcxx . . $ element#freq. < heapcxx . . $ heap.[p].freqcxx . . $ element#freq.) {
853 t = heapcxx . . $ heap.[c]; heapcxx . . $ heap.[c] = heapcxx . . $ heap.[p]; heapcxx . . $ heap.[p] = t;
854 } else {
855 break;
856 }
857 p = c;
858 }
859 return;
860 }
861
862
863 /*
864 static void
865 heap_plot (void)
866 {
867 unsigned int line, limit, j;
868 int n = heap_end;
869 printf("===========================\n");
870 line = 1;
871 j = 1;
872 while (j < n) {
873 limit = 1 << line;
874 while (j < limit && j < n) {
875 printf("%3d:%c ",heap[j].freq, hufftree[heap[j].huffidx].value);
876 j++;
877 }
878 while (j < limit) {
879 printf("%3s ","--");
880 j++;
881 }
882 line++; printf("\n");
883 }
884 printf("===========================\n");
885 return;
886 }
887 */
888
889 /*
890 static void
891 freq_report (void)
892 {
893 int i;
894 printf ("\nFREQUENCIES\n");
895 for (i = 0; i < MAXDIVERSITY; i++) {
896 if (freq[i] > 0) {
897 printf ("%c: %2d: %d: %d\n", i, i, freq[i], code_table[i]);
898 }
899 }
900 printf ("\n");
901 return;
902 }
903 */
904
905 static void
906 freq_initcxx . . $ freq_init(1cebb3dd0b1c9c51). (const unsigned char *in, size_tcxx . . $ size_t# max)
907 {
908 size_tcxx . . $ size_t# i;
909
910 /* clean up frequencies */
911 for (i = 0; i < MAXDIVERSITYcxx . . $ `compression/huffman/hzip.c:49:9`!; i++) {
912 freqcxx . . $ freq. [i] = 0;
913 code_tablecxx . . $ code_table.[i] = 0;
914 size_tablecxx . . $ size_table.[i] = 0;
915 }
916
917 /* build frequencies */
918 for (i = 0; i < max; i++) {
919 freqcxx . . $ freq. [in[i]]++;
920 }
921
922 #ifdef TRACK
923 freq_report();
924 #endif
925
926 return;
927 }
928
929 static int
930 hufftree_from_freqcxx . . $ hufftree_from_freq(b126dc7c1de90089). (void)
931 {
932 int i;
933 struct huffcxx . . $ huff# h;
934 struct elementcxx . . $ element# e;
935 int root;
936
937 hufftree_resetcxx . . $ hufftree_reset(49f6e7a06ebc5aa8). ();
938
939 /* build huff tree elements */
940 huff_endcxx . . $ huff_end. = 0;
941 for (i = 0; i < MAXDIVERSITYcxx . . $ `compression/huffman/hzip.c:49:9`!; i++) {
942 if (freqcxx . . $ freq.[i] > 0) {
943 h.isleafcxx . . $ huff#isleaf. = TRUEcxx . . $ `compression/huffman/hzip.c:56:9`!;
944 h.valuecxx . . $ huff#value. = i;
945 h.freqcxx . . $ huff#freq. = freqcxx . . $ freq.[i];
946 h.pleftcxx . . $ huff#pleft. = 0;
947 h.prightcxx . . $ huff#pright. = 0;
948 hufftreecxx . . $ hufftree.[huff_endcxx . . $ huff_end.++] = h;
949 }
950 }
951
952 /* build heap */
953 heap_initcxx . . $ heap_init(49f6e7a06ebc5aa8).();
954 for (i = 0; i < huff_endcxx . . $ huff_end.; i++) {
955 e.freqcxx . . $ element#freq. = hufftreecxx . . $ hufftree.[i].freqcxx . . $ huff#freq.;
956 e.huffidxcxx . . $ element#huffidx. = i;
957 heap_appendcxx . . $ heap_append(4bfb29fe608e0bf2).(e);
958 heap_sift_upcxx . . $ heap_sift_up(d4f767463ce0a6b3).(heap_endcxx . . $ heap_end.-1);
959 }
960
961 #ifdef TRACE
962 heap_plot ();
963 #endif
964
965 root = hufftree_from_heapcxx . . $ hufftree_from_heap(b126dc7c1de90089).();
966
967 /*hufftree_to_codes (root, 0, 0);*/
968
969 return root;
970 }
971
972
973