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