1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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-2015 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 //---------------------------------------------------------------------------------
  54 
  55 #include "lcms2_internal.h"
  56 
  57 
  58 // This function is here to help applications to prevent mixing lcms versions on header and shared objects.
  59 int CMSEXPORT cmsGetEncodedCMMversion(void)
  60 {
  61        return LCMS_VERSION;
  62 }
  63 
  64 // I am so tired about incompatibilities on those functions that here are some replacements
  65 // that hopefully would be fully portable.
  66 
  67 // compare two strings ignoring case
  68 int CMSEXPORT cmsstrcasecmp(const char* s1, const char* s2)
  69 {
  70     register const unsigned char *us1 = (const unsigned char *)s1,
  71                                  *us2 = (const unsigned char *)s2;
  72 
  73     while (toupper(*us1) == toupper(*us2++))
  74         if (*us1++ == '\0')
  75             return 0;
  76 
  77     return (toupper(*us1) - toupper(*--us2));
  78 }
  79 
  80 // long int because C99 specifies ftell in such way (7.19.9.2)
  81 long int CMSEXPORT cmsfilelength(FILE* f)
  82 {
  83     long int p , n;
  84 
  85     p = ftell(f); // register current file position
  86 
  87     if (fseek(f, 0, SEEK_END) != 0) {
  88         return -1;
  89     }
  90 
  91     n = ftell(f);
  92     fseek(f, p, SEEK_SET); // file position restored
  93 
  94     return n;
  95 }
  96 
  97 
  98 // Memory handling ------------------------------------------------------------------
  99 //
 100 // This is the interface to low-level memory management routines. By default a simple
 101 // wrapping to malloc/free/realloc is provided, although there is a limit on the max
 102 // amount of memoy that can be reclaimed. This is mostly as a safety feature to prevent
 103 // bogus or evil code to allocate huge blocks that otherwise lcms would never need.
 104 
 105 #define MAX_MEMORY_FOR_ALLOC  ((cmsUInt32Number)(1024U*1024U*512U))
 106 
 107 // User may override this behaviour by using a memory plug-in, which basically replaces
 108 // the default memory management functions. In this case, no check is performed and it
 109 // is up to the plug-in writter to keep in the safe side. There are only three functions
 110 // required to be implemented: malloc, realloc and free, although the user may want to
 111 // replace the optional mallocZero, calloc and dup as well.
 112 
 113 cmsBool   _cmsRegisterMemHandlerPlugin(cmsContext ContextID, cmsPluginBase* Plugin);
 114 
 115 // *********************************************************************************
 116 
 117 // This is the default memory allocation function. It does a very coarse
 118 // check of amout of memory, just to prevent exploits
 119 static
 120 void* _cmsMallocDefaultFn(cmsContext ContextID, cmsUInt32Number size)
 121 {
 122     if (size > MAX_MEMORY_FOR_ALLOC) return NULL;  // Never allow over maximum
 123 
 124     return (void*) malloc(size);
 125 
 126     cmsUNUSED_PARAMETER(ContextID);
 127 }
 128 
 129 // Generic allocate & zero
 130 static
 131 void* _cmsMallocZeroDefaultFn(cmsContext ContextID, cmsUInt32Number size)
 132 {
 133     void *pt = _cmsMalloc(ContextID, size);
 134     if (pt == NULL) return NULL;
 135 
 136     memset(pt, 0, size);
 137     return pt;
 138 }
 139 
 140 
 141 // The default free function. The only check proformed is against NULL pointers
 142 static
 143 void _cmsFreeDefaultFn(cmsContext ContextID, void *Ptr)
 144 {
 145     // free(NULL) is defined a no-op by C99, therefore it is safe to
 146     // avoid the check, but it is here just in case...
 147 
 148     if (Ptr) free(Ptr);
 149 
 150     cmsUNUSED_PARAMETER(ContextID);
 151 }
 152 
 153 // The default realloc function. Again it checks for exploits. If Ptr is NULL,
 154 // realloc behaves the same way as malloc and allocates a new block of size bytes.
 155 static
 156 void* _cmsReallocDefaultFn(cmsContext ContextID, void* Ptr, cmsUInt32Number size)
 157 {
 158 
 159     if (size > MAX_MEMORY_FOR_ALLOC) return NULL;  // Never realloc over 512Mb
 160 
 161     return realloc(Ptr, size);
 162 
 163     cmsUNUSED_PARAMETER(ContextID);
 164 }
 165 
 166 
 167 // The default calloc function. Allocates an array of num elements, each one of size bytes
 168 // all memory is initialized to zero.
 169 static
 170 void* _cmsCallocDefaultFn(cmsContext ContextID, cmsUInt32Number num, cmsUInt32Number size)
 171 {
 172     cmsUInt32Number Total = num * size;
 173 
 174     // Preserve calloc behaviour
 175     if (Total == 0) return NULL;
 176 
 177     // Safe check for overflow.
 178     if (num >= UINT_MAX / size) return NULL;
 179 
 180     // Check for overflow
 181     if (Total < num || Total < size) {
 182         return NULL;
 183     }
 184 
 185     if (Total > MAX_MEMORY_FOR_ALLOC) return NULL;  // Never alloc over 512Mb
 186 
 187     return _cmsMallocZero(ContextID, Total);
 188 }
 189 
 190 // Generic block duplication
 191 static
 192 void* _cmsDupDefaultFn(cmsContext ContextID, const void* Org, cmsUInt32Number size)
 193 {
 194     void* mem;
 195 
 196     if (size > MAX_MEMORY_FOR_ALLOC) return NULL;  // Never dup over 512Mb
 197 
 198     mem = _cmsMalloc(ContextID, size);
 199 
 200     if (mem != NULL && Org != NULL)
 201         memmove(mem, Org, size);
 202 
 203     return mem;
 204 }
 205 
 206 
 207 // Pointers to memory manager functions in Context0
 208 _cmsMemPluginChunkType _cmsMemPluginChunk = { _cmsMallocDefaultFn, _cmsMallocZeroDefaultFn, _cmsFreeDefaultFn,
 209                                               _cmsReallocDefaultFn, _cmsCallocDefaultFn,    _cmsDupDefaultFn
 210                                             };
 211 
 212 
 213 // Reset and duplicate memory manager
 214 void _cmsAllocMemPluginChunk(struct _cmsContext_struct* ctx, const struct _cmsContext_struct* src)
 215 {
 216     _cmsAssert(ctx != NULL);
 217 
 218     if (src != NULL) {
 219 
 220         // Duplicate
 221         ctx ->chunks[MemPlugin] = _cmsSubAllocDup(ctx ->MemPool, src ->chunks[MemPlugin], sizeof(_cmsMemPluginChunkType));
 222     }
 223     else {
 224 
 225         // To reset it, we use the default allocators, which cannot be overriden
 226         ctx ->chunks[MemPlugin] = &ctx ->DefaultMemoryManager;
 227     }
 228 }
 229 
 230 // Auxiliar to fill memory management functions from plugin (or context 0 defaults)
 231 void _cmsInstallAllocFunctions(cmsPluginMemHandler* Plugin, _cmsMemPluginChunkType* ptr)
 232 {
 233     if (Plugin == NULL) {
 234 
 235         memcpy(ptr, &_cmsMemPluginChunk, sizeof(_cmsMemPluginChunk));
 236     }
 237     else {
 238 
 239         ptr ->MallocPtr  = Plugin -> MallocPtr;
 240         ptr ->FreePtr    = Plugin -> FreePtr;
 241         ptr ->ReallocPtr = Plugin -> ReallocPtr;
 242 
 243         // Make sure we revert to defaults
 244         ptr ->MallocZeroPtr= _cmsMallocZeroDefaultFn;
 245         ptr ->CallocPtr    = _cmsCallocDefaultFn;
 246         ptr ->DupPtr       = _cmsDupDefaultFn;
 247 
 248         if (Plugin ->MallocZeroPtr != NULL) ptr ->MallocZeroPtr = Plugin -> MallocZeroPtr;
 249         if (Plugin ->CallocPtr != NULL)     ptr ->CallocPtr     = Plugin -> CallocPtr;
 250         if (Plugin ->DupPtr != NULL)        ptr ->DupPtr        = Plugin -> DupPtr;
 251 
 252     }
 253 }
 254 
 255 
 256 // Plug-in replacement entry
 257 cmsBool  _cmsRegisterMemHandlerPlugin(cmsContext ContextID, cmsPluginBase *Data)
 258 {
 259     cmsPluginMemHandler* Plugin = (cmsPluginMemHandler*) Data;
 260     _cmsMemPluginChunkType* ptr;
 261 
 262     // NULL forces to reset to defaults. In this special case, the defaults are stored in the context structure.
 263     // Remaining plug-ins does NOT have any copy in the context structure, but this is somehow special as the
 264     // context internal data should be malloce'd by using those functions.
 265     if (Data == NULL) {
 266 
 267        struct _cmsContext_struct* ctx = ( struct _cmsContext_struct*) ContextID;
 268 
 269        // Return to the default allocators
 270         if (ContextID != NULL) {
 271             ctx->chunks[MemPlugin] = (void*) &ctx->DefaultMemoryManager;
 272         }
 273         return TRUE;
 274     }
 275 
 276     // Check for required callbacks
 277     if (Plugin -> MallocPtr == NULL ||
 278         Plugin -> FreePtr == NULL ||
 279         Plugin -> ReallocPtr == NULL) return FALSE;
 280 
 281     // Set replacement functions
 282     ptr = (_cmsMemPluginChunkType*) _cmsContextGetClientChunk(ContextID, MemPlugin);
 283     if (ptr == NULL)
 284         return FALSE;
 285 
 286     _cmsInstallAllocFunctions(Plugin, ptr);
 287     return TRUE;
 288 }
 289 
 290 // Generic allocate
 291 void* CMSEXPORT _cmsMalloc(cmsContext ContextID, cmsUInt32Number size)
 292 {
 293     _cmsMemPluginChunkType* ptr = (_cmsMemPluginChunkType*) _cmsContextGetClientChunk(ContextID, MemPlugin);
 294     return ptr ->MallocPtr(ContextID, size);
 295 }
 296 
 297 // Generic allocate & zero
 298 void* CMSEXPORT _cmsMallocZero(cmsContext ContextID, cmsUInt32Number size)
 299 {
 300     _cmsMemPluginChunkType* ptr = (_cmsMemPluginChunkType*) _cmsContextGetClientChunk(ContextID, MemPlugin);
 301     return ptr->MallocZeroPtr(ContextID, size);
 302 }
 303 
 304 // Generic calloc
 305 void* CMSEXPORT _cmsCalloc(cmsContext ContextID, cmsUInt32Number num, cmsUInt32Number size)
 306 {
 307     _cmsMemPluginChunkType* ptr = (_cmsMemPluginChunkType*) _cmsContextGetClientChunk(ContextID, MemPlugin);
 308     return ptr->CallocPtr(ContextID, num, size);
 309 }
 310 
 311 // Generic reallocate
 312 void* CMSEXPORT _cmsRealloc(cmsContext ContextID, void* Ptr, cmsUInt32Number size)
 313 {
 314     _cmsMemPluginChunkType* ptr = (_cmsMemPluginChunkType*) _cmsContextGetClientChunk(ContextID, MemPlugin);
 315     return ptr->ReallocPtr(ContextID, Ptr, size);
 316 }
 317 
 318 // Generic free memory
 319 void CMSEXPORT _cmsFree(cmsContext ContextID, void* Ptr)
 320 {
 321     if (Ptr != NULL) {
 322         _cmsMemPluginChunkType* ptr = (_cmsMemPluginChunkType*) _cmsContextGetClientChunk(ContextID, MemPlugin);
 323         ptr ->FreePtr(ContextID, Ptr);
 324     }
 325 }
 326 
 327 // Generic block duplication
 328 void* CMSEXPORT _cmsDupMem(cmsContext ContextID, const void* Org, cmsUInt32Number size)
 329 {
 330     _cmsMemPluginChunkType* ptr = (_cmsMemPluginChunkType*) _cmsContextGetClientChunk(ContextID, MemPlugin);
 331     return ptr ->DupPtr(ContextID, Org, size);
 332 }
 333 
 334 // ********************************************************************************************
 335 
 336 // Sub allocation takes care of many pointers of small size. The memory allocated in
 337 // this way have be freed at once. Next function allocates a single chunk for linked list
 338 // I prefer this method over realloc due to the big inpact on xput realloc may have if
 339 // memory is being swapped to disk. This approach is safer (although that may not be true on all platforms)
 340 static
 341 _cmsSubAllocator_chunk* _cmsCreateSubAllocChunk(cmsContext ContextID, cmsUInt32Number Initial)
 342 {
 343     _cmsSubAllocator_chunk* chunk;
 344 
 345     // 20K by default
 346     if (Initial == 0)
 347         Initial = 20*1024;
 348 
 349     // Create the container
 350     chunk = (_cmsSubAllocator_chunk*) _cmsMallocZero(ContextID, sizeof(_cmsSubAllocator_chunk));
 351     if (chunk == NULL) return NULL;
 352 
 353     // Initialize values
 354     chunk ->Block     = (cmsUInt8Number*) _cmsMalloc(ContextID, Initial);
 355     if (chunk ->Block == NULL) {
 356 
 357         // Something went wrong
 358         _cmsFree(ContextID, chunk);
 359         return NULL;
 360     }
 361 
 362     chunk ->BlockSize = Initial;
 363     chunk ->Used      = 0;
 364     chunk ->next      = NULL;
 365 
 366     return chunk;
 367 }
 368 
 369 // The suballocated is nothing but a pointer to the first element in the list. We also keep
 370 // the thread ID in this structure.
 371 _cmsSubAllocator* _cmsCreateSubAlloc(cmsContext ContextID, cmsUInt32Number Initial)
 372 {
 373     _cmsSubAllocator* sub;
 374 
 375     // Create the container
 376     sub = (_cmsSubAllocator*) _cmsMallocZero(ContextID, sizeof(_cmsSubAllocator));
 377     if (sub == NULL) return NULL;
 378 
 379     sub ->ContextID = ContextID;
 380 
 381     sub ->h = _cmsCreateSubAllocChunk(ContextID, Initial);
 382     if (sub ->h == NULL) {
 383         _cmsFree(ContextID, sub);
 384         return NULL;
 385     }
 386 
 387     return sub;
 388 }
 389 
 390 
 391 // Get rid of whole linked list
 392 void _cmsSubAllocDestroy(_cmsSubAllocator* sub)
 393 {
 394     _cmsSubAllocator_chunk *chunk, *n;
 395 
 396     for (chunk = sub ->h; chunk != NULL; chunk = n) {
 397 
 398         n = chunk->next;
 399         if (chunk->Block != NULL) _cmsFree(sub ->ContextID, chunk->Block);
 400         _cmsFree(sub ->ContextID, chunk);
 401     }
 402 
 403     // Free the header
 404     _cmsFree(sub ->ContextID, sub);
 405 }
 406 
 407 
 408 // Get a pointer to small memory block.
 409 void*  _cmsSubAlloc(_cmsSubAllocator* sub, cmsUInt32Number size)
 410 {
 411     cmsUInt32Number Free = sub -> h ->BlockSize - sub -> h -> Used;
 412     cmsUInt8Number* ptr;
 413 
 414     size = _cmsALIGNMEM(size);
 415 
 416     // Check for memory. If there is no room, allocate a new chunk of double memory size.
 417     if (size > Free) {
 418 
 419         _cmsSubAllocator_chunk* chunk;
 420         cmsUInt32Number newSize;
 421 
 422         newSize = sub -> h ->BlockSize * 2;
 423         if (newSize < size) newSize = size;
 424 
 425         chunk = _cmsCreateSubAllocChunk(sub -> ContextID, newSize);
 426         if (chunk == NULL) return NULL;
 427 
 428         // Link list
 429         chunk ->next = sub ->h;
 430         sub ->h    = chunk;
 431 
 432     }
 433 
 434     ptr =  sub -> h ->Block + sub -> h ->Used;
 435     sub -> h -> Used += size;
 436 
 437     return (void*) ptr;
 438 }
 439 
 440 // Duplicate in pool
 441 void* _cmsSubAllocDup(_cmsSubAllocator* s, const void *ptr, cmsUInt32Number size)
 442 {
 443     void *NewPtr;
 444 
 445     // Dup of null pointer is also NULL
 446     if (ptr == NULL)
 447         return NULL;
 448 
 449     NewPtr = _cmsSubAlloc(s, size);
 450 
 451     if (ptr != NULL && NewPtr != NULL) {
 452         memcpy(NewPtr, ptr, size);
 453     }
 454 
 455     return NewPtr;
 456 }
 457 
 458 
 459 
 460 // Error logging ******************************************************************
 461 
 462 // There is no error handling at all. When a funtion fails, it returns proper value.
 463 // For example, all create functions does return NULL on failure. Other return FALSE
 464 // It may be interesting, for the developer, to know why the function is failing.
 465 // for that reason, lcms2 does offer a logging function. This function does recive
 466 // a ENGLISH string with some clues on what is going wrong. You can show this
 467 // info to the end user, or just create some sort of log.
 468 // The logging function should NOT terminate the program, as this obviously can leave
 469 // resources. It is the programmer's responsability to check each function return code
 470 // to make sure it didn't fail.
 471 
 472 // Error messages are limited to MAX_ERROR_MESSAGE_LEN
 473 
 474 #define MAX_ERROR_MESSAGE_LEN   1024
 475 
 476 // ---------------------------------------------------------------------------------------------------------
 477 
 478 // This is our default log error
 479 static void DefaultLogErrorHandlerFunction(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *Text);
 480 
 481 // Context0 storage, which is global
 482 _cmsLogErrorChunkType _cmsLogErrorChunk = { DefaultLogErrorHandlerFunction };
 483 
 484 // Allocates and inits error logger container for a given context. If src is NULL, only initializes the value
 485 // to the default. Otherwise, it duplicates the value. The interface is standard across all context clients
 486 void _cmsAllocLogErrorChunk(struct _cmsContext_struct* ctx,
 487                             const struct _cmsContext_struct* src)
 488 {
 489     static _cmsLogErrorChunkType LogErrorChunk = { DefaultLogErrorHandlerFunction };
 490     void* from;
 491 
 492      if (src != NULL) {
 493         from = src ->chunks[Logger];
 494     }
 495     else {
 496        from = &LogErrorChunk;
 497     }
 498 
 499     ctx ->chunks[Logger] = _cmsSubAllocDup(ctx ->MemPool, from, sizeof(_cmsLogErrorChunkType));
 500 }
 501 
 502 // The default error logger does nothing.
 503 static
 504 void DefaultLogErrorHandlerFunction(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *Text)
 505 {
 506     // fprintf(stderr, "[lcms]: %s\n", Text);
 507     // fflush(stderr);
 508 
 509      cmsUNUSED_PARAMETER(ContextID);
 510      cmsUNUSED_PARAMETER(ErrorCode);
 511      cmsUNUSED_PARAMETER(Text);
 512 }
 513 
 514 // Change log error, context based
 515 void CMSEXPORT cmsSetLogErrorHandlerTHR(cmsContext ContextID, cmsLogErrorHandlerFunction Fn)
 516 {
 517     _cmsLogErrorChunkType* lhg = (_cmsLogErrorChunkType*) _cmsContextGetClientChunk(ContextID, Logger);
 518 
 519     if (lhg != NULL) {
 520 
 521         if (Fn == NULL)
 522             lhg -> LogErrorHandler = DefaultLogErrorHandlerFunction;
 523         else
 524             lhg -> LogErrorHandler = Fn;
 525     }
 526 }
 527 
 528 // Change log error, legacy
 529 void CMSEXPORT cmsSetLogErrorHandler(cmsLogErrorHandlerFunction Fn)
 530 {
 531     cmsSetLogErrorHandlerTHR(NULL, Fn);
 532 }
 533 
 534 // Log an error
 535 // ErrorText is a text holding an english description of error.
 536 void CMSEXPORT cmsSignalError(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *ErrorText, ...)
 537 {
 538     va_list args;
 539     char Buffer[MAX_ERROR_MESSAGE_LEN];
 540     _cmsLogErrorChunkType* lhg;
 541 
 542 
 543     va_start(args, ErrorText);
 544     vsnprintf(Buffer, MAX_ERROR_MESSAGE_LEN-1, ErrorText, args);
 545     va_end(args);
 546 
 547     // Check for the context, if specified go there. If not, go for the global
 548     lhg = (_cmsLogErrorChunkType*) _cmsContextGetClientChunk(ContextID, Logger);
 549     if (lhg ->LogErrorHandler) {
 550         lhg ->LogErrorHandler(ContextID, ErrorCode, Buffer);
 551     }
 552 }
 553 
 554 // Utility function to print signatures
 555 void _cmsTagSignature2String(char String[5], cmsTagSignature sig)
 556 {
 557     cmsUInt32Number be;
 558 
 559     // Convert to big endian
 560     be = _cmsAdjustEndianess32((cmsUInt32Number) sig);
 561 
 562     // Move chars
 563     memmove(String, &be, 4);
 564 
 565     // Make sure of terminator
 566     String[4] = 0;
 567 }
 568 
 569 //--------------------------------------------------------------------------------------------------
 570 
 571 
 572 static
 573 void* defMtxCreate(cmsContext id)
 574 {
 575     _cmsMutex* ptr_mutex = (_cmsMutex*) _cmsMalloc(id, sizeof(_cmsMutex));
 576     _cmsInitMutexPrimitive(ptr_mutex);
 577     return (void*) ptr_mutex;
 578 }
 579 
 580 static
 581 void defMtxDestroy(cmsContext id, void* mtx)
 582 {
 583     _cmsDestroyMutexPrimitive((_cmsMutex *) mtx);
 584     _cmsFree(id, mtx);
 585 }
 586 
 587 static
 588 cmsBool defMtxLock(cmsContext id, void* mtx)
 589 {
 590     cmsUNUSED_PARAMETER(id);
 591     return _cmsLockPrimitive((_cmsMutex *) mtx) == 0;
 592 }
 593 
 594 static
 595 void defMtxUnlock(cmsContext id, void* mtx)
 596 {
 597     cmsUNUSED_PARAMETER(id);
 598     _cmsUnlockPrimitive((_cmsMutex *) mtx);
 599 }
 600 
 601 
 602 
 603 // Pointers to memory manager functions in Context0
 604 _cmsMutexPluginChunkType _cmsMutexPluginChunk = { defMtxCreate, defMtxDestroy, defMtxLock, defMtxUnlock };
 605 
 606 // Allocate and init mutex container.
 607 void _cmsAllocMutexPluginChunk(struct _cmsContext_struct* ctx,
 608                                         const struct _cmsContext_struct* src)
 609 {
 610     static _cmsMutexPluginChunkType MutexChunk = {defMtxCreate, defMtxDestroy, defMtxLock, defMtxUnlock };
 611     void* from;
 612 
 613      if (src != NULL) {
 614         from = src ->chunks[MutexPlugin];
 615     }
 616     else {
 617        from = &MutexChunk;
 618     }
 619 
 620     ctx ->chunks[MutexPlugin] = _cmsSubAllocDup(ctx ->MemPool, from, sizeof(_cmsMutexPluginChunkType));
 621 }
 622 
 623 // Register new ways to transform
 624 cmsBool  _cmsRegisterMutexPlugin(cmsContext ContextID, cmsPluginBase* Data)
 625 {
 626     cmsPluginMutex* Plugin = (cmsPluginMutex*) Data;
 627     _cmsMutexPluginChunkType* ctx = ( _cmsMutexPluginChunkType*) _cmsContextGetClientChunk(ContextID, MutexPlugin);
 628 
 629     if (Data == NULL) {
 630 
 631         // No lock routines
 632         ctx->CreateMutexPtr = NULL;
 633         ctx->DestroyMutexPtr = NULL;
 634         ctx->LockMutexPtr = NULL;
 635         ctx ->UnlockMutexPtr = NULL;
 636         return TRUE;
 637     }
 638 
 639     // Factory callback is required
 640     if (Plugin ->CreateMutexPtr == NULL || Plugin ->DestroyMutexPtr == NULL ||
 641         Plugin ->LockMutexPtr == NULL || Plugin ->UnlockMutexPtr == NULL) return FALSE;
 642 
 643 
 644     ctx->CreateMutexPtr  = Plugin->CreateMutexPtr;
 645     ctx->DestroyMutexPtr = Plugin ->DestroyMutexPtr;
 646     ctx ->LockMutexPtr   = Plugin ->LockMutexPtr;
 647     ctx ->UnlockMutexPtr = Plugin ->UnlockMutexPtr;
 648 
 649     // All is ok
 650     return TRUE;
 651 }
 652 
 653 // Generic Mutex fns
 654 void* CMSEXPORT _cmsCreateMutex(cmsContext ContextID)
 655 {
 656     _cmsMutexPluginChunkType* ptr = (_cmsMutexPluginChunkType*) _cmsContextGetClientChunk(ContextID, MutexPlugin);
 657 
 658     if (ptr ->CreateMutexPtr == NULL) return NULL;
 659 
 660     return ptr ->CreateMutexPtr(ContextID);
 661 }
 662 
 663 void CMSEXPORT _cmsDestroyMutex(cmsContext ContextID, void* mtx)
 664 {
 665     _cmsMutexPluginChunkType* ptr = (_cmsMutexPluginChunkType*) _cmsContextGetClientChunk(ContextID, MutexPlugin);
 666 
 667     if (ptr ->DestroyMutexPtr != NULL) {
 668 
 669         ptr ->DestroyMutexPtr(ContextID, mtx);
 670     }
 671 }
 672 
 673 cmsBool CMSEXPORT _cmsLockMutex(cmsContext ContextID, void* mtx)
 674 {
 675     _cmsMutexPluginChunkType* ptr = (_cmsMutexPluginChunkType*) _cmsContextGetClientChunk(ContextID, MutexPlugin);
 676 
 677     if (ptr ->LockMutexPtr == NULL) return TRUE;
 678 
 679     return ptr ->LockMutexPtr(ContextID, mtx);
 680 }
 681 
 682 void CMSEXPORT _cmsUnlockMutex(cmsContext ContextID, void* mtx)
 683 {
 684     _cmsMutexPluginChunkType* ptr = (_cmsMutexPluginChunkType*) _cmsContextGetClientChunk(ContextID, MutexPlugin);
 685 
 686     if (ptr ->UnlockMutexPtr != NULL) {
 687 
 688         ptr ->UnlockMutexPtr(ContextID, mtx);
 689     }
 690 }