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