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;
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, "OLYMP");
109 base = MNOTE_NIKON1_TAG_BASE;
111 /* v1 has offsets based to main IFD, not makernote IFD */
112 datao += n->offset + 10;
113 /* subtract the size here, so the increment in the next case will not harm us */
118 *buf_size += 4; /* Next IFD pointer */
119 *buf = exif_mem_alloc (ne->mem, *buf_size);
122 /* Write the header and the number of entries. */
123 strcpy ((char *)*buf, "Nikon");
124 (*buf)[6] = n->version;
126 if (n->version == nikonV2) {
127 exif_set_short (*buf + 10, n->order, (ExifShort) (
128 (n->order == EXIF_BYTE_ORDER_INTEL) ?
131 exif_set_short (*buf + 12, n->order, (ExifShort) 0x2A);
132 exif_set_long (*buf + 14, n->order, (ExifShort) 8);
136 /* Reset next IFD pointer */
137 exif_set_long (*buf + o2 + 2 + n->count * 12, n->order, 0);
144 exif_set_short (*buf + o2, n->order, (ExifShort) n->count);
147 /* Save each entry */
148 for (i = 0; i < n->count; i++) {
150 exif_set_short (*buf + o + 0, n->order,
151 (ExifShort) (n->entries[i].tag - base));
152 exif_set_short (*buf + o + 2, n->order,
153 (ExifShort) n->entries[i].format);
154 exif_set_long (*buf + o + 4, n->order,
155 n->entries[i].components);
157 s = exif_format_get_size (n->entries[i].format) *
158 n->entries[i].components;
162 *buf = exif_mem_realloc (ne->mem, *buf,
163 sizeof (char) * *buf_size);
165 exif_set_long (*buf + o, n->order, datao + doff);
169 /* Write the data. */
170 if (n->entries[i].data) {
171 memcpy (*buf + doff, n->entries[i].data, s);
173 /* Most certainly damaged input file */
174 memset (*buf + doff, 0, s);
180 exif_mnote_data_olympus_load (ExifMnoteData *en,
181 const unsigned char *buf, unsigned int buf_size)
183 ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) en;
185 unsigned int i, s, o, o2 = 0, datao = 6, base = 0;
187 if (!n || !buf) return;
189 /* Start of interesting data */
193 * Olympus headers start with "OLYMP" and need to have at least
194 * a size of 22 bytes (6 for 'OLYMP', 2 other bytes, 2 for the
195 * number of entries, and 12 for one entry.
197 * Nikon headers start with "Nikon" (6 bytes including '\0'),
198 * version number (1 or 2).
200 * Version 1 continues with 0, 1, 0, number_of_tags,
201 * or just with number_of_tags (models D1H, D1X...).
203 * Version 2 continues with an unknown byte (0 or 10),
204 * two unknown bytes (0), "MM" or "II", another byte 0 and
207 if (buf_size - n->offset < 22) return;
208 if (!memcmp (buf + o2, "OLYMP", 5)) {
209 exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataOlympus",
210 "Parsing Olympus maker note...");
212 /* The number of entries is at position 8. */
213 n->version = olympus;
216 } else if (!memcmp (buf + o2, "Nikon", 6)) {
218 exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataOlympus",
219 "Parsing Nikon maker note (0x%02x, %02x, %02x, "
220 "%02x, %02x, %02x, %02x, %02x)...",
221 buf[o2 + 0], buf[o2 + 1], buf[o2 + 2], buf[o2 + 3],
222 buf[o2 + 4], buf[o2 + 5], buf[o2 + 6], buf[o2 + 7]);
223 /* The first byte is the version. */
224 if (o2 >= buf_size) return;
225 n->version = buf[o2];
228 /* Skip an unknown byte (00 or 0A). */
231 switch (n->version) {
234 base = MNOTE_NIKON1_TAG_BASE;
239 /* Skip 2 unknown bytes (00 00). */
243 * Byte order. From here the data offset
247 if (o2 >= buf_size) return;
248 if (!strncmp ((char *)&buf[o2], "II", 2))
249 n->order = EXIF_BYTE_ORDER_INTEL;
250 else if (!strncmp ((char *)&buf[o2], "MM", 2))
251 n->order = EXIF_BYTE_ORDER_MOTOROLA;
253 exif_log (en->log, EXIF_LOG_CODE_DEBUG,
254 "ExifMnoteDatalympus", "Unknown "
255 "byte order '%c%c'", buf[o2],
261 /* Skip 2 unknown bytes (00 2A). */
264 /* Go to where the number of entries is. */
265 if (o2 >= buf_size) return;
266 o2 = datao + exif_get_long (buf + o2, n->order);
270 exif_log (en->log, EXIF_LOG_CODE_DEBUG,
271 "ExifMnoteDataOlympus", "Unknown version "
272 "number %i.", n->version);
275 } else if (!memcmp (buf + o2, "\0\x1b", 2)) {
276 n->version = nikonV2;
281 /* Number of entries */
282 if (o2 >= buf_size) return;
283 c = exif_get_short (buf + o2, n->order);
286 /* Read the number of entries and remove old ones. */
287 exif_mnote_data_olympus_clear (n);
289 n->entries = exif_mem_alloc (en->mem, sizeof (MnoteOlympusEntry) * c);
290 if (!n->entries) return;
292 /* Parse the entries */
293 for (i = 0; i < c; i++) {
295 if (o + 12 > buf_size) return;
298 n->entries[i].tag = exif_get_short (buf + o, n->order) + base;
299 n->entries[i].format = exif_get_short (buf + o + 2, n->order);
300 n->entries[i].components = exif_get_long (buf + o + 4, n->order);
301 n->entries[i].order = n->order;
303 exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteOlympus",
304 "Loading entry 0x%x ('%s')...", n->entries[i].tag,
305 mnote_olympus_tag_get_name (n->entries[i].tag));
308 * Size? If bigger than 4 bytes, the actual data is not
309 * in the entry but somewhere else (offset).
311 s = exif_format_get_size (n->entries[i].format) *
312 n->entries[i].components;
315 if (s > 4) o = exif_get_long (buf + o, n->order) + datao;
316 if (o + s > buf_size) continue;
319 n->entries[i].data = exif_mem_alloc (en->mem, s);
320 if (!n->entries[i].data) continue;
321 n->entries[i].size = s;
322 memcpy (n->entries[i].data, buf + o, s);
327 exif_mnote_data_olympus_count (ExifMnoteData *n)
329 return n ? ((ExifMnoteDataOlympus *) n)->count : 0;
333 exif_mnote_data_olympus_get_id (ExifMnoteData *d, unsigned int n)
335 ExifMnoteDataOlympus *note = (ExifMnoteDataOlympus *) d;
338 if (note->count <= n) return 0;
339 return note->entries[n].tag;
343 exif_mnote_data_olympus_get_name (ExifMnoteData *d, unsigned int i)
345 ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) d;
348 if (i >= n->count) return NULL;
349 return mnote_olympus_tag_get_name (n->entries[i].tag);
353 exif_mnote_data_olympus_get_title (ExifMnoteData *d, unsigned int i)
355 ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) d;
358 if (i >= n->count) return NULL;
359 return mnote_olympus_tag_get_title (n->entries[i].tag);
363 exif_mnote_data_olympus_get_description (ExifMnoteData *d, unsigned int i)
365 ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) d;
368 if (i >= n->count) return NULL;
369 return mnote_olympus_tag_get_title (n->entries[i].tag);
373 exif_mnote_data_olympus_set_byte_order (ExifMnoteData *d, ExifByteOrder o)
375 ExifByteOrder o_orig;
376 ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) d;
383 for (i = 0; i < n->count; i++) {
384 n->entries[i].order = o;
385 exif_array_set_byte_order (n->entries[i].format, n->entries[i].data,
386 n->entries[i].components, o_orig, o);
391 exif_mnote_data_olympus_set_offset (ExifMnoteData *n, unsigned int o)
393 if (n) ((ExifMnoteDataOlympus *) n)->offset = o;
397 exif_mnote_data_olympus_new (ExifMem *mem)
401 if (!mem) return NULL;
403 d = exif_mem_alloc (mem, sizeof (ExifMnoteDataOlympus));
406 exif_mnote_data_construct (d, mem);
408 /* Set up function pointers */
409 d->methods.free = exif_mnote_data_olympus_free;
410 d->methods.set_byte_order = exif_mnote_data_olympus_set_byte_order;
411 d->methods.set_offset = exif_mnote_data_olympus_set_offset;
412 d->methods.load = exif_mnote_data_olympus_load;
413 d->methods.save = exif_mnote_data_olympus_save;
414 d->methods.count = exif_mnote_data_olympus_count;
415 d->methods.get_id = exif_mnote_data_olympus_get_id;
416 d->methods.get_name = exif_mnote_data_olympus_get_name;
417 d->methods.get_title = exif_mnote_data_olympus_get_title;
418 d->methods.get_description = exif_mnote_data_olympus_get_description;
419 d->methods.get_value = exif_mnote_data_olympus_get_value;