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
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)
36 if(!chm->strings_stream)
39 if(chm->strings_size <= (offset >> BLOCK_BITS)) {
40 chm->strings_size = (offset >> BLOCK_BITS)+1;
42 chm->strings = heap_realloc_zero(chm->strings,
43 chm->strings_size*sizeof(char*));
45 chm->strings = heap_alloc_zero(
46 chm->strings_size*sizeof(char*));
50 if(!chm->strings[offset >> BLOCK_BITS]) {
55 pos.QuadPart = offset & ~BLOCK_MASK;
56 hres = IStream_Seek(chm->strings_stream, pos, STREAM_SEEK_SET, NULL);
58 WARN("Seek failed: %08x\n", hres);
62 chm->strings[offset >> BLOCK_BITS] = heap_alloc(BLOCK_SIZE);
64 hres = IStream_Read(chm->strings_stream, chm->strings[offset >> BLOCK_BITS],
67 WARN("Read failed: %08x\n", hres);
68 heap_free(chm->strings[offset >> BLOCK_BITS]);
69 chm->strings[offset >> BLOCK_BITS] = NULL;
74 str = chm->strings[offset >> BLOCK_BITS] + (offset & BLOCK_MASK);
75 TRACE("offset %#x => %s\n", offset, debugstr_a(str));
79 static BOOL ReadChmSystem(CHMInfo *chm)
82 DWORD ver=0xdeadbeef, read, buf_size;
91 static const WCHAR wszSYSTEM[] = {'#','S','Y','S','T','E','M',0};
93 hres = IStorage_OpenStream(chm->pStorage, wszSYSTEM, NULL, STGM_READ, 0, &stream);
95 WARN("Could not open #SYSTEM stream: %08x\n", hres);
99 IStream_Read(stream, &ver, sizeof(ver), &read);
100 TRACE("version is %x\n", ver);
102 buf = heap_alloc(8*sizeof(DWORD));
103 buf_size = 8*sizeof(DWORD);
106 hres = IStream_Read(stream, &entry, sizeof(entry), &read);
110 if(entry.len > buf_size)
111 buf = heap_realloc(buf, buf_size=entry.len);
113 hres = IStream_Read(stream, buf, entry.len, &read);
119 TRACE("TOC is %s\n", debugstr_an(buf, entry.len));
120 heap_free(chm->defToc);
121 chm->defToc = strdupnAtoW(buf, entry.len);
124 TRACE("Default topic is %s\n", debugstr_an(buf, entry.len));
125 heap_free(chm->defTopic);
126 chm->defTopic = strdupnAtoW(buf, entry.len);
129 TRACE("Title is %s\n", debugstr_an(buf, entry.len));
130 heap_free(chm->defTitle);
131 chm->defTitle = strdupnAtoW(buf, entry.len);
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;
141 TRACE("Window name is %s\n", debugstr_an(buf, entry.len));
142 chm->defWindow = strdupnAtoW(buf, entry.len);
145 TRACE("Compiled file is %s\n", debugstr_an(buf, entry.len));
146 heap_free(chm->compiledFile);
147 chm->compiledFile = strdupnAtoW(buf, entry.len);
150 TRACE("Version is %s\n", debugstr_an(buf, entry.len));
153 TRACE("Time is %08x\n", *(DWORD*)buf);
156 TRACE("Number of info types: %d\n", *(DWORD*)buf);
159 TRACE("Check sum: %x\n", *(DWORD*)buf);
162 TRACE("unhandled code %x, size %x\n", entry.code, entry.len);
167 IStream_Release(stream);
169 return SUCCEEDED(hres);
172 LPWSTR FindContextAlias(CHMInfo *chm, DWORD index)
180 static const WCHAR wszIVB[] = {'#','I','V','B',0};
182 hres = IStorage_OpenStream(chm->pStorage, wszIVB, NULL, STGM_READ, 0, &ivb_stream);
184 WARN("Could not open #IVB stream: %08x\n", hres);
188 hres = IStream_Read(ivb_stream, &size, sizeof(size), &read);
190 WARN("Read failed: %08x\n", hres);
191 IStream_Release(ivb_stream);
195 buf = heap_alloc(size);
196 hres = IStream_Read(ivb_stream, buf, size, &read);
197 IStream_Release(ivb_stream);
199 WARN("Read failed: %08x\n", hres);
204 size /= 2*sizeof(DWORD);
206 for(i=0; i<size; i++) {
207 if(buf[2*i] == index) {
208 ret = GetChmString(chm, buf[2*i+1]);
215 TRACE("returning %s\n", debugstr_a(ret));
216 return strdupAtoW(ret);
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.
223 static WCHAR *FindHTMLHelpSetting(HHInfo *info, const WCHAR *extW)
225 static const WCHAR periodW[] = {'.',0};
226 IStorage *pStorage = info->pCHMInfo->pStorage;
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);
240 return strdupAtoW("");
242 IStream_Release(pStream);
246 static inline WCHAR *MergeChmString(LPCWSTR src, WCHAR **dst)
253 void MergeChmProperties(HH_WINTYPEW *src, HHInfo *info, BOOL override)
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;
263 if (unhandled_params)
264 FIXME("Unsupported fsValidMembers fields: 0x%x\n", unhandled_params);
266 if (merge & HHWIN_PARAM_PROPERTIES) dst->fsWinProperties = src->fsWinProperties;
267 if (merge & HHWIN_PARAM_STYLES) dst->dwStyles = src->dwStyles;
268 if (merge & HHWIN_PARAM_EXSTYLES) dst->dwExStyles = src->dwExStyles;
269 if (merge & HHWIN_PARAM_RECT) dst->rcWindowPos = src->rcWindowPos;
270 if (merge & HHWIN_PARAM_NAV_WIDTH) dst->iNavWidth = src->iNavWidth;
271 if (merge & HHWIN_PARAM_SHOWSTATE) dst->nShowState = src->nShowState;
272 if (merge & HHWIN_PARAM_INFOTYPES) dst->paInfoTypes = src->paInfoTypes;
273 if (merge & HHWIN_PARAM_TB_FLAGS) dst->fsToolBarFlags = src->fsToolBarFlags;
274 if (merge & HHWIN_PARAM_EXPANSION) dst->fNotExpanded = src->fNotExpanded;
275 if (merge & HHWIN_PARAM_TABPOS) dst->tabpos = src->tabpos;
276 if (merge & HHWIN_PARAM_TABORDER) memcpy(&dst->tabOrder, &src->tabOrder, sizeof(src->tabOrder));
277 if (merge & HHWIN_PARAM_HISTORY_COUNT) dst->cHistory = src->cHistory;
278 if (merge & HHWIN_PARAM_CUR_TAB) dst->curNavType = src->curNavType;
279 dst->fsValidMembers |= merge;
282 * Note: We assume that hwndHelp, hwndCaller, hwndToolBar, hwndNavigation, and hwndHTML cannot be
283 * modified by the user. rcHTML and rcMinSize are not currently supported, so don't bother to copy them.
286 dst->pszType = MergeChmString(src->pszType, &info->stringsW.pszType);
287 dst->pszFile = MergeChmString(src->pszFile, &info->stringsW.pszFile);
288 dst->pszToc = MergeChmString(src->pszToc, &info->stringsW.pszToc);
289 dst->pszIndex = MergeChmString(src->pszIndex, &info->stringsW.pszIndex);
290 dst->pszCaption = MergeChmString(src->pszCaption, &info->stringsW.pszCaption);
291 dst->pszHome = MergeChmString(src->pszHome, &info->stringsW.pszHome);
292 dst->pszJump1 = MergeChmString(src->pszJump1, &info->stringsW.pszJump1);
293 dst->pszJump2 = MergeChmString(src->pszJump2, &info->stringsW.pszJump2);
294 dst->pszUrlJump1 = MergeChmString(src->pszUrlJump1, &info->stringsW.pszUrlJump1);
295 dst->pszUrlJump2 = MergeChmString(src->pszUrlJump2, &info->stringsW.pszUrlJump2);
297 /* FIXME: pszCustomTabs is a list of multiple zero-terminated strings so ReadString won't
301 dst->pszCustomTabs = MergeChmString(src->pszCustomTabs, &info->pszCustomTabs);
305 static inline WCHAR *ConvertChmString(HHInfo *info, const WCHAR **str)
310 *str = ret = strdupAtoW(GetChmString(info->pCHMInfo, (DWORD_PTR)*str));
314 /* Loads the HH_WINTYPE data from the CHM file
316 * FIXME: There may be more than one window type in the file, so
317 * add the ability to choose a certain window type
319 BOOL LoadWinTypeFromCHM(HHInfo *info)
321 LARGE_INTEGER liOffset;
322 WCHAR *pszType = NULL, *pszFile = NULL, *pszToc = NULL, *pszIndex = NULL, *pszCaption = NULL;
323 WCHAR *pszHome = NULL, *pszJump1 = NULL, *pszJump2 = NULL, *pszUrlJump1 = NULL, *pszUrlJump2 = NULL;
324 IStorage *pStorage = info->pCHMInfo->pStorage;
325 IStream *pStream = NULL;
331 static const WCHAR null[] = {0};
332 static const WCHAR toc_extW[] = {'h','h','c',0};
333 static const WCHAR index_extW[] = {'h','h','k',0};
334 static const WCHAR windowsW[] = {'#','W','I','N','D','O','W','S',0};
336 hr = IStorage_OpenStream(pStorage, windowsW, NULL, STGM_READ, 0, &pStream);
339 /* jump past the #WINDOWS header */
340 liOffset.QuadPart = sizeof(DWORD) * 2;
342 hr = IStream_Seek(pStream, liOffset, STREAM_SEEK_SET, NULL);
343 if (FAILED(hr)) goto done;
345 /* read the HH_WINTYPE struct data */
346 hr = IStream_Read(pStream, &wintype, sizeof(wintype), &cbRead);
347 if (FAILED(hr)) goto done;
349 /* convert the #STRINGS offsets to actual strings */
350 pszType = ConvertChmString(info, &wintype.pszType);
351 pszFile = ConvertChmString(info, &wintype.pszFile);
352 pszToc = ConvertChmString(info, &wintype.pszToc);
353 pszIndex = ConvertChmString(info, &wintype.pszIndex);
354 pszCaption = ConvertChmString(info, &wintype.pszCaption);
355 pszHome = ConvertChmString(info, &wintype.pszHome);
356 pszJump1 = ConvertChmString(info, &wintype.pszJump1);
357 pszJump2 = ConvertChmString(info, &wintype.pszJump2);
358 pszUrlJump1 = ConvertChmString(info, &wintype.pszUrlJump1);
359 pszUrlJump2 = ConvertChmString(info, &wintype.pszUrlJump2);
363 /* no defined window types so use (hopefully) sane defaults */
364 static const WCHAR defaultwinW[] = {'d','e','f','a','u','l','t','w','i','n','\0'};
365 memset(&wintype, 0, sizeof(wintype));
366 wintype.cbStruct = sizeof(wintype);
367 wintype.fUniCodeStrings = TRUE;
368 wintype.pszType = pszType = strdupW(info->pCHMInfo->defWindow ? info->pCHMInfo->defWindow : defaultwinW);
369 wintype.pszToc = pszToc = strdupW(info->pCHMInfo->defToc ? info->pCHMInfo->defToc : null);
370 wintype.pszIndex = pszIndex = strdupW(null);
371 wintype.fsValidMembers = 0;
372 wintype.fsWinProperties = HHWIN_PROP_TRI_PANE;
373 wintype.dwStyles = WS_POPUP;
374 wintype.dwExStyles = 0;
375 wintype.nShowState = SW_SHOW;
376 wintype.curNavType = HHWIN_NAVTYPE_TOC;
379 /* merge the new data with any pre-existing HH_WINTYPE structure */
380 MergeChmProperties(&wintype, info, FALSE);
381 if (!info->WinType.pszCaption)
382 info->WinType.pszCaption = info->stringsW.pszCaption = strdupW(info->pCHMInfo->defTitle ? info->pCHMInfo->defTitle : null);
383 if (!info->WinType.pszFile)
384 info->WinType.pszFile = info->stringsW.pszFile = strdupW(info->pCHMInfo->defTopic ? info->pCHMInfo->defTopic : null);
385 if (!info->WinType.pszToc)
386 info->WinType.pszToc = info->stringsW.pszToc = FindHTMLHelpSetting(info, toc_extW);
387 if (!info->WinType.pszIndex)
388 info->WinType.pszIndex = info->stringsW.pszIndex = FindHTMLHelpSetting(info, index_extW);
394 heap_free(pszCaption);
398 heap_free(pszUrlJump1);
399 heap_free(pszUrlJump2);
404 IStream_Release(pStream);
409 LPCWSTR skip_schema(LPCWSTR url)
411 static const WCHAR its_schema[] = {'i','t','s',':'};
412 static const WCHAR msits_schema[] = {'m','s','-','i','t','s',':'};
413 static const WCHAR mk_schema[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':'};
415 if(!strncmpiW(its_schema, url, sizeof(its_schema)/sizeof(WCHAR)))
416 return url+sizeof(its_schema)/sizeof(WCHAR);
417 if(!strncmpiW(msits_schema, url, sizeof(msits_schema)/sizeof(WCHAR)))
418 return url+sizeof(msits_schema)/sizeof(WCHAR);
419 if(!strncmpiW(mk_schema, url, sizeof(mk_schema)/sizeof(WCHAR)))
420 return url+sizeof(mk_schema)/sizeof(WCHAR);
425 void SetChmPath(ChmPath *file, LPCWSTR base_file, LPCWSTR path)
428 static const WCHAR separatorW[] = {':',':',0};
430 path = skip_schema(path);
432 ptr = strstrW(path, separatorW);
434 WCHAR chm_file[MAX_PATH];
435 WCHAR rel_path[MAX_PATH];
436 WCHAR base_path[MAX_PATH];
439 strcpyW(base_path, base_file);
440 p = strrchrW(base_path, '\\');
444 memcpy(rel_path, path, (ptr-path)*sizeof(WCHAR));
445 rel_path[ptr-path] = 0;
447 PathCombineW(chm_file, base_path, rel_path);
449 file->chm_file = strdupW(chm_file);
452 file->chm_file = strdupW(base_file);
456 file->chm_index = strdupW(ptr);
458 TRACE("ChmFile = {%s %s}\n", debugstr_w(file->chm_file), debugstr_w(file->chm_index));
461 IStream *GetChmStream(CHMInfo *info, LPCWSTR parent_chm, ChmPath *chm_file)
464 IStream *stream = NULL;
467 TRACE("%s (%s :: %s)\n", debugstr_w(parent_chm), debugstr_w(chm_file->chm_file),
468 debugstr_w(chm_file->chm_index));
470 if(parent_chm || chm_file->chm_file) {
471 hres = IITStorage_StgOpenStorage(info->pITStorage,
472 chm_file->chm_file ? chm_file->chm_file : parent_chm, NULL,
473 STGM_READ | STGM_SHARE_DENY_WRITE, NULL, 0, &storage);
475 WARN("Could not open storage: %08x\n", hres);
479 storage = info->pStorage;
480 IStorage_AddRef(info->pStorage);
483 hres = IStorage_OpenStream(storage, chm_file->chm_index, NULL, STGM_READ, 0, &stream);
484 IStorage_Release(storage);
486 WARN("Could not open stream: %08x\n", hres);
492 * Retrieve a CHM document and parse the data from the <title> element to get the document's title.
494 WCHAR *GetDocumentTitle(CHMInfo *info, LPCWSTR document)
496 strbuf_t node, node_name, content;
497 WCHAR *document_title = NULL;
503 TRACE("%s\n", debugstr_w(document));
505 storage = info->pStorage;
507 WARN("Could not open storage to obtain the title for a document.\n");
510 IStorage_AddRef(storage);
512 hres = IStorage_OpenStream(storage, document, NULL, STGM_READ, 0, &str);
513 IStorage_Release(storage);
515 WARN("Could not open stream: %08x\n", hres);
517 stream_init(&stream, str);
519 strbuf_init(&content);
520 strbuf_init(&node_name);
522 while(next_node(&stream, &node)) {
523 get_node_name(&node, &node_name);
525 TRACE("%s\n", node.buf);
527 if(!strcasecmp(node_name.buf, "title")) {
528 if(next_content(&stream, &content) && content.len > 1)
530 document_title = strdupnAtoW(&content.buf[1], content.len-1);
531 FIXME("magic: %s\n", debugstr_w(document_title));
540 strbuf_free(&content);
541 strbuf_free(&node_name);
542 IStream_Release(str);
544 return document_title;
547 /* Opens the CHM file for reading */
548 CHMInfo *OpenCHM(LPCWSTR szFile)
553 static const WCHAR wszSTRINGS[] = {'#','S','T','R','I','N','G','S',0};
555 if (!(ret = heap_alloc_zero(sizeof(CHMInfo))))
557 ret->codePage = CP_ACP;
559 if (!(ret->szFile = strdupW(szFile))) {
564 hres = CoCreateInstance(&CLSID_ITStorage, NULL, CLSCTX_INPROC_SERVER,
565 &IID_IITStorage, (void **) &ret->pITStorage) ;
567 WARN("Could not create ITStorage: %08x\n", hres);
568 return CloseCHM(ret);
571 hres = IITStorage_StgOpenStorage(ret->pITStorage, szFile, NULL,
572 STGM_READ | STGM_SHARE_DENY_WRITE, NULL, 0, &ret->pStorage);
574 WARN("Could not open storage: %08x\n", hres);
575 return CloseCHM(ret);
577 hres = IStorage_OpenStream(ret->pStorage, wszSTRINGS, NULL, STGM_READ, 0,
578 &ret->strings_stream);
580 WARN("Could not open #STRINGS stream: %08x\n", hres);
581 /* It's not critical, so we pass */
584 if(!ReadChmSystem(ret)) {
585 WARN("Could not read #SYSTEM\n");
586 return CloseCHM(ret);
592 CHMInfo *CloseCHM(CHMInfo *chm)
595 IITStorage_Release(chm->pITStorage);
598 IStorage_Release(chm->pStorage);
600 if(chm->strings_stream)
601 IStream_Release(chm->strings_stream);
603 if(chm->strings_size) {
606 for(i=0; i<chm->strings_size; i++)
607 heap_free(chm->strings[i]);
610 heap_free(chm->strings);
611 heap_free(chm->defWindow);
612 heap_free(chm->defTitle);
613 heap_free(chm->defTopic);
614 heap_free(chm->defToc);
615 heap_free(chm->szFile);
616 heap_free(chm->compiledFile);