get the tree of a deflated block with dynamic tree, the tree itself is also Huffman compressed with a known tree*/
| 1128 | |
| 1129 | /*get the tree of a deflated block with dynamic tree, the tree itself is also Huffman compressed with a known tree*/ |
| 1130 | static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d, |
| 1131 | LodePNGBitReader* reader) { |
| 1132 | /*make sure that length values that aren't filled in will be 0, or a wrong tree will be generated*/ |
| 1133 | unsigned error = 0; |
| 1134 | unsigned n, HLIT, HDIST, HCLEN, i; |
| 1135 | |
| 1136 | /*see comments in deflateDynamic for explanation of the context and these variables, it is analogous*/ |
| 1137 | unsigned* bitlen_ll = 0; /*lit,len code lengths*/ |
| 1138 | unsigned* bitlen_d = 0; /*dist code lengths*/ |
| 1139 | /*code length code lengths ("clcl"), the bit lengths of the huffman tree used to compress bitlen_ll and bitlen_d*/ |
| 1140 | unsigned* bitlen_cl = 0; |
| 1141 | HuffmanTree tree_cl; /*the code tree for code length codes (the huffman tree for compressed huffman trees)*/ |
| 1142 | |
| 1143 | if(!ensureBits17(reader, 14)) return 49; /*error: the bit pointer is or will go past the memory*/ |
| 1144 | |
| 1145 | /*number of literal/length codes + 257. Unlike the spec, the value 257 is added to it here already*/ |
| 1146 | HLIT = readBits(reader, 5) + 257; |
| 1147 | /*number of distance codes. Unlike the spec, the value 1 is added to it here already*/ |
| 1148 | HDIST = readBits(reader, 5) + 1; |
| 1149 | /*number of code length codes. Unlike the spec, the value 4 is added to it here already*/ |
| 1150 | HCLEN = readBits(reader, 4) + 4; |
| 1151 | |
| 1152 | bitlen_cl = (unsigned*)lodepng_malloc(NUM_CODE_LENGTH_CODES * sizeof(unsigned)); |
| 1153 | if(!bitlen_cl) return 83 /*alloc fail*/; |
| 1154 | |
| 1155 | HuffmanTree_init(&tree_cl); |
| 1156 | |
| 1157 | while(!error) { |
| 1158 | /*read the code length codes out of 3 * (amount of code length codes) bits*/ |
| 1159 | if(lodepng_gtofl(reader->bp, HCLEN * 3, reader->bitsize)) { |
| 1160 | ERROR_BREAK(50); /*error: the bit pointer is or will go past the memory*/ |
| 1161 | } |
| 1162 | for(i = 0; i != HCLEN; ++i) { |
| 1163 | ensureBits9(reader, 3); /*out of bounds already checked above */ |
| 1164 | bitlen_cl[CLCL_ORDER[i]] = readBits(reader, 3); |
| 1165 | } |
| 1166 | for(i = HCLEN; i != NUM_CODE_LENGTH_CODES; ++i) { |
| 1167 | bitlen_cl[CLCL_ORDER[i]] = 0; |
| 1168 | } |
| 1169 | |
| 1170 | error = HuffmanTree_makeFromLengths(&tree_cl, bitlen_cl, NUM_CODE_LENGTH_CODES, 7); |
| 1171 | if(error) break; |
| 1172 | |
| 1173 | /*now we can use this tree to read the lengths for the tree that this function will return*/ |
| 1174 | bitlen_ll = (unsigned*)lodepng_malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned)); |
| 1175 | bitlen_d = (unsigned*)lodepng_malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned)); |
| 1176 | if(!bitlen_ll || !bitlen_d) ERROR_BREAK(83 /*alloc fail*/); |
| 1177 | lodepng_memset(bitlen_ll, 0, NUM_DEFLATE_CODE_SYMBOLS * sizeof(*bitlen_ll)); |
| 1178 | lodepng_memset(bitlen_d, 0, NUM_DISTANCE_SYMBOLS * sizeof(*bitlen_d)); |
| 1179 | |
| 1180 | /*i is the current symbol we're reading in the part that contains the code lengths of lit/len and dist codes*/ |
| 1181 | i = 0; |
| 1182 | while(i < HLIT + HDIST) { |
| 1183 | unsigned code; |
| 1184 | ensureBits25(reader, 22); /* up to 15 bits for huffman code, up to 7 extra bits below*/ |
| 1185 | code = huffmanDecodeSymbol(reader, &tree_cl); |
| 1186 | if(code <= 15) /*a length code*/ { |
| 1187 | if(i < HLIT) bitlen_ll[i] = code; |
no test coverage detected