/*  check if only one IFD exists */
ret_t check_has_only_one_ifd(ctiff_t * ctif) {
  //printf("check if only one IFD exists\n");
  tif_rules("only one IFD exists");
  /* next commented lines, because TIFFNumberOfDirectories are in endless loop,
   * if the TIFF file from https://github.com/EasyinnovaSL/DPFManager/blob/develop/src/test/resources/IFD%20struct/Circular%20E.tif
   */
  /* seek the image file directory (bytes 4-7) */
  uint32 offset = get_ifd0_pos(ctif );
  uint32 IFDn = get_next_ifd_pos( ctif, offset );
  if (0 == IFDn) {
    ret_t res;
    res.returnmsg=NULL;
    res.returncode=0;
    return res;
  } else {
    // FIXME: tif_fails?
      char array[TIFFAILSTRLEN];
      snprintf(array, sizeof(array), "baseline TIFF should have only one IFD, but IFD0 at 0x%08x has pointer to IFDn 0x%08x", offset, IFDn );
      return tif_fails( array);
  }
}
예제 #2
0
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);
  }

}