< prev index next >

src/java.desktop/share/native/liblcms/cmsmd5.c

Print this page




  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 // This file is available under and governed by the GNU General Public
  26 // License version 2 only, as published by the Free Software Foundation.
  27 // However, the following notice accompanied the original version of this
  28 // file:
  29 //
  30 //---------------------------------------------------------------------------------
  31 //
  32 //  Little Color Management System
  33 //  Copyright (c) 1998-2017 Marti Maria Saguer
  34 //
  35 // Permission is hereby granted, free of charge, to any person obtaining
  36 // a copy of this software and associated documentation files (the "Software"),
  37 // to deal in the Software without restriction, including without limitation
  38 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  39 // and/or sell copies of the Software, and to permit persons to whom the Software
  40 // is furnished to do so, subject to the following conditions:
  41 //
  42 // The above copyright notice and this permission notice shall be included in
  43 // all copies or substantial portions of the Software.
  44 //
  45 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  46 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  47 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  48 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  49 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  50 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  51 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  52 //
  53 //---------------------------------------------------------------------------------


  77 
  78 typedef struct {
  79 
  80     cmsUInt32Number buf[4];
  81     cmsUInt32Number bits[2];
  82     cmsUInt8Number in[64];
  83     cmsContext ContextID;
  84 
  85 } _cmsMD5;
  86 
  87 #define F1(x, y, z) (z ^ (x & (y ^ z)))
  88 #define F2(x, y, z) F1(z, x, y)
  89 #define F3(x, y, z) (x ^ y ^ z)
  90 #define F4(x, y, z) (y ^ (x | ~z))
  91 
  92 #define STEP(f, w, x, y, z, data, s) \
  93     ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
  94 
  95 
  96 static
  97 void MD5_Transform(cmsUInt32Number buf[4], cmsUInt32Number in[16])
  98 
  99 {
 100     register cmsUInt32Number a, b, c, d;
 101 
 102     a = buf[0];
 103     b = buf[1];
 104     c = buf[2];
 105     d = buf[3];
 106 
 107     STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
 108     STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
 109     STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
 110     STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
 111     STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
 112     STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
 113     STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
 114     STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
 115     STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
 116     STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
 117     STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
 118     STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
 119     STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
 120     STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);


 163     STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
 164     STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
 165     STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
 166     STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
 167     STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
 168     STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
 169     STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
 170     STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
 171     STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
 172     STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
 173     STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
 174 
 175     buf[0] += a;
 176     buf[1] += b;
 177     buf[2] += c;
 178     buf[3] += d;
 179 }
 180 
 181 
 182 // Create a MD5 object
 183 static
 184 cmsHANDLE  MD5alloc(cmsContext ContextID)
 185 {
 186     _cmsMD5* ctx = (_cmsMD5*) _cmsMallocZero(ContextID, sizeof(_cmsMD5));
 187     if (ctx == NULL) return NULL;
 188 
 189     ctx ->ContextID = ContextID;
 190 
 191     ctx->buf[0] = 0x67452301;
 192     ctx->buf[1] = 0xefcdab89;
 193     ctx->buf[2] = 0x98badcfe;
 194     ctx->buf[3] = 0x10325476;
 195 
 196     ctx->bits[0] = 0;
 197     ctx->bits[1] = 0;
 198 
 199     return (cmsHANDLE) ctx;
 200 }
 201 
 202 
 203 static
 204 void MD5add(cmsHANDLE Handle, cmsUInt8Number* buf, cmsUInt32Number len)
 205 {
 206     _cmsMD5* ctx = (_cmsMD5*) Handle;
 207     cmsUInt32Number t;
 208 
 209     t = ctx->bits[0];
 210     if ((ctx->bits[0] = t + (len << 3)) < t)
 211         ctx->bits[1]++;
 212 
 213     ctx->bits[1] += len >> 29;
 214 
 215     t = (t >> 3) & 0x3f;
 216 
 217     if (t) {
 218 
 219         cmsUInt8Number *p = (cmsUInt8Number *) ctx->in + t;
 220 
 221         t = 64 - t;
 222         if (len < t) {
 223             memmove(p, buf, len);
 224             return;
 225         }
 226 
 227         memmove(p, buf, t);
 228         byteReverse(ctx->in, 16);
 229 
 230         MD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
 231         buf += t;
 232         len -= t;
 233     }
 234 
 235     while (len >= 64) {
 236         memmove(ctx->in, buf, 64);
 237         byteReverse(ctx->in, 16);
 238         MD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
 239         buf += 64;
 240         len -= 64;
 241     }
 242 
 243     memmove(ctx->in, buf, len);
 244 }
 245 
 246 // Destroy the object and return the checksum
 247 static
 248 void MD5finish(cmsProfileID* ProfileID,  cmsHANDLE Handle)
 249 {
 250     _cmsMD5* ctx = (_cmsMD5*) Handle;
 251     cmsUInt32Number count;
 252     cmsUInt8Number *p;
 253 
 254     count = (ctx->bits[0] >> 3) & 0x3F;
 255 
 256     p = ctx->in + count;
 257     *p++ = 0x80;
 258 
 259     count = 64 - 1 - count;
 260 
 261     if (count < 8) {
 262 
 263         memset(p, 0, count);
 264         byteReverse(ctx->in, 16);
 265         MD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
 266 
 267         memset(ctx->in, 0, 56);
 268     } else {
 269         memset(p, 0, count - 8);
 270     }
 271     byteReverse(ctx->in, 14);
 272 
 273     ((cmsUInt32Number *) ctx->in)[14] = ctx->bits[0];
 274     ((cmsUInt32Number *) ctx->in)[15] = ctx->bits[1];
 275 
 276     MD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
 277 
 278     byteReverse((cmsUInt8Number *) ctx->buf, 4);
 279     memmove(ProfileID ->ID8, ctx->buf, 16);
 280 
 281     _cmsFree(ctx ->ContextID, ctx);
 282 }
 283 
 284 
 285 
 286 // Assuming io points to an ICC profile, compute and store MD5 checksum
 287 // In the header, rendering intentent, attributes and ID should be set to zero
 288 // before computing MD5 checksum (per 6.1.13 in ICC spec)
 289 
 290 cmsBool CMSEXPORT cmsMD5computeID(cmsHPROFILE hProfile)
 291 {
 292     cmsContext   ContextID;
 293     cmsUInt32Number BytesNeeded;
 294     cmsUInt8Number* Mem = NULL;
 295     cmsHANDLE  MD5 = NULL;
 296     _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;


 302 
 303     // Save a copy of the profile header
 304     memmove(&Keep, Icc, sizeof(_cmsICCPROFILE));
 305 
 306     // Set RI, attributes and ID
 307     memset(&Icc ->attributes, 0, sizeof(Icc ->attributes));
 308     Icc ->RenderingIntent = 0;
 309     memset(&Icc ->ProfileID, 0, sizeof(Icc ->ProfileID));
 310 
 311     // Compute needed storage
 312     if (!cmsSaveProfileToMem(hProfile, NULL, &BytesNeeded)) goto Error;
 313 
 314     // Allocate memory
 315     Mem = (cmsUInt8Number*) _cmsMalloc(ContextID, BytesNeeded);
 316     if (Mem == NULL) goto Error;
 317 
 318     // Save to temporary storage
 319     if (!cmsSaveProfileToMem(hProfile, Mem, &BytesNeeded)) goto Error;
 320 
 321     // Create MD5 object
 322     MD5 = MD5alloc(ContextID);
 323     if (MD5 == NULL) goto Error;
 324 
 325     // Add all bytes
 326     MD5add(MD5, Mem, BytesNeeded);
 327 
 328     // Temp storage is no longer needed
 329     _cmsFree(ContextID, Mem);
 330 
 331     // Restore header
 332     memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
 333 
 334     // And store the ID
 335     MD5finish(&Icc ->ProfileID,  MD5);
 336     return TRUE;
 337 
 338 Error:
 339 
 340     // Free resources as something went wrong
 341     // "MD5" cannot be other than NULL here, so no need to free it
 342     if (Mem != NULL) _cmsFree(ContextID, Mem);
 343     memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
 344     return FALSE;
 345 }
 346 


  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 // This file is available under and governed by the GNU General Public
  26 // License version 2 only, as published by the Free Software Foundation.
  27 // However, the following notice accompanied the original version of this
  28 // file:
  29 //
  30 //---------------------------------------------------------------------------------
  31 //
  32 //  Little Color Management System
  33 //  Copyright (c) 1998-2020 Marti Maria Saguer
  34 //
  35 // Permission is hereby granted, free of charge, to any person obtaining
  36 // a copy of this software and associated documentation files (the "Software"),
  37 // to deal in the Software without restriction, including without limitation
  38 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  39 // and/or sell copies of the Software, and to permit persons to whom the Software
  40 // is furnished to do so, subject to the following conditions:
  41 //
  42 // The above copyright notice and this permission notice shall be included in
  43 // all copies or substantial portions of the Software.
  44 //
  45 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  46 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  47 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  48 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  49 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  50 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  51 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  52 //
  53 //---------------------------------------------------------------------------------


  77 
  78 typedef struct {
  79 
  80     cmsUInt32Number buf[4];
  81     cmsUInt32Number bits[2];
  82     cmsUInt8Number in[64];
  83     cmsContext ContextID;
  84 
  85 } _cmsMD5;
  86 
  87 #define F1(x, y, z) (z ^ (x & (y ^ z)))
  88 #define F2(x, y, z) F1(z, x, y)
  89 #define F3(x, y, z) (x ^ y ^ z)
  90 #define F4(x, y, z) (y ^ (x | ~z))
  91 
  92 #define STEP(f, w, x, y, z, data, s) \
  93     ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
  94 
  95 
  96 static
  97 void cmsMD5_Transform(cmsUInt32Number buf[4], cmsUInt32Number in[16])

  98 {
  99     CMSREGISTER cmsUInt32Number a, b, c, d;
 100 
 101     a = buf[0];
 102     b = buf[1];
 103     c = buf[2];
 104     d = buf[3];
 105 
 106     STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
 107     STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
 108     STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
 109     STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
 110     STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
 111     STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
 112     STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
 113     STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
 114     STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
 115     STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
 116     STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
 117     STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
 118     STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
 119     STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);


 162     STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
 163     STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
 164     STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
 165     STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
 166     STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
 167     STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
 168     STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
 169     STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
 170     STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
 171     STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
 172     STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
 173 
 174     buf[0] += a;
 175     buf[1] += b;
 176     buf[2] += c;
 177     buf[3] += d;
 178 }
 179 
 180 
 181 // Create a MD5 object
 182 
 183 cmsHANDLE CMSEXPORT cmsMD5alloc(cmsContext ContextID)
 184 {
 185     _cmsMD5* ctx = (_cmsMD5*) _cmsMallocZero(ContextID, sizeof(_cmsMD5));
 186     if (ctx == NULL) return NULL;
 187 
 188     ctx ->ContextID = ContextID;
 189 
 190     ctx->buf[0] = 0x67452301;
 191     ctx->buf[1] = 0xefcdab89;
 192     ctx->buf[2] = 0x98badcfe;
 193     ctx->buf[3] = 0x10325476;
 194 
 195     ctx->bits[0] = 0;
 196     ctx->bits[1] = 0;
 197 
 198     return (cmsHANDLE) ctx;
 199 }
 200 
 201 void CMSEXPORT cmsMD5add(cmsHANDLE Handle, const cmsUInt8Number* buf, cmsUInt32Number len)


 202 {
 203     _cmsMD5* ctx = (_cmsMD5*) Handle;
 204     cmsUInt32Number t;
 205 
 206     t = ctx->bits[0];
 207     if ((ctx->bits[0] = t + (len << 3)) < t)
 208         ctx->bits[1]++;
 209 
 210     ctx->bits[1] += len >> 29;
 211 
 212     t = (t >> 3) & 0x3f;
 213 
 214     if (t) {
 215 
 216         cmsUInt8Number *p = (cmsUInt8Number *) ctx->in + t;
 217 
 218         t = 64 - t;
 219         if (len < t) {
 220             memmove(p, buf, len);
 221             return;
 222         }
 223 
 224         memmove(p, buf, t);
 225         byteReverse(ctx->in, 16);
 226 
 227         cmsMD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
 228         buf += t;
 229         len -= t;
 230     }
 231 
 232     while (len >= 64) {
 233         memmove(ctx->in, buf, 64);
 234         byteReverse(ctx->in, 16);
 235         cmsMD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
 236         buf += 64;
 237         len -= 64;
 238     }
 239 
 240     memmove(ctx->in, buf, len);
 241 }
 242 
 243 // Destroy the object and return the checksum
 244 void CMSEXPORT cmsMD5finish(cmsProfileID* ProfileID,  cmsHANDLE Handle)

 245 {
 246     _cmsMD5* ctx = (_cmsMD5*) Handle;
 247     cmsUInt32Number count;
 248     cmsUInt8Number *p;
 249 
 250     count = (ctx->bits[0] >> 3) & 0x3F;
 251 
 252     p = ctx->in + count;
 253     *p++ = 0x80;
 254 
 255     count = 64 - 1 - count;
 256 
 257     if (count < 8) {
 258 
 259         memset(p, 0, count);
 260         byteReverse(ctx->in, 16);
 261         cmsMD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
 262 
 263         memset(ctx->in, 0, 56);
 264     } else {
 265         memset(p, 0, count - 8);
 266     }
 267     byteReverse(ctx->in, 14);
 268 
 269     ((cmsUInt32Number *) ctx->in)[14] = ctx->bits[0];
 270     ((cmsUInt32Number *) ctx->in)[15] = ctx->bits[1];
 271 
 272     cmsMD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
 273 
 274     byteReverse((cmsUInt8Number *) ctx->buf, 4);
 275     memmove(ProfileID ->ID8, ctx->buf, 16);
 276 
 277     _cmsFree(ctx ->ContextID, ctx);
 278 }
 279 
 280 
 281 
 282 // Assuming io points to an ICC profile, compute and store MD5 checksum
 283 // In the header, rendering intentent, attributes and ID should be set to zero
 284 // before computing MD5 checksum (per 6.1.13 in ICC spec)
 285 
 286 cmsBool CMSEXPORT cmsMD5computeID(cmsHPROFILE hProfile)
 287 {
 288     cmsContext   ContextID;
 289     cmsUInt32Number BytesNeeded;
 290     cmsUInt8Number* Mem = NULL;
 291     cmsHANDLE  MD5 = NULL;
 292     _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;


 298 
 299     // Save a copy of the profile header
 300     memmove(&Keep, Icc, sizeof(_cmsICCPROFILE));
 301 
 302     // Set RI, attributes and ID
 303     memset(&Icc ->attributes, 0, sizeof(Icc ->attributes));
 304     Icc ->RenderingIntent = 0;
 305     memset(&Icc ->ProfileID, 0, sizeof(Icc ->ProfileID));
 306 
 307     // Compute needed storage
 308     if (!cmsSaveProfileToMem(hProfile, NULL, &BytesNeeded)) goto Error;
 309 
 310     // Allocate memory
 311     Mem = (cmsUInt8Number*) _cmsMalloc(ContextID, BytesNeeded);
 312     if (Mem == NULL) goto Error;
 313 
 314     // Save to temporary storage
 315     if (!cmsSaveProfileToMem(hProfile, Mem, &BytesNeeded)) goto Error;
 316 
 317     // Create MD5 object
 318     MD5 = cmsMD5alloc(ContextID);
 319     if (MD5 == NULL) goto Error;
 320 
 321     // Add all bytes
 322     cmsMD5add(MD5, Mem, BytesNeeded);
 323 
 324     // Temp storage is no longer needed
 325     _cmsFree(ContextID, Mem);
 326 
 327     // Restore header
 328     memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
 329 
 330     // And store the ID
 331     cmsMD5finish(&Icc ->ProfileID,  MD5);
 332     return TRUE;
 333 
 334 Error:
 335 
 336     // Free resources as something went wrong
 337     // "MD5" cannot be other than NULL here, so no need to free it
 338     if (Mem != NULL) _cmsFree(ContextID, Mem);
 339     memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
 340     return FALSE;
 341 }
 342 
< prev index next >