1 //---------------------------------------------------------------------------------
2 //
3 // Little Color Management System
4 // Copyright (c) 1998-2016 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
27 #include "lcms2_internal.h"
28
29
30 // ----------------------------------------------------------------------------------
31 // Encoding & Decoding support functions
32 // ----------------------------------------------------------------------------------
33
34 // Little-Endian to Big-Endian
35
36 // Adjust a word value after being readed/ before being written from/to an ICC profile
_cmsAdjustEndianess16(cmsUInt16Number Word)37 cmsUInt16Number CMSEXPORT _cmsAdjustEndianess16(cmsUInt16Number Word)
38 {
39 #ifndef CMS_USE_BIG_ENDIAN
40
41 cmsUInt8Number* pByte = (cmsUInt8Number*) &Word;
42 cmsUInt8Number tmp;
43
44 tmp = pByte[0];
45 pByte[0] = pByte[1];
46 pByte[1] = tmp;
47 #endif
48
49 return Word;
50 }
51
52
53 // Transports to properly encoded values - note that icc profiles does use big endian notation.
54
55 // 1 2 3 4
56 // 4 3 2 1
57
_cmsAdjustEndianess32(cmsUInt32Number DWord)58 cmsUInt32Number CMSEXPORT _cmsAdjustEndianess32(cmsUInt32Number DWord)
59 {
60 #ifndef CMS_USE_BIG_ENDIAN
61
62 cmsUInt8Number* pByte = (cmsUInt8Number*) &DWord;
63 cmsUInt8Number temp1;
64 cmsUInt8Number temp2;
65
66 temp1 = *pByte++;
67 temp2 = *pByte++;
68 *(pByte-1) = *pByte;
69 *pByte++ = temp2;
70 *(pByte-3) = *pByte;
71 *pByte = temp1;
72 #endif
73 return DWord;
74 }
75
76 // 1 2 3 4 5 6 7 8
77 // 8 7 6 5 4 3 2 1
78
_cmsAdjustEndianess64(cmsUInt64Number * Result,cmsUInt64Number * QWord)79 void CMSEXPORT _cmsAdjustEndianess64(cmsUInt64Number* Result, cmsUInt64Number* QWord)
80 {
81
82 #ifndef CMS_USE_BIG_ENDIAN
83
84 cmsUInt8Number* pIn = (cmsUInt8Number*) QWord;
85 cmsUInt8Number* pOut = (cmsUInt8Number*) Result;
86
87 _cmsAssert(Result != NULL);
88
89 pOut[7] = pIn[0];
90 pOut[6] = pIn[1];
91 pOut[5] = pIn[2];
92 pOut[4] = pIn[3];
93 pOut[3] = pIn[4];
94 pOut[2] = pIn[5];
95 pOut[1] = pIn[6];
96 pOut[0] = pIn[7];
97
98 #else
99 _cmsAssert(Result != NULL);
100
101 # ifdef CMS_DONT_USE_INT64
102 (*Result)[0] = QWord[0];
103 (*Result)[1] = QWord[1];
104 # else
105 *Result = *QWord;
106 # endif
107 #endif
108 }
109
110 // Auxiliary -- read 8, 16 and 32-bit numbers
_cmsReadUInt8Number(cmsIOHANDLER * io,cmsUInt8Number * n)111 cmsBool CMSEXPORT _cmsReadUInt8Number(cmsIOHANDLER* io, cmsUInt8Number* n)
112 {
113 cmsUInt8Number tmp;
114
115 _cmsAssert(io != NULL);
116
117 if (io -> Read(io, &tmp, sizeof(cmsUInt8Number), 1) != 1)
118 return FALSE;
119
120 if (n != NULL) *n = tmp;
121 return TRUE;
122 }
123
_cmsReadUInt16Number(cmsIOHANDLER * io,cmsUInt16Number * n)124 cmsBool CMSEXPORT _cmsReadUInt16Number(cmsIOHANDLER* io, cmsUInt16Number* n)
125 {
126 cmsUInt16Number tmp;
127
128 _cmsAssert(io != NULL);
129
130 if (io -> Read(io, &tmp, sizeof(cmsUInt16Number), 1) != 1)
131 return FALSE;
132
133 if (n != NULL) *n = _cmsAdjustEndianess16(tmp);
134 return TRUE;
135 }
136
_cmsReadUInt16Array(cmsIOHANDLER * io,cmsUInt32Number n,cmsUInt16Number * Array)137 cmsBool CMSEXPORT _cmsReadUInt16Array(cmsIOHANDLER* io, cmsUInt32Number n, cmsUInt16Number* Array)
138 {
139 cmsUInt32Number i;
140
141 _cmsAssert(io != NULL);
142
143 for (i=0; i < n; i++) {
144
145 if (Array != NULL) {
146 if (!_cmsReadUInt16Number(io, Array + i)) return FALSE;
147 }
148 else {
149 if (!_cmsReadUInt16Number(io, NULL)) return FALSE;
150 }
151
152 }
153 return TRUE;
154 }
155
_cmsReadUInt32Number(cmsIOHANDLER * io,cmsUInt32Number * n)156 cmsBool CMSEXPORT _cmsReadUInt32Number(cmsIOHANDLER* io, cmsUInt32Number* n)
157 {
158 cmsUInt32Number tmp;
159
160 _cmsAssert(io != NULL);
161
162 if (io -> Read(io, &tmp, sizeof(cmsUInt32Number), 1) != 1)
163 return FALSE;
164
165 if (n != NULL) *n = _cmsAdjustEndianess32(tmp);
166 return TRUE;
167 }
168
_cmsReadFloat32Number(cmsIOHANDLER * io,cmsFloat32Number * n)169 cmsBool CMSEXPORT _cmsReadFloat32Number(cmsIOHANDLER* io, cmsFloat32Number* n)
170 {
171 cmsUInt32Number tmp;
172
173 _cmsAssert(io != NULL);
174
175 if (io -> Read(io, &tmp, sizeof(cmsUInt32Number), 1) != 1)
176 return FALSE;
177
178 if (n != NULL) {
179
180 tmp = _cmsAdjustEndianess32(tmp);
181 *n = *(cmsFloat32Number*) (void*) &tmp;
182 }
183 return TRUE;
184 }
185
186
_cmsReadUInt64Number(cmsIOHANDLER * io,cmsUInt64Number * n)187 cmsBool CMSEXPORT _cmsReadUInt64Number(cmsIOHANDLER* io, cmsUInt64Number* n)
188 {
189 cmsUInt64Number tmp;
190
191 _cmsAssert(io != NULL);
192
193 if (io -> Read(io, &tmp, sizeof(cmsUInt64Number), 1) != 1)
194 return FALSE;
195
196 if (n != NULL) _cmsAdjustEndianess64(n, &tmp);
197 return TRUE;
198 }
199
200
_cmsRead15Fixed16Number(cmsIOHANDLER * io,cmsFloat64Number * n)201 cmsBool CMSEXPORT _cmsRead15Fixed16Number(cmsIOHANDLER* io, cmsFloat64Number* n)
202 {
203 cmsUInt32Number tmp;
204
205 _cmsAssert(io != NULL);
206
207 if (io -> Read(io, &tmp, sizeof(cmsUInt32Number), 1) != 1)
208 return FALSE;
209
210 if (n != NULL) {
211 *n = _cms15Fixed16toDouble(_cmsAdjustEndianess32(tmp));
212 }
213
214 return TRUE;
215 }
216
217
_cmsReadXYZNumber(cmsIOHANDLER * io,cmsCIEXYZ * XYZ)218 cmsBool CMSEXPORT _cmsReadXYZNumber(cmsIOHANDLER* io, cmsCIEXYZ* XYZ)
219 {
220 cmsEncodedXYZNumber xyz;
221
222 _cmsAssert(io != NULL);
223
224 if (io ->Read(io, &xyz, sizeof(cmsEncodedXYZNumber), 1) != 1) return FALSE;
225
226 if (XYZ != NULL) {
227
228 XYZ->X = _cms15Fixed16toDouble(_cmsAdjustEndianess32(xyz.X));
229 XYZ->Y = _cms15Fixed16toDouble(_cmsAdjustEndianess32(xyz.Y));
230 XYZ->Z = _cms15Fixed16toDouble(_cmsAdjustEndianess32(xyz.Z));
231 }
232 return TRUE;
233 }
234
_cmsWriteUInt8Number(cmsIOHANDLER * io,cmsUInt8Number n)235 cmsBool CMSEXPORT _cmsWriteUInt8Number(cmsIOHANDLER* io, cmsUInt8Number n)
236 {
237 _cmsAssert(io != NULL);
238
239 if (io -> Write(io, sizeof(cmsUInt8Number), &n) != 1)
240 return FALSE;
241
242 return TRUE;
243 }
244
_cmsWriteUInt16Number(cmsIOHANDLER * io,cmsUInt16Number n)245 cmsBool CMSEXPORT _cmsWriteUInt16Number(cmsIOHANDLER* io, cmsUInt16Number n)
246 {
247 cmsUInt16Number tmp;
248
249 _cmsAssert(io != NULL);
250
251 tmp = _cmsAdjustEndianess16(n);
252 if (io -> Write(io, sizeof(cmsUInt16Number), &tmp) != 1)
253 return FALSE;
254
255 return TRUE;
256 }
257
_cmsWriteUInt16Array(cmsIOHANDLER * io,cmsUInt32Number n,const cmsUInt16Number * Array)258 cmsBool CMSEXPORT _cmsWriteUInt16Array(cmsIOHANDLER* io, cmsUInt32Number n, const cmsUInt16Number* Array)
259 {
260 cmsUInt32Number i;
261
262 _cmsAssert(io != NULL);
263 _cmsAssert(Array != NULL);
264
265 for (i=0; i < n; i++) {
266 if (!_cmsWriteUInt16Number(io, Array[i])) return FALSE;
267 }
268
269 return TRUE;
270 }
271
_cmsWriteUInt32Number(cmsIOHANDLER * io,cmsUInt32Number n)272 cmsBool CMSEXPORT _cmsWriteUInt32Number(cmsIOHANDLER* io, cmsUInt32Number n)
273 {
274 cmsUInt32Number tmp;
275
276 _cmsAssert(io != NULL);
277
278 tmp = _cmsAdjustEndianess32(n);
279 if (io -> Write(io, sizeof(cmsUInt32Number), &tmp) != 1)
280 return FALSE;
281
282 return TRUE;
283 }
284
285
_cmsWriteFloat32Number(cmsIOHANDLER * io,cmsFloat32Number n)286 cmsBool CMSEXPORT _cmsWriteFloat32Number(cmsIOHANDLER* io, cmsFloat32Number n)
287 {
288 cmsUInt32Number tmp;
289
290 _cmsAssert(io != NULL);
291
292 tmp = *(cmsUInt32Number*) (void*) &n;
293 tmp = _cmsAdjustEndianess32(tmp);
294 if (io -> Write(io, sizeof(cmsUInt32Number), &tmp) != 1)
295 return FALSE;
296
297 return TRUE;
298 }
299
_cmsWriteUInt64Number(cmsIOHANDLER * io,cmsUInt64Number * n)300 cmsBool CMSEXPORT _cmsWriteUInt64Number(cmsIOHANDLER* io, cmsUInt64Number* n)
301 {
302 cmsUInt64Number tmp;
303
304 _cmsAssert(io != NULL);
305
306 _cmsAdjustEndianess64(&tmp, n);
307 if (io -> Write(io, sizeof(cmsUInt64Number), &tmp) != 1)
308 return FALSE;
309
310 return TRUE;
311 }
312
_cmsWrite15Fixed16Number(cmsIOHANDLER * io,cmsFloat64Number n)313 cmsBool CMSEXPORT _cmsWrite15Fixed16Number(cmsIOHANDLER* io, cmsFloat64Number n)
314 {
315 cmsUInt32Number tmp;
316
317 _cmsAssert(io != NULL);
318
319 tmp = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(n));
320 if (io -> Write(io, sizeof(cmsUInt32Number), &tmp) != 1)
321 return FALSE;
322
323 return TRUE;
324 }
325
_cmsWriteXYZNumber(cmsIOHANDLER * io,const cmsCIEXYZ * XYZ)326 cmsBool CMSEXPORT _cmsWriteXYZNumber(cmsIOHANDLER* io, const cmsCIEXYZ* XYZ)
327 {
328 cmsEncodedXYZNumber xyz;
329
330 _cmsAssert(io != NULL);
331 _cmsAssert(XYZ != NULL);
332
333 xyz.X = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(XYZ->X));
334 xyz.Y = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(XYZ->Y));
335 xyz.Z = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(XYZ->Z));
336
337 return io -> Write(io, sizeof(cmsEncodedXYZNumber), &xyz);
338 }
339
340 // from Fixed point 8.8 to double
_cms8Fixed8toDouble(cmsUInt16Number fixed8)341 cmsFloat64Number CMSEXPORT _cms8Fixed8toDouble(cmsUInt16Number fixed8)
342 {
343 cmsUInt8Number msb, lsb;
344
345 lsb = (cmsUInt8Number) (fixed8 & 0xff);
346 msb = (cmsUInt8Number) (((cmsUInt16Number) fixed8 >> 8) & 0xff);
347
348 return (cmsFloat64Number) ((cmsFloat64Number) msb + ((cmsFloat64Number) lsb / 256.0));
349 }
350
_cmsDoubleTo8Fixed8(cmsFloat64Number val)351 cmsUInt16Number CMSEXPORT _cmsDoubleTo8Fixed8(cmsFloat64Number val)
352 {
353 cmsS15Fixed16Number GammaFixed32 = _cmsDoubleTo15Fixed16(val);
354 return (cmsUInt16Number) ((GammaFixed32 >> 8) & 0xFFFF);
355 }
356
357 // from Fixed point 15.16 to double
_cms15Fixed16toDouble(cmsS15Fixed16Number fix32)358 cmsFloat64Number CMSEXPORT _cms15Fixed16toDouble(cmsS15Fixed16Number fix32)
359 {
360 cmsFloat64Number floater, sign, mid;
361 int Whole, FracPart;
362
363 sign = (fix32 < 0 ? -1 : 1);
364 fix32 = abs(fix32);
365
366 Whole = (cmsUInt16Number)(fix32 >> 16) & 0xffff;
367 FracPart = (cmsUInt16Number)(fix32 & 0xffff);
368
369 mid = (cmsFloat64Number) FracPart / 65536.0;
370 floater = (cmsFloat64Number) Whole + mid;
371
372 return sign * floater;
373 }
374
375 // from double to Fixed point 15.16
_cmsDoubleTo15Fixed16(cmsFloat64Number v)376 cmsS15Fixed16Number CMSEXPORT _cmsDoubleTo15Fixed16(cmsFloat64Number v)
377 {
378 return ((cmsS15Fixed16Number) floor((v)*65536.0 + 0.5));
379 }
380
381 // Date/Time functions
382
_cmsDecodeDateTimeNumber(const cmsDateTimeNumber * Source,struct tm * Dest)383 void CMSEXPORT _cmsDecodeDateTimeNumber(const cmsDateTimeNumber *Source, struct tm *Dest)
384 {
385
386 _cmsAssert(Dest != NULL);
387 _cmsAssert(Source != NULL);
388
389 Dest->tm_sec = _cmsAdjustEndianess16(Source->seconds);
390 Dest->tm_min = _cmsAdjustEndianess16(Source->minutes);
391 Dest->tm_hour = _cmsAdjustEndianess16(Source->hours);
392 Dest->tm_mday = _cmsAdjustEndianess16(Source->day);
393 Dest->tm_mon = _cmsAdjustEndianess16(Source->month) - 1;
394 Dest->tm_year = _cmsAdjustEndianess16(Source->year) - 1900;
395 Dest->tm_wday = -1;
396 Dest->tm_yday = -1;
397 Dest->tm_isdst = 0;
398 }
399
_cmsEncodeDateTimeNumber(cmsDateTimeNumber * Dest,const struct tm * Source)400 void CMSEXPORT _cmsEncodeDateTimeNumber(cmsDateTimeNumber *Dest, const struct tm *Source)
401 {
402 _cmsAssert(Dest != NULL);
403 _cmsAssert(Source != NULL);
404
405 Dest->seconds = _cmsAdjustEndianess16((cmsUInt16Number) Source->tm_sec);
406 Dest->minutes = _cmsAdjustEndianess16((cmsUInt16Number) Source->tm_min);
407 Dest->hours = _cmsAdjustEndianess16((cmsUInt16Number) Source->tm_hour);
408 Dest->day = _cmsAdjustEndianess16((cmsUInt16Number) Source->tm_mday);
409 Dest->month = _cmsAdjustEndianess16((cmsUInt16Number) (Source->tm_mon + 1));
410 Dest->year = _cmsAdjustEndianess16((cmsUInt16Number) (Source->tm_year + 1900));
411 }
412
413 // Read base and return type base
_cmsReadTypeBase(cmsIOHANDLER * io)414 cmsTagTypeSignature CMSEXPORT _cmsReadTypeBase(cmsIOHANDLER* io)
415 {
416 _cmsTagBase Base;
417
418 _cmsAssert(io != NULL);
419
420 if (io -> Read(io, &Base, sizeof(_cmsTagBase), 1) != 1)
421 return (cmsTagTypeSignature) 0;
422
423 return (cmsTagTypeSignature) _cmsAdjustEndianess32(Base.sig);
424 }
425
426 // Setup base marker
_cmsWriteTypeBase(cmsIOHANDLER * io,cmsTagTypeSignature sig)427 cmsBool CMSEXPORT _cmsWriteTypeBase(cmsIOHANDLER* io, cmsTagTypeSignature sig)
428 {
429 _cmsTagBase Base;
430
431 _cmsAssert(io != NULL);
432
433 Base.sig = (cmsTagTypeSignature) _cmsAdjustEndianess32(sig);
434 memset(&Base.reserved, 0, sizeof(Base.reserved));
435 return io -> Write(io, sizeof(_cmsTagBase), &Base);
436 }
437
_cmsReadAlignment(cmsIOHANDLER * io)438 cmsBool CMSEXPORT _cmsReadAlignment(cmsIOHANDLER* io)
439 {
440 cmsUInt8Number Buffer[4];
441 cmsUInt32Number NextAligned, At;
442 cmsUInt32Number BytesToNextAlignedPos;
443
444 _cmsAssert(io != NULL);
445
446 At = io -> Tell(io);
447 NextAligned = _cmsALIGNLONG(At);
448 BytesToNextAlignedPos = NextAligned - At;
449 if (BytesToNextAlignedPos == 0) return TRUE;
450 if (BytesToNextAlignedPos > 4) return FALSE;
451
452 return (io ->Read(io, Buffer, BytesToNextAlignedPos, 1) == 1);
453 }
454
_cmsWriteAlignment(cmsIOHANDLER * io)455 cmsBool CMSEXPORT _cmsWriteAlignment(cmsIOHANDLER* io)
456 {
457 cmsUInt8Number Buffer[4];
458 cmsUInt32Number NextAligned, At;
459 cmsUInt32Number BytesToNextAlignedPos;
460
461 _cmsAssert(io != NULL);
462
463 At = io -> Tell(io);
464 NextAligned = _cmsALIGNLONG(At);
465 BytesToNextAlignedPos = NextAligned - At;
466 if (BytesToNextAlignedPos == 0) return TRUE;
467 if (BytesToNextAlignedPos > 4) return FALSE;
468
469 memset(Buffer, 0, BytesToNextAlignedPos);
470 return io -> Write(io, BytesToNextAlignedPos, Buffer);
471 }
472
473
474 // To deal with text streams. 2K at most
_cmsIOPrintf(cmsIOHANDLER * io,const char * frm,...)475 cmsBool CMSEXPORT _cmsIOPrintf(cmsIOHANDLER* io, const char* frm, ...)
476 {
477 va_list args;
478 int len;
479 cmsUInt8Number Buffer[2048];
480 cmsBool rc;
481
482 _cmsAssert(io != NULL);
483 _cmsAssert(frm != NULL);
484
485 va_start(args, frm);
486
487 len = vsnprintf((char*) Buffer, 2047, frm, args);
488 if (len < 0) {
489 va_end(args);
490 return FALSE; // Truncated, which is a fatal error for us
491 }
492
493 rc = io ->Write(io, len, Buffer);
494
495 va_end(args);
496
497 return rc;
498 }
499
500
501 // Plugin memory management -------------------------------------------------------------------------------------------------
502
503 // Specialized malloc for plug-ins, that is freed upon exit.
_cmsPluginMalloc(cmsContext ContextID,cmsUInt32Number size)504 void* _cmsPluginMalloc(cmsContext ContextID, cmsUInt32Number size)
505 {
506 struct _cmsContext_struct* ctx = _cmsGetContext(ContextID);
507
508 if (ctx ->MemPool == NULL) {
509
510 if (ContextID == NULL) {
511
512 ctx->MemPool = _cmsCreateSubAlloc(0, 2*1024);
513 if (ctx->MemPool == NULL) return NULL;
514 }
515 else {
516 cmsSignalError(ContextID, cmsERROR_CORRUPTION_DETECTED, "NULL memory pool on context");
517 return NULL;
518 }
519 }
520
521 return _cmsSubAlloc(ctx->MemPool, size);
522 }
523
524
525 // Main plug-in dispatcher
cmsPlugin(void * Plug_in)526 cmsBool CMSEXPORT cmsPlugin(void* Plug_in)
527 {
528 return cmsPluginTHR(NULL, Plug_in);
529 }
530
cmsPluginTHR(cmsContext id,void * Plug_in)531 cmsBool CMSEXPORT cmsPluginTHR(cmsContext id, void* Plug_in)
532 {
533 cmsPluginBase* Plugin;
534
535 for (Plugin = (cmsPluginBase*) Plug_in;
536 Plugin != NULL;
537 Plugin = Plugin -> Next) {
538
539 if (Plugin -> Magic != cmsPluginMagicNumber) {
540 cmsSignalError(id, cmsERROR_UNKNOWN_EXTENSION, "Unrecognized plugin");
541 return FALSE;
542 }
543
544 if (Plugin ->ExpectedVersion > LCMS_VERSION) {
545 cmsSignalError(id, cmsERROR_UNKNOWN_EXTENSION, "plugin needs Little CMS %d, current version is %d",
546 Plugin ->ExpectedVersion, LCMS_VERSION);
547 return FALSE;
548 }
549
550 switch (Plugin -> Type) {
551
552 case cmsPluginMemHandlerSig:
553 if (!_cmsRegisterMemHandlerPlugin(id, Plugin)) return FALSE;
554 break;
555
556 case cmsPluginInterpolationSig:
557 if (!_cmsRegisterInterpPlugin(id, Plugin)) return FALSE;
558 break;
559
560 case cmsPluginTagTypeSig:
561 if (!_cmsRegisterTagTypePlugin(id, Plugin)) return FALSE;
562 break;
563
564 case cmsPluginTagSig:
565 if (!_cmsRegisterTagPlugin(id, Plugin)) return FALSE;
566 break;
567
568 case cmsPluginFormattersSig:
569 if (!_cmsRegisterFormattersPlugin(id, Plugin)) return FALSE;
570 break;
571
572 case cmsPluginRenderingIntentSig:
573 if (!_cmsRegisterRenderingIntentPlugin(id, Plugin)) return FALSE;
574 break;
575
576 case cmsPluginParametricCurveSig:
577 if (!_cmsRegisterParametricCurvesPlugin(id, Plugin)) return FALSE;
578 break;
579
580 case cmsPluginMultiProcessElementSig:
581 if (!_cmsRegisterMultiProcessElementPlugin(id, Plugin)) return FALSE;
582 break;
583
584 case cmsPluginOptimizationSig:
585 if (!_cmsRegisterOptimizationPlugin(id, Plugin)) return FALSE;
586 break;
587
588 case cmsPluginTransformSig:
589 if (!_cmsRegisterTransformPlugin(id, Plugin)) return FALSE;
590 break;
591
592 case cmsPluginMutexSig:
593 if (!_cmsRegisterMutexPlugin(id, Plugin)) return FALSE;
594 break;
595
596 default:
597 cmsSignalError(id, cmsERROR_UNKNOWN_EXTENSION, "Unrecognized plugin type '%X'", Plugin -> Type);
598 return FALSE;
599 }
600 }
601
602 // Keep a reference to the plug-in
603 return TRUE;
604 }
605
606
607 // Revert all plug-ins to default
cmsUnregisterPlugins(void)608 void CMSEXPORT cmsUnregisterPlugins(void)
609 {
610 cmsUnregisterPluginsTHR(NULL);
611 }
612
613
614 // The Global storage for system context. This is the one and only global variable
615 // pointers structure. All global vars are referenced here.
616 static struct _cmsContext_struct globalContext = {
617
618 NULL, // Not in the linked list
619 NULL, // No suballocator
620 {
621 NULL, // UserPtr,
622 &_cmsLogErrorChunk, // Logger,
623 &_cmsAlarmCodesChunk, // AlarmCodes,
624 &_cmsAdaptationStateChunk, // AdaptationState,
625 &_cmsMemPluginChunk, // MemPlugin,
626 &_cmsInterpPluginChunk, // InterpPlugin,
627 &_cmsCurvesPluginChunk, // CurvesPlugin,
628 &_cmsFormattersPluginChunk, // FormattersPlugin,
629 &_cmsTagTypePluginChunk, // TagTypePlugin,
630 &_cmsTagPluginChunk, // TagPlugin,
631 &_cmsIntentsPluginChunk, // IntentPlugin,
632 &_cmsMPETypePluginChunk, // MPEPlugin,
633 &_cmsOptimizationPluginChunk, // OptimizationPlugin,
634 &_cmsTransformPluginChunk, // TransformPlugin,
635 &_cmsMutexPluginChunk // MutexPlugin
636 },
637
638 { NULL, NULL, NULL, NULL, NULL, NULL } // The default memory allocator is not used for context 0
639 };
640
641
642 // The context pool (linked list head)
643 static _cmsMutex _cmsContextPoolHeadMutex = CMS_MUTEX_INITIALIZER;
644 static struct _cmsContext_struct* _cmsContextPoolHead = NULL;
645
646 // Internal, get associated pointer, with guessing. Never returns NULL.
_cmsGetContext(cmsContext ContextID)647 struct _cmsContext_struct* _cmsGetContext(cmsContext ContextID)
648 {
649 struct _cmsContext_struct* id = (struct _cmsContext_struct*) ContextID;
650 struct _cmsContext_struct* ctx;
651
652
653 // On 0, use global settings
654 if (id == NULL)
655 return &globalContext;
656
657 // Search
658 for (ctx = _cmsContextPoolHead;
659 ctx != NULL;
660 ctx = ctx ->Next) {
661
662 // Found it?
663 if (id == ctx)
664 return ctx; // New-style context,
665 }
666
667 return &globalContext;
668 }
669
670
671 // Internal: get the memory area associanted with each context client
672 // Returns the block assigned to the specific zone. Never return NULL.
_cmsContextGetClientChunk(cmsContext ContextID,_cmsMemoryClient mc)673 void* _cmsContextGetClientChunk(cmsContext ContextID, _cmsMemoryClient mc)
674 {
675 struct _cmsContext_struct* ctx;
676 void *ptr;
677
678 if ((int) mc < 0 || mc >= MemoryClientMax) {
679
680 cmsSignalError(ContextID, cmsERROR_INTERNAL, "Bad context client -- possible corruption");
681
682 // This is catastrophic. Should never reach here
683 _cmsAssert(0);
684
685 // Reverts to global context
686 return globalContext.chunks[UserPtr];
687 }
688
689 ctx = _cmsGetContext(ContextID);
690 ptr = ctx ->chunks[mc];
691
692 if (ptr != NULL)
693 return ptr;
694
695 // A null ptr means no special settings for that context, and this
696 // reverts to Context0 globals
697 return globalContext.chunks[mc];
698 }
699
700
701 // This function returns the given context its default pristine state,
702 // as no plug-ins were declared. There is no way to unregister a single
703 // plug-in, as a single call to cmsPluginTHR() function may register
704 // many different plug-ins simultaneously, then there is no way to
705 // identify which plug-in to unregister.
cmsUnregisterPluginsTHR(cmsContext ContextID)706 void CMSEXPORT cmsUnregisterPluginsTHR(cmsContext ContextID)
707 {
708 _cmsRegisterMemHandlerPlugin(ContextID, NULL);
709 _cmsRegisterInterpPlugin(ContextID, NULL);
710 _cmsRegisterTagTypePlugin(ContextID, NULL);
711 _cmsRegisterTagPlugin(ContextID, NULL);
712 _cmsRegisterFormattersPlugin(ContextID, NULL);
713 _cmsRegisterRenderingIntentPlugin(ContextID, NULL);
714 _cmsRegisterParametricCurvesPlugin(ContextID, NULL);
715 _cmsRegisterMultiProcessElementPlugin(ContextID, NULL);
716 _cmsRegisterOptimizationPlugin(ContextID, NULL);
717 _cmsRegisterTransformPlugin(ContextID, NULL);
718 _cmsRegisterMutexPlugin(ContextID, NULL);
719 }
720
721
722 // Returns the memory manager plug-in, if any, from the Plug-in bundle
723 static
_cmsFindMemoryPlugin(void * PluginBundle)724 cmsPluginMemHandler* _cmsFindMemoryPlugin(void* PluginBundle)
725 {
726 cmsPluginBase* Plugin;
727
728 for (Plugin = (cmsPluginBase*) PluginBundle;
729 Plugin != NULL;
730 Plugin = Plugin -> Next) {
731
732 if (Plugin -> Magic == cmsPluginMagicNumber &&
733 Plugin -> ExpectedVersion <= LCMS_VERSION &&
734 Plugin -> Type == cmsPluginMemHandlerSig) {
735
736 // Found!
737 return (cmsPluginMemHandler*) Plugin;
738 }
739 }
740
741 // Nope, revert to defaults
742 return NULL;
743 }
744
745
746 // Creates a new context with optional associated plug-ins. Caller may also specify an optional pointer to user-defined
747 // data that will be forwarded to plug-ins and logger.
cmsCreateContext(void * Plugin,void * UserData)748 cmsContext CMSEXPORT cmsCreateContext(void* Plugin, void* UserData)
749 {
750 struct _cmsContext_struct* ctx;
751 struct _cmsContext_struct fakeContext;
752
753 _cmsInstallAllocFunctions(_cmsFindMemoryPlugin(Plugin), &fakeContext.DefaultMemoryManager);
754
755 fakeContext.chunks[UserPtr] = UserData;
756 fakeContext.chunks[MemPlugin] = &fakeContext.DefaultMemoryManager;
757
758 // Create the context structure.
759 ctx = (struct _cmsContext_struct*) _cmsMalloc(&fakeContext, sizeof(struct _cmsContext_struct));
760 if (ctx == NULL)
761 return NULL; // Something very wrong happened!
762
763 // Init the structure and the memory manager
764 memset(ctx, 0, sizeof(struct _cmsContext_struct));
765
766 // Keep memory manager
767 memcpy(&ctx->DefaultMemoryManager, &fakeContext.DefaultMemoryManager, sizeof(_cmsMemPluginChunk));
768
769 // Maintain the linked list (with proper locking)
770 _cmsEnterCriticalSectionPrimitive(&_cmsContextPoolHeadMutex);
771 ctx ->Next = _cmsContextPoolHead;
772 _cmsContextPoolHead = ctx;
773 _cmsLeaveCriticalSectionPrimitive(&_cmsContextPoolHeadMutex);
774
775 ctx ->chunks[UserPtr] = UserData;
776 ctx ->chunks[MemPlugin] = &ctx->DefaultMemoryManager;
777
778 // Now we can allocate the pool by using default memory manager
779 ctx ->MemPool = _cmsCreateSubAlloc(ctx, 22 * sizeof(void*)); // default size about 32 pointers
780 if (ctx ->MemPool == NULL) {
781
782 cmsDeleteContext(ctx);
783 return NULL;
784 }
785
786 _cmsAllocLogErrorChunk(ctx, NULL);
787 _cmsAllocAlarmCodesChunk(ctx, NULL);
788 _cmsAllocAdaptationStateChunk(ctx, NULL);
789 _cmsAllocMemPluginChunk(ctx, NULL);
790 _cmsAllocInterpPluginChunk(ctx, NULL);
791 _cmsAllocCurvesPluginChunk(ctx, NULL);
792 _cmsAllocFormattersPluginChunk(ctx, NULL);
793 _cmsAllocTagTypePluginChunk(ctx, NULL);
794 _cmsAllocMPETypePluginChunk(ctx, NULL);
795 _cmsAllocTagPluginChunk(ctx, NULL);
796 _cmsAllocIntentsPluginChunk(ctx, NULL);
797 _cmsAllocOptimizationPluginChunk(ctx, NULL);
798 _cmsAllocTransformPluginChunk(ctx, NULL);
799 _cmsAllocMutexPluginChunk(ctx, NULL);
800
801 // Setup the plug-ins
802 if (!cmsPluginTHR(ctx, Plugin)) {
803
804 cmsDeleteContext(ctx);
805 return NULL;
806 }
807
808 return (cmsContext) ctx;
809 }
810
811 // Duplicates a context with all associated plug-ins.
812 // Caller may specify an optional pointer to user-defined
813 // data that will be forwarded to plug-ins and logger.
cmsDupContext(cmsContext ContextID,void * NewUserData)814 cmsContext CMSEXPORT cmsDupContext(cmsContext ContextID, void* NewUserData)
815 {
816 int i;
817 struct _cmsContext_struct* ctx;
818 const struct _cmsContext_struct* src = _cmsGetContext(ContextID);
819
820 void* userData = (NewUserData != NULL) ? NewUserData : src -> chunks[UserPtr];
821
822
823 ctx = (struct _cmsContext_struct*) _cmsMalloc(ContextID, sizeof(struct _cmsContext_struct));
824 if (ctx == NULL)
825 return NULL; // Something very wrong happened
826
827 // Setup default memory allocators
828 memcpy(&ctx->DefaultMemoryManager, &src->DefaultMemoryManager, sizeof(ctx->DefaultMemoryManager));
829
830 // Maintain the linked list
831 _cmsEnterCriticalSectionPrimitive(&_cmsContextPoolHeadMutex);
832 ctx ->Next = _cmsContextPoolHead;
833 _cmsContextPoolHead = ctx;
834 _cmsLeaveCriticalSectionPrimitive(&_cmsContextPoolHeadMutex);
835
836 ctx ->chunks[UserPtr] = userData;
837 ctx ->chunks[MemPlugin] = &ctx->DefaultMemoryManager;
838
839 ctx ->MemPool = _cmsCreateSubAlloc(ctx, 22 * sizeof(void*));
840 if (ctx ->MemPool == NULL) {
841
842 cmsDeleteContext(ctx);
843 return NULL;
844 }
845
846 // Allocate all required chunks.
847 _cmsAllocLogErrorChunk(ctx, src);
848 _cmsAllocAlarmCodesChunk(ctx, src);
849 _cmsAllocAdaptationStateChunk(ctx, src);
850 _cmsAllocMemPluginChunk(ctx, src);
851 _cmsAllocInterpPluginChunk(ctx, src);
852 _cmsAllocCurvesPluginChunk(ctx, src);
853 _cmsAllocFormattersPluginChunk(ctx, src);
854 _cmsAllocTagTypePluginChunk(ctx, src);
855 _cmsAllocMPETypePluginChunk(ctx, src);
856 _cmsAllocTagPluginChunk(ctx, src);
857 _cmsAllocIntentsPluginChunk(ctx, src);
858 _cmsAllocOptimizationPluginChunk(ctx, src);
859 _cmsAllocTransformPluginChunk(ctx, src);
860 _cmsAllocMutexPluginChunk(ctx, src);
861
862 // Make sure no one failed
863 for (i=Logger; i < MemoryClientMax; i++) {
864
865 if (src ->chunks[i] == NULL) {
866 cmsDeleteContext((cmsContext) ctx);
867 return NULL;
868 }
869 }
870
871 return (cmsContext) ctx;
872 }
873
874
875 /*
876 static
877 struct _cmsContext_struct* FindPrev(struct _cmsContext_struct* id)
878 {
879 struct _cmsContext_struct* prev;
880
881 // Search for previous
882 for (prev = _cmsContextPoolHead;
883 prev != NULL;
884 prev = prev ->Next)
885 {
886 if (prev ->Next == id)
887 return prev;
888 }
889
890 return NULL; // List is empty or only one element!
891 }
892 */
893
894 // Frees any resources associated with the given context,
895 // and destroys the context placeholder.
896 // The ContextID can no longer be used in any THR operation.
cmsDeleteContext(cmsContext ContextID)897 void CMSEXPORT cmsDeleteContext(cmsContext ContextID)
898 {
899 if (ContextID != NULL) {
900
901 struct _cmsContext_struct* ctx = (struct _cmsContext_struct*) ContextID;
902 struct _cmsContext_struct fakeContext;
903 struct _cmsContext_struct* prev;
904
905 memcpy(&fakeContext.DefaultMemoryManager, &ctx->DefaultMemoryManager, sizeof(ctx->DefaultMemoryManager));
906
907 fakeContext.chunks[UserPtr] = ctx ->chunks[UserPtr];
908 fakeContext.chunks[MemPlugin] = &fakeContext.DefaultMemoryManager;
909
910 // Get rid of plugins
911 cmsUnregisterPluginsTHR(ContextID);
912
913 // Since all memory is allocated in the private pool, all what we need to do is destroy the pool
914 if (ctx -> MemPool != NULL)
915 _cmsSubAllocDestroy(ctx ->MemPool);
916 ctx -> MemPool = NULL;
917
918 // Maintain list
919 _cmsEnterCriticalSectionPrimitive(&_cmsContextPoolHeadMutex);
920 if (_cmsContextPoolHead == ctx) {
921
922 _cmsContextPoolHead = ctx->Next;
923 }
924 else {
925
926 // Search for previous
927 for (prev = _cmsContextPoolHead;
928 prev != NULL;
929 prev = prev ->Next)
930 {
931 if (prev -> Next == ctx) {
932 prev -> Next = ctx ->Next;
933 break;
934 }
935 }
936 }
937 _cmsLeaveCriticalSectionPrimitive(&_cmsContextPoolHeadMutex);
938
939 // free the memory block itself
940 _cmsFree(&fakeContext, ctx);
941 }
942 }
943
944 // Returns the user data associated to the given ContextID, or NULL if no user data was attached on context creation
cmsGetContextUserData(cmsContext ContextID)945 void* CMSEXPORT cmsGetContextUserData(cmsContext ContextID)
946 {
947 return _cmsContextGetClientChunk(ContextID, UserPtr);
948 }
949
950
951