3 #define RNC_SIGNATURE 0x524E4301 /* "RNC\001" */
6 unsigned long bitbuf
; /* holds between 16 and 32 bits */
7 int bitcount
; /* how many bits does bitbuf hold? */
11 int num
; /* number of nodes in the tree */
19 static long rnc_crc (void *data
, long len
);
20 static void read_huftable (huf_table
*h
, bit_stream
*bs
, unsigned char **p
);
21 static unsigned long huf_read (huf_table
*h
, bit_stream
*bs
,unsigned char **p
);
23 static void bitread_init (bit_stream
*bs
, unsigned char **p
);
24 static void bitread_fix (bit_stream
*bs
, unsigned char **p
);
25 static unsigned long bit_peek (bit_stream
*bs
, unsigned long mask
);
26 static void bit_advance (bit_stream
*bs
, int n
, unsigned char **p
);
27 static unsigned long bit_read (bit_stream
*bs
, unsigned long mask
,int n
, unsigned char **p
);
29 static unsigned long blong (unsigned char *p
);
30 //static unsigned long llong (unsigned char *p);
31 static unsigned long bword (unsigned char *p
);
32 static unsigned long lword (unsigned char *p
);
34 static unsigned long mirror (unsigned long x
, int n
);
36 s32
depackrnc1_ulen(void *packed
)
38 unsigned char *p
= packed
;
39 if (blong (p
) != RNC_SIGNATURE
)
40 return RNC_FILE_IS_NOT_RNC
;
44 s32
depackrnc1(void *packed
,void *unpacked
)
46 unsigned char *input
= packed
;
47 unsigned char *output
= unpacked
;
48 unsigned char *inputend
, *outputend
;
50 huf_table raw
, dist
, len
;
51 unsigned long ch_count
;
52 unsigned long ret_len
;
55 if (blong(input
) != RNC_SIGNATURE
)
56 return RNC_FILE_IS_NOT_RNC
;
57 ret_len
= blong (input
+4);
58 outputend
= output
+ ret_len
;
59 inputend
= input
+ 18 + blong(input
+8);
61 input
+= 18; /* skip header */
64 * Check the packed-data CRC. Also save the unpacked-data CRC
67 if (rnc_crc(input
, inputend
-input
) != bword(input
-4))
68 return RNC_PACKED_CRC_ERROR
;
69 out_crc
= bword(input
-6);
71 bitread_init (&bs
, &input
);
72 bit_advance (&bs
, 2, &input
); /* discard first two bits */
77 while (output
< outputend
) {
78 read_huftable (&raw
, &bs
, &input
);
79 read_huftable (&dist
, &bs
, &input
);
80 read_huftable (&len
, &bs
, &input
);
81 ch_count
= bit_read (&bs
, 0xFFFF, 16, &input
);
86 length
= huf_read (&raw
, &bs
, &input
);
88 return RNC_HUF_DECODE_ERROR
;
92 bitread_fix (&bs
, &input
);
97 posn
= huf_read (&dist
, &bs
, &input
);
99 return RNC_HUF_DECODE_ERROR
;
100 length
= huf_read (&len
, &bs
, &input
);
102 return RNC_HUF_DECODE_ERROR
;
106 *output
= output
[-posn
];
111 if (outputend
!= output
)
112 return RNC_FILE_SIZE_MISMATCH
;
114 if (rnc_crc(outputend
-ret_len
, ret_len
) != out_crc
)
115 return RNC_UNPACKED_CRC_ERROR
;
121 * Read a Huffman table out of the bit stream and data stream given.
123 static void read_huftable (huf_table
*h
, bit_stream
*bs
, unsigned char **p
) {
127 unsigned long codeb
; /* big-endian form of code */
129 num
= bit_read (bs
, 0x1F, 5, p
);
134 for (i
=0; i
<num
; i
++) {
135 leaflen
[i
] = bit_read (bs
, 0x0F, 4, p
);
136 if (leafmax
< leaflen
[i
])
137 leafmax
= leaflen
[i
];
142 for (i
=1; i
<=leafmax
; i
++) {
143 for (j
=0; j
<num
; j
++)
144 if (leaflen
[j
] == i
) {
145 h
->table
[k
].code
= mirror (codeb
, i
);
146 h
->table
[k
].codelen
= i
;
147 h
->table
[k
].value
= j
;
158 * Read a value out of the bit stream using the given Huffman table.
160 static unsigned long huf_read (huf_table
*h
, bit_stream
*bs
,
165 for (i
=0; i
<h
->num
; i
++) {
166 unsigned long mask
= (1 << h
->table
[i
].codelen
) - 1;
167 if (bit_peek(bs
, mask
) == h
->table
[i
].code
)
172 bit_advance (bs
, h
->table
[i
].codelen
, p
);
174 val
= h
->table
[i
].value
;
178 val
|= bit_read (bs
, val
-1, h
->table
[i
].value
- 1, p
);
184 * Initialises a bit stream with the first two bytes of the packed
187 static void bitread_init (bit_stream
*bs
, unsigned char **p
) {
188 bs
->bitbuf
= lword (*p
);
193 * Fixes up a bit stream after literals have been read out of the
196 static void bitread_fix (bit_stream
*bs
, unsigned char **p
) {
198 bs
->bitbuf
&= (1<<bs
->bitcount
)-1; /* remove the top 16 bits */
199 bs
->bitbuf
|= (lword(*p
)<<bs
->bitcount
);/* replace with what's at *p */
206 static unsigned long bit_peek (bit_stream
*bs
, unsigned long mask
) {
207 return bs
->bitbuf
& mask
;
211 * Advances the bit stream.
213 static void bit_advance (bit_stream
*bs
, int n
, unsigned char **p
) {
216 if (bs
->bitcount
< 16) {
218 bs
->bitbuf
|= (lword(*p
)<<bs
->bitcount
);
224 * Reads some bits in one go (ie the above two routines combined).
226 static unsigned long bit_read (bit_stream
*bs
, unsigned long mask
,
227 int n
, unsigned char **p
) {
228 unsigned long result
= bit_peek (bs
, mask
);
229 bit_advance (bs
, n
, p
);
234 * Return the big-endian longword at p.
236 static unsigned long blong (unsigned char *p
) {
247 * Return the little-endian longword at p.
249 static unsigned long llong (unsigned char *p
) {
260 * Return the big-endian word at p.
262 static unsigned long bword (unsigned char *p
) {
270 * Return the little-endian word at p.
272 static unsigned long lword (unsigned char *p
) {
280 * Mirror the bottom n bits of x.
282 static unsigned long mirror (unsigned long x
, int n
) {
283 unsigned long top
= 1 << (n
-1), bottom
= 1;
284 while (top
> bottom
) {
285 unsigned long mask
= top
| bottom
;
286 unsigned long masked
= x
& mask
;
287 if (masked
!= 0 && masked
!= mask
)
296 * Calculate a CRC, the RNC way. It re-computes its CRC table every
297 * time it's run, but who cares? ;-)
299 static long rnc_crc (void *data
, long len
) {
300 unsigned short crctab
[256];
303 unsigned char *p
= data
;
305 for (i
=0; i
<256; i
++) {
308 for (j
=0; j
<8; j
++) {
310 val
= (val
>> 1) ^ 0xA001;
320 val
= (val
>> 8) ^ crctab
[val
& 0xFF];