urlmon: Fixed misc.c tests on IE9 and make image_tiff_filter behave like IE9.
[wine] / dlls / propsys / propsys_main.c
1 /*
2  * propsys main
3  *
4  * Copyright 1997, 2002 Alexandre Julliard
5  * Copyright 2008 James Hawkins
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23
24 #include <stdarg.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "objbase.h"
29 #include "propsys.h"
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(propsys);
34
35 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
36 {
37     TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
38
39     switch (fdwReason)
40     {
41         case DLL_WINE_PREATTACH:
42             return FALSE;    /* prefer native version */
43         case DLL_PROCESS_ATTACH:
44             DisableThreadLibraryCalls(hinstDLL);
45             break;
46         case DLL_PROCESS_DETACH:
47             break;
48         default:
49             break;
50     }
51
52     return TRUE;
53 }
54
55 HRESULT WINAPI PSRegisterPropertySchema(PCWSTR path)
56 {
57     FIXME("%s stub\n", debugstr_w(path));
58
59     return S_OK;
60 }
61
62 HRESULT WINAPI PSUnregisterPropertySchema(PCWSTR path)
63 {
64     FIXME("%s stub\n", debugstr_w(path));
65
66     return E_NOTIMPL;
67 }
68
69 HRESULT WINAPI PSGetPropertyDescription(REFPROPERTYKEY propkey, REFIID riid, void **ppv)
70 {
71     FIXME("%p, %p, %p\n", propkey, riid, ppv);
72     return E_NOTIMPL;
73 }
74
75 HRESULT WINAPI PSRefreshPropertySchema(void)
76 {
77     FIXME("\n");
78     return S_OK;
79 }
80
81 HRESULT WINAPI PSStringFromPropertyKey(REFPROPERTYKEY pkey, LPWSTR psz, UINT cch)
82 {
83     static const WCHAR guid_fmtW[] = {'{','%','0','8','X','-','%','0','4','X','-',
84                                       '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
85                                       '%','0','2','X','%','0','2','X','%','0','2','X',
86                                       '%','0','2','X','%','0','2','X','%','0','2','X','}',0};
87     static const WCHAR pid_fmtW[] = {'%','u',0};
88
89     WCHAR pidW[PKEY_PIDSTR_MAX + 1];
90     LPWSTR p = psz;
91     int len;
92
93     TRACE("(%p, %p, %u)\n", pkey, psz, cch);
94
95     if (!psz)
96         return E_POINTER;
97
98     /* GUIDSTRING_MAX accounts for null terminator, +1 for space character. */
99     if (cch <= GUIDSTRING_MAX + 1)
100         return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
101
102     if (!pkey)
103     {
104         psz[0] = '\0';
105         return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
106     }
107
108     sprintfW(psz, guid_fmtW, pkey->fmtid.Data1, pkey->fmtid.Data2,
109              pkey->fmtid.Data3, pkey->fmtid.Data4[0], pkey->fmtid.Data4[1],
110              pkey->fmtid.Data4[2], pkey->fmtid.Data4[3], pkey->fmtid.Data4[4],
111              pkey->fmtid.Data4[5], pkey->fmtid.Data4[6], pkey->fmtid.Data4[7]);
112
113     /* Overwrite the null terminator with the space character. */
114     p += GUIDSTRING_MAX - 1;
115     *p++ = ' ';
116     cch -= GUIDSTRING_MAX - 1 + 1;
117
118     len = sprintfW(pidW, pid_fmtW, pkey->pid);
119
120     if (cch >= len + 1)
121     {
122         strcpyW(p, pidW);
123         return S_OK;
124     }
125     else
126     {
127         WCHAR *ptr = pidW + len - 1;
128
129         psz[0] = '\0';
130         *p++ = '\0';
131         cch--;
132
133         /* Replicate a quirk of the native implementation where the contents
134          * of the property ID string are written backwards to the output
135          * buffer, skipping the rightmost digit. */
136         if (cch)
137         {
138             ptr--;
139             while (cch--)
140                 *p++ = *ptr--;
141         }
142
143         return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
144     }
145 }
146
147 static const BYTE hex2bin[] =
148 {
149     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x00 */
150     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x10 */
151     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x20 */
152     0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,        /* 0x30 */
153     0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,  /* 0x40 */
154     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x50 */
155     0,10,11,12,13,14,15                     /* 0x60 */
156 };
157
158 static BOOL validate_indices(LPCWSTR s, int min, int max)
159 {
160     int i;
161
162     for (i = min; i <= max; i++)
163     {
164         if (!s[i])
165             return FALSE;
166
167         if (i == 0)
168         {
169             if (s[i] != '{')
170                 return FALSE;
171         }
172         else if (i == 9 || i == 14 || i == 19 || i == 24)
173         {
174             if (s[i] != '-')
175                 return FALSE;
176         }
177         else if (i == 37)
178         {
179             if (s[i] != '}')
180                 return FALSE;
181         }
182         else
183         {
184             if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0'))
185                 return FALSE;
186         }
187     }
188
189     return TRUE;
190 }
191
192 /* Adapted from CLSIDFromString helper in dlls/ole32/compobj.c and
193  * UuidFromString in dlls/rpcrt4/rpcrt4_main.c. */
194 static BOOL string_to_guid(LPCWSTR s, LPGUID id)
195 {
196     /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
197
198     if (!validate_indices(s, 0, 8)) return FALSE;
199     id->Data1 = (hex2bin[s[1]] << 28 | hex2bin[s[2]] << 24 | hex2bin[s[3]] << 20 | hex2bin[s[4]] << 16 |
200                  hex2bin[s[5]] << 12 | hex2bin[s[6]] << 8  | hex2bin[s[7]] << 4  | hex2bin[s[8]]);
201     if (!validate_indices(s, 9, 14)) return FALSE;
202     id->Data2 = hex2bin[s[10]] << 12 | hex2bin[s[11]] << 8 | hex2bin[s[12]] << 4 | hex2bin[s[13]];
203     if (!validate_indices(s, 15, 19)) return FALSE;
204     id->Data3 = hex2bin[s[15]] << 12 | hex2bin[s[16]] << 8 | hex2bin[s[17]] << 4 | hex2bin[s[18]];
205
206     /* these are just sequential bytes */
207
208     if (!validate_indices(s, 20, 21)) return FALSE;
209     id->Data4[0] = hex2bin[s[20]] << 4 | hex2bin[s[21]];
210     if (!validate_indices(s, 22, 24)) return FALSE;
211     id->Data4[1] = hex2bin[s[22]] << 4 | hex2bin[s[23]];
212
213     if (!validate_indices(s, 25, 26)) return FALSE;
214     id->Data4[2] = hex2bin[s[25]] << 4 | hex2bin[s[26]];
215     if (!validate_indices(s, 27, 28)) return FALSE;
216     id->Data4[3] = hex2bin[s[27]] << 4 | hex2bin[s[28]];
217     if (!validate_indices(s, 29, 30)) return FALSE;
218     id->Data4[4] = hex2bin[s[29]] << 4 | hex2bin[s[30]];
219     if (!validate_indices(s, 31, 32)) return FALSE;
220     id->Data4[5] = hex2bin[s[31]] << 4 | hex2bin[s[32]];
221     if (!validate_indices(s, 33, 34)) return FALSE;
222     id->Data4[6] = hex2bin[s[33]] << 4 | hex2bin[s[34]];
223     if (!validate_indices(s, 35, 37)) return FALSE;
224     id->Data4[7] = hex2bin[s[35]] << 4 | hex2bin[s[36]];
225
226     return TRUE;
227 }
228
229 HRESULT WINAPI PSPropertyKeyFromString(LPCWSTR pszString, PROPERTYKEY *pkey)
230 {
231     int has_minus = 0, has_comma = 0;
232
233     TRACE("(%s, %p)\n", debugstr_w(pszString), pkey);
234
235     if (!pszString || !pkey)
236         return E_POINTER;
237
238     memset(pkey, 0, sizeof(PROPERTYKEY));
239
240     if (!string_to_guid(pszString, &pkey->fmtid))
241         return E_INVALIDARG;
242
243     pszString += GUIDSTRING_MAX - 1;
244
245     if (!*pszString)
246         return E_INVALIDARG;
247
248     /* Only the space seems to be recognized as whitespace. The comma is only
249      * recognized once and processing terminates if another comma is found. */
250     while (*pszString == ' ' || *pszString == ',')
251     {
252         if (*pszString == ',')
253         {
254             if (has_comma)
255                 return S_OK;
256             else
257                 has_comma = 1;
258         }
259         pszString++;
260     }
261
262     if (!*pszString)
263         return E_INVALIDARG;
264
265     /* Only two minus signs are recognized if no comma is detected. The first
266      * sign is ignored, and the second is interpreted. If a comma is detected
267      * before the minus sign, then only one minus sign counts, and property ID
268      * interpretation begins with the next character. */
269     if (has_comma)
270     {
271         if (*pszString == '-')
272         {
273             has_minus = 1;
274             pszString++;
275         }
276     }
277     else
278     {
279         if (*pszString == '-')
280             pszString++;
281
282         /* Skip any intermediate spaces after the first minus sign. */
283         while (*pszString == ' ')
284             pszString++;
285
286         if (*pszString == '-')
287         {
288             has_minus = 1;
289             pszString++;
290         }
291
292         /* Skip any remaining spaces after minus sign. */
293         while (*pszString == ' ')
294             pszString++;
295     }
296
297     /* Overflow is not checked. */
298     while (isdigitW(*pszString))
299     {
300         pkey->pid *= 10;
301         pkey->pid += (*pszString - '0');
302         pszString++;
303     }
304
305     if (has_minus)
306         pkey->pid = ~pkey->pid + 1;
307
308     return S_OK;
309 }