#include <libexif/exif-utils.h>
#include <libexif/exif-loader.h>
#include <libexif/exif-log.h>
-
-#include <libjpeg/jpeg-marker.h>
+#include <libexif/i18n.h>
#include <libexif/olympus/exif-mnote-data-olympus.h>
#include <libexif/canon/exif-mnote-data-canon.h>
# define strncasecmp strnicmp
#endif
+#undef JPEG_MARKER_SOI
+#define JPEG_MARKER_SOI 0xd8
+#undef JPEG_MARKER_APP0
+#define JPEG_MARKER_APP0 0xe0
+#undef JPEG_MARKER_APP1
+#define JPEG_MARKER_APP1 0xe1
+
static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
struct _ExifDataPrivate
* the offset must be an even number. If we need to introduce
* a padding byte, we set it to 0.
*/
- if (s & 1) *ds++;
+ if (s & 1) (*ds)++;
*d = exif_mem_realloc (data->priv->mem, *d, *ds);
if (!*d) {
EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", *ds);
}
}
+static int
+cmp_func (const unsigned char *p1, const unsigned char *p2, ExifByteOrder o)
+{
+ ExifShort tag1 = exif_get_short (p1, o);
+ ExifShort tag2 = exif_get_short (p2, o);
+
+ return (tag1 < tag2) ? -1 : (tag1 > tag2) ? 1 : 0;
+}
+
+static int
+cmp_func_intel (const void *elem1, const void *elem2)
+{
+ return cmp_func ((const unsigned char *) elem1,
+ (const unsigned char *) elem2, EXIF_BYTE_ORDER_INTEL);
+}
+
+static int
+cmp_func_motorola (const void *elem1, const void *elem2)
+{
+ return cmp_func ((const unsigned char *) elem1,
+ (const unsigned char *) elem2, EXIF_BYTE_ORDER_MOTOROLA);
+}
+
static void
exif_data_save_data_content (ExifData *data, ExifContent *ifd,
unsigned char **d, unsigned int *ds,
{
unsigned int j, n_ptr = 0, n_thumb = 0;
ExifIfd i;
- ExifEntry *le = NULL;
if (!data || !data->priv || !ifd || !d || !ds) return;
(ExifShort) (ifd->count + n_ptr + n_thumb));
offset += 2;
- /*
- * Save each entry. TIFF specification requires entries to be sorted
- * by tags. Yes, the sorting routine can be improved. Please submit a
- * patch.
- */
+ /* Save each entry */
exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
"Saving %i entries (IFD '%s', offset: %i)...",
ifd->count, exif_ifd_get_name (i), offset);
- for (j = 0; j < ifd->count; j++) {
- ExifEntry *me = NULL;
- unsigned int k;
-
- for (k = 0; k < ifd->count; k++) {
- if (le && ((ifd->entries[k]->tag < le->tag) ||
- ((ifd->entries[k]->tag == le->tag) && (le <= ifd->entries[k]))))
- continue;
- if (!me || (me && ((me->tag > ifd->entries[k]->tag) ||
- ((me->tag == ifd->entries[k]->tag) && (me <= ifd->entries[k])))))
- me = ifd->entries[k];
- }
- if (!me) break;
- exif_data_save_data_entry (data, me, d, ds, offset + 12 * j);
- le = me;
- }
+ for (j = 0; j < ifd->count; j++)
+ exif_data_save_data_entry (data, ifd->entries[j], d, ds, offset + 12 * j);
offset += 12 * ifd->count;
break;
}
+ /* Sort the directory according to TIFF specification */
+ qsort (*d + 6 + offset - (ifd->count + n_ptr + n_thumb) * 12,
+ (ifd->count + n_ptr + n_thumb), 12,
+ data->priv->order == EXIF_BYTE_ORDER_INTEL ? cmp_func_intel : cmp_func_motorola);
+
/* Correctly terminate the directory */
if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
data->size)) {
if (!em) return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
/* Canon */
- if (!strcmp (exif_entry_get_value (em, value, sizeof(value)), "Canon"))
+ if (!strcmp (exif_entry_get_value (em, value, sizeof (value)), "Canon"))
return EXIF_DATA_TYPE_MAKER_NOTE_CANON;
/* Pentax & some variant of Nikon */
return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
}
+#define LOG_TOO_SMALL \
+exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData", \
+ _("Size of data too small to allow for EXIF data."));
+
void
exif_data_load_data (ExifData *data, const unsigned char *d_orig,
unsigned int ds_orig)
* not, search the EXIF marker.
*/
if (ds < 6) {
- exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
- "Size too small.");
+ LOG_TOO_SMALL;
return;
}
if (!memcmp (d, ExifHeader, 6)) {
break;
/* Unknown marker or data. Give up. */
- exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
- "ExifData", "EXIF marker not found.");
+ exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
+ "ExifData", _("EXIF marker not found."));
return;
}
d++;
ds--;
if (ds < 2) {
- exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
- "ExifData", "Size too small.");
+ LOG_TOO_SMALL;
return;
}
len = (d[0] << 8) | d[1];
* (offset 2, length 6).
*/
if (ds < 6) {
- exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
- "Size too small.");
+ LOG_TOO_SMALL;
return;
}
if (memcmp (d, ExifHeader, 6)) {
- exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
- "EXIF header not found.");
+ exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
+ "ExifData", _("EXIF header not found."));
return;
}
data->priv->order = EXIF_BYTE_ORDER_INTEL;
else if (!memcmp (d + 6, "MM", 2))
data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
- else
+ else {
+ exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
+ "ExifData", _("Unknown encoding."));
return;
+ }
/* Fixed value */
if (exif_get_short (d + 8, data->priv->order) != 0x002a)
/* Sanity check. */
if (offset > ds - 6) {
- exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG,
- "ExifData", "Bogus offset!");
+ exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
+ "ExifData", "Bogus offset.");
return;
}