3 * Creates all the types of tags supported in exif_entry_initialize() and
4 * ensures that exif_entry_get_value() properly truncates the output of each
5 * one according to the buffer size available.
7 * Copyright 2002 Lutz Mueller <lutz@users.sourceforge.net>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301 USA.
25 #include <libexif/exif-utils.h>
26 #include <libexif/exif-data.h>
33 * List of tags to test, one per default initialized type.
34 * There should be one for every block in exif_entry_initialize() and
35 * exif_entry_get_value().
37 ExifTag trunc_test_tags[] = {
38 EXIF_TAG_PIXEL_X_DIMENSION,
39 EXIF_TAG_SUBJECT_LOCATION,
42 EXIF_TAG_SAMPLES_PER_PIXEL,
43 EXIF_TAG_BITS_PER_SAMPLE,
44 EXIF_TAG_X_RESOLUTION,
46 EXIF_TAG_REFERENCE_BLACK_WHITE,
48 EXIF_TAG_IMAGE_DESCRIPTION,
49 EXIF_TAG_EXIF_VERSION,
50 EXIF_TAG_FLASH_PIX_VERSION,
53 EXIF_TAG_COMPONENTS_CONFIGURATION,
55 EXIF_TAG_YCBCR_SUB_SAMPLING,
56 EXIF_TAG_PLANAR_CONFIGURATION,
60 * These tags produce different outputs depending on the amount of buffer space
63 ExifTag nonuniform_test_tags[] = {
64 EXIF_TAG_RESOLUTION_UNIT,
66 EXIF_TAG_METERING_MODE,
70 * These tags need a nonzero rational number to be interesting.
71 * They must have space for a rational or srational created automatically by
72 * exif_entry_initialize().
74 ExifTag rational_test_tags[] = {
76 EXIF_TAG_APERTURE_VALUE,
77 EXIF_TAG_MAX_APERTURE_VALUE,
78 EXIF_TAG_FOCAL_LENGTH,
79 EXIF_TAG_SUBJECT_DISTANCE,
80 EXIF_TAG_EXPOSURE_TIME,
81 EXIF_TAG_SHUTTER_SPEED_VALUE,
82 EXIF_TAG_BRIGHTNESS_VALUE,
83 EXIF_TAG_EXPOSURE_BIAS_VALUE,
87 * Verify that the entry is properly truncated to the buffer length within
88 * exif_entry_get_value(). If uniform is zero, then only check that the
89 * resulting string fits within the buffer and don't check its content.
91 static void check_entry_trunc(ExifEntry *e, int uniform)
94 char v[1024], full[1024]; /* Large enough to never truncate output */
96 printf ("Tag 0x%x\n", (int) e->tag);
98 /* Get the full, untruncated string to use as the expected value */
99 exif_entry_get_value (e, full, sizeof(full));
100 printf ("Full: '%s'\n", full);
102 for (i = strlen(full); i > 0; i--) {
103 /* Make sure the buffer isn't NUL-terminated to begin with */
104 memset(v, '*', sizeof(v));
105 exif_entry_get_value (e, v, i);
106 /* Truncate the full string by one on each iteration */
108 if ((strlen(v) >= i) || (uniform && strcmp(full, v))) {
109 printf("Bad truncation!\n");
110 printf("Length %2i: '%s'\n", i, v);
123 static const ExifSRational r = {1., 20.}; /* a nonzero number */
124 static const char user_comment[] = "ASCII\0\0\0A Long User Comment";
125 static const char xp_comment[] = "U\0C\0S\0-\0002\0 \0C\0o\0m\0m\0e\0n\0t\0";
126 static const char interop[] = "R98";
127 static const char subsec[] = "130 ";
128 static const ExifRational gpsh = {12., 1.};
129 static const ExifRational gpsm = {34., 1.};
130 static const ExifRational gpss = {56780., 1000.};
132 data = exif_data_new ();
134 fprintf (stderr, "Error running exif_data_new()\n");
138 /* Full initialization/truncation tests */
139 for (i=0; i < sizeof(trunc_test_tags)/sizeof(trunc_test_tags[0]); ++i) {
140 e = exif_entry_new ();
142 fprintf (stderr, "Error running exif_entry_new()\n");
145 exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
146 exif_entry_initialize (e, trunc_test_tags[i]);
147 check_entry_trunc(e, 1);
148 exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
149 exif_entry_unref (e);
152 /* Nonuniform initialization/truncation tests */
153 for (i=0; i < sizeof(nonuniform_test_tags)/sizeof(nonuniform_test_tags[0]);
155 e = exif_entry_new ();
157 fprintf (stderr, "Error running exif_entry_new()\n");
160 exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
161 exif_entry_initialize (e, nonuniform_test_tags[i]);
162 check_entry_trunc(e, 0);
163 exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
164 exif_entry_unref (e);
167 /* Rational number initialization/truncation tests */
168 for (i=0; i < sizeof(rational_test_tags)/sizeof(rational_test_tags[0]);
170 e = exif_entry_new ();
172 fprintf (stderr, "Error running exif_entry_new()\n");
175 exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
176 exif_entry_initialize (e, rational_test_tags[i]);
177 exif_set_srational (e->data, exif_data_get_byte_order (data), r);
178 /* In case this tag needs an unsigned rational instead,
179 * fix the type automatically */
181 check_entry_trunc(e, 1);
182 exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
183 exif_entry_unref (e);
186 /* Create a memory allocator to manage the remaining ExifEntry structs */
187 mem = exif_mem_new_default();
189 fprintf (stderr, "Out of memory\n");
193 /* EXIF_TAG_SUB_SEC_TIME initialization/truncation tests */
194 e = exif_entry_new_mem (mem);
196 fprintf (stderr, "Out of memory\n");
199 exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
200 exif_entry_initialize (e, EXIF_TAG_SUB_SEC_TIME);
201 e->size = sizeof(subsec); /* include NUL */
202 e->components = e->size;
203 /* Allocate memory to use for holding the tag data */
204 e->data = exif_mem_alloc(mem, e->size);
206 fprintf (stderr, "Out of memory\n");
209 memcpy(e->data, subsec, e->size);
210 check_entry_trunc(e, 1);
211 exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
212 exif_entry_unref (e);
214 /* EXIF_TAG_USER_COMMENT initialization/truncation tests */
215 e = exif_entry_new_mem (mem);
217 fprintf (stderr, "Out of memory\n");
220 exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
221 exif_entry_initialize (e, EXIF_TAG_USER_COMMENT);
222 e->size = sizeof(user_comment) - 1;
223 e->components = e->size;
224 /* Allocate memory to use for holding the tag data */
225 e->data = exif_mem_alloc(mem, e->size);
227 fprintf (stderr, "Out of memory\n");
230 memcpy(e->data, user_comment, e->size);
231 check_entry_trunc(e, 1);
232 exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
233 exif_entry_unref (e);
235 /* EXIF_TAG_XP_COMMENT truncation tests */
236 e = exif_entry_new_mem (mem);
238 fprintf (stderr, "Out of memory\n");
241 exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
242 exif_entry_initialize (e, EXIF_TAG_XP_COMMENT);
243 e->format = EXIF_FORMAT_BYTE;
244 e->size = sizeof(xp_comment) - 1;
245 e->components = e->size;
246 /* Allocate memory to use for holding the tag data */
247 e->data = exif_mem_alloc(mem, e->size);
249 fprintf (stderr, "Out of memory\n");
252 memcpy(e->data, xp_comment, e->size);
253 check_entry_trunc(e, 1);
254 exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
255 exif_entry_unref (e);
257 /* EXIF_TAG_INTEROPERABILITY_VERSION truncation tests */
258 e = exif_entry_new_mem (mem);
260 fprintf (stderr, "Out of memory\n");
263 exif_content_add_entry (data->ifd[EXIF_IFD_INTEROPERABILITY], e);
264 exif_entry_initialize (e, EXIF_TAG_INTEROPERABILITY_VERSION);
265 e->format = EXIF_FORMAT_UNDEFINED; /* The spec says ASCII, but libexif
267 e->size = sizeof(interop); /* include NUL */
268 e->components = e->size;
269 /* Allocate memory to use for holding the tag data */
270 e->data = exif_mem_alloc(mem, e->size);
272 fprintf (stderr, "Out of memory\n");
275 memcpy(e->data, interop, e->size);
276 check_entry_trunc(e, 1);
277 exif_content_remove_entry (data->ifd[EXIF_IFD_INTEROPERABILITY], e);
278 exif_entry_unref (e);
280 /* EXIF_TAG_GPS_VERSION_ID truncation tests */
281 e = exif_entry_new_mem (mem);
283 fprintf (stderr, "Out of memory\n");
286 exif_content_add_entry (data->ifd[EXIF_IFD_GPS], e);
287 exif_entry_initialize (e, EXIF_TAG_GPS_VERSION_ID);
288 e->format = EXIF_FORMAT_BYTE;
290 e->components = e->size;
291 /* Allocate memory to use for holding the tag data */
292 e->data = exif_mem_alloc(mem, e->size);
294 fprintf (stderr, "Out of memory\n");
301 check_entry_trunc(e, 1);
302 exif_content_remove_entry (data->ifd[EXIF_IFD_GPS], e);
303 exif_entry_unref (e);
305 /* EXIF_TAG_GPS_ALTITUDE_REF truncation tests */
306 e = exif_entry_new_mem (mem);
308 fprintf (stderr, "Out of memory\n");
311 exif_content_add_entry (data->ifd[EXIF_IFD_GPS], e);
312 exif_entry_initialize (e, EXIF_TAG_GPS_ALTITUDE_REF);
313 e->format = EXIF_FORMAT_BYTE;
315 e->components = e->size;
316 /* Allocate memory to use for holding the tag data */
317 e->data = exif_mem_alloc(mem, e->size);
319 fprintf (stderr, "Out of memory\n");
323 check_entry_trunc(e, 1);
324 exif_content_remove_entry (data->ifd[EXIF_IFD_GPS], e);
325 exif_entry_unref (e);
327 /* EXIF_TAG_GPS_TIME_STAMP truncation tests */
328 e = exif_entry_new_mem (mem);
330 fprintf (stderr, "Out of memory\n");
333 exif_content_add_entry (data->ifd[EXIF_IFD_GPS], e);
334 exif_entry_initialize (e, EXIF_TAG_GPS_TIME_STAMP);
335 e->format = EXIF_FORMAT_RATIONAL;
337 e->size = e->components * exif_format_get_size(EXIF_FORMAT_RATIONAL);
338 /* Allocate memory to use for holding the tag data */
339 e->data = exif_mem_alloc(mem, e->size);
341 fprintf (stderr, "Out of memory\n");
344 exif_set_rational(e->data, exif_data_get_byte_order (data), gpsh);
345 exif_set_rational(e->data+8, exif_data_get_byte_order (data), gpsm);
346 exif_set_rational(e->data+16, exif_data_get_byte_order (data), gpss);
347 check_entry_trunc(e, 1);
348 exif_content_remove_entry (data->ifd[EXIF_IFD_GPS], e);
349 exif_entry_unref (e);
351 /* EXIF_TAG_SUBJECT_AREA truncation tests */
352 e = exif_entry_new_mem (mem);
354 fprintf (stderr, "Out of memory\n");
357 exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
358 exif_entry_initialize (e, EXIF_TAG_SUBJECT_AREA);
359 e->format = EXIF_FORMAT_SHORT;
360 /* This tags is interpreted differently depending on # components */
363 e->size = e->components * exif_format_get_size(EXIF_FORMAT_SHORT);
364 /* Allocate memory to use for holding the tag data */
365 e->data = exif_mem_alloc(mem, e->size);
367 fprintf (stderr, "Out of memory\n");
370 exif_set_short(e->data, exif_data_get_byte_order (data), 123);
371 exif_set_short(e->data+2, exif_data_get_byte_order (data), 456);
372 exif_set_short(e->data+4, exif_data_get_byte_order (data), 78);
373 exif_set_short(e->data+6, exif_data_get_byte_order (data), 90);
374 check_entry_trunc(e, 1);
377 e->size = e->components * exif_format_get_size(EXIF_FORMAT_SHORT);
378 check_entry_trunc(e, 1);
381 e->size = e->components * exif_format_get_size(EXIF_FORMAT_SHORT);
382 check_entry_trunc(e, 1);
385 e->size = e->components * exif_format_get_size(EXIF_FORMAT_SHORT);
386 check_entry_trunc(e, 1);
387 exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
388 exif_entry_unref (e);
391 exif_data_unref (data);