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