xref: /reactos/dll/win32/mscms/profile.c (revision 234f89c0)
1 /*
2  * MSCMS - Color Management System for Wine
3  *
4  * Copyright 2004, 2005, 2006, 2008 Hans Leidekker
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #include "config.h"
22 #include "wine/debug.h"
23 #include "wine/unicode.h"
24 
25 #include <stdarg.h>
26 
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnls.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "winreg.h"
33 #include "shlwapi.h"
34 #include "icm.h"
35 
36 #include "mscms_priv.h"
37 
38 static void basename( LPCWSTR path, LPWSTR name )
39 {
40     INT i = lstrlenW( path );
41 
42     while (i > 0 && path[i - 1] != '\\' && path[i - 1] != '/') i--;
43     lstrcpyW( name, &path[i] );
44 }
45 
46 static inline LPWSTR strdupW( LPCSTR str )
47 {
48     LPWSTR ret = NULL;
49     if (str)
50     {
51         DWORD len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
52         if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
53             MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
54     }
55     return ret;
56 }
57 
58 const char *dbgstr_tag( DWORD tag )
59 {
60     return wine_dbg_sprintf( "'%c%c%c%c'",
61         (char)(tag >> 24), (char)(tag >> 16), (char)(tag >> 8), (char)(tag) );
62 }
63 
64 WINE_DEFAULT_DEBUG_CHANNEL(mscms);
65 
66 /******************************************************************************
67  * AssociateColorProfileWithDeviceA               [MSCMS.@]
68  */
69 BOOL WINAPI AssociateColorProfileWithDeviceA( PCSTR machine, PCSTR profile, PCSTR device )
70 {
71     int len;
72     BOOL ret = FALSE;
73     WCHAR *profileW, *deviceW;
74 
75     TRACE( "( %s, %s, %s )\n", debugstr_a(machine), debugstr_a(profile), debugstr_a(device) );
76 
77     if (!profile || !device)
78     {
79         SetLastError( ERROR_INVALID_PARAMETER );
80         return FALSE;
81     }
82     if (machine)
83     {
84         SetLastError( ERROR_NOT_SUPPORTED );
85         return FALSE;
86     }
87 
88     len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
89     if (!(profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;
90 
91     MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
92 
93     len = MultiByteToWideChar( CP_ACP, 0, device, -1, NULL, 0 );
94     if ((deviceW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
95     {
96         MultiByteToWideChar( CP_ACP, 0, device, -1, deviceW, len );
97         ret = AssociateColorProfileWithDeviceW( NULL, profileW, deviceW );
98     }
99 
100     HeapFree( GetProcessHeap(), 0, profileW );
101     HeapFree( GetProcessHeap(), 0, deviceW );
102     return ret;
103 }
104 
105 static BOOL set_profile_device_key( PCWSTR file, const BYTE *value, DWORD size )
106 {
107     static const WCHAR fmtW[] = {'%','c','%','c','%','c','%','c',0};
108     static const WCHAR icmW[] = {'S','o','f','t','w','a','r','e','\\',
109                                  'M','i','c','r','o','s','o','f','t','\\',
110                                  'W','i','n','d','o','w','s',' ','N','T','\\',
111                                  'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
112                                  'I','C','M',0};
113     PROFILEHEADER header;
114     PROFILE profile;
115     HPROFILE handle;
116     HKEY icm_key, class_key;
117     WCHAR basenameW[MAX_PATH], classW[5];
118 
119     profile.dwType = PROFILE_FILENAME;
120     profile.pProfileData = (PVOID)file;
121     profile.cbDataSize = (lstrlenW( file ) + 1) * sizeof(WCHAR);
122 
123     /* FIXME is the profile installed? */
124     if (!(handle = OpenColorProfileW( &profile, PROFILE_READ, 0, OPEN_EXISTING )))
125     {
126         SetLastError( ERROR_INVALID_PROFILE );
127         return FALSE;
128     }
129     if (!GetColorProfileHeader( handle, &header ))
130     {
131         CloseColorProfile( handle );
132         SetLastError( ERROR_INVALID_PROFILE );
133         return FALSE;
134     }
135     RegCreateKeyExW( HKEY_LOCAL_MACHINE, icmW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &icm_key, NULL );
136 
137     basename( file, basenameW );
138     sprintfW( classW, fmtW, (header.phClass >> 24) & 0xff, (header.phClass >> 16) & 0xff,
139                             (header.phClass >> 8) & 0xff,  header.phClass & 0xff );
140 
141     RegCreateKeyExW( icm_key, classW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &class_key, NULL );
142     if (value) RegSetValueExW( class_key, basenameW, 0, REG_BINARY, value, size );
143     else RegDeleteValueW( class_key, basenameW );
144 
145     RegCloseKey( class_key );
146     RegCloseKey( icm_key );
147     CloseColorProfile( handle );
148     return TRUE;
149 }
150 
151 /******************************************************************************
152  * AssociateColorProfileWithDeviceW               [MSCMS.@]
153  */
154 BOOL WINAPI AssociateColorProfileWithDeviceW( PCWSTR machine, PCWSTR profile, PCWSTR device )
155 {
156     static const BYTE dummy_value[12];
157 
158     TRACE( "( %s, %s, %s )\n", debugstr_w(machine), debugstr_w(profile), debugstr_w(device) );
159 
160     if (!profile || !device)
161     {
162         SetLastError( ERROR_INVALID_PARAMETER );
163         return FALSE;
164     }
165     if (machine)
166     {
167         SetLastError( ERROR_NOT_SUPPORTED );
168         return FALSE;
169     }
170 
171     return set_profile_device_key( profile, dummy_value, sizeof(dummy_value) );
172 }
173 
174 /******************************************************************************
175  * DisassociateColorProfileFromDeviceA            [MSCMS.@]
176  */
177 BOOL WINAPI DisassociateColorProfileFromDeviceA( PCSTR machine, PCSTR profile, PCSTR device )
178 {
179     int len;
180     BOOL ret = FALSE;
181     WCHAR *profileW, *deviceW;
182 
183     TRACE( "( %s, %s, %s )\n", debugstr_a(machine), debugstr_a(profile), debugstr_a(device) );
184 
185     if (!profile || !device)
186     {
187         SetLastError( ERROR_INVALID_PARAMETER );
188         return FALSE;
189     }
190     if (machine)
191     {
192         SetLastError( ERROR_NOT_SUPPORTED );
193         return FALSE;
194     }
195 
196     len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
197     if (!(profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;
198 
199     MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
200 
201     len = MultiByteToWideChar( CP_ACP, 0, device, -1, NULL, 0 );
202     if ((deviceW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
203     {
204         MultiByteToWideChar( CP_ACP, 0, device, -1, deviceW, len );
205         ret = DisassociateColorProfileFromDeviceW( NULL, profileW, deviceW );
206     }
207 
208     HeapFree( GetProcessHeap(), 0, profileW );
209     HeapFree( GetProcessHeap(), 0, deviceW );
210     return ret;
211 }
212 
213 /******************************************************************************
214  * DisassociateColorProfileFromDeviceW            [MSCMS.@]
215  */
216 BOOL WINAPI DisassociateColorProfileFromDeviceW( PCWSTR machine, PCWSTR profile, PCWSTR device )
217 {
218     TRACE( "( %s, %s, %s )\n", debugstr_w(machine), debugstr_w(profile), debugstr_w(device) );
219 
220     if (!profile || !device)
221     {
222         SetLastError( ERROR_INVALID_PARAMETER );
223         return FALSE;
224     }
225     if (machine)
226     {
227         SetLastError( ERROR_NOT_SUPPORTED );
228         return FALSE;
229     }
230 
231     return set_profile_device_key( profile, NULL, 0 );
232 }
233 
234 /******************************************************************************
235  * GetColorDirectoryA               [MSCMS.@]
236  *
237  * See GetColorDirectoryW.
238  */
239 BOOL WINAPI GetColorDirectoryA( PCSTR machine, PSTR buffer, PDWORD size )
240 {
241     INT len;
242     LPWSTR bufferW;
243     BOOL ret = FALSE;
244     DWORD sizeW;
245 
246     TRACE( "( %p, %p )\n", buffer, size );
247 
248     if (machine || !size) return FALSE;
249 
250     if (!buffer)
251     {
252         ret = GetColorDirectoryW( NULL, NULL, &sizeW );
253         *size = sizeW / sizeof(WCHAR);
254         return ret;
255     }
256 
257     sizeW = *size * sizeof(WCHAR);
258 
259     bufferW = HeapAlloc( GetProcessHeap(), 0, sizeW );
260     if (bufferW)
261     {
262         if ((ret = GetColorDirectoryW( NULL, bufferW, &sizeW )))
263         {
264             *size = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
265             len = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, *size, NULL, NULL );
266             if (!len) ret = FALSE;
267         }
268         else *size = sizeW / sizeof(WCHAR);
269 
270         HeapFree( GetProcessHeap(), 0, bufferW );
271     }
272     return ret;
273 }
274 
275 /******************************************************************************
276  * GetColorDirectoryW               [MSCMS.@]
277  *
278  * Get the directory where color profiles are stored.
279  *
280  * PARAMS
281  *  machine  [I]   Name of the machine for which to get the color directory.
282  *                 Must be NULL, which indicates the local machine.
283  *  buffer   [I]   Buffer to receive the path name.
284  *  size     [I/O] Size of the buffer in bytes. On return the variable holds
285  *                 the number of bytes actually needed.
286  */
287 BOOL WINAPI GetColorDirectoryW( PCWSTR machine, PWSTR buffer, PDWORD size )
288 {
289     WCHAR colordir[MAX_PATH];
290     static const WCHAR colorsubdir[] =
291         {'\\','s','p','o','o','l','\\','d','r','i','v','e','r','s','\\','c','o','l','o','r',0};
292     DWORD len;
293 
294     TRACE( "( %p, %p )\n", buffer, size );
295 
296     if (machine || !size) return FALSE;
297 
298     GetSystemDirectoryW( colordir, sizeof(colordir) / sizeof(WCHAR) );
299     lstrcatW( colordir, colorsubdir );
300 
301     len = lstrlenW( colordir ) * sizeof(WCHAR);
302 
303     if (buffer && len <= *size)
304     {
305         lstrcpyW( buffer, colordir );
306         *size = len;
307         return TRUE;
308     }
309 
310     SetLastError( ERROR_MORE_DATA );
311     *size = len;
312     return FALSE;
313 }
314 
315 /******************************************************************************
316  * GetColorProfileElement               [MSCMS.@]
317  *
318  * Retrieve data for a specified tag type.
319  *
320  * PARAMS
321  *  profile  [I]   Handle to a color profile.
322  *  type     [I]   ICC tag type.
323  *  offset   [I]   Offset in bytes to start copying from.
324  *  size     [I/O] Size of the buffer in bytes. On return the variable holds
325  *                 the number of bytes actually needed.
326  *  buffer   [O]   Buffer to receive the tag data.
327  *  ref      [O]   Pointer to a BOOL that specifies whether more than one tag
328  *                 references the data.
329  *
330  * RETURNS
331  *  Success: TRUE
332  *  Failure: FALSE
333  */
334 BOOL WINAPI GetColorProfileElement( HPROFILE handle, TAGTYPE type, DWORD offset, PDWORD size,
335                                     PVOID buffer, PBOOL ref )
336 {
337     BOOL ret = FALSE;
338 #ifdef HAVE_LCMS2
339     struct profile *profile = grab_profile( handle );
340 
341     TRACE( "( %p, 0x%08x, %d, %p, %p, %p )\n", handle, type, offset, size, buffer, ref );
342 
343     if (!profile) return FALSE;
344 
345     if (!size || !ref)
346     {
347         release_profile( profile );
348         return FALSE;
349     }
350     if (!get_tag_data( profile, type, offset, buffer, size ))
351     {
352         release_profile( profile );
353         return FALSE;
354     }
355     ret = get_tag_data( profile, type, offset, buffer, size );
356     *ref = cmsTagLinkedTo( profile->cmsprofile, type ) != 0;
357     release_profile( profile );
358 #endif /* HAVE_LCMS2 */
359     return ret;
360 }
361 
362 /******************************************************************************
363  * GetColorProfileElementTag               [MSCMS.@]
364  *
365  * Get the tag type from a color profile by index.
366  *
367  * PARAMS
368  *  profile  [I]   Handle to a color profile.
369  *  index    [I]   Index into the tag table of the color profile.
370  *  type     [O]   Pointer to a variable that holds the ICC tag type on return.
371  *
372  * RETURNS
373  *  Success: TRUE
374  *  Failure: FALSE
375  *
376  * NOTES
377  *  The tag table index starts at 1.
378  *  Use GetCountColorProfileElements to retrieve a count of tagged elements.
379  */
380 BOOL WINAPI GetColorProfileElementTag( HPROFILE handle, DWORD index, PTAGTYPE type )
381 {
382     BOOL ret = FALSE;
383 #ifdef HAVE_LCMS2
384     struct profile *profile = grab_profile( handle );
385     cmsInt32Number num_tags;
386     cmsTagSignature sig;
387 
388     TRACE( "( %p, %d, %p )\n", handle, index, type );
389 
390     if (!profile) return FALSE;
391 
392     if (!type)
393     {
394         release_profile( profile );
395         return FALSE;
396     }
397     num_tags = cmsGetTagCount( profile->cmsprofile );
398     if (num_tags < 0 || index > num_tags || index < 1)
399     {
400         release_profile( profile );
401         return FALSE;
402     }
403     if ((sig = cmsGetTagSignature( profile->cmsprofile, index - 1 )))
404     {
405         *type = sig;
406         ret = TRUE;
407     }
408     release_profile( profile );
409 
410 #endif /* HAVE_LCMS2 */
411     return ret;
412 }
413 
414 /******************************************************************************
415  * GetColorProfileFromHandle               [MSCMS.@]
416  *
417  * Retrieve an ICC color profile by handle.
418  *
419  * PARAMS
420  *  profile  [I]   Handle to a color profile.
421  *  buffer   [O]   Buffer to receive the ICC profile.
422  *  size     [I/O] Size of the buffer in bytes. On return the variable holds the
423  *                 number of bytes actually needed.
424  *
425  * RETURNS
426  *  Success: TRUE
427  *  Failure: FALSE
428  *
429  * NOTES
430  *  The profile returned will be in big-endian format.
431  */
432 BOOL WINAPI GetColorProfileFromHandle( HPROFILE handle, PBYTE buffer, PDWORD size )
433 {
434     BOOL ret = FALSE;
435 #ifdef HAVE_LCMS2
436     struct profile *profile = grab_profile( handle );
437     PROFILEHEADER header;
438 
439     TRACE( "( %p, %p, %p )\n", handle, buffer, size );
440 
441     if (!profile) return FALSE;
442 
443     if (!size)
444     {
445         release_profile( profile );
446         return FALSE;
447     }
448     get_profile_header( profile, &header );
449 
450     if (!buffer || header.phSize > *size)
451     {
452         *size = header.phSize;
453         release_profile( profile );
454         return FALSE;
455     }
456 
457     /* No endian conversion needed */
458     memcpy( buffer, profile->data, profile->size );
459     *size = profile->size;
460 
461     release_profile( profile );
462     ret = TRUE;
463 
464 #endif /* HAVE_LCMS2 */
465     return ret;
466 }
467 
468 /******************************************************************************
469  * GetColorProfileHeader               [MSCMS.@]
470  *
471  * Retrieve a color profile header by handle.
472  *
473  * PARAMS
474  *  profile  [I]   Handle to a color profile.
475  *  header   [O]   Buffer to receive the ICC profile header.
476  *
477  * RETURNS
478  *  Success: TRUE
479  *  Failure: FALSE
480  *
481  * NOTES
482  *  The profile header returned will be adjusted for endianness.
483  */
484 BOOL WINAPI GetColorProfileHeader( HPROFILE handle, PPROFILEHEADER header )
485 {
486 #ifdef HAVE_LCMS2
487     struct profile *profile = grab_profile( handle );
488 
489     TRACE( "( %p, %p )\n", handle, header );
490 
491     if (!profile) return FALSE;
492 
493     if (!header)
494     {
495         release_profile( profile );
496         return FALSE;
497     }
498     get_profile_header( profile, header );
499     release_profile( profile );
500     return TRUE;
501 
502 #else
503     return FALSE;
504 #endif /* HAVE_LCMS2 */
505 }
506 
507 /******************************************************************************
508  * GetCountColorProfileElements               [MSCMS.@]
509  *
510  * Retrieve the number of elements in a color profile.
511  *
512  * PARAMS
513  *  profile  [I] Handle to a color profile.
514  *  count    [O] Pointer to a variable which is set to the number of elements
515  *               in the color profile.
516  *
517  * RETURNS
518  *  Success: TRUE
519  *  Failure: FALSE
520  */
521 BOOL WINAPI GetCountColorProfileElements( HPROFILE handle, PDWORD count )
522 {
523     BOOL ret = FALSE;
524 #ifdef HAVE_LCMS2
525     struct profile *profile = grab_profile( handle );
526     cmsInt32Number num_tags;
527 
528     TRACE( "( %p, %p )\n", handle, count );
529 
530     if (!profile) return FALSE;
531 
532     if (!count)
533     {
534         release_profile( profile );
535         return FALSE;
536     }
537     if ((num_tags = cmsGetTagCount( profile->cmsprofile )) >= 0)
538     {
539         *count = num_tags;
540         ret = TRUE;
541     }
542     release_profile( profile );
543 
544 #endif /* HAVE_LCMS2 */
545     return ret;
546 }
547 
548 /******************************************************************************
549  * GetStandardColorSpaceProfileA               [MSCMS.@]
550  *
551  * See GetStandardColorSpaceProfileW.
552  */
553 BOOL WINAPI GetStandardColorSpaceProfileA( PCSTR machine, DWORD id, PSTR profile, PDWORD size )
554 {
555     INT len;
556     LPWSTR profileW;
557     BOOL ret = FALSE;
558     DWORD sizeW;
559 
560     TRACE( "( 0x%08x, %p, %p )\n", id, profile, size );
561 
562     if (machine)
563     {
564         SetLastError( ERROR_NOT_SUPPORTED );
565         return FALSE;
566     }
567 
568     if (!size)
569     {
570         SetLastError( ERROR_INVALID_PARAMETER );
571         return FALSE;
572     }
573 
574     sizeW = *size * sizeof(WCHAR);
575 
576     if (!profile)
577     {
578         ret = GetStandardColorSpaceProfileW( NULL, id, NULL, &sizeW );
579         *size = sizeW / sizeof(WCHAR);
580         return ret;
581     }
582 
583     profileW = HeapAlloc( GetProcessHeap(), 0, sizeW );
584     if (profileW)
585     {
586         if ((ret = GetStandardColorSpaceProfileW( NULL, id, profileW, &sizeW )))
587         {
588             *size = WideCharToMultiByte( CP_ACP, 0, profileW, -1, NULL, 0, NULL, NULL );
589             len = WideCharToMultiByte( CP_ACP, 0, profileW, -1, profile, *size, NULL, NULL );
590             if (!len) ret = FALSE;
591         }
592         else *size = sizeW / sizeof(WCHAR);
593 
594         HeapFree( GetProcessHeap(), 0, profileW );
595     }
596     return ret;
597 }
598 
599 /******************************************************************************
600  * GetStandardColorSpaceProfileW               [MSCMS.@]
601  *
602  * Retrieve the profile filename for a given standard color space id.
603  *
604  * PARAMS
605  *  machine  [I]   Name of the machine for which to get the standard color space.
606  *                 Must be NULL, which indicates the local machine.
607  *  id       [I]   Id of a standard color space.
608  *  profile  [O]   Buffer to receive the profile filename.
609  *  size     [I/O] Size of the filename buffer in bytes.
610  *
611  * RETURNS
612  *  Success: TRUE
613  *  Failure: FALSE
614  */
615 BOOL WINAPI GetStandardColorSpaceProfileW( PCWSTR machine, DWORD id, PWSTR profile, PDWORD size )
616 {
617     static const WCHAR rgbprofilefile[] =
618         { '\\','s','r','g','b',' ','c','o','l','o','r',' ',
619           's','p','a','c','e',' ','p','r','o','f','i','l','e','.','i','c','m',0 };
620     WCHAR rgbprofile[MAX_PATH];
621     DWORD len = sizeof(rgbprofile);
622 
623     TRACE( "( 0x%08x, %p, %p )\n", id, profile, size );
624 
625     if (machine)
626     {
627         SetLastError( ERROR_NOT_SUPPORTED );
628         return FALSE;
629     }
630 
631     if (!size)
632     {
633         SetLastError( ERROR_INVALID_PARAMETER );
634         return FALSE;
635     }
636 
637     if (!profile)
638     {
639         SetLastError( ERROR_INSUFFICIENT_BUFFER );
640         return FALSE;
641     }
642 
643     GetColorDirectoryW( machine, rgbprofile, &len );
644 
645     switch (id)
646     {
647         case LCS_sRGB:
648         case LCS_WINDOWS_COLOR_SPACE: /* FIXME */
649             lstrcatW( rgbprofile, rgbprofilefile );
650             len = lstrlenW( rgbprofile ) * sizeof(WCHAR);
651 
652             if (*size < len)
653             {
654                 *size = len;
655                 SetLastError( ERROR_MORE_DATA );
656                 return FALSE;
657             }
658 
659             lstrcpyW( profile, rgbprofile );
660             break;
661 
662         default:
663             SetLastError( ERROR_FILE_NOT_FOUND );
664             return FALSE;
665     }
666     return TRUE;
667 }
668 
669 static BOOL header_from_file( LPCWSTR file, PPROFILEHEADER header )
670 {
671     BOOL ret;
672     PROFILE profile;
673     WCHAR path[MAX_PATH], slash[] = {'\\',0};
674     DWORD size = sizeof(path);
675     HANDLE handle;
676 
677     ret = GetColorDirectoryW( NULL, path, &size );
678     if (!ret)
679     {
680         WARN( "Can't retrieve color directory\n" );
681         return FALSE;
682     }
683     if (size + sizeof(slash) + sizeof(WCHAR) * lstrlenW( file ) > sizeof(path))
684     {
685         WARN( "Filename too long\n" );
686         return FALSE;
687     }
688 
689     lstrcatW( path, slash );
690     lstrcatW( path, file );
691 
692     profile.dwType = PROFILE_FILENAME;
693     profile.pProfileData = path;
694     profile.cbDataSize = lstrlenW( path ) + 1;
695 
696     handle = OpenColorProfileW( &profile, PROFILE_READ, FILE_SHARE_READ, OPEN_EXISTING );
697     if (!handle)
698     {
699         WARN( "Can't open color profile\n" );
700         return FALSE;
701     }
702 
703     ret = GetColorProfileHeader( handle, header );
704     if (!ret)
705         WARN( "Can't retrieve color profile header\n" );
706 
707     CloseColorProfile( handle );
708     return ret;
709 }
710 
711 static BOOL match_profile( PENUMTYPEW rec, PPROFILEHEADER hdr )
712 {
713     if (rec->dwFields & ET_DEVICENAME)
714     {
715         FIXME( "ET_DEVICENAME: %s\n", debugstr_w(rec->pDeviceName) );
716     }
717     if (rec->dwFields & ET_MEDIATYPE)
718     {
719         FIXME( "ET_MEDIATYPE: 0x%08x\n", rec->dwMediaType );
720     }
721     if (rec->dwFields & ET_DITHERMODE)
722     {
723         FIXME( "ET_DITHERMODE: 0x%08x\n", rec->dwDitheringMode );
724     }
725     if (rec->dwFields & ET_RESOLUTION)
726     {
727         FIXME( "ET_RESOLUTION: 0x%08x, 0x%08x\n",
728                rec->dwResolution[0], rec->dwResolution[1] );
729     }
730     if (rec->dwFields & ET_DEVICECLASS)
731     {
732         FIXME( "ET_DEVICECLASS: %s\n", dbgstr_tag(rec->dwMediaType) );
733     }
734     if (rec->dwFields & ET_CMMTYPE)
735     {
736         TRACE( "ET_CMMTYPE: %s\n", dbgstr_tag(rec->dwCMMType) );
737         if (rec->dwCMMType != hdr->phCMMType) return FALSE;
738     }
739     if (rec->dwFields & ET_CLASS)
740     {
741         TRACE( "ET_CLASS: %s\n", dbgstr_tag(rec->dwClass) );
742         if (rec->dwClass != hdr->phClass) return FALSE;
743     }
744     if (rec->dwFields & ET_DATACOLORSPACE)
745     {
746         TRACE( "ET_DATACOLORSPACE: %s\n", dbgstr_tag(rec->dwDataColorSpace) );
747         if (rec->dwDataColorSpace != hdr->phDataColorSpace) return FALSE;
748     }
749     if (rec->dwFields & ET_CONNECTIONSPACE)
750     {
751         TRACE( "ET_CONNECTIONSPACE: %s\n", dbgstr_tag(rec->dwConnectionSpace) );
752         if (rec->dwConnectionSpace != hdr->phConnectionSpace) return FALSE;
753     }
754     if (rec->dwFields & ET_SIGNATURE)
755     {
756         TRACE( "ET_SIGNATURE: %s\n", dbgstr_tag(rec->dwSignature) );
757         if (rec->dwSignature != hdr->phSignature) return FALSE;
758     }
759     if (rec->dwFields & ET_PLATFORM)
760     {
761         TRACE( "ET_PLATFORM: %s\n", dbgstr_tag(rec->dwPlatform) );
762         if (rec->dwPlatform != hdr->phPlatform) return FALSE;
763     }
764     if (rec->dwFields & ET_PROFILEFLAGS)
765     {
766         TRACE( "ET_PROFILEFLAGS: 0x%08x\n", rec->dwProfileFlags );
767         if (rec->dwProfileFlags != hdr->phProfileFlags) return FALSE;
768     }
769     if (rec->dwFields & ET_MANUFACTURER)
770     {
771         TRACE( "ET_MANUFACTURER: %s\n", dbgstr_tag(rec->dwManufacturer) );
772         if (rec->dwManufacturer != hdr->phManufacturer) return FALSE;
773     }
774     if (rec->dwFields & ET_MODEL)
775     {
776         TRACE( "ET_MODEL: %s\n", dbgstr_tag(rec->dwModel) );
777         if (rec->dwModel != hdr->phModel) return FALSE;
778     }
779     if (rec->dwFields & ET_ATTRIBUTES)
780     {
781         TRACE( "ET_ATTRIBUTES: 0x%08x, 0x%08x\n",
782                rec->dwAttributes[0], rec->dwAttributes[1] );
783         if (rec->dwAttributes[0] != hdr->phAttributes[0] ||
784             rec->dwAttributes[1] != hdr->phAttributes[1]) return FALSE;
785     }
786     if (rec->dwFields & ET_RENDERINGINTENT)
787     {
788         TRACE( "ET_RENDERINGINTENT: 0x%08x\n", rec->dwRenderingIntent );
789         if (rec->dwRenderingIntent != hdr->phRenderingIntent) return FALSE;
790     }
791     if (rec->dwFields & ET_CREATOR)
792     {
793         TRACE( "ET_CREATOR: %s\n", dbgstr_tag(rec->dwCreator) );
794         if (rec->dwCreator != hdr->phCreator) return FALSE;
795     }
796     return TRUE;
797 }
798 
799 /******************************************************************************
800  * EnumColorProfilesA               [MSCMS.@]
801  *
802  * See EnumColorProfilesW.
803  */
804 BOOL WINAPI EnumColorProfilesA( PCSTR machine, PENUMTYPEA record, PBYTE buffer,
805                                 PDWORD size, PDWORD number )
806 {
807     BOOL match, ret = FALSE;
808     char spec[] = "\\*.icm";
809     char colordir[MAX_PATH], glob[MAX_PATH], **profiles = NULL;
810     DWORD i, len = sizeof(colordir), count = 0, totalsize = 0;
811     PROFILEHEADER header;
812     WIN32_FIND_DATAA data;
813     ENUMTYPEW recordW;
814     WCHAR *fileW = NULL, *deviceW = NULL;
815     HANDLE find;
816 
817     TRACE( "( %p, %p, %p, %p, %p )\n", machine, record, buffer, size, number );
818 
819     if (machine || !record || !size ||
820         record->dwSize != sizeof(ENUMTYPEA) ||
821         record->dwVersion != ENUM_TYPE_VERSION) return FALSE;
822 
823     ret = GetColorDirectoryA( machine, colordir, &len );
824     if (!ret || len + sizeof(spec) > MAX_PATH)
825     {
826         WARN( "can't retrieve color directory\n" );
827         return FALSE;
828     }
829 
830     lstrcpyA( glob, colordir );
831     lstrcatA( glob, spec );
832 
833     find = FindFirstFileA( glob, &data );
834     if (find == INVALID_HANDLE_VALUE) return FALSE;
835 
836     profiles = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(char *) + 1 );
837     if (!profiles) goto exit;
838 
839     memcpy( &recordW, record, sizeof(ENUMTYPEA) );
840     if (record->pDeviceName)
841     {
842         deviceW = strdupW( record->pDeviceName );
843         if (!(recordW.pDeviceName = deviceW)) goto exit;
844     }
845 
846     fileW = strdupW( data.cFileName );
847     if (!fileW) goto exit;
848 
849     ret = header_from_file( fileW, &header );
850     if (ret)
851     {
852         match = match_profile( &recordW, &header );
853         if (match)
854         {
855             len = sizeof(char) * (lstrlenA( data.cFileName ) + 1);
856             profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
857 
858             if (!profiles[count]) goto exit;
859             else
860             {
861                 TRACE( "matching profile: %s\n", debugstr_a(data.cFileName) );
862                 lstrcpyA( profiles[count], data.cFileName );
863                 totalsize += len;
864                 count++;
865             }
866         }
867     }
868     HeapFree( GetProcessHeap(), 0, fileW );
869     fileW = NULL;
870 
871     while (FindNextFileA( find, &data ))
872     {
873         fileW = strdupW( data.cFileName );
874         if (!fileW) goto exit;
875 
876         ret = header_from_file( fileW, &header );
877         if (!ret)
878         {
879             HeapFree( GetProcessHeap(), 0, fileW );
880             continue;
881         }
882 
883         match = match_profile( &recordW, &header );
884         if (match)
885         {
886             char **tmp = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
887                                       profiles, sizeof(char *) * (count + 1) );
888             if (!tmp) goto exit;
889             else profiles = tmp;
890 
891             len = sizeof(char) * (lstrlenA( data.cFileName ) + 1);
892             profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
893 
894             if (!profiles[count]) goto exit;
895             else
896             {
897                 TRACE( "matching profile: %s\n", debugstr_a(data.cFileName) );
898                 lstrcpyA( profiles[count], data.cFileName );
899                 totalsize += len;
900                 count++;
901             }
902         }
903         HeapFree( GetProcessHeap(), 0, fileW );
904         fileW = NULL;
905     }
906 
907     totalsize++;
908     if (buffer && *size >= totalsize)
909     {
910         char *p = (char *)buffer;
911 
912         for (i = 0; i < count; i++)
913         {
914             lstrcpyA( p, profiles[i] );
915             p += lstrlenA( profiles[i] ) + 1;
916         }
917         *p = 0;
918         ret = TRUE;
919     }
920     else ret = FALSE;
921 
922     *size = totalsize;
923     if (number) *number = count;
924 
925 exit:
926     for (i = 0; i < count; i++)
927         HeapFree( GetProcessHeap(), 0, profiles[i] );
928     HeapFree( GetProcessHeap(), 0, profiles );
929     HeapFree( GetProcessHeap(), 0, deviceW );
930     HeapFree( GetProcessHeap(), 0, fileW );
931     FindClose( find );
932 
933     return ret;
934 }
935 
936 /******************************************************************************
937  * EnumColorProfilesW               [MSCMS.@]
938  *
939  * Enumerate profiles that match given criteria.
940  *
941  * PARAMS
942  *  machine  [I]   Name of the machine for which to enumerate profiles.
943  *                 Must be NULL, which indicates the local machine.
944  *  record   [I]   Record of criteria that a profile must match.
945  *  buffer   [O]   Buffer to receive a string array of profile filenames.
946  *  size     [I/O] Size of the filename buffer in bytes.
947  *  number   [O]   Number of filenames copied into buffer.
948  *
949  * RETURNS
950  *  Success: TRUE
951  *  Failure: FALSE
952  */
953 BOOL WINAPI EnumColorProfilesW( PCWSTR machine, PENUMTYPEW record, PBYTE buffer,
954                                 PDWORD size, PDWORD number )
955 {
956     BOOL match, ret = FALSE;
957     WCHAR spec[] = {'\\','*','i','c','m',0};
958     WCHAR colordir[MAX_PATH], glob[MAX_PATH], **profiles = NULL;
959     DWORD i, len = sizeof(colordir), count = 0, totalsize = 0;
960     PROFILEHEADER header;
961     WIN32_FIND_DATAW data;
962     HANDLE find;
963 
964     TRACE( "( %p, %p, %p, %p, %p )\n", machine, record, buffer, size, number );
965 
966     if (machine || !record || !size ||
967         record->dwSize != sizeof(ENUMTYPEW) ||
968         record->dwVersion != ENUM_TYPE_VERSION) return FALSE;
969 
970     ret = GetColorDirectoryW( machine, colordir, &len );
971     if (!ret || len + sizeof(spec) > MAX_PATH)
972     {
973         WARN( "Can't retrieve color directory\n" );
974         return FALSE;
975     }
976 
977     lstrcpyW( glob, colordir );
978     lstrcatW( glob, spec );
979 
980     find = FindFirstFileW( glob, &data );
981     if (find == INVALID_HANDLE_VALUE) return FALSE;
982 
983     profiles = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR *) + 1 );
984     if (!profiles) goto exit;
985 
986     ret = header_from_file( data.cFileName, &header );
987     if (ret)
988     {
989         match = match_profile( record, &header );
990         if (match)
991         {
992             len = sizeof(WCHAR) * (lstrlenW( data.cFileName ) + 1);
993             profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
994 
995             if (!profiles[count]) goto exit;
996             else
997             {
998                 TRACE( "matching profile: %s\n", debugstr_w(data.cFileName) );
999                 lstrcpyW( profiles[count], data.cFileName );
1000                 totalsize += len;
1001                 count++;
1002             }
1003         }
1004     }
1005 
1006     while (FindNextFileW( find, &data ))
1007     {
1008         ret = header_from_file( data.cFileName, &header );
1009         if (!ret) continue;
1010 
1011         match = match_profile( record, &header );
1012         if (match)
1013         {
1014             WCHAR **tmp = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
1015                                        profiles, sizeof(WCHAR *) * (count + 1) );
1016             if (!tmp) goto exit;
1017             else profiles = tmp;
1018 
1019             len = sizeof(WCHAR) * (lstrlenW( data.cFileName ) + 1);
1020             profiles[count] = HeapAlloc( GetProcessHeap(), 0, len );
1021 
1022             if (!profiles[count]) goto exit;
1023             else
1024             {
1025                 TRACE( "matching profile: %s\n", debugstr_w(data.cFileName) );
1026                 lstrcpyW( profiles[count], data.cFileName );
1027                 totalsize += len;
1028                 count++;
1029             }
1030         }
1031     }
1032 
1033     totalsize++;
1034     if (buffer && *size >= totalsize)
1035     {
1036         WCHAR *p = (WCHAR *)buffer;
1037 
1038         for (i = 0; i < count; i++)
1039         {
1040             lstrcpyW( p, profiles[i] );
1041             p += lstrlenW( profiles[i] ) + 1;
1042         }
1043         *p = 0;
1044         ret = TRUE;
1045     }
1046     else ret = FALSE;
1047 
1048     *size = totalsize;
1049     if (number) *number = count;
1050 
1051 exit:
1052     for (i = 0; i < count; i++)
1053         HeapFree( GetProcessHeap(), 0, profiles[i] );
1054     HeapFree( GetProcessHeap(), 0, profiles );
1055     FindClose( find );
1056 
1057     return ret;
1058 }
1059 
1060 /******************************************************************************
1061  * InstallColorProfileA               [MSCMS.@]
1062  *
1063  * See InstallColorProfileW.
1064  */
1065 BOOL WINAPI InstallColorProfileA( PCSTR machine, PCSTR profile )
1066 {
1067     UINT len;
1068     LPWSTR profileW;
1069     BOOL ret = FALSE;
1070 
1071     TRACE( "( %s )\n", debugstr_a(profile) );
1072 
1073     if (machine || !profile) return FALSE;
1074 
1075     len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
1076     profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1077 
1078     if (profileW)
1079     {
1080         MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
1081 
1082         ret = InstallColorProfileW( NULL, profileW );
1083         HeapFree( GetProcessHeap(), 0, profileW );
1084     }
1085     return ret;
1086 }
1087 
1088 /******************************************************************************
1089  * InstallColorProfileW               [MSCMS.@]
1090  *
1091  * Install a color profile.
1092  *
1093  * PARAMS
1094  *  machine  [I] Name of the machine to install the profile on. Must be NULL,
1095  *               which indicates the local machine.
1096  *  profile  [I] Full path name of the profile to install.
1097  *
1098  * RETURNS
1099  *  Success: TRUE
1100  *  Failure: FALSE
1101  */
1102 BOOL WINAPI InstallColorProfileW( PCWSTR machine, PCWSTR profile )
1103 {
1104     WCHAR dest[MAX_PATH], base[MAX_PATH];
1105     DWORD size = sizeof(dest);
1106     static const WCHAR slash[] = { '\\', 0 };
1107 
1108     TRACE( "( %s )\n", debugstr_w(profile) );
1109 
1110     if (machine || !profile) return FALSE;
1111 
1112     if (!GetColorDirectoryW( machine, dest, &size )) return FALSE;
1113 
1114     basename( profile, base );
1115 
1116     lstrcatW( dest, slash );
1117     lstrcatW( dest, base );
1118 
1119     /* Is source equal to destination? */
1120     if (!lstrcmpW( profile, dest )) return TRUE;
1121 
1122     return CopyFileW( profile, dest, TRUE );
1123 }
1124 
1125 /******************************************************************************
1126  * IsColorProfileTagPresent               [MSCMS.@]
1127  *
1128  * Determine if a given ICC tag type is present in a color profile.
1129  *
1130  * PARAMS
1131  *  profile  [I] Color profile handle.
1132  *  tag      [I] ICC tag type.
1133  *  present  [O] Pointer to a BOOL variable. Set to TRUE if tag type is present,
1134  *               FALSE otherwise.
1135  *
1136  * RETURNS
1137  *  Success: TRUE
1138  *  Failure: FALSE
1139  */
1140 BOOL WINAPI IsColorProfileTagPresent( HPROFILE handle, TAGTYPE type, PBOOL present )
1141 {
1142     BOOL ret = FALSE;
1143 #ifdef HAVE_LCMS2
1144     struct profile *profile = grab_profile( handle );
1145 
1146     TRACE( "( %p, 0x%08x, %p )\n", handle, type, present );
1147 
1148     if (!profile) return FALSE;
1149 
1150     if (!present)
1151     {
1152         release_profile( profile );
1153         return FALSE;
1154     }
1155     *present = (cmsIsTag( profile->cmsprofile, type ) != 0);
1156     release_profile( profile );
1157     ret = TRUE;
1158 
1159 #endif /* HAVE_LCMS2 */
1160     return ret;
1161 }
1162 
1163 /******************************************************************************
1164  * IsColorProfileValid               [MSCMS.@]
1165  *
1166  * Determine if a given color profile is valid.
1167  *
1168  * PARAMS
1169  *  profile  [I] Color profile handle.
1170  *  valid    [O] Pointer to a BOOL variable. Set to TRUE if profile is valid,
1171  *               FALSE otherwise.
1172  *
1173  * RETURNS
1174  *  Success: TRUE
1175  *  Failure: FALSE
1176  */
1177 BOOL WINAPI IsColorProfileValid( HPROFILE handle, PBOOL valid )
1178 {
1179     BOOL ret = FALSE;
1180 #ifdef HAVE_LCMS2
1181     struct profile *profile = grab_profile( handle );
1182 
1183     TRACE( "( %p, %p )\n", handle, valid );
1184 
1185     if (!profile) return FALSE;
1186 
1187     if (!valid)
1188     {
1189         release_profile( profile );
1190         return FALSE;
1191     }
1192     if (profile->data) ret = *valid = TRUE;
1193     release_profile( profile );
1194 
1195 #endif /* HAVE_LCMS2 */
1196     return ret;
1197 }
1198 
1199 /******************************************************************************
1200  * SetColorProfileElement               [MSCMS.@]
1201  *
1202  * Set data for a specified tag type.
1203  *
1204  * PARAMS
1205  *  profile  [I]   Handle to a color profile.
1206  *  type     [I]   ICC tag type.
1207  *  offset   [I]   Offset in bytes to start copying to.
1208  *  size     [I/O] Size of the buffer in bytes. On return the variable holds the
1209  *                 number of bytes actually needed.
1210  *  buffer   [O]   Buffer holding the tag data.
1211  *
1212  * RETURNS
1213  *  Success: TRUE
1214  *  Failure: FALSE
1215  */
1216 BOOL WINAPI SetColorProfileElement( HPROFILE handle, TAGTYPE type, DWORD offset, PDWORD size,
1217                                     PVOID buffer )
1218 {
1219     BOOL ret = FALSE;
1220 #ifdef HAVE_LCMS2
1221     struct profile *profile = grab_profile( handle );
1222 
1223     TRACE( "( %p, 0x%08x, %d, %p, %p )\n", handle, type, offset, size, buffer );
1224 
1225     if (!profile) return FALSE;
1226 
1227     if (!size || !buffer || !(profile->access & PROFILE_READWRITE))
1228     {
1229         release_profile( profile );
1230         return FALSE;
1231     }
1232     ret = set_tag_data( profile, type, offset, buffer, size );
1233     release_profile( profile );
1234 #endif /* HAVE_LCMS2 */
1235     return ret;
1236 }
1237 
1238 /******************************************************************************
1239  * SetColorProfileHeader               [MSCMS.@]
1240  *
1241  * Set header data for a given profile.
1242  *
1243  * PARAMS
1244  *  profile  [I] Handle to a color profile.
1245  *  header   [I] Buffer holding the header data.
1246  *
1247  * RETURNS
1248  *  Success: TRUE
1249  *  Failure: FALSE
1250  */
1251 BOOL WINAPI SetColorProfileHeader( HPROFILE handle, PPROFILEHEADER header )
1252 {
1253 #ifdef HAVE_LCMS2
1254     struct profile *profile = grab_profile( handle );
1255 
1256     TRACE( "( %p, %p )\n", handle, header );
1257 
1258     if (!profile) return FALSE;
1259 
1260     if (!header || !(profile->access & PROFILE_READWRITE))
1261     {
1262         release_profile( profile );
1263         return FALSE;
1264     }
1265     set_profile_header( profile, header );
1266     release_profile( profile );
1267     return TRUE;
1268 
1269 #else
1270     return FALSE;
1271 #endif /* HAVE_LCMS2 */
1272 }
1273 
1274 /******************************************************************************
1275  * UninstallColorProfileA               [MSCMS.@]
1276  *
1277  * See UninstallColorProfileW.
1278  */
1279 BOOL WINAPI UninstallColorProfileA( PCSTR machine, PCSTR profile, BOOL delete )
1280 {
1281     UINT len;
1282     LPWSTR profileW;
1283     BOOL ret = FALSE;
1284 
1285     TRACE( "( %s, %x )\n", debugstr_a(profile), delete );
1286 
1287     if (machine || !profile) return FALSE;
1288 
1289     len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
1290     profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1291 
1292     if (profileW)
1293     {
1294         MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
1295 
1296         ret = UninstallColorProfileW( NULL, profileW , delete );
1297 
1298         HeapFree( GetProcessHeap(), 0, profileW );
1299     }
1300     return ret;
1301 }
1302 
1303 /******************************************************************************
1304  * UninstallColorProfileW               [MSCMS.@]
1305  *
1306  * Uninstall a color profile.
1307  *
1308  * PARAMS
1309  *  machine  [I] Name of the machine to uninstall the profile on. Must be NULL,
1310  *               which indicates the local machine.
1311  *  profile  [I] Full path name of the profile to uninstall.
1312  *  delete   [I] Bool that specifies whether the profile file should be deleted.
1313  *
1314  * RETURNS
1315  *  Success: TRUE
1316  *  Failure: FALSE
1317  */
1318 BOOL WINAPI UninstallColorProfileW( PCWSTR machine, PCWSTR profile, BOOL delete )
1319 {
1320     TRACE( "( %s, %x )\n", debugstr_w(profile), delete );
1321 
1322     if (machine || !profile) return FALSE;
1323 
1324     if (delete) return DeleteFileW( profile );
1325 
1326     return TRUE;
1327 }
1328 
1329 /******************************************************************************
1330  * OpenColorProfileA               [MSCMS.@]
1331  *
1332  * See OpenColorProfileW.
1333  */
1334 HPROFILE WINAPI OpenColorProfileA( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
1335 {
1336     HPROFILE handle = NULL;
1337 
1338     TRACE( "( %p, 0x%08x, 0x%08x, 0x%08x )\n", profile, access, sharing, creation );
1339 
1340     if (!profile || !profile->pProfileData) return NULL;
1341 
1342     /* No AW conversion needed for memory based profiles */
1343     if (profile->dwType & PROFILE_MEMBUFFER)
1344         return OpenColorProfileW( profile, access, sharing, creation );
1345 
1346     if (profile->dwType & PROFILE_FILENAME)
1347     {
1348         UINT len;
1349         PROFILE profileW;
1350 
1351         profileW.dwType = profile->dwType;
1352 
1353         len = MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, NULL, 0 );
1354         profileW.pProfileData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1355 
1356         if (profileW.pProfileData)
1357         {
1358             profileW.cbDataSize = len * sizeof(WCHAR);
1359             MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, profileW.pProfileData, len );
1360 
1361             handle = OpenColorProfileW( &profileW, access, sharing, creation );
1362             HeapFree( GetProcessHeap(), 0, profileW.pProfileData );
1363         }
1364     }
1365     return handle;
1366 }
1367 
1368 /******************************************************************************
1369  * OpenColorProfileW               [MSCMS.@]
1370  *
1371  * Open a color profile.
1372  *
1373  * PARAMS
1374  *  profile   [I] Pointer to a color profile structure.
1375  *  access    [I] Desired access.
1376  *  sharing   [I] Sharing mode.
1377  *  creation  [I] Creation mode.
1378  *
1379  * RETURNS
1380  *  Success: Handle to the opened profile.
1381  *  Failure: NULL
1382  *
1383  * NOTES
1384  *  Values for access:   PROFILE_READ or PROFILE_READWRITE.
1385  *  Values for sharing:  0 (no sharing), FILE_SHARE_READ and/or FILE_SHARE_WRITE.
1386  *  Values for creation: one of CREATE_NEW, CREATE_ALWAYS, OPEN_EXISTING,
1387  *                       OPEN_ALWAYS, TRUNCATE_EXISTING.
1388  *  Sharing and creation flags are ignored for memory based profiles.
1389  */
1390 HPROFILE WINAPI OpenColorProfileW( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
1391 {
1392 #ifdef HAVE_LCMS2
1393     cmsHPROFILE cmsprofile = NULL;
1394     char *data = NULL;
1395     HANDLE handle = INVALID_HANDLE_VALUE;
1396     DWORD size;
1397 
1398     TRACE( "( %p, 0x%08x, 0x%08x, 0x%08x )\n", profile, access, sharing, creation );
1399 
1400     if (!profile || !profile->pProfileData) return NULL;
1401 
1402     if (profile->dwType == PROFILE_MEMBUFFER)
1403     {
1404         /* FIXME: access flags not implemented for memory based profiles */
1405 
1406         if (!(data = HeapAlloc( GetProcessHeap(), 0, profile->cbDataSize ))) return NULL;
1407         memcpy( data, profile->pProfileData, profile->cbDataSize );
1408 
1409         if (!(cmsprofile = cmsOpenProfileFromMem( data, profile->cbDataSize )))
1410         {
1411             HeapFree( GetProcessHeap(), 0, data );
1412             return FALSE;
1413         }
1414         size = profile->cbDataSize;
1415     }
1416     else if (profile->dwType == PROFILE_FILENAME)
1417     {
1418         DWORD read, flags = 0;
1419 
1420         TRACE( "profile file: %s\n", debugstr_w( profile->pProfileData ) );
1421 
1422         if (access & PROFILE_READ) flags = GENERIC_READ;
1423         if (access & PROFILE_READWRITE) flags = GENERIC_READ|GENERIC_WRITE;
1424 
1425         if (!flags) return NULL;
1426         if (!sharing) sharing = FILE_SHARE_READ;
1427 
1428         if (!PathIsRelativeW( profile->pProfileData ))
1429             handle = CreateFileW( profile->pProfileData, flags, sharing, NULL, creation, 0, NULL );
1430         else
1431         {
1432             WCHAR *path;
1433 
1434             if (!GetColorDirectoryW( NULL, NULL, &size ) && GetLastError() == ERROR_MORE_DATA)
1435             {
1436                 size += (strlenW( profile->pProfileData ) + 2) * sizeof(WCHAR);
1437                 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return NULL;
1438                 GetColorDirectoryW( NULL, path, &size );
1439                 PathAddBackslashW( path );
1440                 strcatW( path, profile->pProfileData );
1441             }
1442             else return NULL;
1443             handle = CreateFileW( path, flags, sharing, NULL, creation, 0, NULL );
1444             HeapFree( GetProcessHeap(), 0, path );
1445         }
1446         if (handle == INVALID_HANDLE_VALUE)
1447         {
1448             WARN( "Unable to open color profile %u\n", GetLastError() );
1449             return NULL;
1450         }
1451         if ((size = GetFileSize( handle, NULL )) == INVALID_FILE_SIZE)
1452         {
1453             ERR( "Unable to retrieve size of color profile\n" );
1454             CloseHandle( handle );
1455             return NULL;
1456         }
1457         if (!(data = HeapAlloc( GetProcessHeap(), 0, size )))
1458         {
1459             ERR( "Unable to allocate memory for color profile\n" );
1460             CloseHandle( handle );
1461             return NULL;
1462         }
1463         if (!ReadFile( handle, data, size, &read, NULL ) || read != size)
1464         {
1465             ERR( "Unable to read color profile\n" );
1466 
1467             CloseHandle( handle );
1468             HeapFree( GetProcessHeap(), 0, data );
1469             return NULL;
1470         }
1471         if (!(cmsprofile = cmsOpenProfileFromMem( data, size )))
1472         {
1473             CloseHandle( handle );
1474             HeapFree( GetProcessHeap(), 0, data );
1475             return NULL;
1476         }
1477     }
1478     else
1479     {
1480         ERR( "Invalid profile type %u\n", profile->dwType );
1481         return NULL;
1482     }
1483 
1484     if (cmsprofile)
1485     {
1486         struct profile profile;
1487         HPROFILE hprof;
1488 
1489         profile.file       = handle;
1490         profile.access     = access;
1491         profile.data       = data;
1492         profile.size       = size;
1493         profile.cmsprofile = cmsprofile;
1494 
1495         if ((hprof = create_profile( &profile ))) return hprof;
1496         HeapFree( GetProcessHeap(), 0, data );
1497         cmsCloseProfile( cmsprofile );
1498     }
1499     CloseHandle( handle );
1500 
1501 #endif /* HAVE_LCMS2 */
1502     return NULL;
1503 }
1504 
1505 /******************************************************************************
1506  * CloseColorProfile               [MSCMS.@]
1507  *
1508  * Close a color profile.
1509  *
1510  * PARAMS
1511  *  profile  [I] Handle to the profile.
1512  *
1513  * RETURNS
1514  *  Success: TRUE
1515  *  Failure: FALSE
1516  */
1517 BOOL WINAPI CloseColorProfile( HPROFILE profile )
1518 {
1519     BOOL ret = FALSE;
1520 #ifdef HAVE_LCMS2
1521 
1522     TRACE( "( %p )\n", profile );
1523     ret = close_profile( profile );
1524 
1525 #endif /* HAVE_LCMS2 */
1526     return ret;
1527 }
1528