Ensure the MakerNote data pointers are initialized with NULL.
[platform/upstream/libexif.git] / libexif / canon / exif-mnote-data-canon.c
index 9416433..b8a21e1 100644 (file)
@@ -1,7 +1,7 @@
 /* exif-mnote-data-canon.c
  *
- * Copyright © 2002, 2003 Lutz Müller <lutz@users.sourceforge.net>
- * Copyright © 2003 Matthieu Castet <mat-c@users.sourceforge.net>
+ * Copyright (c) 2002, 2003 Lutz Mueller <lutz@users.sourceforge.net>
+ * Copyright (c) 2003 Matthieu Castet <mat-c@users.sourceforge.net>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -15,8 +15,8 @@
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA.
  */
 
 #include <config.h>
@@ -30,7 +30,7 @@
 #include <libexif/exif-utils.h>
 #include <libexif/exif-data.h>
 
-#define DEBUG
+#define CHECKOVERFLOW(offset,datasize,structsize) (( offset >= datasize) || (structsize > datasize) || (offset > datasize - structsize ))
 
 static void
 exif_mnote_data_canon_clear (ExifMnoteDataCanon *n)
@@ -101,6 +101,8 @@ exif_mnote_data_canon_set_byte_order (ExifMnoteData *d, ExifByteOrder o)
        o_orig = n->order;
        n->order = o;
        for (i = 0; i < n->count; i++) {
+               if (n->entries[i].components && (n->entries[i].size/n->entries[i].components < exif_format_get_size (n->entries[i].format)))
+                       continue;
                n->entries[i].order = o;
                exif_array_set_byte_order (n->entries[i].format, n->entries[i].data,
                                n->entries[i].components, o_orig, o);
@@ -118,7 +120,9 @@ exif_mnote_data_canon_save (ExifMnoteData *ne,
        unsigned char **buf, unsigned int *buf_size)
 {
        ExifMnoteDataCanon *n = (ExifMnoteDataCanon *) ne;
-       unsigned int i, o, s, doff;
+       size_t i, o, s, doff;
+       unsigned char *t;
+       size_t ts;
 
        if (!n || !buf || !buf_size) return;
 
@@ -128,7 +132,10 @@ exif_mnote_data_canon_save (ExifMnoteData *ne,
         */
        *buf_size = 2 + n->count * 12 + 4;
        *buf = exif_mem_alloc (ne->mem, sizeof (char) * *buf_size);
-       if (!*buf) return;
+       if (!*buf) {
+               EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteCanon", *buf_size);
+               return;
+       }
 
        /* Save the number of entries */
        exif_set_short (*buf, n->order, (ExifShort) n->count);
@@ -143,14 +150,25 @@ exif_mnote_data_canon_save (ExifMnoteData *ne,
                o += 8;
                s = exif_format_get_size (n->entries[i].format) *
                                                n->entries[i].components;
+               if (s > 65536) {
+                       /* Corrupt data: EXIF data size is limited to the
+                        * maximum size of a JPEG segment (64 kb).
+                        */
+                       continue;
+               }
                if (s > 4) {
-                       *buf_size += s;
+                       ts = *buf_size + s;
 
                        /* Ensure even offsets. Set padding bytes to 0. */
-                       if (s & 1) *buf_size += 1;
-                       *buf = exif_mem_realloc (ne->mem, *buf,
-                                                sizeof (char) * *buf_size);
-                       if (!*buf) return;
+                       if (s & 1) ts += 1;
+                       t = exif_mem_realloc (ne->mem, *buf,
+                                                sizeof (char) * ts);
+                       if (!t) {
+                               EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteCanon", ts);
+                               return;
+                       }
+                       *buf = t;
+                       *buf_size = ts;
                        doff = *buf_size - s;
                        if (s & 1) { doff--; *(*buf + *buf_size - 1) = '\0'; }
                        exif_set_long (*buf + o, n->order, n->offset + doff);
@@ -185,44 +203,103 @@ exif_mnote_data_canon_load (ExifMnoteData *ne,
 {
        ExifMnoteDataCanon *n = (ExifMnoteDataCanon *) ne;
        ExifShort c;
-       unsigned int i, o, s;
+       size_t i, tcount, o, datao;
 
-       if (!n || !buf || !buf_size || (buf_size < 6 + n->offset + 2)) return;
+       if (!n || !buf || !buf_size) {
+               exif_log (ne->log, EXIF_LOG_CODE_CORRUPT_DATA,
+                         "ExifMnoteCanon", "Short MakerNote");
+               return;
+       }
+       datao = 6 + n->offset;
+       if (CHECKOVERFLOW(datao, buf_size, 2)) {
+               exif_log (ne->log, EXIF_LOG_CODE_CORRUPT_DATA,
+                         "ExifMnoteCanon", "Short MakerNote");
+               return;
+       }
+
+       /* Read the number of tags */
+       c = exif_get_short (buf + datao, n->order);
+       datao += 2;
 
-       /* Read the number of entries and remove old ones. */
-       c = exif_get_short (buf + 6 + n->offset, n->order);
+       /* Remove any old entries */
        exif_mnote_data_canon_clear (n);
 
+       /* Reserve enough space for all the possible MakerNote tags */
+       n->entries = exif_mem_alloc (ne->mem, sizeof (MnoteCanonEntry) * c);
+       if (!n->entries) {
+               EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteCanon", sizeof (MnoteCanonEntry) * c);
+               return;
+       }
+
        /* Parse the entries */
-       for (i = 0; i < c; i++) {
-               o = 6 + 2 + n->offset + 12 * i;
-         if (o + 8 > buf_size) return;
-
-               n->count = i + 1;
-               n->entries = exif_mem_realloc (ne->mem, n->entries,
-                               sizeof (MnoteCanonEntry) * (i+1));
-               memset (&n->entries[i], 0, sizeof (MnoteCanonEntry));
-         n->entries[i].tag        = exif_get_short (buf + o, n->order);
-         n->entries[i].format     = exif_get_short (buf + o + 2, n->order);
-         n->entries[i].components = exif_get_long (buf + o + 4, n->order);
-         n->entries[i].order      = n->order;
-
-         /*
-          * Size? If bigger than 4 bytes, the actual data is not
-          * in the entry but somewhere else (offset).
-          */
-         s = exif_format_get_size (n->entries[i].format) * n->entries[i].components;
-               if (!s) return;
-               o += 8;
-               if (s > 4) o = exif_get_long (buf + o, n->order) + 6;
-               if (o + s > buf_size) return;
-
-               /* Sanity check */
-               n->entries[i].data = exif_mem_alloc (ne->mem, sizeof (char) * s);
-               if (!n->entries[i].data) return;
-               n->entries[i].size = s;
-               memcpy (n->entries[i].data, buf + o, s);
+       tcount = 0;
+       for (i = c, o = datao; i; --i, o += 12) {
+               size_t s;
+
+               memset(&n->entries[tcount], 0, sizeof(MnoteCanonEntry));
+               if (CHECKOVERFLOW(o,buf_size,12)) {
+                       exif_log (ne->log, EXIF_LOG_CODE_CORRUPT_DATA,
+                               "ExifMnoteCanon", "Short MakerNote");
+                       break;
+               }
+
+               n->entries[tcount].tag        = exif_get_short (buf + o, n->order);
+               n->entries[tcount].format     = exif_get_short (buf + o + 2, n->order);
+               n->entries[tcount].components = exif_get_long (buf + o + 4, n->order);
+               n->entries[tcount].order      = n->order;
+
+               exif_log (ne->log, EXIF_LOG_CODE_DEBUG, "ExifMnoteCanon",
+                       "Loading entry 0x%x ('%s')...", n->entries[tcount].tag,
+                        mnote_canon_tag_get_name (n->entries[tcount].tag));
+
+               /* Check if we overflow the multiplication. Use buf_size as the max size for integer overflow detection,
+                * we will check the buffer sizes closer later. */
+               if (    exif_format_get_size (n->entries[tcount].format) &&
+                       buf_size / exif_format_get_size (n->entries[tcount].format) < n->entries[tcount].components
+               ) {
+                       exif_log (ne->log, EXIF_LOG_CODE_CORRUPT_DATA,
+                                 "ExifMnoteCanon", "Tag size overflow detected (%u * %lu)", exif_format_get_size (n->entries[tcount].format), n->entries[tcount].components);
+                       continue;
+               }
+
+               /*
+                * Size? If bigger than 4 bytes, the actual data is not
+                * in the entry but somewhere else (offset).
+                */
+               s = exif_format_get_size (n->entries[tcount].format) * 
+                                                                 n->entries[tcount].components;
+               n->entries[tcount].size = s;
+               if (!s) {
+                       exif_log (ne->log, EXIF_LOG_CODE_CORRUPT_DATA,
+                                 "ExifMnoteCanon",
+                                 "Invalid zero-length tag size");
+                       continue;
+
+               } else {
+                       size_t dataofs = o + 8;
+                       if (s > 4) dataofs = exif_get_long (buf + dataofs, n->order) + 6;
+
+                       if (CHECKOVERFLOW(dataofs, buf_size, s)) {
+                               exif_log (ne->log, EXIF_LOG_CODE_DEBUG,
+                                       "ExifMnoteCanon",
+                                       "Tag data past end of buffer (%u > %u)",
+                                       (unsigned)(dataofs + s), buf_size);
+                               continue;
+                       }
+
+                       n->entries[tcount].data = exif_mem_alloc (ne->mem, s);
+                       if (!n->entries[tcount].data) {
+                               EXIF_LOG_NO_MEMORY(ne->log, "ExifMnoteCanon", s);
+                               continue;
+                       }
+                       memcpy (n->entries[tcount].data, buf + dataofs, s);
+               }
+
+               /* Tag was successfully parsed */
+               ++tcount;
        }
+       /* Store the count of successfully parsed tags */
+       n->count = tcount;
 }
 
 static unsigned int
@@ -257,7 +334,7 @@ exif_mnote_data_canon_get_name (ExifMnoteData *note, unsigned int i)
        if (!dc) return NULL;
        exif_mnote_data_canon_get_tags (dc, i, &m, &s);
        if (m >= dc->count) return NULL;
-       return mnote_canon_tag_get_name_sub (dc->entries[m].tag, s);
+       return mnote_canon_tag_get_name_sub (dc->entries[m].tag, s, dc->options);
 }
 
 static const char *
@@ -269,7 +346,7 @@ exif_mnote_data_canon_get_title (ExifMnoteData *note, unsigned int i)
        if (!dc) return NULL;
        exif_mnote_data_canon_get_tags (dc, i, &m, &s);
        if (m >= dc->count) return NULL;
-       return mnote_canon_tag_get_title_sub (dc->entries[m].tag, s);
+       return mnote_canon_tag_get_title_sub (dc->entries[m].tag, s, dc->options);
 }
 
 static const char *
@@ -284,15 +361,29 @@ exif_mnote_data_canon_get_description (ExifMnoteData *note, unsigned int i)
        return mnote_canon_tag_get_description (dc->entries[m].tag);
 }
 
+int
+exif_mnote_data_canon_identify (const ExifData *ed, const ExifEntry *e)
+{
+       char value[8];
+
+       (void) e;  /* unused */
+       ExifEntry *em = exif_data_get_entry (ed, EXIF_TAG_MAKE);
+       if (!em) 
+               return 0;
+       return !strcmp (exif_entry_get_value (em, value, sizeof (value)), "Canon");
+}
+
 ExifMnoteData *
-exif_mnote_data_canon_new (ExifMem *mem)
+exif_mnote_data_canon_new (ExifMem *mem, ExifDataOption o)
 {
        ExifMnoteData *d;
+       ExifMnoteDataCanon *dc;
 
        if (!mem) return NULL;
 
        d = exif_mem_alloc (mem, sizeof (ExifMnoteDataCanon));
-       if (!d) return NULL;
+       if (!d)
+               return NULL;
 
        exif_mnote_data_construct (d, mem);
 
@@ -309,5 +400,7 @@ exif_mnote_data_canon_new (ExifMem *mem)
        d->methods.get_description = exif_mnote_data_canon_get_description;
        d->methods.get_value       = exif_mnote_data_canon_get_value;
 
+       dc = (ExifMnoteDataCanon*)d;
+       dc->options = o;
        return d;
 }