| 1 |
/* 'checkit_tiff' is a conformance checker for baseline TIFFs |
| 2 |
* |
| 3 |
* author: Andreas Romeyke, 2015-2022 |
| 4 |
* licensed under conditions of libtiff |
| 5 |
* (see http://libtiff.maptools.org/misc.html) |
| 6 |
* |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <math.h> |
| 10 |
#include "check.h" |
| 11 |
#include "check_helper.h" |
| 12 |
#include <assert.h> |
| 13 |
#include <fcntl.h> |
| 14 |
#include "ctstring.h" |
| 15 |
#include <errno.h> |
| 16 |
#include <string.h> |
| 17 |
#ifdef HAVE_MMAP |
| 18 |
#include <sys/mman.h> |
| 19 |
#endif |
| 20 |
#include <endian.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <unistd.h> |
| 23 |
|
| 24 |
|
| 25 |
off_t ct_seek(ctiff_t * ctif, off_t pos, int whence) { |
| 26 |
#ifdef HAVE_MMAP |
| 27 |
switch (ctif->ioflag) { |
| 28 |
case is_filep: |
| 29 |
assert(ctif->fd >= 0); |
| 30 |
// TODO: add checks if seek will be outside of file!!! |
| 31 |
return lseek(ctif->fd, pos, whence); |
| 32 |
break; |
| 33 |
|
| 34 |
case is_memmap: |
| 35 |
assert( ctif->streamp != NULL); |
| 36 |
assert( ctif->actual_streamp != NULL); |
| 37 |
switch (whence) { |
| 38 |
case SEEK_SET: |
| 39 |
ctif->actual_streamp = ctif->streamp + pos; |
| 40 |
break; |
| 41 |
case SEEK_CUR: |
| 42 |
ctif->actual_streamp+=pos; |
| 43 |
break; |
| 44 |
case SEEK_END: |
| 45 |
ctif->actual_streamp = ctif->streamp + ctif->streamlen + pos; |
| 46 |
break; |
| 47 |
} |
| 48 |
return ctif->actual_streamp - ctif->streamp; |
| 49 |
break; |
| 50 |
} |
| 51 |
#else |
| 52 |
assert(ctif->fd >= 0); |
| 53 |
// TODO: add checks if seek will be outside of file!!! |
| 54 |
return lseek(ctif->fd, pos, whence); |
| 55 |
#endif |
| 56 |
return no_file_found; /* should not occur */ |
| 57 |
} |
| 58 |
|
| 59 |
static ssize_t ct_read_intern(ctiff_t * ctif, void *buf, size_t count) { |
| 60 |
if (NULL == buf) { |
| 61 |
perror("Buffer buf is a NULL-pointer"); |
| 62 |
exit( EXIT_FAILURE ); |
| 63 |
} |
| 64 |
#ifdef HAVE_MMAP |
| 65 |
switch (ctif->ioflag) { |
| 66 |
case is_filep: |
| 67 |
// TODO: add checks if seek will be outside of file!!! |
| 68 |
assert(ctif->fd >= 0); |
| 69 |
return read(ctif->fd, buf, count); |
| 70 |
break; |
| 71 |
case is_memmap: { |
| 72 |
assert( ctif->streamp != NULL); |
| 73 |
assert( ctif->actual_streamp != NULL); |
| 74 |
|
| 75 |
off_t testpos = (ctif->actual_streamp+count) - (ctif->streamp); |
| 76 |
if ( (testpos > 0) && ((size_t) testpos > ctif->streamlen)) { |
| 77 |
/* |
| 78 |
* fprintf(stderr, "read offset outside of file on new pos=%li (filesize=%lu)\n", testpos, ctif->streamlen); |
| 79 |
* exit(EXIT_FAILURE); |
| 80 |
*/ |
| 81 |
return no_file_found; |
| 82 |
} |
| 83 |
if ( testpos < 0 ) { |
| 84 |
/* |
| 85 |
* fprintf(stderr, "read offset outside of file on new pos=%li (filesize=%lu)\n", testpos, ctif->streamlen); |
| 86 |
* exit(EXIT_FAILURE); |
| 87 |
*/ |
| 88 |
return no_file_found; |
| 89 |
} |
| 90 |
memcpy(buf, ctif->actual_streamp, count); |
| 91 |
ctif->actual_streamp+=count; |
| 92 |
return count; |
| 93 |
/* break; */ |
| 94 |
} |
| 95 |
} |
| 96 |
#else |
| 97 |
// TODO: add checks if seek will be outside of file!!! |
| 98 |
assert(ctif->fd >= 0); |
| 99 |
return read(ctif->fd, buf, count); |
| 100 |
#endif |
| 101 |
return no_file_found; /* should not occur */ |
| 102 |
} |
| 103 |
|
| 104 |
ssize_t ct_read8(ctiff_t * ctif, uint8 *buf, size_t byte_count) { |
| 105 |
return ct_read_intern(ctif, buf, byte_count); |
| 106 |
} |
| 107 |
static ssize_t ct_read8s(ctiff_t * ctif, int8 *buf, size_t byte_count) { |
| 108 |
return ct_read_intern(ctif, buf, byte_count); |
| 109 |
} |
| 110 |
|
| 111 |
ssize_t ct_read16(ctiff_t * ctif, uint16 *buf, size_t word_count) { |
| 112 |
size_t count = word_count * sizeof(uint16); |
| 113 |
ssize_t rdbytes = ct_read_intern(ctif, buf, count); |
| 114 |
for (size_t i = 0; i < word_count; i++) { |
| 115 |
uint16 *tmpbuf = buf + i; |
| 116 |
*tmpbuf = htole16(*tmpbuf); |
| 117 |
} |
| 118 |
return rdbytes; |
| 119 |
} |
| 120 |
static ssize_t ct_read16s(ctiff_t * ctif, int16 *buf, size_t word_count) { |
| 121 |
size_t count = word_count * sizeof(int16); |
| 122 |
ssize_t rdbytes = ct_read_intern(ctif, buf, count); |
| 123 |
for (size_t i = 0; i < word_count; i++) { |
| 124 |
int16 *tmpbuf = buf + i; |
| 125 |
/* unsure if this works as expected on negative values */ |
| 126 |
*tmpbuf = (int16) htole16((uint16) *tmpbuf); |
| 127 |
} |
| 128 |
return rdbytes; |
| 129 |
} |
| 130 |
|
| 131 |
ssize_t ct_read32(ctiff_t * ctif, uint32 *buf, size_t quad_count) { |
| 132 |
size_t count = quad_count * sizeof(uint32); |
| 133 |
ssize_t rdbytes = ct_read_intern(ctif, buf, count); |
| 134 |
//printf("buf32: %lu (0x%x) quadcount=%lu count=%lu rdbytes=%lu\n", *buf, *buf, quad_count, count, rdbytes); |
| 135 |
for (size_t i = 0; i < quad_count; i++) { |
| 136 |
uint32 * tmpbuf = buf+i; |
| 137 |
*tmpbuf = htole32( *tmpbuf ); |
| 138 |
} |
| 139 |
//printf("buf32': %lu (0x%x)\n", *buf, *buf); |
| 140 |
return rdbytes; |
| 141 |
} |
| 142 |
|
| 143 |
static ssize_t ct_read32s(ctiff_t * ctif, int32 *buf, size_t quad_count) { |
| 144 |
size_t count = quad_count * sizeof(int32); |
| 145 |
ssize_t rdbytes = ct_read_intern(ctif, buf, count); |
| 146 |
//printf("buf32: %lu (0x%x) quadcount=%lu count=%lu rdbytes=%lu\n", *buf, *buf, quad_count, count, rdbytes); |
| 147 |
for (size_t i = 0; i < quad_count; i++) { |
| 148 |
int32 * tmpbuf = buf+i; |
| 149 |
/* unsure if this works as expected on negative values */ |
| 150 |
*tmpbuf = (int32) htole32((uint32) *tmpbuf ); |
| 151 |
} |
| 152 |
//printf("buf32': %lu (0x%x)\n", *buf, *buf); |
| 153 |
return rdbytes; |
| 154 |
} |
| 155 |
|
| 156 |
static ssize_t ct_readf(ctiff_t * ctif, float *buf, size_t float_count) { |
| 157 |
size_t count = float_count * sizeof(float); |
| 158 |
ssize_t rdbytes = ct_read_intern(ctif, buf, count); |
| 159 |
return rdbytes; |
| 160 |
} |
| 161 |
|
| 162 |
static ssize_t ct_readd(ctiff_t * ctif, double *buf, size_t double_count) { |
| 163 |
size_t count = double_count * sizeof(double); |
| 164 |
ssize_t rdbytes = ct_read_intern(ctif, buf, count); |
| 165 |
return rdbytes; |
| 166 |
} |
| 167 |
|
| 168 |
int TIFFGetRawTagListIndex(const cifd_t *ifd, tag_t tag) { /* find n-th entry in IFD for given tag, return -1 if not found */ |
| 169 |
return ifd->index_cache_by_tag[tag]; |
| 170 |
} |
| 171 |
|
| 172 |
#ifdef __TINYC__ |
| 173 |
float fabsf(float f) { |
| 174 |
return (f >= 0.0) ? f : -f; |
| 175 |
} |
| 176 |
#endif |
| 177 |
|
| 178 |
//------------------------------------------------------------------------------ |
| 179 |
ret_t check_tag_has_fvalue(ctiff_t * ctif, cifd_t *ifd, tag_t tag, float value) { |
| 180 |
GET_EMPTY_RET(ret); |
| 181 |
TIFP_CHECK( ctif, ret); |
| 182 |
float * valp = NULL; |
| 183 |
uint32 found; |
| 184 |
ret =TIFFGetFieldRATIONAL(ctif, ifd, tag, &valp, &found); |
| 185 |
if ((1 == found) && (NULL != valp)) { |
| 186 |
float val = * valp; |
| 187 |
if ( fabsf(val - value) < 0.01 ) { |
| 188 |
ret.returncode=is_valid; |
| 189 |
} else { |
| 190 |
string_t msg = float2str(val); |
| 191 |
ret = set_value_found_ret(&ret, msg ) ; |
| 192 |
ret.returncode = tagerror_value_differs; |
| 193 |
} |
| 194 |
} |
| 195 |
free(valp); |
| 196 |
return ret; |
| 197 |
} |
| 198 |
|
| 199 |
//------------------------------------------------------------------------------ |
| 200 |
ret_t check_tag_has_u16value(ctiff_t * ctif, cifd_t *ifd, tag_t tag, uint16 value) { |
| 201 |
GET_EMPTY_RET(ret); |
| 202 |
TIFP_CHECK( ctif, ret); |
| 203 |
|
| 204 |
uint16 * valp = NULL; |
| 205 |
uint32 found; |
| 206 |
ret=TIFFGetFieldSHORT(ctif, ifd, tag, &valp, &found); |
| 207 |
if ((1 == found) && (NULL != valp)) { |
| 208 |
uint16 val = *valp; |
| 209 |
if ( val == value ) { |
| 210 |
ret.returncode=is_valid; |
| 211 |
} else { |
| 212 |
string_t msg = int2str(val); |
| 213 |
ret = set_value_found_ret_with_returncode(&ret, msg, tagerror_value_differs); |
| 214 |
} |
| 215 |
} |
| 216 |
free(valp); |
| 217 |
return ret; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
//------------------------------------------------------------------------------ |
| 222 |
ret_t check_tag_has_u32value(ctiff_t * ctif, cifd_t *ifd, tag_t tag, uint32 value) { |
| 223 |
GET_EMPTY_RET(ret); |
| 224 |
TIFP_CHECK( ctif, ret); |
| 225 |
|
| 226 |
uint32 * valp = NULL; |
| 227 |
uint32 found; |
| 228 |
ret=TIFFGetFieldLONG(ctif, ifd, tag, &valp, &found); |
| 229 |
if ((1 == found) && (NULL != valp)) { |
| 230 |
uint32 val = *valp; |
| 231 |
if ( val == value ) { |
| 232 |
ret.returncode=is_valid; |
| 233 |
} else { |
| 234 |
string_t msg = uint2str(val); |
| 235 |
ret = set_value_found_ret_with_returncode(&ret, msg, tagerror_value_differs); |
| 236 |
} |
| 237 |
} |
| 238 |
free(valp); |
| 239 |
return ret; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
ret_t parse_header_and_endianess(ctiff_t * ctif) { |
| 244 |
GET_EMPTY_RET(ret); |
| 245 |
TIFP_CHECK( ctif, ret); |
| 246 |
|
| 247 |
/* seek the image file directory (bytes 4-7) */ |
| 248 |
//ct_seek(fd, (off_t) 0, SEEK_SET); |
| 249 |
if ( ct_seek(ctif, 0, SEEK_SET) != 0) { |
| 250 |
ret = set_value_found_ret(&ret, str("TIFF header ct_seek error to 0")); |
| 251 |
ret.returncode=tiff_seek_error_header; |
| 252 |
return ret; |
| 253 |
} |
| 254 |
uint16 header; |
| 255 |
uint16 magic; |
| 256 |
int endianness; |
| 257 |
if ( ct_read16( ctif, &header, 1) != 2 ) { |
| 258 |
ret = set_value_found_ret(&ret, str("TIFF Header ct_read error to magic byte header (first 2 bytes)")); |
| 259 |
ret.returncode=tiff_read_error_header; |
| 260 |
return ret; |
| 261 |
} |
| 262 |
if (header == 0x4949) { endianness = 0; /* little endian */ |
| 263 |
} else if (header == 0x4d4d) { endianness = 1; /* big endian */ |
| 264 |
} else { |
| 265 |
char errmsg[VALUESTRLEN]=""; |
| 266 |
snprintf (errmsg, VALUESTRLEN, "TIFF Header error, not Byte Order Bytes for TIFF: 0x%04x", header); |
| 267 |
if (header == 0x4550) { |
| 268 |
strcat(errmsg, ", could be a Microsoft Document Image file (little endian), if header begins with by 0x45 0x50 0x2a 0x00"); |
| 269 |
} |
| 270 |
ret=set_value_found_ret(&ret, str(errmsg)); |
| 271 |
ret.returncode = tiff_byteorder_error; |
| 272 |
return ret; |
| 273 |
} |
| 274 |
ctif->isbyteswapped = endianness; |
| 275 |
if ( ct_read16( ctif, &magic, 1) != 2 ) { |
| 276 |
ret = set_value_found_ret(&ret, str("TIFF Header ct_read error to magic byte header (second 2 bytes == 42)")); |
| 277 |
ret.returncode=tiff_read_error_header; |
| 278 |
return ret; |
| 279 |
} |
| 280 |
|
| 281 |
uint16 magic2 = magic; |
| 282 |
if (endianness) { |
| 283 |
magic2 = TIFFSwabShort( magic2 ); /* big endian */ |
| 284 |
} |
| 285 |
ret.returncode = should_not_occur; |
| 286 |
if (magic2 == 42) { |
| 287 |
ret.returncode=is_valid; |
| 288 |
} else { |
| 289 |
char errmsg[VALUESTRLEN]=""; |
| 290 |
snprintf (errmsg, VALUESTRLEN, "TIFF Header error, not a MAGIC BYTE for TIFF: 0x%04x\n", magic); |
| 291 |
if (magic2==0x002b) { strcat(errmsg, ", but could be a BigTIFF, see http://www.awaresystems.be/imaging/tiff/bigtiff.html"); |
| 292 |
} |
| 293 |
if (magic2==0x0055) { strcat(errmsg, ", but could be a Panasonic Raw/RW2, see http://libopenraw.freedesktop.org/wiki/Panasonic_RAW/"); |
| 294 |
} |
| 295 |
if (magic2==0x01bc) { strcat(errmsg, ", but could be a JPEG XR, see http://www.itu.int/rec/T-REC-T.832"); |
| 296 |
} |
| 297 |
if (magic2==0x314e) { strcat(errmsg, ", but could be a Navy Image FileFormat, see http://www.navsea.navy.mil/nswc/carderock/tecinfsys/cal-std/doc/28002c.pdf"); |
| 298 |
} |
| 299 |
if (magic2==0x4352) { strcat(errmsg, ", but could be a DNG camera profile, see http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/products/photoshop/pdfs/dng_spec_1.4.0.0.pdf"); |
| 300 |
} |
| 301 |
if (magic2==0x4f52) { strcat(errmsg, ", but could be an Olympus ORF, see http://libopenraw.freedesktop.org/wiki/Olympus_ORF/"); |
| 302 |
} |
| 303 |
if (magic2==0x5352) { strcat(errmsg, ", but could be an Olympus ORF, see http://libopenraw.freedesktop.org/wiki/Olympus_ORF/"); |
| 304 |
} |
| 305 |
ret = set_value_found_ret_with_returncode(&ret, str(errmsg), tiff_byteorder_error); |
| 306 |
} |
| 307 |
assert( ret.returncode != should_not_occur); |
| 308 |
return ret; |
| 309 |
} |
| 310 |
|
| 311 |
void empty_cache(cifd_t * ifd) { |
| 312 |
/* init with -1 indicating 'not found' */ |
| 313 |
memset(ifd->index_cache_by_tag, -1, sizeof(int)* MAXTAGS); |
| 314 |
memset(ifd->tag_cache_by_index, 0, sizeof(tag_t)* MAXTAGS); |
| 315 |
} |
| 316 |
|
| 317 |
ret_t get_IFD_at_pos(ctiff_t * ctif, uint32 offset, cifd_t * resulting_ifd) { |
| 318 |
GET_EMPTY_RET(ret);TIFP_CHECK(ctif, ret); |
| 319 |
if (ct_seek(ctif, offset, SEEK_SET) != offset) { |
| 320 |
char msg[VALUESTRLEN]; |
| 321 |
snprintf(msg, VALUESTRLEN, "TIFF Header seek error, seek set to byte %u", offset); |
| 322 |
ret = set_value_found_ret(&ret, str(msg)); |
| 323 |
ret.returncode = tiff_seek_error_header; |
| 324 |
return ret; |
| 325 |
} |
| 326 |
uint16 count; |
| 327 |
if (ct_read16(ctif, &count, 1) != 2) { |
| 328 |
char msg[VALUESTRLEN]; |
| 329 |
snprintf(msg, VALUESTRLEN, "TIFF Header ct_read error2, reading ifd count (2 bytes) at file pos=%u (%x)", |
| 330 |
offset, offset); |
| 331 |
ret = set_value_found_ret(&ret, str(msg)); |
| 332 |
ret.returncode = tiff_read_error_header; |
| 333 |
return ret; |
| 334 |
} |
| 335 |
if (is_byteswapped(ctif)) { |
| 336 |
count = TIFFSwabShort(count); |
| 337 |
} |
| 338 |
resulting_ifd->tag_count = count; |
| 339 |
resulting_ifd->position = (off_t) offset; |
| 340 |
resulting_ifd->tagorder = unknown_tag_order; |
| 341 |
/* init with -1 indicating 'not found' */ |
| 342 |
empty_cache(resulting_ifd); |
| 343 |
switch (resulting_ifd->tagorder) { |
| 344 |
case has_unsorted_tags: |
| 345 |
case has_sorted_tags: |
| 346 |
break; |
| 347 |
case unknown_tag_order: { |
| 348 |
tag_t last_tag = TIFFGetRawTagListEntry(ctif, resulting_ifd, 0); |
| 349 |
resulting_ifd->index_cache_by_tag[last_tag] = 0; |
| 350 |
resulting_ifd->tag_cache_by_index[0] = last_tag; |
| 351 |
int is_unsorted = 0; |
| 352 |
for (uint16 tagidx = 1; tagidx < count; tagidx++) { |
| 353 |
tag_t current_tag = TIFFGetRawTagListEntry(ctif, resulting_ifd, tagidx); |
| 354 |
|
| 355 |
is_unsorted += (last_tag >= current_tag); |
| 356 |
resulting_ifd->index_cache_by_tag[current_tag] = tagidx; |
| 357 |
resulting_ifd->tag_cache_by_index[tagidx] = current_tag; |
| 358 |
last_tag = current_tag; |
| 359 |
} |
| 360 |
if (is_unsorted > 0) { resulting_ifd->tagorder = has_unsorted_tags; } |
| 361 |
} |
| 362 |
} |
| 363 |
ret.returncode = is_valid; |
| 364 |
return ret; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
ret_t get_first_IFD(ctiff_t * ctif, uint32 * ifd) { |
| 369 |
GET_EMPTY_RET(ret); |
| 370 |
TIFP_CHECK( ctif, ret); |
| 371 |
|
| 372 |
int isByteSwapped = ctif->isbyteswapped; |
| 373 |
/* seek the image file directory (bytes 4-7) */ |
| 374 |
if (ct_seek(ctif, 4, SEEK_SET) != 4 ) { |
| 375 |
ret = set_value_found_ret(&ret, str("TIFF Header seak error, seek set to byte 4")); |
| 376 |
ret.returncode=tiff_seek_error_header; |
| 377 |
return ret; |
| 378 |
} |
| 379 |
uint32 offset; |
| 380 |
if ( ct_read32( ctif, &offset, 1) != 4 ) { |
| 381 |
ret = set_value_found_ret(&ret, str("TIFF Header ct_read error, reading 4 bytes from 4")); |
| 382 |
ret.returncode=tiff_read_error_header; |
| 383 |
return ret; |
| 384 |
} |
| 385 |
if (isByteSwapped) { |
| 386 |
offset = TIFFSwabLong (offset); |
| 387 |
} |
| 388 |
if (offset <= 7) { |
| 389 |
char msg[VALUESTRLEN]; |
| 390 |
snprintf(msg, VALUESTRLEN, "pointer to IFD0 is %u", offset); |
| 391 |
ret=set_value_found_ret(&ret, str(msg)); |
| 392 |
ret.returncode=tiff_ifd0_offset_must_be_greater_than_eight; |
| 393 |
return ret; |
| 394 |
} |
| 395 |
ret = get_IFD_at_pos(ctif, offset, ctif->ifd0); |
| 396 |
*ifd = offset; |
| 397 |
return ret; |
| 398 |
} |
| 399 |
|
| 400 |
/** reads IDS and try to find index of given tag, returns -1 if not found |
| 401 |
* @param ctif pointer to tiff structure |
| 402 |
* @param ifd pointer to IFD |
| 403 |
* @param tag tag |
| 404 |
* @return index or -1 if not found |
| 405 |
*/ |
| 406 |
static int TIFFFind_Index_of_Tag(ctiff_t * ctif, cifd_t *ifd, tag_t tag) { |
| 407 |
assert(tag >= MINTAGS); |
| 408 |
uint16 count = get_ifd_count(ifd); |
| 409 |
|
| 410 |
if (ifd->tagorder != unknown_tag_order) { |
| 411 |
return ifd->index_cache_by_tag[tag]; |
| 412 |
} else { |
| 413 |
for (uint16 tagidx = 0; tagidx < count; tagidx++) { |
| 414 |
if (tag == TIFFGetRawTagListEntry(ctif, ifd, tagidx)) { |
| 415 |
return tagidx; |
| 416 |
}; |
| 417 |
}; |
| 418 |
} |
| 419 |
return no_index_found; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
/* scans first IDF and returns the n-th tag, if errorneous it returns a value 0 */ |
| 424 |
tag_t TIFFGetRawTagListEntry(ctiff_t * ctif, const cifd_t *ifd, int tagidx) { |
| 425 |
tag_t tag; |
| 426 |
if (ifd->tagorder == has_sorted_tags) { // only if sorted, the caches should be used |
| 427 |
//printf("RETURN FROM CACHE: tagidx=%i -> tag=%u\n", tagidx, ifd->tag_cache_by_index[tagidx]); |
| 428 |
return ifd->tag_cache_by_index[tagidx]; |
| 429 |
} |
| 430 |
get_ifd0_count(ctif); /* return code ignored, used to read TIFF header */ |
| 431 |
/* ct_read count of tags (2 Bytes) */ |
| 432 |
off_t adress = (off_t) ifd->position + 2 + tagidx * 12; /* IFD has 2 bytes for count of tags, plus 12 bytes for each tag */ |
| 433 |
if (ct_seek(ctif, adress, SEEK_SET) != adress) { /* IFD0 plus 2byte to get IFD-entries, then nth tag */ |
| 434 |
//perror ("TIFF Header ct_seek error, seeking adress from ifd0 for tagid"); |
| 435 |
//exit( EXIT_FAILURE ); |
| 436 |
return 0; |
| 437 |
} |
| 438 |
if (ct_read16(ctif, &tag, 1) != 2) { |
| 439 |
//perror ("TIFF Header ct_read error2, reading tagid from ifd0 (2bytes)"); |
| 440 |
// exit( EXIT_FAILURE ); |
| 441 |
return 0; |
| 442 |
} |
| 443 |
if (is_byteswapped(ctif)) { |
| 444 |
tag = TIFFSwabShort(tag); |
| 445 |
} |
| 446 |
return tag; |
| 447 |
} |
| 448 |
|
| 449 |
/* offset data is pointer to memory of values of type offset_type */ |
| 450 |
#define OFFSET_MALLOC(ctread, ctif_p, offsetdata, offset_type, count ) {\ |
| 451 |
size_t size = (size_t) sizeof(offset_type) * (count); \ |
| 452 |
/*printf("OFFSET_MALLOC: count=%lu, size=%lu\n", count, size); */ \ |
| 453 |
if((size_t) (ctif_p)->streamlen < size) {\ |
| 454 |
char msg[VALUESTRLEN]; \ |
| 455 |
snprintf(msg, VALUESTRLEN, "TIFF Offset ct_read error, try to read from offset count=%zu bytes, but file has size=%lu\n", size, (ctif_p)->streamlen); \ |
| 456 |
*ret_p = set_value_found_ret( ret_p, str(msg)); \ |
| 457 |
ret_p->returncode = tiff_seek_error_offset;\ |
| 458 |
return * ret_p;\ |
| 459 |
}\ |
| 460 |
(offsetdata) = NULL; (offsetdata) = malloc (size);\ |
| 461 |
if (NULL == (offsetdata)) {\ |
| 462 |
fprintf( stderr, "could not allocate memory for offset_t\n");\ |
| 463 |
exit (EXIT_FAILURE);\ |
| 464 |
}\ |
| 465 |
ssize_t result = ctread( ctif_p, (offsetdata), count); \ |
| 466 |
/*printf("RESULT=%lu\n", result); */ \ |
| 467 |
if (result == 0) {\ |
| 468 |
char msg[VALUESTRLEN]; \ |
| 469 |
snprintf(msg, VALUESTRLEN, "TIFF Offset ct_read error, try to read from offset count=%zu bytes, but EOF detected\n", size); \ |
| 470 |
*ret_p = set_value_found_ret( ret_p, str(msg)); \ |
| 471 |
ret_p->returncode = tiff_read_error_offset; \ |
| 472 |
return *ret_p; \ |
| 473 |
}\ |
| 474 |
if (\ |
| 475 |
(result == no_file_found) ||\ |
| 476 |
(result != (ssize_t) size)\ |
| 477 |
) {\ |
| 478 |
/* fprintf(stderr, "TIFF Offset ct_read error, try to read from offset count=%lu bytes\n", sizeof(offset_type) * count); */\ |
| 479 |
/* exit( EXIT_FAILURE ); */\ |
| 480 |
char msg[VALUESTRLEN]; \ |
| 481 |
snprintf(msg, VALUESTRLEN, "TIFF Offset ct_read error, try to read from offset count=%zu bytes\n", size); \ |
| 482 |
*ret_p = set_value_found_ret( ret_p, str(msg)); \ |
| 483 |
ret_p->returncode = tiff_read_error_offset; \ |
| 484 |
return *ret_p; \ |
| 485 |
}\ |
| 486 |
} |
| 487 |
|
| 488 |
static void offset_swabshort(const ctiff_t * ctif, uint16 * address, uint16 count) { |
| 489 |
if (is_byteswapped(ctif)) { |
| 490 |
for (int i=0; i<count; i++, address++) { |
| 491 |
*address = TIFFSwabShort(*address); |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
static void offset_swablong(const ctiff_t * ctif, uint32 * address, uint16 count) { |
| 497 |
if (is_byteswapped(ctif)) { |
| 498 |
for (int i=0; i<count; i++, address++) { |
| 499 |
*address = TIFFSwabLong(*address); |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
/* get count-data datastream from offset-address */ |
| 504 |
ret_t read_offsetdata(ctiff_t * ctif, uint32 address, uint32 count, uint16 datatype, offset_t * offset_p, ret_t * ret_p) { |
| 505 |
assert(NULL != offset_p); |
| 506 |
assert(NULL != ret_p); |
| 507 |
offset_p->count = count; |
| 508 |
offset_p->datatype = datatype; |
| 509 |
free_value_if_set(ret_p); |
| 510 |
ret_p->returncode = is_valid; |
| 511 |
/* ct_read and seek to IFD address */ |
| 512 |
if (ct_seek(ctif, address, SEEK_SET) != (off_t) address) { |
| 513 |
offset_p->count = 0; |
| 514 |
ret_p->returncode = tiff_seek_error_offset; |
| 515 |
return * ret_p; |
| 516 |
} |
| 517 |
#ifdef DEBUG |
| 518 |
printf("read_offsetdata(tif, address=%u, count=%u, datatype=%u)\n", address, count, datatype); |
| 519 |
#endif |
| 520 |
switch (datatype) { |
| 521 |
case TIFF_BYTE: /* 8-bit unsigned integer */ |
| 522 |
case TIFF_UNDEFINED: /* !8-bit untyped data */ |
| 523 |
/* |
| 524 |
offset_p->data8p = NULL; |
| 525 |
offset_p->data8p = malloc ( sizeof(uint8) * count); |
| 526 |
if (ct_read(fd, offset_p->data8p, sizeof(uint8) * count) != sizeof(uint8) *count) |
| 527 |
perror ("TIFF Offset ct_read error"); |
| 528 |
*/ |
| 529 |
OFFSET_MALLOC(ct_read8, ctif, offset_p->data8p, uint8, count) |
| 530 |
break; |
| 531 |
case TIFF_ASCII: /* 8-bit bytes w/ last byte null */ |
| 532 |
assert( sizeof(char) == sizeof(uint8)); |
| 533 |
OFFSET_MALLOC(ct_read_intern,ctif, offset_p->datacharp, char, count) |
| 534 |
break; |
| 535 |
case TIFF_SBYTE: /* !8-bit signed integer */ |
| 536 |
OFFSET_MALLOC(ct_read_intern, ctif, offset_p->datas8p, int8, count) |
| 537 |
break; |
| 538 |
case TIFF_SHORT: /* 16-bit unsigned integer */ |
| 539 |
OFFSET_MALLOC(ct_read16, ctif, offset_p->data16p, uint16, count) |
| 540 |
offset_swabshort(ctif, offset_p->data16p, count); |
| 541 |
break; |
| 542 |
case TIFF_SSHORT: /* !16-bit signed integer */ |
| 543 |
OFFSET_MALLOC(ct_read16s, ctif, offset_p->datas16p, int16, count) |
| 544 |
offset_swabshort(ctif, (uint16 *) offset_p->datas16p, count); |
| 545 |
break; |
| 546 |
case TIFF_LONG: /* 32-bit unsigned integer */ |
| 547 |
case TIFF_IFD: /* %32-bit unsigned integer (offset) */ |
| 548 |
OFFSET_MALLOC(ct_read32, ctif, offset_p->data32p, uint32, count) |
| 549 |
offset_swablong(ctif, offset_p->data32p, count); |
| 550 |
break; |
| 551 |
case TIFF_SLONG: /* !32-bit signed integer */ |
| 552 |
OFFSET_MALLOC(ct_read32s, ctif, offset_p->datas32p, int32, count) |
| 553 |
offset_swablong(ctif, (uint32 *) offset_p->data32p, count); |
| 554 |
break; |
| 555 |
case TIFF_RATIONAL: /* 64-bit unsigned fraction */ |
| 556 |
//printf("TIFF RATIONAL, count=%lu\n", count); |
| 557 |
OFFSET_MALLOC(ct_read32, ctif, offset_p->data32p, uint32, 2*count) /* because numerator + denominator */ |
| 558 |
offset_swablong(ctif, offset_p->data32p, 2*count); |
| 559 |
break; |
| 560 |
case TIFF_SRATIONAL: /* !64-bit signed fraction */ |
| 561 |
fprintf(stderr, "offsetdata datatype=%i not supported yet", datatype); |
| 562 |
exit(EXIT_FAILURE); |
| 563 |
case TIFF_FLOAT: /* !32-bit IEEE floating point */ |
| 564 |
assert( sizeof(float) == 4); |
| 565 |
OFFSET_MALLOC(ct_readf, ctif, offset_p->datafloatp, float, count) |
| 566 |
break; |
| 567 |
case TIFF_DOUBLE: /* !64-bit IEEE floating point */ |
| 568 |
assert( sizeof(double) == 8); |
| 569 |
OFFSET_MALLOC(ct_readd, ctif, offset_p->datadoublep, double, count) |
| 570 |
break; |
| 571 |
case TIFF_LONG8: /* BigTIFF 64-bit unsigned integer */ |
| 572 |
case TIFF_IFD8: /* BigTIFF 64-bit unsigned integer (offset) */ |
| 573 |
assert( sizeof(double) == 8); |
| 574 |
OFFSET_MALLOC(ct_read_intern, ctif, offset_p->data64p, uint64, count) |
| 575 |
break; |
| 576 |
case TIFF_SLONG8: /* BigTIFF 64-bit signed integer */ |
| 577 |
assert( sizeof(double) == 8); |
| 578 |
OFFSET_MALLOC(ct_read_intern, ctif, offset_p->datas64p, int64, count) |
| 579 |
break; |
| 580 |
default: /* should not occur */ |
| 581 |
{ |
| 582 |
char msg[VALUESTRLEN]; |
| 583 |
snprintf(msg, VALUESTRLEN, "offsetdata datatype=%i not supported yet", datatype); |
| 584 |
*ret_p = set_value_found_ret(ret_p, str(msg)); |
| 585 |
ret_p->returncode = should_not_occur; |
| 586 |
assert( ret_p->returncode != should_not_occur); |
| 587 |
return *ret_p; |
| 588 |
} |
| 589 |
}; |
| 590 |
#ifdef DEBUG |
| 591 |
printf ("is valid offset\n"); |
| 592 |
printf ("RET=%s\n", get_parser_error_description(ret_p->returncode)); |
| 593 |
#endif |
| 594 |
return *ret_p; |
| 595 |
} |
| 596 |
|
| 597 |
/* scans first IDF and returns the type of the n-th tag */ |
| 598 |
ifd_entry_t TIFFGetRawTagIFDListEntry( ctiff_t * ctif, const cifd_t *ifd, int tagidx ) { |
| 599 |
uint16 tagcount = get_ifd_count( ifd); |
| 600 |
assert( tagcount > 0); |
| 601 |
int byteswapped = is_byteswapped(ctif); |
| 602 |
#ifdef DEBUG |
| 603 |
printf(" count of tags = %i\n", tagcount); |
| 604 |
#endif |
| 605 |
ifd_entry_t ifd_entry={0}; |
| 606 |
ifd_entry.value_or_offset = is_error; |
| 607 |
off_t adress=(off_t) get_ifd_pos(ifd) +2+tagidx*12; /* IFD has 2 bytes for count of tags, plus 12 bytes for each tag */ |
| 608 |
if (ct_seek(ctif, adress, SEEK_SET) !=adress) { /* IFD0 plus 2byte to get IFD-entries, then nth tag */ |
| 609 |
//perror ("TIFF Header ct_seek error, seeking adress from ifd0 for tagid"); |
| 610 |
return ifd_entry; |
| 611 |
} |
| 612 |
uint16 tagid; |
| 613 |
if ( ct_read16( ctif, &tagid, 1) != 2) { |
| 614 |
//perror ("TIFF Header ct_read error2, reading tagid from ifd0 (2bytes)"); |
| 615 |
return ifd_entry; |
| 616 |
} |
| 617 |
if (byteswapped) { |
| 618 |
tagid = TIFFSwabShort(tagid); |
| 619 |
} |
| 620 |
// tag type check |
| 621 |
uint16 tagtype; |
| 622 |
if ( ct_read16( ctif, &tagtype, 1) != 2) { |
| 623 |
//perror ("TIFF Header ct_read error2, reading tagtype from ifd0 (2bytes)"); |
| 624 |
return ifd_entry; |
| 625 |
} |
| 626 |
if (byteswapped) { |
| 627 |
tagtype = TIFFSwabShort(tagtype); |
| 628 |
} |
| 629 |
uint32 count; |
| 630 |
if ( ct_read32( ctif, &count, 1) != 4) { |
| 631 |
//perror ("TIFF Header ct_read error4, reading count from ifd0 (4bytes)"); |
| 632 |
return ifd_entry; |
| 633 |
} |
| 634 |
if (byteswapped) { |
| 635 |
count = TIFFSwabLong( count); |
| 636 |
} |
| 637 |
#ifdef DEBUG |
| 638 |
printf("\ntagid=%0x, tagtype=%0x count=%0x\n\n", tagid, tagtype, count); |
| 639 |
#endif |
| 640 |
|
| 641 |
/* is value or offset? */ |
| 642 |
ifd_entry.count=count; |
| 643 |
ifd_entry.datatype=tagtype; |
| 644 |
|
| 645 |
uint32 value_or_offset; |
| 646 |
if ( ct_read32( ctif, &value_or_offset, 1) != 4) { |
| 647 |
//perror ("TIFF Header ct_read error4, reading value/offset from ifd0 (4bytes)"); |
| 648 |
return ifd_entry; |
| 649 |
} |
| 650 |
if (byteswapped) { |
| 651 |
value_or_offset = TIFFSwabLong( value_or_offset); |
| 652 |
} |
| 653 |
switch( tagtype) { |
| 654 |
case TIFF_BYTE: /* 8-bit unsigned integer */ |
| 655 |
case TIFF_ASCII: /* 8-bit bytes w/ last byte null */ |
| 656 |
case TIFF_SBYTE: /* !8-bit signed integer */ |
| 657 |
case TIFF_UNDEFINED: /* !8-bit untyped data */ |
| 658 |
if (count > 4) { /* offset */ |
| 659 |
ifd_entry.value_or_offset=is_offset; |
| 660 |
ifd_entry.data32offset=value_or_offset; |
| 661 |
} else { /* values */ |
| 662 |
ifd_entry.value_or_offset=is_value; |
| 663 |
ifd_entry.data8[0] = (uint8) (value_or_offset & 0x000000ff); |
| 664 |
ifd_entry.data8[1] = (uint8) ((value_or_offset >> 8) & 0x000000ff); |
| 665 |
ifd_entry.data8[2] = (uint8) ((value_or_offset >> 16) & 0x000000ff); |
| 666 |
ifd_entry.data8[3] = (uint8) ((value_or_offset >> 24) & 0x000000ff); |
| 667 |
#ifdef DEBUG |
| 668 |
printf("data8[0]=%u\n", ifd_entry.data8[0] ); |
| 669 |
printf("data8[1]=%u\n", ifd_entry.data8[1] ); |
| 670 |
printf("data8[2]=%u\n", ifd_entry.data8[2] ); |
| 671 |
printf("data8[3]=%u\n", ifd_entry.data8[3] ); |
| 672 |
#endif |
| 673 |
}; break; |
| 674 |
case TIFF_SHORT: /* 16-bit unsigned integer */ |
| 675 |
case TIFF_SSHORT: /* !16-bit signed integer */ |
| 676 |
if (count > 2) { /* offset */ |
| 677 |
ifd_entry.value_or_offset=is_offset; |
| 678 |
ifd_entry.data32offset=value_or_offset; |
| 679 |
} else { /* values */ |
| 680 |
ifd_entry.value_or_offset=is_value; |
| 681 |
// swap back if byteswapped |
| 682 |
if (byteswapped) { |
| 683 |
value_or_offset = TIFFSwabLong( value_or_offset); |
| 684 |
} |
| 685 |
uint16 w0 = (uint16) (value_or_offset & 0x0000ffff); |
| 686 |
uint16 w1 = (uint16) ((value_or_offset >> 16) & 0x0000ffff);; |
| 687 |
if (byteswapped) { /* swap correctly 16bit */ |
| 688 |
w0 = TIFFSwabShort( w0 ); |
| 689 |
w1 = TIFFSwabShort( w1 ); |
| 690 |
} |
| 691 |
ifd_entry.data16[0] = w0; |
| 692 |
ifd_entry.data16[1] = w1; |
| 693 |
#ifdef DEBUG |
| 694 |
printf("data16[0]=%u\n", w0); |
| 695 |
printf("data16[1]=%u\n", w1); |
| 696 |
#endif |
| 697 |
}; break; |
| 698 |
case TIFF_LONG: /* 32-bit unsigned integer */ |
| 699 |
case TIFF_SLONG: /* !32-bit signed integer */ |
| 700 |
if (count > 1) { /* offset */ |
| 701 |
ifd_entry.value_or_offset=is_offset; |
| 702 |
ifd_entry.data32offset=value_or_offset; |
| 703 |
} else { /* values */ |
| 704 |
ifd_entry.value_or_offset=is_value; |
| 705 |
ifd_entry.data32=value_or_offset; |
| 706 |
#ifdef DEBUG |
| 707 |
printf("data32[0]=%u\n", value_or_offset); |
| 708 |
#endif |
| 709 |
}; break; |
| 710 |
case TIFF_RATIONAL: /* 64-bit unsigned fraction */ |
| 711 |
case TIFF_SRATIONAL: /* !64-bit signed fraction */ |
| 712 |
case TIFF_FLOAT: /* !32-bit IEEE floating point */ |
| 713 |
case TIFF_DOUBLE: /* !64-bit IEEE floating point */ |
| 714 |
case TIFF_IFD: /* %32-bit unsigned integer (offset) */ |
| 715 |
case TIFF_LONG8: /* BigTIFF 64-bit unsigned integer */ |
| 716 |
case TIFF_SLONG8: /* BigTIFF 64-bit signed integer */ |
| 717 |
case TIFF_IFD8: /* BigTIFF 64-bit unsigned integer (offset) */ |
| 718 |
ifd_entry.value_or_offset=is_offset; |
| 719 |
ifd_entry.data32offset=value_or_offset; |
| 720 |
} |
| 721 |
#ifdef DEBUG |
| 722 |
printf(" tag=%u (0x%04x) tagtype=0x%04x is_offset=%s count=%u value_or_offset=0x%08x\n", tagid, tagid, tagtype, (ifd_entry.value_or_offset==is_offset ? "true" : "false"), count, value_or_offset); |
| 723 |
#endif |
| 724 |
return ifd_entry; |
| 725 |
} |
| 726 |
|
| 727 |
/* TODO */ |
| 728 |
ifd_entry_t TIFFGetRawIFDEntry( ctiff_t * ctif, cifd_t *ifd, tag_t tag) { |
| 729 |
int tagidx = TIFFFind_Index_of_Tag(ctif, ifd, tag); |
| 730 |
ifd_entry_t ifd_entry; |
| 731 |
if (tagidx >= 0) { |
| 732 |
ifd_entry = TIFFGetRawTagIFDListEntry( ctif, ifd, tagidx ); |
| 733 |
} else { /* tag not defined */ |
| 734 |
printf("\ttag %u (%s) was not found, but requested because defined\n", tag, TIFFTagName(tag)); |
| 735 |
ifd_entry.value_or_offset = is_error; |
| 736 |
ifd_entry.count = 0; |
| 737 |
ifd_entry.data32offset=0; |
| 738 |
ifd_entry.datatype = TIFF_ERROR; |
| 739 |
} |
| 740 |
return ifd_entry; |
| 741 |
} |
| 742 |
/* scans first IDF and returns the type of the n-th tag */ |
| 743 |
uint32 TIFFGetRawTagTypeListEntry( ctiff_t * ctif, cifd_t *ifd, int tagidx ) { |
| 744 |
if (tagidx >= 0) { |
| 745 |
ifd_entry_t ifd_entry = TIFFGetRawTagIFDListEntry(ctif, ifd, tagidx); |
| 746 |
return ifd_entry.datatype; |
| 747 |
} else { /* tag not defined */ |
| 748 |
fprintf(stderr, "tagidx should be greater equal 0"); |
| 749 |
return TIFF_ERROR; |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
|
| 754 |
/** ct_reads the datatype of given tag on specified TIFF, |
| 755 |
* because FieldType of libtiff does not return the true value (because it maps |
| 756 |
* its own datastructure), we need to use this function instead |
| 757 |
* @param ctif pointer to TIFF structure |
| 758 |
* @param ifd pointer to IFD |
| 759 |
* @param tag tag |
| 760 |
* @return datatype of given tag |
| 761 |
* if tag does not exists the function aborts with an error |
| 762 |
*/ |
| 763 |
TIFFDataType TIFFGetRawTagType(ctiff_t * ctif, cifd_t *ifd, tag_t tag) { |
| 764 |
int tagidx = TIFFFind_Index_of_Tag(ctif, ifd, tag); |
| 765 |
if (tagidx >= 0) { |
| 766 |
TIFFDataType datatype = TIFFGetRawTagTypeListEntry( ctif, ifd, tagidx ); |
| 767 |
#ifdef DEBUG |
| 768 |
printf("### datatype=%i \n", datatype); |
| 769 |
#endif |
| 770 |
return datatype; |
| 771 |
} else { /* tag not defined */ |
| 772 |
printf("\ttag %u (%s) was not found, but requested because defined\n", tag, TIFFTagName(tag)); |
| 773 |
return TIFF_ERROR; |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
ctiff_t * initialize_ctif(const char * tiff_file, ct_ioflag_t ioflag) { |
| 778 |
ctiff_t * ctif = malloc ( sizeof( ctiff_t) ); |
| 779 |
if (NULL == ctif) { |
| 780 |
fprintf( stderr, "could not allocate memory for ctiff_t\n"); |
| 781 |
exit (EXIT_FAILURE); |
| 782 |
} |
| 783 |
/* load tiff file */ |
| 784 |
#ifdef __WIN32__ |
| 785 |
int tif = open(tiff_file, O_RDONLY | O_BINARY); |
| 786 |
#else |
| 787 |
int tif = open(tiff_file, O_RDONLY); |
| 788 |
#endif |
| 789 |
if (no_file_found == tif) { |
| 790 |
fprintf( stderr, "TIFF file '%s' could not be opened, %s\n", tiff_file, strerror(errno)); |
| 791 |
exit (EXIT_FAILURE); |
| 792 |
}; |
| 793 |
ctif->fd = tif; |
| 794 |
ctif->streamlen = (ssize_t) fsize(tif); |
| 795 |
ctif->streamp = NULL; |
| 796 |
ctif->actual_streamp = NULL; |
| 797 |
static cifd_t ifd0 = { |
| 798 |
.tagorder=unknown_tag_order, |
| 799 |
.position=0, |
| 800 |
.tag_count=0, |
| 801 |
.index_cache_by_tag={-1}, |
| 802 |
.tag_cache_by_index={0}, |
| 803 |
}; |
| 804 |
/* empty_cache(&ifd0); */ |
| 805 |
ctif->ifd0 = &ifd0; |
| 806 |
|
| 807 |
#ifdef HAVE_MMAP |
| 808 |
switch (ioflag) { |
| 809 |
case is_filep: { |
| 810 |
/* streamlen */ |
| 811 |
break; |
| 812 |
} |
| 813 |
case is_memmap: { |
| 814 |
|
| 815 |
void * tifmap = mmap( NULL, ctif->streamlen, PROT_READ, MAP_PRIVATE, tif, 0 ); |
| 816 |
if (MAP_FAILED == tifmap) { |
| 817 |
fprintf( stderr, "TIFF file '%s' could not be mem-mapped, %s\n", tiff_file, strerror(errno)); |
| 818 |
exit (EXIT_FAILURE); |
| 819 |
}; |
| 820 |
ctif->streamp=tifmap; |
| 821 |
ctif->actual_streamp=tifmap; |
| 822 |
break; |
| 823 |
} |
| 824 |
} |
| 825 |
#endif |
| 826 |
ctif->ioflag = ioflag; |
| 827 |
ctif->filename = strdup(tiff_file); |
| 828 |
return ctif; |
| 829 |
} |
| 830 |
|
| 831 |
void free_ctif( ctiff_t * ctif) { |
| 832 |
assert( NULL != ctif); |
| 833 |
free(ctif->filename); |
| 834 |
ctif->filename=NULL; |
| 835 |
switch (ctif->ioflag) { |
| 836 |
case is_filep: { |
| 837 |
break; |
| 838 |
} |
| 839 |
case is_memmap: { |
| 840 |
|
| 841 |
/* TODO */ |
| 842 |
break; |
| 843 |
} |
| 844 |
} |
| 845 |
close(ctif->fd); |
| 846 |
ctif->fd = no_file_found; |
| 847 |
free (ctif); |
| 848 |
/* ctif = NULL; */ |
| 849 |
} |
| 850 |
|
| 851 |
uint32 get_ifd_pos(const cifd_t *ifd ) { |
| 852 |
return ifd->position; |
| 853 |
} |
| 854 |
|
| 855 |
uint16 get_ifd_count(const cifd_t *ifd) { |
| 856 |
return ifd->tag_count; |
| 857 |
} |
| 858 |
|
| 859 |
uint32 get_ifd0_pos(const ctiff_t * ctif) { |
| 860 |
assert( NULL != ctif); |
| 861 |
return (get_ifd_pos(ctif->ifd0)); |
| 862 |
} |
| 863 |
|
| 864 |
uint16 get_ifd0_count(const ctiff_t * ctif) { |
| 865 |
assert( NULL != ctif); |
| 866 |
return (get_ifd_count(ctif->ifd0)); |
| 867 |
} |
| 868 |
|
| 869 |
inline int is_byteswapped(const ctiff_t * ctif ) { |
| 870 |
assert( NULL != ctif); |
| 871 |
return ctif->isbyteswapped; |
| 872 |
} |
| 873 |
|
| 874 |
uint32 get_next_ifd_pos(ctiff_t * ctif, uint32 actual_pos) { |
| 875 |
assert( NULL != ctif); |
| 876 |
|
| 877 |
ct_seek(ctif, actual_pos, SEEK_SET); |
| 878 |
uint16 count; |
| 879 |
if ( ct_read16( ctif, &count, 1) != 2 ) { |
| 880 |
/* FIXME: replace perror/exit with own error handling routine */ |
| 881 |
//perror ("TIFF Header ct_read error2, reading count from ifd0 (2bytes)"); |
| 882 |
//exit( EXIT_FAILURE ); |
| 883 |
return 0; |
| 884 |
} |
| 885 |
|
| 886 |
if (ctif->isbyteswapped) { |
| 887 |
count = TIFFSwabShort(count); |
| 888 |
} |
| 889 |
ct_seek(ctif, 12 * count, SEEK_CUR); /* each IFD entry is 12 bytes long */ |
| 890 |
uint32 offset; |
| 891 |
if ( ct_read32( ctif, &offset, 1) != 4 ) { |
| 892 |
/* FIXME: replace perror/exit with own error handling routine */ |
| 893 |
//perror ("TIFF Header ct_read error3, reading next ifd offset from ifd0 (4bytes)"); |
| 894 |
//exit( EXIT_FAILURE ); |
| 895 |
return 0; |
| 896 |
} |
| 897 |
if (ctif->isbyteswapped) { |
| 898 |
offset = TIFFSwabLong(offset); |
| 899 |
} |
| 900 |
return offset; |
| 901 |
} |
| 902 |
|
| 903 |
ret_t TIFFGetFieldASCII(ctiff_t * ctif, cifd_t *ifd, tag_t tag, char** string_pp, uint32 * countp ) { |
| 904 |
assert( *string_pp == NULL); |
| 905 |
assert( countp != NULL); |
| 906 |
GET_EMPTY_RET(ret) |
| 907 |
int tagidx = TIFFGetRawTagListIndex(ifd, tag); |
| 908 |
if (tagidx >= 0) { /* there exists the tag */ |
| 909 |
ifd_entry_t entry = TIFFGetRawTagIFDListEntry( ctif, ifd, tagidx ); |
| 910 |
/* assert (entry.datatype == TIFF_ASCII); */ |
| 911 |
if (entry.datatype != TIFF_ASCII) { |
| 912 |
ret = set_value_found_ret_formatted_with_returncode(&ret, tagerror_unexpected_type_found, "data of type ASCII expected, but got type %s", TIFFTypeName(entry.datatype)); |
| 913 |
return ret; |
| 914 |
} |
| 915 |
*countp = entry.count; |
| 916 |
/* |
| 917 |
*(string_pp) = malloc( sizeof(char) * entry.count +1); |
| 918 |
if (NULL == (* string_pp)) { |
| 919 |
ret.returncode=could_not_allocate_memory; |
| 920 |
return ret; |
| 921 |
} |
| 922 |
memset(*string_pp, '\0', entry.count+1); |
| 923 |
*/ |
| 924 |
*(string_pp) = calloc(entry.count+1, sizeof(char) ); |
| 925 |
if (NULL == (* string_pp)) { |
| 926 |
ret.returncode=could_not_allocate_memory; |
| 927 |
return ret; |
| 928 |
} |
| 929 |
if (entry.value_or_offset == is_value) { |
| 930 |
assert (entry.count <= 4); |
| 931 |
for (uint32 i=0; i<entry.count; i++) { |
| 932 |
(*string_pp)[i]=(char) entry.data8[i]; |
| 933 |
} |
| 934 |
ret.returncode=is_valid; |
| 935 |
return ret; |
| 936 |
} else if (entry.value_or_offset == is_offset) { |
| 937 |
uint32 data32offset = entry.data32offset; |
| 938 |
offset_t offset; |
| 939 |
ret = read_offsetdata( ctif, data32offset, entry.count, entry.datatype, &offset, &ret); |
| 940 |
if (ret.returncode != is_valid) { |
| 941 |
/* FIXME: free(offset.datacharp); */ |
| 942 |
return ret; |
| 943 |
} |
| 944 |
char const * p = offset.datacharp; |
| 945 |
char * s = *(string_pp); |
| 946 |
#ifdef DEBUG |
| 947 |
/* DEBUG: */ |
| 948 |
printf("offset.count=%u, offset.datacharp=%p\n", offset.count, offset.datacharp); |
| 949 |
printf("tag=%i entry.count=%u offset.count=%u\n", tag, entry.count, offset.count); |
| 950 |
#endif |
| 951 |
for (uint32 i=0; i<entry.count; i++) { |
| 952 |
#ifdef DEBUG |
| 953 |
printf("P[%u]=%c\n", i, *p); |
| 954 |
#endif |
| 955 |
*(s++) = *(p++); |
| 956 |
} |
| 957 |
//printf("ASCII='%s'\n", *(string_pp)); |
| 958 |
free(offset.datacharp); |
| 959 |
ret.returncode=is_valid; |
| 960 |
return ret; |
| 961 |
} |
| 962 |
} |
| 963 |
ret.returncode = tag_does_not_exist; |
| 964 |
return ret; |
| 965 |
} |
| 966 |
|
| 967 |
ret_t TIFFGetFieldLONG(ctiff_t * ctif, cifd_t *ifd, tag_t tag, uint32 ** long_pp, uint32 * countp) { |
| 968 |
assert( *long_pp == NULL); |
| 969 |
assert( countp != NULL); |
| 970 |
*countp = 0; /* init */ |
| 971 |
GET_EMPTY_RET(ret) |
| 972 |
int tagidx = TIFFGetRawTagListIndex(ifd, tag); |
| 973 |
if (tagidx >= 0) { /* there exists the tag */ |
| 974 |
ifd_entry_t entry = TIFFGetRawTagIFDListEntry( ctif, ifd, tagidx ); |
| 975 |
uint32 overflow = (0xffffffff / (sizeof(uint32))); |
| 976 |
if (entry.count > overflow) { |
| 977 |
ret = set_value_found_ret_formatted_with_returncode(&ret, tagerror_count_results_in_offsets_overflow, "count=%u overflow boundary=%u", entry.count, overflow); |
| 978 |
return ret; |
| 979 |
} |
| 980 |
|
| 981 |
*countp = entry.count; |
| 982 |
*(long_pp) = malloc( sizeof(uint32) * entry.count); |
| 983 |
if (NULL == (*long_pp)) { |
| 984 |
ret.returncode=could_not_allocate_memory; |
| 985 |
return ret; |
| 986 |
} |
| 987 |
/* assert (entry.datatype == TIFF_LONG); */ |
| 988 |
if (entry.datatype != TIFF_LONG) { |
| 989 |
ret = set_value_found_ret_formatted_with_returncode(&ret, tagerror_unexpected_type_found, "data of type LONG expected, but got type %s", TIFFTypeName(entry.datatype)); |
| 990 |
return ret; |
| 991 |
} |
| 992 |
|
| 993 |
if (entry.value_or_offset == is_value) { |
| 994 |
assert (entry.count <= 1); |
| 995 |
//printf("LONG (direct)=%lu\n", entry.data32); |
| 996 |
memcpy((void *) (*long_pp), (void *) &entry.data32, (sizeof(uint32)*entry.count)); |
| 997 |
ret.returncode=is_valid; |
| 998 |
return ret; |
| 999 |
} else if (entry.value_or_offset == is_offset) { |
| 1000 |
uint32 data32offset = entry.data32offset; |
| 1001 |
offset_t offset; |
| 1002 |
ret = read_offsetdata( ctif, data32offset, entry.count, entry.datatype, &offset, &ret); |
| 1003 |
if (ret.returncode != is_valid) { |
| 1004 |
/* FIXME: free(offset.datacharp); */ |
| 1005 |
return ret; |
| 1006 |
} |
| 1007 |
//printf("LONG (offset)=%lu\n", *offset.datacharp); |
| 1008 |
memcpy((void *) (*long_pp), (void *) offset.datacharp, (sizeof(uint32)*offset.count)); |
| 1009 |
free(offset.datacharp); |
| 1010 |
ret.returncode=is_valid; |
| 1011 |
return ret; |
| 1012 |
} |
| 1013 |
} |
| 1014 |
ret.returncode = tag_does_not_exist; |
| 1015 |
return ret; |
| 1016 |
} |
| 1017 |
|
| 1018 |
ret_t TIFFGetFieldSHORT(ctiff_t * ctif, cifd_t *ifd, tag_t tag, uint16 ** short_pp, uint32 * countp) { |
| 1019 |
assert( *short_pp == NULL); |
| 1020 |
assert( countp != NULL); |
| 1021 |
*countp = 0; /* init */ |
| 1022 |
GET_EMPTY_RET(ret) |
| 1023 |
int tagidx = TIFFGetRawTagListIndex(ifd, tag); |
| 1024 |
if (tagidx >= 0) { /* there exists the tag */ |
| 1025 |
ifd_entry_t entry = TIFFGetRawTagIFDListEntry( ctif, ifd, tagidx ); |
| 1026 |
uint32 overflow = (0xffffffff / (sizeof(uint16))); |
| 1027 |
if (entry.count > overflow) { |
| 1028 |
ret = set_value_found_ret_formatted_with_returncode(&ret, tagerror_count_results_in_offsets_overflow, "count=%u overflow boundary=%u", entry.count, overflow); |
| 1029 |
return ret; |
| 1030 |
} |
| 1031 |
|
| 1032 |
*countp = entry.count; |
| 1033 |
*(short_pp) = malloc( (size_t) sizeof(uint16) * entry.count); |
| 1034 |
if (NULL == *(short_pp)) { |
| 1035 |
ret.returncode=could_not_allocate_memory; |
| 1036 |
return ret; |
| 1037 |
} |
| 1038 |
/* assert (entry.datatype == TIFF_SHORT); */ |
| 1039 |
if (entry.datatype != TIFF_SHORT) { |
| 1040 |
ret = set_value_found_ret_formatted_with_returncode(&ret, tagerror_unexpected_type_found, "data of type SHORT expected, but got type %s", TIFFTypeName(entry.datatype)); |
| 1041 |
return ret; |
| 1042 |
} |
| 1043 |
|
| 1044 |
if (entry.value_or_offset == is_value) { |
| 1045 |
assert (entry.count <= 2); |
| 1046 |
memcpy((void *) (*short_pp), (void *) &entry.data16, (sizeof(uint16)*entry.count)); |
| 1047 |
//printf("SHORT (direct)=%u %u\n", entry.data32, **short_pp); |
| 1048 |
ret.returncode=is_valid; |
| 1049 |
return ret; |
| 1050 |
} else if (entry.value_or_offset == is_offset) { |
| 1051 |
uint32 data32offset = entry.data32offset; |
| 1052 |
offset_t offset; |
| 1053 |
ret = read_offsetdata( ctif, data32offset, entry.count, entry.datatype, &offset, &ret); |
| 1054 |
if (ret.returncode != is_valid) { |
| 1055 |
/* FIXME: free(offset.datacharp); */ |
| 1056 |
return ret; |
| 1057 |
} |
| 1058 |
//printf("SHORT (offset)=%u\n", *offset.datacharp); |
| 1059 |
memcpy((void *) (*short_pp), (void *)offset.datacharp, (sizeof(uint16)*offset.count)); |
| 1060 |
free( offset.datacharp ); |
| 1061 |
offset.datacharp=NULL; |
| 1062 |
ret.returncode=is_valid; |
| 1063 |
return ret; |
| 1064 |
} |
| 1065 |
} |
| 1066 |
ret.returncode = tag_does_not_exist; |
| 1067 |
return ret; |
| 1068 |
} |
| 1069 |
|
| 1070 |
ret_t TIFFGetFieldRATIONAL(ctiff_t * ctif, cifd_t *ifd, tag_t tag, float ** float_pp, uint32 * countp) { |
| 1071 |
assert( *float_pp == NULL); |
| 1072 |
assert( countp != NULL); |
| 1073 |
*countp = 0; /* init */ |
| 1074 |
GET_EMPTY_RET(ret) |
| 1075 |
int tagidx = TIFFGetRawTagListIndex(ifd, tag); |
| 1076 |
if (tagidx >= 0) { /* there exists the tag */ |
| 1077 |
ifd_entry_t entry = TIFFGetRawTagIFDListEntry( ctif, ifd, tagidx ); |
| 1078 |
uint32 overflow = (0xffffffff / (2*sizeof(uint32))); |
| 1079 |
if (entry.count > overflow) { |
| 1080 |
ret = set_value_found_ret_formatted_with_returncode(&ret, tagerror_count_results_in_offsets_overflow, "count=%u overflow boundary=%u", entry.count, overflow); |
| 1081 |
return ret; |
| 1082 |
} |
| 1083 |
//printf("entry.count=%i\n", entry.count); |
| 1084 |
*countp = entry.count; |
| 1085 |
*(float_pp) = malloc( sizeof(float) * (entry.count)); |
| 1086 |
if (NULL == *(float_pp)) { |
| 1087 |
ret.returncode=could_not_allocate_memory; |
| 1088 |
return ret; |
| 1089 |
} |
| 1090 |
/* assert (entry.datatype == TIFF_RATIONAL); */ |
| 1091 |
if (entry.datatype != TIFF_RATIONAL) { |
| 1092 |
ret = set_value_found_ret_formatted_with_returncode(&ret, tagerror_unexpected_type_found, "data of type RATIONAL expected, but got type %s", TIFFTypeName(entry.datatype)); |
| 1093 |
return ret; |
| 1094 |
} |
| 1095 |
|
| 1096 |
/* rational is defined as 2x32bits */ |
| 1097 |
if (entry.value_or_offset == is_value) { |
| 1098 |
ret.returncode = tagerror_expected_offsetdata; |
| 1099 |
return ret; |
| 1100 |
} else if (entry.value_or_offset == is_offset) { |
| 1101 |
uint32 data32offset = entry.data32offset; |
| 1102 |
offset_t offset; |
| 1103 |
//printf("data32offset=%u count=%i\n", data32offset, entry.count); |
| 1104 |
ret = read_offsetdata( ctif, data32offset, entry.count, entry.datatype, &offset, &ret); |
| 1105 |
if (ret.returncode != is_valid) { |
| 1106 |
/* FIXME: free(offset.datacharp); */ |
| 1107 |
return ret; |
| 1108 |
} |
| 1109 |
/* copy to a float */ |
| 1110 |
uint32 const * orig_data32p = offset.data32p; |
| 1111 |
for (uint32 i = 0; i< entry.count; i++, orig_data32p+=2) { |
| 1112 |
uint32 numerator = *(orig_data32p); |
| 1113 |
uint32 denominator = *(orig_data32p+1); |
| 1114 |
//printf("DEBUG: numerator=%u denumeator=%u\n", numerator, denominator); |
| 1115 |
float v; |
| 1116 |
if (0 == denominator) { |
| 1117 |
v=NAN; |
| 1118 |
} else { |
| 1119 |
v = (float) numerator / (float) denominator; |
| 1120 |
} |
| 1121 |
//printf("DEBUG2: *float_pp[%i]=%f (%u / %u)\n", i, v, numerator, denominator); |
| 1122 |
(*(float_pp))[i]=v; |
| 1123 |
} |
| 1124 |
free( offset.data32p ); |
| 1125 |
offset.data32p=NULL; |
| 1126 |
ret.returncode=is_valid; |
| 1127 |
return ret; |
| 1128 |
} |
| 1129 |
} |
| 1130 |
ret.returncode = tag_does_not_exist; |
| 1131 |
return ret; |
| 1132 |
} |
| 1133 |
|
| 1134 |
/* vim: set tabstop=2 softtabstop=2 shiftwidth=2 smarttab expandtab :*/ |