| 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 <assert.h> |
| 10 |
#include <stdio.h> |
| 11 |
|
| 12 |
#include "check.h" |
| 13 |
#include "check_helper.h" |
| 14 |
#include "risk_analyzer.h" |
| 15 |
|
| 16 |
|
| 17 |
static void add_mem_entry(mem_map_t * memmap_p, uint32 offset, uint32 count, memtype_t type) { |
| 18 |
mem_map_t memmap = *( memmap_p ); |
| 19 |
assert(NULL != memmap.base_p); |
| 20 |
assert(0 < memmap.max_entries); |
| 21 |
assert(memmap.count < memmap.max_entries); |
| 22 |
assert(type < mt_END_marker); |
| 23 |
mem_map_entry_t * memmap_it_p = memmap.base_p + memmap.count; |
| 24 |
memmap_it_p->offset=offset; |
| 25 |
memmap_it_p->count=count; |
| 26 |
memmap_it_p->mem_type=type; |
| 27 |
memmap.count++; |
| 28 |
assert( memmap.max_entries > memmap.count); |
| 29 |
*( memmap_p) = memmap; |
| 30 |
} |
| 31 |
|
| 32 |
static int compare_memmap(void const * lhs, void const * rhs) { |
| 33 |
const mem_map_entry_t left = *((mem_map_entry_t *) lhs); |
| 34 |
const mem_map_entry_t right = *((mem_map_entry_t *) rhs); |
| 35 |
return (((int) left.offset) - ((int) right.offset)); |
| 36 |
} |
| 37 |
|
| 38 |
/* TODO: add support for StripOffsets and StripByteCounts */ |
| 39 |
mem_map_t * scan_mem_map(ctiff_t * ctif) { |
| 40 |
assert( NULL != ctif); |
| 41 |
static mem_map_t memmap; |
| 42 |
memmap.count = 0; |
| 43 |
memmap.base_p = NULL; |
| 44 |
memmap.max_entries = 2048; |
| 45 |
memmap.base_p = malloc (sizeof(mem_map_entry_t) * memmap.max_entries); |
| 46 |
if (NULL == memmap.base_p) { |
| 47 |
perror ("could not allocate mem for memmap, abort"); |
| 48 |
} |
| 49 |
/* size of tiff file in bytes */ |
| 50 |
memmap.max_len = ctif->streamlen; |
| 51 |
/* header */ |
| 52 |
add_mem_entry( &memmap, 0, 4, mt_constant); |
| 53 |
/* IFD0 Offset */ |
| 54 |
add_mem_entry( &memmap, 4, 4, mt_offset_to_ifd0); |
| 55 |
/* IFDO */ |
| 56 |
uint32 ifdpos = get_ifd0_pos(ctif ); |
| 57 |
uint16 ifdcount = get_ifd0_count( ctif); /* how many tags */ |
| 58 |
|
| 59 |
add_mem_entry(&memmap, ifdpos, 2, mt_ifd); /* count of tags in ifdpos */ |
| 60 |
unsigned int ifdbase= 2 + ifdpos; |
| 61 |
/* iterate through IFD0 entries */ |
| 62 |
uint16 tagidx; |
| 63 |
for (tagidx = 0; tagidx< ifdcount; tagidx++) { |
| 64 |
add_mem_entry(&memmap, (uint32) (ifdbase + (tagidx * 12)), 8, mt_ifd); /* tagid, field type, count */ |
| 65 |
ifd_entry_t ifd_entry = TIFFGetRawTagIFDListEntry( ctif, ctif->ifd0, tagidx ); |
| 66 |
tag_t tag = TIFFGetRawTagListEntry( ctif, ctif->ifd0, tagidx); |
| 67 |
TIFFDataType datatype = TIFFGetRawTagType( ctif, ctif->ifd0, tag); |
| 68 |
int datasize; |
| 69 |
switch (datatype) { |
| 70 |
case TIFF_ASCII: datasize = 1; break; |
| 71 |
case TIFF_LONG: datasize = 4; break; |
| 72 |
case TIFF_SHORT: datasize = 2; break; |
| 73 |
case TIFF_BYTE: datasize = 1; break; |
| 74 |
case TIFF_UNDEFINED: datasize = 1; break; |
| 75 |
case TIFF_RATIONAL: datasize = 8; break; |
| 76 |
case TIFF_SSHORT: datasize = 2; break; |
| 77 |
case TIFF_SBYTE: datasize = 1; break; |
| 78 |
case TIFF_SLONG: datasize = 4; break; |
| 79 |
case TIFF_SRATIONAL: datasize = 8; break; |
| 80 |
case TIFF_FLOAT: datasize = 4; break; |
| 81 |
case TIFF_DOUBLE: datasize = 8; break; |
| 82 |
default: fprintf(stderr, "unknown datatype %i, possible a program error", datatype); exit(EXIT_FAILURE); |
| 83 |
} |
| 84 |
uint32 offset = ifd_entry.data32offset; |
| 85 |
uint32 count = ifd_entry.count; |
| 86 |
|
| 87 |
if (tag == TIFFTAG_STRIPOFFSETS) { |
| 88 |
if (ifd_entry.value_or_offset == is_offset) { |
| 89 |
add_mem_entry(&memmap, (uint32) (ifdbase + (tagidx * 12) + 8), 4, mt_ifd_offset_to_standardized_value ); |
| 90 |
add_mem_entry( &memmap, offset, ((uint32) count)*datasize, mt_ifd_offset_to_stripoffsets ); |
| 91 |
} else if (ifd_entry.value_or_offset==is_value) { |
| 92 |
add_mem_entry(&memmap, (uint32) (ifdbase + (tagidx * 12) + 8), 4, mt_ifd_embedded_standardized_value ); |
| 93 |
} |
| 94 |
/* |
| 95 |
ifd_entry_t stripoffset_entry; |
| 96 |
stripoffset_entry=ifd_entry; |
| 97 |
//handle stripoffset data |
| 98 |
|
| 99 |
//printf("ifd_count=%i, stripoffset_count=%i\n", stripoffset_entry.count, stripoffset_count); |
| 100 |
uint32 stripoffset_values[stripoffset_entry.count]; |
| 101 |
switch (stripoffset_entry.datatype) { |
| 102 |
case TIFF_LONG: { |
| 103 |
// value |
| 104 |
if (stripoffset_entry.value_or_offset == is_value) { |
| 105 |
for (uint32 i=0; i< stripoffset_entry.count; i++) { |
| 106 |
stripoffset_values[i] = stripoffset_entry.data32; |
| 107 |
} |
| 108 |
} |
| 109 |
// offset |
| 110 |
if (stripoffset_entry.value_or_offset == is_offset) { |
| 111 |
offset_t offset; |
| 112 |
ret_t ret = read_offsetdata(ctif, stripoffset_entry.data32offset, stripoffset_entry.count, stripoffset_entry.datatype, &offset, &ret); |
| 113 |
if (ret.returncode != is_valid) { |
| 114 |
fprintf(stderr, "error reading offset data, stripoffset_entry.count has size %u, resulting address %zu, but only offset to %lu is possible\n", stripoffset_entry.count, ((uint64) stripoffset_entry.count*sizeof(uint32)), (uint64) 0xffffffff); |
| 115 |
exit(EXIT_FAILURE); |
| 116 |
} |
| 117 |
uint32 * p = offset.data32p; |
| 118 |
if ((uint64) stripoffset_entry.count*sizeof(uint32) > 0xffffffff) { |
| 119 |
fprintf(stderr, "stripoffset_entry.count has size %u, resulting address %zu, but only offset to %lu is possible\n", stripoffset_entry.count, ((uint64) stripoffset_entry.count*sizeof(uint32)), (uint64) 0xffffffff); |
| 120 |
exit(EXIT_FAILURE); |
| 121 |
} |
| 122 |
for (uint32 i=0; i< stripoffset_entry.count; i++) { |
| 123 |
uint32 pval = *p; |
| 124 |
if (is_byteswapped(ctif)) { |
| 125 |
TIFFSwabLong(&pval); |
| 126 |
} |
| 127 |
stripoffset_values[i]=pval; |
| 128 |
p++; |
| 129 |
} |
| 130 |
} |
| 131 |
break; |
| 132 |
} |
| 133 |
case TIFF_SHORT: { |
| 134 |
// value |
| 135 |
if (stripoffset_entry.value_or_offset == is_value) { |
| 136 |
for (uint32 i=0; i< stripoffset_entry.count; i++) { |
| 137 |
stripoffset_values[i]= stripoffset_entry.data16[i]; |
| 138 |
} |
| 139 |
} |
| 140 |
// offset |
| 141 |
if (stripoffset_entry.value_or_offset == is_offset) { |
| 142 |
offset_t offset; |
| 143 |
ret_t ret = read_offsetdata(ctif, stripoffset_entry.data32offset, stripoffset_entry.count, stripoffset_entry.datatype, &offset, &ret); |
| 144 |
if (ret.returncode != is_valid) { |
| 145 |
fprintf(stderr, "error reading offset data, stripoffset_entry.count has size %u, resulting address %zu, but only offset to %lu is possible\n", stripoffset_entry.count, ((uint64) stripoffset_entry.count*sizeof(uint32)), (uint64) 0xffffffff); |
| 146 |
exit(EXIT_FAILURE); |
| 147 |
} |
| 148 |
uint16 * p = offset.data16p; |
| 149 |
if ((uint64) stripoffset_entry.count*sizeof(uint16) > 0xffffffff) { |
| 150 |
fprintf(stderr, "stripoffset_entry.count has size %u, resulting address %zu, but only offset to %lu is possible\n", stripoffset_entry.count, ((uint64) stripoffset_entry.count*sizeof(uint16)), (uint64) 0xffffffff); |
| 151 |
exit(EXIT_FAILURE); |
| 152 |
} |
| 153 |
for (uint32 i=0; i< stripoffset_entry.count; i++) { |
| 154 |
uint16 pval = *p; |
| 155 |
if (is_byteswapped(ctif)) { |
| 156 |
TIFFSwabShort(&pval); |
| 157 |
} |
| 158 |
stripoffset_values[i]=pval; |
| 159 |
p++; |
| 160 |
} |
| 161 |
} |
| 162 |
break; |
| 163 |
} |
| 164 |
default: { |
| 165 |
perror("stripoffset_entry.datatype has an unexpected value, possible a program error\n"); |
| 166 |
exit(EXIT_FAILURE); |
| 167 |
} |
| 168 |
} |
| 169 |
//printf("count=%i\n", stripoffset_entry.count); |
| 170 |
//TODO: |
| 171 |
// for (int i=0; i< stripoffset_entry.count; i++) { |
| 172 |
// uint32 rawstriplen = TIFFRawStripSize(ctif->tif, i); |
| 173 |
//printf("OFFSET: p[%i]=%u len=%i\n", i,stripoffset_values[i], rawstriplen); |
| 174 |
//add_mem_entry( &memmap, stripoffset_values[i], rawstriplen, mt_stripoffset_value); |
| 175 |
//} |
| 176 |
*/ |
| 177 |
|
| 178 |
} else if (ifd_entry.value_or_offset==is_offset) { /* offset */ |
| 179 |
if (tag < 32768) { /* standard tag */ |
| 180 |
add_mem_entry(&memmap, (uint32) (ifdbase + (tagidx * 12) + 8), 4, mt_ifd_offset_to_standardized_value ); |
| 181 |
add_mem_entry( &memmap, offset, ((uint32) count)*datasize, mt_standardized_value ); |
| 182 |
} else if (tag < 65000) { /* registered tag */ |
| 183 |
add_mem_entry(&memmap, (uint32) (ifdbase + (tagidx * 12) + 8), 4, mt_ifd_offset_to_registered_value ); |
| 184 |
add_mem_entry( &memmap, offset, ((uint32) count)*datasize, mt_registered_value ); |
| 185 |
} else { /* private tag */ |
| 186 |
add_mem_entry(&memmap, (uint32) (ifdbase + (tagidx * 12) + 8), 4, mt_ifd_offset_to_private_value ); |
| 187 |
add_mem_entry( &memmap, offset, ((uint32) count)*datasize, mt_private_value ); |
| 188 |
} |
| 189 |
} else if (ifd_entry.value_or_offset==is_value) { /* embedded value */ |
| 190 |
if (tag < 32768) { /* standard tag */ |
| 191 |
add_mem_entry(&memmap, (uint32) (ifdbase + (tagidx * 12) + 8), 4, mt_ifd_embedded_standardized_value ); |
| 192 |
} else if (tag < 65000) { /* registered tag */ |
| 193 |
add_mem_entry(&memmap, (uint32) (ifdbase + (tagidx * 12) + 8), 4, mt_ifd_embedded_registered_value ); |
| 194 |
} else { /* private tag */ |
| 195 |
add_mem_entry(&memmap, (uint32) (ifdbase + (tagidx * 12) + 8), 4, mt_ifd_embedded_private_value ); |
| 196 |
} |
| 197 |
|
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
/* check next IFD mark */ |
| 202 |
// uint32 offset = get_ifd0_pos(ctif ); |
| 203 |
// uint32 IFDn = get_next_ifd_pos( ctif, offset ); |
| 204 |
// printf("IFD: offset=%i, IFD0=%i IFDn=%i ifdpos+count=%i\n", offset, ifdpos, IFDn, ifdbase+12*count); |
| 205 |
add_mem_entry(&memmap, (uint32) (ifdbase + 12 * ifdcount), 4, mt_offset_to_ifd); |
| 206 |
|
| 207 |
/* sort entries by offset */ |
| 208 |
qsort(memmap.base_p, (size_t) memmap.count, sizeof( mem_map_entry_t), compare_memmap); |
| 209 |
/* |
| 210 |
printf("memmap before HOLE detection\n"); |
| 211 |
print_mem_map( &memmap ); |
| 212 |
printf("----------------------------\n"); |
| 213 |
*/ |
| 214 |
|
| 215 |
/* add all unused areas */ |
| 216 |
uint32 memmap_orig_count = (uint32) memmap.count; |
| 217 |
for (uint32 j=1; j< memmap_orig_count; j++) { |
| 218 |
const mem_map_entry_t * prev=memmap.base_p+j-1; |
| 219 |
const mem_map_entry_t * act =memmap.base_p+j; |
| 220 |
uint32 estimated_offset = (prev->offset + prev->count); |
| 221 |
if (estimated_offset < act->offset) { /* found a hole */ |
| 222 |
printf("HOLE FOUND at %u\n", estimated_offset); |
| 223 |
printf("\tprev->offset=%u prev->count=%u estimated=%u\n", prev->offset, prev->count, estimated_offset); |
| 224 |
printf("\tact->offset=%u act->count=%u\n", act->offset, act->count); |
| 225 |
add_mem_entry( &memmap, estimated_offset, (act->offset -estimated_offset), mt_unused); |
| 226 |
} |
| 227 |
} |
| 228 |
/* sort entries by offset again */ |
| 229 |
qsort(memmap.base_p, (size_t) memmap.count, sizeof( mem_map_entry_t), compare_memmap); |
| 230 |
/* add unused area at end */ |
| 231 |
mem_map_entry_t * last = memmap.base_p + memmap.count-1; |
| 232 |
uint32 estimated_offset = (last->offset + last->count); |
| 233 |
if (memmap.max_len > estimated_offset) { |
| 234 |
printf("HOLE (at end) FOUND at %u\n", estimated_offset); |
| 235 |
add_mem_entry( &memmap, estimated_offset, (memmap.max_len -estimated_offset), mt_unused); |
| 236 |
} |
| 237 |
/* sort entries by offset again */ |
| 238 |
qsort(memmap.base_p, (size_t) memmap.count, sizeof( mem_map_entry_t), compare_memmap); |
| 239 |
return &memmap; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
void print_mem_map(const mem_map_t *memmap_p ) { |
| 245 |
assert(NULL != memmap_p); |
| 246 |
const mem_map_t memmap = *( memmap_p ); |
| 247 |
printf("size of file is %u bytes\n", memmap.max_len); |
| 248 |
for (int j=0; j< memmap.count; j++) { |
| 249 |
mem_map_entry_t * i=memmap.base_p+j; |
| 250 |
printf ("entry=%03i, offset_start=%10u, count=%10u, offset_next=%10u, [%02i], type=%s\n", j,i->offset, i->count, (i->offset + i->count), i->mem_type, memtype_string[i->mem_type]); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
void print_mem_stats(const mem_map_t *memmap_p) { |
| 255 |
uint32 size = memmap_p->max_len; |
| 256 |
uint32 stat[ mt_END_marker ]; |
| 257 |
uint32 counted=0; |
| 258 |
for (int i=0; i< mt_END_marker; i++) { |
| 259 |
stat[i]=0; |
| 260 |
} |
| 261 |
|
| 262 |
for (int j=0; j< memmap_p->count; j++) { |
| 263 |
mem_map_entry_t * i=memmap_p->base_p+j; |
| 264 |
stat[i->mem_type]+= i->count; |
| 265 |
counted+=i->count; |
| 266 |
} |
| 267 |
for (int i=0; i<mt_END_marker; i++) { |
| 268 |
float ratio = ((float) (stat[i])) / ((float) size); |
| 269 |
printf("[%02i], type=%32s, bytes=%10u, ratio=%0.5f\n", i, memtype_string[i], stat[i], ratio); |
| 270 |
} |
| 271 |
printf("counted: %u bytes, size: %u bytes\n", counted, size); |
| 272 |
} |
| 273 |
/* vim: set tabstop=2 softtabstop=2 shiftwidth=2 smarttab expandtab :*/ |