aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/png_extractor.c
blob: 07243a1ca5ca22d98dec96f72728a7855535fe5b (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
     This file is part of libextractor.
     (C) 2002, 2003, 2004, 2005, 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 <zlib.h>
#include "convert.h"

static char *
stndup (const char *str, size_t n)
{
  char *tmp;
  tmp = malloc (n + 1);
  if (tmp == NULL)
    return NULL;
  tmp[n] = '\0';
  memcpy (tmp, str, n);
  return tmp;
}

/**
 * strnlen is GNU specific, let's redo it here to be
 * POSIX compliant.
 */
static size_t
stnlen (const char *str, size_t maxlen)
{
  size_t ret;
  ret = 0;
  while ((ret < maxlen) && (str[ret] != '\0'))
    ret++;
  return ret;
}


static int
getIntAt (const void *pos)
{
  char p[4];

  memcpy (p, pos, 4);           /* ensure alignment! */
  return *(int *) &p[0];
}


static struct
{
  char *name;
  enum EXTRACTOR_MetaType type;
} tagmap[] =
{
  { "Author", EXTRACTOR_METATYPE_AUTHOR_NAME},
  { "Description", EXTRACTOR_METATYPE_DESCRIPTION},
  { "Comment", EXTRACTOR_METATYPE_COMMENT},
  { "Copyright", EXTRACTOR_METATYPE_COPYRIGHT},
  { "Source", EXTRACTOR_METATYPE_SOURCE_DEVICE },
  { "Creation Time", EXTRACTOR_METATYPE_CREATION_DATE},
  { "Title", EXTRACTOR_METATYPE_TITLE},
  { "Software", EXTRACTOR_METATYPE_PRODUCED_BY_SOFTWARE},
  { "Disclaimer", EXTRACTOR_METATYPE_DISCLAIMER},
  { "Warning", EXTRACTOR_METATYPE_WARNING},
  { NULL, EXTRACTOR_METATYPE_RESERVED }
};


#define ADD(t,s) do { if (0 != (ret = proc (proc_cls, "png", t, EXTRACTOR_METAFORMAT_UTF8, "text/plain", s, strlen(s)+1))) goto FINISH; } while (0)
#define ADDF(t,s) do { if ( (s != NULL) && (0 != (ret = proc (proc_cls, "png", t, EXTRACTOR_METAFORMAT_UTF8, "text/plain", s, strlen(s)+1))) ) { free(s); goto FINISH; } if (s != NULL) free (s); } while (0)


static int
processtEXt (const char *data,
             unsigned int length,
	     EXTRACTOR_MetaDataProcessor proc,
	     void *proc_cls)
{
  char *keyword;
  unsigned int off;
  int i;
  int ret;

  data += 4;
  off = stnlen (data, length) + 1;
  if (off >= length)
    return 0;                /* failed to find '\0' */
  keyword = EXTRACTOR_common_convert_to_utf8 (&data[off], length - off, "ISO-8859-1");
  if (keyword == NULL)
    return 0;
  i = 0;
  ret = 0;
  while (tagmap[i].name != NULL)
    {
      if (0 == strcmp (tagmap[i].name, data))
	{
	  ADDF (tagmap[i].type, keyword);
	  return 0;
	}

      i++;
    }
  ADDF (EXTRACTOR_METATYPE_KEYWORDS, keyword);
 FINISH:
  return ret;
}

static int
processiTXt (const char *data,
             unsigned int length, 
	     EXTRACTOR_MetaDataProcessor proc,
	     void *proc_cls)
{
  unsigned int pos;
  char *keyword;
  const char *language;
  const char *translated;
  int i;
  int compressed;
  char *buf;
  char *lan;
  uLongf bufLen;
  int ret;
  int zret;

  pos = stnlen (data, length) + 1;
  if (pos + 3 >= length)
    return 0;
  compressed = data[pos++];
  if (compressed && (data[pos++] != 0))
    return 0;                /* bad compression method */
  language = &data[pos];
  ret = 0;
  if (stnlen (language, length - pos) > 0)
    {
      lan = stndup (language, length - pos);
      ADDF (EXTRACTOR_METATYPE_LANGUAGE,
	    lan);
    }
  pos += stnlen (language, length - pos) + 1;
  if (pos + 1 >= length)
    return 0;
  translated = &data[pos];      /* already in utf-8! */
  if (stnlen (translated, length - pos) > 0)
    {
      lan = stndup (translated, length - pos);
      ADDF (EXTRACTOR_METATYPE_KEYWORDS,
	    lan);
    }
  pos += stnlen (translated, length - pos) + 1;
  if (pos >= length)
    return 0;

  if (compressed)
    {
      bufLen = 1024 + 2 * (length - pos);
      while (1)
        {
          if (bufLen * 2 < bufLen)
            return 0;
          bufLen *= 2;
          if (bufLen > 50 * (length - pos))
            {
              /* printf("zlib problem"); */
              return 0;
            }
          buf = malloc (bufLen);
          if (buf == NULL)
            {
              /* printf("out of memory"); */
              return 0;      /* out of memory */
            }
          zret = uncompress ((Bytef *) buf,
                            &bufLen,
                            (const Bytef *) &data[pos], length - pos);
          if (zret == Z_OK)
            {
              /* printf("zlib ok"); */
              break;
            }
          free (buf);
          if (zret != Z_BUF_ERROR)
            return 0;        /* unknown error, abort */
        }
      keyword = stndup (buf, bufLen);
      free (buf);
    }
  else
    {
      keyword = stndup (&data[pos], length - pos);
    }
  i = 0;
  while (tagmap[i].name != NULL)
    {
      if (0 == strcmp (tagmap[i].name, data))
	{
	  ADDF (tagmap[i].type, keyword /* already in utf8 */);
	  return 0;
	}
      i++;
    }
  ADDF (EXTRACTOR_METATYPE_COMMENT, keyword);
 FINISH:
  return ret;
}


static int
processIHDR (const char *data,
             unsigned int length, 
	     EXTRACTOR_MetaDataProcessor proc,
	     void *proc_cls)
{
  char tmp[128];
  int ret;

  if (length < 12)
    return 0;
  ret = 0;
  snprintf (tmp,
            sizeof(tmp),
            "%ux%u",
            htonl (getIntAt (&data[4])), htonl (getIntAt (&data[8])));
  ADD (EXTRACTOR_METATYPE_IMAGE_DIMENSIONS, tmp);
 FINISH:
  return ret;
}

static int
processzTXt (const char *data,
             unsigned int length,
	     EXTRACTOR_MetaDataProcessor proc,
	     void *proc_cls)
{
  char *keyword;
  unsigned int off;
  int i;
  char *buf;
  uLongf bufLen;
  int zret;
  int ret;

  data += 4;
  off = stnlen (data, length) + 1;
  if (off >= length)
    return 0;                /* failed to find '\0' */
  if (data[off] != 0)
    return 0;                /* compression method must be 0 */
  off++;
  ret = 0;
  bufLen = 1024 + 2 * (length - off);
  while (1)
    {
      if (bufLen * 2 < bufLen)
        return 0;
      bufLen *= 2;
      if (bufLen > 50 * (length - off))
        {
          /* printf("zlib problem"); */
          return 0;
        }
      buf = malloc (bufLen);
      if (buf == NULL)
        {
          /* printf("out of memory"); */
          return 0;          /* out of memory */
        }
      zret = uncompress ((Bytef *) buf,
			 &bufLen, (const Bytef *) &data[off], length - off);
      if (zret == Z_OK)
        {
          /* printf("zlib ok"); */
          break;
        }
      free (buf);
      if (zret != Z_BUF_ERROR)
        return 0;            /* unknown error, abort */
    }
  keyword = EXTRACTOR_common_convert_to_utf8 (buf, bufLen, "ISO-8859-1");
  free (buf);
  i = 0;
  while (tagmap[i].name != NULL)
    {
      if (0 == strcmp (tagmap[i].name, data))
	{
	  ADDF (tagmap[i].type, keyword);
	  return 0;
	}
      i++;
    }
  ADDF (EXTRACTOR_METATYPE_COMMENT, keyword);
 FINISH:
  return ret;
}

static int
processtIME (const char *data,
             unsigned int length,
	     EXTRACTOR_MetaDataProcessor proc,
	     void *proc_cls)
{
  unsigned short y;
  unsigned int year;
  unsigned int mo;
  unsigned int day;
  unsigned int h;
  unsigned int m;
  unsigned int s;
  char val[256];
  int ret;

  if (length != 7)
    return 0;
  ret = 0;
  memcpy (&y, &data[4], sizeof (unsigned short));
  year = ntohs (y);
  mo = (unsigned char) data[6];
  day = (unsigned char) data[7];
  h = (unsigned char) data[8];
  m = (unsigned char) data[9];
  s = (unsigned char) data[10];
  snprintf (val, 
	    sizeof(val),
	    "%04u-%02u-%02u %02d:%02d:%02d", year, mo, day, h, m, s);
  ADD (EXTRACTOR_METATYPE_MODIFICATION_DATE, val);
 FINISH:
  return ret;
}

#define PNG_HEADER "\211PNG\r\n\032\n"



int 
EXTRACTOR_png_extract (const char *data,
		       size_t size,
		       EXTRACTOR_MetaDataProcessor proc,
		       void *proc_cls,
		       const char *options)
{
  const char *pos;
  const char *end;
  unsigned int length;
  int ret;

  if (size < strlen (PNG_HEADER))
    return 0;
  if (0 != strncmp (data, PNG_HEADER, strlen (PNG_HEADER)))
    return 0;
  end = &data[size];
  pos = &data[strlen (PNG_HEADER)];
  ADD (EXTRACTOR_METATYPE_MIMETYPE, "image/png");
  ret = 0;
  while (ret == 0)
    {
      if (pos + 12 >= end)
        break;
      length = htonl (getIntAt (pos));
      pos += 4;
      /* printf("Length: %u, pos %u\n", length, pos - data); */
      if ((pos + 4 + length + 4 > end) || (pos + 4 + length + 4 < pos + 8))
        break;
      if (0 == strncmp (pos, "IHDR", 4))
        ret = processIHDR (pos, length, proc, proc_cls);
      if (0 == strncmp (pos, "iTXt", 4))
        ret = processiTXt (pos, length, proc, proc_cls);
      if (0 == strncmp (pos, "tEXt", 4))
        ret = processtEXt (pos, length, proc, proc_cls);
      if (0 == strncmp (pos, "zTXt", 4))
        ret = processzTXt (pos, length, proc, proc_cls);
      if (0 == strncmp (pos, "tIME", 4))
        ret = processtIME (pos, length, proc, proc_cls);
      pos += 4 + length + 4;    /* Chunk type, data, crc */
    }
 FINISH:
  return ret;
}