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>
32 exif_mnote_data_pentax_clear (ExifMnoteDataPentax *n)
34 ExifMnoteData *d = (ExifMnoteData *) n;
40 for (i = 0; i < n->count; i++)
41 if (n->entries[i].data) {
42 exif_mem_free (d->mem, n->entries[i].data);
43 n->entries[i].data = NULL;
45 exif_mem_free (d->mem, n->entries);
52 exif_mnote_data_pentax_free (ExifMnoteData *n)
56 exif_mnote_data_pentax_clear ((ExifMnoteDataPentax *) n);
60 exif_mnote_data_pentax_get_value (ExifMnoteData *d, unsigned int i, char *val, unsigned int maxlen)
62 ExifMnoteDataPentax *n = (ExifMnoteDataPentax *) d;
65 if (n->count <= i) return NULL;
66 return mnote_pentax_entry_get_value (&n->entries[i], val, maxlen);
70 * @brief save the MnoteData from ne to buf
72 * @param ne extract the data from this structure
73 * @param *buf write the mnoteData to this buffer (buffer will be allocated)
74 * @param buf_size the final size of the buffer
77 exif_mnote_data_pentax_save (ExifMnoteData *ne,
78 unsigned char **buf, unsigned int *buf_size)
80 ExifMnoteDataPentax *n = (ExifMnoteDataPentax *) ne;
82 base = 0, /* internal MakerNote tag number offset */
83 o2 = 4 + 2; /* offset to first tag entry, past header */
85 if (!n || !buf || !buf_size) return;
86 datao = n->offset; /* this MakerNote style uses offsets
87 based on main IFD, not makernote IFD */
90 * Allocate enough memory for header, the number of entries, entries,
91 * and next IFD pointer
93 *buf_size = o2 + 2 + n->count * 12 + 4;
96 base = MNOTE_PENTAX2_TAG_BASE;
97 *buf = exif_mem_alloc (ne->mem, *buf_size);
99 EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteDataPentax", *buf_size);
102 /* Write the magic header */
103 strcpy ((char *)*buf, "QVC");
104 exif_set_short (*buf + 4, n->order, (ExifShort) 0);
109 base = MNOTE_PENTAX2_TAG_BASE;
110 *buf = exif_mem_alloc (ne->mem, *buf_size);
112 EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteDataPentax", *buf_size);
116 /* Write the magic header */
117 strcpy ((char *)*buf, "AOC");
118 exif_set_short (*buf + 4, n->order, (ExifShort) (
119 (n->order == EXIF_BYTE_ORDER_INTEL) ?
125 base = MNOTE_PENTAX2_TAG_BASE;
126 *buf = exif_mem_alloc (ne->mem, *buf_size);
128 EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteDataPentax", *buf_size);
132 /* Write the magic header */
133 strcpy ((char *)*buf, "AOC");
134 exif_set_short (*buf + 4, n->order, (ExifShort) 0);
138 /* It looks like this format doesn't have a magic header as
139 * such, just has a fixed number of entries equal to 0x001b */
142 *buf = exif_mem_alloc (ne->mem, *buf_size);
144 EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteDataPentax", *buf_size);
154 /* Write the number of entries. */
155 exif_set_short (*buf + o2, n->order, (ExifShort) n->count);
158 /* Save each entry */
159 for (i = 0; i < n->count; i++) {
160 size_t doff; /* offset to current data portion of tag */
163 size_t o = o2 + i * 12; /* current offset into output buffer */
164 exif_set_short (*buf + o + 0, n->order,
165 (ExifShort) (n->entries[i].tag - base));
166 exif_set_short (*buf + o + 2, n->order,
167 (ExifShort) n->entries[i].format);
168 exif_set_long (*buf + o + 4, n->order,
169 n->entries[i].components);
171 s = exif_format_get_size (n->entries[i].format) *
172 n->entries[i].components;
174 /* Corrupt data: EXIF data size is limited to the
175 * maximum size of a JPEG segment (64 kb).
180 size_t ts = *buf_size + s;
182 t = exif_mem_realloc (ne->mem, *buf,
185 EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteDataPentax", ts);
190 exif_set_long (*buf + o, n->order, datao + doff);
194 /* Write the data. */
195 if (n->entries[i].data) {
196 memcpy (*buf + doff, n->entries[i].data, s);
198 /* Most certainly damaged input file */
199 memset (*buf + doff, 0, s);
203 /* Sanity check the buffer size */
204 if (*buf_size < (o2 + n->count * 12 + 4)) {
205 exif_log (ne->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifMnoteDataPentax",
209 /* Reset next IFD pointer */
210 exif_set_long (*buf + o2 + n->count * 12, n->order, 0);
214 exif_mnote_data_pentax_load (ExifMnoteData *en,
215 const unsigned char *buf, unsigned int buf_size)
217 ExifMnoteDataPentax *n = (ExifMnoteDataPentax *) en;
218 size_t i, tcount, o, datao, base = 0;
221 if (!n || !buf || !buf_size) {
222 exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
223 "ExifMnoteDataPentax", "Short MakerNote");
226 datao = 6 + n->offset;
227 if ((datao + 8 < datao) || (datao + 8 < 8) || (datao + 8 > buf_size)) {
228 exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
229 "ExifMnoteDataPentax", "Short MakerNote");
233 /* Detect variant of Pentax/Casio MakerNote found */
234 if (!memcmp(buf + datao, "AOC", 4)) {
235 if ((buf[datao + 4] == 'I') && (buf[datao + 5] == 'I')) {
236 n->version = pentaxV3;
237 n->order = EXIF_BYTE_ORDER_INTEL;
238 } else if ((buf[datao + 4] == 'M') && (buf[datao + 5] == 'M')) {
239 n->version = pentaxV3;
240 n->order = EXIF_BYTE_ORDER_MOTOROLA;
242 /* Uses Casio v2 tags */
243 n->version = pentaxV2;
245 exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataPentax",
246 "Parsing Pentax maker note v%d...", (int)n->version);
248 base = MNOTE_PENTAX2_TAG_BASE;
249 } else if (!memcmp(buf + datao, "QVC", 4)) {
250 exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataPentax",
251 "Parsing Casio maker note v2...");
252 n->version = casioV2;
253 base = MNOTE_CASIO2_TAG_BASE;
256 /* probably assert(!memcmp(buf + datao, "\x00\x1b", 2)) */
257 exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataPentax",
258 "Parsing Pentax maker note v1...");
259 n->version = pentaxV1;
262 /* Read the number of tags */
263 c = exif_get_short (buf + datao, n->order);
266 /* Remove any old entries */
267 exif_mnote_data_pentax_clear (n);
269 /* Reserve enough space for all the possible MakerNote tags */
270 n->entries = exif_mem_alloc (en->mem, sizeof (MnotePentaxEntry) * c);
272 EXIF_LOG_NO_MEMORY(en->log, "ExifMnoteDataPentax", sizeof (MnotePentaxEntry) * c);
276 /* Parse all c entries, storing ones that are successfully parsed */
278 for (i = c, o = datao; i; --i, o += 12) {
280 if ((o + 12 < o) || (o + 12 < 12) || (o + 12 > buf_size)) {
281 exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
282 "ExifMnoteDataPentax", "Short MakerNote");
286 n->entries[tcount].tag = exif_get_short (buf + o + 0, n->order) + base;
287 n->entries[tcount].format = exif_get_short (buf + o + 2, n->order);
288 n->entries[tcount].components = exif_get_long (buf + o + 4, n->order);
289 n->entries[tcount].order = n->order;
291 exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnotePentax",
292 "Loading entry 0x%x ('%s')...", n->entries[tcount].tag,
293 mnote_pentax_tag_get_name (n->entries[tcount].tag));
296 * Size? If bigger than 4 bytes, the actual data is not
297 * in the entry but somewhere else (offset).
299 s = exif_format_get_size (n->entries[tcount].format) *
300 n->entries[tcount].components;
301 n->entries[tcount].size = s;
303 size_t dataofs = o + 8;
305 /* The data in this case is merely a pointer */
306 dataofs = exif_get_long (buf + dataofs, n->order) + 6;
307 if ((dataofs + s < dataofs) || (dataofs + s < s) ||
308 (dataofs + s > buf_size)) {
309 exif_log (en->log, EXIF_LOG_CODE_DEBUG,
310 "ExifMnoteDataPentax", "Tag data past end "
311 "of buffer (%u > %u)", (unsigned)(dataofs + s), buf_size);
315 n->entries[tcount].data = exif_mem_alloc (en->mem, s);
316 if (!n->entries[tcount].data) {
317 EXIF_LOG_NO_MEMORY(en->log, "ExifMnoteDataPentax", s);
320 memcpy (n->entries[tcount].data, buf + dataofs, s);
323 /* Tag was successfully parsed */
326 /* Store the count of successfully parsed tags */
331 exif_mnote_data_pentax_count (ExifMnoteData *n)
333 return n ? ((ExifMnoteDataPentax *) n)->count : 0;
337 exif_mnote_data_pentax_get_id (ExifMnoteData *d, unsigned int n)
339 ExifMnoteDataPentax *note = (ExifMnoteDataPentax *) d;
342 if (note->count <= n) return 0;
343 return note->entries[n].tag;
347 exif_mnote_data_pentax_get_name (ExifMnoteData *d, unsigned int n)
349 ExifMnoteDataPentax *note = (ExifMnoteDataPentax *) d;
351 if (!note) return NULL;
352 if (note->count <= n) return NULL;
353 return mnote_pentax_tag_get_name (note->entries[n].tag);
357 exif_mnote_data_pentax_get_title (ExifMnoteData *d, unsigned int n)
359 ExifMnoteDataPentax *note = (ExifMnoteDataPentax *) d;
361 if (!note) return NULL;
362 if (note->count <= n) return NULL;
363 return mnote_pentax_tag_get_title (note->entries[n].tag);
367 exif_mnote_data_pentax_get_description (ExifMnoteData *d, unsigned int n)
369 ExifMnoteDataPentax *note = (ExifMnoteDataPentax *) d;
371 if (!note) return NULL;
372 if (note->count <= n) return NULL;
373 return mnote_pentax_tag_get_description (note->entries[n].tag);
377 exif_mnote_data_pentax_set_offset (ExifMnoteData *d, unsigned int o)
379 if (d) ((ExifMnoteDataPentax *) d)->offset = o;
383 exif_mnote_data_pentax_set_byte_order (ExifMnoteData *d, ExifByteOrder o)
385 ExifByteOrder o_orig;
386 ExifMnoteDataPentax *n = (ExifMnoteDataPentax *) d;
393 for (i = 0; i < n->count; i++) {
394 if (n->entries[i].components && (n->entries[i].size/n->entries[i].components < exif_format_get_size (n->entries[i].format)))
396 n->entries[i].order = o;
397 exif_array_set_byte_order (n->entries[i].format, n->entries[i].data,
398 n->entries[i].components, o_orig, o);
403 exif_mnote_data_pentax_identify (const ExifData *ed, const ExifEntry *e)
405 (void) ed; /* unused */
406 if ((e->size >= 8) && !memcmp (e->data, "AOC", 4)) {
407 if (((e->data[4] == 'I') && (e->data[5] == 'I')) ||
408 ((e->data[4] == 'M') && (e->data[5] == 'M')))
411 /* Uses Casio v2 tags */
415 if ((e->size >= 8) && !memcmp (e->data, "QVC", 4))
418 /* This isn't a very robust test, so make sure it's done last */
419 /* Maybe we should additionally check for a make of Asahi or Pentax */
420 if ((e->size >= 2) && (e->data[0] == 0x00) && (e->data[1] == 0x1b))
427 exif_mnote_data_pentax_new (ExifMem *mem)
431 if (!mem) return NULL;
433 d = exif_mem_alloc (mem, sizeof (ExifMnoteDataPentax));
436 exif_mnote_data_construct (d, mem);
438 /* Set up function pointers */
439 d->methods.free = exif_mnote_data_pentax_free;
440 d->methods.set_byte_order = exif_mnote_data_pentax_set_byte_order;
441 d->methods.set_offset = exif_mnote_data_pentax_set_offset;
442 d->methods.load = exif_mnote_data_pentax_load;
443 d->methods.save = exif_mnote_data_pentax_save;
444 d->methods.count = exif_mnote_data_pentax_count;
445 d->methods.get_id = exif_mnote_data_pentax_get_id;
446 d->methods.get_name = exif_mnote_data_pentax_get_name;
447 d->methods.get_title = exif_mnote_data_pentax_get_title;
448 d->methods.get_description = exif_mnote_data_pentax_get_description;
449 d->methods.get_value = exif_mnote_data_pentax_get_value;