hhctrl.ocx: Remove a superfluous line that uses an uninitialized variable.
[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
24 #include "winreg.h"
25 #include "shlwapi.h"
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
29
30 #define BLOCK_BITS 12
31 #define BLOCK_SIZE (1 << BLOCK_BITS)
32 #define BLOCK_MASK (BLOCK_SIZE-1)
33
34 /* Reads a string from the #STRINGS section in the CHM file */
35 static LPCSTR GetChmString(CHMInfo *chm, DWORD offset)
36 {
37     if(!chm->strings_stream)
38         return NULL;
39
40     if(chm->strings_size <= (offset >> BLOCK_BITS)) {
41         chm->strings_size = (offset >> BLOCK_BITS)+1;
42         if(chm->strings)
43             chm->strings = heap_realloc_zero(chm->strings,
44                     chm->strings_size*sizeof(char*));
45         else
46             chm->strings = heap_alloc_zero(
47                     chm->strings_size*sizeof(char*));
48
49     }
50
51     if(!chm->strings[offset >> BLOCK_BITS]) {
52         LARGE_INTEGER pos;
53         DWORD read;
54         HRESULT hres;
55
56         pos.QuadPart = offset & ~BLOCK_MASK;
57         hres = IStream_Seek(chm->strings_stream, pos, STREAM_SEEK_SET, NULL);
58         if(FAILED(hres)) {
59             WARN("Seek failed: %08x\n", hres);
60             return NULL;
61         }
62
63         chm->strings[offset >> BLOCK_BITS] = heap_alloc(BLOCK_SIZE);
64
65         hres = IStream_Read(chm->strings_stream, chm->strings[offset >> BLOCK_BITS],
66                             BLOCK_SIZE, &read);
67         if(FAILED(hres)) {
68             WARN("Read failed: %08x\n", hres);
69             heap_free(chm->strings[offset >> BLOCK_BITS]);
70             chm->strings[offset >> BLOCK_BITS] = NULL;
71             return NULL;
72         }
73     }
74
75     return chm->strings[offset >> BLOCK_BITS] + (offset & BLOCK_MASK);
76 }
77
78 static BOOL ReadChmSystem(CHMInfo *chm)
79 {
80     IStream *stream;
81     DWORD ver=0xdeadbeef, read, buf_size;
82     char *buf;
83     HRESULT hres;
84
85     struct {
86         WORD code;
87         WORD len;
88     } entry;
89
90     static const WCHAR wszSYSTEM[] = {'#','S','Y','S','T','E','M',0};
91
92     hres = IStorage_OpenStream(chm->pStorage, wszSYSTEM, NULL, STGM_READ, 0, &stream);
93     if(FAILED(hres)) {
94         WARN("Could not open #SYSTEM stream: %08x\n", hres);
95         return FALSE;
96     }
97
98     IStream_Read(stream, &ver, sizeof(ver), &read);
99     TRACE("version is %x\n", ver);
100
101     buf = heap_alloc(8*sizeof(DWORD));
102     buf_size = 8*sizeof(DWORD);
103
104     while(1) {
105         hres = IStream_Read(stream, &entry, sizeof(entry), &read);
106         if(hres != S_OK)
107             break;
108
109         if(entry.len > buf_size)
110             buf = heap_realloc(buf, buf_size=entry.len);
111
112         hres = IStream_Read(stream, buf, entry.len, &read);
113         if(hres != S_OK)
114             break;
115
116         switch(entry.code) {
117         case 0x0:
118             TRACE("TOC is %s\n", debugstr_an(buf, entry.len));
119             heap_free(chm->defToc);
120             chm->defToc = strdupnAtoW(buf, entry.len);
121             break;
122         case 0x2:
123             TRACE("Default topic is %s\n", debugstr_an(buf, entry.len));
124             heap_free(chm->defTopic);
125             chm->defTopic = strdupnAtoW(buf, entry.len);
126             break;
127         case 0x3:
128             TRACE("Title is %s\n", debugstr_an(buf, entry.len));
129             heap_free(chm->defTitle);
130             chm->defTitle = strdupnAtoW(buf, entry.len);
131             break;
132         case 0x5:
133             TRACE("Default window is %s\n", debugstr_an(buf, entry.len));
134             break;
135         case 0x6:
136             TRACE("Compiled file is %s\n", debugstr_an(buf, entry.len));
137             break;
138         case 0x9:
139             TRACE("Version is %s\n", debugstr_an(buf, entry.len));
140             break;
141         case 0xa:
142             TRACE("Time is %08x\n", *(DWORD*)buf);
143             break;
144         case 0xc:
145             TRACE("Number of info types: %d\n", *(DWORD*)buf);
146             break;
147         case 0xf:
148             TRACE("Check sum: %x\n", *(DWORD*)buf);
149             break;
150         default:
151             TRACE("unhandled code %x, size %x\n", entry.code, entry.len);
152         }
153     }
154
155     heap_free(buf);
156     IStream_Release(stream);
157
158     return SUCCEEDED(hres);
159 }
160
161 LPWSTR FindContextAlias(CHMInfo *chm, DWORD index)
162 {
163     IStream *ivb_stream;
164     DWORD size, read, i;
165     DWORD *buf;
166     LPCSTR ret = NULL;
167     HRESULT hres;
168
169     static const WCHAR wszIVB[] = {'#','I','V','B',0};
170
171     hres = IStorage_OpenStream(chm->pStorage, wszIVB, NULL, STGM_READ, 0, &ivb_stream);
172     if(FAILED(hres)) {
173         WARN("Could not open #IVB stream: %08x\n", hres);
174         return NULL;
175     }
176
177     hres = IStream_Read(ivb_stream, &size, sizeof(size), &read);
178     if(FAILED(hres)) {
179         WARN("Read failed: %08x\n", hres);
180         IStream_Release(ivb_stream);
181         return NULL;
182     }
183
184     buf = heap_alloc(size);
185     hres = IStream_Read(ivb_stream, buf, size, &read);
186     IStream_Release(ivb_stream);
187     if(FAILED(hres)) {
188         WARN("Read failed: %08x\n", hres);
189         heap_free(buf);
190         return NULL;
191     }
192
193     size /= 2*sizeof(DWORD);
194
195     for(i=0; i<size; i++) {
196         if(buf[2*i] == index) {
197             ret = GetChmString(chm, buf[2*i+1]);
198             break;
199         }
200     }
201
202     heap_free(buf);
203
204     TRACE("returning %s\n", debugstr_a(ret));
205     return strdupAtoW(ret);
206 }
207
208 /* Loads the HH_WINTYPE data from the CHM file
209  *
210  * FIXME: There may be more than one window type in the file, so
211  *        add the ability to choose a certain window type
212  */
213 BOOL LoadWinTypeFromCHM(HHInfo *info)
214 {
215     LARGE_INTEGER liOffset;
216     IStorage *pStorage = info->pCHMInfo->pStorage;
217     IStream *pStream;
218     HRESULT hr;
219     DWORD cbRead;
220
221     static const WCHAR windowsW[] = {'#','W','I','N','D','O','W','S',0};
222
223     hr = IStorage_OpenStream(pStorage, windowsW, NULL, STGM_READ, 0, &pStream);
224     if (FAILED(hr))
225     {
226         /* no defined window types so use (hopefully) sane defaults */
227         static const WCHAR defaultwinW[] = {'d','e','f','a','u','l','t','w','i','n','\0'};
228         static const WCHAR null[] = {0};
229         memset((void*)&(info->WinType), 0, sizeof(info->WinType));
230         info->WinType.cbStruct=sizeof(info->WinType);
231         info->WinType.fUniCodeStrings=TRUE;
232         info->WinType.pszType=strdupW(defaultwinW);
233         info->WinType.pszToc = strdupW(info->pCHMInfo->defToc ? info->pCHMInfo->defToc : null);
234         info->WinType.pszIndex = strdupW(null);
235         info->WinType.fsValidMembers=0;
236         info->WinType.fsWinProperties=HHWIN_PROP_TRI_PANE;
237         info->WinType.pszCaption=strdupW(info->pCHMInfo->defTitle);
238         info->WinType.dwStyles=WS_POPUP;
239         info->WinType.dwExStyles=0;
240         info->WinType.nShowState=SW_SHOW;
241         info->WinType.pszFile=strdupW(info->pCHMInfo->defTopic);
242         info->WinType.curNavType=HHWIN_NAVTYPE_TOC;
243         return TRUE;
244     }
245
246     /* jump past the #WINDOWS header */
247     liOffset.QuadPart = sizeof(DWORD) * 2;
248
249     hr = IStream_Seek(pStream, liOffset, STREAM_SEEK_SET, NULL);
250     if (FAILED(hr)) goto done;
251
252     /* read the HH_WINTYPE struct data */
253     hr = IStream_Read(pStream, &info->WinType, sizeof(info->WinType), &cbRead);
254     if (FAILED(hr)) goto done;
255
256     /* convert the #STRINGS offsets to actual strings */
257     info->WinType.pszType     = info->pszType     = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszType));
258     info->WinType.pszCaption  = info->pszCaption  = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszCaption));
259     info->WinType.pszToc      = info->pszToc      = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszToc));
260     info->WinType.pszIndex    = info->pszIndex    = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszIndex));
261     info->WinType.pszFile     = info->pszFile     = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszFile));
262     info->WinType.pszHome     = info->pszHome     = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszHome));
263     info->WinType.pszJump1    = info->pszJump1    = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszJump1));
264     info->WinType.pszJump2    = info->pszJump2    = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszJump2));
265     info->WinType.pszUrlJump1 = info->pszUrlJump1 = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszUrlJump1));
266     info->WinType.pszUrlJump2 = info->pszUrlJump2 = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)info->WinType.pszUrlJump2));
267
268     /* FIXME: pszCustomTabs is a list of multiple zero-terminated strings so ReadString won't
269      * work in this case
270      */
271 #if 0
272     info->WinType.pszCustomTabs = info->pszCustomTabs = CHM_ReadString(pChmInfo, (DWORD_PTR)info->WinType.pszCustomTabs);
273 #endif
274
275 done:
276     IStream_Release(pStream);
277
278     return SUCCEEDED(hr);
279 }
280
281 static LPCWSTR skip_schema(LPCWSTR url)
282 {
283     static const WCHAR its_schema[] = {'i','t','s',':'};
284     static const WCHAR msits_schema[] = {'m','s','-','i','t','s',':'};
285     static const WCHAR mk_schema[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':'};
286
287     if(!strncmpiW(its_schema, url, sizeof(its_schema)/sizeof(WCHAR)))
288         return url+sizeof(its_schema)/sizeof(WCHAR);
289     if(!strncmpiW(msits_schema, url, sizeof(msits_schema)/sizeof(WCHAR)))
290         return url+sizeof(msits_schema)/sizeof(WCHAR);
291     if(!strncmpiW(mk_schema, url, sizeof(mk_schema)/sizeof(WCHAR)))
292         return url+sizeof(mk_schema)/sizeof(WCHAR);
293
294     return url;
295 }
296
297 void SetChmPath(ChmPath *file, LPCWSTR base_file, LPCWSTR path)
298 {
299     LPCWSTR ptr;
300     static const WCHAR separatorW[] = {':',':',0};
301
302     path = skip_schema(path);
303
304     ptr = strstrW(path, separatorW);
305     if(ptr) {
306         WCHAR chm_file[MAX_PATH];
307         WCHAR rel_path[MAX_PATH];
308         WCHAR base_path[MAX_PATH];
309         LPWSTR p;
310
311         strcpyW(base_path, base_file);
312         p = strrchrW(base_path, '\\');
313         if(p)
314             *p = 0;
315
316         memcpy(rel_path, path, (ptr-path)*sizeof(WCHAR));
317         rel_path[ptr-path] = 0;
318
319         PathCombineW(chm_file, base_path, rel_path);
320
321         file->chm_file = strdupW(chm_file);
322         ptr += 2;
323     }else {
324         file->chm_file = strdupW(base_file);
325         ptr = path;
326     }
327
328     file->chm_index = strdupW(ptr);
329
330     TRACE("ChmFile = {%s %s}\n", debugstr_w(file->chm_file), debugstr_w(file->chm_index));
331 }
332
333 IStream *GetChmStream(CHMInfo *info, LPCWSTR parent_chm, ChmPath *chm_file)
334 {
335     IStorage *storage;
336     IStream *stream = NULL;
337     HRESULT hres;
338
339     TRACE("%s (%s :: %s)\n", debugstr_w(parent_chm), debugstr_w(chm_file->chm_file),
340           debugstr_w(chm_file->chm_index));
341
342     if(parent_chm || chm_file->chm_file) {
343         hres = IITStorage_StgOpenStorage(info->pITStorage,
344                 chm_file->chm_file ? chm_file->chm_file : parent_chm, NULL,
345                 STGM_READ | STGM_SHARE_DENY_WRITE, NULL, 0, &storage);
346         if(FAILED(hres)) {
347             WARN("Could not open storage: %08x\n", hres);
348             return NULL;
349         }
350     }else {
351         storage = info->pStorage;
352         IStorage_AddRef(info->pStorage);
353     }
354
355     hres = IStorage_OpenStream(storage, chm_file->chm_index, NULL, STGM_READ, 0, &stream);
356     IStorage_Release(storage);
357     if(FAILED(hres))
358         WARN("Could not open stream: %08x\n", hres);
359
360     return stream;
361 }
362
363 /* Opens the CHM file for reading */
364 CHMInfo *OpenCHM(LPCWSTR szFile)
365 {
366     HRESULT hres;
367     CHMInfo *ret;
368
369     static const WCHAR wszSTRINGS[] = {'#','S','T','R','I','N','G','S',0};
370
371     if (!(ret = heap_alloc_zero(sizeof(CHMInfo))))
372         return NULL;
373
374     if (!(ret->szFile = strdupW(szFile))) {
375         heap_free(ret);
376         return NULL;
377     }
378
379     hres = CoCreateInstance(&CLSID_ITStorage, NULL, CLSCTX_INPROC_SERVER,
380             &IID_IITStorage, (void **) &ret->pITStorage) ;
381     if(FAILED(hres)) {
382         WARN("Could not create ITStorage: %08x\n", hres);
383         return CloseCHM(ret);
384     }
385
386     hres = IITStorage_StgOpenStorage(ret->pITStorage, szFile, NULL,
387             STGM_READ | STGM_SHARE_DENY_WRITE, NULL, 0, &ret->pStorage);
388     if(FAILED(hres)) {
389         WARN("Could not open storage: %08x\n", hres);
390         return CloseCHM(ret);
391     }
392
393     hres = IStorage_OpenStream(ret->pStorage, wszSTRINGS, NULL, STGM_READ, 0,
394             &ret->strings_stream);
395     if(FAILED(hres)) {
396         WARN("Could not open #STRINGS stream: %08x\n", hres);
397         /* It's not critical, so we pass */
398     }
399
400     if(!ReadChmSystem(ret)) {
401         WARN("Could not read #SYSTEM\n");
402         return CloseCHM(ret);
403     }
404
405     return ret;
406 }
407
408 CHMInfo *CloseCHM(CHMInfo *chm)
409 {
410     if(chm->pITStorage)
411         IITStorage_Release(chm->pITStorage);
412
413     if(chm->pStorage)
414         IStorage_Release(chm->pStorage);
415
416     if(chm->strings_stream)
417         IStream_Release(chm->strings_stream);
418
419     if(chm->strings_size) {
420         DWORD i;
421
422         for(i=0; i<chm->strings_size; i++)
423             heap_free(chm->strings[i]);
424     }
425
426     heap_free(chm->strings);
427     heap_free(chm->defTitle);
428     heap_free(chm->defTopic);
429     heap_free(chm->defToc);
430     heap_free(chm->szFile);
431     heap_free(chm);
432
433     return NULL;
434 }