Tizen 2.0 Release
[external/lcms.git] / src / cmserr.c
1 //---------------------------------------------------------------------------------
2 //
3 //  Little Color Management System
4 //  Copyright (c) 1998-2010 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         return (toupper(*us1) - toupper(*--us2));
41 }
42
43 // long int because C99 specifies ftell in such way (7.19.9.2)
44 long int CMSEXPORT cmsfilelength(FILE* f)
45 {
46     long int p , n; 
47
48     p = ftell(f); // register current file position 
49
50     if (fseek(f, 0, SEEK_END) != 0) { 
51         return -1; 
52     } 
53
54     n = ftell(f); 
55     fseek(f, p, SEEK_SET); // file position restored 
56
57     return n; 
58 }
59
60
61 // Memory handling ------------------------------------------------------------------
62 //
63 // This is the interface to low-level memory management routines. By default a simple
64 // wrapping to malloc/free/realloc is provided, although there is a limit on the max
65 // amount of memoy that can be reclaimed. This is mostly as a safety feature to 
66 // prevent bogus or malintentionated code to allocate huge blocks that otherwise lcms
67 // 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(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 check 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 // Pointers to malloc and _cmsFree functions in current environment
171 static void * (* MallocPtr)(cmsContext ContextID, cmsUInt32Number size)                     = _cmsMallocDefaultFn;
172 static void * (* MallocZeroPtr)(cmsContext ContextID, cmsUInt32Number size)                 = _cmsMallocZeroDefaultFn;
173 static void   (* FreePtr)(cmsContext ContextID, void *Ptr)                                  = _cmsFreeDefaultFn;
174 static void * (* ReallocPtr)(cmsContext ContextID, void *Ptr, cmsUInt32Number NewSize)      = _cmsReallocDefaultFn; 
175 static void * (* CallocPtr)(cmsContext ContextID, cmsUInt32Number num, cmsUInt32Number size)= _cmsCallocDefaultFn;
176 static void * (* DupPtr)(cmsContext ContextID, const void* Org, cmsUInt32Number size)       = _cmsDupDefaultFn;
177
178 // Plug-in replacement entry
179 cmsBool  _cmsRegisterMemHandlerPlugin(cmsPluginBase *Data)
180 {
181     cmsPluginMemHandler* Plugin = (cmsPluginMemHandler*) Data;
182
183     // NULL forces to reset to defaults
184     if (Data == NULL) {
185
186         MallocPtr    = _cmsMallocDefaultFn;
187         MallocZeroPtr= _cmsMallocZeroDefaultFn;
188         FreePtr      = _cmsFreeDefaultFn;
189         ReallocPtr   = _cmsReallocDefaultFn; 
190         CallocPtr    = _cmsCallocDefaultFn;
191         DupPtr       = _cmsDupDefaultFn;
192         return TRUE;
193     }
194
195     // Check for required callbacks
196     if (Plugin -> MallocPtr == NULL ||
197         Plugin -> FreePtr == NULL ||
198         Plugin -> ReallocPtr == NULL) return FALSE;
199
200     // Set replacement functions
201     MallocPtr  = Plugin -> MallocPtr;
202     FreePtr    = Plugin -> FreePtr;
203     ReallocPtr = Plugin -> ReallocPtr;
204
205     if (Plugin ->MallocZeroPtr != NULL) MallocZeroPtr = Plugin ->MallocZeroPtr;
206     if (Plugin ->CallocPtr != NULL)     CallocPtr     = Plugin -> CallocPtr;
207     if (Plugin ->DupPtr != NULL)        DupPtr        = Plugin -> DupPtr;
208
209     return TRUE;
210 }
211
212 // Generic allocate
213 void* CMSEXPORT _cmsMalloc(cmsContext ContextID, cmsUInt32Number size)
214 {
215     return MallocPtr(ContextID, size);
216 }
217
218 // Generic allocate & zero
219 void* CMSEXPORT _cmsMallocZero(cmsContext ContextID, cmsUInt32Number size)
220 {
221     return MallocZeroPtr(ContextID, size);
222 }
223
224 // Generic calloc
225 void* CMSEXPORT _cmsCalloc(cmsContext ContextID, cmsUInt32Number num, cmsUInt32Number size)
226 {
227     return CallocPtr(ContextID, num, size);
228 }
229
230 // Generic reallocate
231 void* CMSEXPORT _cmsRealloc(cmsContext ContextID, void* Ptr, cmsUInt32Number size)
232 {
233     return ReallocPtr(ContextID, Ptr, size);
234 }
235
236 // Generic free memory
237 void CMSEXPORT _cmsFree(cmsContext ContextID, void* Ptr)
238 {
239     if (Ptr != NULL) FreePtr(ContextID, Ptr);
240 }
241
242 // Generic block duplication
243 void* CMSEXPORT _cmsDupMem(cmsContext ContextID, const void* Org, cmsUInt32Number size)
244 {
245     return DupPtr(ContextID, Org, size);
246 }
247
248 // ********************************************************************************************
249
250 // Sub allocation takes care of many pointers of small size. The memory allocated in
251 // this way have be freed at once. Next function allocates a single chunk for linked list
252 // I prefer this method over realloc due to the big inpact on xput realloc may have if 
253 // memory is being swapped to disk. This approach is safer (although that may not be true on all platforms)
254 static
255 _cmsSubAllocator_chunk* _cmsCreateSubAllocChunk(cmsContext ContextID, cmsUInt32Number Initial)
256 {
257     _cmsSubAllocator_chunk* chunk;
258
259     // 20K by default
260     if (Initial == 0)
261         Initial = 20*1024;
262
263     // Create the container
264     chunk = (_cmsSubAllocator_chunk*) _cmsMallocZero(ContextID, sizeof(_cmsSubAllocator_chunk));
265     if (chunk == NULL) return NULL;
266
267     // Initialize values
268     chunk ->Block     = (cmsUInt8Number*) _cmsMalloc(ContextID, Initial);
269     if (chunk ->Block == NULL) {
270
271         // Something went wrong
272         _cmsFree(ContextID, chunk);
273         return NULL;
274     }
275
276
277
278     chunk ->BlockSize = Initial;
279     chunk ->Used      = 0;
280     chunk ->next      = NULL;
281
282     return chunk;
283 }
284
285 // The suballocated is nothing but a pointer to the first element in the list. We also keep
286 // the thread ID in this structure.
287 _cmsSubAllocator* _cmsCreateSubAlloc(cmsContext ContextID, cmsUInt32Number Initial)
288 {
289     _cmsSubAllocator* sub;
290
291     // Create the container
292     sub = (_cmsSubAllocator*) _cmsMallocZero(ContextID, sizeof(_cmsSubAllocator));
293     if (sub == NULL) return NULL;
294
295     sub ->ContextID = ContextID;
296
297     sub ->h = _cmsCreateSubAllocChunk(ContextID, Initial);
298     if (sub ->h == NULL) {
299         _cmsFree(ContextID, sub);
300         return NULL;
301     }
302
303     return sub;
304 }
305
306
307 // Get rid of whole linked list
308 void _cmsSubAllocDestroy(_cmsSubAllocator* sub)
309 {
310     _cmsSubAllocator_chunk *chunk, *n;
311
312     for (chunk = sub ->h; chunk != NULL; chunk = n) {
313
314         n = chunk->next;
315         if (chunk->Block != NULL) _cmsFree(sub ->ContextID, chunk->Block);
316         _cmsFree(sub ->ContextID, chunk);
317     }
318
319     // Free the header
320     _cmsFree(sub ->ContextID, sub);
321 }
322
323
324 // Get a pointer to small memory block.
325 void*  _cmsSubAlloc(_cmsSubAllocator* sub, cmsUInt32Number size)
326 {
327     cmsUInt32Number Free = sub -> h ->BlockSize - sub -> h -> Used;
328     cmsUInt8Number* ptr;
329
330     size = _cmsALIGNMEM(size);
331
332     // Check for memory. If there is no room, allocate a new chunk of double memory size.   
333     if (size > Free) {
334
335         _cmsSubAllocator_chunk* chunk;
336         cmsUInt32Number newSize;
337
338         newSize = sub -> h ->BlockSize * 2;
339         if (newSize < size) newSize = size;
340
341         chunk = _cmsCreateSubAllocChunk(sub -> ContextID, newSize);
342         if (chunk == NULL) return NULL;
343
344         // Link list
345         chunk ->next = sub ->h;
346         sub ->h    = chunk;
347
348     }
349             
350     ptr =  sub -> h ->Block + sub -> h ->Used;
351     sub -> h -> Used += size;
352
353     return (void*) ptr;
354 }
355
356 // Error logging ******************************************************************
357
358 // There is no error handling at all. When a funtion fails, it returns proper value.
359 // For example, all create functions does return NULL on failure. Other return FALSE
360 // It may be interesting, for the developer, to know why the function is failing.
361 // for that reason, lcms2 does offer a logging function. This function does recive
362 // a ENGLISH string with some clues on what is going wrong. You can show this 
363 // info to the end user, or just create some sort of log.
364 // The logging function should NOT terminate the program, as this obviously can leave
365 // resources. It is the programmer's responsability to check each function return code
366 // to make sure it didn't fail.
367
368 // Error messages are limited to MAX_ERROR_MESSAGE_LEN
369
370 #define MAX_ERROR_MESSAGE_LEN   1024
371
372 // ---------------------------------------------------------------------------------------------------------
373
374 // This is our default log error
375 static void DefaultLogErrorHandlerFunction(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *Text);
376
377 // The current handler in actual environment
378 static cmsLogErrorHandlerFunction LogErrorHandler   = DefaultLogErrorHandlerFunction;
379
380 // The default error logger does nothing.
381 static
382 void DefaultLogErrorHandlerFunction(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *Text)
383 {
384     // fprintf(stderr, "[lcms]: %s\n", Text);
385     // fflush(stderr);
386    
387      cmsUNUSED_PARAMETER(ContextID);
388      cmsUNUSED_PARAMETER(ErrorCode);
389      cmsUNUSED_PARAMETER(Text);
390 }
391
392 // Change log error
393 void CMSEXPORT cmsSetLogErrorHandler(cmsLogErrorHandlerFunction Fn)
394 {
395     if (Fn == NULL) 
396         LogErrorHandler = DefaultLogErrorHandlerFunction;
397     else
398         LogErrorHandler = Fn;
399 }
400
401 // Log an error 
402 // ErrorText is a text holding an english description of error.
403 void CMSEXPORT cmsSignalError(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *ErrorText, ...)
404 {
405     va_list args;
406     char Buffer[MAX_ERROR_MESSAGE_LEN];
407
408     va_start(args, ErrorText);
409     vsnprintf(Buffer, MAX_ERROR_MESSAGE_LEN-1, ErrorText, args);
410     va_end(args);   
411
412     // Call handler
413     LogErrorHandler(ContextID, ErrorCode, Buffer);
414 }
415
416 // Utility function to print signatures
417 void _cmsTagSignature2String(char String[5], cmsTagSignature sig)
418 {
419     cmsUInt32Number be;
420
421     // Convert to big endian
422     be = _cmsAdjustEndianess32((cmsUInt32Number) sig);
423
424     // Move chars 
425     memmove(String, &be, 4);
426
427     // Make sure of terminator
428     String[4] = 0;
429 }
430