1 /* exif-mnote-data-fuji.c
3 * Copyright (c) 2002 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.
26 #include <libexif/exif-byte-order.h>
27 #include <libexif/exif-utils.h>
29 #include "exif-mnote-data-fuji.h"
31 struct _MNoteFujiDataPrivate {
36 exif_mnote_data_fuji_clear (ExifMnoteDataFuji *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_fuji_free (ExifMnoteData *n)
60 exif_mnote_data_fuji_clear ((ExifMnoteDataFuji *) n);
64 exif_mnote_data_fuji_get_value (ExifMnoteData *d, unsigned int i, char *val, unsigned int maxlen)
66 ExifMnoteDataFuji *n = (ExifMnoteDataFuji *) d;
68 if (!d || !val) return NULL;
69 if (i > n->count -1) return NULL;
71 exif_log (d->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataFuji",
72 "Querying value for tag '%s'...",
73 mnote_fuji_tag_get_name (n->entries[i].tag));
75 return mnote_fuji_entry_get_value (&n->entries[i], val, maxlen);
79 exif_mnote_data_fuji_save (ExifMnoteData *ne, unsigned char **buf,
80 unsigned int *buf_size)
82 ExifMnoteDataFuji *n = (ExifMnoteDataFuji *) ne;
87 if (!n || !buf || !buf_size) return;
90 * Allocate enough memory for all entries and the number
93 *buf_size = 8 + 4 + 2 + n->count * 12 + 4;
94 *buf = exif_mem_alloc (ne->mem, *buf_size);
101 * Header: "FUJIFILM" and 4 bytes offset to the first entry.
102 * As the first entry will start right thereafter, the offset is 12.
104 memcpy (*buf, "FUJIFILM", 8);
105 exif_set_long (*buf + 8, n->order, 12);
107 /* Save the number of entries */
108 exif_set_short (*buf + 8 + 4, n->order, (ExifShort) n->count);
110 /* Save each entry */
111 for (i = 0; i < n->count; i++) {
112 o = 8 + 4 + 2 + i * 12;
113 exif_set_short (*buf + o + 0, n->order, (ExifShort) n->entries[i].tag);
114 exif_set_short (*buf + o + 2, n->order, (ExifShort) n->entries[i].format);
115 exif_set_long (*buf + o + 4, n->order, n->entries[i].components);
117 s = exif_format_get_size (n->entries[i].format) *
118 n->entries[i].components;
120 /* Corrupt data: EXIF data size is limited to the
121 * maximum size of a JPEG segment (64 kb).
128 /* Ensure even offsets. Set padding bytes to 0. */
130 t = exif_mem_realloc (ne->mem, *buf, ts);
136 doff = *buf_size - s;
137 if (s & 1) { doff--; *(*buf + *buf_size - 1) = '\0'; }
138 exif_set_long (*buf + o, n->order, doff);
143 * Write the data. Fill unneeded bytes with 0. Do not
144 * crash if data is NULL.
146 if (!n->entries[i].data) memset (*buf + doff, 0, s);
147 else memcpy (*buf + doff, n->entries[i].data, s);
152 exif_mnote_data_fuji_load (ExifMnoteData *en,
153 const unsigned char *buf, unsigned int buf_size)
155 ExifMnoteDataFuji *n = (ExifMnoteDataFuji*) en;
157 size_t i, tcount, o, datao;
159 if (!n || !buf || !buf_size) {
160 exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
161 "ExifMnoteDataFuji", "Short MakerNote");
164 datao = 6 + n->offset;
165 if ((datao + 12 < datao) || (datao + 12 < 12) || (datao + 12 > buf_size)) {
166 exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
167 "ExifMnoteDataFuji", "Short MakerNote");
171 n->order = EXIF_BYTE_ORDER_INTEL;
172 datao += exif_get_long (buf + datao + 8, EXIF_BYTE_ORDER_INTEL);
173 if ((datao + 2 < datao) || (datao + 2 < 2) ||
174 (datao + 2 > buf_size)) {
175 exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
176 "ExifMnoteDataFuji", "Short MakerNote");
180 /* Read the number of tags */
181 c = exif_get_short (buf + datao, EXIF_BYTE_ORDER_INTEL);
184 /* Remove any old entries */
185 exif_mnote_data_fuji_clear (n);
187 /* Reserve enough space for all the possible MakerNote tags */
188 n->entries = exif_mem_alloc (en->mem, sizeof (MnoteFujiEntry) * c);
190 EXIF_LOG_NO_MEMORY(en->log, "ExifMnoteDataFuji", sizeof (MnoteFujiEntry) * c);
194 /* Parse all c entries, storing ones that are successfully parsed */
196 for (i = c, o = datao; i; --i, o += 12) {
198 if ((o + 12 < o) || (o + 12 < 12) || (o + 12 > buf_size)) {
199 exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
200 "ExifMnoteDataFuji", "Short MakerNote");
204 n->entries[tcount].tag = exif_get_short (buf + o, n->order);
205 n->entries[tcount].format = exif_get_short (buf + o + 2, n->order);
206 n->entries[tcount].components = exif_get_long (buf + o + 4, n->order);
207 n->entries[tcount].order = n->order;
209 exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataFuji",
210 "Loading entry 0x%x ('%s')...", n->entries[tcount].tag,
211 mnote_fuji_tag_get_name (n->entries[tcount].tag));
214 * Size? If bigger than 4 bytes, the actual data is not
215 * in the entry but somewhere else (offset).
217 s = exif_format_get_size (n->entries[tcount].format) * n->entries[tcount].components;
218 n->entries[tcount].size = s;
220 size_t dataofs = o + 8;
222 /* The data in this case is merely a pointer */
223 dataofs = exif_get_long (buf + dataofs, n->order) + 6 + n->offset;
224 if ((dataofs + s < dataofs) || (dataofs + s < s) ||
225 (dataofs + s >= buf_size)) {
226 exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
227 "ExifMnoteDataFuji", "Tag data past end of "
228 "buffer (%u >= %u)", (unsigned)(dataofs + s), buf_size);
232 n->entries[tcount].data = exif_mem_alloc (en->mem, s);
233 if (!n->entries[tcount].data) {
234 EXIF_LOG_NO_MEMORY(en->log, "ExifMnoteDataFuji", s);
237 memcpy (n->entries[tcount].data, buf + dataofs, s);
240 /* Tag was successfully parsed */
243 /* Store the count of successfully parsed tags */
248 exif_mnote_data_fuji_count (ExifMnoteData *n)
250 return n ? ((ExifMnoteDataFuji *) n)->count : 0;
254 exif_mnote_data_fuji_get_id (ExifMnoteData *d, unsigned int n)
256 ExifMnoteDataFuji *note = (ExifMnoteDataFuji *) d;
259 if (note->count <= n) return 0;
260 return note->entries[n].tag;
264 exif_mnote_data_fuji_get_name (ExifMnoteData *d, unsigned int i)
266 ExifMnoteDataFuji *n = (ExifMnoteDataFuji *) d;
269 if (i >= n->count) return NULL;
270 return mnote_fuji_tag_get_name (n->entries[i].tag);
274 exif_mnote_data_fuji_get_title (ExifMnoteData *d, unsigned int i)
276 ExifMnoteDataFuji *n = (ExifMnoteDataFuji *) d;
279 if (i >= n->count) return NULL;
280 return mnote_fuji_tag_get_title (n->entries[i].tag);
284 exif_mnote_data_fuji_get_description (ExifMnoteData *d, unsigned int i)
286 ExifMnoteDataFuji *n = (ExifMnoteDataFuji *) d;
289 if (i >= n->count) return NULL;
290 return mnote_fuji_tag_get_description (n->entries[i].tag);
294 exif_mnote_data_fuji_set_byte_order (ExifMnoteData *d, ExifByteOrder o)
296 ExifByteOrder o_orig;
297 ExifMnoteDataFuji *n = (ExifMnoteDataFuji *) d;
304 for (i = 0; i < n->count; i++) {
305 if (n->entries[i].components && (n->entries[i].size/n->entries[i].components < exif_format_get_size (n->entries[i].format)))
307 n->entries[i].order = o;
308 exif_array_set_byte_order (n->entries[i].format, n->entries[i].data,
309 n->entries[i].components, o_orig, o);
314 exif_mnote_data_fuji_set_offset (ExifMnoteData *n, unsigned int o)
316 if (n) ((ExifMnoteDataFuji *) n)->offset = o;
320 exif_mnote_data_fuji_identify (const ExifData *ed, const ExifEntry *e)
322 (void) ed; /* unused */
323 return ((e->size >= 12) && !memcmp (e->data, "FUJIFILM", 8));
327 exif_mnote_data_fuji_new (ExifMem *mem)
331 if (!mem) return NULL;
333 d = exif_mem_alloc (mem, sizeof (ExifMnoteDataFuji));
336 exif_mnote_data_construct (d, mem);
338 /* Set up function pointers */
339 d->methods.free = exif_mnote_data_fuji_free;
340 d->methods.set_byte_order = exif_mnote_data_fuji_set_byte_order;
341 d->methods.set_offset = exif_mnote_data_fuji_set_offset;
342 d->methods.load = exif_mnote_data_fuji_load;
343 d->methods.save = exif_mnote_data_fuji_save;
344 d->methods.count = exif_mnote_data_fuji_count;
345 d->methods.get_id = exif_mnote_data_fuji_get_id;
346 d->methods.get_name = exif_mnote_data_fuji_get_name;
347 d->methods.get_title = exif_mnote_data_fuji_get_title;
348 d->methods.get_description = exif_mnote_data_fuji_get_description;
349 d->methods.get_value = exif_mnote_data_fuji_get_value;