Path: blob/master/src/java.desktop/share/native/liblcms/cmsmd5.c
41149 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324// This file is available under and governed by the GNU General Public25// License version 2 only, as published by the Free Software Foundation.26// However, the following notice accompanied the original version of this27// file:28//29//---------------------------------------------------------------------------------30//31// Little Color Management System32// Copyright (c) 1998-2020 Marti Maria Saguer33//34// Permission is hereby granted, free of charge, to any person obtaining35// a copy of this software and associated documentation files (the "Software"),36// to deal in the Software without restriction, including without limitation37// the rights to use, copy, modify, merge, publish, distribute, sublicense,38// and/or sell copies of the Software, and to permit persons to whom the Software39// is furnished to do so, subject to the following conditions:40//41// The above copyright notice and this permission notice shall be included in42// all copies or substantial portions of the Software.43//44// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,45// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO46// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND47// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE48// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION49// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION50// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.51//52//---------------------------------------------------------------------------------535455#include "lcms2_internal.h"5657#ifdef CMS_USE_BIG_ENDIAN5859static60void byteReverse(cmsUInt8Number * buf, cmsUInt32Number longs)61{62do {6364cmsUInt32Number t = _cmsAdjustEndianess32(*(cmsUInt32Number *) buf);65*(cmsUInt32Number *) buf = t;66buf += sizeof(cmsUInt32Number);6768} while (--longs);6970}7172#else73#define byteReverse(buf, len)74#endif757677typedef struct {7879cmsUInt32Number buf[4];80cmsUInt32Number bits[2];81cmsUInt8Number in[64];82cmsContext ContextID;8384} _cmsMD5;8586#define F1(x, y, z) (z ^ (x & (y ^ z)))87#define F2(x, y, z) F1(z, x, y)88#define F3(x, y, z) (x ^ y ^ z)89#define F4(x, y, z) (y ^ (x | ~z))9091#define STEP(f, w, x, y, z, data, s) \92( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )939495static96void cmsMD5_Transform(cmsUInt32Number buf[4], cmsUInt32Number in[16])97{98CMSREGISTER cmsUInt32Number a, b, c, d;99100a = buf[0];101b = buf[1];102c = buf[2];103d = buf[3];104105STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);106STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);107STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);108STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);109STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);110STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);111STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);112STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);113STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);114STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);115STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);116STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);117STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);118STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);119STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);120STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);121122STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);123STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);124STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);125STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);126STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);127STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);128STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);129STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);130STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);131STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);132STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);133STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);134STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);135STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);136STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);137STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);138139STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);140STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);141STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);142STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);143STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);144STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);145STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);146STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);147STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);148STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);149STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);150STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);151STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);152STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);153STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);154STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);155156STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);157STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);158STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);159STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);160STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);161STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);162STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);163STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);164STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);165STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);166STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);167STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);168STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);169STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);170STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);171STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);172173buf[0] += a;174buf[1] += b;175buf[2] += c;176buf[3] += d;177}178179180// Create a MD5 object181182cmsHANDLE CMSEXPORT cmsMD5alloc(cmsContext ContextID)183{184_cmsMD5* ctx = (_cmsMD5*) _cmsMallocZero(ContextID, sizeof(_cmsMD5));185if (ctx == NULL) return NULL;186187ctx ->ContextID = ContextID;188189ctx->buf[0] = 0x67452301;190ctx->buf[1] = 0xefcdab89;191ctx->buf[2] = 0x98badcfe;192ctx->buf[3] = 0x10325476;193194ctx->bits[0] = 0;195ctx->bits[1] = 0;196197return (cmsHANDLE) ctx;198}199200void CMSEXPORT cmsMD5add(cmsHANDLE Handle, const cmsUInt8Number* buf, cmsUInt32Number len)201{202_cmsMD5* ctx = (_cmsMD5*) Handle;203cmsUInt32Number t;204205t = ctx->bits[0];206if ((ctx->bits[0] = t + (len << 3)) < t)207ctx->bits[1]++;208209ctx->bits[1] += len >> 29;210211t = (t >> 3) & 0x3f;212213if (t) {214215cmsUInt8Number *p = (cmsUInt8Number *) ctx->in + t;216217t = 64 - t;218if (len < t) {219memmove(p, buf, len);220return;221}222223memmove(p, buf, t);224byteReverse(ctx->in, 16);225226cmsMD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);227buf += t;228len -= t;229}230231while (len >= 64) {232memmove(ctx->in, buf, 64);233byteReverse(ctx->in, 16);234cmsMD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);235buf += 64;236len -= 64;237}238239memmove(ctx->in, buf, len);240}241242// Destroy the object and return the checksum243void CMSEXPORT cmsMD5finish(cmsProfileID* ProfileID, cmsHANDLE Handle)244{245_cmsMD5* ctx = (_cmsMD5*) Handle;246cmsUInt32Number count;247cmsUInt8Number *p;248249count = (ctx->bits[0] >> 3) & 0x3F;250251p = ctx->in + count;252*p++ = 0x80;253254count = 64 - 1 - count;255256if (count < 8) {257258memset(p, 0, count);259byteReverse(ctx->in, 16);260cmsMD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);261262memset(ctx->in, 0, 56);263} else {264memset(p, 0, count - 8);265}266byteReverse(ctx->in, 14);267268((cmsUInt32Number *) ctx->in)[14] = ctx->bits[0];269((cmsUInt32Number *) ctx->in)[15] = ctx->bits[1];270271cmsMD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);272273byteReverse((cmsUInt8Number *) ctx->buf, 4);274memmove(ProfileID ->ID8, ctx->buf, 16);275276_cmsFree(ctx ->ContextID, ctx);277}278279280281// Assuming io points to an ICC profile, compute and store MD5 checksum282// In the header, rendering intentent, attributes and ID should be set to zero283// before computing MD5 checksum (per 6.1.13 in ICC spec)284285cmsBool CMSEXPORT cmsMD5computeID(cmsHPROFILE hProfile)286{287cmsContext ContextID;288cmsUInt32Number BytesNeeded;289cmsUInt8Number* Mem = NULL;290cmsHANDLE MD5 = NULL;291_cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;292_cmsICCPROFILE Keep;293294_cmsAssert(hProfile != NULL);295296ContextID = cmsGetProfileContextID(hProfile);297298// Save a copy of the profile header299memmove(&Keep, Icc, sizeof(_cmsICCPROFILE));300301// Set RI, attributes and ID302memset(&Icc ->attributes, 0, sizeof(Icc ->attributes));303Icc ->RenderingIntent = 0;304memset(&Icc ->ProfileID, 0, sizeof(Icc ->ProfileID));305306// Compute needed storage307if (!cmsSaveProfileToMem(hProfile, NULL, &BytesNeeded)) goto Error;308309// Allocate memory310Mem = (cmsUInt8Number*) _cmsMalloc(ContextID, BytesNeeded);311if (Mem == NULL) goto Error;312313// Save to temporary storage314if (!cmsSaveProfileToMem(hProfile, Mem, &BytesNeeded)) goto Error;315316// Create MD5 object317MD5 = cmsMD5alloc(ContextID);318if (MD5 == NULL) goto Error;319320// Add all bytes321cmsMD5add(MD5, Mem, BytesNeeded);322323// Temp storage is no longer needed324_cmsFree(ContextID, Mem);325326// Restore header327memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));328329// And store the ID330cmsMD5finish(&Icc ->ProfileID, MD5);331return TRUE;332333Error:334335// Free resources as something went wrong336// "MD5" cannot be other than NULL here, so no need to free it337if (Mem != NULL) _cmsFree(ContextID, Mem);338memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));339return FALSE;340}341342343344