Nikon v1 makernotes were saved with wrong offsets, Nikon v1 & v2 maker notes
[platform/upstream/libexif.git] / libexif / olympus / exif-mnote-data-olympus.c
1 /* exif-mnote-data-olympus.c
2  *
3  * Copyright © 2002, 2003 Lutz Mueller <lutz@users.sourceforge.net>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include <config.h>
22 #include "exif-mnote-data-olympus.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27
28 #include <libexif/exif-utils.h>
29 #include <libexif/exif-data.h>
30
31 #define DEBUG
32
33 static void
34 exif_mnote_data_olympus_clear (ExifMnoteDataOlympus *n)
35 {
36         ExifMnoteData *d = (ExifMnoteData *) n;
37         unsigned int i;
38
39         if (!n) return;
40
41         if (n->entries) {
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;
46                         }
47                 exif_mem_free (d->mem, n->entries);
48                 n->entries = NULL;
49                 n->count = 0;
50         }
51 }
52
53 static void
54 exif_mnote_data_olympus_free (ExifMnoteData *n)
55 {
56         if (!n) return;
57
58         exif_mnote_data_olympus_clear ((ExifMnoteDataOlympus *) n);
59 }
60
61 static char *
62 exif_mnote_data_olympus_get_value (ExifMnoteData *d, unsigned int i, char *val, unsigned int maxlen)
63 {
64         ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) d;
65
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);
72 }
73
74
75
76
77 /** 
78  * @brief save the MnoteData from ne to buf
79  * 
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
83  */
84 static void
85 exif_mnote_data_olympus_save (ExifMnoteData *ne,
86                 unsigned char **buf, unsigned int *buf_size)
87 {
88         ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) ne;
89         unsigned int i, o, s, doff, base = 0, o2 = 6;
90         int datao = 0;
91
92         if (!n || !buf || !buf_size) return;
93
94         /*
95          * Allocate enough memory for all entries and the number of entries.
96          */
97         *buf_size = 6 + 2 + 2 + n->count * 12;
98         o2 += 2;
99         switch (n->version) {
100         case olympus: 
101                 *buf = exif_mem_alloc (ne->mem, *buf_size);
102                 if (!*buf) return;
103
104                 /* Write the header and the number of entries. */
105                 strcpy ((char *)*buf, "OLYMP");
106                 datao = n->offset;
107                 break;
108         case nikonV1: 
109                 base = MNOTE_NIKON1_TAG_BASE;
110
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 */
114                 *buf_size -= 8 + 2;
115                 /* Fall through */
116         case nikonV2: 
117                 *buf_size += 8 + 2;
118                 *buf_size += 4; /* Next IFD pointer */
119                 *buf = exif_mem_alloc (ne->mem, *buf_size);
120                 if (!*buf) return;
121
122                 /* Write the header and the number of entries. */
123                 strcpy ((char *)*buf, "Nikon");
124                 (*buf)[6] = n->version;
125
126                 if (n->version == nikonV2) {
127                         exif_set_short (*buf + 10, n->order, (ExifShort) (
128                                 (n->order == EXIF_BYTE_ORDER_INTEL) ?
129                                 ('I' << 8) | 'I' :
130                                 ('M' << 8) | 'M'));
131                         exif_set_short (*buf + 12, n->order, (ExifShort) 0x2A);
132                         exif_set_long (*buf + 14, n->order, (ExifShort) 8);
133                         o2 += 2 + 8;
134                 }
135                 datao -= 10;
136                 /* Reset next IFD pointer */
137                 exif_set_long (*buf + o2 + 2 + n->count * 12, n->order, 0);
138                 break;
139
140         default:
141                 return;
142         }
143
144         exif_set_short (*buf + o2, n->order, (ExifShort) n->count);
145         o2 += 2;
146
147         /* Save each entry */
148         for (i = 0; i < n->count; i++) {
149                 o = o2 + i * 12;
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);
156                 o += 8;
157                 s = exif_format_get_size (n->entries[i].format) *
158                                                 n->entries[i].components;
159                 if (s > 4) {
160                         doff = *buf_size;
161                         *buf_size += s;
162                         *buf = exif_mem_realloc (ne->mem, *buf,
163                                                  sizeof (char) * *buf_size);
164                         if (!*buf) return;
165                         exif_set_long (*buf + o, n->order, datao + doff);
166                 } else
167                         doff = o;
168
169                 /* Write the data. */
170                 if (n->entries[i].data) {
171                         memcpy (*buf + doff, n->entries[i].data, s);
172                 } else {
173                         /* Most certainly damaged input file */
174                         memset (*buf + doff, 0, s);
175                 }
176         }
177 }
178
179 static void
180 exif_mnote_data_olympus_load (ExifMnoteData *en,
181                               const unsigned char *buf, unsigned int buf_size)
182 {
183         ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) en;
184         ExifShort c;
185         unsigned int i, s, o, o2 = 0, datao = 6, base = 0;
186
187         if (!n || !buf) return;
188
189         /* Start of interesting data */
190         o2 = 6 + n->offset;
191
192         /*
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.
196          *
197          * Nikon headers start with "Nikon" (6 bytes including '\0'), 
198          * version number (1 or 2).
199          * 
200          * Version 1 continues with 0, 1, 0, number_of_tags,
201          * or just with number_of_tags (models D1H, D1X...).
202          * 
203          * Version 2 continues with an unknown byte (0 or 10),
204          * two unknown bytes (0), "MM" or "II", another byte 0 and 
205          * lastly 0x2A.
206          */
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...");
211
212                 /* The number of entries is at position 8. */
213                 n->version = olympus;
214                 o2 += 8;
215
216         } else if (!memcmp (buf + o2, "Nikon", 6)) {
217                 o2 += 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];
226                 o2 += 1;
227
228                 /* Skip an unknown byte (00 or 0A). */
229                 o2 += 1;
230
231                 switch (n->version) {
232                 case nikonV1:
233
234                         base = MNOTE_NIKON1_TAG_BASE;
235                         break;
236
237                 case nikonV2:
238
239                         /* Skip 2 unknown bytes (00 00). */
240                         o2 += 2;
241
242                         /*
243                          * Byte order. From here the data offset
244                          * gets calculated.
245                          */
246                         datao = o2;
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;
252                         else {
253                                 exif_log (en->log, EXIF_LOG_CODE_DEBUG,
254                                         "ExifMnoteDatalympus", "Unknown "
255                                         "byte order '%c%c'", buf[o2],
256                                         buf[o2 + 1]);
257                                 return;
258                         }
259                         o2 += 2;
260
261                         /* Skip 2 unknown bytes (00 2A). */
262                         o2 += 2;
263
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);
267                         break;
268
269                 default:
270                         exif_log (en->log, EXIF_LOG_CODE_DEBUG,
271                                 "ExifMnoteDataOlympus", "Unknown version "
272                                 "number %i.", n->version);
273                         return;
274                 }
275         } else if (!memcmp (buf + o2, "\0\x1b", 2)) {
276                 n->version = nikonV2;
277         } else {
278                 return;
279         }
280
281         /* Number of entries */
282         if (o2 >= buf_size) return;
283         c = exif_get_short (buf + o2, n->order);
284         o2 += 2;
285
286         /* Read the number of entries and remove old ones. */
287         exif_mnote_data_olympus_clear (n);
288
289         n->entries = exif_mem_alloc (en->mem, sizeof (MnoteOlympusEntry) * c);
290         if (!n->entries) return;
291
292         /* Parse the entries */
293         for (i = 0; i < c; i++) {
294             o = o2 + 12 * i;
295             if (o + 12 > buf_size) return;
296
297             n->count = i + 1;
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;
302
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));
306
307             /*
308              * Size? If bigger than 4 bytes, the actual data is not
309              * in the entry but somewhere else (offset).
310              */
311             s = exif_format_get_size (n->entries[i].format) *
312                                          n->entries[i].components;
313             if (!s) continue;
314             o += 8;
315             if (s > 4) o = exif_get_long (buf + o, n->order) + datao;
316             if (o + s > buf_size) continue;
317
318             /* Sanity check */
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);
323         }
324 }
325
326 static unsigned int
327 exif_mnote_data_olympus_count (ExifMnoteData *n)
328 {
329         return n ? ((ExifMnoteDataOlympus *) n)->count : 0;
330 }
331
332 static unsigned int
333 exif_mnote_data_olympus_get_id (ExifMnoteData *d, unsigned int n)
334 {
335         ExifMnoteDataOlympus *note = (ExifMnoteDataOlympus *) d;
336
337         if (!note) return 0;
338         if (note->count <= n) return 0;
339         return note->entries[n].tag;
340 }
341
342 static const char *
343 exif_mnote_data_olympus_get_name (ExifMnoteData *d, unsigned int i)
344 {
345         ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) d;
346
347         if (!n) return NULL;
348         if (i >= n->count) return NULL;
349         return mnote_olympus_tag_get_name (n->entries[i].tag);
350 }
351
352 static const char *
353 exif_mnote_data_olympus_get_title (ExifMnoteData *d, unsigned int i)
354 {
355         ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) d;
356         
357         if (!n) return NULL;
358         if (i >= n->count) return NULL;
359         return mnote_olympus_tag_get_title (n->entries[i].tag);
360 }
361
362 static const char *
363 exif_mnote_data_olympus_get_description (ExifMnoteData *d, unsigned int i)
364 {
365         ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) d;
366         
367         if (!n) return NULL;
368         if (i >= n->count) return NULL;
369         return mnote_olympus_tag_get_title (n->entries[i].tag);
370 }
371
372 static void
373 exif_mnote_data_olympus_set_byte_order (ExifMnoteData *d, ExifByteOrder o)
374 {
375         ExifByteOrder o_orig;
376         ExifMnoteDataOlympus *n = (ExifMnoteDataOlympus *) d;
377         unsigned int i;
378
379         if (!n) return;
380
381         o_orig = n->order;
382         n->order = o;
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);
387         }
388 }
389
390 static void
391 exif_mnote_data_olympus_set_offset (ExifMnoteData *n, unsigned int o)
392 {
393         if (n) ((ExifMnoteDataOlympus *) n)->offset = o;
394 }
395
396 ExifMnoteData *
397 exif_mnote_data_olympus_new (ExifMem *mem)
398 {
399         ExifMnoteData *d;
400
401         if (!mem) return NULL;
402         
403         d = exif_mem_alloc (mem, sizeof (ExifMnoteDataOlympus));
404         if (!d) return NULL;
405
406         exif_mnote_data_construct (d, mem);
407
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;
420
421         return d;
422 }