Coverage Report

Created: 2025-07-07 10:01

/work/workdir/UnpackedTarball/lcms2/src/cmsnamed.c
Line
Count
Source (jump to first uncovered line)
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
0
{
35
0
    cmsMLU* mlu;
36
37
    // nItems should be positive if given
38
0
    if (nItems <= 0) nItems = 2;
39
40
    // Create the container
41
0
    mlu = (cmsMLU*) _cmsMallocZero(ContextID, sizeof(cmsMLU));
42
0
    if (mlu == NULL) return NULL;
43
44
0
    mlu ->ContextID = ContextID;
45
46
    // Create entry array
47
0
    mlu ->Entries = (_cmsMLUentry*) _cmsCalloc(ContextID, nItems, sizeof(_cmsMLUentry));
48
0
    if (mlu ->Entries == NULL) {
49
0
        _cmsFree(ContextID, mlu);
50
0
        return NULL;
51
0
    }
52
53
    // Ok, keep indexes up to date
54
0
    mlu ->AllocatedEntries    = nItems;
55
0
    mlu ->UsedEntries         = 0;
56
57
0
    return mlu;
58
0
}
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
0
{
65
0
    cmsUInt32Number size;
66
0
    void *NewPtr;
67
68
    // Sanity check
69
0
    if (mlu == NULL) return FALSE;
70
71
0
    if (mlu ->PoolSize == 0)
72
0
        size = 256;
73
0
    else
74
0
        size = mlu ->PoolSize * 2;
75
76
    // Check for overflow
77
0
    if (size < mlu ->PoolSize) return FALSE;
78
79
    // Reallocate the pool
80
0
    NewPtr = _cmsRealloc(mlu ->ContextID, mlu ->MemPool, size);
81
0
    if (NewPtr == NULL) return FALSE;
82
83
84
0
    mlu ->MemPool  = NewPtr;
85
0
    mlu ->PoolSize = size;
86
87
0
    return TRUE;
88
0
}
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
0
{
95
0
    cmsUInt32Number AllocatedEntries;
96
0
    _cmsMLUentry *NewPtr;
97
98
    // Sanity check
99
0
    if (mlu == NULL) return FALSE;
100
101
0
    AllocatedEntries = mlu ->AllocatedEntries * 2;
102
103
    // Check for overflow
104
0
    if (AllocatedEntries / 2 != mlu ->AllocatedEntries) return FALSE;
105
106
    // Reallocate the memory
107
0
    NewPtr = (_cmsMLUentry*)_cmsRealloc(mlu ->ContextID, mlu ->Entries, AllocatedEntries*sizeof(_cmsMLUentry));
108
0
    if (NewPtr == NULL) return FALSE;
109
110
0
    mlu ->Entries          = NewPtr;
111
0
    mlu ->AllocatedEntries = AllocatedEntries;
112
113
0
    return TRUE;
114
0
}
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
0
{
121
0
    cmsUInt32Number i;
122
123
    // Sanity check
124
0
    if (mlu == NULL) return -1;
125
126
    // Iterate whole table
127
0
    for (i=0; i < mlu ->UsedEntries; i++) {
128
129
0
        if (mlu ->Entries[i].Country  == CountryCode &&
130
0
            mlu ->Entries[i].Language == LanguageCode) return (int) i;
131
0
    }
132
133
    // Not found
134
0
    return -1;
135
0
}
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
0
{
143
0
    cmsUInt32Number Offset;
144
0
    cmsUInt8Number* Ptr;
145
146
    // Sanity check
147
0
    if (mlu == NULL) return FALSE;
148
149
    // Is there any room available?
150
0
    if (mlu ->UsedEntries >= mlu ->AllocatedEntries) {
151
0
        if (!GrowMLUtable(mlu)) return FALSE;
152
0
    }
153
154
    // Only one ASCII string
155
0
    if (SearchMLUEntry(mlu, LanguageCode, CountryCode) >= 0) return FALSE;  // Only one  is allowed!
156
157
    // Check for size
158
0
    while ((mlu ->PoolSize - mlu ->PoolUsed) < size) {
159
160
0
            if (!GrowMLUpool(mlu)) return FALSE;
161
0
    }
162
163
0
    Offset = mlu ->PoolUsed;
164
165
0
    Ptr = (cmsUInt8Number*) mlu ->MemPool;
166
0
    if (Ptr == NULL) return FALSE;
167
168
    // Set the entry
169
0
    memmove(Ptr + Offset, Block, size);
170
0
    mlu ->PoolUsed += size;
171
172
0
    mlu ->Entries[mlu ->UsedEntries].StrW     = Offset;
173
0
    mlu ->Entries[mlu ->UsedEntries].Len      = size;
174
0
    mlu ->Entries[mlu ->UsedEntries].Country  = CountryCode;
175
0
    mlu ->Entries[mlu ->UsedEntries].Language = LanguageCode;
176
0
    mlu ->UsedEntries++;
177
178
0
    return TRUE;
179
0
}
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
0
{   
186
0
    const cmsUInt8Number* ptr8;
187
0
    cmsUInt16Number n;
188
189
    // For non-existent strings
190
0
    if (str == NULL) return 0;
191
0
    ptr8 = (const cmsUInt8Number*)str;
192
0
    n = (cmsUInt16Number)(((cmsUInt16Number)ptr8[0] << 8) | ptr8[1]);
193
194
0
    return n;
195
0
}
196
197
static
198
void strFrom16(char str[3], cmsUInt16Number n)
199
0
{
200
0
    str[0] = (char)(n >> 8);
201
0
    str[1] = (char)n;
202
0
    str[2] = (char)0;
203
0
}
204
205
206
// Convert from UTF8 to wchar, returns len.
207
static
208
cmsUInt32Number decodeUTF8(wchar_t* out, const char* in)
209
0
{    
210
0
    cmsUInt32Number codepoint = 0;    
211
0
    cmsUInt32Number size = 0;
212
213
0
    while (*in)
214
0
    {
215
0
        cmsUInt8Number ch = (cmsUInt8Number) *in;
216
217
0
        if (ch <= 0x7f)
218
0
        {
219
0
            codepoint = ch;
220
0
        }
221
0
        else if (ch <= 0xbf)
222
0
        {
223
0
            codepoint = (codepoint << 6) | (ch & 0x3f);
224
0
        }
225
0
        else if (ch <= 0xdf)
226
0
        {
227
0
            codepoint = ch & 0x1f;
228
0
        }
229
0
        else if (ch <= 0xef)
230
0
        {
231
0
            codepoint = ch & 0x0f;
232
0
        }
233
0
        else
234
0
        {
235
0
            codepoint = ch & 0x07;
236
0
        }
237
238
0
        in++; 
239
240
0
        if (((*in & 0xc0) != 0x80) && (codepoint <= 0x10ffff))
241
0
        {
242
0
            if (sizeof(wchar_t) > 2)
243
0
            {
244
0
                if (out) *out++ = (wchar_t) codepoint;
245
0
                size++;
246
0
            }
247
0
            else
248
0
                if (codepoint > 0xffff)
249
0
                {
250
0
                    if (out)
251
0
                    {
252
0
                        *out++ = (wchar_t)(0xd800 + (codepoint >> 10));
253
0
                        *out++ = (wchar_t)(0xdc00 + (codepoint & 0x03ff));
254
0
                        size += 2;
255
0
                    }
256
0
                }
257
0
                else
258
0
                    if (codepoint < 0xd800 || codepoint >= 0xe000)
259
0
                    {
260
0
                        if (out) *out++ = (wchar_t) codepoint;
261
0
                        size++;
262
0
                    }
263
0
        }
264
0
    }   
265
    
266
0
    return size;
267
0
}
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
0
{       
273
0
    cmsUInt32Number codepoint = 0;
274
0
    cmsUInt32Number size = 0;
275
0
    cmsUInt32Number len_w = 0;
276
        
277
0
    while (*in && len_w < max_wchars)
278
0
    {
279
0
        if (*in >= 0xd800 && *in <= 0xdbff)
280
0
            codepoint = ((*in - 0xd800) << 10) + 0x10000;
281
0
        else
282
0
        {
283
0
            if (*in >= 0xdc00 && *in <= 0xdfff)
284
0
                codepoint |= *in - 0xdc00;
285
0
            else
286
0
                codepoint = *in;
287
288
0
            if (codepoint <= 0x7f)
289
0
            {
290
0
                if (out && (size + 1 < max_chars)) *out++ = (char)codepoint;
291
0
                size++;
292
0
            }
293
294
0
            else if (codepoint <= 0x7ff)
295
0
            {
296
0
                if (out && (max_chars > 0) && (size + 2 < max_chars))
297
0
                {
298
0
                    *out++ = (char)(cmsUInt32Number)(0xc0 | ((codepoint >> 6) & 0x1f));
299
0
                    *out++ = (char)(cmsUInt32Number)(0x80 | (codepoint & 0x3f));                    
300
0
                }
301
0
                size += 2;
302
0
            }
303
0
            else if (codepoint <= 0xffff)
304
0
            {
305
0
                if (out && (max_chars > 0) && (size + 3 < max_chars))
306
0
                {
307
0
                    *out++ = (char)(cmsUInt32Number)(0xe0 | ((codepoint >> 12) & 0x0f));
308
0
                    *out++ = (char)(cmsUInt32Number)(0x80 | ((codepoint >> 6) & 0x3f));
309
0
                    *out++ = (char)(cmsUInt32Number)(0x80 | (codepoint & 0x3f));
310
0
                }
311
0
                size += 3;
312
0
            }
313
0
            else
314
0
            {
315
0
                if (out && (max_chars > 0) && (size + 4 < max_chars))
316
0
                {
317
0
                    *out++ = (char)(cmsUInt32Number)(0xf0 | ((codepoint >> 18) & 0x07));
318
0
                    *out++ = (char)(cmsUInt32Number)(0x80 | ((codepoint >> 12) & 0x3f));
319
0
                    *out++ = (char)(cmsUInt32Number)(0x80 | ((codepoint >> 6) & 0x3f));
320
0
                    *out++ = (char)(cmsUInt32Number)(0x80 | (codepoint & 0x3f));
321
0
                }
322
0
                size += 4;
323
0
            }
324
325
0
            codepoint = 0;
326
0
        }
327
328
0
        in++; len_w++;         
329
0
    }
330
331
0
    return size;
332
0
}
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
0
{
338
0
    cmsUInt32Number i, len = (cmsUInt32Number)strlen(ASCIIString);
339
0
    wchar_t* WStr;
340
0
    cmsBool  rc;
341
0
    cmsUInt16Number Lang = strTo16(LanguageCode);
342
0
    cmsUInt16Number Cntry = strTo16(CountryCode);
343
344
0
    if (mlu == NULL) return FALSE;
345
346
    // len == 0 would prevent operation, so we set a empty string pointing to zero
347
0
    if (len == 0)
348
0
    {
349
0
        wchar_t empty = 0;
350
0
        return AddMLUBlock(mlu, sizeof(wchar_t), &empty, Lang, Cntry);
351
0
    }
352
353
0
    WStr = (wchar_t*)_cmsCalloc(mlu->ContextID, len, sizeof(wchar_t));
354
0
    if (WStr == NULL) return FALSE;
355
356
0
    for (i = 0; i < len; i++)
357
0
        WStr[i] = (wchar_t)ASCIIString[i];
358
359
0
    rc = AddMLUBlock(mlu, len * sizeof(wchar_t), WStr, Lang, Cntry);
360
361
0
    _cmsFree(mlu->ContextID, WStr);
362
0
    return rc;
363
364
0
}
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
0
{
370
0
    cmsUInt32Number UTF8len;
371
0
    wchar_t* WStr;
372
0
    cmsBool  rc;
373
0
    cmsUInt16Number Lang  = strTo16(LanguageCode);
374
0
    cmsUInt16Number Cntry = strTo16(CountryCode);
375
376
0
    if (mlu == NULL) return FALSE;
377
378
0
    if (*UTF8String == '\0')
379
0
    {
380
0
        wchar_t empty = 0;
381
0
        return AddMLUBlock(mlu, sizeof(wchar_t), &empty, Lang, Cntry);
382
0
    }
383
    
384
    // Len excluding terminator 0
385
0
    UTF8len = decodeUTF8(NULL, UTF8String);
386
    
387
    // Get space for dest
388
0
    WStr = (wchar_t*) _cmsCalloc(mlu ->ContextID, UTF8len,  sizeof(wchar_t));
389
0
    if (WStr == NULL) return FALSE;
390
391
0
    decodeUTF8(WStr, UTF8String);
392
    
393
0
    rc = AddMLUBlock(mlu, UTF8len  * sizeof(wchar_t), WStr, Lang, Cntry);
394
395
0
    _cmsFree(mlu ->ContextID, WStr);
396
0
    return rc;
397
0
}
398
399
// We don't need any wcs support library
400
static
401
cmsUInt32Number mywcslen(const wchar_t *s)
402
0
{
403
0
    const wchar_t *p;
404
405
0
    p = s;
406
0
    while (*p)
407
0
        p++;
408
409
0
    return (cmsUInt32Number)(p - s);
410
0
}
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
0
{
415
0
    cmsUInt16Number Lang  = strTo16(Language);
416
0
    cmsUInt16Number Cntry = strTo16(Country);
417
0
    cmsUInt32Number len;
418
419
0
    if (mlu == NULL) return FALSE;
420
0
    if (WideString == NULL) return FALSE;
421
422
0
    len = (cmsUInt32Number) (mywcslen(WideString)) * sizeof(wchar_t);
423
0
    if (len == 0)
424
0
        len = sizeof(wchar_t);
425
426
0
    return AddMLUBlock(mlu, len, WideString, Lang, Cntry);
427
0
}
428
429
// Duplicating a MLU is as easy as copying all members
430
cmsMLU* CMSEXPORT cmsMLUdup(const cmsMLU* mlu)
431
0
{
432
0
    cmsMLU* NewMlu = NULL;
433
434
    // Duplicating a NULL obtains a NULL
435
0
    if (mlu == NULL) return NULL;
436
437
0
    NewMlu = cmsMLUalloc(mlu ->ContextID, mlu ->UsedEntries);
438
0
    if (NewMlu == NULL) return NULL;
439
440
    // Should never happen
441
0
    if (NewMlu ->AllocatedEntries < mlu ->UsedEntries)
442
0
        goto Error;
443
444
    // Sanitize...
445
0
    if (NewMlu ->Entries == NULL || mlu ->Entries == NULL)  goto Error;
446
447
0
    memmove(NewMlu ->Entries, mlu ->Entries, mlu ->UsedEntries * sizeof(_cmsMLUentry));
448
0
    NewMlu ->UsedEntries = mlu ->UsedEntries;
449
450
    // The MLU may be empty
451
0
    if (mlu ->PoolUsed == 0) {
452
0
        NewMlu ->MemPool = NULL;
453
0
    }
454
0
    else {
455
        // It is not empty
456
0
        NewMlu ->MemPool = _cmsMalloc(mlu ->ContextID, mlu ->PoolUsed);
457
0
        if (NewMlu ->MemPool == NULL) goto Error;
458
0
    }
459
460
0
    NewMlu ->PoolSize = mlu ->PoolUsed;
461
462
0
    if (NewMlu ->MemPool == NULL || mlu ->MemPool == NULL) goto Error;
463
464
0
    memmove(NewMlu ->MemPool, mlu->MemPool, mlu ->PoolUsed);
465
0
    NewMlu ->PoolUsed = mlu ->PoolUsed;
466
467
0
    return NewMlu;
468
469
0
Error:
470
471
0
    if (NewMlu != NULL) cmsMLUfree(NewMlu);
472
0
    return NULL;
473
0
}
474
475
// Free any used memory
476
void CMSEXPORT cmsMLUfree(cmsMLU* mlu)
477
0
{
478
0
    if (mlu) {
479
480
0
        if (mlu -> Entries) _cmsFree(mlu ->ContextID, mlu->Entries);
481
0
        if (mlu -> MemPool) _cmsFree(mlu ->ContextID, mlu->MemPool);
482
483
0
        _cmsFree(mlu ->ContextID, mlu);
484
0
    }
485
0
}
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
0
{
496
0
    cmsUInt32Number i;
497
0
    int Best = -1;
498
0
    _cmsMLUentry* v;
499
500
0
    if (mlu == NULL) return NULL;
501
502
0
    if (mlu -> AllocatedEntries <= 0) return NULL;
503
504
0
    for (i=0; i < mlu ->UsedEntries; i++) {
505
506
0
        v = mlu ->Entries + i;
507
508
0
        if (v -> Language == LanguageCode) {
509
510
0
            if (Best == -1) Best = (int) i;
511
512
0
            if (v -> Country == CountryCode) {
513
514
0
                if (UsedLanguageCode != NULL) *UsedLanguageCode = v ->Language;
515
0
                if (UsedCountryCode  != NULL) *UsedCountryCode = v ->Country;
516
517
0
                if (len != NULL) *len = v ->Len;
518
519
0
                return (wchar_t*) ((cmsUInt8Number*) mlu ->MemPool + v -> StrW);        // Found exact match
520
0
            }
521
0
        }
522
0
    }
523
524
    // No string found. Return First one
525
0
    if (Best == -1)
526
0
        Best = 0;
527
528
0
    v = mlu ->Entries + Best;
529
530
0
    if (UsedLanguageCode != NULL) *UsedLanguageCode = v ->Language;
531
0
    if (UsedCountryCode  != NULL) *UsedCountryCode = v ->Country;
532
533
0
    if (len != NULL) *len   = v ->Len;
534
535
0
    if (v->StrW + v->Len > mlu->PoolSize) return NULL;
536
537
0
    return (wchar_t*) ((cmsUInt8Number*) mlu ->MemPool + v ->StrW);
538
0
}
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
0
{
546
0
    const wchar_t *Wide;
547
0
    cmsUInt32Number  StrLen = 0;
548
0
    cmsUInt32Number ASCIIlen, i;
549
550
0
    cmsUInt16Number Lang  = strTo16(LanguageCode);
551
0
    cmsUInt16Number Cntry = strTo16(CountryCode);
552
553
    // Sanitize
554
0
    if (mlu == NULL) return 0;
555
556
    // Get WideChar
557
0
    Wide = _cmsMLUgetWide(mlu, &StrLen, Lang, Cntry, NULL, NULL);
558
0
    if (Wide == NULL) return 0;
559
560
0
    ASCIIlen = StrLen / sizeof(wchar_t);
561
562
    // Maybe we want only to know the len?
563
0
    if (Buffer == NULL) return ASCIIlen + 1; // Note the zero at the end
564
565
    // No buffer size means no data
566
0
    if (BufferSize <= 0) return 0;
567
568
    // Some clipping may be required
569
0
    if (BufferSize < ASCIIlen + 1)
570
0
        ASCIIlen = BufferSize - 1;
571
572
    // Process each character
573
0
    for (i=0; i < ASCIIlen; i++) {
574
575
0
        wchar_t wc = Wide[i];
576
577
0
        if (wc < 0xff)
578
0
            Buffer[i] = (char)wc;
579
0
        else
580
0
            Buffer[i] = '?';
581
0
    }
582
583
    // We put a termination "\0"
584
0
    Buffer[ASCIIlen] = 0;
585
0
    return ASCIIlen + 1;
586
0
}
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
0
{
594
0
    const wchar_t *Wide;
595
0
    cmsUInt32Number  StrLen = 0;
596
0
    cmsUInt32Number UTF8len;
597
598
0
    cmsUInt16Number Lang  = strTo16(LanguageCode);
599
0
    cmsUInt16Number Cntry = strTo16(CountryCode);
600
601
    // Sanitize
602
0
    if (mlu == NULL) return 0;
603
604
    // Get WideChar
605
0
    Wide = _cmsMLUgetWide(mlu, &StrLen, Lang, Cntry, NULL, NULL);
606
0
    if (Wide == NULL) return 0;
607
608
0
    UTF8len = encodeUTF8(NULL, Wide, StrLen / sizeof(wchar_t), BufferSize);
609
       
610
    // Maybe we want only to know the len?
611
0
    if (Buffer == NULL) return UTF8len + 1; // Note the zero at the end
612
613
    // No buffer size means no data
614
0
    if (BufferSize <= 0) return 0;
615
616
    // Some clipping may be required
617
0
    if (BufferSize < UTF8len + 1)
618
0
        UTF8len = BufferSize - 1;
619
620
    // Process it
621
0
    encodeUTF8(Buffer, Wide, StrLen / sizeof(wchar_t), BufferSize);    
622
623
    // We put a termination "\0"
624
0
    Buffer[UTF8len] = 0;
625
0
    return UTF8len + 1;
626
0
}
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
0
{
633
0
    const wchar_t *Wide;
634
0
    cmsUInt32Number  StrLen = 0;
635
636
0
    cmsUInt16Number Lang  = strTo16(LanguageCode);
637
0
    cmsUInt16Number Cntry = strTo16(CountryCode);
638
639
    // Sanitize
640
0
    if (mlu == NULL) return 0;
641
642
0
    Wide = _cmsMLUgetWide(mlu, &StrLen, Lang, Cntry, NULL, NULL);
643
0
    if (Wide == NULL) return 0;
644
645
    // Maybe we want only to know the len?
646
0
    if (Buffer == NULL) return StrLen + sizeof(wchar_t);
647
648
    // Invalid buffer size means no data
649
0
    if (BufferSize < sizeof(wchar_t)) return 0;
650
651
    // Some clipping may be required
652
0
    if (BufferSize < StrLen + sizeof(wchar_t))
653
0
        StrLen = BufferSize - sizeof(wchar_t);
654
655
0
    memmove(Buffer, Wide, StrLen);
656
0
    Buffer[StrLen / sizeof(wchar_t)] = 0;
657
658
0
    return StrLen + sizeof(wchar_t);
659
0
}
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
0
{
667
0
    const wchar_t *Wide;
668
669
0
    cmsUInt16Number Lang  = strTo16(LanguageCode);
670
0
    cmsUInt16Number Cntry = strTo16(CountryCode);
671
0
    cmsUInt16Number ObtLang, ObtCode;
672
673
    // Sanitize
674
0
    if (mlu == NULL) return FALSE;
675
676
0
    Wide = _cmsMLUgetWide(mlu, NULL, Lang, Cntry, &ObtLang, &ObtCode);
677
0
    if (Wide == NULL) return FALSE;
678
679
    // Get used language and code
680
0
    strFrom16(ObtainedLanguage, ObtLang);
681
0
    strFrom16(ObtainedCountry, ObtCode);
682
683
0
    return TRUE;
684
0
}
685
686
687
688
// Get the number of translations in the MLU object
689
cmsUInt32Number CMSEXPORT cmsMLUtranslationsCount(const cmsMLU* mlu)
690
0
{
691
0
    if (mlu == NULL) return 0;
692
0
    return mlu->UsedEntries;
693
0
}
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
0
{
701
0
    _cmsMLUentry *entry;
702
703
0
    if (mlu == NULL) return FALSE;
704
705
0
    if (idx >= mlu->UsedEntries) return FALSE;
706
707
0
    entry = &mlu->Entries[idx];
708
    
709
0
    strFrom16(LanguageCode, entry->Language);
710
0
    strFrom16(CountryCode, entry->Country);
711
712
0
    return TRUE;
713
0
}
714
715
716
// Named color lists --------------------------------------------------------------------------------------------
717
718
// Grow the list to keep at least NumElements
719
static
720
cmsBool  GrowNamedColorList(cmsNAMEDCOLORLIST* v)
721
0
{
722
0
    cmsUInt32Number size;
723
0
    _cmsNAMEDCOLOR * NewPtr;
724
725
0
    if (v == NULL) return FALSE;
726
727
0
    if (v ->Allocated == 0)
728
0
        size = 64;   // Initial guess
729
0
    else
730
0
        size = v ->Allocated * 2;
731
732
    // Keep a maximum color lists can grow, 100K entries seems reasonable
733
0
    if (size > 1024 * 100) {
734
0
        _cmsFree(v->ContextID, (void*) v->List);
735
0
        v->List = NULL;
736
0
        return FALSE;
737
0
    }
738
739
0
    NewPtr = (_cmsNAMEDCOLOR*) _cmsRealloc(v ->ContextID, v ->List, size * sizeof(_cmsNAMEDCOLOR));
740
0
    if (NewPtr == NULL)
741
0
        return FALSE;
742
743
0
    v ->List      = NewPtr;
744
0
    v ->Allocated = size;
745
0
    return TRUE;
746
0
}
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
0
{
751
0
    cmsNAMEDCOLORLIST* v;
752
    
753
0
    if (ColorantCount > cmsMAXCHANNELS) 
754
0
        return NULL;
755
   
756
0
    v = (cmsNAMEDCOLORLIST*)_cmsMallocZero(ContextID, sizeof(cmsNAMEDCOLORLIST));
757
0
    if (v == NULL) return NULL;
758
    
759
0
    v ->List      = NULL;
760
0
    v ->nColors   = 0;
761
0
    v ->ContextID  = ContextID;
762
763
0
    while (v -> Allocated < n) {
764
0
        if (!GrowNamedColorList(v)) {
765
0
            cmsFreeNamedColorList(v);
766
0
            return NULL;
767
0
        }
768
0
    }
769
770
0
    strncpy(v ->Prefix, Prefix, sizeof(v ->Prefix)-1);
771
0
    strncpy(v ->Suffix, Suffix, sizeof(v ->Suffix)-1);
772
0
    v->Prefix[32] = v->Suffix[32] = 0;
773
774
0
    v -> ColorantCount = ColorantCount;
775
776
0
    return v;
777
0
}
778
779
// Free a list
780
void CMSEXPORT cmsFreeNamedColorList(cmsNAMEDCOLORLIST* v)
781
0
{
782
0
    if (v == NULL) return;
783
0
    if (v ->List) _cmsFree(v ->ContextID, v ->List);
784
0
    _cmsFree(v ->ContextID, v);
785
0
}
786
787
cmsNAMEDCOLORLIST* CMSEXPORT cmsDupNamedColorList(const cmsNAMEDCOLORLIST* v)
788
0
{
789
0
    cmsNAMEDCOLORLIST* NewNC;
790
791
0
    if (v == NULL) return NULL;
792
793
0
    NewNC= cmsAllocNamedColorList(v ->ContextID, v -> nColors, v ->ColorantCount, v ->Prefix, v ->Suffix);
794
0
    if (NewNC == NULL) return NULL;
795
796
    // For really large tables we need this
797
0
    while (NewNC ->Allocated < v ->Allocated){
798
0
        if (!GrowNamedColorList(NewNC))
799
0
        {
800
0
            cmsFreeNamedColorList(NewNC);
801
0
            return NULL;
802
0
        }
803
0
    }
804
805
0
    memmove(NewNC ->Prefix, v ->Prefix, sizeof(v ->Prefix));
806
0
    memmove(NewNC ->Suffix, v ->Suffix, sizeof(v ->Suffix));
807
0
    NewNC ->ColorantCount = v ->ColorantCount;
808
0
    memmove(NewNC->List, v ->List, v->nColors * sizeof(_cmsNAMEDCOLOR));
809
0
    NewNC ->nColors = v ->nColors;
810
0
    return NewNC;
811
0
}
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
0
{
819
0
    cmsUInt32Number i;
820
821
0
    if (NamedColorList == NULL) return FALSE;
822
823
0
    if (NamedColorList ->nColors + 1 > NamedColorList ->Allocated) {
824
0
        if (!GrowNamedColorList(NamedColorList)) return FALSE;
825
0
    }
826
827
0
    for (i=0; i < NamedColorList ->ColorantCount; i++)
828
0
        NamedColorList ->List[NamedColorList ->nColors].DeviceColorant[i] = Colorant == NULL ? (cmsUInt16Number)0 : Colorant[i];
829
830
0
    for (i=0; i < 3; i++)
831
0
        NamedColorList ->List[NamedColorList ->nColors].PCS[i] = PCS == NULL ? (cmsUInt16Number) 0 : PCS[i];
832
833
0
    if (Name != NULL) {
834
835
0
        strncpy(NamedColorList ->List[NamedColorList ->nColors].Name, Name, cmsMAX_PATH-1);
836
0
        NamedColorList ->List[NamedColorList ->nColors].Name[cmsMAX_PATH-1] = 0;
837
838
0
    }
839
0
    else
840
0
        NamedColorList ->List[NamedColorList ->nColors].Name[0] = 0;
841
842
843
0
    NamedColorList ->nColors++;
844
0
    return TRUE;
845
0
}
846
847
// Returns number of elements
848
cmsUInt32Number CMSEXPORT cmsNamedColorCount(const cmsNAMEDCOLORLIST* NamedColorList)
849
0
{
850
0
     if (NamedColorList == NULL) return 0;
851
0
     return NamedColorList ->nColors;
852
0
}
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
0
{
862
0
    if (NamedColorList == NULL) return FALSE;
863
864
0
    if (nColor >= cmsNamedColorCount(NamedColorList)) return FALSE;
865
866
    // strcpy instead of strncpy because many apps are using small buffers
867
0
    if (Name) strcpy(Name, NamedColorList->List[nColor].Name);
868
0
    if (Prefix) strcpy(Prefix, NamedColorList->Prefix);
869
0
    if (Suffix) strcpy(Suffix, NamedColorList->Suffix);
870
0
    if (PCS)
871
0
        memmove(PCS, NamedColorList ->List[nColor].PCS, 3*sizeof(cmsUInt16Number));
872
873
0
    if (Colorant)
874
0
        memmove(Colorant, NamedColorList ->List[nColor].DeviceColorant,
875
0
                                sizeof(cmsUInt16Number) * NamedColorList ->ColorantCount);
876
877
878
0
    return TRUE;
879
0
}
880
881
// Search for a given color name (no prefix or suffix)
882
cmsInt32Number CMSEXPORT cmsNamedColorIndex(const cmsNAMEDCOLORLIST* NamedColorList, const char* Name)
883
0
{
884
0
    cmsUInt32Number i;
885
0
    cmsUInt32Number n;
886
887
0
    if (NamedColorList == NULL) return -1;
888
0
    n = cmsNamedColorCount(NamedColorList);
889
0
    for (i=0; i < n; i++) {
890
0
        if (cmsstrcasecmp(Name,  NamedColorList->List[i].Name) == 0)
891
0
            return (cmsInt32Number) i;
892
0
    }
893
894
0
    return -1;
895
0
}
896
897
// MPE support -----------------------------------------------------------------------------------------------------------------
898
899
static
900
void FreeNamedColorList(cmsStage* mpe)
901
0
{
902
0
    cmsNAMEDCOLORLIST* List = (cmsNAMEDCOLORLIST*) mpe ->Data;
903
0
    cmsFreeNamedColorList(List);
904
0
}
905
906
static
907
void* DupNamedColorList(cmsStage* mpe)
908
0
{
909
0
    cmsNAMEDCOLORLIST* List = (cmsNAMEDCOLORLIST*) mpe ->Data;
910
0
    return cmsDupNamedColorList(List);
911
0
}
912
913
static
914
void EvalNamedColorPCS(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe)
915
0
{
916
0
    cmsNAMEDCOLORLIST* NamedColorList = (cmsNAMEDCOLORLIST*) mpe ->Data;
917
0
    cmsUInt16Number index = (cmsUInt16Number) _cmsQuickSaturateWord(In[0] * 65535.0);
918
919
0
    if (index >= NamedColorList-> nColors) {
920
0
        cmsSignalError(NamedColorList ->ContextID, cmsERROR_RANGE, "Color %d out of range", index);
921
0
        Out[0] = Out[1] = Out[2] = 0.0f;
922
0
    }
923
0
    else {
924
925
            // Named color always uses Lab
926
0
            Out[0] = (cmsFloat32Number) (NamedColorList->List[index].PCS[0] / 65535.0);
927
0
            Out[1] = (cmsFloat32Number) (NamedColorList->List[index].PCS[1] / 65535.0);
928
0
            Out[2] = (cmsFloat32Number) (NamedColorList->List[index].PCS[2] / 65535.0);
929
0
    }
930
0
}
931
932
static
933
void EvalNamedColor(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe)
934
0
{
935
0
    cmsNAMEDCOLORLIST* NamedColorList = (cmsNAMEDCOLORLIST*) mpe ->Data;
936
0
    cmsUInt16Number index = (cmsUInt16Number) _cmsQuickSaturateWord(In[0] * 65535.0);
937
0
    cmsUInt32Number j;
938
939
0
    if (index >= NamedColorList-> nColors) {
940
0
        cmsSignalError(NamedColorList ->ContextID, cmsERROR_RANGE, "Color %d out of range", index);
941
0
        for (j = 0; j < NamedColorList->ColorantCount; j++)
942
0
            Out[j] = 0.0f;
943
944
0
    }
945
0
    else {
946
0
        for (j=0; j < NamedColorList ->ColorantCount; j++)
947
0
            Out[j] = (cmsFloat32Number) (NamedColorList->List[index].DeviceColorant[j] / 65535.0);
948
0
    }
949
0
}
950
951
952
// Named color lookup element
953
cmsStage* CMSEXPORT _cmsStageAllocNamedColor(cmsNAMEDCOLORLIST* NamedColorList, cmsBool UsePCS)
954
0
{
955
0
    return _cmsStageAllocPlaceholder(NamedColorList ->ContextID,
956
0
                                   cmsSigNamedColorElemType,
957
0
                                   1, UsePCS ? 3 : NamedColorList ->ColorantCount,
958
0
                                   UsePCS ? EvalNamedColorPCS : EvalNamedColor,
959
0
                                   DupNamedColorList,
960
0
                                   FreeNamedColorList,
961
0
                                   cmsDupNamedColorList(NamedColorList));
962
963
0
}
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
0
{
969
0
    _cmsTRANSFORM* v = (_cmsTRANSFORM*) xform;
970
0
    cmsStage* mpe;
971
    
972
0
    if (v == NULL) return NULL;
973
0
    if (v->Lut == NULL) return NULL;
974
975
0
    mpe = v->Lut->Elements;
976
0
    if (mpe == NULL) return NULL;
977
978
0
    if (mpe ->Type != cmsSigNamedColorElemType) return NULL;
979
0
    return (cmsNAMEDCOLORLIST*) mpe ->Data;
980
0
}
981
982
983
// Profile sequence description routines -------------------------------------------------------------------------------------
984
985
cmsSEQ* CMSEXPORT cmsAllocProfileSequenceDescription(cmsContext ContextID, cmsUInt32Number n)
986
0
{
987
0
    cmsSEQ* Seq;
988
0
    cmsUInt32Number i;
989
990
0
    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
0
    if (n > 255) return NULL;
995
996
0
    Seq = (cmsSEQ*) _cmsMallocZero(ContextID, sizeof(cmsSEQ));
997
0
    if (Seq == NULL) return NULL;
998
999
0
    Seq -> ContextID = ContextID;
1000
0
    Seq -> seq      = (cmsPSEQDESC*) _cmsCalloc(ContextID, n, sizeof(cmsPSEQDESC));
1001
0
    Seq -> n        = n;
1002
1003
0
    if (Seq -> seq == NULL) {
1004
0
        _cmsFree(ContextID, Seq);
1005
0
        return NULL;
1006
0
    }
1007
1008
0
    for (i=0; i < n; i++) {
1009
0
        Seq -> seq[i].Manufacturer = NULL;
1010
0
        Seq -> seq[i].Model        = NULL;
1011
0
        Seq -> seq[i].Description  = NULL;
1012
0
    }
1013
1014
0
    return Seq;
1015
0
}
1016
1017
void CMSEXPORT cmsFreeProfileSequenceDescription(cmsSEQ* pseq)
1018
0
{
1019
0
    cmsUInt32Number i;
1020
1021
0
    if (pseq == NULL)
1022
0
        return;
1023
1024
0
    if (pseq ->seq != NULL) {
1025
0
        for (i=0; i < pseq ->n; i++) {
1026
0
            if (pseq ->seq[i].Manufacturer != NULL) cmsMLUfree(pseq ->seq[i].Manufacturer);
1027
0
            if (pseq ->seq[i].Model != NULL) cmsMLUfree(pseq ->seq[i].Model);
1028
0
            if (pseq ->seq[i].Description != NULL) cmsMLUfree(pseq ->seq[i].Description);
1029
0
        }
1030
1031
0
        _cmsFree(pseq ->ContextID, pseq ->seq);
1032
0
    }
1033
1034
0
    _cmsFree(pseq -> ContextID, pseq);
1035
0
}
1036
1037
cmsSEQ* CMSEXPORT cmsDupProfileSequenceDescription(const cmsSEQ* pseq)
1038
0
{
1039
0
    cmsSEQ *NewSeq;
1040
0
    cmsUInt32Number i;
1041
1042
0
    if (pseq == NULL)
1043
0
        return NULL;
1044
1045
0
    NewSeq = (cmsSEQ*) _cmsMalloc(pseq -> ContextID, sizeof(cmsSEQ));
1046
0
    if (NewSeq == NULL) return NULL;
1047
1048
1049
0
    NewSeq -> seq      = (cmsPSEQDESC*) _cmsCalloc(pseq ->ContextID, pseq ->n, sizeof(cmsPSEQDESC));
1050
0
    if (NewSeq ->seq == NULL) goto Error;
1051
1052
0
    NewSeq -> ContextID = pseq ->ContextID;
1053
0
    NewSeq -> n        = pseq ->n;
1054
1055
0
    for (i=0; i < pseq->n; i++) {
1056
1057
0
        memmove(&NewSeq ->seq[i].attributes, &pseq ->seq[i].attributes, sizeof(cmsUInt64Number));
1058
1059
0
        NewSeq ->seq[i].deviceMfg   = pseq ->seq[i].deviceMfg;
1060
0
        NewSeq ->seq[i].deviceModel = pseq ->seq[i].deviceModel;
1061
0
        memmove(&NewSeq ->seq[i].ProfileID, &pseq ->seq[i].ProfileID, sizeof(cmsProfileID));
1062
0
        NewSeq ->seq[i].technology  = pseq ->seq[i].technology;
1063
1064
0
        NewSeq ->seq[i].Manufacturer = cmsMLUdup(pseq ->seq[i].Manufacturer);
1065
0
        NewSeq ->seq[i].Model        = cmsMLUdup(pseq ->seq[i].Model);
1066
0
        NewSeq ->seq[i].Description  = cmsMLUdup(pseq ->seq[i].Description);
1067
1068
0
    }
1069
1070
0
    return NewSeq;
1071
1072
0
Error:
1073
1074
0
    cmsFreeProfileSequenceDescription(NewSeq);
1075
0
    return NULL;
1076
0
}
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
0
{
1092
0
    _cmsDICT* dict = (_cmsDICT*) _cmsMallocZero(ContextID, sizeof(_cmsDICT));
1093
0
    if (dict == NULL) return NULL;
1094
1095
0
    dict ->ContextID = ContextID;
1096
0
    return (cmsHANDLE) dict;
1097
1098
0
}
1099
1100
// Dispose resources
1101
void CMSEXPORT cmsDictFree(cmsHANDLE hDict)
1102
0
{
1103
0
    _cmsDICT* dict = (_cmsDICT*) hDict;
1104
0
    cmsDICTentry *entry, *next;
1105
1106
0
    _cmsAssert(dict != NULL);
1107
1108
    // Walk the list freeing all nodes
1109
0
    entry = dict ->head;
1110
0
    while (entry != NULL) {
1111
1112
0
            if (entry ->DisplayName  != NULL) cmsMLUfree(entry ->DisplayName);
1113
0
            if (entry ->DisplayValue != NULL) cmsMLUfree(entry ->DisplayValue);
1114
0
            if (entry ->Name != NULL) _cmsFree(dict ->ContextID, entry -> Name);
1115
0
            if (entry ->Value != NULL) _cmsFree(dict ->ContextID, entry -> Value);
1116
1117
            // Don't fall in the habitual trap...
1118
0
            next = entry ->Next;
1119
0
            _cmsFree(dict ->ContextID, entry);
1120
1121
0
            entry = next;
1122
0
    }
1123
1124
0
    _cmsFree(dict ->ContextID, dict);
1125
0
}
1126
1127
1128
// Duplicate a wide char string
1129
static
1130
wchar_t* DupWcs(cmsContext ContextID, const wchar_t* ptr)
1131
0
{
1132
0
    if (ptr == NULL) return NULL;
1133
0
    return (wchar_t*) _cmsDupMem(ContextID, ptr, (mywcslen(ptr) + 1) * sizeof(wchar_t));
1134
0
}
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
0
{
1139
0
    _cmsDICT* dict = (_cmsDICT*) hDict;
1140
0
    cmsDICTentry *entry;
1141
1142
0
    _cmsAssert(dict != NULL);
1143
0
    _cmsAssert(Name != NULL);
1144
1145
0
    entry = (cmsDICTentry*) _cmsMallocZero(dict ->ContextID, sizeof(cmsDICTentry));
1146
0
    if (entry == NULL) return FALSE;
1147
1148
0
    entry ->DisplayName  = cmsMLUdup(DisplayName);
1149
0
    entry ->DisplayValue = cmsMLUdup(DisplayValue);
1150
0
    entry ->Name         = DupWcs(dict ->ContextID, Name);
1151
0
    entry ->Value        = DupWcs(dict ->ContextID, Value);
1152
1153
0
    entry ->Next = dict ->head;
1154
0
    dict ->head = entry;
1155
1156
0
    return TRUE;
1157
0
}
1158
1159
1160
// Duplicates an existing dictionary
1161
cmsHANDLE CMSEXPORT cmsDictDup(cmsHANDLE hDict)
1162
0
{
1163
0
    _cmsDICT* old_dict = (_cmsDICT*) hDict;
1164
0
    cmsHANDLE hNew;
1165
0
    cmsDICTentry *entry;
1166
1167
0
    _cmsAssert(old_dict != NULL);
1168
1169
0
    hNew  = cmsDictAlloc(old_dict ->ContextID);
1170
0
    if (hNew == NULL) return NULL;
1171
1172
    // Walk the list freeing all nodes
1173
0
    entry = old_dict ->head;
1174
0
    while (entry != NULL) {
1175
1176
0
        if (!cmsDictAddEntry(hNew, entry ->Name, entry ->Value, entry ->DisplayName, entry ->DisplayValue)) {
1177
1178
0
            cmsDictFree(hNew);
1179
0
            return NULL;
1180
0
        }
1181
1182
0
        entry = entry -> Next;
1183
0
    }
1184
1185
0
    return hNew;
1186
0
}
1187
1188
// Get a pointer to the linked list
1189
const cmsDICTentry* CMSEXPORT cmsDictGetEntryList(cmsHANDLE hDict)
1190
0
{
1191
0
    _cmsDICT* dict = (_cmsDICT*) hDict;
1192
1193
0
    if (dict == NULL) return NULL;
1194
0
    return dict ->head;
1195
0
}
1196
1197
// Helper For external languages
1198
const cmsDICTentry* CMSEXPORT cmsDictNextEntry(const cmsDICTentry* e)
1199
0
{
1200
0
     if (e == NULL) return NULL;
1201
0
     return e ->Next;
1202
0
}