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