EXIF library (libexif) Internals 0.6.26
exif-mnote-data-fuji.c
Go to the documentation of this file.
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 * SPDX-License-Identifier: LGPL-2.0-or-later
21 */
22
23#include <stdlib.h>
24#include <string.h>
25
26
27#include <config.h>
29#include <libexif/exif-utils.h>
30
32
33#define CHECKOVERFLOW(offset,datasize,structsize) (( (offset) >= (datasize)) || ((structsize) > (datasize)) || ((offset) > (datasize) - (structsize) ))
34
37};
38
39static void
41{
43 unsigned int i;
44
45 if (!n) return;
46
47 if (n->entries) {
48 for (i = 0; i < n->count; i++)
49 if (n->entries[i].data) {
50 exif_mem_free (d->mem, n->entries[i].data);
51 n->entries[i].data = NULL;
52 }
53 exif_mem_free (d->mem, n->entries);
54 n->entries = NULL;
55 n->count = 0;
56 }
57}
58
59static void
61{
62 if (!n) return;
63
65}
66
67static char *
68exif_mnote_data_fuji_get_value (ExifMnoteData *d, unsigned int i, char *val, unsigned int maxlen)
69{
71
72 if (!d || !val) return NULL;
73 if (i >= n->count) return NULL;
74/*
75 exif_log (d->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataFuji",
76 "Querying value for tag '%s'...",
77 mnote_fuji_tag_get_name (n->entries[i].tag));
78*/
79 return mnote_fuji_entry_get_value (&n->entries[i], val, maxlen);
80}
81
82static void
83exif_mnote_data_fuji_save (ExifMnoteData *ne, unsigned char **buf,
84 unsigned int *buf_size)
85{
87 size_t i, o, s, doff;
88 unsigned char *t;
89 size_t ts;
90
91 if (!n || !buf || !buf_size) return;
92
93 /*
94 * Allocate enough memory for all entries and the number
95 * of entries.
96 */
97 *buf_size = 8 + 4 + 2 + n->count * 12 + 4;
98 *buf = exif_mem_alloc (ne->mem, *buf_size);
99 if (!*buf) {
100 *buf_size = 0;
101 return;
102 }
103
104 /*
105 * Header: "FUJIFILM" and 4 bytes offset to the first entry.
106 * As the first entry will start right thereafter, the offset is 12.
107 */
108 memcpy (*buf, "FUJIFILM", 8);
109 exif_set_long (*buf + 8, n->order, 12);
110
111 /* Save the number of entries */
112 exif_set_short (*buf + 8 + 4, n->order, (ExifShort) n->count);
113
114 /* Save each entry */
115 for (i = 0; i < n->count; i++) {
116 o = 8 + 4 + 2 + i * 12;
117 exif_set_short (*buf + o + 0, n->order, (ExifShort) n->entries[i].tag);
118 exif_set_short (*buf + o + 2, n->order, (ExifShort) n->entries[i].format);
119 exif_set_long (*buf + o + 4, n->order, n->entries[i].components);
120 o += 8;
122 n->entries[i].components;
123 if (s > 65536) {
124 /* Corrupt data: EXIF data size is limited to the
125 * maximum size of a JPEG segment (64 kb).
126 */
127 continue;
128 }
129 if (s > 4) {
130 ts = *buf_size + s;
131
132 /* Ensure even offsets. Set padding bytes to 0. */
133 if (s & 1) ts += 1;
134 t = exif_mem_realloc (ne->mem, *buf, ts);
135 if (!t) {
136 return;
137 }
138 *buf = t;
139 *buf_size = ts;
140 doff = *buf_size - s;
141 if (s & 1) { doff--; *(*buf + *buf_size - 1) = '\0'; }
142 exif_set_long (*buf + o, n->order, doff);
143 } else
144 doff = o;
145
146 /*
147 * Write the data. Fill unneeded bytes with 0. Do not
148 * crash if data is NULL.
149 */
150 if (!n->entries[i].data) memset (*buf + doff, 0, s);
151 else memcpy (*buf + doff, n->entries[i].data, s);
152 }
153}
154
155static void
157 const unsigned char *buf, unsigned int buf_size)
158{
160 ExifLong c;
161 size_t i, tcount, o, datao;
162
163 if (!n) return;
164
165 if (!buf || !buf_size) {
167 "ExifMnoteDataFuji", "Short MakerNote");
168 return;
169 }
170 if (CHECKOVERFLOW(n->offset, buf_size, 6+8+4)) {
172 "ExifMnoteDataFuji", "Short MakerNote");
173 return;
174 }
175 datao = 6 + n->offset;
176
178
179 datao += exif_get_long (buf + datao + 8, EXIF_BYTE_ORDER_INTEL);
180 if (CHECKOVERFLOW(datao, buf_size, 2)) {
182 "ExifMnoteDataFuji", "Short MakerNote");
183 return;
184 }
185
186 /* Read the number of tags */
187 c = exif_get_short (buf + datao, EXIF_BYTE_ORDER_INTEL);
188 datao += 2;
189
190 /* Just use an arbitrary max tag limit here to avoid needing to much memory or time. There are 50 named tags currently.
191 * Fuji XT2 had 56 entries or so, GFX500 has 68. 150 seems a safe upper bound currently.
192 * The format allows specifying the same range of memory as often as it can, so this multiplies quickly. */
193 if (c > 150) {
194 exif_log (en->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifMnoteDataFuji", "Too much tags (%d) in Fuji MakerNote", c);
195 return;
196 }
197
198 /* Remove any old entries */
200
201 /* Reserve enough space for all the possible MakerNote tags */
202 n->entries = exif_mem_alloc (en->mem, sizeof (MnoteFujiEntry) * c);
203 if (!n->entries) {
204 EXIF_LOG_NO_MEMORY(en->log, "ExifMnoteDataFuji", sizeof (MnoteFujiEntry) * c);
205 return;
206 }
207
208 /* Parse all c entries, storing ones that are successfully parsed */
209 tcount = 0;
210 for (i = c, o = datao; i; --i, o += 12) {
211 size_t s;
212
213 memset(&n->entries[tcount], 0, sizeof(MnoteFujiEntry));
214 if (CHECKOVERFLOW(o, buf_size, 12)) {
216 "ExifMnoteDataFuji", "Short MakerNote");
217 break;
218 }
219
220 n->entries[tcount].tag = exif_get_short (buf + o, n->order);
221 n->entries[tcount].format = exif_get_short (buf + o + 2, n->order);
222 n->entries[tcount].components = exif_get_long (buf + o + 4, n->order);
223 n->entries[tcount].order = n->order;
224
225 exif_log (en->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteDataFuji",
226 "Loading entry 0x%x ('%s')...", n->entries[tcount].tag,
227 mnote_fuji_tag_get_name (n->entries[tcount].tag));
228
229 /* Check if we overflow the multiplication. Use buf_size as the max size for integer overflow detection,
230 * we will check the buffer sizes closer later. */
231 if ( exif_format_get_size (n->entries[tcount].format) &&
232 buf_size / exif_format_get_size (n->entries[tcount].format) < n->entries[tcount].components
233 ) {
235 "ExifMnoteDataFuji", "Tag size overflow detected (%u * %lu)", exif_format_get_size (n->entries[tcount].format), n->entries[tcount].components);
236 continue;
237 }
238 /*
239 * Size? If bigger than 4 bytes, the actual data is not
240 * in the entry but somewhere else (offset).
241 */
242 s = exif_format_get_size (n->entries[tcount].format) * n->entries[tcount].components;
243 n->entries[tcount].size = s;
244 if (s) {
245 size_t dataofs = o + 8;
246 if (s > 4)
247 /* The data in this case is merely a pointer */
248 dataofs = exif_get_long (buf + dataofs, n->order) + 6 + n->offset;
249
250 if (CHECKOVERFLOW(dataofs, buf_size, s)) {
252 "ExifMnoteDataFuji", "Tag data past end of "
253 "buffer (%u >= %u)", (unsigned)(dataofs + s), buf_size);
254 continue;
255 }
256
257 n->entries[tcount].data = exif_mem_alloc (en->mem, s);
258 if (!n->entries[tcount].data) {
259 EXIF_LOG_NO_MEMORY(en->log, "ExifMnoteDataFuji", s);
260 continue;
261 }
262 memcpy (n->entries[tcount].data, buf + dataofs, s);
263 }
264
265 /* Tag was successfully parsed */
266 ++tcount;
267 }
268 /* Store the count of successfully parsed tags */
269 n->count = tcount;
270}
271
272static unsigned int
274{
275 return n ? ((ExifMnoteDataFuji *) n)->count : 0;
276}
277
278static unsigned int
280{
282
283 if (!note) return 0;
284 if (note->count <= n) return 0;
285 return note->entries[n].tag;
286}
287
288static const char *
290{
292
293 if (!n) return NULL;
294 if (i >= n->count) return NULL;
295 return mnote_fuji_tag_get_name (n->entries[i].tag);
296}
297
298static const char *
300{
302
303 if (!n) return NULL;
304 if (i >= n->count) return NULL;
305 return mnote_fuji_tag_get_title (n->entries[i].tag);
306}
307
308static const char *
310{
312
313 if (!n) return NULL;
314 if (i >= n->count) return NULL;
316}
317
318static void
320{
321 ExifByteOrder o_orig;
323 unsigned int i;
324
325 if (!n) return;
326
327 o_orig = n->order;
328 n->order = o;
329 for (i = 0; i < n->count; i++) {
331 continue;
332 n->entries[i].order = o;
334 n->entries[i].components, o_orig, o);
335 }
336}
337
338static void
340{
341 if (n) ((ExifMnoteDataFuji *) n)->offset = o;
342}
343
344int
346{
347 (void) ed; /* unused */
348 return ((e->size >= 12) && !memcmp (e->data, "FUJIFILM", 8));
349}
350
353{
354 ExifMnoteData *d;
355
356 if (!mem) return NULL;
357
358 d = exif_mem_alloc (mem, sizeof (ExifMnoteDataFuji));
359 if (!d) return NULL;
360
362
363 /* Set up function pointers */
375
376 return d;
377}
Defines the ExifByteOrder enum and the associated functions.
ExifByteOrder
Which byte order to use.
@ EXIF_BYTE_ORDER_INTEL
Little-endian byte order.
unsigned char exif_format_get_size(ExifFormat format)
Return the raw size of the given EXIF data type.
Definition exif-format.c:68
void exif_log(ExifLog *log, ExifLogCode code, const char *domain, const char *format,...)
Definition exif-log.c:137
@ EXIF_LOG_CODE_CORRUPT_DATA
Definition exif-log.h:60
@ EXIF_LOG_CODE_DEBUG
Definition exif-log.h:58
#define EXIF_LOG_NO_MEMORY(l, d, s)
Definition exif-log.h:112
void exif_mem_free(ExifMem *mem, void *d)
Definition exif-mem.c:91
void * exif_mem_alloc(ExifMem *mem, ExifLong ds)
Definition exif-mem.c:101
void * exif_mem_realloc(ExifMem *mem, void *d, ExifLong ds)
Definition exif-mem.c:111
static const char * exif_mnote_data_fuji_get_title(ExifMnoteData *d, unsigned int i)
static void exif_mnote_data_fuji_load(ExifMnoteData *en, const unsigned char *buf, unsigned int buf_size)
static void exif_mnote_data_fuji_set_offset(ExifMnoteData *n, unsigned int o)
static const char * exif_mnote_data_fuji_get_description(ExifMnoteData *d, unsigned int i)
static void exif_mnote_data_fuji_save(ExifMnoteData *ne, unsigned char **buf, unsigned int *buf_size)
static unsigned int exif_mnote_data_fuji_count(ExifMnoteData *n)
ExifMnoteData * exif_mnote_data_fuji_new(ExifMem *mem)
int exif_mnote_data_fuji_identify(const ExifData *ed, const ExifEntry *e)
Detect if MakerNote is recognized as one handled by the Fuji module.
static unsigned int exif_mnote_data_fuji_get_id(ExifMnoteData *d, unsigned int n)
static const char * exif_mnote_data_fuji_get_name(ExifMnoteData *d, unsigned int i)
static void exif_mnote_data_fuji_free(ExifMnoteData *n)
#define CHECKOVERFLOW(offset, datasize, structsize)
static void exif_mnote_data_fuji_clear(ExifMnoteDataFuji *n)
static void exif_mnote_data_fuji_set_byte_order(ExifMnoteData *d, ExifByteOrder o)
static char * exif_mnote_data_fuji_get_value(ExifMnoteData *d, unsigned int i, char *val, unsigned int maxlen)
void exif_mnote_data_construct(ExifMnoteData *, ExifMem *mem)
ExifLong exif_get_long(const unsigned char *buf, ExifByteOrder order)
Retrieve an ExifLong value from memory.
Definition exif-utils.c:167
ExifShort exif_get_short(const unsigned char *buf, ExifByteOrder order)
Retrieve an ExifShort value from memory.
Definition exif-utils.c:104
void exif_array_set_byte_order(ExifFormat f, unsigned char *b, unsigned int n, ExifByteOrder o_orig, ExifByteOrder o_new)
Definition exif-utils.c:28
void exif_set_long(unsigned char *b, ExifByteOrder order, ExifLong value)
Store an ExifLong value into memory in EXIF format.
Definition exif-utils.c:173
void exif_set_short(unsigned char *b, ExifByteOrder order, ExifShort value)
Store an ExifShort value into memory in EXIF format.
Definition exif-utils.c:126
EXIF data manipulation functions and types.
uint32_t ExifLong
EXIF Unsigned Long data type.
Definition exif-utils.h:56
uint16_t ExifShort
EXIF Unsigned Short data type.
Definition exif-utils.h:50
char * mnote_fuji_entry_get_value(MnoteFujiEntry *entry, char *val, unsigned int maxlen)
const char * mnote_fuji_tag_get_name(MnoteFujiTag t)
const char * mnote_fuji_tag_get_title(MnoteFujiTag t)
const char * mnote_fuji_tag_get_description(MnoteFujiTag t)
Represents the entire EXIF data found in an image.
Definition exif-data.h:48
Data found in one EXIF tag.
Definition exif-entry.h:45
unsigned char * data
Pointer to the raw EXIF data for this entry.
Definition exif-entry.h:59
unsigned int size
Number of bytes in the buffer at data.
Definition exif-entry.h:63
MnoteFujiEntry * entries
const char *(* get_name)(ExifMnoteData *, unsigned int)
const char *(* get_description)(ExifMnoteData *, unsigned int)
void(* load)(ExifMnoteData *, const unsigned char *, unsigned int)
char *(* get_value)(ExifMnoteData *, unsigned int, char *val, unsigned int maxlen)
unsigned int(* get_id)(ExifMnoteData *, unsigned int)
void(* save)(ExifMnoteData *, unsigned char **, unsigned int *)
unsigned int(* count)(ExifMnoteData *)
void(* set_offset)(ExifMnoteData *, unsigned int)
const char *(* get_title)(ExifMnoteData *, unsigned int)
void(* set_byte_order)(ExifMnoteData *, ExifByteOrder)
void(* free)(ExifMnoteData *)
ExifMnoteDataMethods methods
unsigned char * data
ExifByteOrder order
unsigned int size
unsigned long components

libexif Generated by doxygen