10c76a0501fd92087cf1b66648bf493b7015c14e
[reactos.git] / reactos / dll / win32 / 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("Window name is %s\n", debugstr_an(buf, entry.len));
142 chm->defWindow = strdupnAtoW(buf, entry.len);
143 break;
144 case 0x6:
145 TRACE("Compiled file is %s\n", debugstr_an(buf, entry.len));
146 heap_free(chm->compiledFile);
147 chm->compiledFile = strdupnAtoW(buf, entry.len);
148 break;
149 case 0x9:
150 TRACE("Version is %s\n", debugstr_an(buf, entry.len));
151 break;
152 case 0xa:
153 TRACE("Time is %08x\n", *(DWORD*)buf);
154 break;
155 case 0xc:
156 TRACE("Number of info types: %d\n", *(DWORD*)buf);
157 break;
158 case 0xf:
159 TRACE("Check sum: %x\n", *(DWORD*)buf);
160 break;
161 default:
162 TRACE("unhandled code %x, size %x\n", entry.code, entry.len);
163 }
164 }
165
166 heap_free(buf);
167 IStream_Release(stream);
168
169 return SUCCEEDED(hres);
170 }
171
172 LPWSTR FindContextAlias(CHMInfo *chm, DWORD index)
173 {
174 IStream *ivb_stream;
175 DWORD size, read, i;
176 DWORD *buf;
177 LPCSTR ret = NULL;
178 HRESULT hres;
179
180 static const WCHAR wszIVB[] = {'#','I','V','B',0};
181
182 hres = IStorage_OpenStream(chm->pStorage, wszIVB, NULL, STGM_READ, 0, &ivb_stream);
183 if(FAILED(hres)) {
184 WARN("Could not open #IVB stream: %08x\n", hres);
185 return NULL;
186 }
187
188 hres = IStream_Read(ivb_stream, &size, sizeof(size), &read);
189 if(FAILED(hres)) {
190 WARN("Read failed: %08x\n", hres);
191 IStream_Release(ivb_stream);
192 return NULL;
193 }
194
195 buf = heap_alloc(size);
196 hres = IStream_Read(ivb_stream, buf, size, &read);
197 IStream_Release(ivb_stream);
198 if(FAILED(hres)) {
199 WARN("Read failed: %08x\n", hres);
200 heap_free(buf);
201 return NULL;
202 }
203
204 size /= 2*sizeof(DWORD);
205
206 for(i=0; i<size; i++) {
207 if(buf[2*i] == index) {
208 ret = GetChmString(chm, buf[2*i+1]);
209 break;
210 }
211 }
212
213 heap_free(buf);
214
215 TRACE("returning %s\n", debugstr_a(ret));
216 return strdupAtoW(ret);
217 }
218
219 /*
220 * Tests if the file <chmfile>.<ext> exists, used for loading Indices, Table of Contents, etc.
221 * when these files are not available from the HH_WINTYPE structure.
222 */
223 static WCHAR *FindHTMLHelpSetting(HHInfo *info, const WCHAR *extW)
224 {
225 static const WCHAR periodW[] = {'.',0};
226 IStorage *pStorage = info->pCHMInfo->pStorage;
227 IStream *pStream;
228 WCHAR *filename;
229 HRESULT hr;
230
231 filename = heap_alloc( (strlenW(info->pCHMInfo->compiledFile)
232 + strlenW(periodW) + strlenW(extW) + 1) * sizeof(WCHAR) );
233 strcpyW(filename, info->pCHMInfo->compiledFile);
234 strcatW(filename, periodW);
235 strcatW(filename, extW);
236 hr = IStorage_OpenStream(pStorage, filename, NULL, STGM_READ, 0, &pStream);
237 if (FAILED(hr))
238 {
239 heap_free(filename);
240 return strdupAtoW("");
241 }
242 IStream_Release(pStream);
243 return filename;
244 }
245
246 static inline WCHAR *MergeChmString(LPCWSTR src, WCHAR **dst)
247 {
248 if(*dst == NULL)
249 *dst = strdupW(src);
250 return *dst;
251 }
252
253 void MergeChmProperties(HH_WINTYPEW *src, HHInfo *info, BOOL override)
254 {
255 DWORD unhandled_params = src->fsValidMembers & ~(HHWIN_PARAM_PROPERTIES|HHWIN_PARAM_STYLES
256 |HHWIN_PARAM_EXSTYLES|HHWIN_PARAM_RECT|HHWIN_PARAM_NAV_WIDTH
257 |HHWIN_PARAM_SHOWSTATE|HHWIN_PARAM_INFOTYPES|HHWIN_PARAM_TB_FLAGS
258 |HHWIN_PARAM_EXPANSION|HHWIN_PARAM_TABPOS|HHWIN_PARAM_TABORDER
259 |HHWIN_PARAM_HISTORY_COUNT|HHWIN_PARAM_CUR_TAB);
260 HH_WINTYPEW *dst = &info->WinType;
261 DWORD merge = override ? src->fsValidMembers : src->fsValidMembers & ~dst->fsValidMembers;
262
263 if (unhandled_params)
264 FIXME("Unsupported fsValidMembers fields: 0x%x\n", unhandled_params);
265
266 dst->fsValidMembers |= merge;
267 if (dst->cbStruct == 0)
268 {
269 /* If the structure has not been filled in yet then use all of the values */
270 dst->cbStruct = sizeof(HH_WINTYPEW);
271 merge = ~0;
272 }
273 if (merge & HHWIN_PARAM_PROPERTIES) dst->fsWinProperties = src->fsWinProperties;
274 if (merge & HHWIN_PARAM_STYLES) dst->dwStyles = src->dwStyles;
275 if (merge & HHWIN_PARAM_EXSTYLES) dst->dwExStyles = src->dwExStyles;
276 if (merge & HHWIN_PARAM_RECT) dst->rcWindowPos = src->rcWindowPos;
277 if (merge & HHWIN_PARAM_NAV_WIDTH) dst->iNavWidth = src->iNavWidth;
278 if (merge & HHWIN_PARAM_SHOWSTATE) dst->nShowState = src->nShowState;
279 if (merge & HHWIN_PARAM_INFOTYPES) dst->paInfoTypes = src->paInfoTypes;
280 if (merge & HHWIN_PARAM_TB_FLAGS) dst->fsToolBarFlags = src->fsToolBarFlags;
281 if (merge & HHWIN_PARAM_EXPANSION) dst->fNotExpanded = src->fNotExpanded;
282 if (merge & HHWIN_PARAM_TABPOS) dst->tabpos = src->tabpos;
283 if (merge & HHWIN_PARAM_TABORDER) memcpy(dst->tabOrder, src->tabOrder, sizeof(src->tabOrder));
284 if (merge & HHWIN_PARAM_HISTORY_COUNT) dst->cHistory = src->cHistory;
285 if (merge & HHWIN_PARAM_CUR_TAB) dst->curNavType = src->curNavType;
286
287 /*
288 * Note: We assume that hwndHelp, hwndCaller, hwndToolBar, hwndNavigation, and hwndHTML cannot be
289 * modified by the user. rcHTML and rcMinSize are not currently supported, so don't bother to copy them.
290 */
291
292 dst->pszType = MergeChmString(src->pszType, &info->stringsW.pszType);
293 dst->pszFile = MergeChmString(src->pszFile, &info->stringsW.pszFile);
294 dst->pszToc = MergeChmString(src->pszToc, &info->stringsW.pszToc);
295 dst->pszIndex = MergeChmString(src->pszIndex, &info->stringsW.pszIndex);
296 dst->pszCaption = MergeChmString(src->pszCaption, &info->stringsW.pszCaption);
297 dst->pszHome = MergeChmString(src->pszHome, &info->stringsW.pszHome);
298 dst->pszJump1 = MergeChmString(src->pszJump1, &info->stringsW.pszJump1);
299 dst->pszJump2 = MergeChmString(src->pszJump2, &info->stringsW.pszJump2);
300 dst->pszUrlJump1 = MergeChmString(src->pszUrlJump1, &info->stringsW.pszUrlJump1);
301 dst->pszUrlJump2 = MergeChmString(src->pszUrlJump2, &info->stringsW.pszUrlJump2);
302
303 /* FIXME: pszCustomTabs is a list of multiple zero-terminated strings so ReadString won't
304 * work in this case
305 */
306 #if 0
307 dst->pszCustomTabs = MergeChmString(src->pszCustomTabs, &info->pszCustomTabs);
308 #endif
309 }
310
311 static inline WCHAR *ConvertChmString(HHInfo *info, const WCHAR **str)
312 {
313 WCHAR *ret = NULL;
314
315 if(*str)
316 *str = ret = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)*str));
317 return ret;
318 }
319
320 /* Loads the HH_WINTYPE data from the CHM file
321 *
322 * FIXME: There may be more than one window type in the file, so
323 * add the ability to choose a certain window type
324 */
325 BOOL LoadWinTypeFromCHM(HHInfo *info)
326 {
327 LARGE_INTEGER liOffset;
328 WCHAR *pszType = NULL, *pszFile = NULL, *pszToc = NULL, *pszIndex = NULL, *pszCaption = NULL;
329 WCHAR *pszHome = NULL, *pszJump1 = NULL, *pszJump2 = NULL, *pszUrlJump1 = NULL, *pszUrlJump2 = NULL;
330 IStorage *pStorage = info->pCHMInfo->pStorage;
331 IStream *pStream = NULL;
332 HH_WINTYPEW wintype;
333 HRESULT hr;
334 DWORD cbRead;
335 BOOL ret = FALSE;
336
337 static const WCHAR null[] = {0};
338 static const WCHAR toc_extW[] = {'h','h','c',0};
339 static const WCHAR index_extW[] = {'h','h','k',0};
340 static const WCHAR windowsW[] = {'#','W','I','N','D','O','W','S',0};
341
342 hr = IStorage_OpenStream(pStorage, windowsW, NULL, STGM_READ, 0, &pStream);
343 if (SUCCEEDED(hr))
344 {
345 /* jump past the #WINDOWS header */
346 liOffset.QuadPart = sizeof(DWORD) * 2;
347
348 hr = IStream_Seek(pStream, liOffset, STREAM_SEEK_SET, NULL);
349 if (FAILED(hr)) goto done;
350
351 /* read the HH_WINTYPE struct data */
352 hr = IStream_Read(pStream, &wintype, sizeof(wintype), &cbRead);
353 if (FAILED(hr)) goto done;
354
355 /* convert the #STRINGS offsets to actual strings */
356 pszType = ConvertChmString(info, &wintype.pszType);
357 pszFile = ConvertChmString(info, &wintype.pszFile);
358 pszToc = ConvertChmString(info, &wintype.pszToc);
359 pszIndex = ConvertChmString(info, &wintype.pszIndex);
360 pszCaption = ConvertChmString(info, &wintype.pszCaption);
361 pszHome = ConvertChmString(info, &wintype.pszHome);
362 pszJump1 = ConvertChmString(info, &wintype.pszJump1);
363 pszJump2 = ConvertChmString(info, &wintype.pszJump2);
364 pszUrlJump1 = ConvertChmString(info, &wintype.pszUrlJump1);
365 pszUrlJump2 = ConvertChmString(info, &wintype.pszUrlJump2);
366 }
367 else
368 {
369 /* no defined window types so use (hopefully) sane defaults */
370 static const WCHAR defaultwinW[] = {'d','e','f','a','u','l','t','w','i','n','\0'};
371 memset(&wintype, 0, sizeof(wintype));
372 wintype.cbStruct = sizeof(wintype);
373 wintype.fUniCodeStrings = TRUE;
374 wintype.pszType = pszType = strdupW(info->pCHMInfo->defWindow ? info->pCHMInfo->defWindow : defaultwinW);
375 wintype.pszToc = pszToc = strdupW(info->pCHMInfo->defToc ? info->pCHMInfo->defToc : null);
376 wintype.pszIndex = pszIndex = strdupW(null);
377 wintype.fsValidMembers = 0;
378 wintype.fsWinProperties = HHWIN_PROP_TRI_PANE;
379 wintype.dwStyles = WS_POPUP;
380 wintype.dwExStyles = 0;
381 wintype.nShowState = SW_SHOW;
382 wintype.curNavType = HHWIN_NAVTYPE_TOC;
383 }
384
385 /* merge the new data with any pre-existing HH_WINTYPE structure */
386 MergeChmProperties(&wintype, info, FALSE);
387 if (!info->WinType.pszCaption)
388 info->WinType.pszCaption = info->stringsW.pszCaption = strdupW(info->pCHMInfo->defTitle ? info->pCHMInfo->defTitle : null);
389 if (!info->WinType.pszFile)
390 info->WinType.pszFile = info->stringsW.pszFile = strdupW(info->pCHMInfo->defTopic ? info->pCHMInfo->defTopic : null);
391 if (!info->WinType.pszToc)
392 info->WinType.pszToc = info->stringsW.pszToc = FindHTMLHelpSetting(info, toc_extW);
393 if (!info->WinType.pszIndex)
394 info->WinType.pszIndex = info->stringsW.pszIndex = FindHTMLHelpSetting(info, index_extW);
395
396 heap_free(pszType);
397 heap_free(pszFile);
398 heap_free(pszToc);
399 heap_free(pszIndex);
400 heap_free(pszCaption);
401 heap_free(pszHome);
402 heap_free(pszJump1);
403 heap_free(pszJump2);
404 heap_free(pszUrlJump1);
405 heap_free(pszUrlJump2);
406 ret = TRUE;
407
408 done:
409 if (pStream)
410 IStream_Release(pStream);
411
412 return ret;
413 }
414
415 LPCWSTR skip_schema(LPCWSTR url)
416 {
417 static const WCHAR its_schema[] = {'i','t','s',':'};
418 static const WCHAR msits_schema[] = {'m','s','-','i','t','s',':'};
419 static const WCHAR mk_schema[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':'};
420
421 if(!strncmpiW(its_schema, url, sizeof(its_schema)/sizeof(WCHAR)))
422 return url+sizeof(its_schema)/sizeof(WCHAR);
423 if(!strncmpiW(msits_schema, url, sizeof(msits_schema)/sizeof(WCHAR)))
424 return url+sizeof(msits_schema)/sizeof(WCHAR);
425 if(!strncmpiW(mk_schema, url, sizeof(mk_schema)/sizeof(WCHAR)))
426 return url+sizeof(mk_schema)/sizeof(WCHAR);
427
428 return url;
429 }
430
431 void SetChmPath(ChmPath *file, LPCWSTR base_file, LPCWSTR path)
432 {
433 LPCWSTR ptr;
434 static const WCHAR separatorW[] = {':',':',0};
435
436 path = skip_schema(path);
437
438 ptr = strstrW(path, separatorW);
439 if(ptr) {
440 WCHAR chm_file[MAX_PATH];
441 WCHAR rel_path[MAX_PATH];
442 WCHAR base_path[MAX_PATH];
443 LPWSTR p;
444
445 strcpyW(base_path, base_file);
446 p = strrchrW(base_path, '\\');
447 if(p)
448 *p = 0;
449
450 memcpy(rel_path, path, (ptr-path)*sizeof(WCHAR));
451 rel_path[ptr-path] = 0;
452
453 PathCombineW(chm_file, base_path, rel_path);
454
455 file->chm_file = strdupW(chm_file);
456 ptr += 2;
457 }else {
458 file->chm_file = strdupW(base_file);
459 ptr = path;
460 }
461
462 file->chm_index = strdupW(ptr);
463
464 TRACE("ChmFile = {%s %s}\n", debugstr_w(file->chm_file), debugstr_w(file->chm_index));
465 }
466
467 IStream *GetChmStream(CHMInfo *info, LPCWSTR parent_chm, ChmPath *chm_file)
468 {
469 IStorage *storage;
470 IStream *stream = NULL;
471 HRESULT hres;
472
473 TRACE("%s (%s :: %s)\n", debugstr_w(parent_chm), debugstr_w(chm_file->chm_file),
474 debugstr_w(chm_file->chm_index));
475
476 if(parent_chm || chm_file->chm_file) {
477 hres = IITStorage_StgOpenStorage(info->pITStorage,
478 chm_file->chm_file ? chm_file->chm_file : parent_chm, NULL,
479 STGM_READ | STGM_SHARE_DENY_WRITE, NULL, 0, &storage);
480 if(FAILED(hres)) {
481 WARN("Could not open storage: %08x\n", hres);
482 return NULL;
483 }
484 }else {
485 storage = info->pStorage;
486 IStorage_AddRef(info->pStorage);
487 }
488
489 hres = IStorage_OpenStream(storage, chm_file->chm_index, NULL, STGM_READ, 0, &stream);
490 IStorage_Release(storage);
491 if(FAILED(hres))
492 WARN("Could not open stream: %08x\n", hres);
493
494 return stream;
495 }
496
497 /*
498 * Retrieve a CHM document and parse the data from the <title> element to get the document's title.
499 */
500 WCHAR *GetDocumentTitle(CHMInfo *info, LPCWSTR document)
501 {
502 strbuf_t node, node_name, content;
503 WCHAR *document_title = NULL;
504 IStream *str = NULL;
505 IStorage *storage;
506 stream_t stream;
507 HRESULT hres;
508
509 TRACE("%s\n", debugstr_w(document));
510
511 storage = info->pStorage;
512 if(!storage) {
513 WARN("Could not open storage to obtain the title for a document.\n");
514 return NULL;
515 }
516 IStorage_AddRef(storage);
517
518 hres = IStorage_OpenStream(storage, document, NULL, STGM_READ, 0, &str);
519 IStorage_Release(storage);
520 if(FAILED(hres))
521 WARN("Could not open stream: %08x\n", hres);
522
523 stream_init(&stream, str);
524 strbuf_init(&node);
525 strbuf_init(&content);
526 strbuf_init(&node_name);
527
528 while(next_node(&stream, &node)) {
529 get_node_name(&node, &node_name);
530
531 TRACE("%s\n", node.buf);
532
533 if(!strcasecmp(node_name.buf, "title")) {
534 if(next_content(&stream, &content) && content.len > 1)
535 {
536 document_title = strdupnAtoW(&content.buf[1], content.len-1);
537 FIXME("magic: %s\n", debugstr_w(document_title));
538 break;
539 }
540 }
541
542 strbuf_zero(&node);
543 }
544
545 strbuf_free(&node);
546 strbuf_free(&content);
547 strbuf_free(&node_name);
548 IStream_Release(str);
549
550 return document_title;
551 }
552
553 /* Opens the CHM file for reading */
554 CHMInfo *OpenCHM(LPCWSTR szFile)
555 {
556 HRESULT hres;
557 CHMInfo *ret;
558
559 static const WCHAR wszSTRINGS[] = {'#','S','T','R','I','N','G','S',0};
560
561 if (!(ret = heap_alloc_zero(sizeof(CHMInfo))))
562 return NULL;
563 ret->codePage = CP_ACP;
564
565 if (!(ret->szFile = strdupW(szFile))) {
566 heap_free(ret);
567 return NULL;
568 }
569
570 hres = CoCreateInstance(&CLSID_ITStorage, NULL, CLSCTX_INPROC_SERVER,
571 &IID_IITStorage, (void **) &ret->pITStorage) ;
572 if(FAILED(hres)) {
573 WARN("Could not create ITStorage: %08x\n", hres);
574 return CloseCHM(ret);
575 }
576
577 hres = IITStorage_StgOpenStorage(ret->pITStorage, szFile, NULL,
578 STGM_READ | STGM_SHARE_DENY_WRITE, NULL, 0, &ret->pStorage);
579 if(FAILED(hres)) {
580 WARN("Could not open storage: %08x\n", hres);
581 return CloseCHM(ret);
582 }
583 hres = IStorage_OpenStream(ret->pStorage, wszSTRINGS, NULL, STGM_READ, 0,
584 &ret->strings_stream);
585 if(FAILED(hres)) {
586 WARN("Could not open #STRINGS stream: %08x\n", hres);
587 /* It's not critical, so we pass */
588 }
589
590 if(!ReadChmSystem(ret)) {
591 WARN("Could not read #SYSTEM\n");
592 return CloseCHM(ret);
593 }
594
595 return ret;
596 }
597
598 CHMInfo *CloseCHM(CHMInfo *chm)
599 {
600 if(chm->pITStorage)
601 IITStorage_Release(chm->pITStorage);
602
603 if(chm->pStorage)
604 IStorage_Release(chm->pStorage);
605
606 if(chm->strings_stream)
607 IStream_Release(chm->strings_stream);
608
609 if(chm->strings_size) {
610 DWORD i;
611
612 for(i=0; i<chm->strings_size; i++)
613 heap_free(chm->strings[i]);
614 }
615
616 heap_free(chm->strings);
617 heap_free(chm->defWindow);
618 heap_free(chm->defTitle);
619 heap_free(chm->defTopic);
620 heap_free(chm->defToc);
621 heap_free(chm->szFile);
622 heap_free(chm->compiledFile);
623 heap_free(chm);
624
625 return NULL;
626 }