get the tree of a deflated block with dynamic tree, the tree itself is also Huffman compressed with a known tree*/
| 936 | |
| 937 | /*get the tree of a deflated block with dynamic tree, the tree itself is also Huffman compressed with a known tree*/ |
| 938 | static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d, |
| 939 | const unsigned char* in, size_t* bp, size_t inlength) |
| 940 | { |
| 941 | /*make sure that length values that aren't filled in will be 0, or a wrong tree will be generated*/ |
| 942 | unsigned error = 0; |
| 943 | unsigned n, HLIT, HDIST, HCLEN, i; |
| 944 | size_t inbitlength = inlength * 8; |
| 945 | |
| 946 | /*see comments in deflateDynamic for explanation of the context and these variables, it is analogous*/ |
| 947 | unsigned* bitlen_ll = 0; /*lit,len code lengths*/ |
| 948 | unsigned* bitlen_d = 0; /*dist code lengths*/ |
| 949 | /*code length code lengths ("clcl"), the bit lengths of the huffman tree used to compress bitlen_ll and bitlen_d*/ |
| 950 | unsigned* bitlen_cl = 0; |
| 951 | HuffmanTree tree_cl; /*the code tree for code length codes (the huffman tree for compressed huffman trees)*/ |
| 952 | |
| 953 | if((*bp) >> 3 >= inlength - 2) return 49; /*error: the bit pointer is or will go past the memory*/ |
| 954 | |
| 955 | /*number of literal/length codes + 257. Unlike the spec, the value 257 is added to it here already*/ |
| 956 | HLIT = readBitsFromStream(bp, in, 5) + 257; |
| 957 | /*number of distance codes. Unlike the spec, the value 1 is added to it here already*/ |
| 958 | HDIST = readBitsFromStream(bp, in, 5) + 1; |
| 959 | /*number of code length codes. Unlike the spec, the value 4 is added to it here already*/ |
| 960 | HCLEN = readBitsFromStream(bp, in, 4) + 4; |
| 961 | |
| 962 | HuffmanTree_init(&tree_cl); |
| 963 | |
| 964 | while(!error) |
| 965 | { |
| 966 | /*read the code length codes out of 3 * (amount of code length codes) bits*/ |
| 967 | |
| 968 | bitlen_cl = (unsigned*)lodepng_malloc(NUM_CODE_LENGTH_CODES * sizeof(unsigned)); |
| 969 | if(!bitlen_cl) ERROR_BREAK(83 /*alloc fail*/); |
| 970 | |
| 971 | for(i = 0; i < NUM_CODE_LENGTH_CODES; i++) |
| 972 | { |
| 973 | if(i < HCLEN) bitlen_cl[CLCL_ORDER[i]] = readBitsFromStream(bp, in, 3); |
| 974 | else bitlen_cl[CLCL_ORDER[i]] = 0; /*if not, it must stay 0*/ |
| 975 | } |
| 976 | |
| 977 | error = HuffmanTree_makeFromLengths(&tree_cl, bitlen_cl, NUM_CODE_LENGTH_CODES, 7); |
| 978 | if(error) break; |
| 979 | |
| 980 | /*now we can use this tree to read the lengths for the tree that this function will return*/ |
| 981 | bitlen_ll = (unsigned*)lodepng_malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned)); |
| 982 | bitlen_d = (unsigned*)lodepng_malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned)); |
| 983 | if(!bitlen_ll || !bitlen_d) ERROR_BREAK(83 /*alloc fail*/); |
| 984 | for(i = 0; i < NUM_DEFLATE_CODE_SYMBOLS; i++) bitlen_ll[i] = 0; |
| 985 | for(i = 0; i < NUM_DISTANCE_SYMBOLS; i++) bitlen_d[i] = 0; |
| 986 | |
| 987 | /*i is the current symbol we're reading in the part that contains the code lengths of lit/len and dist codes*/ |
| 988 | i = 0; |
| 989 | while(i < HLIT + HDIST) |
| 990 | { |
| 991 | unsigned code = huffmanDecodeSymbol(in, bp, &tree_cl, inbitlength); |
| 992 | if(code <= 15) /*a length code*/ |
| 993 | { |
| 994 | if(i < HLIT) bitlen_ll[i] = code; |
| 995 | else bitlen_d[i - HLIT] = code; |
no test coverage detected