hhctrl.ocx: Store full chm paths to solve problems with relative paths.
[wine/gsoc_dplay.git] / dlls / hhctrl.ocx / chm.c
blob493ed5b825fb83c2be0616c643b824bed3315d84
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"
24 #include "winreg.h"
25 #include "shlwapi.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
30 #define BLOCK_BITS 12
31 #define BLOCK_SIZE (1 << BLOCK_BITS)
32 #define BLOCK_MASK (BLOCK_SIZE-1)
34 /* Reads a string from the #STRINGS section in the CHM file */
35 static LPCSTR GetChmString(CHMInfo *chm, DWORD offset)
37 if(!chm->strings_stream)
38 return NULL;
40 if(chm->strings_size <= (offset >> BLOCK_BITS)) {
41 if(chm->strings)
42 chm->strings = hhctrl_realloc_zero(chm->strings,
43 chm->strings_size = ((offset >> BLOCK_BITS)+1)*sizeof(char*));
44 else
45 chm->strings = hhctrl_alloc_zero(
46 chm->strings_size = ((offset >> BLOCK_BITS)+1)*sizeof(char*));
50 if(!chm->strings[offset >> BLOCK_BITS]) {
51 LARGE_INTEGER pos;
52 DWORD read;
53 HRESULT hres;
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;
62 chm->strings[offset >> BLOCK_BITS] = hhctrl_alloc(BLOCK_SIZE);
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 hhctrl_free(chm->strings[offset >> BLOCK_BITS]);
69 chm->strings[offset >> BLOCK_BITS] = NULL;
70 return NULL;
74 return chm->strings[offset >> BLOCK_BITS] + (offset & BLOCK_MASK);
77 static BOOL ReadChmSystem(CHMInfo *chm)
79 IStream *stream;
80 DWORD ver=0xdeadbeef, read, buf_size;
81 char *buf;
82 HRESULT hres;
84 struct {
85 WORD code;
86 WORD len;
87 } entry;
89 static const WCHAR wszSYSTEM[] = {'#','S','Y','S','T','E','M',0};
91 hres = IStorage_OpenStream(chm->pStorage, wszSYSTEM, NULL, STGM_READ, 0, &stream);
92 if(FAILED(hres)) {
93 WARN("Could not open #SYSTEM stream: %08x\n", hres);
94 return FALSE;
97 IStream_Read(stream, &ver, sizeof(ver), &read);
98 TRACE("version is %x\n", ver);
100 buf = hhctrl_alloc(8*sizeof(DWORD));
101 buf_size = 8*sizeof(DWORD);
103 while(1) {
104 hres = IStream_Read(stream, &entry, sizeof(entry), &read);
105 if(hres != S_OK)
106 break;
108 if(entry.len > buf_size)
109 buf = hhctrl_realloc(buf, buf_size=entry.len);
111 hres = IStream_Read(stream, buf, entry.len, &read);
112 if(hres != S_OK)
113 break;
115 switch(entry.code) {
116 case 0x2:
117 TRACE("Default topic is %s\n", debugstr_an(buf, entry.len));
118 break;
119 case 0x3:
120 TRACE("Title is %s\n", debugstr_an(buf, entry.len));
121 break;
122 case 0x5:
123 TRACE("Default window is %s\n", debugstr_an(buf, entry.len));
124 break;
125 case 0x6:
126 TRACE("Compiled file is %s\n", debugstr_an(buf, entry.len));
127 break;
128 case 0x9:
129 TRACE("Version is %s\n", debugstr_an(buf, entry.len));
130 break;
131 case 0xa:
132 TRACE("Time is %08x\n", *(DWORD*)buf);
133 break;
134 case 0xc:
135 TRACE("Number of info types: %d\n", *(DWORD*)buf);
136 break;
137 case 0xf:
138 TRACE("Check sum: %x\n", *(DWORD*)buf);
139 break;
140 default:
141 TRACE("unhandled code %x, size %x\n", entry.code, entry.len);
145 hhctrl_free(buf);
146 IStream_Release(stream);
148 return SUCCEEDED(hres);
151 LPWSTR FindContextAlias(CHMInfo *chm, DWORD index)
153 IStream *ivb_stream;
154 DWORD size, read, i;
155 DWORD *buf;
156 LPCSTR ret = NULL;
157 HRESULT hres;
159 static const WCHAR wszIVB[] = {'#','I','V','B',0};
161 hres = IStorage_OpenStream(chm->pStorage, wszIVB, NULL, STGM_READ, 0, &ivb_stream);
162 if(FAILED(hres)) {
163 WARN("Could not open #IVB stream: %08x\n", hres);
164 return NULL;
167 hres = IStream_Read(ivb_stream, &size, sizeof(size), &read);
168 if(FAILED(hres)) {
169 WARN("Read failed: %08x\n", hres);
170 IStream_Release(ivb_stream);
171 return NULL;
174 buf = hhctrl_alloc(size);
175 hres = IStream_Read(ivb_stream, buf, size, &read);
176 IStream_Release(ivb_stream);
177 if(FAILED(hres)) {
178 WARN("Read failed: %08x\n", hres);
179 hhctrl_free(buf);
180 return NULL;
183 size /= 2*sizeof(DWORD);
185 for(i=0; i<size; i++) {
186 if(buf[2*i] == index) {
187 ret = GetChmString(chm, buf[2*i+1]);
188 break;
192 hhctrl_free(buf);
194 TRACE("returning %s\n", debugstr_a(ret));
195 return strdupAtoW(ret);
198 /* Loads the HH_WINTYPE data from the CHM file
200 * FIXME: There may be more than one window type in the file, so
201 * add the ability to choose a certain window type
203 BOOL LoadWinTypeFromCHM(CHMInfo *pChmInfo, HH_WINTYPEW *pHHWinType)
205 LARGE_INTEGER liOffset;
206 IStorage *pStorage = pChmInfo->pStorage;
207 IStream *pStream;
208 HRESULT hr;
209 DWORD cbRead;
211 static const WCHAR windowsW[] = {'#','W','I','N','D','O','W','S',0};
213 hr = IStorage_OpenStream(pStorage, windowsW, NULL, STGM_READ, 0, &pStream);
214 if (FAILED(hr))
215 return FALSE;
217 /* jump past the #WINDOWS header */
218 liOffset.QuadPart = sizeof(DWORD) * 2;
220 hr = IStream_Seek(pStream, liOffset, STREAM_SEEK_SET, NULL);
221 if (FAILED(hr)) goto done;
223 /* read the HH_WINTYPE struct data */
224 hr = IStream_Read(pStream, pHHWinType, sizeof(*pHHWinType), &cbRead);
225 if (FAILED(hr)) goto done;
227 /* convert the #STRINGS offsets to actual strings */
228 pHHWinType->pszType = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszType));
229 pHHWinType->pszCaption = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszCaption));
230 pHHWinType->pszToc = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszToc));
231 pHHWinType->pszIndex = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszIndex));
232 pHHWinType->pszFile = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszFile));
233 pHHWinType->pszHome = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszHome));
234 pHHWinType->pszJump1 = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszJump1));
235 pHHWinType->pszJump2 = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszJump2));
236 pHHWinType->pszUrlJump1 = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszUrlJump1));
237 pHHWinType->pszUrlJump2 = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszUrlJump2));
239 /* FIXME: pszCustomTabs is a list of multiple zero-terminated strings so ReadString won't
240 * work in this case
242 #if 0
243 pHHWinType->pszCustomTabs = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszCustomTabs);
244 #endif
246 done:
247 IStream_Release(pStream);
249 return SUCCEEDED(hr);
252 void SetChmPath(ChmPath *file, LPCWSTR base_file, LPCWSTR path)
254 LPCWSTR ptr;
255 static const WCHAR separatorW[] = {':',':',0};
257 ptr = strstrW(path, separatorW);
258 if(ptr) {
259 WCHAR chm_file[MAX_PATH];
260 WCHAR rel_path[MAX_PATH];
261 WCHAR base_path[MAX_PATH];
262 LPWSTR p;
264 strcpyW(base_path, base_file);
265 p = strrchrW(base_path, '\\');
266 if(p)
267 *p = 0;
269 memcpy(rel_path, path, (ptr-path)*sizeof(WCHAR));
270 rel_path[ptr-path] = 0;
272 PathCombineW(chm_file, base_path, rel_path);
274 file->chm_file = strdupW(chm_file);
275 ptr += 2;
276 }else {
277 file->chm_file = strdupW(base_file);
278 ptr = path;
281 file->chm_index = strdupW(ptr);
283 TRACE("ChmFile = {%s %s}\n", debugstr_w(file->chm_file), debugstr_w(file->chm_index));
286 IStream *GetChmStream(CHMInfo *info, LPCWSTR parent_chm, ChmPath *chm_file)
288 IStorage *storage;
289 IStream *stream;
290 HRESULT hres;
292 TRACE("%s (%s :: %s)\n", debugstr_w(parent_chm), debugstr_w(chm_file->chm_file),
293 debugstr_w(chm_file->chm_index));
295 if(parent_chm || chm_file->chm_file) {
296 hres = IITStorage_StgOpenStorage(info->pITStorage,
297 chm_file->chm_file ? chm_file->chm_file : parent_chm, NULL,
298 STGM_READ | STGM_SHARE_DENY_WRITE, NULL, 0, &storage);
299 if(FAILED(hres)) {
300 WARN("Could not open storage: %08x\n", hres);
301 return NULL;
303 }else {
304 storage = info->pStorage;
305 IStorage_AddRef(info->pStorage);
308 hres = IStorage_OpenStream(storage, chm_file->chm_index, NULL, STGM_READ, 0, &stream);
309 IStorage_Release(storage);
310 if(FAILED(hres))
311 WARN("Could not open stream: %08x\n", hres);
313 return stream;
316 /* Opens the CHM file for reading */
317 CHMInfo *OpenCHM(LPCWSTR szFile)
319 WCHAR file[MAX_PATH] = {0};
320 DWORD res;
321 HRESULT hres;
323 static const WCHAR wszSTRINGS[] = {'#','S','T','R','I','N','G','S',0};
325 CHMInfo *ret = hhctrl_alloc_zero(sizeof(CHMInfo));
327 res = GetFullPathNameW(szFile, sizeof(file), file, NULL);
328 ret->szFile = strdupW(file);
330 hres = CoCreateInstance(&CLSID_ITStorage, NULL, CLSCTX_INPROC_SERVER,
331 &IID_IITStorage, (void **) &ret->pITStorage) ;
332 if(FAILED(hres)) {
333 WARN("Could not create ITStorage: %08x\n", hres);
334 return CloseCHM(ret);
337 hres = IITStorage_StgOpenStorage(ret->pITStorage, szFile, NULL,
338 STGM_READ | STGM_SHARE_DENY_WRITE, NULL, 0, &ret->pStorage);
339 if(FAILED(hres)) {
340 WARN("Could not open storage: %08x\n", hres);
341 return CloseCHM(ret);
344 hres = IStorage_OpenStream(ret->pStorage, wszSTRINGS, NULL, STGM_READ, 0,
345 &ret->strings_stream);
346 if(FAILED(hres)) {
347 WARN("Could not open #STRINGS stream: %08x\n", hres);
348 return CloseCHM(ret);
351 if(!ReadChmSystem(ret)) {
352 WARN("Could not read #SYSTEM\n");
353 return CloseCHM(ret);
356 return ret;
359 CHMInfo *CloseCHM(CHMInfo *chm)
361 if(chm->pITStorage)
362 IITStorage_Release(chm->pITStorage);
364 if(chm->pStorage)
365 IStorage_Release(chm->pStorage);
367 if(chm->strings_stream)
368 IStream_Release(chm->strings_stream);
370 if(chm->strings_size) {
371 int i;
373 for(i=0; i<chm->strings_size; i++)
374 hhctrl_free(chm->strings[i]);
377 hhctrl_free(chm->strings);
378 hhctrl_free(chm);
380 return NULL;