Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/lcms2/src/cmsnamed.c
4391 views
1
//---------------------------------------------------------------------------------
2
//
3
// Little Color Management System
4
// Copyright (c) 1998-2024 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
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
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
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
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
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
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
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
// Convert from UTF8 to wchar, returns len.
207
static
208
cmsUInt32Number decodeUTF8(wchar_t* out, const char* in)
209
{
210
cmsUInt32Number codepoint = 0;
211
cmsUInt32Number size = 0;
212
213
while (*in)
214
{
215
cmsUInt8Number ch = (cmsUInt8Number) *in;
216
217
if (ch <= 0x7f)
218
{
219
codepoint = ch;
220
}
221
else if (ch <= 0xbf)
222
{
223
codepoint = (codepoint << 6) | (ch & 0x3f);
224
}
225
else if (ch <= 0xdf)
226
{
227
codepoint = ch & 0x1f;
228
}
229
else if (ch <= 0xef)
230
{
231
codepoint = ch & 0x0f;
232
}
233
else
234
{
235
codepoint = ch & 0x07;
236
}
237
238
in++;
239
240
if (((*in & 0xc0) != 0x80) && (codepoint <= 0x10ffff))
241
{
242
if (sizeof(wchar_t) > 2)
243
{
244
if (out) *out++ = (wchar_t) codepoint;
245
size++;
246
}
247
else
248
if (codepoint > 0xffff)
249
{
250
if (out)
251
{
252
*out++ = (wchar_t)(0xd800 + (codepoint >> 10));
253
*out++ = (wchar_t)(0xdc00 + (codepoint & 0x03ff));
254
size += 2;
255
}
256
}
257
else
258
if (codepoint < 0xd800 || codepoint >= 0xe000)
259
{
260
if (out) *out++ = (wchar_t) codepoint;
261
size++;
262
}
263
}
264
}
265
266
return size;
267
}
268
269
// Convert from wchar_t to UTF8
270
static
271
cmsUInt32Number encodeUTF8(char* out, const wchar_t* in, cmsUInt32Number max_wchars, cmsUInt32Number max_chars)
272
{
273
cmsUInt32Number codepoint = 0;
274
cmsUInt32Number size = 0;
275
cmsUInt32Number len_w = 0;
276
277
while (*in && len_w < max_wchars)
278
{
279
if (*in >= 0xd800 && *in <= 0xdbff)
280
codepoint = ((*in - 0xd800) << 10) + 0x10000;
281
else
282
{
283
if (*in >= 0xdc00 && *in <= 0xdfff)
284
codepoint |= *in - 0xdc00;
285
else
286
codepoint = *in;
287
288
if (codepoint <= 0x7f)
289
{
290
if (out && (size + 1 < max_chars)) *out++ = (char)codepoint;
291
size++;
292
}
293
294
else if (codepoint <= 0x7ff)
295
{
296
if (out && (max_chars > 0) && (size + 2 < max_chars))
297
{
298
*out++ = (char)(cmsUInt32Number)(0xc0 | ((codepoint >> 6) & 0x1f));
299
*out++ = (char)(cmsUInt32Number)(0x80 | (codepoint & 0x3f));
300
}
301
size += 2;
302
}
303
else if (codepoint <= 0xffff)
304
{
305
if (out && (max_chars > 0) && (size + 3 < max_chars))
306
{
307
*out++ = (char)(cmsUInt32Number)(0xe0 | ((codepoint >> 12) & 0x0f));
308
*out++ = (char)(cmsUInt32Number)(0x80 | ((codepoint >> 6) & 0x3f));
309
*out++ = (char)(cmsUInt32Number)(0x80 | (codepoint & 0x3f));
310
}
311
size += 3;
312
}
313
else
314
{
315
if (out && (max_chars > 0) && (size + 4 < max_chars))
316
{
317
*out++ = (char)(cmsUInt32Number)(0xf0 | ((codepoint >> 18) & 0x07));
318
*out++ = (char)(cmsUInt32Number)(0x80 | ((codepoint >> 12) & 0x3f));
319
*out++ = (char)(cmsUInt32Number)(0x80 | ((codepoint >> 6) & 0x3f));
320
*out++ = (char)(cmsUInt32Number)(0x80 | (codepoint & 0x3f));
321
}
322
size += 4;
323
}
324
325
codepoint = 0;
326
}
327
328
in++; len_w++;
329
}
330
331
return size;
332
}
333
334
// Add an ASCII entry. Do not add any \0 termination (ICC1v43_2010-12.pdf page 61)
335
// In the case the user explicitly sets an empty string, we force a \0
336
cmsBool CMSEXPORT cmsMLUsetASCII(cmsMLU* mlu, const char LanguageCode[3], const char CountryCode[3], const char* ASCIIString)
337
{
338
cmsUInt32Number i, len = (cmsUInt32Number)strlen(ASCIIString);
339
wchar_t* WStr;
340
cmsBool rc;
341
cmsUInt16Number Lang = strTo16(LanguageCode);
342
cmsUInt16Number Cntry = strTo16(CountryCode);
343
344
if (mlu == NULL) return FALSE;
345
346
// len == 0 would prevent operation, so we set a empty string pointing to zero
347
if (len == 0)
348
{
349
wchar_t empty = 0;
350
return AddMLUBlock(mlu, sizeof(wchar_t), &empty, Lang, Cntry);
351
}
352
353
WStr = (wchar_t*)_cmsCalloc(mlu->ContextID, len, sizeof(wchar_t));
354
if (WStr == NULL) return FALSE;
355
356
for (i = 0; i < len; i++)
357
WStr[i] = (wchar_t)ASCIIString[i];
358
359
rc = AddMLUBlock(mlu, len * sizeof(wchar_t), WStr, Lang, Cntry);
360
361
_cmsFree(mlu->ContextID, WStr);
362
return rc;
363
364
}
365
366
// Add an UTF8 entry. Do not add any \0 termination (ICC1v43_2010-12.pdf page 61)
367
// In the case the user explicitly sets an empty string, we force a \0
368
cmsBool CMSEXPORT cmsMLUsetUTF8(cmsMLU* mlu, const char LanguageCode[3], const char CountryCode[3], const char* UTF8String)
369
{
370
cmsUInt32Number UTF8len;
371
wchar_t* WStr;
372
cmsBool rc;
373
cmsUInt16Number Lang = strTo16(LanguageCode);
374
cmsUInt16Number Cntry = strTo16(CountryCode);
375
376
if (mlu == NULL) return FALSE;
377
378
if (*UTF8String == '\0')
379
{
380
wchar_t empty = 0;
381
return AddMLUBlock(mlu, sizeof(wchar_t), &empty, Lang, Cntry);
382
}
383
384
// Len excluding terminator 0
385
UTF8len = decodeUTF8(NULL, UTF8String);
386
387
// Get space for dest
388
WStr = (wchar_t*) _cmsCalloc(mlu ->ContextID, UTF8len, sizeof(wchar_t));
389
if (WStr == NULL) return FALSE;
390
391
decodeUTF8(WStr, UTF8String);
392
393
rc = AddMLUBlock(mlu, UTF8len * sizeof(wchar_t), WStr, Lang, Cntry);
394
395
_cmsFree(mlu ->ContextID, WStr);
396
return rc;
397
}
398
399
// We don't need any wcs support library
400
static
401
cmsUInt32Number mywcslen(const wchar_t *s)
402
{
403
const wchar_t *p;
404
405
p = s;
406
while (*p)
407
p++;
408
409
return (cmsUInt32Number)(p - s);
410
}
411
412
// Add a wide entry. Do not add any \0 terminator (ICC1v43_2010-12.pdf page 61)
413
cmsBool CMSEXPORT cmsMLUsetWide(cmsMLU* mlu, const char Language[3], const char Country[3], const wchar_t* WideString)
414
{
415
cmsUInt16Number Lang = strTo16(Language);
416
cmsUInt16Number Cntry = strTo16(Country);
417
cmsUInt32Number len;
418
419
if (mlu == NULL) return FALSE;
420
if (WideString == NULL) return FALSE;
421
422
len = (cmsUInt32Number) (mywcslen(WideString)) * sizeof(wchar_t);
423
if (len == 0)
424
len = sizeof(wchar_t);
425
426
return AddMLUBlock(mlu, len, WideString, Lang, Cntry);
427
}
428
429
// Duplicating a MLU is as easy as copying all members
430
cmsMLU* CMSEXPORT cmsMLUdup(const cmsMLU* mlu)
431
{
432
cmsMLU* NewMlu = NULL;
433
434
// Duplicating a NULL obtains a NULL
435
if (mlu == NULL) return NULL;
436
437
NewMlu = cmsMLUalloc(mlu ->ContextID, mlu ->UsedEntries);
438
if (NewMlu == NULL) return NULL;
439
440
// Should never happen
441
if (NewMlu ->AllocatedEntries < mlu ->UsedEntries)
442
goto Error;
443
444
// Sanitize...
445
if (NewMlu ->Entries == NULL || mlu ->Entries == NULL) goto Error;
446
447
memmove(NewMlu ->Entries, mlu ->Entries, mlu ->UsedEntries * sizeof(_cmsMLUentry));
448
NewMlu ->UsedEntries = mlu ->UsedEntries;
449
450
// The MLU may be empty
451
if (mlu ->PoolUsed == 0) {
452
NewMlu ->MemPool = NULL;
453
}
454
else {
455
// It is not empty
456
NewMlu ->MemPool = _cmsMalloc(mlu ->ContextID, mlu ->PoolUsed);
457
if (NewMlu ->MemPool == NULL) goto Error;
458
}
459
460
NewMlu ->PoolSize = mlu ->PoolUsed;
461
462
if (NewMlu ->MemPool == NULL || mlu ->MemPool == NULL) goto Error;
463
464
memmove(NewMlu ->MemPool, mlu->MemPool, mlu ->PoolUsed);
465
NewMlu ->PoolUsed = mlu ->PoolUsed;
466
467
return NewMlu;
468
469
Error:
470
471
if (NewMlu != NULL) cmsMLUfree(NewMlu);
472
return NULL;
473
}
474
475
// Free any used memory
476
void CMSEXPORT cmsMLUfree(cmsMLU* mlu)
477
{
478
if (mlu) {
479
480
if (mlu -> Entries) _cmsFree(mlu ->ContextID, mlu->Entries);
481
if (mlu -> MemPool) _cmsFree(mlu ->ContextID, mlu->MemPool);
482
483
_cmsFree(mlu ->ContextID, mlu);
484
}
485
}
486
487
488
// The algorithm first searches for an exact match of country and language, if not found it uses
489
// the Language. If none is found, first entry is used instead.
490
static
491
const wchar_t* _cmsMLUgetWide(const cmsMLU* mlu,
492
cmsUInt32Number *len,
493
cmsUInt16Number LanguageCode, cmsUInt16Number CountryCode,
494
cmsUInt16Number* UsedLanguageCode, cmsUInt16Number* UsedCountryCode)
495
{
496
cmsUInt32Number i;
497
int Best = -1;
498
_cmsMLUentry* v;
499
500
if (mlu == NULL) return NULL;
501
502
if (mlu -> AllocatedEntries <= 0) return NULL;
503
504
for (i=0; i < mlu ->UsedEntries; i++) {
505
506
v = mlu ->Entries + i;
507
508
if (v -> Language == LanguageCode) {
509
510
if (Best == -1) Best = (int) i;
511
512
if (v -> Country == CountryCode) {
513
514
if (UsedLanguageCode != NULL) *UsedLanguageCode = v ->Language;
515
if (UsedCountryCode != NULL) *UsedCountryCode = v ->Country;
516
517
if (len != NULL) *len = v ->Len;
518
519
return (wchar_t*) ((cmsUInt8Number*) mlu ->MemPool + v -> StrW); // Found exact match
520
}
521
}
522
}
523
524
// No string found. Return First one
525
if (Best == -1)
526
Best = 0;
527
528
v = mlu ->Entries + Best;
529
530
if (UsedLanguageCode != NULL) *UsedLanguageCode = v ->Language;
531
if (UsedCountryCode != NULL) *UsedCountryCode = v ->Country;
532
533
if (len != NULL) *len = v ->Len;
534
535
if (v->StrW + v->Len > mlu->PoolSize) return NULL;
536
537
return (wchar_t*) ((cmsUInt8Number*) mlu ->MemPool + v ->StrW);
538
}
539
540
541
// Obtain an ASCII representation of the wide string. Setting buffer to NULL returns the len
542
cmsUInt32Number CMSEXPORT cmsMLUgetASCII(const cmsMLU* mlu,
543
const char LanguageCode[3], const char CountryCode[3],
544
char* Buffer, cmsUInt32Number BufferSize)
545
{
546
const wchar_t *Wide;
547
cmsUInt32Number StrLen = 0;
548
cmsUInt32Number ASCIIlen, i;
549
550
cmsUInt16Number Lang = strTo16(LanguageCode);
551
cmsUInt16Number Cntry = strTo16(CountryCode);
552
553
// Sanitize
554
if (mlu == NULL) return 0;
555
556
// Get WideChar
557
Wide = _cmsMLUgetWide(mlu, &StrLen, Lang, Cntry, NULL, NULL);
558
if (Wide == NULL) return 0;
559
560
ASCIIlen = StrLen / sizeof(wchar_t);
561
562
// Maybe we want only to know the len?
563
if (Buffer == NULL) return ASCIIlen + 1; // Note the zero at the end
564
565
// No buffer size means no data
566
if (BufferSize <= 0) return 0;
567
568
// Some clipping may be required
569
if (BufferSize < ASCIIlen + 1)
570
ASCIIlen = BufferSize - 1;
571
572
// Process each character
573
for (i=0; i < ASCIIlen; i++) {
574
575
wchar_t wc = Wide[i];
576
577
if (wc < 0xff)
578
Buffer[i] = (char)wc;
579
else
580
Buffer[i] = '?';
581
}
582
583
// We put a termination "\0"
584
Buffer[ASCIIlen] = 0;
585
return ASCIIlen + 1;
586
}
587
588
589
// Obtain a UTF8 representation of the wide string. Setting buffer to NULL returns the len
590
cmsUInt32Number CMSEXPORT cmsMLUgetUTF8(const cmsMLU* mlu,
591
const char LanguageCode[3], const char CountryCode[3],
592
char* Buffer, cmsUInt32Number BufferSize)
593
{
594
const wchar_t *Wide;
595
cmsUInt32Number StrLen = 0;
596
cmsUInt32Number UTF8len;
597
598
cmsUInt16Number Lang = strTo16(LanguageCode);
599
cmsUInt16Number Cntry = strTo16(CountryCode);
600
601
// Sanitize
602
if (mlu == NULL) return 0;
603
604
// Get WideChar
605
Wide = _cmsMLUgetWide(mlu, &StrLen, Lang, Cntry, NULL, NULL);
606
if (Wide == NULL) return 0;
607
608
UTF8len = encodeUTF8(NULL, Wide, StrLen / sizeof(wchar_t), BufferSize);
609
610
// Maybe we want only to know the len?
611
if (Buffer == NULL) return UTF8len + 1; // Note the zero at the end
612
613
// No buffer size means no data
614
if (BufferSize <= 0) return 0;
615
616
// Some clipping may be required
617
if (BufferSize < UTF8len + 1)
618
UTF8len = BufferSize - 1;
619
620
// Process it
621
encodeUTF8(Buffer, Wide, StrLen / sizeof(wchar_t), BufferSize);
622
623
// We put a termination "\0"
624
Buffer[UTF8len] = 0;
625
return UTF8len + 1;
626
}
627
628
// Obtain a wide representation of the MLU, on depending on current locale settings
629
cmsUInt32Number CMSEXPORT cmsMLUgetWide(const cmsMLU* mlu,
630
const char LanguageCode[3], const char CountryCode[3],
631
wchar_t* Buffer, cmsUInt32Number BufferSize)
632
{
633
const wchar_t *Wide;
634
cmsUInt32Number StrLen = 0;
635
636
cmsUInt16Number Lang = strTo16(LanguageCode);
637
cmsUInt16Number Cntry = strTo16(CountryCode);
638
639
// Sanitize
640
if (mlu == NULL) return 0;
641
642
Wide = _cmsMLUgetWide(mlu, &StrLen, Lang, Cntry, NULL, NULL);
643
if (Wide == NULL) return 0;
644
645
// Maybe we want only to know the len?
646
if (Buffer == NULL) return StrLen + sizeof(wchar_t);
647
648
// Invalid buffer size means no data
649
if (BufferSize < sizeof(wchar_t)) return 0;
650
651
// Some clipping may be required
652
if (BufferSize < StrLen + sizeof(wchar_t))
653
StrLen = BufferSize - sizeof(wchar_t);
654
655
memmove(Buffer, Wide, StrLen);
656
Buffer[StrLen / sizeof(wchar_t)] = 0;
657
658
return StrLen + sizeof(wchar_t);
659
}
660
661
662
// Get also the language and country
663
CMSAPI cmsBool CMSEXPORT cmsMLUgetTranslation(const cmsMLU* mlu,
664
const char LanguageCode[3], const char CountryCode[3],
665
char ObtainedLanguage[3], char ObtainedCountry[3])
666
{
667
const wchar_t *Wide;
668
669
cmsUInt16Number Lang = strTo16(LanguageCode);
670
cmsUInt16Number Cntry = strTo16(CountryCode);
671
cmsUInt16Number ObtLang, ObtCode;
672
673
// Sanitize
674
if (mlu == NULL) return FALSE;
675
676
Wide = _cmsMLUgetWide(mlu, NULL, Lang, Cntry, &ObtLang, &ObtCode);
677
if (Wide == NULL) return FALSE;
678
679
// Get used language and code
680
strFrom16(ObtainedLanguage, ObtLang);
681
strFrom16(ObtainedCountry, ObtCode);
682
683
return TRUE;
684
}
685
686
687
688
// Get the number of translations in the MLU object
689
cmsUInt32Number CMSEXPORT cmsMLUtranslationsCount(const cmsMLU* mlu)
690
{
691
if (mlu == NULL) return 0;
692
return mlu->UsedEntries;
693
}
694
695
// Get the language and country codes for a specific MLU index
696
cmsBool CMSEXPORT cmsMLUtranslationsCodes(const cmsMLU* mlu,
697
cmsUInt32Number idx,
698
char LanguageCode[3],
699
char CountryCode[3])
700
{
701
_cmsMLUentry *entry;
702
703
if (mlu == NULL) return FALSE;
704
705
if (idx >= mlu->UsedEntries) return FALSE;
706
707
entry = &mlu->Entries[idx];
708
709
strFrom16(LanguageCode, entry->Language);
710
strFrom16(CountryCode, entry->Country);
711
712
return TRUE;
713
}
714
715
716
// Named color lists --------------------------------------------------------------------------------------------
717
718
// Grow the list to keep at least NumElements
719
static
720
cmsBool GrowNamedColorList(cmsNAMEDCOLORLIST* v)
721
{
722
cmsUInt32Number size;
723
_cmsNAMEDCOLOR * NewPtr;
724
725
if (v == NULL) return FALSE;
726
727
if (v ->Allocated == 0)
728
size = 64; // Initial guess
729
else
730
size = v ->Allocated * 2;
731
732
// Keep a maximum color lists can grow, 100K entries seems reasonable
733
if (size > 1024 * 100) {
734
_cmsFree(v->ContextID, (void*) v->List);
735
v->List = NULL;
736
return FALSE;
737
}
738
739
NewPtr = (_cmsNAMEDCOLOR*) _cmsRealloc(v ->ContextID, v ->List, size * sizeof(_cmsNAMEDCOLOR));
740
if (NewPtr == NULL)
741
return FALSE;
742
743
v ->List = NewPtr;
744
v ->Allocated = size;
745
return TRUE;
746
}
747
748
// Allocate a list for n elements
749
cmsNAMEDCOLORLIST* CMSEXPORT cmsAllocNamedColorList(cmsContext ContextID, cmsUInt32Number n, cmsUInt32Number ColorantCount, const char* Prefix, const char* Suffix)
750
{
751
cmsNAMEDCOLORLIST* v;
752
753
if (ColorantCount > cmsMAXCHANNELS)
754
return NULL;
755
756
v = (cmsNAMEDCOLORLIST*)_cmsMallocZero(ContextID, sizeof(cmsNAMEDCOLORLIST));
757
if (v == NULL) return NULL;
758
759
v ->List = NULL;
760
v ->nColors = 0;
761
v ->ContextID = ContextID;
762
763
while (v -> Allocated < n) {
764
if (!GrowNamedColorList(v)) {
765
cmsFreeNamedColorList(v);
766
return NULL;
767
}
768
}
769
770
strncpy(v ->Prefix, Prefix, sizeof(v ->Prefix)-1);
771
strncpy(v ->Suffix, Suffix, sizeof(v ->Suffix)-1);
772
v->Prefix[32] = v->Suffix[32] = 0;
773
774
v -> ColorantCount = ColorantCount;
775
776
return v;
777
}
778
779
// Free a list
780
void CMSEXPORT cmsFreeNamedColorList(cmsNAMEDCOLORLIST* v)
781
{
782
if (v == NULL) return;
783
if (v ->List) _cmsFree(v ->ContextID, v ->List);
784
_cmsFree(v ->ContextID, v);
785
}
786
787
cmsNAMEDCOLORLIST* CMSEXPORT cmsDupNamedColorList(const cmsNAMEDCOLORLIST* v)
788
{
789
cmsNAMEDCOLORLIST* NewNC;
790
791
if (v == NULL) return NULL;
792
793
NewNC= cmsAllocNamedColorList(v ->ContextID, v -> nColors, v ->ColorantCount, v ->Prefix, v ->Suffix);
794
if (NewNC == NULL) return NULL;
795
796
// For really large tables we need this
797
while (NewNC ->Allocated < v ->Allocated){
798
if (!GrowNamedColorList(NewNC))
799
{
800
cmsFreeNamedColorList(NewNC);
801
return NULL;
802
}
803
}
804
805
memmove(NewNC ->Prefix, v ->Prefix, sizeof(v ->Prefix));
806
memmove(NewNC ->Suffix, v ->Suffix, sizeof(v ->Suffix));
807
NewNC ->ColorantCount = v ->ColorantCount;
808
memmove(NewNC->List, v ->List, v->nColors * sizeof(_cmsNAMEDCOLOR));
809
NewNC ->nColors = v ->nColors;
810
return NewNC;
811
}
812
813
814
// Append a color to a list. List pointer may change if reallocated
815
cmsBool CMSEXPORT cmsAppendNamedColor(cmsNAMEDCOLORLIST* NamedColorList,
816
const char* Name,
817
cmsUInt16Number PCS[3], cmsUInt16Number Colorant[cmsMAXCHANNELS])
818
{
819
cmsUInt32Number i;
820
821
if (NamedColorList == NULL) return FALSE;
822
823
if (NamedColorList ->nColors + 1 > NamedColorList ->Allocated) {
824
if (!GrowNamedColorList(NamedColorList)) return FALSE;
825
}
826
827
for (i=0; i < NamedColorList ->ColorantCount; i++)
828
NamedColorList ->List[NamedColorList ->nColors].DeviceColorant[i] = Colorant == NULL ? (cmsUInt16Number)0 : Colorant[i];
829
830
for (i=0; i < 3; i++)
831
NamedColorList ->List[NamedColorList ->nColors].PCS[i] = PCS == NULL ? (cmsUInt16Number) 0 : PCS[i];
832
833
if (Name != NULL) {
834
835
strncpy(NamedColorList ->List[NamedColorList ->nColors].Name, Name, cmsMAX_PATH-1);
836
NamedColorList ->List[NamedColorList ->nColors].Name[cmsMAX_PATH-1] = 0;
837
838
}
839
else
840
NamedColorList ->List[NamedColorList ->nColors].Name[0] = 0;
841
842
843
NamedColorList ->nColors++;
844
return TRUE;
845
}
846
847
// Returns number of elements
848
cmsUInt32Number CMSEXPORT cmsNamedColorCount(const cmsNAMEDCOLORLIST* NamedColorList)
849
{
850
if (NamedColorList == NULL) return 0;
851
return NamedColorList ->nColors;
852
}
853
854
// Info about a given color
855
cmsBool CMSEXPORT cmsNamedColorInfo(const cmsNAMEDCOLORLIST* NamedColorList, cmsUInt32Number nColor,
856
char* Name,
857
char* Prefix,
858
char* Suffix,
859
cmsUInt16Number* PCS,
860
cmsUInt16Number* Colorant)
861
{
862
if (NamedColorList == NULL) return FALSE;
863
864
if (nColor >= cmsNamedColorCount(NamedColorList)) return FALSE;
865
866
// strcpy instead of strncpy because many apps are using small buffers
867
if (Name) strcpy(Name, NamedColorList->List[nColor].Name);
868
if (Prefix) strcpy(Prefix, NamedColorList->Prefix);
869
if (Suffix) strcpy(Suffix, NamedColorList->Suffix);
870
if (PCS)
871
memmove(PCS, NamedColorList ->List[nColor].PCS, 3*sizeof(cmsUInt16Number));
872
873
if (Colorant)
874
memmove(Colorant, NamedColorList ->List[nColor].DeviceColorant,
875
sizeof(cmsUInt16Number) * NamedColorList ->ColorantCount);
876
877
878
return TRUE;
879
}
880
881
// Search for a given color name (no prefix or suffix)
882
cmsInt32Number CMSEXPORT cmsNamedColorIndex(const cmsNAMEDCOLORLIST* NamedColorList, const char* Name)
883
{
884
cmsUInt32Number i;
885
cmsUInt32Number n;
886
887
if (NamedColorList == NULL) return -1;
888
n = cmsNamedColorCount(NamedColorList);
889
for (i=0; i < n; i++) {
890
if (cmsstrcasecmp(Name, NamedColorList->List[i].Name) == 0)
891
return (cmsInt32Number) i;
892
}
893
894
return -1;
895
}
896
897
// MPE support -----------------------------------------------------------------------------------------------------------------
898
899
static
900
void FreeNamedColorList(cmsStage* mpe)
901
{
902
cmsNAMEDCOLORLIST* List = (cmsNAMEDCOLORLIST*) mpe ->Data;
903
cmsFreeNamedColorList(List);
904
}
905
906
static
907
void* DupNamedColorList(cmsStage* mpe)
908
{
909
cmsNAMEDCOLORLIST* List = (cmsNAMEDCOLORLIST*) mpe ->Data;
910
return cmsDupNamedColorList(List);
911
}
912
913
static
914
void EvalNamedColorPCS(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe)
915
{
916
cmsNAMEDCOLORLIST* NamedColorList = (cmsNAMEDCOLORLIST*) mpe ->Data;
917
cmsUInt16Number index = (cmsUInt16Number) _cmsQuickSaturateWord(In[0] * 65535.0);
918
919
if (index >= NamedColorList-> nColors) {
920
cmsSignalError(NamedColorList ->ContextID, cmsERROR_RANGE, "Color %d out of range", index);
921
Out[0] = Out[1] = Out[2] = 0.0f;
922
}
923
else {
924
925
// Named color always uses Lab
926
Out[0] = (cmsFloat32Number) (NamedColorList->List[index].PCS[0] / 65535.0);
927
Out[1] = (cmsFloat32Number) (NamedColorList->List[index].PCS[1] / 65535.0);
928
Out[2] = (cmsFloat32Number) (NamedColorList->List[index].PCS[2] / 65535.0);
929
}
930
}
931
932
static
933
void EvalNamedColor(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe)
934
{
935
cmsNAMEDCOLORLIST* NamedColorList = (cmsNAMEDCOLORLIST*) mpe ->Data;
936
cmsUInt16Number index = (cmsUInt16Number) _cmsQuickSaturateWord(In[0] * 65535.0);
937
cmsUInt32Number j;
938
939
if (index >= NamedColorList-> nColors) {
940
cmsSignalError(NamedColorList ->ContextID, cmsERROR_RANGE, "Color %d out of range", index);
941
for (j = 0; j < NamedColorList->ColorantCount; j++)
942
Out[j] = 0.0f;
943
944
}
945
else {
946
for (j=0; j < NamedColorList ->ColorantCount; j++)
947
Out[j] = (cmsFloat32Number) (NamedColorList->List[index].DeviceColorant[j] / 65535.0);
948
}
949
}
950
951
952
// Named color lookup element
953
cmsStage* CMSEXPORT _cmsStageAllocNamedColor(cmsNAMEDCOLORLIST* NamedColorList, cmsBool UsePCS)
954
{
955
return _cmsStageAllocPlaceholder(NamedColorList ->ContextID,
956
cmsSigNamedColorElemType,
957
1, UsePCS ? 3 : NamedColorList ->ColorantCount,
958
UsePCS ? EvalNamedColorPCS : EvalNamedColor,
959
DupNamedColorList,
960
FreeNamedColorList,
961
cmsDupNamedColorList(NamedColorList));
962
963
}
964
965
966
// Retrieve the named color list from a transform. Should be first element in the LUT
967
cmsNAMEDCOLORLIST* CMSEXPORT cmsGetNamedColorList(cmsHTRANSFORM xform)
968
{
969
_cmsTRANSFORM* v = (_cmsTRANSFORM*) xform;
970
cmsStage* mpe;
971
972
if (v == NULL) return NULL;
973
if (v->Lut == NULL) return NULL;
974
975
mpe = v->Lut->Elements;
976
if (mpe == NULL) return NULL;
977
978
if (mpe ->Type != cmsSigNamedColorElemType) return NULL;
979
return (cmsNAMEDCOLORLIST*) mpe ->Data;
980
}
981
982
983
// Profile sequence description routines -------------------------------------------------------------------------------------
984
985
cmsSEQ* CMSEXPORT cmsAllocProfileSequenceDescription(cmsContext ContextID, cmsUInt32Number n)
986
{
987
cmsSEQ* Seq;
988
cmsUInt32Number i;
989
990
if (n == 0) return NULL;
991
992
// In a absolutely arbitrary way, I hereby decide to allow a maxim of 255 profiles linked
993
// in a devicelink. It makes not sense anyway and may be used for exploits, so let's close the door!
994
if (n > 255) return NULL;
995
996
Seq = (cmsSEQ*) _cmsMallocZero(ContextID, sizeof(cmsSEQ));
997
if (Seq == NULL) return NULL;
998
999
Seq -> ContextID = ContextID;
1000
Seq -> seq = (cmsPSEQDESC*) _cmsCalloc(ContextID, n, sizeof(cmsPSEQDESC));
1001
Seq -> n = n;
1002
1003
if (Seq -> seq == NULL) {
1004
_cmsFree(ContextID, Seq);
1005
return NULL;
1006
}
1007
1008
for (i=0; i < n; i++) {
1009
Seq -> seq[i].Manufacturer = NULL;
1010
Seq -> seq[i].Model = NULL;
1011
Seq -> seq[i].Description = NULL;
1012
}
1013
1014
return Seq;
1015
}
1016
1017
void CMSEXPORT cmsFreeProfileSequenceDescription(cmsSEQ* pseq)
1018
{
1019
cmsUInt32Number i;
1020
1021
if (pseq == NULL)
1022
return;
1023
1024
if (pseq ->seq != NULL) {
1025
for (i=0; i < pseq ->n; i++) {
1026
if (pseq ->seq[i].Manufacturer != NULL) cmsMLUfree(pseq ->seq[i].Manufacturer);
1027
if (pseq ->seq[i].Model != NULL) cmsMLUfree(pseq ->seq[i].Model);
1028
if (pseq ->seq[i].Description != NULL) cmsMLUfree(pseq ->seq[i].Description);
1029
}
1030
1031
_cmsFree(pseq ->ContextID, pseq ->seq);
1032
}
1033
1034
_cmsFree(pseq -> ContextID, pseq);
1035
}
1036
1037
cmsSEQ* CMSEXPORT cmsDupProfileSequenceDescription(const cmsSEQ* pseq)
1038
{
1039
cmsSEQ *NewSeq;
1040
cmsUInt32Number i;
1041
1042
if (pseq == NULL)
1043
return NULL;
1044
1045
NewSeq = (cmsSEQ*) _cmsMalloc(pseq -> ContextID, sizeof(cmsSEQ));
1046
if (NewSeq == NULL) return NULL;
1047
1048
1049
NewSeq -> seq = (cmsPSEQDESC*) _cmsCalloc(pseq ->ContextID, pseq ->n, sizeof(cmsPSEQDESC));
1050
if (NewSeq ->seq == NULL) goto Error;
1051
1052
NewSeq -> ContextID = pseq ->ContextID;
1053
NewSeq -> n = pseq ->n;
1054
1055
for (i=0; i < pseq->n; i++) {
1056
1057
memmove(&NewSeq ->seq[i].attributes, &pseq ->seq[i].attributes, sizeof(cmsUInt64Number));
1058
1059
NewSeq ->seq[i].deviceMfg = pseq ->seq[i].deviceMfg;
1060
NewSeq ->seq[i].deviceModel = pseq ->seq[i].deviceModel;
1061
memmove(&NewSeq ->seq[i].ProfileID, &pseq ->seq[i].ProfileID, sizeof(cmsProfileID));
1062
NewSeq ->seq[i].technology = pseq ->seq[i].technology;
1063
1064
NewSeq ->seq[i].Manufacturer = cmsMLUdup(pseq ->seq[i].Manufacturer);
1065
NewSeq ->seq[i].Model = cmsMLUdup(pseq ->seq[i].Model);
1066
NewSeq ->seq[i].Description = cmsMLUdup(pseq ->seq[i].Description);
1067
1068
}
1069
1070
return NewSeq;
1071
1072
Error:
1073
1074
cmsFreeProfileSequenceDescription(NewSeq);
1075
return NULL;
1076
}
1077
1078
// Dictionaries --------------------------------------------------------------------------------------------------------
1079
1080
// Dictionaries are just very simple linked lists
1081
1082
1083
typedef struct _cmsDICT_struct {
1084
cmsDICTentry* head;
1085
cmsContext ContextID;
1086
} _cmsDICT;
1087
1088
1089
// Allocate an empty dictionary
1090
cmsHANDLE CMSEXPORT cmsDictAlloc(cmsContext ContextID)
1091
{
1092
_cmsDICT* dict = (_cmsDICT*) _cmsMallocZero(ContextID, sizeof(_cmsDICT));
1093
if (dict == NULL) return NULL;
1094
1095
dict ->ContextID = ContextID;
1096
return (cmsHANDLE) dict;
1097
1098
}
1099
1100
// Dispose resources
1101
void CMSEXPORT cmsDictFree(cmsHANDLE hDict)
1102
{
1103
_cmsDICT* dict = (_cmsDICT*) hDict;
1104
cmsDICTentry *entry, *next;
1105
1106
_cmsAssert(dict != NULL);
1107
1108
// Walk the list freeing all nodes
1109
entry = dict ->head;
1110
while (entry != NULL) {
1111
1112
if (entry ->DisplayName != NULL) cmsMLUfree(entry ->DisplayName);
1113
if (entry ->DisplayValue != NULL) cmsMLUfree(entry ->DisplayValue);
1114
if (entry ->Name != NULL) _cmsFree(dict ->ContextID, entry -> Name);
1115
if (entry ->Value != NULL) _cmsFree(dict ->ContextID, entry -> Value);
1116
1117
// Don't fall in the habitual trap...
1118
next = entry ->Next;
1119
_cmsFree(dict ->ContextID, entry);
1120
1121
entry = next;
1122
}
1123
1124
_cmsFree(dict ->ContextID, dict);
1125
}
1126
1127
1128
// Duplicate a wide char string
1129
static
1130
wchar_t* DupWcs(cmsContext ContextID, const wchar_t* ptr)
1131
{
1132
if (ptr == NULL) return NULL;
1133
return (wchar_t*) _cmsDupMem(ContextID, ptr, (mywcslen(ptr) + 1) * sizeof(wchar_t));
1134
}
1135
1136
// Add a new entry to the linked list
1137
cmsBool CMSEXPORT cmsDictAddEntry(cmsHANDLE hDict, const wchar_t* Name, const wchar_t* Value, const cmsMLU *DisplayName, const cmsMLU *DisplayValue)
1138
{
1139
_cmsDICT* dict = (_cmsDICT*) hDict;
1140
cmsDICTentry *entry;
1141
1142
_cmsAssert(dict != NULL);
1143
_cmsAssert(Name != NULL);
1144
1145
entry = (cmsDICTentry*) _cmsMallocZero(dict ->ContextID, sizeof(cmsDICTentry));
1146
if (entry == NULL) return FALSE;
1147
1148
entry ->DisplayName = cmsMLUdup(DisplayName);
1149
entry ->DisplayValue = cmsMLUdup(DisplayValue);
1150
entry ->Name = DupWcs(dict ->ContextID, Name);
1151
entry ->Value = DupWcs(dict ->ContextID, Value);
1152
1153
entry ->Next = dict ->head;
1154
dict ->head = entry;
1155
1156
return TRUE;
1157
}
1158
1159
1160
// Duplicates an existing dictionary
1161
cmsHANDLE CMSEXPORT cmsDictDup(cmsHANDLE hDict)
1162
{
1163
_cmsDICT* old_dict = (_cmsDICT*) hDict;
1164
cmsHANDLE hNew;
1165
cmsDICTentry *entry;
1166
1167
_cmsAssert(old_dict != NULL);
1168
1169
hNew = cmsDictAlloc(old_dict ->ContextID);
1170
if (hNew == NULL) return NULL;
1171
1172
// Walk the list freeing all nodes
1173
entry = old_dict ->head;
1174
while (entry != NULL) {
1175
1176
if (!cmsDictAddEntry(hNew, entry ->Name, entry ->Value, entry ->DisplayName, entry ->DisplayValue)) {
1177
1178
cmsDictFree(hNew);
1179
return NULL;
1180
}
1181
1182
entry = entry -> Next;
1183
}
1184
1185
return hNew;
1186
}
1187
1188
// Get a pointer to the linked list
1189
const cmsDICTentry* CMSEXPORT cmsDictGetEntryList(cmsHANDLE hDict)
1190
{
1191
_cmsDICT* dict = (_cmsDICT*) hDict;
1192
1193
if (dict == NULL) return NULL;
1194
return dict ->head;
1195
}
1196
1197
// Helper For external languages
1198
const cmsDICTentry* CMSEXPORT cmsDictNextEntry(const cmsDICTentry* e)
1199
{
1200
if (e == NULL) return NULL;
1201
return e ->Next;
1202
}
1203
1204