/* TODO */ ifd_entry_t TIFFGetRawIFDEntry( TIFF * tif, tag_t tag) { int tagidx = -1; int i; for (i= 0; i < TIFFGetRawTagListCount( tif ); i++) { if (tag == TIFFGetRawTagListEntry( tif, i ) && tag > 253) { tagidx= i; break; }; }; if (tagidx >= 0) { ifd_entry_t ifd_entry; ifd_entry = TIFFGetRawTagIFDListEntry( tif, tagidx ); return ifd_entry; } else { /* tag not defined */ fprintf(stderr, "tag %u (%s) should exist, because defined\n", tag, TIFFTagName(tif, tag)); exit(EXIT_FAILURE); } }
/* reads the datatype of given tag on specified TIFF, * because FieldType of libtiff does not return the true value (because it maps * its own datastructure), we need to use this function instead * @param tif pointer to TIFF structure * @param tag tag * @return datatype of given tag * if tag does not exists the function aborts with an error */ TIFFDataType TIFFGetRawTagType(TIFF * tif, tag_t tag) { int tagidx = -1; int i; for (i= 0; i < TIFFGetRawTagListCount( tif ); i++) { if (tag == TIFFGetRawTagListEntry( tif, i ) && tag > 253) { tagidx= i; break; }; }; if (tagidx >= 0) { TIFFDataType datatype = TIFFGetRawTagTypeListEntry( tif, tagidx ); #ifdef DEBUG printf("### datatype=%i \n", datatype); #endif return datatype; } else { /* tag not defined */ fprintf(stderr, "tag %u (%s) should exist, because defined\n", tag, TIFFTagName(tif, tag)); exit(EXIT_FAILURE); } }
ret_t check_tagorder(TIFF* tif) { printf("check if tags are in ascending order\n"); int count = TIFFGetRawTagListCount( tif); int fd = TIFFFileno( tif); //printf("count %i\n", count); /* 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); if (read(fd, 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 (TIFFIsByteSwapped(tif)) 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 ); tif_fails("Invalid TIFF directory; tags are not sorted in ascending order, previous tag:%u (%s) , actual tag:%u (%s)\n", lasttag, TIFFTagName(tif, lasttag), tag, TIFFTagName(tif, tag)); } lasttag = tag; e+=10; } /* loop each tag until end or given tag found */ free( ifdentries ); ret_t res; res.returnmsg=NULL; res.returncode=0; return res; }
ret_t check_tagorder(ctiff_t * ctif) { tif_rules("tags are in ascending order"); thandle_t client = TIFFClientdata(ctif->tif); TIFFReadWriteProc readproc = TIFFGetReadProc(ctif->tif); TIFFSeekProc seekproc = TIFFGetSeekProc(ctif->tif); if (! seekproc) { perror ("could not get TIFFGetSeekProc"); } if (! readproc) { perror ("could not get TIFFGetReadProc"); } 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); /* if (read(fd, ifdentries, 12 * count) != 12*count) { perror ("TIFF Header read error5"); exit(EXIT_FAILURE); } */ seekproc(client, offset+2, SEEK_SET); if ( readproc( client, 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); } lasttag = tag; e+=10; } /* loop each tag until end or given tag found */ free( ifdentries ); ret_t res; res.returnmsg=NULL; res.returncode=0; return res; }
ret_t check_tag_has_value_in_range(TIFF* tif, tag_t tag, unsigned int a, unsigned int b) { printf("check if tag %u (%s) has value in range %u - %u\n", tag, TIFFTagName(tif, tag), a, b); tifp_check( tif) if (a > b) { unsigned int c=a; a=b; b=c; } TIFFDataType datatype = TIFFGetRawTagType( tif, tag ); switch (datatype) { case TIFF_LONG: { uint32 val; int found=TIFFGetField(tif, tag, &val); if (1 == found) { if ((val >= a && val <= b )) { ret_t res; res.returnmsg=NULL; res.returncode=0; return res; } else { tif_fails("tag %u (%s) should have value in range %u - %u, but have count/value=%u\n", tag,TIFFTagName(tif, tag), a, b, val); } } else { tif_fails("tag %u (%s) should exist, because defined\n", tag,TIFFTagName(tif, tag)); } break; } case TIFF_SHORT: { uint16 val; int found=TIFFGetField(tif, tag, &val); if (1 == found) { if ((val >= a && val <= b )) { ret_t res; res.returnmsg=NULL; res.returncode=0; return res; } else { tif_fails("tag %u (%s) should have value in range %u - %u, but have count/value=%u\n", tag,TIFFTagName(tif, tag), a, b, val); } } else { tif_fails("tag %u (%s) should exist, because defined\n", tag, TIFFTagName(tif, tag)); } break; } case TIFF_RATIONAL: { float val; int found=TIFFGetField(tif, tag, &val); if (1 == found) { if ((val >= a && val <= b )) { ret_t res; res.returnmsg=NULL; res.returncode=0; return res; } else { tif_fails("tag %u (%s) should have value in range %u - %u, but have count/value=%f\n", tag,TIFFTagName(tif, tag), a, b, val); } } else { tif_fails("tag %u (%s) should exist, because defined\n", tag,TIFFTagName(tif, tag)); } break; } default: /* none */ tif_fails("tag %u (%s) should have values of type long, short or float, but was:%i\n", tag,TIFFTagName(tif, tag), datatype); } }