1 /*********************************************************************
3 * File : $Source: /cvsroot/ijbswa/current/deanimate.c,v $
5 * Purpose : Declares functions to manipulate binary images on the
6 * fly. High-level functions include:
7 * - Deanimation of GIF images
9 * Copyright : Written by and Copyright (C) 2001 - 2004, 2006 by the
10 * Privoxy team. https://www.privoxy.org/
12 * Based on the GIF file format specification (see
13 * http://tronche.com/computer-graphics/gif/gif89a.html)
14 * and ideas from the Image::DeAnim Perl module by
15 * Ken MacFarlane, <ksm+cpan@universal.dca.net>
17 * This program is free software; you can redistribute it
18 * and/or modify it under the terms of the GNU General
19 * Public License as published by the Free Software
20 * Foundation; either version 2 of the License, or (at
21 * your option) any later version.
23 * This program is distributed in the hope that it will
24 * be useful, but WITHOUT ANY WARRANTY; without even the
25 * implied warranty of MERCHANTABILITY or FITNESS FOR A
26 * PARTICULAR PURPOSE. See the GNU General Public
27 * License for more details.
29 * The GNU General Public License should be included with
30 * this file. If not, you can view it at
31 * http://www.gnu.org/copyleft/gpl.html
32 * or write to the Free Software Foundation, Inc., 59
33 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 **********************************************************************/
45 #include "deanimate.h"
48 /*********************************************************************
52 * Description : Safely frees a struct binbuffer
55 * 1 : buf = Pointer to the binbuffer to be freed
59 *********************************************************************/
60 void buf_free(struct binbuffer *buf)
62 if (buf == NULL) return;
64 if (buf->buffer != NULL)
74 /*********************************************************************
76 * Function : buf_extend
78 * Description : Ensure that a given binbuffer can hold a given amount
79 * of bytes, by reallocating its buffer if necessary.
80 * Allocate new mem in chunks of 1024 bytes, so we don't
81 * have to realloc() too often.
84 * 1 : buf = Pointer to the binbuffer
85 * 2 : length = Desired minimum size
88 * Returns : 0 on success, 1 on failure.
90 *********************************************************************/
91 static int buf_extend(struct binbuffer *buf, size_t length)
95 if (buf->offset + length > buf->size)
97 buf->size = ((buf->size + length + (size_t)1023) & ~(size_t)1023);
98 newbuf = (char *)realloc(buf->buffer, buf->size);
107 buf->buffer = newbuf;
116 /*********************************************************************
118 * Function : buf_copy
120 * Description : Safely copies a given amount of bytes from one
121 * struct binbuffer to another, advancing the
122 * offsets appropriately.
125 * 1 : src = Pointer to the source binbuffer
126 * 2 : dst = Pointer to the destination binbuffer
127 * 3 : length = Number of bytes to be copied
129 * Returns : 0 on success, 1 on failure.
131 *********************************************************************/
132 static int buf_copy(struct binbuffer *src, struct binbuffer *dst, size_t length)
136 * Sanity check: Can't copy more data than we have
138 if (src->offset + length > src->size)
144 * Ensure that dst can hold the new data
146 if (buf_extend(dst, length))
152 * Now that it's safe, memcpy() the desired amount of
153 * data from src to dst and adjust the offsets
155 memcpy(dst->buffer + dst->offset, src->buffer + src->offset, length);
156 src->offset += length;
157 dst->offset += length;
164 /*********************************************************************
166 * Function : buf_getbyte
168 * Description : Safely gets a byte from a given binbuffer at a
172 * 1 : src = Pointer to the source binbuffer
173 * 2 : offset = Offset to the desired byte
175 * Returns : The byte on success, or 0 on failure
177 *********************************************************************/
178 static unsigned char buf_getbyte(const struct binbuffer *src, size_t offset)
180 if (src->offset + offset < src->size)
182 return (unsigned char)*(src->buffer + src->offset + offset);
192 /*********************************************************************
194 * Function : gif_skip_data_block
196 * Description : Safely advances the offset of a given struct binbuffer
197 * that contains a GIF image and whose offset is
198 * positioned at the start of a data block, behind
202 * 1 : buf = Pointer to the binbuffer
204 * Returns : 0 on success, or 1 on failure
206 *********************************************************************/
207 static int gif_skip_data_block(struct binbuffer *buf)
212 * Data blocks are sequences of chunks, which are headed
213 * by a one-byte length field, with the last chunk having
216 while((c = buf_getbyte(buf, 0)) != '\0')
218 buf->offset += (size_t)c + 1;
219 if (buf->offset >= buf->size - 1)
231 /*********************************************************************
233 * Function : gif_extract_image
235 * Description : Safely extracts an image data block from a given
236 * struct binbuffer that contains a GIF image and whose
237 * offset is positioned at the start of a data block
238 * into a given destination binbuffer.
241 * 1 : src = Pointer to the source binbuffer
242 * 2 : dst = Pointer to the destination binbuffer
244 * Returns : 0 on success, or 1 on failure
246 *********************************************************************/
247 static int gif_extract_image(struct binbuffer *src, struct binbuffer *dst)
252 * Remember the colormap flag and copy the image head
254 c = buf_getbyte(src, 9);
255 if (buf_copy(src, dst, 10))
261 * If the image has a local colormap, copy it.
265 int map_length = 3 * (1 << ((c & 0x07) + 1));
268 log_error(LOG_LEVEL_DEANIMATE,
269 "colormap length = %d (%c)?", map_length, c);
272 if (buf_copy(src, dst, (size_t)map_length))
277 if (buf_copy(src, dst, 1)) return 1;
280 * Copy the image chunk by chunk.
282 while((c = buf_getbyte(src, 0)) != '\0')
284 if (buf_copy(src, dst, 1 + (size_t) c)) return 1;
286 if (buf_copy(src, dst, 1)) return 1;
289 * Trim and rewind the dst buffer
291 if (NULL == (dst->buffer = (char *)realloc(dst->buffer, dst->offset))) return 1;
292 dst->size = dst->offset;
299 /*********************************************************************
301 * Function : gif_deanimate
303 * Description : Deanimate a given GIF image, i.e. given a GIF with
304 * an (optional) image block and an arbitrary number
305 * of image extension blocks, produce an output GIF with
306 * only one image block that contains the last image
307 * (extension) block of the original.
308 * Also strip Comments, Application extensions, etc.
311 * 1 : src = Pointer to the source binbuffer
312 * 2 : dst = Pointer to the destination binbuffer
313 * 3 : get_first_image = Flag: If set, get the first image
314 * If unset (default), get the last
316 * Returns : 0 on success, or 1 on failure
318 *********************************************************************/
319 int gif_deanimate(struct binbuffer *src, struct binbuffer *dst, int get_first_image)
322 struct binbuffer *image;
324 if (NULL == src || NULL == dst)
329 c = buf_getbyte(src, 10);
332 * Check & copy GIF header
334 if (strncmp(src->buffer, "GIF89a", 6) && strncmp(src->buffer, "GIF87a", 6))
340 if (buf_copy(src, dst, 13))
347 * Look for global colormap and copy if found.
351 int map_length = 3 * (1 << ((c & 0x07) + 1));
354 log_error(LOG_LEVEL_DEANIMATE,
355 "colormap length = %d (%c)?", map_length, c);
358 if (buf_copy(src, dst, (size_t)map_length))
365 * Reserve a buffer for the current image block
367 image = zalloc_or_die(sizeof(*image));
370 * Parse the GIF block by block and copy the relevant
373 while(src->offset < src->size)
375 switch(buf_getbyte(src, 0))
378 * End-of-GIF Marker: Append current image and return
384 * Image block: Extract to current image buffer.
388 if (gif_extract_image(src, image)) goto failed;
389 if (get_first_image) goto write;
393 * Extension block: Look at next byte and decide
396 switch (buf_getbyte(src, 1))
399 * Image extension: Copy extension header and image
400 * to the current image buffer
404 if (buf_copy(src, image, 8) || buf_getbyte(src, 0) != 0x2c) goto failed;
405 if (gif_extract_image(src, image)) goto failed;
406 if (get_first_image) goto write;
410 * Application extension: Skip
413 if ((src->offset += 14) >= src->size || gif_skip_data_block(src)) goto failed;
417 * Comment extension: Skip
420 if ((src->offset += 2) >= src->size || gif_skip_data_block(src)) goto failed;
424 * Plain text extension: Skip
427 if ((src->offset += 15) >= src->size || gif_skip_data_block(src)) goto failed;
431 * Ooops, what type of extension is that?
439 * Ooops, what type of block is that?
445 } /* -END- while src */
448 * Either we got here by goto, or because the GIF is
449 * bogus and EOF was reached before an end-of-gif marker
458 * Append the current image to dst and return
462 if (buf_copy(image, dst, image->size)) goto failed;
463 if (buf_extend(dst, 1)) goto failed;
464 *(dst->buffer + dst->offset++) = 0x3b;