3 * Copyright © 2002 Lutz Müller <lutz@users.sourceforge.net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include <libexif/exif-loader.h>
24 #include <libexif/i18n.h>
30 #undef JPEG_MARKER_SOI
31 #define JPEG_MARKER_SOI 0xd8
32 #undef JPEG_MARKER_APP0
33 #define JPEG_MARKER_APP0 0xe0
34 #undef JPEG_MARKER_APP1
35 #define JPEG_MARKER_APP1 0xe1
36 #undef JPEG_MARKER_APP2
37 #define JPEG_MARKER_APP2 0xe2
38 #undef JPEG_MARKER_APP13
39 #define JPEG_MARKER_APP13 0xed
40 #undef JPEG_MARKER_COM
41 #define JPEG_MARKER_COM 0xfe
44 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
57 EL_DATA_FORMAT_UNKNOWN,
60 EL_DATA_FORMAT_FUJI_RAW
61 } ExifLoaderDataFormat;
64 ExifLoaderState state;
65 ExifLoaderDataFormat data_format;
67 /* Small buffer used for detection of format */
73 unsigned int bytes_read;
75 unsigned int ref_count;
81 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
84 exif_loader_alloc (ExifLoader *l, unsigned int i)
91 d = exif_mem_alloc (l->mem, i);
95 EXIF_LOG_NO_MEMORY (l->log, "ExifLog", i);
100 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
103 exif_loader_write_file (ExifLoader *l, const char *path)
107 unsigned char data[1024];
112 f = fopen (path, "rb");
114 exif_log (l->log, EXIF_LOG_CODE_NONE, "ExifLoader",
115 _("The file '%s' could not be opened."), path);
119 size = fread (data, 1, sizeof (data), f);
122 if (!exif_loader_write (l, data, size))
129 exif_loader_copy (ExifLoader *eld, unsigned char *buf, unsigned int len)
131 if (!eld || (len && !buf) || (eld->bytes_read >= eld->size))
134 /* If needed, allocate the buffer. */
136 eld->buf = exif_loader_alloc (eld, eld->size);
141 len = MIN (len, eld->size - eld->bytes_read);
142 memcpy (eld->buf + eld->bytes_read, buf, len);
143 eld->bytes_read += len;
145 return (eld->bytes_read >= eld->size) ? 0 : 1;
149 exif_loader_write (ExifLoader *eld, unsigned char *buf, unsigned int len)
153 if (!eld || (len && !buf))
156 switch (eld->state) {
158 return exif_loader_copy (eld, buf, len);
160 if (eld->size > len) {
168 switch (eld->data_format) {
169 case EL_DATA_FORMAT_FUJI_RAW:
170 eld->state = EL_READ_SIZE_BYTE_24;
173 eld->state = EL_READ;
181 exif_log (eld->log, EXIF_LOG_CODE_DEBUG, "ExifLoader",
182 "Scanning %i byte(s) of data...", len);
185 * First fill the small buffer. Only continue if the buffer
186 * is filled. Note that EXIF data contains at least 12 bytes.
188 i = MIN (len, sizeof (eld->b) - eld->b_len);
190 memcpy (&eld->b[eld->b_len], buf, i);
192 if (eld->b_len < sizeof (eld->b))
198 switch (eld->data_format) {
199 case EL_DATA_FORMAT_UNKNOWN:
201 /* Check the small buffer against known formats. */
202 if (!memcmp (eld->b, "FUJIFILM", 8)) {
204 /* Skip to byte 84. There is another offset there. */
205 eld->data_format = EL_DATA_FORMAT_FUJI_RAW;
207 eld->state = EL_SKIP_BYTES;
210 } else if (!memcmp (eld->b + 2, ExifHeader, sizeof (ExifHeader))) {
212 /* Read the size (2 bytes). */
213 eld->data_format = EL_DATA_FORMAT_EXIF;
214 eld->state = EL_READ_SIZE_BYTE_08;
220 for (i = 0; i < sizeof (eld->b); i++)
221 switch (eld->state) {
223 if (!exif_loader_copy (eld, eld->b + i,
224 sizeof (eld->b) - i))
226 return exif_loader_copy (eld, buf, len);
230 eld->state = EL_READ;
233 case EL_READ_SIZE_BYTE_24:
234 eld->size |= eld->b[i] << 24;
235 eld->state = EL_READ_SIZE_BYTE_16;
237 case EL_READ_SIZE_BYTE_16:
238 eld->size |= eld->b[i] << 16;
239 eld->state = EL_READ_SIZE_BYTE_08;
241 case EL_READ_SIZE_BYTE_08:
242 eld->size |= eld->b[i] << 8;
243 eld->state = EL_READ_SIZE_BYTE_00;
245 case EL_READ_SIZE_BYTE_00:
246 eld->size |= eld->b[i] << 0;
247 switch (eld->data_format) {
248 case EL_DATA_FORMAT_JPEG:
249 eld->state = EL_SKIP_BYTES;
252 case EL_DATA_FORMAT_FUJI_RAW:
253 eld->data_format = EL_DATA_FORMAT_EXIF;
254 eld->state = EL_SKIP_BYTES;
257 case EL_DATA_FORMAT_EXIF:
258 eld->state = EL_EXIF_FOUND;
267 case JPEG_MARKER_APP1:
268 if (!memcmp (eld->b + i + 3, ExifHeader, MIN(sizeof (ExifHeader), MAX(0, sizeof (eld->b) - i - 3)))) {
269 eld->data_format = EL_DATA_FORMAT_EXIF;
271 eld->data_format = EL_DATA_FORMAT_JPEG; /* Probably JFIF - keep searching for APP1 EXIF*/
274 eld->state = EL_READ_SIZE_BYTE_08;
276 case JPEG_MARKER_APP0:
277 case JPEG_MARKER_APP2:
278 case JPEG_MARKER_APP13:
279 case JPEG_MARKER_COM:
280 eld->data_format = EL_DATA_FORMAT_JPEG;
282 eld->state = EL_READ_SIZE_BYTE_08;
285 case JPEG_MARKER_SOI:
289 EXIF_LOG_CODE_CORRUPT_DATA,
290 "ExifLoader", _("The data supplied "
291 "does not seem to contain "
293 exif_loader_reset (eld);
299 * If we reach this point, the buffer has not been big enough
300 * to read all data we need. Fill it with new data.
303 return exif_loader_write (eld, buf, len);
307 exif_loader_new (void)
309 ExifMem *mem = exif_mem_new_default ();
310 ExifLoader *l = exif_loader_new_mem (mem);
312 exif_mem_unref (mem);
318 exif_loader_new_mem (ExifMem *mem)
325 loader = exif_mem_alloc (mem, sizeof (ExifLoader));
328 loader->ref_count = 1;
337 exif_loader_ref (ExifLoader *loader)
344 exif_loader_free (ExifLoader *loader)
352 exif_loader_reset (loader);
353 exif_mem_free (mem, loader);
354 exif_mem_unref (mem);
358 exif_loader_unref (ExifLoader *loader)
362 if (!--loader->ref_count)
363 exif_loader_free (loader);
367 exif_loader_reset (ExifLoader *loader)
371 exif_mem_free (loader->mem, loader->buf); loader->buf = NULL;
373 loader->bytes_read = 0;
376 loader->data_format = EL_DATA_FORMAT_UNKNOWN;
380 exif_loader_get_data (ExifLoader *loader)
387 ed = exif_data_new_mem (loader->mem);
388 exif_data_log (ed, loader->log);
389 exif_data_load_data (ed, loader->buf, loader->bytes_read);
395 exif_loader_log (ExifLoader *loader, ExifLog *log)
399 exif_log_unref (loader->log);