| 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 |
#ifndef FIXIT_TIFF_CHECK |
| 10 |
#define FIXIT_TIFF_CHECK |
| 11 |
#ifndef GNU_SOURCE |
| 12 |
#define GNU_SOURCE |
| 13 |
#endif |
| 14 |
#include <stdlib.h> |
| 15 |
#include <unistd.h> /* for off_t definition */ |
| 16 |
#include "tiff.h" |
| 17 |
#include "ctstring.h" |
| 18 |
#include <stdbool.h> |
| 19 |
|
| 20 |
typedef int thandle_t; |
| 21 |
|
| 22 |
typedef uint16 tag_t; |
| 23 |
|
| 24 |
/* MINTAGS - MAXTAGS is range of possible existing TAG numbers */ |
| 25 |
#define MINTAGS 254U |
| 26 |
#define MAXTAGS 65536U |
| 27 |
|
| 28 |
typedef enum{ is_memmap, is_filep } ct_ioflag_t ; /* flag */ |
| 29 |
|
| 30 |
typedef enum{ unknown_tag_order, has_sorted_tags, has_unsorted_tags, } tagorder_t; |
| 31 |
|
| 32 |
typedef struct cifd_s { |
| 33 |
off_t position; /* offset in tif stream (in bytes) */ |
| 34 |
uint16 tag_count; /* count of tags in first ifd */ |
| 35 |
tagorder_t tagorder; |
| 36 |
int tag_if_subifd; |
| 37 |
int index_cache_by_tag[MAXTAGS]; |
| 38 |
tag_t tag_cache_by_index[MAXTAGS]; |
| 39 |
} cifd_t; |
| 40 |
|
| 41 |
typedef struct ctiff_s { |
| 42 |
cifd_t *ifd0; /* first IFD */ |
| 43 |
char *filename; /* filename */ |
| 44 |
uint8 * streamp; /* address of memory-mapped tif stream */ |
| 45 |
uint8 * actual_streamp; |
| 46 |
size_t streamlen; /* length of tif stream/file (in bytes) */ |
| 47 |
thandle_t fd; /* filedescriptor */ |
| 48 |
ct_ioflag_t ioflag; |
| 49 |
bool isbyteswapped; /* true if BigEndian */ |
| 50 |
} ctiff_t; |
| 51 |
|
| 52 |
typedef enum{ |
| 53 |
rm_default, |
| 54 |
rm_file, |
| 55 |
rm_rule, |
| 56 |
rm_tag, |
| 57 |
rm_value, |
| 58 |
rm_expected, |
| 59 |
rm_hard_error, |
| 60 |
rm_error, |
| 61 |
rm_warning, |
| 62 |
rm_precondition, |
| 63 |
rm_logicalor_error, |
| 64 |
rm_logicalor_valid, |
| 65 |
rm_is_valid, |
| 66 |
rm_endrule, |
| 67 |
rm_endtiff, |
| 68 |
rm_mode, |
| 69 |
rm_lineno, |
| 70 |
rm_error_description, |
| 71 |
rm_count_valid, |
| 72 |
rm_count_invalid, |
| 73 |
rm_summary_valid, |
| 74 |
rm_summary_invalid, |
| 75 |
} rm_type_t; |
| 76 |
|
| 77 |
typedef struct retmsg_s { |
| 78 |
string_t rm_msg; |
| 79 |
struct retmsg_s * next; |
| 80 |
rm_type_t rm_type; |
| 81 |
} retmsg_t; |
| 82 |
|
| 83 |
#define ICC_ERROR_OFFSET 100 |
| 84 |
|
| 85 |
typedef enum { |
| 86 |
is_valid=0, /* no problems detected */ |
| 87 |
calling_error_count_size, /* a called function has wrong arguments */ |
| 88 |
could_not_allocate_memory, /* malloc fails */ |
| 89 |
could_not_print, /* snprintf, fprintf, print fails */ |
| 90 |
should_not_occur, /* dummy, for dead code */ |
| 91 |
tagerror_expected_count_differs, /* if a tag reports count=m, but the rule expects count=n */ |
| 92 |
tagerror_expected_count_iszero, /* if a tag reports count=0, but the rule expects count=n */ |
| 93 |
tagerror_expected_count_isgreaterone, |
| 94 |
tagerror_expected_count_less_than_four, /* needed for geotiff */ |
| 95 |
tagerror_expected_count_not_a_multiple_of_four, /* needed for geotiff */ |
| 96 |
tagerror_value_differs, /* if a tag reports value=m, but the rule expects value=n */ |
| 97 |
tagerror_unexpected_type_found, /* if a tag reports a type=m, but a type=n was expected */ |
| 98 |
tagerror_datetime_not_plausible, /* if a datetime tag has date values out of range */ |
| 99 |
tagerror_datetime_wrong_format, /* if a datetime tag has date with wrong format string */ |
| 100 |
tagerror_datetime_wrong_size, /* if a datetime tag has date with wrong size */ |
| 101 |
tagerror_value_not_found, /* if a value of a given tag is not found */ |
| 102 |
tagwarn_type_of_unknown_tag_could_not_be_checked, /* there is no explicite type check defined yet */ |
| 103 |
tag_exist, /* if a tag exists, but should not (only used by fc_notag) */ |
| 104 |
tag_does_not_exist, /* if a tag does not exist, but should (only used by fc_tag_quiet) */ |
| 105 |
tagerror_encoded_as_value_excited_space, /* tag encoded as value instead as offset, but there is no enough space to hold it, pE. ICC-Profile encoded without offset */ |
| 106 |
tagerror_pcre_nomatch, /* regex does not match value */ |
| 107 |
pcre_compile_error, /* regex is wrong */ |
| 108 |
tagerror_multiple_zeros_in_asciivalue, /* doubled \0 in string */ |
| 109 |
tagerror_no_zero_as_end_of_string_in_asciivalue, |
| 110 |
tagerror_denominator_is_zero_in_fract, |
| 111 |
ifderror_offset_used_twice, |
| 112 |
ifderror_multiple_ifd_detected, |
| 113 |
ifderror_tags_not_in_ascending_order, |
| 114 |
tagerror_offset_not_word_aligned, |
| 115 |
ifderror_offset_not_word_aligned, |
| 116 |
iccerror_profileclass= ICC_ERROR_OFFSET + 0, /* profile class ('%s'), should be one of following strings for device classes: 'scnr', 'mntr', 'prtr' or for profile classes: 'link', 'spac', 'abst', 'nmcl'" */ |
| 117 |
iccerror_colorspacedata= ICC_ERROR_OFFSET + 1, /* colorspace data ('%s'), should be one of following strings: 'XYZ ' 'Lab ' 'Luv ' 'YCbr' 'Yvx ' 'RGB ' 'GRAY' 'HSV ' 'HLS ' 'CMYK' 'CMY ' '2CLR' '3CLR' '4CLR' '5CLR' '6CLR' '7CLR' '8CLR' '9CLR' 'ACLR' 'BCLR' 'CCLR' 'DCLR' 'ECLR' 'FCLR'" */ |
| 118 |
iccerror_connectionspacedata= ICC_ERROR_OFFSET + 2, /* "connection space data ('%s') should be one of following strings: 'XYZ ' 'Lab '" */ |
| 119 |
iccerror_primaryplatformsignature= ICC_ERROR_OFFSET + 3, /* primary plattform signature ('%s') should be empty or one of following strings: 'APPL', 'MSFT', 'SGI ', 'SUNW', 'TGNT' */ |
| 120 |
iccerror_header_1v43_2010= ICC_ERROR_OFFSET + 4, /* Invalid ICC profile 1v43_2010, see http://www.color.org/specification/ICC1v43_2010-12.pdf for details */ |
| 121 |
iccerror_header_v240_v430= ICC_ERROR_OFFSET + 5, /* Invalid ICC profile ICC.1:2001-04, see http://www.color.org/ICC_Minor_Revision_for_Web.pdf for details */ |
| 122 |
iccerror_header_generic= ICC_ERROR_OFFSET + 6, /* size < 10 */ |
| 123 |
iccerror_preferredcmmtype= ICC_ERROR_OFFSET + 7, /* preferred cmmtype ('%s') should be empty or (possibly, because ICC validation is alpha code) one of following strings: 'ADBE' 'ACMS' 'appl' 'CCMS' 'UCCM' 'UCMS' 'EFI ' 'FF ' 'EXAC' 'HCMM' 'argl' 'LgoS' 'HDM ' 'lcms' 'KCMS' 'MCML' 'WCS ' 'SIGN' 'RGMS' 'SICC' 'TCMM' '32BT' 'WTG ' 'zc00'" */ |
| 124 |
iccerror_committed_size_differs = ICC_ERROR_OFFSET + 8, |
| 125 |
iccerror_header_v500_2016 = ICC_ERROR_OFFSET +9, |
| 126 |
iccerror_header_version_undefined = ICC_ERROR_OFFSET +10, |
| 127 |
iccerror_header_version_outdated = ICC_ERROR_OFFSET +11, |
| 128 |
iccerror_badprofile_id = ICC_ERROR_OFFSET +12, /* if md5 missmatch */ |
| 129 |
iccerror_not_ascii = ICC_ERROR_OFFSET +13, /* if text fields uses 8bit */ |
| 130 |
iccerror_profile_description_tag = ICC_ERROR_OFFSET +14, /* if profile description tag has wrong length */ |
| 131 |
iccerror_error_found_by_IccProfLib = ICC_ERROR_OFFSET +90, /* generic IccProfLib */ |
| 132 |
iccerror_harderror_found_by_IccProfLib = ICC_ERROR_OFFSET +91, /* generic IccProfLib */ |
| 133 |
iccerror_should_not_occure = ICC_ERROR_OFFSET + 99, |
| 134 |
tiff_seek_error_header, |
| 135 |
tiff_read_error_header, |
| 136 |
tiff_seek_error_offset, |
| 137 |
tiff_read_error_offset, |
| 138 |
tiff_byteorder_error, /* not an expected byteorder found */ |
| 139 |
tiff_ifd0_offset_must_be_greater_than_eight, /* must be greater than 8, because first 8 Bytes contains the TIFF header */ |
| 140 |
code_error_streampointer_empty, |
| 141 |
code_error_filedescriptor_empty, |
| 142 |
code_error_ctif_empty, |
| 143 |
parser_error_wrong_function_found_in_parser_state_exe_stack, |
| 144 |
parser_logicalor_error, |
| 145 |
tagerror_expected_offsetdata, |
| 146 |
tagerror_count_results_in_offsets_overflow, /* example: rational is defined as 2 uint32. offset is defined as uint32. If we read count>2147483647 we got offset overflow */ |
| 147 |
parser_logical_combine_open, /* if fc_logicalcombine was called first (no error) */ |
| 148 |
parser_logical_combine_close, /* if fc_logicalcombine was called first (no error) */ |
| 149 |
tagerror_offset_is_zero, |
| 150 |
ifderror_different_cardinality, |
| 151 |
tagerror_not_white_listed, |
| 152 |
ifderror_offset_out_of_filesize, |
| 153 |
ifderror_zero_tags, |
| 154 |
} returncode_t; |
| 155 |
|
| 156 |
typedef struct ret_s { |
| 157 |
returncode_t returncode; |
| 158 |
int logical_or_count; |
| 159 |
string_t value_found; |
| 160 |
string_t expected_value; |
| 161 |
} ret_t; |
| 162 |
|
| 163 |
typedef struct ifd_entry_s { |
| 164 |
uint32 count; /* count of data */ |
| 165 |
TIFFDataType datatype; |
| 166 |
enum{ is_offset, is_value, is_error } value_or_offset; |
| 167 |
union { |
| 168 |
uint32 data32; |
| 169 |
uint16 data16[2]; |
| 170 |
uint8 data8[4]; |
| 171 |
uint32 data32offset; |
| 172 |
}; |
| 173 |
} ifd_entry_t; |
| 174 |
|
| 175 |
typedef struct offset_s { |
| 176 |
uint32 count; /* count of data */ |
| 177 |
TIFFDataType datatype; |
| 178 |
union { |
| 179 |
uint8 *data8p; |
| 180 |
uint16 *data16p; |
| 181 |
uint32 *data32p; |
| 182 |
char *datacharp; |
| 183 |
int8 *datas8p; |
| 184 |
int16 *datas16p; |
| 185 |
int32 *datas32p; |
| 186 |
float *datafloatp; |
| 187 |
double *datadoublep; |
| 188 |
uint64 *data64p; |
| 189 |
int64 *datas64p; |
| 190 |
}; |
| 191 |
} offset_t; |
| 192 |
|
| 193 |
|
| 194 |
static const int no_index_found = -1; |
| 195 |
static const int no_file_found = -1; |
| 196 |
#define EXPECTSTRLEN 64 |
| 197 |
#define VALUESTRLEN 1024 |
| 198 |
#define TIFFAILSTRLEN (EXPECTSTRLEN + VALUESTRLEN) |
| 199 |
#define MAXRESULT 48000U |
| 200 |
|
| 201 |
#ifdef HARDEN |
| 202 |
#define TIFP_CHECK( ctif, ret ) \ |
| 203 |
if (NULL == ctif) { ret.returncode = code_error_ctif_empty; ret.value_found=str("code error, empty ctif-structure"); return ret; };\ |
| 204 |
if ((0 > ctif->fd) && (NULL == ctif->streamp)) {\ |
| 205 |
if (0 > ctif->fd) { ret.returncode = code_error_filedescriptor_empty; ret.value_found=str("empty filedescriptor"); return ret; };\ |
| 206 |
if (NULL == ctif->streamp) { ret.returncode = code_error_streampointer_empty; ret.value_found=str("empty stream pointer"); return ret; }; \ |
| 207 |
} |
| 208 |
#else |
| 209 |
#define TIFP_CHECK( ctif, ret ) ; |
| 210 |
#endif |
| 211 |
|
| 212 |
|
| 213 |
//returncode_t tifp_check( ctiff_t * ctif); |
| 214 |
/* |
| 215 |
ret_t tif_returns(tag_t tag, const char* expected, const char* value); |
| 216 |
ret_t tif_fails_tag(tag_t tag, const char* expected, const char* value); |
| 217 |
ret_t tif_fails(const char* fail_message); |
| 218 |
ret_t tif_fails_by_returns( ret_t ret ); |
| 219 |
ret_t tif_rules_tag(tag_t tag, const char *msg); |
| 220 |
ret_t tif_rules(const char *msg); |
| 221 |
ret_t tif_files(const char *msg); |
| 222 |
ret_t tif_no_tag(tag_t tag); |
| 223 |
ret_t _empty_result(); |
| 224 |
*/ |
| 225 |
|
| 226 |
string_t float2str(float v); |
| 227 |
string_t tag2str(tag_t tag); |
| 228 |
string_t int2str(int v); |
| 229 |
string_t uint2str(unsigned int v); |
| 230 |
string_t frac2str(int d, int n); |
| 231 |
string_t range2str(int d, int n); |
| 232 |
off_t ct_seek(ctiff_t * ctif, off_t pos, int whence); |
| 233 |
ssize_t ct_read8(ctiff_t * ctif, uint8 *buf, size_t byte_count); |
| 234 |
ssize_t ct_read16(ctiff_t * ctif, uint16 *buf, size_t word_count); |
| 235 |
ssize_t ct_read32(ctiff_t * ctif, uint32 *buf, size_t quadruple_count); |
| 236 |
ret_t check_tag_has_some_of_these_values( ctiff_t * ctif, tag_t tag, unsigned int count, const unsigned int * values); |
| 237 |
ret_t check_tag_has_valuelist( ctiff_t * ctif, tag_t tag, unsigned int count, const unsigned int * values); |
| 238 |
ret_t check_tag_has_value_in_range(ctiff_t * ctif, tag_t tag, unsigned int a, unsigned int b); |
| 239 |
ret_t check_tag_has_value(ctiff_t * ctif, tag_t tag, unsigned int value); |
| 240 |
ret_t check_tag_has_value_quiet(ctiff_t * ctif, tag_t tag, unsigned int expected_value); |
| 241 |
ret_t check_tag(ctiff_t * ctif, tag_t tag); |
| 242 |
returncode_t check_tag_quiet(ctiff_t * ctif, tag_t tag); |
| 243 |
ret_t check_notag(ctiff_t * ctif, tag_t tag); |
| 244 |
ret_t check_tag_has_valid_type(ctiff_t * ctif, tag_t tag); |
| 245 |
ret_t check_datetime(ctiff_t * ctif); |
| 246 |
ret_t check_icc(ctiff_t * ctif); |
| 247 |
ret_t check_has_only_one_ifd(ctiff_t * ctif); |
| 248 |
ret_t check_tagorder(ctiff_t * ctif); |
| 249 |
ret_t check_tag_has_valid_asciivalue(ctiff_t * ctif, tag_t tag); |
| 250 |
ret_t check_tag_has_value_matching_regex(ctiff_t * ctif, tag_t tag, const char* regex_string); |
| 251 |
ret_t check_all_offsets_are_word_aligned(ctiff_t *ctif); |
| 252 |
ret_t check_all_offsets_are_used_once_only(ctiff_t * ctif); |
| 253 |
ret_t check_all_offsets_are_greater_zero(ctiff_t * ctif); |
| 254 |
ret_t check_all_IFDs_are_word_aligned(ctiff_t *ctif); |
| 255 |
ret_t check_all_geotiff_tags( ctiff_t * ctif); |
| 256 |
ret_t check_cardinality_of_some_tags_are_equal( ctiff_t * ctif); |
| 257 |
ret_t check_all_offsets_within_filesize(ctiff_t * ctif); |
| 258 |
ret_t check_all_IFDs_have_tags(ctiff_t * ctif); |
| 259 |
void clean_ret(ret_t ret); |
| 260 |
|
| 261 |
#endif |
| 262 |
/* _FIXIT_TIFF_CHECK */ |