/src/gifsicle/test/fuzz/gifcheck.c
Line | Count | Source |
1 | | /* gifcheck.c - Strict, independent GIF parser for checking gifsicle output. |
2 | | This file is part of gifsicle's fuzzing harness. See gifcheck.h. */ |
3 | | |
4 | | #include "gifcheck.h" |
5 | | #include <stdlib.h> |
6 | | #include <string.h> |
7 | | |
8 | | typedef struct { |
9 | | const uint8_t* p; |
10 | | const uint8_t* end; |
11 | | } reader; |
12 | | |
13 | 11.4M | static int rd_byte(reader* r) { |
14 | 11.4M | return r->p < r->end ? *r->p++ : -1; |
15 | 11.4M | } |
16 | | |
17 | 2.46M | static int rd_u16(reader* r) { |
18 | 2.46M | int lo = rd_byte(r), hi = rd_byte(r); |
19 | 2.46M | return lo < 0 || hi < 0 ? -1 : lo | (hi << 8); |
20 | 2.46M | } |
21 | | |
22 | | /* Skip a sequence of data sub-blocks, including the zero terminator. */ |
23 | 91.5k | static int skip_subblocks(reader* r) { |
24 | 91.5k | int n; |
25 | 229k | while ((n = rd_byte(r)) > 0) { |
26 | 138k | if (r->end - r->p < n) |
27 | 67 | return 0; |
28 | 138k | r->p += n; |
29 | 138k | } |
30 | 91.4k | return n == 0; |
31 | 91.5k | } |
32 | | |
33 | | /* Row `r` of an interlaced image is stored in output row interlace_row(r). */ |
34 | 36.9M | static int interlace_row(int r, int height) { |
35 | 36.9M | static const int start[4] = {0, 4, 2, 1}, step[4] = {8, 8, 4, 2}; |
36 | 36.9M | int pass; |
37 | 115M | for (pass = 0; pass < 4; ++pass) { |
38 | 115M | int n = height > start[pass] |
39 | 115M | ? (height - start[pass] + step[pass] - 1) / step[pass] : 0; |
40 | 115M | if (r < n) |
41 | 36.9M | return start[pass] + r * step[pass]; |
42 | 78.5M | r -= n; |
43 | 78.5M | } |
44 | 0 | return -1; |
45 | 36.9M | } |
46 | | |
47 | | /* Strictly decode an LZW stream. Code width grows as soon as the table |
48 | | reaches a power of two (as in giflib and browsers). The stream must start |
49 | | with CLEAR, must end with a complete EOI, must decode to exactly `npixels` |
50 | | pixels, and may have fewer than 8 bits of padding after EOI. */ |
51 | | static const char* lzw_decode(const uint8_t* data, size_t len, int mcb, |
52 | 456k | uint8_t* out, size_t npixels) { |
53 | 456k | uint16_t prefix[4096]; |
54 | 456k | uint8_t suffix[4096], first[4096]; |
55 | 456k | uint16_t length[4096]; |
56 | 456k | int clear = 1 << mcb, eoi = clear + 1; |
57 | 456k | int width = mcb + 1, next = clear + 2, prev = -1, ncodes = 0, i; |
58 | 456k | size_t bitpos = 0, nbits = len * 8, pos = 0; |
59 | | |
60 | 15.2M | for (i = 0; i < clear; ++i) { |
61 | 14.8M | prefix[i] = 0; |
62 | 14.8M | suffix[i] = first[i] = (uint8_t) i; |
63 | 14.8M | length[i] = 1; |
64 | 14.8M | } |
65 | | |
66 | 284M | for (;;) { |
67 | 284M | int code = 0, b, cur; |
68 | 284M | if (bitpos + width > nbits) |
69 | 25 | return "LZW data ends before EOI (or EOI is truncated)"; |
70 | 3.31G | for (b = 0; b < width; ++b, ++bitpos) |
71 | 3.03G | code |= ((data[bitpos >> 3] >> (bitpos & 7)) & 1) << b; |
72 | 284M | ++ncodes; |
73 | | |
74 | 284M | if (ncodes == 1 && code != clear) |
75 | 25 | return "LZW data does not start with CLEAR"; |
76 | 284M | if (code == clear) { |
77 | 504k | width = mcb + 1; |
78 | 504k | next = clear + 2; |
79 | 504k | prev = -1; |
80 | 504k | continue; |
81 | 504k | } |
82 | 283M | if (code == eoi) |
83 | 456k | break; |
84 | 283M | if (prev < 0) { |
85 | 504k | if (code > clear) |
86 | 7 | return "first code after CLEAR is not a literal"; |
87 | 504k | cur = code; |
88 | 282M | } else if (code < next) { |
89 | 260M | cur = code; |
90 | 260M | } else if (code == next && next < 4096) { |
91 | 22.4M | cur = -1; /* KwKwK */ |
92 | 22.4M | } else |
93 | 248 | return "LZW code beyond end of table"; |
94 | | |
95 | 283M | if (prev >= 0 && next < 4096) { |
96 | 277M | prefix[next] = (uint16_t) prev; |
97 | 277M | first[next] = first[prev]; |
98 | 277M | suffix[next] = cur < 0 ? first[prev] : first[cur]; |
99 | 277M | length[next] = (uint16_t) (length[prev] + 1); |
100 | 277M | if (cur < 0) |
101 | 22.4M | cur = next; |
102 | 277M | ++next; |
103 | 277M | if (next == (1 << width) && width < 12) |
104 | 997k | ++width; |
105 | 277M | } |
106 | | |
107 | | /* emit string for `cur` */ |
108 | 283M | if (npixels - pos < length[cur]) |
109 | 77 | return "LZW data decodes to too many pixels"; |
110 | 283M | { |
111 | 283M | int c = cur; |
112 | 283M | size_t k = pos + length[cur]; |
113 | 8.55G | while (k > pos) { |
114 | 8.26G | out[--k] = suffix[c]; |
115 | 8.26G | c = prefix[c]; |
116 | 8.26G | } |
117 | 283M | pos += length[cur]; |
118 | 283M | } |
119 | 283M | prev = code; |
120 | 283M | } |
121 | | |
122 | 456k | if (pos != npixels) |
123 | 117 | return "LZW data decodes to too few pixels"; |
124 | 456k | if (nbits - bitpos >= 8) |
125 | 5 | return "unexpected data after EOI"; |
126 | 456k | return NULL; |
127 | 456k | } |
128 | | |
129 | 457k | static const char* parse_image(reader* r, gc_file* gf, const int gce[3]) { |
130 | 457k | gc_image* gi; |
131 | 457k | int flags, mcb, n; |
132 | 457k | size_t npixels, datalen = 0, datacap = 256; |
133 | 457k | uint8_t* data; |
134 | 457k | uint8_t* raw; |
135 | 457k | const char* err; |
136 | 457k | int y; |
137 | | |
138 | 457k | gf->images = (gc_image*) realloc(gf->images, |
139 | 457k | sizeof(gc_image) * (gf->nimages + 1)); |
140 | 457k | gi = &gf->images[gf->nimages]; |
141 | 457k | memset(gi, 0, sizeof(*gi)); |
142 | 457k | gi->left = rd_u16(r); |
143 | 457k | gi->top = rd_u16(r); |
144 | 457k | gi->width = rd_u16(r); |
145 | 457k | gi->height = rd_u16(r); |
146 | 457k | flags = rd_byte(r); |
147 | 457k | if (flags < 0) |
148 | 21 | return "truncated image descriptor"; |
149 | 457k | gi->interlace = (flags & 0x40) != 0; |
150 | 457k | if (flags & 0x80) { |
151 | 26.6k | gi->has_local_colormap = 1; |
152 | 26.6k | gi->local_colormap_size = 2 << (flags & 7); |
153 | 26.6k | if (r->end - r->p < 3 * gi->local_colormap_size) |
154 | 6 | return "truncated local colormap"; |
155 | 26.6k | gi->local_colormap = r->p; |
156 | 26.6k | r->p += 3 * gi->local_colormap_size; |
157 | 26.6k | } |
158 | 457k | gi->transparent = gce[0]; |
159 | 457k | gi->disposal = gce[1]; |
160 | 457k | gi->delay = gce[2]; |
161 | 457k | ++gf->nimages; /* gc_free frees gi->pixels from here on */ |
162 | | |
163 | 457k | if (gi->left + gi->width > 65535 || gi->top + gi->height > 65535) |
164 | 35 | return "image extends beyond 65535x65535"; |
165 | | |
166 | 457k | mcb = rd_byte(r); |
167 | 457k | if (mcb < 2 || mcb > 8) |
168 | 414 | return "bad LZW minimum code size"; |
169 | 457k | gi->min_code_size = mcb; |
170 | | |
171 | 457k | data = (uint8_t*) malloc(datacap); |
172 | 2.33M | while ((n = rd_byte(r)) > 0) { |
173 | 1.88M | if (r->end - r->p < n) { |
174 | 149 | free(data); |
175 | 149 | return "truncated image data sub-block"; |
176 | 149 | } |
177 | 1.88M | if (datalen + n > datacap) { |
178 | 363k | while (datalen + n > datacap) |
179 | 181k | datacap *= 2; |
180 | 181k | data = (uint8_t*) realloc(data, datacap); |
181 | 181k | } |
182 | 1.88M | memcpy(data + datalen, r->p, n); |
183 | 1.88M | datalen += n; |
184 | 1.88M | r->p += n; |
185 | 1.88M | } |
186 | 456k | if (n < 0) { |
187 | 18 | free(data); |
188 | 18 | return "missing image data terminator"; |
189 | 18 | } |
190 | | |
191 | 456k | npixels = (size_t) gi->width * gi->height; |
192 | 456k | raw = (uint8_t*) malloc(npixels ? npixels : 1); |
193 | 456k | err = lzw_decode(data, datalen, mcb, raw, npixels); |
194 | 456k | free(data); |
195 | 456k | if (err) { |
196 | 504 | free(raw); |
197 | 504 | return err; |
198 | 504 | } |
199 | | |
200 | 456k | if (!gi->interlace || gi->height <= 1) |
201 | 404k | gi->pixels = raw; |
202 | 51.8k | else { |
203 | 51.8k | gi->pixels = (uint8_t*) malloc(npixels); |
204 | 37.0M | for (y = 0; y < gi->height; ++y) |
205 | 36.9M | memcpy(gi->pixels + (size_t) interlace_row(y, gi->height) * gi->width, |
206 | 36.9M | raw + (size_t) y * gi->width, gi->width); |
207 | 51.8k | free(raw); |
208 | 51.8k | } |
209 | 456k | return NULL; |
210 | 456k | } |
211 | | |
212 | 140k | const char* gc_parse(const uint8_t* data, size_t len, gc_file* gf) { |
213 | 140k | reader r; |
214 | 140k | int flags, t; |
215 | 140k | int gce[3] = {-1, 0, 0}; /* transparent, disposal, delay */ |
216 | 140k | const char* err; |
217 | | |
218 | 140k | memset(gf, 0, sizeof(*gf)); |
219 | 140k | gf->loopcount = -1; |
220 | 140k | r.p = data; |
221 | 140k | r.end = data + len; |
222 | | |
223 | 140k | if (len < 13 || (memcmp(data, "GIF87a", 6) != 0 |
224 | 111k | && memcmp(data, "GIF89a", 6) != 0)) |
225 | 3.49k | return "bad GIF header"; |
226 | 137k | r.p += 6; |
227 | 137k | gf->screen_width = rd_u16(&r); |
228 | 137k | gf->screen_height = rd_u16(&r); |
229 | 137k | flags = rd_byte(&r); |
230 | 137k | r.p += 2; /* background, aspect ratio */ |
231 | 137k | if (flags & 0x80) { |
232 | 136k | gf->global_colormap_size = 2 << (flags & 7); |
233 | 136k | if (r.end - r.p < 3 * gf->global_colormap_size) |
234 | 11 | return "truncated global colormap"; |
235 | 136k | gf->global_colormap = r.p; |
236 | 136k | r.p += 3 * gf->global_colormap_size; |
237 | 136k | } |
238 | | |
239 | 1.04M | while ((t = rd_byte(&r)) != 0x3B) { |
240 | 908k | if (t == 0x2C) { |
241 | 457k | if ((err = parse_image(&r, gf, gce))) |
242 | 1.14k | return err; |
243 | 456k | gce[0] = -1; |
244 | 456k | gce[1] = gce[2] = 0; |
245 | 456k | } else if (t == 0x21) { |
246 | 450k | int label = rd_byte(&r); |
247 | 450k | if (label == 0xF9) { |
248 | | /* graphic control extension: exactly one 4-byte block */ |
249 | 358k | int size = rd_byte(&r), packed = rd_byte(&r), |
250 | 358k | delay = rd_u16(&r), transp = rd_byte(&r); |
251 | 358k | if (size != 4 || transp < 0 || rd_byte(&r) != 0) |
252 | 59 | return "malformed graphic control extension"; |
253 | 358k | gce[0] = (packed & 1) ? transp : -1; |
254 | 358k | gce[1] = (packed >> 2) & 7; |
255 | 358k | gce[2] = delay; |
256 | 358k | } else if (label == 0xFF && r.end - r.p >= 12 && r.p[0] == 11 |
257 | 31.0k | && memcmp(r.p + 1, "NETSCAPE2.0", 11) == 0) { |
258 | | /* loop count: a 3-byte sub-block {1, count (2 bytes)} */ |
259 | 17.5k | r.p += 12; |
260 | 17.5k | if (r.end - r.p >= 4 && r.p[0] == 3 && r.p[1] == 1) |
261 | 17.5k | gf->loopcount = r.p[2] | (r.p[3] << 8); |
262 | 17.5k | if (!skip_subblocks(&r)) |
263 | 20 | return "truncated extension"; |
264 | 73.9k | } else if (label < 0 || !skip_subblocks(&r)) |
265 | 65 | return "truncated extension"; |
266 | 450k | } else if (t < 0) |
267 | 16 | return "missing GIF trailer"; |
268 | 981 | else |
269 | 981 | return "unknown block type"; |
270 | 908k | } |
271 | 134k | if (r.p != r.end) |
272 | 4 | return "data after GIF trailer"; |
273 | 134k | return NULL; |
274 | 134k | } |
275 | | |
276 | 140k | void gc_free(gc_file* gf) { |
277 | 140k | int i; |
278 | 598k | for (i = 0; i < gf->nimages; ++i) |
279 | 457k | free(gf->images[i].pixels); |
280 | 140k | free(gf->images); |
281 | 140k | memset(gf, 0, sizeof(*gf)); |
282 | 140k | } |