1 const char deanimate_rcs[] = "$Id: deanimate.c,v 2.1 2002/08/26 11:06:27 sarantis Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/src/deanimate.c,v $
6 * Purpose : Declares functions to manipulate binary images on the
7 * fly. High-level functions include:
8 * - Deanimation of GIF images
9 * - Fixup of malformed comment block in JPEG headers
11 * Functions declared include: gif_deanimate, buf_free,
12 * buf_copy, buf_getbyte, gif_skip_data_block,
13 * gif_extract_image and jpeg_inspect
15 * Copyright : Written by and Copyright (C) 2001 - 2004 by the the
16 * SourceForge Privoxy team. http://www.privoxy.org/
18 * Based on the GIF file format specification (see
19 * http://tronche.com/computer-graphics/gif/gif89a.html)
20 * and ideas from the Image::DeAnim Perl module by
21 * Ken MacFarlane, <ksm+cpan@universal.dca.net>
23 * This program is free software; you can redistribute it
24 * and/or modify it under the terms of the GNU General
25 * Public License as published by the Free Software
26 * Foundation; either version 2 of the License, or (at
27 * your option) any later version.
29 * This program is distributed in the hope that it will
30 * be useful, but WITHOUT ANY WARRANTY; without even the
31 * implied warranty of MERCHANTABILITY or FITNESS FOR A
32 * PARTICULAR PURPOSE. See the GNU General Public
33 * License for more details.
35 * The GNU General Public License should be included with
36 * this file. If not, you can view it at
37 * http://www.gnu.org/copyleft/gpl.html
38 * or write to the Free Software Foundation, Inc., 59
39 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
42 * $Log: deanimate.c,v $
43 * Revision 2.1 2002/08/26 11:06:27 sarantis
44 * Fix typo failiure -> failure
46 * Revision 2.0 2002/06/04 14:34:21 jongfoster
47 * Moving source files to src/
49 * Revision 1.12 2002/05/12 21:36:29 jongfoster
50 * Correcting function comments
52 * Revision 1.11 2002/03/26 22:29:54 swa
53 * we have a new homepage!
55 * Revision 1.10 2002/03/24 13:25:43 swa
56 * name change related issues
58 * Revision 1.9 2002/03/13 00:27:04 jongfoster
61 * Revision 1.8 2002/03/09 19:42:47 jongfoster
62 * Fixing more warnings
64 * Revision 1.7 2002/03/08 17:46:04 jongfoster
65 * Fixing int/size_t warnings
67 * Revision 1.6 2002/03/07 03:46:17 oes
68 * Fixed compiler warnings
70 * Revision 1.5 2001/09/10 10:16:06 oes
71 * Silenced compiler warnings
73 * Revision 1.4 2001/07/18 12:28:49 oes
74 * - Added feature for extracting the first frame
76 * - Separated image buffer extension into buf_extend
77 * - Extended gif deanimation to GIF87a (untested!)
80 * Revision 1.3 2001/07/15 13:57:50 jongfoster
81 * Adding #includes string.h and miscutil.h
83 * Revision 1.2 2001/07/13 13:46:20 oes
84 * Introduced GIF deanimation feature
87 **********************************************************************/
97 #include "deanimate.h"
100 const char deanimate_h_rcs[] = DEANIMATE_H_VERSION;
102 /*********************************************************************
104 * Function : buf_free
106 * Description : Safely frees a struct binbuffer
109 * 1 : buf = Pointer to the binbuffer to be freed
113 *********************************************************************/
114 void buf_free(struct binbuffer *buf)
116 if (buf == NULL) return;
118 if (buf->buffer != NULL)
128 /*********************************************************************
130 * Function : buf_extend
132 * Description : Ensure that a given binbuffer can hold a given amount
133 * of bytes, by reallocating its buffer if necessary.
134 * Allocate new mem in chunks of 1024 bytes, so we don't
135 * have to realloc() too often.
138 * 1 : buf = Pointer to the binbuffer
139 * 2 : length = Desired minimum size
142 * Returns : 0 on success, 1 on failure.
144 *********************************************************************/
145 int buf_extend(struct binbuffer *buf, size_t length)
149 if (buf->offset + length > buf->size)
151 buf->size = ((buf->size + length + (size_t)1023) & ~(size_t)1023);
152 newbuf = (char *)realloc(buf->buffer, buf->size);
161 buf->buffer = newbuf;
170 /*********************************************************************
172 * Function : buf_copy
174 * Description : Safely copies a given amount of bytes from one
175 * struct binbuffer to another, advancing the
176 * offsets appropriately.
179 * 1 : src = Pointer to the source binbuffer
180 * 2 : dst = Pointer to the destination binbuffer
181 * 3 : length = Number of bytes to be copied
183 * Returns : 0 on success, 1 on failure.
185 *********************************************************************/
186 int buf_copy(struct binbuffer *src, struct binbuffer *dst, size_t length)
190 * Sanity check: Can't copy more data than we have
192 if (src->offset + length > src->size)
198 * Ensure that dst can hold the new data
200 if (buf_extend(dst, length))
206 * Now that it's safe, memcpy() the desired amount of
207 * data from src to dst and adjust the offsets
209 memcpy(dst->buffer + dst->offset, src->buffer + src->offset, length);
210 src->offset += length;
211 dst->offset += length;
218 /*********************************************************************
220 * Function : buf_getbyte
222 * Description : Safely gets a byte from a given binbuffer at a
226 * 1 : src = Pointer to the source binbuffer
227 * 2 : offset = Offset to the desired byte
229 * Returns : The byte on success, or 0 on failure
231 *********************************************************************/
232 unsigned char buf_getbyte(struct binbuffer *src, size_t offset)
234 if (src->offset + offset < src->size)
236 return (unsigned char)*(src->buffer + src->offset + offset);
246 /*********************************************************************
248 * Function : gif_skip_data_block
250 * Description : Safely advances the offset of a given struct binbuffer
251 * that contains a GIF image and whose offset is
252 * positioned at the start of a data block, behind
256 * 1 : buf = Pointer to the binbuffer
258 * Returns : 0 on success, or 1 on failure
260 *********************************************************************/
261 int gif_skip_data_block(struct binbuffer *buf)
266 * Data blocks are sequences of chunks, which are headed
267 * by a one-byte length field, with the last chunk having
270 while((c = buf_getbyte(buf, 0)) != '\0')
272 if ((buf->offset += c + 1) >= buf->size - 1)
284 /*********************************************************************
286 * Function : gif_extract_image
288 * Description : Safely extracts an image data block from a given
289 * struct binbuffer that contains a GIF image and whose
290 * offset is positioned at the start of a data block
291 * into a given destination binbuffer.
294 * 1 : src = Pointer to the source binbuffer
295 * 2 : dst = Pointer to the destination binbuffer
297 * Returns : 0 on success, or 1 on failure
299 *********************************************************************/
300 int gif_extract_image(struct binbuffer *src, struct binbuffer *dst)
305 * Remember the colormap flag and copy the image head
307 c = buf_getbyte(src, 9);
308 if (buf_copy(src, dst, 10))
314 * If the image has a local colormap, copy it.
318 if (buf_copy(src, dst, (size_t) 3 * (1 << ((c & 0x07) + 1))))
323 if (buf_copy(src, dst, 1)) return 1;
326 * Copy the image chunk by chunk.
328 while((c = buf_getbyte(src, 0)) != '\0')
330 if (buf_copy(src, dst, 1 + (size_t) c)) return 1;
332 if (buf_copy(src, dst, 1)) return 1;
335 * Trim and rewind the dst buffer
337 if (NULL == (dst->buffer = (char *)realloc(dst->buffer, dst->offset))) return 1;
338 dst->size = dst->offset;
345 /*********************************************************************
347 * Function : gif_deanimate
349 * Description : Deanimate a given GIF image, i.e. given a GIF with
350 * an (optional) image block and an arbitrary number
351 * of image extension blocks, produce an output GIF with
352 * only one image block that contains the last image
353 * (extenstion) block of the original.
354 * Also strip Comments, Application extenstions, etc.
357 * 1 : src = Pointer to the source binbuffer
358 * 2 : dst = Pointer to the destination binbuffer
359 * 3 : get_first_image = Flag: If set, get the first image
360 * If unset (default), get the last
362 * Returns : 0 on success, or 1 on failure
364 *********************************************************************/
365 int gif_deanimate(struct binbuffer *src, struct binbuffer *dst, int get_first_image)
368 struct binbuffer *image;
370 if (NULL == src || NULL == dst)
375 c = buf_getbyte(src, 10);
378 * Check & copy GIF header
380 if (strncmp(src->buffer, "GIF89a", 6) && strncmp(src->buffer, "GIF87a", 6))
386 if (buf_copy(src, dst, 13))
393 * Look for global colormap and copy if found.
397 if (buf_copy(src, dst, (size_t) 3 * (1 << ((c & 0x07) + 1))))
404 * Reserve a buffer for the current image block
406 if (NULL == (image = (struct binbuffer *)zalloc(sizeof(*image))))
412 * Parse the GIF block by block and copy the relevant
415 while(src->offset < src->size)
417 switch(buf_getbyte(src, 0))
420 * End-of-GIF Marker: Append current image and return
426 * Image block: Extract to current image buffer.
430 if (gif_extract_image(src, image)) goto failed;
431 if (get_first_image) goto write;
435 * Extension block: Look at next byte and decide
438 switch (buf_getbyte(src, 1))
441 * Image extension: Copy extension header and image
442 * to the current image buffer
446 if (buf_copy(src, image, 8) || buf_getbyte(src, 0) != 0x2c) goto failed;
447 if (gif_extract_image(src, image)) goto failed;
448 if (get_first_image) goto write;
452 * Application extension: Skip
455 if ((src->offset += 14) >= src->size || gif_skip_data_block(src)) goto failed;
459 * Comment extension: Skip
462 if ((src->offset += 2) >= src->size || gif_skip_data_block(src)) goto failed;
466 * Plain text extension: Skip
469 if ((src->offset += 15) >= src->size || gif_skip_data_block(src)) goto failed;
473 * Ooops, what type of extension is that?
481 * Ooops, what type of block is that?
487 } /* -END- while src */
490 * Either we got here by goto, or because the GIF is
491 * bogus and EOF was reached before an end-of-gif marker
500 * Append the current image to dst and return
504 if (buf_copy(image, dst, image->size)) goto failed;
505 if (buf_extend(dst, 1)) goto failed;
506 *(dst->buffer + dst->offset++) = 0x3b;
513 /*********************************************************************
515 * Function : jpeg_inspect
517 * Description : Checks a jpeg image for an invalid length in a
518 * comment block (0xFFFE0000 or 0xFFFE0001) and
519 * changes it to 0xFFFE0002. Defensive strategy
520 * against the exploit:
521 * Microsoft Security Bulletin MS04-028
522 * Buffer Overrun in JPEG Processing (GDI+) Could
523 * Allow Code Execution (833987)
526 * 1 : src = Pointer to the image binbuffer
528 * Returns : 0 on success, or 1 on failure
530 *********************************************************************/
531 int jpeg_inspect(struct binbuffer *src, struct binbuffer *dst)
535 * We process the image using a simple finite state machine,
536 * searching for byte patterns.
538 enum { J_INIT, /* The initial state */
539 J_FF, /* Found byte 0xFF */
540 J_FE, /* Found bytes 0xFF 0xFE */
541 J_00, /* Found bytes 0xFF 0xFE 0x00 */
543 * Found bytes 0xFF 0xDA; short-circuit to done-ness
544 * since this signals the beginning end of headers.
547 short state = J_INIT;
550 if (NULL == src || NULL == dst)
555 if (buf_copy(src, dst, src->size))
560 /* Need to search the jpg for patterns:
561 * 0xFF 0xFE 0x00 0x00
563 * 0xFF 0xFE 0x00 0x01
564 * from beginning until:
566 * (or the end of the buffer)
567 * If found, change the pattern to 0xFF 0xFE 0x00 0x02
570 for (i = 0; i < dst->size; i++)
581 state = J_DA; /* End of headers - we're done with this image. */
594 if ((c == 0x00) || (c == 0x01))
596 dst->buffer[i] = 2; /* Reset comment block size to 2. */
597 log_error(LOG_LEVEL_INFO, "JPEG comment exploit removed.");
599 * I'm unsure if we can have more than one comment block. Just in case,
600 * we'll scan the rest of the header for more by going back to J_INIT
601 * state. If there is no possibility of >1 comment block, we could
602 * short-circuit to done-ness here.