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
|
/*
* Copyright (c) 2001-2003, Eric M. Johnston <emj@postal.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Eric M. Johnston.
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: exifutil.c,v 1.24 2003/08/08 22:31:32 ejohnst Exp $
*/
/*
* Utilities for dealing with Exif data.
*
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "exif.h"
#include "exifint.h"
/*
* Some global variables we all need.
*/
int debug;
const char *progname;
/*
* Logging and error functions.
*/
void
exifdie(const char *msg)
{
fprintf(stderr, "%s: %s\n", progname, msg);
exit(1);
}
void
exifwarn(const char *msg)
{
fprintf(stderr, "%s: %s\n", progname, msg);
}
void
exifwarn2(const char *msg1, const char *msg2)
{
fprintf(stderr, "%s: %s (%s)\n", progname, msg1, msg2);
}
/*
* Read an unsigned 2-byte int from the buffer.
*/
u_int16_t
exif2byte(unsigned char *b, enum byteorder o)
{
if (o == BIG)
return ((b[0] << 8) | b[1]);
else
return ((b[1] << 8) | b[0]);
}
/*
* Read a signed 2-byte int from the buffer.
*/
int16_t
exif2sbyte(unsigned char *b, enum byteorder o)
{
if (o == BIG)
return ((b[0] << 8) | b[1]);
else
return ((b[1] << 8) | b[0]);
}
/*
* Read an unsigned 4-byte int from the buffer.
*/
u_int32_t
exif4byte(unsigned char *b, enum byteorder o)
{
if (o == BIG)
return ((b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]);
else
return ((b[3] << 24) | (b[2] << 16) | (b[1] << 8) | b[0]);
}
/*
* Write an unsigned 4-byte int to a buffer.
*/
void
byte4exif(u_int32_t n, unsigned char *b, enum byteorder o)
{
int i;
if (o == BIG)
for (i = 0; i < 4; i++)
b[3 - i] = (unsigned char)((n >> (i * 8)) & 0xff);
else
for (i = 0; i < 4; i++)
b[i] = (unsigned char)((n >> (i * 8)) & 0xff);
}
/*
* Read a signed 4-byte int from the buffer.
*/
int32_t
exif4sbyte(unsigned char *b, enum byteorder o)
{
if (o == BIG)
return ((b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]);
else
return ((b[3] << 24) | (b[2] << 16) | (b[1] << 8) | b[0]);
}
/*
* Lookup and allocate description for a value.
*/
char *
finddescr(struct descrip *table, u_int16_t val)
{
int i;
char *c;
for (i = 0; table[i].val != -1 && table[i].val != val; i++);
if (!(c = (char *)malloc(strlen(table[i].descr) + 1)))
exifdie((const char *)strerror(errno));
strcpy(c, table[i].descr);
return (c);
}
/*
* Lookup a property entry belonging to a particular set of tags.
*/
struct exifprop *
findprop(struct exifprop *prop, struct exiftag *tagset, u_int16_t tag)
{
for (; prop && (prop->tagset != tagset || prop->tag != tag);
prop = prop->next);
return (prop);
}
/*
* Allocate memory for an Exif property.
*/
struct exifprop *
newprop(void)
{
struct exifprop *prop;
prop = (struct exifprop *)malloc(sizeof(struct exifprop));
if (!prop)
exifdie((const char *)strerror(errno));
memset(prop, 0, sizeof(struct exifprop));
return (prop);
}
/*
* Given a parent, create a new child Exif property. These are
* typically used by maker note modules when a single tag may contain
* multiple items of interest.
*/
struct exifprop *
childprop(struct exifprop *parent)
{
struct exifprop *prop;
prop = newprop();
/* By default, the child inherits most values from its parent. */
prop->tag = parent->tag;
prop->type = TIFF_UNKN;
prop->name = parent->name;
prop->descr = parent->descr;
prop->lvl = parent->lvl;
prop->ifdseq = parent->ifdseq;
prop->par = parent;
prop->next = parent->next;
/* Now insert the new property into our list. */
parent->next = prop;
return (prop);
}
/*
* Allocate a buffer for a property's display string.
*/
void
exifstralloc(char **str, int len)
{
if (*str) {
exifwarn("tried to alloc over non-null string");
abort();
}
if (!(*str = (char *)calloc(1, len)))
exifdie((const char *)strerror(errno));
}
/*
* Print hex values of a buffer.
*/
void
hexprint(unsigned char *b, int len)
{
int i;
for (i = 0; i < len; i++)
printf(" %02X", b[i]);
}
/*
* Print debug info for a property.
*/
void
dumpprop(struct exifprop *prop, struct field *afield)
{
int i;
if (!debug) return;
for (i = 0; ftypes[i].type && ftypes[i].type != prop->type; i++);
if (afield) {
printf(" %s (0x%04X): %s, %d; %d\n", prop->name,
prop->tag, ftypes[i].name, prop->count,
prop->value);
printf(" ");
hexprint(afield->tag, 2);
printf(" |");
hexprint(afield->type, 2);
printf(" |");
hexprint(afield->count, 4);
printf(" |");
hexprint(afield->value, 4);
printf("\n");
} else
printf(" %s (0x%04X): %s, %d; %d, 0x%04X\n",
prop->name, prop->tag, ftypes[i].name,
prop->count, prop->value, prop->value);
}
/*
* Allocate and read an individual IFD. Takes the beginning and end of the
* Exif buffer, returns the IFD and an offset to the next IFD.
*/
u_int32_t
readifd(u_int32_t offset, struct ifd **dir, struct exiftag *tagset,
struct tiffmeta *md)
{
u_int32_t ifdsize;
unsigned char *b;
b = md->btiff;
/*
* Verify that we have a valid offset. Some maker note IFDs prepend
* a string and will screw us up otherwise (e.g., Olympus).
* (Number of directory entries is in the first 2 bytes.)
*/
if (b + offset + 2 > md->etiff) {
*dir = NULL;
return (0);
}
*dir = (struct ifd *)malloc(sizeof(struct ifd));
if (!*dir)
exifdie((const char *)strerror(errno));
(*dir)->num = exif2byte(b + offset, md->order);
(*dir)->par = NULL;
(*dir)->tagset = tagset;
(*dir)->md = *md;
(*dir)->next = NULL;
ifdsize = (*dir)->num * sizeof(struct field);
b += offset + 2;
/* Sanity check our sizes. */
if (b + ifdsize > md->etiff) {
free(*dir);
*dir = NULL;
return (0);
}
/* Point to our array of fields. */
(*dir)->fields = (struct field *)b;
/*
* While we're here, find the offset to the next IFD.
*
* Note that this offset isn't always going to be valid. It
* seems that some camera implementations of Exif ignore the spec
* and do not include the offset for all IFDs (e.g., maker note).
* Therefore, it may be necessary to call readifd() directly (in
* leiu of readifds()) to avoid problems when reading these non-
* standard IFDs.
*/
return ((b + ifdsize + 4 > md->etiff) ? 0 :
exif4byte(b + ifdsize, md->order));
}
/*
* Read a chain of IFDs. Takes the IFD offset and returns the first
* node in a chain of IFDs. Note that it can return NULL.
*/
struct ifd *
readifds(u_int32_t offset, struct exiftag *tagset, struct tiffmeta *md)
{
struct ifd *firstifd, *curifd;
/* Fetch our first one. */
offset = readifd(offset, &firstifd, tagset, md);
curifd = firstifd;
/* Fetch any remaining ones. */
while (offset) {
offset = readifd(offset, &(curifd->next), tagset, md);
curifd = curifd->next;
}
return (firstifd);
}
/*
* Euclid's algorithm to find the GCD.
*/
u_int32_t
gcd(u_int32_t a, u_int32_t b)
{
if (!b) return (a);
return (gcd(b, a % b));
}
|