| 4356 | } |
| 4357 | |
| 4358 | static int stbi__compute_huffman_codes(stbi__zbuf *a) |
| 4359 | { |
| 4360 | static const stbi_uc length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 }; |
| 4361 | stbi__zhuffman z_codelength; |
| 4362 | stbi_uc lencodes[286+32+137];//padding for maximum single op |
| 4363 | stbi_uc codelength_sizes[19]; |
| 4364 | int i,n; |
| 4365 | |
| 4366 | int hlit = stbi__zreceive(a,5) + 257; |
| 4367 | int hdist = stbi__zreceive(a,5) + 1; |
| 4368 | int hclen = stbi__zreceive(a,4) + 4; |
| 4369 | int ntot = hlit + hdist; |
| 4370 | |
| 4371 | memset(codelength_sizes, 0, sizeof(codelength_sizes)); |
| 4372 | for (i=0; i < hclen; ++i) { |
| 4373 | int s = stbi__zreceive(a,3); |
| 4374 | codelength_sizes[length_dezigzag[i]] = (stbi_uc) s; |
| 4375 | } |
| 4376 | if (!stbi__zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0; |
| 4377 | |
| 4378 | n = 0; |
| 4379 | while (n < ntot) { |
| 4380 | int c = stbi__zhuffman_decode(a, &z_codelength); |
| 4381 | if (c < 0 || c >= 19) return stbi__err("bad codelengths", "Corrupt PNG"); |
| 4382 | if (c < 16) |
| 4383 | lencodes[n++] = (stbi_uc) c; |
| 4384 | else { |
| 4385 | stbi_uc fill = 0; |
| 4386 | if (c == 16) { |
| 4387 | c = stbi__zreceive(a,2)+3; |
| 4388 | if (n == 0) return stbi__err("bad codelengths", "Corrupt PNG"); |
| 4389 | fill = lencodes[n-1]; |
| 4390 | } else if (c == 17) { |
| 4391 | c = stbi__zreceive(a,3)+3; |
| 4392 | } else if (c == 18) { |
| 4393 | c = stbi__zreceive(a,7)+11; |
| 4394 | } else { |
| 4395 | return stbi__err("bad codelengths", "Corrupt PNG"); |
| 4396 | } |
| 4397 | if (ntot - n < c) return stbi__err("bad codelengths", "Corrupt PNG"); |
| 4398 | memset(lencodes+n, fill, c); |
| 4399 | n += c; |
| 4400 | } |
| 4401 | } |
| 4402 | if (n != ntot) return stbi__err("bad codelengths","Corrupt PNG"); |
| 4403 | if (!stbi__zbuild_huffman(&a->z_length, lencodes, hlit)) return 0; |
| 4404 | if (!stbi__zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0; |
| 4405 | return 1; |
| 4406 | } |
| 4407 | |
| 4408 | static int stbi__parse_uncompressed_block(stbi__zbuf *a) |
| 4409 | { |
no test coverage detected