/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.0M | static int rd_byte(reader* r) { |
14 | 11.0M | return r->p < r->end ? *r->p++ : -1; |
15 | 11.0M | } |
16 | | |
17 | 2.41M | static int rd_u16(reader* r) { |
18 | 2.41M | int lo = rd_byte(r), hi = rd_byte(r); |
19 | 2.41M | return lo < 0 || hi < 0 ? -1 : lo | (hi << 8); |
20 | 2.41M | } |
21 | | |
22 | | /* Skip a sequence of data sub-blocks, including the zero terminator. */ |
23 | 80.0k | static int skip_subblocks(reader* r) { |
24 | 80.0k | int n; |
25 | 192k | while ((n = rd_byte(r)) > 0) { |
26 | 112k | if (r->end - r->p < n) |
27 | 61 | return 0; |
28 | 112k | r->p += n; |
29 | 112k | } |
30 | 80.0k | return n == 0; |
31 | 80.0k | } |
32 | | |
33 | | /* Row `r` of an interlaced image is stored in output row interlace_row(r). */ |
34 | 34.3M | static int interlace_row(int r, int height) { |
35 | 34.3M | static const int start[4] = {0, 4, 2, 1}, step[4] = {8, 8, 4, 2}; |
36 | 34.3M | int pass; |
37 | 107M | for (pass = 0; pass < 4; ++pass) { |
38 | 107M | int n = height > start[pass] |
39 | 107M | ? (height - start[pass] + step[pass] - 1) / step[pass] : 0; |
40 | 107M | if (r < n) |
41 | 34.3M | return start[pass] + r * step[pass]; |
42 | 72.8M | r -= n; |
43 | 72.8M | } |
44 | 0 | return -1; |
45 | 34.3M | } |
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 | 450k | uint8_t* out, size_t npixels) { |
53 | 450k | uint16_t prefix[4096]; |
54 | 450k | uint8_t suffix[4096], first[4096]; |
55 | 450k | uint16_t length[4096]; |
56 | 450k | int clear = 1 << mcb, eoi = clear + 1; |
57 | 450k | int width = mcb + 1, next = clear + 2, prev = -1, ncodes = 0, i; |
58 | 450k | size_t bitpos = 0, nbits = len * 8, pos = 0; |
59 | | |
60 | 15.3M | 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 | 250M | for (;;) { |
67 | 250M | int code = 0, b, cur; |
68 | 250M | if (bitpos + width > nbits) |
69 | 31 | return "LZW data ends before EOI (or EOI is truncated)"; |
70 | 2.90G | for (b = 0; b < width; ++b, ++bitpos) |
71 | 2.65G | code |= ((data[bitpos >> 3] >> (bitpos & 7)) & 1) << b; |
72 | 250M | ++ncodes; |
73 | | |
74 | 250M | if (ncodes == 1 && code != clear) |
75 | 18 | return "LZW data does not start with CLEAR"; |
76 | 250M | if (code == clear) { |
77 | 489k | width = mcb + 1; |
78 | 489k | next = clear + 2; |
79 | 489k | prev = -1; |
80 | 489k | continue; |
81 | 489k | } |
82 | 249M | if (code == eoi) |
83 | 450k | break; |
84 | 249M | if (prev < 0) { |
85 | 489k | if (code > clear) |
86 | 4 | return "first code after CLEAR is not a literal"; |
87 | 489k | cur = code; |
88 | 248M | } else if (code < next) { |
89 | 225M | cur = code; |
90 | 225M | } else if (code == next && next < 4096) { |
91 | 23.4M | cur = -1; /* KwKwK */ |
92 | 23.4M | } else |
93 | 209 | return "LZW code beyond end of table"; |
94 | | |
95 | 249M | if (prev >= 0 && next < 4096) { |
96 | 243M | prefix[next] = (uint16_t) prev; |
97 | 243M | first[next] = first[prev]; |
98 | 243M | suffix[next] = cur < 0 ? first[prev] : first[cur]; |
99 | 243M | length[next] = (uint16_t) (length[prev] + 1); |
100 | 243M | if (cur < 0) |
101 | 23.4M | cur = next; |
102 | 243M | ++next; |
103 | 243M | if (next == (1 << width) && width < 12) |
104 | 938k | ++width; |
105 | 243M | } |
106 | | |
107 | | /* emit string for `cur` */ |
108 | 249M | if (npixels - pos < length[cur]) |
109 | 66 | return "LZW data decodes to too many pixels"; |
110 | 249M | { |
111 | 249M | int c = cur; |
112 | 249M | size_t k = pos + length[cur]; |
113 | 8.53G | while (k > pos) { |
114 | 8.28G | out[--k] = suffix[c]; |
115 | 8.28G | c = prefix[c]; |
116 | 8.28G | } |
117 | 249M | pos += length[cur]; |
118 | 249M | } |
119 | 249M | prev = code; |
120 | 249M | } |
121 | | |
122 | 450k | if (pos != npixels) |
123 | 79 | return "LZW data decodes to too few pixels"; |
124 | 450k | if (nbits - bitpos >= 8) |
125 | 15 | return "unexpected data after EOI"; |
126 | 450k | return NULL; |
127 | 450k | } |
128 | | |
129 | 451k | static const char* parse_image(reader* r, gc_file* gf, const int gce[3]) { |
130 | 451k | gc_image* gi; |
131 | 451k | int flags, mcb, n; |
132 | 451k | size_t npixels, datalen = 0, datacap = 256; |
133 | 451k | uint8_t* data; |
134 | 451k | uint8_t* raw; |
135 | 451k | const char* err; |
136 | 451k | int y; |
137 | | |
138 | 451k | gf->images = (gc_image*) realloc(gf->images, |
139 | 451k | sizeof(gc_image) * (gf->nimages + 1)); |
140 | 451k | gi = &gf->images[gf->nimages]; |
141 | 451k | memset(gi, 0, sizeof(*gi)); |
142 | 451k | gi->left = rd_u16(r); |
143 | 451k | gi->top = rd_u16(r); |
144 | 451k | gi->width = rd_u16(r); |
145 | 451k | gi->height = rd_u16(r); |
146 | 451k | flags = rd_byte(r); |
147 | 451k | if (flags < 0) |
148 | 25 | return "truncated image descriptor"; |
149 | 451k | gi->interlace = (flags & 0x40) != 0; |
150 | 451k | if (flags & 0x80) { |
151 | 27.9k | gi->has_local_colormap = 1; |
152 | 27.9k | gi->local_colormap_size = 2 << (flags & 7); |
153 | 27.9k | if (r->end - r->p < 3 * gi->local_colormap_size) |
154 | 3 | return "truncated local colormap"; |
155 | 27.9k | gi->local_colormap = r->p; |
156 | 27.9k | r->p += 3 * gi->local_colormap_size; |
157 | 27.9k | } |
158 | 451k | gi->transparent = gce[0]; |
159 | 451k | gi->disposal = gce[1]; |
160 | 451k | gi->delay = gce[2]; |
161 | 451k | ++gf->nimages; /* gc_free frees gi->pixels from here on */ |
162 | | |
163 | 451k | if (gi->left + gi->width > 65535 || gi->top + gi->height > 65535) |
164 | 26 | return "image extends beyond 65535x65535"; |
165 | | |
166 | 451k | mcb = rd_byte(r); |
167 | 451k | if (mcb < 2 || mcb > 8) |
168 | 358 | return "bad LZW minimum code size"; |
169 | 451k | gi->min_code_size = mcb; |
170 | | |
171 | 451k | data = (uint8_t*) malloc(datacap); |
172 | 2.15M | while ((n = rd_byte(r)) > 0) { |
173 | 1.70M | if (r->end - r->p < n) { |
174 | 105 | free(data); |
175 | 105 | return "truncated image data sub-block"; |
176 | 105 | } |
177 | 1.70M | if (datalen + n > datacap) { |
178 | 367k | while (datalen + n > datacap) |
179 | 183k | datacap *= 2; |
180 | 183k | data = (uint8_t*) realloc(data, datacap); |
181 | 183k | } |
182 | 1.70M | memcpy(data + datalen, r->p, n); |
183 | 1.70M | datalen += n; |
184 | 1.70M | r->p += n; |
185 | 1.70M | } |
186 | 450k | if (n < 0) { |
187 | 24 | free(data); |
188 | 24 | return "missing image data terminator"; |
189 | 24 | } |
190 | | |
191 | 450k | npixels = (size_t) gi->width * gi->height; |
192 | 450k | raw = (uint8_t*) malloc(npixels ? npixels : 1); |
193 | 450k | err = lzw_decode(data, datalen, mcb, raw, npixels); |
194 | 450k | free(data); |
195 | 450k | if (err) { |
196 | 422 | free(raw); |
197 | 422 | return err; |
198 | 422 | } |
199 | | |
200 | 450k | if (!gi->interlace || gi->height <= 1) |
201 | 400k | gi->pixels = raw; |
202 | 50.3k | else { |
203 | 50.3k | gi->pixels = (uint8_t*) malloc(npixels); |
204 | 34.3M | for (y = 0; y < gi->height; ++y) |
205 | 34.3M | memcpy(gi->pixels + (size_t) interlace_row(y, gi->height) * gi->width, |
206 | 34.3M | raw + (size_t) y * gi->width, gi->width); |
207 | 50.3k | free(raw); |
208 | 50.3k | } |
209 | 450k | return NULL; |
210 | 450k | } |
211 | | |
212 | 133k | const char* gc_parse(const uint8_t* data, size_t len, gc_file* gf) { |
213 | 133k | reader r; |
214 | 133k | int flags, t; |
215 | 133k | int gce[3] = {-1, 0, 0}; /* transparent, disposal, delay */ |
216 | 133k | const char* err; |
217 | | |
218 | 133k | memset(gf, 0, sizeof(*gf)); |
219 | 133k | gf->loopcount = -1; |
220 | 133k | r.p = data; |
221 | 133k | r.end = data + len; |
222 | | |
223 | 133k | if (len < 13 || (memcmp(data, "GIF87a", 6) != 0 |
224 | 102k | && memcmp(data, "GIF89a", 6) != 0)) |
225 | 3.70k | return "bad GIF header"; |
226 | 129k | r.p += 6; |
227 | 129k | gf->screen_width = rd_u16(&r); |
228 | 129k | gf->screen_height = rd_u16(&r); |
229 | 129k | flags = rd_byte(&r); |
230 | 129k | r.p += 2; /* background, aspect ratio */ |
231 | 129k | if (flags & 0x80) { |
232 | 128k | gf->global_colormap_size = 2 << (flags & 7); |
233 | 128k | if (r.end - r.p < 3 * gf->global_colormap_size) |
234 | 10 | return "truncated global colormap"; |
235 | 128k | gf->global_colormap = r.p; |
236 | 128k | r.p += 3 * gf->global_colormap_size; |
237 | 128k | } |
238 | | |
239 | 1.01M | while ((t = rd_byte(&r)) != 0x3B) { |
240 | 882k | if (t == 0x2C) { |
241 | 451k | if ((err = parse_image(&r, gf, gce))) |
242 | 963 | return err; |
243 | 450k | gce[0] = -1; |
244 | 450k | gce[1] = gce[2] = 0; |
245 | 450k | } else if (t == 0x21) { |
246 | 430k | int label = rd_byte(&r); |
247 | 430k | if (label == 0xF9) { |
248 | | /* graphic control extension: exactly one 4-byte block */ |
249 | 350k | int size = rd_byte(&r), packed = rd_byte(&r), |
250 | 350k | delay = rd_u16(&r), transp = rd_byte(&r); |
251 | 350k | if (size != 4 || transp < 0 || rd_byte(&r) != 0) |
252 | 51 | return "malformed graphic control extension"; |
253 | 350k | gce[0] = (packed & 1) ? transp : -1; |
254 | 350k | gce[1] = (packed >> 2) & 7; |
255 | 350k | gce[2] = delay; |
256 | 350k | } else if (label == 0xFF && r.end - r.p >= 12 && r.p[0] == 11 |
257 | 26.3k | && memcmp(r.p + 1, "NETSCAPE2.0", 11) == 0) { |
258 | | /* loop count: a 3-byte sub-block {1, count (2 bytes)} */ |
259 | 14.7k | r.p += 12; |
260 | 14.7k | if (r.end - r.p >= 4 && r.p[0] == 3 && r.p[1] == 1) |
261 | 13.9k | gf->loopcount = r.p[2] | (r.p[3] << 8); |
262 | 14.7k | if (!skip_subblocks(&r)) |
263 | 23 | return "truncated extension"; |
264 | 65.3k | } else if (label < 0 || !skip_subblocks(&r)) |
265 | 58 | return "truncated extension"; |
266 | 430k | } else if (t < 0) |
267 | 13 | return "missing GIF trailer"; |
268 | 803 | else |
269 | 803 | return "unknown block type"; |
270 | 882k | } |
271 | 127k | if (r.p != r.end) |
272 | 3 | return "data after GIF trailer"; |
273 | 127k | return NULL; |
274 | 127k | } |
275 | | |
276 | 133k | void gc_free(gc_file* gf) { |
277 | 133k | int i; |
278 | 584k | for (i = 0; i < gf->nimages; ++i) |
279 | 451k | free(gf->images[i].pixels); |
280 | 133k | free(gf->images); |
281 | 133k | memset(gf, 0, sizeof(*gf)); |
282 | 133k | } |