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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
/* exif-loader.c
*
* Copyright (c) 2002 Lutz Mueller <lutz@users.sourceforge.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <libexif/exif-loader.h>
#include <libexif/exif-utils.h>
#include <libexif/i18n.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#undef JPEG_MARKER_DHT
#define JPEG_MARKER_DHT 0xc4
#undef JPEG_MARKER_SOI
#define JPEG_MARKER_SOI 0xd8
#undef JPEG_MARKER_DQT
#define JPEG_MARKER_DQT 0xdb
#undef JPEG_MARKER_APP0
#define JPEG_MARKER_APP0 0xe0
#undef JPEG_MARKER_APP1
#define JPEG_MARKER_APP1 0xe1
#undef JPEG_MARKER_APP2
#define JPEG_MARKER_APP2 0xe2
#undef JPEG_MARKER_APP13
#define JPEG_MARKER_APP13 0xed
#undef JPEG_MARKER_COM
#define JPEG_MARKER_COM 0xfe
typedef enum {
EL_READ = 0,
EL_READ_SIZE_BYTE_24,
EL_READ_SIZE_BYTE_16,
EL_READ_SIZE_BYTE_08,
EL_READ_SIZE_BYTE_00,
EL_SKIP_BYTES,
EL_EXIF_FOUND,
} ExifLoaderState;
typedef enum {
EL_DATA_FORMAT_UNKNOWN,
EL_DATA_FORMAT_EXIF,
EL_DATA_FORMAT_JPEG,
EL_DATA_FORMAT_FUJI_RAW
} ExifLoaderDataFormat;
struct _ExifLoader {
ExifLoaderState state;
ExifLoaderDataFormat data_format;
/* Small buffer used for detection of format */
unsigned char b[12];
unsigned char b_len;
unsigned int size;
unsigned char *buf;
unsigned int bytes_read;
unsigned int ref_count;
ExifLog *log;
ExifMem *mem;
};
static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
static void *
exif_loader_alloc (ExifLoader *l, unsigned int i)
{
void *d;
if (!l || !i)
return NULL;
d = exif_mem_alloc (l->mem, i);
if (d)
return d;
EXIF_LOG_NO_MEMORY (l->log, "ExifLog", i);
return NULL;
}
#undef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
void
exif_loader_write_file (ExifLoader *l, const char *path)
{
FILE *f;
int size;
unsigned char data[1024];
if (!l)
return;
f = fopen (path, "rb");
if (!f) {
exif_log (l->log, EXIF_LOG_CODE_NONE, "ExifLoader",
_("The file '%s' could not be opened."), path);
return;
}
while (1) {
size = fread (data, 1, sizeof (data), f);
if (size <= 0)
break;
if (!exif_loader_write (l, data, size))
break;
}
fclose (f);
}
static unsigned int
exif_loader_copy (ExifLoader *eld, unsigned char *buf, unsigned int len)
{
if (!eld || (len && !buf) || (eld->bytes_read >= eld->size))
return 0;
/* If needed, allocate the buffer. */
if (!eld->buf)
eld->buf = exif_loader_alloc (eld, eld->size);
if (!eld->buf)
return 0;
/* Copy memory */
len = MIN (len, eld->size - eld->bytes_read);
memcpy (eld->buf + eld->bytes_read, buf, len);
eld->bytes_read += len;
return (eld->bytes_read >= eld->size) ? 0 : 1;
}
unsigned char
exif_loader_write (ExifLoader *eld, unsigned char *buf, unsigned int len)
{
unsigned int i;
if (!eld || (len && !buf))
return 0;
switch (eld->state) {
case EL_EXIF_FOUND:
return exif_loader_copy (eld, buf, len);
case EL_SKIP_BYTES:
if (eld->size > len) {
eld->size -= len;
return 1;
}
len -= eld->size;
buf += eld->size;
eld->size = 0;
eld->b_len = 0;
switch (eld->data_format) {
case EL_DATA_FORMAT_FUJI_RAW:
eld->state = EL_READ_SIZE_BYTE_24;
break;
default:
eld->state = EL_READ;
break;
}
break;
default:
break;
}
if (!len)
return 1;
exif_log (eld->log, EXIF_LOG_CODE_DEBUG, "ExifLoader",
"Scanning %i byte(s) of data...", len);
/*
* First fill the small buffer. Only continue if the buffer
* is filled. Note that EXIF data contains at least 12 bytes.
*/
i = MIN (len, sizeof (eld->b) - eld->b_len);
if (i) {
memcpy (&eld->b[eld->b_len], buf, i);
eld->b_len += i;
if (eld->b_len < sizeof (eld->b))
return 1;
buf += i;
len -= i;
}
switch (eld->data_format) {
case EL_DATA_FORMAT_UNKNOWN:
/* Check the small buffer against known formats. */
if (!memcmp (eld->b, "FUJIFILM", 8)) {
/* Skip to byte 84. There is another offset there. */
eld->data_format = EL_DATA_FORMAT_FUJI_RAW;
eld->size = 84;
eld->state = EL_SKIP_BYTES;
eld->size = 84;
} else if (!memcmp (eld->b + 2, ExifHeader, sizeof (ExifHeader))) {
/* Read the size (2 bytes). */
eld->data_format = EL_DATA_FORMAT_EXIF;
eld->state = EL_READ_SIZE_BYTE_08;
}
default:
break;
}
for (i = 0; i < sizeof (eld->b); i++)
switch (eld->state) {
case EL_EXIF_FOUND:
if (!exif_loader_copy (eld, eld->b + i,
sizeof (eld->b) - i))
return 0;
return exif_loader_copy (eld, buf, len);
case EL_SKIP_BYTES:
eld->size--;
if (!eld->size)
eld->state = EL_READ;
break;
case EL_READ_SIZE_BYTE_24:
eld->size |= eld->b[i] << 24;
eld->state = EL_READ_SIZE_BYTE_16;
break;
case EL_READ_SIZE_BYTE_16:
eld->size |= eld->b[i] << 16;
eld->state = EL_READ_SIZE_BYTE_08;
break;
case EL_READ_SIZE_BYTE_08:
eld->size |= eld->b[i] << 8;
eld->state = EL_READ_SIZE_BYTE_00;
break;
case EL_READ_SIZE_BYTE_00:
eld->size |= eld->b[i] << 0;
switch (eld->data_format) {
case EL_DATA_FORMAT_JPEG:
eld->state = EL_SKIP_BYTES;
eld->size -= 2;
break;
case EL_DATA_FORMAT_FUJI_RAW:
eld->data_format = EL_DATA_FORMAT_EXIF;
eld->state = EL_SKIP_BYTES;
eld->size -= 86;
break;
case EL_DATA_FORMAT_EXIF:
eld->state = EL_EXIF_FOUND;
break;
default:
break;
}
break;
default:
switch (eld->b[i]) {
case JPEG_MARKER_APP1:
if (!memcmp (eld->b + i + 3, ExifHeader, MIN((ssize_t)(sizeof(ExifHeader)), MAX(0, ((ssize_t)(sizeof(eld->b))) - ((ssize_t)i) - 3)))) {
eld->data_format = EL_DATA_FORMAT_EXIF;
} else {
eld->data_format = EL_DATA_FORMAT_JPEG; /* Probably JFIF - keep searching for APP1 EXIF*/
}
eld->size = 0;
eld->state = EL_READ_SIZE_BYTE_08;
break;
case JPEG_MARKER_DHT:
case JPEG_MARKER_DQT:
case JPEG_MARKER_APP0:
case JPEG_MARKER_APP2:
case JPEG_MARKER_APP13:
case JPEG_MARKER_COM:
eld->data_format = EL_DATA_FORMAT_JPEG;
eld->size = 0;
eld->state = EL_READ_SIZE_BYTE_08;
break;
case 0xff:
case JPEG_MARKER_SOI:
break;
default:
exif_log (eld->log,
EXIF_LOG_CODE_CORRUPT_DATA,
"ExifLoader", _("The data supplied "
"does not seem to contain "
"EXIF data."));
exif_loader_reset (eld);
return 0;
}
}
/*
* If we reach this point, the buffer has not been big enough
* to read all data we need. Fill it with new data.
*/
eld->b_len = 0;
return exif_loader_write (eld, buf, len);
}
ExifLoader *
exif_loader_new (void)
{
ExifMem *mem = exif_mem_new_default ();
ExifLoader *l = exif_loader_new_mem (mem);
exif_mem_unref (mem);
return l;
}
ExifLoader *
exif_loader_new_mem (ExifMem *mem)
{
ExifLoader *loader;
if (!mem)
return NULL;
loader = exif_mem_alloc (mem, sizeof (ExifLoader));
if (!loader)
return NULL;
loader->ref_count = 1;
loader->mem = mem;
exif_mem_ref (mem);
return loader;
}
void
exif_loader_ref (ExifLoader *loader)
{
if (loader)
loader->ref_count++;
}
static void
exif_loader_free (ExifLoader *loader)
{
ExifMem *mem;
if (!loader)
return;
mem = loader->mem;
exif_loader_reset (loader);
exif_log_unref (loader->log);
exif_mem_free (mem, loader);
exif_mem_unref (mem);
}
void
exif_loader_unref (ExifLoader *loader)
{
if (!loader)
return;
if (!--loader->ref_count)
exif_loader_free (loader);
}
void
exif_loader_reset (ExifLoader *loader)
{
if (!loader)
return;
exif_mem_free (loader->mem, loader->buf); loader->buf = NULL;
loader->size = 0;
loader->bytes_read = 0;
loader->state = 0;
loader->b_len = 0;
loader->data_format = EL_DATA_FORMAT_UNKNOWN;
}
ExifData *
exif_loader_get_data (ExifLoader *loader)
{
ExifData *ed;
if (!loader || (loader->data_format == EL_DATA_FORMAT_UNKNOWN) ||
!loader->bytes_read)
return NULL;
ed = exif_data_new_mem (loader->mem);
exif_data_log (ed, loader->log);
exif_data_load_data (ed, loader->buf, loader->bytes_read);
return ed;
}
void
exif_loader_log (ExifLoader *loader, ExifLog *log)
{
if (!loader)
return;
exif_log_unref (loader->log);
loader->log = log;
exif_log_ref (log);
}
|