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