MCPcopy Create free account
hub / github.com/abbeycode/UnrarKit / MakeDecodeTables

Method MakeDecodeTables

Libraries/unrar/unpack.cpp:227–365  ·  view source on GitHub ↗

LengthTable contains the length in bits for every element of alphabet. Dec is the structure to decode Huffman code/ Size is size of length table and DecodeNum field in Dec structure,

Source from the content-addressed store, hash-verified

225// Dec is the structure to decode Huffman code/
226// Size is size of length table and DecodeNum field in Dec structure,
227void Unpack::MakeDecodeTables(byte *LengthTable,DecodeTable *Dec,uint Size)
228{
229 // Size of alphabet and DecodePos array.
230 Dec->MaxNum=Size;
231
232 // Calculate how many entries for every bit length in LengthTable we have.
233 uint LengthCount[16];
234 memset(LengthCount,0,sizeof(LengthCount));
235 for (size_t I=0;I<Size;I++)
236 LengthCount[LengthTable[I] & 0xf]++;
237
238 // We must not calculate the number of zero length codes.
239 LengthCount[0]=0;
240
241 // Set the entire DecodeNum to zero.
242 memset(Dec->DecodeNum,0,Size*sizeof(*Dec->DecodeNum));
243
244 // Initialize not really used entry for zero length code.
245 Dec->DecodePos[0]=0;
246
247 // Start code for bit length 1 is 0.
248 Dec->DecodeLen[0]=0;
249
250 // Right aligned upper limit code for current bit length.
251 uint UpperLimit=0;
252
253 for (size_t I=1;I<16;I++)
254 {
255 // Adjust the upper limit code.
256 UpperLimit+=LengthCount[I];
257
258 // Left aligned upper limit code.
259 uint LeftAligned=UpperLimit<<(16-I);
260
261 // Prepare the upper limit code for next bit length.
262 UpperLimit*=2;
263
264 // Store the left aligned upper limit code.
265 Dec->DecodeLen[I]=(uint)LeftAligned;
266
267 // Every item of this array contains the sum of all preceding items.
268 // So it contains the start position in code list for every bit length.
269 Dec->DecodePos[I]=Dec->DecodePos[I-1]+LengthCount[I-1];
270 }
271
272 // Prepare the copy of DecodePos. We'll modify this copy below,
273 // so we cannot use the original DecodePos.
274 uint CopyDecodePos[ASIZE(Dec->DecodePos)];
275 memcpy(CopyDecodePos,Dec->DecodePos,sizeof(CopyDecodePos));
276
277 // For every bit length in the bit length table and so for every item
278 // of alphabet.
279 for (uint I=0;I<Size;I++)
280 {
281 // Get the current bit length.
282 byte CurBitLength=LengthTable[I] & 0xf;
283
284 if (CurBitLength!=0)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected