mshtml: Added IHTMLWindow2::focus implementation.
[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 PSStringFromPropertyKey(REFPROPERTYKEY pkey, LPWSTR psz, UINT cch)
76 {
77     static const WCHAR guid_fmtW[] = {'{','%','0','8','X','-','%','0','4','X','-',
78                                       '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
79                                       '%','0','2','X','%','0','2','X','%','0','2','X',
80                                       '%','0','2','X','%','0','2','X','%','0','2','X','}',0};
81     static const WCHAR pid_fmtW[] = {'%','u',0};
82
83     WCHAR pidW[PKEY_PIDSTR_MAX + 1];
84     LPWSTR p = psz;
85     int len;
86
87     TRACE("(%p, %p, %u)\n", pkey, psz, cch);
88
89     if (!psz)
90         return E_POINTER;
91
92     /* GUIDSTRING_MAX accounts for null terminator, +1 for space character. */
93     if (cch <= GUIDSTRING_MAX + 1)
94         return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
95
96     if (!pkey)
97     {
98         psz[0] = '\0';
99         return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
100     }
101
102     sprintfW(psz, guid_fmtW, pkey->fmtid.Data1, pkey->fmtid.Data2,
103              pkey->fmtid.Data3, pkey->fmtid.Data4[0], pkey->fmtid.Data4[1],
104              pkey->fmtid.Data4[2], pkey->fmtid.Data4[3], pkey->fmtid.Data4[4],
105              pkey->fmtid.Data4[5], pkey->fmtid.Data4[6], pkey->fmtid.Data4[7]);
106
107     /* Overwrite the null terminator with the space character. */
108     p += GUIDSTRING_MAX - 1;
109     *p++ = ' ';
110     cch -= GUIDSTRING_MAX - 1 + 1;
111
112     len = sprintfW(pidW, pid_fmtW, pkey->pid);
113
114     if (cch >= len + 1)
115     {
116         strcpyW(p, pidW);
117         return S_OK;
118     }
119     else
120     {
121         WCHAR *ptr = pidW + len - 1;
122
123         psz[0] = '\0';
124         *p++ = '\0';
125         cch--;
126
127         /* Replicate a quirk of the native implementation where the contents
128          * of the property ID string are written backwards to the output
129          * buffer, skipping the rightmost digit. */
130         if (cch)
131         {
132             ptr--;
133             while (cch--)
134                 *p++ = *ptr--;
135         }
136
137         return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
138     }
139 }
140
141 static const BYTE hex2bin[] =
142 {
143     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x00 */
144     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x10 */
145     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x20 */
146     0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,        /* 0x30 */
147     0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,  /* 0x40 */
148     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x50 */
149     0,10,11,12,13,14,15                     /* 0x60 */
150 };
151
152 static BOOL validate_indices(LPCWSTR s, int min, int max)
153 {
154     int i;
155
156     for (i = min; i <= max; i++)
157     {
158         if (!s[i])
159             return FALSE;
160
161         if (i == 0)
162         {
163             if (s[i] != '{')
164                 return FALSE;
165         }
166         else if (i == 9 || i == 14 || i == 19 || i == 24)
167         {
168             if (s[i] != '-')
169                 return FALSE;
170         }
171         else if (i == 37)
172         {
173             if (s[i] != '}')
174                 return FALSE;
175         }
176         else
177         {
178             if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0'))
179                 return FALSE;
180         }
181     }
182
183     return TRUE;
184 }
185
186 /* Adapted from CLSIDFromString helper in dlls/ole32/compobj.c and
187  * UuidFromString in dlls/rpcrt4/rpcrt4_main.c. */
188 static BOOL string_to_guid(LPCWSTR s, LPGUID id)
189 {
190     /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
191
192     if (!validate_indices(s, 0, 8)) return FALSE;
193     id->Data1 = (hex2bin[s[1]] << 28 | hex2bin[s[2]] << 24 | hex2bin[s[3]] << 20 | hex2bin[s[4]] << 16 |
194                  hex2bin[s[5]] << 12 | hex2bin[s[6]] << 8  | hex2bin[s[7]] << 4  | hex2bin[s[8]]);
195     if (!validate_indices(s, 9, 14)) return FALSE;
196     id->Data2 = hex2bin[s[10]] << 12 | hex2bin[s[11]] << 8 | hex2bin[s[12]] << 4 | hex2bin[s[13]];
197     if (!validate_indices(s, 15, 19)) return FALSE;
198     id->Data3 = hex2bin[s[15]] << 12 | hex2bin[s[16]] << 8 | hex2bin[s[17]] << 4 | hex2bin[s[18]];
199
200     /* these are just sequential bytes */
201
202     if (!validate_indices(s, 20, 21)) return FALSE;
203     id->Data4[0] = hex2bin[s[20]] << 4 | hex2bin[s[21]];
204     if (!validate_indices(s, 22, 24)) return FALSE;
205     id->Data4[1] = hex2bin[s[22]] << 4 | hex2bin[s[23]];
206
207     if (!validate_indices(s, 25, 26)) return FALSE;
208     id->Data4[2] = hex2bin[s[25]] << 4 | hex2bin[s[26]];
209     if (!validate_indices(s, 27, 28)) return FALSE;
210     id->Data4[3] = hex2bin[s[27]] << 4 | hex2bin[s[28]];
211     if (!validate_indices(s, 29, 30)) return FALSE;
212     id->Data4[4] = hex2bin[s[29]] << 4 | hex2bin[s[30]];
213     if (!validate_indices(s, 31, 32)) return FALSE;
214     id->Data4[5] = hex2bin[s[31]] << 4 | hex2bin[s[32]];
215     if (!validate_indices(s, 33, 34)) return FALSE;
216     id->Data4[6] = hex2bin[s[33]] << 4 | hex2bin[s[34]];
217     if (!validate_indices(s, 35, 37)) return FALSE;
218     id->Data4[7] = hex2bin[s[35]] << 4 | hex2bin[s[36]];
219
220     return TRUE;
221 }
222
223 HRESULT WINAPI PSPropertyKeyFromString(LPCWSTR pszString, PROPERTYKEY *pkey)
224 {
225     int has_minus = 0, has_comma = 0;
226
227     TRACE("(%s, %p)\n", debugstr_w(pszString), pkey);
228
229     if (!pszString || !pkey)
230         return E_POINTER;
231
232     memset(pkey, 0, sizeof(PROPERTYKEY));
233
234     if (!string_to_guid(pszString, &pkey->fmtid))
235         return E_INVALIDARG;
236
237     pszString += GUIDSTRING_MAX - 1;
238
239     if (!*pszString)
240         return E_INVALIDARG;
241
242     /* Only the space seems to be recognized as whitespace. The comma is only
243      * recognized once and processing terminates if another comma is found. */
244     while (*pszString == ' ' || *pszString == ',')
245     {
246         if (*pszString == ',')
247         {
248             if (has_comma)
249                 return S_OK;
250             else
251                 has_comma = 1;
252         }
253         pszString++;
254     }
255
256     if (!*pszString)
257         return E_INVALIDARG;
258
259     /* Only two minus signs are recognized if no comma is detected. The first
260      * sign is ignored, and the second is interpreted. If a comma is detected
261      * before the minus sign, then only one minus sign counts, and property ID
262      * interpretation begins with the next character. */
263     if (has_comma)
264     {
265         if (*pszString == '-')
266         {
267             has_minus = 1;
268             pszString++;
269         }
270     }
271     else
272     {
273         if (*pszString == '-')
274             pszString++;
275
276         /* Skip any intermediate spaces after the first minus sign. */
277         while (*pszString == ' ')
278             pszString++;
279
280         if (*pszString == '-')
281         {
282             has_minus = 1;
283             pszString++;
284         }
285
286         /* Skip any remaining spaces after minus sign. */
287         while (*pszString == ' ')
288             pszString++;
289     }
290
291     /* Overflow is not checked. */
292     while (isdigitW(*pszString))
293     {
294         pkey->pid *= 10;
295         pkey->pid += (*pszString - '0');
296         pszString++;
297     }
298
299     if (has_minus)
300         pkey->pid = ~pkey->pid + 1;
301
302     return S_OK;
303 }