| 1 |
#include <string.h> |
| 2 |
|
| 3 |
#include "pm_c_util.h" |
| 4 |
#include "pm.h" |
| 5 |
|
| 6 |
#include "decode.h" |
| 7 |
|
| 8 |
struct decode first_decode[2048], *second_decode, *free_decode; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
void init_decoder() { |
| 13 |
memset (first_decode, 0, sizeof first_decode); |
| 14 |
free_decode = first_decode; |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
/* |
| 20 |
Construct a decode tree according the specification in *source. |
| 21 |
The first 16 bytes specify how many codes should be 1-bit, 2-bit |
| 22 |
3-bit, etc. Bytes after that are the leaf values. |
| 23 |
|
| 24 |
For example, if the source is |
| 25 |
|
| 26 |
{ 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0, |
| 27 |
0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff }, |
| 28 |
|
| 29 |
then the code is |
| 30 |
|
| 31 |
00 0x04 |
| 32 |
010 0x03 |
| 33 |
011 0x05 |
| 34 |
100 0x06 |
| 35 |
101 0x02 |
| 36 |
1100 0x07 |
| 37 |
1101 0x01 |
| 38 |
11100 0x08 |
| 39 |
11101 0x09 |
| 40 |
11110 0x00 |
| 41 |
111110 0x0a |
| 42 |
1111110 0x0b |
| 43 |
1111111 0xff |
| 44 |
*/ |
| 45 |
|
| 46 |
|
| 47 |
unsigned char * |
| 48 |
make_decoder(const unsigned char * const source, |
| 49 |
int const level) { |
| 50 |
|
| 51 |
struct decode *cur; |
| 52 |
static int leaf; |
| 53 |
int i, next; |
| 54 |
|
| 55 |
if (level==0) |
| 56 |
leaf=0; |
| 57 |
cur = free_decode++; |
| 58 |
if (free_decode > first_decode+2048) { |
| 59 |
pm_error ("decoder table overflow"); |
| 60 |
} |
| 61 |
for (i=next=0; i <= leaf && next < 16; ) |
| 62 |
i += source[next++]; |
| 63 |
if (i > leaf) { |
| 64 |
if (level < next) { |
| 65 |
cur->branch[0] = free_decode; |
| 66 |
make_decoder (source, level+1); |
| 67 |
cur->branch[1] = free_decode; |
| 68 |
make_decoder (source, level+1); |
| 69 |
} else |
| 70 |
cur->leaf = source[16 + leaf++]; |
| 71 |
} |
| 72 |
return (unsigned char *) source + 16 + leaf; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
const int * |
| 78 |
make_decoder_int(const int * const source, |
| 79 |
int const level) { |
| 80 |
|
| 81 |
const int * p; |
| 82 |
struct decode *cur; |
| 83 |
|
| 84 |
p = source; /* initial value */ |
| 85 |
|
| 86 |
cur = free_decode++; |
| 87 |
if (level < p[0]) { |
| 88 |
cur->branch[0] = free_decode; |
| 89 |
p = make_decoder_int (p, level+1); |
| 90 |
cur->branch[1] = free_decode; |
| 91 |
p = make_decoder_int (p, level+1); |
| 92 |
} else { |
| 93 |
cur->leaf = p[1]; |
| 94 |
p += 2; |
| 95 |
} |
| 96 |
return p; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
void |
| 102 |
crw_init_tables(unsigned int const table) { |
| 103 |
|
| 104 |
unsigned int const clippedTableNum = MIN(2, table); |
| 105 |
|
| 106 |
static const unsigned char first_tree[3][29] = { |
| 107 |
{ 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0, |
| 108 |
0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff }, |
| 109 |
|
| 110 |
{ 0,2,2,3,1,1,1,1,2,0,0,0,0,0,0,0, |
| 111 |
0x03,0x02,0x04,0x01,0x05,0x00,0x06,0x07,0x09,0x08,0x0a,0x0b,0xff }, |
| 112 |
|
| 113 |
{ 0,0,6,3,1,1,2,0,0,0,0,0,0,0,0,0, |
| 114 |
0x06,0x05,0x07,0x04,0x08,0x03,0x09,0x02,0x00,0x0a,0x01,0x0b,0xff }, |
| 115 |
}; |
| 116 |
|
| 117 |
static const unsigned char second_tree[3][180] = { |
| 118 |
{ 0,2,2,2,1,4,2,1,2,5,1,1,0,0,0,139, |
| 119 |
0x03,0x04,0x02,0x05,0x01,0x06,0x07,0x08, |
| 120 |
0x12,0x13,0x11,0x14,0x09,0x15,0x22,0x00,0x21,0x16,0x0a,0xf0, |
| 121 |
0x23,0x17,0x24,0x31,0x32,0x18,0x19,0x33,0x25,0x41,0x34,0x42, |
| 122 |
0x35,0x51,0x36,0x37,0x38,0x29,0x79,0x26,0x1a,0x39,0x56,0x57, |
| 123 |
0x28,0x27,0x52,0x55,0x58,0x43,0x76,0x59,0x77,0x54,0x61,0xf9, |
| 124 |
0x71,0x78,0x75,0x96,0x97,0x49,0xb7,0x53,0xd7,0x74,0xb6,0x98, |
| 125 |
0x47,0x48,0x95,0x69,0x99,0x91,0xfa,0xb8,0x68,0xb5,0xb9,0xd6, |
| 126 |
0xf7,0xd8,0x67,0x46,0x45,0x94,0x89,0xf8,0x81,0xd5,0xf6,0xb4, |
| 127 |
0x88,0xb1,0x2a,0x44,0x72,0xd9,0x87,0x66,0xd4,0xf5,0x3a,0xa7, |
| 128 |
0x73,0xa9,0xa8,0x86,0x62,0xc7,0x65,0xc8,0xc9,0xa1,0xf4,0xd1, |
| 129 |
0xe9,0x5a,0x92,0x85,0xa6,0xe7,0x93,0xe8,0xc1,0xc6,0x7a,0x64, |
| 130 |
0xe1,0x4a,0x6a,0xe6,0xb3,0xf1,0xd3,0xa5,0x8a,0xb2,0x9a,0xba, |
| 131 |
0x84,0xa4,0x63,0xe5,0xc5,0xf3,0xd2,0xc4,0x82,0xaa,0xda,0xe4, |
| 132 |
0xf2,0xca,0x83,0xa3,0xa2,0xc3,0xea,0xc2,0xe2,0xe3,0xff,0xff }, |
| 133 |
|
| 134 |
{ 0,2,2,1,4,1,4,1,3,3,1,0,0,0,0,140, |
| 135 |
0x02,0x03,0x01,0x04,0x05,0x12,0x11,0x06, |
| 136 |
0x13,0x07,0x08,0x14,0x22,0x09,0x21,0x00,0x23,0x15,0x31,0x32, |
| 137 |
0x0a,0x16,0xf0,0x24,0x33,0x41,0x42,0x19,0x17,0x25,0x18,0x51, |
| 138 |
0x34,0x43,0x52,0x29,0x35,0x61,0x39,0x71,0x62,0x36,0x53,0x26, |
| 139 |
0x38,0x1a,0x37,0x81,0x27,0x91,0x79,0x55,0x45,0x28,0x72,0x59, |
| 140 |
0xa1,0xb1,0x44,0x69,0x54,0x58,0xd1,0xfa,0x57,0xe1,0xf1,0xb9, |
| 141 |
0x49,0x47,0x63,0x6a,0xf9,0x56,0x46,0xa8,0x2a,0x4a,0x78,0x99, |
| 142 |
0x3a,0x75,0x74,0x86,0x65,0xc1,0x76,0xb6,0x96,0xd6,0x89,0x85, |
| 143 |
0xc9,0xf5,0x95,0xb4,0xc7,0xf7,0x8a,0x97,0xb8,0x73,0xb7,0xd8, |
| 144 |
0xd9,0x87,0xa7,0x7a,0x48,0x82,0x84,0xea,0xf4,0xa6,0xc5,0x5a, |
| 145 |
0x94,0xa4,0xc6,0x92,0xc3,0x68,0xb5,0xc8,0xe4,0xe5,0xe6,0xe9, |
| 146 |
0xa2,0xa3,0xe3,0xc2,0x66,0x67,0x93,0xaa,0xd4,0xd5,0xe7,0xf8, |
| 147 |
0x88,0x9a,0xd7,0x77,0xc4,0x64,0xe2,0x98,0xa5,0xca,0xda,0xe8, |
| 148 |
0xf3,0xf6,0xa9,0xb2,0xb3,0xf2,0xd2,0x83,0xba,0xd3,0xff,0xff }, |
| 149 |
|
| 150 |
{ 0,0,6,2,1,3,3,2,5,1,2,2,8,10,0,117, |
| 151 |
0x04,0x05,0x03,0x06,0x02,0x07,0x01,0x08, |
| 152 |
0x09,0x12,0x13,0x14,0x11,0x15,0x0a,0x16,0x17,0xf0,0x00,0x22, |
| 153 |
0x21,0x18,0x23,0x19,0x24,0x32,0x31,0x25,0x33,0x38,0x37,0x34, |
| 154 |
0x35,0x36,0x39,0x79,0x57,0x58,0x59,0x28,0x56,0x78,0x27,0x41, |
| 155 |
0x29,0x77,0x26,0x42,0x76,0x99,0x1a,0x55,0x98,0x97,0xf9,0x48, |
| 156 |
0x54,0x96,0x89,0x47,0xb7,0x49,0xfa,0x75,0x68,0xb6,0x67,0x69, |
| 157 |
0xb9,0xb8,0xd8,0x52,0xd7,0x88,0xb5,0x74,0x51,0x46,0xd9,0xf8, |
| 158 |
0x3a,0xd6,0x87,0x45,0x7a,0x95,0xd5,0xf6,0x86,0xb4,0xa9,0x94, |
| 159 |
0x53,0x2a,0xa8,0x43,0xf5,0xf7,0xd4,0x66,0xa7,0x5a,0x44,0x8a, |
| 160 |
0xc9,0xe8,0xc8,0xe7,0x9a,0x6a,0x73,0x4a,0x61,0xc7,0xf4,0xc6, |
| 161 |
0x65,0xe9,0x72,0xe6,0x71,0x91,0x93,0xa6,0xda,0x92,0x85,0x62, |
| 162 |
0xf3,0xc5,0xb2,0xa4,0x84,0xba,0x64,0xa5,0xb3,0xd2,0x81,0xe5, |
| 163 |
0xd3,0xaa,0xc4,0xca,0xf2,0xb1,0xe4,0xd1,0x83,0x63,0xea,0xc3, |
| 164 |
0xe2,0x82,0xf1,0xa3,0xc2,0xa1,0xc1,0xe3,0xa2,0xe1,0xff,0xff } |
| 165 |
}; |
| 166 |
|
| 167 |
init_decoder(); |
| 168 |
make_decoder ( first_tree[clippedTableNum], 0); |
| 169 |
second_decode = free_decode; |
| 170 |
make_decoder (second_tree[clippedTableNum], 0); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|