wbemprox: Implement IEnumWbemClassObject::Clone.
[wine] / dlls / hhctrl.ocx / chm.c
1 /*
2  * CHM Utility API
3  *
4  * Copyright 2005 James Hawkins
5  * Copyright 2007 Jacek Caban
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 "hhctrl.h"
23 #include "stream.h"
24
25 #include "winreg.h"
26 #include "shlwapi.h"
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
30
31 /* Reads a string from the #STRINGS section in the CHM file */
32 static LPCSTR GetChmString(CHMInfo *chm, DWORD offset)
33 {
34     LPCSTR str;
35
36     if(!chm->strings_stream)
37         return NULL;
38
39     if(chm->strings_size <= (offset >> BLOCK_BITS)) {
40         chm->strings_size = (offset >> BLOCK_BITS)+1;
41         if(chm->strings)
42             chm->strings = heap_realloc_zero(chm->strings,
43                     chm->strings_size*sizeof(char*));
44         else
45             chm->strings = heap_alloc_zero(
46                     chm->strings_size*sizeof(char*));
47
48     }
49
50     if(!chm->strings[offset >> BLOCK_BITS]) {
51         LARGE_INTEGER pos;
52         DWORD read;
53         HRESULT hres;
54
55         pos.QuadPart = offset & ~BLOCK_MASK;
56         hres = IStream_Seek(chm->strings_stream, pos, STREAM_SEEK_SET, NULL);
57         if(FAILED(hres)) {
58             WARN("Seek failed: %08x\n", hres);
59             return NULL;
60         }
61
62         chm->strings[offset >> BLOCK_BITS] = heap_alloc(BLOCK_SIZE);
63
64         hres = IStream_Read(chm->strings_stream, chm->strings[offset >> BLOCK_BITS],
65                             BLOCK_SIZE, &read);
66         if(FAILED(hres)) {
67             WARN("Read failed: %08x\n", hres);
68             heap_free(chm->strings[offset >> BLOCK_BITS]);
69             chm->strings[offset >> BLOCK_BITS] = NULL;
70             return NULL;
71         }
72     }
73
74     str = chm->strings[offset >> BLOCK_BITS] + (offset & BLOCK_MASK);
75     TRACE("offset %#x => %s\n", offset, debugstr_a(str));
76     return str;
77 }
78
79 static BOOL ReadChmSystem(CHMInfo *chm)
80 {
81     IStream *stream;
82     DWORD ver=0xdeadbeef, read, buf_size;
83     char *buf;
84     HRESULT hres;
85
86     struct {
87         WORD code;
88         WORD len;
89     } entry;
90
91     static const WCHAR wszSYSTEM[] = {'#','S','Y','S','T','E','M',0};
92
93     hres = IStorage_OpenStream(chm->pStorage, wszSYSTEM, NULL, STGM_READ, 0, &stream);
94     if(FAILED(hres)) {
95         WARN("Could not open #SYSTEM stream: %08x\n", hres);
96         return FALSE;
97     }
98
99     IStream_Read(stream, &ver, sizeof(ver), &read);
100     TRACE("version is %x\n", ver);
101
102     buf = heap_alloc(8*sizeof(DWORD));
103     buf_size = 8*sizeof(DWORD);
104
105     while(1) {
106         hres = IStream_Read(stream, &entry, sizeof(entry), &read);
107         if(hres != S_OK)
108             break;
109
110         if(entry.len > buf_size)
111             buf = heap_realloc(buf, buf_size=entry.len);
112
113         hres = IStream_Read(stream, buf, entry.len, &read);
114         if(hres != S_OK)
115             break;
116
117         switch(entry.code) {
118         case 0x0:
119             TRACE("TOC is %s\n", debugstr_an(buf, entry.len));
120             heap_free(chm->defToc);
121             chm->defToc = strdupnAtoW(buf, entry.len);
122             break;
123         case 0x2:
124             TRACE("Default topic is %s\n", debugstr_an(buf, entry.len));
125             heap_free(chm->defTopic);
126             chm->defTopic = strdupnAtoW(buf, entry.len);
127             break;
128         case 0x3:
129             TRACE("Title is %s\n", debugstr_an(buf, entry.len));
130             heap_free(chm->defTitle);
131             chm->defTitle = strdupnAtoW(buf, entry.len);
132             break;
133         case 0x4:
134             /* TODO: Currently only the Locale ID is loaded from this field */
135             TRACE("Locale is: %d\n", *(LCID*)&buf[0]);
136             if(!GetLocaleInfoW(*(LCID*)&buf[0], LOCALE_IDEFAULTANSICODEPAGE|LOCALE_RETURN_NUMBER,
137                                (WCHAR *)&chm->codePage, sizeof(chm->codePage)/sizeof(WCHAR)))
138                 chm->codePage = CP_ACP;
139             break;
140         case 0x5:
141             TRACE("Default window is %s\n", debugstr_an(buf, entry.len));
142             break;
143         case 0x6:
144             TRACE("Compiled file is %s\n", debugstr_an(buf, entry.len));
145             heap_free(chm->compiledFile);
146             chm->compiledFile = strdupnAtoW(buf, entry.len);
147             break;
148         case 0x9:
149             TRACE("Version is %s\n", debugstr_an(buf, entry.len));
150             break;
151         case 0xa:
152             TRACE("Time is %08x\n", *(DWORD*)buf);
153             break;
154         case 0xc:
155             TRACE("Number of info types: %d\n", *(DWORD*)buf);
156             break;
157         case 0xf:
158             TRACE("Check sum: %x\n", *(DWORD*)buf);
159             break;
160         default:
161             TRACE("unhandled code %x, size %x\n", entry.code, entry.len);
162         }
163     }
164
165     heap_free(buf);
166     IStream_Release(stream);
167
168     return SUCCEEDED(hres);
169 }
170
171 LPWSTR FindContextAlias(CHMInfo *chm, DWORD index)
172 {
173     IStream *ivb_stream;
174     DWORD size, read, i;
175     DWORD *buf;
176     LPCSTR ret = NULL;
177     HRESULT hres;
178
179     static const WCHAR wszIVB[] = {'#','I','V','B',0};
180
181     hres = IStorage_OpenStream(chm->pStorage, wszIVB, NULL, STGM_READ, 0, &ivb_stream);
182     if(FAILED(hres)) {
183         WARN("Could not open #IVB stream: %08x\n", hres);
184         return NULL;
185     }
186
187     hres = IStream_Read(ivb_stream, &size, sizeof(size), &read);
188     if(FAILED(hres)) {
189         WARN("Read failed: %08x\n", hres);
190         IStream_Release(ivb_stream);
191         return NULL;
192     }
193
194     buf = heap_alloc(size);
195     hres = IStream_Read(ivb_stream, buf, size, &read);
196     IStream_Release(ivb_stream);
197     if(FAILED(hres)) {
198         WARN("Read failed: %08x\n", hres);
199         heap_free(buf);
200         return NULL;
201     }
202
203     size /= 2*sizeof(DWORD);
204
205     for(i=0; i<size; i++) {
206         if(buf[2*i] == index) {
207             ret = GetChmString(chm, buf[2*i+1]);
208             break;
209         }
210     }
211
212     heap_free(buf);
213
214     TRACE("returning %s\n", debugstr_a(ret));
215     return strdupAtoW(ret);
216 }
217
218 /*
219  * Tests if the file <chmfile>.<ext> exists, used for loading Indices, Table of Contents, etc.
220  * when these files are not available from the HH_WINTYPE structure.
221  */
222 static WCHAR *FindHTMLHelpSetting(HHInfo *info, const WCHAR *extW)
223 {
224     static const WCHAR periodW[] = {'.',0};
225     IStorage *pStorage = info->pCHMInfo->pStorage;
226     IStream *pStream;
227     WCHAR *filename;
228     HRESULT hr;
229
230     filename = heap_alloc( (strlenW(info->pCHMInfo->compiledFile)
231                             + strlenW(periodW) + strlenW(extW) + 1) * sizeof(WCHAR) );
232     strcpyW(filename, info->pCHMInfo->compiledFile);
233     strcatW(filename, periodW);
234     strcatW(filename, extW);
235     hr = IStorage_OpenStream(pStorage, filename, NULL, STGM_READ, 0, &pStream);
236     if (FAILED(hr))
237     {
238         heap_free(filename);
239         return strdupAtoW("");
240     }
241     IStream_Release(pStream);
242     return filename;
243 }
244
245 /* Loads the HH_WINTYPE data from the CHM file
246  *
247  * FIXME: There may be more than one window type in the file, so
248  *        add the ability to choose a certain window type
249  */
250 BOOL LoadWinTypeFromCHM(HHInfo *info)
251 {
252     LARGE_INTEGER liOffset;
253     IStorage *pStorage = info->pCHMInfo->pStorage;
254     IStream *pStream;
255     HRESULT hr;
256     DWORD cbRead;
257
258     static const WCHAR null[] = {0};
259     static const WCHAR toc_extW[] = {'h','h','c',0};
260     static const WCHAR index_extW[] = {'h','h','k',0};
261     static const WCHAR windowsW[] = {'#','W','I','N','D','O','W','S',0};
262
263     hr = IStorage_OpenStream(pStorage, windowsW, NULL, STGM_READ, 0, &pStream);
264     if (FAILED(hr))
265     {
266         /* no defined window types so use (hopefully) sane defaults */
267         static const WCHAR defaultwinW[] = {'d','e','f','a','u','l','t','w','i','n','\0'};
268         memset((void*)&(info->WinType), 0, sizeof(info->WinType));
269         info->WinType.cbStruct=sizeof(info->WinType);
270         info->WinType.fUniCodeStrings=TRUE;
271         info->WinType.pszType=strdupW(defaultwinW);
272         info->WinType.pszToc = strdupW(info->pCHMInfo->defToc ? info->pCHMInfo->defToc : null);
273         info->WinType.pszIndex = strdupW(null);
274         info->WinType.fsValidMembers=0;
275         info->WinType.fsWinProperties=HHWIN_PROP_TRI_PANE;
276         info->WinType.pszCaption=strdupW(info->pCHMInfo->defTitle ? info->pCHMInfo->defTitle : null);
277         info->WinType.dwStyles=WS_POPUP;
278         info->WinType.dwExStyles=0;
279         info->WinType.nShowState=SW_SHOW;
280         info->WinType.pszFile=strdupW(info->pCHMInfo->defTopic ? info->pCHMInfo->defTopic : null);
281         info->WinType.curNavType=HHWIN_NAVTYPE_TOC;
282         return TRUE;
283     }
284
285     /* jump past the #WINDOWS header */
286     liOffset.QuadPart = sizeof(DWORD) * 2;
287
288     hr = IStream_Seek(pStream, liOffset, STREAM_SEEK_SET, NULL);
289     if (FAILED(hr)) goto done;
290
291     /* read the HH_WINTYPE struct data */
292     hr = IStream_Read(pStream, &info->WinType, sizeof(info->WinType), &cbRead);
293     if (FAILED(hr)) goto done;
294
295     /* convert the #STRINGS offsets to actual strings */
296
297     info->WinType.pszType      = info->pszType     = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszType));
298     info->WinType.pszCaption   = info->pszCaption  = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszCaption));
299     info->WinType.pszHome      = info->pszHome     = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszHome));
300     info->WinType.pszJump1     = info->pszJump1    = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszJump1));
301     info->WinType.pszJump2     = info->pszJump2    = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszJump2));
302     info->WinType.pszUrlJump1  = info->pszUrlJump1 = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszUrlJump1));
303     info->WinType.pszUrlJump2  = info->pszUrlJump2 = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszUrlJump2));
304
305     if (info->WinType.pszFile)
306         info->WinType.pszFile  = info->pszFile     = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszFile));
307     else
308         info->WinType.pszFile  = strdupW(info->pCHMInfo->defTopic ? info->pCHMInfo->defTopic : null);
309     if (info->WinType.pszToc)
310         info->WinType.pszToc   = info->pszToc      = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszToc));
311     else
312         info->WinType.pszToc   = info->pszToc      = FindHTMLHelpSetting(info, toc_extW);
313     if (info->WinType.pszIndex)
314         info->WinType.pszIndex = info->pszIndex    = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszIndex));
315     else
316         info->WinType.pszIndex = info->pszIndex    = FindHTMLHelpSetting(info, index_extW);
317
318     /* FIXME: pszCustomTabs is a list of multiple zero-terminated strings so ReadString won't
319      * work in this case
320      */
321 #if 0
322     info->WinType.pszCustomTabs = info->pszCustomTabs = CHM_ReadString(pChmInfo, (DWORD_PTR)info->WinType.pszCustomTabs);
323 #endif
324
325 done:
326     IStream_Release(pStream);
327
328     return SUCCEEDED(hr);
329 }
330
331 LPCWSTR skip_schema(LPCWSTR url)
332 {
333     static const WCHAR its_schema[] = {'i','t','s',':'};
334     static const WCHAR msits_schema[] = {'m','s','-','i','t','s',':'};
335     static const WCHAR mk_schema[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':'};
336
337     if(!strncmpiW(its_schema, url, sizeof(its_schema)/sizeof(WCHAR)))
338         return url+sizeof(its_schema)/sizeof(WCHAR);
339     if(!strncmpiW(msits_schema, url, sizeof(msits_schema)/sizeof(WCHAR)))
340         return url+sizeof(msits_schema)/sizeof(WCHAR);
341     if(!strncmpiW(mk_schema, url, sizeof(mk_schema)/sizeof(WCHAR)))
342         return url+sizeof(mk_schema)/sizeof(WCHAR);
343
344     return url;
345 }
346
347 void SetChmPath(ChmPath *file, LPCWSTR base_file, LPCWSTR path)
348 {
349     LPCWSTR ptr;
350     static const WCHAR separatorW[] = {':',':',0};
351
352     path = skip_schema(path);
353
354     ptr = strstrW(path, separatorW);
355     if(ptr) {
356         WCHAR chm_file[MAX_PATH];
357         WCHAR rel_path[MAX_PATH];
358         WCHAR base_path[MAX_PATH];
359         LPWSTR p;
360
361         strcpyW(base_path, base_file);
362         p = strrchrW(base_path, '\\');
363         if(p)
364             *p = 0;
365
366         memcpy(rel_path, path, (ptr-path)*sizeof(WCHAR));
367         rel_path[ptr-path] = 0;
368
369         PathCombineW(chm_file, base_path, rel_path);
370
371         file->chm_file = strdupW(chm_file);
372         ptr += 2;
373     }else {
374         file->chm_file = strdupW(base_file);
375         ptr = path;
376     }
377
378     file->chm_index = strdupW(ptr);
379
380     TRACE("ChmFile = {%s %s}\n", debugstr_w(file->chm_file), debugstr_w(file->chm_index));
381 }
382
383 IStream *GetChmStream(CHMInfo *info, LPCWSTR parent_chm, ChmPath *chm_file)
384 {
385     IStorage *storage;
386     IStream *stream = NULL;
387     HRESULT hres;
388
389     TRACE("%s (%s :: %s)\n", debugstr_w(parent_chm), debugstr_w(chm_file->chm_file),
390           debugstr_w(chm_file->chm_index));
391
392     if(parent_chm || chm_file->chm_file) {
393         hres = IITStorage_StgOpenStorage(info->pITStorage,
394                 chm_file->chm_file ? chm_file->chm_file : parent_chm, NULL,
395                 STGM_READ | STGM_SHARE_DENY_WRITE, NULL, 0, &storage);
396         if(FAILED(hres)) {
397             WARN("Could not open storage: %08x\n", hres);
398             return NULL;
399         }
400     }else {
401         storage = info->pStorage;
402         IStorage_AddRef(info->pStorage);
403     }
404
405     hres = IStorage_OpenStream(storage, chm_file->chm_index, NULL, STGM_READ, 0, &stream);
406     IStorage_Release(storage);
407     if(FAILED(hres))
408         WARN("Could not open stream: %08x\n", hres);
409
410     return stream;
411 }
412
413 /*
414  * Retrieve a CHM document and parse the data from the <title> element to get the document's title.
415  */
416 WCHAR *GetDocumentTitle(CHMInfo *info, LPCWSTR document)
417 {
418     strbuf_t node, node_name, content;
419     WCHAR *document_title = NULL;
420     IStream *str = NULL;
421     IStorage *storage;
422     stream_t stream;
423     HRESULT hres;
424
425     TRACE("%s\n", debugstr_w(document));
426
427     storage = info->pStorage;
428     if(!storage) {
429         WARN("Could not open storage to obtain the title for a document.\n");
430         return NULL;
431     }
432     IStorage_AddRef(storage);
433
434     hres = IStorage_OpenStream(storage, document, NULL, STGM_READ, 0, &str);
435     IStorage_Release(storage);
436     if(FAILED(hres))
437         WARN("Could not open stream: %08x\n", hres);
438
439     stream_init(&stream, str);
440     strbuf_init(&node);
441     strbuf_init(&content);
442     strbuf_init(&node_name);
443
444     while(next_node(&stream, &node)) {
445         get_node_name(&node, &node_name);
446
447         TRACE("%s\n", node.buf);
448
449         if(!strcasecmp(node_name.buf, "title")) {
450             if(next_content(&stream, &content) && content.len > 1)
451             {
452                 document_title = strdupnAtoW(&content.buf[1], content.len-1);
453                 FIXME("magic: %s\n", debugstr_w(document_title));
454                 break;
455             }
456         }
457
458         strbuf_zero(&node);
459     }
460
461     strbuf_free(&node);
462     strbuf_free(&content);
463     strbuf_free(&node_name);
464     IStream_Release(str);
465
466     return document_title;
467 }
468
469 /* Opens the CHM file for reading */
470 CHMInfo *OpenCHM(LPCWSTR szFile)
471 {
472     HRESULT hres;
473     CHMInfo *ret;
474
475     static const WCHAR wszSTRINGS[] = {'#','S','T','R','I','N','G','S',0};
476
477     if (!(ret = heap_alloc_zero(sizeof(CHMInfo))))
478         return NULL;
479     ret->codePage = CP_ACP;
480
481     if (!(ret->szFile = strdupW(szFile))) {
482         heap_free(ret);
483         return NULL;
484     }
485
486     hres = CoCreateInstance(&CLSID_ITStorage, NULL, CLSCTX_INPROC_SERVER,
487             &IID_IITStorage, (void **) &ret->pITStorage) ;
488     if(FAILED(hres)) {
489         WARN("Could not create ITStorage: %08x\n", hres);
490         return CloseCHM(ret);
491     }
492
493     hres = IITStorage_StgOpenStorage(ret->pITStorage, szFile, NULL,
494             STGM_READ | STGM_SHARE_DENY_WRITE, NULL, 0, &ret->pStorage);
495     if(FAILED(hres)) {
496         WARN("Could not open storage: %08x\n", hres);
497         return CloseCHM(ret);
498     }
499     hres = IStorage_OpenStream(ret->pStorage, wszSTRINGS, NULL, STGM_READ, 0,
500             &ret->strings_stream);
501     if(FAILED(hres)) {
502         WARN("Could not open #STRINGS stream: %08x\n", hres);
503         /* It's not critical, so we pass */
504     }
505
506     if(!ReadChmSystem(ret)) {
507         WARN("Could not read #SYSTEM\n");
508         return CloseCHM(ret);
509     }
510
511     return ret;
512 }
513
514 CHMInfo *CloseCHM(CHMInfo *chm)
515 {
516     if(chm->pITStorage)
517         IITStorage_Release(chm->pITStorage);
518
519     if(chm->pStorage)
520         IStorage_Release(chm->pStorage);
521
522     if(chm->strings_stream)
523         IStream_Release(chm->strings_stream);
524
525     if(chm->strings_size) {
526         DWORD i;
527
528         for(i=0; i<chm->strings_size; i++)
529             heap_free(chm->strings[i]);
530     }
531
532     heap_free(chm->strings);
533     heap_free(chm->defTitle);
534     heap_free(chm->defTopic);
535     heap_free(chm->defToc);
536     heap_free(chm->szFile);
537     heap_free(chm->compiledFile);
538     heap_free(chm);
539
540     return NULL;
541 }