1 /* exif-mnote-data-olympus.c
3 * Copyright © 2002, 2003 Lutz Mueller <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.
22 #include "exif-mnote-data-olympus.h"
28 #include <libexif/exif-utils.h>
29 #include <libexif/exif-data.h>
34 exif_mnote_data_olympus_clear (ExifMnoteDataOlympus *n)
36 ExifMnoteData *d = (ExifMnoteData *) n;
42 for (i = 0; i < n->count; i++)
43 if (n->entries[i].data) {
44 exif_mem_free (d->mem, n->entries[i].data);
45 n->entries[i].data = NULL;
47 exif_mem_free (d->mem, n->entries);
54 exif_mnote_data_olympus_free (ExifMnoteData *n)
58 exif_mnote_data_olympus_clear ((ExifMnoteDataOlympus *) n);
62 exif_mnote_data_olympus_get_value (ExifMnoteData *d, unsigned int i, char *val, unsigned int maxlen)
64 ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) d;
66 if (!d || !val) return NULL;
67 if (i > n->count -1) return NULL;
68 exif_log (d->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataOlympus",
69 "Querying value for tag '%s'...",
70 mnote_olympus_tag_get_name (n->entries[i].tag));
71 return mnote_olympus_entry_get_value (&n->entries[i], val, maxlen);
78 * @brief save the MnoteData from ne to buf
80 * @param ne extract the data from this structure
81 * @param *buf write the mnoteData to this buffer (buffer will be allocated)
82 * @param buf_size the size of the buffer
85 exif_mnote_data_olympus_save (ExifMnoteData *ne,
86 unsigned char **buf, unsigned int *buf_size)
88 ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) ne;
89 unsigned int i, o, s, doff, base = 0, o2 = 6 + 2;
92 if (!n || !buf || !buf_size) return;
95 * Allocate enough memory for all entries and the number of entries.
97 *buf_size = 6 + 2 + 2 + n->count * 12;
101 *buf = exif_mem_alloc (ne->mem, *buf_size);
104 /* Write the header and the number of entries. */
105 strcpy ((char *)*buf, n->version==sanyoV1?"SANYO":"OLYMP");
106 exif_set_short (*buf + 6, n->order, (ExifShort) 1);
110 *buf_size += 8-6 + 4;
111 *buf = exif_mem_alloc (ne->mem, *buf_size);
114 /* Write the header and the number of entries. */
115 strcpy ((char *)*buf, "OLYMPUS");
116 exif_set_short (*buf + 8, n->order, (ExifShort) (
117 (n->order == EXIF_BYTE_ORDER_INTEL) ?
120 exif_set_short (*buf + 10, n->order, (ExifShort) 3);
124 base = MNOTE_NIKON1_TAG_BASE;
126 /* v1 has offsets based to main IFD, not makernote IFD */
127 datao += n->offset + 10;
128 /* subtract the size here, so the increment in the next case will not harm us */
133 *buf_size += 4; /* Next IFD pointer */
134 *buf = exif_mem_alloc (ne->mem, *buf_size);
137 /* Write the header and the number of entries. */
138 strcpy ((char *)*buf, "Nikon");
139 (*buf)[6] = n->version;
141 if (n->version == nikonV2) {
142 exif_set_short (*buf + 10, n->order, (ExifShort) (
143 (n->order == EXIF_BYTE_ORDER_INTEL) ?
146 exif_set_short (*buf + 12, n->order, (ExifShort) 0x2A);
147 exif_set_long (*buf + 14, n->order, (ExifShort) 8);
151 /* Reset next IFD pointer */
152 exif_set_long (*buf + o2 + 2 + n->count * 12, n->order, 0);
159 exif_set_short (*buf + o2, n->order, (ExifShort) n->count);
162 /* Save each entry */
163 for (i = 0; i < n->count; i++) {
165 exif_set_short (*buf + o + 0, n->order,
166 (ExifShort) (n->entries[i].tag - base));
167 exif_set_short (*buf + o + 2, n->order,
168 (ExifShort) n->entries[i].format);
169 exif_set_long (*buf + o + 4, n->order,
170 n->entries[i].components);
172 s = exif_format_get_size (n->entries[i].format) *
173 n->entries[i].components;
177 *buf = exif_mem_realloc (ne->mem, *buf,
178 sizeof (char) * *buf_size);
180 exif_set_long (*buf + o, n->order, datao + doff);
184 /* Write the data. */
185 if (n->entries[i].data) {
186 memcpy (*buf + doff, n->entries[i].data, s);
188 /* Most certainly damaged input file */
189 memset (*buf + doff, 0, s);
195 exif_mnote_data_olympus_load (ExifMnoteData *en,
196 const unsigned char *buf, unsigned int buf_size)
198 ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) en;
200 unsigned int i, s, o, o2 = 0, datao = 6, base = 0;
202 if (!n || !buf) return;
204 /* Start of interesting data */
208 * Olympus headers start with "OLYMP" and need to have at least
209 * a size of 22 bytes (6 for 'OLYMP', 2 other bytes, 2 for the
210 * number of entries, and 12 for one entry.
212 * Sanyo format is identical and uses identical tags except that
213 * header starts with "SANYO".
215 * Nikon headers start with "Nikon" (6 bytes including '\0'),
216 * version number (1 or 2).
218 * Version 1 continues with 0, 1, 0, number_of_tags,
219 * or just with number_of_tags (models D1H, D1X...).
221 * Version 2 continues with an unknown byte (0 or 10),
222 * two unknown bytes (0), "MM" or "II", another byte 0 and
225 if (buf_size - n->offset < 22) return;
226 if (!memcmp (buf + o2, "OLYMP", 6) || !memcmp (buf + o2, "SANYO", 6)) {
227 exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataOlympus",
228 "Parsing Olympus/Sanyo maker note v1...");
230 /* The number of entries is at position 8. */
231 if (!memcmp (buf + o2, "SANYO", 6))
232 n->version = sanyoV1;
234 n->version = olympusV1;
235 if (buf[o2 + 6] == 1)
236 n->order = EXIF_BYTE_ORDER_INTEL;
237 else if (buf[o2 + 6 + 1] == 1)
238 n->order = EXIF_BYTE_ORDER_MOTOROLA;
240 if (o2 >= buf_size) return;
241 c = exif_get_short (buf + o2, n->order);
242 if ((!(c & 0xFF)) && (c > 0x500)) {
243 if (n->order == EXIF_BYTE_ORDER_INTEL) {
244 n->order = EXIF_BYTE_ORDER_MOTOROLA;
246 n->order = EXIF_BYTE_ORDER_INTEL;
250 } else if (!memcmp (buf + o2, "OLYMPUS", 8)) {
251 /* Olympus S760, S770 */
254 exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataOlympus",
255 "Parsing Olympus maker note v2 (0x%02x, %02x, %02x, %02x)...",
256 buf[o2], buf[o2 + 1], buf[o2 + 2], buf[o2 + 3]);
258 if ((buf[o2] == 'I') && (buf[o2 + 1] == 'I'))
259 n->order = EXIF_BYTE_ORDER_INTEL;
260 else if ((buf[o2] == 'M') && (buf[o2 + 1] == 'M'))
261 n->order = EXIF_BYTE_ORDER_MOTOROLA;
263 /* The number of entries is at position 8+4. */
264 n->version = olympusV2;
267 } else if (!memcmp (buf + o2, "Nikon", 6)) {
269 exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataOlympus",
270 "Parsing Nikon maker note (0x%02x, %02x, %02x, "
271 "%02x, %02x, %02x, %02x, %02x)...",
272 buf[o2 + 0], buf[o2 + 1], buf[o2 + 2], buf[o2 + 3],
273 buf[o2 + 4], buf[o2 + 5], buf[o2 + 6], buf[o2 + 7]);
274 /* The first byte is the version. */
275 if (o2 >= buf_size) return;
276 n->version = buf[o2];
279 /* Skip an unknown byte (00 or 0A). */
282 switch (n->version) {
285 base = MNOTE_NIKON1_TAG_BASE;
286 /* Fix endianness, if needed */
287 if (o2 >= buf_size) return;
288 c = exif_get_short (buf + o2, n->order);
289 if ((!(c & 0xFF)) && (c > 0x500)) {
290 if (n->order == EXIF_BYTE_ORDER_INTEL) {
291 n->order = EXIF_BYTE_ORDER_MOTOROLA;
293 n->order = EXIF_BYTE_ORDER_INTEL;
300 /* Skip 2 unknown bytes (00 00). */
304 * Byte order. From here the data offset
308 if (o2 >= buf_size) return;
309 if (!strncmp ((char *)&buf[o2], "II", 2))
310 n->order = EXIF_BYTE_ORDER_INTEL;
311 else if (!strncmp ((char *)&buf[o2], "MM", 2))
312 n->order = EXIF_BYTE_ORDER_MOTOROLA;
314 exif_log (en->log, EXIF_LOG_CODE_DEBUG,
315 "ExifMnoteDatalympus", "Unknown "
316 "byte order '%c%c'", buf[o2],
322 /* Skip 2 unknown bytes (00 2A). */
325 /* Go to where the number of entries is. */
326 if (o2 >= buf_size) return;
327 o2 = datao + exif_get_long (buf + o2, n->order);
331 exif_log (en->log, EXIF_LOG_CODE_DEBUG,
332 "ExifMnoteDataOlympus", "Unknown version "
333 "number %i.", n->version);
336 } else if (!memcmp (buf + o2, "\0\x1b", 2)) {
337 n->version = nikonV2;
342 /* Number of entries */
343 if (o2 >= buf_size) return;
344 c = exif_get_short (buf + o2, n->order);
347 /* Read the number of entries and remove old ones. */
348 exif_mnote_data_olympus_clear (n);
350 n->entries = exif_mem_alloc (en->mem, sizeof (MnoteOlympusEntry) * c);
351 if (!n->entries) return;
353 /* Parse the entries */
354 for (i = 0; i < c; i++) {
356 if (o + 12 > buf_size) return;
359 n->entries[i].tag = exif_get_short (buf + o, n->order) + base;
360 n->entries[i].format = exif_get_short (buf + o + 2, n->order);
361 n->entries[i].components = exif_get_long (buf + o + 4, n->order);
362 n->entries[i].order = n->order;
364 exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteOlympus",
365 "Loading entry 0x%x ('%s')...", n->entries[i].tag,
366 mnote_olympus_tag_get_name (n->entries[i].tag));
369 * Size? If bigger than 4 bytes, the actual data is not
370 * in the entry but somewhere else (offset).
372 s = exif_format_get_size (n->entries[i].format) *
373 n->entries[i].components;
376 if (s > 4) o = exif_get_long (buf + o, n->order) + datao;
377 if (o + s > buf_size) continue;
380 n->entries[i].data = exif_mem_alloc (en->mem, s);
381 if (!n->entries[i].data) continue;
382 n->entries[i].size = s;
383 memcpy (n->entries[i].data, buf + o, s);
388 exif_mnote_data_olympus_count (ExifMnoteData *n)
390 return n ? ((ExifMnoteDataOlympus *) n)->count : 0;
394 exif_mnote_data_olympus_get_id (ExifMnoteData *d, unsigned int n)
396 ExifMnoteDataOlympus *note = (ExifMnoteDataOlympus *) d;
399 if (note->count <= n) return 0;
400 return note->entries[n].tag;
404 exif_mnote_data_olympus_get_name (ExifMnoteData *d, unsigned int i)
406 ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) d;
409 if (i >= n->count) return NULL;
410 return mnote_olympus_tag_get_name (n->entries[i].tag);
414 exif_mnote_data_olympus_get_title (ExifMnoteData *d, unsigned int i)
416 ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) d;
419 if (i >= n->count) return NULL;
420 return mnote_olympus_tag_get_title (n->entries[i].tag);
424 exif_mnote_data_olympus_get_description (ExifMnoteData *d, unsigned int i)
426 ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) d;
429 if (i >= n->count) return NULL;
430 return mnote_olympus_tag_get_description (n->entries[i].tag);
434 exif_mnote_data_olympus_set_byte_order (ExifMnoteData *d, ExifByteOrder o)
436 ExifByteOrder o_orig;
437 ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) d;
444 for (i = 0; i < n->count; i++) {
445 n->entries[i].order = o;
446 exif_array_set_byte_order (n->entries[i].format, n->entries[i].data,
447 n->entries[i].components, o_orig, o);
452 exif_mnote_data_olympus_set_offset (ExifMnoteData *n, unsigned int o)
454 if (n) ((ExifMnoteDataOlympus *) n)->offset = o;
458 exif_mnote_data_olympus_new (ExifMem *mem)
462 if (!mem) return NULL;
464 d = exif_mem_alloc (mem, sizeof (ExifMnoteDataOlympus));
467 exif_mnote_data_construct (d, mem);
469 /* Set up function pointers */
470 d->methods.free = exif_mnote_data_olympus_free;
471 d->methods.set_byte_order = exif_mnote_data_olympus_set_byte_order;
472 d->methods.set_offset = exif_mnote_data_olympus_set_offset;
473 d->methods.load = exif_mnote_data_olympus_load;
474 d->methods.save = exif_mnote_data_olympus_save;
475 d->methods.count = exif_mnote_data_olympus_count;
476 d->methods.get_id = exif_mnote_data_olympus_get_id;
477 d->methods.get_name = exif_mnote_data_olympus_get_name;
478 d->methods.get_title = exif_mnote_data_olympus_get_title;
479 d->methods.get_description = exif_mnote_data_olympus_get_description;
480 d->methods.get_value = exif_mnote_data_olympus_get_value;