0N/A/*
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A// This file is available under and governed by the GNU General Public
0N/A// License version 2 only, as published by the Free Software Foundation.
0N/A// However, the following notice accompanied the original version of this
0N/A// file:
0N/A//
2693N/A//---------------------------------------------------------------------------------
0N/A//
2693N/A// Little Color Management System
6271N/A// Copyright (c) 1998-2012 Marti Maria Saguer
0N/A//
0N/A// Permission is hereby granted, free of charge, to any person obtaining
0N/A// a copy of this software and associated documentation files (the "Software"),
0N/A// to deal in the Software without restriction, including without limitation
0N/A// the rights to use, copy, modify, merge, publish, distribute, sublicense,
0N/A// and/or sell copies of the Software, and to permit persons to whom the Software
0N/A// is furnished to do so, subject to the following conditions:
0N/A//
0N/A// The above copyright notice and this permission notice shall be included in
0N/A// all copies or substantial portions of the Software.
0N/A//
0N/A// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0N/A// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
0N/A// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0N/A// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
0N/A// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0N/A// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0N/A// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2693N/A//
2693N/A//---------------------------------------------------------------------------------
0N/A
2693N/A#include "lcms2_internal.h"
2693N/A
2693N/A// I am so tired about incompatibilities on those functions that here are some replacements
2693N/A// that hopefully would be fully portable.
2693N/A
2693N/A// compare two strings ignoring case
2693N/Aint CMSEXPORT cmsstrcasecmp(const char* s1, const char* s2)
2693N/A{
2693N/A register const unsigned char *us1 = (const unsigned char *)s1,
2693N/A *us2 = (const unsigned char *)s2;
0N/A
2693N/A while (toupper(*us1) == toupper(*us2++))
2693N/A if (*us1++ == '\0')
2693N/A return (0);
2693N/A return (toupper(*us1) - toupper(*--us2));
2693N/A}
0N/A
2693N/A// long int because C99 specifies ftell in such way (7.19.9.2)
2693N/Along int CMSEXPORT cmsfilelength(FILE* f)
2693N/A{
6271N/A long int p , n;
6271N/A
6271N/A p = ftell(f); // register current file position
2693N/A
2693N/A if (fseek(f, 0, SEEK_END) != 0) {
2693N/A return -1;
2693N/A }
6271N/A
2693N/A n = ftell(f);
6271N/A fseek(f, p, SEEK_SET); // file position restored
2693N/A
2693N/A return n;
2693N/A}
0N/A
6271N/A
2693N/A// Memory handling ------------------------------------------------------------------
2693N/A//
2693N/A// This is the interface to low-level memory management routines. By default a simple
2693N/A// wrapping to malloc/free/realloc is provided, although there is a limit on the max
2693N/A// amount of memoy that can be reclaimed. This is mostly as a safety feature to
2693N/A// prevent bogus or malintentionated code to allocate huge blocks that otherwise lcms
2693N/A// would never need.
2693N/A
2693N/A#define MAX_MEMORY_FOR_ALLOC ((cmsUInt32Number)(1024U*1024U*512U))
2693N/A
2693N/A// User may override this behaviour by using a memory plug-in, which basically replaces
2693N/A// the default memory management functions. In this case, no check is performed and it
2693N/A// is up to the plug-in writter to keep in the safe side. There are only three functions
2693N/A// required to be implemented: malloc, realloc and free, although the user may want to
2693N/A// replace the optional mallocZero, calloc and dup as well.
2693N/A
2693N/AcmsBool _cmsRegisterMemHandlerPlugin(cmsPluginBase* Plugin);
2693N/A
2693N/A// *********************************************************************************
0N/A
2693N/A// This is the default memory allocation function. It does a very coarse
2693N/A// check of amout of memory, just to prevent exploits
2693N/Astatic
2693N/Avoid* _cmsMallocDefaultFn(cmsContext ContextID, cmsUInt32Number size)
2693N/A{
2693N/A if (size > MAX_MEMORY_FOR_ALLOC) return NULL; // Never allow over maximum
2693N/A
2693N/A return (void*) malloc(size);
1002N/A
2693N/A cmsUNUSED_PARAMETER(ContextID);
2693N/A}
2693N/A
2693N/A// Generic allocate & zero
2693N/Astatic
2693N/Avoid* _cmsMallocZeroDefaultFn(cmsContext ContextID, cmsUInt32Number size)
2693N/A{
2693N/A void *pt = _cmsMalloc(ContextID, size);
2693N/A if (pt == NULL) return NULL;
2693N/A
2693N/A memset(pt, 0, size);
2693N/A return pt;
2693N/A}
0N/A
0N/A
2693N/A// The default free function. The only check proformed is against NULL pointers
2693N/Astatic
2693N/Avoid _cmsFreeDefaultFn(cmsContext ContextID, void *Ptr)
2693N/A{
2693N/A // free(NULL) is defined a no-op by C99, therefore it is safe to
2693N/A // avoid the check, but it is here just in case...
0N/A
2693N/A if (Ptr) free(Ptr);
0N/A
2693N/A cmsUNUSED_PARAMETER(ContextID);
0N/A}
0N/A
2693N/A// The default realloc function. Again it check for exploits. If Ptr is NULL,
2693N/A// realloc behaves the same way as malloc and allocates a new block of size bytes.
2693N/Astatic
2693N/Avoid* _cmsReallocDefaultFn(cmsContext ContextID, void* Ptr, cmsUInt32Number size)
0N/A{
2693N/A
2693N/A if (size > MAX_MEMORY_FOR_ALLOC) return NULL; // Never realloc over 512Mb
2693N/A
2693N/A return realloc(Ptr, size);
2693N/A
2693N/A cmsUNUSED_PARAMETER(ContextID);
0N/A}
0N/A
0N/A
2693N/A// The default calloc function. Allocates an array of num elements, each one of size bytes
2693N/A// all memory is initialized to zero.
2693N/Astatic
2693N/Avoid* _cmsCallocDefaultFn(cmsContext ContextID, cmsUInt32Number num, cmsUInt32Number size)
2693N/A{
2693N/A cmsUInt32Number Total = num * size;
2693N/A
6271N/A // Preserve calloc behaviour
6271N/A if (Total == 0) return NULL;
6271N/A
6271N/A // Safe check for overflow.
6271N/A if (num >= UINT_MAX / size) return NULL;
6271N/A
2693N/A // Check for overflow
2693N/A if (Total < num || Total < size) {
2693N/A return NULL;
2693N/A }
2693N/A
2693N/A if (Total > MAX_MEMORY_FOR_ALLOC) return NULL; // Never alloc over 512Mb
2693N/A
2693N/A return _cmsMallocZero(ContextID, Total);
2693N/A}
2693N/A
2693N/A// Generic block duplication
2693N/Astatic
2693N/Avoid* _cmsDupDefaultFn(cmsContext ContextID, const void* Org, cmsUInt32Number size)
2693N/A{
2693N/A void* mem;
2693N/A
2693N/A if (size > MAX_MEMORY_FOR_ALLOC) return NULL; // Never dup over 512Mb
2693N/A
2693N/A mem = _cmsMalloc(ContextID, size);
2693N/A
2693N/A if (mem != NULL && Org != NULL)
2693N/A memmove(mem, Org, size);
2693N/A
2693N/A return mem;
2693N/A}
2693N/A
2693N/A// Pointers to malloc and _cmsFree functions in current environment
2693N/Astatic void * (* MallocPtr)(cmsContext ContextID, cmsUInt32Number size) = _cmsMallocDefaultFn;
2693N/Astatic void * (* MallocZeroPtr)(cmsContext ContextID, cmsUInt32Number size) = _cmsMallocZeroDefaultFn;
2693N/Astatic void (* FreePtr)(cmsContext ContextID, void *Ptr) = _cmsFreeDefaultFn;
2693N/Astatic void * (* ReallocPtr)(cmsContext ContextID, void *Ptr, cmsUInt32Number NewSize) = _cmsReallocDefaultFn;
2693N/Astatic void * (* CallocPtr)(cmsContext ContextID, cmsUInt32Number num, cmsUInt32Number size)= _cmsCallocDefaultFn;
2693N/Astatic void * (* DupPtr)(cmsContext ContextID, const void* Org, cmsUInt32Number size) = _cmsDupDefaultFn;
2693N/A
2693N/A// Plug-in replacement entry
2693N/AcmsBool _cmsRegisterMemHandlerPlugin(cmsPluginBase *Data)
2693N/A{
2693N/A cmsPluginMemHandler* Plugin = (cmsPluginMemHandler*) Data;
2693N/A
2693N/A // NULL forces to reset to defaults
2693N/A if (Data == NULL) {
2693N/A
2693N/A MallocPtr = _cmsMallocDefaultFn;
2693N/A MallocZeroPtr= _cmsMallocZeroDefaultFn;
2693N/A FreePtr = _cmsFreeDefaultFn;
2693N/A ReallocPtr = _cmsReallocDefaultFn;
2693N/A CallocPtr = _cmsCallocDefaultFn;
2693N/A DupPtr = _cmsDupDefaultFn;
2693N/A return TRUE;
2693N/A }
2693N/A
2693N/A // Check for required callbacks
2693N/A if (Plugin -> MallocPtr == NULL ||
2693N/A Plugin -> FreePtr == NULL ||
2693N/A Plugin -> ReallocPtr == NULL) return FALSE;
2693N/A
2693N/A // Set replacement functions
2693N/A MallocPtr = Plugin -> MallocPtr;
2693N/A FreePtr = Plugin -> FreePtr;
2693N/A ReallocPtr = Plugin -> ReallocPtr;
2693N/A
2693N/A if (Plugin ->MallocZeroPtr != NULL) MallocZeroPtr = Plugin ->MallocZeroPtr;
2693N/A if (Plugin ->CallocPtr != NULL) CallocPtr = Plugin -> CallocPtr;
2693N/A if (Plugin ->DupPtr != NULL) DupPtr = Plugin -> DupPtr;
2693N/A
2693N/A return TRUE;
2693N/A}
2693N/A
2693N/A// Generic allocate
2693N/Avoid* CMSEXPORT _cmsMalloc(cmsContext ContextID, cmsUInt32Number size)
2693N/A{
2693N/A return MallocPtr(ContextID, size);
2693N/A}
2693N/A
2693N/A// Generic allocate & zero
2693N/Avoid* CMSEXPORT _cmsMallocZero(cmsContext ContextID, cmsUInt32Number size)
2693N/A{
2693N/A return MallocZeroPtr(ContextID, size);
2693N/A}
2693N/A
2693N/A// Generic calloc
2693N/Avoid* CMSEXPORT _cmsCalloc(cmsContext ContextID, cmsUInt32Number num, cmsUInt32Number size)
2693N/A{
2693N/A return CallocPtr(ContextID, num, size);
2693N/A}
2693N/A
2693N/A// Generic reallocate
2693N/Avoid* CMSEXPORT _cmsRealloc(cmsContext ContextID, void* Ptr, cmsUInt32Number size)
2693N/A{
2693N/A return ReallocPtr(ContextID, Ptr, size);
2693N/A}
2693N/A
2693N/A// Generic free memory
2693N/Avoid CMSEXPORT _cmsFree(cmsContext ContextID, void* Ptr)
2693N/A{
2693N/A if (Ptr != NULL) FreePtr(ContextID, Ptr);
2693N/A}
2693N/A
2693N/A// Generic block duplication
2693N/Avoid* CMSEXPORT _cmsDupMem(cmsContext ContextID, const void* Org, cmsUInt32Number size)
2693N/A{
2693N/A return DupPtr(ContextID, Org, size);
2693N/A}
2693N/A
2693N/A// ********************************************************************************************
2693N/A
2693N/A// Sub allocation takes care of many pointers of small size. The memory allocated in
2693N/A// this way have be freed at once. Next function allocates a single chunk for linked list
2693N/A// I prefer this method over realloc due to the big inpact on xput realloc may have if
6271N/A// memory is being swapped to disk. This approach is safer (although that may not be true on all platforms)
2693N/Astatic
2693N/A_cmsSubAllocator_chunk* _cmsCreateSubAllocChunk(cmsContext ContextID, cmsUInt32Number Initial)
2693N/A{
2693N/A _cmsSubAllocator_chunk* chunk;
2693N/A
6271N/A // 20K by default
6271N/A if (Initial == 0)
6271N/A Initial = 20*1024;
6271N/A
2693N/A // Create the container
2693N/A chunk = (_cmsSubAllocator_chunk*) _cmsMallocZero(ContextID, sizeof(_cmsSubAllocator_chunk));
2693N/A if (chunk == NULL) return NULL;
2693N/A
2693N/A // Initialize values
2693N/A chunk ->Block = (cmsUInt8Number*) _cmsMalloc(ContextID, Initial);
2693N/A if (chunk ->Block == NULL) {
2693N/A
2693N/A // Something went wrong
2693N/A _cmsFree(ContextID, chunk);
2693N/A return NULL;
2693N/A }
2693N/A
6271N/A
2693N/A
2693N/A chunk ->BlockSize = Initial;
2693N/A chunk ->Used = 0;
2693N/A chunk ->next = NULL;
2693N/A
2693N/A return chunk;
2693N/A}
2693N/A
2693N/A// The suballocated is nothing but a pointer to the first element in the list. We also keep
2693N/A// the thread ID in this structure.
2693N/A_cmsSubAllocator* _cmsCreateSubAlloc(cmsContext ContextID, cmsUInt32Number Initial)
2693N/A{
2693N/A _cmsSubAllocator* sub;
2693N/A
2693N/A // Create the container
2693N/A sub = (_cmsSubAllocator*) _cmsMallocZero(ContextID, sizeof(_cmsSubAllocator));
2693N/A if (sub == NULL) return NULL;
2693N/A
2693N/A sub ->ContextID = ContextID;
2693N/A
2693N/A sub ->h = _cmsCreateSubAllocChunk(ContextID, Initial);
2693N/A if (sub ->h == NULL) {
2693N/A _cmsFree(ContextID, sub);
2693N/A return NULL;
2693N/A }
2693N/A
2693N/A return sub;
2693N/A}
0N/A
0N/A
2693N/A// Get rid of whole linked list
2693N/Avoid _cmsSubAllocDestroy(_cmsSubAllocator* sub)
0N/A{
2693N/A _cmsSubAllocator_chunk *chunk, *n;
2693N/A
2693N/A for (chunk = sub ->h; chunk != NULL; chunk = n) {
2693N/A
2693N/A n = chunk->next;
2693N/A if (chunk->Block != NULL) _cmsFree(sub ->ContextID, chunk->Block);
2693N/A _cmsFree(sub ->ContextID, chunk);
2693N/A }
0N/A
2693N/A // Free the header
2693N/A _cmsFree(sub ->ContextID, sub);
2693N/A}
2693N/A
0N/A
2693N/A// Get a pointer to small memory block.
2693N/Avoid* _cmsSubAlloc(_cmsSubAllocator* sub, cmsUInt32Number size)
2693N/A{
2693N/A cmsUInt32Number Free = sub -> h ->BlockSize - sub -> h -> Used;
2693N/A cmsUInt8Number* ptr;
0N/A
6271N/A size = _cmsALIGNMEM(size);
2693N/A
2693N/A // Check for memory. If there is no room, allocate a new chunk of double memory size.
2693N/A if (size > Free) {
0N/A
2693N/A _cmsSubAllocator_chunk* chunk;
2693N/A cmsUInt32Number newSize;
2693N/A
2693N/A newSize = sub -> h ->BlockSize * 2;
2693N/A if (newSize < size) newSize = size;
0N/A
2693N/A chunk = _cmsCreateSubAllocChunk(sub -> ContextID, newSize);
2693N/A if (chunk == NULL) return NULL;
2693N/A
2693N/A // Link list
2693N/A chunk ->next = sub ->h;
2693N/A sub ->h = chunk;
2693N/A
2693N/A }
0N/A
2693N/A ptr = sub -> h ->Block + sub -> h ->Used;
2693N/A sub -> h -> Used += size;
2693N/A
2693N/A return (void*) ptr;
2693N/A}
2693N/A
2693N/A// Error logging ******************************************************************
0N/A
2693N/A// There is no error handling at all. When a funtion fails, it returns proper value.
2693N/A// For example, all create functions does return NULL on failure. Other return FALSE
2693N/A// It may be interesting, for the developer, to know why the function is failing.
2693N/A// for that reason, lcms2 does offer a logging function. This function does recive
2693N/A// a ENGLISH string with some clues on what is going wrong. You can show this
2693N/A// info to the end user, or just create some sort of log.
2693N/A// The logging function should NOT terminate the program, as this obviously can leave
2693N/A// resources. It is the programmer's responsability to check each function return code
2693N/A// to make sure it didn't fail.
0N/A
2693N/A// Error messages are limited to MAX_ERROR_MESSAGE_LEN
2693N/A
2693N/A#define MAX_ERROR_MESSAGE_LEN 1024
2693N/A
2693N/A// ---------------------------------------------------------------------------------------------------------
2693N/A
2693N/A// This is our default log error
2693N/Astatic void DefaultLogErrorHandlerFunction(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *Text);
2693N/A
2693N/A// The current handler in actual environment
2693N/Astatic cmsLogErrorHandlerFunction LogErrorHandler = DefaultLogErrorHandlerFunction;
0N/A
2693N/A// The default error logger does nothing.
2693N/Astatic
2693N/Avoid DefaultLogErrorHandlerFunction(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *Text)
2693N/A{
2693N/A // fprintf(stderr, "[lcms]: %s\n", Text);
2693N/A // fflush(stderr);
2693N/A
2693N/A cmsUNUSED_PARAMETER(ContextID);
2693N/A cmsUNUSED_PARAMETER(ErrorCode);
2693N/A cmsUNUSED_PARAMETER(Text);
2693N/A}
0N/A
2693N/A// Change log error
2693N/Avoid CMSEXPORT cmsSetLogErrorHandler(cmsLogErrorHandlerFunction Fn)
2693N/A{
2693N/A if (Fn == NULL)
2693N/A LogErrorHandler = DefaultLogErrorHandlerFunction;
2693N/A else
2693N/A LogErrorHandler = Fn;
2693N/A}
0N/A
2693N/A// Log an error
2693N/A// ErrorText is a text holding an english description of error.
2693N/Avoid CMSEXPORT cmsSignalError(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *ErrorText, ...)
2693N/A{
2693N/A va_list args;
2693N/A char Buffer[MAX_ERROR_MESSAGE_LEN];
2693N/A
2693N/A va_start(args, ErrorText);
2693N/A vsnprintf(Buffer, MAX_ERROR_MESSAGE_LEN-1, ErrorText, args);
2693N/A va_end(args);
0N/A
2693N/A // Call handler
2693N/A LogErrorHandler(ContextID, ErrorCode, Buffer);
2693N/A}
2693N/A
2693N/A// Utility function to print signatures
2693N/Avoid _cmsTagSignature2String(char String[5], cmsTagSignature sig)
2693N/A{
2693N/A cmsUInt32Number be;
0N/A
2693N/A // Convert to big endian
2693N/A be = _cmsAdjustEndianess32((cmsUInt32Number) sig);
2693N/A
2693N/A // Move chars
2693N/A memmove(String, &be, 4);
2693N/A
2693N/A // Make sure of terminator
2693N/A String[4] = 0;
0N/A}
2693N/A