1 /* exif-mnote-data-canon.c
3 * Copyright (c) 2002, 2003 Lutz Mueller <lutz@users.sourceforge.net>
4 * Copyright (c) 2003 Matthieu Castet <mat-c@users.sourceforge.net>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 #include "exif-mnote-data-canon.h"
29 #include <libexif/exif-byte-order.h>
30 #include <libexif/exif-utils.h>
31 #include <libexif/exif-data.h>
36 exif_mnote_data_canon_clear (ExifMnoteDataCanon *n)
38 ExifMnoteData *d = (ExifMnoteData *) n;
44 for (i = 0; i < n->count; i++)
45 if (n->entries[i].data) {
46 exif_mem_free (d->mem, n->entries[i].data);
47 n->entries[i].data = NULL;
49 exif_mem_free (d->mem, n->entries);
56 exif_mnote_data_canon_free (ExifMnoteData *n)
60 exif_mnote_data_canon_clear ((ExifMnoteDataCanon *) n);
64 exif_mnote_data_canon_get_tags (ExifMnoteDataCanon *dc, unsigned int n,
65 unsigned int *m, unsigned int *s)
67 unsigned int from = 0, to;
69 if (!dc || !m) return;
70 for (*m = 0; *m < dc->count; (*m)++) {
71 to = from + mnote_canon_entry_count_values (&dc->entries[*m]);
81 exif_mnote_data_canon_get_value (ExifMnoteData *note, unsigned int n, char *val, unsigned int maxlen)
83 ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) note;
87 exif_mnote_data_canon_get_tags (dc, n, &m, &s);
88 if (m >= dc->count) return NULL;
89 return mnote_canon_entry_get_value (&dc->entries[m], s, val, maxlen);
93 exif_mnote_data_canon_set_byte_order (ExifMnoteData *d, ExifByteOrder o)
96 ExifMnoteDataCanon *n = (ExifMnoteDataCanon *) d;
103 for (i = 0; i < n->count; i++) {
104 n->entries[i].order = o;
105 exif_array_set_byte_order (n->entries[i].format, n->entries[i].data,
106 n->entries[i].components, o_orig, o);
111 exif_mnote_data_canon_set_offset (ExifMnoteData *n, unsigned int o)
113 if (n) ((ExifMnoteDataCanon *) n)->offset = o;
117 exif_mnote_data_canon_save (ExifMnoteData *ne,
118 unsigned char **buf, unsigned int *buf_size)
120 ExifMnoteDataCanon *n = (ExifMnoteDataCanon *) ne;
121 size_t i, o, s, doff;
125 if (!n || !buf || !buf_size) return;
128 * Allocate enough memory for all entries and the number
131 *buf_size = 2 + n->count * 12 + 4;
132 *buf = exif_mem_alloc (ne->mem, sizeof (char) * *buf_size);
134 EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteCanon", *buf_size);
138 /* Save the number of entries */
139 exif_set_short (*buf, n->order, (ExifShort) n->count);
141 /* Save each entry */
142 for (i = 0; i < n->count; i++) {
144 exif_set_short (*buf + o + 0, n->order, (ExifShort) n->entries[i].tag);
145 exif_set_short (*buf + o + 2, n->order, (ExifShort) n->entries[i].format);
146 exif_set_long (*buf + o + 4, n->order,
147 n->entries[i].components);
149 s = exif_format_get_size (n->entries[i].format) *
150 n->entries[i].components;
152 /* Corrupt data: EXIF data size is limited to the
153 * maximum size of a JPEG segment (64 kb).
160 /* Ensure even offsets. Set padding bytes to 0. */
162 t = exif_mem_realloc (ne->mem, *buf,
165 EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteCanon", ts);
170 doff = *buf_size - s;
171 if (s & 1) { doff--; *(*buf + *buf_size - 1) = '\0'; }
172 exif_set_long (*buf + o, n->order, n->offset + doff);
177 * Write the data. Fill unneeded bytes with 0. Do not
178 * crash if data is NULL.
180 if (!n->entries[i].data) memset (*buf + doff, 0, s);
181 else memcpy (*buf + doff, n->entries[i].data, s);
182 if (s < 4) memset (*buf + doff + s, 0, (4 - s));
187 * FIXME: exif_mnote_data_canon_load() may fail and there is no
188 * semantics to express that.
189 * See bug #1054323 for details, especially the comment by liblit
190 * after it has supposedly been fixed:
192 * https://sourceforge.net/tracker/?func=detail&aid=1054323&group_id=12272&atid=112272
193 * Unfortunately, the "return" statements aren't commented at
194 * all, so it isn't trivial to find out what is a normal
195 * return, and what is a reaction to an error condition.
199 exif_mnote_data_canon_load (ExifMnoteData *ne,
200 const unsigned char *buf, unsigned int buf_size)
202 ExifMnoteDataCanon *n = (ExifMnoteDataCanon *) ne;
204 size_t i, tcount, o, datao = 6 + n->offset;
206 if (!n || !buf || !buf_size || (datao + 2 < datao) ||
207 (datao + 2 < 2) || (datao + 2 > buf_size)) {
208 exif_log (ne->log, EXIF_LOG_CODE_CORRUPT_DATA,
209 "ExifMnoteCanon", "Short MakerNote");
213 /* Read the number of tags */
214 c = exif_get_short (buf + datao, n->order);
217 /* Remove any old entries */
218 exif_mnote_data_canon_clear (n);
220 /* Reserve enough space for all the possible MakerNote tags */
221 n->entries = exif_mem_alloc (ne->mem, sizeof (MnoteCanonEntry) * c);
223 EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteCanon", sizeof (MnoteCanonEntry) * c);
227 /* Parse the entries */
229 for (i = c, o = datao; i; --i, o += 12) {
231 if ((o + 12 < o) || (o + 12 < 12) || (o + 12 > buf_size)) {
232 exif_log (ne->log, EXIF_LOG_CODE_CORRUPT_DATA,
233 "ExifMnoteCanon", "Short MakerNote");
237 n->entries[tcount].tag = exif_get_short (buf + o, n->order);
238 n->entries[tcount].format = exif_get_short (buf + o + 2, n->order);
239 n->entries[tcount].components = exif_get_long (buf + o + 4, n->order);
240 n->entries[tcount].order = n->order;
242 exif_log (ne->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteCanon",
243 "Loading entry 0x%x ('%s')...", n->entries[tcount].tag,
244 mnote_canon_tag_get_name (n->entries[tcount].tag));
247 * Size? If bigger than 4 bytes, the actual data is not
248 * in the entry but somewhere else (offset).
250 s = exif_format_get_size (n->entries[tcount].format) *
251 n->entries[tcount].components;
252 n->entries[tcount].size = s;
254 exif_log (ne->log, EXIF_LOG_CODE_CORRUPT_DATA,
256 "Invalid zero-length tag size");
260 size_t dataofs = o + 8;
261 if (s > 4) dataofs = exif_get_long (buf + dataofs, n->order) + 6;
262 if ((dataofs + s < s) || (dataofs + s < dataofs) || (dataofs + s > buf_size)) {
263 exif_log (ne->log, EXIF_LOG_CODE_CORRUPT_DATA,
265 "Tag data past end of buffer (%u > %u)",
266 dataofs + s, buf_size);
270 n->entries[tcount].data = exif_mem_alloc (ne->mem, s);
271 if (!n->entries[tcount].data) {
272 EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteCanon", s);
275 memcpy (n->entries[tcount].data, buf + dataofs, s);
278 /* Tag was successfully parsed */
281 /* Store the count of successfully parsed tags */
286 exif_mnote_data_canon_count (ExifMnoteData *n)
288 ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) n;
291 for (i = c = 0; dc && (i < dc->count); i++)
292 c += mnote_canon_entry_count_values (&dc->entries[i]);
297 exif_mnote_data_canon_get_id (ExifMnoteData *d, unsigned int i)
299 ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) d;
303 exif_mnote_data_canon_get_tags (dc, i, &m, NULL);
304 if (m >= dc->count) return 0;
305 return dc->entries[m].tag;
309 exif_mnote_data_canon_get_name (ExifMnoteData *note, unsigned int i)
311 ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) note;
314 if (!dc) return NULL;
315 exif_mnote_data_canon_get_tags (dc, i, &m, &s);
316 if (m >= dc->count) return NULL;
317 return mnote_canon_tag_get_name_sub (dc->entries[m].tag, s, dc->options);
321 exif_mnote_data_canon_get_title (ExifMnoteData *note, unsigned int i)
323 ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) note;
326 if (!dc) return NULL;
327 exif_mnote_data_canon_get_tags (dc, i, &m, &s);
328 if (m >= dc->count) return NULL;
329 return mnote_canon_tag_get_title_sub (dc->entries[m].tag, s, dc->options);
333 exif_mnote_data_canon_get_description (ExifMnoteData *note, unsigned int i)
335 ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) note;
338 if (!dc) return NULL;
339 exif_mnote_data_canon_get_tags (dc, i, &m, NULL);
340 if (m >= dc->count) return NULL;
341 return mnote_canon_tag_get_description (dc->entries[m].tag);
345 exif_mnote_data_canon_new (ExifMem *mem, ExifDataOption o)
348 ExifMnoteDataCanon *dc;
350 if (!mem) return NULL;
352 d = exif_mem_alloc (mem, sizeof (ExifMnoteDataCanon));
356 exif_mnote_data_construct (d, mem);
358 /* Set up function pointers */
359 d->methods.free = exif_mnote_data_canon_free;
360 d->methods.set_byte_order = exif_mnote_data_canon_set_byte_order;
361 d->methods.set_offset = exif_mnote_data_canon_set_offset;
362 d->methods.load = exif_mnote_data_canon_load;
363 d->methods.save = exif_mnote_data_canon_save;
364 d->methods.count = exif_mnote_data_canon_count;
365 d->methods.get_id = exif_mnote_data_canon_get_id;
366 d->methods.get_name = exif_mnote_data_canon_get_name;
367 d->methods.get_title = exif_mnote_data_canon_get_title;
368 d->methods.get_description = exif_mnote_data_canon_get_description;
369 d->methods.get_value = exif_mnote_data_canon_get_value;
371 dc = (ExifMnoteDataCanon*)d;