Permalink
Browse files

- added ctif->tag_cache[] to minimize TIFFGetRawTagListIndex() proces…

…sing time
  • Loading branch information...
Andreas Romeyke
Andreas Romeyke committed Jan 18, 2017
1 parent 3c98d59 commit 22ced8023577c25e982ed301bf7dee4f14792fe6
Showing with 79 additions and 42 deletions.
  1. +5 −1 src/headers/check.h
  2. +37 −4 src/helper/check_tiffparse.c
  3. +37 −37 src/ifdrules/check_tagorder.c
View
@@ -15,11 +15,16 @@
typedef int thandle_t;
+typedef uint16 tag_t;
typedef enum{ is_memmap, is_filep } ct_ioflag_t ; /* flag */
+typedef enum{ has_sorted_tags, has_unsorted_tags, unknown_tag_order } tagorder_t;
+
typedef struct ctiff_s {
ct_ioflag_t ioflag;
+ tagorder_t tagorder;
+ int tag_cache[65536];
void * streamp; /* TODO: adress of memory-mapped tif stream */
void * actual_streamp;
uint32 streamlen; /* TODO: length of tif stream/file (in bytes) */
@@ -75,7 +80,6 @@ typedef struct offset_s {
};
} offset_t;
-typedef uint16 tag_t;
typedef enum {
mt_unused, /* memory areas, which are not referenced within TIFF file */
@@ -521,10 +521,39 @@ const char * TIFFTagName( tag_t tag ) {
}
int TIFFGetRawTagListIndex(ctiff_t * ctif, tag_t tag) { /* find n-th entry in IFD for given tag, return -1 if not found */
- for (int i= 0; i < get_ifd0_count( ctif ); i++) {
- if (tag > 253 && tag == TIFFGetRawTagListEntry( ctif, i )) {
- return i;
- };
+ /*
+ for (int i= 0; i < get_ifd0_count( ctif ); i++) {
+ if (tag > 253 && tag == TIFFGetRawTagListEntry( ctif, i )) {
+ return i;
+ };
+ }
+ */
+
+ switch (ctif->tagorder) {
+ case unknown_tag_order:
+ {
+ ctif->tagorder=has_sorted_tags;
+ tag_t last = TIFFGetRawTagListEntry(ctif, 0);
+ for (int i= 1; i < get_ifd0_count( ctif ); i++) {
+ tag_t current = TIFFGetRawTagListEntry( ctif, i );
+ if (last >= current) {
+ ctif->tagorder=has_unsorted_tags;
+ break;
+ }
+ last = current;
+ };
+ if (has_sorted_tags == ctif->tagorder) {
+ for (int i= 0; i < get_ifd0_count( ctif ); i++) {
+ tag_t tag = TIFFGetRawTagListEntry( ctif, i );
+ ctif->tag_cache[tag] = i;
+ }
+ return ctif->tag_cache[tag];
+ }
+ }
+ break;
+ case has_unsorted_tags:
+ case has_sorted_tags:
+ return ctif->tag_cache[tag];
}
return -1;
}
@@ -1092,6 +1121,10 @@ ctiff_t * initialize_ctif(const char * tiff_file, ct_ioflag_t ioflag) {
ctif->streamlen = fsize(tif);
ctif->streamp = NULL;
ctif->actual_streamp = NULL;
+ ctif->tagorder=unknown_tag_order;
+ for (int i= 0; i < 65536; i++) {
+ ctif->tag_cache[i]= -1;
+ }
switch (ioflag) {
case is_filep: {
/* streamlen */
@@ -12,46 +12,46 @@
ret_t check_tagorder(ctiff_t * ctif) {
tif_rules("tags are in ascending order");
+ if (has_sorted_tags != ctif->tagorder) {
+ uint32 offset = get_ifd0_pos(ctif);
+ int count = get_ifd0_count(ctif);
+
+ /* read count of tags (2 Bytes) */
+ int i;
+ /* replace i/o operatrions with in-memory-operations */
+ uint8 * ifdentries = NULL;
+ ifdentries = malloc ( sizeof(uint8) * 12 * count);
+ ct_seek(ctif, offset+2, SEEK_SET);
+
+ if ( ct_read( ctif, ifdentries, 12 * count) != 12*count ) {
+ perror ("TIFF Header read error5");
+ exit( EXIT_FAILURE );
+ }
-
- uint32 offset = get_ifd0_pos(ctif);
- int count = get_ifd0_count(ctif);
-
- /* read count of tags (2 Bytes) */
- int i;
- /* replace i/o operatrions with in-memory-operations */
- uint8 * ifdentries = NULL;
- ifdentries = malloc ( sizeof(uint8) * 12 * count);
- ct_seek(ctif, offset+2, SEEK_SET);
-
- if ( ct_read( ctif, ifdentries, 12 * count) != 12*count ) {
- perror ("TIFF Header read error5");
- exit( EXIT_FAILURE );
- }
-
- uint8 * e = ifdentries;
- uint16 lasttag = 0;
- for (i = 0; i<count; i++) {
- uint8 lo = *e;
- e++;
- uint8 hi = *e;
- uint16 tag = (hi << 8) + lo;
- e++;
- if (is_byteswapped(ctif))
- TIFFSwabShort(&tag);
- if (i>0 && lasttag >= tag) {
- // printf("tag idx=%i, tag=%u (0x%04x) (0x%02x) (0x%02x)\n", i, tag, tag, hi, lo);
- free( ifdentries );
- // FIXME: tif_fails?
- char array[TIFFAILSTRLEN];
- snprintf(array, sizeof(array), "Invalid TIFF directory; tags are not sorted in ascending order, previous tag:%u (%s) , actual tag:%u (%s) at pos %i of %i\n", lasttag, TIFFTagName(lasttag), tag, TIFFTagName(tag), i, count);
- return tif_fails(array);
+ uint8 * e = ifdentries;
+ uint16 lasttag = 0;
+ for (i = 0; i<count; i++) {
+ uint8 lo = *e;
+ e++;
+ uint8 hi = *e;
+ uint16 tag = (hi << 8) + lo;
+ e++;
+ if (is_byteswapped(ctif))
+ TIFFSwabShort(&tag);
+ if (i>0 && lasttag >= tag) {
+ // printf("tag idx=%i, tag=%u (0x%04x) (0x%02x) (0x%02x)\n", i, tag, tag, hi, lo);
+ free( ifdentries );
+ // FIXME: tif_fails?
+ char array[TIFFAILSTRLEN];
+ snprintf(array, sizeof(array), "Invalid TIFF directory; tags are not sorted in ascending order, previous tag:%u (%s) , actual tag:%u (%s) at pos %i of %i\n", lasttag, TIFFTagName(lasttag), tag, TIFFTagName(tag), i, count);
+ return tif_fails(array);
+ }
+ lasttag = tag;
+ e+=10;
}
- lasttag = tag;
- e+=10;
+ /* loop each tag until end or given tag found */
+ free( ifdentries );
}
- /* loop each tag until end or given tag found */
- free( ifdentries );
ret_t res;
res.returnmsg=NULL;
res.returncode=0;

0 comments on commit 22ced80

Please sign in to comment.