Replaced unsigned int by size_t in some places
[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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, 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 DEBUG
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                 n->entries[i].order = o;
105                 exif_array_set_byte_order (n->entries[i].format, n->entries[i].data,
106                                 n->entries[i].components, o_orig, o);
107         }
108 }
109
110 static void
111 exif_mnote_data_canon_set_offset (ExifMnoteData *n, unsigned int o)
112 {
113         if (n) ((ExifMnoteDataCanon *) n)->offset = o;
114 }
115
116 static void
117 exif_mnote_data_canon_save (ExifMnoteData *ne, 
118         unsigned char **buf, unsigned int *buf_size)
119 {
120         ExifMnoteDataCanon *n = (ExifMnoteDataCanon *) ne;
121         size_t i, o, s, doff;
122         unsigned char *t;
123         size_t ts;
124
125         if (!n || !buf || !buf_size) return;
126
127         /*
128          * Allocate enough memory for all entries and the number
129          * of entries.
130          */
131         *buf_size = 2 + n->count * 12 + 4;
132         *buf = exif_mem_alloc (ne->mem, sizeof (char) * *buf_size);
133         if (!*buf) return;
134
135         /* Save the number of entries */
136         exif_set_short (*buf, n->order, (ExifShort) n->count);
137         
138         /* Save each entry */
139         for (i = 0; i < n->count; i++) {
140                 o = 2 + i * 12;
141                 exif_set_short (*buf + o + 0, n->order, (ExifShort) n->entries[i].tag);
142                 exif_set_short (*buf + o + 2, n->order, (ExifShort) n->entries[i].format);
143                 exif_set_long  (*buf + o + 4, n->order,
144                                 n->entries[i].components);
145                 o += 8;
146                 s = exif_format_get_size (n->entries[i].format) *
147                                                 n->entries[i].components;
148                 if (s > 65536) {
149                         /* Corrupt data: EXIF data size is limited to the
150                          * maximum size of a JPEG segment (64 kb).
151                          */
152                         continue;
153                 }
154                 if (s > 4) {
155                         ts = *buf_size + s;
156
157                         /* Ensure even offsets. Set padding bytes to 0. */
158                         if (s & 1) ts += 1;
159                         t = exif_mem_realloc (ne->mem, *buf,
160                                                  sizeof (char) * ts);
161                         if (!t) return;
162                         *buf = t;
163                         *buf_size = ts;
164                         doff = *buf_size - s;
165                         if (s & 1) { doff--; *(*buf + *buf_size - 1) = '\0'; }
166                         exif_set_long (*buf + o, n->order, n->offset + doff);
167                 } else
168                         doff = o;
169
170                 /*
171                  * Write the data. Fill unneeded bytes with 0. Do not
172                  * crash if data is NULL.
173                  */
174                 if (!n->entries[i].data) memset (*buf + doff, 0, s);
175                 else memcpy (*buf + doff, n->entries[i].data, s);
176                 if (s < 4) memset (*buf + doff + s, 0, (4 - s));
177         }
178 }
179
180 /* XXX
181  * FIXME: exif_mnote_data_canon_load() may fail and there is no
182  *        semantics to express that.
183  *        See bug #1054323 for details, especially the comment by liblit
184  *        after it has supposedly been fixed:
185  *
186  *        https://sourceforge.net/tracker/?func=detail&aid=1054323&group_id=12272&atid=112272
187  *        Unfortunately, the "return" statements aren't commented at
188  *        all, so it isn't trivial to find out what is a normal
189  *        return, and what is a reaction to an error condition.
190  */
191
192 static void
193 exif_mnote_data_canon_load (ExifMnoteData *ne,
194         const unsigned char *buf, unsigned int buf_size)
195 {
196         ExifMnoteDataCanon *n = (ExifMnoteDataCanon *) ne;
197         ExifShort c;
198         size_t i, o, s;
199         MnoteCanonEntry *t;
200
201         if (!n || !buf || !buf_size || (buf_size < 6 + n->offset + 2)) return;
202
203         /* Read the number of entries and remove old ones. */
204         c = exif_get_short (buf + 6 + n->offset, n->order);
205         exif_mnote_data_canon_clear (n);
206
207         /* Parse the entries */
208         for (i = 0; i < c; i++) {
209                 o = 6 + 2 + n->offset + 12 * i;
210           if (o + 8 > buf_size) return;
211
212                 t = exif_mem_realloc (ne->mem, n->entries,
213                                 sizeof (MnoteCanonEntry) * (i + 1));
214                 if (!t) return;
215                 n->count = i + 1;
216                 n->entries = t;
217                 memset (&n->entries[i], 0, sizeof (MnoteCanonEntry));
218           n->entries[i].tag        = exif_get_short (buf + o, n->order);
219           n->entries[i].format     = exif_get_short (buf + o + 2, n->order);
220           n->entries[i].components = exif_get_long (buf + o + 4, n->order);
221           n->entries[i].order      = n->order;
222
223           /*
224            * Size? If bigger than 4 bytes, the actual data is not
225            * in the entry but somewhere else (offset).
226            */
227           s = exif_format_get_size (n->entries[i].format) * n->entries[i].components;
228                 if (!s) return;
229                 o += 8;
230                 if (s > 4) o = exif_get_long (buf + o, n->order) + 6;
231                 if (o + s > buf_size) return;
232
233                 /* Sanity check */
234                 n->entries[i].data = exif_mem_alloc (ne->mem, sizeof (char) * s);
235                 if (!n->entries[i].data) return;
236                 n->entries[i].size = s;
237                 memcpy (n->entries[i].data, buf + o, s);
238         }
239 }
240
241 static unsigned int
242 exif_mnote_data_canon_count (ExifMnoteData *n)
243 {
244         ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) n;
245         unsigned int i, c;
246
247         for (i = c = 0; dc && (i < dc->count); i++)
248                 c += mnote_canon_entry_count_values (&dc->entries[i]);
249         return c;
250 }
251
252 static unsigned int
253 exif_mnote_data_canon_get_id (ExifMnoteData *d, unsigned int i)
254 {
255         ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) d;
256         unsigned int m;
257
258         if (!dc) return 0;
259         exif_mnote_data_canon_get_tags (dc, i, &m, NULL);
260         if (m >= dc->count) return 0;
261         return dc->entries[m].tag;
262 }
263
264 static const char *
265 exif_mnote_data_canon_get_name (ExifMnoteData *note, unsigned int i)
266 {
267         ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) note;
268         unsigned int m, s;
269
270         if (!dc) return NULL;
271         exif_mnote_data_canon_get_tags (dc, i, &m, &s);
272         if (m >= dc->count) return NULL;
273         return mnote_canon_tag_get_name_sub (dc->entries[m].tag, s, dc->options);
274 }
275
276 static const char *
277 exif_mnote_data_canon_get_title (ExifMnoteData *note, unsigned int i)
278 {
279         ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) note;
280         unsigned int m, s;
281
282         if (!dc) return NULL;
283         exif_mnote_data_canon_get_tags (dc, i, &m, &s);
284         if (m >= dc->count) return NULL;
285         return mnote_canon_tag_get_title_sub (dc->entries[m].tag, s, dc->options);
286 }
287
288 static const char *
289 exif_mnote_data_canon_get_description (ExifMnoteData *note, unsigned int i)
290 {
291         ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) note;
292         unsigned int m;
293
294         if (!dc) return NULL;
295         exif_mnote_data_canon_get_tags (dc, i, &m, NULL);
296         if (m >= dc->count) return NULL;
297         return mnote_canon_tag_get_description (dc->entries[m].tag);
298 }
299
300 ExifMnoteData *
301 exif_mnote_data_canon_new (ExifMem *mem, ExifDataOption o)
302 {
303         ExifMnoteData *d;
304         ExifMnoteDataCanon *dc;
305
306         if (!mem) return NULL;
307
308         d = exif_mem_alloc (mem, sizeof (ExifMnoteDataCanon));
309         if (!d) return NULL;
310
311         exif_mnote_data_construct (d, mem);
312
313         /* Set up function pointers */
314         d->methods.free            = exif_mnote_data_canon_free;
315         d->methods.set_byte_order  = exif_mnote_data_canon_set_byte_order;
316         d->methods.set_offset      = exif_mnote_data_canon_set_offset;
317         d->methods.load            = exif_mnote_data_canon_load;
318         d->methods.save            = exif_mnote_data_canon_save;
319         d->methods.count           = exif_mnote_data_canon_count;
320         d->methods.get_id          = exif_mnote_data_canon_get_id;
321         d->methods.get_name        = exif_mnote_data_canon_get_name;
322         d->methods.get_title       = exif_mnote_data_canon_get_title;
323         d->methods.get_description = exif_mnote_data_canon_get_description;
324         d->methods.get_value       = exif_mnote_data_canon_get_value;
325
326         dc = (ExifMnoteDataCanon*)d;
327         dc->options = o;
328         return d;
329 }