aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/tiff_extractor.c
blob: d944be1f8b904e652502a53ed55e9d334d85df24 (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
/*
     This file is part of libextractor.
     (C) 2004, 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 0

static int
addKeyword (EXTRACTOR_MetaDataProcessor proc,
	    void *proc_cls,
            const char *keyword, 
	    enum EXTRACTOR_MetaType type)
{
  if (keyword == NULL)
    return 0;
  return proc (proc_cls,
	       "tiff",
	       type,
	       EXTRACTOR_METAFORMAT_UTF8,
	       "text/plain",
	       keyword,
	       strlen(keyword)+1);
}

typedef struct
{
  unsigned short byteorder;
  unsigned short fourty_two;
  unsigned int ifd_offset;
} TIFF_HEADER;
#define TIFF_HEADER_SIZE 8
#define TIFF_HEADER_FIELDS(p) \
  &(p)->byteorder,	      \
    &(p)->fourty_two,	      \
    &(p)->ifd_offset
static char *TIFF_HEADER_SPECS[] = {
  "hhw",
  "HHW",
};

typedef struct
{
  unsigned short tag;
  unsigned short type;
  unsigned int count;
  unsigned int value_or_offset;
} DIRECTORY_ENTRY;
#define DIRECTORY_ENTRY_SIZE 12
#define DIRECTORY_ENTRY_FIELDS(p)		\
  &(p)->tag,					\
    &(p)->type,					\
    &(p)->count,				\
    &(p)->value_or_offset
static char *DIRECTORY_ENTRY_SPECS[] = {
  "hhww",
  "HHWW"
};

#define TAG_LENGTH 0x101
#define TAG_WIDTH 0x100
#define TAG_SOFTWARE 0x131
#define TAG_DAYTIME 0x132
#define TAG_ARTIST 0x315
#define TAG_COPYRIGHT 0x8298
#define TAG_DESCRIPTION 0x10E
#define TAG_DOCUMENT_NAME 0x10D
#define TAG_HOST 0x13C
#define TAG_SCANNER 0x110
#define TAG_ORIENTATION 0x112

#define TYPE_BYTE 1
#define TYPE_ASCII 2
#define TYPE_SHORT 3
#define TYPE_LONG 4
#define TYPE_RATIONAL 5

static int
addASCII (EXTRACTOR_MetaDataProcessor proc,
	  void *proc_cls,
          const char *data,
          size_t size, DIRECTORY_ENTRY * entry,
	  enum EXTRACTOR_MetaType type)
{
  if (entry->count > size)
    return 0;                     /* invalid! */
  if (entry->type != TYPE_ASCII)
    return 0;                     /* huh? */
  if (entry->count + entry->value_or_offset > size)
    return 0;
  if (data[entry->value_or_offset + entry->count - 1] != 0)
    return 0;
  return addKeyword (proc, proc_cls,
		     &data[entry->value_or_offset], type);
}


int 
EXTRACTOR_tiff_extract (const char *data,
			size_t size,
			EXTRACTOR_MetaDataProcessor proc,
			void *proc_cls,
			const char *options)
{
  TIFF_HEADER hdr;
  int byteOrder;                /* 0: do not convert;
                                   1: do convert */
  unsigned int current_ifd;
  unsigned int length = -1;
  unsigned int width = -1;

  if (size < TIFF_HEADER_SIZE)
    return 0;                /*  can not be tiff */
  if ((data[0] == 0x49) && (data[1] == 0x49))
    byteOrder = 0;
  else if ((data[0] == 0x4D) && (data[1] == 0x4D))
    byteOrder = 1;
  else
    return 0;                /* can not be tiff */
#if __BYTE_ORDER == __BIG_ENDIAN
  byteOrder = 1 - byteOrder;
#endif
  EXTRACTOR_common_cat_unpack (data, TIFF_HEADER_SPECS[byteOrder], TIFF_HEADER_FIELDS (&hdr));
  if (hdr.fourty_two != 42)
    return 0;                /* can not be tiff */
  if (hdr.ifd_offset + 6 > size)
    return 0;                /* malformed tiff */
  if (0 != addKeyword (proc, proc_cls, "image/tiff", EXTRACTOR_METATYPE_MIMETYPE))
    return 1;
  current_ifd = hdr.ifd_offset;
  while (current_ifd != 0)
    {
      unsigned short len;
      unsigned int off;
      int i;
      if ( (current_ifd + 6 > size) ||
	   (current_ifd + 6 < current_ifd) )
        return 0;
      if (byteOrder == 0)
        len = data[current_ifd + 1] << 8 | data[current_ifd];
      else
        len = data[current_ifd] << 8 | data[current_ifd + 1];
      if (len * DIRECTORY_ENTRY_SIZE + 2 + 4 + current_ifd > size)
        {
#if DEBUG
          printf ("WARNING: malformed tiff\n");
#endif
          return 0;
        }
      for (i = 0; i < len; i++)
        {
          DIRECTORY_ENTRY entry;
          off = current_ifd + 2 + DIRECTORY_ENTRY_SIZE * i;

          EXTRACTOR_common_cat_unpack (&data[off],
                      DIRECTORY_ENTRY_SPECS[byteOrder],
                      DIRECTORY_ENTRY_FIELDS (&entry));
          switch (entry.tag)
            {
            case TAG_LENGTH:
              if ((entry.type == TYPE_SHORT) && (byteOrder == 1))
                {
                  length = entry.value_or_offset >> 16;
                }
              else
                {
                  length = entry.value_or_offset;
                }
              if (width != -1)
                {
                  char tmp[128];
                  snprintf (tmp, 
			    sizeof(tmp), "%ux%u",
			    width, length);
                  addKeyword (proc, 
			      proc_cls, 
			      tmp, 
			      EXTRACTOR_METATYPE_IMAGE_DIMENSIONS);
                }
              break;
            case TAG_WIDTH:
              if ((entry.type == TYPE_SHORT) && (byteOrder == 1))
                width = entry.value_or_offset >> 16;
              else
                width = entry.value_or_offset;
              if (length != -1)
                {
                  char tmp[128];
                  snprintf (tmp, 
			    sizeof(tmp), 
			    "%ux%u",
			    width, length);
                  addKeyword (proc, proc_cls, 
			      tmp, 
			      EXTRACTOR_METATYPE_IMAGE_DIMENSIONS);
                }
              break;
            case TAG_SOFTWARE:
              if (0 != addASCII (proc, proc_cls, data, size, &entry, EXTRACTOR_METATYPE_CREATED_BY_SOFTWARE))
		return 1;
              break;
            case TAG_ARTIST:
              if (0 != addASCII (proc, proc_cls, data, size, &entry, EXTRACTOR_METATYPE_ARTIST))
		return 1;
              break;
            case TAG_DOCUMENT_NAME:
              if (0 != addASCII (proc, proc_cls, data, size, &entry, EXTRACTOR_METATYPE_TITLE))
		return 1;
              break;
            case TAG_COPYRIGHT:
              if (0 != addASCII (proc, proc_cls, data, size, &entry, EXTRACTOR_METATYPE_COPYRIGHT))
		return 1;
              break;
            case TAG_DESCRIPTION:
              if (0 != addASCII (proc, proc_cls, data, size, &entry, EXTRACTOR_METATYPE_DESCRIPTION))
		return 1;
              break;
            case TAG_HOST:
              if (0 != addASCII (proc, proc_cls, data, size, &entry, EXTRACTOR_METATYPE_BUILDHOST))
		return 1;
              break;
            case TAG_SCANNER:
              if (0 != addASCII (proc, proc_cls, data, size, &entry, EXTRACTOR_METATYPE_SOURCE))
		return 1;
              break;
            case TAG_DAYTIME:
              if (0 != addASCII (proc, proc_cls, data, size, &entry, EXTRACTOR_METATYPE_CREATION_DATE))
		return 1;
              break;
            }
        }

      off = current_ifd + 2 + DIRECTORY_ENTRY_SIZE * len;
      if (byteOrder == 0)
        current_ifd =
          data[off + 3] << 24 | data[off + 2] << 16 | 
	  data[off + 1] << 8  | data[off];
      else
        current_ifd =
          data[off] << 24 | data[off + 1] << 16 |
	  data[off + 2] << 8 | data[off + 3];
    }
  return 0;
}