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