2693N/A/*
2693N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2693N/A *
2693N/A * This code is free software; you can redistribute it and/or modify it
2693N/A * under the terms of the GNU General Public License version 2 only, as
2693N/A * published by the Free Software Foundation. Oracle designates this
2693N/A * particular file as subject to the "Classpath" exception as provided
2693N/A * by Oracle in the LICENSE file that accompanied this code.
2693N/A *
2693N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2693N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2693N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2693N/A * version 2 for more details (a copy is included in the LICENSE file that
2693N/A * accompanied this code).
2693N/A *
2693N/A * You should have received a copy of the GNU General Public License version
2693N/A * 2 along with this work; if not, write to the Free Software Foundation,
2693N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2693N/A *
2693N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2693N/A * or visit www.oracle.com if you need additional information or have any
2693N/A * questions.
2693N/A */
2693N/A
2693N/A// This file is available under and governed by the GNU General Public
2693N/A// License version 2 only, as published by the Free Software Foundation.
2693N/A// However, the following notice accompanied the original version of this
2693N/A// file:
2693N/A//
2693N/A//---------------------------------------------------------------------------------
2693N/A//
2693N/A// Little Color Management System
6271N/A// Copyright (c) 1998-2012 Marti Maria Saguer
2693N/A//
2693N/A// Permission is hereby granted, free of charge, to any person obtaining
2693N/A// a copy of this software and associated documentation files (the "Software"),
2693N/A// to deal in the Software without restriction, including without limitation
2693N/A// the rights to use, copy, modify, merge, publish, distribute, sublicense,
2693N/A// and/or sell copies of the Software, and to permit persons to whom the Software
2693N/A// is furnished to do so, subject to the following conditions:
2693N/A//
2693N/A// The above copyright notice and this permission notice shall be included in
2693N/A// all copies or substantial portions of the Software.
2693N/A//
2693N/A// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2693N/A// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
2693N/A// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2693N/A// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
2693N/A// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
2693N/A// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2693N/A// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2693N/A//
2693N/A//---------------------------------------------------------------------------------
2693N/A
2693N/A
2693N/A#include "lcms2_internal.h"
2693N/A
2693N/A#ifdef CMS_USE_BIG_ENDIAN
2693N/A
2693N/Astatic
2693N/Avoid byteReverse(cmsUInt8Number * buf, cmsUInt32Number longs)
2693N/A{
2693N/A do {
2693N/A
2693N/A cmsUInt32Number t = _cmsAdjustEndianess32(*(cmsUInt32Number *) buf);
2693N/A *(cmsUInt32Number *) buf = t;
2693N/A buf += sizeof(cmsUInt32Number);
2693N/A
2693N/A } while (--longs);
2693N/A
2693N/A}
2693N/A
2693N/A#else
2693N/A#define byteReverse(buf, len)
2693N/A#endif
2693N/A
2693N/A
2693N/Atypedef struct {
2693N/A
2693N/A cmsUInt32Number buf[4];
2693N/A cmsUInt32Number bits[2];
2693N/A cmsUInt8Number in[64];
2693N/A cmsContext ContextID;
2693N/A
2693N/A} _cmsMD5;
2693N/A
2693N/A#define F1(x, y, z) (z ^ (x & (y ^ z)))
2693N/A#define F2(x, y, z) F1(z, x, y)
2693N/A#define F3(x, y, z) (x ^ y ^ z)
2693N/A#define F4(x, y, z) (y ^ (x | ~z))
2693N/A
2693N/A#define STEP(f, w, x, y, z, data, s) \
2693N/A ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
2693N/A
2693N/A
2693N/Astatic
2693N/Avoid MD5_Transform(cmsUInt32Number buf[4], cmsUInt32Number in[16])
2693N/A
2693N/A{
2693N/A register cmsUInt32Number a, b, c, d;
2693N/A
2693N/A a = buf[0];
2693N/A b = buf[1];
2693N/A c = buf[2];
2693N/A d = buf[3];
2693N/A
2693N/A STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
2693N/A STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
2693N/A STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
2693N/A STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
2693N/A STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
2693N/A STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
2693N/A STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
2693N/A STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
2693N/A STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
2693N/A STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
2693N/A STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
2693N/A STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
2693N/A STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
2693N/A STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
2693N/A STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
2693N/A STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
2693N/A
2693N/A STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
2693N/A STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
2693N/A STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
2693N/A STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
2693N/A STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
2693N/A STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
2693N/A STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
2693N/A STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
2693N/A STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
2693N/A STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
2693N/A STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
2693N/A STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
2693N/A STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
2693N/A STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
2693N/A STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
2693N/A STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
2693N/A
2693N/A STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
2693N/A STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
2693N/A STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
2693N/A STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
2693N/A STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
2693N/A STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
2693N/A STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
2693N/A STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
2693N/A STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
2693N/A STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
2693N/A STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
2693N/A STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
2693N/A STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
2693N/A STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
2693N/A STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
2693N/A STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
2693N/A
2693N/A STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
2693N/A STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
2693N/A STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
2693N/A STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
2693N/A STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
2693N/A STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
2693N/A STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
2693N/A STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
2693N/A STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
2693N/A STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
2693N/A STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
2693N/A STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
2693N/A STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
2693N/A STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
2693N/A STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
2693N/A STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
2693N/A
2693N/A buf[0] += a;
2693N/A buf[1] += b;
2693N/A buf[2] += c;
2693N/A buf[3] += d;
2693N/A}
2693N/A
2693N/A
2693N/A// Create a MD5 object
2693N/Astatic
2693N/AcmsHANDLE MD5alloc(cmsContext ContextID)
2693N/A{
2693N/A _cmsMD5* ctx = (_cmsMD5*) _cmsMallocZero(ContextID, sizeof(_cmsMD5));
2693N/A if (ctx == NULL) return NULL;
2693N/A
2693N/A ctx ->ContextID = ContextID;
2693N/A
2693N/A ctx->buf[0] = 0x67452301;
2693N/A ctx->buf[1] = 0xefcdab89;
2693N/A ctx->buf[2] = 0x98badcfe;
2693N/A ctx->buf[3] = 0x10325476;
2693N/A
2693N/A ctx->bits[0] = 0;
2693N/A ctx->bits[1] = 0;
2693N/A
2693N/A return (cmsHANDLE) ctx;
2693N/A}
2693N/A
2693N/A
2693N/Astatic
2693N/Avoid MD5add(cmsHANDLE Handle, cmsUInt8Number* buf, cmsUInt32Number len)
2693N/A{
2693N/A _cmsMD5* ctx = (_cmsMD5*) Handle;
2693N/A cmsUInt32Number t;
2693N/A
2693N/A t = ctx->bits[0];
2693N/A if ((ctx->bits[0] = t + (len << 3)) < t)
2693N/A ctx->bits[1]++;
2693N/A
2693N/A ctx->bits[1] += len >> 29;
2693N/A
2693N/A t = (t >> 3) & 0x3f;
2693N/A
2693N/A if (t) {
2693N/A
2693N/A cmsUInt8Number *p = (cmsUInt8Number *) ctx->in + t;
2693N/A
2693N/A t = 64 - t;
2693N/A if (len < t) {
2693N/A memmove(p, buf, len);
2693N/A return;
2693N/A }
2693N/A
2693N/A memmove(p, buf, t);
2693N/A byteReverse(ctx->in, 16);
2693N/A
2693N/A MD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
2693N/A buf += t;
2693N/A len -= t;
2693N/A }
2693N/A
2693N/A while (len >= 64) {
2693N/A memmove(ctx->in, buf, 64);
2693N/A byteReverse(ctx->in, 16);
2693N/A MD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
2693N/A buf += 64;
2693N/A len -= 64;
2693N/A }
2693N/A
2693N/A memmove(ctx->in, buf, len);
2693N/A}
2693N/A
2693N/A// Destroy the object and return the checksum
2693N/Astatic
2693N/Avoid MD5finish(cmsProfileID* ProfileID, cmsHANDLE Handle)
2693N/A{
2693N/A _cmsMD5* ctx = (_cmsMD5*) Handle;
2693N/A cmsUInt32Number count;
2693N/A cmsUInt8Number *p;
2693N/A
2693N/A count = (ctx->bits[0] >> 3) & 0x3F;
2693N/A
2693N/A p = ctx->in + count;
2693N/A *p++ = 0x80;
2693N/A
2693N/A count = 64 - 1 - count;
2693N/A
2693N/A if (count < 8) {
2693N/A
2693N/A memset(p, 0, count);
2693N/A byteReverse(ctx->in, 16);
2693N/A MD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
2693N/A
2693N/A memset(ctx->in, 0, 56);
2693N/A } else {
2693N/A memset(p, 0, count - 8);
2693N/A }
2693N/A byteReverse(ctx->in, 14);
2693N/A
2693N/A ((cmsUInt32Number *) ctx->in)[14] = ctx->bits[0];
2693N/A ((cmsUInt32Number *) ctx->in)[15] = ctx->bits[1];
2693N/A
2693N/A MD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
2693N/A
2693N/A byteReverse((cmsUInt8Number *) ctx->buf, 4);
2693N/A memmove(ProfileID ->ID8, ctx->buf, 16);
2693N/A
2693N/A _cmsFree(ctx ->ContextID, ctx);
2693N/A}
2693N/A
2693N/A
2693N/A
2693N/A// Assuming io points to an ICC profile, compute and store MD5 checksum
2693N/A// In the header, rendering intentent, attributes and ID should be set to zero
2693N/A// before computing MD5 checksum (per 6.1.13 in ICC spec)
2693N/A
2693N/AcmsBool CMSEXPORT cmsMD5computeID(cmsHPROFILE hProfile)
2693N/A{
2693N/A cmsContext ContextID;
2693N/A cmsUInt32Number BytesNeeded;
2693N/A cmsUInt8Number* Mem = NULL;
2693N/A cmsHANDLE MD5 = NULL;
2693N/A _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
2693N/A _cmsICCPROFILE Keep;
2693N/A
2693N/A _cmsAssert(hProfile != NULL);
2693N/A
2693N/A ContextID = cmsGetProfileContextID(hProfile);
2693N/A
2693N/A // Save a copy of the profile header
2693N/A memmove(&Keep, Icc, sizeof(_cmsICCPROFILE));
2693N/A
2693N/A // Set RI, attributes and ID
2693N/A memset(&Icc ->attributes, 0, sizeof(Icc ->attributes));
2693N/A Icc ->RenderingIntent = 0;
2693N/A memset(&Icc ->ProfileID, 0, sizeof(Icc ->ProfileID));
2693N/A
2693N/A // Compute needed storage
2693N/A if (!cmsSaveProfileToMem(hProfile, NULL, &BytesNeeded)) goto Error;
2693N/A
2693N/A // Allocate memory
2693N/A Mem = (cmsUInt8Number*) _cmsMalloc(ContextID, BytesNeeded);
2693N/A if (Mem == NULL) goto Error;
2693N/A
2693N/A // Save to temporary storage
2693N/A if (!cmsSaveProfileToMem(hProfile, Mem, &BytesNeeded)) goto Error;
2693N/A
2693N/A // Create MD5 object
2693N/A MD5 = MD5alloc(ContextID);
2693N/A if (MD5 == NULL) goto Error;
2693N/A
2693N/A // Add all bytes
2693N/A MD5add(MD5, Mem, BytesNeeded);
2693N/A
2693N/A // Temp storage is no longer needed
2693N/A _cmsFree(ContextID, Mem);
2693N/A
2693N/A // Restore header
2693N/A memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
2693N/A
2693N/A // And store the ID
2693N/A MD5finish(&Icc ->ProfileID, MD5);
2693N/A return TRUE;
2693N/A
2693N/AError:
2693N/A
2693N/A // Free resources as something went wrong
2693N/A if (MD5 != NULL) _cmsFree(ContextID, MD5);
2693N/A if (Mem != NULL) _cmsFree(ContextID, Mem);
2693N/A memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
2693N/A return FALSE;
2693N/A}
2693N/A