| 4307 | { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13}; |
| 4308 | |
| 4309 | static int stbi__parse_huffman_block(stbi__zbuf *a) |
| 4310 | { |
| 4311 | char *zout = a->zout; |
| 4312 | for(;;) { |
| 4313 | int z = stbi__zhuffman_decode(a, &a->z_length); |
| 4314 | if (z < 256) { |
| 4315 | if (z < 0) return stbi__err("bad huffman code","Corrupt PNG"); // error in huffman codes |
| 4316 | if (zout >= a->zout_end) { |
| 4317 | if (!stbi__zexpand(a, zout, 1)) return 0; |
| 4318 | zout = a->zout; |
| 4319 | } |
| 4320 | *zout++ = (char) z; |
| 4321 | } else { |
| 4322 | stbi_uc *p; |
| 4323 | int len,dist; |
| 4324 | if (z == 256) { |
| 4325 | a->zout = zout; |
| 4326 | if (a->hit_zeof_once && a->num_bits < 16) { |
| 4327 | // The first time we hit zeof, we inserted 16 extra zero bits into our bit |
| 4328 | // buffer so the decoder can just do its speculative decoding. But if we |
| 4329 | // actually consumed any of those bits (which is the case when num_bits < 16), |
| 4330 | // the stream actually read past the end so it is malformed. |
| 4331 | return stbi__err("unexpected end","Corrupt PNG"); |
| 4332 | } |
| 4333 | return 1; |
| 4334 | } |
| 4335 | if (z >= 286) return stbi__err("bad huffman code","Corrupt PNG"); // per DEFLATE, length codes 286 and 287 must not appear in compressed data |
| 4336 | z -= 257; |
| 4337 | len = stbi__zlength_base[z]; |
| 4338 | if (stbi__zlength_extra[z]) len += stbi__zreceive(a, stbi__zlength_extra[z]); |
| 4339 | z = stbi__zhuffman_decode(a, &a->z_distance); |
| 4340 | if (z < 0 || z >= 30) return stbi__err("bad huffman code","Corrupt PNG"); // per DEFLATE, distance codes 30 and 31 must not appear in compressed data |
| 4341 | dist = stbi__zdist_base[z]; |
| 4342 | if (stbi__zdist_extra[z]) dist += stbi__zreceive(a, stbi__zdist_extra[z]); |
| 4343 | if (zout - a->zout_start < dist) return stbi__err("bad dist","Corrupt PNG"); |
| 4344 | if (len > a->zout_end - zout) { |
| 4345 | if (!stbi__zexpand(a, zout, len)) return 0; |
| 4346 | zout = a->zout; |
| 4347 | } |
| 4348 | p = (stbi_uc *) (zout - dist); |
| 4349 | if (dist == 1) { // run of one byte; common in images. |
| 4350 | stbi_uc v = *p; |
| 4351 | if (len) { do *zout++ = v; while (--len); } |
| 4352 | } else { |
| 4353 | if (len) { do *zout++ = *p++; while (--len); } |
| 4354 | } |
| 4355 | } |
| 4356 | } |
| 4357 | } |
| 4358 | |
| 4359 | static int stbi__compute_huffman_codes(stbi__zbuf *a) |
| 4360 | { |
no test coverage detected