| 3096 | } |
| 3097 | |
| 3098 | static int stbi__process_marker(stbi__jpeg *z, int m) |
| 3099 | { |
| 3100 | int L; |
| 3101 | switch (m) { |
| 3102 | case STBI__MARKER_none: // no marker found |
| 3103 | return stbi__err("expected marker","Corrupt JPEG"); |
| 3104 | |
| 3105 | case 0xDD: // DRI - specify restart interval |
| 3106 | if (stbi__get16be(z->s) != 4) return stbi__err("bad DRI len","Corrupt JPEG"); |
| 3107 | z->restart_interval = stbi__get16be(z->s); |
| 3108 | return 1; |
| 3109 | |
| 3110 | case 0xDB: // DQT - define quantization table |
| 3111 | L = stbi__get16be(z->s)-2; |
| 3112 | while (L > 0) { |
| 3113 | int q = stbi__get8(z->s); |
| 3114 | int p = q >> 4, sixteen = (p != 0); |
| 3115 | int t = q & 15,i; |
| 3116 | if (p != 0 && p != 1) return stbi__err("bad DQT type","Corrupt JPEG"); |
| 3117 | if (t > 3) return stbi__err("bad DQT table","Corrupt JPEG"); |
| 3118 | |
| 3119 | for (i=0; i < 64; ++i) |
| 3120 | z->dequant[t][stbi__jpeg_dezigzag[i]] = (stbi__uint16)(sixteen ? stbi__get16be(z->s) : stbi__get8(z->s)); |
| 3121 | L -= (sixteen ? 129 : 65); |
| 3122 | } |
| 3123 | return L==0; |
| 3124 | |
| 3125 | case 0xC4: // DHT - define huffman table |
| 3126 | L = stbi__get16be(z->s)-2; |
| 3127 | while (L > 0) { |
| 3128 | stbi_uc *v; |
| 3129 | int sizes[16],i,n=0; |
| 3130 | int q = stbi__get8(z->s); |
| 3131 | int tc = q >> 4; |
| 3132 | int th = q & 15; |
| 3133 | if (tc > 1 || th > 3) return stbi__err("bad DHT header","Corrupt JPEG"); |
| 3134 | for (i=0; i < 16; ++i) { |
| 3135 | sizes[i] = stbi__get8(z->s); |
| 3136 | n += sizes[i]; |
| 3137 | } |
| 3138 | if(n > 256) return stbi__err("bad DHT header","Corrupt JPEG"); // Loop over i < n would write past end of values! |
| 3139 | L -= 17; |
| 3140 | if (tc == 0) { |
| 3141 | if (!stbi__build_huffman(z->huff_dc+th, sizes)) return 0; |
| 3142 | v = z->huff_dc[th].values; |
| 3143 | } else { |
| 3144 | if (!stbi__build_huffman(z->huff_ac+th, sizes)) return 0; |
| 3145 | v = z->huff_ac[th].values; |
| 3146 | } |
| 3147 | for (i=0; i < n; ++i) |
| 3148 | v[i] = stbi__get8(z->s); |
| 3149 | if (tc != 0) |
| 3150 | stbi__build_fast_ac(z->fast_ac[th], z->huff_ac + th); |
| 3151 | L -= n; |
| 3152 | } |
| 3153 | return L==0; |
| 3154 | } |
| 3155 |
no test coverage detected