EXIF library (libexif) Internals 0.6.26
exif-mnote-data-apple.c
Go to the documentation of this file.
1/* exif-mnote-data-apple.c
2 *
3 * Copyright (c) 2018 zhanwang-sky <zhanwang_sky@163.com>
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 <config.h>
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30#include <libexif/exif-data.h>
31#include <libexif/exif-utils.h>
32
33static void
36 unsigned int i;
37
38 /*printf("%s\n", __FUNCTION__);*/
39
40 if (!d) {
41 return;
42 }
43
44 if (d->entries) {
45 for (i = 0; i < d->count; i++) {
46 if (d->entries[i].data) {
47 exif_mem_free(md->mem, d->entries[i].data);
48 }
49 }
50 exif_mem_free(md->mem, d->entries);
51 d->entries = NULL;
52 d->count = 0;
53 }
54
55 return;
56}
57
58static void
59exif_mnote_data_apple_load(ExifMnoteData *md, const unsigned char *buf, unsigned int buf_size) {
61 unsigned int tcount, i;
62 unsigned int dsize;
63 unsigned int ofs, dofs;
64
65 /*printf("%s\n", __FUNCTION__);*/
66
67 if (!d || !buf || (buf_size < 6 + 16)) {
69 "ExifMnoteDataApple", "Short MakerNote");
70 return;
71 }
72
73 /* Start of interesting data */
74 ofs = d->offset + 6;
75 if (ofs > buf_size - 16) {
77 "ExifMnoteDataApple", "Short MakerNote");
78 return;
79 }
80
81 if ((buf[ofs + 12] == 'M') && (buf[ofs + 13] == 'M')) {
83 } else if ((buf[ofs + 12] == 'I') && (buf[ofs + 13] == 'I')) {
85 } else {
87 "ExifMnoteDataApple", "Unrecognized byte order");
88 return;
89 }
90
91 tcount = (unsigned int) exif_get_short(buf + ofs + 14, d->order);
92
93 /* Sanity check the offset */
94 if (buf_size < d->offset + 6 + 16 + tcount * 12 + 4) {
96 "ExifMnoteDataApple", "Short MakerNote");
97 return;
98 }
99
100 ofs += 16;
101
103
104 /* Reserve enough space for all the possible MakerNote tags */
105 d->entries = exif_mem_alloc(md->mem, sizeof(MnoteAppleEntry) * tcount);
106 if (!d->entries) {
107 EXIF_LOG_NO_MEMORY(md->log, "ExifMnoteApple", sizeof(MnoteAppleEntry) * tcount);
108 return;
109 }
110 memset(d->entries, 0, sizeof(MnoteAppleEntry) * tcount);
111
112 for (i = 0; i < tcount; i++) {
113 if (ofs + 12 > buf_size) {
115 "ExifMnoteApplet", "Tag size overflow detected (%u vs size %u)", ofs + 12, buf_size);
116 break;
117 }
118 d->entries[i].tag = exif_get_short(buf + ofs, d->order);
119 d->entries[i].format = exif_get_short(buf + ofs + 2, d->order);
120 d->entries[i].components = exif_get_long(buf + ofs + 4, d->order);
121 d->entries[i].order = d->order;
122 if ((d->entries[i].components) && (buf_size / d->entries[i].components < exif_format_get_size(d->entries[i].format))) {
124 "ExifMnoteApplet", "Tag size overflow detected (components %lu vs size %u)", d->entries[i].components, buf_size);
125 break;
126 }
128 if ((dsize > 65536) || (dsize > buf_size)) {
129 /* Corrupt data: EXIF data size is limited to the
130 * maximum size of a JPEG segment (64 kb).
131 */
132 break;
133 }
134 if (dsize > 4) {
135 dofs = d->offset + exif_get_long(buf + ofs + 8, d->order);
136 } else {
137 dofs = ofs + 8;
138 }
139 if (dofs > buf_size) {
141 "ExifMnoteApplet", "Tag size overflow detected (%u vs size %u)", dofs, buf_size);
142 continue;
143 }
144 ofs += 12;
145 d->entries[i].data = exif_mem_alloc(md->mem, dsize);
146 if (!d->entries[i].data) {
147 EXIF_LOG_NO_MEMORY(md->log, "ExifMnoteApple", dsize);
148 continue;
149 }
150 if (dofs + dsize > buf_size) {
152 "ExifMnoteApplet", "Tag size overflow detected (%u vs size %u)", dofs + dsize, buf_size);
153 continue;
154 }
155 memcpy(d->entries[i].data, buf + dofs, dsize);
156 d->entries[i].size = dsize;
157 }
158 d->count = tcount;
159
160 return;
161}
162
163static void
165 /*printf("%s\n", __FUNCTION__);*/
166
167 if (md) {
168 ((ExifMnoteDataApple *) md)->offset = o;
169 }
170
171 return;
172}
173
174static void
177 unsigned int i;
178
179 /*printf("%s\n", __FUNCTION__);*/
180
181 if (!d || d->order == o) {
182 return;
183 }
184
185 for (i = 0; i < d->count; i++) {
187 continue;
189 d->entries[i].components, d->entries[i].order, o);
190 d->entries[i].order = o;
191 }
192 d->order = o;
193
194 return;
195}
196
197static unsigned int
199 /*printf("%s\n", __FUNCTION__);*/
200
201 return md ? ((ExifMnoteDataApple *) md)->count : 0;
202}
203
204static unsigned int
207
208 if (!d || (d->count <= i)) {
209 return 0;
210 }
211
212 return d->entries[i].tag;
213}
214
215static const char *
218
219 if (!d || (d->count <= i)) {
220 return NULL;
221 }
222
224}
225
226static const char *
229
230 if (!d || (d->count <= i)) {
231 return NULL;
232 }
233
235}
236
237static const char *
240
241 if (!d || (d->count <= i)) {
242 return NULL;
243 }
244
246}
247
248static char *
249exif_mnote_data_apple_get_value(ExifMnoteData *md, unsigned int i, char *val, unsigned int maxlen) {
251
252 if (!val || !d || (d->count <= i)) {
253 return NULL;
254 }
255
256 return mnote_apple_entry_get_value(&d->entries[i], val, maxlen);
257}
258
259int
261 (void) ed;
262
263 if (e->size < strlen("Apple iOS")+1)
264 return 0;
265
266 return !memcmp((const char *) e->data, "Apple iOS", strlen("Apple iOS"));
267}
268
271 ExifMnoteData *md;
272
273 /*printf("%s\n", __FUNCTION__);*/
274
275 if (!mem) {
276 return NULL;
277 }
278
279 md = exif_mem_alloc(mem, sizeof(ExifMnoteDataApple));
280 if (!md) {
281 return NULL;
282 }
283
285
296
297 return md;
298}
ExifByteOrder
Which byte order to use.
@ EXIF_BYTE_ORDER_INTEL
Little-endian byte order.
@ EXIF_BYTE_ORDER_MOTOROLA
Big-endian byte order.
Defines the ExifData type and the associated functions.
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
#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
static const char * exif_mnote_data_apple_get_name(ExifMnoteData *md, unsigned int i)
static char * exif_mnote_data_apple_get_value(ExifMnoteData *md, unsigned int i, char *val, unsigned int maxlen)
static void exif_mnote_data_apple_set_offset(ExifMnoteData *md, unsigned int o)
int exif_mnote_data_apple_identify(const ExifData *ed, const ExifEntry *e)
static void exif_mnote_data_apple_load(ExifMnoteData *md, const unsigned char *buf, unsigned int buf_size)
static const char * exif_mnote_data_apple_get_description(ExifMnoteData *md, unsigned int i)
static void exif_mnote_data_apple_free(ExifMnoteData *md)
static unsigned int exif_mnote_data_apple_get_id(ExifMnoteData *md, unsigned int i)
static unsigned int exif_mnote_data_apple_count(ExifMnoteData *md)
ExifMnoteData * exif_mnote_data_apple_new(ExifMem *mem)
static const char * exif_mnote_data_apple_get_title(ExifMnoteData *md, unsigned int i)
static void exif_mnote_data_apple_set_byte_order(ExifMnoteData *md, ExifByteOrder o)
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
EXIF data manipulation functions and types.
char * mnote_apple_entry_get_value(MnoteAppleEntry *entry, char *v, unsigned int maxlen)
const char * mnote_apple_tag_get_name(MnoteAppleTag t)
const char * mnote_apple_tag_get_description(MnoteAppleTag t)
const char * mnote_apple_tag_get_title(MnoteAppleTag 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
MnoteAppleEntry * 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)
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
unsigned long components
ExifByteOrder order

libexif Generated by doxygen