b8a21e16b5dda5dc2828860ec6d659dac6f80f8d
[platform/upstream/libexif.git] / libexif / canon / exif-mnote-data-canon.c
1 /* exif-mnote-data-canon.c
2  *
3  * Copyright (c) 2002, 2003 Lutz Mueller <lutz@users.sourceforge.net>
4  * Copyright (c) 2003 Matthieu Castet <mat-c@users.sourceforge.net>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA  02110-1301  USA.
20  */
21
22 #include <config.h>
23 #include "exif-mnote-data-canon.h"
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include <libexif/exif-byte-order.h>
30 #include <libexif/exif-utils.h>
31 #include <libexif/exif-data.h>
32
33 #define CHECKOVERFLOW(offset,datasize,structsize) (( offset >= datasize) || (structsize > datasize) || (offset > datasize - structsize ))
34
35 static void
36 exif_mnote_data_canon_clear (ExifMnoteDataCanon *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
56 exif_mnote_data_canon_free (ExifMnoteData *n)
57 {
58         if (!n) return;
59
60         exif_mnote_data_canon_clear ((ExifMnoteDataCanon *) n);
61 }
62
63 static void
64 exif_mnote_data_canon_get_tags (ExifMnoteDataCanon *dc, unsigned int n,
65                 unsigned int *m, unsigned int *s)
66 {
67         unsigned int from = 0, to;
68
69         if (!dc || !m) return;
70         for (*m = 0; *m < dc->count; (*m)++) {
71                 to = from + mnote_canon_entry_count_values (&dc->entries[*m]);
72                 if (to > n) {
73                         if (s) *s = n - from;
74                         break;
75                 }
76                 from = to;
77         }
78 }
79
80 static char *
81 exif_mnote_data_canon_get_value (ExifMnoteData *note, unsigned int n, char *val, unsigned int maxlen)
82 {
83         ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) note;
84         unsigned int m, s;
85
86         if (!dc) return NULL;
87         exif_mnote_data_canon_get_tags (dc, n, &m, &s);
88         if (m >= dc->count) return NULL;
89         return mnote_canon_entry_get_value (&dc->entries[m], s, val, maxlen);
90 }
91
92 static void
93 exif_mnote_data_canon_set_byte_order (ExifMnoteData *d, ExifByteOrder o)
94 {
95         ExifByteOrder o_orig;
96         ExifMnoteDataCanon *n = (ExifMnoteDataCanon *) d;
97         unsigned int i;
98
99         if (!n) return;
100
101         o_orig = n->order;
102         n->order = o;
103         for (i = 0; i < n->count; i++) {
104                 if (n->entries[i].components && (n->entries[i].size/n->entries[i].components < exif_format_get_size (n->entries[i].format)))
105                         continue;
106                 n->entries[i].order = o;
107                 exif_array_set_byte_order (n->entries[i].format, n->entries[i].data,
108                                 n->entries[i].components, o_orig, o);
109         }
110 }
111
112 static void
113 exif_mnote_data_canon_set_offset (ExifMnoteData *n, unsigned int o)
114 {
115         if (n) ((ExifMnoteDataCanon *) n)->offset = o;
116 }
117
118 static void
119 exif_mnote_data_canon_save (ExifMnoteData *ne, 
120         unsigned char **buf, unsigned int *buf_size)
121 {
122         ExifMnoteDataCanon *n = (ExifMnoteDataCanon *) ne;
123         size_t i, o, s, doff;
124         unsigned char *t;
125         size_t ts;
126
127         if (!n || !buf || !buf_size) return;
128
129         /*
130          * Allocate enough memory for all entries and the number
131          * of entries.
132          */
133         *buf_size = 2 + n->count * 12 + 4;
134         *buf = exif_mem_alloc (ne->mem, sizeof (char) * *buf_size);
135         if (!*buf) {
136                 EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteCanon", *buf_size);
137                 return;
138         }
139
140         /* Save the number of entries */
141         exif_set_short (*buf, n->order, (ExifShort) n->count);
142         
143         /* Save each entry */
144         for (i = 0; i < n->count; i++) {
145                 o = 2 + i * 12;
146                 exif_set_short (*buf + o + 0, n->order, (ExifShort) n->entries[i].tag);
147                 exif_set_short (*buf + o + 2, n->order, (ExifShort) n->entries[i].format);
148                 exif_set_long  (*buf + o + 4, n->order,
149                                 n->entries[i].components);
150                 o += 8;
151                 s = exif_format_get_size (n->entries[i].format) *
152                                                 n->entries[i].components;
153                 if (s > 65536) {
154                         /* Corrupt data: EXIF data size is limited to the
155                          * maximum size of a JPEG segment (64 kb).
156                          */
157                         continue;
158                 }
159                 if (s > 4) {
160                         ts = *buf_size + s;
161
162                         /* Ensure even offsets. Set padding bytes to 0. */
163                         if (s & 1) ts += 1;
164                         t = exif_mem_realloc (ne->mem, *buf,
165                                                  sizeof (char) * ts);
166                         if (!t) {
167                                 EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteCanon", ts);
168                                 return;
169                         }
170                         *buf = t;
171                         *buf_size = ts;
172                         doff = *buf_size - s;
173                         if (s & 1) { doff--; *(*buf + *buf_size - 1) = '\0'; }
174                         exif_set_long (*buf + o, n->order, n->offset + doff);
175                 } else
176                         doff = o;
177
178                 /*
179                  * Write the data. Fill unneeded bytes with 0. Do not
180                  * crash if data is NULL.
181                  */
182                 if (!n->entries[i].data) memset (*buf + doff, 0, s);
183                 else memcpy (*buf + doff, n->entries[i].data, s);
184                 if (s < 4) memset (*buf + doff + s, 0, (4 - s));
185         }
186 }
187
188 /* XXX
189  * FIXME: exif_mnote_data_canon_load() may fail and there is no
190  *        semantics to express that.
191  *        See bug #1054323 for details, especially the comment by liblit
192  *        after it has supposedly been fixed:
193  *
194  *        https://sourceforge.net/tracker/?func=detail&aid=1054323&group_id=12272&atid=112272
195  *        Unfortunately, the "return" statements aren't commented at
196  *        all, so it isn't trivial to find out what is a normal
197  *        return, and what is a reaction to an error condition.
198  */
199
200 static void
201 exif_mnote_data_canon_load (ExifMnoteData *ne,
202         const unsigned char *buf, unsigned int buf_size)
203 {
204         ExifMnoteDataCanon *n = (ExifMnoteDataCanon *) ne;
205         ExifShort c;
206         size_t i, tcount, o, datao;
207
208         if (!n || !buf || !buf_size) {
209                 exif_log (ne->log, EXIF_LOG_CODE_CORRUPT_DATA,
210                           "ExifMnoteCanon", "Short MakerNote");
211                 return;
212         }
213         datao = 6 + n->offset;
214         if (CHECKOVERFLOW(datao, buf_size, 2)) {
215                 exif_log (ne->log, EXIF_LOG_CODE_CORRUPT_DATA,
216                           "ExifMnoteCanon", "Short MakerNote");
217                 return;
218         }
219
220         /* Read the number of tags */
221         c = exif_get_short (buf + datao, n->order);
222         datao += 2;
223
224         /* Remove any old entries */
225         exif_mnote_data_canon_clear (n);
226
227         /* Reserve enough space for all the possible MakerNote tags */
228         n->entries = exif_mem_alloc (ne->mem, sizeof (MnoteCanonEntry) * c);
229         if (!n->entries) {
230                 EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteCanon", sizeof (MnoteCanonEntry) * c);
231                 return;
232         }
233
234         /* Parse the entries */
235         tcount = 0;
236         for (i = c, o = datao; i; --i, o += 12) {
237                 size_t s;
238
239                 memset(&n->entries[tcount], 0, sizeof(MnoteCanonEntry));
240                 if (CHECKOVERFLOW(o,buf_size,12)) {
241                         exif_log (ne->log, EXIF_LOG_CODE_CORRUPT_DATA,
242                                 "ExifMnoteCanon", "Short MakerNote");
243                         break;
244                 }
245
246                 n->entries[tcount].tag        = exif_get_short (buf + o, n->order);
247                 n->entries[tcount].format     = exif_get_short (buf + o + 2, n->order);
248                 n->entries[tcount].components = exif_get_long (buf + o + 4, n->order);
249                 n->entries[tcount].order      = n->order;
250
251                 exif_log (ne->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteCanon",
252                         "Loading entry 0x%x ('%s')...", n->entries[tcount].tag,
253                          mnote_canon_tag_get_name (n->entries[tcount].tag));
254
255                 /* Check if we overflow the multiplication. Use buf_size as the max size for integer overflow detection,
256                  * we will check the buffer sizes closer later. */
257                 if (    exif_format_get_size (n->entries[tcount].format) &&
258                         buf_size / exif_format_get_size (n->entries[tcount].format) < n->entries[tcount].components
259                 ) {
260                         exif_log (ne->log, EXIF_LOG_CODE_CORRUPT_DATA,
261                                   "ExifMnoteCanon", "Tag size overflow detected (%u * %lu)", exif_format_get_size (n->entries[tcount].format), n->entries[tcount].components);
262                         continue;
263                 }
264
265                 /*
266                  * Size? If bigger than 4 bytes, the actual data is not
267                  * in the entry but somewhere else (offset).
268                  */
269                 s = exif_format_get_size (n->entries[tcount].format) * 
270                                                                   n->entries[tcount].components;
271                 n->entries[tcount].size = s;
272                 if (!s) {
273                         exif_log (ne->log, EXIF_LOG_CODE_CORRUPT_DATA,
274                                   "ExifMnoteCanon",
275                                   "Invalid zero-length tag size");
276                         continue;
277
278                 } else {
279                         size_t dataofs = o + 8;
280                         if (s > 4) dataofs = exif_get_long (buf + dataofs, n->order) + 6;
281
282                         if (CHECKOVERFLOW(dataofs, buf_size, s)) {
283                                 exif_log (ne->log, EXIF_LOG_CODE_DEBUG,
284                                         "ExifMnoteCanon",
285                                         "Tag data past end of buffer (%u > %u)",
286                                         (unsigned)(dataofs + s), buf_size);
287                                 continue;
288                         }
289
290                         n->entries[tcount].data = exif_mem_alloc (ne->mem, s);
291                         if (!n->entries[tcount].data) {
292                                 EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteCanon", s);
293                                 continue;
294                         }
295                         memcpy (n->entries[tcount].data, buf + dataofs, s);
296                 }
297
298                 /* Tag was successfully parsed */
299                 ++tcount;
300         }
301         /* Store the count of successfully parsed tags */
302         n->count = tcount;
303 }
304
305 static unsigned int
306 exif_mnote_data_canon_count (ExifMnoteData *n)
307 {
308         ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) n;
309         unsigned int i, c;
310
311         for (i = c = 0; dc && (i < dc->count); i++)
312                 c += mnote_canon_entry_count_values (&dc->entries[i]);
313         return c;
314 }
315
316 static unsigned int
317 exif_mnote_data_canon_get_id (ExifMnoteData *d, unsigned int i)
318 {
319         ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) d;
320         unsigned int m;
321
322         if (!dc) return 0;
323         exif_mnote_data_canon_get_tags (dc, i, &m, NULL);
324         if (m >= dc->count) return 0;
325         return dc->entries[m].tag;
326 }
327
328 static const char *
329 exif_mnote_data_canon_get_name (ExifMnoteData *note, unsigned int i)
330 {
331         ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) note;
332         unsigned int m, s;
333
334         if (!dc) return NULL;
335         exif_mnote_data_canon_get_tags (dc, i, &m, &s);
336         if (m >= dc->count) return NULL;
337         return mnote_canon_tag_get_name_sub (dc->entries[m].tag, s, dc->options);
338 }
339
340 static const char *
341 exif_mnote_data_canon_get_title (ExifMnoteData *note, unsigned int i)
342 {
343         ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) note;
344         unsigned int m, s;
345
346         if (!dc) return NULL;
347         exif_mnote_data_canon_get_tags (dc, i, &m, &s);
348         if (m >= dc->count) return NULL;
349         return mnote_canon_tag_get_title_sub (dc->entries[m].tag, s, dc->options);
350 }
351
352 static const char *
353 exif_mnote_data_canon_get_description (ExifMnoteData *note, unsigned int i)
354 {
355         ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) note;
356         unsigned int m;
357
358         if (!dc) return NULL;
359         exif_mnote_data_canon_get_tags (dc, i, &m, NULL);
360         if (m >= dc->count) return NULL;
361         return mnote_canon_tag_get_description (dc->entries[m].tag);
362 }
363
364 int
365 exif_mnote_data_canon_identify (const ExifData *ed, const ExifEntry *e)
366 {
367         char value[8];
368
369         (void) e;  /* unused */
370         ExifEntry *em = exif_data_get_entry (ed, EXIF_TAG_MAKE);
371         if (!em) 
372                 return 0;
373         return !strcmp (exif_entry_get_value (em, value, sizeof (value)), "Canon");
374 }
375
376 ExifMnoteData *
377 exif_mnote_data_canon_new (ExifMem *mem, ExifDataOption o)
378 {
379         ExifMnoteData *d;
380         ExifMnoteDataCanon *dc;
381
382         if (!mem) return NULL;
383
384         d = exif_mem_alloc (mem, sizeof (ExifMnoteDataCanon));
385         if (!d)
386                 return NULL;
387
388         exif_mnote_data_construct (d, mem);
389
390         /* Set up function pointers */
391         d->methods.free            = exif_mnote_data_canon_free;
392         d->methods.set_byte_order  = exif_mnote_data_canon_set_byte_order;
393         d->methods.set_offset      = exif_mnote_data_canon_set_offset;
394         d->methods.load            = exif_mnote_data_canon_load;
395         d->methods.save            = exif_mnote_data_canon_save;
396         d->methods.count           = exif_mnote_data_canon_count;
397         d->methods.get_id          = exif_mnote_data_canon_get_id;
398         d->methods.get_name        = exif_mnote_data_canon_get_name;
399         d->methods.get_title       = exif_mnote_data_canon_get_title;
400         d->methods.get_description = exif_mnote_data_canon_get_description;
401         d->methods.get_value       = exif_mnote_data_canon_get_value;
402
403         dc = (ExifMnoteDataCanon*)d;
404         dc->options = o;
405         return d;
406 }