1 /* exif-content.c
2 *
3 * Copyright (c) 2001 Lutz Mueller <lutz@users.sourceforge.net>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA.
19 */
20
21 #include <config.h>
22
23 #include <libexif/exif-content.h>
24 #include <libexif/exif-system.h>
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 /* unused constant
31 * static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
32 */
33
34 const static unsigned char FOCUS_MODE_AUTO[] = {'A', 'u', 't', 'o'};
35 const static unsigned char FOCUS_MODE_AF_MF[] = {'A', 'F', '_', 'M', 'F'};
36 const static unsigned char FOCUS_MODE_AF_C[] = {'A', 'F', '_', 'C'};
37 const static unsigned char FOCUS_MODE_AF_S[] = {'A', 'F', '_', 'S'};
38
39 struct _ExifContentPrivate
40 {
41 unsigned int ref_count;
42
43 ExifMem *mem;
44 ExifLog *log;
45 };
46
47 ExifContent *
exif_content_new(void)48 exif_content_new (void)
49 {
50 ExifMem *mem = exif_mem_new_default ();
51 ExifContent *content = exif_content_new_mem (mem);
52
53 exif_mem_unref (mem);
54
55 return content;
56 }
57
58 ExifContent *
exif_content_new_mem(ExifMem * mem)59 exif_content_new_mem (ExifMem *mem)
60 {
61 ExifContent *content;
62
63 if (!mem) return NULL;
64
65 content = exif_mem_alloc (mem, (ExifLong) sizeof (ExifContent));
66 if (!content)
67 return NULL;
68 content->priv = exif_mem_alloc (mem,
69 (ExifLong) sizeof (ExifContentPrivate));
70 if (!content->priv) {
71 exif_mem_free (mem, content);
72 return NULL;
73 }
74
75 content->priv->ref_count = 1;
76
77 content->priv->mem = mem;
78 exif_mem_ref (mem);
79
80 return content;
81 }
82
83 void
exif_content_ref(ExifContent * content)84 exif_content_ref (ExifContent *content)
85 {
86 if (!content)
87 return;
88
89 content->priv->ref_count++;
90 }
91
92 void
exif_content_unref(ExifContent * content)93 exif_content_unref (ExifContent *content)
94 {
95 if (!content || !content->priv)
96 return;
97
98 content->priv->ref_count--;
99 if (!content->priv->ref_count)
100 exif_content_free (content);
101 }
102
103 void
exif_content_free(ExifContent * content)104 exif_content_free (ExifContent *content)
105 {
106 ExifMem *mem = (content && content->priv) ? content->priv->mem : NULL;
107 unsigned int i;
108
109 if (!content) return;
110
111 for (i = 0; i < content->count; i++)
112 exif_entry_unref (content->entries[i]);
113 exif_mem_free (mem, content->entries);
114
115 if (content->priv) {
116 exif_log_unref (content->priv->log);
117 }
118
119 exif_mem_free (mem, content->priv);
120 exif_mem_free (mem, content);
121 exif_mem_unref (mem);
122 }
123
124 void
exif_content_dump(ExifContent * content,unsigned int indent)125 exif_content_dump (ExifContent *content, unsigned int indent)
126 {
127 char buf[1024];
128 unsigned int i, l;
129
130 if (!content)
131 return;
132
133 l = MIN(sizeof(buf)-1, 2*indent);
134 memset(buf, ' ', l);
135 buf[l] = '\0';
136
137 printf ("%sDumping exif content (%u entries)...\n", buf,
138 content->count);
139 for (i = 0; i < content->count; i++)
140 exif_entry_dump (content->entries[i], indent + 1);
141 }
142
143 void
exif_content_add_entry(ExifContent * c,ExifEntry * entry)144 exif_content_add_entry (ExifContent *c, ExifEntry *entry)
145 {
146 ExifEntry **entries;
147 if (!c || !c->priv || !entry || entry->parent) return;
148
149 /* One tag can only be added once to an IFD. */
150 if (exif_content_get_entry (c, entry->tag) && entry->tag != EXIF_TAG_MAKER_NOTE) {
151 exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "ExifContent",
152 "An attempt has been made to add "
153 "the tag '%s' twice to an IFD. This is against "
154 "specification.", exif_tag_get_name (entry->tag));
155 return;
156 }
157
158 /* Add the entry */
159 if (c->entries) {
160 entries = exif_mem_realloc (c->priv->mem,
161 c->entries, sizeof (ExifEntry*) * (c->count + 1));
162 } else {
163 entries = exif_mem_alloc (c->priv->mem,
164 sizeof (ExifEntry*) * (c->count + 1));
165 }
166
167 if (!entries) return;
168 entry->parent = c;
169 entries[c->count++] = entry;
170 c->entries = entries;
171 exif_entry_ref (entry);
172 }
173
174 void
exif_content_remove_entry(ExifContent * c,ExifEntry * e)175 exif_content_remove_entry (ExifContent *c, ExifEntry *e)
176 {
177 unsigned int i;
178 ExifEntry **t, *temp;
179
180 if (!c || !c->priv || !e || (e->parent != c)) return;
181
182 /* Search the entry */
183 for (i = 0; i < c->count; i++)
184 if (c->entries[i] == e)
185 break;
186
187 if (i == c->count)
188 return;
189
190 /* Remove the entry */
191 temp = c->entries[c->count-1];
192 if (c->count > 1) {
193 t = exif_mem_realloc (c->priv->mem, c->entries,
194 sizeof(ExifEntry*) * (c->count - 1));
195 if (!t) {
196 return;
197 }
198 c->entries = t;
199 c->count--;
200 if (i != c->count) { /* we deallocated the last slot already */
201 memmove (&t[i], &t[i + 1], sizeof (ExifEntry*) * (c->count - i - 1));
202 t[c->count-1] = temp;
203 }
204 } else {
205 exif_mem_free (c->priv->mem, c->entries);
206 c->entries = NULL;
207 c->count = 0;
208 }
209 e->parent = NULL;
210 exif_entry_unref (e);
211 }
212
213 ExifEntry *
exif_content_get_entry(ExifContent * content,ExifTag tag)214 exif_content_get_entry (ExifContent *content, ExifTag tag)
215 {
216 unsigned int i;
217
218 if (!content)
219 return (NULL);
220
221 for (i = 0; i < content->count; i++)
222 if (content->entries[i]->tag == tag)
223 return (content->entries[i]);
224 return (NULL);
225 }
226
227 ExifEntry *
exif_content_get_entry_ext(ExifContent * content,ExifTag tag)228 exif_content_get_entry_ext (ExifContent *content, ExifTag tag)
229 {
230 if (!content) {
231 return (NULL);
232 }
233 for (unsigned int i = 0; i < content->count; i++) {
234 if (content->entries[i]->tag == tag) {
235 unsigned char* data = content->entries[i]->data;
236 if (tag == EXIF_TAG_MAKER_NOTE && data &&
237 (!memcmp(data, FOCUS_MODE_AUTO, sizeof(FOCUS_MODE_AUTO) / sizeof(FOCUS_MODE_AUTO[0])) ||
238 !memcmp(data, FOCUS_MODE_AF_C, sizeof(FOCUS_MODE_AF_C) / sizeof(FOCUS_MODE_AF_C[0])) ||
239 !memcmp(data, FOCUS_MODE_AF_MF, sizeof(FOCUS_MODE_AF_MF) / sizeof(FOCUS_MODE_AF_MF[0])) ||
240 !memcmp(data, FOCUS_MODE_AF_S, sizeof(FOCUS_MODE_AF_S) / sizeof(FOCUS_MODE_AF_S[0])))) {
241 return (content->entries[i]);
242 }
243 continue;
244 }
245 }
246 return (NULL);
247 }
248
249 ExifEntry *
exif_content_get_huawei_makenote_entry(ExifContent * content)250 exif_content_get_huawei_makenote_entry (ExifContent *content)
251 {
252 if (!content)
253 return (NULL);
254
255 ExifEntry *entry = NULL;
256 for (unsigned int i = 0; i < content->count; i++) {
257 entry = content->entries[i];
258 if (entry->tag == EXIF_TAG_MAKER_NOTE) {
259 if (entry->data && (entry->size >= 8) &&
260 (!memcmp(entry->data, "HUAWEI\0\0", 8))) {
261 return entry;
262 }
263 }
264 }
265
266 return (NULL);
267 }
268
269 void
exif_content_foreach_entry(ExifContent * content,ExifContentForeachEntryFunc func,void * data)270 exif_content_foreach_entry (ExifContent *content,
271 ExifContentForeachEntryFunc func, void *data)
272 {
273 unsigned int i;
274
275 if (!content || !func)
276 return;
277
278 for (i = 0; i < content->count; i++)
279 func (content->entries[i], data);
280 }
281
282 void
exif_content_log(ExifContent * content,ExifLog * log)283 exif_content_log (ExifContent *content, ExifLog *log)
284 {
285 if (!content || !content->priv || !log || content->priv->log == log)
286 return;
287
288 if (content->priv->log) exif_log_unref (content->priv->log);
289 content->priv->log = log;
290 exif_log_ref (log);
291 }
292
293 ExifIfd
exif_content_get_ifd(ExifContent * c)294 exif_content_get_ifd (ExifContent *c)
295 {
296 if (!c || !c->parent) return EXIF_IFD_COUNT;
297
298 return
299 ((c)->parent->ifd[EXIF_IFD_EXIF] == (c)) ? EXIF_IFD_EXIF :
300 ((c)->parent->ifd[EXIF_IFD_0] == (c)) ? EXIF_IFD_0 :
301 ((c)->parent->ifd[EXIF_IFD_1] == (c)) ? EXIF_IFD_1 :
302 ((c)->parent->ifd[EXIF_IFD_GPS] == (c)) ? EXIF_IFD_GPS :
303 ((c)->parent->ifd[EXIF_IFD_INTEROPERABILITY] == (c)) ? EXIF_IFD_INTEROPERABILITY :
304 EXIF_IFD_COUNT;
305 }
306
307 static void
fix_func(ExifEntry * e,void * UNUSED (data))308 fix_func (ExifEntry *e, void *UNUSED(data))
309 {
310 exif_entry_fix (e);
311 }
312
313 /*!
314 * Check if this entry is unknown and if so, delete it.
315 * \note Be careful calling this function in a loop. Deleting an entry from
316 * an ExifContent changes the index of subsequent entries, as well as the
317 * total size of the entries array.
318 */
319 static void
remove_not_recorded(ExifEntry * e,void * UNUSED (data))320 remove_not_recorded (ExifEntry *e, void *UNUSED(data))
321 {
322 ExifIfd ifd = exif_entry_get_ifd(e) ;
323 ExifContent *c = e->parent;
324 ExifDataType dt = exif_data_get_data_type (c->parent);
325 ExifTag t = e->tag;
326
327 if (exif_tag_get_support_level_in_ifd (t, ifd, dt) ==
328 EXIF_SUPPORT_LEVEL_NOT_RECORDED) {
329 exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "exif-content",
330 "Tag 0x%04x is not recorded in IFD '%s' and has therefore been "
331 "removed.", t, exif_ifd_get_name (ifd));
332 exif_content_remove_entry (c, e);
333 }
334
335 }
336
337 void
exif_content_fix(ExifContent * c)338 exif_content_fix (ExifContent *c)
339 {
340 ExifIfd ifd = exif_content_get_ifd (c);
341 ExifDataType dt;
342 ExifEntry *e;
343 unsigned int i, num;
344
345 if (!c)
346 return;
347
348 dt = exif_data_get_data_type (c->parent);
349
350 /*
351 * First of all, fix all existing entries.
352 */
353 exif_content_foreach_entry (c, fix_func, NULL);
354
355 /*
356 * Go through each tag and if it's not recorded, remove it. If one
357 * is removed, exif_content_foreach_entry() will skip the next entry,
358 * so if this happens do the loop again from the beginning to ensure
359 * they're all checked. This could be avoided if we stop relying on
360 * exif_content_foreach_entry but loop intelligently here.
361 */
362 do {
363 num = c->count;
364 exif_content_foreach_entry (c, remove_not_recorded, NULL);
365 } while (num != c->count);
366
367 /*
368 * Then check for non-existing mandatory tags and create them if needed
369 */
370 num = exif_tag_table_count();
371 for (i = 0; i < num; ++i) {
372 const ExifTag t = exif_tag_table_get_tag (i);
373 if (exif_tag_get_support_level_in_ifd (t, ifd, dt) ==
374 EXIF_SUPPORT_LEVEL_MANDATORY) {
375 if (exif_content_get_entry (c, t))
376 /* This tag already exists */
377 continue;
378 exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "exif-content",
379 "Tag '%s' is mandatory in IFD '%s' and has therefore been added.",
380 exif_tag_get_name_in_ifd (t, ifd), exif_ifd_get_name (ifd));
381 e = exif_entry_new ();
382 exif_content_add_entry (c, e);
383 exif_entry_initialize (e, t);
384 exif_entry_unref (e);
385 }
386 }
387 }
388