comparison mupdf-source/thirdparty/lcms2/src/cmsmd5.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 //---------------------------------------------------------------------------------
2 //
3 // Little Color Management System
4 // Copyright (c) 1998-2023 Marti Maria Saguer
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the Software
11 // is furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
18 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24 //---------------------------------------------------------------------------------
25
26
27 #include "lcms2_internal.h"
28
29 #ifdef CMS_USE_BIG_ENDIAN
30
31 static
32 void byteReverse(cmsUInt8Number * buf, cmsUInt32Number longs)
33 {
34 do {
35
36 cmsUInt32Number t = _cmsAdjustEndianess32(*(cmsUInt32Number *) buf);
37 *(cmsUInt32Number *) buf = t;
38 buf += sizeof(cmsUInt32Number);
39
40 } while (--longs);
41
42 }
43
44 #else
45 #define byteReverse(buf, len)
46 #endif
47
48
49 typedef struct {
50
51 cmsUInt32Number buf[4];
52 cmsUInt32Number bits[2];
53 cmsUInt8Number in[64];
54
55 } _cmsMD5;
56
57 #define F1(x, y, z) (z ^ (x & (y ^ z)))
58 #define F2(x, y, z) F1(z, x, y)
59 #define F3(x, y, z) (x ^ y ^ z)
60 #define F4(x, y, z) (y ^ (x | ~z))
61
62 #define STEP(f, w, x, y, z, data, s) \
63 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
64
65
66 static
67 void cmsMD5_Transform(cmsUInt32Number buf[4], cmsUInt32Number in[16])
68 {
69 CMSREGISTER cmsUInt32Number a, b, c, d;
70
71 a = buf[0];
72 b = buf[1];
73 c = buf[2];
74 d = buf[3];
75
76 STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
77 STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
78 STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
79 STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
80 STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
81 STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
82 STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
83 STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
84 STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
85 STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
86 STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
87 STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
88 STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
89 STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
90 STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
91 STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
92
93 STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
94 STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
95 STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
96 STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
97 STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
98 STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
99 STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
100 STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
101 STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
102 STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
103 STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
104 STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
105 STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
106 STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
107 STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
108 STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
109
110 STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
111 STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
112 STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
113 STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
114 STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
115 STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
116 STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
117 STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
118 STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
119 STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
120 STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
121 STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
122 STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
123 STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
124 STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
125 STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
126
127 STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
128 STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
129 STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
130 STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
131 STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
132 STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
133 STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
134 STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
135 STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
136 STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
137 STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
138 STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
139 STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
140 STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
141 STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
142 STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
143
144 buf[0] += a;
145 buf[1] += b;
146 buf[2] += c;
147 buf[3] += d;
148 }
149
150
151 // Create a MD5 object
152
153 cmsHANDLE CMSEXPORT cmsMD5alloc(cmsContext ContextID)
154 {
155 _cmsMD5* ctx = (_cmsMD5*) _cmsMallocZero(ContextID, sizeof(_cmsMD5));
156 if (ctx == NULL) return NULL;
157
158 ctx->buf[0] = 0x67452301;
159 ctx->buf[1] = 0xefcdab89;
160 ctx->buf[2] = 0x98badcfe;
161 ctx->buf[3] = 0x10325476;
162
163 ctx->bits[0] = 0;
164 ctx->bits[1] = 0;
165
166 return (cmsHANDLE) ctx;
167 }
168
169 void CMSEXPORT cmsMD5add(cmsHANDLE Handle, const cmsUInt8Number* buf, cmsUInt32Number len)
170 {
171 _cmsMD5* ctx = (_cmsMD5*) Handle;
172 cmsUInt32Number t;
173
174 t = ctx->bits[0];
175 if ((ctx->bits[0] = t + (len << 3)) < t)
176 ctx->bits[1]++;
177
178 ctx->bits[1] += len >> 29;
179
180 t = (t >> 3) & 0x3f;
181
182 if (t) {
183
184 cmsUInt8Number *p = (cmsUInt8Number *) ctx->in + t;
185
186 t = 64 - t;
187 if (len < t) {
188 memmove(p, buf, len);
189 return;
190 }
191
192 memmove(p, buf, t);
193 byteReverse(ctx->in, 16);
194
195 cmsMD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
196 buf += t;
197 len -= t;
198 }
199
200 while (len >= 64) {
201 memmove(ctx->in, buf, 64);
202 byteReverse(ctx->in, 16);
203 cmsMD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
204 buf += 64;
205 len -= 64;
206 }
207
208 memmove(ctx->in, buf, len);
209 }
210
211 // Destroy the object and return the checksum
212 void CMSEXPORT cmsMD5finish(cmsContext ContextID, cmsProfileID* ProfileID, cmsHANDLE Handle)
213 {
214 _cmsMD5* ctx = (_cmsMD5*) Handle;
215 cmsUInt32Number count;
216 cmsUInt8Number *p;
217
218 count = (ctx->bits[0] >> 3) & 0x3F;
219
220 p = ctx->in + count;
221 *p++ = 0x80;
222
223 count = 64 - 1 - count;
224
225 if (count < 8) {
226
227 memset(p, 0, count);
228 byteReverse(ctx->in, 16);
229 cmsMD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
230
231 memset(ctx->in, 0, 56);
232 } else {
233 memset(p, 0, count - 8);
234 }
235 byteReverse(ctx->in, 14);
236
237 ((cmsUInt32Number *) ctx->in)[14] = ctx->bits[0];
238 ((cmsUInt32Number *) ctx->in)[15] = ctx->bits[1];
239
240 cmsMD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
241
242 byteReverse((cmsUInt8Number *) ctx->buf, 4);
243 memmove(ProfileID ->ID8, ctx->buf, 16);
244
245 _cmsFree(ContextID, ctx);
246 }
247
248
249
250 // Assuming io points to an ICC profile, compute and store MD5 checksum
251 // In the header, rendering intentent, attributes and ID should be set to zero
252 // before computing MD5 checksum (per 6.1.13 in ICC spec)
253
254 cmsBool CMSEXPORT cmsMD5computeID(cmsContext ContextID, cmsHPROFILE hProfile)
255 {
256 cmsUInt32Number BytesNeeded;
257 cmsUInt8Number* Mem = NULL;
258 cmsHANDLE MD5 = NULL;
259 _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
260 _cmsICCPROFILE Keep;
261
262 _cmsAssert(hProfile != NULL);
263
264 // Save a copy of the profile header
265 memmove(&Keep, Icc, sizeof(_cmsICCPROFILE));
266
267 // Set RI, attributes and ID
268 memset(&Icc ->attributes, 0, sizeof(Icc ->attributes));
269 Icc ->RenderingIntent = 0;
270 memset(&Icc ->ProfileID, 0, sizeof(Icc ->ProfileID));
271
272 // Compute needed storage
273 if (!cmsSaveProfileToMem(ContextID, hProfile, NULL, &BytesNeeded)) goto Error;
274
275 // Allocate memory
276 Mem = (cmsUInt8Number*) _cmsMalloc(ContextID, BytesNeeded);
277 if (Mem == NULL) goto Error;
278
279 // Save to temporary storage
280 if (!cmsSaveProfileToMem(ContextID, hProfile, Mem, &BytesNeeded)) goto Error;
281
282 // Create MD5 object
283 MD5 = cmsMD5alloc(ContextID);
284 if (MD5 == NULL) goto Error;
285
286 // Add all bytes
287 cmsMD5add(MD5, Mem, BytesNeeded);
288
289 // Temp storage is no longer needed
290 _cmsFree(ContextID, Mem);
291
292 // Restore header
293 memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
294
295 // And store the ID
296 cmsMD5finish(ContextID, &Icc ->ProfileID, MD5);
297 return TRUE;
298
299 Error:
300
301 // Free resources as something went wrong
302 // "MD5" cannot be other than NULL here, so no need to free it
303 if (Mem != NULL) _cmsFree(ContextID, Mem);
304 memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
305 return FALSE;
306 }