/exiftags/exifutil.c

https://code.google.com/p/xee/ · C · 433 lines · 223 code · 84 blank · 126 comment · 53 complexity · ee636219f9fa90c4e3337dd8800d1ae1 MD5 · raw file

  1. /*
  2. * Copyright (c) 2001-2003, Eric M. Johnston <emj@postal.net>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by Eric M. Johnston.
  16. * 4. Neither the name of the author nor the names of any co-contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * $Id: exifutil.c,v 1.26 2004/11/06 18:36:46 ejohnst Exp $
  33. */
  34. /*
  35. * Utilities for dealing with Exif data.
  36. *
  37. */
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <stdio.h>
  41. #include <errno.h>
  42. #include "exif.h"
  43. #include "exifint.h"
  44. /*
  45. * Some global variables we all need.
  46. */
  47. int debug;
  48. const char *progname;
  49. /*
  50. * Logging and error functions.
  51. */
  52. void
  53. exifdie(const char *msg)
  54. {
  55. fprintf(stderr, "%s: %s\n", progname, msg);
  56. exit(1);
  57. }
  58. void
  59. exifwarn(const char *msg)
  60. {
  61. fprintf(stderr, "%s: %s\n", progname, msg);
  62. }
  63. void
  64. exifwarn2(const char *msg1, const char *msg2)
  65. {
  66. fprintf(stderr, "%s: %s (%s)\n", progname, msg1, msg2);
  67. }
  68. /*
  69. * Read an unsigned 2-byte int from the buffer.
  70. */
  71. u_int16_t
  72. exif2byte(unsigned char *b, struct tiffmeta *md)
  73. {
  74. if (b < md->btiff || b+2 > md->etiff) return 0;
  75. if (md->order == BIG)
  76. return ((b[0] << 8) | b[1]);
  77. else
  78. return ((b[1] << 8) | b[0]);
  79. }
  80. /*
  81. * Read a signed 2-byte int from the buffer.
  82. */
  83. int16_t
  84. exif2sbyte(unsigned char *b, struct tiffmeta *md)
  85. {
  86. if (b < md->btiff || b+2 > md->etiff) return 0;
  87. if (md->order == BIG)
  88. return ((b[0] << 8) | b[1]);
  89. else
  90. return ((b[1] << 8) | b[0]);
  91. }
  92. /*
  93. * Read an unsigned 4-byte int from the buffer.
  94. */
  95. u_int32_t
  96. exif4byte(unsigned char *b, struct tiffmeta *md)
  97. {
  98. if (b < md->btiff || b+4 > md->etiff) return 0;
  99. if (md->order == BIG)
  100. return ((b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]);
  101. else
  102. return ((b[3] << 24) | (b[2] << 16) | (b[1] << 8) | b[0]);
  103. }
  104. /*
  105. * Write an unsigned 4-byte int to a buffer.
  106. */
  107. void
  108. byte4exif(u_int32_t n, unsigned char *b, enum byteorder o)
  109. {
  110. int i;
  111. if (o == BIG)
  112. for (i = 0; i < 4; i++)
  113. b[3 - i] = (unsigned char)((n >> (i * 8)) & 0xff);
  114. else
  115. for (i = 0; i < 4; i++)
  116. b[i] = (unsigned char)((n >> (i * 8)) & 0xff);
  117. }
  118. /*
  119. * Read a signed 4-byte int from the buffer.
  120. */
  121. int32_t
  122. exif4sbyte(unsigned char *b, struct tiffmeta *md)
  123. {
  124. if (b < md->btiff || b+4 > md->etiff) return 0;
  125. if (md->order == BIG)
  126. return ((b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]);
  127. else
  128. return ((b[3] << 24) | (b[2] << 16) | (b[1] << 8) | b[0]);
  129. }
  130. /*
  131. * Lookup and allocate description for a value.
  132. */
  133. char *
  134. finddescr(struct descrip *table, u_int16_t val)
  135. {
  136. int i;
  137. char *c;
  138. for (i = 0; table[i].val != -1 && table[i].val != val; i++);
  139. if (!(c = (char *)malloc(strlen(table[i].descr) + 1)))
  140. exifdie((const char *)strerror(errno));
  141. strcpy(c, table[i].descr);
  142. return (c);
  143. }
  144. /*
  145. * Lookup and append description for a value.
  146. * Doesn't do anything if the value is unknown; first adds ", " if dest
  147. * contains a value; returns number of bytes added. len is total size
  148. * of destination buffer.
  149. */
  150. int
  151. catdescr(char *c, struct descrip *table, u_int16_t val, int len)
  152. {
  153. int i, l;
  154. l = 0;
  155. len -= 1;
  156. c[len] = '\0';
  157. for (i = 0; table[i].val != -1 && table[i].val != val; i++);
  158. if (table[i].val == -1)
  159. return (0);
  160. if (strlen(c)) {
  161. strncat(c, ", ", len - strlen(c));
  162. l += 2;
  163. }
  164. strncat(c, table[i].descr, len - strlen(c));
  165. l += strlen(table[i].descr);
  166. return (l);
  167. }
  168. /*
  169. * Lookup a property entry belonging to a particular set of tags.
  170. */
  171. struct exifprop *
  172. findprop(struct exifprop *prop, struct exiftag *tagset, u_int16_t tag)
  173. {
  174. for (; prop && (prop->tagset != tagset || prop->tag != tag);
  175. prop = prop->next);
  176. return (prop);
  177. }
  178. /*
  179. * Allocate memory for an Exif property.
  180. */
  181. struct exifprop *
  182. newprop(void)
  183. {
  184. struct exifprop *prop;
  185. prop = (struct exifprop *)malloc(sizeof(struct exifprop));
  186. if (!prop)
  187. exifdie((const char *)strerror(errno));
  188. memset(prop, 0, sizeof(struct exifprop));
  189. return (prop);
  190. }
  191. /*
  192. * Given a parent, create a new child Exif property. These are
  193. * typically used by maker note modules when a single tag may contain
  194. * multiple items of interest.
  195. */
  196. struct exifprop *
  197. childprop(struct exifprop *parent)
  198. {
  199. struct exifprop *prop;
  200. prop = newprop();
  201. /* By default, the child inherits most values from its parent. */
  202. prop->tag = parent->tag;
  203. prop->type = TIFF_UNKN;
  204. prop->name = parent->name;
  205. prop->descr = parent->descr;
  206. prop->lvl = parent->lvl;
  207. prop->ifdseq = parent->ifdseq;
  208. prop->par = parent;
  209. prop->next = parent->next;
  210. /* Now insert the new property into our list. */
  211. parent->next = prop;
  212. return (prop);
  213. }
  214. /*
  215. * Allocate a buffer for a property's display string.
  216. */
  217. void
  218. exifstralloc(char **str, int len)
  219. {
  220. if (*str) {
  221. exifwarn("tried to alloc over non-null string");
  222. // XeeHack
  223. free(*str);
  224. //abort();
  225. }
  226. if (!(*str = (char *)calloc(1, len)))
  227. exifdie((const char *)strerror(errno));
  228. }
  229. /*
  230. * Print hex values of a buffer.
  231. */
  232. void
  233. hexprint(unsigned char *b, int len)
  234. {
  235. int i;
  236. for (i = 0; i < len; i++)
  237. printf(" %02X", b[i]);
  238. }
  239. /*
  240. * Print debug info for a property.
  241. */
  242. void
  243. dumpprop(struct exifprop *prop, struct field *afield)
  244. {
  245. int i;
  246. if (!debug) return;
  247. for (i = 0; ftypes[i].type && ftypes[i].type != prop->type; i++);
  248. if (afield) {
  249. printf(" %s (0x%04X): %s, %d; %d\n", prop->name,
  250. prop->tag, ftypes[i].name, prop->count,
  251. prop->value);
  252. printf(" ");
  253. hexprint(afield->tag, 2);
  254. printf(" |");
  255. hexprint(afield->type, 2);
  256. printf(" |");
  257. hexprint(afield->count, 4);
  258. printf(" |");
  259. hexprint(afield->value, 4);
  260. printf("\n");
  261. } else
  262. printf(" %s (0x%04X): %s, %d; %d, 0x%04X\n",
  263. prop->name, prop->tag, ftypes[i].name,
  264. prop->count, prop->value, prop->value);
  265. }
  266. /*
  267. * Allocate and read an individual IFD. Takes the beginning and end of the
  268. * Exif buffer, returns the IFD and an offset to the next IFD.
  269. */
  270. u_int32_t
  271. readifd(u_int32_t offset, struct ifd **dir, struct exiftag *tagset,
  272. struct tiffmeta *md)
  273. {
  274. u_int32_t ifdsize;
  275. unsigned char *b;
  276. b = md->btiff;
  277. /*
  278. * Verify that we have a valid offset. Some maker note IFDs prepend
  279. * a string and will screw us up otherwise (e.g., Olympus).
  280. * (Number of directory entries is in the first 2 bytes.)
  281. */
  282. if (b + offset + 2 > md->etiff) {
  283. *dir = NULL;
  284. return (0);
  285. }
  286. *dir = (struct ifd *)malloc(sizeof(struct ifd));
  287. if (!*dir)
  288. exifdie((const char *)strerror(errno));
  289. (*dir)->num = exif2byte(b + offset, md);
  290. (*dir)->par = NULL;
  291. (*dir)->tagset = tagset;
  292. (*dir)->md = *md;
  293. (*dir)->next = NULL;
  294. ifdsize = (*dir)->num * sizeof(struct field);
  295. b += offset + 2;
  296. /* Sanity check our sizes. */
  297. if (b + ifdsize > md->etiff) {
  298. free(*dir);
  299. *dir = NULL;
  300. return (0);
  301. }
  302. /* Point to our array of fields. */
  303. (*dir)->fields = (struct field *)b;
  304. /*
  305. * While we're here, find the offset to the next IFD.
  306. *
  307. * Note that this offset isn't always going to be valid. It
  308. * seems that some camera implementations of Exif ignore the spec
  309. * and do not include the offset for all IFDs (e.g., maker note).
  310. * Therefore, it may be necessary to call readifd() directly (in
  311. * leiu of readifds()) to avoid problems when reading these non-
  312. * standard IFDs.
  313. */
  314. return exif4byte(b + ifdsize, md);
  315. }
  316. /*
  317. * Read a chain of IFDs. Takes the IFD offset and returns the first
  318. * node in a chain of IFDs. Note that it can return NULL.
  319. */
  320. struct ifd *
  321. readifds(u_int32_t offset, struct exiftag *tagset, struct tiffmeta *md)
  322. {
  323. struct ifd *firstifd, *curifd;
  324. /*
  325. * XXX Note: we really should be checking to see if this IFD
  326. * has already been visited...
  327. */
  328. /* Fetch our first one. */
  329. offset = readifd(offset, &firstifd, tagset, md);
  330. curifd = firstifd;
  331. /* Fetch any remaining ones. */
  332. while (offset) {
  333. // XeeFix
  334. u_int32_t nextoffset = readifd(offset, &(curifd->next), tagset, md);
  335. if (nextoffset == offset) break;
  336. offset = nextoffset;
  337. curifd = curifd->next;
  338. }
  339. return (firstifd);
  340. }
  341. /*
  342. * Euclid's algorithm to find the GCD.
  343. */
  344. u_int32_t
  345. gcd(u_int32_t a, u_int32_t b)
  346. {
  347. if (!b) return (a);
  348. return (gcd(b, a % b));
  349. }