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