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