1 /* exif-mnote-data-pentax.c
3 * Copyright (c) 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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA.
22 #include "exif-mnote-data-pentax.h"
28 #include <libexif/exif-byte-order.h>
29 #include <libexif/exif-utils.h>
31 #define CHECKOVERFLOW(offset,datasize,structsize) (( offset >= datasize) || (structsize > datasize) || (offset > datasize - structsize ))
34 exif_mnote_data_pentax_clear (ExifMnoteDataPentax *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_pentax_free (ExifMnoteData *n)
58 exif_mnote_data_pentax_clear ((ExifMnoteDataPentax *) n);
62 exif_mnote_data_pentax_get_value (ExifMnoteData *d, unsigned int i, char *val, unsigned int maxlen)
64 ExifMnoteDataPentax *n = (ExifMnoteDataPentax *) d;
67 if (n->count <= i) return NULL;
68 return mnote_pentax_entry_get_value (&n->entries[i], val, maxlen);
72 * @brief save the MnoteData from ne to buf
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
79 exif_mnote_data_pentax_save (ExifMnoteData *ne,
80 unsigned char **buf, unsigned int *buf_size)
82 ExifMnoteDataPentax *n = (ExifMnoteDataPentax *) ne;
84 base = 0, /* internal MakerNote tag number offset */
85 o2 = 4 + 2; /* offset to first tag entry, past header */
87 if (!n || !buf || !buf_size) return;
88 datao = n->offset; /* this MakerNote style uses offsets
89 based on main IFD, not makernote IFD */
92 * Allocate enough memory for header, the number of entries, entries,
93 * and next IFD pointer
95 *buf_size = o2 + 2 + n->count * 12 + 4;
98 base = MNOTE_PENTAX2_TAG_BASE;
99 *buf = exif_mem_alloc (ne->mem, *buf_size);
101 EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteDataPentax", *buf_size);
104 /* Write the magic header */
105 strcpy ((char *)*buf, "QVC");
106 exif_set_short (*buf + 4, n->order, (ExifShort) 0);
111 base = MNOTE_PENTAX2_TAG_BASE;
112 *buf = exif_mem_alloc (ne->mem, *buf_size);
114 EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteDataPentax", *buf_size);
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) ?
127 base = MNOTE_PENTAX2_TAG_BASE;
128 *buf = exif_mem_alloc (ne->mem, *buf_size);
130 EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteDataPentax", *buf_size);
134 /* Write the magic header */
135 strcpy ((char *)*buf, "AOC");
136 exif_set_short (*buf + 4, n->order, (ExifShort) 0);
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 */
144 *buf = exif_mem_alloc (ne->mem, *buf_size);
146 EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteDataPentax", *buf_size);
156 /* Write the number of entries. */
157 exif_set_short (*buf + o2, n->order, (ExifShort) n->count);
160 /* Save each entry */
161 for (i = 0; i < n->count; i++) {
162 size_t doff; /* offset to current data portion of tag */
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);
173 s = exif_format_get_size (n->entries[i].format) *
174 n->entries[i].components;
176 /* Corrupt data: EXIF data size is limited to the
177 * maximum size of a JPEG segment (64 kb).
182 size_t ts = *buf_size + s;
184 t = exif_mem_realloc (ne->mem, *buf,
187 EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteDataPentax", ts);
192 exif_set_long (*buf + o, n->order, datao + doff);
196 /* Write the data. */
197 if (n->entries[i].data) {
198 memcpy (*buf + doff, n->entries[i].data, s);
200 /* Most certainly damaged input file */
201 memset (*buf + doff, 0, s);
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",
211 /* Reset next IFD pointer */
212 exif_set_long (*buf + o2 + n->count * 12, n->order, 0);
216 exif_mnote_data_pentax_load (ExifMnoteData *en,
217 const unsigned char *buf, unsigned int buf_size)
219 ExifMnoteDataPentax *n = (ExifMnoteDataPentax *) en;
220 size_t i, tcount, o, datao, base = 0;
223 if (!n || !buf || !buf_size) {
224 exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
225 "ExifMnoteDataPentax", "Short MakerNote");
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");
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;
244 /* Uses Casio v2 tags */
245 n->version = pentaxV2;
247 exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataPentax",
248 "Parsing Pentax maker note v%d...", (int)n->version);
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;
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;
264 /* Read the number of tags */
265 c = exif_get_short (buf + datao, n->order);
268 /* Remove any old entries */
269 exif_mnote_data_pentax_clear (n);
271 /* Reserve enough space for all the possible MakerNote tags */
272 n->entries = exif_mem_alloc (en->mem, sizeof (MnotePentaxEntry) * c);
274 EXIF_LOG_NO_MEMORY(en->log, "ExifMnoteDataPentax", sizeof (MnotePentaxEntry) * c);
278 /* Parse all c entries, storing ones that are successfully parsed */
280 for (i = c, o = datao; i; --i, o += 12) {
283 if (CHECKOVERFLOW(o,buf_size,12)) {
284 exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
285 "ExifMnoteDataPentax", "Short MakerNote");
289 n->entries[tcount].tag = exif_get_short (buf + o + 0, n->order) + base;
290 n->entries[tcount].format = exif_get_short (buf + o + 2, n->order);
291 n->entries[tcount].components = exif_get_long (buf + o + 4, n->order);
292 n->entries[tcount].order = n->order;
294 exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnotePentax",
295 "Loading entry 0x%x ('%s')...", n->entries[tcount].tag,
296 mnote_pentax_tag_get_name (n->entries[tcount].tag));
298 /* Check if we overflow the multiplication. Use buf_size as the max size for integer overflow detection,
299 * we will check the buffer sizes closer later. */
300 if ( exif_format_get_size (n->entries[tcount].format) &&
301 buf_size / exif_format_get_size (n->entries[tcount].format) < n->entries[tcount].components
303 exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
304 "ExifMnoteDataPentax", "Tag size overflow detected (%u * %lu)", exif_format_get_size (n->entries[tcount].format), n->entries[tcount].components);
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[tcount].format) *
312 n->entries[tcount].components;
313 n->entries[tcount].size = s;
315 size_t dataofs = o + 8;
317 /* The data in this case is merely a pointer */
318 dataofs = exif_get_long (buf + dataofs, n->order) + 6;
320 if (CHECKOVERFLOW(dataofs, buf_size, s)) {
321 exif_log (en->log, EXIF_LOG_CODE_DEBUG,
322 "ExifMnoteDataPentax", "Tag data past end "
323 "of buffer (%u > %u)", (unsigned)(dataofs + s), buf_size);
327 n->entries[tcount].data = exif_mem_alloc (en->mem, s);
328 if (!n->entries[tcount].data) {
329 EXIF_LOG_NO_MEMORY(en->log, "ExifMnoteDataPentax", s);
332 memcpy (n->entries[tcount].data, buf + dataofs, s);
335 /* Tag was successfully parsed */
338 /* Store the count of successfully parsed tags */
343 exif_mnote_data_pentax_count (ExifMnoteData *n)
345 return n ? ((ExifMnoteDataPentax *) n)->count : 0;
349 exif_mnote_data_pentax_get_id (ExifMnoteData *d, unsigned int n)
351 ExifMnoteDataPentax *note = (ExifMnoteDataPentax *) d;
354 if (note->count <= n) return 0;
355 return note->entries[n].tag;
359 exif_mnote_data_pentax_get_name (ExifMnoteData *d, unsigned int n)
361 ExifMnoteDataPentax *note = (ExifMnoteDataPentax *) d;
363 if (!note) return NULL;
364 if (note->count <= n) return NULL;
365 return mnote_pentax_tag_get_name (note->entries[n].tag);
369 exif_mnote_data_pentax_get_title (ExifMnoteData *d, unsigned int n)
371 ExifMnoteDataPentax *note = (ExifMnoteDataPentax *) d;
373 if (!note) return NULL;
374 if (note->count <= n) return NULL;
375 return mnote_pentax_tag_get_title (note->entries[n].tag);
379 exif_mnote_data_pentax_get_description (ExifMnoteData *d, unsigned int n)
381 ExifMnoteDataPentax *note = (ExifMnoteDataPentax *) d;
383 if (!note) return NULL;
384 if (note->count <= n) return NULL;
385 return mnote_pentax_tag_get_description (note->entries[n].tag);
389 exif_mnote_data_pentax_set_offset (ExifMnoteData *d, unsigned int o)
391 if (d) ((ExifMnoteDataPentax *) d)->offset = o;
395 exif_mnote_data_pentax_set_byte_order (ExifMnoteData *d, ExifByteOrder o)
397 ExifByteOrder o_orig;
398 ExifMnoteDataPentax *n = (ExifMnoteDataPentax *) d;
405 for (i = 0; i < n->count; i++) {
406 if (n->entries[i].components && (n->entries[i].size/n->entries[i].components < exif_format_get_size (n->entries[i].format)))
408 n->entries[i].order = o;
409 exif_array_set_byte_order (n->entries[i].format, n->entries[i].data,
410 n->entries[i].components, o_orig, o);
415 exif_mnote_data_pentax_identify (const ExifData *ed, const ExifEntry *e)
417 (void) ed; /* unused */
418 if ((e->size >= 8) && !memcmp (e->data, "AOC", 4)) {
419 if (((e->data[4] == 'I') && (e->data[5] == 'I')) ||
420 ((e->data[4] == 'M') && (e->data[5] == 'M')))
423 /* Uses Casio v2 tags */
427 if ((e->size >= 8) && !memcmp (e->data, "QVC", 4))
430 /* This isn't a very robust test, so make sure it's done last */
431 /* Maybe we should additionally check for a make of Asahi or Pentax */
432 if ((e->size >= 2) && (e->data[0] == 0x00) && (e->data[1] == 0x1b))
439 exif_mnote_data_pentax_new (ExifMem *mem)
443 if (!mem) return NULL;
445 d = exif_mem_alloc (mem, sizeof (ExifMnoteDataPentax));
448 exif_mnote_data_construct (d, mem);
450 /* Set up function pointers */
451 d->methods.free = exif_mnote_data_pentax_free;
452 d->methods.set_byte_order = exif_mnote_data_pentax_set_byte_order;
453 d->methods.set_offset = exif_mnote_data_pentax_set_offset;
454 d->methods.load = exif_mnote_data_pentax_load;
455 d->methods.save = exif_mnote_data_pentax_save;
456 d->methods.count = exif_mnote_data_pentax_count;
457 d->methods.get_id = exif_mnote_data_pentax_get_id;
458 d->methods.get_name = exif_mnote_data_pentax_get_name;
459 d->methods.get_title = exif_mnote_data_pentax_get_title;
460 d->methods.get_description = exif_mnote_data_pentax_get_description;
461 d->methods.get_value = exif_mnote_data_pentax_get_value;