1 const char deanimate_rcs[] = "$Id: deanimate.c,v 1.20 2009/05/16 13:27:20 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/deanimate.c,v $
6 * Purpose : Declares functions to manipulate binary images on the
7 * fly. High-level functions include:
8 * - Deanimation of GIF images
10 * Functions declared include: gif_deanimate, buf_free,
11 * buf_copy, buf_getbyte, gif_skip_data_block
12 * and gif_extract_image
14 * Copyright : Written by and Copyright (C) 2001 - 2004, 2006 by the
15 * SourceForge Privoxy team. http://www.privoxy.org/
17 * Based on the GIF file format specification (see
18 * http://tronche.com/computer-graphics/gif/gif89a.html)
19 * and ideas from the Image::DeAnim Perl module by
20 * Ken MacFarlane, <ksm+cpan@universal.dca.net>
22 * This program is free software; you can redistribute it
23 * and/or modify it under the terms of the GNU General
24 * Public License as published by the Free Software
25 * Foundation; either version 2 of the License, or (at
26 * your option) any later version.
28 * This program is distributed in the hope that it will
29 * be useful, but WITHOUT ANY WARRANTY; without even the
30 * implied warranty of MERCHANTABILITY or FITNESS FOR A
31 * PARTICULAR PURPOSE. See the GNU General Public
32 * License for more details.
34 * The GNU General Public License should be included with
35 * this file. If not, you can view it at
36 * http://www.gnu.org/copyleft/gpl.html
37 * or write to the Free Software Foundation, Inc., 59
38 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
40 **********************************************************************/
50 #include "deanimate.h"
53 const char deanimate_h_rcs[] = DEANIMATE_H_VERSION;
55 /*********************************************************************
59 * Description : Safely frees a struct binbuffer
62 * 1 : buf = Pointer to the binbuffer to be freed
66 *********************************************************************/
67 void buf_free(struct binbuffer *buf)
69 if (buf == NULL) return;
71 if (buf->buffer != NULL)
81 /*********************************************************************
83 * Function : buf_extend
85 * Description : Ensure that a given binbuffer can hold a given amount
86 * of bytes, by reallocating its buffer if necessary.
87 * Allocate new mem in chunks of 1024 bytes, so we don't
88 * have to realloc() too often.
91 * 1 : buf = Pointer to the binbuffer
92 * 2 : length = Desired minimum size
95 * Returns : 0 on success, 1 on failure.
97 *********************************************************************/
98 static int buf_extend(struct binbuffer *buf, size_t length)
102 if (buf->offset + length > buf->size)
104 buf->size = ((buf->size + length + (size_t)1023) & ~(size_t)1023);
105 newbuf = (char *)realloc(buf->buffer, buf->size);
114 buf->buffer = newbuf;
123 /*********************************************************************
125 * Function : buf_copy
127 * Description : Safely copies a given amount of bytes from one
128 * struct binbuffer to another, advancing the
129 * offsets appropriately.
132 * 1 : src = Pointer to the source binbuffer
133 * 2 : dst = Pointer to the destination binbuffer
134 * 3 : length = Number of bytes to be copied
136 * Returns : 0 on success, 1 on failure.
138 *********************************************************************/
139 static int buf_copy(struct binbuffer *src, struct binbuffer *dst, size_t length)
143 * Sanity check: Can't copy more data than we have
145 if (src->offset + length > src->size)
151 * Ensure that dst can hold the new data
153 if (buf_extend(dst, length))
159 * Now that it's safe, memcpy() the desired amount of
160 * data from src to dst and adjust the offsets
162 memcpy(dst->buffer + dst->offset, src->buffer + src->offset, length);
163 src->offset += length;
164 dst->offset += length;
171 /*********************************************************************
173 * Function : buf_getbyte
175 * Description : Safely gets a byte from a given binbuffer at a
179 * 1 : src = Pointer to the source binbuffer
180 * 2 : offset = Offset to the desired byte
182 * Returns : The byte on success, or 0 on failure
184 *********************************************************************/
185 static unsigned char buf_getbyte(const struct binbuffer *src, size_t offset)
187 if (src->offset + offset < src->size)
189 return (unsigned char)*(src->buffer + src->offset + offset);
199 /*********************************************************************
201 * Function : gif_skip_data_block
203 * Description : Safely advances the offset of a given struct binbuffer
204 * that contains a GIF image and whose offset is
205 * positioned at the start of a data block, behind
209 * 1 : buf = Pointer to the binbuffer
211 * Returns : 0 on success, or 1 on failure
213 *********************************************************************/
214 static int gif_skip_data_block(struct binbuffer *buf)
219 * Data blocks are sequences of chunks, which are headed
220 * by a one-byte length field, with the last chunk having
223 while((c = buf_getbyte(buf, 0)) != '\0')
225 buf->offset += (size_t)c + 1;
226 if (buf->offset >= buf->size - 1)
238 /*********************************************************************
240 * Function : gif_extract_image
242 * Description : Safely extracts an image data block from a given
243 * struct binbuffer that contains a GIF image and whose
244 * offset is positioned at the start of a data block
245 * into a given destination binbuffer.
248 * 1 : src = Pointer to the source binbuffer
249 * 2 : dst = Pointer to the destination binbuffer
251 * Returns : 0 on success, or 1 on failure
253 *********************************************************************/
254 static int gif_extract_image(struct binbuffer *src, struct binbuffer *dst)
259 * Remember the colormap flag and copy the image head
261 c = buf_getbyte(src, 9);
262 if (buf_copy(src, dst, 10))
268 * If the image has a local colormap, copy it.
272 int map_length = 3 * (1 << ((c & 0x07) + 1));
275 log_error(LOG_LEVEL_DEANIMATE,
276 "colormap length = %d (%c)?", map_length, c);
279 if (buf_copy(src, dst, (size_t)map_length))
284 if (buf_copy(src, dst, 1)) return 1;
287 * Copy the image chunk by chunk.
289 while((c = buf_getbyte(src, 0)) != '\0')
291 if (buf_copy(src, dst, 1 + (size_t) c)) return 1;
293 if (buf_copy(src, dst, 1)) return 1;
296 * Trim and rewind the dst buffer
298 if (NULL == (dst->buffer = (char *)realloc(dst->buffer, dst->offset))) return 1;
299 dst->size = dst->offset;
306 /*********************************************************************
308 * Function : gif_deanimate
310 * Description : Deanimate a given GIF image, i.e. given a GIF with
311 * an (optional) image block and an arbitrary number
312 * of image extension blocks, produce an output GIF with
313 * only one image block that contains the last image
314 * (extenstion) block of the original.
315 * Also strip Comments, Application extenstions, etc.
318 * 1 : src = Pointer to the source binbuffer
319 * 2 : dst = Pointer to the destination binbuffer
320 * 3 : get_first_image = Flag: If set, get the first image
321 * If unset (default), get the last
323 * Returns : 0 on success, or 1 on failure
325 *********************************************************************/
326 int gif_deanimate(struct binbuffer *src, struct binbuffer *dst, int get_first_image)
329 struct binbuffer *image;
331 if (NULL == src || NULL == dst)
336 c = buf_getbyte(src, 10);
339 * Check & copy GIF header
341 if (strncmp(src->buffer, "GIF89a", 6) && strncmp(src->buffer, "GIF87a", 6))
347 if (buf_copy(src, dst, 13))
354 * Look for global colormap and copy if found.
358 int map_length = 3 * (1 << ((c & 0x07) + 1));
361 log_error(LOG_LEVEL_DEANIMATE,
362 "colormap length = %d (%c)?", map_length, c);
365 if (buf_copy(src, dst, (size_t)map_length))
372 * Reserve a buffer for the current image block
374 if (NULL == (image = (struct binbuffer *)zalloc(sizeof(*image))))
380 * Parse the GIF block by block and copy the relevant
383 while(src->offset < src->size)
385 switch(buf_getbyte(src, 0))
388 * End-of-GIF Marker: Append current image and return
394 * Image block: Extract to current image buffer.
398 if (gif_extract_image(src, image)) goto failed;
399 if (get_first_image) goto write;
403 * Extension block: Look at next byte and decide
406 switch (buf_getbyte(src, 1))
409 * Image extension: Copy extension header and image
410 * to the current image buffer
414 if (buf_copy(src, image, 8) || buf_getbyte(src, 0) != 0x2c) goto failed;
415 if (gif_extract_image(src, image)) goto failed;
416 if (get_first_image) goto write;
420 * Application extension: Skip
423 if ((src->offset += 14) >= src->size || gif_skip_data_block(src)) goto failed;
427 * Comment extension: Skip
430 if ((src->offset += 2) >= src->size || gif_skip_data_block(src)) goto failed;
434 * Plain text extension: Skip
437 if ((src->offset += 15) >= src->size || gif_skip_data_block(src)) goto failed;
441 * Ooops, what type of extension is that?
449 * Ooops, what type of block is that?
455 } /* -END- while src */
458 * Either we got here by goto, or because the GIF is
459 * bogus and EOF was reached before an end-of-gif marker
468 * Append the current image to dst and return
472 if (buf_copy(image, dst, image->size)) goto failed;
473 if (buf_extend(dst, 1)) goto failed;
474 *(dst->buffer + dst->offset++) = 0x3b;