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 static LPCWSTR
skip_schema(LPCWSTR url
)
254 static const WCHAR its_schema
[] = {'i','t','s',':'};
255 static const WCHAR msits_schema
[] = {'m','s','-','i','t','s',':'};
256 static const WCHAR mk_schema
[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':'};
258 if(!strncmpiW(its_schema
, url
, sizeof(its_schema
)/sizeof(WCHAR
)))
259 return url
+sizeof(its_schema
)/sizeof(WCHAR
);
260 if(!strncmpiW(msits_schema
, url
, sizeof(msits_schema
)/sizeof(WCHAR
)))
261 return url
+sizeof(msits_schema
)/sizeof(WCHAR
);
262 if(!strncmpiW(mk_schema
, url
, sizeof(mk_schema
)/sizeof(WCHAR
)))
263 return url
+sizeof(mk_schema
)/sizeof(WCHAR
);
268 void SetChmPath(ChmPath
*file
, LPCWSTR base_file
, LPCWSTR path
)
271 static const WCHAR separatorW
[] = {':',':',0};
273 path
= skip_schema(path
);
275 ptr
= strstrW(path
, separatorW
);
277 WCHAR chm_file
[MAX_PATH
];
278 WCHAR rel_path
[MAX_PATH
];
279 WCHAR base_path
[MAX_PATH
];
282 strcpyW(base_path
, base_file
);
283 p
= strrchrW(base_path
, '\\');
287 memcpy(rel_path
, path
, (ptr
-path
)*sizeof(WCHAR
));
288 rel_path
[ptr
-path
] = 0;
290 PathCombineW(chm_file
, base_path
, rel_path
);
292 file
->chm_file
= strdupW(chm_file
);
295 file
->chm_file
= strdupW(base_file
);
299 file
->chm_index
= strdupW(ptr
);
301 TRACE("ChmFile = {%s %s}\n", debugstr_w(file
->chm_file
), debugstr_w(file
->chm_index
));
304 IStream
*GetChmStream(CHMInfo
*info
, LPCWSTR parent_chm
, ChmPath
*chm_file
)
310 TRACE("%s (%s :: %s)\n", debugstr_w(parent_chm
), debugstr_w(chm_file
->chm_file
),
311 debugstr_w(chm_file
->chm_index
));
313 if(parent_chm
|| chm_file
->chm_file
) {
314 hres
= IITStorage_StgOpenStorage(info
->pITStorage
,
315 chm_file
->chm_file
? chm_file
->chm_file
: parent_chm
, NULL
,
316 STGM_READ
| STGM_SHARE_DENY_WRITE
, NULL
, 0, &storage
);
318 WARN("Could not open storage: %08x\n", hres
);
322 storage
= info
->pStorage
;
323 IStorage_AddRef(info
->pStorage
);
326 hres
= IStorage_OpenStream(storage
, chm_file
->chm_index
, NULL
, STGM_READ
, 0, &stream
);
327 IStorage_Release(storage
);
329 WARN("Could not open stream: %08x\n", hres
);
334 /* Opens the CHM file for reading */
335 CHMInfo
*OpenCHM(LPCWSTR szFile
)
337 WCHAR file
[MAX_PATH
] = {0};
341 static const WCHAR wszSTRINGS
[] = {'#','S','T','R','I','N','G','S',0};
343 CHMInfo
*ret
= hhctrl_alloc_zero(sizeof(CHMInfo
));
345 res
= GetFullPathNameW(szFile
, sizeof(file
), file
, NULL
);
346 ret
->szFile
= strdupW(file
);
348 hres
= CoCreateInstance(&CLSID_ITStorage
, NULL
, CLSCTX_INPROC_SERVER
,
349 &IID_IITStorage
, (void **) &ret
->pITStorage
) ;
351 WARN("Could not create ITStorage: %08x\n", hres
);
352 return CloseCHM(ret
);
355 hres
= IITStorage_StgOpenStorage(ret
->pITStorage
, szFile
, NULL
,
356 STGM_READ
| STGM_SHARE_DENY_WRITE
, NULL
, 0, &ret
->pStorage
);
358 WARN("Could not open storage: %08x\n", hres
);
359 return CloseCHM(ret
);
362 hres
= IStorage_OpenStream(ret
->pStorage
, wszSTRINGS
, NULL
, STGM_READ
, 0,
363 &ret
->strings_stream
);
365 WARN("Could not open #STRINGS stream: %08x\n", hres
);
366 return CloseCHM(ret
);
369 if(!ReadChmSystem(ret
)) {
370 WARN("Could not read #SYSTEM\n");
371 return CloseCHM(ret
);
377 CHMInfo
*CloseCHM(CHMInfo
*chm
)
380 IITStorage_Release(chm
->pITStorage
);
383 IStorage_Release(chm
->pStorage
);
385 if(chm
->strings_stream
)
386 IStream_Release(chm
->strings_stream
);
388 if(chm
->strings_size
) {
391 for(i
=0; i
<chm
->strings_size
; i
++)
392 hhctrl_free(chm
->strings
[i
]);
395 hhctrl_free(chm
->strings
);