windowscodecs: Silence fixme for IID_CMetaBitmapRenderTarget.
[wine.git] / dlls / hhctrl.ocx / chm.c
blob4be86bd165a8a4177c4ecfef78e2aefeb4f10712
1 /*
2 * CHM Utility API
4 * Copyright 2005 James Hawkins
5 * Copyright 2007 Jacek Caban
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.
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.
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
22 #include "hhctrl.h"
23 #include "stream.h"
25 #include "winreg.h"
26 #include "shlwapi.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
31 /* Reads a string from the #STRINGS section in the CHM file */
32 static LPCSTR GetChmString(CHMInfo *chm, DWORD offset)
34 LPCSTR str;
35 char **new_strings;
36 DWORD new_strings_size;
38 if(!chm->strings_stream)
39 return NULL;
41 if(chm->strings_size <= (offset >> BLOCK_BITS)) {
42 new_strings_size = (offset >> BLOCK_BITS) + 1;
43 new_strings = realloc(chm->strings, new_strings_size * sizeof(char*));
44 if(!new_strings)
45 return NULL;
46 memset(new_strings + chm->strings_size, 0,
47 (new_strings_size - chm->strings_size) * sizeof(char*));
48 chm->strings = new_strings;
49 chm->strings_size = new_strings_size;
52 if(!chm->strings[offset >> BLOCK_BITS]) {
53 LARGE_INTEGER pos;
54 DWORD read;
55 HRESULT hres;
57 pos.QuadPart = offset & ~BLOCK_MASK;
58 hres = IStream_Seek(chm->strings_stream, pos, STREAM_SEEK_SET, NULL);
59 if(FAILED(hres)) {
60 WARN("Seek failed: %08lx\n", hres);
61 return NULL;
64 chm->strings[offset >> BLOCK_BITS] = malloc(BLOCK_SIZE);
66 hres = IStream_Read(chm->strings_stream, chm->strings[offset >> BLOCK_BITS],
67 BLOCK_SIZE, &read);
68 if(FAILED(hres)) {
69 WARN("Read failed: %08lx\n", hres);
70 free(chm->strings[offset >> BLOCK_BITS]);
71 chm->strings[offset >> BLOCK_BITS] = NULL;
72 return NULL;
76 str = chm->strings[offset >> BLOCK_BITS] + (offset & BLOCK_MASK);
77 TRACE("offset %#lx => %s\n", offset, debugstr_a(str));
78 return str;
81 static BOOL ReadChmSystem(CHMInfo *chm)
83 IStream *stream;
84 DWORD ver=0xdeadbeef, read, buf_size;
85 char *buf, *new_buf;
86 HRESULT hres;
88 struct {
89 WORD code;
90 WORD len;
91 } entry;
93 static const WCHAR wszSYSTEM[] = {'#','S','Y','S','T','E','M',0};
95 hres = IStorage_OpenStream(chm->pStorage, wszSYSTEM, NULL, STGM_READ, 0, &stream);
96 if(FAILED(hres)) {
97 WARN("Could not open #SYSTEM stream: %08lx\n", hres);
98 return FALSE;
101 IStream_Read(stream, &ver, sizeof(ver), &read);
102 TRACE("version is %lx\n", ver);
104 buf_size = 8 * sizeof(DWORD);
105 buf = malloc(buf_size);
106 if(!buf) {
107 IStream_Release(stream);
108 return FALSE;
111 while(1) {
112 hres = IStream_Read(stream, &entry, sizeof(entry), &read);
113 if(hres != S_OK)
114 break;
116 if(entry.len > buf_size) {
117 new_buf = realloc(buf, entry.len);
118 if(!new_buf) {
119 hres = E_OUTOFMEMORY;
120 break;
122 buf = new_buf;
123 buf_size = entry.len;
126 hres = IStream_Read(stream, buf, entry.len, &read);
127 if(hres != S_OK)
128 break;
130 switch(entry.code) {
131 case 0x0:
132 TRACE("TOC is %s\n", debugstr_an(buf, entry.len));
133 free(chm->defToc);
134 chm->defToc = strdupnAtoW(buf, entry.len);
135 break;
136 case 0x2:
137 TRACE("Default topic is %s\n", debugstr_an(buf, entry.len));
138 free(chm->defTopic);
139 chm->defTopic = strdupnAtoW(buf, entry.len);
140 break;
141 case 0x3:
142 TRACE("Title is %s\n", debugstr_an(buf, entry.len));
143 free(chm->defTitle);
144 chm->defTitle = strdupnAtoW(buf, entry.len);
145 break;
146 case 0x4:
147 /* TODO: Currently only the Locale ID is loaded from this field */
148 TRACE("Locale is: %ld\n", *(LCID*)&buf[0]);
149 if(!GetLocaleInfoW(*(LCID*)&buf[0], LOCALE_IDEFAULTANSICODEPAGE|LOCALE_RETURN_NUMBER,
150 (WCHAR *)&chm->codePage, sizeof(chm->codePage)/sizeof(WCHAR)))
151 chm->codePage = CP_ACP;
152 break;
153 case 0x5:
154 TRACE("Window name is %s\n", debugstr_an(buf, entry.len));
155 chm->defWindow = strdupnAtoW(buf, entry.len);
156 break;
157 case 0x6:
158 TRACE("Compiled file is %s\n", debugstr_an(buf, entry.len));
159 free(chm->compiledFile);
160 chm->compiledFile = strdupnAtoW(buf, entry.len);
161 break;
162 case 0x9:
163 TRACE("Version is %s\n", debugstr_an(buf, entry.len));
164 break;
165 case 0xa:
166 TRACE("Time is %08lx\n", *(DWORD*)buf);
167 break;
168 case 0xc:
169 TRACE("Number of info types: %ld\n", *(DWORD*)buf);
170 break;
171 case 0xf:
172 TRACE("Check sum: %lx\n", *(DWORD*)buf);
173 break;
174 default:
175 TRACE("unhandled code %x, size %x\n", entry.code, entry.len);
179 free(buf);
180 IStream_Release(stream);
182 return SUCCEEDED(hres);
185 LPWSTR FindContextAlias(CHMInfo *chm, DWORD index)
187 IStream *ivb_stream;
188 DWORD size, read, i;
189 DWORD *buf;
190 LPCSTR ret = NULL;
191 HRESULT hres;
193 static const WCHAR wszIVB[] = {'#','I','V','B',0};
195 hres = IStorage_OpenStream(chm->pStorage, wszIVB, NULL, STGM_READ, 0, &ivb_stream);
196 if(FAILED(hres)) {
197 WARN("Could not open #IVB stream: %08lx\n", hres);
198 return NULL;
201 hres = IStream_Read(ivb_stream, &size, sizeof(size), &read);
202 if(FAILED(hres)) {
203 WARN("Read failed: %08lx\n", hres);
204 IStream_Release(ivb_stream);
205 return NULL;
208 buf = malloc(size);
209 hres = IStream_Read(ivb_stream, buf, size, &read);
210 IStream_Release(ivb_stream);
211 if(FAILED(hres)) {
212 WARN("Read failed: %08lx\n", hres);
213 free(buf);
214 return NULL;
217 size /= 2*sizeof(DWORD);
219 for(i=0; i<size; i++) {
220 if(buf[2*i] == index) {
221 ret = GetChmString(chm, buf[2*i+1]);
222 break;
226 free(buf);
228 TRACE("returning %s\n", debugstr_a(ret));
229 return strdupAtoW(ret);
233 * Tests if the file <chmfile>.<ext> exists, used for loading Indices, Table of Contents, etc.
234 * when these files are not available from the HH_WINTYPE structure.
236 static WCHAR *FindHTMLHelpSetting(HHInfo *info, const WCHAR *extW)
238 static const WCHAR periodW[] = {'.',0};
239 IStorage *pStorage = info->pCHMInfo->pStorage;
240 IStream *pStream;
241 WCHAR *filename;
242 HRESULT hr;
244 filename = malloc( (wcslen(info->pCHMInfo->compiledFile)
245 + wcslen(periodW) + wcslen(extW) + 1) * sizeof(WCHAR) );
246 lstrcpyW(filename, info->pCHMInfo->compiledFile);
247 lstrcatW(filename, periodW);
248 lstrcatW(filename, extW);
249 hr = IStorage_OpenStream(pStorage, filename, NULL, STGM_READ, 0, &pStream);
250 if (FAILED(hr))
252 free(filename);
253 return strdupAtoW("");
255 IStream_Release(pStream);
256 return filename;
259 static inline WCHAR *MergeChmString(LPCWSTR src, WCHAR **dst)
261 if(*dst == NULL)
262 *dst = wcsdup(src);
263 return *dst;
266 void MergeChmProperties(HH_WINTYPEW *src, HHInfo *info, BOOL override)
268 DWORD unhandled_params = src->fsValidMembers & ~(HHWIN_PARAM_PROPERTIES|HHWIN_PARAM_STYLES
269 |HHWIN_PARAM_EXSTYLES|HHWIN_PARAM_RECT|HHWIN_PARAM_NAV_WIDTH
270 |HHWIN_PARAM_SHOWSTATE|HHWIN_PARAM_INFOTYPES|HHWIN_PARAM_TB_FLAGS
271 |HHWIN_PARAM_EXPANSION|HHWIN_PARAM_TABPOS|HHWIN_PARAM_TABORDER
272 |HHWIN_PARAM_HISTORY_COUNT|HHWIN_PARAM_CUR_TAB);
273 HH_WINTYPEW *dst = &info->WinType;
274 DWORD merge = override ? src->fsValidMembers : src->fsValidMembers & ~dst->fsValidMembers;
276 if (unhandled_params)
277 FIXME("Unsupported fsValidMembers fields: 0x%lx\n", unhandled_params);
279 dst->fsValidMembers |= merge;
280 if (dst->cbStruct == 0)
282 /* If the structure has not been filled in yet then use all of the values */
283 dst->cbStruct = sizeof(HH_WINTYPEW);
284 merge = ~0;
286 if (merge & HHWIN_PARAM_PROPERTIES) dst->fsWinProperties = src->fsWinProperties;
287 if (merge & HHWIN_PARAM_STYLES) dst->dwStyles = src->dwStyles;
288 if (merge & HHWIN_PARAM_EXSTYLES) dst->dwExStyles = src->dwExStyles;
289 if (merge & HHWIN_PARAM_RECT) dst->rcWindowPos = src->rcWindowPos;
290 if (merge & HHWIN_PARAM_NAV_WIDTH) dst->iNavWidth = src->iNavWidth;
291 if (merge & HHWIN_PARAM_SHOWSTATE) dst->nShowState = src->nShowState;
292 if (merge & HHWIN_PARAM_INFOTYPES) dst->paInfoTypes = src->paInfoTypes;
293 if (merge & HHWIN_PARAM_TB_FLAGS) dst->fsToolBarFlags = src->fsToolBarFlags;
294 if (merge & HHWIN_PARAM_EXPANSION) dst->fNotExpanded = src->fNotExpanded;
295 if (merge & HHWIN_PARAM_TABPOS) dst->tabpos = src->tabpos;
296 if (merge & HHWIN_PARAM_TABORDER) memcpy(dst->tabOrder, src->tabOrder, sizeof(src->tabOrder));
297 if (merge & HHWIN_PARAM_HISTORY_COUNT) dst->cHistory = src->cHistory;
298 if (merge & HHWIN_PARAM_CUR_TAB) dst->curNavType = src->curNavType;
301 * Note: We assume that hwndHelp, hwndCaller, hwndToolBar, hwndNavigation, and hwndHTML cannot be
302 * modified by the user. rcHTML and rcMinSize are not currently supported, so don't bother to copy them.
305 dst->pszType = MergeChmString(src->pszType, &info->stringsW.pszType);
306 dst->pszFile = MergeChmString(src->pszFile, &info->stringsW.pszFile);
307 dst->pszToc = MergeChmString(src->pszToc, &info->stringsW.pszToc);
308 dst->pszIndex = MergeChmString(src->pszIndex, &info->stringsW.pszIndex);
309 dst->pszCaption = MergeChmString(src->pszCaption, &info->stringsW.pszCaption);
310 dst->pszHome = MergeChmString(src->pszHome, &info->stringsW.pszHome);
311 dst->pszJump1 = MergeChmString(src->pszJump1, &info->stringsW.pszJump1);
312 dst->pszJump2 = MergeChmString(src->pszJump2, &info->stringsW.pszJump2);
313 dst->pszUrlJump1 = MergeChmString(src->pszUrlJump1, &info->stringsW.pszUrlJump1);
314 dst->pszUrlJump2 = MergeChmString(src->pszUrlJump2, &info->stringsW.pszUrlJump2);
316 /* FIXME: pszCustomTabs is a list of multiple zero-terminated strings so ReadString won't
317 * work in this case
319 #if 0
320 dst->pszCustomTabs = MergeChmString(src->pszCustomTabs, &info->pszCustomTabs);
321 #endif
324 static inline WCHAR *ConvertChmString(HHInfo *info, DWORD id)
326 WCHAR *ret = NULL;
328 if(id)
329 ret = strdupAtoW(GetChmString(info->pCHMInfo, id));
330 return ret;
333 static inline void wintype_free(HH_WINTYPEW *wintype)
335 free((void *)wintype->pszType);
336 free((void *)wintype->pszCaption);
337 free(wintype->paInfoTypes);
338 free((void *)wintype->pszToc);
339 free((void *)wintype->pszIndex);
340 free((void *)wintype->pszFile);
341 free((void *)wintype->pszHome);
342 free((void *)wintype->pszJump1);
343 free((void *)wintype->pszJump2);
344 free((void *)wintype->pszUrlJump1);
345 free((void *)wintype->pszUrlJump2);
346 free((void *)wintype->pszCustomTabs);
349 /* Loads the HH_WINTYPE data from the CHM file
351 * FIXME: There may be more than one window type in the file, so
352 * add the ability to choose a certain window type
354 BOOL LoadWinTypeFromCHM(HHInfo *info)
356 LARGE_INTEGER liOffset;
357 IStorage *pStorage = info->pCHMInfo->pStorage;
358 IStream *pStream = NULL;
359 HH_WINTYPEW wintype;
360 HRESULT hr;
361 DWORD cbRead;
362 BOOL ret = FALSE;
364 static const WCHAR empty[] = {0};
365 static const WCHAR toc_extW[] = {'h','h','c',0};
366 static const WCHAR index_extW[] = {'h','h','k',0};
367 static const WCHAR windowsW[] = {'#','W','I','N','D','O','W','S',0};
369 /* HH_WINTYPE as stored on disk. It's identical to HH_WINTYPE except that the pointer fields
370 have been changed to DWORDs, so that the layout on 64-bit remains unchanged. */
371 struct file_wintype
373 int cbStruct;
374 BOOL fUniCodeStrings;
375 DWORD pszType;
376 DWORD fsValidMembers;
377 DWORD fsWinProperties;
378 DWORD pszCaption;
379 DWORD dwStyles;
380 DWORD dwExStyles;
381 RECT rcWindowPos;
382 int nShowState;
383 DWORD hwndHelp;
384 DWORD hwndCaller;
385 DWORD paInfoTypes;
386 DWORD hwndToolBar;
387 DWORD hwndNavigation;
388 DWORD hwndHTML;
389 int iNavWidth;
390 RECT rcHTML;
391 DWORD pszToc;
392 DWORD pszIndex;
393 DWORD pszFile;
394 DWORD pszHome;
395 DWORD fsToolBarFlags;
396 BOOL fNotExpanded;
397 int curNavType;
398 int tabpos;
399 int idNotify;
400 BYTE tabOrder[HH_MAX_TABS+1];
401 int cHistory;
402 DWORD pszJump1;
403 DWORD pszJump2;
404 DWORD pszUrlJump1;
405 DWORD pszUrlJump2;
406 RECT rcMinSize;
407 int cbInfoTypes;
408 DWORD pszCustomTabs;
409 } file_wintype;
411 memset(&wintype, 0, sizeof(wintype));
412 wintype.cbStruct = sizeof(wintype);
413 wintype.fUniCodeStrings = TRUE;
415 hr = IStorage_OpenStream(pStorage, windowsW, NULL, STGM_READ, 0, &pStream);
416 if (SUCCEEDED(hr))
418 /* jump past the #WINDOWS header */
419 liOffset.QuadPart = sizeof(DWORD) * 2;
421 hr = IStream_Seek(pStream, liOffset, STREAM_SEEK_SET, NULL);
422 if (FAILED(hr)) goto done;
424 /* read the HH_WINTYPE struct data */
425 hr = IStream_Read(pStream, &file_wintype, sizeof(file_wintype), &cbRead);
426 if (FAILED(hr)) goto done;
428 /* convert the #STRINGS offsets to actual strings */
429 wintype.pszType = ConvertChmString(info, file_wintype.pszType);
430 wintype.fsValidMembers = file_wintype.fsValidMembers;
431 wintype.fsWinProperties = file_wintype.fsWinProperties;
432 wintype.pszCaption = ConvertChmString(info, file_wintype.pszCaption);
433 wintype.dwStyles = file_wintype.dwStyles;
434 wintype.dwExStyles = file_wintype.dwExStyles;
435 wintype.rcWindowPos = file_wintype.rcWindowPos;
436 wintype.nShowState = file_wintype.nShowState;
437 wintype.iNavWidth = file_wintype.iNavWidth;
438 wintype.rcHTML = file_wintype.rcHTML;
439 wintype.pszToc = ConvertChmString(info, file_wintype.pszToc);
440 wintype.pszIndex = ConvertChmString(info, file_wintype.pszIndex);
441 wintype.pszFile = ConvertChmString(info, file_wintype.pszFile);
442 wintype.pszHome = ConvertChmString(info, file_wintype.pszHome);
443 wintype.fsToolBarFlags = file_wintype.fsToolBarFlags;
444 wintype.fNotExpanded = file_wintype.fNotExpanded;
445 wintype.curNavType = file_wintype.curNavType;
446 wintype.tabpos = file_wintype.tabpos;
447 wintype.idNotify = file_wintype.idNotify;
448 memcpy(&wintype.tabOrder, file_wintype.tabOrder, sizeof(wintype.tabOrder));
449 wintype.cHistory = file_wintype.cHistory;
450 wintype.pszJump1 = ConvertChmString(info, file_wintype.pszJump1);
451 wintype.pszJump2 = ConvertChmString(info, file_wintype.pszJump2);
452 wintype.pszUrlJump1 = ConvertChmString(info, file_wintype.pszUrlJump1);
453 wintype.pszUrlJump2 = ConvertChmString(info, file_wintype.pszUrlJump2);
454 wintype.rcMinSize = file_wintype.rcMinSize;
455 wintype.cbInfoTypes = file_wintype.cbInfoTypes;
457 else
459 /* no defined window types so use (hopefully) sane defaults */
460 static const WCHAR defaultwinW[] = {'d','e','f','a','u','l','t','w','i','n','\0'};
461 wintype.pszType = wcsdup(info->pCHMInfo->defWindow ? info->pCHMInfo->defWindow : defaultwinW);
462 wintype.pszToc = wcsdup(info->pCHMInfo->defToc ? info->pCHMInfo->defToc : empty);
463 wintype.pszIndex = wcsdup(empty);
464 wintype.fsValidMembers = 0;
465 wintype.fsWinProperties = HHWIN_PROP_TRI_PANE;
466 wintype.dwStyles = WS_POPUP;
467 wintype.dwExStyles = 0;
468 wintype.nShowState = SW_SHOW;
469 wintype.curNavType = HHWIN_NAVTYPE_TOC;
472 /* merge the new data with any pre-existing HH_WINTYPE structure */
473 MergeChmProperties(&wintype, info, FALSE);
474 if (!info->WinType.pszCaption)
475 info->WinType.pszCaption = info->stringsW.pszCaption = wcsdup(info->pCHMInfo->defTitle ? info->pCHMInfo->defTitle : empty);
476 if (!info->WinType.pszFile)
477 info->WinType.pszFile = info->stringsW.pszFile = wcsdup(info->pCHMInfo->defTopic ? info->pCHMInfo->defTopic : empty);
478 if (!info->WinType.pszToc)
479 info->WinType.pszToc = info->stringsW.pszToc = FindHTMLHelpSetting(info, toc_extW);
480 if (!info->WinType.pszIndex)
481 info->WinType.pszIndex = info->stringsW.pszIndex = FindHTMLHelpSetting(info, index_extW);
483 wintype_free(&wintype);
484 ret = TRUE;
486 done:
487 if (pStream)
488 IStream_Release(pStream);
490 return ret;
493 LPCWSTR skip_schema(LPCWSTR url)
495 static const WCHAR its_schema[] = {'i','t','s',':'};
496 static const WCHAR msits_schema[] = {'m','s','-','i','t','s',':'};
497 static const WCHAR mk_schema[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':'};
499 if(!wcsnicmp(its_schema, url, ARRAY_SIZE(its_schema)))
500 return url + ARRAY_SIZE(its_schema);
501 if(!wcsnicmp(msits_schema, url, ARRAY_SIZE(msits_schema)))
502 return url + ARRAY_SIZE(msits_schema);
503 if(!wcsnicmp(mk_schema, url, ARRAY_SIZE(mk_schema)))
504 return url + ARRAY_SIZE(mk_schema);
506 return url;
509 void SetChmPath(ChmPath *file, LPCWSTR base_file, LPCWSTR path)
511 LPCWSTR ptr;
512 static const WCHAR separatorW[] = {':',':',0};
514 path = skip_schema(path);
516 ptr = wcsstr(path, separatorW);
517 if(ptr) {
518 WCHAR chm_file[MAX_PATH];
519 WCHAR rel_path[MAX_PATH];
520 WCHAR base_path[MAX_PATH];
521 LPWSTR p;
523 lstrcpyW(base_path, base_file);
524 p = wcsrchr(base_path, '\\');
525 if(p)
526 *p = 0;
528 memcpy(rel_path, path, (ptr-path)*sizeof(WCHAR));
529 rel_path[ptr-path] = 0;
531 PathCombineW(chm_file, base_path, rel_path);
533 file->chm_file = wcsdup(chm_file);
534 ptr += 2;
535 }else {
536 file->chm_file = wcsdup(base_file);
537 ptr = path;
540 file->chm_index = wcsdup(ptr);
542 TRACE("ChmFile = {%s %s}\n", debugstr_w(file->chm_file), debugstr_w(file->chm_index));
545 IStream *GetChmStream(CHMInfo *info, LPCWSTR parent_chm, ChmPath *chm_file)
547 IStorage *storage;
548 IStream *stream = NULL;
549 HRESULT hres;
551 TRACE("%s (%s :: %s)\n", debugstr_w(parent_chm), debugstr_w(chm_file->chm_file),
552 debugstr_w(chm_file->chm_index));
554 if(parent_chm || chm_file->chm_file) {
555 hres = IITStorage_StgOpenStorage(info->pITStorage,
556 chm_file->chm_file ? chm_file->chm_file : parent_chm, NULL,
557 STGM_READ | STGM_SHARE_DENY_WRITE, NULL, 0, &storage);
558 if(FAILED(hres)) {
559 WARN("Could not open storage: %08lx\n", hres);
560 return NULL;
562 }else {
563 storage = info->pStorage;
564 IStorage_AddRef(info->pStorage);
567 hres = IStorage_OpenStream(storage, chm_file->chm_index, NULL, STGM_READ, 0, &stream);
568 IStorage_Release(storage);
569 if(FAILED(hres))
570 WARN("Could not open stream: %08lx\n", hres);
572 return stream;
576 * Retrieve a CHM document and parse the data from the <title> element to get the document's title.
578 WCHAR *GetDocumentTitle(CHMInfo *info, LPCWSTR document)
580 strbuf_t node, node_name, content;
581 WCHAR *document_title = NULL;
582 IStream *str = NULL;
583 IStorage *storage;
584 stream_t stream;
585 HRESULT hres;
587 TRACE("%s\n", debugstr_w(document));
589 storage = info->pStorage;
590 if(!storage) {
591 WARN("Could not open storage to obtain the title for a document.\n");
592 return NULL;
594 IStorage_AddRef(storage);
596 hres = IStorage_OpenStream(storage, document, NULL, STGM_READ, 0, &str);
597 IStorage_Release(storage);
598 if(FAILED(hres))
599 WARN("Could not open stream: %08lx\n", hres);
601 stream_init(&stream, str);
602 strbuf_init(&node);
603 strbuf_init(&content);
604 strbuf_init(&node_name);
606 while(next_node(&stream, &node)) {
607 get_node_name(&node, &node_name);
609 TRACE("%s\n", node.buf);
611 if(!stricmp(node_name.buf, "title")) {
612 if(next_content(&stream, &content) && content.len > 1)
614 document_title = strdupnAtoW(&content.buf[1], content.len-1);
615 FIXME("magic: %s\n", debugstr_w(document_title));
616 break;
620 strbuf_zero(&node);
623 strbuf_free(&node);
624 strbuf_free(&content);
625 strbuf_free(&node_name);
626 IStream_Release(str);
628 return document_title;
631 /* Opens the CHM file for reading */
632 CHMInfo *OpenCHM(LPCWSTR szFile)
634 HRESULT hres;
635 CHMInfo *ret;
637 static const WCHAR wszSTRINGS[] = {'#','S','T','R','I','N','G','S',0};
639 if (!(ret = calloc(1, sizeof(CHMInfo))))
640 return NULL;
641 ret->codePage = CP_ACP;
643 if (!(ret->szFile = wcsdup(szFile))) {
644 free(ret);
645 return NULL;
648 hres = CoCreateInstance(&CLSID_ITStorage, NULL, CLSCTX_INPROC_SERVER,
649 &IID_IITStorage, (void **) &ret->pITStorage) ;
650 if(FAILED(hres)) {
651 WARN("Could not create ITStorage: %08lx\n", hres);
652 return CloseCHM(ret);
655 hres = IITStorage_StgOpenStorage(ret->pITStorage, szFile, NULL,
656 STGM_READ | STGM_SHARE_DENY_WRITE, NULL, 0, &ret->pStorage);
657 if(FAILED(hres)) {
658 WARN("Could not open storage: %08lx\n", hres);
659 return CloseCHM(ret);
661 hres = IStorage_OpenStream(ret->pStorage, wszSTRINGS, NULL, STGM_READ, 0,
662 &ret->strings_stream);
663 if(FAILED(hres)) {
664 WARN("Could not open #STRINGS stream: %08lx\n", hres);
665 /* It's not critical, so we pass */
668 if(!ReadChmSystem(ret)) {
669 WARN("Could not read #SYSTEM\n");
670 return CloseCHM(ret);
673 return ret;
676 CHMInfo *CloseCHM(CHMInfo *chm)
678 if(chm->pITStorage)
679 IITStorage_Release(chm->pITStorage);
681 if(chm->pStorage)
682 IStorage_Release(chm->pStorage);
684 if(chm->strings_stream)
685 IStream_Release(chm->strings_stream);
687 if(chm->strings_size) {
688 DWORD i;
690 for(i=0; i<chm->strings_size; i++)
691 free(chm->strings[i]);
694 free(chm->strings);
695 free(chm->defWindow);
696 free(chm->defTitle);
697 free(chm->defTopic);
698 free(chm->defToc);
699 free(chm->szFile);
700 free(chm->compiledFile);
701 free(chm);
703 return NULL;