1 /***************************************************************************
2 * chm_lib.c - CHM archive manipulation routines *
3 * ------------------- *
5 * author: Jed Wing <jedwin@ugcs.caltech.edu> *
7 * notes: These routines are meant for the manipulation of microsoft *
8 * .chm (compiled html help) files, but may likely be used *
9 * for the manipulation of any ITSS archive, if ever ITSS *
10 * archives are used for any other purpose. *
12 * Note also that the section names are statically handled. *
13 * To be entirely correct, the section names should be read *
14 * from the section names meta-file, and then the various *
15 * content sections and the "transforms" to apply to the data *
16 * they contain should be inferred from the section name and *
17 * the meta-files referenced using that name; however, all of *
18 * the files I've been able to get my hands on appear to have *
19 * only two sections: Uncompressed and MSCompressed. *
20 * Additionally, the ITSS.DLL file included with Windows does *
21 * not appear to handle any different transforms than the *
22 * simple LZX-transform. Furthermore, the list of transforms *
23 * to apply is broken, in that only half the required space *
24 * is allocated for the list. (It appears as though the *
25 * space is allocated for ASCII strings, but the strings are *
26 * written as unicode. As a result, only the first half of *
27 * the string appears.) So this is probably not too big of *
28 * a deal, at least until CHM v4 (MS .lit files), which also *
29 * incorporate encryption, of some description. *
31 ***************************************************************************/
33 /***************************************************************************
35 * This library is free software; you can redistribute it and/or
36 * modify it under the terms of the GNU Lesser General Public
37 * License as published by the Free Software Foundation; either
38 * version 2.1 of the License, or (at your option) any later version.
40 * This library is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
43 * Lesser General Public License for more details.
45 * You should have received a copy of the GNU Lesser General Public
46 * License along with this library; if not, write to the Free Software
47 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
49 ***************************************************************************/
51 /***************************************************************************
53 * Adapted for Wine by Mike McCormack *
55 ***************************************************************************/
59 #define CHM_ACQUIRE_LOCK(a) do { \
60 EnterCriticalSection(&(a)); \
62 #define CHM_RELEASE_LOCK(a) do { \
63 LeaveCriticalSection(&(a)); \
66 #define CHM_NULL_FD (INVALID_HANDLE_VALUE)
67 #define CHM_CLOSE_FILE(fd) CloseHandle((fd))
70 * defines related to tuning
72 #ifndef CHM_MAX_BLOCKS_CACHED
73 #define CHM_MAX_BLOCKS_CACHED 5
75 #define CHM_PARAM_MAX_BLOCKS_CACHED 0
78 * architecture specific defines
80 * Note: as soon as C99 is more widespread, the below defines should
81 * probably just use the C99 sized-int types.
83 * The following settings will probably work for many platforms. The sizes
84 * don't have to be exactly correct, but the types must accommodate at least as
85 * many bits as they specify.
88 /* i386, 32-bit, Windows */
91 typedef USHORT UInt16
;
94 typedef LONGLONG Int64
;
95 typedef ULONGLONG UInt64
;
97 /* utilities for unmarshalling data */
98 static BOOL
_unmarshal_char_array(unsigned char **pData
,
99 unsigned int *pLenRemain
,
103 if (count
<= 0 || (unsigned int)count
> *pLenRemain
)
105 memcpy(dest
, (*pData
), count
);
107 *pLenRemain
-= count
;
111 static BOOL
_unmarshal_uchar_array(unsigned char **pData
,
112 unsigned int *pLenRemain
,
116 if (count
<= 0 || (unsigned int)count
> *pLenRemain
)
118 memcpy(dest
, (*pData
), count
);
120 *pLenRemain
-= count
;
124 static BOOL
_unmarshal_int32(unsigned char **pData
,
125 unsigned int *pLenRemain
,
130 *dest
= (*pData
)[0] | (*pData
)[1]<<8 | (*pData
)[2]<<16 | (*pData
)[3]<<24;
136 static BOOL
_unmarshal_uint32(unsigned char **pData
,
137 unsigned int *pLenRemain
,
142 *dest
= (*pData
)[0] | (*pData
)[1]<<8 | (*pData
)[2]<<16 | (*pData
)[3]<<24;
148 static BOOL
_unmarshal_int64(unsigned char **pData
,
149 unsigned int *pLenRemain
,
160 temp
|= (*pData
)[i
-1];
168 static BOOL
_unmarshal_uint64(unsigned char **pData
,
169 unsigned int *pLenRemain
,
180 temp
|= (*pData
)[i
-1];
188 static BOOL
_unmarshal_uuid(unsigned char **pData
,
189 unsigned int *pDataLen
,
192 return _unmarshal_uchar_array(pData
, pDataLen
, dest
, 16);
195 /* names of sections essential to decompression */
196 static const WCHAR _CHMU_RESET_TABLE
[] = {
197 ':',':','D','a','t','a','S','p','a','c','e','/',
198 'S','t','o','r','a','g','e','/',
199 'M','S','C','o','m','p','r','e','s','s','e','d','/',
200 'T','r','a','n','s','f','o','r','m','/',
201 '{','7','F','C','2','8','9','4','0','-','9','D','3','1',
202 '-','1','1','D','0','-','9','B','2','7','-',
203 '0','0','A','0','C','9','1','E','9','C','7','C','}','/',
204 'I','n','s','t','a','n','c','e','D','a','t','a','/',
205 'R','e','s','e','t','T','a','b','l','e',0
207 static const WCHAR _CHMU_LZXC_CONTROLDATA
[] = {
208 ':',':','D','a','t','a','S','p','a','c','e','/',
209 'S','t','o','r','a','g','e','/',
210 'M','S','C','o','m','p','r','e','s','s','e','d','/',
211 'C','o','n','t','r','o','l','D','a','t','a',0
213 static const WCHAR _CHMU_CONTENT
[] = {
214 ':',':','D','a','t','a','S','p','a','c','e','/',
215 'S','t','o','r','a','g','e','/',
216 'M','S','C','o','m','p','r','e','s','s','e','d','/',
217 'C','o','n','t','e','n','t',0
221 * structures local to this module
224 /* structure of ITSF headers */
225 #define _CHM_ITSF_V2_LEN (0x58)
226 #define _CHM_ITSF_V3_LEN (0x60)
229 char signature
[4]; /* 0 (ITSF) */
230 Int32 version
; /* 4 */
231 Int32 header_len
; /* 8 */
232 Int32 unknown_000c
; /* c */
233 UInt32 last_modified
; /* 10 */
234 UInt32 lang_id
; /* 14 */
235 UChar dir_uuid
[16]; /* 18 */
236 UChar stream_uuid
[16]; /* 28 */
237 UInt64 unknown_offset
; /* 38 */
238 UInt64 unknown_len
; /* 40 */
239 UInt64 dir_offset
; /* 48 */
240 UInt64 dir_len
; /* 50 */
241 UInt64 data_offset
; /* 58 (Not present before V3) */
242 }; /* __attribute__ ((aligned (1))); */
244 static BOOL
_unmarshal_itsf_header(unsigned char **pData
,
245 unsigned int *pDataLen
,
246 struct chmItsfHeader
*dest
)
248 /* we only know how to deal with the 0x58 and 0x60 byte structures */
249 if (*pDataLen
!= _CHM_ITSF_V2_LEN
&& *pDataLen
!= _CHM_ITSF_V3_LEN
)
252 /* unmarshal common fields */
253 _unmarshal_char_array(pData
, pDataLen
, dest
->signature
, 4);
254 _unmarshal_int32 (pData
, pDataLen
, &dest
->version
);
255 _unmarshal_int32 (pData
, pDataLen
, &dest
->header_len
);
256 _unmarshal_int32 (pData
, pDataLen
, &dest
->unknown_000c
);
257 _unmarshal_uint32 (pData
, pDataLen
, &dest
->last_modified
);
258 _unmarshal_uint32 (pData
, pDataLen
, &dest
->lang_id
);
259 _unmarshal_uuid (pData
, pDataLen
, dest
->dir_uuid
);
260 _unmarshal_uuid (pData
, pDataLen
, dest
->stream_uuid
);
261 _unmarshal_uint64 (pData
, pDataLen
, &dest
->unknown_offset
);
262 _unmarshal_uint64 (pData
, pDataLen
, &dest
->unknown_len
);
263 _unmarshal_uint64 (pData
, pDataLen
, &dest
->dir_offset
);
264 _unmarshal_uint64 (pData
, pDataLen
, &dest
->dir_len
);
266 /* error check the data */
267 /* XXX: should also check UUIDs, probably, though with a version 3 file,
268 * current MS tools do not seem to use them.
270 if (memcmp(dest
->signature
, "ITSF", 4) != 0)
272 if (dest
->version
== 2)
274 if (dest
->header_len
< _CHM_ITSF_V2_LEN
)
277 else if (dest
->version
== 3)
279 if (dest
->header_len
< _CHM_ITSF_V3_LEN
)
285 /* now, if we have a V3 structure, unmarshal the rest.
286 * otherwise, compute it
288 if (dest
->version
== 3)
291 _unmarshal_uint64(pData
, pDataLen
, &dest
->data_offset
);
296 dest
->data_offset
= dest
->dir_offset
+ dest
->dir_len
;
301 /* structure of ITSP headers */
302 #define _CHM_ITSP_V1_LEN (0x54)
305 char signature
[4]; /* 0 (ITSP) */
306 Int32 version
; /* 4 */
307 Int32 header_len
; /* 8 */
308 Int32 unknown_000c
; /* c */
309 UInt32 block_len
; /* 10 */
310 Int32 blockidx_intvl
; /* 14 */
311 Int32 index_depth
; /* 18 */
312 Int32 index_root
; /* 1c */
313 Int32 index_head
; /* 20 */
314 Int32 unknown_0024
; /* 24 */
315 UInt32 num_blocks
; /* 28 */
316 Int32 unknown_002c
; /* 2c */
317 UInt32 lang_id
; /* 30 */
318 UChar system_uuid
[16]; /* 34 */
319 UChar unknown_0044
[16]; /* 44 */
320 }; /* __attribute__ ((aligned (1))); */
322 static BOOL
_unmarshal_itsp_header(unsigned char **pData
,
323 unsigned int *pDataLen
,
324 struct chmItspHeader
*dest
)
326 /* we only know how to deal with a 0x54 byte structures */
327 if (*pDataLen
!= _CHM_ITSP_V1_LEN
)
330 /* unmarshal fields */
331 _unmarshal_char_array(pData
, pDataLen
, dest
->signature
, 4);
332 _unmarshal_int32 (pData
, pDataLen
, &dest
->version
);
333 _unmarshal_int32 (pData
, pDataLen
, &dest
->header_len
);
334 _unmarshal_int32 (pData
, pDataLen
, &dest
->unknown_000c
);
335 _unmarshal_uint32 (pData
, pDataLen
, &dest
->block_len
);
336 _unmarshal_int32 (pData
, pDataLen
, &dest
->blockidx_intvl
);
337 _unmarshal_int32 (pData
, pDataLen
, &dest
->index_depth
);
338 _unmarshal_int32 (pData
, pDataLen
, &dest
->index_root
);
339 _unmarshal_int32 (pData
, pDataLen
, &dest
->index_head
);
340 _unmarshal_int32 (pData
, pDataLen
, &dest
->unknown_0024
);
341 _unmarshal_uint32 (pData
, pDataLen
, &dest
->num_blocks
);
342 _unmarshal_int32 (pData
, pDataLen
, &dest
->unknown_002c
);
343 _unmarshal_uint32 (pData
, pDataLen
, &dest
->lang_id
);
344 _unmarshal_uuid (pData
, pDataLen
, dest
->system_uuid
);
345 _unmarshal_uchar_array(pData
, pDataLen
, dest
->unknown_0044
, 16);
347 /* error check the data */
348 if (memcmp(dest
->signature
, "ITSP", 4) != 0)
350 if (dest
->version
!= 1)
352 if (dest
->header_len
!= _CHM_ITSP_V1_LEN
)
358 /* structure of PMGL headers */
359 static const char _chm_pmgl_marker
[4] = "PMGL";
360 #define _CHM_PMGL_LEN (0x14)
363 char signature
[4]; /* 0 (PMGL) */
364 UInt32 free_space
; /* 4 */
365 UInt32 unknown_0008
; /* 8 */
366 Int32 block_prev
; /* c */
367 Int32 block_next
; /* 10 */
368 }; /* __attribute__ ((aligned (1))); */
370 static BOOL
_unmarshal_pmgl_header(unsigned char **pData
,
371 unsigned int *pDataLen
,
372 struct chmPmglHeader
*dest
)
374 /* we only know how to deal with a 0x14 byte structures */
375 if (*pDataLen
!= _CHM_PMGL_LEN
)
378 /* unmarshal fields */
379 _unmarshal_char_array(pData
, pDataLen
, dest
->signature
, 4);
380 _unmarshal_uint32 (pData
, pDataLen
, &dest
->free_space
);
381 _unmarshal_uint32 (pData
, pDataLen
, &dest
->unknown_0008
);
382 _unmarshal_int32 (pData
, pDataLen
, &dest
->block_prev
);
383 _unmarshal_int32 (pData
, pDataLen
, &dest
->block_next
);
385 /* check structure */
386 if (memcmp(dest
->signature
, _chm_pmgl_marker
, 4) != 0)
392 /* structure of PMGI headers */
393 static const char _chm_pmgi_marker
[4] = "PMGI";
394 #define _CHM_PMGI_LEN (0x08)
397 char signature
[4]; /* 0 (PMGI) */
398 UInt32 free_space
; /* 4 */
399 }; /* __attribute__ ((aligned (1))); */
401 static BOOL
_unmarshal_pmgi_header(unsigned char **pData
,
402 unsigned int *pDataLen
,
403 struct chmPmgiHeader
*dest
)
405 /* we only know how to deal with a 0x8 byte structures */
406 if (*pDataLen
!= _CHM_PMGI_LEN
)
409 /* unmarshal fields */
410 _unmarshal_char_array(pData
, pDataLen
, dest
->signature
, 4);
411 _unmarshal_uint32 (pData
, pDataLen
, &dest
->free_space
);
413 /* check structure */
414 if (memcmp(dest
->signature
, _chm_pmgi_marker
, 4) != 0)
420 /* structure of LZXC reset table */
421 #define _CHM_LZXC_RESETTABLE_V1_LEN (0x28)
422 struct chmLzxcResetTable
428 UInt64 uncompressed_len
;
429 UInt64 compressed_len
;
431 }; /* __attribute__ ((aligned (1))); */
433 static BOOL
_unmarshal_lzxc_reset_table(unsigned char **pData
,
434 unsigned int *pDataLen
,
435 struct chmLzxcResetTable
*dest
)
437 /* we only know how to deal with a 0x28 byte structures */
438 if (*pDataLen
!= _CHM_LZXC_RESETTABLE_V1_LEN
)
441 /* unmarshal fields */
442 _unmarshal_uint32 (pData
, pDataLen
, &dest
->version
);
443 _unmarshal_uint32 (pData
, pDataLen
, &dest
->block_count
);
444 _unmarshal_uint32 (pData
, pDataLen
, &dest
->unknown
);
445 _unmarshal_uint32 (pData
, pDataLen
, &dest
->table_offset
);
446 _unmarshal_uint64 (pData
, pDataLen
, &dest
->uncompressed_len
);
447 _unmarshal_uint64 (pData
, pDataLen
, &dest
->compressed_len
);
448 _unmarshal_uint64 (pData
, pDataLen
, &dest
->block_len
);
450 /* check structure */
451 if (dest
->version
!= 2)
457 /* structure of LZXC control data block */
458 #define _CHM_LZXC_MIN_LEN (0x18)
459 #define _CHM_LZXC_V2_LEN (0x1c)
460 struct chmLzxcControlData
463 char signature
[4]; /* 4 (LZXC) */
464 UInt32 version
; /* 8 */
465 UInt32 resetInterval
; /* c */
466 UInt32 windowSize
; /* 10 */
467 UInt32 windowsPerReset
; /* 14 */
468 UInt32 unknown_18
; /* 18 */
471 static BOOL
_unmarshal_lzxc_control_data(unsigned char **pData
,
472 unsigned int *pDataLen
,
473 struct chmLzxcControlData
*dest
)
475 /* we want at least 0x18 bytes */
476 if (*pDataLen
< _CHM_LZXC_MIN_LEN
)
479 /* unmarshal fields */
480 _unmarshal_uint32 (pData
, pDataLen
, &dest
->size
);
481 _unmarshal_char_array(pData
, pDataLen
, dest
->signature
, 4);
482 _unmarshal_uint32 (pData
, pDataLen
, &dest
->version
);
483 _unmarshal_uint32 (pData
, pDataLen
, &dest
->resetInterval
);
484 _unmarshal_uint32 (pData
, pDataLen
, &dest
->windowSize
);
485 _unmarshal_uint32 (pData
, pDataLen
, &dest
->windowsPerReset
);
487 if (*pDataLen
>= _CHM_LZXC_V2_LEN
)
488 _unmarshal_uint32 (pData
, pDataLen
, &dest
->unknown_18
);
490 dest
->unknown_18
= 0;
492 if (dest
->version
== 2)
494 dest
->resetInterval
*= 0x8000;
495 dest
->windowSize
*= 0x8000;
497 if (dest
->windowSize
== 0 || dest
->resetInterval
== 0)
500 /* for now, only support resetInterval a multiple of windowSize/2 */
501 if (dest
->windowSize
== 1)
503 if ((dest
->resetInterval
% (dest
->windowSize
/2)) != 0)
506 /* check structure */
507 if (memcmp(dest
->signature
, "LZXC", 4) != 0)
513 /* the structure used for chm file handles */
518 CRITICAL_SECTION mutex
;
519 CRITICAL_SECTION lzx_mutex
;
520 CRITICAL_SECTION cache_mutex
;
530 struct chmUnitInfo rt_unit
;
531 struct chmUnitInfo cn_unit
;
532 struct chmLzxcResetTable reset_table
;
534 /* LZX control data */
535 int compression_enabled
;
537 UInt32 reset_interval
;
538 UInt32 reset_blkcount
;
540 /* decompressor state */
541 struct LZXstate
*lzx_state
;
544 /* cache for decompressed blocks */
545 UChar
**cache_blocks
;
546 Int64
*cache_block_indices
;
547 Int32 cache_num_blocks
;
551 * utility functions local to this module
554 /* utility function to handle differences between {pread,read}(64)? */
555 static Int64
_chm_fetch_bytes(struct chmFile
*h
,
561 if (h
->fd
== CHM_NULL_FD
)
564 CHM_ACQUIRE_LOCK(h
->mutex
);
565 /* NOTE: this might be better done with CreateFileMapping, et cetera... */
567 LARGE_INTEGER old_pos
, new_pos
;
570 /* awkward Win32 Seek/Tell */
571 new_pos
.QuadPart
= 0;
572 SetFilePointerEx( h
->fd
, new_pos
, &old_pos
, FILE_CURRENT
);
573 new_pos
.QuadPart
= os
;
574 SetFilePointerEx( h
->fd
, new_pos
, NULL
, FILE_BEGIN
);
586 /* restore original position */
587 SetFilePointerEx( h
->fd
, old_pos
, NULL
, FILE_BEGIN
);
589 CHM_RELEASE_LOCK(h
->mutex
);
594 * set a parameter on the file handle.
595 * valid parameter types:
596 * CHM_PARAM_MAX_BLOCKS_CACHED:
597 * how many decompressed blocks should be cached? A simple
598 * caching scheme is used, wherein the index of the block is
599 * used as a hash value, and hash collision results in the
600 * invalidation of the previously cached block.
602 static void chm_set_param(struct chmFile
*h
,
608 case CHM_PARAM_MAX_BLOCKS_CACHED
:
609 CHM_ACQUIRE_LOCK(h
->cache_mutex
);
610 if (paramVal
!= h
->cache_num_blocks
)
616 /* allocate new cached blocks */
617 newBlocks
= HeapAlloc(GetProcessHeap(), 0, paramVal
* sizeof (UChar
*));
618 newIndices
= HeapAlloc(GetProcessHeap(), 0, paramVal
* sizeof (UInt64
));
619 for (i
=0; i
<paramVal
; i
++)
625 /* re-distribute old cached blocks */
628 for (i
=0; i
<h
->cache_num_blocks
; i
++)
630 int newSlot
= (int)(h
->cache_block_indices
[i
] % paramVal
);
632 if (h
->cache_blocks
[i
])
634 /* in case of collision, destroy newcomer */
635 if (newBlocks
[newSlot
])
637 HeapFree(GetProcessHeap(), 0, h
->cache_blocks
[i
]);
638 h
->cache_blocks
[i
] = NULL
;
642 newBlocks
[newSlot
] = h
->cache_blocks
[i
];
643 newIndices
[newSlot
] =
644 h
->cache_block_indices
[i
];
649 HeapFree(GetProcessHeap(), 0, h
->cache_blocks
);
650 HeapFree(GetProcessHeap(), 0, h
->cache_block_indices
);
653 /* now, set new values */
654 h
->cache_blocks
= newBlocks
;
655 h
->cache_block_indices
= newIndices
;
656 h
->cache_num_blocks
= paramVal
;
658 CHM_RELEASE_LOCK(h
->cache_mutex
);
666 /* open an ITS archive */
667 struct chmFile
*chm_openW(const WCHAR
*filename
)
669 unsigned char sbuffer
[256];
670 unsigned int sremain
;
671 unsigned char *sbufpos
;
672 struct chmFile
*newHandle
=NULL
;
673 struct chmItsfHeader itsfHeader
;
674 struct chmItspHeader itspHeader
;
676 struct chmUnitInfo uiSpan
;
678 struct chmUnitInfo uiLzxc
;
679 struct chmLzxcControlData ctlData
;
681 /* allocate handle */
682 newHandle
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct chmFile
));
683 newHandle
->fd
= CHM_NULL_FD
;
684 newHandle
->lzx_state
= NULL
;
685 newHandle
->cache_blocks
= NULL
;
686 newHandle
->cache_block_indices
= NULL
;
687 newHandle
->cache_num_blocks
= 0;
690 if ((newHandle
->fd
=CreateFileW(filename
,
695 FILE_ATTRIBUTE_NORMAL
,
696 NULL
)) == CHM_NULL_FD
)
698 HeapFree(GetProcessHeap(), 0, newHandle
);
702 /* initialize mutexes, if needed */
703 InitializeCriticalSection(&newHandle
->mutex
);
704 newHandle
->mutex
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": chmFile.mutex");
705 InitializeCriticalSection(&newHandle
->lzx_mutex
);
706 newHandle
->lzx_mutex
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": chmFile.lzx_mutex");
707 InitializeCriticalSection(&newHandle
->cache_mutex
);
708 newHandle
->cache_mutex
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": chmFile.cache_mutex");
710 /* read and verify header */
711 sremain
= _CHM_ITSF_V3_LEN
;
713 if (_chm_fetch_bytes(newHandle
, sbuffer
, 0, sremain
) != sremain
||
714 !_unmarshal_itsf_header(&sbufpos
, &sremain
, &itsfHeader
))
716 chm_close(newHandle
);
720 /* stash important values from header */
721 newHandle
->dir_offset
= itsfHeader
.dir_offset
;
722 newHandle
->dir_len
= itsfHeader
.dir_len
;
723 newHandle
->data_offset
= itsfHeader
.data_offset
;
725 /* now, read and verify the directory header chunk */
726 sremain
= _CHM_ITSP_V1_LEN
;
728 if (_chm_fetch_bytes(newHandle
, sbuffer
,
729 itsfHeader
.dir_offset
, sremain
) != sremain
||
730 !_unmarshal_itsp_header(&sbufpos
, &sremain
, &itspHeader
))
732 chm_close(newHandle
);
736 /* grab essential information from ITSP header */
737 newHandle
->dir_offset
+= itspHeader
.header_len
;
738 newHandle
->dir_len
-= itspHeader
.header_len
;
739 newHandle
->index_root
= itspHeader
.index_root
;
740 newHandle
->index_head
= itspHeader
.index_head
;
741 newHandle
->block_len
= itspHeader
.block_len
;
743 /* if the index root is -1, this means we don't have any PMGI blocks.
744 * as a result, we must use the sole PMGL block as the index root
746 if (newHandle
->index_root
== -1)
747 newHandle
->index_root
= newHandle
->index_head
;
749 /* initialize cache */
750 chm_set_param(newHandle
, CHM_PARAM_MAX_BLOCKS_CACHED
,
751 CHM_MAX_BLOCKS_CACHED
);
753 /* By default, compression is enabled. */
754 newHandle
->compression_enabled
= 1;
756 /* prefetch most commonly needed unit infos */
757 if (CHM_RESOLVE_SUCCESS
!= chm_resolve_object(newHandle
,
759 &newHandle
->rt_unit
) ||
760 newHandle
->rt_unit
.space
== CHM_COMPRESSED
||
761 CHM_RESOLVE_SUCCESS
!= chm_resolve_object(newHandle
,
763 &newHandle
->cn_unit
) ||
764 newHandle
->cn_unit
.space
== CHM_COMPRESSED
||
765 CHM_RESOLVE_SUCCESS
!= chm_resolve_object(newHandle
,
766 _CHMU_LZXC_CONTROLDATA
,
768 uiLzxc
.space
== CHM_COMPRESSED
)
770 newHandle
->compression_enabled
= 0;
773 /* read reset table info */
774 if (newHandle
->compression_enabled
)
776 sremain
= _CHM_LZXC_RESETTABLE_V1_LEN
;
778 if (chm_retrieve_object(newHandle
, &newHandle
->rt_unit
, sbuffer
,
779 0, sremain
) != sremain
||
780 !_unmarshal_lzxc_reset_table(&sbufpos
, &sremain
,
781 &newHandle
->reset_table
))
783 newHandle
->compression_enabled
= 0;
787 /* read control data */
788 if (newHandle
->compression_enabled
)
790 sremain
= (unsigned long)uiLzxc
.length
;
792 if (chm_retrieve_object(newHandle
, &uiLzxc
, sbuffer
,
793 0, sremain
) != sremain
||
794 !_unmarshal_lzxc_control_data(&sbufpos
, &sremain
,
797 newHandle
->compression_enabled
= 0;
800 newHandle
->window_size
= ctlData
.windowSize
;
801 newHandle
->reset_interval
= ctlData
.resetInterval
;
803 /* Jed, Mon Jun 28: Experimentally, it appears that the reset block count */
804 /* must be multiplied by this formerly unknown ctrl data field in */
805 /* order to decompress some files. */
807 newHandle
->reset_blkcount
= newHandle
->reset_interval
/
808 (newHandle
->window_size
/ 2);
810 newHandle
->reset_blkcount
= newHandle
->reset_interval
/
811 (newHandle
->window_size
/ 2) *
812 ctlData
.windowsPerReset
;
819 /* Duplicate an ITS archive handle */
820 struct chmFile
*chm_dup(struct chmFile
*oldHandle
)
822 struct chmFile
*newHandle
=NULL
;
824 newHandle
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct chmFile
));
825 *newHandle
= *oldHandle
;
827 /* duplicate fd handle */
828 DuplicateHandle(GetCurrentProcess(), oldHandle
->fd
,
829 GetCurrentProcess(), &(newHandle
->fd
),
830 0, FALSE
, DUPLICATE_SAME_ACCESS
);
831 newHandle
->lzx_state
= NULL
;
832 newHandle
->cache_blocks
= NULL
;
833 newHandle
->cache_block_indices
= NULL
;
834 newHandle
->cache_num_blocks
= 0;
836 /* initialize mutexes, if needed */
837 InitializeCriticalSection(&newHandle
->mutex
);
838 newHandle
->mutex
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": chmFile.mutex");
839 InitializeCriticalSection(&newHandle
->lzx_mutex
);
840 newHandle
->lzx_mutex
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": chmFile.lzx_mutex");
841 InitializeCriticalSection(&newHandle
->cache_mutex
);
842 newHandle
->cache_mutex
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": chmFile.cache_mutex");
844 /* initialize cache */
845 chm_set_param(newHandle
, CHM_PARAM_MAX_BLOCKS_CACHED
,
846 CHM_MAX_BLOCKS_CACHED
);
851 /* close an ITS archive */
852 void chm_close(struct chmFile
*h
)
856 if (h
->fd
!= CHM_NULL_FD
)
857 CHM_CLOSE_FILE(h
->fd
);
860 h
->mutex
.DebugInfo
->Spare
[0] = 0;
861 DeleteCriticalSection(&h
->mutex
);
862 h
->lzx_mutex
.DebugInfo
->Spare
[0] = 0;
863 DeleteCriticalSection(&h
->lzx_mutex
);
864 h
->cache_mutex
.DebugInfo
->Spare
[0] = 0;
865 DeleteCriticalSection(&h
->cache_mutex
);
868 LZXteardown(h
->lzx_state
);
874 for (i
=0; i
<h
->cache_num_blocks
; i
++)
876 HeapFree(GetProcessHeap(), 0, h
->cache_blocks
[i
]);
878 HeapFree(GetProcessHeap(), 0, h
->cache_blocks
);
879 h
->cache_blocks
= NULL
;
882 HeapFree(GetProcessHeap(), 0, h
->cache_block_indices
);
883 h
->cache_block_indices
= NULL
;
885 HeapFree(GetProcessHeap(), 0, h
);
890 * helper methods for chm_resolve_object
893 /* skip a compressed dword */
894 static void _chm_skip_cword(UChar
**pEntry
)
896 while (*(*pEntry
)++ >= 0x80)
900 /* skip the data from a PMGL entry */
901 static void _chm_skip_PMGL_entry_data(UChar
**pEntry
)
903 _chm_skip_cword(pEntry
);
904 _chm_skip_cword(pEntry
);
905 _chm_skip_cword(pEntry
);
908 /* parse a compressed dword */
909 static UInt64
_chm_parse_cword(UChar
**pEntry
)
913 while ((temp
=*(*pEntry
)++) >= 0x80)
916 accum
+= temp
& 0x7f;
919 return (accum
<< 7) + temp
;
922 /* parse a utf-8 string into an ASCII char buffer */
923 static BOOL
_chm_parse_UTF8(UChar
**pEntry
, UInt64 count
, WCHAR
*path
)
925 /* MJM - Modified to return real Unicode strings */
928 *path
++ = (*(*pEntry
)++);
936 /* parse a PMGL entry into a chmUnitInfo struct; return 1 on success. */
937 static BOOL
_chm_parse_PMGL_entry(UChar
**pEntry
, struct chmUnitInfo
*ui
)
942 strLen
= _chm_parse_cword(pEntry
);
943 if (strLen
> CHM_MAX_PATHLEN
)
947 if (! _chm_parse_UTF8(pEntry
, strLen
, ui
->path
))
951 ui
->space
= (int)_chm_parse_cword(pEntry
);
952 ui
->start
= _chm_parse_cword(pEntry
);
953 ui
->length
= _chm_parse_cword(pEntry
);
957 /* find an exact entry in PMGL; return NULL if we fail */
958 static UChar
*_chm_find_in_PMGL(UChar
*page_buf
,
960 const WCHAR
*objPath
)
962 /* XXX: modify this to do a binary search using the nice index structure
963 * that is provided for us.
965 struct chmPmglHeader header
;
971 WCHAR buffer
[CHM_MAX_PATHLEN
+1];
973 /* figure out where to start and end */
975 hremain
= _CHM_PMGL_LEN
;
976 if (! _unmarshal_pmgl_header(&cur
, &hremain
, &header
))
978 end
= page_buf
+ block_len
- (header
.free_space
);
980 /* now, scan progressively */
985 strLen
= _chm_parse_cword(&cur
);
986 if (! _chm_parse_UTF8(&cur
, strLen
, buffer
))
989 /* check if it is the right name */
990 if (! strcmpiW(buffer
, objPath
))
993 _chm_skip_PMGL_entry_data(&cur
);
999 /* find which block should be searched next for the entry; -1 if no block */
1000 static Int32
_chm_find_in_PMGI(UChar
*page_buf
,
1002 const WCHAR
*objPath
)
1004 /* XXX: modify this to do a binary search using the nice index structure
1005 * that is provided for us
1007 struct chmPmgiHeader header
;
1013 WCHAR buffer
[CHM_MAX_PATHLEN
+1];
1015 /* figure out where to start and end */
1017 hremain
= _CHM_PMGI_LEN
;
1018 if (! _unmarshal_pmgi_header(&cur
, &hremain
, &header
))
1020 end
= page_buf
+ block_len
- (header
.free_space
);
1022 /* now, scan progressively */
1026 strLen
= _chm_parse_cword(&cur
);
1027 if (! _chm_parse_UTF8(&cur
, strLen
, buffer
))
1030 /* check if it is the right name */
1031 if (strcmpiW(buffer
, objPath
) > 0)
1034 /* load next value for path */
1035 page
= (int)_chm_parse_cword(&cur
);
1041 /* resolve a particular object from the archive */
1042 int chm_resolve_object(struct chmFile
*h
,
1043 const WCHAR
*objPath
,
1044 struct chmUnitInfo
*ui
)
1047 * XXX: implement caching scheme for dir pages
1052 /* buffer to hold whatever page we're looking at */
1053 UChar
*page_buf
= HeapAlloc(GetProcessHeap(), 0, h
->block_len
);
1056 curPage
= h
->index_root
;
1058 /* until we have either returned or given up */
1059 while (curPage
!= -1)
1062 /* try to fetch the index page */
1063 if (_chm_fetch_bytes(h
, page_buf
,
1064 h
->dir_offset
+ (UInt64
)curPage
*h
->block_len
,
1065 h
->block_len
) != h
->block_len
)
1067 HeapFree(GetProcessHeap(), 0, page_buf
);
1068 return CHM_RESOLVE_FAILURE
;
1071 /* now, if it is a leaf node: */
1072 if (memcmp(page_buf
, _chm_pmgl_marker
, 4) == 0)
1075 UChar
*pEntry
= _chm_find_in_PMGL(page_buf
,
1080 HeapFree(GetProcessHeap(), 0, page_buf
);
1081 return CHM_RESOLVE_FAILURE
;
1084 /* parse entry and return */
1085 _chm_parse_PMGL_entry(&pEntry
, ui
);
1086 HeapFree(GetProcessHeap(), 0, page_buf
);
1087 return CHM_RESOLVE_SUCCESS
;
1090 /* else, if it is a branch node: */
1091 else if (memcmp(page_buf
, _chm_pmgi_marker
, 4) == 0)
1092 curPage
= _chm_find_in_PMGI(page_buf
, h
->block_len
, objPath
);
1094 /* else, we are confused. give up. */
1097 HeapFree(GetProcessHeap(), 0, page_buf
);
1098 return CHM_RESOLVE_FAILURE
;
1102 /* didn't find anything. fail. */
1103 HeapFree(GetProcessHeap(), 0, page_buf
);
1104 return CHM_RESOLVE_FAILURE
;
1108 * utility methods for dealing with compressed data
1111 /* get the bounds of a compressed block. Returns FALSE on failure */
1112 static BOOL
_chm_get_cmpblock_bounds(struct chmFile
*h
,
1117 UChar buffer
[8], *dummy
;
1120 /* for all but the last block, use the reset table */
1121 if (block
< h
->reset_table
.block_count
-1)
1123 /* unpack the start address */
1126 if (_chm_fetch_bytes(h
, buffer
,
1129 + h
->reset_table
.table_offset
1131 remain
) != remain
||
1132 !_unmarshal_uint64(&dummy
, &remain
, start
))
1135 /* unpack the end address */
1138 if (_chm_fetch_bytes(h
, buffer
,
1141 + h
->reset_table
.table_offset
1143 remain
) != remain
||
1144 !_unmarshal_int64(&dummy
, &remain
, len
))
1148 /* for the last block, use the span in addition to the reset table */
1151 /* unpack the start address */
1154 if (_chm_fetch_bytes(h
, buffer
,
1157 + h
->reset_table
.table_offset
1159 remain
) != remain
||
1160 !_unmarshal_uint64(&dummy
, &remain
, start
))
1163 *len
= h
->reset_table
.compressed_len
;
1166 /* compute the length and absolute start address */
1168 *start
+= h
->data_offset
+ h
->cn_unit
.start
;
1173 /* decompress the block. must have lzx_mutex. */
1174 static Int64
_chm_decompress_block(struct chmFile
*h
,
1178 UChar
*cbuffer
= HeapAlloc( GetProcessHeap(), 0,
1179 ((unsigned int)h
->reset_table
.block_len
+ 6144));
1180 UInt64 cmpStart
; /* compressed start */
1181 Int64 cmpLen
; /* compressed len */
1182 int indexSlot
; /* cache index slot */
1183 UChar
*lbuffer
; /* local buffer ptr */
1184 UInt32 blockAlign
= (UInt32
)(block
% h
->reset_blkcount
); /* reset interval align */
1185 UInt32 i
; /* local loop index */
1187 /* let the caching system pull its weight! */
1188 if (block
- blockAlign
<= h
->lzx_last_block
&&
1189 block
>= h
->lzx_last_block
)
1190 blockAlign
= (block
- h
->lzx_last_block
);
1192 /* check if we need previous blocks */
1193 if (blockAlign
!= 0)
1195 /* fetch all required previous blocks since last reset */
1196 for (i
= blockAlign
; i
> 0; i
--)
1198 UInt32 curBlockIdx
= block
- i
;
1200 /* check if we most recently decompressed the previous block */
1201 if (h
->lzx_last_block
!= curBlockIdx
)
1203 if ((curBlockIdx
% h
->reset_blkcount
) == 0)
1206 fprintf(stderr
, "***RESET (1)***\n");
1208 LZXreset(h
->lzx_state
);
1211 indexSlot
= (int)((curBlockIdx
) % h
->cache_num_blocks
);
1212 h
->cache_block_indices
[indexSlot
] = curBlockIdx
;
1213 if (! h
->cache_blocks
[indexSlot
])
1214 h
->cache_blocks
[indexSlot
] =
1215 HeapAlloc(GetProcessHeap(), 0,
1216 (unsigned int)(h
->reset_table
.block_len
));
1217 lbuffer
= h
->cache_blocks
[indexSlot
];
1219 /* decompress the previous block */
1221 fprintf(stderr
, "Decompressing block #%4d (EXTRA)\n", curBlockIdx
);
1223 if (!_chm_get_cmpblock_bounds(h
, curBlockIdx
, &cmpStart
, &cmpLen
) ||
1224 _chm_fetch_bytes(h
, cbuffer
, cmpStart
, cmpLen
) != cmpLen
||
1225 LZXdecompress(h
->lzx_state
, cbuffer
, lbuffer
, (int)cmpLen
,
1226 (int)h
->reset_table
.block_len
) != DECR_OK
)
1229 fprintf(stderr
, " (DECOMPRESS FAILED!)\n");
1231 HeapFree(GetProcessHeap(), 0, cbuffer
);
1235 h
->lzx_last_block
= (int)curBlockIdx
;
1241 if ((block
% h
->reset_blkcount
) == 0)
1244 fprintf(stderr
, "***RESET (2)***\n");
1246 LZXreset(h
->lzx_state
);
1250 /* allocate slot in cache */
1251 indexSlot
= (int)(block
% h
->cache_num_blocks
);
1252 h
->cache_block_indices
[indexSlot
] = block
;
1253 if (! h
->cache_blocks
[indexSlot
])
1254 h
->cache_blocks
[indexSlot
] =
1255 HeapAlloc(GetProcessHeap(), 0, ((unsigned int)h
->reset_table
.block_len
));
1256 lbuffer
= h
->cache_blocks
[indexSlot
];
1259 /* decompress the block we actually want */
1261 fprintf(stderr
, "Decompressing block #%4d (REAL )\n", block
);
1263 if (! _chm_get_cmpblock_bounds(h
, block
, &cmpStart
, &cmpLen
) ||
1264 _chm_fetch_bytes(h
, cbuffer
, cmpStart
, cmpLen
) != cmpLen
||
1265 LZXdecompress(h
->lzx_state
, cbuffer
, lbuffer
, (int)cmpLen
,
1266 (int)h
->reset_table
.block_len
) != DECR_OK
)
1269 fprintf(stderr
, " (DECOMPRESS FAILED!)\n");
1271 HeapFree(GetProcessHeap(), 0, cbuffer
);
1274 h
->lzx_last_block
= (int)block
;
1276 /* XXX: modify LZX routines to return the length of the data they
1277 * decompressed and return that instead, for an extra sanity check.
1279 HeapFree(GetProcessHeap(), 0, cbuffer
);
1280 return h
->reset_table
.block_len
;
1283 /* grab a region from a compressed block */
1284 static Int64
_chm_decompress_region(struct chmFile
*h
,
1289 UInt64 nBlock
, nOffset
;
1292 UChar
*ubuffer
= NULL
;
1297 /* figure out what we need to read */
1298 nBlock
= start
/ h
->reset_table
.block_len
;
1299 nOffset
= start
% h
->reset_table
.block_len
;
1301 if (nLen
> (h
->reset_table
.block_len
- nOffset
))
1302 nLen
= h
->reset_table
.block_len
- nOffset
;
1304 /* if block is cached, return data from it. */
1305 CHM_ACQUIRE_LOCK(h
->lzx_mutex
);
1306 CHM_ACQUIRE_LOCK(h
->cache_mutex
);
1307 if (h
->cache_block_indices
[nBlock
% h
->cache_num_blocks
] == nBlock
&&
1308 h
->cache_blocks
[nBlock
% h
->cache_num_blocks
] != NULL
)
1311 h
->cache_blocks
[nBlock
% h
->cache_num_blocks
] + nOffset
,
1312 (unsigned int)nLen
);
1313 CHM_RELEASE_LOCK(h
->cache_mutex
);
1314 CHM_RELEASE_LOCK(h
->lzx_mutex
);
1317 CHM_RELEASE_LOCK(h
->cache_mutex
);
1319 /* data request not satisfied, so... start up the decompressor machine */
1322 int window_size
= ffs(h
->window_size
) - 1;
1323 h
->lzx_last_block
= -1;
1324 h
->lzx_state
= LZXinit(window_size
);
1327 /* decompress some data */
1328 gotLen
= _chm_decompress_block(h
, nBlock
, &ubuffer
);
1331 memcpy(buf
, ubuffer
+nOffset
, (unsigned int)nLen
);
1332 CHM_RELEASE_LOCK(h
->lzx_mutex
);
1336 /* retrieve (part of) an object */
1337 LONGINT64
chm_retrieve_object(struct chmFile
*h
,
1338 struct chmUnitInfo
*ui
,
1343 /* must be valid file handle */
1347 /* starting address must be in correct range */
1348 if (addr
>= ui
->length
)
1352 if (addr
+ len
> ui
->length
)
1353 len
= ui
->length
- addr
;
1355 /* if the file is uncompressed, it's simple */
1356 if (ui
->space
== CHM_UNCOMPRESSED
)
1359 return _chm_fetch_bytes(h
,
1361 h
->data_offset
+ ui
->start
+ addr
,
1365 /* else if the file is compressed, it's a little trickier */
1366 else /* ui->space == CHM_COMPRESSED */
1368 Int64 swath
=0, total
=0;
1370 /* if compression is not enabled for this file... */
1371 if (! h
->compression_enabled
)
1376 /* swill another mouthful */
1377 swath
= _chm_decompress_region(h
, buf
, ui
->start
+ addr
, len
);
1379 /* if we didn't get any... */
1395 BOOL
chm_enumerate_dir(struct chmFile
*h
,
1396 const WCHAR
*prefix
,
1402 * XXX: do this efficiently (i.e. using the tree index)
1407 /* buffer to hold whatever page we're looking at */
1408 UChar
*page_buf
= HeapAlloc(GetProcessHeap(), 0, h
->block_len
);
1409 struct chmPmglHeader header
;
1412 unsigned int lenRemain
;
1414 /* set to TRUE once we've started */
1415 BOOL it_has_begun
= FALSE
;
1417 /* the current ui */
1418 struct chmUnitInfo ui
;
1422 /* the length of the prefix */
1423 WCHAR prefixRectified
[CHM_MAX_PATHLEN
+1];
1425 WCHAR lastPath
[CHM_MAX_PATHLEN
];
1429 curPage
= h
->index_head
;
1431 /* initialize pathname state */
1432 lstrcpynW(prefixRectified
, prefix
, CHM_MAX_PATHLEN
);
1433 prefixLen
= strlenW(prefixRectified
);
1436 if (prefixRectified
[prefixLen
-1] != '/')
1438 prefixRectified
[prefixLen
] = '/';
1439 prefixRectified
[prefixLen
+1] = '\0';
1446 /* until we have either returned or given up */
1447 while (curPage
!= -1)
1450 /* try to fetch the index page */
1451 if (_chm_fetch_bytes(h
,
1453 h
->dir_offset
+ (UInt64
)curPage
*h
->block_len
,
1454 h
->block_len
) != h
->block_len
)
1456 HeapFree(GetProcessHeap(), 0, page_buf
);
1460 /* figure out start and end for this page */
1462 lenRemain
= _CHM_PMGL_LEN
;
1463 if (! _unmarshal_pmgl_header(&cur
, &lenRemain
, &header
))
1465 HeapFree(GetProcessHeap(), 0, page_buf
);
1468 end
= page_buf
+ h
->block_len
- (header
.free_space
);
1470 /* loop over this page */
1473 if (! _chm_parse_PMGL_entry(&cur
, &ui
))
1475 HeapFree(GetProcessHeap(), 0, page_buf
);
1479 /* check if we should start */
1482 if (ui
.length
== 0 && strncmpiW(ui
.path
, prefixRectified
, prefixLen
) == 0)
1483 it_has_begun
= TRUE
;
1487 if (ui
.path
[prefixLen
] == '\0')
1491 /* check if we should stop */
1494 if (strncmpiW(ui
.path
, prefixRectified
, prefixLen
) != 0)
1496 HeapFree(GetProcessHeap(), 0, page_buf
);
1501 /* check if we should include this path */
1502 if (lastPathLen
!= -1)
1504 if (strncmpiW(ui
.path
, lastPath
, lastPathLen
) == 0)
1507 strcpyW(lastPath
, ui
.path
);
1508 lastPathLen
= strlenW(lastPath
);
1510 /* get the length of the path */
1511 ui_path_len
= strlenW(ui
.path
)-1;
1513 /* check for DIRS */
1514 if (ui
.path
[ui_path_len
] == '/' && !(what
& CHM_ENUMERATE_DIRS
))
1517 /* check for FILES */
1518 if (ui
.path
[ui_path_len
] != '/' && !(what
& CHM_ENUMERATE_FILES
))
1521 /* check for NORMAL vs. META */
1522 if (ui
.path
[0] == '/')
1525 /* check for NORMAL vs. SPECIAL */
1526 if (ui
.path
[1] == '#' || ui
.path
[1] == '$')
1527 flag
= CHM_ENUMERATE_SPECIAL
;
1529 flag
= CHM_ENUMERATE_NORMAL
;
1532 flag
= CHM_ENUMERATE_META
;
1533 if (! (what
& flag
))
1536 /* call the enumerator */
1538 int status
= (*e
)(h
, &ui
, context
);
1541 case CHM_ENUMERATOR_FAILURE
:
1542 HeapFree(GetProcessHeap(), 0, page_buf
);
1544 case CHM_ENUMERATOR_CONTINUE
:
1546 case CHM_ENUMERATOR_SUCCESS
:
1547 HeapFree(GetProcessHeap(), 0, page_buf
);
1555 /* advance to next page */
1556 curPage
= header
.block_next
;
1559 HeapFree(GetProcessHeap(), 0, page_buf
);