Debian Codegraph @

src/lib/validate_icc/check_icc_header.c

raw
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 * checks ICC Profile, using Spec http://www.color.org/specification/ICC1v43_2010-12.pdf
9 */
10
11 #include <assert.h>
12 #include <stdio.h>
13 #include <string.h>
14
15 #include "validate_icc.h"
16 #include "simple_string.h"
17
18 #ifdef USE_WARNING
19
20 #define INFO(...) fprintf(stderr,__VA_ARGS__);
21 #else
22 /* #define FAIL(RETCODE,...) {return RETCODE;}; */
23 #define INFO(...) ;
24 #endif
25
26 #define FAIL(RETCODE, ...) {snprintf(errmessage, errsize, __VA_ARGS__); return RETCODE;};
27
28 /* TODO: errormessage pointer und maxsize übergeben */
29
30 /* parses common parts of 4.3.0 and 5.0.0 (ICC MAX) headers */
31 static icc_returncode_t parse_icc_common_header(unsigned long iccsize, const char * iccdata, unsigned long errsize, char * errmessage, icc_profile_version_t icc_profile_version) {
32 assert(iccdata != NULL);
33 assert(errmessage != NULL);
34 assert(errsize > 0);
35 if (iccsize < 128) FAIL(icc_error_header_generic, "iccsize=%lu", iccsize);
36 unsigned long profilesize = (
37 ((iccdata[0] & 0x00ff) <<24) |
38 ((iccdata[1] & 0x00ff) <<16) |
39 ((iccdata[2] & 0x00ff) <<8) |
40 (iccdata[3] & 0x00ff)
41 ) & 0xffffffff;
42 INFO("ICC: profilesize=%lu %0lx\n", profilesize, profilesize);
43 if (profilesize != iccsize) FAIL(icc_error_committed_size_differs, "committed ICC size (%lu / 0x%04lx) differs from encoded profilesize (%lu / 0x%04lx)", iccsize, iccsize, profilesize, profilesize);
44 /* -- */
45 char preferredcmmtype[5]=" "; memcpy(preferredcmmtype, &iccdata[4],4);
46 INFO("ICC: preferredcmmtype='%s'\n", preferredcmmtype);
47 /* TODO: check if this is really correct, because found some TIFFs with
48 * value 'Lino', handle with care! */
49 /* should be empty or (possibly, because ICC validation is alpha code) one of following strings:
50 * 'ADBE' 'ACMS' 'appl' 'CCMS' 'UCCM' 'UCMS' 'EFI ' 'FF ' 'EXAC' 'HCMM' 'argl' 'LgoS' 'HDM '
51 * 'lcms' 'KCMS' 'MCML' 'WCS ' 'SIGN' 'RGMS' 'SICC' 'TCMM' '32BT' 'WTG ' 'zc00'"
52 */
53 if ( /* see document http://www.color.org/registry/signature/TagRegistry-2016-05.pdf */
54 ('\0' != preferredcmmtype[0]) &&
55 (0 != strncmp("ADBE", preferredcmmtype, 4)) &&
56 (0 != strncmp("ACMS", preferredcmmtype, 4)) &&
57 (0 != strncmp("appl", preferredcmmtype, 4)) &&
58 (0 != strncmp("CCMS", preferredcmmtype, 4)) &&
59 (0 != strncmp("UCCM", preferredcmmtype, 4)) &&
60 (0 != strncmp("UCMS", preferredcmmtype, 4)) &&
61 (0 != strncmp("EFI ", preferredcmmtype, 4)) &&
62 (0 != strncmp("FF ", preferredcmmtype, 4)) &&
63 (0 != strncmp("EXAC", preferredcmmtype, 4)) &&
64 (0 != strncmp("HCMM", preferredcmmtype, 4)) &&
65 (0 != strncmp("argl", preferredcmmtype, 4)) &&
66 (0 != strncmp("LgoS", preferredcmmtype, 4)) &&
67 (0 != strncmp("HDM ", preferredcmmtype, 4)) &&
68 (0 != strncmp("lcms", preferredcmmtype, 4)) &&
69 (0 != strncmp("KCMS", preferredcmmtype, 4)) &&
70 (0 != strncmp("MCML", preferredcmmtype, 4)) &&
71 (0 != strncmp("WCS ", preferredcmmtype, 4)) &&
72 (0 != strncmp("SIGN", preferredcmmtype, 4)) &&
73 (0 != strncmp("RGMS", preferredcmmtype, 4)) &&
74 (0 != strncmp("SICC", preferredcmmtype, 4)) &&
75 (0 != strncmp("TCMM", preferredcmmtype, 4)) &&
76 (0 != strncmp("32BT", preferredcmmtype, 4)) &&
77 (0 != strncmp("WTG ", preferredcmmtype, 4)) &&
78 #ifdef ALLOW_CMMTYPE_LINO
79 (0 != strncmp("LINO", preferredcmmtype, 4)) &&
80 (0 != strncmp("Lino", preferredcmmtype, 4)) &&
81 #endif
82 (0 != strncmp("zc00", preferredcmmtype, 4))
83
84 ) {
85 char hexdump[8192];
86 sprint_string_as_hexdump(hexdump, 8192, preferredcmmtype, 4);
87 FAIL(icc_error_preferredcmmtype, "preferred cmmtype ('%s')", hexdump);
88 }
89
90 /* -- */
91 char profileversion[6]=" ";
92 int snprintf_error = snprintf(profileversion, 6, "%i.%i.%i", (iccdata[8]) & 0x000f, ((iccdata[9] & 0x00f0) >>4), (iccdata[9] & 0x000f));
93 if (snprintf_error != 5) { FAIL(icc_error_header_version_undefined, "invalid profile version %s", profileversion) };
94 INFO("ICC: profileversion='%s'\n", profileversion);
95 // TODO: 2.4.0 or 4.3.0, others not allowed?
96 /* -- */
97 char profileclass[5]=" "; memcpy(profileclass, &iccdata[12],4);
98 INFO("ICC: profileclass='%s'\n", profileclass);
99 if ( /* see page 13, table 11 of http://www.color.org/ICC_Minor_Revision_for_Web.pdf */
100 (0 != strncmp("scnr", profileclass, 4)) &&
101 (0 != strncmp("mntr", profileclass, 4)) &&
102 (0 != strncmp("prtr", profileclass, 4)) &&
103 (0 != strncmp("link", profileclass, 4)) &&
104 (0 != strncmp("spac", profileclass, 4)) &&
105 (0 != strncmp("abst", profileclass, 4)) &&
106 (0 != strncmp("nmcl", profileclass, 4))
107 ) FAIL(icc_error_profileclass, "profile class ('%s'), should be one of following strings for device classes: 'scnr', 'mntr', 'prtr' or for profile classes: 'link', 'spac', 'abst', 'nmcl'", profileclass);
108 /* -- */
109 char colorspacedata[5]=" "; memcpy(colorspacedata, &iccdata[16],4);
110 INFO("ICC: colorspacedata='%s'\n", colorspacedata);
111 if ( /* see page 14, table 13 of http://www.color.org/ICC_Minor_Revision_for_Web.pdf */
112 (0 != strncmp("XYZ ", colorspacedata, 4)) &&
113 (0 != strncmp("Lab ", colorspacedata, 4)) &&
114 (0 != strncmp("Luv ", colorspacedata, 4)) &&
115 (0 != strncmp("YCbr", colorspacedata, 4)) &&
116 (0 != strncmp("Yvx ", colorspacedata, 4)) &&
117 (0 != strncmp("RGB ", colorspacedata, 4)) &&
118 (0 != strncmp("GRAY", colorspacedata, 4)) &&
119 (0 != strncmp("HSV ", colorspacedata, 4)) &&
120 (0 != strncmp("HLS ", colorspacedata, 4)) &&
121 (0 != strncmp("CMYK", colorspacedata, 4)) &&
122 (0 != strncmp("CMY ", colorspacedata, 4)) &&
123 (0 != strncmp("2CLR", colorspacedata, 4)) &&
124 (0 != strncmp("3CLR", colorspacedata, 4)) &&
125 (0 != strncmp("4CLR", colorspacedata, 4)) &&
126 (0 != strncmp("5CLR", colorspacedata, 4)) &&
127 (0 != strncmp("6CLR", colorspacedata, 4)) &&
128 (0 != strncmp("7CLR", colorspacedata, 4)) &&
129 (0 != strncmp("8CLR", colorspacedata, 4)) &&
130 (0 != strncmp("9CLR", colorspacedata, 4)) &&
131 (0 != strncmp("ACLR", colorspacedata, 4)) &&
132 (0 != strncmp("BCLR", colorspacedata, 4)) &&
133 (0 != strncmp("CCLR", colorspacedata, 4)) &&
134 (0 != strncmp("DCLR", colorspacedata, 4)) &&
135 (0 != strncmp("ECLR", colorspacedata, 4)) &&
136 (0 != strncmp("FCLR", colorspacedata, 4))
137 ) FAIL(icc_error_colorspacedata, "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'", colorspacedata);
138 /* -- */
139 char connectionspacedata[5]=" "; memcpy(connectionspacedata, &iccdata[20],4);
140 INFO("ICC: connectionspacedata(PCS)='%s'\n", connectionspacedata);
141 if ( /* see page 15, table 14 of http://www.color.org/ICC_Minor_Revision_for_Web.pdf */
142 (0 != strncmp("XYZ ", connectionspacedata, 4)) &&
143 (0 != strncmp("Lab ", connectionspacedata, 4))
144 ) FAIL(icc_error_connectionspacedata, "connection space data ('%s') should be one of following strings: 'XYZ ' 'Lab '", connectionspacedata);
145 /* -- */
146 // datetime 24-35
147 char datetime[37]; snprintf(datetime, 37, "%.4d:%.2d:%.2d %.2d:%.2d:%.2d",
148 ((iccdata[24] & 0x00ff)<<8 | (iccdata[25] & 0x00ff)), // yyyy
149 ((iccdata[26] & 0x00ff)<<8 | (iccdata[27] & 0x00ff)), // MM
150 ((iccdata[28] & 0x00ff)<<8 | (iccdata[29] & 0x00ff)), // DD
151 ((iccdata[30] & 0x00ff)<<8 | (iccdata[31] & 0x00ff)), // hh
152 ((iccdata[32] & 0x00ff)<<8 | (iccdata[33] & 0x00ff)), // mm
153 ((iccdata[34] & 0x00ff)<<8 | (iccdata[35] & 0x00ff)) // ss
154 );
155 INFO("ICC: datetime='%s'\n", datetime);
156
157
158 /* -- */
159 char profilefilesignature[5]=" "; memcpy(profilefilesignature, &iccdata[36],4);
160 INFO("ICC: profilefilesignature='%s'\n", profilefilesignature);
161 /* -- */
162 char primaryplattformsignature[5]=" "; memcpy(primaryplattformsignature, &iccdata[40],4);
163 INFO("ICC: primaryplattformsignature='%s'\n", primaryplattformsignature);
164 if ( /* see page 15, table 15 of http://www.color.org/ICC_Minor_Revision_for_Web.pdf */
165 ('\0' != primaryplattformsignature[0]) &&
166 (0 != strncmp("APPL", primaryplattformsignature, 4)) &&
167 (0 != strncmp("MSFT", primaryplattformsignature, 4)) &&
168 (0 != strncmp("SGI ", primaryplattformsignature, 4)) &&
169 (0 != strncmp("SUNW", primaryplattformsignature, 4)) &&
170 ((icc_profile_version == icc_profile_v240_v430) && (0 != strncmp("TGNT", primaryplattformsignature, 4)))
171 ) FAIL(icc_error_primaryplatformsignature, "primary plattform signature ('%s') should be empty or one of following strings: 'APPL', 'MSFT', 'SGI ', 'SUNW' (and for v2.x.x only: additional 'TGNT')", primaryplattformsignature);
172
173 /* -- */
174 // Profile Flags 44-47
175 /* -- */
176 char devicemanufacturer[5]=" "; memcpy(devicemanufacturer, &iccdata[48],4);
177 INFO("ICC: devicemanufacturer='%s'\n", devicemanufacturer);
178 /* -- */
179 char devicemodel[5]=" "; memcpy(devicemodel, &iccdata[52],4);
180 INFO("ICC: devicemodel='%s'\n", devicemodel);
181 /* -- */
182 // device attributes 56-63
183 /* -- */
184 // rendering intent field 64-67
185 /* -- */
186 /* TODO: check profile id 84-99, zero or md5sum over entire profile starting at byte 129 with length of profile_size */
187
188 return icc_is_valid;
189 }
190
191
192 /* checks an ICC header for ICC.1:2004-08, see chapter 6 of http://www.color.org/ICC_Minor_Revision_for_Web.pdf
193 * it also allos older 2.x.x versions, but there is no spec available anymore.
194 * But we have checked 2.2.0 with 2.4.0, there is no difference in ICC profile
195 * header */
196 static icc_returncode_t parse_icc_v240(unsigned long iccsize, const char* iccdata, unsigned long errsize, char * errmessage) {
197 assert(iccdata != NULL);
198 assert(errmessage != NULL);
199 assert(errsize > 0);
200 if (iccsize < 128) FAIL (icc_error_header_v240_v430, "Invalid ICC profile ICC.1:2001-04, see http://www.color.org/ICC_Minor_Revision_for_Web.pdf for details");
201 icc_returncode_t ret_header = parse_icc_common_header( iccsize, iccdata, errsize, errmessage, icc_profile_v240_v430);
202 if (icc_is_valid != ret_header) return ret_header;
203 else {
204 // PCS xyz illuminant 68-79
205 /* -- */
206 // Profile creator signature 80-83
207 /* -- */
208 // Reserved field bytes 84-127
209 };
210 return icc_is_valid;
211 }
212
213 /* checks an ICC header for ICC.1:2010, see chapter 7 of http://www.color.org/specification/ICC1v43_2010-12.pdf */
214 static icc_returncode_t parse_icc_v430(unsigned long iccsize, const char* iccdata, unsigned long errsize, char * errmessage) {
215 assert(iccdata != NULL);
216 assert(errmessage != NULL);
217 assert(errsize > 0);
218 if (iccsize < 128) FAIL (icc_error_header_1v43_2010, "Invalid ICC profile 1v43_2010, see http://www.color.org/specification/ICC1v43_2010-12.pdf for details");
219 icc_returncode_t ret_header = parse_icc_common_header( iccsize, iccdata, errsize, errmessage, icc_profile_1v43_2010);
220 if (icc_is_valid != ret_header) return ret_header;
221 else {
222 // PCS illuminant field 68-79
223 /* -- */
224 // Profile creator field 80-83
225 /* -- */
226 // Profile ID field 84-99
227 /* -- */
228 // Reserved field bytes 100-127
229
230 };
231 return icc_is_valid;
232 }
233
234 /* checks an ICC header for ICC.2 (ICC MAX), see chapter 7 of http://www.color.org/iccmax/ICC.2-2016-7.pdf */
235 static icc_returncode_t parse_icc_v500(unsigned long iccsize, const char* iccdata, unsigned long errsize, char * errmessage) {
236 assert(iccdata != NULL);
237 assert(errmessage != NULL);
238 assert(errsize > 0);
239 if (iccsize < 128) FAIL (icc_error_header_v500_2016, "Invalid ICC profile ICC.2:2016, see http://www.color.org/iccmax/ICC.2-2016-7.pdf for details");
240 int ret_header = parse_icc_common_header( iccsize, iccdata, errsize, errmessage, icc_profile_v500_2016);
241 if (icc_is_valid != ret_header) return ret_header;
242 else {
243 // PCS xyz illuminant 68-79
244 /* -- */
245 // Profile creator signature 80-83
246 /* -- */
247 // Profile Id Field 84-99
248 /* -- */
249 // Spectral PCS 100-103
250 /* -- */
251 // Spectral PCS wavelength range 104-109
252 /* -- */
253 // Bi-spectral PCS wavelength range 110-115
254 /* -- */
255 // MCS signature 116-119
256 /* -- */
257 // Profile/Device Subclass 120-123
258 /* -- */
259 // Reserved field bytes 124-127
260 };
261 return icc_is_valid;
262 }
263
264 /* returns 0 if valid, !0 if errorneous */
265 icc_returncode_t parse_icc(unsigned long iccsize, const char* iccdata, unsigned long errsize, char * errmessage) {
266 assert(iccdata != NULL);
267 assert(errmessage != NULL);
268 assert(errsize > 0);
269 if (iccsize < 10) FAIL (icc_error_header_generic, "Invalid ICC profile, size is less 10 bytes");
270 unsigned short a = (unsigned short) ((iccdata[8]) & 0x000f);
271 unsigned short b = (unsigned short) ((iccdata[9] & 0x00f0) >> 4);
272 unsigned short c = (unsigned short) (iccdata[9] & 0x000f);
273 char profileversion[6]=" ";
274 int snprintf_error = snprintf(profileversion, 6, "%hu.%hu.%hu", a, b, c);
275 icc_returncode_t ret = icc_should_not_occur;
276 if (snprintf_error == 5) { INFO("ICC profileversion='%s'\n", profileversion);
277 #ifdef WITH_STRONG_ICC
278 if (0 == strncmp(profileversion, "5.0.0", 5)) {ret = parse_icc_v500(iccsize, iccdata, errsize, errmessage);}
279 else if (0 == strncmp(profileversion, "4.3.0", 5)) {ret = parse_icc_v430(iccsize, iccdata, errsize, errmessage);}
280 #else
281 if (profileversion[0] == '5' && profileversion[1] == '.') {ret = parse_icc_v500(iccsize, iccdata, errsize, errmessage);}
282 else if (profileversion[0] == '4' && profileversion[1] == '.') {ret = parse_icc_v430(iccsize, iccdata, errsize, errmessage);}
283 #endif
284 #ifdef ALLOW_ICC1_2001_04
285 /* DISABLED, because 2.4.0 deprecated since 2019-12: */
286 else if (0==strncmp(profileversion, "2.4.0", 5)) ret=parse_icc_v240(iccsize,iccdata, errsize, errmessage);
287 #endif
288 else {
289 FAIL (icc_error_header_version_outdated, "ICC profileversion='%s'", profileversion);
290 }
291 #ifdef HAVE_IccProfLib
292 if (ret == icc_is_valid) {
293 return validate_icc_with_IccProfLib(iccsize, iccdata, errsize, errmessage);
294 }
295 #endif
296 return ret;
297 } else { FAIL (icc_error_header_version_undefined, "ICC profileversion='%s'", profileversion) }
298 return icc_should_not_occur;
299 }
300
301 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 smarttab expandtab :*/