10 #ifndef MSPACK_READHUFF_H
11 #define MSPACK_READHUFF_H 1
15 #if !(defined(BITS_ORDER_MSB) || defined(BITS_ORDER_LSB))
16 # error "readhuff.h is used in conjunction with readbits.h, include that first"
18 #if !(defined(TABLEBITS) && defined(MAXSYMBOLS))
19 # error "define TABLEBITS(tbl) and MAXSYMBOLS(tbl) before using readhuff.h"
21 #if !(defined(HUFF_TABLE) && defined(HUFF_LEN))
22 # error "define HUFF_TABLE(tbl) and HUFF_LEN(tbl) before using readhuff.h"
25 # error "define HUFF_ERROR before using readhuff.h"
28 # define HUFF_MAXBITS 16
34 #define READ_HUFFSYM(tbl, var) do { \
35 ENSURE_BITS(HUFF_MAXBITS); \
36 sym = HUFF_TABLE(tbl, PEEK_BITS(TABLEBITS(tbl))); \
37 if (sym >= MAXSYMBOLS(tbl)) HUFF_TRAVERSE(tbl); \
39 i = HUFF_LEN(tbl, sym); \
44 # define HUFF_TRAVERSE(tbl) do { \
45 i = TABLEBITS(tbl) - 1; \
47 if (i++ > HUFF_MAXBITS) HUFF_ERROR; \
48 sym = HUFF_TABLE(tbl, \
49 (sym << 1) | ((bit_buffer >> i) & 1)); \
50 } while (sym >= MAXSYMBOLS(tbl)); \
53 #define HUFF_TRAVERSE(tbl) do { \
54 i = 1 << (BITBUF_WIDTH - TABLEBITS(tbl)); \
56 if ((i >>= 1) == 0) HUFF_ERROR; \
57 sym = HUFF_TABLE(tbl, \
58 (sym << 1) | ((bit_buffer & i) ? 1 : 0)); \
59 } while (sym >= MAXSYMBOLS(tbl)); \
79 unsigned char *
length,
unsigned short *table)
81 register unsigned short sym, next_symbol;
82 register unsigned int leaf, fill;
86 register unsigned char bit_num;
88 unsigned int table_mask = 1 << nbits;
89 unsigned int bit_mask = table_mask >> 1;
92 for (bit_num = 1; bit_num <= nbits; bit_num++) {
93 for (sym = 0; sym < nsyms; sym++) {
94 if (
length[sym] != bit_num)
continue;
100 do {leaf <<= 1; leaf |= reverse & 1; reverse >>= 1;}
while (--fill);
106 #ifdef BITS_ORDER_MSB
107 for (fill =
bit_mask; fill-- > 0;) table[leaf++] = sym;
109 fill =
bit_mask; next_symbol = 1 << bit_num;
110 do { table[leaf] = sym; leaf += next_symbol; }
while (--fill);
117 if (
pos == table_mask)
return 0;
120 for (sym =
pos; sym < table_mask; sym++) {
121 #ifdef BITS_ORDER_MSB
124 reverse = sym; leaf = 0; fill = nbits;
125 do { leaf <<= 1; leaf |= reverse & 1; reverse >>= 1; }
while (--fill);
126 table[leaf] = 0xFFFF;
131 next_symbol = ((table_mask >> 1) < nsyms) ? nsyms : (table_mask >> 1);
139 for (bit_num = nbits+1; bit_num <=
HUFF_MAXBITS; bit_num++) {
140 for (sym = 0; sym < nsyms; sym++) {
141 if (
length[sym] != bit_num)
continue;
142 if (
pos >= table_mask)
return 1;
144 #ifdef BITS_ORDER_MSB
149 do {leaf <<= 1; leaf |= reverse & 1; reverse >>= 1;}
while (--fill);
151 for (fill = 0; fill < (bit_num - nbits); fill++) {
153 if (table[leaf] == 0xFFFF) {
154 table[(next_symbol << 1) ] = 0xFFFF;
155 table[(next_symbol << 1) + 1 ] = 0xFFFF;
156 table[leaf] = next_symbol++;
160 leaf = table[leaf] << 1;
161 if ((
pos >> (15-fill)) & 1) leaf++;
170 return (
pos == table_mask) ? 0 : 1;
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length