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