aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/gif_extractor.c
blob: 055fd9f97ee355936b4a73c29985cab61486dd57 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
     This file is part of libextractor.
     (C) 2002, 2003, 2009 Vidyut Samanta and Christian Grothoff

     libextractor is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     libextractor is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with libextractor; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
 */

#include "platform.h"
#include "extractor.h"
#include "pack.h"

#define DEBUG_GIF 0
#if DEBUG_GIF
#define PRINT(a,b) fprintf(stderr,a,b)
#else
#define PRINT(a,b)
#endif

struct GifHeader
{
  char gif[3];
  char version[3];
  uint16_t screen_width;
  uint16_t screen_height;
  unsigned char flags;
#define HEADER_FLAGS__SIZE_OF_GLOBAL_COLOR_TABLE 0x07
#define HEADER_FLAGS__SORT_FLAG 0x08
#define HEADER_FLAGS__COLOR_RESOLUTION 0x70
#define HEADER_FLAGS__GLOBAL_COLOR_TABLE_FLAG 0x80
  unsigned char background_color_index;
  unsigned char pixel_aspect_ratio;
};

#define GIF_HEADER_SIZE 13
#define GIF_HEADER_SPEC "3b3bhhbbb"
#define GIF_HEADER_FIELDS(p) \
 &(p)->gif,\
 &(p)->version, \
 &(p)->screen_width, \
 &(p)->screen_height, \
 &(p)->flags, \
 &(p)->background_color_index, \
 &(p)->pixel_aspect_ratio

struct GifDescriptor
{
  unsigned char image_separator;
  uint16_t image_left;
  uint16_t image_top;
  uint16_t image_width;
  uint16_t image_height;
  unsigned char flags;
#define DESCRIPTOR_FLAGS__PIXEL_SIZE 0x07
#define DESCRIPTOR_FLAGS__RESERVED 0x18
#define DESCRIPTOR_FLAGS__SORT_FLAG 0x20
#define DESCRIPTOR_FLAGS__INTERLACE_FLAG 0x40
#define DESCRIPTOR_FLAGS__LOCAL_COLOR_TABLE_FLAG 0x80
};

#define GIF_DESCRIPTOR_SIZE 10
#define GIF_DESCRIPTOR_SPEC "chhhhc"
#define GIF_DESCRIPTOR_FIELDS(p) \
 &(p)->image_separator, \
 &(p)->image_left, \
 &(p)->image_top, \
 &(p)->image_width, \
 &(p)->image_height, \
 &(p)->flags

struct GifExtension
{
  unsigned char extension_introducer;
  unsigned char graphic_control_label;
};

/**
 * Skip a data block.
 * @return the position after the block
 */
static size_t
skipDataBlock (const unsigned char *data, size_t pos, const size_t size)
{
  while ((pos < size) && (data[pos] != 0))
    pos += data[pos] + 1;
  return pos + 1;
}

/**
 * skip an extention block
 * @return the position after the block
 */
static size_t
skipExtensionBlock (const unsigned char *data,
                    size_t pos, const size_t size, 
		    const struct GifExtension * ext)
{
  return skipDataBlock (data, pos + sizeof (struct GifExtension), size);
}

/**
 * @return the offset after the global color map
 */
static size_t
skipGlobalColorMap (const unsigned char *data,
                    const size_t size,
		    const struct GifHeader * header)
{
  size_t gct_size;

  if ((header->flags & HEADER_FLAGS__GLOBAL_COLOR_TABLE_FLAG) > 0)
    gct_size =
      3 *
      (1 << ((header->flags & HEADER_FLAGS__SIZE_OF_GLOBAL_COLOR_TABLE) + 1));
  else
    gct_size = 0;
  return GIF_HEADER_SIZE + gct_size;
}

/**
 * @return the offset after the local color map
 */
static size_t
skipLocalColorMap (const unsigned char *data,
                   size_t pos, const size_t size,
		   const struct GifDescriptor * descriptor)
{
  size_t lct_size;

  if (pos + GIF_DESCRIPTOR_SIZE > size)
    return size;
  if ((descriptor->flags & DESCRIPTOR_FLAGS__LOCAL_COLOR_TABLE_FLAG) > 0)
    lct_size =
      3 * (1 << ((descriptor->flags & DESCRIPTOR_FLAGS__PIXEL_SIZE) + 1));
  else
    lct_size = 0;
  return pos + GIF_DESCRIPTOR_SIZE + lct_size;
}

static int
parseComment (const unsigned char *data,
              size_t pos, const size_t size, 
	      EXTRACTOR_MetaDataProcessor proc,
	      void *proc_cls)
{
  size_t length;
  size_t off;
  size_t curr = pos;
  int ret;

  length = 0;
  while ( (curr < size) && 
	  (data[curr] != 0) )
    {
      length += data[curr];
      curr += data[curr] + 1;
      if (length > 65536)
	break;
    }
  if ( (length < 65536) &&
       (curr < size) )
    {
      char comment[length+1];

      curr = pos;
      off = 0;
      while ((data[curr] != 0) && (curr < size))
	{
	  if (off + data[curr] >= size)
	    break;
	  memcpy (&comment[off], 
		  &data[curr] + 1,
		  data[curr]);
	  off += data[curr];
	  curr += data[curr] + 1;
	}
      comment[off] = '\0';
      ret = proc (proc_cls, 
		  "gif",
		  EXTRACTOR_METATYPE_COMMENT,
		  EXTRACTOR_METAFORMAT_UTF8,
		  "text/plain",
		  comment,
		  length+1);
    }
  else
    {
      /* too big */
      ret = 0;
    }
  return ret;
}


int 
EXTRACTOR_gif_extract (const unsigned char *data,
		       size_t size,
		       EXTRACTOR_MetaDataProcessor proc,
		       void *proc_cls,
		       const char *options)
{
  size_t pos;
  struct GifHeader header;
  struct GifDescriptor gd;
  char tmp[128];

  if (size < GIF_HEADER_SIZE)
    return 0;
  EXTRACTOR_common_cat_unpack (data, GIF_HEADER_SPEC, GIF_HEADER_FIELDS (&header));
  if (0 != strncmp (&header.gif[0], "GIF", 3))
    return 0;
  if (0 != strncmp (&header.version[0], "89a", 3))
    return 0;                /* only 89a has support for comments */
  if (0 != proc (proc_cls, 
		 "gif",
		 EXTRACTOR_METATYPE_MIMETYPE,
		 EXTRACTOR_METAFORMAT_UTF8,
		 "text/plain",
		 "image/gif",
		 strlen ("image/gif")+1))
    return 1;
  snprintf (tmp, 
	    sizeof(tmp),
	    "%ux%u", 
	    header.screen_width, header.screen_height);
  if (0 != proc (proc_cls, 
		 "gif",
		 EXTRACTOR_METATYPE_IMAGE_DIMENSIONS,
		 EXTRACTOR_METAFORMAT_UTF8,
		 "text/plain",
		 tmp,
		 strlen (tmp)+1))
    return 1;
  pos = skipGlobalColorMap (data, size, &header);
  PRINT ("global color map ends at %d\n", pos);
  while (pos < size)
    {
      switch (data[pos])
        {
        case ',':              /* image descriptor block */
          PRINT ("skipping local color map %d\n", pos);
          EXTRACTOR_common_cat_unpack (&data[pos],
				       GIF_DESCRIPTOR_SPEC,
				       GIF_DESCRIPTOR_FIELDS (&gd));
          pos = skipLocalColorMap (data, pos, size, &gd);
          break;
        case '!':              /* extension block */
          PRINT ("skipping extension block %d\n", pos);
          if (data[pos + 1] == (unsigned char) 0xFE)
            {
              if (0 != parseComment (data, pos + 2, size, proc, proc_cls))
		return 1;
            }
          pos = skipExtensionBlock (data, pos, size,
                                    (const struct GifExtension *) & data[pos]);
          break;
        case ';':
          PRINT ("hit terminator at %d!\n", pos);
          return 0;        /* terminator! */
        default:               /* raster data block */
          PRINT ("skipping data block at %d\n", pos);
          pos = skipDataBlock (data, pos + 1, size);
          break;
        }
    }
  PRINT ("returning at %d\n", pos);
  return 0;
}