MCPcopy Create free account
hub / github.com/creatale/node-dv / getTreeInflateDynamic

Function getTreeInflateDynamic

deps/lodepng/lodepng.cpp:991–1136  ·  view source on GitHub ↗

get the tree of a deflated block with dynamic tree, the tree itself is also Huffman compressed with a known tree*/

Source from the content-addressed store, hash-verified

989
990/*get the tree of a deflated block with dynamic tree, the tree itself is also Huffman compressed with a known tree*/
991static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d,
992 const unsigned char* in, size_t* bp, size_t inlength)
993{
994 /*make sure that length values that aren't filled in will be 0, or a wrong tree will be generated*/
995 unsigned error = 0;
996 unsigned n, HLIT, HDIST, HCLEN, i;
997 size_t inbitlength = inlength * 8;
998
999 /*see comments in deflateDynamic for explanation of the context and these variables, it is analogous*/
1000 unsigned* bitlen_ll = 0; /*lit,len code lengths*/
1001 unsigned* bitlen_d = 0; /*dist code lengths*/
1002 /*code length code lengths ("clcl"), the bit lengths of the huffman tree used to compress bitlen_ll and bitlen_d*/
1003 unsigned* bitlen_cl = 0;
1004 HuffmanTree tree_cl; /*the code tree for code length codes (the huffman tree for compressed huffman trees)*/
1005
1006 if((*bp) + 14 > (inlength << 3)) return 49; /*error: the bit pointer is or will go past the memory*/
1007
1008 /*number of literal/length codes + 257. Unlike the spec, the value 257 is added to it here already*/
1009 HLIT = readBitsFromStream(bp, in, 5) + 257;
1010 /*number of distance codes. Unlike the spec, the value 1 is added to it here already*/
1011 HDIST = readBitsFromStream(bp, in, 5) + 1;
1012 /*number of code length codes. Unlike the spec, the value 4 is added to it here already*/
1013 HCLEN = readBitsFromStream(bp, in, 4) + 4;
1014
1015 if((*bp) + HCLEN * 3 > (inlength << 3)) return 50; /*error: the bit pointer is or will go past the memory*/
1016
1017 HuffmanTree_init(&tree_cl);
1018
1019 while(!error)
1020 {
1021 /*read the code length codes out of 3 * (amount of code length codes) bits*/
1022
1023 bitlen_cl = (unsigned*)lodepng_malloc(NUM_CODE_LENGTH_CODES * sizeof(unsigned));
1024 if(!bitlen_cl) ERROR_BREAK(83 /*alloc fail*/);
1025
1026 for(i = 0; i != NUM_CODE_LENGTH_CODES; ++i)
1027 {
1028 if(i < HCLEN) bitlen_cl[CLCL_ORDER[i]] = readBitsFromStream(bp, in, 3);
1029 else bitlen_cl[CLCL_ORDER[i]] = 0; /*if not, it must stay 0*/
1030 }
1031
1032 error = HuffmanTree_makeFromLengths(&tree_cl, bitlen_cl, NUM_CODE_LENGTH_CODES, 7);
1033 if(error) break;
1034
1035 /*now we can use this tree to read the lengths for the tree that this function will return*/
1036 bitlen_ll = (unsigned*)lodepng_malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned));
1037 bitlen_d = (unsigned*)lodepng_malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned));
1038 if(!bitlen_ll || !bitlen_d) ERROR_BREAK(83 /*alloc fail*/);
1039 for(i = 0; i != NUM_DEFLATE_CODE_SYMBOLS; ++i) bitlen_ll[i] = 0;
1040 for(i = 0; i != NUM_DISTANCE_SYMBOLS; ++i) bitlen_d[i] = 0;
1041
1042 /*i is the current symbol we're reading in the part that contains the code lengths of lit/len and dist codes*/
1043 i = 0;
1044 while(i < HLIT + HDIST)
1045 {
1046 unsigned code = huffmanDecodeSymbol(in, bp, &tree_cl, inbitlength);
1047 if(code <= 15) /*a length code*/
1048 {

Callers 1

inflateHuffmanBlockFunction · 0.85

Calls 7

readBitsFromStreamFunction · 0.85
HuffmanTree_initFunction · 0.85
lodepng_mallocFunction · 0.85
huffmanDecodeSymbolFunction · 0.85
lodepng_freeFunction · 0.85
HuffmanTree_cleanupFunction · 0.85

Tested by

no test coverage detected