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