+#include "imager.h"
#include "imexif.h"
#include <stdlib.h>
#include <float.h>
+#include <string.h>
+#include <stdio.h>
/*
=head1 NAME
=head1 SYNOPSIS
- if (i_int_decode_exif(im, app1data, app1datasize)) {
+ if (im_decode_exif(im, app1data, app1datasize)) {
// exif block seen
}
static int tiff_load_ifd(imtiff *tiff, unsigned long offset);
static void tiff_final(imtiff *tiff);
static void tiff_clear_ifd(imtiff *tiff);
+#if 0 /* currently unused, but that may change */
static int tiff_get_bytes(imtiff *tiff, unsigned char *to, size_t offset,
size_t count);
+#endif
static int tiff_get_tag_double(imtiff *, int index, double *result);
static int tiff_get_tag_int(imtiff *, int index, int *result);
static unsigned tiff_get16(imtiff *, unsigned long offset);
=over
-=item i_int_decode_exit
+=item im_decode_exif
-i_int_decode_exif(im, data_base, data_size);
+im_decode_exif(im, data_base, data_size);
-The data from data_base for data_size bytes will be scanned for EXIF
-data.
+The data from C<data_base> for C<data_size> bytes will be scanned for
+EXIF data.
Any data found will be used to set tags in the supplied image.
write to the log. In no case should this code exit when supplied
invalid data.
-Returns true if an Exif header was seen.
+Returns true if an EXIF header was seen.
+=cut
*/
int
-i_int_decode_exif(i_img *im, unsigned char *data, size_t length) {
+im_decode_exif(i_img *im, unsigned char *data, size_t length) {
imtiff tiff;
unsigned long exif_ifd_offset = 0;
unsigned long gps_ifd_offset = 0;
- /* basic checks - must start with "Exif\0\0" */
-
- if (length < 6 || memcmp(data, "Exif\0\0", 6) != 0) {
- return 0;
- }
-
- data += 6;
- length -= 6;
if (!tiff_init(&tiff, data, length)) {
mm_log((2, "Exif header found, but no valid TIFF header\n"));
memcpy(user_comment, tiff->base + entry->offset, entry->size);
/* the first 8 bytes indicate the encoding, make them into spaces
for better presentation */
- for (i = 0; i < 8; ++i) {
+ for (i = 0; i < entry->size && i < 8; ++i) {
if (user_comment[i] == '\0')
user_comment[i] = ' ';
}
/* find the actual end of the string */
while (i < entry->size && user_comment[i])
++i;
- i_tags_add(&im->tags, "exif_user_comment", 0, user_comment, i, 0);
+ i_tags_set(&im->tags, "exif_user_comment", user_comment, i);
myfree(user_comment);
break;
/* rough check count + 1 entry + next offset */
if (offset + (2+12+4) > tiff->size) {
- mm_log((2, "offset %uld beyond end off Exif block"));
+ mm_log((2, "offset %lu beyond end off Exif block", offset));
return 0;
}
/* check we can fit the whole thing */
ifd_size = 2 + count * 12 + 4; /* count + count entries + next offset */
if (offset + ifd_size > tiff->size) {
- mm_log((2, "offset %uld beyond end off Exif block"));
+ mm_log((2, "offset %lu beyond end off Exif block", offset));
return 0;
}
entry->tag = tiff_get16(tiff, base);
entry->type = tiff_get16(tiff, base+2);
entry->count = tiff_get32(tiff, base+4);
- if (entry->type >= 1 || entry->type <= ift_last) {
+ if (entry->type >= 1 && entry->type <= ift_last) {
entry->item_size = type_sizes[entry->type];
entry->size = entry->item_size * entry->count;
if (entry->size / entry->item_size != entry->count) {
+ myfree(entries);
mm_log((1, "Integer overflow calculating tag data size processing EXIF block\n"));
return 0;
}
ifd_entry *entry;
unsigned long offset;
if (index < 0 || index >= tiff->ifd_size) {
- m_fatal(3, "tiff_get_tag_double_array() tag index out of range");
+ mm_log((3, "tiff_get_tag_double_array() tag index out of range"));
+ return 0;
}
entry = tiff->ifd + index;
tiff_get_tag_double(imtiff *tiff, int index, double *result) {
ifd_entry *entry;
if (index < 0 || index >= tiff->ifd_size) {
- m_fatal(3, "tiff_get_tag_double() index out of range");
+ mm_log((3, "tiff_get_tag_double() index out of range"));
+ return 0;
}
entry = tiff->ifd + index;
ifd_entry *entry;
unsigned long offset;
if (index < 0 || index >= tiff->ifd_size) {
- m_fatal(3, "tiff_get_tag_int_array() tag index out of range");
+ mm_log((3, "tiff_get_tag_int_array() tag index out of range"));
+ return 0;
}
entry = tiff->ifd + index;
if (array_index < 0 || array_index >= entry->count) {
- m_fatal(3, "tiff_get_tag_int_array() array index out of range");
+ mm_log((3, "tiff_get_tag_int_array() array index out of range"));
+ return 0;
}
offset = entry->offset + array_index * entry->item_size;
tiff_get_tag_int(imtiff *tiff, int index, int *result) {
ifd_entry *entry;
if (index < 0 || index >= tiff->ifd_size) {
- m_fatal(3, "tiff_get_tag_int() index out of range");
+ mm_log((3, "tiff_get_tag_int() index out of range"));
+ return 0;
}
entry = tiff->ifd + index;
int value;
if (map[i].tag == entry->tag
&& tiff_get_tag_int(tiff, tag_index, &value)) {
- i_tags_addn(&im->tags, map[i].name, 0, value);
+ i_tags_setn(&im->tags, map[i].name, value);
break;
}
}
for (i = 0; i < map_count; ++i) {
if (map[i].tag == entry->tag) {
int len = entry->type == ift_ascii ? entry->size - 1 : entry->size;
- i_tags_add(&im->tags, map[i].name, 0,
- (char const *)(tiff->base + entry->offset), len, 0);
+ i_tags_set(&im->tags, map[i].name,
+ (char const *)(tiff->base + entry->offset), len);
break;
}
}
if (entry->type == ift_rational || entry->type == ift_srational) {
double value;
char workstr[MAX_ARRAY_STRING];
+ size_t len = 0, item_len;
*workstr = '\0';
for (j = 0; j < entry->count; ++j) {
if (!tiff_get_tag_double_array(tiff, tag_index, &value, j)) {
- m_fatal(3, "unexpected failure from tiff_get_tag_double_array(..., %d, ..., %d)\n", tag_index, j);
+ mm_log((3, "unexpected failure from tiff_get_tag_double_array(..., %d, ..., %d)\n", tag_index, j));
+ return;
+ }
+ if (len >= sizeof(workstr) - 1) {
+ mm_log((3, "Buffer would overflow reading tag %#x\n", entry->tag));
+ return;
}
- if (j)
+ if (j) {
strcat(workstr, " ");
- sprintf(workstr + strlen(workstr), "%.6g", value);
+ ++len;
+ }
+#ifdef IMAGER_SNPRINTF
+ item_len = snprintf(workstr + len, sizeof(workstr)-len, "%.6g", value);
+#else
+ item_len = sprintf(workstr + len, "%.6g", value);
+#endif
+ len += item_len;
}
- i_tags_add(&im->tags, map[i].name, 0, workstr, -1, 0);
+ i_tags_set(&im->tags, map[i].name, workstr, -1);
}
else if (entry->type == ift_short || entry->type == ift_long
|| entry->type == ift_sshort || entry->type == ift_slong
|| entry->type == ift_byte) {
int value;
char workstr[MAX_ARRAY_STRING];
+ size_t len = 0, item_len;
*workstr = '\0';
for (j = 0; j < entry->count; ++j) {
if (!tiff_get_tag_int_array(tiff, tag_index, &value, j)) {
- m_fatal(3, "unexpected failure from tiff_get_tag_int_array(..., %d, ..., %d)\n", tag_index, j);
+ mm_log((3, "unexpected failure from tiff_get_tag_int_array(..., %d, ..., %d)\n", tag_index, j));
+ return;
+ }
+ if (len >= sizeof(workstr) - 1) {
+ mm_log((3, "Buffer would overflow reading tag %#x\n", entry->tag));
+ return;
}
- if (j)
+ if (j) {
strcat(workstr, " ");
- sprintf(workstr + strlen(workstr), "%d", value);
+ ++len;
+ }
+#ifdef IMAGER_SNPRINTF
+ item_len = snprintf(workstr + len, sizeof(workstr) - len, "%d", value);
+#else
+ item_len = sprintf(workstr + len, "%d", value);
+#endif
+ len += item_len;
}
- i_tags_add(&im->tags, map[i].name, 0, workstr, -1, 0);
+ i_tags_set(&im->tags, map[i].name, workstr, -1);
}
break;
}
}
}
if (found) {
- i_tags_add(&im->tags, map[i].name, 0, found->name, -1, 0);
+ i_tags_set(&im->tags, map[i].name, found->name, -1);
}
break;
}
static unsigned
tiff_get16(imtiff *tiff, unsigned long offset) {
- if (offset + 2 > tiff->size)
- m_fatal(3, "attempt to get16 at %uld in %uld image", offset, tiff->size);
+ if (offset + 2 > tiff->size) {
+ mm_log((3, "attempt to get16 at %lu in %lu image", offset,
+ (unsigned long)tiff->size));
+ return 0;
+ }
if (tiff->type == tt_intel)
return tiff->base[offset] + 0x100 * tiff->base[offset+1];
static unsigned
tiff_get32(imtiff *tiff, unsigned long offset) {
- if (offset + 4 > tiff->size)
- m_fatal(3, "attempt to get16 at %uld in %uld image", offset, tiff->size);
+ if (offset + 4 > tiff->size) {
+ mm_log((3, "attempt to get16 at %lu in %lu image", offset,
+ (unsigned long)tiff->size));
+ return 0;
+ }
if (tiff->type == tt_intel)
return tiff->base[offset] + 0x100 * tiff->base[offset+1]
+ 0x10000 * tiff->base[offset+1] + 0x1000000 * tiff->base[offset];
}
+#if 0 /* currently unused, but that may change */
+
/*
=item tiff_get_bytes
return 1;
}
+#endif
+
/*
=item tiff_get16s
tiff_get16s(imtiff *tiff, unsigned long offset) {
int result;
- if (offset + 2 > tiff->size)
- m_fatal(3, "attempt to get16 at %uld in %uld image", offset, tiff->size);
+ if (offset + 2 > tiff->size) {
+ mm_log((3, "attempt to get16 at %lu in %lu image", offset,
+ (unsigned long)tiff->size));
+ return 0;
+ }
if (tiff->type == tt_intel)
result = tiff->base[offset] + 0x100 * tiff->base[offset+1];
tiff_get32s(imtiff *tiff, unsigned long offset) {
unsigned work;
- if (offset + 4 > tiff->size)
- m_fatal(3, "attempt to get16 at %uld in %uld image", offset, tiff->size);
+ if (offset + 4 > tiff->size) {
+ mm_log((3, "attempt to get16 at %lu in %lu image", offset,
+ (unsigned long)tiff->size));
+ return 0;
+ }
if (tiff->type == tt_intel)
work = tiff->base[offset] + 0x100 * tiff->base[offset+1]
static double
tiff_get_rat(imtiff *tiff, unsigned long offset) {
unsigned long numer, denom;
- if (offset + 8 > tiff->size)
- m_fatal(3, "attempt to get_rat at %lu in %lu image", offset, tiff->size);
+ if (offset + 8 > tiff->size) {
+ mm_log((3, "attempt to get_rat at %lu in %lu image", offset,
+ (unsigned long)tiff->size));
+ return 0;
+ }
numer = tiff_get32(tiff, offset);
denom = tiff_get32(tiff, offset+4);
static double
tiff_get_rats(imtiff *tiff, unsigned long offset) {
long numer, denom;
- if (offset + 8 > tiff->size)
- m_fatal(3, "attempt to get_rat at %lu in %lu image", offset, tiff->size);
+ if (offset + 8 > tiff->size) {
+ mm_log((3, "attempt to get_rat at %lu in %lu image", offset,
+ (unsigned long)tiff->size));
+ return 0;
+ }
numer = tiff_get32s(tiff, offset);
denom = tiff_get32s(tiff, offset+4);
=head1 AUTHOR
-Tony Cook <tony@imager.perl.org>
+Tony Cook <tonyc@cpan.org>
=head1 REVISION