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
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp
);
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
)
40 if(chm
->strings_size
<= (offset
>> BLOCK_BITS
)) {
42 chm
->strings
= hhctrl_realloc_zero(chm
->strings
,
43 chm
->strings_size
= ((offset
>> BLOCK_BITS
)+1)*sizeof(char*));
45 chm
->strings
= hhctrl_alloc_zero(
46 chm
->strings_size
= ((offset
>> BLOCK_BITS
)+1)*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
] = hhctrl_alloc(BLOCK_SIZE
);
64 hres
= IStream_Read(chm
->strings_stream
, chm
->strings
[offset
>> BLOCK_BITS
],
67 WARN("Read failed: %08x\n", hres
);
68 hhctrl_free(chm
->strings
[offset
>> BLOCK_BITS
]);
69 chm
->strings
[offset
>> BLOCK_BITS
] = NULL
;
74 return chm
->strings
[offset
>> BLOCK_BITS
] + (offset
& BLOCK_MASK
);
77 static BOOL
ReadChmSystem(CHMInfo
*chm
)
80 DWORD ver
=0xdeadbeef, read
, buf_size
;
89 static const WCHAR wszSYSTEM
[] = {'#','S','Y','S','T','E','M',0};
91 hres
= IStorage_OpenStream(chm
->pStorage
, wszSYSTEM
, NULL
, STGM_READ
, 0, &stream
);
93 WARN("Could not open #SYSTEM stream: %08x\n", hres
);
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
);
104 hres
= IStream_Read(stream
, &entry
, sizeof(entry
), &read
);
108 if(entry
.len
> buf_size
)
109 buf
= hhctrl_realloc(buf
, buf_size
=entry
.len
);
111 hres
= IStream_Read(stream
, buf
, entry
.len
, &read
);
117 TRACE("Default topic is %s\n", debugstr_an(buf
, entry
.len
));
120 TRACE("Title is %s\n", debugstr_an(buf
, entry
.len
));
123 TRACE("Default window is %s\n", debugstr_an(buf
, entry
.len
));
126 TRACE("Compiled file is %s\n", debugstr_an(buf
, entry
.len
));
129 TRACE("Version is %s\n", debugstr_an(buf
, entry
.len
));
132 TRACE("Time is %08x\n", *(DWORD
*)buf
);
135 TRACE("Number of info types: %d\n", *(DWORD
*)buf
);
138 TRACE("Check sum: %x\n", *(DWORD
*)buf
);
141 TRACE("unhandled code %x, size %x\n", entry
.code
, entry
.len
);
146 IStream_Release(stream
);
148 return SUCCEEDED(hres
);
151 LPWSTR
FindContextAlias(CHMInfo
*chm
, DWORD index
)
159 static const WCHAR wszIVB
[] = {'#','I','V','B',0};
161 hres
= IStorage_OpenStream(chm
->pStorage
, wszIVB
, NULL
, STGM_READ
, 0, &ivb_stream
);
163 WARN("Could not open #IVB stream: %08x\n", hres
);
167 hres
= IStream_Read(ivb_stream
, &size
, sizeof(size
), &read
);
169 WARN("Read failed: %08x\n", hres
);
170 IStream_Release(ivb_stream
);
174 buf
= hhctrl_alloc(size
);
175 hres
= IStream_Read(ivb_stream
, buf
, size
, &read
);
176 IStream_Release(ivb_stream
);
178 WARN("Read failed: %08x\n", hres
);
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]);
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
;
211 static const WCHAR windowsW
[] = {'#','W','I','N','D','O','W','S',0};
213 hr
= IStorage_OpenStream(pStorage
, windowsW
, NULL
, STGM_READ
, 0, &pStream
);
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
243 pHHWinType
->pszCustomTabs
= CHM_ReadString(pChmInfo
, (DWORD
)pHHWinType
->pszCustomTabs
);
247 IStream_Release(pStream
);
249 return SUCCEEDED(hr
);
252 void SetChmPath(ChmPath
*file
, LPCWSTR base_file
, LPCWSTR path
)
255 static const WCHAR separatorW
[] = {':',':',0};
257 ptr
= strstrW(path
, separatorW
);
259 WCHAR chm_file
[MAX_PATH
];
260 WCHAR rel_path
[MAX_PATH
];
261 WCHAR base_path
[MAX_PATH
];
264 strcpyW(base_path
, base_file
);
265 p
= strrchrW(base_path
, '\\');
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
);
277 file
->chm_file
= strdupW(base_file
);
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
)
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
);
300 WARN("Could not open storage: %08x\n", hres
);
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
);
311 WARN("Could not open stream: %08x\n", hres
);
316 /* Opens the CHM file for reading */
317 CHMInfo
*OpenCHM(LPCWSTR szFile
)
319 WCHAR file
[MAX_PATH
] = {0};
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
) ;
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
);
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
);
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
);
359 CHMInfo
*CloseCHM(CHMInfo
*chm
)
362 IITStorage_Release(chm
->pITStorage
);
365 IStorage_Release(chm
->pStorage
);
367 if(chm
->strings_stream
)
368 IStream_Release(chm
->strings_stream
);
370 if(chm
->strings_size
) {
373 for(i
=0; i
<chm
->strings_size
; i
++)
374 hhctrl_free(chm
->strings
[i
]);
377 hhctrl_free(chm
->strings
);