Coverage Report

Created: 2024-11-01 06:12

/src/lcms/src/cmsio0.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
// Generic I/O, tag dictionary management, profile struct
30
31
// IOhandlers are abstractions used by littleCMS to read from whatever file, stream,
32
// memory block or any storage. Each IOhandler provides implementations for read,
33
// write, seek and tell functions. LittleCMS code deals with IO across those objects.
34
// In this way, is easier to add support for new storage media.
35
36
// NULL stream, for taking care of used space -------------------------------------
37
38
// NULL IOhandler basically does nothing but keep track on how many bytes have been
39
// written. This is handy when creating profiles, where the file size is needed in the
40
// header. Then, whole profile is serialized across NULL IOhandler and a second pass
41
// writes the bytes to the pertinent IOhandler.
42
43
typedef struct {
44
    cmsUInt32Number Pointer;         // Points to current location
45
} FILENULL;
46
47
static
48
cmsUInt32Number NULLRead(cmsIOHANDLER* iohandler, void *Buffer, cmsUInt32Number size, cmsUInt32Number count)
49
0
{
50
0
    FILENULL* ResData = (FILENULL*) iohandler ->stream;
51
52
0
    cmsUInt32Number len = size * count;
53
0
    ResData -> Pointer += len;
54
0
    return count;
55
56
0
    cmsUNUSED_PARAMETER(Buffer);
57
0
}
58
59
static
60
cmsBool  NULLSeek(cmsIOHANDLER* iohandler, cmsUInt32Number offset)
61
180
{
62
180
    FILENULL* ResData = (FILENULL*) iohandler ->stream;
63
64
180
    ResData ->Pointer = offset;
65
180
    return TRUE;
66
180
}
67
68
static
69
cmsUInt32Number NULLTell(cmsIOHANDLER* iohandler)
70
60.1k
{
71
60.1k
    FILENULL* ResData = (FILENULL*) iohandler ->stream;
72
60.1k
    return ResData -> Pointer;
73
60.1k
}
74
75
static
76
cmsBool  NULLWrite(cmsIOHANDLER* iohandler, cmsUInt32Number size, const void *Ptr)
77
660M
{
78
660M
    FILENULL* ResData = (FILENULL*) iohandler ->stream;
79
80
660M
    ResData ->Pointer += size;
81
660M
    if (ResData ->Pointer > iohandler->UsedSpace)
82
653M
        iohandler->UsedSpace = ResData ->Pointer;
83
84
660M
    return TRUE;
85
86
0
    cmsUNUSED_PARAMETER(Ptr);
87
0
}
88
89
static
90
cmsBool  NULLClose(cmsIOHANDLER* iohandler)
91
20.1k
{
92
20.1k
    FILENULL* ResData = (FILENULL*) iohandler ->stream;
93
94
20.1k
    _cmsFree(iohandler ->ContextID, ResData);
95
20.1k
    _cmsFree(iohandler ->ContextID, iohandler);
96
20.1k
    return TRUE;
97
20.1k
}
98
99
// The NULL IOhandler creator
100
cmsIOHANDLER*  CMSEXPORT cmsOpenIOhandlerFromNULL(cmsContext ContextID)
101
20.1k
{
102
20.1k
    struct _cms_io_handler* iohandler = NULL;
103
20.1k
    FILENULL* fm = NULL;
104
105
20.1k
    iohandler = (struct _cms_io_handler*) _cmsMallocZero(ContextID, sizeof(struct _cms_io_handler));
106
20.1k
    if (iohandler == NULL) return NULL;
107
108
20.1k
    fm = (FILENULL*) _cmsMallocZero(ContextID, sizeof(FILENULL));
109
20.1k
    if (fm == NULL) goto Error;
110
111
20.1k
    fm ->Pointer = 0;
112
113
20.1k
    iohandler ->ContextID = ContextID;
114
20.1k
    iohandler ->stream  = (void*) fm;
115
20.1k
    iohandler ->UsedSpace = 0;
116
20.1k
    iohandler ->ReportedSize = 0;
117
20.1k
    iohandler ->PhysicalFile[0] = 0;
118
119
20.1k
    iohandler ->Read    = NULLRead;
120
20.1k
    iohandler ->Seek    = NULLSeek;
121
20.1k
    iohandler ->Close   = NULLClose;
122
20.1k
    iohandler ->Tell    = NULLTell;
123
20.1k
    iohandler ->Write   = NULLWrite;
124
125
20.1k
    return iohandler;
126
127
0
Error:    
128
0
    if (iohandler) _cmsFree(ContextID, iohandler);
129
0
    return NULL;
130
131
20.1k
}
132
133
134
// Memory-based stream --------------------------------------------------------------
135
136
// Those functions implements an iohandler which takes a block of memory as storage medium.
137
138
typedef struct {
139
    cmsUInt8Number* Block;    // Points to allocated memory
140
    cmsUInt32Number Size;     // Size of allocated memory
141
    cmsUInt32Number Pointer;  // Points to current location
142
    int FreeBlockOnClose;     // As title
143
144
} FILEMEM;
145
146
static
147
cmsUInt32Number MemoryRead(struct _cms_io_handler* iohandler, void *Buffer, cmsUInt32Number size, cmsUInt32Number count)
148
6.57M
{
149
6.57M
    FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
150
6.57M
    cmsUInt8Number* Ptr;
151
6.57M
    cmsUInt32Number len = size * count;
152
153
6.57M
    if (ResData -> Pointer + len > ResData -> Size){
154
155
7.07k
        len = (ResData -> Size - ResData -> Pointer);
156
7.07k
        cmsSignalError(iohandler ->ContextID, cmsERROR_READ, "Read from memory error. Got %d bytes, block should be of %d bytes", len, count * size);
157
7.07k
        return 0;
158
7.07k
    }
159
160
6.56M
    Ptr  = ResData -> Block;
161
6.56M
    Ptr += ResData -> Pointer;
162
6.56M
    memmove(Buffer, Ptr, len);
163
6.56M
    ResData -> Pointer += len;
164
165
6.56M
    return count;
166
6.57M
}
167
168
// SEEK_CUR is assumed
169
static
170
cmsBool  MemorySeek(struct _cms_io_handler* iohandler, cmsUInt32Number offset)
171
70.6k
{
172
70.6k
    FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
173
174
70.6k
    if (offset > ResData ->Size) {
175
2.42k
        cmsSignalError(iohandler ->ContextID, cmsERROR_SEEK,  "Too few data; probably corrupted profile");
176
2.42k
        return FALSE;
177
2.42k
    }
178
179
68.2k
    ResData ->Pointer = offset;
180
68.2k
    return TRUE;
181
70.6k
}
182
183
// Tell for memory
184
static
185
cmsUInt32Number MemoryTell(struct _cms_io_handler* iohandler)
186
42.5k
{
187
42.5k
    FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
188
189
42.5k
    if (ResData == NULL) return 0;
190
42.5k
    return ResData -> Pointer;
191
42.5k
}
192
193
194
// Writes data to memory, also keeps used space for further reference.
195
static
196
cmsBool MemoryWrite(struct _cms_io_handler* iohandler, cmsUInt32Number size, const void *Ptr)
197
4.48M
{
198
4.48M
    FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
199
200
4.48M
    if (ResData == NULL) return FALSE; // Housekeeping
201
202
    // Check for available space. Clip.
203
4.48M
    if (ResData->Pointer + size > ResData->Size) {
204
4.47M
        size = ResData ->Size - ResData->Pointer;
205
4.47M
    }
206
      
207
4.48M
    if (size == 0) return TRUE;     // Write zero bytes is ok, but does nothing
208
209
12.4k
    memmove(ResData ->Block + ResData ->Pointer, Ptr, size);
210
12.4k
    ResData ->Pointer += size;
211
212
12.4k
    if (ResData ->Pointer > iohandler->UsedSpace)
213
12.4k
        iohandler->UsedSpace = ResData ->Pointer;
214
215
12.4k
    return TRUE;
216
4.48M
}
217
218
219
static
220
cmsBool  MemoryClose(struct _cms_io_handler* iohandler)
221
55.1k
{
222
55.1k
    FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
223
224
55.1k
    if (ResData ->FreeBlockOnClose) {
225
226
53.9k
        if (ResData ->Block) _cmsFree(iohandler ->ContextID, ResData ->Block);
227
53.9k
    }
228
229
55.1k
    _cmsFree(iohandler ->ContextID, ResData);
230
55.1k
    _cmsFree(iohandler ->ContextID, iohandler);
231
232
55.1k
    return TRUE;
233
55.1k
}
234
235
// Create a iohandler for memory block. AccessMode=='r' assumes the iohandler is going to read, and makes
236
// a copy of the memory block for letting user to free the memory after invoking open profile. In write
237
// mode ("w"), Buffer points to the begin of memory block to be written.
238
cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromMem(cmsContext ContextID, void *Buffer, cmsUInt32Number size, const char* AccessMode)
239
55.1k
{
240
55.1k
    cmsIOHANDLER* iohandler = NULL;
241
55.1k
    FILEMEM* fm = NULL;
242
243
55.1k
    _cmsAssert(AccessMode != NULL);
244
245
55.1k
    iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER));
246
55.1k
    if (iohandler == NULL) return NULL;
247
248
55.1k
    switch (*AccessMode) {
249
250
53.9k
    case 'r':
251
53.9k
        fm = (FILEMEM*) _cmsMallocZero(ContextID, sizeof(FILEMEM));
252
53.9k
        if (fm == NULL) goto Error;
253
254
53.9k
        if (Buffer == NULL) {
255
0
            cmsSignalError(ContextID, cmsERROR_READ, "Couldn't read profile from NULL pointer");
256
0
            goto Error;
257
0
        }
258
259
53.9k
        fm ->Block = (cmsUInt8Number*) _cmsMalloc(ContextID, size);
260
53.9k
        if (fm ->Block == NULL) {
261
262
0
            _cmsFree(ContextID, fm);
263
0
            _cmsFree(ContextID, iohandler);
264
0
            cmsSignalError(ContextID, cmsERROR_READ, "Couldn't allocate %ld bytes for profile", (long) size);
265
0
            return NULL;
266
0
        }
267
268
269
53.9k
        memmove(fm->Block, Buffer, size);
270
53.9k
        fm ->FreeBlockOnClose = TRUE;
271
53.9k
        fm ->Size    = size;
272
53.9k
        fm ->Pointer = 0;
273
53.9k
        iohandler -> ReportedSize = size;
274
53.9k
        break;
275
276
1.21k
    case 'w':
277
1.21k
        fm = (FILEMEM*) _cmsMallocZero(ContextID, sizeof(FILEMEM));
278
1.21k
        if (fm == NULL) goto Error;
279
280
1.21k
        if (Buffer == NULL) {
281
0
            cmsSignalError(ContextID, cmsERROR_WRITE, "Couldn't write profile to NULL pointer");
282
0
            goto Error;
283
0
        }
284
285
1.21k
        fm ->Block = (cmsUInt8Number*) Buffer;
286
1.21k
        fm ->FreeBlockOnClose = FALSE;
287
1.21k
        fm ->Size    = size;
288
1.21k
        fm ->Pointer = 0;
289
1.21k
        iohandler -> ReportedSize = 0;
290
1.21k
        break;
291
292
0
    default:
293
0
        cmsSignalError(ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unknown access mode '%c'", *AccessMode);
294
0
        return NULL;
295
55.1k
    }
296
297
55.1k
    iohandler ->ContextID = ContextID;
298
55.1k
    iohandler ->stream  = (void*) fm;
299
55.1k
    iohandler ->UsedSpace = 0;
300
55.1k
    iohandler ->PhysicalFile[0] = 0;
301
302
55.1k
    iohandler ->Read    = MemoryRead;
303
55.1k
    iohandler ->Seek    = MemorySeek;
304
55.1k
    iohandler ->Close   = MemoryClose;
305
55.1k
    iohandler ->Tell    = MemoryTell;
306
55.1k
    iohandler ->Write   = MemoryWrite;
307
308
55.1k
    return iohandler;
309
310
0
Error:
311
0
    if (fm) _cmsFree(ContextID, fm);
312
0
    if (iohandler) _cmsFree(ContextID, iohandler);
313
0
    return NULL;
314
55.1k
}
315
316
// File-based stream -------------------------------------------------------
317
318
// Read count elements of size bytes each. Return number of elements read
319
static
320
cmsUInt32Number FileRead(cmsIOHANDLER* iohandler, void *Buffer, cmsUInt32Number size, cmsUInt32Number count)
321
536k
{
322
536k
    cmsUInt32Number nReaded = (cmsUInt32Number) fread(Buffer, size, count, (FILE*) iohandler->stream);
323
324
536k
    if (nReaded != count) {
325
58.5k
            cmsSignalError(iohandler ->ContextID, cmsERROR_FILE, "Read error. Got %d bytes, block should be of %d bytes", nReaded * size, count * size);
326
58.5k
            return 0;
327
58.5k
    }
328
329
478k
    return nReaded;
330
536k
}
331
332
// Position file pointer in the file
333
static
334
cmsBool  FileSeek(cmsIOHANDLER* iohandler, cmsUInt32Number offset)
335
76.2k
{
336
76.2k
    if (fseek((FILE*) iohandler ->stream, (long) offset, SEEK_SET) != 0) {
337
338
0
       cmsSignalError(iohandler ->ContextID, cmsERROR_FILE, "Seek error; probably corrupted file");
339
0
       return FALSE;
340
0
    }
341
342
76.2k
    return TRUE;
343
76.2k
}
344
345
// Returns file pointer position or 0 on error, which is also a valid position.
346
static
347
cmsUInt32Number FileTell(cmsIOHANDLER* iohandler)
348
53.8k
{
349
53.8k
    long t = ftell((FILE*)iohandler ->stream);
350
53.8k
    if (t == -1L) {
351
0
        cmsSignalError(iohandler->ContextID, cmsERROR_FILE, "Tell error; probably corrupted file");
352
0
        return 0;
353
0
    }
354
355
53.8k
    return (cmsUInt32Number)t;
356
53.8k
}
357
358
// Writes data to stream, also keeps used space for further reference. Returns TRUE on success, FALSE on error
359
static
360
cmsBool  FileWrite(cmsIOHANDLER* iohandler, cmsUInt32Number size, const void* Buffer)
361
3.30M
{
362
3.30M
    if (size == 0) return TRUE;  // We allow to write 0 bytes, but nothing is written
363
364
3.30M
    iohandler->UsedSpace += size;
365
3.30M
    return (fwrite(Buffer, size, 1, (FILE*)iohandler->stream) == 1);
366
3.30M
}
367
368
// Closes the file
369
static
370
cmsBool  FileClose(cmsIOHANDLER* iohandler)
371
13.6k
{
372
13.6k
    if (fclose((FILE*) iohandler ->stream) != 0) return FALSE;
373
13.6k
    _cmsFree(iohandler ->ContextID, iohandler);
374
13.6k
    return TRUE;
375
13.6k
}
376
377
// Create a iohandler for disk based files.
378
cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromFile(cmsContext ContextID, const char* FileName, const char* AccessMode)
379
13.6k
{
380
13.6k
    cmsIOHANDLER* iohandler = NULL;
381
13.6k
    FILE* fm = NULL;
382
13.6k
    cmsInt32Number fileLen;    
383
13.6k
    char mode[4] = { 0,0,0,0 };
384
385
13.6k
    _cmsAssert(FileName != NULL);
386
13.6k
    _cmsAssert(AccessMode != NULL);
387
388
13.6k
    iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER));
389
13.6k
    if (iohandler == NULL) return NULL;
390
           
391
    // Validate access mode
392
27.2k
    while (*AccessMode) {
393
394
13.6k
        switch (*AccessMode)
395
13.6k
        {        
396
2.86k
        case 'r':
397
13.6k
        case 'w':
398
399
13.6k
            if (mode[0] == 0) {
400
13.6k
                mode[0] = *AccessMode;
401
13.6k
                mode[1] = 'b';                
402
13.6k
            }
403
0
            else {
404
0
                _cmsFree(ContextID, iohandler);
405
0
                cmsSignalError(ContextID, cmsERROR_FILE, "Access mode already specified '%c'", *AccessMode);
406
0
                return NULL;
407
0
            }
408
13.6k
            break;
409
410
        // Close on exec. Not all runtime supports that. Up to the caller to decide.
411
13.6k
        case 'e':
412
0
            mode[2] = 'e';
413
0
            break;
414
415
0
        default:
416
0
            _cmsFree(ContextID, iohandler);
417
0
            cmsSignalError(ContextID, cmsERROR_FILE, "Wrong access mode '%c'", *AccessMode);
418
0
            return NULL;
419
13.6k
        }
420
421
13.6k
        AccessMode++;
422
13.6k
    }
423
        
424
13.6k
    switch (mode[0]) {
425
426
2.86k
    case 'r':
427
2.86k
        fm = fopen(FileName, mode);
428
2.86k
        if (fm == NULL) {
429
0
            _cmsFree(ContextID, iohandler);
430
0
             cmsSignalError(ContextID, cmsERROR_FILE, "File '%s' not found", FileName);
431
0
            return NULL;
432
0
        }                                     
433
2.86k
        fileLen = (cmsInt32Number)cmsfilelength(fm);
434
2.86k
        if (fileLen < 0)
435
0
        {
436
0
            fclose(fm);
437
0
            _cmsFree(ContextID, iohandler);
438
0
            cmsSignalError(ContextID, cmsERROR_FILE, "Cannot get size of file '%s'", FileName);
439
0
            return NULL;
440
0
        }
441
2.86k
        iohandler -> ReportedSize = (cmsUInt32Number) fileLen;
442
2.86k
        break;
443
444
10.7k
    case 'w':
445
10.7k
        fm = fopen(FileName, mode);
446
10.7k
        if (fm == NULL) {
447
0
            _cmsFree(ContextID, iohandler);
448
0
             cmsSignalError(ContextID, cmsERROR_FILE, "Couldn't create '%s'", FileName);
449
0
            return NULL;
450
0
        }
451
10.7k
        iohandler -> ReportedSize = 0;
452
10.7k
        break;
453
454
0
    default:
455
0
        _cmsFree(ContextID, iohandler);   // Would never reach      
456
0
        return NULL;
457
13.6k
    }
458
459
13.6k
    iohandler ->ContextID = ContextID;
460
13.6k
    iohandler ->stream = (void*) fm;
461
13.6k
    iohandler ->UsedSpace = 0;
462
463
    // Keep track of the original file    
464
13.6k
    strncpy(iohandler -> PhysicalFile, FileName, sizeof(iohandler -> PhysicalFile)-1);
465
13.6k
    iohandler -> PhysicalFile[sizeof(iohandler -> PhysicalFile)-1] = 0;
466
467
13.6k
    iohandler ->Read    = FileRead;
468
13.6k
    iohandler ->Seek    = FileSeek;
469
13.6k
    iohandler ->Close   = FileClose;
470
13.6k
    iohandler ->Tell    = FileTell;
471
13.6k
    iohandler ->Write   = FileWrite;
472
473
13.6k
    return iohandler;
474
13.6k
}
475
476
// Create a iohandler for stream based files
477
cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromStream(cmsContext ContextID, FILE* Stream)
478
0
{
479
0
    cmsIOHANDLER* iohandler = NULL;
480
0
    cmsInt32Number fileSize;
481
482
0
    fileSize = (cmsInt32Number)cmsfilelength(Stream);
483
0
    if (fileSize < 0)
484
0
    {
485
0
        cmsSignalError(ContextID, cmsERROR_FILE, "Cannot get size of stream");
486
0
        return NULL;
487
0
    }
488
489
0
    iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER));
490
0
    if (iohandler == NULL) return NULL;
491
492
0
    iohandler -> ContextID = ContextID;
493
0
    iohandler -> stream = (void*) Stream;
494
0
    iohandler -> UsedSpace = 0;
495
0
    iohandler -> ReportedSize = (cmsUInt32Number) fileSize;
496
0
    iohandler -> PhysicalFile[0] = 0;
497
498
0
    iohandler ->Read    = FileRead;
499
0
    iohandler ->Seek    = FileSeek;
500
0
    iohandler ->Close   = FileClose;
501
0
    iohandler ->Tell    = FileTell;
502
0
    iohandler ->Write   = FileWrite;
503
504
0
    return iohandler;
505
0
}
506
507
508
509
// Close an open IO handler
510
cmsBool CMSEXPORT cmsCloseIOhandler(cmsIOHANDLER* io)
511
88.8k
{
512
88.8k
    return io -> Close(io);
513
88.8k
}
514
515
// -------------------------------------------------------------------------------------------------------
516
517
cmsIOHANDLER* CMSEXPORT cmsGetProfileIOhandler(cmsHPROFILE hProfile)
518
0
{
519
0
    _cmsICCPROFILE* Icc = (_cmsICCPROFILE*)hProfile;
520
521
0
    if (Icc == NULL) return NULL;
522
0
    return Icc->IOhandler;
523
0
}
524
525
// Creates an empty structure holding all required parameters
526
cmsHPROFILE CMSEXPORT cmsCreateProfilePlaceholder(cmsContext ContextID)
527
89.4k
{
528
89.4k
    _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) _cmsMallocZero(ContextID, sizeof(_cmsICCPROFILE));
529
89.4k
    if (Icc == NULL) return NULL;
530
531
89.4k
    Icc ->ContextID = ContextID;
532
533
    // Set it to empty
534
89.4k
    Icc -> TagCount   = 0;
535
536
    // Set default version
537
89.4k
    Icc ->Version =  0x02100000;
538
539
    // Set default CMM (that's me!)
540
89.4k
    Icc ->CMM = lcmsSignature;
541
542
    // Set default creator
543
    // Created by LittleCMS (that's me!)
544
89.4k
    Icc ->creator = lcmsSignature;
545
546
    // Set default platform
547
#ifdef CMS_IS_WINDOWS_
548
    Icc ->platform = cmsSigMicrosoft;
549
#else
550
89.4k
    Icc ->platform = cmsSigMacintosh;
551
89.4k
#endif
552
553
    // Set default device class
554
89.4k
    Icc->DeviceClass = cmsSigDisplayClass;
555
556
    // Set creation date/time
557
89.4k
    if (!_cmsGetTime(&Icc->Created))
558
0
        goto Error;
559
560
    // Create a mutex if the user provided proper plugin. NULL otherwise
561
89.4k
    Icc ->UsrMutex = _cmsCreateMutex(ContextID);
562
563
    // Return the handle
564
89.4k
    return (cmsHPROFILE) Icc;
565
566
0
Error:
567
0
    _cmsFree(ContextID, Icc);
568
0
    return NULL;
569
89.4k
}
570
571
cmsContext CMSEXPORT cmsGetProfileContextID(cmsHPROFILE hProfile)
572
188k
{
573
188k
     _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
574
575
188k
    if (Icc == NULL) return NULL;
576
185k
    return Icc -> ContextID;
577
188k
}
578
579
580
// Return the number of tags
581
cmsInt32Number CMSEXPORT cmsGetTagCount(cmsHPROFILE hProfile)
582
2.12k
{
583
2.12k
    _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
584
2.12k
    if (Icc == NULL) return -1;
585
586
2.12k
    return  (cmsInt32Number) Icc->TagCount;
587
2.12k
}
588
589
// Return the tag signature of a given tag number
590
cmsTagSignature CMSEXPORT cmsGetTagSignature(cmsHPROFILE hProfile, cmsUInt32Number n)
591
2.12k
{
592
2.12k
    _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
593
594
2.12k
    if (n > Icc->TagCount) return (cmsTagSignature) 0;  // Mark as not available
595
499
    if (n >= MAX_TABLE_TAG) return (cmsTagSignature) 0; // As double check
596
597
499
    return Icc ->TagNames[n];
598
499
}
599
600
601
static
602
int SearchOneTag(_cmsICCPROFILE* Profile, cmsTagSignature sig)
603
1.27M
{
604
1.27M
    int i;
605
606
4.57M
    for (i=0; i < (int) Profile -> TagCount; i++) {
607
608
3.83M
        if (sig == Profile -> TagNames[i])
609
537k
            return i;
610
3.83M
    }
611
612
741k
    return -1;
613
1.27M
}
614
615
// Search for a specific tag in tag dictionary. Returns position or -1 if tag not found.
616
// If followlinks is turned on, then the position of the linked tag is returned
617
int _cmsSearchTag(_cmsICCPROFILE* Icc, cmsTagSignature sig, cmsBool lFollowLinks)
618
1.26M
{
619
1.26M
    int n;
620
1.26M
    cmsTagSignature LinkedSig;
621
622
1.27M
    do {
623
624
        // Search for given tag in ICC profile directory
625
1.27M
        n = SearchOneTag(Icc, sig);
626
1.27M
        if (n < 0)
627
741k
            return -1;        // Not found
628
629
537k
        if (!lFollowLinks)
630
177k
            return n;         // Found, don't follow links
631
632
        // Is this a linked tag?
633
360k
        LinkedSig = Icc ->TagLinked[n];
634
635
        // Yes, follow link
636
360k
        if (LinkedSig != (cmsTagSignature) 0) {
637
14.6k
            sig = LinkedSig;
638
14.6k
        }
639
640
360k
    } while (LinkedSig != (cmsTagSignature) 0);
641
642
345k
    return n;
643
1.26M
}
644
645
// Deletes a tag entry
646
647
static
648
void _cmsDeleteTagByPos(_cmsICCPROFILE* Icc, int i)
649
84.1k
{
650
84.1k
    _cmsAssert(Icc != NULL);
651
84.1k
    _cmsAssert(i >= 0);
652
653
   
654
84.1k
    if (Icc -> TagPtrs[i] != NULL) {
655
656
        // Free previous version
657
61.2k
        if (Icc ->TagSaveAsRaw[i]) {
658
5.73k
            _cmsFree(Icc ->ContextID, Icc ->TagPtrs[i]);
659
5.73k
        }
660
55.4k
        else {
661
55.4k
            cmsTagTypeHandler* TypeHandler = Icc ->TagTypeHandlers[i];
662
663
55.4k
            if (TypeHandler != NULL) {
664
665
55.4k
                cmsTagTypeHandler LocalTypeHandler = *TypeHandler;
666
55.4k
                LocalTypeHandler.ContextID = Icc ->ContextID;              // As an additional parameter
667
55.4k
                LocalTypeHandler.ICCVersion = Icc ->Version;
668
55.4k
                LocalTypeHandler.FreePtr(&LocalTypeHandler, Icc -> TagPtrs[i]);
669
55.4k
                Icc ->TagPtrs[i] = NULL;
670
55.4k
            }
671
55.4k
        }
672
673
61.2k
    } 
674
84.1k
}
675
676
677
// Creates a new tag entry
678
static
679
cmsBool _cmsNewTag(_cmsICCPROFILE* Icc, cmsTagSignature sig, int* NewPos)
680
311k
{
681
311k
    int i;
682
683
    // Search for the tag
684
311k
    i = _cmsSearchTag(Icc, sig, FALSE);
685
311k
    if (i >= 0) {
686
687
        // Already exists? delete it
688
84.1k
        _cmsDeleteTagByPos(Icc, i);
689
84.1k
        *NewPos = i;
690
84.1k
    }
691
227k
    else  {
692
693
        // No, make a new one
694
227k
        if (Icc -> TagCount >= MAX_TABLE_TAG) {
695
0
            cmsSignalError(Icc ->ContextID, cmsERROR_RANGE, "Too many tags (%d)", MAX_TABLE_TAG);
696
0
            return FALSE;
697
0
        }
698
699
227k
        *NewPos = (int) Icc ->TagCount;
700
227k
        Icc -> TagCount++;
701
227k
    }
702
703
311k
    return TRUE;
704
311k
}
705
706
707
// Check existence
708
cmsBool CMSEXPORT cmsIsTag(cmsHPROFILE hProfile, cmsTagSignature sig)
709
224k
{
710
224k
       _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) (void*) hProfile;
711
224k
       return _cmsSearchTag(Icc, sig, FALSE) >= 0;
712
224k
}
713
714
715
716
// Checks for link compatibility
717
static
718
cmsBool CompatibleTypes(const cmsTagDescriptor* desc1, const cmsTagDescriptor* desc2)
719
57.4k
{
720
57.4k
    cmsUInt32Number i;
721
722
57.4k
    if (desc1 == NULL || desc2 == NULL) return FALSE;
723
724
29.0k
    if (desc1->nSupportedTypes != desc2->nSupportedTypes) return FALSE;
725
23.0k
    if (desc1->ElemCount != desc2->ElemCount) return FALSE;
726
727
42.4k
    for (i = 0; i < desc1->nSupportedTypes; i++)
728
28.9k
    {
729
28.9k
        if (desc1->SupportedTypes[i] != desc2->SupportedTypes[i]) return FALSE;
730
28.9k
    }
731
732
13.4k
    return TRUE;
733
17.0k
}
734
735
// Enforces that the profile version is per. spec.
736
// Operates on the big endian bytes from the profile.
737
// Called before converting to platform endianness.
738
// Byte 0 is BCD major version, so max 9.
739
// Byte 1 is 2 BCD digits, one per nibble.
740
// Reserved bytes 2 & 3 must be 0.
741
static 
742
cmsUInt32Number _validatedVersion(cmsUInt32Number DWord)
743
45.6k
{
744
45.6k
    cmsUInt8Number* pByte = (cmsUInt8Number*) &DWord;
745
45.6k
    cmsUInt8Number temp1;
746
45.6k
    cmsUInt8Number temp2;
747
748
45.6k
    if (*pByte > 0x09) *pByte = (cmsUInt8Number) 0x09;
749
45.6k
    temp1 = (cmsUInt8Number) (*(pByte+1) & 0xf0);
750
45.6k
    temp2 = (cmsUInt8Number) (*(pByte+1) & 0x0f);
751
45.6k
    if (temp1 > 0x90U) temp1 = 0x90U;
752
45.6k
    if (temp2 > 0x09U) temp2 = 0x09U;
753
45.6k
    *(pByte+1) = (cmsUInt8Number)(temp1 | temp2);
754
45.6k
    *(pByte+2) = (cmsUInt8Number)0;
755
45.6k
    *(pByte+3) = (cmsUInt8Number)0;
756
757
45.6k
    return DWord;
758
45.6k
}
759
760
// Check device class
761
static 
762
cmsBool validDeviceClass(cmsProfileClassSignature cl)
763
45.5k
{
764
45.5k
    if ((int)cl == 0) return TRUE; // We allow zero because older lcms versions defaulted to that.
765
766
32.3k
    switch (cl)
767
32.3k
    {    
768
2.09k
    case cmsSigInputClass:
769
7.33k
    case cmsSigDisplayClass:
770
14.5k
    case cmsSigOutputClass:
771
19.8k
    case cmsSigLinkClass:
772
26.2k
    case cmsSigAbstractClass:
773
27.7k
    case cmsSigColorSpaceClass:
774
28.9k
    case cmsSigNamedColorClass:
775
28.9k
        return TRUE;
776
777
3.33k
    default:
778
3.33k
        return FALSE;
779
32.3k
    }
780
781
32.3k
}
782
783
// Read profile header and validate it
784
cmsBool _cmsReadHeader(_cmsICCPROFILE* Icc)
785
56.7k
{
786
56.7k
    cmsTagEntry Tag;
787
56.7k
    cmsICCHeader Header;
788
56.7k
    cmsUInt32Number i, j;
789
56.7k
    cmsUInt32Number HeaderSize;
790
56.7k
    cmsIOHANDLER* io = Icc ->IOhandler;
791
56.7k
    cmsUInt32Number TagCount;
792
793
794
    // Read the header
795
56.7k
    if (io -> Read(io, &Header, sizeof(cmsICCHeader), 1) != 1) {
796
1.49k
        return FALSE;
797
1.49k
    }
798
799
    // Validate file as an ICC profile
800
55.2k
    if (_cmsAdjustEndianess32(Header.magic) != cmsMagicNumber) {
801
9.62k
        cmsSignalError(Icc ->ContextID, cmsERROR_BAD_SIGNATURE, "not an ICC profile, invalid signature");
802
9.62k
        return FALSE;
803
9.62k
    }
804
805
    // Adjust endianness of the used parameters
806
45.6k
    Icc -> CMM             = _cmsAdjustEndianess32(Header.cmmId);
807
45.6k
    Icc -> DeviceClass     = (cmsProfileClassSignature) _cmsAdjustEndianess32(Header.deviceClass);
808
45.6k
    Icc -> ColorSpace      = (cmsColorSpaceSignature)   _cmsAdjustEndianess32(Header.colorSpace);
809
45.6k
    Icc -> PCS             = (cmsColorSpaceSignature)   _cmsAdjustEndianess32(Header.pcs);
810
   
811
45.6k
    Icc -> RenderingIntent = _cmsAdjustEndianess32(Header.renderingIntent);
812
45.6k
    Icc -> platform        = (cmsPlatformSignature)_cmsAdjustEndianess32(Header.platform);
813
45.6k
    Icc -> flags           = _cmsAdjustEndianess32(Header.flags);
814
45.6k
    Icc -> manufacturer    = _cmsAdjustEndianess32(Header.manufacturer);
815
45.6k
    Icc -> model           = _cmsAdjustEndianess32(Header.model);
816
45.6k
    Icc -> creator         = _cmsAdjustEndianess32(Header.creator);
817
818
45.6k
    _cmsAdjustEndianess64(&Icc -> attributes, &Header.attributes);
819
45.6k
    Icc -> Version         = _cmsAdjustEndianess32(_validatedVersion(Header.version));
820
821
45.6k
    if (Icc->Version > 0x5000000) {
822
130
        cmsSignalError(Icc->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported profile version '0x%x'", Icc->Version);
823
130
        return FALSE;
824
130
    }
825
826
45.5k
    if (!validDeviceClass(Icc->DeviceClass)) {
827
3.33k
        cmsSignalError(Icc->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported device class '0x%x'", Icc->DeviceClass);
828
3.33k
        return FALSE;
829
3.33k
    }
830
831
    // Get size as reported in header
832
42.2k
    HeaderSize = _cmsAdjustEndianess32(Header.size);
833
834
    // Make sure HeaderSize is lower than profile size
835
42.2k
    if (HeaderSize >= Icc ->IOhandler ->ReportedSize)
836
41.4k
            HeaderSize = Icc ->IOhandler ->ReportedSize;
837
838
839
    // Get creation date/time
840
42.2k
    _cmsDecodeDateTimeNumber(&Header.date, &Icc ->Created);
841
842
    // The profile ID are 32 raw bytes
843
42.2k
    memmove(Icc ->ProfileID.ID32, Header.profileID.ID32, 16);
844
845
846
    // Read tag directory
847
42.2k
    if (!_cmsReadUInt32Number(io, &TagCount)) return FALSE;
848
41.7k
    if (TagCount > MAX_TABLE_TAG) {
849
850
385
        cmsSignalError(Icc ->ContextID, cmsERROR_RANGE, "Too many tags (%d)", TagCount);
851
385
        return FALSE;
852
385
    }
853
854
855
    // Read tag directory
856
41.3k
    Icc -> TagCount = 0;
857
339k
    for (i=0; i < TagCount; i++) {
858
859
300k
        if (!_cmsReadUInt32Number(io, (cmsUInt32Number *) &Tag.sig)) return FALSE;
860
298k
        if (!_cmsReadUInt32Number(io, &Tag.offset)) return FALSE;
861
298k
        if (!_cmsReadUInt32Number(io, &Tag.size)) return FALSE;
862
863
        // Perform some sanity check. Offset + size should fall inside file.
864
298k
        if (Tag.size == 0 || Tag.offset == 0) continue;
865
263k
        if (Tag.offset + Tag.size > HeaderSize ||
866
263k
            Tag.offset + Tag.size < Tag.offset)
867
206k
                  continue;
868
869
57.0k
        Icc -> TagNames[Icc ->TagCount]   = Tag.sig;
870
57.0k
        Icc -> TagOffsets[Icc ->TagCount] = Tag.offset;
871
57.0k
        Icc -> TagSizes[Icc ->TagCount]   = Tag.size;
872
873
       // Search for links
874
320k
        for (j=0; j < Icc ->TagCount; j++) {
875
           
876
263k
            if ((Icc ->TagOffsets[j] == Tag.offset) &&
877
263k
                (Icc ->TagSizes[j]   == Tag.size)) {
878
879
                // Check types. 
880
57.4k
                if (CompatibleTypes(_cmsGetTagDescriptor(Icc->ContextID, Icc->TagNames[j]),
881
57.4k
                                    _cmsGetTagDescriptor(Icc->ContextID, Tag.sig))) {
882
883
13.4k
                    Icc->TagLinked[Icc->TagCount] = Icc->TagNames[j];
884
13.4k
                }
885
57.4k
            }
886
887
263k
        }
888
889
57.0k
        Icc ->TagCount++;
890
57.0k
    }
891
892
893
88.5k
    for (i = 0; i < Icc->TagCount; i++) {
894
467k
        for (j = 0; j < Icc->TagCount; j++) {
895
896
            // Tags cannot be duplicate
897
418k
            if ((i != j) && (Icc->TagNames[i] == Icc->TagNames[j])) {
898
138
                cmsSignalError(Icc->ContextID, cmsERROR_RANGE, "Duplicate tag found");
899
138
                return FALSE;
900
138
            }
901
902
418k
        }
903
48.9k
    }
904
905
39.6k
    return TRUE;
906
39.7k
}
907
908
// Saves profile header
909
cmsBool _cmsWriteHeader(_cmsICCPROFILE* Icc, cmsUInt32Number UsedSpace)
910
16.6k
{
911
16.6k
    cmsICCHeader Header;
912
16.6k
    cmsUInt32Number i;
913
16.6k
    cmsTagEntry Tag;
914
16.6k
    cmsUInt32Number Count;
915
916
16.6k
    Header.size        = _cmsAdjustEndianess32(UsedSpace);
917
16.6k
    Header.cmmId       = _cmsAdjustEndianess32(Icc ->CMM);
918
16.6k
    Header.version     = _cmsAdjustEndianess32(Icc ->Version);
919
920
16.6k
    Header.deviceClass = (cmsProfileClassSignature) _cmsAdjustEndianess32(Icc -> DeviceClass);
921
16.6k
    Header.colorSpace  = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Icc -> ColorSpace);
922
16.6k
    Header.pcs         = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Icc -> PCS);
923
924
    //   NOTE: in v4 Timestamp must be in UTC rather than in local time
925
16.6k
    _cmsEncodeDateTimeNumber(&Header.date, &Icc ->Created);
926
927
16.6k
    Header.magic       = _cmsAdjustEndianess32(cmsMagicNumber);
928
929
16.6k
    Header.platform    = (cmsPlatformSignature) _cmsAdjustEndianess32(Icc -> platform);
930
931
16.6k
    Header.flags        = _cmsAdjustEndianess32(Icc -> flags);
932
16.6k
    Header.manufacturer = _cmsAdjustEndianess32(Icc -> manufacturer);
933
16.6k
    Header.model        = _cmsAdjustEndianess32(Icc -> model);
934
935
16.6k
    _cmsAdjustEndianess64(&Header.attributes, &Icc -> attributes);
936
937
    // Rendering intent in the header (for embedded profiles)
938
16.6k
    Header.renderingIntent = _cmsAdjustEndianess32(Icc -> RenderingIntent);
939
940
    // Illuminant is always D50
941
16.6k
    Header.illuminant.X = (cmsS15Fixed16Number) _cmsAdjustEndianess32((cmsUInt32Number) _cmsDoubleTo15Fixed16(cmsD50_XYZ()->X));
942
16.6k
    Header.illuminant.Y = (cmsS15Fixed16Number) _cmsAdjustEndianess32((cmsUInt32Number) _cmsDoubleTo15Fixed16(cmsD50_XYZ()->Y));
943
16.6k
    Header.illuminant.Z = (cmsS15Fixed16Number) _cmsAdjustEndianess32((cmsUInt32Number) _cmsDoubleTo15Fixed16(cmsD50_XYZ()->Z));
944
945
16.6k
    Header.creator      = _cmsAdjustEndianess32(Icc ->creator);
946
947
16.6k
    memset(&Header.reserved, 0, sizeof(Header.reserved));
948
949
    // Set profile ID. Endianness is always big endian
950
16.6k
    memmove(&Header.profileID, &Icc ->ProfileID, 16);
951
952
    // Dump the header
953
16.6k
    if (!Icc -> IOhandler->Write(Icc->IOhandler, sizeof(cmsICCHeader), &Header)) return FALSE;
954
955
    // Saves Tag directory
956
957
    // Get true count
958
16.6k
    Count = 0;
959
131k
    for (i=0;  i < Icc -> TagCount; i++) {
960
114k
        if (Icc ->TagNames[i] != (cmsTagSignature) 0)
961
114k
            Count++;
962
114k
    }
963
964
    // Store number of tags
965
16.6k
    if (!_cmsWriteUInt32Number(Icc ->IOhandler, Count)) return FALSE;
966
967
131k
    for (i=0; i < Icc -> TagCount; i++) {
968
969
114k
        if (Icc ->TagNames[i] == (cmsTagSignature) 0) continue;   // It is just a placeholder
970
971
114k
        Tag.sig    = (cmsTagSignature) _cmsAdjustEndianess32((cmsUInt32Number) Icc -> TagNames[i]);
972
114k
        Tag.offset = _cmsAdjustEndianess32((cmsUInt32Number) Icc -> TagOffsets[i]);
973
114k
        Tag.size   = _cmsAdjustEndianess32((cmsUInt32Number) Icc -> TagSizes[i]);
974
975
114k
        if (!Icc ->IOhandler -> Write(Icc-> IOhandler, sizeof(cmsTagEntry), &Tag)) return FALSE;
976
114k
    }
977
978
16.6k
    return TRUE;
979
16.6k
}
980
981
// ----------------------------------------------------------------------- Set/Get several struct members
982
983
984
cmsUInt32Number CMSEXPORT cmsGetHeaderRenderingIntent(cmsHPROFILE hProfile)
985
0
{
986
0
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
987
0
    return Icc -> RenderingIntent;
988
0
}
989
990
void CMSEXPORT cmsSetHeaderRenderingIntent(cmsHPROFILE hProfile, cmsUInt32Number RenderingIntent)
991
29.6k
{
992
29.6k
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
993
29.6k
    Icc -> RenderingIntent = RenderingIntent;
994
29.6k
}
995
996
cmsUInt32Number CMSEXPORT cmsGetHeaderFlags(cmsHPROFILE hProfile)
997
0
{
998
0
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
999
0
    return (cmsUInt32Number) Icc -> flags;
1000
0
}
1001
1002
void CMSEXPORT cmsSetHeaderFlags(cmsHPROFILE hProfile, cmsUInt32Number Flags)
1003
0
{
1004
0
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1005
0
    Icc -> flags = (cmsUInt32Number) Flags;
1006
0
}
1007
1008
cmsUInt32Number CMSEXPORT cmsGetHeaderManufacturer(cmsHPROFILE hProfile)
1009
1.12k
{
1010
1.12k
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1011
1.12k
    return Icc ->manufacturer;
1012
1.12k
}
1013
1014
void CMSEXPORT cmsSetHeaderManufacturer(cmsHPROFILE hProfile, cmsUInt32Number manufacturer)
1015
0
{
1016
0
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1017
0
    Icc -> manufacturer = manufacturer;
1018
0
}
1019
1020
cmsUInt32Number CMSEXPORT cmsGetHeaderCreator(cmsHPROFILE hProfile)
1021
0
{
1022
0
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1023
0
    return Icc ->creator;
1024
0
}
1025
1026
cmsUInt32Number CMSEXPORT cmsGetHeaderModel(cmsHPROFILE hProfile)
1027
1.12k
{
1028
1.12k
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1029
1.12k
    return Icc ->model;
1030
1.12k
}
1031
1032
void CMSEXPORT cmsSetHeaderModel(cmsHPROFILE hProfile, cmsUInt32Number model)
1033
0
{
1034
0
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1035
0
    Icc -> model = model;
1036
0
}
1037
1038
void CMSEXPORT cmsGetHeaderAttributes(cmsHPROFILE hProfile, cmsUInt64Number* Flags)
1039
1.12k
{
1040
1.12k
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1041
1.12k
    memmove(Flags, &Icc -> attributes, sizeof(cmsUInt64Number));
1042
1.12k
}
1043
1044
void CMSEXPORT cmsSetHeaderAttributes(cmsHPROFILE hProfile, cmsUInt64Number Flags)
1045
0
{
1046
0
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1047
0
    memmove(&Icc -> attributes, &Flags, sizeof(cmsUInt64Number));
1048
0
}
1049
1050
void CMSEXPORT cmsGetHeaderProfileID(cmsHPROFILE hProfile, cmsUInt8Number* ProfileID)
1051
1.12k
{
1052
1.12k
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1053
1.12k
    memmove(ProfileID, Icc ->ProfileID.ID8, 16);
1054
1.12k
}
1055
1056
void CMSEXPORT cmsSetHeaderProfileID(cmsHPROFILE hProfile, cmsUInt8Number* ProfileID)
1057
0
{
1058
0
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1059
0
    memmove(&Icc -> ProfileID, ProfileID, 16);
1060
0
}
1061
1062
cmsBool  CMSEXPORT cmsGetHeaderCreationDateTime(cmsHPROFILE hProfile, struct tm *Dest)
1063
0
{
1064
0
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1065
0
    memmove(Dest, &Icc ->Created, sizeof(struct tm));
1066
0
    return TRUE;
1067
0
}
1068
1069
cmsColorSpaceSignature CMSEXPORT cmsGetPCS(cmsHPROFILE hProfile)
1070
183k
{
1071
183k
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1072
183k
    return Icc -> PCS;
1073
183k
}
1074
1075
void CMSEXPORT cmsSetPCS(cmsHPROFILE hProfile, cmsColorSpaceSignature pcs)
1076
47.9k
{
1077
47.9k
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1078
47.9k
    Icc -> PCS = pcs;
1079
47.9k
}
1080
1081
cmsColorSpaceSignature CMSEXPORT cmsGetColorSpace(cmsHPROFILE hProfile)
1082
297k
{
1083
297k
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1084
297k
    return Icc -> ColorSpace;
1085
297k
}
1086
1087
void CMSEXPORT cmsSetColorSpace(cmsHPROFILE hProfile, cmsColorSpaceSignature sig)
1088
47.9k
{
1089
47.9k
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1090
47.9k
    Icc -> ColorSpace = sig;
1091
47.9k
}
1092
1093
cmsProfileClassSignature CMSEXPORT cmsGetDeviceClass(cmsHPROFILE hProfile)
1094
232k
{
1095
232k
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1096
232k
    return Icc -> DeviceClass;
1097
232k
}
1098
1099
void CMSEXPORT cmsSetDeviceClass(cmsHPROFILE hProfile, cmsProfileClassSignature sig)
1100
47.9k
{
1101
47.9k
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1102
47.9k
    Icc -> DeviceClass = sig;
1103
47.9k
}
1104
1105
cmsUInt32Number CMSEXPORT cmsGetEncodedICCversion(cmsHPROFILE hProfile)
1106
45.6k
{
1107
45.6k
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1108
45.6k
    return Icc -> Version;
1109
45.6k
}
1110
1111
void CMSEXPORT cmsSetEncodedICCversion(cmsHPROFILE hProfile, cmsUInt32Number Version)
1112
0
{
1113
0
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1114
0
    Icc -> Version = Version;
1115
0
}
1116
1117
// Get an hexadecimal number with same digits as v
1118
static
1119
cmsUInt32Number BaseToBase(cmsUInt32Number in, int BaseIn, int BaseOut)
1120
304k
{
1121
304k
    char Buff[100];
1122
304k
    int i, len;
1123
304k
    cmsUInt32Number out;
1124
1125
1.21M
    for (len=0; in > 0 && len < 100; len++) {
1126
1127
908k
        Buff[len] = (char) (in % BaseIn);
1128
908k
        in /= BaseIn;
1129
908k
    }
1130
1131
1.21M
    for (i=len-1, out=0; i >= 0; --i) {
1132
908k
        out = out * BaseOut + Buff[i];
1133
908k
    }
1134
1135
304k
    return out;
1136
304k
}
1137
1138
void  CMSEXPORT cmsSetProfileVersion(cmsHPROFILE hProfile, cmsFloat64Number Version)
1139
47.4k
{
1140
47.4k
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1141
1142
    // 4.2 -> 0x4200000
1143
1144
47.4k
    Icc -> Version = BaseToBase((cmsUInt32Number) floor(Version * 100.0 + 0.5), 10, 16) << 16;
1145
47.4k
}
1146
1147
cmsFloat64Number CMSEXPORT cmsGetProfileVersion(cmsHPROFILE hProfile)
1148
257k
{
1149
257k
    _cmsICCPROFILE*  Icc = (_cmsICCPROFILE*) hProfile;
1150
257k
    cmsUInt32Number n = Icc -> Version >> 16;
1151
1152
257k
    return BaseToBase(n, 16, 10) / 100.0;
1153
257k
}
1154
// --------------------------------------------------------------------------------------------------------------
1155
1156
1157
// Create profile from IOhandler
1158
cmsHPROFILE CMSEXPORT cmsOpenProfileFromIOhandlerTHR(cmsContext ContextID, cmsIOHANDLER* io)
1159
0
{
1160
0
    _cmsICCPROFILE* NewIcc;
1161
0
    cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
1162
1163
0
    if (hEmpty == NULL) return NULL;
1164
1165
0
    NewIcc = (_cmsICCPROFILE*) hEmpty;
1166
1167
0
    NewIcc ->IOhandler = io;
1168
0
    if (!_cmsReadHeader(NewIcc)) goto Error;
1169
0
    return hEmpty;
1170
1171
0
Error:
1172
0
    cmsCloseProfile(hEmpty);
1173
0
    return NULL;
1174
0
}
1175
1176
// Create profile from IOhandler
1177
cmsHPROFILE CMSEXPORT cmsOpenProfileFromIOhandler2THR(cmsContext ContextID, cmsIOHANDLER* io, cmsBool write)
1178
0
{
1179
0
    _cmsICCPROFILE* NewIcc;
1180
0
    cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
1181
1182
0
    if (hEmpty == NULL) return NULL;
1183
1184
0
    NewIcc = (_cmsICCPROFILE*) hEmpty;
1185
1186
0
    NewIcc ->IOhandler = io;
1187
0
    if (write) {
1188
1189
0
        NewIcc -> IsWrite = TRUE;
1190
0
        return hEmpty;
1191
0
    }
1192
1193
0
    if (!_cmsReadHeader(NewIcc)) goto Error;
1194
0
    return hEmpty;
1195
1196
0
Error:
1197
0
    cmsCloseProfile(hEmpty);
1198
0
    return NULL;
1199
0
}
1200
1201
1202
// Create profile from disk file
1203
cmsHPROFILE CMSEXPORT cmsOpenProfileFromFileTHR(cmsContext ContextID, const char *lpFileName, const char *sAccess)
1204
5.73k
{
1205
5.73k
    _cmsICCPROFILE* NewIcc;
1206
5.73k
    cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
1207
1208
5.73k
    if (hEmpty == NULL) return NULL;
1209
1210
5.73k
    NewIcc = (_cmsICCPROFILE*) hEmpty;
1211
1212
5.73k
    NewIcc ->IOhandler = cmsOpenIOhandlerFromFile(ContextID, lpFileName, sAccess);
1213
5.73k
    if (NewIcc ->IOhandler == NULL) goto Error;
1214
1215
5.73k
    if (*sAccess == 'W' || *sAccess == 'w') {
1216
1217
2.86k
        NewIcc -> IsWrite = TRUE;
1218
1219
2.86k
        return hEmpty;
1220
2.86k
    }
1221
1222
2.86k
    if (!_cmsReadHeader(NewIcc)) goto Error;
1223
2.12k
    return hEmpty;
1224
1225
741
Error:
1226
741
    cmsCloseProfile(hEmpty);
1227
741
    return NULL;
1228
2.86k
}
1229
1230
1231
cmsHPROFILE CMSEXPORT cmsOpenProfileFromFile(const char *ICCProfile, const char *sAccess)
1232
5.73k
{
1233
5.73k
    return cmsOpenProfileFromFileTHR(NULL, ICCProfile, sAccess);
1234
5.73k
}
1235
1236
1237
cmsHPROFILE  CMSEXPORT cmsOpenProfileFromStreamTHR(cmsContext ContextID, FILE* ICCProfile, const char *sAccess)
1238
0
{
1239
0
    _cmsICCPROFILE* NewIcc;
1240
0
    cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
1241
1242
0
    if (hEmpty == NULL) return NULL;
1243
1244
0
    NewIcc = (_cmsICCPROFILE*) hEmpty;
1245
1246
0
    NewIcc ->IOhandler = cmsOpenIOhandlerFromStream(ContextID, ICCProfile);
1247
0
    if (NewIcc ->IOhandler == NULL) goto Error;
1248
1249
0
    if (*sAccess == 'w') {
1250
1251
0
        NewIcc -> IsWrite = TRUE;
1252
0
        return hEmpty;
1253
0
    }
1254
1255
0
    if (!_cmsReadHeader(NewIcc)) goto Error;
1256
0
    return hEmpty;
1257
1258
0
Error:
1259
0
    cmsCloseProfile(hEmpty);
1260
0
    return NULL;
1261
1262
0
}
1263
1264
cmsHPROFILE  CMSEXPORT cmsOpenProfileFromStream(FILE* ICCProfile, const char *sAccess)
1265
0
{
1266
0
    return cmsOpenProfileFromStreamTHR(NULL, ICCProfile, sAccess);
1267
0
}
1268
1269
1270
// Open from memory block
1271
cmsHPROFILE CMSEXPORT cmsOpenProfileFromMemTHR(cmsContext ContextID, const void* MemPtr, cmsUInt32Number dwSize)
1272
53.9k
{
1273
53.9k
    _cmsICCPROFILE* NewIcc;
1274
53.9k
    cmsHPROFILE hEmpty;
1275
1276
53.9k
    hEmpty = cmsCreateProfilePlaceholder(ContextID);
1277
53.9k
    if (hEmpty == NULL) return NULL;
1278
1279
53.9k
    NewIcc = (_cmsICCPROFILE*) hEmpty;
1280
1281
    // Ok, in this case const void* is casted to void* just because open IO handler
1282
    // shares read and writing modes. Don't abuse this feature!
1283
53.9k
    NewIcc ->IOhandler = cmsOpenIOhandlerFromMem(ContextID, (void*) MemPtr, dwSize, "r");
1284
53.9k
    if (NewIcc ->IOhandler == NULL) goto Error;
1285
1286
53.9k
    if (!_cmsReadHeader(NewIcc)) goto Error;
1287
1288
37.5k
    return hEmpty;
1289
1290
16.4k
Error:
1291
16.4k
    cmsCloseProfile(hEmpty);
1292
16.4k
    return NULL;
1293
53.9k
}
1294
1295
cmsHPROFILE CMSEXPORT cmsOpenProfileFromMem(const void* MemPtr, cmsUInt32Number dwSize)
1296
53.9k
{
1297
53.9k
    return cmsOpenProfileFromMemTHR(NULL, MemPtr, dwSize);
1298
53.9k
}
1299
1300
1301
1302
// Dump tag contents. If the profile is being modified, untouched tags are copied from FileOrig
1303
static
1304
cmsBool SaveTags(_cmsICCPROFILE* Icc, _cmsICCPROFILE* FileOrig)
1305
16.6k
{
1306
16.6k
    cmsUInt8Number* Data;
1307
16.6k
    cmsUInt32Number i;
1308
16.6k
    cmsUInt32Number Begin;
1309
16.6k
    cmsIOHANDLER* io = Icc ->IOhandler;
1310
16.6k
    cmsTagDescriptor* TagDescriptor;
1311
16.6k
    cmsTagTypeSignature TypeBase;
1312
16.6k
    cmsTagTypeSignature Type;
1313
16.6k
    cmsTagTypeHandler* TypeHandler;
1314
16.6k
    cmsFloat64Number   Version = cmsGetProfileVersion((cmsHPROFILE) Icc);
1315
16.6k
    cmsTagTypeHandler LocalTypeHandler;
1316
1317
130k
    for (i=0; i < Icc -> TagCount; i++) {
1318
1319
114k
        if (Icc ->TagNames[i] == (cmsTagSignature) 0) continue;
1320
1321
        // Linked tags are not written
1322
114k
        if (Icc ->TagLinked[i] != (cmsTagSignature) 0) continue;
1323
1324
113k
        Icc -> TagOffsets[i] = Begin = io ->UsedSpace;
1325
1326
113k
        Data = (cmsUInt8Number*)  Icc -> TagPtrs[i];
1327
1328
113k
        if (!Data) {
1329
1330
            // Reach here if we are copying a tag from a disk-based ICC profile which has not been modified by user.
1331
            // In this case a blind copy of the block data is performed
1332
20.5k
            if (FileOrig != NULL && Icc -> TagOffsets[i]) {
1333
1334
20.5k
                if (FileOrig->IOhandler != NULL)
1335
20.5k
                {
1336
20.5k
                    cmsUInt32Number TagSize = FileOrig->TagSizes[i];
1337
20.5k
                    cmsUInt32Number TagOffset = FileOrig->TagOffsets[i];
1338
20.5k
                    void* Mem;
1339
1340
20.5k
                    if (!FileOrig->IOhandler->Seek(FileOrig->IOhandler, TagOffset)) return FALSE;
1341
1342
20.5k
                    Mem = _cmsMalloc(Icc->ContextID, TagSize);
1343
20.5k
                    if (Mem == NULL) return FALSE;
1344
1345
20.5k
                    if (FileOrig->IOhandler->Read(FileOrig->IOhandler, Mem, TagSize, 1) != 1) return FALSE;
1346
20.5k
                    if (!io->Write(io, TagSize, Mem)) return FALSE;
1347
20.5k
                    _cmsFree(Icc->ContextID, Mem);
1348
1349
20.5k
                    Icc->TagSizes[i] = (io->UsedSpace - Begin);
1350
1351
1352
                    // Align to 32 bit boundary.
1353
20.5k
                    if (!_cmsWriteAlignment(io))
1354
0
                        return FALSE;
1355
20.5k
                }
1356
20.5k
            }
1357
1358
20.5k
            continue;
1359
20.5k
        }
1360
1361
1362
        // Should this tag be saved as RAW? If so, tagsizes should be specified in advance (no further cooking is done)
1363
93.0k
        if (Icc ->TagSaveAsRaw[i]) {
1364
1365
91.8k
            if (io -> Write(io, Icc ->TagSizes[i], Data) != 1) return FALSE;
1366
91.8k
        }
1367
1.26k
        else {
1368
1369
            // Search for support on this tag
1370
1.26k
            TagDescriptor = _cmsGetTagDescriptor(Icc-> ContextID, Icc -> TagNames[i]);
1371
1.26k
            if (TagDescriptor == NULL) continue;                        // Unsupported, ignore it
1372
           
1373
1.26k
            if (TagDescriptor ->DecideType != NULL) {
1374
1375
1.15k
                Type = TagDescriptor ->DecideType(Version, Data);
1376
1.15k
            }
1377
117
            else {
1378
1379
117
                Type = TagDescriptor ->SupportedTypes[0];
1380
117
            }
1381
1382
1.26k
            TypeHandler =  _cmsGetTagTypeHandler(Icc->ContextID, Type);
1383
1384
1.26k
            if (TypeHandler == NULL) {
1385
0
                cmsSignalError(Icc ->ContextID, cmsERROR_INTERNAL, "(Internal) no handler for tag %x", Icc -> TagNames[i]);
1386
0
                continue;
1387
0
            }
1388
1389
1.26k
            TypeBase = TypeHandler ->Signature;
1390
1.26k
            if (!_cmsWriteTypeBase(io, TypeBase))
1391
0
                return FALSE;
1392
1393
1.26k
            LocalTypeHandler = *TypeHandler;
1394
1.26k
            LocalTypeHandler.ContextID  = Icc ->ContextID;
1395
1.26k
            LocalTypeHandler.ICCVersion = Icc ->Version;
1396
1.26k
            if (!LocalTypeHandler.WritePtr(&LocalTypeHandler, io, Data, TagDescriptor ->ElemCount)) {
1397
1398
64
                char String[5];
1399
1400
64
                _cmsTagSignature2String(String, (cmsTagSignature) TypeBase);
1401
64
                cmsSignalError(Icc ->ContextID, cmsERROR_WRITE, "Couldn't write type '%s'", String);
1402
64
                return FALSE;
1403
64
            }
1404
1.26k
        }
1405
1406
1407
93.0k
        Icc -> TagSizes[i] = (io ->UsedSpace - Begin);
1408
1409
        // Align to 32 bit boundary.
1410
93.0k
        if (! _cmsWriteAlignment(io))
1411
0
            return FALSE;
1412
93.0k
    }
1413
1414
1415
16.5k
    return TRUE;
1416
16.6k
}
1417
1418
1419
// Fill the offset and size fields for all linked tags
1420
static
1421
cmsBool SetLinks( _cmsICCPROFILE* Icc)
1422
8.13k
{
1423
8.13k
    cmsUInt32Number i;
1424
1425
63.1k
    for (i=0; i < Icc -> TagCount; i++) {
1426
1427
55.0k
        cmsTagSignature lnk = Icc ->TagLinked[i];
1428
55.0k
        if (lnk != (cmsTagSignature) 0) {
1429
1430
252
            int j = _cmsSearchTag(Icc, lnk, FALSE);
1431
252
            if (j >= 0) {
1432
1433
252
                Icc ->TagOffsets[i] = Icc ->TagOffsets[j];
1434
252
                Icc ->TagSizes[i]   = Icc ->TagSizes[j];
1435
252
            }
1436
1437
252
        }
1438
55.0k
    }
1439
1440
8.13k
    return TRUE;
1441
8.13k
}
1442
1443
// Low-level save to IOHANDLER. It returns the number of bytes used to
1444
// store the profile, or zero on error. io may be NULL and in this case
1445
// no data is written--only sizes are calculated
1446
cmsUInt32Number CMSEXPORT cmsSaveProfileToIOhandler(cmsHPROFILE hProfile, cmsIOHANDLER* io)
1447
8.52k
{
1448
8.52k
    _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1449
8.52k
    _cmsICCPROFILE Keep;
1450
8.52k
    cmsIOHANDLER* PrevIO = NULL;
1451
8.52k
    cmsUInt32Number UsedSpace;
1452
8.52k
    cmsContext ContextID;
1453
1454
8.52k
    _cmsAssert(hProfile != NULL);
1455
    
1456
8.52k
    if (!_cmsLockMutex(Icc->ContextID, Icc->UsrMutex)) return 0;
1457
8.52k
    memmove(&Keep, Icc, sizeof(_cmsICCPROFILE));
1458
1459
8.52k
    ContextID = cmsGetProfileContextID(hProfile);
1460
8.52k
    PrevIO = Icc ->IOhandler = cmsOpenIOhandlerFromNULL(ContextID);
1461
8.52k
    if (PrevIO == NULL) {
1462
0
        _cmsUnlockMutex(Icc->ContextID, Icc->UsrMutex);
1463
0
        return 0;
1464
0
    }
1465
1466
    // Pass #1 does compute offsets
1467
1468
8.52k
    if (!_cmsWriteHeader(Icc, 0)) goto Error;
1469
8.52k
    if (!SaveTags(Icc, &Keep)) goto Error;
1470
1471
8.46k
    UsedSpace = PrevIO ->UsedSpace;
1472
1473
    // Pass #2 does save to iohandler
1474
1475
8.46k
    if (io != NULL) {
1476
1477
8.13k
        Icc ->IOhandler = io;
1478
8.13k
        if (!SetLinks(Icc)) goto Error;
1479
8.13k
        if (!_cmsWriteHeader(Icc, UsedSpace)) goto Error;
1480
8.13k
        if (!SaveTags(Icc, &Keep)) goto Error;
1481
8.13k
    }
1482
1483
8.46k
    memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
1484
8.46k
    if (!cmsCloseIOhandler(PrevIO)) 
1485
0
        UsedSpace = 0; // As a error marker
1486
1487
8.46k
    _cmsUnlockMutex(Icc->ContextID, Icc->UsrMutex);
1488
1489
8.46k
    return UsedSpace;
1490
1491
1492
64
Error:
1493
64
    cmsCloseIOhandler(PrevIO);
1494
64
    memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
1495
64
    _cmsUnlockMutex(Icc->ContextID, Icc->UsrMutex);
1496
1497
64
    return 0;
1498
8.46k
}
1499
1500
1501
// Low-level save to disk.
1502
cmsBool  CMSEXPORT cmsSaveProfileToFile(cmsHPROFILE hProfile, const char* FileName)
1503
7.86k
{
1504
7.86k
    cmsContext ContextID = cmsGetProfileContextID(hProfile);
1505
7.86k
    cmsIOHANDLER* io = cmsOpenIOhandlerFromFile(ContextID, FileName, "w");
1506
7.86k
    cmsBool rc;
1507
1508
7.86k
    if (io == NULL) return FALSE;
1509
1510
7.86k
    rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0);
1511
7.86k
    rc &= cmsCloseIOhandler(io);
1512
1513
7.86k
    if (rc == FALSE) {          // remove() is C99 per 7.19.4.1
1514
64
            remove(FileName);   // We have to IGNORE return value in this case
1515
64
    }
1516
7.86k
    return rc;
1517
7.86k
}
1518
1519
// Same as anterior, but for streams
1520
cmsBool CMSEXPORT cmsSaveProfileToStream(cmsHPROFILE hProfile, FILE* Stream)
1521
0
{
1522
0
    cmsBool rc;
1523
0
    cmsContext ContextID = cmsGetProfileContextID(hProfile);
1524
0
    cmsIOHANDLER* io = cmsOpenIOhandlerFromStream(ContextID, Stream);
1525
1526
0
    if (io == NULL) return FALSE;
1527
1528
0
    rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0);
1529
0
    rc &= cmsCloseIOhandler(io);
1530
1531
0
    return rc;
1532
0
}
1533
1534
1535
// Same as anterior, but for memory blocks. In this case, a NULL as MemPtr means calculate needed space only
1536
cmsBool CMSEXPORT cmsSaveProfileToMem(cmsHPROFILE hProfile, void *MemPtr, cmsUInt32Number* BytesNeeded)
1537
662
{
1538
662
    cmsBool rc;
1539
662
    cmsIOHANDLER* io;
1540
662
    cmsContext ContextID = cmsGetProfileContextID(hProfile);
1541
1542
662
    _cmsAssert(BytesNeeded != NULL);
1543
1544
    // Should we just calculate the needed space?
1545
662
    if (MemPtr == NULL) {
1546
1547
331
           *BytesNeeded =  cmsSaveProfileToIOhandler(hProfile, NULL);
1548
331
            return (*BytesNeeded == 0) ? FALSE : TRUE;
1549
331
    }
1550
1551
    // That is a real write operation
1552
331
    io =  cmsOpenIOhandlerFromMem(ContextID, MemPtr, *BytesNeeded, "w");
1553
331
    if (io == NULL) return FALSE;
1554
1555
331
    rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0);
1556
331
    rc &= cmsCloseIOhandler(io);
1557
1558
331
    return rc;
1559
331
}
1560
1561
// Free one tag contents
1562
static
1563
void freeOneTag(_cmsICCPROFILE* Icc, cmsUInt32Number i)
1564
353k
{
1565
353k
    if (Icc->TagPtrs[i]) {
1566
1567
247k
        cmsTagTypeHandler* TypeHandler = Icc->TagTypeHandlers[i];
1568
1569
247k
        if (TypeHandler != NULL) {
1570
202k
            cmsTagTypeHandler LocalTypeHandler = *TypeHandler;
1571
1572
202k
            LocalTypeHandler.ContextID = Icc->ContextID;             
1573
202k
            LocalTypeHandler.ICCVersion = Icc->Version;
1574
202k
            LocalTypeHandler.FreePtr(&LocalTypeHandler, Icc->TagPtrs[i]);
1575
202k
        }
1576
45.9k
        else
1577
45.9k
            _cmsFree(Icc->ContextID, Icc->TagPtrs[i]);
1578
247k
    }
1579
353k
}
1580
1581
// Closes a profile freeing any involved resources
1582
cmsBool  CMSEXPORT cmsCloseProfile(cmsHPROFILE hProfile)
1583
102k
{
1584
102k
    _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1585
102k
    cmsBool  rc = TRUE;
1586
102k
    cmsUInt32Number i;
1587
1588
102k
    if (!Icc) return FALSE;
1589
1590
    // Was open in write mode?
1591
89.4k
    if (Icc ->IsWrite) {
1592
1593
2.86k
        Icc ->IsWrite = FALSE;      // Assure no further writing
1594
2.86k
        rc &= cmsSaveProfileToFile(hProfile, Icc ->IOhandler->PhysicalFile);
1595
2.86k
    }
1596
1597
374k
    for (i=0; i < Icc -> TagCount; i++) {
1598
1599
284k
        freeOneTag(Icc, i);        
1600
284k
    }
1601
1602
89.4k
    if (Icc ->IOhandler != NULL) {
1603
59.6k
        rc &= cmsCloseIOhandler(Icc->IOhandler);
1604
59.6k
    }
1605
1606
89.4k
    _cmsDestroyMutex(Icc->ContextID, Icc->UsrMutex);
1607
1608
89.4k
    _cmsFree(Icc ->ContextID, Icc);   // Free placeholder memory
1609
1610
89.4k
    return rc;
1611
102k
}
1612
1613
1614
// -------------------------------------------------------------------------------------------------------------------
1615
1616
1617
// Returns TRUE if a given tag is supported by a plug-in
1618
static
1619
cmsBool IsTypeSupported(cmsTagDescriptor* TagDescriptor, cmsTagTypeSignature Type)
1620
344k
{
1621
344k
    cmsUInt32Number i, nMaxTypes;
1622
1623
344k
    nMaxTypes = TagDescriptor->nSupportedTypes;
1624
344k
    if (nMaxTypes >= MAX_TYPES_IN_LCMS_PLUGIN)
1625
0
        nMaxTypes = MAX_TYPES_IN_LCMS_PLUGIN;
1626
1627
510k
    for (i=0; i < nMaxTypes; i++) {
1628
508k
        if (Type == TagDescriptor ->SupportedTypes[i]) return TRUE;
1629
508k
    }
1630
1631
1.51k
    return FALSE;
1632
344k
}
1633
1634
1635
// That's the main read function
1636
void* CMSEXPORT cmsReadTag(cmsHPROFILE hProfile, cmsTagSignature sig)
1637
276k
{
1638
276k
    _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1639
276k
    cmsIOHANDLER* io;
1640
276k
    cmsTagTypeHandler* TypeHandler;
1641
276k
    cmsTagTypeHandler LocalTypeHandler;
1642
276k
    cmsTagDescriptor*  TagDescriptor;
1643
276k
    cmsTagTypeSignature BaseType;
1644
276k
    cmsUInt32Number Offset, TagSize;
1645
276k
    cmsUInt32Number ElemCount;
1646
276k
    int n;
1647
1648
276k
    if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return NULL;
1649
1650
276k
    n = _cmsSearchTag(Icc, sig, TRUE);
1651
276k
    if (n < 0)
1652
120k
    {
1653
        // Not found, return NULL
1654
120k
        _cmsUnlockMutex(Icc->ContextID, Icc->UsrMutex);
1655
120k
        return NULL;
1656
120k
    }
1657
1658
    // If the element is already in memory, return the pointer
1659
156k
    if (Icc -> TagPtrs[n]) {
1660
1661
93.4k
        if (Icc->TagTypeHandlers[n] == NULL) goto Error;
1662
1663
        // Sanity check
1664
70.4k
        BaseType = Icc->TagTypeHandlers[n]->Signature;
1665
70.4k
        if (BaseType == 0) goto Error;
1666
1667
70.4k
        TagDescriptor = _cmsGetTagDescriptor(Icc->ContextID, sig);
1668
70.4k
        if (TagDescriptor == NULL) goto Error;
1669
1670
70.4k
        if (!IsTypeSupported(TagDescriptor, BaseType)) goto Error;
1671
1672
70.4k
        if (Icc ->TagSaveAsRaw[n]) goto Error;  // We don't support read raw tags as cooked
1673
1674
70.4k
        _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1675
70.4k
        return Icc -> TagPtrs[n];
1676
70.4k
    }
1677
1678
    // We need to read it. Get the offset and size to the file
1679
62.7k
    Offset    = Icc -> TagOffsets[n];
1680
62.7k
    TagSize   = Icc -> TagSizes[n];
1681
1682
62.7k
    if (TagSize < 8) goto Error;
1683
1684
33.9k
    io = Icc ->IOhandler;
1685
1686
33.9k
    if (io == NULL) { // This is a built-in profile that has been manipulated, abort early
1687
1688
0
        cmsSignalError(Icc->ContextID, cmsERROR_CORRUPTION_DETECTED, "Corrupted built-in profile.");
1689
0
        goto Error;
1690
0
    }
1691
1692
    // Seek to its location
1693
33.9k
    if (!io -> Seek(io, Offset))
1694
0
        goto Error;
1695
1696
    // Search for support on this tag
1697
33.9k
    TagDescriptor = _cmsGetTagDescriptor(Icc-> ContextID, sig);
1698
33.9k
    if (TagDescriptor == NULL) {
1699
1700
50
        char String[5];
1701
1702
50
        _cmsTagSignature2String(String, sig);
1703
1704
        // An unknown element was found.
1705
50
        cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unknown tag type '%s' found.", String);
1706
50
        goto Error;     // Unsupported.
1707
50
    }
1708
1709
    // if supported, get type and check if in list
1710
33.8k
    BaseType = _cmsReadTypeBase(io);
1711
33.8k
    if (BaseType == 0) goto Error;
1712
1713
33.6k
    if (!IsTypeSupported(TagDescriptor, BaseType)) goto Error;
1714
   
1715
32.1k
    TagSize  -= 8;       // Already read by the type base logic
1716
1717
    // Get type handler
1718
32.1k
    TypeHandler = _cmsGetTagTypeHandler(Icc ->ContextID, BaseType);
1719
32.1k
    if (TypeHandler == NULL) goto Error;
1720
32.1k
    LocalTypeHandler = *TypeHandler;
1721
1722
1723
    // Read the tag
1724
32.1k
    Icc -> TagTypeHandlers[n] = TypeHandler;
1725
1726
32.1k
    LocalTypeHandler.ContextID = Icc ->ContextID;
1727
32.1k
    LocalTypeHandler.ICCVersion = Icc ->Version;
1728
32.1k
    Icc -> TagPtrs[n] = LocalTypeHandler.ReadPtr(&LocalTypeHandler, io, &ElemCount, TagSize);
1729
1730
    // The tag type is supported, but something wrong happened and we cannot read the tag.
1731
    // let know the user about this (although it is just a warning)
1732
32.1k
    if (Icc -> TagPtrs[n] == NULL) {
1733
1734
15.3k
        char String[5];
1735
1736
15.3k
        _cmsTagSignature2String(String, sig);
1737
15.3k
        cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "Corrupted tag '%s'", String);
1738
15.3k
        goto Error;
1739
15.3k
    }
1740
1741
    // This is a weird error that may be a symptom of something more serious, the number of
1742
    // stored item is actually less than the number of required elements.
1743
16.8k
    if (ElemCount < TagDescriptor ->ElemCount) {
1744
1745
9
        char String[5];
1746
1747
9
        _cmsTagSignature2String(String, sig);
1748
9
        cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "'%s' Inconsistent number of items: expected %d, got %d",
1749
9
            String, TagDescriptor ->ElemCount, ElemCount);
1750
9
        goto Error;
1751
9
    }
1752
1753
1754
    // Return the data
1755
16.8k
    _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1756
16.8k
    return Icc -> TagPtrs[n];
1757
1758
1759
    // Return error and unlock the data
1760
68.8k
Error:
1761
1762
68.8k
    freeOneTag(Icc, n);    
1763
68.8k
    Icc->TagPtrs[n] = NULL;
1764
    
1765
68.8k
    _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1766
68.8k
    return NULL;
1767
16.8k
}
1768
1769
1770
// Get true type of data
1771
cmsTagTypeSignature _cmsGetTagTrueType(cmsHPROFILE hProfile, cmsTagSignature sig)
1772
25.3k
{
1773
25.3k
    _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1774
25.3k
    cmsTagTypeHandler* TypeHandler;
1775
25.3k
    int n;
1776
1777
    // Search for given tag in ICC profile directory
1778
25.3k
    n = _cmsSearchTag(Icc, sig, TRUE);
1779
25.3k
    if (n < 0) return (cmsTagTypeSignature) 0;                // Not found, return NULL
1780
1781
    // Get the handler. The true type is there
1782
25.3k
    TypeHandler =  Icc -> TagTypeHandlers[n];
1783
25.3k
    return TypeHandler ->Signature;
1784
25.3k
}
1785
1786
1787
// Write a single tag. This just keeps track of the tak into a list of "to be written". If the tag is already
1788
// in that list, the previous version is deleted.
1789
cmsBool CMSEXPORT cmsWriteTag(cmsHPROFILE hProfile, cmsTagSignature sig, const void* data)
1790
240k
{
1791
240k
    _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1792
240k
    cmsTagTypeHandler* TypeHandler = NULL;
1793
240k
    cmsTagTypeHandler LocalTypeHandler;
1794
240k
    cmsTagDescriptor* TagDescriptor = NULL;
1795
240k
    cmsTagTypeSignature Type;
1796
240k
    int i;
1797
240k
    cmsFloat64Number Version;
1798
240k
    char TypeString[5], SigString[5];
1799
1800
240k
    if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return FALSE;
1801
1802
    // To delete tags.
1803
240k
    if (data == NULL) {
1804
1805
         // Delete the tag
1806
0
         i = _cmsSearchTag(Icc, sig, FALSE);
1807
0
         if (i >= 0) {
1808
                
1809
             // Use zero as a mark of deleted 
1810
0
             _cmsDeleteTagByPos(Icc, i);
1811
0
             Icc ->TagNames[i] = (cmsTagSignature) 0;
1812
0
             _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1813
0
             return TRUE;
1814
0
         }
1815
         // Didn't find the tag
1816
0
        goto Error;
1817
0
    }
1818
1819
240k
    if (!_cmsNewTag(Icc, sig, &i)) goto Error;
1820
1821
    // This is not raw
1822
240k
    Icc ->TagSaveAsRaw[i] = FALSE;
1823
1824
    // This is not a link
1825
240k
    Icc ->TagLinked[i] = (cmsTagSignature) 0;
1826
1827
    // Get information about the TAG.
1828
240k
    TagDescriptor = _cmsGetTagDescriptor(Icc-> ContextID, sig);
1829
240k
    if (TagDescriptor == NULL){
1830
0
         cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported tag '%x'", sig);
1831
0
        goto Error;
1832
0
    }
1833
1834
1835
    // Now we need to know which type to use. It depends on the version.
1836
240k
    Version = cmsGetProfileVersion(hProfile);
1837
1838
240k
    if (TagDescriptor ->DecideType != NULL) {
1839
1840
        // Let the tag descriptor to decide the type base on depending on
1841
        // the data. This is useful for example on parametric curves, where
1842
        // curves specified by a table cannot be saved as parametric and needs
1843
        // to be casted to single v2-curves, even on v4 profiles.
1844
1845
173k
        Type = TagDescriptor ->DecideType(Version, data);
1846
173k
    }
1847
67.3k
    else {
1848
1849
67.3k
        Type = TagDescriptor ->SupportedTypes[0];
1850
67.3k
    }
1851
1852
    // Does the tag support this type?
1853
240k
    if (!IsTypeSupported(TagDescriptor, Type)) {
1854
1855
0
        _cmsTagSignature2String(TypeString, (cmsTagSignature) Type);
1856
0
        _cmsTagSignature2String(SigString,  sig);
1857
1858
0
        cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported type '%s' for tag '%s'", TypeString, SigString);
1859
0
        goto Error;
1860
0
    }
1861
1862
    // Does we have a handler for this type?
1863
240k
    TypeHandler =  _cmsGetTagTypeHandler(Icc->ContextID, Type);
1864
240k
    if (TypeHandler == NULL) {
1865
1866
0
        _cmsTagSignature2String(TypeString, (cmsTagSignature) Type);
1867
0
        _cmsTagSignature2String(SigString,  sig);
1868
1869
0
        cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported type '%s' for tag '%s'", TypeString, SigString);
1870
0
        goto Error;           // Should never happen
1871
0
    }
1872
1873
1874
    // Fill fields on icc structure
1875
240k
    Icc ->TagTypeHandlers[i]  = TypeHandler;
1876
240k
    Icc ->TagNames[i]         = sig;
1877
240k
    Icc ->TagSizes[i]         = 0;
1878
240k
    Icc ->TagOffsets[i]       = 0;
1879
1880
240k
    LocalTypeHandler = *TypeHandler;
1881
240k
    LocalTypeHandler.ContextID  = Icc ->ContextID;
1882
240k
    LocalTypeHandler.ICCVersion = Icc ->Version;
1883
240k
    Icc ->TagPtrs[i]            = LocalTypeHandler.DupPtr(&LocalTypeHandler, data, TagDescriptor ->ElemCount);
1884
1885
240k
    if (Icc ->TagPtrs[i] == NULL)  {
1886
1887
0
        _cmsTagSignature2String(TypeString, (cmsTagSignature) Type);
1888
0
        _cmsTagSignature2String(SigString,  sig);
1889
0
        cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "Malformed struct in type '%s' for tag '%s'", TypeString, SigString);
1890
1891
0
        goto Error;
1892
0
    }
1893
1894
240k
    _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1895
240k
    return TRUE;
1896
1897
0
Error:
1898
0
    _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1899
0
    return FALSE;
1900
1901
240k
}
1902
1903
// Read and write raw data. Read/Write Raw/cooked pairs try to maintain consistency within the pair. Some sequences
1904
// raw/cooked would work, but at a cost. Data "cooked" may be converted to "raw" by using the "write" serialization logic.
1905
// In general it is better to avoid mixing pairs.
1906
1907
cmsUInt32Number CMSEXPORT cmsReadRawTag(cmsHPROFILE hProfile, cmsTagSignature sig, void* data, cmsUInt32Number BufferSize)
1908
424k
{
1909
424k
    _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
1910
424k
    void *Object;
1911
424k
    int i;
1912
424k
    cmsIOHANDLER* MemIO;
1913
424k
    cmsTagTypeHandler* TypeHandler = NULL;
1914
424k
    cmsTagTypeHandler LocalTypeHandler;
1915
424k
    cmsTagDescriptor* TagDescriptor = NULL;
1916
424k
    cmsUInt32Number rc;
1917
424k
    cmsUInt32Number Offset, TagSize;
1918
1919
    // Sanity check
1920
424k
    if (data != NULL && BufferSize == 0) return 0;
1921
1922
424k
    if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return 0;
1923
1924
    // Search for given tag in ICC profile directory
1925
    
1926
424k
    i = _cmsSearchTag(Icc, sig, TRUE);
1927
424k
    if (i < 0) goto Error;                 // Not found, 
1928
1929
    // It is already read?
1930
164k
    if (Icc -> TagPtrs[i] == NULL) {
1931
1932
        // Not yet, get original position
1933
93.9k
        Offset   = Icc ->TagOffsets[i];
1934
93.9k
        TagSize  = Icc ->TagSizes[i];
1935
1936
        // read the data directly, don't keep copy
1937
        
1938
93.9k
        if (data != NULL) {
1939
1940
62.6k
            if (BufferSize < TagSize)
1941
5.23k
                TagSize = BufferSize;
1942
1943
62.6k
            if (!Icc ->IOhandler ->Seek(Icc ->IOhandler, Offset)) goto Error;
1944
62.6k
            if (!Icc ->IOhandler ->Read(Icc ->IOhandler, data, 1, TagSize)) goto Error;
1945
1946
5.28k
            _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1947
5.28k
            return TagSize;
1948
62.6k
        }
1949
1950
31.3k
        _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1951
31.3k
        return Icc ->TagSizes[i];
1952
93.9k
    }
1953
1954
    // The data has been already read, or written. But wait!, maybe the user choose to save as
1955
    // raw data. In this case, return the raw data directly
1956
    
1957
70.1k
    if (Icc ->TagSaveAsRaw[i]) {
1958
1959
68.8k
        if (data != NULL)  {
1960
1961
45.9k
            TagSize  = Icc ->TagSizes[i];
1962
45.9k
            if (BufferSize < TagSize)
1963
0
                TagSize = BufferSize;
1964
1965
45.9k
            memmove(data, Icc ->TagPtrs[i], TagSize);
1966
1967
45.9k
            _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1968
45.9k
            return TagSize;
1969
45.9k
        }
1970
1971
22.9k
        _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1972
22.9k
        return Icc ->TagSizes[i];
1973
68.8k
    }
1974
1975
    // Already read, or previously set by cmsWriteTag(). We need to serialize that
1976
    // data to raw to get something that makes sense
1977
    
1978
1.32k
    _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
1979
1.32k
    Object = cmsReadTag(hProfile, sig);
1980
1.32k
    if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return 0;
1981
1982
1.32k
    if (Object == NULL) goto Error;
1983
1984
    // Now we need to serialize to a memory block: just use a memory iohandler
1985
1986
1.32k
    if (data == NULL) {
1987
440
        MemIO = cmsOpenIOhandlerFromNULL(cmsGetProfileContextID(hProfile));
1988
880
    } else{
1989
880
        MemIO = cmsOpenIOhandlerFromMem(cmsGetProfileContextID(hProfile), data, BufferSize, "w");
1990
880
    }
1991
1.32k
    if (MemIO == NULL) goto Error;
1992
1993
    // Obtain type handling for the tag
1994
1.32k
    TypeHandler = Icc ->TagTypeHandlers[i];
1995
1.32k
    TagDescriptor = _cmsGetTagDescriptor(Icc-> ContextID, sig);
1996
1.32k
    if (TagDescriptor == NULL) {
1997
0
        cmsCloseIOhandler(MemIO);
1998
0
        goto Error;
1999
0
    }
2000
    
2001
1.32k
    if (TypeHandler == NULL) goto Error;
2002
2003
    // Serialize
2004
1.32k
    LocalTypeHandler = *TypeHandler;
2005
1.32k
    LocalTypeHandler.ContextID  = Icc ->ContextID;
2006
1.32k
    LocalTypeHandler.ICCVersion = Icc ->Version;
2007
2008
1.32k
    if (!_cmsWriteTypeBase(MemIO, TypeHandler ->Signature)) {
2009
0
        cmsCloseIOhandler(MemIO);
2010
0
        goto Error;
2011
0
    }
2012
2013
1.32k
    if (!LocalTypeHandler.WritePtr(&LocalTypeHandler, MemIO, Object, TagDescriptor ->ElemCount)) {
2014
183
        cmsCloseIOhandler(MemIO);
2015
183
        goto Error;
2016
183
    }
2017
2018
    // Get Size and close
2019
1.13k
    rc = MemIO ->Tell(MemIO);
2020
1.13k
    cmsCloseIOhandler(MemIO);      // Ignore return code this time
2021
2022
1.13k
    _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
2023
1.13k
    return rc;
2024
2025
318k
Error:
2026
318k
    _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
2027
318k
    return 0;
2028
1.32k
}
2029
2030
// Similar to the anterior. This function allows to write directly to the ICC profile any data, without
2031
// checking anything. As a rule, mixing Raw with cooked doesn't work, so writing a tag as raw and then reading
2032
// it as cooked without serializing does result into an error. If that is what you want, you will need to dump
2033
// the profile to memry or disk and then reopen it.
2034
cmsBool CMSEXPORT cmsWriteRawTag(cmsHPROFILE hProfile, cmsTagSignature sig, const void* data, cmsUInt32Number Size)
2035
51.6k
{
2036
51.6k
    _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
2037
51.6k
    int i;
2038
2039
51.6k
    if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return 0;
2040
2041
51.6k
    if (!_cmsNewTag(Icc, sig, &i)) {
2042
0
        _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
2043
0
         return FALSE;
2044
0
    }
2045
2046
    // Mark the tag as being written as RAW
2047
51.6k
    Icc ->TagSaveAsRaw[i] = TRUE;
2048
51.6k
    Icc ->TagNames[i]     = sig;
2049
51.6k
    Icc ->TagLinked[i]    = (cmsTagSignature) 0;
2050
2051
    // Keep a copy of the block
2052
51.6k
    Icc ->TagPtrs[i]  = _cmsDupMem(Icc ->ContextID, data, Size);
2053
51.6k
    Icc ->TagSizes[i] = Size;
2054
2055
51.6k
    _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
2056
2057
51.6k
    if (Icc->TagPtrs[i] == NULL) {           
2058
0
           Icc->TagNames[i] = (cmsTagSignature) 0;
2059
0
           return FALSE;
2060
0
    }
2061
51.6k
    return TRUE;
2062
51.6k
}
2063
2064
// Using this function you can collapse several tag entries to the same block in the profile
2065
cmsBool CMSEXPORT cmsLinkTag(cmsHPROFILE hProfile, cmsTagSignature sig, cmsTagSignature dest)
2066
19.2k
{
2067
19.2k
    _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
2068
19.2k
    int i;
2069
2070
19.2k
     if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return FALSE;
2071
2072
19.2k
    if (!_cmsNewTag(Icc, sig, &i)) {
2073
0
        _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
2074
0
        return FALSE;
2075
0
    }
2076
2077
    // Keep necessary information
2078
19.2k
    Icc ->TagSaveAsRaw[i] = FALSE;
2079
19.2k
    Icc ->TagNames[i]     = sig;
2080
19.2k
    Icc ->TagLinked[i]    = dest;
2081
2082
19.2k
    Icc ->TagPtrs[i]    = NULL;
2083
19.2k
    Icc ->TagSizes[i]   = 0;
2084
19.2k
    Icc ->TagOffsets[i] = 0;
2085
2086
19.2k
    _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
2087
19.2k
    return TRUE;
2088
19.2k
}
2089
2090
2091
// Returns the tag linked to sig, in the case two tags are sharing same resource
2092
cmsTagSignature  CMSEXPORT cmsTagLinkedTo(cmsHPROFILE hProfile, cmsTagSignature sig)
2093
2.12k
{
2094
2.12k
    _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
2095
2.12k
    int i;
2096
2097
    // Search for given tag in ICC profile directory
2098
2.12k
    i = _cmsSearchTag(Icc, sig, FALSE);
2099
2.12k
    if (i < 0) return (cmsTagSignature) 0;                 // Not found, return 0
2100
2101
8
    return Icc -> TagLinked[i];
2102
2.12k
}