exif-mnote-data-pentax.c

Go to the documentation of this file.
00001 /* exif-mnote-data-pentax.c
00002  *
00003  * Copyright © 2002, 2003 Lutz Mueller <lutz@users.sourceforge.net>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful, 
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details. 
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the
00017  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  * Boston, MA 02111-1307, USA.
00019  */
00020 
00021 #include "config.h"
00022 #include "exif-mnote-data-pentax.h"
00023 
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <stdio.h>
00027 
00028 #include <libexif/exif-byte-order.h>
00029 #include <libexif/exif-utils.h>
00030 
00031 /* #define DEBUG */
00032 
00033 static void
00034 exif_mnote_data_pentax_clear (ExifMnoteDataPentax *n)
00035 {
00036         ExifMnoteData *d = (ExifMnoteData *) n;
00037         unsigned int i;
00038 
00039         if (!n) return;
00040 
00041         if (n->entries) {
00042                 for (i = 0; i < n->count; i++)
00043                         if (n->entries[i].data) {
00044                                 exif_mem_free (d->mem, n->entries[i].data);
00045                                 n->entries[i].data = NULL;
00046                         }
00047                 exif_mem_free (d->mem, n->entries);
00048                 n->entries = NULL;
00049                 n->count = 0;
00050         }
00051 }
00052 
00053 static void
00054 exif_mnote_data_pentax_free (ExifMnoteData *n)
00055 {
00056         if (!n) return;
00057 
00058         exif_mnote_data_pentax_clear ((ExifMnoteDataPentax *) n);
00059 }
00060 
00061 static char *
00062 exif_mnote_data_pentax_get_value (ExifMnoteData *d, unsigned int i, char *val, unsigned int maxlen)
00063 {
00064         ExifMnoteDataPentax *n = (ExifMnoteDataPentax *) d;
00065 
00066         if (!n) return NULL;
00067         if (n->count <= i) return NULL;
00068         return mnote_pentax_entry_get_value (&n->entries[i], val, maxlen);
00069 }
00070 
00071 static void
00072 exif_mnote_data_pentax_load (ExifMnoteData *en,
00073                 const unsigned char *buf, unsigned int buf_size)
00074 {
00075         ExifMnoteDataPentax *n = (ExifMnoteDataPentax *) en;
00076         unsigned int i, o, s;
00077         ExifShort c;
00078 
00079         /* Number of entries */
00080         if (buf_size < 2) return;
00081         c = exif_get_short (buf + 6 + n->offset, n->order);
00082         n->entries = exif_mem_alloc (en->mem, sizeof (MnotePentaxEntry) * c);
00083         if (!n->entries) return;
00084 
00085         for (i = 0; i < c; i++) {
00086             o = 6 + 2 + n->offset + 12 * i;
00087             if (o + 8 > buf_size) return;
00088 
00089             n->count = i + 1;
00090             n->entries[i].tag        = exif_get_short (buf + o + 0, n->order);
00091             n->entries[i].format     = exif_get_short (buf + o + 2, n->order);
00092             n->entries[i].components = exif_get_long  (buf + o + 4, n->order);
00093             n->entries[i].order      = n->order;
00094 
00095             /*
00096              * Size? If bigger than 4 bytes, the actual data is not
00097              * in the entry but somewhere else (offset).
00098              */
00099             s = exif_format_get_size (n->entries[i].format) *
00100                                       n->entries[i].components;
00101             if (!s) return;
00102             o += 8;
00103             if (s > 4) o = exif_get_long (buf + o, n->order) + 6;
00104             if (o + s > buf_size) return;
00105                                                                                 
00106             /* Sanity check */
00107             n->entries[i].data = exif_mem_alloc (en->mem, sizeof (char) * s);
00108             if (!n->entries[i].data) return;
00109             n->entries[i].size = s;
00110             memcpy (n->entries[i].data, buf + o, s);
00111         }
00112 }
00113 
00114 static unsigned int
00115 exif_mnote_data_pentax_count (ExifMnoteData *n)
00116 {
00117         return n ? ((ExifMnoteDataPentax *) n)->count : 0;
00118 }
00119 
00120 static unsigned int
00121 exif_mnote_data_pentax_get_id (ExifMnoteData *d, unsigned int n)
00122 {
00123         ExifMnoteDataPentax *note = (ExifMnoteDataPentax *) d;
00124 
00125         if (!note) return 0;
00126         if (note->count <= n) return 0;
00127         return note->entries[n].tag;
00128 }
00129 
00130 static const char *
00131 exif_mnote_data_pentax_get_name (ExifMnoteData *d, unsigned int n)
00132 {
00133         ExifMnoteDataPentax *note = (ExifMnoteDataPentax *) d;
00134 
00135         if (!note) return NULL;
00136         if (note->count <= n) return NULL;
00137         return mnote_pentax_tag_get_name (note->entries[n].tag);
00138 }
00139 
00140 static const char *
00141 exif_mnote_data_pentax_get_title (ExifMnoteData *d, unsigned int n)
00142 {
00143         ExifMnoteDataPentax *note = (ExifMnoteDataPentax *) d;
00144 
00145         if (!note) return NULL;
00146         if (note->count <= n) return NULL;
00147         return mnote_pentax_tag_get_title (note->entries[n].tag);
00148 }
00149 
00150 static const char *
00151 exif_mnote_data_pentax_get_description (ExifMnoteData *d, unsigned int n)
00152 {
00153         ExifMnoteDataPentax *note = (ExifMnoteDataPentax *) d;
00154         
00155         if (!note) return NULL;
00156         if (note->count <= n) return NULL;
00157         return mnote_pentax_tag_get_description (note->entries[n].tag);
00158 }
00159 
00160 static void
00161 exif_mnote_data_pentax_set_offset (ExifMnoteData *d, unsigned int o)
00162 {
00163         if (d) ((ExifMnoteDataPentax *) d)->offset = o;
00164 }
00165 
00166 static void
00167 exif_mnote_data_pentax_set_byte_order (ExifMnoteData *d, ExifByteOrder o)
00168 {
00169         ExifByteOrder o_orig;
00170         ExifMnoteDataPentax *n = (ExifMnoteDataPentax *) d;
00171         unsigned int i;
00172 
00173         if (!n) return;
00174 
00175         o_orig = n->order;
00176         n->order = o;
00177         for (i = 0; i < n->count; i++) {
00178                 n->entries[i].order = o;
00179                 exif_array_set_byte_order (n->entries[i].format, n->entries[i].data,
00180                                 n->entries[i].components, o_orig, o);
00181         }
00182 }
00183 
00184 ExifMnoteData *
00185 exif_mnote_data_pentax_new (ExifMem *mem)
00186 {
00187         ExifMnoteData *d;
00188 
00189         if (!mem) return NULL;
00190 
00191         d = exif_mem_alloc (mem, sizeof (ExifMnoteDataPentax));
00192         if (!d) return NULL;
00193 
00194         exif_mnote_data_construct (d, mem);
00195 
00196         /* Set up function pointers */
00197         d->methods.free            = exif_mnote_data_pentax_free;
00198         d->methods.set_byte_order  = exif_mnote_data_pentax_set_byte_order;
00199         d->methods.set_offset      = exif_mnote_data_pentax_set_offset;
00200         d->methods.load            = exif_mnote_data_pentax_load;
00201         d->methods.count           = exif_mnote_data_pentax_count;
00202         d->methods.get_id          = exif_mnote_data_pentax_get_id;
00203         d->methods.get_name        = exif_mnote_data_pentax_get_name;
00204         d->methods.get_title       = exif_mnote_data_pentax_get_title;
00205         d->methods.get_description = exif_mnote_data_pentax_get_description;
00206         d->methods.get_value       = exif_mnote_data_pentax_get_value;
00207 
00208         return d;
00209 }

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