dinput: Move few helper functions and standardize them.
[wine] / dlls / itss / chm_lib.c
1 /***************************************************************************
2  *             chm_lib.c - CHM archive manipulation routines               *
3  *                           -------------------                           *
4  *                                                                         *
5  *  author:     Jed Wing <jedwin@ugcs.caltech.edu>                         *
6  *  version:    0.3                                                        *
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.                   *
11  *                                                                         *
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.               *
30  *                                                                         *
31  ***************************************************************************/
32
33 /***************************************************************************
34  *                                                                         *
35  *   This library is free software; you can redistribute it and/or modify  *
36  *   it under the terms of the GNU Lesser General Public License as        *
37  *   published by the Free Software Foundation; either version 2.1 of the  *
38  *   License, or (at your option) any later version.                       *
39  *                                                                         *
40  ***************************************************************************/
41
42 /***************************************************************************
43  *                                                                         *
44  * Adapted for Wine by Mike McCormack                                      *
45  *                                                                         *
46  ***************************************************************************/
47
48 #include "config.h"
49 #include "wine/port.h"
50
51 #include <stdarg.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55
56 #include "windef.h"
57 #include "winbase.h"
58 #include "wine/unicode.h"
59
60 #include "chm_lib.h"
61 #include "lzx.h"
62
63 #define CHM_ACQUIRE_LOCK(a) do {                        \
64         EnterCriticalSection(&(a));                     \
65     } while(0)
66 #define CHM_RELEASE_LOCK(a) do {                        \
67         LeaveCriticalSection(&(a));                     \
68     } while(0)
69
70 #define CHM_NULL_FD (INVALID_HANDLE_VALUE)
71 #define CHM_CLOSE_FILE(fd) CloseHandle((fd))
72
73 /*
74  * defines related to tuning
75  */
76 #ifndef CHM_MAX_BLOCKS_CACHED
77 #define CHM_MAX_BLOCKS_CACHED 5
78 #endif
79
80 /*
81  * architecture specific defines
82  *
83  * Note: as soon as C99 is more widespread, the below defines should
84  * probably just use the C99 sized-int types.
85  *
86  * The following settings will probably work for many platforms.  The sizes
87  * don't have to be exactly correct, but the types must accommodate at least as
88  * many bits as they specify.
89  */
90
91 /* i386, 32-bit, Windows */
92 typedef BYTE   UChar;
93 typedef SHORT  Int16;
94 typedef USHORT UInt16;
95 typedef LONG   Int32;
96 typedef DWORD      UInt32;
97 typedef LONGLONG   Int64;
98 typedef ULONGLONG  UInt64;
99
100 /* utilities for unmarshalling data */
101 static int _unmarshal_char_array(unsigned char **pData,
102                                  unsigned int *pLenRemain,
103                                  char *dest,
104                                  int count)
105 {
106     if (count <= 0  ||  (unsigned int)count > *pLenRemain)
107         return 0;
108     memcpy(dest, (*pData), count);
109     *pData += count;
110     *pLenRemain -= count;
111     return 1;
112 }
113
114 static int _unmarshal_uchar_array(unsigned char **pData,
115                                   unsigned int *pLenRemain,
116                                   unsigned char *dest,
117                                   int count)
118 {
119         if (count <= 0  ||  (unsigned int)count > *pLenRemain)
120         return 0;
121     memcpy(dest, (*pData), count);
122     *pData += count;
123     *pLenRemain -= count;
124     return 1;
125 }
126
127 static int _unmarshal_int32(unsigned char **pData,
128                             unsigned int *pLenRemain,
129                             Int32 *dest)
130 {
131     if (4 > *pLenRemain)
132         return 0;
133     *dest = (*pData)[0] | (*pData)[1]<<8 | (*pData)[2]<<16 | (*pData)[3]<<24;
134     *pData += 4;
135     *pLenRemain -= 4;
136     return 1;
137 }
138
139 static int _unmarshal_uint32(unsigned char **pData,
140                              unsigned int *pLenRemain,
141                              UInt32 *dest)
142 {
143     if (4 > *pLenRemain)
144         return 0;
145     *dest = (*pData)[0] | (*pData)[1]<<8 | (*pData)[2]<<16 | (*pData)[3]<<24;
146     *pData += 4;
147     *pLenRemain -= 4;
148     return 1;
149 }
150
151 static int _unmarshal_int64(unsigned char **pData,
152                             unsigned int *pLenRemain,
153                             Int64 *dest)
154 {
155     Int64 temp;
156     int i;
157     if (8 > *pLenRemain)
158         return 0;
159     temp=0;
160     for(i=8; i>0; i--)
161     {
162         temp <<= 8;
163         temp |= (*pData)[i-1];
164     }
165     *dest = temp;
166     *pData += 8;
167     *pLenRemain -= 8;
168     return 1;
169 }
170
171 static int _unmarshal_uint64(unsigned char **pData,
172                              unsigned int *pLenRemain,
173                              UInt64 *dest)
174 {
175     UInt64 temp;
176     int i;
177     if (8 > *pLenRemain)
178         return 0;
179     temp=0;
180     for(i=8; i>0; i--)
181     {
182         temp <<= 8;
183         temp |= (*pData)[i-1];
184     }
185     *dest = temp;
186     *pData += 8;
187     *pLenRemain -= 8;
188     return 1;
189 }
190
191 static int _unmarshal_uuid(unsigned char **pData,
192                            unsigned int *pDataLen,
193                            unsigned char *dest)
194 {
195     return _unmarshal_uchar_array(pData, pDataLen, dest, 16);
196 }
197
198 /* names of sections essential to decompression */
199 static const WCHAR _CHMU_RESET_TABLE[] = {
200 ':',':','D','a','t','a','S','p','a','c','e','/',
201         'S','t','o','r','a','g','e','/',
202         'M','S','C','o','m','p','r','e','s','s','e','d','/',
203         'T','r','a','n','s','f','o','r','m','/',
204         '{','7','F','C','2','8','9','4','0','-','9','D','3','1',
205           '-','1','1','D','0','-','9','B','2','7','-',
206           '0','0','A','0','C','9','1','E','9','C','7','C','}','/',
207         'I','n','s','t','a','n','c','e','D','a','t','a','/',
208         'R','e','s','e','t','T','a','b','l','e',0
209 };
210 static const WCHAR _CHMU_LZXC_CONTROLDATA[] = {
211 ':',':','D','a','t','a','S','p','a','c','e','/',
212         'S','t','o','r','a','g','e','/',
213         'M','S','C','o','m','p','r','e','s','s','e','d','/',
214         'C','o','n','t','r','o','l','D','a','t','a',0
215 };
216 static const WCHAR _CHMU_CONTENT[] = {
217 ':',':','D','a','t','a','S','p','a','c','e','/',
218         'S','t','o','r','a','g','e','/',
219         'M','S','C','o','m','p','r','e','s','s','e','d','/',
220         'C','o','n','t','e','n','t',0
221 };
222 static const WCHAR _CHMU_SPANINFO[] = {
223 ':',':','D','a','t','a','S','p','a','c','e','/',
224         'S','t','o','r','a','g','e','/',
225         'M','S','C','o','m','p','r','e','s','s','e','d','/',
226         'S','p','a','n','I','n','f','o',
227 };
228
229 /*
230  * structures local to this module
231  */
232
233 /* structure of ITSF headers */
234 #define _CHM_ITSF_V2_LEN (0x58)
235 #define _CHM_ITSF_V3_LEN (0x60)
236 struct chmItsfHeader
237 {
238     char        signature[4];           /*  0 (ITSF) */
239     Int32       version;                /*  4 */
240     Int32       header_len;             /*  8 */
241     Int32       unknown_000c;           /*  c */
242     UInt32      last_modified;          /* 10 */
243     UInt32      lang_id;                /* 14 */
244     UChar       dir_uuid[16];           /* 18 */
245     UChar       stream_uuid[16];        /* 28 */
246     UInt64      unknown_offset;         /* 38 */
247     UInt64      unknown_len;            /* 40 */
248     UInt64      dir_offset;             /* 48 */
249     UInt64      dir_len;                /* 50 */
250     UInt64      data_offset;            /* 58 (Not present before V3) */
251 }; /* __attribute__ ((aligned (1))); */
252
253 static int _unmarshal_itsf_header(unsigned char **pData,
254                                   unsigned int *pDataLen,
255                                   struct chmItsfHeader *dest)
256 {
257     /* we only know how to deal with the 0x58 and 0x60 byte structures */
258     if (*pDataLen != _CHM_ITSF_V2_LEN  &&  *pDataLen != _CHM_ITSF_V3_LEN)
259         return 0;
260
261     /* unmarshal common fields */
262     _unmarshal_char_array(pData, pDataLen,  dest->signature, 4);
263     _unmarshal_int32     (pData, pDataLen, &dest->version);
264     _unmarshal_int32     (pData, pDataLen, &dest->header_len);
265     _unmarshal_int32     (pData, pDataLen, &dest->unknown_000c);
266     _unmarshal_uint32    (pData, pDataLen, &dest->last_modified);
267     _unmarshal_uint32    (pData, pDataLen, &dest->lang_id);
268     _unmarshal_uuid      (pData, pDataLen,  dest->dir_uuid);
269     _unmarshal_uuid      (pData, pDataLen,  dest->stream_uuid);
270     _unmarshal_uint64    (pData, pDataLen, &dest->unknown_offset);
271     _unmarshal_uint64    (pData, pDataLen, &dest->unknown_len);
272     _unmarshal_uint64    (pData, pDataLen, &dest->dir_offset);
273     _unmarshal_uint64    (pData, pDataLen, &dest->dir_len);
274
275     /* error check the data */
276     /* XXX: should also check UUIDs, probably, though with a version 3 file,
277      * current MS tools do not seem to use them.
278      */
279     if (memcmp(dest->signature, "ITSF", 4) != 0)
280         return 0;
281     if (dest->version == 2)
282     {
283         if (dest->header_len < _CHM_ITSF_V2_LEN)
284             return 0;
285     }
286     else if (dest->version == 3)
287     {
288         if (dest->header_len < _CHM_ITSF_V3_LEN)
289             return 0;
290     }
291     else
292         return 0;
293
294     /* now, if we have a V3 structure, unmarshal the rest.
295      * otherwise, compute it
296      */
297     if (dest->version == 3)
298     {
299         if (*pDataLen != 0)
300             _unmarshal_uint64(pData, pDataLen, &dest->data_offset);
301         else
302             return 0;
303     }
304     else
305         dest->data_offset = dest->dir_offset + dest->dir_len;
306
307     return 1;
308 }
309
310 /* structure of ITSP headers */
311 #define _CHM_ITSP_V1_LEN (0x54)
312 struct chmItspHeader
313 {
314     char        signature[4];           /*  0 (ITSP) */
315     Int32       version;                /*  4 */
316     Int32       header_len;             /*  8 */
317     Int32       unknown_000c;           /*  c */
318     UInt32      block_len;              /* 10 */
319     Int32       blockidx_intvl;         /* 14 */
320     Int32       index_depth;            /* 18 */
321     Int32       index_root;             /* 1c */
322     Int32       index_head;             /* 20 */
323     Int32       unknown_0024;           /* 24 */
324     UInt32      num_blocks;             /* 28 */
325     Int32       unknown_002c;           /* 2c */
326     UInt32      lang_id;                /* 30 */
327     UChar       system_uuid[16];        /* 34 */
328     UChar       unknown_0044[16];       /* 44 */
329 }; /* __attribute__ ((aligned (1))); */
330
331 static int _unmarshal_itsp_header(unsigned char **pData,
332                                   unsigned int *pDataLen,
333                                   struct chmItspHeader *dest)
334 {
335     /* we only know how to deal with a 0x54 byte structures */
336     if (*pDataLen != _CHM_ITSP_V1_LEN)
337         return 0;
338
339     /* unmarshal fields */
340     _unmarshal_char_array(pData, pDataLen,  dest->signature, 4);
341     _unmarshal_int32     (pData, pDataLen, &dest->version);
342     _unmarshal_int32     (pData, pDataLen, &dest->header_len);
343     _unmarshal_int32     (pData, pDataLen, &dest->unknown_000c);
344     _unmarshal_uint32    (pData, pDataLen, &dest->block_len);
345     _unmarshal_int32     (pData, pDataLen, &dest->blockidx_intvl);
346     _unmarshal_int32     (pData, pDataLen, &dest->index_depth);
347     _unmarshal_int32     (pData, pDataLen, &dest->index_root);
348     _unmarshal_int32     (pData, pDataLen, &dest->index_head);
349     _unmarshal_int32     (pData, pDataLen, &dest->unknown_0024);
350     _unmarshal_uint32    (pData, pDataLen, &dest->num_blocks);
351     _unmarshal_int32     (pData, pDataLen, &dest->unknown_002c);
352     _unmarshal_uint32    (pData, pDataLen, &dest->lang_id);
353     _unmarshal_uuid      (pData, pDataLen,  dest->system_uuid);
354     _unmarshal_uchar_array(pData, pDataLen, dest->unknown_0044, 16);
355
356     /* error check the data */
357     if (memcmp(dest->signature, "ITSP", 4) != 0)
358         return 0;
359     if (dest->version != 1)
360         return 0;
361     if (dest->header_len != _CHM_ITSP_V1_LEN)
362         return 0;
363
364     return 1;
365 }
366
367 /* structure of PMGL headers */
368 static const char _chm_pmgl_marker[4] = "PMGL";
369 #define _CHM_PMGL_LEN (0x14)
370 struct chmPmglHeader
371 {
372     char        signature[4];           /*  0 (PMGL) */
373     UInt32      free_space;             /*  4 */
374     UInt32      unknown_0008;           /*  8 */
375     Int32       block_prev;             /*  c */
376     Int32       block_next;             /* 10 */
377 }; /* __attribute__ ((aligned (1))); */
378
379 static int _unmarshal_pmgl_header(unsigned char **pData,
380                                   unsigned int *pDataLen,
381                                   struct chmPmglHeader *dest)
382 {
383     /* we only know how to deal with a 0x14 byte structures */
384     if (*pDataLen != _CHM_PMGL_LEN)
385         return 0;
386
387     /* unmarshal fields */
388     _unmarshal_char_array(pData, pDataLen,  dest->signature, 4);
389     _unmarshal_uint32    (pData, pDataLen, &dest->free_space);
390     _unmarshal_uint32    (pData, pDataLen, &dest->unknown_0008);
391     _unmarshal_int32     (pData, pDataLen, &dest->block_prev);
392     _unmarshal_int32     (pData, pDataLen, &dest->block_next);
393
394     /* check structure */
395     if (memcmp(dest->signature, _chm_pmgl_marker, 4) != 0)
396         return 0;
397
398     return 1;
399 }
400
401 /* structure of PMGI headers */
402 static const char _chm_pmgi_marker[4] = "PMGI";
403 #define _CHM_PMGI_LEN (0x08)
404 struct chmPmgiHeader
405 {
406     char        signature[4];           /*  0 (PMGI) */
407     UInt32      free_space;             /*  4 */
408 }; /* __attribute__ ((aligned (1))); */
409
410 static int _unmarshal_pmgi_header(unsigned char **pData,
411                                   unsigned int *pDataLen,
412                                   struct chmPmgiHeader *dest)
413 {
414     /* we only know how to deal with a 0x8 byte structures */
415     if (*pDataLen != _CHM_PMGI_LEN)
416         return 0;
417
418     /* unmarshal fields */
419     _unmarshal_char_array(pData, pDataLen,  dest->signature, 4);
420     _unmarshal_uint32    (pData, pDataLen, &dest->free_space);
421
422     /* check structure */
423     if (memcmp(dest->signature, _chm_pmgi_marker, 4) != 0)
424         return 0;
425
426     return 1;
427 }
428
429 /* structure of LZXC reset table */
430 #define _CHM_LZXC_RESETTABLE_V1_LEN (0x28)
431 struct chmLzxcResetTable
432 {
433     UInt32      version;
434     UInt32      block_count;
435     UInt32      unknown;
436     UInt32      table_offset;
437     UInt64      uncompressed_len;
438     UInt64      compressed_len;
439     UInt64      block_len;     
440 }; /* __attribute__ ((aligned (1))); */
441
442 static int _unmarshal_lzxc_reset_table(unsigned char **pData,
443                                        unsigned int *pDataLen,
444                                        struct chmLzxcResetTable *dest)
445 {
446     /* we only know how to deal with a 0x28 byte structures */
447     if (*pDataLen != _CHM_LZXC_RESETTABLE_V1_LEN)
448         return 0;
449
450     /* unmarshal fields */
451     _unmarshal_uint32    (pData, pDataLen, &dest->version);
452     _unmarshal_uint32    (pData, pDataLen, &dest->block_count);
453     _unmarshal_uint32    (pData, pDataLen, &dest->unknown);
454     _unmarshal_uint32    (pData, pDataLen, &dest->table_offset);
455     _unmarshal_uint64    (pData, pDataLen, &dest->uncompressed_len);
456     _unmarshal_uint64    (pData, pDataLen, &dest->compressed_len);
457     _unmarshal_uint64    (pData, pDataLen, &dest->block_len);
458
459     /* check structure */
460     if (dest->version != 2)
461         return 0;
462
463     return 1;
464 }
465
466 /* structure of LZXC control data block */
467 #define _CHM_LZXC_MIN_LEN (0x18)
468 #define _CHM_LZXC_V2_LEN (0x1c)
469 struct chmLzxcControlData
470 {
471     UInt32      size;                   /*  0        */
472     char        signature[4];           /*  4 (LZXC) */
473     UInt32      version;                /*  8        */
474     UInt32      resetInterval;          /*  c        */
475     UInt32      windowSize;             /* 10        */
476     UInt32      windowsPerReset;        /* 14        */
477     UInt32      unknown_18;             /* 18        */
478 };
479
480 static int _unmarshal_lzxc_control_data(unsigned char **pData,
481                                         unsigned int *pDataLen,
482                                         struct chmLzxcControlData *dest)
483 {
484     /* we want at least 0x18 bytes */
485     if (*pDataLen < _CHM_LZXC_MIN_LEN)
486         return 0;
487
488     /* unmarshal fields */
489     _unmarshal_uint32    (pData, pDataLen, &dest->size);
490     _unmarshal_char_array(pData, pDataLen,  dest->signature, 4);
491     _unmarshal_uint32    (pData, pDataLen, &dest->version);
492     _unmarshal_uint32    (pData, pDataLen, &dest->resetInterval);
493     _unmarshal_uint32    (pData, pDataLen, &dest->windowSize);
494     _unmarshal_uint32    (pData, pDataLen, &dest->windowsPerReset);
495
496     if (*pDataLen >= _CHM_LZXC_V2_LEN)
497         _unmarshal_uint32    (pData, pDataLen, &dest->unknown_18);
498     else
499         dest->unknown_18 = 0;
500
501     if (dest->version == 2)
502     {
503         dest->resetInterval *= 0x8000;
504         dest->windowSize *= 0x8000;
505     }
506     if (dest->windowSize == 0  ||  dest->resetInterval == 0)
507         return 0;
508
509     /* for now, only support resetInterval a multiple of windowSize/2 */
510     if (dest->windowSize == 1)
511         return 0;
512     if ((dest->resetInterval % (dest->windowSize/2)) != 0)
513         return 0;
514
515     /* check structure */
516     if (memcmp(dest->signature, "LZXC", 4) != 0)
517         return 0;
518
519     return 1;
520 }
521
522 /* the structure used for chm file handles */
523 struct chmFile
524 {
525     HANDLE              fd;
526
527     CRITICAL_SECTION    mutex;
528     CRITICAL_SECTION    lzx_mutex;
529     CRITICAL_SECTION    cache_mutex;
530
531     UInt64              dir_offset;
532     UInt64              dir_len;    
533     UInt64              data_offset;
534     Int32               index_root;
535     Int32               index_head;
536     UInt32              block_len;     
537
538     UInt64              span;
539     struct chmUnitInfo  rt_unit;
540     struct chmUnitInfo  cn_unit;
541     struct chmLzxcResetTable reset_table;
542
543     /* LZX control data */
544     int                 compression_enabled;
545     UInt32              window_size;
546     UInt32              reset_interval;
547     UInt32              reset_blkcount;
548
549     /* decompressor state */
550     struct LZXstate    *lzx_state;
551     int                 lzx_last_block;
552
553     /* cache for decompressed blocks */
554     UChar             **cache_blocks;
555     Int64              *cache_block_indices;
556     Int32               cache_num_blocks;
557 };
558
559 /*
560  * utility functions local to this module
561  */
562
563 /* utility function to handle differences between {pread,read}(64)? */
564 static Int64 _chm_fetch_bytes(struct chmFile *h,
565                               UChar *buf,
566                               UInt64 os,
567                               Int64 len)
568 {
569     Int64 readLen=0;
570     if (h->fd  ==  CHM_NULL_FD)
571         return readLen;
572
573     CHM_ACQUIRE_LOCK(h->mutex);
574     /* NOTE: this might be better done with CreateFileMapping, et cetera... */
575     {
576         LARGE_INTEGER old_pos, new_pos;
577         DWORD actualLen=0;
578
579         /* awkward Win32 Seek/Tell */
580         new_pos.QuadPart = 0;
581         SetFilePointerEx( h->fd, new_pos, &old_pos, FILE_CURRENT );
582         new_pos.QuadPart = os;
583         SetFilePointerEx( h->fd, new_pos, NULL, FILE_BEGIN );
584
585         /* read the data */
586         if (ReadFile(h->fd,
587                      buf,
588                      (DWORD)len,
589                      &actualLen,
590                      NULL))
591             readLen = actualLen;
592         else
593             readLen = 0;
594
595         /* restore original position */
596         SetFilePointerEx( h->fd, old_pos, NULL, FILE_BEGIN );
597     }
598     CHM_RELEASE_LOCK(h->mutex);
599     return readLen;
600 }
601
602 /* open an ITS archive */
603 struct chmFile *chm_openW(const WCHAR *filename)
604 {
605     unsigned char               sbuffer[256];
606     unsigned int                sremain;
607     unsigned char              *sbufpos;
608     struct chmFile             *newHandle=NULL;
609     struct chmItsfHeader        itsfHeader;
610     struct chmItspHeader        itspHeader;
611 #if 0
612     struct chmUnitInfo          uiSpan;
613 #endif
614     struct chmUnitInfo          uiLzxc;
615     struct chmLzxcControlData   ctlData;
616
617     /* allocate handle */
618     newHandle = malloc(sizeof(struct chmFile));
619     newHandle->fd = CHM_NULL_FD;
620     newHandle->lzx_state = NULL;
621     newHandle->cache_blocks = NULL;
622     newHandle->cache_block_indices = NULL;
623     newHandle->cache_num_blocks = 0;
624
625     /* open file */
626     if ((newHandle->fd=CreateFileW(filename,
627                                    GENERIC_READ,
628                                    FILE_SHARE_READ,
629                                    NULL,
630                                    OPEN_EXISTING,
631                                    FILE_ATTRIBUTE_NORMAL,
632                                    NULL)) == CHM_NULL_FD)
633     {
634         free(newHandle);
635         return NULL;
636     }
637
638     /* initialize mutexes, if needed */
639     InitializeCriticalSection(&newHandle->mutex);
640     InitializeCriticalSection(&newHandle->lzx_mutex);
641     InitializeCriticalSection(&newHandle->cache_mutex);
642
643     /* read and verify header */
644     sremain = _CHM_ITSF_V3_LEN;
645     sbufpos = sbuffer;
646     if (_chm_fetch_bytes(newHandle, sbuffer, (UInt64)0, sremain) != sremain    ||
647         !_unmarshal_itsf_header(&sbufpos, &sremain, &itsfHeader))
648     {
649         chm_close(newHandle);
650         return NULL;
651     }
652
653     /* stash important values from header */
654     newHandle->dir_offset  = itsfHeader.dir_offset;
655     newHandle->dir_len     = itsfHeader.dir_len;
656     newHandle->data_offset = itsfHeader.data_offset;
657
658     /* now, read and verify the directory header chunk */
659     sremain = _CHM_ITSP_V1_LEN;
660     sbufpos = sbuffer;
661     if (_chm_fetch_bytes(newHandle, sbuffer,
662                          (UInt64)itsfHeader.dir_offset, sremain) != sremain       ||
663         !_unmarshal_itsp_header(&sbufpos, &sremain, &itspHeader))
664     {
665         chm_close(newHandle);
666         return NULL;
667     }
668
669     /* grab essential information from ITSP header */
670     newHandle->dir_offset += itspHeader.header_len;
671     newHandle->dir_len    -= itspHeader.header_len;
672     newHandle->index_root  = itspHeader.index_root;
673     newHandle->index_head  = itspHeader.index_head;
674     newHandle->block_len   = itspHeader.block_len;
675
676     /* if the index root is -1, this means we don't have any PMGI blocks.
677      * as a result, we must use the sole PMGL block as the index root
678      */
679     if (newHandle->index_root == -1)
680         newHandle->index_root = newHandle->index_head;
681
682     /* By default, compression is enabled. */
683     newHandle->compression_enabled = 1;
684
685 /* Jed, Sun Jun 27: 'span' doesn't seem to be used anywhere?! */
686 #if 0
687     /* fetch span */
688     if (CHM_RESOLVE_SUCCESS != chm_resolve_object(newHandle,
689                                                   _CHMU_SPANINFO,
690                                                   &uiSpan)                ||
691         uiSpan.space == CHM_COMPRESSED)
692     {
693         chm_close(newHandle);
694         return NULL;
695     }
696
697     /* N.B.: we've already checked that uiSpan is in the uncompressed section,
698      *       so this should not require attempting to decompress, which may
699      *       rely on having a valid "span"
700      */
701     sremain = 8;
702     sbufpos = sbuffer;
703     if (chm_retrieve_object(newHandle, &uiSpan, sbuffer,
704                             0, sremain) != sremain                        ||
705         !_unmarshal_uint64(&sbufpos, &sremain, &newHandle->span))
706     {
707         chm_close(newHandle);
708         return NULL;
709     }
710 #endif
711
712     /* prefetch most commonly needed unit infos */
713     if (CHM_RESOLVE_SUCCESS != chm_resolve_object(newHandle,
714                                                   _CHMU_RESET_TABLE,
715                                                   &newHandle->rt_unit)    ||
716         newHandle->rt_unit.space == CHM_COMPRESSED                        ||
717         CHM_RESOLVE_SUCCESS != chm_resolve_object(newHandle,
718                                                   _CHMU_CONTENT,
719                                                   &newHandle->cn_unit)    ||
720         newHandle->cn_unit.space == CHM_COMPRESSED                        ||
721         CHM_RESOLVE_SUCCESS != chm_resolve_object(newHandle,
722                                                   _CHMU_LZXC_CONTROLDATA,
723                                                   &uiLzxc)                ||
724         uiLzxc.space == CHM_COMPRESSED)
725     {
726         newHandle->compression_enabled = 0;
727     }
728
729     /* read reset table info */
730     if (newHandle->compression_enabled)
731     {
732         sremain = _CHM_LZXC_RESETTABLE_V1_LEN;
733         sbufpos = sbuffer;
734         if (chm_retrieve_object(newHandle, &newHandle->rt_unit, sbuffer,
735                                 0, sremain) != sremain                        ||
736             !_unmarshal_lzxc_reset_table(&sbufpos, &sremain,
737                                          &newHandle->reset_table))
738         {
739             newHandle->compression_enabled = 0;
740         }
741     }
742
743     /* read control data */
744     if (newHandle->compression_enabled)
745     {
746         sremain = (unsigned long)uiLzxc.length;
747         sbufpos = sbuffer;
748         if (chm_retrieve_object(newHandle, &uiLzxc, sbuffer,
749                                 0, sremain) != sremain                       ||
750             !_unmarshal_lzxc_control_data(&sbufpos, &sremain,
751                                           &ctlData))
752         {
753             newHandle->compression_enabled = 0;
754         }
755
756         newHandle->window_size = ctlData.windowSize;
757         newHandle->reset_interval = ctlData.resetInterval;
758
759 /* Jed, Mon Jun 28: Experimentally, it appears that the reset block count */
760 /*       must be multiplied by this formerly unknown ctrl data field in   */
761 /*       order to decompress some files.                                  */
762 #if 0
763         newHandle->reset_blkcount = newHandle->reset_interval /
764                     (newHandle->window_size / 2);
765 #else
766         newHandle->reset_blkcount = newHandle->reset_interval    /
767                                     (newHandle->window_size / 2) *
768                                     ctlData.windowsPerReset;
769 #endif
770     }
771
772     /* initialize cache */
773     chm_set_param(newHandle, CHM_PARAM_MAX_BLOCKS_CACHED,
774                   CHM_MAX_BLOCKS_CACHED);
775
776     return newHandle;
777 }
778
779 /* close an ITS archive */
780 void chm_close(struct chmFile *h)
781 {
782     if (h != NULL)
783     {
784         if (h->fd != CHM_NULL_FD)
785             CHM_CLOSE_FILE(h->fd);
786         h->fd = CHM_NULL_FD;
787
788         DeleteCriticalSection(&h->mutex);
789         DeleteCriticalSection(&h->lzx_mutex);
790         DeleteCriticalSection(&h->cache_mutex);
791
792         if (h->lzx_state)
793             LZXteardown(h->lzx_state);
794         h->lzx_state = NULL;
795
796         if (h->cache_blocks)
797         {
798             int i;
799             for (i=0; i<h->cache_num_blocks; i++)
800             {
801                 if (h->cache_blocks[i])
802                     free(h->cache_blocks[i]);
803             }
804             free(h->cache_blocks);
805             h->cache_blocks = NULL;
806         }
807
808         free(h->cache_block_indices);
809         h->cache_block_indices = NULL;
810
811         free(h);
812     }
813 }
814
815 /*
816  * set a parameter on the file handle.
817  * valid parameter types:
818  *          CHM_PARAM_MAX_BLOCKS_CACHED:
819  *                 how many decompressed blocks should be cached?  A simple
820  *                 caching scheme is used, wherein the index of the block is
821  *                 used as a hash value, and hash collision results in the
822  *                 invalidation of the previously cached block.
823  */
824 void chm_set_param(struct chmFile *h,
825                    int paramType,
826                    int paramVal)
827 {
828     switch (paramType)
829     {
830         case CHM_PARAM_MAX_BLOCKS_CACHED:
831             CHM_ACQUIRE_LOCK(h->cache_mutex);
832             if (paramVal != h->cache_num_blocks)
833             {
834                 UChar **newBlocks;
835                 Int64 *newIndices;
836                 int     i;
837
838                 /* allocate new cached blocks */
839                 newBlocks = malloc(paramVal * sizeof (UChar *));
840                 newIndices = malloc(paramVal * sizeof (UInt64));
841                 for (i=0; i<paramVal; i++)
842                 {
843                     newBlocks[i] = NULL;
844                     newIndices[i] = 0;
845                 }
846
847                 /* re-distribute old cached blocks */
848                 if (h->cache_blocks)
849                 {
850                     for (i=0; i<h->cache_num_blocks; i++)
851                     {
852                         int newSlot = (int)(h->cache_block_indices[i] % paramVal);
853
854                         if (h->cache_blocks[i])
855                         {
856                             /* in case of collision, destroy newcomer */
857                             if (newBlocks[newSlot])
858                             {
859                                 free(h->cache_blocks[i]);
860                                 h->cache_blocks[i] = NULL;
861                             }
862                             else
863                             {
864                                 newBlocks[newSlot] = h->cache_blocks[i];
865                                 newIndices[newSlot] =
866                                             h->cache_block_indices[i];
867                             }
868                         }
869                     }
870
871                     free(h->cache_blocks);
872                     free(h->cache_block_indices);
873                 }
874
875                 /* now, set new values */
876                 h->cache_blocks = newBlocks;
877                 h->cache_block_indices = newIndices;
878                 h->cache_num_blocks = paramVal;
879             }
880             CHM_RELEASE_LOCK(h->cache_mutex);
881             break;
882
883         default:
884             break;
885     }
886 }
887
888 /*
889  * helper methods for chm_resolve_object
890  */
891
892 /* skip a compressed dword */
893 static void _chm_skip_cword(UChar **pEntry)
894 {
895     while (*(*pEntry)++ >= 0x80)
896         ;
897 }
898
899 /* skip the data from a PMGL entry */
900 static void _chm_skip_PMGL_entry_data(UChar **pEntry)
901 {
902     _chm_skip_cword(pEntry);
903     _chm_skip_cword(pEntry);
904     _chm_skip_cword(pEntry);
905 }
906
907 /* parse a compressed dword */
908 static UInt64 _chm_parse_cword(UChar **pEntry)
909 {
910     UInt64 accum = 0;
911     UChar temp;
912     while ((temp=*(*pEntry)++) >= 0x80)
913     {
914         accum <<= 7;
915         accum += temp & 0x7f;
916     }
917
918     return (accum << 7) + temp;
919 }
920
921 /* parse a utf-8 string into an ASCII char buffer */
922 static int _chm_parse_UTF8(UChar **pEntry, UInt64 count, WCHAR *path)
923 {
924     /* MJM - Modified to return real Unicode strings */ 
925     while (count != 0)
926     {
927         *path++ = (*(*pEntry)++);
928         --count;
929     }
930
931     *path = '\0';
932     return 1;
933 }
934
935 /* parse a PMGL entry into a chmUnitInfo struct; return 1 on success. */
936 static int _chm_parse_PMGL_entry(UChar **pEntry, struct chmUnitInfo *ui)
937 {
938     UInt64 strLen;
939
940     /* parse str len */
941     strLen = _chm_parse_cword(pEntry);
942     if (strLen > CHM_MAX_PATHLEN)
943         return 0;
944
945     /* parse path */
946     if (! _chm_parse_UTF8(pEntry, strLen, ui->path))
947         return 0;
948
949     /* parse info */
950     ui->space  = (int)_chm_parse_cword(pEntry);
951     ui->start  = _chm_parse_cword(pEntry);
952     ui->length = _chm_parse_cword(pEntry);
953     return 1;
954 }
955
956 /* find an exact entry in PMGL; return NULL if we fail */
957 static UChar *_chm_find_in_PMGL(UChar *page_buf,
958                          UInt32 block_len,
959                          const WCHAR *objPath)
960 {
961     /* XXX: modify this to do a binary search using the nice index structure
962      *      that is provided for us.
963      */
964     struct chmPmglHeader header;
965     UInt32 hremain;
966     UChar *end;
967     UChar *cur;
968     UChar *temp;
969     UInt64 strLen;
970     WCHAR buffer[CHM_MAX_PATHLEN+1];
971
972     /* figure out where to start and end */
973     cur = page_buf;
974     hremain = _CHM_PMGL_LEN;
975     if (! _unmarshal_pmgl_header(&cur, &hremain, &header))
976         return NULL;
977     end = page_buf + block_len - (header.free_space);
978
979     /* now, scan progressively */
980     while (cur < end)
981     {
982         /* grab the name */
983         temp = cur;
984         strLen = _chm_parse_cword(&cur);
985         if (! _chm_parse_UTF8(&cur, strLen, buffer))
986             return NULL;
987
988         /* check if it is the right name */
989         if (! strcmpiW(buffer, objPath))
990             return temp;
991
992         _chm_skip_PMGL_entry_data(&cur);
993     }
994
995     return NULL;
996 }
997
998 /* find which block should be searched next for the entry; -1 if no block */
999 static Int32 _chm_find_in_PMGI(UChar *page_buf,
1000                         UInt32 block_len,
1001                         const WCHAR *objPath)
1002 {
1003     /* XXX: modify this to do a binary search using the nice index structure
1004      *      that is provided for us
1005      */
1006     struct chmPmgiHeader header;
1007     UInt32 hremain;
1008     int page=-1;
1009     UChar *end;
1010     UChar *cur;
1011     UInt64 strLen;
1012     WCHAR buffer[CHM_MAX_PATHLEN+1];
1013
1014     /* figure out where to start and end */
1015     cur = page_buf;
1016     hremain = _CHM_PMGI_LEN;
1017     if (! _unmarshal_pmgi_header(&cur, &hremain, &header))
1018         return -1;
1019     end = page_buf + block_len - (header.free_space);
1020
1021     /* now, scan progressively */
1022     while (cur < end)
1023     {
1024         /* grab the name */
1025         strLen = _chm_parse_cword(&cur);
1026         if (! _chm_parse_UTF8(&cur, strLen, buffer))
1027             return -1;
1028
1029         /* check if it is the right name */
1030         if (strcmpiW(buffer, objPath) > 0)
1031             return page;
1032
1033         /* load next value for path */
1034         page = (int)_chm_parse_cword(&cur);
1035     }
1036
1037     return page;
1038 }
1039
1040 /* resolve a particular object from the archive */
1041 int chm_resolve_object(struct chmFile *h,
1042                        const WCHAR *objPath,
1043                        struct chmUnitInfo *ui)
1044 {
1045     /*
1046      * XXX: implement caching scheme for dir pages
1047      */
1048
1049     Int32 curPage;
1050
1051     /* buffer to hold whatever page we're looking at */
1052     UChar *page_buf = HeapAlloc(GetProcessHeap(), 0, h->block_len);
1053
1054     /* starting page */
1055     curPage = h->index_root;
1056
1057     /* until we have either returned or given up */
1058     while (curPage != -1)
1059     {
1060
1061         /* try to fetch the index page */
1062         if (_chm_fetch_bytes(h, page_buf,
1063                              (UInt64)h->dir_offset + (UInt64)curPage*h->block_len,
1064                              h->block_len) != h->block_len)
1065         {
1066             HeapFree(GetProcessHeap(), 0, page_buf);
1067             return CHM_RESOLVE_FAILURE;
1068         }
1069
1070         /* now, if it is a leaf node: */
1071         if (memcmp(page_buf, _chm_pmgl_marker, 4) == 0)
1072         {
1073             /* scan block */
1074             UChar *pEntry = _chm_find_in_PMGL(page_buf,
1075                                               h->block_len,
1076                                               objPath);
1077             if (pEntry == NULL)
1078             {
1079                 HeapFree(GetProcessHeap(), 0, page_buf);
1080                 return CHM_RESOLVE_FAILURE;
1081             }
1082
1083             /* parse entry and return */
1084             _chm_parse_PMGL_entry(&pEntry, ui);
1085             HeapFree(GetProcessHeap(), 0, page_buf);
1086             return CHM_RESOLVE_SUCCESS;
1087         }
1088
1089         /* else, if it is a branch node: */
1090         else if (memcmp(page_buf, _chm_pmgi_marker, 4) == 0)
1091             curPage = _chm_find_in_PMGI(page_buf, h->block_len, objPath);
1092
1093         /* else, we are confused.  give up. */
1094         else
1095         {
1096             HeapFree(GetProcessHeap(), 0, page_buf);
1097             return CHM_RESOLVE_FAILURE;
1098         }
1099     }
1100
1101     /* didn't find anything.  fail. */
1102     HeapFree(GetProcessHeap(), 0, page_buf);
1103     return CHM_RESOLVE_FAILURE;
1104 }
1105
1106 /*
1107  * utility methods for dealing with compressed data
1108  */
1109
1110 /* get the bounds of a compressed block.  return 0 on failure */
1111 static int _chm_get_cmpblock_bounds(struct chmFile *h,
1112                              UInt64 block,
1113                              UInt64 *start,
1114                              Int64 *len)
1115 {
1116     UChar buffer[8], *dummy;
1117     UInt32 remain;
1118
1119     /* for all but the last block, use the reset table */
1120     if (block < h->reset_table.block_count-1)
1121     {
1122         /* unpack the start address */
1123         dummy = buffer;
1124         remain = 8;
1125         if (_chm_fetch_bytes(h, buffer,
1126                              (UInt64)h->data_offset
1127                                 + (UInt64)h->rt_unit.start
1128                                 + (UInt64)h->reset_table.table_offset
1129                                 + (UInt64)block*8,
1130                              remain) != remain                            ||
1131             !_unmarshal_uint64(&dummy, &remain, start))
1132             return 0;
1133
1134         /* unpack the end address */
1135         dummy = buffer;
1136         remain = 8;
1137         if (_chm_fetch_bytes(h, buffer,
1138                          (UInt64)h->data_offset
1139                                 + (UInt64)h->rt_unit.start
1140                                 + (UInt64)h->reset_table.table_offset
1141                                 + (UInt64)block*8 + 8,
1142                          remain) != remain                                ||
1143             !_unmarshal_int64(&dummy, &remain, len))
1144             return 0;
1145     }
1146
1147     /* for the last block, use the span in addition to the reset table */
1148     else
1149     {
1150         /* unpack the start address */
1151         dummy = buffer;
1152         remain = 8;
1153         if (_chm_fetch_bytes(h, buffer,
1154                              (UInt64)h->data_offset
1155                                 + (UInt64)h->rt_unit.start
1156                                 + (UInt64)h->reset_table.table_offset
1157                                 + (UInt64)block*8,
1158                              remain) != remain                            ||
1159             !_unmarshal_uint64(&dummy, &remain, start))
1160             return 0;
1161
1162         *len = h->reset_table.compressed_len;
1163     }
1164
1165     /* compute the length and absolute start address */
1166     *len -= *start;
1167     *start += h->data_offset + h->cn_unit.start;
1168
1169     return 1;
1170 }
1171
1172 /* decompress the block.  must have lzx_mutex. */
1173 static Int64 _chm_decompress_block(struct chmFile *h,
1174                                    UInt64 block,
1175                                    UChar **ubuffer)
1176 {
1177     UChar *cbuffer = HeapAlloc( GetProcessHeap(), 0,
1178                               ((unsigned int)h->reset_table.block_len + 6144));
1179     UInt64 cmpStart;                                    /* compressed start  */
1180     Int64 cmpLen;                                       /* compressed len    */
1181     int indexSlot;                                      /* cache index slot  */
1182     UChar *lbuffer;                                     /* local buffer ptr  */
1183     UInt32 blockAlign = (UInt32)(block % h->reset_blkcount); /* reset intvl. aln. */
1184     UInt32 i;                                           /* local loop index  */
1185
1186     /* let the caching system pull its weight! */
1187     if (block - blockAlign <= h->lzx_last_block  &&
1188         block              >= h->lzx_last_block)
1189         blockAlign = (block - h->lzx_last_block);
1190
1191     /* check if we need previous blocks */
1192     if (blockAlign != 0)
1193     {
1194         /* fetch all required previous blocks since last reset */
1195         for (i = blockAlign; i > 0; i--)
1196         {
1197             UInt32 curBlockIdx = block - i;
1198
1199             /* check if we most recently decompressed the previous block */
1200             if (h->lzx_last_block != curBlockIdx)
1201             {
1202                 if ((curBlockIdx % h->reset_blkcount) == 0)
1203                 {
1204 #ifdef CHM_DEBUG
1205                     fprintf(stderr, "***RESET (1)***\n");
1206 #endif
1207                     LZXreset(h->lzx_state);
1208                 }
1209
1210                 indexSlot = (int)((curBlockIdx) % h->cache_num_blocks);
1211                 h->cache_block_indices[indexSlot] = curBlockIdx;
1212                 if (! h->cache_blocks[indexSlot])
1213                     h->cache_blocks[indexSlot] = malloc( (unsigned int)(h->reset_table.block_len));
1214                 lbuffer = h->cache_blocks[indexSlot];
1215
1216                 /* decompress the previous block */
1217 #ifdef CHM_DEBUG
1218                 fprintf(stderr, "Decompressing block #%4d (EXTRA)\n", curBlockIdx);
1219 #endif
1220                 if (!_chm_get_cmpblock_bounds(h, curBlockIdx, &cmpStart, &cmpLen) ||
1221                     _chm_fetch_bytes(h, cbuffer, cmpStart, cmpLen) != cmpLen      ||
1222                     LZXdecompress(h->lzx_state, cbuffer, lbuffer, (int)cmpLen,
1223                                   (int)h->reset_table.block_len) != DECR_OK)
1224                 {
1225 #ifdef CHM_DEBUG
1226                     fprintf(stderr, "   (DECOMPRESS FAILED!)\n");
1227 #endif
1228                     HeapFree(GetProcessHeap(), 0, cbuffer);
1229                     return (Int64)0;
1230                 }
1231
1232                 h->lzx_last_block = (int)curBlockIdx;
1233             }
1234         }
1235     }
1236     else
1237     {
1238         if ((block % h->reset_blkcount) == 0)
1239         {
1240 #ifdef CHM_DEBUG
1241             fprintf(stderr, "***RESET (2)***\n");
1242 #endif
1243             LZXreset(h->lzx_state);
1244         }
1245     }
1246
1247     /* allocate slot in cache */
1248     indexSlot = (int)(block % h->cache_num_blocks);
1249     h->cache_block_indices[indexSlot] = block;
1250     if (! h->cache_blocks[indexSlot])
1251         h->cache_blocks[indexSlot] = malloc( ((unsigned int)h->reset_table.block_len));
1252     lbuffer = h->cache_blocks[indexSlot];
1253     *ubuffer = lbuffer;
1254
1255     /* decompress the block we actually want */
1256 #ifdef CHM_DEBUG
1257     fprintf(stderr, "Decompressing block #%4d (REAL )\n", block);
1258 #endif
1259     if (! _chm_get_cmpblock_bounds(h, block, &cmpStart, &cmpLen)          ||
1260         _chm_fetch_bytes(h, cbuffer, cmpStart, cmpLen) != cmpLen          ||
1261         LZXdecompress(h->lzx_state, cbuffer, lbuffer, (int)cmpLen,
1262                       (int)h->reset_table.block_len) != DECR_OK)
1263     {
1264 #ifdef CHM_DEBUG
1265         fprintf(stderr, "   (DECOMPRESS FAILED!)\n");
1266 #endif
1267         HeapFree(GetProcessHeap(), 0, cbuffer);
1268         return (Int64)0;
1269     }
1270     h->lzx_last_block = (int)block;
1271
1272     /* XXX: modify LZX routines to return the length of the data they
1273      * decompressed and return that instead, for an extra sanity check.
1274      */
1275     HeapFree(GetProcessHeap(), 0, cbuffer);
1276     return h->reset_table.block_len;
1277 }
1278
1279 /* grab a region from a compressed block */
1280 static Int64 _chm_decompress_region(struct chmFile *h,
1281                                     UChar *buf,
1282                                     UInt64 start,
1283                                     Int64 len)
1284 {
1285     UInt64 nBlock, nOffset;
1286     UInt64 nLen;
1287     UInt64 gotLen;
1288     UChar *ubuffer = NULL;
1289
1290         if (len <= 0)
1291                 return (Int64)0;
1292
1293     /* figure out what we need to read */
1294     nBlock = start / h->reset_table.block_len;
1295     nOffset = start % h->reset_table.block_len;
1296     nLen = len;
1297     if (nLen > (h->reset_table.block_len - nOffset))
1298         nLen = h->reset_table.block_len - nOffset;
1299
1300     /* if block is cached, return data from it. */
1301     CHM_ACQUIRE_LOCK(h->lzx_mutex);
1302     CHM_ACQUIRE_LOCK(h->cache_mutex);
1303     if (h->cache_block_indices[nBlock % h->cache_num_blocks] == nBlock    &&
1304         h->cache_blocks[nBlock % h->cache_num_blocks] != NULL)
1305     {
1306         memcpy(buf,
1307                h->cache_blocks[nBlock % h->cache_num_blocks] + nOffset,
1308                (unsigned int)nLen);
1309         CHM_RELEASE_LOCK(h->cache_mutex);
1310         CHM_RELEASE_LOCK(h->lzx_mutex);
1311         return nLen;
1312     }
1313     CHM_RELEASE_LOCK(h->cache_mutex);
1314
1315     /* data request not satisfied, so... start up the decompressor machine */
1316     if (! h->lzx_state)
1317     {
1318         int window_size = ffs(h->window_size) - 1;
1319         h->lzx_last_block = -1;
1320         h->lzx_state = LZXinit(window_size);
1321     }
1322
1323     /* decompress some data */
1324     gotLen = _chm_decompress_block(h, nBlock, &ubuffer);
1325     if (gotLen < nLen)
1326         nLen = gotLen;
1327     memcpy(buf, ubuffer+nOffset, (unsigned int)nLen);
1328     CHM_RELEASE_LOCK(h->lzx_mutex);
1329     return nLen;
1330 }
1331
1332 /* retrieve (part of) an object */
1333 LONGINT64 chm_retrieve_object(struct chmFile *h,
1334                                struct chmUnitInfo *ui,
1335                                unsigned char *buf,
1336                                LONGUINT64 addr,
1337                                LONGINT64 len)
1338 {
1339     /* must be valid file handle */
1340     if (h == NULL)
1341         return (Int64)0;
1342
1343     /* starting address must be in correct range */
1344     if (addr < 0  ||  addr >= ui->length)
1345         return (Int64)0;
1346
1347     /* clip length */
1348     if (addr + len > ui->length)
1349         len = ui->length - addr;
1350
1351     /* if the file is uncompressed, it's simple */
1352     if (ui->space == CHM_UNCOMPRESSED)
1353     {
1354         /* read data */
1355         return _chm_fetch_bytes(h,
1356                                 buf,
1357                                 (UInt64)h->data_offset + (UInt64)ui->start + (UInt64)addr,
1358                                 len);
1359     }
1360
1361     /* else if the file is compressed, it's a little trickier */
1362     else /* ui->space == CHM_COMPRESSED */
1363     {
1364         Int64 swath=0, total=0;
1365
1366         /* if compression is not enabled for this file... */
1367         if (! h->compression_enabled)
1368             return total;
1369
1370         do {
1371
1372             /* swill another mouthful */
1373             swath = _chm_decompress_region(h, buf, ui->start + addr, len);
1374
1375             /* if we didn't get any... */
1376             if (swath == 0)
1377                 return total;
1378
1379             /* update stats */
1380             total += swath;
1381             len -= swath;
1382             addr += swath;
1383             buf += swath;
1384
1385         } while (len != 0);
1386
1387         return total;
1388     }
1389 }
1390
1391 /* enumerate the objects in the .chm archive */
1392 int chm_enumerate(struct chmFile *h,
1393                   int what,
1394                   CHM_ENUMERATOR e,
1395                   void *context)
1396 {
1397     Int32 curPage;
1398
1399     /* buffer to hold whatever page we're looking at */
1400     UChar *page_buf = HeapAlloc(GetProcessHeap(), 0, (unsigned int)h->block_len);
1401     struct chmPmglHeader header;
1402     UChar *end;
1403     UChar *cur;
1404     unsigned int lenRemain;
1405     UInt64 ui_path_len;
1406
1407     /* the current ui */
1408     struct chmUnitInfo ui;
1409     int flag;
1410
1411     /* starting page */
1412     curPage = h->index_head;
1413
1414     /* until we have either returned or given up */
1415     while (curPage != -1)
1416     {
1417
1418         /* try to fetch the index page */
1419         if (_chm_fetch_bytes(h,
1420                              page_buf,
1421                              (UInt64)h->dir_offset + (UInt64)curPage*h->block_len,
1422                              h->block_len) != h->block_len)
1423         {
1424             HeapFree(GetProcessHeap(), 0, page_buf);
1425             return 0;
1426         }
1427
1428         /* figure out start and end for this page */
1429         cur = page_buf;
1430         lenRemain = _CHM_PMGL_LEN;
1431         if (! _unmarshal_pmgl_header(&cur, &lenRemain, &header))
1432         {
1433             HeapFree(GetProcessHeap(), 0, page_buf);
1434             return 0;
1435         }
1436         end = page_buf + h->block_len - (header.free_space);
1437
1438         /* loop over this page */
1439         while (cur < end)
1440         {
1441             if (! _chm_parse_PMGL_entry(&cur, &ui))
1442             {
1443                 HeapFree(GetProcessHeap(), 0, page_buf);
1444                 return 0;
1445             }
1446
1447             /* get the length of the path */
1448             ui_path_len = strlenW(ui.path)-1;
1449
1450             /* check for DIRS */
1451             if (ui.path[ui_path_len] == '/'  &&  !(what & CHM_ENUMERATE_DIRS))
1452                 continue;
1453
1454             /* check for FILES */
1455             if (ui.path[ui_path_len] != '/'  &&  !(what & CHM_ENUMERATE_FILES))
1456                 continue;
1457
1458             /* check for NORMAL vs. META */
1459             if (ui.path[0] == '/')
1460             {
1461
1462                 /* check for NORMAL vs. SPECIAL */
1463                 if (ui.path[1] == '#'  ||  ui.path[1] == '$')
1464                     flag = CHM_ENUMERATE_SPECIAL;
1465                 else
1466                     flag = CHM_ENUMERATE_NORMAL;
1467             }
1468             else
1469                 flag = CHM_ENUMERATE_META;
1470             if (! (what & flag))
1471                 continue;
1472
1473             /* call the enumerator */
1474             {
1475                 int status = (*e)(h, &ui, context);
1476                 switch (status)
1477                 {
1478                     case CHM_ENUMERATOR_FAILURE:
1479                         HeapFree(GetProcessHeap(), 0, page_buf);
1480                         return 0;
1481                     case CHM_ENUMERATOR_CONTINUE:
1482                         break;
1483                     case CHM_ENUMERATOR_SUCCESS:
1484                         HeapFree(GetProcessHeap(), 0, page_buf);
1485                         return 1;
1486                     default:
1487                         break;
1488                 }
1489             }
1490         }
1491
1492         /* advance to next page */
1493         curPage = header.block_next;
1494     }
1495
1496     HeapFree(GetProcessHeap(), 0, page_buf);
1497     return 1;
1498 }
1499
1500 int chm_enumerate_dir(struct chmFile *h,
1501                       const WCHAR *prefix,
1502                       int what,
1503                       CHM_ENUMERATOR e,
1504                       void *context)
1505 {
1506     /*
1507      * XXX: do this efficiently (i.e. using the tree index)
1508      */
1509
1510     Int32 curPage;
1511
1512     /* buffer to hold whatever page we're looking at */
1513     UChar *page_buf = HeapAlloc(GetProcessHeap(), 0, (unsigned int)h->block_len);
1514     struct chmPmglHeader header;
1515     UChar *end;
1516     UChar *cur;
1517     unsigned int lenRemain;
1518
1519     /* set to 1 once we've started */
1520     int it_has_begun=0;
1521
1522     /* the current ui */
1523     struct chmUnitInfo ui;
1524     int flag;
1525     UInt64 ui_path_len;
1526
1527     /* the length of the prefix */
1528     WCHAR prefixRectified[CHM_MAX_PATHLEN+1];
1529     int prefixLen;
1530     WCHAR lastPath[CHM_MAX_PATHLEN];
1531     int lastPathLen;
1532
1533     /* starting page */
1534     curPage = h->index_head;
1535
1536     /* initialize pathname state */
1537     lstrcpynW(prefixRectified, prefix, CHM_MAX_PATHLEN);
1538     prefixLen = strlenW(prefixRectified);
1539     if (prefixLen != 0)
1540     {
1541         if (prefixRectified[prefixLen-1] != '/')
1542         {
1543             prefixRectified[prefixLen] = '/';
1544             prefixRectified[prefixLen+1] = '\0';
1545             ++prefixLen;
1546         }
1547     }
1548     lastPath[0] = '\0';
1549     lastPathLen = -1;
1550
1551     /* until we have either returned or given up */
1552     while (curPage != -1)
1553     {
1554
1555         /* try to fetch the index page */
1556         if (_chm_fetch_bytes(h,
1557                              page_buf,
1558                              (UInt64)h->dir_offset + (UInt64)curPage*h->block_len,
1559                              h->block_len) != h->block_len)
1560         {
1561             HeapFree(GetProcessHeap(), 0, page_buf);
1562             return 0;
1563         }
1564
1565         /* figure out start and end for this page */
1566         cur = page_buf;
1567         lenRemain = _CHM_PMGL_LEN;
1568         if (! _unmarshal_pmgl_header(&cur, &lenRemain, &header))
1569         {
1570             HeapFree(GetProcessHeap(), 0, page_buf);
1571             return 0;
1572         }
1573         end = page_buf + h->block_len - (header.free_space);
1574
1575         /* loop over this page */
1576         while (cur < end)
1577         {
1578             if (! _chm_parse_PMGL_entry(&cur, &ui))
1579             {
1580                 HeapFree(GetProcessHeap(), 0, page_buf);
1581                 return 0;
1582             }
1583
1584             /* check if we should start */
1585             if (! it_has_begun)
1586             {
1587                 if (ui.length == 0  &&  strncmpiW(ui.path, prefixRectified, prefixLen) == 0)
1588                     it_has_begun = 1;
1589                 else
1590                     continue;
1591
1592                 if (ui.path[prefixLen] == '\0')
1593                     continue;
1594             }
1595
1596             /* check if we should stop */
1597             else
1598             {
1599                 if (strncmpiW(ui.path, prefixRectified, prefixLen) != 0)
1600                 {
1601                     HeapFree(GetProcessHeap(), 0, page_buf);
1602                     return 1;
1603                 }
1604             }
1605
1606             /* check if we should include this path */
1607             if (lastPathLen != -1)
1608             {
1609                 if (strncmpiW(ui.path, lastPath, lastPathLen) == 0)
1610                     continue;
1611             }
1612             strcpyW(lastPath, ui.path);
1613             lastPathLen = strlenW(lastPath);
1614
1615             /* get the length of the path */
1616             ui_path_len = strlenW(ui.path)-1;
1617
1618             /* check for DIRS */
1619             if (ui.path[ui_path_len] == '/'  &&  !(what & CHM_ENUMERATE_DIRS))
1620                 continue;
1621
1622             /* check for FILES */
1623             if (ui.path[ui_path_len] != '/'  &&  !(what & CHM_ENUMERATE_FILES))
1624                 continue;
1625
1626             /* check for NORMAL vs. META */
1627             if (ui.path[0] == '/')
1628             {
1629
1630                 /* check for NORMAL vs. SPECIAL */
1631                 if (ui.path[1] == '#'  ||  ui.path[1] == '$')
1632                     flag = CHM_ENUMERATE_SPECIAL;
1633                 else
1634                     flag = CHM_ENUMERATE_NORMAL;
1635             }
1636             else
1637                 flag = CHM_ENUMERATE_META;
1638             if (! (what & flag))
1639                 continue;
1640
1641             /* call the enumerator */
1642             {
1643                 int status = (*e)(h, &ui, context);
1644                 switch (status)
1645                 {
1646                     case CHM_ENUMERATOR_FAILURE:
1647                         HeapFree(GetProcessHeap(), 0, page_buf);
1648                         return 0;
1649                     case CHM_ENUMERATOR_CONTINUE:
1650                         break;
1651                     case CHM_ENUMERATOR_SUCCESS:
1652                         HeapFree(GetProcessHeap(), 0, page_buf);
1653                         return 1;
1654                     default:
1655                         break;
1656                 }
1657             }
1658         }
1659
1660         /* advance to next page */
1661         curPage = header.block_next;
1662     }
1663
1664     HeapFree(GetProcessHeap(), 0, page_buf);
1665     return 1;
1666 }