File size: 12,110 Bytes
4a85060 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
/* test-value.c
*
* Creates all the types of tags supported in exif_entry_initialize() and
* ensures that exif_entry_get_value() properly truncates the output of each
* one according to the buffer size available.
*
* Copyright 2002 Lutz Mueller <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*/
#include <libexif/exif-utils.h>
#include <libexif/exif-data.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* List of tags to test, one per default initialized type.
* There should be one for every block in exif_entry_initialize() and
* exif_entry_get_value().
*/
ExifTag trunc_test_tags[] = {
EXIF_TAG_PIXEL_X_DIMENSION,
EXIF_TAG_SUBJECT_LOCATION,
EXIF_TAG_IMAGE_WIDTH,
EXIF_TAG_ORIENTATION,
EXIF_TAG_SAMPLES_PER_PIXEL,
EXIF_TAG_BITS_PER_SAMPLE,
EXIF_TAG_X_RESOLUTION,
EXIF_TAG_WHITE_POINT,
EXIF_TAG_REFERENCE_BLACK_WHITE,
EXIF_TAG_DATE_TIME,
EXIF_TAG_IMAGE_DESCRIPTION,
EXIF_TAG_EXIF_VERSION,
EXIF_TAG_FLASH_PIX_VERSION,
EXIF_TAG_COPYRIGHT,
EXIF_TAG_FILE_SOURCE,
EXIF_TAG_COMPONENTS_CONFIGURATION,
EXIF_TAG_SCENE_TYPE,
EXIF_TAG_YCBCR_SUB_SAMPLING,
EXIF_TAG_PLANAR_CONFIGURATION,
};
/*
* These tags produce different outputs depending on the amount of buffer space
* available.
*/
ExifTag nonuniform_test_tags[] = {
EXIF_TAG_RESOLUTION_UNIT,
EXIF_TAG_COLOR_SPACE,
EXIF_TAG_METERING_MODE,
};
/*
* These tags need a nonzero rational number to be interesting.
* They must have space for a rational or srational created automatically by
* exif_entry_initialize().
*/
ExifTag rational_test_tags[] = {
EXIF_TAG_FNUMBER,
EXIF_TAG_APERTURE_VALUE,
EXIF_TAG_MAX_APERTURE_VALUE,
EXIF_TAG_FOCAL_LENGTH,
EXIF_TAG_SUBJECT_DISTANCE,
EXIF_TAG_EXPOSURE_TIME,
EXIF_TAG_SHUTTER_SPEED_VALUE,
EXIF_TAG_BRIGHTNESS_VALUE,
EXIF_TAG_EXPOSURE_BIAS_VALUE,
};
/*
* Verify that the entry is properly truncated to the buffer length within
* exif_entry_get_value(). If uniform is zero, then only check that the
* resulting string fits within the buffer and don't check its content.
*/
static void check_entry_trunc(ExifEntry *e, int uniform)
{
unsigned int i;
char v[1024], full[1024]; /* Large enough to never truncate output */
printf ("Tag 0x%x\n", (int) e->tag);
/* Get the full, untruncated string to use as the expected value */
exif_entry_get_value (e, full, sizeof(full));
printf ("Full: '%s'\n", full);
for (i = strlen(full); i > 0; i--) {
/* Make sure the buffer isn't NUL-terminated to begin with */
memset(v, '*', sizeof(v));
exif_entry_get_value (e, v, i);
/* Truncate the full string by one on each iteration */
full[i-1] = '\0';
if ((strlen(v) >= i) || (uniform && strcmp(full, v))) {
printf("Bad truncation!\n");
printf("Length %2i: '%s'\n", i, v);
exit(1);
}
}
}
int
main ()
{
ExifData *data;
ExifEntry *e;
ExifMem *mem;
unsigned i;
static const ExifSRational r = {1., 20.}; /* a nonzero number */
static const char user_comment[] = "ASCII\0\0\0A Long User Comment";
static const char xp_comment[] = "U\0C\0S\0-\0002\0 \0C\0o\0m\0m\0e\0n\0t\0";
static const char interop[] = "R98";
static const char subsec[] = "130 ";
static const ExifRational gpsh = {12., 1.};
static const ExifRational gpsm = {34., 1.};
static const ExifRational gpss = {56780., 1000.};
data = exif_data_new ();
if (!data) {
fprintf (stderr, "Error running exif_data_new()\n");
exit(13);
}
/* Full initialization/truncation tests */
for (i=0; i < sizeof(trunc_test_tags)/sizeof(trunc_test_tags[0]); ++i) {
e = exif_entry_new ();
if (!e) {
fprintf (stderr, "Error running exif_entry_new()\n");
exit(13);
}
exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
exif_entry_initialize (e, trunc_test_tags[i]);
check_entry_trunc(e, 1);
exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
exif_entry_unref (e);
}
/* Nonuniform initialization/truncation tests */
for (i=0; i < sizeof(nonuniform_test_tags)/sizeof(nonuniform_test_tags[0]);
++i) {
e = exif_entry_new ();
if (!e) {
fprintf (stderr, "Error running exif_entry_new()\n");
exit(13);
}
exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
exif_entry_initialize (e, nonuniform_test_tags[i]);
check_entry_trunc(e, 0);
exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
exif_entry_unref (e);
}
/* Rational number initialization/truncation tests */
for (i=0; i < sizeof(rational_test_tags)/sizeof(rational_test_tags[0]);
++i) {
e = exif_entry_new ();
if (!e) {
fprintf (stderr, "Error running exif_entry_new()\n");
exit(13);
}
exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
exif_entry_initialize (e, rational_test_tags[i]);
exif_set_srational (e->data, exif_data_get_byte_order (data), r);
/* In case this tag needs an unsigned rational instead,
* fix the type automatically */
exif_entry_fix (e);
check_entry_trunc(e, 1);
exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
exif_entry_unref (e);
}
/* Create a memory allocator to manage the remaining ExifEntry structs */
mem = exif_mem_new_default();
if (!mem) {
fprintf (stderr, "Out of memory\n");
exit(13);
}
/* EXIF_TAG_SUB_SEC_TIME initialization/truncation tests */
e = exif_entry_new_mem (mem);
if (!e) {
fprintf (stderr, "Out of memory\n");
exit(13);
}
exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
exif_entry_initialize (e, EXIF_TAG_SUB_SEC_TIME);
e->size = sizeof(subsec); /* include NUL */
e->components = e->size;
/* Allocate memory to use for holding the tag data */
e->data = exif_mem_alloc(mem, e->size);
if (!e->data) {
fprintf (stderr, "Out of memory\n");
exit(13);
}
memcpy(e->data, subsec, e->size);
check_entry_trunc(e, 1);
exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
exif_entry_unref (e);
/* EXIF_TAG_USER_COMMENT initialization/truncation tests */
e = exif_entry_new_mem (mem);
if (!e) {
fprintf (stderr, "Out of memory\n");
exit(13);
}
exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
exif_entry_initialize (e, EXIF_TAG_USER_COMMENT);
e->size = sizeof(user_comment) - 1;
e->components = e->size;
/* Allocate memory to use for holding the tag data */
e->data = exif_mem_alloc(mem, e->size);
if (!e->data) {
fprintf (stderr, "Out of memory\n");
exit(13);
}
memcpy(e->data, user_comment, e->size);
check_entry_trunc(e, 1);
exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
exif_entry_unref (e);
/* EXIF_TAG_XP_COMMENT truncation tests */
e = exif_entry_new_mem (mem);
if (!e) {
fprintf (stderr, "Out of memory\n");
exit(13);
}
exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
exif_entry_initialize (e, EXIF_TAG_XP_COMMENT);
e->format = EXIF_FORMAT_BYTE;
e->size = sizeof(xp_comment) - 1;
e->components = e->size;
/* Allocate memory to use for holding the tag data */
e->data = exif_mem_alloc(mem, e->size);
if (!e->data) {
fprintf (stderr, "Out of memory\n");
exit(13);
}
memcpy(e->data, xp_comment, e->size);
check_entry_trunc(e, 1);
exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
exif_entry_unref (e);
/* EXIF_TAG_INTEROPERABILITY_VERSION truncation tests */
e = exif_entry_new_mem (mem);
if (!e) {
fprintf (stderr, "Out of memory\n");
exit(13);
}
exif_content_add_entry (data->ifd[EXIF_IFD_INTEROPERABILITY], e);
exif_entry_initialize (e, EXIF_TAG_INTEROPERABILITY_VERSION);
e->format = EXIF_FORMAT_UNDEFINED; /* The spec says ASCII, but libexif
allows UNDEFINED */
e->size = sizeof(interop); /* include NUL */
e->components = e->size;
/* Allocate memory to use for holding the tag data */
e->data = exif_mem_alloc(mem, e->size);
if (!e->data) {
fprintf (stderr, "Out of memory\n");
exit(13);
}
memcpy(e->data, interop, e->size);
check_entry_trunc(e, 1);
exif_content_remove_entry (data->ifd[EXIF_IFD_INTEROPERABILITY], e);
exif_entry_unref (e);
/* EXIF_TAG_GPS_VERSION_ID truncation tests */
e = exif_entry_new_mem (mem);
if (!e) {
fprintf (stderr, "Out of memory\n");
exit(13);
}
exif_content_add_entry (data->ifd[EXIF_IFD_GPS], e);
exif_entry_initialize (e, EXIF_TAG_GPS_VERSION_ID);
e->format = EXIF_FORMAT_BYTE;
e->size = 4;
e->components = e->size;
/* Allocate memory to use for holding the tag data */
e->data = exif_mem_alloc(mem, e->size);
if (!e->data) {
fprintf (stderr, "Out of memory\n");
exit(13);
}
e->data[0] = 2;
e->data[1] = 2;
e->data[2] = 0;
e->data[3] = 0;
check_entry_trunc(e, 1);
exif_content_remove_entry (data->ifd[EXIF_IFD_GPS], e);
exif_entry_unref (e);
/* EXIF_TAG_GPS_ALTITUDE_REF truncation tests */
e = exif_entry_new_mem (mem);
if (!e) {
fprintf (stderr, "Out of memory\n");
exit(13);
}
exif_content_add_entry (data->ifd[EXIF_IFD_GPS], e);
exif_entry_initialize (e, EXIF_TAG_GPS_ALTITUDE_REF);
e->format = EXIF_FORMAT_BYTE;
e->size = 1;
e->components = e->size;
/* Allocate memory to use for holding the tag data */
e->data = exif_mem_alloc(mem, e->size);
if (!e->data) {
fprintf (stderr, "Out of memory\n");
exit(13);
}
e->data[0] = 1;
check_entry_trunc(e, 1);
exif_content_remove_entry (data->ifd[EXIF_IFD_GPS], e);
exif_entry_unref (e);
/* EXIF_TAG_GPS_TIME_STAMP truncation tests */
e = exif_entry_new_mem (mem);
if (!e) {
fprintf (stderr, "Out of memory\n");
exit(13);
}
exif_content_add_entry (data->ifd[EXIF_IFD_GPS], e);
exif_entry_initialize (e, EXIF_TAG_GPS_TIME_STAMP);
e->format = EXIF_FORMAT_RATIONAL;
e->components = 3;
e->size = e->components * exif_format_get_size(EXIF_FORMAT_RATIONAL);
/* Allocate memory to use for holding the tag data */
e->data = exif_mem_alloc(mem, e->size);
if (!e->data) {
fprintf (stderr, "Out of memory\n");
exit(13);
}
exif_set_rational(e->data, exif_data_get_byte_order (data), gpsh);
exif_set_rational(e->data+8, exif_data_get_byte_order (data), gpsm);
exif_set_rational(e->data+16, exif_data_get_byte_order (data), gpss);
check_entry_trunc(e, 1);
exif_content_remove_entry (data->ifd[EXIF_IFD_GPS], e);
exif_entry_unref (e);
/* EXIF_TAG_SUBJECT_AREA truncation tests */
e = exif_entry_new_mem (mem);
if (!e) {
fprintf (stderr, "Out of memory\n");
exit(13);
}
exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
exif_entry_initialize (e, EXIF_TAG_SUBJECT_AREA);
e->format = EXIF_FORMAT_SHORT;
/* This tags is interpreted differently depending on # components */
/* Rectangle */
e->components = 4;
e->size = e->components * exif_format_get_size(EXIF_FORMAT_SHORT);
/* Allocate memory to use for holding the tag data */
e->data = exif_mem_alloc(mem, e->size);
if (!e->data) {
fprintf (stderr, "Out of memory\n");
exit(13);
}
exif_set_short(e->data, exif_data_get_byte_order (data), 123);
exif_set_short(e->data+2, exif_data_get_byte_order (data), 456);
exif_set_short(e->data+4, exif_data_get_byte_order (data), 78);
exif_set_short(e->data+6, exif_data_get_byte_order (data), 90);
check_entry_trunc(e, 1);
/* Circle */
e->components = 3;
e->size = e->components * exif_format_get_size(EXIF_FORMAT_SHORT);
check_entry_trunc(e, 1);
/* Centre */
e->components = 2;
e->size = e->components * exif_format_get_size(EXIF_FORMAT_SHORT);
check_entry_trunc(e, 1);
/* Invalid */
e->components = 1;
e->size = e->components * exif_format_get_size(EXIF_FORMAT_SHORT);
check_entry_trunc(e, 1);
exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
exif_entry_unref (e);
exif_mem_unref(mem);
exif_data_unref (data);
return 0;
}
|