exif-mnote-data-canon.c

Go to the documentation of this file.
00001 /* exif-mnote-data-canon.c
00002  *
00003  * Copyright © 2002, 2003 Lutz Müller <lutz@users.sourceforge.net>
00004  * Copyright © 2003 Matthieu Castet <mat-c@users.sourceforge.net>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the
00018  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019  * Boston, MA 02111-1307, USA.
00020  */
00021 
00022 #include <config.h>
00023 #include "exif-mnote-data-canon.h"
00024 
00025 #include <stdlib.h>
00026 #include <stdio.h>
00027 #include <string.h>
00028 
00029 #include <libexif/exif-byte-order.h>
00030 #include <libexif/exif-utils.h>
00031 #include <libexif/exif-data.h>
00032 
00033 #define DEBUG
00034 
00035 static void
00036 exif_mnote_data_canon_clear (ExifMnoteDataCanon *n)
00037 {
00038         ExifMnoteData *d = (ExifMnoteData *) n;
00039         unsigned int i;
00040 
00041         if (!n) return;
00042 
00043         if (n->entries) {
00044                 for (i = 0; i < n->count; i++)
00045                         if (n->entries[i].data) {
00046                                 exif_mem_free (d->mem, n->entries[i].data);
00047                                 n->entries[i].data = NULL;
00048                         }
00049                 exif_mem_free (d->mem, n->entries);
00050                 n->entries = NULL;
00051                 n->count = 0;
00052         }
00053 }
00054 
00055 static void
00056 exif_mnote_data_canon_free (ExifMnoteData *n)
00057 {
00058         if (!n) return;
00059 
00060         exif_mnote_data_canon_clear ((ExifMnoteDataCanon *) n);
00061 }
00062 
00063 static void
00064 exif_mnote_data_canon_get_tags (ExifMnoteDataCanon *dc, unsigned int n,
00065                 unsigned int *m, unsigned int *s)
00066 {
00067         unsigned int from = 0, to;
00068 
00069         if (!dc || !m) return;
00070         for (*m = 0; *m < dc->count; (*m)++) {
00071                 to = from + mnote_canon_entry_count_values (&dc->entries[*m]);
00072                 if (to > n) {
00073                         if (s) *s = n - from;
00074                         break;
00075                 }
00076                 from = to;
00077         }
00078 }
00079 
00080 static char *
00081 exif_mnote_data_canon_get_value (ExifMnoteData *note, unsigned int n, char *val, unsigned int maxlen)
00082 {
00083         ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) note;
00084         unsigned int m, s;
00085 
00086         if (!dc) return NULL;
00087         exif_mnote_data_canon_get_tags (dc, n, &m, &s);
00088         if (m >= dc->count) return NULL;
00089         return mnote_canon_entry_get_value (&dc->entries[m], s, val, maxlen);
00090 }
00091 
00092 static void
00093 exif_mnote_data_canon_set_byte_order (ExifMnoteData *d, ExifByteOrder o)
00094 {
00095         ExifByteOrder o_orig;
00096         ExifMnoteDataCanon *n = (ExifMnoteDataCanon *) d;
00097         unsigned int i;
00098 
00099         if (!n) return;
00100 
00101         o_orig = n->order;
00102         n->order = o;
00103         for (i = 0; i < n->count; i++) {
00104                 n->entries[i].order = o;
00105                 exif_array_set_byte_order (n->entries[i].format, n->entries[i].data,
00106                                 n->entries[i].components, o_orig, o);
00107         }
00108 }
00109 
00110 static void
00111 exif_mnote_data_canon_set_offset (ExifMnoteData *n, unsigned int o)
00112 {
00113         if (n) ((ExifMnoteDataCanon *) n)->offset = o;
00114 }
00115 
00116 static void
00117 exif_mnote_data_canon_save (ExifMnoteData *ne, 
00118         unsigned char **buf, unsigned int *buf_size)
00119 {
00120         ExifMnoteDataCanon *n = (ExifMnoteDataCanon *) ne;
00121         unsigned int i, o, s, doff;
00122 
00123         if (!n || !buf || !buf_size) return;
00124 
00125         /*
00126          * Allocate enough memory for all entries and the number
00127          * of entries.
00128          */
00129         *buf_size = 2 + n->count * 12 + 4;
00130         *buf = exif_mem_alloc (ne->mem, sizeof (char) * *buf_size);
00131         if (!*buf) return;
00132 
00133         /* Save the number of entries */
00134         exif_set_short (*buf, n->order, (ExifShort) n->count);
00135         
00136         /* Save each entry */
00137         for (i = 0; i < n->count; i++) {
00138                 o = 2 + i * 12;
00139                 exif_set_short (*buf + o + 0, n->order, (ExifShort) n->entries[i].tag);
00140                 exif_set_short (*buf + o + 2, n->order, (ExifShort) n->entries[i].format);
00141                 exif_set_long  (*buf + o + 4, n->order,
00142                                 n->entries[i].components);
00143                 o += 8;
00144                 s = exif_format_get_size (n->entries[i].format) *
00145                                                 n->entries[i].components;
00146                 if (s > 4) {
00147                         *buf_size += s;
00148 
00149                         /* Ensure even offsets. Set padding bytes to 0. */
00150                         if (s & 1) *buf_size += 1;
00151                         *buf = exif_mem_realloc (ne->mem, *buf,
00152                                                  sizeof (char) * *buf_size);
00153                         if (!*buf) return;
00154                         doff = *buf_size - s;
00155                         if (s & 1) { doff--; *(*buf + *buf_size - 1) = '\0'; }
00156                         exif_set_long (*buf + o, n->order, n->offset + doff);
00157                 } else
00158                         doff = o;
00159 
00160                 /*
00161                  * Write the data. Fill unneeded bytes with 0. Do not
00162                  * crash if data is NULL.
00163                  */
00164                 if (!n->entries[i].data) memset (*buf + doff, 0, s);
00165                 else memcpy (*buf + doff, n->entries[i].data, s);
00166                 if (s < 4) memset (*buf + doff + s, 0, (4 - s));
00167         }
00168 }
00169 
00170 /* XXX
00171  * FIXME: exif_mnote_data_canon_load() may fail and there is no
00172  *        semantics to express that.
00173  *        See bug #1054323 for details, especially the comment by liblit
00174  *        after it has supposedly been fixed:
00175  *
00176  *        https://sourceforge.net/tracker/?func=detail&aid=1054323&group_id=12272&atid=112272
00177  *        Unfortunately, the "return" statements aren't commented at
00178  *        all, so it isn't trivial to find out what is a normal
00179  *        return, and what is a reaction to an error condition.
00180  */
00181 
00182 static void
00183 exif_mnote_data_canon_load (ExifMnoteData *ne,
00184         const unsigned char *buf, unsigned int buf_size)
00185 {
00186         ExifMnoteDataCanon *n = (ExifMnoteDataCanon *) ne;
00187         ExifShort c;
00188         unsigned int i, o, s;
00189 
00190         if (!n || !buf || !buf_size || (buf_size < 6 + n->offset + 2)) return;
00191 
00192         /* Read the number of entries and remove old ones. */
00193         c = exif_get_short (buf + 6 + n->offset, n->order);
00194         exif_mnote_data_canon_clear (n);
00195 
00196         /* Parse the entries */
00197         for (i = 0; i < c; i++) {
00198                 o = 6 + 2 + n->offset + 12 * i;
00199           if (o + 8 > buf_size) return;
00200 
00201                 n->count = i + 1;
00202                 n->entries = exif_mem_realloc (ne->mem, n->entries,
00203                                 sizeof (MnoteCanonEntry) * (i+1));
00204                 memset (&n->entries[i], 0, sizeof (MnoteCanonEntry));
00205           n->entries[i].tag        = exif_get_short (buf + o, n->order);
00206           n->entries[i].format     = exif_get_short (buf + o + 2, n->order);
00207           n->entries[i].components = exif_get_long (buf + o + 4, n->order);
00208           n->entries[i].order      = n->order;
00209 
00210           /*
00211            * Size? If bigger than 4 bytes, the actual data is not
00212            * in the entry but somewhere else (offset).
00213            */
00214           s = exif_format_get_size (n->entries[i].format) * n->entries[i].components;
00215                 if (!s) return;
00216                 o += 8;
00217                 if (s > 4) o = exif_get_long (buf + o, n->order) + 6;
00218                 if (o + s > buf_size) return;
00219 
00220                 /* Sanity check */
00221                 n->entries[i].data = exif_mem_alloc (ne->mem, sizeof (char) * s);
00222                 if (!n->entries[i].data) return;
00223                 n->entries[i].size = s;
00224                 memcpy (n->entries[i].data, buf + o, s);
00225         }
00226 }
00227 
00228 static unsigned int
00229 exif_mnote_data_canon_count (ExifMnoteData *n)
00230 {
00231         ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) n;
00232         unsigned int i, c;
00233 
00234         for (i = c = 0; dc && (i < dc->count); i++)
00235                 c += mnote_canon_entry_count_values (&dc->entries[i]);
00236         return c;
00237 }
00238 
00239 static unsigned int
00240 exif_mnote_data_canon_get_id (ExifMnoteData *d, unsigned int i)
00241 {
00242         ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) d;
00243         unsigned int m;
00244 
00245         if (!dc) return 0;
00246         exif_mnote_data_canon_get_tags (dc, i, &m, NULL);
00247         if (m >= dc->count) return 0;
00248         return dc->entries[m].tag;
00249 }
00250 
00251 static const char *
00252 exif_mnote_data_canon_get_name (ExifMnoteData *note, unsigned int i)
00253 {
00254         ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) note;
00255         unsigned int m, s;
00256 
00257         if (!dc) return NULL;
00258         exif_mnote_data_canon_get_tags (dc, i, &m, &s);
00259         if (m >= dc->count) return NULL;
00260         return mnote_canon_tag_get_name_sub (dc->entries[m].tag, s);
00261 }
00262 
00263 static const char *
00264 exif_mnote_data_canon_get_title (ExifMnoteData *note, unsigned int i)
00265 {
00266         ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) note;
00267         unsigned int m, s;
00268 
00269         if (!dc) return NULL;
00270         exif_mnote_data_canon_get_tags (dc, i, &m, &s);
00271         if (m >= dc->count) return NULL;
00272         return mnote_canon_tag_get_title_sub (dc->entries[m].tag, s);
00273 }
00274 
00275 static const char *
00276 exif_mnote_data_canon_get_description (ExifMnoteData *note, unsigned int i)
00277 {
00278         ExifMnoteDataCanon *dc = (ExifMnoteDataCanon *) note;
00279         unsigned int m;
00280 
00281         if (!dc) return NULL;
00282         exif_mnote_data_canon_get_tags (dc, i, &m, NULL);
00283         if (m >= dc->count) return NULL;
00284         return mnote_canon_tag_get_description (dc->entries[m].tag);
00285 }
00286 
00287 ExifMnoteData *
00288 exif_mnote_data_canon_new (ExifMem *mem)
00289 {
00290         ExifMnoteData *d;
00291 
00292         if (!mem) return NULL;
00293 
00294         d = exif_mem_alloc (mem, sizeof (ExifMnoteDataCanon));
00295         if (!d) return NULL;
00296 
00297         exif_mnote_data_construct (d, mem);
00298 
00299         /* Set up function pointers */
00300         d->methods.free            = exif_mnote_data_canon_free;
00301         d->methods.set_byte_order  = exif_mnote_data_canon_set_byte_order;
00302         d->methods.set_offset      = exif_mnote_data_canon_set_offset;
00303         d->methods.load            = exif_mnote_data_canon_load;
00304         d->methods.save            = exif_mnote_data_canon_save;
00305         d->methods.count           = exif_mnote_data_canon_count;
00306         d->methods.get_id          = exif_mnote_data_canon_get_id;
00307         d->methods.get_name        = exif_mnote_data_canon_get_name;
00308         d->methods.get_title       = exif_mnote_data_canon_get_title;
00309         d->methods.get_description = exif_mnote_data_canon_get_description;
00310         d->methods.get_value       = exif_mnote_data_canon_get_value;
00311 
00312         return d;
00313 }

Generated on Tue Dec 19 14:33:53 2006 for EXIF library (libexif) Internals by  doxygen 1.5.1