1 /* exif-mnote-data-fuji.c
2 *
3 * Copyright (c) 2002 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 <stdlib.h>
22 #include <string.h>
23
24
25 #include <config.h>
26 #include <libexif/exif-byte-order.h>
27 #include <libexif/exif-utils.h>
28
29 #include "exif-mnote-data-fuji.h"
30
31 struct _MNoteFujiDataPrivate {
32 ExifByteOrder order;
33 };
34
35 static void
exif_mnote_data_fuji_clear(ExifMnoteDataFuji * n)36 exif_mnote_data_fuji_clear (ExifMnoteDataFuji *n)
37 {
38 ExifMnoteData *d = (ExifMnoteData *) n;
39 unsigned int i;
40
41 if (!n) return;
42
43 if (n->entries) {
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;
48 }
49 exif_mem_free (d->mem, n->entries);
50 n->entries = NULL;
51 n->count = 0;
52 }
53 }
54
55 static void
exif_mnote_data_fuji_free(ExifMnoteData * n)56 exif_mnote_data_fuji_free (ExifMnoteData *n)
57 {
58 if (!n) return;
59
60 exif_mnote_data_fuji_clear ((ExifMnoteDataFuji *) n);
61 }
62
63 static char *
exif_mnote_data_fuji_get_value(ExifMnoteData * d,unsigned int i,char * val,unsigned int maxlen)64 exif_mnote_data_fuji_get_value (ExifMnoteData *d, unsigned int i, char *val, unsigned int maxlen)
65 {
66 ExifMnoteDataFuji *n = (ExifMnoteDataFuji *) d;
67
68 if (!d || !val) return NULL;
69 if (i > n->count -1) return NULL;
70 /*
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));
74 */
75 return mnote_fuji_entry_get_value (&n->entries[i], val, maxlen);
76 }
77
78 static void
exif_mnote_data_fuji_save(ExifMnoteData * ne,unsigned char ** buf,unsigned int * buf_size)79 exif_mnote_data_fuji_save (ExifMnoteData *ne, unsigned char **buf,
80 unsigned int *buf_size)
81 {
82 ExifMnoteDataFuji *n = (ExifMnoteDataFuji *) ne;
83 size_t i, o, s, doff;
84 unsigned char *t;
85 size_t ts;
86
87 if (!n || !buf || !buf_size) return;
88
89 /*
90 * Allocate enough memory for all entries and the number
91 * of entries.
92 */
93 *buf_size = 8 + 4 + 2 + n->count * 12 + 4;
94 *buf = exif_mem_alloc (ne->mem, *buf_size);
95 if (!*buf) {
96 *buf_size = 0;
97 return;
98 }
99
100 /*
101 * Header: "FUJIFILM" and 4 bytes offset to the first entry.
102 * As the first entry will start right thereafter, the offset is 12.
103 */
104 memcpy (*buf, "FUJIFILM", 8);
105 exif_set_long (*buf + 8, n->order, 12);
106
107 /* Save the number of entries */
108 exif_set_short (*buf + 8 + 4, n->order, (ExifShort) n->count);
109
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);
116 o += 8;
117 s = exif_format_get_size (n->entries[i].format) *
118 n->entries[i].components;
119 if (s > 65536) {
120 /* Corrupt data: EXIF data size is limited to the
121 * maximum size of a JPEG segment (64 kb).
122 */
123 continue;
124 }
125 if (s > 4) {
126 ts = *buf_size + s;
127
128 /* Ensure even offsets. Set padding bytes to 0. */
129 if (s & 1) ts += 1;
130 t = exif_mem_realloc (ne->mem, *buf, ts);
131 if (!t) {
132 return;
133 }
134 *buf = t;
135 *buf_size = 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);
139 } else
140 doff = o;
141
142 /*
143 * Write the data. Fill unneeded bytes with 0. Do not
144 * crash if data is NULL.
145 */
146 if (!n->entries[i].data) memset (*buf + doff, 0, s);
147 else memcpy (*buf + doff, n->entries[i].data, s);
148 }
149 }
150
151 static void
exif_mnote_data_fuji_load(ExifMnoteData * en,const unsigned char * buf,unsigned int buf_size)152 exif_mnote_data_fuji_load (ExifMnoteData *en,
153 const unsigned char *buf, unsigned int buf_size)
154 {
155 ExifMnoteDataFuji *n = (ExifMnoteDataFuji*) en;
156 ExifLong c;
157 size_t i, tcount, o, datao;
158
159 if (!n || !buf || !buf_size) {
160 exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA,
161 "ExifMnoteDataFuji", "Short MakerNote");
162 return;
163 }
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");
168 return;
169 }
170
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");
177 return;
178 }
179
180 /* Read the number of tags */
181 c = exif_get_short (buf + datao, EXIF_BYTE_ORDER_INTEL);
182 datao += 2;
183
184 /* Remove any old entries */
185 exif_mnote_data_fuji_clear (n);
186
187 /* Reserve enough space for all the possible MakerNote tags */
188 n->entries = exif_mem_alloc (en->mem, sizeof (MnoteFujiEntry) * c);
189 if (!n->entries) {
190 EXIF_LOG_NO_MEMORY(en->log, "ExifMnoteDataFuji", sizeof (MnoteFujiEntry) * c);
191 return;
192 }
193
194 /* Parse all c entries, storing ones that are successfully parsed */
195 tcount = 0;
196 for (i = c, o = datao; i; --i, o += 12) {
197 size_t s;
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");
201 break;
202 }
203
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;
208
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));
212
213 /*
214 * Size? If bigger than 4 bytes, the actual data is not
215 * in the entry but somewhere else (offset).
216 */
217 s = exif_format_get_size (n->entries[tcount].format) * n->entries[tcount].components;
218 n->entries[tcount].size = s;
219 if (s) {
220 size_t dataofs = o + 8;
221 if (s > 4)
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 (%zu >= %u)", dataofs + s, buf_size);
229 continue;
230 }
231
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);
235 continue;
236 }
237 memcpy (n->entries[tcount].data, buf + dataofs, s);
238 }
239
240 /* Tag was successfully parsed */
241 ++tcount;
242 }
243 /* Store the count of successfully parsed tags */
244 n->count = tcount;
245 }
246
247 static unsigned int
exif_mnote_data_fuji_count(ExifMnoteData * n)248 exif_mnote_data_fuji_count (ExifMnoteData *n)
249 {
250 return n ? ((ExifMnoteDataFuji *) n)->count : 0;
251 }
252
253 static unsigned int
exif_mnote_data_fuji_get_id(ExifMnoteData * d,unsigned int n)254 exif_mnote_data_fuji_get_id (ExifMnoteData *d, unsigned int n)
255 {
256 ExifMnoteDataFuji *note = (ExifMnoteDataFuji *) d;
257
258 if (!note) return 0;
259 if (note->count <= n) return 0;
260 return note->entries[n].tag;
261 }
262
263 static const char *
exif_mnote_data_fuji_get_name(ExifMnoteData * d,unsigned int i)264 exif_mnote_data_fuji_get_name (ExifMnoteData *d, unsigned int i)
265 {
266 ExifMnoteDataFuji *n = (ExifMnoteDataFuji *) d;
267
268 if (!n) return NULL;
269 if (i >= n->count) return NULL;
270 return mnote_fuji_tag_get_name (n->entries[i].tag);
271 }
272
273 static const char *
exif_mnote_data_fuji_get_title(ExifMnoteData * d,unsigned int i)274 exif_mnote_data_fuji_get_title (ExifMnoteData *d, unsigned int i)
275 {
276 ExifMnoteDataFuji *n = (ExifMnoteDataFuji *) d;
277
278 if (!n) return NULL;
279 if (i >= n->count) return NULL;
280 return mnote_fuji_tag_get_title (n->entries[i].tag);
281 }
282
283 static const char *
exif_mnote_data_fuji_get_description(ExifMnoteData * d,unsigned int i)284 exif_mnote_data_fuji_get_description (ExifMnoteData *d, unsigned int i)
285 {
286 ExifMnoteDataFuji *n = (ExifMnoteDataFuji *) d;
287
288 if (!n) return NULL;
289 if (i >= n->count) return NULL;
290 return mnote_fuji_tag_get_description (n->entries[i].tag);
291 }
292
293 static void
exif_mnote_data_fuji_set_byte_order(ExifMnoteData * d,ExifByteOrder o)294 exif_mnote_data_fuji_set_byte_order (ExifMnoteData *d, ExifByteOrder o)
295 {
296 ExifByteOrder o_orig;
297 ExifMnoteDataFuji *n = (ExifMnoteDataFuji *) d;
298 unsigned int i;
299
300 if (!n) return;
301
302 o_orig = n->order;
303 n->order = o;
304 for (i = 0; i < n->count; i++) {
305 n->entries[i].order = o;
306 exif_array_set_byte_order (n->entries[i].format, n->entries[i].data,
307 n->entries[i].components, o_orig, o);
308 }
309 }
310
311 static void
exif_mnote_data_fuji_set_offset(ExifMnoteData * n,unsigned int o)312 exif_mnote_data_fuji_set_offset (ExifMnoteData *n, unsigned int o)
313 {
314 if (n) ((ExifMnoteDataFuji *) n)->offset = o;
315 }
316
317 int
exif_mnote_data_fuji_identify(const ExifData * ed,const ExifEntry * e)318 exif_mnote_data_fuji_identify (const ExifData *ed, const ExifEntry *e)
319 {
320 return ((e->size >= 12) && !memcmp (e->data, "FUJIFILM", 8));
321 }
322
323 ExifMnoteData *
exif_mnote_data_fuji_new(ExifMem * mem)324 exif_mnote_data_fuji_new (ExifMem *mem)
325 {
326 ExifMnoteData *d;
327
328 if (!mem) return NULL;
329
330 d = exif_mem_alloc (mem, sizeof (ExifMnoteDataFuji));
331 if (!d) return NULL;
332
333 exif_mnote_data_construct (d, mem);
334
335 /* Set up function pointers */
336 d->methods.free = exif_mnote_data_fuji_free;
337 d->methods.set_byte_order = exif_mnote_data_fuji_set_byte_order;
338 d->methods.set_offset = exif_mnote_data_fuji_set_offset;
339 d->methods.load = exif_mnote_data_fuji_load;
340 d->methods.save = exif_mnote_data_fuji_save;
341 d->methods.count = exif_mnote_data_fuji_count;
342 d->methods.get_id = exif_mnote_data_fuji_get_id;
343 d->methods.get_name = exif_mnote_data_fuji_get_name;
344 d->methods.get_title = exif_mnote_data_fuji_get_title;
345 d->methods.get_description = exif_mnote_data_fuji_get_description;
346 d->methods.get_value = exif_mnote_data_fuji_get_value;
347
348 return d;
349 }
350