1diff --git a/third_party/lcms/src/cmserr.c b/third_party/lcms/src/cmserr.c
2index 700152ee3..3c3848b2a 100644
3--- a/third_party/lcms/src/cmserr.c
4+++ b/third_party/lcms/src/cmserr.c
5@@ -25,6 +25,8 @@
6
7 #include "lcms2_internal.h"
8
9+#include "core/fxcrt/fx_memory.h"
10+#include "core/fxcrt/fx_system.h"
11
12 // This function is here to help applications to prevent mixing lcms versions on header and shared objects.
13 int CMSEXPORT cmsGetEncodedCMMversion(void)
14@@ -67,140 +67,75 @@ long int CMSEXPORT cmsfilelength(FILE* f)
15     return n;
16 }
17
18-
19-// Memory handling ------------------------------------------------------------------
20-//
21-// This is the interface to low-level memory management routines. By default a simple
22-// wrapping to malloc/free/realloc is provided, although there is a limit on the max
23-// amount of memoy that can be reclaimed. This is mostly as a safety feature to prevent
24-// bogus or evil code to allocate huge blocks that otherwise lcms would never need.
25-
26-#define MAX_MEMORY_FOR_ALLOC  ((cmsUInt32Number)(1024U*1024U*512U))
27-
28-// User may override this behaviour by using a memory plug-in, which basically replaces
29-// the default memory management functions. In this case, no check is performed and it
30-// is up to the plug-in writter to keep in the safe side. There are only three functions
31-// required to be implemented: malloc, realloc and free, although the user may want to
32-// replace the optional mallocZero, calloc and dup as well.
33-
34-cmsBool   _cmsRegisterMemHandlerPlugin(cmsContext ContextID, cmsPluginBase* Plugin);
35-
36-// *********************************************************************************
37-
38-// This is the default memory allocation function. It does a very coarse
39-// check of amount of memory, just to prevent exploits
40-static
41-void* _cmsMallocDefaultFn(cmsContext ContextID, cmsUInt32Number size)
42+cmsBool  _cmsRegisterMemHandlerPlugin(cmsContext ContextID, cmsPluginBase* Plugin)
43 {
44-    if (size > MAX_MEMORY_FOR_ALLOC) return NULL;  // Never allow over maximum
45-
46-    return (void*) malloc(size);
47+    return TRUE;
48+}
49
50-    cmsUNUSED_PARAMETER(ContextID);
51+// Generic allocate
52+void* CMSEXPORT _cmsMalloc(cmsContext ContextID, cmsUInt32Number size)
53+{
54+    return FXMEM_DefaultAlloc(size);
55 }
56
57 // Generic allocate & zero
58-static
59-void* _cmsMallocZeroDefaultFn(cmsContext ContextID, cmsUInt32Number size)
60+void* CMSEXPORT _cmsMallocZero(cmsContext ContextID, cmsUInt32Number size)
61 {
62-    void *pt = _cmsMalloc(ContextID, size);
63-    if (pt == NULL) return NULL;
64-
65-    memset(pt, 0, size);
66-    return pt;
67+    void* p = FXMEM_DefaultAlloc(size);
68+    if (p) memset(p, 0, size);
69+    return p;
70 }
71
72-
73-// The default free function. The only check proformed is against NULL pointers
74-static
75-void _cmsFreeDefaultFn(cmsContext ContextID, void *Ptr)
76+// Generic calloc
77+void* CMSEXPORT _cmsCalloc(cmsContext ContextID, cmsUInt32Number num, cmsUInt32Number size)
78 {
79-    // free(NULL) is defined a no-op by C99, therefore it is safe to
80-    // avoid the check, but it is here just in case...
81-
82-    if (Ptr) free(Ptr);
83+    cmsUInt32Number total = num * size;
84+    if (total == 0 || total / size != num || total >= 512 * 1024 * 1024)
85+        return NULL;
86
87-    cmsUNUSED_PARAMETER(ContextID);
88+    return _cmsMallocZero(ContextID, num * size);
89 }
90
91-// The default realloc function. Again it checks for exploits. If Ptr is NULL,
92-// realloc behaves the same way as malloc and allocates a new block of size bytes.
93-static
94-void* _cmsReallocDefaultFn(cmsContext ContextID, void* Ptr, cmsUInt32Number size)
95+// Generic reallocate
96+void* CMSEXPORT _cmsRealloc(cmsContext ContextID, void* Ptr, cmsUInt32Number size)
97 {
98-
99-    if (size > MAX_MEMORY_FOR_ALLOC) return NULL;  // Never realloc over 512Mb
100-
101-    return realloc(Ptr, size);
102-
103-    cmsUNUSED_PARAMETER(ContextID);
104+    return FXMEM_DefaultRealloc(Ptr, size);
105 }
106
107-
108-// The default calloc function. Allocates an array of num elements, each one of size bytes
109-// all memory is initialized to zero.
110-static
111-void* _cmsCallocDefaultFn(cmsContext ContextID, cmsUInt32Number num, cmsUInt32Number size)
112+// Generic free memory
113+void CMSEXPORT _cmsFree(cmsContext ContextID, void* Ptr)
114 {
115-    cmsUInt32Number Total = num * size;
116-
117-    // Preserve calloc behaviour
118-    if (Total == 0) return NULL;
119-
120-    // Safe check for overflow.
121-    if (num >= UINT_MAX / size) return NULL;
122-
123-    // Check for overflow
124-    if (Total < num || Total < size) {
125-        return NULL;
126-    }
127-
128-    if (Total > MAX_MEMORY_FOR_ALLOC) return NULL;  // Never alloc over 512Mb
129-
130-    return _cmsMallocZero(ContextID, Total);
131+    if (Ptr != NULL) FXMEM_DefaultFree(Ptr);
132 }
133
134 // Generic block duplication
135-static
136-void* _cmsDupDefaultFn(cmsContext ContextID, const void* Org, cmsUInt32Number size)
137+void* CMSEXPORT _cmsDupMem(cmsContext ContextID, const void* Org, cmsUInt32Number size)
138 {
139-    void* mem;
140-
141-    if (size > MAX_MEMORY_FOR_ALLOC) return NULL;  // Never dup over 512Mb
142-
143-    mem = _cmsMalloc(ContextID, size);
144-
145-    if (mem != NULL && Org != NULL)
146-        memmove(mem, Org, size);
147-
148-    return mem;
149+    void* p = FXMEM_DefaultAlloc(size);
150+    memmove(p, Org, size);
151+    return p;
152 }
153
154-
155-// Pointers to memory manager functions in Context0
156-_cmsMemPluginChunkType _cmsMemPluginChunk = { _cmsMallocDefaultFn, _cmsMallocZeroDefaultFn, _cmsFreeDefaultFn,
157-                                              _cmsReallocDefaultFn, _cmsCallocDefaultFn,    _cmsDupDefaultFn
158+_cmsMemPluginChunkType _cmsMemPluginChunk = {_cmsMalloc, _cmsMallocZero, _cmsFree,
159+                                             _cmsRealloc, _cmsCalloc,    _cmsDupMem
160                                             };
161
162-
163-// Reset and duplicate memory manager
164 void _cmsAllocMemPluginChunk(struct _cmsContext_struct* ctx, const struct _cmsContext_struct* src)
165 {
166     _cmsAssert(ctx != NULL);
167
168-    if (src != NULL) {
169+    if (src != NULL) {
170
171         // Duplicate
172-        ctx ->chunks[MemPlugin] = _cmsSubAllocDup(ctx ->MemPool, src ->chunks[MemPlugin], sizeof(_cmsMemPluginChunkType));
173+        ctx ->chunks[MemPlugin] = _cmsSubAllocDup(ctx ->MemPool, src ->chunks[MemPlugin], sizeof(_cmsMemPluginChunkType));
174     }
175     else {
176
177         // To reset it, we use the default allocators, which cannot be overridden
178         ctx ->chunks[MemPlugin] = &ctx ->DefaultMemoryManager;
179-    }
180+    }
181 }
182
183-// Auxiliary to fill memory management functions from plugin (or context 0 defaults)
184 void _cmsInstallAllocFunctions(cmsPluginMemHandler* Plugin, _cmsMemPluginChunkType* ptr)
185 {
186     if (Plugin == NULL) {
187@@ -214,94 +149,15 @@ void _cmsInstallAllocFunctions(cmsPluginMemHandler* Plugin, _cmsMemPluginChunkTy
188         ptr ->ReallocPtr = Plugin -> ReallocPtr;
189
190         // Make sure we revert to defaults
191-        ptr ->MallocZeroPtr= _cmsMallocZeroDefaultFn;
192-        ptr ->CallocPtr    = _cmsCallocDefaultFn;
193-        ptr ->DupPtr       = _cmsDupDefaultFn;
194-
195+        ptr ->MallocZeroPtr= _cmsMallocZero;
196+        ptr ->CallocPtr    = _cmsCalloc;
197+        ptr ->DupPtr       = _cmsDupMem;
198+
199         if (Plugin ->MallocZeroPtr != NULL) ptr ->MallocZeroPtr = Plugin -> MallocZeroPtr;
200         if (Plugin ->CallocPtr != NULL)     ptr ->CallocPtr     = Plugin -> CallocPtr;
201         if (Plugin ->DupPtr != NULL)        ptr ->DupPtr        = Plugin -> DupPtr;
202-
203-    }
204-}
205-
206
207-// Plug-in replacement entry
208-cmsBool  _cmsRegisterMemHandlerPlugin(cmsContext ContextID, cmsPluginBase *Data)
209-{
210-    cmsPluginMemHandler* Plugin = (cmsPluginMemHandler*) Data;
211-    _cmsMemPluginChunkType* ptr;
212-
213-    // NULL forces to reset to defaults. In this special case, the defaults are stored in the context structure.
214-    // Remaining plug-ins does NOT have any copy in the context structure, but this is somehow special as the
215-    // context internal data should be malloce'd by using those functions.
216-    if (Data == NULL) {
217-
218-       struct _cmsContext_struct* ctx = ( struct _cmsContext_struct*) ContextID;
219-
220-       // Return to the default allocators
221-        if (ContextID != NULL) {
222-            ctx->chunks[MemPlugin] = (void*) &ctx->DefaultMemoryManager;
223-        }
224-        return TRUE;
225     }
226-
227-    // Check for required callbacks
228-    if (Plugin -> MallocPtr == NULL ||
229-        Plugin -> FreePtr == NULL ||
230-        Plugin -> ReallocPtr == NULL) return FALSE;
231-
232-    // Set replacement functions
233-    ptr = (_cmsMemPluginChunkType*) _cmsContextGetClientChunk(ContextID, MemPlugin);
234-    if (ptr == NULL)
235-        return FALSE;
236-
237-    _cmsInstallAllocFunctions(Plugin, ptr);
238-    return TRUE;
239-}
240-
241-// Generic allocate
242-void* CMSEXPORT _cmsMalloc(cmsContext ContextID, cmsUInt32Number size)
243-{
244-    _cmsMemPluginChunkType* ptr = (_cmsMemPluginChunkType*) _cmsContextGetClientChunk(ContextID, MemPlugin);
245-    return ptr ->MallocPtr(ContextID, size);
246-}
247-
248-// Generic allocate & zero
249-void* CMSEXPORT _cmsMallocZero(cmsContext ContextID, cmsUInt32Number size)
250-{
251-    _cmsMemPluginChunkType* ptr = (_cmsMemPluginChunkType*) _cmsContextGetClientChunk(ContextID, MemPlugin);
252-    return ptr->MallocZeroPtr(ContextID, size);
253-}
254-
255-// Generic calloc
256-void* CMSEXPORT _cmsCalloc(cmsContext ContextID, cmsUInt32Number num, cmsUInt32Number size)
257-{
258-    _cmsMemPluginChunkType* ptr = (_cmsMemPluginChunkType*) _cmsContextGetClientChunk(ContextID, MemPlugin);
259-    return ptr->CallocPtr(ContextID, num, size);
260-}
261-
262-// Generic reallocate
263-void* CMSEXPORT _cmsRealloc(cmsContext ContextID, void* Ptr, cmsUInt32Number size)
264-{
265-    _cmsMemPluginChunkType* ptr = (_cmsMemPluginChunkType*) _cmsContextGetClientChunk(ContextID, MemPlugin);
266-    return ptr->ReallocPtr(ContextID, Ptr, size);
267-}
268-
269-// Generic free memory
270-void CMSEXPORT _cmsFree(cmsContext ContextID, void* Ptr)
271-{
272-    if (Ptr != NULL) {
273-        _cmsMemPluginChunkType* ptr = (_cmsMemPluginChunkType*) _cmsContextGetClientChunk(ContextID, MemPlugin);
274-        ptr ->FreePtr(ContextID, Ptr);
275-    }
276-}
277-
278-// Generic block duplication
279-void* CMSEXPORT _cmsDupMem(cmsContext ContextID, const void* Org, cmsUInt32Number size)
280-{
281-    _cmsMemPluginChunkType* ptr = (_cmsMemPluginChunkType*) _cmsContextGetClientChunk(ContextID, MemPlugin);
282-    return ptr ->DupPtr(ContextID, Org, size);
283 }
284
285 // ********************************************************************************************
286