• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* exif-entry.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-entry.h>
24 #include <libexif/exif-ifd.h>
25 #include <libexif/exif-utils.h>
26 #include <libexif/i18n.h>
27 
28 #include <ctype.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <time.h>
33 #include <math.h>
34 
35 #ifndef M_PI
36 #define M_PI 3.14159265358979323846
37 #endif
38 
39 struct _ExifEntryPrivate
40 {
41 	unsigned int ref_count;
42 
43 	ExifMem *mem;
44 };
45 
46 /* This function is hidden in exif-data.c */
47 ExifLog *exif_data_get_log (ExifData *);
48 
49 #ifndef NO_VERBOSE_TAG_STRINGS
50 static void
exif_entry_log(ExifEntry * e,ExifLogCode code,const char * format,...)51 exif_entry_log (ExifEntry *e, ExifLogCode code, const char *format, ...)
52 {
53 	va_list args;
54 	ExifLog *l = NULL;
55 
56 	if (e && e->parent && e->parent->parent)
57 		l = exif_data_get_log (e->parent->parent);
58 	va_start (args, format);
59 	exif_logv (l, code, "ExifEntry", format, args);
60 	va_end (args);
61 }
62 #else
63 #if defined(__STDC_VERSION__) &&  __STDC_VERSION__ >= 199901L
64 #define exif_entry_log(...) do { } while (0)
65 #elif defined(__GNUC__)
66 #define exif_entry_log(x...) do { } while (0)
67 #else
68 #define exif_entry_log (void)
69 #endif
70 #endif
71 
72 static void *
exif_entry_alloc(ExifEntry * e,unsigned int i)73 exif_entry_alloc (ExifEntry *e, unsigned int i)
74 {
75 	void *d;
76 	ExifLog *l = NULL;
77 
78 	if (!e || !e->priv || !i) return NULL;
79 
80 	d = exif_mem_alloc (e->priv->mem, i);
81 	if (d) return d;
82 
83 	if (e->parent && e->parent->parent)
84 		l = exif_data_get_log (e->parent->parent);
85 	EXIF_LOG_NO_MEMORY (l, "ExifEntry", i);
86 	return NULL;
87 }
88 
89 static void *
exif_entry_realloc(ExifEntry * e,void * d_orig,unsigned int i)90 exif_entry_realloc (ExifEntry *e, void *d_orig, unsigned int i)
91 {
92 	void *d;
93 	ExifLog *l = NULL;
94 
95 	if (!e || !e->priv) return NULL;
96 
97 	if (!i) { exif_mem_free (e->priv->mem, d_orig); return NULL; }
98 
99 	d = exif_mem_realloc (e->priv->mem, d_orig, i);
100 	if (d) return d;
101 
102 	if (e->parent && e->parent->parent)
103 		l = exif_data_get_log (e->parent->parent);
104 	EXIF_LOG_NO_MEMORY (l, "ExifEntry", i);
105 	return NULL;
106 }
107 
108 ExifEntry *
exif_entry_new(void)109 exif_entry_new (void)
110 {
111 	ExifMem *mem = exif_mem_new_default ();
112 	ExifEntry *e = exif_entry_new_mem (mem);
113 
114 	exif_mem_unref (mem);
115 
116 	return e;
117 }
118 
119 ExifEntry *
exif_entry_new_mem(ExifMem * mem)120 exif_entry_new_mem (ExifMem *mem)
121 {
122 	ExifEntry *e = NULL;
123 
124 	e = exif_mem_alloc (mem, sizeof (ExifEntry));
125 	if (!e) return NULL;
126 	e->priv = exif_mem_alloc (mem, sizeof (ExifEntryPrivate));
127 	if (!e->priv) { exif_mem_free (mem, e); return NULL; }
128 	e->priv->ref_count = 1;
129 
130 	e->priv->mem = mem;
131 	exif_mem_ref (mem);
132 
133 	return e;
134 }
135 
136 void
exif_entry_ref(ExifEntry * e)137 exif_entry_ref (ExifEntry *e)
138 {
139 	if (!e) return;
140 
141 	e->priv->ref_count++;
142 }
143 
144 void
exif_entry_unref(ExifEntry * e)145 exif_entry_unref (ExifEntry *e)
146 {
147 	if (!e) return;
148 
149 	e->priv->ref_count--;
150 	if (!e->priv->ref_count)
151 		exif_entry_free (e);
152 }
153 
154 void
exif_entry_free(ExifEntry * e)155 exif_entry_free (ExifEntry *e)
156 {
157 	if (!e) return;
158 
159 	if (e->priv) {
160 		ExifMem *mem = e->priv->mem;
161 		if (e->data)
162 			exif_mem_free (mem, e->data);
163 		exif_mem_free (mem, e->priv);
164 		exif_mem_free (mem, e);
165 		exif_mem_unref (mem);
166 	}
167 }
168 
169 static void
clear_entry(ExifEntry * e)170 clear_entry (ExifEntry *e)
171 {
172 	e->components = 0;
173 	e->size = 0;
174 }
175 
176 /*! Get a value and convert it to an ExifShort.
177  * \bug Not all types are converted that could be converted and no indication
178  *      is made when that occurs
179  */
180 static inline ExifShort
exif_get_short_convert(const unsigned char * buf,ExifFormat format,ExifByteOrder order)181 exif_get_short_convert (const unsigned char *buf, ExifFormat format,
182 			ExifByteOrder order)
183 {
184 	switch (format) {
185 	case EXIF_FORMAT_LONG:
186 		return (ExifShort) exif_get_long (buf, order);
187 	case EXIF_FORMAT_SLONG:
188 		return (ExifShort) exif_get_slong (buf, order);
189 	case EXIF_FORMAT_SHORT:
190 		return (ExifShort) exif_get_short (buf, order);
191 	case EXIF_FORMAT_SSHORT:
192 		return (ExifShort) exif_get_sshort (buf, order);
193 	case EXIF_FORMAT_BYTE:
194 	case EXIF_FORMAT_SBYTE:
195 		return (ExifShort) buf[0];
196 	default:
197 		/* Unsupported type */
198 		return (ExifShort) 0;
199 	}
200 }
201 
202 void
exif_entry_fix(ExifEntry * e)203 exif_entry_fix (ExifEntry *e)
204 {
205 	unsigned int i, newsize;
206 	unsigned char *newdata;
207 	ExifByteOrder o;
208 	ExifRational r;
209 	ExifSRational sr;
210 
211 	if (!e || !e->priv) return;
212 
213 	switch (e->tag) {
214 
215 	/* These tags all need to be of format SHORT. */
216 	case EXIF_TAG_YCBCR_SUB_SAMPLING:
217 	case EXIF_TAG_SUBJECT_AREA:
218 	case EXIF_TAG_COLOR_SPACE:
219 	case EXIF_TAG_PLANAR_CONFIGURATION:
220 	case EXIF_TAG_SENSING_METHOD:
221 	case EXIF_TAG_ORIENTATION:
222 	case EXIF_TAG_YCBCR_POSITIONING:
223 	case EXIF_TAG_PHOTOMETRIC_INTERPRETATION:
224 	case EXIF_TAG_CUSTOM_RENDERED:
225 	case EXIF_TAG_EXPOSURE_MODE:
226 	case EXIF_TAG_WHITE_BALANCE:
227 	case EXIF_TAG_SCENE_CAPTURE_TYPE:
228 	case EXIF_TAG_GAIN_CONTROL:
229 	case EXIF_TAG_SATURATION:
230 	case EXIF_TAG_CONTRAST:
231 	case EXIF_TAG_SHARPNESS:
232 	case EXIF_TAG_ISO_SPEED_RATINGS:
233 		switch (e->format) {
234 		case EXIF_FORMAT_LONG:
235 		case EXIF_FORMAT_SLONG:
236 		case EXIF_FORMAT_BYTE:
237 		case EXIF_FORMAT_SBYTE:
238 		case EXIF_FORMAT_SSHORT:
239 			if (!e->parent || !e->parent->parent) break;
240 			exif_entry_log (e, EXIF_LOG_CODE_DEBUG,
241 				_("Tag '%s' was of format '%s' (which is "
242 				"against specification) and has been "
243 				"changed to format '%s'."),
244 				exif_tag_get_name_in_ifd(e->tag,
245 							exif_entry_get_ifd(e)),
246 				exif_format_get_name (e->format),
247 				exif_format_get_name (EXIF_FORMAT_SHORT));
248 
249 			o = exif_data_get_byte_order (e->parent->parent);
250 			newsize = e->components * exif_format_get_size (EXIF_FORMAT_SHORT);
251 			newdata = exif_entry_alloc (e, newsize);
252 			if (!newdata) {
253 				exif_entry_log (e, EXIF_LOG_CODE_NO_MEMORY,
254 					"Could not allocate %lu byte(s).", (unsigned long)newsize);
255 				break;
256 			}
257 
258 			for (i = 0; i < e->components; i++)
259 				exif_set_short (
260 					newdata + i *
261 					exif_format_get_size (
262 					 EXIF_FORMAT_SHORT), o,
263 					 exif_get_short_convert (
264 					  e->data + i *
265 					  exif_format_get_size (e->format),
266 					  e->format, o));
267 
268 			exif_mem_free (e->priv->mem, e->data);
269 			e->data = newdata;
270 			e->size = newsize;
271 			e->format = EXIF_FORMAT_SHORT;
272 			break;
273 		case EXIF_FORMAT_SHORT:
274 			/* No conversion necessary */
275 			break;
276 		default:
277 			exif_entry_log (e, EXIF_LOG_CODE_CORRUPT_DATA,
278 				_("Tag '%s' is of format '%s' (which is "
279 				"against specification) but cannot be changed "
280 				"to format '%s'."),
281 				exif_tag_get_name_in_ifd(e->tag,
282 							exif_entry_get_ifd(e)),
283 				exif_format_get_name (e->format),
284 				exif_format_get_name (EXIF_FORMAT_SHORT));
285 			break;
286 		}
287 		break;
288 
289 	/* All these tags need to be of format 'Rational'. */
290 	case EXIF_TAG_FNUMBER:
291 	case EXIF_TAG_APERTURE_VALUE:
292 	case EXIF_TAG_EXPOSURE_TIME:
293 	case EXIF_TAG_FOCAL_LENGTH:
294 		switch (e->format) {
295 		case EXIF_FORMAT_SRATIONAL:
296 			if (!e->parent || !e->parent->parent) break;
297 			o = exif_data_get_byte_order (e->parent->parent);
298 			for (i = 0; i < e->components; i++) {
299 				sr = exif_get_srational (e->data + i *
300 					exif_format_get_size (
301 						EXIF_FORMAT_SRATIONAL), o);
302 				r.numerator = (ExifLong) sr.numerator;
303 				r.denominator = (ExifLong) sr.denominator;
304 				exif_set_rational (e->data + i *
305 					exif_format_get_size (
306 						EXIF_FORMAT_RATIONAL), o, r);
307 			}
308 			e->format = EXIF_FORMAT_RATIONAL;
309 			exif_entry_log (e, EXIF_LOG_CODE_DEBUG,
310 				_("Tag '%s' was of format '%s' (which is "
311 				"against specification) and has been "
312 				"changed to format '%s'."),
313 				exif_tag_get_name_in_ifd(e->tag,
314 							exif_entry_get_ifd(e)),
315 				exif_format_get_name (EXIF_FORMAT_SRATIONAL),
316 				exif_format_get_name (EXIF_FORMAT_RATIONAL));
317 			break;
318 		default:
319 			break;
320 		}
321 		break;
322 
323 	/* All these tags need to be of format 'SRational'. */
324 	case EXIF_TAG_EXPOSURE_BIAS_VALUE:
325 	case EXIF_TAG_BRIGHTNESS_VALUE:
326 	case EXIF_TAG_SHUTTER_SPEED_VALUE:
327 		switch (e->format) {
328 		case EXIF_FORMAT_RATIONAL:
329 			if (!e->parent || !e->parent->parent) break;
330 			o = exif_data_get_byte_order (e->parent->parent);
331 			for (i = 0; i < e->components; i++) {
332 				r = exif_get_rational (e->data + i *
333 					exif_format_get_size (
334 						EXIF_FORMAT_RATIONAL), o);
335 				sr.numerator = (ExifLong) r.numerator;
336 				sr.denominator = (ExifLong) r.denominator;
337 				exif_set_srational (e->data + i *
338 					exif_format_get_size (
339 						EXIF_FORMAT_SRATIONAL), o, sr);
340 			}
341 			e->format = EXIF_FORMAT_SRATIONAL;
342 			exif_entry_log (e, EXIF_LOG_CODE_DEBUG,
343 				_("Tag '%s' was of format '%s' (which is "
344 				"against specification) and has been "
345 				"changed to format '%s'."),
346 				exif_tag_get_name_in_ifd(e->tag,
347 							exif_entry_get_ifd(e)),
348 				exif_format_get_name (EXIF_FORMAT_RATIONAL),
349 				exif_format_get_name (EXIF_FORMAT_SRATIONAL));
350 			break;
351 		default:
352 			break;
353 		}
354 		break;
355 
356 	case EXIF_TAG_USER_COMMENT:
357 
358 		/* Format needs to be UNDEFINED. */
359 		if (e->format != EXIF_FORMAT_UNDEFINED) {
360 			exif_entry_log (e, EXIF_LOG_CODE_DEBUG,
361 				_("Tag 'UserComment' had invalid format '%s'. "
362 				"Format has been set to 'undefined'."),
363 				exif_format_get_name (e->format));
364 			e->format = EXIF_FORMAT_UNDEFINED;
365 		}
366 
367 		/* Some packages like Canon ZoomBrowser EX 4.5 store
368 		   only one zero byte followed by 7 bytes of rubbish */
369 		if ((e->size >= 8) && (e->data[0] == 0)) {
370 			memcpy(e->data, "\0\0\0\0\0\0\0\0", 8);
371 		}
372 
373 		/* There need to be at least 8 bytes. */
374 		if (e->size < 8) {
375 			e->data = exif_entry_realloc (e, e->data, 8 + e->size);
376 			if (!e->data) {
377 				clear_entry(e);
378 				return;
379 			}
380 
381 			/* Assume ASCII */
382 			memmove (e->data + 8, e->data, e->size);
383 			memcpy (e->data, "ASCII\0\0\0", 8);
384 			e->size += 8;
385 			e->components += 8;
386 			exif_entry_log (e, EXIF_LOG_CODE_DEBUG,
387 				_("Tag 'UserComment' has been expanded to at "
388 				"least 8 bytes in order to follow the "
389 				"specification."));
390 			break;
391 		}
392 
393 		/*
394 		 * If the first 8 bytes are empty and real data starts
395 		 * afterwards, let's assume ASCII and claim the 8 first
396 		 * bytes for the format specifyer.
397 		 */
398 		for (i = 0; (i < e->size) && !e->data[i]; i++);
399 		if (!i) for ( ; (i < e->size) && (e->data[i] == ' '); i++);
400 		if ((i >= 8) && (i < e->size)) {
401 			exif_entry_log (e, EXIF_LOG_CODE_DEBUG,
402 				_("Tag 'UserComment' is not empty but does not "
403 				"start with a format identifier. "
404 				"This has been fixed."));
405 			memcpy (e->data, "ASCII\0\0\0", 8);
406 			break;
407 		}
408 
409 		/*
410 		 * First 8 bytes need to follow the specification. If they don't,
411 		 * assume ASCII.
412 		 */
413 		if (memcmp (e->data, "ASCII\0\0\0"     , 8) &&
414 		    memcmp (e->data, "UNICODE\0"       , 8) &&
415 		    memcmp (e->data, "JIS\0\0\0\0\0"   , 8) &&
416 		    memcmp (e->data, "\0\0\0\0\0\0\0\0", 8)) {
417 			e->data = exif_entry_realloc (e, e->data, 8 + e->size);
418 			if (!e->data) {
419 				clear_entry(e);
420 				break;
421 			}
422 
423 			/* Assume ASCII */
424 			memmove (e->data + 8, e->data, e->size);
425 			memcpy (e->data, "ASCII\0\0\0", 8);
426 			e->size += 8;
427 			e->components += 8;
428 			exif_entry_log (e, EXIF_LOG_CODE_DEBUG,
429 				_("Tag 'UserComment' did not start with a "
430 				"format identifier. This has been fixed."));
431 			break;
432 		}
433 
434 		break;
435 	default:
436 		break;
437 	}
438 }
439 
440 /*! Format the value of an ExifEntry for human display in a generic way.
441  * The output is localized. The formatting is independent of the tag number
442  * and is based entirely on the data type.
443  * \pre The ExifEntry is already a member of an ExifData.
444  * \param[in] e EXIF entry
445  * \param[out] val buffer in which to store value
446  * \param[in] maxlen the length of the buffer val
447  */
448 static void
exif_entry_format_value(ExifEntry * e,char * val,size_t maxlen)449 exif_entry_format_value(ExifEntry *e, char *val, size_t maxlen)
450 {
451 	ExifByte v_byte;
452 	ExifShort v_short;
453 	ExifSShort v_sshort;
454 	ExifLong v_long;
455 	ExifRational v_rat;
456 	ExifSRational v_srat;
457 	ExifSLong v_slong;
458 	unsigned int i;
459 	size_t len;
460 	const ExifByteOrder o = exif_data_get_byte_order (e->parent->parent);
461 
462 	if (!e->size || !maxlen)
463 		return;
464 	switch (e->format) {
465 	case EXIF_FORMAT_UNDEFINED:
466 		snprintf (val, maxlen, _("%i bytes undefined data"), e->size);
467 		break;
468 	case EXIF_FORMAT_BYTE:
469 	case EXIF_FORMAT_SBYTE:
470 		v_byte = e->data[0];
471 		snprintf (val, maxlen, "0x%02x", v_byte);
472 		len = strlen (val);
473 		for (i = 1; i < e->components; i++) {
474 			v_byte = e->data[i];
475 			snprintf (val+len, maxlen-len, ", 0x%02x", v_byte);
476 			len += strlen (val+len);
477 			if (len >= maxlen-1) break;
478 		}
479 		break;
480 	case EXIF_FORMAT_SHORT:
481 		v_short = exif_get_short (e->data, o);
482 		snprintf (val, maxlen, "%u", v_short);
483 		len = strlen (val);
484 		for (i = 1; i < e->components; i++) {
485 			v_short = exif_get_short (e->data +
486 				exif_format_get_size (e->format) * i, o);
487 			snprintf (val+len, maxlen-len, ", %u", v_short);
488 			len += strlen (val+len);
489 			if (len >= maxlen-1) break;
490 		}
491 		break;
492 	case EXIF_FORMAT_SSHORT:
493 		v_sshort = exif_get_sshort (e->data, o);
494 		snprintf (val, maxlen, "%i", v_sshort);
495 		len = strlen (val);
496 		for (i = 1; i < e->components; i++) {
497 			v_sshort = exif_get_short (e->data +
498 				exif_format_get_size (e->format) *
499 				i, o);
500 			snprintf (val+len, maxlen-len, ", %i", v_sshort);
501 			len += strlen (val+len);
502 			if (len >= maxlen-1) break;
503 		}
504 		break;
505 	case EXIF_FORMAT_LONG:
506 		v_long = exif_get_long (e->data, o);
507 		snprintf (val, maxlen, "%lu", (unsigned long) v_long);
508 		len = strlen (val);
509 		for (i = 1; i < e->components; i++) {
510 			v_long = exif_get_long (e->data +
511 				exif_format_get_size (e->format) *
512 				i, o);
513 			snprintf (val+len, maxlen-len, ", %lu", (unsigned long) v_long);
514 			len += strlen (val+len);
515 			if (len >= maxlen-1) break;
516 		}
517 		break;
518 	case EXIF_FORMAT_SLONG:
519 		v_slong = exif_get_slong (e->data, o);
520 		snprintf (val, maxlen, "%li", (long) v_slong);
521 		len = strlen (val);
522 		for (i = 1; i < e->components; i++) {
523 			v_slong = exif_get_slong (e->data +
524 				exif_format_get_size (e->format) * i, o);
525 			snprintf (val+len, maxlen-len, ", %li", (long) v_slong);
526 			len += strlen (val+len);
527 			if (len >= maxlen-1) break;
528 		}
529 		break;
530 	case EXIF_FORMAT_ASCII:
531 		strncpy (val, (char *) e->data, MIN (maxlen-1, e->size));
532 		val[MIN (maxlen-1, e->size)] = 0;
533 		break;
534 	case EXIF_FORMAT_RATIONAL:
535 		len = 0;
536 		for (i = 0; i < e->components; i++) {
537 			if (i > 0) {
538 				snprintf (val+len, maxlen-len, ", ");
539 				len += strlen (val+len);
540 			}
541 			v_rat = exif_get_rational (
542 				e->data + 8 * i, o);
543 			if (v_rat.denominator) {
544 				/*
545 				 * Choose the number of significant digits to
546 				 * display based on the size of the denominator.
547 				 * It is scaled so that denominators within the
548 				 * range 13..120 will show 2 decimal points.
549 				 */
550 				int decimals = (int)(log10(v_rat.denominator)-0.08+1.0);
551 				snprintf (val+len, maxlen-len, "%2.*f",
552 					  decimals,
553 					  (double) v_rat.numerator /
554 					  (double) v_rat.denominator);
555 			} else
556 				snprintf (val+len, maxlen-len, "%lu/%lu",
557 				  (unsigned long) v_rat.numerator,
558 				  (unsigned long) v_rat.denominator);
559 			len += strlen (val+len);
560 			if (len >= maxlen-1) break;
561 		}
562 		break;
563 	case EXIF_FORMAT_SRATIONAL:
564 		len = 0;
565 		for (i = 0; i < e->components; i++) {
566 			if (i > 0) {
567 				snprintf (val+len, maxlen-len, ", ");
568 				len += strlen (val+len);
569 			}
570 			v_srat = exif_get_srational (
571 				e->data + 8 * i, o);
572 			if (v_srat.denominator) {
573 				int decimals = (int)(log10(abs(v_srat.denominator))-0.08+1.0);
574 				snprintf (val+len, maxlen-len, "%2.*f",
575 					  decimals,
576 					  (double) v_srat.numerator /
577 					  (double) v_srat.denominator);
578 			} else
579 				snprintf (val+len, maxlen-len, "%li/%li",
580 				  (long) v_srat.numerator,
581 				  (long) v_srat.denominator);
582 			len += strlen (val+len);
583 			if (len >= maxlen-1) break;
584 		}
585 		break;
586 	case EXIF_FORMAT_DOUBLE:
587 	case EXIF_FORMAT_FLOAT:
588 	default:
589 		snprintf (val, maxlen, _("%i bytes unsupported data type"),
590 			  e->size);
591 		break;
592 	}
593 }
594 
595 void
exif_entry_dump(ExifEntry * e,unsigned int indent)596 exif_entry_dump (ExifEntry *e, unsigned int indent)
597 {
598 	char buf[1024];
599 	char value[1024];
600 	unsigned int l;
601 
602 	if (!e)
603 		return;
604 
605 	l = MIN(sizeof(buf)-1, 2*indent);
606 	memset(buf, ' ', l);
607 	buf[l] = '\0';
608 
609 	printf ("%sTag: 0x%x ('%s')\n", buf, e->tag,
610 		exif_tag_get_name_in_ifd (e->tag, exif_entry_get_ifd(e)));
611 	printf ("%s  Format: %i ('%s')\n", buf, e->format,
612 		exif_format_get_name (e->format));
613 	printf ("%s  Components: %i\n", buf, (int) e->components);
614 	printf ("%s  Size: %i\n", buf, e->size);
615 	printf ("%s  Value: %s\n", buf, exif_entry_get_value (e, value, sizeof(value)));
616 }
617 
618 /*! Check if a string consists entirely of a single, repeated character.
619  * Up to first n bytes are checked.
620  *
621  * \param[in] data pointer of string to check
622  * \param[in] ch character to match
623  * \param[in] n maximum number of characters to match
624  *
625  * \return 0 if the string matches or is of zero length, nonzero otherwise
626  */
627 static int
match_repeated_char(const unsigned char * data,unsigned char ch,size_t n)628 match_repeated_char(const unsigned char *data, unsigned char ch, size_t n)
629 {
630 	int i;
631 	for (i=n; i; --i, ++data) {
632 		if (*data == 0) {
633 			i = 0;	/* all bytes before NUL matched */
634 			break;
635 		}
636 		if (*data != ch)
637 			break;
638 	}
639 	return i;
640 }
641 
642 #define CF(entry,target,v,maxlen)					\
643 {									\
644 	if (entry->format != target) {					\
645 		exif_entry_log (entry, EXIF_LOG_CODE_CORRUPT_DATA,	\
646 			_("The tag '%s' contains data of an invalid "	\
647 			"format ('%s', expected '%s')."),		\
648 			exif_tag_get_name (entry->tag),			\
649 			exif_format_get_name (entry->format),		\
650 			exif_format_get_name (target));			\
651 		break;							\
652 	}								\
653 }
654 
655 #define CC(entry,target,v,maxlen)					\
656 {									\
657 	if (entry->components != target) {				\
658 		exif_entry_log (entry, EXIF_LOG_CODE_CORRUPT_DATA,	\
659 			_("The tag '%s' contains an invalid number of "	\
660 			  "components (%i, expected %i)."),		\
661 			exif_tag_get_name (entry->tag),		\
662 			(int) entry->components, (int) target);		\
663 		break;							\
664 	}								\
665 }
666 
667 static const struct {
668 	ExifTag tag;
669 	const char *strings[10];
670 } list[] = {
671 #ifndef NO_VERBOSE_TAG_DATA
672   { EXIF_TAG_PLANAR_CONFIGURATION,
673     { N_("Chunky format"), N_("Planar format"), NULL}},
674   { EXIF_TAG_SENSING_METHOD,
675     { "", N_("Not defined"), N_("One-chip color area sensor"),
676       N_("Two-chip color area sensor"), N_("Three-chip color area sensor"),
677       N_("Color sequential area sensor"), "", N_("Trilinear sensor"),
678       N_("Color sequential linear sensor"), NULL}},
679   { EXIF_TAG_ORIENTATION,
680     { "", N_("Top-left"), N_("Top-right"), N_("Bottom-right"),
681       N_("Bottom-left"), N_("Left-top"), N_("Right-top"),
682       N_("Right-bottom"), N_("Left-bottom"), NULL}},
683   { EXIF_TAG_YCBCR_POSITIONING,
684     { "", N_("Centered"), N_("Co-sited"), NULL}},
685   { EXIF_TAG_PHOTOMETRIC_INTERPRETATION,
686     { N_("Reversed mono"), N_("Normal mono"), N_("RGB"), N_("Palette"), "",
687       N_("CMYK"), N_("YCbCr"), "", N_("CieLAB"), NULL}},
688   { EXIF_TAG_CUSTOM_RENDERED,
689     { N_("Normal process"), N_("Custom process"), NULL}},
690   { EXIF_TAG_EXPOSURE_MODE,
691     { N_("Auto exposure"), N_("Manual exposure"), N_("Auto bracket"), NULL}},
692   { EXIF_TAG_WHITE_BALANCE,
693     { N_("Auto white balance"), N_("Manual white balance"), NULL}},
694   { EXIF_TAG_SCENE_CAPTURE_TYPE,
695     { N_("Standard"), N_("Landscape"), N_("Portrait"),
696       N_("Night scene"), NULL}},
697   { EXIF_TAG_GAIN_CONTROL,
698     { N_("Normal"), N_("Low gain up"), N_("High gain up"),
699       N_("Low gain down"), N_("High gain down"), NULL}},
700   { EXIF_TAG_SATURATION,
701     { N_("Normal"), N_("Low saturation"), N_("High saturation"), NULL}},
702   { EXIF_TAG_CONTRAST , {N_("Normal"), N_("Soft"), N_("Hard"), NULL}},
703   { EXIF_TAG_SHARPNESS, {N_("Normal"), N_("Soft"), N_("Hard"), NULL}},
704 #endif
705   { 0, {NULL}}
706 };
707 
708 static const struct {
709   ExifTag tag;
710   struct {
711     int index;
712     const char *values[4]; /*!< list of progressively shorter string
713 			    descriptions; the longest one that fits will be
714 			    selected */
715   } elem[25];
716 } list2[] = {
717 #ifndef NO_VERBOSE_TAG_DATA
718   { EXIF_TAG_METERING_MODE,
719     { {  0, {N_("Unknown"), NULL}},
720       {  1, {N_("Average"), N_("Avg"), NULL}},
721       {  2, {N_("Center-weighted average"), N_("Center-weight"), NULL}},
722       {  3, {N_("Spot"), NULL}},
723       {  4, {N_("Multi spot"), NULL}},
724       {  5, {N_("Pattern"), NULL}},
725       {  6, {N_("Partial"), NULL}},
726       {255, {N_("Other"), NULL}},
727       {  0, {NULL}}}},
728   { EXIF_TAG_COMPRESSION,
729     { {1, {N_("Uncompressed"), NULL}},
730       {5, {N_("LZW compression"), NULL}},
731       {6, {N_("JPEG compression"), NULL}},
732       {7, {N_("JPEG compression"), NULL}},
733       {8, {N_("Deflate/ZIP compression"), NULL}},
734       {32773, {N_("PackBits compression"), NULL}},
735       {0, {NULL}}}},
736   { EXIF_TAG_LIGHT_SOURCE,
737     { {  0, {N_("Unknown"), NULL}},
738       {  1, {N_("Daylight"), NULL}},
739       {  2, {N_("Fluorescent"), NULL}},
740       {  3, {N_("Tungsten incandescent light"), N_("Tungsten"), NULL}},
741       {  4, {N_("Flash"), NULL}},
742       {  9, {N_("Fine weather"), NULL}},
743       { 10, {N_("Cloudy weather"), N_("Cloudy"), NULL}},
744       { 11, {N_("Shade"), NULL}},
745       { 12, {N_("Daylight fluorescent"), NULL}},
746       { 13, {N_("Day white fluorescent"), NULL}},
747       { 14, {N_("Cool white fluorescent"), NULL}},
748       { 15, {N_("White fluorescent"), NULL}},
749       { 17, {N_("Standard light A"), NULL}},
750       { 18, {N_("Standard light B"), NULL}},
751       { 19, {N_("Standard light C"), NULL}},
752       { 20, {N_("D55"), NULL}},
753       { 21, {N_("D65"), NULL}},
754       { 22, {N_("D75"), NULL}},
755       { 24, {N_("ISO studio tungsten"),NULL}},
756       {255, {N_("Other"), NULL}},
757       {  0, {NULL}}}},
758   { EXIF_TAG_FOCAL_PLANE_RESOLUTION_UNIT,
759     { {2, {N_("Inch"), N_("in"), NULL}},
760       {3, {N_("Centimeter"), N_("cm"), NULL}},
761       {0, {NULL}}}},
762   { EXIF_TAG_RESOLUTION_UNIT,
763     { {2, {N_("Inch"), N_("in"), NULL}},
764       {3, {N_("Centimeter"), N_("cm"), NULL}},
765       {0, {NULL}}}},
766   { EXIF_TAG_EXPOSURE_PROGRAM,
767     { {0, {N_("Not defined"), NULL}},
768       {1, {N_("Manual"), NULL}},
769       {2, {N_("Normal program"), N_("Normal"), NULL}},
770       {3, {N_("Aperture priority"), N_("Aperture"), NULL}},
771       {4, {N_("Shutter priority"),N_("Shutter"), NULL}},
772       {5, {N_("Creative program (biased toward depth of field)"),
773 	   N_("Creative"), NULL}},
774       {6, {N_("Creative program (biased toward fast shutter speed)"),
775 	   N_("Action"), NULL}},
776       {7, {N_("Portrait mode (for closeup photos with the background out "
777 	      "of focus)"), N_("Portrait"), NULL}},
778       {8, {N_("Landscape mode (for landscape photos with the background "
779 	      "in focus)"), N_("Landscape"), NULL}},
780       {0, {NULL}}}},
781   { EXIF_TAG_FLASH,
782     { {0x0000, {N_("Flash did not fire"), N_("No flash"), NULL}},
783       {0x0001, {N_("Flash fired"), N_("Flash"), N_("Yes"), NULL}},
784       {0x0005, {N_("Strobe return light not detected"), N_("Without strobe"),
785 		NULL}},
786       {0x0007, {N_("Strobe return light detected"), N_("With strobe"), NULL}},
787       {0x0008, {N_("Flash did not fire"), NULL}}, /* Olympus E-330 */
788       {0x0009, {N_("Flash fired, compulsory flash mode"), NULL}},
789       {0x000d, {N_("Flash fired, compulsory flash mode, return light "
790 		   "not detected"), NULL}},
791       {0x000f, {N_("Flash fired, compulsory flash mode, return light "
792 		   "detected"), NULL}},
793       {0x0010, {N_("Flash did not fire, compulsory flash mode"), NULL}},
794       {0x0018, {N_("Flash did not fire, auto mode"), NULL}},
795       {0x0019, {N_("Flash fired, auto mode"), NULL}},
796       {0x001d, {N_("Flash fired, auto mode, return light not detected"),
797 		NULL}},
798       {0x001f, {N_("Flash fired, auto mode, return light detected"), NULL}},
799       {0x0020, {N_("No flash function"),NULL}},
800       {0x0041, {N_("Flash fired, red-eye reduction mode"), NULL}},
801       {0x0045, {N_("Flash fired, red-eye reduction mode, return light "
802 		   "not detected"), NULL}},
803       {0x0047, {N_("Flash fired, red-eye reduction mode, return light "
804 		   "detected"), NULL}},
805       {0x0049, {N_("Flash fired, compulsory flash mode, red-eye reduction "
806 		   "mode"), NULL}},
807       {0x004d, {N_("Flash fired, compulsory flash mode, red-eye reduction "
808 		  "mode, return light not detected"), NULL}},
809       {0x004f, {N_("Flash fired, compulsory flash mode, red-eye reduction mode, "
810 		   "return light detected"), NULL}},
811       {0x0058, {N_("Flash did not fire, auto mode, red-eye reduction mode"), NULL}},
812       {0x0059, {N_("Flash fired, auto mode, red-eye reduction mode"), NULL}},
813       {0x005d, {N_("Flash fired, auto mode, return light not detected, "
814 		   "red-eye reduction mode"), NULL}},
815       {0x005f, {N_("Flash fired, auto mode, return light detected, "
816 		   "red-eye reduction mode"), NULL}},
817       {0x0000, {NULL}}}},
818   { EXIF_TAG_SUBJECT_DISTANCE_RANGE,
819     { {0, {N_("Unknown"), N_("?"), NULL}},
820       {1, {N_("Macro"), NULL}},
821       {2, {N_("Close view"), N_("Close"), NULL}},
822       {3, {N_("Distant view"), N_("Distant"), NULL}},
823       {0, {NULL}}}},
824   { EXIF_TAG_COLOR_SPACE,
825     { {1, {N_("sRGB"), NULL}},
826       {2, {N_("Adobe RGB"), NULL}},
827       {0xffff, {N_("Uncalibrated"), NULL}},
828       {0x0000, {NULL}}}},
829 #endif
830   {0, { { 0, {NULL}}} }
831 };
832 
833 const char *
exif_entry_get_value(ExifEntry * e,char * val,unsigned int maxlen)834 exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
835 {
836 	unsigned int i, j, k;
837 	ExifShort v_short, v_short2, v_short3, v_short4;
838 	ExifByte v_byte;
839 	ExifRational v_rat;
840 	ExifSRational v_srat;
841 	char b[64];
842 	const char *c;
843 	ExifByteOrder o;
844 	double d;
845 	ExifEntry *entry;
846 	static const struct {
847 		char label[5];
848 		char major, minor;
849 	} versions[] = {
850 		{"0110", 1,  1},
851 		{"0120", 1,  2},
852 		{"0200", 2,  0},
853 		{"0210", 2,  1},
854 		{"0220", 2,  2},
855 		{"0221", 2, 21},
856 		{"0230", 2,  3},
857 		{""    , 0,  0}
858 	};
859 
860 	(void) bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
861 
862 	if (!e || !e->parent || !e->parent->parent || !maxlen || !val)
863 		return val;
864 
865 	/* make sure the returned string is zero terminated */
866 	/* FIXME: this is inefficient in the case of long buffers and should
867 	 * instead be taken care of on each write instead. */
868 	memset (val, 0, maxlen);
869 
870 	/* We need the byte order */
871 	o = exif_data_get_byte_order (e->parent->parent);
872 
873 	/* Sanity check */
874 	if (e->size != e->components * exif_format_get_size (e->format)) {
875 		snprintf (val, maxlen, _("Invalid size of entry (%i, "
876 			"expected %li x %i)."), e->size, e->components,
877 				exif_format_get_size (e->format));
878 		return val;
879 	}
880 
881 	switch (e->tag) {
882 	case EXIF_TAG_USER_COMMENT:
883 
884 		/*
885 		 * The specification says UNDEFINED, but some
886 		 * manufacturers don't care and use ASCII. If this is the
887 		 * case here, only refuse to read it if there is no chance
888 		 * of finding readable data.
889 		 */
890 		if ((e->format != EXIF_FORMAT_ASCII) ||
891 		    (e->size <= 8) ||
892 		    ( memcmp (e->data, "ASCII\0\0\0"  , 8) &&
893 		      memcmp (e->data, "UNICODE\0"    , 8) &&
894 		      memcmp (e->data, "JIS\0\0\0\0\0", 8) &&
895 		      memcmp (e->data, "\0\0\0\0\0\0\0\0", 8)))
896 			CF (e, EXIF_FORMAT_UNDEFINED, val, maxlen);
897 
898 		/*
899 		 * Note that, according to the specification (V2.1, p 40),
900 		 * the user comment field does not have to be
901 		 * NULL terminated.
902 		 */
903 		if ((e->size >= 8) && !memcmp (e->data, "ASCII\0\0\0", 8)) {
904 			strncpy (val, (char *) e->data + 8, MIN (e->size - 8, maxlen-1));
905 			break;
906 		}
907 		if ((e->size >= 8) && !memcmp (e->data, "UNICODE\0", 8)) {
908 			strncpy (val, _("Unsupported UNICODE string"), maxlen-1);
909 		/* FIXME: use iconv to convert into the locale encoding.
910 		 * EXIF 2.2 implies (but does not say) that this encoding is
911 		 * UCS-2.
912 		 */
913 			break;
914 		}
915 		if ((e->size >= 8) && !memcmp (e->data, "JIS\0\0\0\0\0", 8)) {
916 			strncpy (val, _("Unsupported JIS string"), maxlen-1);
917 		/* FIXME: use iconv to convert into the locale encoding */
918 			break;
919 		}
920 
921 		/* Check if there is really some information in the tag. */
922 		for (i = 0; (i < e->size) &&
923 			    (!e->data[i] || (e->data[i] == ' ')); i++);
924 		if (i == e->size) break;
925 
926 		/*
927 		 * If we reach this point, the tag does not
928  		 * comply with the standard but seems to contain data.
929 		 * Print as much as possible.
930 		 * Note: make sure we do not overwrite the final \0 at maxlen-1
931 		 */
932 		exif_entry_log (e, EXIF_LOG_CODE_DEBUG,
933 			_("Tag UserComment contains data but is "
934 			  "against specification."));
935  		for (j = 0; (i < e->size) && (j < maxlen-1); i++, j++) {
936 			exif_entry_log (e, EXIF_LOG_CODE_DEBUG,
937 				_("Byte at position %i: 0x%02x"), i, e->data[i]);
938  			val[j] = isprint (e->data[i]) ? e->data[i] : '.';
939 		}
940 		break;
941 
942 	case EXIF_TAG_EXIF_VERSION:
943 		CF (e, EXIF_FORMAT_UNDEFINED, val, maxlen);
944 		CC (e, 4, val, maxlen);
945 		strncpy (val, _("Unknown Exif Version"), maxlen-1);
946 		for (i = 0; *versions[i].label; i++) {
947 			if (!memcmp (e->data, versions[i].label, 4)) {
948     				snprintf (val, maxlen,
949 					_("Exif Version %d.%d"),
950 					versions[i].major,
951 					versions[i].minor);
952     				break;
953 			}
954 		}
955 		break;
956 	case EXIF_TAG_FLASH_PIX_VERSION:
957 		CF (e, EXIF_FORMAT_UNDEFINED, val, maxlen);
958 		CC (e, 4, val, maxlen);
959 		if (!memcmp (e->data, "0100", 4))
960 			strncpy (val, _("FlashPix Version 1.0"), maxlen-1);
961 		else if (!memcmp (e->data, "0101", 4))
962 			strncpy (val, _("FlashPix Version 1.01"), maxlen-1);
963 		else
964 			strncpy (val, _("Unknown FlashPix Version"), maxlen-1);
965 		break;
966 	case EXIF_TAG_COPYRIGHT:
967 		CF (e, EXIF_FORMAT_ASCII, val, maxlen);
968 
969 		/*
970 		 * First part: Photographer.
971 		 * Some cameras store a string like "   " here. Ignore it.
972 		 * Remember that a corrupted tag might not be NUL-terminated
973 		 */
974 		if (e->size && e->data && match_repeated_char(e->data, ' ', e->size))
975 			strncpy (val, (char *) e->data, MIN (maxlen-1, e->size));
976 		else
977 			strncpy (val, _("[None]"), maxlen-1);
978 		strncat (val, " ", maxlen-1 - strlen (val));
979 		strncat (val, _("(Photographer)"), maxlen-1 - strlen (val));
980 
981 		/* Second part: Editor. */
982 		strncat (val, " - ", maxlen-1 - strlen (val));
983 		k = 0;
984 		if (e->size && e->data) {
985 			const unsigned char *tagdata = memchr(e->data, 0, e->size);
986 			if (tagdata++) {
987 				unsigned int editor_ofs = tagdata - e->data;
988 				unsigned int remaining = e->size - editor_ofs;
989 				if (match_repeated_char(tagdata, ' ', remaining)) {
990 					strncat (val, (const char*)tagdata, MIN (maxlen-1 - strlen (val), remaining));
991 					++k;
992 				}
993 			}
994 		}
995 		if (!k)
996 			strncat (val, _("[None]"), maxlen-1 - strlen (val));
997 		strncat (val, " ", maxlen-1 - strlen (val));
998 		strncat (val, _("(Editor)"), maxlen-1 - strlen (val));
999 
1000 		break;
1001 	case EXIF_TAG_FNUMBER:
1002 		CF (e, EXIF_FORMAT_RATIONAL, val, maxlen);
1003 		CC (e, 1, val, maxlen);
1004 		v_rat = exif_get_rational (e->data, o);
1005 		if (!v_rat.denominator) {
1006 			exif_entry_format_value(e, val, maxlen);
1007 			break;
1008 		}
1009 		d = (double) v_rat.numerator / (double) v_rat.denominator;
1010 		snprintf (val, maxlen, "f/%.01f", d);
1011 		break;
1012 	case EXIF_TAG_APERTURE_VALUE:
1013 	case EXIF_TAG_MAX_APERTURE_VALUE:
1014 		CF (e, EXIF_FORMAT_RATIONAL, val, maxlen);
1015 		CC (e, 1, val, maxlen);
1016 		v_rat = exif_get_rational (e->data, o);
1017 		if (!v_rat.denominator || (0x80000000 == v_rat.numerator)) {
1018 			exif_entry_format_value(e, val, maxlen);
1019 			break;
1020 		}
1021 		d = (double) v_rat.numerator / (double) v_rat.denominator;
1022 		snprintf (val, maxlen, _("%.02f EV"), d);
1023 		snprintf (b, sizeof (b), _(" (f/%.01f)"), pow (2, d / 2.));
1024 		strncat (val, b, maxlen-1 - strlen (val));
1025 		break;
1026 	case EXIF_TAG_FOCAL_LENGTH:
1027 		CF (e, EXIF_FORMAT_RATIONAL, val, maxlen);
1028 		CC (e, 1, val, maxlen);
1029 		v_rat = exif_get_rational (e->data, o);
1030 		if (!v_rat.denominator) {
1031 			exif_entry_format_value(e, val, maxlen);
1032 			break;
1033 		}
1034 
1035 		/*
1036 		 * For calculation of the 35mm equivalent,
1037 		 * Minolta cameras need a multiplier that depends on the
1038 		 * camera model.
1039 		 */
1040 		d = 0.;
1041 		entry = exif_content_get_entry (
1042 			e->parent->parent->ifd[EXIF_IFD_0], EXIF_TAG_MAKE);
1043 		if (entry && entry->data && entry->size >= 7 &&
1044 		    !strncmp ((char *)entry->data, "Minolta", 7)) {
1045 			entry = exif_content_get_entry (
1046 					e->parent->parent->ifd[EXIF_IFD_0],
1047 					EXIF_TAG_MODEL);
1048 			if (entry && entry->data && entry->size >= 8) {
1049 				if (!strncmp ((char *)entry->data, "DiMAGE 7", 8))
1050 					d = 3.9;
1051 				else if (!strncmp ((char *)entry->data, "DiMAGE 5", 8))
1052 					d = 4.9;
1053 			}
1054 		}
1055 		if (d)
1056 			snprintf (b, sizeof (b), _(" (35 equivalent: %.0f mm)"),
1057 				  (d * (double) v_rat.numerator /
1058 				       (double) v_rat.denominator));
1059 		else
1060 			b[0] = 0;
1061 
1062 		d = (double) v_rat.numerator / (double) v_rat.denominator;
1063 		snprintf (val, maxlen, "%.1f mm", d);
1064 		strncat (val, b, maxlen-1 - strlen (val));
1065 		break;
1066 	case EXIF_TAG_SUBJECT_DISTANCE:
1067 		CF (e, EXIF_FORMAT_RATIONAL, val, maxlen);
1068 		CC (e, 1, val, maxlen);
1069 		v_rat = exif_get_rational (e->data, o);
1070 		if (!v_rat.denominator) {
1071 			exif_entry_format_value(e, val, maxlen);
1072 			break;
1073 		}
1074 		d = (double) v_rat.numerator / (double) v_rat.denominator;
1075 		snprintf (val, maxlen, "%.1f m", d);
1076 		break;
1077 	case EXIF_TAG_EXPOSURE_TIME:
1078 		CF (e, EXIF_FORMAT_RATIONAL, val, maxlen);
1079 		CC (e, 1, val, maxlen);
1080 		v_rat = exif_get_rational (e->data, o);
1081 		if (!v_rat.denominator) {
1082 			exif_entry_format_value(e, val, maxlen);
1083 			break;
1084 		}
1085 		d = (double) v_rat.numerator / (double) v_rat.denominator;
1086 		if (d < 1 && d)
1087 			snprintf (val, maxlen, _("1/%.0f"), 1. / d);
1088 		else
1089 			snprintf (val, maxlen, "%.0f", d);
1090 		strncat (val, _(" sec."), maxlen-1 - strlen (val));
1091 		break;
1092 	case EXIF_TAG_SHUTTER_SPEED_VALUE:
1093 		CF (e, EXIF_FORMAT_SRATIONAL, val, maxlen);
1094 		CC (e, 1, val, maxlen);
1095 		v_srat = exif_get_srational (e->data, o);
1096 		if (!v_srat.denominator) {
1097 			exif_entry_format_value(e, val, maxlen);
1098 			break;
1099 		}
1100 		d = (double) v_srat.numerator / (double) v_srat.denominator;
1101 		snprintf (val, maxlen, _("%.02f EV"), d);
1102 		if (pow (2, d))
1103 			d = 1. / pow (2, d);
1104 		if (d < 1 && d)
1105 		  snprintf (b, sizeof (b), _(" (1/%.0f sec.)"), 1. / d);
1106 		else
1107 		  snprintf (b, sizeof (b), _(" (%.0f sec.)"), d);
1108 		strncat (val, b, maxlen-1 - strlen (val));
1109 		break;
1110 	case EXIF_TAG_BRIGHTNESS_VALUE:
1111 		CF (e, EXIF_FORMAT_SRATIONAL, val, maxlen);
1112 		CC (e, 1, val, maxlen);
1113 		v_srat = exif_get_srational (e->data, o);
1114 		if (!v_srat.denominator) {
1115 			exif_entry_format_value(e, val, maxlen);
1116 			break;
1117 		}
1118 		d = (double) v_srat.numerator / (double) v_srat.denominator;
1119 		snprintf (val, maxlen, _("%.02f EV"), d);
1120 		snprintf (b, sizeof (b), _(" (%.02f cd/m^2)"),
1121 			1. / (M_PI * 0.3048 * 0.3048) * pow (2, d));
1122 		strncat (val, b, maxlen-1 - strlen (val));
1123 		break;
1124 	case EXIF_TAG_FILE_SOURCE:
1125 		CF (e, EXIF_FORMAT_UNDEFINED, val, maxlen);
1126 		CC (e, 1, val, maxlen);
1127 		v_byte = e->data[0];
1128 		if (v_byte == 3)
1129 			strncpy (val, _("DSC"), maxlen-1);
1130 		else
1131 			snprintf (val, maxlen, _("Internal error (unknown "
1132 				  "value %i)"), v_byte);
1133 		break;
1134 	case EXIF_TAG_COMPONENTS_CONFIGURATION:
1135 		CF (e, EXIF_FORMAT_UNDEFINED, val, maxlen);
1136 		CC (e, 4, val, maxlen);
1137 		for (i = 0; i < 4; i++) {
1138 			switch (e->data[i]) {
1139 			case 0: c = _("-"); break;
1140 			case 1: c = _("Y"); break;
1141 			case 2: c = _("Cb"); break;
1142 			case 3: c = _("Cr"); break;
1143 			case 4: c = _("R"); break;
1144 			case 5: c = _("G"); break;
1145 			case 6: c = _("B"); break;
1146 			default: c = _("Reserved"); break;
1147 			}
1148 			strncat (val, c, maxlen-1 - strlen (val));
1149 			if (i < 3)
1150 				strncat (val, " ", maxlen-1 - strlen (val));
1151 		}
1152 		break;
1153 	case EXIF_TAG_EXPOSURE_BIAS_VALUE:
1154 		CF (e, EXIF_FORMAT_SRATIONAL, val, maxlen);
1155 		CC (e, 1, val, maxlen);
1156 		v_srat = exif_get_srational (e->data, o);
1157 		if (!v_srat.denominator) {
1158 			exif_entry_format_value(e, val, maxlen);
1159 			break;
1160 		}
1161 		d = (double) v_srat.numerator / (double) v_srat.denominator;
1162 		snprintf (val, maxlen, _("%.02f EV"), d);
1163 		break;
1164 	case EXIF_TAG_SCENE_TYPE:
1165 		CF (e, EXIF_FORMAT_UNDEFINED, val, maxlen);
1166 		CC (e, 1, val, maxlen);
1167 		v_byte = e->data[0];
1168 		if (v_byte == 1)
1169 			strncpy (val, _("Directly photographed"), maxlen-1);
1170 		else
1171 			snprintf (val, maxlen, _("Internal error (unknown "
1172 				  "value %i)"), v_byte);
1173 		break;
1174 	case EXIF_TAG_YCBCR_SUB_SAMPLING:
1175 		CF (e, EXIF_FORMAT_SHORT, val, maxlen);
1176 		CC (e, 2, val, maxlen);
1177 		v_short  = exif_get_short (e->data, o);
1178 		v_short2 = exif_get_short (
1179 			e->data + exif_format_get_size (e->format),
1180 			o);
1181 		if ((v_short == 2) && (v_short2 == 1))
1182 			strncpy (val, _("YCbCr4:2:2"), maxlen-1);
1183 		else if ((v_short == 2) && (v_short2 == 2))
1184 			strncpy (val, _("YCbCr4:2:0"), maxlen-1);
1185 		else
1186 			snprintf (val, maxlen, "%u, %u", v_short, v_short2);
1187 		break;
1188 	case EXIF_TAG_SUBJECT_AREA:
1189 		CF (e, EXIF_FORMAT_SHORT, val, maxlen);
1190 		switch (e->components) {
1191 		case 2:
1192 			v_short  = exif_get_short (e->data, o);
1193 			v_short2 = exif_get_short (e->data + 2, o);
1194 			snprintf (val, maxlen, "(x,y) = (%i,%i)",
1195 				  v_short, v_short2);
1196 			break;
1197 		case 3:
1198 			v_short  = exif_get_short (e->data, o);
1199 			v_short2 = exif_get_short (e->data + 2, o);
1200 			v_short3 = exif_get_short (e->data + 4, o);
1201 			snprintf (val, maxlen, _("Within distance %i of "
1202 				"(x,y) = (%i,%i)"), v_short3, v_short,
1203 				v_short2);
1204 			break;
1205 		case 4:
1206 			v_short  = exif_get_short (e->data, o);
1207 			v_short2 = exif_get_short (e->data + 2, o);
1208 			v_short3 = exif_get_short (e->data + 4, o);
1209 			v_short4 = exif_get_short (e->data + 6, o);
1210 			snprintf (val, maxlen, _("Within rectangle "
1211 				"(width %i, height %i) around "
1212 				"(x,y) = (%i,%i)"), v_short3, v_short4,
1213 				v_short, v_short2);
1214 			break;
1215 		default:
1216 			snprintf (val, maxlen, _("Unexpected number "
1217 				"of components (%li, expected 2, 3, or 4)."),
1218 				e->components);
1219 		}
1220 		break;
1221 	case EXIF_TAG_GPS_VERSION_ID:
1222 		/* This is only valid in the GPS IFD */
1223 		CF (e, EXIF_FORMAT_BYTE, val, maxlen);
1224 		CC (e, 4, val, maxlen);
1225 		v_byte = e->data[0];
1226 		snprintf (val, maxlen, "%u", v_byte);
1227 		for (i = 1; i < e->components; i++) {
1228 			v_byte = e->data[i];
1229 			snprintf (b, sizeof (b), ".%u", v_byte);
1230 			strncat (val, b, maxlen-1 - strlen (val));
1231 		}
1232 		break;
1233 	case EXIF_TAG_INTEROPERABILITY_VERSION:
1234 	/* a.k.a. case EXIF_TAG_GPS_LATITUDE: */
1235 		/* This tag occurs in EXIF_IFD_INTEROPERABILITY */
1236 		if (e->format == EXIF_FORMAT_UNDEFINED) {
1237 			strncpy (val, (char *) e->data, MIN (maxlen-1, e->size));
1238 			break;
1239 		}
1240 		/* EXIF_TAG_GPS_LATITUDE is the same numerically as
1241 		 * EXIF_TAG_INTEROPERABILITY_VERSION but in EXIF_IFD_GPS
1242 		 */
1243 		exif_entry_format_value(e, val, maxlen);
1244 		break;
1245 	case EXIF_TAG_GPS_ALTITUDE_REF:
1246 		/* This is only valid in the GPS IFD */
1247 		CF (e, EXIF_FORMAT_BYTE, val, maxlen);
1248 		CC (e, 1, val, maxlen);
1249 		v_byte = e->data[0];
1250 		if (v_byte == 0)
1251 			strncpy (val, _("Sea level"), maxlen-1);
1252 		else if (v_byte == 1)
1253 			strncpy (val, _("Sea level reference"), maxlen-1);
1254 		else
1255 			snprintf (val, maxlen, _("Internal error (unknown "
1256 				  "value %i)"), v_byte);
1257 		break;
1258 	case EXIF_TAG_GPS_TIME_STAMP:
1259 		/* This is only valid in the GPS IFD */
1260 		CF (e, EXIF_FORMAT_RATIONAL, val, maxlen);
1261 		CC (e, 3, val, maxlen);
1262 
1263 		v_rat  = exif_get_rational (e->data, o);
1264 		if (!v_rat.denominator) {
1265 			exif_entry_format_value(e, val, maxlen);
1266 			break;
1267 		}
1268 		i = v_rat.numerator / v_rat.denominator;
1269 
1270 		v_rat = exif_get_rational (e->data +
1271 					     exif_format_get_size (e->format),
1272 					   o);
1273 		if (!v_rat.denominator) {
1274 			exif_entry_format_value(e, val, maxlen);
1275 			break;
1276 		}
1277 		j = v_rat.numerator / v_rat.denominator;
1278 
1279 		v_rat = exif_get_rational (e->data +
1280 					     2*exif_format_get_size (e->format),
1281 					     o);
1282 		if (!v_rat.denominator) {
1283 			exif_entry_format_value(e, val, maxlen);
1284 			break;
1285 		}
1286 		d = (double) v_rat.numerator / (double) v_rat.denominator;
1287 		snprintf (val, maxlen, "%02u:%02u:%05.2f", i, j, d);
1288 		break;
1289 
1290 	case EXIF_TAG_METERING_MODE:
1291 	case EXIF_TAG_COMPRESSION:
1292 	case EXIF_TAG_LIGHT_SOURCE:
1293 	case EXIF_TAG_FOCAL_PLANE_RESOLUTION_UNIT:
1294 	case EXIF_TAG_RESOLUTION_UNIT:
1295 	case EXIF_TAG_EXPOSURE_PROGRAM:
1296 	case EXIF_TAG_FLASH:
1297 	case EXIF_TAG_SUBJECT_DISTANCE_RANGE:
1298 	case EXIF_TAG_COLOR_SPACE:
1299 		CF (e,EXIF_FORMAT_SHORT, val, maxlen);
1300 		CC (e, 1, val, maxlen);
1301 		v_short = exif_get_short (e->data, o);
1302 
1303 		/* Search the tag */
1304 		for (i = 0; list2[i].tag && (list2[i].tag != e->tag); i++);
1305 		if (!list2[i].tag) {
1306 			snprintf (val, maxlen, _("Internal error (unknown "
1307 				  "value %i)"), v_short);
1308 			break;
1309 		}
1310 
1311 		/* Find the value */
1312 		for (j = 0; list2[i].elem[j].values[0] &&
1313 			    (list2[i].elem[j].index < v_short); j++);
1314 		if (list2[i].elem[j].index != v_short) {
1315 			snprintf (val, maxlen, _("Internal error (unknown "
1316 				  "value %i)"), v_short);
1317 			break;
1318 		}
1319 
1320 		/* Find a short enough value */
1321 		memset (val, 0, maxlen);
1322 		for (k = 0; list2[i].elem[j].values[k]; k++) {
1323 			size_t l = strlen (_(list2[i].elem[j].values[k]));
1324 			if ((maxlen > l) && (strlen (val) < l))
1325 				strncpy (val, _(list2[i].elem[j].values[k]), maxlen-1);
1326 		}
1327 		if (!val[0]) snprintf (val, maxlen, "%i", v_short);
1328 
1329 		break;
1330 
1331 	case EXIF_TAG_PLANAR_CONFIGURATION:
1332 	case EXIF_TAG_SENSING_METHOD:
1333 	case EXIF_TAG_ORIENTATION:
1334 	case EXIF_TAG_YCBCR_POSITIONING:
1335 	case EXIF_TAG_PHOTOMETRIC_INTERPRETATION:
1336 	case EXIF_TAG_CUSTOM_RENDERED:
1337 	case EXIF_TAG_EXPOSURE_MODE:
1338 	case EXIF_TAG_WHITE_BALANCE:
1339 	case EXIF_TAG_SCENE_CAPTURE_TYPE:
1340 	case EXIF_TAG_GAIN_CONTROL:
1341 	case EXIF_TAG_SATURATION:
1342 	case EXIF_TAG_CONTRAST:
1343 	case EXIF_TAG_SHARPNESS:
1344 		CF (e, EXIF_FORMAT_SHORT, val, maxlen);
1345 		CC (e, 1, val, maxlen);
1346 		v_short = exif_get_short (e->data, o);
1347 
1348 		/* Search the tag */
1349 		for (i = 0; list[i].tag && (list[i].tag != e->tag); i++);
1350 		if (!list[i].tag) {
1351 			snprintf (val, maxlen, _("Internal error (unknown "
1352 				  "value %i)"), v_short);
1353 			break;
1354 		}
1355 
1356 		/* Find the value */
1357 		for (j = 0; list[i].strings[j] && (j < v_short); j++);
1358 		if (!list[i].strings[j])
1359 			snprintf (val, maxlen, "%i", v_short);
1360 		else if (!*list[i].strings[j])
1361 			snprintf (val, maxlen, _("Unknown value %i"), v_short);
1362 		else
1363 			strncpy (val, _(list[i].strings[j]), maxlen-1);
1364 		break;
1365 
1366 	case EXIF_TAG_XP_TITLE:
1367 	case EXIF_TAG_XP_COMMENT:
1368 	case EXIF_TAG_XP_AUTHOR:
1369 	case EXIF_TAG_XP_KEYWORDS:
1370 	case EXIF_TAG_XP_SUBJECT:
1371 	{
1372 		unsigned char *utf16;
1373 
1374 		/* Sanity check the size to prevent overflow */
1375 		if (e->size+sizeof(uint16_t)+1 < e->size) break;
1376 
1377 		/* The tag may not be U+0000-terminated , so make a local
1378 		   U+0000-terminated copy before converting it */
1379 		utf16 = exif_mem_alloc (e->priv->mem, e->size+sizeof(uint16_t)+1);
1380 		if (!utf16) break;
1381 		memcpy(utf16, e->data, e->size);
1382 
1383 		/* NUL terminate the string. If the size is odd (which isn't possible
1384 		 * for a valid UTF16 string), then this will overwrite the high byte of
1385 		 * the final half word, plus add a full zero NUL word at the end.
1386 		 */
1387 		utf16[e->size] = 0;
1388 		utf16[e->size+1] = 0;
1389 		utf16[e->size+2] = 0;
1390 
1391 		/* Warning! The texts are converted from UTF16 to UTF8 */
1392 		/* FIXME: use iconv to convert into the locale encoding */
1393 		exif_convert_utf16_to_utf8(val, utf16, maxlen);
1394 		exif_mem_free(e->priv->mem, utf16);
1395 		break;
1396 	}
1397 
1398 	default:
1399 		/* Use a generic value formatting */
1400 		exif_entry_format_value(e, val, maxlen);
1401 	}
1402 
1403 	return val;
1404 }
1405 
1406 
1407 /*!
1408  * \bug Log and report failed exif_mem_malloc() calls.
1409  */
1410 void
exif_entry_initialize(ExifEntry * e,ExifTag tag)1411 exif_entry_initialize (ExifEntry *e, ExifTag tag)
1412 {
1413 	ExifRational r;
1414 	ExifByteOrder o;
1415 
1416 	/* We need the byte order */
1417 	if (!e || !e->parent || e->data || !e->parent->parent)
1418 		return;
1419 	o = exif_data_get_byte_order (e->parent->parent);
1420 
1421 	e->tag = tag;
1422 	switch (tag) {
1423 
1424 	/* LONG, 1 component, no default */
1425 	case EXIF_TAG_PIXEL_X_DIMENSION:
1426 	case EXIF_TAG_PIXEL_Y_DIMENSION:
1427 	case EXIF_TAG_EXIF_IFD_POINTER:
1428 	case EXIF_TAG_GPS_INFO_IFD_POINTER:
1429 	case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
1430 	case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
1431 	case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
1432 		e->components = 1;
1433 		e->format = EXIF_FORMAT_LONG;
1434 		e->size = exif_format_get_size (e->format) * e->components;
1435 		e->data = exif_entry_alloc (e, e->size);
1436 		if (!e->data) { clear_entry(e); break; }
1437 		break;
1438 
1439 	/* SHORT, 1 component, no default */
1440 	case EXIF_TAG_SUBJECT_LOCATION:
1441 	case EXIF_TAG_SENSING_METHOD:
1442 	case EXIF_TAG_PHOTOMETRIC_INTERPRETATION:
1443 	case EXIF_TAG_COMPRESSION:
1444 	case EXIF_TAG_EXPOSURE_MODE:
1445 	case EXIF_TAG_WHITE_BALANCE:
1446 	case EXIF_TAG_FOCAL_LENGTH_IN_35MM_FILM:
1447 	case EXIF_TAG_GAIN_CONTROL:
1448 	case EXIF_TAG_SUBJECT_DISTANCE_RANGE:
1449 	case EXIF_TAG_FLASH:
1450 	case EXIF_TAG_ISO_SPEED_RATINGS:
1451 
1452 	/* SHORT, 1 component, default 0 */
1453 	case EXIF_TAG_IMAGE_WIDTH:
1454 	case EXIF_TAG_IMAGE_LENGTH:
1455 	case EXIF_TAG_EXPOSURE_PROGRAM:
1456 	case EXIF_TAG_LIGHT_SOURCE:
1457 	case EXIF_TAG_METERING_MODE:
1458 	case EXIF_TAG_CUSTOM_RENDERED:
1459 	case EXIF_TAG_SCENE_CAPTURE_TYPE:
1460 	case EXIF_TAG_CONTRAST:
1461 	case EXIF_TAG_SATURATION:
1462 	case EXIF_TAG_SHARPNESS:
1463 		e->components = 1;
1464 		e->format = EXIF_FORMAT_SHORT;
1465 		e->size = exif_format_get_size (e->format) * e->components;
1466 		e->data = exif_entry_alloc (e, e->size);
1467 		if (!e->data) { clear_entry(e); break; }
1468 		exif_set_short (e->data, o, 0);
1469 		break;
1470 
1471 	/* SHORT, 1 component, default 1 */
1472 	case EXIF_TAG_ORIENTATION:
1473 	case EXIF_TAG_PLANAR_CONFIGURATION:
1474 	case EXIF_TAG_YCBCR_POSITIONING:
1475 		e->components = 1;
1476 		e->format = EXIF_FORMAT_SHORT;
1477 		e->size = exif_format_get_size (e->format) * e->components;
1478 		e->data = exif_entry_alloc (e, e->size);
1479 		if (!e->data) { clear_entry(e); break; }
1480 		exif_set_short (e->data, o, 1);
1481 		break;
1482 
1483 	/* SHORT, 1 component, default 2 */
1484 	case EXIF_TAG_RESOLUTION_UNIT:
1485 	case EXIF_TAG_FOCAL_PLANE_RESOLUTION_UNIT:
1486 		e->components = 1;
1487 		e->format = EXIF_FORMAT_SHORT;
1488 		e->size = exif_format_get_size (e->format) * e->components;
1489 		e->data = exif_entry_alloc (e, e->size);
1490 		if (!e->data) { clear_entry(e); break; }
1491 		exif_set_short (e->data, o, 2);
1492 		break;
1493 
1494 	/* SHORT, 1 component, default 3 */
1495 	case EXIF_TAG_SAMPLES_PER_PIXEL:
1496 		e->components = 1;
1497 		e->format = EXIF_FORMAT_SHORT;
1498 		e->size = exif_format_get_size (e->format) * e->components;
1499 		e->data = exif_entry_alloc (e, e->size);
1500 		if (!e->data) { clear_entry(e); break; }
1501 		exif_set_short (e->data, o, 3);
1502 		break;
1503 
1504 	/* SHORT, 1 component, default 0xffff */
1505 	case EXIF_TAG_COLOR_SPACE:
1506 		e->components = 1;
1507 		e->format = EXIF_FORMAT_SHORT;
1508 		e->size = exif_format_get_size (e->format) * e->components;
1509 		e->data = exif_entry_alloc (e, e->size);
1510 		if (!e->data) { clear_entry(e); break; }
1511 		exif_set_short (e->data, o, 0xffff);
1512 		break;
1513 
1514 	/* SHORT, 3 components, default 8 8 8 */
1515 	case EXIF_TAG_BITS_PER_SAMPLE:
1516 		e->components = 3;
1517 		e->format = EXIF_FORMAT_SHORT;
1518 		e->size = exif_format_get_size (e->format) * e->components;
1519 		e->data = exif_entry_alloc (e, e->size);
1520 		if (!e->data) { clear_entry(e); break; }
1521 		exif_set_short (e->data, o, 8);
1522 		exif_set_short (
1523 			e->data + exif_format_get_size (e->format),
1524 			o, 8);
1525 		exif_set_short (
1526 			e->data + 2 * exif_format_get_size (e->format),
1527 			o, 8);
1528 		break;
1529 
1530 	/* SHORT, 2 components, default 2 1 */
1531 	case EXIF_TAG_YCBCR_SUB_SAMPLING:
1532 		e->components = 2;
1533 		e->format = EXIF_FORMAT_SHORT;
1534 		e->size = exif_format_get_size (e->format) * e->components;
1535 		e->data = exif_entry_alloc (e, e->size);
1536 		if (!e->data) { clear_entry(e); break; }
1537 		exif_set_short (e->data, o, 2);
1538 		exif_set_short (
1539 			e->data + exif_format_get_size (e->format),
1540 			o, 1);
1541 		break;
1542 
1543 	/* SRATIONAL, 1 component, no default */
1544 	case EXIF_TAG_EXPOSURE_BIAS_VALUE:
1545 	case EXIF_TAG_BRIGHTNESS_VALUE:
1546 	case EXIF_TAG_SHUTTER_SPEED_VALUE:
1547 		e->components = 1;
1548 		e->format = EXIF_FORMAT_SRATIONAL;
1549 		e->size = exif_format_get_size (e->format) * e->components;
1550 		e->data = exif_entry_alloc (e, e->size);
1551 		if (!e->data) { clear_entry(e); break; }
1552 		break;
1553 
1554 	/* RATIONAL, 1 component, no default */
1555 	case EXIF_TAG_EXPOSURE_TIME:
1556 	case EXIF_TAG_FOCAL_PLANE_X_RESOLUTION:
1557 	case EXIF_TAG_FOCAL_PLANE_Y_RESOLUTION:
1558 	case EXIF_TAG_EXPOSURE_INDEX:
1559 	case EXIF_TAG_FLASH_ENERGY:
1560 	case EXIF_TAG_FNUMBER:
1561 	case EXIF_TAG_FOCAL_LENGTH:
1562 	case EXIF_TAG_SUBJECT_DISTANCE:
1563 	case EXIF_TAG_MAX_APERTURE_VALUE:
1564 	case EXIF_TAG_APERTURE_VALUE:
1565 	case EXIF_TAG_COMPRESSED_BITS_PER_PIXEL:
1566 	case EXIF_TAG_PRIMARY_CHROMATICITIES:
1567 	case EXIF_TAG_DIGITAL_ZOOM_RATIO:
1568 		e->components = 1;
1569 		e->format = EXIF_FORMAT_RATIONAL;
1570 		e->size = exif_format_get_size (e->format) * e->components;
1571 		e->data = exif_entry_alloc (e, e->size);
1572 		if (!e->data) { clear_entry(e); break; }
1573 		break;
1574 
1575 	/* RATIONAL, 1 component, default 72/1 */
1576 	case EXIF_TAG_X_RESOLUTION:
1577 	case EXIF_TAG_Y_RESOLUTION:
1578 		e->components = 1;
1579 		e->format = EXIF_FORMAT_RATIONAL;
1580 		e->size = exif_format_get_size (e->format) * e->components;
1581 		e->data = exif_entry_alloc (e, e->size);
1582 		if (!e->data) { clear_entry(e); break; }
1583 		r.numerator = 72;
1584 		r.denominator = 1;
1585 		exif_set_rational (e->data, o, r);
1586 		break;
1587 
1588 	/* RATIONAL, 2 components, no default */
1589 	case EXIF_TAG_WHITE_POINT:
1590 		e->components = 2;
1591 		e->format = EXIF_FORMAT_RATIONAL;
1592 		e->size = exif_format_get_size (e->format) * e->components;
1593 		e->data = exif_entry_alloc (e, e->size);
1594 		if (!e->data) { clear_entry(e); break; }
1595 		break;
1596 
1597 	/* RATIONAL, 6 components */
1598 	case EXIF_TAG_REFERENCE_BLACK_WHITE:
1599 		e->components = 6;
1600 		e->format = EXIF_FORMAT_RATIONAL;
1601 		e->size = exif_format_get_size (e->format) * e->components;
1602 		e->data = exif_entry_alloc (e, e->size);
1603 		if (!e->data) { clear_entry(e); break; }
1604 		r.denominator = 1;
1605 		r.numerator = 0;
1606 		exif_set_rational (e->data, o, r);
1607 		r.numerator = 255;
1608 		exif_set_rational (
1609 			e->data + exif_format_get_size (e->format), o, r);
1610 		r.numerator = 0;
1611 		exif_set_rational (
1612 			e->data + 2 * exif_format_get_size (e->format), o, r);
1613 		r.numerator = 255;
1614 		exif_set_rational (
1615 			e->data + 3 * exif_format_get_size (e->format), o, r);
1616 		r.numerator = 0;
1617 		exif_set_rational (
1618 			e->data + 4 * exif_format_get_size (e->format), o, r);
1619 		r.numerator = 255;
1620 		exif_set_rational (
1621 			e->data + 5 * exif_format_get_size (e->format), o, r);
1622 		break;
1623 
1624 	/* ASCII, 20 components */
1625 	case EXIF_TAG_DATE_TIME:
1626 	case EXIF_TAG_DATE_TIME_ORIGINAL:
1627 	case EXIF_TAG_DATE_TIME_DIGITIZED:
1628 	{
1629 		time_t t;
1630 #ifdef HAVE_LOCALTIME_R
1631 		struct tm tms;
1632 #endif
1633 		struct tm *tm;
1634 
1635 		t = time (NULL);
1636 #ifdef HAVE_LOCALTIME_R
1637 		tm = localtime_r (&t, &tms);
1638 #else
1639 		tm = localtime (&t);
1640 #endif
1641 		e->components = 20;
1642 		e->format = EXIF_FORMAT_ASCII;
1643 		e->size = exif_format_get_size (e->format) * e->components;
1644 		e->data = exif_entry_alloc (e, e->size);
1645 		if (!e->data) { clear_entry(e); break; }
1646 		snprintf ((char *) e->data, e->size,
1647 			  "%04i:%02i:%02i %02i:%02i:%02i",
1648 			  tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
1649 			  tm->tm_hour, tm->tm_min, tm->tm_sec);
1650 		break;
1651 	}
1652 
1653 	/* ASCII, no default */
1654 	case EXIF_TAG_SUB_SEC_TIME:
1655 	case EXIF_TAG_SUB_SEC_TIME_ORIGINAL:
1656 	case EXIF_TAG_SUB_SEC_TIME_DIGITIZED:
1657 		e->components = 0;
1658 		e->format = EXIF_FORMAT_ASCII;
1659 		e->size = 0;
1660 		e->data = NULL;
1661 		break;
1662 
1663 	/* ASCII, default "[None]" */
1664 	case EXIF_TAG_IMAGE_DESCRIPTION:
1665 	case EXIF_TAG_MAKE:
1666 	case EXIF_TAG_MODEL:
1667 	case EXIF_TAG_SOFTWARE:
1668 	case EXIF_TAG_ARTIST:
1669 		e->components = strlen (_("[None]")) + 1;
1670 		e->format = EXIF_FORMAT_ASCII;
1671 		e->size = exif_format_get_size (e->format) * e->components;
1672 		e->data = exif_entry_alloc (e, e->size);
1673 		if (!e->data) { clear_entry(e); break; }
1674 		strncpy ((char *)e->data, _("[None]"), e->size);
1675 		break;
1676 	/* ASCII, default "[None]\0[None]\0" */
1677 	case EXIF_TAG_COPYRIGHT:
1678 		e->components = (strlen (_("[None]")) + 1) * 2;
1679 		e->format = EXIF_FORMAT_ASCII;
1680 		e->size = exif_format_get_size (e->format) * e->components;
1681 		e->data = exif_entry_alloc (e, e->size);
1682 		if (!e->data) { clear_entry(e); break; }
1683 		strcpy (((char *)e->data) + 0, _("[None]"));
1684 		strcpy (((char *)e->data) + strlen (_("[None]")) + 1, _("[None]"));
1685 		break;
1686 
1687 	/* UNDEFINED, 1 component, default 1 */
1688 	case EXIF_TAG_SCENE_TYPE:
1689 		e->components = 1;
1690 		e->format = EXIF_FORMAT_UNDEFINED;
1691 		e->size = exif_format_get_size (e->format) * e->components;
1692 		e->data = exif_entry_alloc (e, e->size);
1693 		if (!e->data) { clear_entry(e); break; }
1694 		e->data[0] = 0x01;
1695 		break;
1696 
1697 	/* UNDEFINED, 1 component, default 3 */
1698 	case EXIF_TAG_FILE_SOURCE:
1699 		e->components = 1;
1700 		e->format = EXIF_FORMAT_UNDEFINED;
1701 		e->size = exif_format_get_size (e->format) * e->components;
1702 		e->data = exif_entry_alloc (e, e->size);
1703 		if (!e->data) { clear_entry(e); break; }
1704 		e->data[0] = 0x03;
1705 		break;
1706 
1707 	/* UNDEFINED, 4 components, default 48 49 48 48 */
1708         case EXIF_TAG_FLASH_PIX_VERSION:
1709                 e->components = 4;
1710                 e->format = EXIF_FORMAT_UNDEFINED;
1711                 e->size = exif_format_get_size (e->format) * e->components;
1712                 e->data = exif_entry_alloc (e, e->size);
1713                 if (!e->data) { clear_entry(e); break; }
1714                 memcpy (e->data, "0100", 4);
1715                 break;
1716 
1717         /* UNDEFINED, 4 components, default 48 50 49 48 */
1718         case EXIF_TAG_EXIF_VERSION:
1719                 e->components = 4;
1720                 e->format = EXIF_FORMAT_UNDEFINED;
1721                 e->size = exif_format_get_size (e->format) * e->components;
1722                 e->data = exif_entry_alloc (e, e->size);
1723                 if (!e->data) { clear_entry(e); break; }
1724                 memcpy (e->data, "0210", 4);
1725                 break;
1726 
1727         /* UNDEFINED, 4 components, default 1 2 3 0 */
1728         case EXIF_TAG_COMPONENTS_CONFIGURATION:
1729                 e->components = 4;
1730                 e->format = EXIF_FORMAT_UNDEFINED;
1731                 e->size = exif_format_get_size (e->format) * e->components;
1732                 e->data = exif_entry_alloc (e, e->size);
1733                 if (!e->data) { clear_entry(e); break; }
1734 		e->data[0] = 1;
1735 		e->data[1] = 2;
1736 		e->data[2] = 3;
1737 		e->data[3] = 0;
1738                 break;
1739 
1740 	/* UNDEFINED, no components, no default */
1741 	/* Use this if the tag is otherwise unsupported */
1742 	case EXIF_TAG_MAKER_NOTE:
1743 	case EXIF_TAG_USER_COMMENT:
1744 	default:
1745 		e->components = 0;
1746 		e->format = EXIF_FORMAT_UNDEFINED;
1747 		e->size = 0;
1748 		e->data = NULL;
1749 		break;
1750 	}
1751 }
1752