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
32int 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)
45long 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#if 0
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
77cmsBool   _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
83static
84void* _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
94static
95void* _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
106static
107void _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.
119static
120void* _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.
133static
134void* _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
155static
156void* _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
178void _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)
195void _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
221cmsBool  _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#else
254#include "../../../../../include/fxcrt/fx_system.h"
255#include "../../../../../include/fxcrt/fx_memory.h"
256
257cmsBool  _cmsRegisterMemHandlerPlugin(cmsContext ContextID, cmsPluginBase* Plugin)
258{
259	return TRUE;
260}
261
262// Generic allocate
263void* CMSEXPORT _cmsMalloc(cmsContext ContextID, cmsUInt32Number size)
264{
265    return FXMEM_DefaultAlloc(size, FXMEM_NONLEAVE);
266}
267
268// Generic allocate & zero
269void* CMSEXPORT _cmsMallocZero(cmsContext ContextID, cmsUInt32Number size)
270{
271	void* p = FXMEM_DefaultAlloc(size, FXMEM_NONLEAVE);
272	if (p) FXSYS_memset32(p, 0, size);
273	return p;
274}
275
276// Generic calloc
277void* CMSEXPORT _cmsCalloc(cmsContext ContextID, cmsUInt32Number num, cmsUInt32Number size)
278{
279	cmsUInt32Number total = num * size;
280	if (total == 0 || total / size != num || total >= 512 * 1024 * 1024)
281		return NULL;
282
283	return _cmsMallocZero(ContextID, num * size);
284}
285
286// Generic reallocate
287void* CMSEXPORT _cmsRealloc(cmsContext ContextID, void* Ptr, cmsUInt32Number size)
288{
289	return FXMEM_DefaultRealloc(Ptr, size, FXMEM_NONLEAVE);
290}
291
292// Generic free memory
293void CMSEXPORT _cmsFree(cmsContext ContextID, void* Ptr)
294{
295	if (Ptr != NULL) FXMEM_DefaultFree(Ptr, 0);
296}
297
298// Generic block duplication
299void* CMSEXPORT _cmsDupMem(cmsContext ContextID, const void* Org, cmsUInt32Number size)
300{
301	void* p = FXMEM_DefaultAlloc(size, FXMEM_NONLEAVE);
302	FXSYS_memmove32(p, Org, size);
303	return p;
304}
305
306_cmsMemPluginChunkType _cmsMemPluginChunk = {_cmsMalloc, _cmsMallocZero, _cmsFree,
307	                                         _cmsRealloc, _cmsCalloc,    _cmsDupMem
308                                            };
309
310void _cmsAllocMemPluginChunk(struct _cmsContext_struct* ctx, const struct _cmsContext_struct* src)
311{
312	_cmsAssert(ctx != NULL);
313
314	if (src != NULL) {
315
316		// Duplicate
317		ctx ->chunks[MemPlugin] = _cmsSubAllocDup(ctx ->MemPool, src ->chunks[MemPlugin], sizeof(_cmsMemPluginChunkType));
318	}
319	else {
320
321		// To reset it, we use the default allocators, which cannot be overriden
322		ctx ->chunks[MemPlugin] = &ctx ->DefaultMemoryManager;
323	}
324}
325
326void _cmsInstallAllocFunctions(cmsPluginMemHandler* Plugin, _cmsMemPluginChunkType* ptr)
327{
328	if (Plugin == NULL) {
329
330		memcpy(ptr, &_cmsMemPluginChunk, sizeof(_cmsMemPluginChunk));
331	}
332	else {
333
334		ptr ->MallocPtr  = Plugin -> MallocPtr;
335		ptr ->FreePtr    = Plugin -> FreePtr;
336		ptr ->ReallocPtr = Plugin -> ReallocPtr;
337
338		// Make sure we revert to defaults
339		ptr ->MallocZeroPtr= _cmsMallocZero;
340		ptr ->CallocPtr    = _cmsCalloc;
341		ptr ->DupPtr       = _cmsDupMem;
342
343		if (Plugin ->MallocZeroPtr != NULL) ptr ->MallocZeroPtr = Plugin -> MallocZeroPtr;
344		if (Plugin ->CallocPtr != NULL)     ptr ->CallocPtr     = Plugin -> CallocPtr;
345		if (Plugin ->DupPtr != NULL)        ptr ->DupPtr        = Plugin -> DupPtr;
346
347	}
348}
349#endif
350
351// ********************************************************************************************
352
353// Sub allocation takes care of many pointers of small size. The memory allocated in
354// this way have be freed at once. Next function allocates a single chunk for linked list
355// I prefer this method over realloc due to the big inpact on xput realloc may have if
356// memory is being swapped to disk. This approach is safer (although that may not be true on all platforms)
357static
358_cmsSubAllocator_chunk* _cmsCreateSubAllocChunk(cmsContext ContextID, cmsUInt32Number Initial)
359{
360    _cmsSubAllocator_chunk* chunk;
361
362    // 20K by default
363    if (Initial == 0)
364        Initial = 20*1024;
365
366    // Create the container
367    chunk = (_cmsSubAllocator_chunk*) _cmsMallocZero(ContextID, sizeof(_cmsSubAllocator_chunk));
368    if (chunk == NULL) return NULL;
369
370    // Initialize values
371    chunk ->Block     = (cmsUInt8Number*) _cmsMalloc(ContextID, Initial);
372    if (chunk ->Block == NULL) {
373
374        // Something went wrong
375        _cmsFree(ContextID, chunk);
376        return NULL;
377    }
378
379    chunk ->BlockSize = Initial;
380    chunk ->Used      = 0;
381    chunk ->next      = NULL;
382
383    return chunk;
384}
385
386// The suballocated is nothing but a pointer to the first element in the list. We also keep
387// the thread ID in this structure.
388_cmsSubAllocator* _cmsCreateSubAlloc(cmsContext ContextID, cmsUInt32Number Initial)
389{
390    _cmsSubAllocator* sub;
391
392    // Create the container
393    sub = (_cmsSubAllocator*) _cmsMallocZero(ContextID, sizeof(_cmsSubAllocator));
394    if (sub == NULL) return NULL;
395
396    sub ->ContextID = ContextID;
397
398    sub ->h = _cmsCreateSubAllocChunk(ContextID, Initial);
399    if (sub ->h == NULL) {
400        _cmsFree(ContextID, sub);
401        return NULL;
402    }
403
404    return sub;
405}
406
407
408// Get rid of whole linked list
409void _cmsSubAllocDestroy(_cmsSubAllocator* sub)
410{
411    _cmsSubAllocator_chunk *chunk, *n;
412
413    for (chunk = sub ->h; chunk != NULL; chunk = n) {
414
415        n = chunk->next;
416        if (chunk->Block != NULL) _cmsFree(sub ->ContextID, chunk->Block);
417        _cmsFree(sub ->ContextID, chunk);
418    }
419
420    // Free the header
421    _cmsFree(sub ->ContextID, sub);
422}
423
424
425// Get a pointer to small memory block.
426void*  _cmsSubAlloc(_cmsSubAllocator* sub, cmsUInt32Number size)
427{
428    cmsUInt32Number Free = sub -> h ->BlockSize - sub -> h -> Used;
429    cmsUInt8Number* ptr;
430
431    size = _cmsALIGNMEM(size);
432
433    // Check for memory. If there is no room, allocate a new chunk of double memory size.
434    if (size > Free) {
435
436        _cmsSubAllocator_chunk* chunk;
437        cmsUInt32Number newSize;
438
439        newSize = sub -> h ->BlockSize * 2;
440        if (newSize < size) newSize = size;
441
442        chunk = _cmsCreateSubAllocChunk(sub -> ContextID, newSize);
443        if (chunk == NULL) return NULL;
444
445        // Link list
446        chunk ->next = sub ->h;
447        sub ->h    = chunk;
448
449    }
450
451    ptr =  sub -> h ->Block + sub -> h ->Used;
452    sub -> h -> Used += size;
453
454    return (void*) ptr;
455}
456
457// Duplicate in pool
458void* _cmsSubAllocDup(_cmsSubAllocator* s, const void *ptr, cmsUInt32Number size)
459{
460    void *NewPtr;
461
462    // Dup of null pointer is also NULL
463    if (ptr == NULL)
464        return NULL;
465
466    NewPtr = _cmsSubAlloc(s, size);
467
468    if (ptr != NULL && NewPtr != NULL) {
469        memcpy(NewPtr, ptr, size);
470    }
471
472    return NewPtr;
473}
474
475
476
477// Error logging ******************************************************************
478
479// There is no error handling at all. When a funtion fails, it returns proper value.
480// For example, all create functions does return NULL on failure. Other return FALSE
481// It may be interesting, for the developer, to know why the function is failing.
482// for that reason, lcms2 does offer a logging function. This function does recive
483// a ENGLISH string with some clues on what is going wrong. You can show this
484// info to the end user, or just create some sort of log.
485// The logging function should NOT terminate the program, as this obviously can leave
486// resources. It is the programmer's responsability to check each function return code
487// to make sure it didn't fail.
488
489// Error messages are limited to MAX_ERROR_MESSAGE_LEN
490
491#define MAX_ERROR_MESSAGE_LEN   1024
492
493// ---------------------------------------------------------------------------------------------------------
494
495// This is our default log error
496static void DefaultLogErrorHandlerFunction(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *Text);
497
498// Context0 storage, which is global
499_cmsLogErrorChunkType _cmsLogErrorChunk = { DefaultLogErrorHandlerFunction };
500
501// Allocates and inits error logger container for a given context. If src is NULL, only initializes the value
502// to the default. Otherwise, it duplicates the value. The interface is standard across all context clients
503void _cmsAllocLogErrorChunk(struct _cmsContext_struct* ctx,
504                            const struct _cmsContext_struct* src)
505{
506    static _cmsLogErrorChunkType LogErrorChunk = { DefaultLogErrorHandlerFunction };
507    void* from;
508
509     if (src != NULL) {
510        from = src ->chunks[Logger];
511    }
512    else {
513       from = &LogErrorChunk;
514    }
515
516    ctx ->chunks[Logger] = _cmsSubAllocDup(ctx ->MemPool, from, sizeof(_cmsLogErrorChunkType));
517}
518
519// The default error logger does nothing.
520static
521void DefaultLogErrorHandlerFunction(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *Text)
522{
523    // fprintf(stderr, "[lcms]: %s\n", Text);
524    // fflush(stderr);
525
526     cmsUNUSED_PARAMETER(ContextID);
527     cmsUNUSED_PARAMETER(ErrorCode);
528     cmsUNUSED_PARAMETER(Text);
529}
530
531// Change log error, context based
532void CMSEXPORT cmsSetLogErrorHandlerTHR(cmsContext ContextID, cmsLogErrorHandlerFunction Fn)
533{
534    _cmsLogErrorChunkType* lhg = (_cmsLogErrorChunkType*) _cmsContextGetClientChunk(ContextID, Logger);
535
536    if (lhg != NULL) {
537
538        if (Fn == NULL)
539            lhg -> LogErrorHandler = DefaultLogErrorHandlerFunction;
540        else
541            lhg -> LogErrorHandler = Fn;
542    }
543}
544
545// Change log error, legacy
546void CMSEXPORT cmsSetLogErrorHandler(cmsLogErrorHandlerFunction Fn)
547{
548    cmsSetLogErrorHandlerTHR(NULL, Fn);
549}
550
551// Log an error
552// ErrorText is a text holding an english description of error.
553void CMSEXPORT cmsSignalError(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *ErrorText, ...)
554{
555    va_list args;
556    char Buffer[MAX_ERROR_MESSAGE_LEN];
557    _cmsLogErrorChunkType* lhg;
558
559
560    va_start(args, ErrorText);
561    vsnprintf(Buffer, MAX_ERROR_MESSAGE_LEN-1, ErrorText, args);
562    va_end(args);
563
564    // Check for the context, if specified go there. If not, go for the global
565    lhg = (_cmsLogErrorChunkType*) _cmsContextGetClientChunk(ContextID, Logger);
566    if (lhg ->LogErrorHandler) {
567        lhg ->LogErrorHandler(ContextID, ErrorCode, Buffer);
568    }
569}
570
571// Utility function to print signatures
572void _cmsTagSignature2String(char String[5], cmsTagSignature sig)
573{
574    cmsUInt32Number be;
575
576    // Convert to big endian
577    be = _cmsAdjustEndianess32((cmsUInt32Number) sig);
578
579    // Move chars
580    memmove(String, &be, 4);
581
582    // Make sure of terminator
583    String[4] = 0;
584}
585
586//--------------------------------------------------------------------------------------------------
587
588
589static
590void* defMtxCreate(cmsContext id)
591{
592    _cmsMutex* ptr_mutex = (_cmsMutex*) _cmsMalloc(id, sizeof(_cmsMutex));
593    _cmsInitMutexPrimitive(ptr_mutex);
594    return (void*) ptr_mutex;
595}
596
597static
598void defMtxDestroy(cmsContext id, void* mtx)
599{
600    _cmsDestroyMutexPrimitive((_cmsMutex *) mtx);
601    _cmsFree(id, mtx);
602}
603
604static
605cmsBool defMtxLock(cmsContext id, void* mtx)
606{
607    cmsUNUSED_PARAMETER(id);
608    return _cmsLockPrimitive((_cmsMutex *) mtx) == 0;
609}
610
611static
612void defMtxUnlock(cmsContext id, void* mtx)
613{
614    cmsUNUSED_PARAMETER(id);
615    _cmsUnlockPrimitive((_cmsMutex *) mtx);
616}
617
618
619
620// Pointers to memory manager functions in Context0
621_cmsMutexPluginChunkType _cmsMutexPluginChunk = { defMtxCreate, defMtxDestroy, defMtxLock, defMtxUnlock };
622
623// Allocate and init mutex container.
624void _cmsAllocMutexPluginChunk(struct _cmsContext_struct* ctx,
625                                        const struct _cmsContext_struct* src)
626{
627    static _cmsMutexPluginChunkType MutexChunk = {defMtxCreate, defMtxDestroy, defMtxLock, defMtxUnlock };
628    void* from;
629
630     if (src != NULL) {
631        from = src ->chunks[MutexPlugin];
632    }
633    else {
634       from = &MutexChunk;
635    }
636
637    ctx ->chunks[MutexPlugin] = _cmsSubAllocDup(ctx ->MemPool, from, sizeof(_cmsMutexPluginChunkType));
638}
639
640// Register new ways to transform
641cmsBool  _cmsRegisterMutexPlugin(cmsContext ContextID, cmsPluginBase* Data)
642{
643    cmsPluginMutex* Plugin = (cmsPluginMutex*) Data;
644    _cmsMutexPluginChunkType* ctx = ( _cmsMutexPluginChunkType*) _cmsContextGetClientChunk(ContextID, MutexPlugin);
645
646    if (Data == NULL) {
647
648        // No lock routines
649        ctx->CreateMutexPtr = NULL;
650        ctx->DestroyMutexPtr = NULL;
651        ctx->LockMutexPtr = NULL;
652        ctx ->UnlockMutexPtr = NULL;
653        return TRUE;
654    }
655
656    // Factory callback is required
657    if (Plugin ->CreateMutexPtr == NULL || Plugin ->DestroyMutexPtr == NULL ||
658        Plugin ->LockMutexPtr == NULL || Plugin ->UnlockMutexPtr == NULL) return FALSE;
659
660
661    ctx->CreateMutexPtr  = Plugin->CreateMutexPtr;
662    ctx->DestroyMutexPtr = Plugin ->DestroyMutexPtr;
663    ctx ->LockMutexPtr   = Plugin ->LockMutexPtr;
664    ctx ->UnlockMutexPtr = Plugin ->UnlockMutexPtr;
665
666    // All is ok
667    return TRUE;
668}
669
670// Generic Mutex fns
671void* CMSEXPORT _cmsCreateMutex(cmsContext ContextID)
672{
673    _cmsMutexPluginChunkType* ptr = (_cmsMutexPluginChunkType*) _cmsContextGetClientChunk(ContextID, MutexPlugin);
674
675    if (ptr ->CreateMutexPtr == NULL) return NULL;
676
677    return ptr ->CreateMutexPtr(ContextID);
678}
679
680void CMSEXPORT _cmsDestroyMutex(cmsContext ContextID, void* mtx)
681{
682    _cmsMutexPluginChunkType* ptr = (_cmsMutexPluginChunkType*) _cmsContextGetClientChunk(ContextID, MutexPlugin);
683
684    if (ptr ->DestroyMutexPtr != NULL) {
685
686        ptr ->DestroyMutexPtr(ContextID, mtx);
687    }
688}
689
690cmsBool CMSEXPORT _cmsLockMutex(cmsContext ContextID, void* mtx)
691{
692    _cmsMutexPluginChunkType* ptr = (_cmsMutexPluginChunkType*) _cmsContextGetClientChunk(ContextID, MutexPlugin);
693
694    if (ptr ->LockMutexPtr == NULL) return TRUE;
695
696    return ptr ->LockMutexPtr(ContextID, mtx);
697}
698
699void CMSEXPORT _cmsUnlockMutex(cmsContext ContextID, void* mtx)
700{
701    _cmsMutexPluginChunkType* ptr = (_cmsMutexPluginChunkType*) _cmsContextGetClientChunk(ContextID, MutexPlugin);
702
703    if (ptr ->UnlockMutexPtr != NULL) {
704
705        ptr ->UnlockMutexPtr(ContextID, mtx);
706    }
707}
708