• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------------
2 //
3 //  Little Color Management System
4 //  Copyright (c) 1998-2023 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 // Multilocalized unicode objects. That is an attempt to encapsulate i18n.
30 
31 
32 // Allocates an empty multi localizad unicode object
cmsMLUalloc(cmsContext ContextID,cmsUInt32Number nItems)33 cmsMLU* CMSEXPORT cmsMLUalloc(cmsContext ContextID, cmsUInt32Number nItems)
34 {
35     cmsMLU* mlu;
36 
37     // nItems should be positive if given
38     if (nItems <= 0) nItems = 2;
39 
40     // Create the container
41     mlu = (cmsMLU*) _cmsMallocZero(ContextID, sizeof(cmsMLU));
42     if (mlu == NULL) return NULL;
43 
44     mlu ->ContextID = ContextID;
45 
46     // Create entry array
47     mlu ->Entries = (_cmsMLUentry*) _cmsCalloc(ContextID, nItems, sizeof(_cmsMLUentry));
48     if (mlu ->Entries == NULL) {
49         _cmsFree(ContextID, mlu);
50         return NULL;
51     }
52 
53     // Ok, keep indexes up to date
54     mlu ->AllocatedEntries    = nItems;
55     mlu ->UsedEntries         = 0;
56 
57     return mlu;
58 }
59 
60 
61 // Grows a mempool table for a MLU. Each time this function is called, mempool size is multiplied times two.
62 static
GrowMLUpool(cmsMLU * mlu)63 cmsBool GrowMLUpool(cmsMLU* mlu)
64 {
65     cmsUInt32Number size;
66     void *NewPtr;
67 
68     // Sanity check
69     if (mlu == NULL) return FALSE;
70 
71     if (mlu ->PoolSize == 0)
72         size = 256;
73     else
74         size = mlu ->PoolSize * 2;
75 
76     // Check for overflow
77     if (size < mlu ->PoolSize) return FALSE;
78 
79     // Reallocate the pool
80     NewPtr = _cmsRealloc(mlu ->ContextID, mlu ->MemPool, size);
81     if (NewPtr == NULL) return FALSE;
82 
83 
84     mlu ->MemPool  = NewPtr;
85     mlu ->PoolSize = size;
86 
87     return TRUE;
88 }
89 
90 
91 // Grows a entry table for a MLU. Each time this function is called, table size is multiplied times two.
92 static
GrowMLUtable(cmsMLU * mlu)93 cmsBool GrowMLUtable(cmsMLU* mlu)
94 {
95     cmsUInt32Number AllocatedEntries;
96     _cmsMLUentry *NewPtr;
97 
98     // Sanity check
99     if (mlu == NULL) return FALSE;
100 
101     AllocatedEntries = mlu ->AllocatedEntries * 2;
102 
103     // Check for overflow
104     if (AllocatedEntries / 2 != mlu ->AllocatedEntries) return FALSE;
105 
106     // Reallocate the memory
107     NewPtr = (_cmsMLUentry*)_cmsRealloc(mlu ->ContextID, mlu ->Entries, AllocatedEntries*sizeof(_cmsMLUentry));
108     if (NewPtr == NULL) return FALSE;
109 
110     mlu ->Entries          = NewPtr;
111     mlu ->AllocatedEntries = AllocatedEntries;
112 
113     return TRUE;
114 }
115 
116 
117 // Search for a specific entry in the structure. Language and Country are used.
118 static
SearchMLUEntry(cmsMLU * mlu,cmsUInt16Number LanguageCode,cmsUInt16Number CountryCode)119 int SearchMLUEntry(cmsMLU* mlu, cmsUInt16Number LanguageCode, cmsUInt16Number CountryCode)
120 {
121     cmsUInt32Number i;
122 
123     // Sanity check
124     if (mlu == NULL) return -1;
125 
126     // Iterate whole table
127     for (i=0; i < mlu ->UsedEntries; i++) {
128 
129         if (mlu ->Entries[i].Country  == CountryCode &&
130             mlu ->Entries[i].Language == LanguageCode) return (int) i;
131     }
132 
133     // Not found
134     return -1;
135 }
136 
137 // Add a block of characters to the intended MLU. Language and country are specified.
138 // Only one entry for Language/country pair is allowed.
139 static
AddMLUBlock(cmsMLU * mlu,cmsUInt32Number size,const wchar_t * Block,cmsUInt16Number LanguageCode,cmsUInt16Number CountryCode)140 cmsBool AddMLUBlock(cmsMLU* mlu, cmsUInt32Number size, const wchar_t *Block,
141                      cmsUInt16Number LanguageCode, cmsUInt16Number CountryCode)
142 {
143     cmsUInt32Number Offset;
144     cmsUInt8Number* Ptr;
145 
146     // Sanity check
147     if (mlu == NULL) return FALSE;
148 
149     // Is there any room available?
150     if (mlu ->UsedEntries >= mlu ->AllocatedEntries) {
151         if (!GrowMLUtable(mlu)) return FALSE;
152     }
153 
154     // Only one ASCII string
155     if (SearchMLUEntry(mlu, LanguageCode, CountryCode) >= 0) return FALSE;  // Only one  is allowed!
156 
157     // Check for size
158     while ((mlu ->PoolSize - mlu ->PoolUsed) < size) {
159 
160             if (!GrowMLUpool(mlu)) return FALSE;
161     }
162 
163     Offset = mlu ->PoolUsed;
164 
165     Ptr = (cmsUInt8Number*) mlu ->MemPool;
166     if (Ptr == NULL) return FALSE;
167 
168     // Set the entry
169     memmove(Ptr + Offset, Block, size);
170     mlu ->PoolUsed += size;
171 
172     mlu ->Entries[mlu ->UsedEntries].StrW     = Offset;
173     mlu ->Entries[mlu ->UsedEntries].Len      = size;
174     mlu ->Entries[mlu ->UsedEntries].Country  = CountryCode;
175     mlu ->Entries[mlu ->UsedEntries].Language = LanguageCode;
176     mlu ->UsedEntries++;
177 
178     return TRUE;
179 }
180 
181 // Convert from a 3-char code to a cmsUInt16Number. It is done in this way because some
182 // compilers don't properly align beginning of strings
183 static
strTo16(const char str[3])184 cmsUInt16Number strTo16(const char str[3])
185 {
186     const cmsUInt8Number* ptr8;
187     cmsUInt16Number n;
188 
189     // For non-existent strings
190     if (str == NULL) return 0;
191     ptr8 = (const cmsUInt8Number*)str;
192     n = (cmsUInt16Number)(((cmsUInt16Number)ptr8[0] << 8) | ptr8[1]);
193 
194     return n;
195 }
196 
197 static
strFrom16(char str[3],cmsUInt16Number n)198 void strFrom16(char str[3], cmsUInt16Number n)
199 {
200     str[0] = (char)(n >> 8);
201     str[1] = (char)n;
202     str[2] = (char)0;
203 
204 }
205 
206 // Add an ASCII entry. Do not add any \0 termination (ICC1v43_2010-12.pdf page 61)
207 // In the case the user explicitly sets an empty string, we force a \0
cmsMLUsetASCII(cmsMLU * mlu,const char LanguageCode[3],const char CountryCode[3],const char * ASCIIString)208 cmsBool CMSEXPORT cmsMLUsetASCII(cmsMLU* mlu, const char LanguageCode[3], const char CountryCode[3], const char* ASCIIString)
209 {
210     cmsUInt32Number i, len = (cmsUInt32Number) strlen(ASCIIString);
211     wchar_t* WStr;
212     cmsBool  rc;
213     cmsUInt16Number Lang  = strTo16(LanguageCode);
214     cmsUInt16Number Cntry = strTo16(CountryCode);
215 
216     if (mlu == NULL) return FALSE;
217 
218     // len == 0 would prevent operation, so we set a empty string pointing to zero
219     if (len == 0)
220     {
221         len = 1;
222     }
223 
224     WStr = (wchar_t*) _cmsCalloc(mlu ->ContextID, len,  sizeof(wchar_t));
225     if (WStr == NULL) return FALSE;
226 
227     for (i=0; i < len; i++)
228         WStr[i] = (wchar_t) ASCIIString[i];
229 
230     rc = AddMLUBlock(mlu, len  * sizeof(wchar_t), WStr, Lang, Cntry);
231 
232     _cmsFree(mlu ->ContextID, WStr);
233     return rc;
234 
235 }
236 
237 // We don't need any wcs support library
238 static
mywcslen(const wchar_t * s)239 cmsUInt32Number mywcslen(const wchar_t *s)
240 {
241     const wchar_t *p;
242 
243     p = s;
244     while (*p)
245         p++;
246 
247     return (cmsUInt32Number)(p - s);
248 }
249 
250 // Add a wide entry. Do not add any \0 terminator (ICC1v43_2010-12.pdf page 61)
cmsMLUsetWide(cmsMLU * mlu,const char Language[3],const char Country[3],const wchar_t * WideString)251 cmsBool  CMSEXPORT cmsMLUsetWide(cmsMLU* mlu, const char Language[3], const char Country[3], const wchar_t* WideString)
252 {
253     cmsUInt16Number Lang  = strTo16(Language);
254     cmsUInt16Number Cntry = strTo16(Country);
255     cmsUInt32Number len;
256 
257     if (mlu == NULL) return FALSE;
258     if (WideString == NULL) return FALSE;
259 
260     len = (cmsUInt32Number) (mywcslen(WideString)) * sizeof(wchar_t);
261     if (len == 0)
262         len = sizeof(wchar_t);
263 
264     return AddMLUBlock(mlu, len, WideString, Lang, Cntry);
265 }
266 
267 // Duplicating a MLU is as easy as copying all members
cmsMLUdup(const cmsMLU * mlu)268 cmsMLU* CMSEXPORT cmsMLUdup(const cmsMLU* mlu)
269 {
270     cmsMLU* NewMlu = NULL;
271 
272     // Duplicating a NULL obtains a NULL
273     if (mlu == NULL) return NULL;
274 
275     NewMlu = cmsMLUalloc(mlu ->ContextID, mlu ->UsedEntries);
276     if (NewMlu == NULL) return NULL;
277 
278     // Should never happen
279     if (NewMlu ->AllocatedEntries < mlu ->UsedEntries)
280         goto Error;
281 
282     // Sanitize...
283     if (NewMlu ->Entries == NULL || mlu ->Entries == NULL)  goto Error;
284 
285     memmove(NewMlu ->Entries, mlu ->Entries, mlu ->UsedEntries * sizeof(_cmsMLUentry));
286     NewMlu ->UsedEntries = mlu ->UsedEntries;
287 
288     // The MLU may be empty
289     if (mlu ->PoolUsed == 0) {
290         NewMlu ->MemPool = NULL;
291     }
292     else {
293         // It is not empty
294         NewMlu ->MemPool = _cmsMalloc(mlu ->ContextID, mlu ->PoolUsed);
295         if (NewMlu ->MemPool == NULL) goto Error;
296     }
297 
298     NewMlu ->PoolSize = mlu ->PoolUsed;
299 
300     if (NewMlu ->MemPool == NULL || mlu ->MemPool == NULL) goto Error;
301 
302     memmove(NewMlu ->MemPool, mlu->MemPool, mlu ->PoolUsed);
303     NewMlu ->PoolUsed = mlu ->PoolUsed;
304 
305     return NewMlu;
306 
307 Error:
308 
309     if (NewMlu != NULL) cmsMLUfree(NewMlu);
310     return NULL;
311 }
312 
313 // Free any used memory
cmsMLUfree(cmsMLU * mlu)314 void CMSEXPORT cmsMLUfree(cmsMLU* mlu)
315 {
316     if (mlu) {
317 
318         if (mlu -> Entries) _cmsFree(mlu ->ContextID, mlu->Entries);
319         if (mlu -> MemPool) _cmsFree(mlu ->ContextID, mlu->MemPool);
320 
321         _cmsFree(mlu ->ContextID, mlu);
322     }
323 }
324 
325 
326 // The algorithm first searches for an exact match of country and language, if not found it uses
327 // the Language. If none is found, first entry is used instead.
328 static
_cmsMLUgetWide(const cmsMLU * mlu,cmsUInt32Number * len,cmsUInt16Number LanguageCode,cmsUInt16Number CountryCode,cmsUInt16Number * UsedLanguageCode,cmsUInt16Number * UsedCountryCode)329 const wchar_t* _cmsMLUgetWide(const cmsMLU* mlu,
330                               cmsUInt32Number *len,
331                               cmsUInt16Number LanguageCode, cmsUInt16Number CountryCode,
332                               cmsUInt16Number* UsedLanguageCode, cmsUInt16Number* UsedCountryCode)
333 {
334     cmsUInt32Number i;
335     int Best = -1;
336     _cmsMLUentry* v;
337 
338     if (mlu == NULL) return NULL;
339 
340     if (mlu -> AllocatedEntries <= 0) return NULL;
341 
342     for (i=0; i < mlu ->UsedEntries; i++) {
343 
344         v = mlu ->Entries + i;
345 
346         if (v -> Language == LanguageCode) {
347 
348             if (Best == -1) Best = (int) i;
349 
350             if (v -> Country == CountryCode) {
351 
352                 if (UsedLanguageCode != NULL) *UsedLanguageCode = v ->Language;
353                 if (UsedCountryCode  != NULL) *UsedCountryCode = v ->Country;
354 
355                 if (len != NULL) *len = v ->Len;
356 
357                 return (wchar_t*) ((cmsUInt8Number*) mlu ->MemPool + v -> StrW);        // Found exact match
358             }
359         }
360     }
361 
362     // No string found. Return First one
363     if (Best == -1)
364         Best = 0;
365 
366     v = mlu ->Entries + Best;
367 
368     if (UsedLanguageCode != NULL) *UsedLanguageCode = v ->Language;
369     if (UsedCountryCode  != NULL) *UsedCountryCode = v ->Country;
370 
371     if (len != NULL) *len   = v ->Len;
372 
373     if (v->StrW + v->Len > mlu->PoolSize) return NULL;
374 
375     return(wchar_t*) ((cmsUInt8Number*) mlu ->MemPool + v ->StrW);
376 }
377 
378 
379 // Obtain an ASCII representation of the wide string. Setting buffer to NULL returns the len
cmsMLUgetASCII(const cmsMLU * mlu,const char LanguageCode[3],const char CountryCode[3],char * Buffer,cmsUInt32Number BufferSize)380 cmsUInt32Number CMSEXPORT cmsMLUgetASCII(const cmsMLU* mlu,
381                                        const char LanguageCode[3], const char CountryCode[3],
382                                        char* Buffer, cmsUInt32Number BufferSize)
383 {
384     const wchar_t *Wide;
385     cmsUInt32Number  StrLen = 0;
386     cmsUInt32Number ASCIIlen, i;
387 
388     cmsUInt16Number Lang  = strTo16(LanguageCode);
389     cmsUInt16Number Cntry = strTo16(CountryCode);
390 
391     // Sanitize
392     if (mlu == NULL) return 0;
393 
394     // Get WideChar
395     Wide = _cmsMLUgetWide(mlu, &StrLen, Lang, Cntry, NULL, NULL);
396     if (Wide == NULL) return 0;
397 
398     ASCIIlen = StrLen / sizeof(wchar_t);
399 
400     // Maybe we want only to know the len?
401     if (Buffer == NULL) return ASCIIlen + 1; // Note the zero at the end
402 
403     // No buffer size means no data
404     if (BufferSize <= 0) return 0;
405 
406     // Some clipping may be required
407     if (BufferSize < ASCIIlen + 1)
408         ASCIIlen = BufferSize - 1;
409 
410     // Precess each character
411     for (i=0; i < ASCIIlen; i++) {
412 
413         if (Wide[i] == 0)
414             Buffer[i] = 0;
415         else
416             Buffer[i] = (char) Wide[i];
417     }
418 
419     // We put a termination "\0"
420     Buffer[ASCIIlen] = 0;
421     return ASCIIlen + 1;
422 }
423 
424 // Obtain a wide representation of the MLU, on depending on current locale settings
cmsMLUgetWide(const cmsMLU * mlu,const char LanguageCode[3],const char CountryCode[3],wchar_t * Buffer,cmsUInt32Number BufferSize)425 cmsUInt32Number CMSEXPORT cmsMLUgetWide(const cmsMLU* mlu,
426                                       const char LanguageCode[3], const char CountryCode[3],
427                                       wchar_t* Buffer, cmsUInt32Number BufferSize)
428 {
429     const wchar_t *Wide;
430     cmsUInt32Number  StrLen = 0;
431 
432     cmsUInt16Number Lang  = strTo16(LanguageCode);
433     cmsUInt16Number Cntry = strTo16(CountryCode);
434 
435     // Sanitize
436     if (mlu == NULL) return 0;
437 
438     Wide = _cmsMLUgetWide(mlu, &StrLen, Lang, Cntry, NULL, NULL);
439     if (Wide == NULL) return 0;
440 
441     // Maybe we want only to know the len?
442     if (Buffer == NULL) return StrLen + sizeof(wchar_t);
443 
444   // No buffer size means no data
445     if (BufferSize <= 0) return 0;
446 
447     // Some clipping may be required
448     if (BufferSize < StrLen + sizeof(wchar_t))
449         StrLen = BufferSize - + sizeof(wchar_t);
450 
451     memmove(Buffer, Wide, StrLen);
452     Buffer[StrLen / sizeof(wchar_t)] = 0;
453 
454     return StrLen + sizeof(wchar_t);
455 }
456 
457 
458 // Get also the language and country
cmsMLUgetTranslation(const cmsMLU * mlu,const char LanguageCode[3],const char CountryCode[3],char ObtainedLanguage[3],char ObtainedCountry[3])459 CMSAPI cmsBool CMSEXPORT cmsMLUgetTranslation(const cmsMLU* mlu,
460                                               const char LanguageCode[3], const char CountryCode[3],
461                                               char ObtainedLanguage[3], char ObtainedCountry[3])
462 {
463     const wchar_t *Wide;
464 
465     cmsUInt16Number Lang  = strTo16(LanguageCode);
466     cmsUInt16Number Cntry = strTo16(CountryCode);
467     cmsUInt16Number ObtLang, ObtCode;
468 
469     // Sanitize
470     if (mlu == NULL) return FALSE;
471 
472     Wide = _cmsMLUgetWide(mlu, NULL, Lang, Cntry, &ObtLang, &ObtCode);
473     if (Wide == NULL) return FALSE;
474 
475     // Get used language and code
476     strFrom16(ObtainedLanguage, ObtLang);
477     strFrom16(ObtainedCountry, ObtCode);
478 
479     return TRUE;
480 }
481 
482 
483 
484 // Get the number of translations in the MLU object
cmsMLUtranslationsCount(const cmsMLU * mlu)485 cmsUInt32Number CMSEXPORT cmsMLUtranslationsCount(const cmsMLU* mlu)
486 {
487     if (mlu == NULL) return 0;
488     return mlu->UsedEntries;
489 }
490 
491 // Get the language and country codes for a specific MLU index
cmsMLUtranslationsCodes(const cmsMLU * mlu,cmsUInt32Number idx,char LanguageCode[3],char CountryCode[3])492 cmsBool CMSEXPORT cmsMLUtranslationsCodes(const cmsMLU* mlu,
493                                           cmsUInt32Number idx,
494                                           char LanguageCode[3],
495                                           char CountryCode[3])
496 {
497     _cmsMLUentry *entry;
498 
499     if (mlu == NULL) return FALSE;
500 
501     if (idx >= mlu->UsedEntries) return FALSE;
502 
503     entry = &mlu->Entries[idx];
504 
505     strFrom16(LanguageCode, entry->Language);
506     strFrom16(CountryCode, entry->Country);
507 
508     return TRUE;
509 }
510 
511 
512 // Named color lists --------------------------------------------------------------------------------------------
513 
514 // Grow the list to keep at least NumElements
515 static
GrowNamedColorList(cmsNAMEDCOLORLIST * v)516 cmsBool  GrowNamedColorList(cmsNAMEDCOLORLIST* v)
517 {
518     cmsUInt32Number size;
519     _cmsNAMEDCOLOR * NewPtr;
520 
521     if (v == NULL) return FALSE;
522 
523     if (v ->Allocated == 0)
524         size = 64;   // Initial guess
525     else
526         size = v ->Allocated * 2;
527 
528     // Keep a maximum color lists can grow, 100K entries seems reasonable
529     if (size > 1024 * 100) {
530         _cmsFree(v->ContextID, (void*) v->List);
531         v->List = NULL;
532         return FALSE;
533     }
534 
535     NewPtr = (_cmsNAMEDCOLOR*) _cmsRealloc(v ->ContextID, v ->List, size * sizeof(_cmsNAMEDCOLOR));
536     if (NewPtr == NULL)
537         return FALSE;
538 
539     v ->List      = NewPtr;
540     v ->Allocated = size;
541     return TRUE;
542 }
543 
544 // Allocate a list for n elements
cmsAllocNamedColorList(cmsContext ContextID,cmsUInt32Number n,cmsUInt32Number ColorantCount,const char * Prefix,const char * Suffix)545 cmsNAMEDCOLORLIST* CMSEXPORT cmsAllocNamedColorList(cmsContext ContextID, cmsUInt32Number n, cmsUInt32Number ColorantCount, const char* Prefix, const char* Suffix)
546 {
547     cmsNAMEDCOLORLIST* v;
548 
549     if (ColorantCount > cmsMAXCHANNELS)
550         return NULL;
551 
552     v = (cmsNAMEDCOLORLIST*)_cmsMallocZero(ContextID, sizeof(cmsNAMEDCOLORLIST));
553     if (v == NULL) return NULL;
554 
555     v ->List      = NULL;
556     v ->nColors   = 0;
557     v ->ContextID  = ContextID;
558 
559     while (v -> Allocated < n) {
560         if (!GrowNamedColorList(v)) {
561             cmsFreeNamedColorList(v);
562             return NULL;
563         }
564     }
565 
566     strncpy(v ->Prefix, Prefix, sizeof(v ->Prefix)-1);
567     strncpy(v ->Suffix, Suffix, sizeof(v ->Suffix)-1);
568     v->Prefix[32] = v->Suffix[32] = 0;
569 
570     v -> ColorantCount = ColorantCount;
571 
572     return v;
573 }
574 
575 // Free a list
cmsFreeNamedColorList(cmsNAMEDCOLORLIST * v)576 void CMSEXPORT cmsFreeNamedColorList(cmsNAMEDCOLORLIST* v)
577 {
578     if (v == NULL) return;
579     if (v ->List) _cmsFree(v ->ContextID, v ->List);
580     _cmsFree(v ->ContextID, v);
581 }
582 
cmsDupNamedColorList(const cmsNAMEDCOLORLIST * v)583 cmsNAMEDCOLORLIST* CMSEXPORT cmsDupNamedColorList(const cmsNAMEDCOLORLIST* v)
584 {
585     cmsNAMEDCOLORLIST* NewNC;
586 
587     if (v == NULL) return NULL;
588 
589     NewNC= cmsAllocNamedColorList(v ->ContextID, v -> nColors, v ->ColorantCount, v ->Prefix, v ->Suffix);
590     if (NewNC == NULL) return NULL;
591 
592     // For really large tables we need this
593     while (NewNC ->Allocated < v ->Allocated){
594         if (!GrowNamedColorList(NewNC))
595         {
596             cmsFreeNamedColorList(NewNC);
597             return NULL;
598         }
599     }
600 
601     memmove(NewNC ->Prefix, v ->Prefix, sizeof(v ->Prefix));
602     memmove(NewNC ->Suffix, v ->Suffix, sizeof(v ->Suffix));
603     NewNC ->ColorantCount = v ->ColorantCount;
604     memmove(NewNC->List, v ->List, v->nColors * sizeof(_cmsNAMEDCOLOR));
605     NewNC ->nColors = v ->nColors;
606     return NewNC;
607 }
608 
609 
610 // Append a color to a list. List pointer may change if reallocated
cmsAppendNamedColor(cmsNAMEDCOLORLIST * NamedColorList,const char * Name,cmsUInt16Number PCS[3],cmsUInt16Number Colorant[cmsMAXCHANNELS])611 cmsBool  CMSEXPORT cmsAppendNamedColor(cmsNAMEDCOLORLIST* NamedColorList,
612                                        const char* Name,
613                                        cmsUInt16Number PCS[3], cmsUInt16Number Colorant[cmsMAXCHANNELS])
614 {
615     cmsUInt32Number i;
616 
617     if (NamedColorList == NULL) return FALSE;
618 
619     if (NamedColorList ->nColors + 1 > NamedColorList ->Allocated) {
620         if (!GrowNamedColorList(NamedColorList)) return FALSE;
621     }
622 
623     for (i=0; i < NamedColorList ->ColorantCount; i++)
624         NamedColorList ->List[NamedColorList ->nColors].DeviceColorant[i] = Colorant == NULL ? (cmsUInt16Number)0 : Colorant[i];
625 
626     for (i=0; i < 3; i++)
627         NamedColorList ->List[NamedColorList ->nColors].PCS[i] = PCS == NULL ? (cmsUInt16Number) 0 : PCS[i];
628 
629     if (Name != NULL) {
630 
631         strncpy(NamedColorList ->List[NamedColorList ->nColors].Name, Name, cmsMAX_PATH-1);
632         NamedColorList ->List[NamedColorList ->nColors].Name[cmsMAX_PATH-1] = 0;
633 
634     }
635     else
636         NamedColorList ->List[NamedColorList ->nColors].Name[0] = 0;
637 
638 
639     NamedColorList ->nColors++;
640     return TRUE;
641 }
642 
643 // Returns number of elements
cmsNamedColorCount(const cmsNAMEDCOLORLIST * NamedColorList)644 cmsUInt32Number CMSEXPORT cmsNamedColorCount(const cmsNAMEDCOLORLIST* NamedColorList)
645 {
646      if (NamedColorList == NULL) return 0;
647      return NamedColorList ->nColors;
648 }
649 
650 // Info about a given color
cmsNamedColorInfo(const cmsNAMEDCOLORLIST * NamedColorList,cmsUInt32Number nColor,char * Name,char * Prefix,char * Suffix,cmsUInt16Number * PCS,cmsUInt16Number * Colorant)651 cmsBool  CMSEXPORT cmsNamedColorInfo(const cmsNAMEDCOLORLIST* NamedColorList, cmsUInt32Number nColor,
652                                      char* Name,
653                                      char* Prefix,
654                                      char* Suffix,
655                                      cmsUInt16Number* PCS,
656                                      cmsUInt16Number* Colorant)
657 {
658     if (NamedColorList == NULL) return FALSE;
659 
660     if (nColor >= cmsNamedColorCount(NamedColorList)) return FALSE;
661 
662     // strcpy instead of strncpy because many apps are using small buffers
663     if (Name) strcpy(Name, NamedColorList->List[nColor].Name);
664     if (Prefix) strcpy(Prefix, NamedColorList->Prefix);
665     if (Suffix) strcpy(Suffix, NamedColorList->Suffix);
666     if (PCS)
667         memmove(PCS, NamedColorList ->List[nColor].PCS, 3*sizeof(cmsUInt16Number));
668 
669     if (Colorant)
670         memmove(Colorant, NamedColorList ->List[nColor].DeviceColorant,
671                                 sizeof(cmsUInt16Number) * NamedColorList ->ColorantCount);
672 
673 
674     return TRUE;
675 }
676 
677 // Search for a given color name (no prefix or suffix)
cmsNamedColorIndex(const cmsNAMEDCOLORLIST * NamedColorList,const char * Name)678 cmsInt32Number CMSEXPORT cmsNamedColorIndex(const cmsNAMEDCOLORLIST* NamedColorList, const char* Name)
679 {
680     cmsUInt32Number i;
681     cmsUInt32Number n;
682 
683     if (NamedColorList == NULL) return -1;
684     n = cmsNamedColorCount(NamedColorList);
685     for (i=0; i < n; i++) {
686         if (cmsstrcasecmp(Name,  NamedColorList->List[i].Name) == 0)
687             return (cmsInt32Number) i;
688     }
689 
690     return -1;
691 }
692 
693 // MPE support -----------------------------------------------------------------------------------------------------------------
694 
695 static
FreeNamedColorList(cmsStage * mpe)696 void FreeNamedColorList(cmsStage* mpe)
697 {
698     cmsNAMEDCOLORLIST* List = (cmsNAMEDCOLORLIST*) mpe ->Data;
699     cmsFreeNamedColorList(List);
700 }
701 
702 static
DupNamedColorList(cmsStage * mpe)703 void* DupNamedColorList(cmsStage* mpe)
704 {
705     cmsNAMEDCOLORLIST* List = (cmsNAMEDCOLORLIST*) mpe ->Data;
706     return cmsDupNamedColorList(List);
707 }
708 
709 static
EvalNamedColorPCS(const cmsFloat32Number In[],cmsFloat32Number Out[],const cmsStage * mpe)710 void EvalNamedColorPCS(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe)
711 {
712     cmsNAMEDCOLORLIST* NamedColorList = (cmsNAMEDCOLORLIST*) mpe ->Data;
713     cmsUInt16Number index = (cmsUInt16Number) _cmsQuickSaturateWord(In[0] * 65535.0);
714 
715     if (index >= NamedColorList-> nColors) {
716         cmsSignalError(NamedColorList ->ContextID, cmsERROR_RANGE, "Color %d out of range", index);
717         Out[0] = Out[1] = Out[2] = 0.0f;
718     }
719     else {
720 
721             // Named color always uses Lab
722             Out[0] = (cmsFloat32Number) (NamedColorList->List[index].PCS[0] / 65535.0);
723             Out[1] = (cmsFloat32Number) (NamedColorList->List[index].PCS[1] / 65535.0);
724             Out[2] = (cmsFloat32Number) (NamedColorList->List[index].PCS[2] / 65535.0);
725     }
726 }
727 
728 static
EvalNamedColor(const cmsFloat32Number In[],cmsFloat32Number Out[],const cmsStage * mpe)729 void EvalNamedColor(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe)
730 {
731     cmsNAMEDCOLORLIST* NamedColorList = (cmsNAMEDCOLORLIST*) mpe ->Data;
732     cmsUInt16Number index = (cmsUInt16Number) _cmsQuickSaturateWord(In[0] * 65535.0);
733     cmsUInt32Number j;
734 
735     if (index >= NamedColorList-> nColors) {
736         cmsSignalError(NamedColorList ->ContextID, cmsERROR_RANGE, "Color %d out of range", index);
737         for (j = 0; j < NamedColorList->ColorantCount; j++)
738             Out[j] = 0.0f;
739 
740     }
741     else {
742         for (j=0; j < NamedColorList ->ColorantCount; j++)
743             Out[j] = (cmsFloat32Number) (NamedColorList->List[index].DeviceColorant[j] / 65535.0);
744     }
745 }
746 
747 
748 // Named color lookup element
_cmsStageAllocNamedColor(cmsNAMEDCOLORLIST * NamedColorList,cmsBool UsePCS)749 cmsStage* CMSEXPORT _cmsStageAllocNamedColor(cmsNAMEDCOLORLIST* NamedColorList, cmsBool UsePCS)
750 {
751     return _cmsStageAllocPlaceholder(NamedColorList ->ContextID,
752                                    cmsSigNamedColorElemType,
753                                    1, UsePCS ? 3 : NamedColorList ->ColorantCount,
754                                    UsePCS ? EvalNamedColorPCS : EvalNamedColor,
755                                    DupNamedColorList,
756                                    FreeNamedColorList,
757                                    cmsDupNamedColorList(NamedColorList));
758 
759 }
760 
761 
762 // Retrieve the named color list from a transform. Should be first element in the LUT
cmsGetNamedColorList(cmsHTRANSFORM xform)763 cmsNAMEDCOLORLIST* CMSEXPORT cmsGetNamedColorList(cmsHTRANSFORM xform)
764 {
765     _cmsTRANSFORM* v = (_cmsTRANSFORM*) xform;
766     cmsStage* mpe;
767 
768     if (v == NULL) return NULL;
769     if (v->Lut == NULL) return NULL;
770 
771     mpe = v->Lut->Elements;
772     if (mpe == NULL) return NULL;
773 
774     if (mpe ->Type != cmsSigNamedColorElemType) return NULL;
775     return (cmsNAMEDCOLORLIST*) mpe ->Data;
776 }
777 
778 
779 // Profile sequence description routines -------------------------------------------------------------------------------------
780 
cmsAllocProfileSequenceDescription(cmsContext ContextID,cmsUInt32Number n)781 cmsSEQ* CMSEXPORT cmsAllocProfileSequenceDescription(cmsContext ContextID, cmsUInt32Number n)
782 {
783     cmsSEQ* Seq;
784     cmsUInt32Number i;
785 
786     if (n == 0) return NULL;
787 
788     // In a absolutely arbitrary way, I hereby decide to allow a maxim of 255 profiles linked
789     // in a devicelink. It makes not sense anyway and may be used for exploits, so let's close the door!
790     if (n > 255) return NULL;
791 
792     Seq = (cmsSEQ*) _cmsMallocZero(ContextID, sizeof(cmsSEQ));
793     if (Seq == NULL) return NULL;
794 
795     Seq -> ContextID = ContextID;
796     Seq -> seq      = (cmsPSEQDESC*) _cmsCalloc(ContextID, n, sizeof(cmsPSEQDESC));
797     Seq -> n        = n;
798 
799     if (Seq -> seq == NULL) {
800         _cmsFree(ContextID, Seq);
801         return NULL;
802     }
803 
804     for (i=0; i < n; i++) {
805         Seq -> seq[i].Manufacturer = NULL;
806         Seq -> seq[i].Model        = NULL;
807         Seq -> seq[i].Description  = NULL;
808     }
809 
810     return Seq;
811 }
812 
cmsFreeProfileSequenceDescription(cmsSEQ * pseq)813 void CMSEXPORT cmsFreeProfileSequenceDescription(cmsSEQ* pseq)
814 {
815     cmsUInt32Number i;
816 
817     for (i=0; i < pseq ->n; i++) {
818         if (pseq ->seq[i].Manufacturer != NULL) cmsMLUfree(pseq ->seq[i].Manufacturer);
819         if (pseq ->seq[i].Model != NULL) cmsMLUfree(pseq ->seq[i].Model);
820         if (pseq ->seq[i].Description != NULL) cmsMLUfree(pseq ->seq[i].Description);
821     }
822 
823     if (pseq ->seq != NULL) _cmsFree(pseq ->ContextID, pseq ->seq);
824     _cmsFree(pseq -> ContextID, pseq);
825 }
826 
cmsDupProfileSequenceDescription(const cmsSEQ * pseq)827 cmsSEQ* CMSEXPORT cmsDupProfileSequenceDescription(const cmsSEQ* pseq)
828 {
829     cmsSEQ *NewSeq;
830     cmsUInt32Number i;
831 
832     if (pseq == NULL)
833         return NULL;
834 
835     NewSeq = (cmsSEQ*) _cmsMalloc(pseq -> ContextID, sizeof(cmsSEQ));
836     if (NewSeq == NULL) return NULL;
837 
838 
839     NewSeq -> seq      = (cmsPSEQDESC*) _cmsCalloc(pseq ->ContextID, pseq ->n, sizeof(cmsPSEQDESC));
840     if (NewSeq ->seq == NULL) goto Error;
841 
842     NewSeq -> ContextID = pseq ->ContextID;
843     NewSeq -> n        = pseq ->n;
844 
845     for (i=0; i < pseq->n; i++) {
846 
847         memmove(&NewSeq ->seq[i].attributes, &pseq ->seq[i].attributes, sizeof(cmsUInt64Number));
848 
849         NewSeq ->seq[i].deviceMfg   = pseq ->seq[i].deviceMfg;
850         NewSeq ->seq[i].deviceModel = pseq ->seq[i].deviceModel;
851         memmove(&NewSeq ->seq[i].ProfileID, &pseq ->seq[i].ProfileID, sizeof(cmsProfileID));
852         NewSeq ->seq[i].technology  = pseq ->seq[i].technology;
853 
854         NewSeq ->seq[i].Manufacturer = cmsMLUdup(pseq ->seq[i].Manufacturer);
855         NewSeq ->seq[i].Model        = cmsMLUdup(pseq ->seq[i].Model);
856         NewSeq ->seq[i].Description  = cmsMLUdup(pseq ->seq[i].Description);
857 
858     }
859 
860     return NewSeq;
861 
862 Error:
863 
864     cmsFreeProfileSequenceDescription(NewSeq);
865     return NULL;
866 }
867 
868 // Dictionaries --------------------------------------------------------------------------------------------------------
869 
870 // Dictionaries are just very simple linked lists
871 
872 
873 typedef struct _cmsDICT_struct {
874     cmsDICTentry* head;
875     cmsContext ContextID;
876 } _cmsDICT;
877 
878 
879 // Allocate an empty dictionary
cmsDictAlloc(cmsContext ContextID)880 cmsHANDLE CMSEXPORT cmsDictAlloc(cmsContext ContextID)
881 {
882     _cmsDICT* dict = (_cmsDICT*) _cmsMallocZero(ContextID, sizeof(_cmsDICT));
883     if (dict == NULL) return NULL;
884 
885     dict ->ContextID = ContextID;
886     return (cmsHANDLE) dict;
887 
888 }
889 
890 // Dispose resources
cmsDictFree(cmsHANDLE hDict)891 void CMSEXPORT cmsDictFree(cmsHANDLE hDict)
892 {
893     _cmsDICT* dict = (_cmsDICT*) hDict;
894     cmsDICTentry *entry, *next;
895 
896     _cmsAssert(dict != NULL);
897 
898     // Walk the list freeing all nodes
899     entry = dict ->head;
900     while (entry != NULL) {
901 
902             if (entry ->DisplayName  != NULL) cmsMLUfree(entry ->DisplayName);
903             if (entry ->DisplayValue != NULL) cmsMLUfree(entry ->DisplayValue);
904             if (entry ->Name != NULL) _cmsFree(dict ->ContextID, entry -> Name);
905             if (entry ->Value != NULL) _cmsFree(dict ->ContextID, entry -> Value);
906 
907             // Don't fall in the habitual trap...
908             next = entry ->Next;
909             _cmsFree(dict ->ContextID, entry);
910 
911             entry = next;
912     }
913 
914     _cmsFree(dict ->ContextID, dict);
915 }
916 
917 
918 // Duplicate a wide char string
919 static
DupWcs(cmsContext ContextID,const wchar_t * ptr)920 wchar_t* DupWcs(cmsContext ContextID, const wchar_t* ptr)
921 {
922     if (ptr == NULL) return NULL;
923     return (wchar_t*) _cmsDupMem(ContextID, ptr, (mywcslen(ptr) + 1) * sizeof(wchar_t));
924 }
925 
926 // Add a new entry to the linked list
cmsDictAddEntry(cmsHANDLE hDict,const wchar_t * Name,const wchar_t * Value,const cmsMLU * DisplayName,const cmsMLU * DisplayValue)927 cmsBool CMSEXPORT cmsDictAddEntry(cmsHANDLE hDict, const wchar_t* Name, const wchar_t* Value, const cmsMLU *DisplayName, const cmsMLU *DisplayValue)
928 {
929     _cmsDICT* dict = (_cmsDICT*) hDict;
930     cmsDICTentry *entry;
931 
932     _cmsAssert(dict != NULL);
933     _cmsAssert(Name != NULL);
934 
935     entry = (cmsDICTentry*) _cmsMallocZero(dict ->ContextID, sizeof(cmsDICTentry));
936     if (entry == NULL) return FALSE;
937 
938     entry ->DisplayName  = cmsMLUdup(DisplayName);
939     entry ->DisplayValue = cmsMLUdup(DisplayValue);
940     entry ->Name         = DupWcs(dict ->ContextID, Name);
941     entry ->Value        = DupWcs(dict ->ContextID, Value);
942 
943     entry ->Next = dict ->head;
944     dict ->head = entry;
945 
946     return TRUE;
947 }
948 
949 
950 // Duplicates an existing dictionary
cmsDictDup(cmsHANDLE hDict)951 cmsHANDLE CMSEXPORT cmsDictDup(cmsHANDLE hDict)
952 {
953     _cmsDICT* old_dict = (_cmsDICT*) hDict;
954     cmsHANDLE hNew;
955     cmsDICTentry *entry;
956 
957     _cmsAssert(old_dict != NULL);
958 
959     hNew  = cmsDictAlloc(old_dict ->ContextID);
960     if (hNew == NULL) return NULL;
961 
962     // Walk the list freeing all nodes
963     entry = old_dict ->head;
964     while (entry != NULL) {
965 
966         if (!cmsDictAddEntry(hNew, entry ->Name, entry ->Value, entry ->DisplayName, entry ->DisplayValue)) {
967 
968             cmsDictFree(hNew);
969             return NULL;
970         }
971 
972         entry = entry -> Next;
973     }
974 
975     return hNew;
976 }
977 
978 // Get a pointer to the linked list
cmsDictGetEntryList(cmsHANDLE hDict)979 const cmsDICTentry* CMSEXPORT cmsDictGetEntryList(cmsHANDLE hDict)
980 {
981     _cmsDICT* dict = (_cmsDICT*) hDict;
982 
983     if (dict == NULL) return NULL;
984     return dict ->head;
985 }
986 
987 // Helper For external languages
cmsDictNextEntry(const cmsDICTentry * e)988 const cmsDICTentry* CMSEXPORT cmsDictNextEntry(const cmsDICTentry* e)
989 {
990      if (e == NULL) return NULL;
991      return e ->Next;
992 }
993