• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 struct _ExifContentPrivate
35 {
36 	unsigned int ref_count;
37 
38 	ExifMem *mem;
39 	ExifLog *log;
40 };
41 
42 ExifContent *
exif_content_new(void)43 exif_content_new (void)
44 {
45 	ExifMem *mem = exif_mem_new_default ();
46 	ExifContent *content = exif_content_new_mem (mem);
47 
48 	exif_mem_unref (mem);
49 
50 	return content;
51 }
52 
53 ExifContent *
exif_content_new_mem(ExifMem * mem)54 exif_content_new_mem (ExifMem *mem)
55 {
56 	ExifContent *content;
57 
58 	if (!mem) return NULL;
59 
60 	content = exif_mem_alloc (mem, (ExifLong) sizeof (ExifContent));
61 	if (!content)
62 		return NULL;
63 	content->priv = exif_mem_alloc (mem,
64 				(ExifLong) sizeof (ExifContentPrivate));
65 	if (!content->priv) {
66 		exif_mem_free (mem, content);
67 		return NULL;
68 	}
69 
70 	content->priv->ref_count = 1;
71 
72 	content->priv->mem = mem;
73 	exif_mem_ref (mem);
74 
75 	return content;
76 }
77 
78 void
exif_content_ref(ExifContent * content)79 exif_content_ref (ExifContent *content)
80 {
81 	if (!content)
82 		return;
83 
84 	content->priv->ref_count++;
85 }
86 
87 void
exif_content_unref(ExifContent * content)88 exif_content_unref (ExifContent *content)
89 {
90 	if (!content)
91 		return;
92 
93 	content->priv->ref_count--;
94 	if (!content->priv->ref_count)
95 		exif_content_free (content);
96 }
97 
98 void
exif_content_free(ExifContent * content)99 exif_content_free (ExifContent *content)
100 {
101 	ExifMem *mem = (content && content->priv) ? content->priv->mem : NULL;
102 	unsigned int i;
103 
104 	if (!content) return;
105 
106 	for (i = 0; i < content->count; i++)
107 		exif_entry_unref (content->entries[i]);
108 	exif_mem_free (mem, content->entries);
109 
110 	if (content->priv) {
111 		exif_log_unref (content->priv->log);
112 	}
113 
114 	exif_mem_free (mem, content->priv);
115 	exif_mem_free (mem, content);
116 	exif_mem_unref (mem);
117 }
118 
119 void
exif_content_dump(ExifContent * content,unsigned int indent)120 exif_content_dump (ExifContent *content, unsigned int indent)
121 {
122 	char buf[1024];
123 	unsigned int i, l;
124 
125 	if (!content)
126 		return;
127 
128 	l = MIN(sizeof(buf)-1, 2*indent);
129 	memset(buf, ' ', l);
130 	buf[l] = '\0';
131 
132 	printf ("%sDumping exif content (%u entries)...\n", buf,
133 		content->count);
134 	for (i = 0; i < content->count; i++)
135 		exif_entry_dump (content->entries[i], indent + 1);
136 }
137 
138 void
exif_content_add_entry(ExifContent * c,ExifEntry * entry)139 exif_content_add_entry (ExifContent *c, ExifEntry *entry)
140 {
141 	ExifEntry **entries;
142 	if (!c || !c->priv || !entry || entry->parent) return;
143 
144 	/* One tag can only be added once to an IFD. */
145 	if (exif_content_get_entry (c, entry->tag)) {
146 		exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "ExifContent",
147 			"An attempt has been made to add "
148 			"the tag '%s' twice to an IFD. This is against "
149 			"specification.", exif_tag_get_name (entry->tag));
150 		return;
151 	}
152 
153 	entries = exif_mem_realloc (c->priv->mem,
154 		c->entries, sizeof (ExifEntry*) * (c->count + 1));
155 	if (!entries) return;
156 	entry->parent = c;
157 	entries[c->count++] = entry;
158 	c->entries = entries;
159 	exif_entry_ref (entry);
160 }
161 
162 void
exif_content_remove_entry(ExifContent * c,ExifEntry * e)163 exif_content_remove_entry (ExifContent *c, ExifEntry *e)
164 {
165 	unsigned int i;
166 	ExifEntry **t, *temp;
167 
168 	if (!c || !c->priv || !e || (e->parent != c)) return;
169 
170 	/* Search the entry */
171 	for (i = 0; i < c->count; i++)
172 			if (c->entries[i] == e)
173 					break;
174 
175 	if (i == c->count)
176 			return;
177 
178 	/* Remove the entry */
179 	temp = c->entries[c->count-1];
180 	if (c->count > 1) {
181 		t = exif_mem_realloc (c->priv->mem, c->entries,
182 					sizeof(ExifEntry*) * (c->count - 1));
183 		if (!t) {
184 			return;
185 		}
186 		c->entries = t;
187 		c->count--;
188 		if (i != c->count) { /* we deallocated the last slot already */
189 			memmove (&t[i], &t[i + 1], sizeof (ExifEntry*) * (c->count - i - 1));
190 			t[c->count-1] = temp;
191 		}
192 	} else {
193 		exif_mem_free (c->priv->mem, c->entries);
194 		c->entries = NULL;
195 		c->count = 0;
196 	}
197 	e->parent = NULL;
198 	exif_entry_unref (e);
199 }
200 
201 ExifEntry *
exif_content_get_entry(ExifContent * content,ExifTag tag)202 exif_content_get_entry (ExifContent *content, ExifTag tag)
203 {
204 	unsigned int i;
205 
206 	if (!content)
207 		return (NULL);
208 
209 	for (i = 0; i < content->count; i++)
210 		if (content->entries[i]->tag == tag)
211 			return (content->entries[i]);
212 	return (NULL);
213 }
214 
215 void
exif_content_foreach_entry(ExifContent * content,ExifContentForeachEntryFunc func,void * data)216 exif_content_foreach_entry (ExifContent *content,
217 			    ExifContentForeachEntryFunc func, void *data)
218 {
219 	unsigned int i;
220 
221 	if (!content || !func)
222 		return;
223 
224 	for (i = 0; i < content->count; i++)
225 		func (content->entries[i], data);
226 }
227 
228 void
exif_content_log(ExifContent * content,ExifLog * log)229 exif_content_log (ExifContent *content, ExifLog *log)
230 {
231 	if (!content || !content->priv || !log || content->priv->log == log)
232 		return;
233 
234 	if (content->priv->log) exif_log_unref (content->priv->log);
235 	content->priv->log = log;
236 	exif_log_ref (log);
237 }
238 
239 ExifIfd
exif_content_get_ifd(ExifContent * c)240 exif_content_get_ifd (ExifContent *c)
241 {
242 	if (!c || !c->parent) return EXIF_IFD_COUNT;
243 
244 	return
245 		((c)->parent->ifd[EXIF_IFD_EXIF] == (c)) ? EXIF_IFD_EXIF :
246 		((c)->parent->ifd[EXIF_IFD_0] == (c)) ? EXIF_IFD_0 :
247 		((c)->parent->ifd[EXIF_IFD_1] == (c)) ? EXIF_IFD_1 :
248 		((c)->parent->ifd[EXIF_IFD_GPS] == (c)) ? EXIF_IFD_GPS :
249 		((c)->parent->ifd[EXIF_IFD_INTEROPERABILITY] == (c)) ? EXIF_IFD_INTEROPERABILITY :
250 		EXIF_IFD_COUNT;
251 }
252 
253 static void
fix_func(ExifEntry * e,void * UNUSED (data))254 fix_func (ExifEntry *e, void *UNUSED(data))
255 {
256 	exif_entry_fix (e);
257 }
258 
259 /*!
260  * Check if this entry is unknown and if so, delete it.
261  * \note Be careful calling this function in a loop. Deleting an entry from
262  * an ExifContent changes the index of subsequent entries, as well as the
263  * total size of the entries array.
264  */
265 static void
remove_not_recorded(ExifEntry * e,void * UNUSED (data))266 remove_not_recorded (ExifEntry *e, void *UNUSED(data))
267 {
268 	ExifIfd ifd = exif_entry_get_ifd(e) ;
269 	ExifContent *c = e->parent;
270 	ExifDataType dt = exif_data_get_data_type (c->parent);
271 	ExifTag t = e->tag;
272 
273 	if (exif_tag_get_support_level_in_ifd (t, ifd, dt) ==
274 			 EXIF_SUPPORT_LEVEL_NOT_RECORDED) {
275 		exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "exif-content",
276 				"Tag 0x%04x is not recorded in IFD '%s' and has therefore been "
277 				"removed.", t, exif_ifd_get_name (ifd));
278 		exif_content_remove_entry (c, e);
279 	}
280 
281 }
282 
283 void
exif_content_fix(ExifContent * c)284 exif_content_fix (ExifContent *c)
285 {
286 	ExifIfd ifd = exif_content_get_ifd (c);
287 	ExifDataType dt;
288 	ExifEntry *e;
289 	unsigned int i, num;
290 
291 	if (!c)
292 		return;
293 
294 	dt = exif_data_get_data_type (c->parent);
295 
296 	/*
297 	 * First of all, fix all existing entries.
298 	 */
299 	exif_content_foreach_entry (c, fix_func, NULL);
300 
301 	/*
302 	 * Go through each tag and if it's not recorded, remove it. If one
303 	 * is removed, exif_content_foreach_entry() will skip the next entry,
304 	 * so if this happens do the loop again from the beginning to ensure
305 	 * they're all checked. This could be avoided if we stop relying on
306 	 * exif_content_foreach_entry but loop intelligently here.
307 	 */
308 	do {
309 		num = c->count;
310 		exif_content_foreach_entry (c, remove_not_recorded, NULL);
311 	} while (num != c->count);
312 
313 	/*
314 	 * Then check for non-existing mandatory tags and create them if needed
315 	 */
316 	num = exif_tag_table_count();
317 	for (i = 0; i < num; ++i) {
318 		const ExifTag t = exif_tag_table_get_tag (i);
319 		if (exif_tag_get_support_level_in_ifd (t, ifd, dt) ==
320 			EXIF_SUPPORT_LEVEL_MANDATORY) {
321 			if (exif_content_get_entry (c, t))
322 				/* This tag already exists */
323 				continue;
324 			exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "exif-content",
325 					"Tag '%s' is mandatory in IFD '%s' and has therefore been added.",
326 					exif_tag_get_name_in_ifd (t, ifd), exif_ifd_get_name (ifd));
327 			e = exif_entry_new ();
328 			exif_content_add_entry (c, e);
329 			exif_entry_initialize (e, t);
330 			exif_entry_unref (e);
331 		}
332 	}
333 }
334