1 /* exif-mnote-data-canon.c
3 * Copyright © 2002, 2003 Lutz Müller <lutz@users.sourceforge.net>
4 * Copyright © 2003 Matthieu Castet <mat-c@users.sourceforge.net>
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.
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.
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.
23 #include "exif-mnote-data-canon.h"
29 #include <libexif/exif-byte-order.h>
30 #include <libexif/exif-utils.h>
31 #include <libexif/exif-data.h>
36 exif_mnote_data_canon_clear (ExifMnoteDataCanon *n)
38 ExifMnoteData *d = (ExifMnoteData *) n;
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;
49 exif_mem_free (d->mem, n->entries);
56 exif_mnote_data_canon_free (ExifMnoteData *n)
60 exif_mnote_data_canon_clear ((ExifMnoteDataCanon *) n);
64 exif_mnote_data_canon_get_tags (ExifMnoteDataCanon *dc, unsigned int n,
65 unsigned int *m, unsigned int *s)
67 unsigned int from = 0, to;
69 if (!dc || !m) return;
70 for (*m = 0; *m < dc->count; (*m)++) {
71 to = from + mnote_canon_entry_count_values (&dc->entries[*m]);
81 exif_mnote_data_canon_get_value (ExifMnoteData *note, unsigned int n, char *val, unsigned int maxlen)
83 ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) note;
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);
93 exif_mnote_data_canon_set_byte_order (ExifMnoteData *d, ExifByteOrder o)
96 ExifMnoteDataCanon *n = (ExifMnoteDataCanon *) d;
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);
111 exif_mnote_data_canon_set_offset (ExifMnoteData *n, unsigned int o)
113 if (n) ((ExifMnoteDataCanon *) n)->offset = o;
117 exif_mnote_data_canon_save (ExifMnoteData *ne,
118 unsigned char **buf, unsigned int *buf_size)
120 ExifMnoteDataCanon *n = (ExifMnoteDataCanon *) ne;
121 unsigned int i, o, s, doff;
123 if (!n || !buf || !buf_size) return;
126 * Allocate enough memory for all entries and the number
129 *buf_size = 2 + n->count * 12 + 4;
130 *buf = exif_mem_alloc (ne->mem, sizeof (char) * *buf_size);
133 /* Save the number of entries */
134 exif_set_short (*buf, n->order, (ExifShort) n->count);
136 /* Save each entry */
137 for (i = 0; i < n->count; i++) {
139 exif_set_short (*buf + o + 0, n->order, (ExifShort) n->entries[i].tag);
140 exif_set_short (*buf + o + 2, n->order, (ExifShort) n->entries[i].format);
141 exif_set_long (*buf + o + 4, n->order,
142 n->entries[i].components);
144 s = exif_format_get_size (n->entries[i].format) *
145 n->entries[i].components;
149 /* Ensure even offsets. Set padding bytes to 0. */
150 if (s & 1) *buf_size += 1;
151 *buf = exif_mem_realloc (ne->mem, *buf,
152 sizeof (char) * *buf_size);
154 doff = *buf_size - s;
155 if (s & 1) { doff--; *(*buf + *buf_size - 1) = '\0'; }
156 exif_set_long (*buf + o, n->order, n->offset + doff);
161 * Write the data. Fill unneeded bytes with 0. Do not
162 * crash if data is NULL.
164 if (!n->entries[i].data) memset (*buf + doff, 0, s);
165 else memcpy (*buf + doff, n->entries[i].data, s);
166 if (s < 4) memset (*buf + doff + s, 0, (4 - s));
171 * FIXME: exif_mnote_data_canon_load() may fail and there is no
172 * semantics to express that.
173 * See bug #1054323 for details, especially the comment by liblit
174 * after it has supposedly been fixed:
176 * https://sourceforge.net/tracker/?func=detail&aid=1054323&group_id=12272&atid=112272
177 * Unfortunately, the "return" statements aren't commented at
178 * all, so it isn't trivial to find out what is a normal
179 * return, and what is a reaction to an error condition.
183 exif_mnote_data_canon_load (ExifMnoteData *ne,
184 const unsigned char *buf, unsigned int buf_size)
186 ExifMnoteDataCanon *n = (ExifMnoteDataCanon *) ne;
188 unsigned int i, o, s;
190 if (!n || !buf || !buf_size || (buf_size < 6 + n->offset + 2)) return;
192 /* Read the number of entries and remove old ones. */
193 c = exif_get_short (buf + 6 + n->offset, n->order);
194 exif_mnote_data_canon_clear (n);
196 /* Parse the entries */
197 for (i = 0; i < c; i++) {
198 o = 6 + 2 + n->offset + 12 * i;
199 if (o + 8 > buf_size) return;
202 n->entries = exif_mem_realloc (ne->mem, n->entries,
203 sizeof (MnoteCanonEntry) * (i+1));
204 memset (&n->entries[i], 0, sizeof (MnoteCanonEntry));
205 n->entries[i].tag = exif_get_short (buf + o, n->order);
206 n->entries[i].format = exif_get_short (buf + o + 2, n->order);
207 n->entries[i].components = exif_get_long (buf + o + 4, n->order);
208 n->entries[i].order = n->order;
211 * Size? If bigger than 4 bytes, the actual data is not
212 * in the entry but somewhere else (offset).
214 s = exif_format_get_size (n->entries[i].format) * n->entries[i].components;
217 if (s > 4) o = exif_get_long (buf + o, n->order) + 6;
218 if (o + s > buf_size) return;
221 n->entries[i].data = exif_mem_alloc (ne->mem, sizeof (char) * s);
222 if (!n->entries[i].data) return;
223 n->entries[i].size = s;
224 memcpy (n->entries[i].data, buf + o, s);
229 exif_mnote_data_canon_count (ExifMnoteData *n)
231 ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) n;
234 for (i = c = 0; dc && (i < dc->count); i++)
235 c += mnote_canon_entry_count_values (&dc->entries[i]);
240 exif_mnote_data_canon_get_id (ExifMnoteData *d, unsigned int i)
242 ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) d;
246 exif_mnote_data_canon_get_tags (dc, i, &m, NULL);
247 if (m >= dc->count) return 0;
248 return dc->entries[m].tag;
252 exif_mnote_data_canon_get_name (ExifMnoteData *note, unsigned int i)
254 ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) note;
257 if (!dc) return NULL;
258 exif_mnote_data_canon_get_tags (dc, i, &m, &s);
259 if (m >= dc->count) return NULL;
260 return mnote_canon_tag_get_name_sub (dc->entries[m].tag, s);
264 exif_mnote_data_canon_get_title (ExifMnoteData *note, unsigned int i)
266 ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) note;
269 if (!dc) return NULL;
270 exif_mnote_data_canon_get_tags (dc, i, &m, &s);
271 if (m >= dc->count) return NULL;
272 return mnote_canon_tag_get_title_sub (dc->entries[m].tag, s);
276 exif_mnote_data_canon_get_description (ExifMnoteData *note, unsigned int i)
278 ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) note;
281 if (!dc) return NULL;
282 exif_mnote_data_canon_get_tags (dc, i, &m, NULL);
283 if (m >= dc->count) return NULL;
284 return mnote_canon_tag_get_description (dc->entries[m].tag);
288 exif_mnote_data_canon_new (ExifMem *mem)
292 if (!mem) return NULL;
294 d = exif_mem_alloc (mem, sizeof (ExifMnoteDataCanon));
297 exif_mnote_data_construct (d, mem);
299 /* Set up function pointers */
300 d->methods.free = exif_mnote_data_canon_free;
301 d->methods.set_byte_order = exif_mnote_data_canon_set_byte_order;
302 d->methods.set_offset = exif_mnote_data_canon_set_offset;
303 d->methods.load = exif_mnote_data_canon_load;
304 d->methods.save = exif_mnote_data_canon_save;
305 d->methods.count = exif_mnote_data_canon_count;
306 d->methods.get_id = exif_mnote_data_canon_get_id;
307 d->methods.get_name = exif_mnote_data_canon_get_name;
308 d->methods.get_title = exif_mnote_data_canon_get_title;
309 d->methods.get_description = exif_mnote_data_canon_get_description;
310 d->methods.get_value = exif_mnote_data_canon_get_value;