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-2021 by the
10 * Privoxy team. https://www.privoxy.org/
12 * Based on the GIF file format specification (see
13 * https://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)
135 * Sanity check: Make sure the source buffer contains
136 * data and there's work to be done.
138 if (src->buffer == NULL || src->size == 0 || length == 0)
144 * Sanity check: Can't copy more data than we have
146 if (src->offset + length > src->size)
152 * Ensure that dst can hold the new data
154 if (buf_extend(dst, length))
160 * Now that it's safe, memcpy() the desired amount of
161 * data from src to dst and adjust the offsets
163 memcpy(dst->buffer + dst->offset, src->buffer + src->offset, length);
164 src->offset += length;
165 dst->offset += length;
172 /*********************************************************************
174 * Function : buf_getbyte
176 * Description : Safely gets a byte from a given binbuffer at a
180 * 1 : src = Pointer to the source binbuffer
181 * 2 : offset = Offset to the desired byte
183 * Returns : The byte on success, or 0 on failure
185 *********************************************************************/
186 static unsigned char buf_getbyte(const struct binbuffer *src, size_t offset)
188 if (src->offset + offset < src->size)
190 return (unsigned char)*(src->buffer + src->offset + offset);
200 /*********************************************************************
202 * Function : gif_skip_data_block
204 * Description : Safely advances the offset of a given struct binbuffer
205 * that contains a GIF image and whose offset is
206 * positioned at the start of a data block, behind
210 * 1 : buf = Pointer to the binbuffer
212 * Returns : 0 on success, or 1 on failure
214 *********************************************************************/
215 static int gif_skip_data_block(struct binbuffer *buf)
220 * Data blocks are sequences of chunks, which are headed
221 * by a one-byte length field, with the last chunk having
224 while((c = buf_getbyte(buf, 0)) != '\0')
226 buf->offset += (size_t)c + 1;
227 if (buf->offset >= buf->size - 1)
239 /*********************************************************************
241 * Function : gif_extract_image
243 * Description : Safely extracts an image data block from a given
244 * struct binbuffer that contains a GIF image and whose
245 * offset is positioned at the start of a data block
246 * into a given destination binbuffer.
249 * 1 : src = Pointer to the source binbuffer
250 * 2 : dst = Pointer to the destination binbuffer
252 * Returns : 0 on success, or 1 on failure
254 *********************************************************************/
255 static int gif_extract_image(struct binbuffer *src, struct binbuffer *dst)
260 * Remember the colormap flag and copy the image head
262 c = buf_getbyte(src, 9);
263 if (buf_copy(src, dst, 10))
269 * If the image has a local colormap, copy it.
273 int map_length = 3 * (1 << ((c & 0x07) + 1));
276 log_error(LOG_LEVEL_DEANIMATE,
277 "colormap length = %d (%c)?", map_length, c);
280 if (buf_copy(src, dst, (size_t)map_length))
285 if (buf_copy(src, dst, 1)) return 1;
288 * Copy the image chunk by chunk.
290 while((c = buf_getbyte(src, 0)) != '\0')
292 if (buf_copy(src, dst, 1 + (size_t) c)) return 1;
294 if (buf_copy(src, dst, 1)) return 1;
297 * Trim and rewind the dst buffer
299 if (NULL == (dst->buffer = (char *)realloc(dst->buffer, dst->offset))) return 1;
300 dst->size = dst->offset;
307 /*********************************************************************
309 * Function : gif_deanimate
311 * Description : Deanimate a given GIF image, i.e. given a GIF with
312 * an (optional) image block and an arbitrary number
313 * of image extension blocks, produce an output GIF with
314 * only one image block that contains the last image
315 * (extension) block of the original.
316 * Also strip Comments, Application extensions, etc.
319 * 1 : src = Pointer to the source binbuffer
320 * 2 : dst = Pointer to the destination binbuffer
321 * 3 : get_first_image = Flag: If set, get the first image
322 * If unset (default), get the last
324 * Returns : 0 on success, or 1 on failure
326 *********************************************************************/
327 int gif_deanimate(struct binbuffer *src, struct binbuffer *dst, int get_first_image)
330 struct binbuffer *image;
331 int image_buffered = 0;
333 if (NULL == src || NULL == dst)
342 c = buf_getbyte(src, 10);
345 * Check & copy GIF header
347 if (strncmp(src->buffer, "GIF89a", 6) && strncmp(src->buffer, "GIF87a", 6))
353 if (buf_copy(src, dst, 13))
360 * Look for global colormap and copy if found.
364 int map_length = 3 * (1 << ((c & 0x07) + 1));
367 log_error(LOG_LEVEL_DEANIMATE,
368 "colormap length = %d (%c)?", map_length, c);
371 if (buf_copy(src, dst, (size_t)map_length))
378 * Reserve a buffer for the current image block
380 image = zalloc_or_die(sizeof(*image));
383 * Parse the GIF block by block and copy the relevant
386 while (src->offset < src->size)
388 switch (buf_getbyte(src, 0))
391 * End-of-GIF Marker: Append current image if we got
395 if (image->size == 0) goto failed;
399 * Image block: Extract to current image buffer.
402 if (image_buffered == 1)
404 /* Discard previous image. */
408 if (gif_extract_image(src, image)) goto failed;
410 if (get_first_image) goto write;
414 * Extension block: Look at next byte and decide
417 switch (buf_getbyte(src, 1))
420 * Image extension: Copy extension header
421 * and continue looking for new blocks.
424 if (image_buffered == 1)
429 if (buf_copy(src, image, 8)) goto failed;
433 * Application extension: Skip
436 if ((src->offset += 14) >= src->size || gif_skip_data_block(src)) goto failed;
440 * Comment extension: Skip
443 if ((src->offset += 2) >= src->size || gif_skip_data_block(src)) goto failed;
447 * Plain text extension: Skip
450 if ((src->offset += 15) >= src->size || gif_skip_data_block(src)) goto failed;
454 * Ooops, what type of extension is that?
462 * Ooops, what type of block is that?
468 } /* -END- while src */
471 * Either we got here by goto, or because the GIF is
472 * bogus and EOF was reached before an end-of-gif marker
481 * Append the current image to dst and return
485 if (buf_copy(image, dst, image->size)) goto failed;
486 if (buf_extend(dst, 1)) goto failed;
487 *(dst->buffer + dst->offset++) = 0x3b;