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