Ensure the MakerNote data pointers are initialized with NULL.
[platform/upstream/libexif.git] / libexif / pentax / exif-mnote-data-pentax.c
1 /* exif-mnote-data-pentax.c
2  *
3  * Copyright (c) 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., 51 Franklin Street, Fifth Floor,
18  * Boston, MA  02110-1301  USA.
19  */
20
21 #include "config.h"
22 #include "exif-mnote-data-pentax.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27
28 #include <libexif/exif-byte-order.h>
29 #include <libexif/exif-utils.h>
30
31 #define CHECKOVERFLOW(offset,datasize,structsize) (( offset >= datasize) || (structsize > datasize) || (offset > datasize - structsize ))
32
33 static void
34 exif_mnote_data_pentax_clear (ExifMnoteDataPentax *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_pentax_free (ExifMnoteData *n)
55 {
56         if (!n) return;
57
58         exif_mnote_data_pentax_clear ((ExifMnoteDataPentax *) n);
59 }
60
61 static char *
62 exif_mnote_data_pentax_get_value (ExifMnoteData *d, unsigned int i, char *val, unsigned int maxlen)
63 {
64         ExifMnoteDataPentax *n = (ExifMnoteDataPentax *) d;
65
66         if (!n) return NULL;
67         if (n->count <= i) return NULL;
68         return mnote_pentax_entry_get_value (&n->entries[i], val, maxlen);
69 }
70
71 /** 
72  * @brief save the MnoteData from ne to buf
73  * 
74  * @param ne extract the data from this structure 
75  * @param *buf write the mnoteData to this buffer (buffer will be allocated)
76  * @param buf_size the final size of the buffer
77  */
78 static void
79 exif_mnote_data_pentax_save (ExifMnoteData *ne,
80                 unsigned char **buf, unsigned int *buf_size)
81 {
82         ExifMnoteDataPentax *n = (ExifMnoteDataPentax *) ne;
83         size_t i,
84            base = 0,            /* internal MakerNote tag number offset */
85            o2 = 4 + 2;          /* offset to first tag entry, past header */
86         size_t datao = n->offset; /* this MakerNote style uses offsets
87                                   based on main IFD, not makernote IFD */
88
89         if (!n || !buf || !buf_size) return;
90
91         /*
92          * Allocate enough memory for header, the number of entries, entries,
93          * and next IFD pointer
94          */
95         *buf_size = o2 + 2 + n->count * 12 + 4;
96         switch (n->version) {
97         case casioV2:
98                 base = MNOTE_PENTAX2_TAG_BASE;
99                 *buf = exif_mem_alloc (ne->mem, *buf_size);
100                 if (!*buf) {
101                         EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteDataPentax", *buf_size);
102                         return;
103                 }
104                 /* Write the magic header */
105                 strcpy ((char *)*buf, "QVC");
106                 exif_set_short (*buf + 4, n->order, (ExifShort) 0);
107
108                 break;
109
110         case pentaxV3:
111                 base = MNOTE_PENTAX2_TAG_BASE;
112                 *buf = exif_mem_alloc (ne->mem, *buf_size);
113                 if (!*buf) {
114                         EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteDataPentax", *buf_size);
115                         return;
116                 }
117
118                 /* Write the magic header */
119                 strcpy ((char *)*buf, "AOC");
120                 exif_set_short (*buf + 4, n->order, (ExifShort) (
121                         (n->order == EXIF_BYTE_ORDER_INTEL) ?
122                         ('I' << 8) | 'I' :
123                         ('M' << 8) | 'M'));
124                 break;
125
126         case pentaxV2:
127                 base = MNOTE_PENTAX2_TAG_BASE;
128                 *buf = exif_mem_alloc (ne->mem, *buf_size);
129                 if (!*buf) {
130                         EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteDataPentax", *buf_size);
131                         return;
132                 }
133
134                 /* Write the magic header */
135                 strcpy ((char *)*buf, "AOC");
136                 exif_set_short (*buf + 4, n->order, (ExifShort) 0);
137                 break;
138
139         case pentaxV1:
140                 /* It looks like this format doesn't have a magic header as
141                  * such, just has a fixed number of entries equal to 0x001b */
142                 *buf_size -= 6;
143                 o2 -= 6;
144                 *buf = exif_mem_alloc (ne->mem, *buf_size);
145                 if (!*buf) {
146                         EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteDataPentax", *buf_size);
147                         return;
148                 }
149                 break;
150
151         default:
152                 /* internal error */
153                 return;
154         }
155
156         /* Write the number of entries. */
157         exif_set_short (*buf + o2, n->order, (ExifShort) n->count);
158         o2 += 2;
159
160         /* Save each entry */
161         for (i = 0; i < n->count; i++) {
162                 size_t doff;    /* offset to current data portion of tag */
163                 size_t s;
164                 unsigned char *t;
165                 size_t o = o2 + i * 12;   /* current offset into output buffer */
166                 exif_set_short (*buf + o + 0, n->order,
167                                 (ExifShort) (n->entries[i].tag - base));
168                 exif_set_short (*buf + o + 2, n->order,
169                                 (ExifShort) n->entries[i].format);
170                 exif_set_long  (*buf + o + 4, n->order,
171                                 n->entries[i].components);
172                 o += 8;
173                 s = exif_format_get_size (n->entries[i].format) *
174                                                 n->entries[i].components;
175                 if (s > 65536) {
176                         /* Corrupt data: EXIF data size is limited to the
177                          * maximum size of a JPEG segment (64 kb).
178                          */
179                         continue;
180                 }
181                 if (s > 4) {
182                         size_t ts = *buf_size + s;
183                         doff = *buf_size;
184                         t = exif_mem_realloc (ne->mem, *buf,
185                                                  sizeof (char) * ts);
186                         if (!t) {
187                                 EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteDataPentax", ts);
188                                 return;
189                         }
190                         *buf = t;
191                         *buf_size = ts;
192                         exif_set_long (*buf + o, n->order, datao + doff);
193                 } else
194                         doff = o;
195
196                 /* Write the data. */
197                 if (n->entries[i].data) {
198                         memcpy (*buf + doff, n->entries[i].data, s);
199                 } else {
200                         /* Most certainly damaged input file */
201                         memset (*buf + doff, 0, s);
202                 }
203         }
204
205         /* Sanity check the buffer size */
206         if (*buf_size < (o2 + n->count * 12 + 4)) {
207                 exif_log (ne->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifMnoteDataPentax",
208                         "Buffer overflow");
209         }
210
211         /* Reset next IFD pointer */
212         exif_set_long (*buf + o2 + n->count * 12, n->order, 0);
213 }
214
215 static void
216 exif_mnote_data_pentax_load (ExifMnoteData *en,
217                 const unsigned char *buf, unsigned int buf_size)
218 {
219         ExifMnoteDataPentax *n = (ExifMnoteDataPentax *) en;
220         size_t i, tcount, o, datao, base = 0;
221         ExifShort c;
222
223         if (!n || !buf || !buf_size) {
224                 exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
225                           "ExifMnoteDataPentax", "Short MakerNote");
226                 return;
227         }
228         datao = 6 + n->offset;
229         if (CHECKOVERFLOW(datao, buf_size, 8)) {
230                 exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
231                           "ExifMnoteDataPentax", "Short MakerNote");
232                 return;
233         }
234
235         /* Detect variant of Pentax/Casio MakerNote found */
236         if (!memcmp(buf + datao, "AOC", 4)) {
237                 if ((buf[datao + 4] == 'I') && (buf[datao + 5] == 'I')) {
238                         n->version = pentaxV3;
239                         n->order = EXIF_BYTE_ORDER_INTEL;
240                 } else if ((buf[datao + 4] == 'M') && (buf[datao + 5] == 'M')) {
241                         n->version = pentaxV3;
242                         n->order = EXIF_BYTE_ORDER_MOTOROLA;
243                 } else {
244                         /* Uses Casio v2 tags */
245                         n->version = pentaxV2;
246                 }
247                 exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataPentax",
248                         "Parsing Pentax maker note v%d...", (int)n->version);
249                 datao += 4 + 2;
250                 base = MNOTE_PENTAX2_TAG_BASE;
251         } else if (!memcmp(buf + datao, "QVC", 4)) {
252                 exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataPentax",
253                         "Parsing Casio maker note v2...");
254                 n->version = casioV2;
255                 base = MNOTE_CASIO2_TAG_BASE;
256                 datao += 4 + 2;
257         } else {
258                 /* probably assert(!memcmp(buf + datao, "\x00\x1b", 2)) */
259                 exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataPentax",
260                         "Parsing Pentax maker note v1...");
261                 n->version = pentaxV1;
262         }
263
264         /* Read the number of tags */
265         c = exif_get_short (buf + datao, n->order);
266         datao += 2;
267
268         /* Remove any old entries */
269         exif_mnote_data_pentax_clear (n);
270
271         /* Reserve enough space for all the possible MakerNote tags */
272         n->entries = exif_mem_alloc (en->mem, sizeof (MnotePentaxEntry) * c);
273         if (!n->entries) {
274                 EXIF_LOG_NO_MEMORY(en->log, "ExifMnoteDataPentax", sizeof (MnotePentaxEntry) * c);
275                 return;
276         }
277
278         /* Parse all c entries, storing ones that are successfully parsed */
279         tcount = 0;
280         for (i = c, o = datao; i; --i, o += 12) {
281                 size_t s;
282
283                 memset(&n->entries[tcount], 0, sizeof(MnotePentaxEntry));
284                 if (CHECKOVERFLOW(o,buf_size,12)) {
285                         exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
286                                   "ExifMnoteDataPentax", "Short MakerNote");
287                         break;
288                 }
289
290                 n->entries[tcount].tag        = exif_get_short (buf + o + 0, n->order) + base;
291                 n->entries[tcount].format     = exif_get_short (buf + o + 2, n->order);
292                 n->entries[tcount].components = exif_get_long  (buf + o + 4, n->order);
293                 n->entries[tcount].order      = n->order;
294
295                 exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnotePentax",
296                           "Loading entry 0x%x ('%s')...", n->entries[tcount].tag,
297                           mnote_pentax_tag_get_name (n->entries[tcount].tag));
298
299                 /* Check if we overflow the multiplication. Use buf_size as the max size for integer overflow detection,
300                  * we will check the buffer sizes closer later. */
301                 if (    exif_format_get_size (n->entries[tcount].format) &&
302                         buf_size / exif_format_get_size (n->entries[tcount].format) < n->entries[tcount].components
303                 ) {
304                         exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
305                                   "ExifMnoteDataPentax", "Tag size overflow detected (%u * %lu)", exif_format_get_size (n->entries[tcount].format), n->entries[tcount].components);
306                         break;
307                 }
308                 /*
309                  * Size? If bigger than 4 bytes, the actual data is not
310                  * in the entry but somewhere else (offset).
311                  */
312                 s = exif_format_get_size (n->entries[tcount].format) *
313                                       n->entries[tcount].components;
314                 n->entries[tcount].size = s;
315                 if (s) {
316                         size_t dataofs = o + 8;
317                         if (s > 4)
318                                 /* The data in this case is merely a pointer */
319                                 dataofs = exif_get_long (buf + dataofs, n->order) + 6;
320
321                         if (CHECKOVERFLOW(dataofs, buf_size, s)) {
322                                 exif_log (en->log, EXIF_LOG_CODE_DEBUG,
323                                                   "ExifMnoteDataPentax", "Tag data past end "
324                                           "of buffer (%u > %u)", dataofs + s, buf_size);
325                                 continue;
326                         }
327
328                         n->entries[tcount].data = exif_mem_alloc (en->mem, s);
329                         if (!n->entries[tcount].data) {
330                                 EXIF_LOG_NO_MEMORY(en->log, "ExifMnoteDataPentax", s);
331                                 continue;
332                         }
333                         memcpy (n->entries[tcount].data, buf + dataofs, s);
334                 }
335
336                 /* Tag was successfully parsed */
337                 ++tcount;
338         }
339         /* Store the count of successfully parsed tags */
340         n->count = tcount;
341 }
342
343 static unsigned int
344 exif_mnote_data_pentax_count (ExifMnoteData *n)
345 {
346         return n ? ((ExifMnoteDataPentax *) n)->count : 0;
347 }
348
349 static unsigned int
350 exif_mnote_data_pentax_get_id (ExifMnoteData *d, unsigned int n)
351 {
352         ExifMnoteDataPentax *note = (ExifMnoteDataPentax *) d;
353
354         if (!note) return 0;
355         if (note->count <= n) return 0;
356         return note->entries[n].tag;
357 }
358
359 static const char *
360 exif_mnote_data_pentax_get_name (ExifMnoteData *d, unsigned int n)
361 {
362         ExifMnoteDataPentax *note = (ExifMnoteDataPentax *) d;
363
364         if (!note) return NULL;
365         if (note->count <= n) return NULL;
366         return mnote_pentax_tag_get_name (note->entries[n].tag);
367 }
368
369 static const char *
370 exif_mnote_data_pentax_get_title (ExifMnoteData *d, unsigned int n)
371 {
372         ExifMnoteDataPentax *note = (ExifMnoteDataPentax *) d;
373
374         if (!note) return NULL;
375         if (note->count <= n) return NULL;
376         return mnote_pentax_tag_get_title (note->entries[n].tag);
377 }
378
379 static const char *
380 exif_mnote_data_pentax_get_description (ExifMnoteData *d, unsigned int n)
381 {
382         ExifMnoteDataPentax *note = (ExifMnoteDataPentax *) d;
383         
384         if (!note) return NULL;
385         if (note->count <= n) return NULL;
386         return mnote_pentax_tag_get_description (note->entries[n].tag);
387 }
388
389 static void
390 exif_mnote_data_pentax_set_offset (ExifMnoteData *d, unsigned int o)
391 {
392         if (d) ((ExifMnoteDataPentax *) d)->offset = o;
393 }
394
395 static void
396 exif_mnote_data_pentax_set_byte_order (ExifMnoteData *d, ExifByteOrder o)
397 {
398         ExifByteOrder o_orig;
399         ExifMnoteDataPentax *n = (ExifMnoteDataPentax *) d;
400         unsigned int i;
401
402         if (!n) return;
403
404         o_orig = n->order;
405         n->order = o;
406         for (i = 0; i < n->count; i++) {
407                 n->entries[i].order = o;
408                 exif_array_set_byte_order (n->entries[i].format, n->entries[i].data,
409                                 n->entries[i].components, o_orig, o);
410         }
411 }
412
413 int
414 exif_mnote_data_pentax_identify (const ExifData *ed, const ExifEntry *e)
415 {
416         if ((e->size >= 8) && !memcmp (e->data, "AOC", 4)) {
417                 if (((e->data[4] == 'I') && (e->data[5] == 'I')) ||
418                     ((e->data[4] == 'M') && (e->data[5] == 'M')))
419                         return pentaxV3;
420                 else
421                         /* Uses Casio v2 tags */
422                         return pentaxV2;
423         }
424
425         if ((e->size >= 8) && !memcmp (e->data, "QVC", 4))
426                 return casioV2;
427
428         /* This isn't a very robust test, so make sure it's done last */
429         /* Maybe we should additionally check for a make of Asahi or Pentax */
430         if ((e->size >= 2) && (e->data[0] == 0x00) && (e->data[1] == 0x1b))
431                 return pentaxV1;
432
433         return 0;
434 }
435
436 ExifMnoteData *
437 exif_mnote_data_pentax_new (ExifMem *mem)
438 {
439         ExifMnoteData *d;
440
441         if (!mem) return NULL;
442
443         d = exif_mem_alloc (mem, sizeof (ExifMnoteDataPentax));
444         if (!d) return NULL;
445
446         exif_mnote_data_construct (d, mem);
447
448         /* Set up function pointers */
449         d->methods.free            = exif_mnote_data_pentax_free;
450         d->methods.set_byte_order  = exif_mnote_data_pentax_set_byte_order;
451         d->methods.set_offset      = exif_mnote_data_pentax_set_offset;
452         d->methods.load            = exif_mnote_data_pentax_load;
453         d->methods.save            = exif_mnote_data_pentax_save;
454         d->methods.count           = exif_mnote_data_pentax_count;
455         d->methods.get_id          = exif_mnote_data_pentax_get_id;
456         d->methods.get_name        = exif_mnote_data_pentax_get_name;
457         d->methods.get_title       = exif_mnote_data_pentax_get_title;
458         d->methods.get_description = exif_mnote_data_pentax_get_description;
459         d->methods.get_value       = exif_mnote_data_pentax_get_value;
460
461         return d;
462 }