summaryrefslogtreecommitdiff
path: root/rbutil/rbutilqt/mspack/readhuff.h
diff options
context:
space:
mode:
Diffstat (limited to 'rbutil/rbutilqt/mspack/readhuff.h')
-rw-r--r--rbutil/rbutilqt/mspack/readhuff.h129
1 files changed, 64 insertions, 65 deletions
diff --git a/rbutil/rbutilqt/mspack/readhuff.h b/rbutil/rbutilqt/mspack/readhuff.h
index bb15c0a123..4d94225789 100644
--- a/rbutil/rbutilqt/mspack/readhuff.h
+++ b/rbutil/rbutilqt/mspack/readhuff.h
@@ -1,5 +1,5 @@
1/* This file is part of libmspack. 1/* This file is part of libmspack.
2 * (C) 2003-2010 Stuart Caie. 2 * (C) 2003-2014 Stuart Caie.
3 * 3 *
4 * libmspack is free software; you can redistribute it and/or modify it under 4 * libmspack is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License (LGPL) version 2.1 5 * the terms of the GNU Lesser General Public License (LGPL) version 2.1
@@ -10,8 +10,7 @@
10#ifndef MSPACK_READHUFF_H 10#ifndef MSPACK_READHUFF_H
11#define MSPACK_READHUFF_H 1 11#define MSPACK_READHUFF_H 1
12 12
13/* This implements a fast Huffman tree decoding system. 13/* This implements a fast Huffman tree decoding system. */
14 */
15 14
16#if !(defined(BITS_ORDER_MSB) || defined(BITS_ORDER_LSB)) 15#if !(defined(BITS_ORDER_MSB) || defined(BITS_ORDER_LSB))
17# error "readhuff.h is used in conjunction with readbits.h, include that first" 16# error "readhuff.h is used in conjunction with readbits.h, include that first"
@@ -32,32 +31,32 @@
32/* Decodes the next huffman symbol from the input bitstream into var. 31/* Decodes the next huffman symbol from the input bitstream into var.
33 * Do not use this macro on a table unless build_decode_table() succeeded. 32 * Do not use this macro on a table unless build_decode_table() succeeded.
34 */ 33 */
35#define READ_HUFFSYM(tbl, var) do { \ 34#define READ_HUFFSYM(tbl, var) do { \
36 ENSURE_BITS(HUFF_MAXBITS); \ 35 ENSURE_BITS(HUFF_MAXBITS); \
37 sym = HUFF_TABLE(tbl, PEEK_BITS(TABLEBITS(tbl))); \ 36 sym = HUFF_TABLE(tbl, PEEK_BITS(TABLEBITS(tbl))); \
38 if (sym >= MAXSYMBOLS(tbl)) HUFF_TRAVERSE(tbl); \ 37 if (sym >= MAXSYMBOLS(tbl)) HUFF_TRAVERSE(tbl); \
39 (var) = sym; \ 38 (var) = sym; \
40 i = HUFF_LEN(tbl, sym); \ 39 i = HUFF_LEN(tbl, sym); \
41 REMOVE_BITS(i); \ 40 REMOVE_BITS(i); \
42} while (0) 41} while (0)
43 42
44#ifdef BITS_ORDER_LSB 43#ifdef BITS_ORDER_LSB
45# define HUFF_TRAVERSE(tbl) do { \ 44# define HUFF_TRAVERSE(tbl) do { \
46 i = TABLEBITS(tbl) - 1; \ 45 i = TABLEBITS(tbl) - 1; \
47 do { \ 46 do { \
48 if (i++ > HUFF_MAXBITS) HUFF_ERROR; \ 47 if (i++ > HUFF_MAXBITS) HUFF_ERROR; \
49 sym = HUFF_TABLE(tbl, \ 48 sym = HUFF_TABLE(tbl, \
50 (sym << 1) | ((bit_buffer >> i) & 1)); \ 49 (sym << 1) | ((bit_buffer >> i) & 1)); \
51 } while (sym >= MAXSYMBOLS(tbl)); \ 50 } while (sym >= MAXSYMBOLS(tbl)); \
52} while (0) 51} while (0)
53#else 52#else
54#define HUFF_TRAVERSE(tbl) do { \ 53#define HUFF_TRAVERSE(tbl) do { \
55 i = 1 << (BITBUF_WIDTH - TABLEBITS(tbl)); \ 54 i = 1 << (BITBUF_WIDTH - TABLEBITS(tbl)); \
56 do { \ 55 do { \
57 if ((i >>= 1) == 0) HUFF_ERROR; \ 56 if ((i >>= 1) == 0) HUFF_ERROR; \
58 sym = HUFF_TABLE(tbl, \ 57 sym = HUFF_TABLE(tbl, \
59 (sym << 1) | ((bit_buffer & i) ? 1 : 0)); \ 58 (sym << 1) | ((bit_buffer & i) ? 1 : 0)); \
60 } while (sym >= MAXSYMBOLS(tbl)); \ 59 } while (sym >= MAXSYMBOLS(tbl)); \
61} while (0) 60} while (0)
62#endif 61#endif
63 62
@@ -77,7 +76,7 @@
77 * Returns 0 for OK or 1 for error 76 * Returns 0 for OK or 1 for error
78 */ 77 */
79static int make_decode_table(unsigned int nsyms, unsigned int nbits, 78static int make_decode_table(unsigned int nsyms, unsigned int nbits,
80 unsigned char *length, unsigned short *table) 79 unsigned char *length, unsigned short *table)
81{ 80{
82 register unsigned short sym, next_symbol; 81 register unsigned short sym, next_symbol;
83 register unsigned int leaf, fill; 82 register unsigned int leaf, fill;
@@ -91,27 +90,27 @@ static int make_decode_table(unsigned int nsyms, unsigned int nbits,
91 90
92 /* fill entries for codes short enough for a direct mapping */ 91 /* fill entries for codes short enough for a direct mapping */
93 for (bit_num = 1; bit_num <= nbits; bit_num++) { 92 for (bit_num = 1; bit_num <= nbits; bit_num++) {
94 for (sym = 0; sym < nsyms; sym++) { 93 for (sym = 0; sym < nsyms; sym++) {
95 if (length[sym] != bit_num) continue; 94 if (length[sym] != bit_num) continue;
96#ifdef BITS_ORDER_MSB 95#ifdef BITS_ORDER_MSB
97 leaf = pos; 96 leaf = pos;
98#else 97#else
99 /* reverse the significant bits */ 98 /* reverse the significant bits */
100 fill = length[sym]; reverse = pos >> (nbits - fill); leaf = 0; 99 fill = length[sym]; reverse = pos >> (nbits - fill); leaf = 0;
101 do {leaf <<= 1; leaf |= reverse & 1; reverse >>= 1;} while (--fill); 100 do {leaf <<= 1; leaf |= reverse & 1; reverse >>= 1;} while (--fill);
102#endif 101#endif
103 102
104 if((pos += bit_mask) > table_mask) return 1; /* table overrun */ 103 if((pos += bit_mask) > table_mask) return 1; /* table overrun */
105 104
106 /* fill all possible lookups of this symbol with the symbol itself */ 105 /* fill all possible lookups of this symbol with the symbol itself */
107#ifdef BITS_ORDER_MSB 106#ifdef BITS_ORDER_MSB
108 for (fill = bit_mask; fill-- > 0;) table[leaf++] = sym; 107 for (fill = bit_mask; fill-- > 0;) table[leaf++] = sym;
109#else 108#else
110 fill = bit_mask; next_symbol = 1 << bit_num; 109 fill = bit_mask; next_symbol = 1 << bit_num;
111 do { table[leaf] = sym; leaf += next_symbol; } while (--fill); 110 do { table[leaf] = sym; leaf += next_symbol; } while (--fill);
112#endif 111#endif
113 } 112 }
114 bit_mask >>= 1; 113 bit_mask >>= 1;
115 } 114 }
116 115
117 /* exit with success if table is now complete */ 116 /* exit with success if table is now complete */
@@ -120,11 +119,11 @@ static int make_decode_table(unsigned int nsyms, unsigned int nbits,
120 /* mark all remaining table entries as unused */ 119 /* mark all remaining table entries as unused */
121 for (sym = pos; sym < table_mask; sym++) { 120 for (sym = pos; sym < table_mask; sym++) {
122#ifdef BITS_ORDER_MSB 121#ifdef BITS_ORDER_MSB
123 table[sym] = 0xFFFF; 122 table[sym] = 0xFFFF;
124#else 123#else
125 reverse = sym; leaf = 0; fill = nbits; 124 reverse = sym; leaf = 0; fill = nbits;
126 do { leaf <<= 1; leaf |= reverse & 1; reverse >>= 1; } while (--fill); 125 do { leaf <<= 1; leaf |= reverse & 1; reverse >>= 1; } while (--fill);
127 table[leaf] = 0xFFFF; 126 table[leaf] = 0xFFFF;
128#endif 127#endif
129 } 128 }
130 129
@@ -138,33 +137,33 @@ static int make_decode_table(unsigned int nsyms, unsigned int nbits,
138 bit_mask = 1 << 15; 137 bit_mask = 1 << 15;
139 138
140 for (bit_num = nbits+1; bit_num <= HUFF_MAXBITS; bit_num++) { 139 for (bit_num = nbits+1; bit_num <= HUFF_MAXBITS; bit_num++) {
141 for (sym = 0; sym < nsyms; sym++) { 140 for (sym = 0; sym < nsyms; sym++) {
142 if (length[sym] != bit_num) continue; 141 if (length[sym] != bit_num) continue;
142 if (pos >= table_mask) return 1; /* table overflow */
143 143
144#ifdef BITS_ORDER_MSB 144#ifdef BITS_ORDER_MSB
145 leaf = pos >> 16; 145 leaf = pos >> 16;
146#else 146#else
147 /* leaf = the first nbits of the code, reversed */ 147 /* leaf = the first nbits of the code, reversed */
148 reverse = pos >> 16; leaf = 0; fill = nbits; 148 reverse = pos >> 16; leaf = 0; fill = nbits;
149 do {leaf <<= 1; leaf |= reverse & 1; reverse >>= 1;} while (--fill); 149 do {leaf <<= 1; leaf |= reverse & 1; reverse >>= 1;} while (--fill);
150#endif 150#endif
151 for (fill = 0; fill < (bit_num - nbits); fill++) { 151 for (fill = 0; fill < (bit_num - nbits); fill++) {
152 /* if this path hasn't been taken yet, 'allocate' two entries */ 152 /* if this path hasn't been taken yet, 'allocate' two entries */
153 if (table[leaf] == 0xFFFF) { 153 if (table[leaf] == 0xFFFF) {
154 table[(next_symbol << 1) ] = 0xFFFF; 154 table[(next_symbol << 1) ] = 0xFFFF;
155 table[(next_symbol << 1) + 1 ] = 0xFFFF; 155 table[(next_symbol << 1) + 1 ] = 0xFFFF;
156 table[leaf] = next_symbol++; 156 table[leaf] = next_symbol++;
157 } 157 }
158 158
159 /* follow the path and select either left or right for next bit */ 159 /* follow the path and select either left or right for next bit */
160 leaf = table[leaf] << 1; 160 leaf = table[leaf] << 1;
161 if ((pos >> (15-fill)) & 1) leaf++; 161 if ((pos >> (15-fill)) & 1) leaf++;
162 } 162 }
163 table[leaf] = sym; 163 table[leaf] = sym;
164 164 pos += bit_mask;
165 if ((pos += bit_mask) > table_mask) return 1; /* table overflow */ 165 }
166 } 166 bit_mask >>= 1;
167 bit_mask >>= 1;
168 } 167 }
169 168
170 /* full table? */ 169 /* full table? */