EXIF library (libexif) Internals  0.6.24
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 
21 #include <config.h>
22 #include "exif-mnote-data-apple.h"
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include <libexif/exif-data.h>
29 #include <libexif/exif-utils.h>
30 
31 static void
34  unsigned int i;
35 
36  /*printf("%s\n", __FUNCTION__);*/
37 
38  if (!d) {
39  return;
40  }
41 
42  if (d->entries) {
43  for (i = 0; i < d->count; i++) {
44  if (d->entries[i].data) {
45  exif_mem_free(md->mem, d->entries[i].data);
46  }
47  }
48  exif_mem_free(md->mem, d->entries);
49  d->entries = NULL;
50  d->count = 0;
51  }
52 
53  return;
54 }
55 
56 static void
57 exif_mnote_data_apple_load(ExifMnoteData *md, const unsigned char *buf, unsigned int buf_size) {
59  unsigned int tcount, i;
60  unsigned int dsize;
61  unsigned int ofs, dofs;
62 
63  /*printf("%s\n", __FUNCTION__);*/
64 
65  if (!d || !buf || (buf_size < 6 + 16)) {
67  "ExifMnoteDataApple", "Short MakerNote");
68  return;
69  }
70 
71  /* Start of interesting data */
72  ofs = d->offset + 6;
73  if (ofs > buf_size - 16) {
75  "ExifMnoteDataApple", "Short MakerNote");
76  return;
77  }
78 
79  if ((buf[ofs + 12] == 'M') && (buf[ofs + 13] == 'M')) {
81  } else if ((buf[ofs + 12] == 'I') && (buf[ofs + 13] == 'I')) {
83  } else {
85  "ExifMnoteDataApple", "Unrecognized byte order");
86  /*printf("%s(%d)\n", __FUNCTION__, __LINE__);*/
87  return;
88  }
89 
90  tcount = (unsigned int) exif_get_short(buf + ofs + 14, d->order);
91 
92  /* Sanity check the offset */
93  if (buf_size < d->offset + 6 + 16 + tcount * 12 + 4) {
95  "ExifMnoteDataApple", "Short MakerNote");
96  /*printf("%s(%d)\n", __FUNCTION__, __LINE__);*/
97  return;
98  }
99 
100  /* printf("%s(%d): total %d tags\n", __FUNCTION__, __LINE__, tcount); */
101 
102  ofs += 16;
103 
105 
106  /* Reserve enough space for all the possible MakerNote tags */
107  d->entries = exif_mem_alloc(md->mem, sizeof(MnoteAppleEntry) * tcount);
108  if (!d->entries) {
109  EXIF_LOG_NO_MEMORY(md->log, "ExifMnoteApple", sizeof(MnoteAppleEntry) * tcount);
110  /*printf("%s(%d)\n", __FUNCTION__, __LINE__);*/
111  return;
112  }
113  memset(d->entries, 0, sizeof(MnoteAppleEntry) * tcount);
114 
115  for (i = 0; i < tcount; i++) {
116  if (ofs + 12 > buf_size) {
118  "ExifMnoteApplet", "Tag size overflow detected (%u vs size %u)", ofs + 12, buf_size);
119  break;
120  }
121  d->entries[i].tag = exif_get_short(buf + ofs, d->order);
122  d->entries[i].format = exif_get_short(buf + ofs + 2, d->order);
123  d->entries[i].components = exif_get_long(buf + ofs + 4, d->order);
124  d->entries[i].order = d->order;
125  if ((d->entries[i].components) && (buf_size / d->entries[i].components < exif_format_get_size(d->entries[i].format))) {
127  "ExifMnoteApplet", "Tag size overflow detected (components %lu vs size %u)", d->entries[i].components, buf_size);
128  break;
129  }
130  dsize = exif_format_get_size(d->entries[i].format) * d->entries[i].components;
131  if ((dsize > 65536) || (dsize > buf_size)) {
132  /* Corrupt data: EXIF data size is limited to the
133  * maximum size of a JPEG segment (64 kb).
134  */
135  break;
136  }
137  if (dsize > 4) {
138  dofs = d->offset + exif_get_long(buf + ofs + 8, d->order);
139  } else {
140  dofs = ofs + 8;
141  }
142  if (dofs > buf_size) {
144  "ExifMnoteApplet", "Tag size overflow detected (%u vs size %u)", dofs, buf_size);
145  continue;
146  }
147  ofs += 12;
148  d->entries[i].data = exif_mem_alloc(md->mem, dsize);
149  if (!d->entries[i].data) {
150  EXIF_LOG_NO_MEMORY(md->log, "ExifMnoteApple", dsize);
151  continue;
152  }
153  if (dofs + dsize > buf_size) {
155  "ExifMnoteApplet", "Tag size overflow detected (%u vs size %u)", dofs + dsize, buf_size);
156  continue;
157  }
158  memcpy(d->entries[i].data, buf + dofs, dsize);
159  d->entries[i].size = dsize;
160  }
161  d->count = tcount;
162 
163  return;
164 }
165 
166 static void
168  /*printf("%s\n", __FUNCTION__);*/
169 
170  if (md) {
171  ((ExifMnoteDataApple *) md)->offset = o;
172  }
173 
174  return;
175 }
176 
177 static void
180  unsigned int i;
181 
182  /*printf("%s\n", __FUNCTION__);*/
183 
184  if (!d || d->order == o) {
185  return;
186  }
187 
188  for (i = 0; i < d->count; i++) {
189  if (d->entries[i].components && (d->entries[i].size/d->entries[i].components < exif_format_get_size (d->entries[i].format)))
190  continue;
192  d->entries[i].components, d->entries[i].order, o);
193  d->entries[i].order = o;
194  }
195  d->order = o;
196 
197  return;
198 }
199 
200 static unsigned int
202  /*printf("%s\n", __FUNCTION__);*/
203 
204  return md ? ((ExifMnoteDataApple *) md)->count : 0;
205 }
206 
207 static unsigned int
210 
211  if (!d || (d->count <= i)) {
212  return 0;
213  }
214 
215  return d->entries[i].tag;
216 }
217 
218 static const char *
221 
222  if (!d || (d->count <= i)) {
223  return NULL;
224  }
225 
226  return mnote_apple_tag_get_name(d->entries[i].tag);
227 }
228 
229 static const char *
232 
233  if (!d || (d->count <= i)) {
234  return NULL;
235  }
236 
237  return mnote_apple_tag_get_title(d->entries[i].tag);
238 }
239 
240 static const char *
243 
244  if (!d || (d->count <= i)) {
245  return NULL;
246  }
247 
249 }
250 
251 static char *
252 exif_mnote_data_apple_get_value(ExifMnoteData *md, unsigned int i, char *val, unsigned int maxlen) {
254 
255  if (!val || !d || (d->count <= i)) {
256  return NULL;
257  }
258 
259  return mnote_apple_entry_get_value(&d->entries[i], val, maxlen);
260 }
261 
262 int
264  (void) ed;
265 
266  if (e->size < strlen("Apple iOS")+1)
267  return 0;
268 
269  return !memcmp((const char *) e->data, "Apple iOS", strlen("Apple iOS"));
270 }
271 
274  ExifMnoteData *md;
275 
276  /*printf("%s\n", __FUNCTION__);*/
277 
278  if (!mem) {
279  return NULL;
280  }
281 
282  md = exif_mem_alloc(mem, sizeof(ExifMnoteDataApple));
283  if (!md) {
284  return NULL;
285  }
286 
287  exif_mnote_data_construct(md, mem);
288 
299 
300  return md;
301 }
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:66
void exif_log(ExifLog *log, ExifLogCode code, const char *domain, const char *format,...)
Definition: exif-log.c:135
@ EXIF_LOG_CODE_CORRUPT_DATA
Definition: exif-log.h:58
#define EXIF_LOG_NO_MEMORY(l, d, s)
Definition: exif-log.h:110
void exif_mem_free(ExifMem *mem, void *d)
Definition: exif-mem.c:69
void * exif_mem_alloc(ExifMem *mem, ExifLong ds)
Definition: exif-mem.c:79
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 char * exif_mnote_data_apple_get_value(ExifMnoteData *md, unsigned int i, char *val, unsigned int maxlen)
static const char * exif_mnote_data_apple_get_name(ExifMnoteData *md, unsigned int i)
static const char * exif_mnote_data_apple_get_title(ExifMnoteData *md, unsigned int i)
static void exif_mnote_data_apple_free(ExifMnoteData *md)
ExifMnoteData * exif_mnote_data_apple_new(ExifMem *mem)
static unsigned int exif_mnote_data_apple_get_id(ExifMnoteData *md, unsigned int i)
static unsigned int exif_mnote_data_apple_count(ExifMnoteData *md)
static const char * exif_mnote_data_apple_get_description(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:165
ExifShort exif_get_short(const unsigned char *buf, ExifByteOrder order)
Retrieve an ExifShort value from memory.
Definition: exif-utils.c:102
void exif_array_set_byte_order(ExifFormat f, unsigned char *b, unsigned int n, ExifByteOrder o_orig, ExifByteOrder o_new)
Definition: exif-utils.c:26
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_title(MnoteAppleTag t)
const char * mnote_apple_tag_get_description(MnoteAppleTag t)
const char * mnote_apple_tag_get_name(MnoteAppleTag t)
Represents the entire EXIF data found in an image.
Definition: exif-data.h:47
Data found in one EXIF tag.
Definition: exif-entry.h:43
unsigned char * data
Pointer to the raw EXIF data for this entry.
Definition: exif-entry.h:57
unsigned int size
Number of bytes in the buffer at data.
Definition: exif-entry.h:61
MnoteAppleEntry * entries
const char *(* get_description)(ExifMnoteData *, unsigned int)
void(* load)(ExifMnoteData *, const unsigned char *, unsigned int)
const char *(* get_name)(ExifMnoteData *, unsigned int)
const char *(* get_title)(ExifMnoteData *, 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)
void(* set_byte_order)(ExifMnoteData *, ExifByteOrder)
void(* free)(ExifMnoteData *)
ExifMnoteDataMethods methods
MnoteAppleTag tag
unsigned char * data
unsigned long components
ExifByteOrder order

libexif Generated by doxygen