4 * Copyright 2000-2002 Stuart Caie
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Principal author: Stuart Caie <kyzer@4u.net>
22 * Based on specification documents from Microsoft Corporation
23 * Quantum decompression researched and implemented by Matthew Russoto
24 * Huffman code adapted from unlzx by Dave Tritscher.
25 * InfoZip team's INFLATE implementation adapted to MSZIP by Dirk Stoecker.
26 * Major LZX fixes by Jae Jung.
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(cabinet
);
43 /* The first result of a search will be returned, and
44 * the remaining results will be chained to it via the cab->next structure
47 cab_UBYTE search_buf
[CAB_SEARCH_SIZE
];
49 cab_decomp_state decomp_state
;
51 /* all the file IO is abstracted into these routines:
52 * cabinet_(open|close|read|seek|skip|getoffset)
53 * file_(open|close|write)
56 /* try to open a cabinet file, returns success */
57 BOOL
cabinet_open(struct cabinet
*cab
)
59 char *name
= (char *)cab
->filename
;
62 TRACE("(cab == ^%p)\n", cab
);
64 if ((fh
= CreateFileA( name
, GENERIC_READ
, FILE_SHARE_READ
,
65 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
)) == INVALID_HANDLE_VALUE
) {
66 ERR("Couldn't open %s\n", debugstr_a(name
));
70 /* seek to end of file and get the length */
71 if ((cab
->filelen
= SetFilePointer(fh
, 0, NULL
, FILE_END
)) == INVALID_SET_FILE_POINTER
) {
72 if (GetLastError() != NO_ERROR
) {
73 ERR("Seek END failed: %s", debugstr_a(name
));
79 /* return to the start of the file */
80 if (SetFilePointer(fh
, 0, NULL
, FILE_BEGIN
) == INVALID_SET_FILE_POINTER
) {
81 ERR("Seek BEGIN failed: %s", debugstr_a(name
));
90 /*******************************************************************
91 * cabinet_close (internal)
93 * close the file handle in a struct cabinet.
95 void cabinet_close(struct cabinet
*cab
) {
96 TRACE("(cab == ^%p)\n", cab
);
97 if (cab
->fh
) CloseHandle(cab
->fh
);
101 /*******************************************************
102 * ensure_filepath2 (internal)
104 BOOL
ensure_filepath2(char *path
) {
109 new_path
= HeapAlloc(GetProcessHeap(), 0, (strlen(path
) + 1));
110 strcpy(new_path
, path
);
112 while((len
= strlen(new_path
)) && new_path
[len
- 1] == '\\')
113 new_path
[len
- 1] = 0;
115 TRACE("About to try to create directory %s\n", debugstr_a(new_path
));
116 while(!CreateDirectoryA(new_path
, NULL
)) {
118 DWORD last_error
= GetLastError();
120 if(last_error
== ERROR_ALREADY_EXISTS
)
123 if(last_error
!= ERROR_PATH_NOT_FOUND
) {
128 if(!(slash
= strrchr(new_path
, '\\'))) {
133 len
= slash
- new_path
;
135 if(! ensure_filepath2(new_path
)) {
139 new_path
[len
] = '\\';
140 TRACE("New path in next iteration: %s\n", debugstr_a(new_path
));
143 HeapFree(GetProcessHeap(), 0, new_path
);
148 /**********************************************************************
149 * ensure_filepath (internal)
151 * ensure_filepath("a\b\c\d.txt") ensures a, a\b and a\b\c exist as dirs
153 BOOL
ensure_filepath(char *path
) {
154 char new_path
[MAX_PATH
];
155 int len
, i
, lastslashpos
= -1;
157 TRACE("(path == %s)\n", debugstr_a(path
));
159 strcpy(new_path
, path
);
160 /* remove trailing slashes (shouldn't need to but wth...) */
161 while ((len
= strlen(new_path
)) && new_path
[len
- 1] == '\\')
162 new_path
[len
- 1] = 0;
163 /* find the position of the last '\\' */
164 for (i
=0; i
<MAX_PATH
; i
++) {
165 if (new_path
[i
] == 0) break;
166 if (new_path
[i
] == '\\')
169 if (lastslashpos
> 0) {
170 new_path
[lastslashpos
] = 0;
171 /* may be trailing slashes but ensure_filepath2 will chop them */
172 return ensure_filepath2(new_path
);
177 /*******************************************************************
178 * file_open (internal)
180 * opens a file for output, returns success
182 BOOL
file_open(struct cab_file
*fi
, BOOL lower
, LPCSTR dir
)
184 char c
, *s
, *d
, *name
;
187 TRACE("(fi == ^%p, lower == %s, dir == %s)\n", fi
, lower
? "TRUE" : "FALSE", debugstr_a(dir
));
189 if (!(name
= malloc(strlen(fi
->filename
) + (dir
? strlen(dir
) : 0) + 2))) {
190 ERR("out of memory!\n");
194 /* start with blank name */
197 /* add output directory if needed */
203 /* remove leading slashes */
204 s
= (char *) fi
->filename
;
205 while (*s
== '\\') s
++;
207 /* copy from fi->filename to new name.
208 * lowercases characters if needed.
210 d
= &name
[strlen(name
)];
213 *d
++ = (lower
? tolower((unsigned char) c
) : c
);
216 /* create directories if needed, attempt to write file */
217 if (ensure_filepath(name
)) {
218 fi
->fh
= CreateFileA(name
, GENERIC_WRITE
, 0, NULL
,
219 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
220 if (fi
->fh
!= INVALID_HANDLE_VALUE
)
223 ERR("CreateFileA returned INVALID_HANDLE_VALUE\n");
227 ERR("Couldn't ensure filepath for %s", debugstr_a(name
));
230 ERR("Couldn't open file %s for writing\n", debugstr_a(name
));
233 /* as full filename is no longer needed, free it */
239 /********************************************************
240 * close_file (internal)
242 * closes a completed file
244 void file_close(struct cab_file
*fi
)
246 TRACE("(fi == ^%p)\n", fi
);
254 /******************************************************************
255 * file_write (internal)
257 * writes from buf to a file specified as a cab_file struct.
258 * returns success/failure
260 BOOL
file_write(struct cab_file
*fi
, cab_UBYTE
*buf
, cab_off_t length
)
264 TRACE("(fi == ^%p, buf == ^%p, length == %u)\n", fi
, buf
, length
);
266 if ((!WriteFile( fi
->fh
, (LPCVOID
) buf
, length
, &bytes_written
, FALSE
) ||
267 (bytes_written
!= length
))) {
268 ERR("Error writing file: %s\n", debugstr_a(fi
->filename
));
275 /*******************************************************************
276 * cabinet_skip (internal)
278 * advance the file pointer associated with the cab structure
281 void cabinet_skip(struct cabinet
*cab
, cab_off_t distance
)
283 TRACE("(cab == ^%p, distance == %u)\n", cab
, distance
);
284 if (SetFilePointer(cab
->fh
, distance
, NULL
, FILE_CURRENT
) == INVALID_SET_FILE_POINTER
) {
285 if (distance
!= INVALID_SET_FILE_POINTER
)
286 ERR("%s", debugstr_a((char *) cab
->filename
));
290 /*******************************************************************
291 * cabinet_seek (internal)
293 * seek to the specified absolute offset in a cab
295 void cabinet_seek(struct cabinet
*cab
, cab_off_t offset
) {
296 TRACE("(cab == ^%p, offset == %u)\n", cab
, offset
);
297 if (SetFilePointer(cab
->fh
, offset
, NULL
, FILE_BEGIN
) != offset
)
298 ERR("%s seek failure\n", debugstr_a((char *)cab
->filename
));
301 /*******************************************************************
302 * cabinet_getoffset (internal)
304 * returns the file pointer position of a cab
306 cab_off_t
cabinet_getoffset(struct cabinet
*cab
)
308 return SetFilePointer(cab
->fh
, 0, NULL
, FILE_CURRENT
);
311 /*******************************************************************
312 * cabinet_read (internal)
314 * read data from a cabinet, returns success
316 BOOL
cabinet_read(struct cabinet
*cab
, cab_UBYTE
*buf
, cab_off_t length
)
319 cab_off_t avail
= cab
->filelen
- cabinet_getoffset(cab
);
321 TRACE("(cab == ^%p, buf == ^%p, length == %u)\n", cab
, buf
, length
);
323 if (length
> avail
) {
324 WARN("%s: WARNING; cabinet is truncated\n", debugstr_a((char *)cab
->filename
));
328 if (! ReadFile( cab
->fh
, (LPVOID
) buf
, length
, &bytes_read
, NULL
)) {
329 ERR("%s read error\n", debugstr_a((char *) cab
->filename
));
331 } else if (bytes_read
!= length
) {
332 ERR("%s read size mismatch\n", debugstr_a((char *) cab
->filename
));
339 /**********************************************************************
340 * cabinet_read_string (internal)
342 * allocate and read an aribitrarily long string from the cabinet
344 char *cabinet_read_string(struct cabinet
*cab
)
346 cab_off_t len
=256, base
= cabinet_getoffset(cab
), maxlen
= cab
->filelen
- base
;
349 cab_UBYTE
*buf
= NULL
;
351 TRACE("(cab == ^%p)\n", cab
);
354 if (len
> maxlen
) len
= maxlen
;
355 if (!(buf
= realloc(buf
, (size_t) len
))) break;
356 if (!cabinet_read(cab
, buf
, (size_t) len
)) break;
358 /* search for a null terminator in what we've just read */
359 for (i
=0; i
< len
; i
++) {
360 if (!buf
[i
]) {ok
=TRUE
; break;}
365 ERR("%s: WARNING; cabinet is truncated\n", debugstr_a((char *) cab
->filename
));
369 cabinet_seek(cab
, base
);
377 ERR("out of memory!\n");
381 /* otherwise, set the stream to just after the string and return */
382 cabinet_seek(cab
, base
+ ((cab_off_t
) strlen((char *) buf
)) + 1);
387 /******************************************************************
388 * cabinet_read_entries (internal)
390 * reads the header and all folder and file entries in this cabinet
392 BOOL
cabinet_read_entries(struct cabinet
*cab
)
394 int num_folders
, num_files
, header_resv
, folder_resv
= 0, i
;
395 struct cab_folder
*fol
, *linkfol
= NULL
;
396 struct cab_file
*file
, *linkfile
= NULL
;
397 cab_off_t base_offset
;
400 TRACE("(cab == ^%p)\n", cab
);
402 /* read in the CFHEADER */
403 base_offset
= cabinet_getoffset(cab
);
404 if (!cabinet_read(cab
, buf
, cfhead_SIZEOF
)) {
408 /* check basic MSCF signature */
409 if (EndGetI32(buf
+cfhead_Signature
) != 0x4643534d) {
410 ERR("%s: not a Microsoft cabinet file\n", debugstr_a((char *) cab
->filename
));
414 /* get the number of folders */
415 num_folders
= EndGetI16(buf
+cfhead_NumFolders
);
416 if (num_folders
== 0) {
417 ERR("%s: no folders in cabinet\n", debugstr_a((char *) cab
->filename
));
421 /* get the number of files */
422 num_files
= EndGetI16(buf
+cfhead_NumFiles
);
423 if (num_files
== 0) {
424 ERR("%s: no files in cabinet\n", debugstr_a((char *) cab
->filename
));
428 /* just check the header revision */
429 if ((buf
[cfhead_MajorVersion
] > 1) ||
430 (buf
[cfhead_MajorVersion
] == 1 && buf
[cfhead_MinorVersion
] > 3))
432 WARN("%s: WARNING; cabinet format version > 1.3\n", debugstr_a((char *) cab
->filename
));
435 /* read the reserved-sizes part of header, if present */
436 cab
->flags
= EndGetI16(buf
+cfhead_Flags
);
437 if (cab
->flags
& cfheadRESERVE_PRESENT
) {
438 if (!cabinet_read(cab
, buf
, cfheadext_SIZEOF
)) return FALSE
;
439 header_resv
= EndGetI16(buf
+cfheadext_HeaderReserved
);
440 folder_resv
= buf
[cfheadext_FolderReserved
];
441 cab
->block_resv
= buf
[cfheadext_DataReserved
];
443 if (header_resv
> 60000) {
444 WARN("%s: WARNING; header reserved space > 60000\n", debugstr_a((char *) cab
->filename
));
447 /* skip the reserved header */
449 if (SetFilePointer(cab
->fh
, (cab_off_t
) header_resv
, NULL
, FILE_CURRENT
) == INVALID_SET_FILE_POINTER
)
450 ERR("seek failure: %s\n", debugstr_a((char *) cab
->filename
));
453 if (cab
->flags
& cfheadPREV_CABINET
) {
454 cab
->prevname
= cabinet_read_string(cab
);
455 if (!cab
->prevname
) return FALSE
;
456 cab
->previnfo
= cabinet_read_string(cab
);
459 if (cab
->flags
& cfheadNEXT_CABINET
) {
460 cab
->nextname
= cabinet_read_string(cab
);
461 if (!cab
->nextname
) return FALSE
;
462 cab
->nextinfo
= cabinet_read_string(cab
);
466 for (i
= 0; i
< num_folders
; i
++) {
467 if (!cabinet_read(cab
, buf
, cffold_SIZEOF
)) return FALSE
;
468 if (folder_resv
) cabinet_skip(cab
, folder_resv
);
470 fol
= (struct cab_folder
*) calloc(1, sizeof(struct cab_folder
));
472 ERR("out of memory!\n");
477 fol
->offset
[0] = base_offset
+ (cab_off_t
) EndGetI32(buf
+cffold_DataOffset
);
478 fol
->num_blocks
= EndGetI16(buf
+cffold_NumBlocks
);
479 fol
->comp_type
= EndGetI16(buf
+cffold_CompType
);
490 for (i
= 0; i
< num_files
; i
++) {
491 if (!cabinet_read(cab
, buf
, cffile_SIZEOF
))
494 file
= (struct cab_file
*) calloc(1, sizeof(struct cab_file
));
496 ERR("out of memory!\n");
500 file
->length
= EndGetI32(buf
+cffile_UncompressedSize
);
501 file
->offset
= EndGetI32(buf
+cffile_FolderOffset
);
502 file
->index
= EndGetI16(buf
+cffile_FolderIndex
);
503 file
->time
= EndGetI16(buf
+cffile_Time
);
504 file
->date
= EndGetI16(buf
+cffile_Date
);
505 file
->attribs
= EndGetI16(buf
+cffile_Attribs
);
506 file
->filename
= cabinet_read_string(cab
);
508 if (!file
->filename
) {
516 linkfile
->next
= file
;
523 /***********************************************************
524 * load_cab_offset (internal)
526 * validates and reads file entries from a cabinet at offset [offset] in
527 * file [name]. Returns a cabinet structure if successful, or NULL
530 struct cabinet
*load_cab_offset(LPCSTR name
, cab_off_t offset
)
532 struct cabinet
*cab
= (struct cabinet
*) calloc(1, sizeof(struct cabinet
));
535 TRACE("(name == %s, offset == %u)\n", debugstr_a((char *) name
), offset
);
537 if (!cab
) return NULL
;
539 cab
->filename
= name
;
540 if ((ok
= cabinet_open(cab
))) {
541 cabinet_seek(cab
, offset
);
542 ok
= cabinet_read_entries(cab
);
551 /* MSZIP decruncher */
553 /* Dirk Stoecker wrote the ZIP decoder, based on the InfoZip deflate code */
555 /* Tables for deflate from PKZIP's appnote.txt. */
556 static const cab_UBYTE Zipborder
[] = /* Order of the bit length code lengths */
557 { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
558 static const cab_UWORD Zipcplens
[] = /* Copy lengths for literal codes 257..285 */
559 { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51,
560 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
561 static const cab_UWORD Zipcplext
[] = /* Extra bits for literal codes 257..285 */
562 { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,
563 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
564 static const cab_UWORD Zipcpdist
[] = /* Copy offsets for distance codes 0..29 */
565 { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385,
566 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
567 static const cab_UWORD Zipcpdext
[] = /* Extra bits for distance codes */
568 { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
569 10, 11, 11, 12, 12, 13, 13};
571 /* And'ing with Zipmask[n] masks the lower n bits */
572 static const cab_UWORD Zipmask
[17] = {
573 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
574 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
577 #define ZIPNEEDBITS(n) {while(k<(n)){cab_LONG c=*(ZIP(inpos)++);\
578 b|=((cab_ULONG)c)<<k;k+=8;}}
579 #define ZIPDUMPBITS(n) {b>>=(n);k-=(n);}
582 /********************************************************
583 * Ziphuft_free (internal)
585 void Ziphuft_free(struct Ziphuft
*t
)
587 register struct Ziphuft
*p
, *q
;
589 /* Go through linked list, freeing from the allocated (t[-1]) address. */
591 while (p
!= (struct Ziphuft
*)NULL
)
599 /*********************************************************
600 * Ziphuft_build (internal)
602 cab_LONG
Ziphuft_build(cab_ULONG
*b
, cab_ULONG n
, cab_ULONG s
, cab_UWORD
*d
, cab_UWORD
*e
,
603 struct Ziphuft
**t
, cab_LONG
*m
)
605 cab_ULONG a
; /* counter for codes of length k */
606 cab_ULONG el
; /* length of EOB code (value 256) */
607 cab_ULONG f
; /* i repeats in table every f entries */
608 cab_LONG g
; /* maximum code length */
609 cab_LONG h
; /* table level */
610 register cab_ULONG i
; /* counter, current code */
611 register cab_ULONG j
; /* counter */
612 register cab_LONG k
; /* number of bits in current code */
613 cab_LONG
*l
; /* stack of bits per table */
614 register cab_ULONG
*p
; /* pointer into ZIP(c)[],ZIP(b)[],ZIP(v)[] */
615 register struct Ziphuft
*q
; /* points to current table */
616 struct Ziphuft r
; /* table entry for structure assignment */
617 register cab_LONG w
; /* bits before this table == (l * h) */
618 cab_ULONG
*xp
; /* pointer into x */
619 cab_LONG y
; /* number of dummy codes added */
620 cab_ULONG z
; /* number of entries in current table */
624 /* Generate counts for each bit length */
625 el
= n
> 256 ? b
[256] : ZIPBMAX
; /* set length of EOB code, if any */
627 for(i
= 0; i
< ZIPBMAX
+1; ++i
)
632 ZIP(c
)[*p
]++; p
++; /* assume all entries <= ZIPBMAX */
634 if (ZIP(c
)[0] == n
) /* null input--all zero length codes */
636 *t
= (struct Ziphuft
*)NULL
;
641 /* Find minimum and maximum length, bound *m by those */
642 for (j
= 1; j
<= ZIPBMAX
; j
++)
645 k
= j
; /* minimum code length */
646 if ((cab_ULONG
)*m
< j
)
648 for (i
= ZIPBMAX
; i
; i
--)
651 g
= i
; /* maximum code length */
652 if ((cab_ULONG
)*m
> i
)
655 /* Adjust last length count to fill out codes, if needed */
656 for (y
= 1 << j
; j
< i
; j
++, y
<<= 1)
657 if ((y
-= ZIP(c
)[j
]) < 0)
658 return 2; /* bad input: more codes than bits */
659 if ((y
-= ZIP(c
)[i
]) < 0)
663 /* Generate starting offsets LONGo the value table for each length */
665 p
= ZIP(c
) + 1; xp
= ZIP(x
) + 2;
667 { /* note that i == g from above */
671 /* Make a table of values in order of bit lengths */
675 ZIP(v
)[ZIP(x
)[j
]++] = i
;
679 /* Generate the Huffman codes and for each, make the table entries */
680 ZIP(x
)[0] = i
= 0; /* first Huffman code is zero */
681 p
= ZIP(v
); /* grab values in bit order */
682 h
= -1; /* no tables yet--level -1 */
683 w
= l
[-1] = 0; /* no bits decoded yet */
684 ZIP(u
)[0] = (struct Ziphuft
*)NULL
; /* just to keep compilers happy */
685 q
= (struct Ziphuft
*)NULL
; /* ditto */
688 /* go through the bit lengths (k already is bits in shortest code) */
694 /* here i is the Huffman code of length k bits for value *p */
695 /* make tables up to required level */
698 w
+= l
[h
++]; /* add bits already decoded */
700 /* compute minimum size table less than or equal to *m bits */
701 z
= (z
= g
- w
) > (cab_ULONG
)*m
? *m
: z
; /* upper limit */
702 if ((f
= 1 << (j
= k
- w
)) > a
+ 1) /* try a k-w bit table */
703 { /* too few codes for k-w bit table */
704 f
-= a
+ 1; /* deduct codes from patterns left */
706 while (++j
< z
) /* try smaller tables up to z bits */
708 if ((f
<<= 1) <= *++xp
)
709 break; /* enough codes to use up j bits */
710 f
-= *xp
; /* else deduct codes from patterns */
713 if ((cab_ULONG
)w
+ j
> el
&& (cab_ULONG
)w
< el
)
714 j
= el
- w
; /* make EOB code end at table */
715 z
= 1 << j
; /* table entries for j-bit table */
716 l
[h
] = j
; /* set table size in stack */
718 /* allocate and link in new table */
719 if (!(q
= (struct Ziphuft
*) malloc((z
+ 1)*sizeof(struct Ziphuft
))))
722 Ziphuft_free(ZIP(u
)[0]);
723 return 3; /* not enough memory */
725 *t
= q
+ 1; /* link to list for Ziphuft_free() */
726 *(t
= &(q
->v
.t
)) = (struct Ziphuft
*)NULL
;
727 ZIP(u
)[h
] = ++q
; /* table starts after link */
729 /* connect to last table, if there is one */
732 ZIP(x
)[h
] = i
; /* save pattern for backing up */
733 r
.b
= (cab_UBYTE
)l
[h
-1]; /* bits to dump before this table */
734 r
.e
= (cab_UBYTE
)(16 + j
); /* bits in this table */
735 r
.v
.t
= q
; /* pointer to this table */
736 j
= (i
& ((1 << w
) - 1)) >> (w
- l
[h
-1]);
737 ZIP(u
)[h
-1][j
] = r
; /* connect to last table */
741 /* set up table entry in r */
742 r
.b
= (cab_UBYTE
)(k
- w
);
744 r
.e
= 99; /* out of values--invalid code */
747 r
.e
= (cab_UBYTE
)(*p
< 256 ? 16 : 15); /* 256 is end-of-block code */
748 r
.v
.n
= *p
++; /* simple code is just the value */
752 r
.e
= (cab_UBYTE
)e
[*p
- s
]; /* non-simple--look up in lists */
756 /* fill code-like entries with r */
758 for (j
= i
>> w
; j
< z
; j
+= f
)
761 /* backwards increment the k-bit code i */
762 for (j
= 1 << (k
- 1); i
& j
; j
>>= 1)
766 /* backup over finished tables */
767 while ((i
& ((1 << w
) - 1)) != ZIP(x
)[h
])
768 w
-= l
[--h
]; /* don't need to update q */
772 /* return actual size of base table */
775 /* Return true (1) if we were given an incomplete table */
776 return y
!= 0 && g
!= 1;
779 /*********************************************************
780 * Zipinflate_codes (internal)
782 cab_LONG
Zipinflate_codes(struct Ziphuft
*tl
, struct Ziphuft
*td
,
783 cab_LONG bl
, cab_LONG bd
)
785 register cab_ULONG e
; /* table entry flag/number of extra bits */
786 cab_ULONG n
, d
; /* length and index for copy */
787 cab_ULONG w
; /* current window position */
788 struct Ziphuft
*t
; /* pointer to table entry */
789 cab_ULONG ml
, md
; /* masks for bl and bd bits */
790 register cab_ULONG b
; /* bit buffer */
791 register cab_ULONG k
; /* number of bits in bit buffer */
793 /* make local copies of globals */
794 b
= ZIP(bb
); /* initialize bit buffer */
796 w
= ZIP(window_posn
); /* initialize window position */
798 /* inflate the coded data */
799 ml
= Zipmask
[bl
]; /* precompute masks for speed */
804 ZIPNEEDBITS((cab_ULONG
)bl
)
805 if((e
= (t
= tl
+ ((cab_ULONG
)b
& ml
))->e
) > 16)
813 } while ((e
= (t
= t
->v
.t
+ ((cab_ULONG
)b
& Zipmask
[e
]))->e
) > 16);
815 if (e
== 16) /* then it's a literal */
816 CAB(outbuf
)[w
++] = (cab_UBYTE
)t
->v
.n
;
817 else /* it's an EOB or a length */
819 /* exit if end of block */
823 /* get length of block to copy */
825 n
= t
->v
.n
+ ((cab_ULONG
)b
& Zipmask
[e
]);
828 /* decode distance of block to copy */
829 ZIPNEEDBITS((cab_ULONG
)bd
)
830 if ((e
= (t
= td
+ ((cab_ULONG
)b
& md
))->e
) > 16)
837 } while ((e
= (t
= t
->v
.t
+ ((cab_ULONG
)b
& Zipmask
[e
]))->e
) > 16);
840 d
= w
- t
->v
.n
- ((cab_ULONG
)b
& Zipmask
[e
]);
844 n
-= (e
= (e
= ZIPWSIZE
- ((d
&= ZIPWSIZE
-1) > w
? d
: w
)) > n
?n
:e
);
847 CAB(outbuf
)[w
++] = CAB(outbuf
)[d
++];
853 /* restore the globals from the locals */
854 ZIP(window_posn
) = w
; /* restore global window pointer */
855 ZIP(bb
) = b
; /* restore global bit buffer */
862 /***********************************************************
863 * Zipinflate_stored (internal)
865 cab_LONG
Zipinflate_stored(void)
866 /* "decompress" an inflated type 0 (stored) block. */
868 cab_ULONG n
; /* number of bytes in block */
869 cab_ULONG w
; /* current window position */
870 register cab_ULONG b
; /* bit buffer */
871 register cab_ULONG k
; /* number of bits in bit buffer */
873 /* make local copies of globals */
874 b
= ZIP(bb
); /* initialize bit buffer */
876 w
= ZIP(window_posn
); /* initialize window position */
878 /* go to byte boundary */
882 /* get the length and its complement */
884 n
= ((cab_ULONG
)b
& 0xffff);
887 if (n
!= (cab_ULONG
)((~b
) & 0xffff))
888 return 1; /* error in compressed data */
891 /* read and output the compressed data */
895 CAB(outbuf
)[w
++] = (cab_UBYTE
)b
;
899 /* restore the globals from the locals */
900 ZIP(window_posn
) = w
; /* restore global window pointer */
901 ZIP(bb
) = b
; /* restore global bit buffer */
906 /******************************************************
907 * Zipinflate_fixed (internal)
909 cab_LONG
Zipinflate_fixed(void)
911 struct Ziphuft
*fixed_tl
;
912 struct Ziphuft
*fixed_td
;
913 cab_LONG fixed_bl
, fixed_bd
;
914 cab_LONG i
; /* temporary variable */
920 for(i
= 0; i
< 144; i
++)
926 for(; i
< 288; i
++) /* make a complete, but wrong code set */
929 if((i
= Ziphuft_build(l
, 288, 257, (cab_UWORD
*) Zipcplens
,
930 (cab_UWORD
*) Zipcplext
, &fixed_tl
, &fixed_bl
)))
934 for(i
= 0; i
< 30; i
++) /* make an incomplete code set */
937 if((i
= Ziphuft_build(l
, 30, 0, (cab_UWORD
*) Zipcpdist
, (cab_UWORD
*) Zipcpdext
,
938 &fixed_td
, &fixed_bd
)) > 1)
940 Ziphuft_free(fixed_tl
);
944 /* decompress until an end-of-block code */
945 i
= Zipinflate_codes(fixed_tl
, fixed_td
, fixed_bl
, fixed_bd
);
947 Ziphuft_free(fixed_td
);
948 Ziphuft_free(fixed_tl
);
952 /**************************************************************
953 * Zipinflate_dynamic (internal)
955 cab_LONG
Zipinflate_dynamic(void)
956 /* decompress an inflated type 2 (dynamic Huffman codes) block. */
958 cab_LONG i
; /* temporary variables */
961 cab_ULONG l
; /* last length */
962 cab_ULONG m
; /* mask for bit lengths table */
963 cab_ULONG n
; /* number of lengths to get */
964 struct Ziphuft
*tl
; /* literal/length code table */
965 struct Ziphuft
*td
; /* distance code table */
966 cab_LONG bl
; /* lookup bits for tl */
967 cab_LONG bd
; /* lookup bits for td */
968 cab_ULONG nb
; /* number of bit length codes */
969 cab_ULONG nl
; /* number of literal/length codes */
970 cab_ULONG nd
; /* number of distance codes */
971 register cab_ULONG b
; /* bit buffer */
972 register cab_ULONG k
; /* number of bits in bit buffer */
974 /* make local bit buffer */
979 /* read in table lengths */
981 nl
= 257 + ((cab_ULONG
)b
& 0x1f); /* number of literal/length codes */
984 nd
= 1 + ((cab_ULONG
)b
& 0x1f); /* number of distance codes */
987 nb
= 4 + ((cab_ULONG
)b
& 0xf); /* number of bit length codes */
989 if(nl
> 288 || nd
> 32)
990 return 1; /* bad lengths */
992 /* read in bit-length-code lengths */
993 for(j
= 0; j
< nb
; j
++)
996 ll
[Zipborder
[j
]] = (cab_ULONG
)b
& 7;
1000 ll
[Zipborder
[j
]] = 0;
1002 /* build decoding table for trees--single level, 7 bit lookup */
1004 if((i
= Ziphuft_build(ll
, 19, 19, NULL
, NULL
, &tl
, &bl
)) != 0)
1008 return i
; /* incomplete code set */
1011 /* read in literal and distance code lengths */
1015 while((cab_ULONG
)i
< n
)
1017 ZIPNEEDBITS((cab_ULONG
)bl
)
1018 j
= (td
= tl
+ ((cab_ULONG
)b
& m
))->b
;
1021 if (j
< 16) /* length of code in bits (0..15) */
1022 ll
[i
++] = l
= j
; /* save last length in l */
1023 else if (j
== 16) /* repeat last length 3 to 6 times */
1026 j
= 3 + ((cab_ULONG
)b
& 3);
1028 if((cab_ULONG
)i
+ j
> n
)
1033 else if (j
== 17) /* 3 to 10 zero length codes */
1036 j
= 3 + ((cab_ULONG
)b
& 7);
1038 if ((cab_ULONG
)i
+ j
> n
)
1044 else /* j == 18: 11 to 138 zero length codes */
1047 j
= 11 + ((cab_ULONG
)b
& 0x7f);
1049 if ((cab_ULONG
)i
+ j
> n
)
1057 /* free decoding table for trees */
1060 /* restore the global bit buffer */
1064 /* build the decoding tables for literal/length and distance codes */
1066 if((i
= Ziphuft_build(ll
, nl
, 257, (cab_UWORD
*) Zipcplens
, (cab_UWORD
*) Zipcplext
, &tl
, &bl
)) != 0)
1070 return i
; /* incomplete code set */
1073 Ziphuft_build(ll
+ nl
, nd
, 0, (cab_UWORD
*) Zipcpdist
, (cab_UWORD
*) Zipcpdext
, &td
, &bd
);
1075 /* decompress until an end-of-block code */
1076 if(Zipinflate_codes(tl
, td
, bl
, bd
))
1079 /* free the decoding tables, return */
1085 /*****************************************************
1086 * Zipinflate_block (internal)
1088 cab_LONG
Zipinflate_block(cab_LONG
*e
) /* e == last block flag */
1089 { /* decompress an inflated block */
1090 cab_ULONG t
; /* block type */
1091 register cab_ULONG b
; /* bit buffer */
1092 register cab_ULONG k
; /* number of bits in bit buffer */
1094 /* make local bit buffer */
1098 /* read in last block bit */
1100 *e
= (cab_LONG
)b
& 1;
1103 /* read in block type */
1105 t
= (cab_ULONG
)b
& 3;
1108 /* restore the global bit buffer */
1112 /* inflate that block type */
1114 return Zipinflate_dynamic();
1116 return Zipinflate_stored();
1118 return Zipinflate_fixed();
1119 /* bad block type */
1123 /****************************************************
1124 * Zipdecompress (internal)
1126 int ZIPdecompress(int inlen
, int outlen
)
1128 cab_LONG e
; /* last block flag */
1130 TRACE("(inlen == %d, outlen == %d)\n", inlen
, outlen
);
1132 ZIP(inpos
) = CAB(inbuf
);
1133 ZIP(bb
) = ZIP(bk
) = ZIP(window_posn
) = 0;
1134 if(outlen
> ZIPWSIZE
)
1135 return DECR_DATAFORMAT
;
1137 /* CK = Chris Kirmse, official Microsoft purloiner */
1138 if(ZIP(inpos
)[0] != 0x43 || ZIP(inpos
)[1] != 0x4B)
1139 return DECR_ILLEGALDATA
;
1144 if(Zipinflate_block(&e
))
1145 return DECR_ILLEGALDATA
;
1148 /* return success */
1152 /* Quantum decruncher */
1154 /* This decruncher was researched and implemented by Matthew Russoto. */
1155 /* It has since been tidied up by Stuart Caie */
1157 static cab_UBYTE q_length_base
[27], q_length_extra
[27], q_extra_bits
[42];
1158 static cab_ULONG q_position_base
[42];
1160 /******************************************************************
1161 * QTMinitmodel (internal)
1163 * Initialise a model which decodes symbols from [s] to [s]+[n]-1
1165 void QTMinitmodel(struct QTMmodel
*m
, struct QTMmodelsym
*sym
, int n
, int s
) {
1170 memset(m
->tabloc
, 0xFF, sizeof(m
->tabloc
)); /* clear out look-up table */
1171 for (i
= 0; i
< n
; i
++) {
1172 m
->tabloc
[i
+s
] = i
; /* set up a look-up entry for symbol */
1173 m
->syms
[i
].sym
= i
+s
; /* actual symbol */
1174 m
->syms
[i
].cumfreq
= n
-i
; /* current frequency of that symbol */
1176 m
->syms
[n
].cumfreq
= 0;
1179 /******************************************************************
1180 * QTMinit (internal)
1182 int QTMinit(int window
, int level
) {
1183 int wndsize
= 1 << window
, msz
= window
* 2, i
;
1186 /* QTM supports window sizes of 2^10 (1Kb) through 2^21 (2Mb) */
1187 /* if a previously allocated window is big enough, keep it */
1188 if (window
< 10 || window
> 21) return DECR_DATAFORMAT
;
1189 if (QTM(actual_size
) < wndsize
) {
1190 if (QTM(window
)) free(QTM(window
));
1194 if (!(QTM(window
) = malloc(wndsize
))) return DECR_NOMEMORY
;
1195 QTM(actual_size
) = wndsize
;
1197 QTM(window_size
) = wndsize
;
1198 QTM(window_posn
) = 0;
1200 /* initialise static slot/extrabits tables */
1201 for (i
= 0, j
= 0; i
< 27; i
++) {
1202 q_length_extra
[i
] = (i
== 26) ? 0 : (i
< 2 ? 0 : i
- 2) >> 2;
1203 q_length_base
[i
] = j
; j
+= 1 << ((i
== 26) ? 5 : q_length_extra
[i
]);
1205 for (i
= 0, j
= 0; i
< 42; i
++) {
1206 q_extra_bits
[i
] = (i
< 2 ? 0 : i
-2) >> 1;
1207 q_position_base
[i
] = j
; j
+= 1 << q_extra_bits
[i
];
1210 /* initialise arithmetic coding models */
1212 QTMinitmodel(&QTM(model7
), &QTM(m7sym
)[0], 7, 0);
1214 QTMinitmodel(&QTM(model00
), &QTM(m00sym
)[0], 0x40, 0x00);
1215 QTMinitmodel(&QTM(model40
), &QTM(m40sym
)[0], 0x40, 0x40);
1216 QTMinitmodel(&QTM(model80
), &QTM(m80sym
)[0], 0x40, 0x80);
1217 QTMinitmodel(&QTM(modelC0
), &QTM(mC0sym
)[0], 0x40, 0xC0);
1219 /* model 4 depends on table size, ranges from 20 to 24 */
1220 QTMinitmodel(&QTM(model4
), &QTM(m4sym
)[0], (msz
< 24) ? msz
: 24, 0);
1221 /* model 5 depends on table size, ranges from 20 to 36 */
1222 QTMinitmodel(&QTM(model5
), &QTM(m5sym
)[0], (msz
< 36) ? msz
: 36, 0);
1223 /* model 6pos depends on table size, ranges from 20 to 42 */
1224 QTMinitmodel(&QTM(model6pos
), &QTM(m6psym
)[0], msz
, 0);
1225 QTMinitmodel(&QTM(model6len
), &QTM(m6lsym
)[0], 27, 0);
1230 /****************************************************************
1231 * QTMupdatemodel (internal)
1233 void QTMupdatemodel(struct QTMmodel
*model
, int sym
) {
1234 struct QTMmodelsym temp
;
1237 for (i
= 0; i
< sym
; i
++) model
->syms
[i
].cumfreq
+= 8;
1239 if (model
->syms
[0].cumfreq
> 3800) {
1240 if (--model
->shiftsleft
) {
1241 for (i
= model
->entries
- 1; i
>= 0; i
--) {
1242 /* -1, not -2; the 0 entry saves this */
1243 model
->syms
[i
].cumfreq
>>= 1;
1244 if (model
->syms
[i
].cumfreq
<= model
->syms
[i
+1].cumfreq
) {
1245 model
->syms
[i
].cumfreq
= model
->syms
[i
+1].cumfreq
+ 1;
1250 model
->shiftsleft
= 50;
1251 for (i
= 0; i
< model
->entries
; i
++) {
1252 /* no -1, want to include the 0 entry */
1253 /* this converts cumfreqs into frequencies, then shifts right */
1254 model
->syms
[i
].cumfreq
-= model
->syms
[i
+1].cumfreq
;
1255 model
->syms
[i
].cumfreq
++; /* avoid losing things entirely */
1256 model
->syms
[i
].cumfreq
>>= 1;
1259 /* now sort by frequencies, decreasing order -- this must be an
1260 * inplace selection sort, or a sort with the same (in)stability
1263 for (i
= 0; i
< model
->entries
- 1; i
++) {
1264 for (j
= i
+ 1; j
< model
->entries
; j
++) {
1265 if (model
->syms
[i
].cumfreq
< model
->syms
[j
].cumfreq
) {
1266 temp
= model
->syms
[i
];
1267 model
->syms
[i
] = model
->syms
[j
];
1268 model
->syms
[j
] = temp
;
1273 /* then convert frequencies back to cumfreq */
1274 for (i
= model
->entries
- 1; i
>= 0; i
--) {
1275 model
->syms
[i
].cumfreq
+= model
->syms
[i
+1].cumfreq
;
1277 /* then update the other part of the table */
1278 for (i
= 0; i
< model
->entries
; i
++) {
1279 model
->tabloc
[model
->syms
[i
].sym
] = i
;
1285 /* Bitstream reading macros (Quantum / normal byte order)
1287 * Q_INIT_BITSTREAM should be used first to set up the system
1288 * Q_READ_BITS(var,n) takes N bits from the buffer and puts them in var.
1289 * unlike LZX, this can loop several times to get the
1290 * requisite number of bits.
1291 * Q_FILL_BUFFER adds more data to the bit buffer, if there is room
1292 * for another 16 bits.
1293 * Q_PEEK_BITS(n) extracts (without removing) N bits from the bit
1295 * Q_REMOVE_BITS(n) removes N bits from the bit buffer
1297 * These bit access routines work by using the area beyond the MSB and the
1298 * LSB as a free source of zeroes. This avoids having to mask any bits.
1299 * So we have to know the bit width of the bitbuffer variable. This is
1300 * defined as ULONG_BITS.
1302 * ULONG_BITS should be at least 16 bits. Unlike LZX's Huffman decoding,
1303 * Quantum's arithmetic decoding only needs 1 bit at a time, it doesn't
1304 * need an assured number. Retrieving larger bitstrings can be done with
1305 * multiple reads and fills of the bitbuffer. The code should work fine
1306 * for machines where ULONG >= 32 bits.
1308 * Also note that Quantum reads bytes in normal order; LZX is in
1309 * little-endian order.
1312 #define Q_INIT_BITSTREAM do { bitsleft = 0; bitbuf = 0; } while (0)
1314 #define Q_FILL_BUFFER do { \
1315 if (bitsleft <= (CAB_ULONG_BITS - 16)) { \
1316 bitbuf |= ((inpos[0]<<8)|inpos[1]) << (CAB_ULONG_BITS-16 - bitsleft); \
1317 bitsleft += 16; inpos += 2; \
1321 #define Q_PEEK_BITS(n) (bitbuf >> (CAB_ULONG_BITS - (n)))
1322 #define Q_REMOVE_BITS(n) ((bitbuf <<= (n)), (bitsleft -= (n)))
1324 #define Q_READ_BITS(v,n) do { \
1326 for (bitsneed = (n); bitsneed; bitsneed -= bitrun) { \
1328 bitrun = (bitsneed > bitsleft) ? bitsleft : bitsneed; \
1329 (v) = ((v) << bitrun) | Q_PEEK_BITS(bitrun); \
1330 Q_REMOVE_BITS(bitrun); \
1334 #define Q_MENTRIES(model) (QTM(model).entries)
1335 #define Q_MSYM(model,symidx) (QTM(model).syms[(symidx)].sym)
1336 #define Q_MSYMFREQ(model,symidx) (QTM(model).syms[(symidx)].cumfreq)
1338 /* GET_SYMBOL(model, var) fetches the next symbol from the stated model
1339 * and puts it in var. it may need to read the bitstream to do this.
1341 #define GET_SYMBOL(m, var) do { \
1342 range = ((H - L) & 0xFFFF) + 1; \
1343 symf = ((((C - L + 1) * Q_MSYMFREQ(m,0)) - 1) / range) & 0xFFFF; \
1345 for (i=1; i < Q_MENTRIES(m); i++) { \
1346 if (Q_MSYMFREQ(m,i) <= symf) break; \
1348 (var) = Q_MSYM(m,i-1); \
1350 range = (H - L) + 1; \
1351 H = L + ((Q_MSYMFREQ(m,i-1) * range) / Q_MSYMFREQ(m,0)) - 1; \
1352 L = L + ((Q_MSYMFREQ(m,i) * range) / Q_MSYMFREQ(m,0)); \
1354 if ((L & 0x8000) != (H & 0x8000)) { \
1355 if ((L & 0x4000) && !(H & 0x4000)) { \
1356 /* underflow case */ \
1357 C ^= 0x4000; L &= 0x3FFF; H |= 0x4000; \
1361 L <<= 1; H = (H << 1) | 1; \
1363 C = (C << 1) | Q_PEEK_BITS(1); \
1367 QTMupdatemodel(&(QTM(m)), i); \
1370 /*******************************************************************
1371 * QTMdecompress (internal)
1373 int QTMdecompress(int inlen
, int outlen
)
1375 cab_UBYTE
*inpos
= CAB(inbuf
);
1376 cab_UBYTE
*window
= QTM(window
);
1377 cab_UBYTE
*runsrc
, *rundest
;
1379 cab_ULONG window_posn
= QTM(window_posn
);
1380 cab_ULONG window_size
= QTM(window_size
);
1382 /* used by bitstream macros */
1383 register int bitsleft
, bitrun
, bitsneed
;
1384 register cab_ULONG bitbuf
;
1386 /* used by GET_SYMBOL */
1391 int extra
, togo
= outlen
, match_length
= 0, copy_length
;
1392 cab_UBYTE selector
, sym
;
1393 cab_ULONG match_offset
= 0;
1395 cab_UWORD H
= 0xFFFF, L
= 0, C
;
1397 TRACE("(inlen == %d, outlen == %d)\n", inlen
, outlen
);
1399 /* read initial value of C */
1403 /* apply 2^x-1 mask */
1404 window_posn
&= window_size
- 1;
1405 /* runs can't straddle the window wraparound */
1406 if ((window_posn
+ togo
) > window_size
) {
1407 TRACE("straddled run\n");
1408 return DECR_DATAFORMAT
;
1412 GET_SYMBOL(model7
, selector
);
1415 GET_SYMBOL(model00
, sym
); window
[window_posn
++] = sym
; togo
--;
1418 GET_SYMBOL(model40
, sym
); window
[window_posn
++] = sym
; togo
--;
1421 GET_SYMBOL(model80
, sym
); window
[window_posn
++] = sym
; togo
--;
1424 GET_SYMBOL(modelC0
, sym
); window
[window_posn
++] = sym
; togo
--;
1428 /* selector 4 = fixed length of 3 */
1429 GET_SYMBOL(model4
, sym
);
1430 Q_READ_BITS(extra
, q_extra_bits
[sym
]);
1431 match_offset
= q_position_base
[sym
] + extra
+ 1;
1436 /* selector 5 = fixed length of 4 */
1437 GET_SYMBOL(model5
, sym
);
1438 Q_READ_BITS(extra
, q_extra_bits
[sym
]);
1439 match_offset
= q_position_base
[sym
] + extra
+ 1;
1444 /* selector 6 = variable length */
1445 GET_SYMBOL(model6len
, sym
);
1446 Q_READ_BITS(extra
, q_length_extra
[sym
]);
1447 match_length
= q_length_base
[sym
] + extra
+ 5;
1448 GET_SYMBOL(model6pos
, sym
);
1449 Q_READ_BITS(extra
, q_extra_bits
[sym
]);
1450 match_offset
= q_position_base
[sym
] + extra
+ 1;
1454 TRACE("Selector is bogus\n");
1455 return DECR_ILLEGALDATA
;
1458 /* if this is a match */
1459 if (selector
>= 4) {
1460 rundest
= window
+ window_posn
;
1461 togo
-= match_length
;
1463 /* copy any wrapped around source data */
1464 if (window_posn
>= match_offset
) {
1466 runsrc
= rundest
- match_offset
;
1468 runsrc
= rundest
+ (window_size
- match_offset
);
1469 copy_length
= match_offset
- window_posn
;
1470 if (copy_length
< match_length
) {
1471 match_length
-= copy_length
;
1472 window_posn
+= copy_length
;
1473 while (copy_length
-- > 0) *rundest
++ = *runsrc
++;
1477 window_posn
+= match_length
;
1479 /* copy match data - no worries about destination wraps */
1480 while (match_length
-- > 0) *rundest
++ = *runsrc
++;
1482 } /* while (togo > 0) */
1485 TRACE("Frame overflow, this_run = %d\n", togo
);
1486 return DECR_ILLEGALDATA
;
1489 memcpy(CAB(outbuf
), window
+ ((!window_posn
) ? window_size
: window_posn
) -
1492 QTM(window_posn
) = window_posn
;
1496 /* LZX decruncher */
1498 /* Microsoft's LZX document and their implementation of the
1499 * com.ms.util.cab Java package do not concur.
1501 * In the LZX document, there is a table showing the correlation between
1502 * window size and the number of position slots. It states that the 1MB
1503 * window = 40 slots and the 2MB window = 42 slots. In the implementation,
1504 * 1MB = 42 slots, 2MB = 50 slots. The actual calculation is 'find the
1505 * first slot whose position base is equal to or more than the required
1506 * window size'. This would explain why other tables in the document refer
1507 * to 50 slots rather than 42.
1509 * The constant NUM_PRIMARY_LENGTHS used in the decompression pseudocode
1510 * is not defined in the specification.
1512 * The LZX document does not state the uncompressed block has an
1513 * uncompressed length field. Where does this length field come from, so
1514 * we can know how large the block is? The implementation has it as the 24
1515 * bits following after the 3 blocktype bits, before the alignment
1518 * The LZX document states that aligned offset blocks have their aligned
1519 * offset huffman tree AFTER the main and length trees. The implementation
1520 * suggests that the aligned offset tree is BEFORE the main and length
1523 * The LZX document decoding algorithm states that, in an aligned offset
1524 * block, if an extra_bits value is 1, 2 or 3, then that number of bits
1525 * should be read and the result added to the match offset. This is
1526 * correct for 1 and 2, but not 3, where just a huffman symbol (using the
1527 * aligned tree) should be read.
1529 * Regarding the E8 preprocessing, the LZX document states 'No translation
1530 * may be performed on the last 6 bytes of the input block'. This is
1531 * correct. However, the pseudocode provided checks for the *E8 leader*
1532 * up to the last 6 bytes. If the leader appears between -10 and -7 bytes
1533 * from the end, this would cause the next four bytes to be modified, at
1534 * least one of which would be in the last 6 bytes, which is not allowed
1535 * according to the spec.
1537 * The specification states that the huffman trees must always contain at
1538 * least one element. However, many CAB files contain blocks where the
1539 * length tree is completely empty (because there are no matches), and
1540 * this is expected to succeed.
1544 /* LZX uses what it calls 'position slots' to represent match offsets.
1545 * What this means is that a small 'position slot' number and a small
1546 * offset from that slot are encoded instead of one large offset for
1548 * - lzx_position_base is an index to the position slot bases
1549 * - lzx_extra_bits states how many bits of offset-from-base data is needed.
1551 static cab_ULONG lzx_position_base
[51];
1552 static cab_UBYTE extra_bits
[51];
1554 /************************************************************
1555 * LZXinit (internal)
1557 int LZXinit(int window
) {
1558 cab_ULONG wndsize
= 1 << window
;
1559 int i
, j
, posn_slots
;
1561 /* LZX supports window sizes of 2^15 (32Kb) through 2^21 (2Mb) */
1562 /* if a previously allocated window is big enough, keep it */
1563 if (window
< 15 || window
> 21) return DECR_DATAFORMAT
;
1564 if (LZX(actual_size
) < wndsize
) {
1565 if (LZX(window
)) free(LZX(window
));
1569 if (!(LZX(window
) = malloc(wndsize
))) return DECR_NOMEMORY
;
1570 LZX(actual_size
) = wndsize
;
1572 LZX(window_size
) = wndsize
;
1574 /* initialise static tables */
1575 for (i
=0, j
=0; i
<= 50; i
+= 2) {
1576 extra_bits
[i
] = extra_bits
[i
+1] = j
; /* 0,0,0,0,1,1,2,2,3,3... */
1577 if ((i
!= 0) && (j
< 17)) j
++; /* 0,0,1,2,3,4...15,16,17,17,17,17... */
1579 for (i
=0, j
=0; i
<= 50; i
++) {
1580 lzx_position_base
[i
] = j
; /* 0,1,2,3,4,6,8,12,16,24,32,... */
1581 j
+= 1 << extra_bits
[i
]; /* 1,1,1,1,2,2,4,4,8,8,16,16,32,32,... */
1584 /* calculate required position slots */
1585 if (window
== 20) posn_slots
= 42;
1586 else if (window
== 21) posn_slots
= 50;
1587 else posn_slots
= window
<< 1;
1589 /*posn_slots=i=0; while (i < wndsize) i += 1 << extra_bits[posn_slots++]; */
1591 LZX(R0
) = LZX(R1
) = LZX(R2
) = 1;
1592 LZX(main_elements
) = LZX_NUM_CHARS
+ (posn_slots
<< 3);
1593 LZX(header_read
) = 0;
1594 LZX(frames_read
) = 0;
1595 LZX(block_remaining
) = 0;
1596 LZX(block_type
) = LZX_BLOCKTYPE_INVALID
;
1597 LZX(intel_curpos
) = 0;
1598 LZX(intel_started
) = 0;
1599 LZX(window_posn
) = 0;
1601 /* initialise tables to 0 (because deltas will be applied to them) */
1602 for (i
= 0; i
< LZX_MAINTREE_MAXSYMBOLS
; i
++) LZX(MAINTREE_len
)[i
] = 0;
1603 for (i
= 0; i
< LZX_LENGTH_MAXSYMBOLS
; i
++) LZX(LENGTH_len
)[i
] = 0;
1608 /* Bitstream reading macros (LZX / intel little-endian byte order)
1610 * INIT_BITSTREAM should be used first to set up the system
1611 * READ_BITS(var,n) takes N bits from the buffer and puts them in var
1613 * ENSURE_BITS(n) ensures there are at least N bits in the bit buffer.
1614 * it can guarantee up to 17 bits (i.e. it can read in
1615 * 16 new bits when there is down to 1 bit in the buffer,
1616 * and it can read 32 bits when there are 0 bits in the
1618 * PEEK_BITS(n) extracts (without removing) N bits from the bit buffer
1619 * REMOVE_BITS(n) removes N bits from the bit buffer
1621 * These bit access routines work by using the area beyond the MSB and the
1622 * LSB as a free source of zeroes. This avoids having to mask any bits.
1623 * So we have to know the bit width of the bitbuffer variable.
1626 #define INIT_BITSTREAM do { bitsleft = 0; bitbuf = 0; } while (0)
1628 /* Quantum reads bytes in normal order; LZX is little-endian order */
1629 #define ENSURE_BITS(n) \
1630 while (bitsleft < (n)) { \
1631 bitbuf |= ((inpos[1]<<8)|inpos[0]) << (CAB_ULONG_BITS-16 - bitsleft); \
1632 bitsleft += 16; inpos+=2; \
1635 #define PEEK_BITS(n) (bitbuf >> (CAB_ULONG_BITS - (n)))
1636 #define REMOVE_BITS(n) ((bitbuf <<= (n)), (bitsleft -= (n)))
1638 #define READ_BITS(v,n) do { \
1641 (v) = PEEK_BITS(n); \
1649 /* Huffman macros */
1651 #define TABLEBITS(tbl) (LZX_##tbl##_TABLEBITS)
1652 #define MAXSYMBOLS(tbl) (LZX_##tbl##_MAXSYMBOLS)
1653 #define SYMTABLE(tbl) (LZX(tbl##_table))
1654 #define LENTABLE(tbl) (LZX(tbl##_len))
1656 /* BUILD_TABLE(tablename) builds a huffman lookup table from code lengths.
1657 * In reality, it just calls make_decode_table() with the appropriate
1658 * values - they're all fixed by some #defines anyway, so there's no point
1659 * writing each call out in full by hand.
1661 #define BUILD_TABLE(tbl) \
1662 if (make_decode_table( \
1663 MAXSYMBOLS(tbl), TABLEBITS(tbl), LENTABLE(tbl), SYMTABLE(tbl) \
1664 )) { return DECR_ILLEGALDATA; }
1666 /* READ_HUFFSYM(tablename, var) decodes one huffman symbol from the
1667 * bitstream using the stated table and puts it in var.
1669 #define READ_HUFFSYM(tbl,var) do { \
1671 hufftbl = SYMTABLE(tbl); \
1672 if ((i = hufftbl[PEEK_BITS(TABLEBITS(tbl))]) >= MAXSYMBOLS(tbl)) { \
1673 j = 1 << (CAB_ULONG_BITS - TABLEBITS(tbl)); \
1675 j >>= 1; i <<= 1; i |= (bitbuf & j) ? 1 : 0; \
1676 if (!j) { return DECR_ILLEGALDATA; } \
1677 } while ((i = hufftbl[i]) >= MAXSYMBOLS(tbl)); \
1679 j = LENTABLE(tbl)[(var) = i]; \
1683 /* READ_LENGTHS(tablename, first, last) reads in code lengths for symbols
1684 * first to last in the given table. The code lengths are stored in their
1685 * own special LZX way.
1687 #define READ_LENGTHS(tbl,first,last) do { \
1688 lb.bb = bitbuf; lb.bl = bitsleft; lb.ip = inpos; \
1689 if (lzx_read_lens(LENTABLE(tbl),(first),(last),&lb)) { \
1690 return DECR_ILLEGALDATA; \
1692 bitbuf = lb.bb; bitsleft = lb.bl; inpos = lb.ip; \
1695 /*************************************************************************
1696 * make_decode_table (internal)
1698 * This function was coded by David Tritscher. It builds a fast huffman
1699 * decoding table out of just a canonical huffman code lengths table.
1702 * nsyms: total number of symbols in this huffman tree.
1703 * nbits: any symbols with a code length of nbits or less can be decoded
1704 * in one lookup of the table.
1705 * length: A table to get code lengths from [0 to syms-1]
1706 * table: The table to fill up with decoded symbols and pointers.
1712 int make_decode_table(cab_ULONG nsyms
, cab_ULONG nbits
, cab_UBYTE
*length
, cab_UWORD
*table
) {
1713 register cab_UWORD sym
;
1714 register cab_ULONG leaf
;
1715 register cab_UBYTE bit_num
= 1;
1717 cab_ULONG pos
= 0; /* the current position in the decode table */
1718 cab_ULONG table_mask
= 1 << nbits
;
1719 cab_ULONG bit_mask
= table_mask
>> 1; /* don't do 0 length codes */
1720 cab_ULONG next_symbol
= bit_mask
; /* base of allocation for long codes */
1722 /* fill entries for codes short enough for a direct mapping */
1723 while (bit_num
<= nbits
) {
1724 for (sym
= 0; sym
< nsyms
; sym
++) {
1725 if (length
[sym
] == bit_num
) {
1728 if((pos
+= bit_mask
) > table_mask
) return 1; /* table overrun */
1730 /* fill all possible lookups of this symbol with the symbol itself */
1732 while (fill
-- > 0) table
[leaf
++] = sym
;
1739 /* if there are any codes longer than nbits */
1740 if (pos
!= table_mask
) {
1741 /* clear the remainder of the table */
1742 for (sym
= pos
; sym
< table_mask
; sym
++) table
[sym
] = 0;
1744 /* give ourselves room for codes to grow by up to 16 more bits */
1749 while (bit_num
<= 16) {
1750 for (sym
= 0; sym
< nsyms
; sym
++) {
1751 if (length
[sym
] == bit_num
) {
1753 for (fill
= 0; fill
< bit_num
- nbits
; fill
++) {
1754 /* if this path hasn't been taken yet, 'allocate' two entries */
1755 if (table
[leaf
] == 0) {
1756 table
[(next_symbol
<< 1)] = 0;
1757 table
[(next_symbol
<< 1) + 1] = 0;
1758 table
[leaf
] = next_symbol
++;
1760 /* follow the path and select either left or right for next bit */
1761 leaf
= table
[leaf
] << 1;
1762 if ((pos
>> (15-fill
)) & 1) leaf
++;
1766 if ((pos
+= bit_mask
) > table_mask
) return 1; /* table overflow */
1775 if (pos
== table_mask
) return 0;
1777 /* either erroneous table, or all elements are 0 - let's find out. */
1778 for (sym
= 0; sym
< nsyms
; sym
++) if (length
[sym
]) return 1;
1788 /************************************************************
1789 * lzx_read_lens (internal)
1791 int lzx_read_lens(cab_UBYTE
*lens
, cab_ULONG first
, cab_ULONG last
, struct lzx_bits
*lb
) {
1795 register cab_ULONG bitbuf
= lb
->bb
;
1796 register int bitsleft
= lb
->bl
;
1797 cab_UBYTE
*inpos
= lb
->ip
;
1800 for (x
= 0; x
< 20; x
++) {
1802 LENTABLE(PRETREE
)[x
] = y
;
1804 BUILD_TABLE(PRETREE
);
1806 for (x
= first
; x
< last
; ) {
1807 READ_HUFFSYM(PRETREE
, z
);
1809 READ_BITS(y
, 4); y
+= 4;
1810 while (y
--) lens
[x
++] = 0;
1813 READ_BITS(y
, 5); y
+= 20;
1814 while (y
--) lens
[x
++] = 0;
1817 READ_BITS(y
, 1); y
+= 4;
1818 READ_HUFFSYM(PRETREE
, z
);
1819 z
= lens
[x
] - z
; if (z
< 0) z
+= 17;
1820 while (y
--) lens
[x
++] = z
;
1823 z
= lens
[x
] - z
; if (z
< 0) z
+= 17;
1834 /*******************************************************
1835 * LZXdecompress (internal)
1837 int LZXdecompress(int inlen
, int outlen
) {
1838 cab_UBYTE
*inpos
= CAB(inbuf
);
1839 cab_UBYTE
*endinp
= inpos
+ inlen
;
1840 cab_UBYTE
*window
= LZX(window
);
1841 cab_UBYTE
*runsrc
, *rundest
;
1842 cab_UWORD
*hufftbl
; /* used in READ_HUFFSYM macro as chosen decoding table */
1844 cab_ULONG window_posn
= LZX(window_posn
);
1845 cab_ULONG window_size
= LZX(window_size
);
1846 cab_ULONG R0
= LZX(R0
);
1847 cab_ULONG R1
= LZX(R1
);
1848 cab_ULONG R2
= LZX(R2
);
1850 register cab_ULONG bitbuf
;
1851 register int bitsleft
;
1852 cab_ULONG match_offset
, i
,j
,k
; /* ijk used in READ_HUFFSYM macro */
1853 struct lzx_bits lb
; /* used in READ_LENGTHS macro */
1855 int togo
= outlen
, this_run
, main_element
, aligned_bits
;
1856 int match_length
, copy_length
, length_footer
, extra
, verbatim_bits
;
1858 TRACE("(inlen == %d, outlen == %d)\n", inlen
, outlen
);
1862 /* read header if necessary */
1863 if (!LZX(header_read
)) {
1865 READ_BITS(k
, 1); if (k
) { READ_BITS(i
,16); READ_BITS(j
,16); }
1866 LZX(intel_filesize
) = (i
<< 16) | j
; /* or 0 if not encoded */
1867 LZX(header_read
) = 1;
1870 /* main decoding loop */
1872 /* last block finished, new block expected */
1873 if (LZX(block_remaining
) == 0) {
1874 if (LZX(block_type
) == LZX_BLOCKTYPE_UNCOMPRESSED
) {
1875 if (LZX(block_length
) & 1) inpos
++; /* realign bitstream to word */
1879 READ_BITS(LZX(block_type
), 3);
1882 LZX(block_remaining
) = LZX(block_length
) = (i
<< 8) | j
;
1884 switch (LZX(block_type
)) {
1885 case LZX_BLOCKTYPE_ALIGNED
:
1886 for (i
= 0; i
< 8; i
++) { READ_BITS(j
, 3); LENTABLE(ALIGNED
)[i
] = j
; }
1887 BUILD_TABLE(ALIGNED
);
1888 /* rest of aligned header is same as verbatim */
1890 case LZX_BLOCKTYPE_VERBATIM
:
1891 READ_LENGTHS(MAINTREE
, 0, 256);
1892 READ_LENGTHS(MAINTREE
, 256, LZX(main_elements
));
1893 BUILD_TABLE(MAINTREE
);
1894 if (LENTABLE(MAINTREE
)[0xE8] != 0) LZX(intel_started
) = 1;
1896 READ_LENGTHS(LENGTH
, 0, LZX_NUM_SECONDARY_LENGTHS
);
1897 BUILD_TABLE(LENGTH
);
1900 case LZX_BLOCKTYPE_UNCOMPRESSED
:
1901 LZX(intel_started
) = 1; /* because we can't assume otherwise */
1902 ENSURE_BITS(16); /* get up to 16 pad bits into the buffer */
1903 if (bitsleft
> 16) inpos
-= 2; /* and align the bitstream! */
1904 R0
= inpos
[0]|(inpos
[1]<<8)|(inpos
[2]<<16)|(inpos
[3]<<24);inpos
+=4;
1905 R1
= inpos
[0]|(inpos
[1]<<8)|(inpos
[2]<<16)|(inpos
[3]<<24);inpos
+=4;
1906 R2
= inpos
[0]|(inpos
[1]<<8)|(inpos
[2]<<16)|(inpos
[3]<<24);inpos
+=4;
1910 return DECR_ILLEGALDATA
;
1914 /* buffer exhaustion check */
1915 if (inpos
> endinp
) {
1916 /* it's possible to have a file where the next run is less than
1917 * 16 bits in size. In this case, the READ_HUFFSYM() macro used
1918 * in building the tables will exhaust the buffer, so we should
1919 * allow for this, but not allow those accidentally read bits to
1920 * be used (so we check that there are at least 16 bits
1921 * remaining - in this boundary case they aren't really part of
1922 * the compressed data)
1924 if (inpos
> (endinp
+2) || bitsleft
< 16) return DECR_ILLEGALDATA
;
1927 while ((this_run
= LZX(block_remaining
)) > 0 && togo
> 0) {
1928 if (this_run
> togo
) this_run
= togo
;
1930 LZX(block_remaining
) -= this_run
;
1932 /* apply 2^x-1 mask */
1933 window_posn
&= window_size
- 1;
1934 /* runs can't straddle the window wraparound */
1935 if ((window_posn
+ this_run
) > window_size
)
1936 return DECR_DATAFORMAT
;
1938 switch (LZX(block_type
)) {
1940 case LZX_BLOCKTYPE_VERBATIM
:
1941 while (this_run
> 0) {
1942 READ_HUFFSYM(MAINTREE
, main_element
);
1944 if (main_element
< LZX_NUM_CHARS
) {
1945 /* literal: 0 to LZX_NUM_CHARS-1 */
1946 window
[window_posn
++] = main_element
;
1950 /* match: LZX_NUM_CHARS + ((slot<<3) | length_header (3 bits)) */
1951 main_element
-= LZX_NUM_CHARS
;
1953 match_length
= main_element
& LZX_NUM_PRIMARY_LENGTHS
;
1954 if (match_length
== LZX_NUM_PRIMARY_LENGTHS
) {
1955 READ_HUFFSYM(LENGTH
, length_footer
);
1956 match_length
+= length_footer
;
1958 match_length
+= LZX_MIN_MATCH
;
1960 match_offset
= main_element
>> 3;
1962 if (match_offset
> 2) {
1963 /* not repeated offset */
1964 if (match_offset
!= 3) {
1965 extra
= extra_bits
[match_offset
];
1966 READ_BITS(verbatim_bits
, extra
);
1967 match_offset
= lzx_position_base
[match_offset
]
1968 - 2 + verbatim_bits
;
1974 /* update repeated offset LRU queue */
1975 R2
= R1
; R1
= R0
; R0
= match_offset
;
1977 else if (match_offset
== 0) {
1980 else if (match_offset
== 1) {
1982 R1
= R0
; R0
= match_offset
;
1984 else /* match_offset == 2 */ {
1986 R2
= R0
; R0
= match_offset
;
1989 rundest
= window
+ window_posn
;
1990 this_run
-= match_length
;
1992 /* copy any wrapped around source data */
1993 if (window_posn
>= match_offset
) {
1995 runsrc
= rundest
- match_offset
;
1997 runsrc
= rundest
+ (window_size
- match_offset
);
1998 copy_length
= match_offset
- window_posn
;
1999 if (copy_length
< match_length
) {
2000 match_length
-= copy_length
;
2001 window_posn
+= copy_length
;
2002 while (copy_length
-- > 0) *rundest
++ = *runsrc
++;
2006 window_posn
+= match_length
;
2008 /* copy match data - no worries about destination wraps */
2009 while (match_length
-- > 0) *rundest
++ = *runsrc
++;
2014 case LZX_BLOCKTYPE_ALIGNED
:
2015 while (this_run
> 0) {
2016 READ_HUFFSYM(MAINTREE
, main_element
);
2018 if (main_element
< LZX_NUM_CHARS
) {
2019 /* literal: 0 to LZX_NUM_CHARS-1 */
2020 window
[window_posn
++] = main_element
;
2024 /* match: LZX_NUM_CHARS + ((slot<<3) | length_header (3 bits)) */
2025 main_element
-= LZX_NUM_CHARS
;
2027 match_length
= main_element
& LZX_NUM_PRIMARY_LENGTHS
;
2028 if (match_length
== LZX_NUM_PRIMARY_LENGTHS
) {
2029 READ_HUFFSYM(LENGTH
, length_footer
);
2030 match_length
+= length_footer
;
2032 match_length
+= LZX_MIN_MATCH
;
2034 match_offset
= main_element
>> 3;
2036 if (match_offset
> 2) {
2037 /* not repeated offset */
2038 extra
= extra_bits
[match_offset
];
2039 match_offset
= lzx_position_base
[match_offset
] - 2;
2041 /* verbatim and aligned bits */
2043 READ_BITS(verbatim_bits
, extra
);
2044 match_offset
+= (verbatim_bits
<< 3);
2045 READ_HUFFSYM(ALIGNED
, aligned_bits
);
2046 match_offset
+= aligned_bits
;
2048 else if (extra
== 3) {
2049 /* aligned bits only */
2050 READ_HUFFSYM(ALIGNED
, aligned_bits
);
2051 match_offset
+= aligned_bits
;
2053 else if (extra
> 0) { /* extra==1, extra==2 */
2054 /* verbatim bits only */
2055 READ_BITS(verbatim_bits
, extra
);
2056 match_offset
+= verbatim_bits
;
2058 else /* extra == 0 */ {
2063 /* update repeated offset LRU queue */
2064 R2
= R1
; R1
= R0
; R0
= match_offset
;
2066 else if (match_offset
== 0) {
2069 else if (match_offset
== 1) {
2071 R1
= R0
; R0
= match_offset
;
2073 else /* match_offset == 2 */ {
2075 R2
= R0
; R0
= match_offset
;
2078 rundest
= window
+ window_posn
;
2079 this_run
-= match_length
;
2081 /* copy any wrapped around source data */
2082 if (window_posn
>= match_offset
) {
2084 runsrc
= rundest
- match_offset
;
2086 runsrc
= rundest
+ (window_size
- match_offset
);
2087 copy_length
= match_offset
- window_posn
;
2088 if (copy_length
< match_length
) {
2089 match_length
-= copy_length
;
2090 window_posn
+= copy_length
;
2091 while (copy_length
-- > 0) *rundest
++ = *runsrc
++;
2095 window_posn
+= match_length
;
2097 /* copy match data - no worries about destination wraps */
2098 while (match_length
-- > 0) *rundest
++ = *runsrc
++;
2103 case LZX_BLOCKTYPE_UNCOMPRESSED
:
2104 if ((inpos
+ this_run
) > endinp
) return DECR_ILLEGALDATA
;
2105 memcpy(window
+ window_posn
, inpos
, (size_t) this_run
);
2106 inpos
+= this_run
; window_posn
+= this_run
;
2110 return DECR_ILLEGALDATA
; /* might as well */
2116 if (togo
!= 0) return DECR_ILLEGALDATA
;
2117 memcpy(CAB(outbuf
), window
+ ((!window_posn
) ? window_size
: window_posn
) -
2118 outlen
, (size_t) outlen
);
2120 LZX(window_posn
) = window_posn
;
2125 /* intel E8 decoding */
2126 if ((LZX(frames_read
)++ < 32768) && LZX(intel_filesize
) != 0) {
2127 if (outlen
<= 6 || !LZX(intel_started
)) {
2128 LZX(intel_curpos
) += outlen
;
2131 cab_UBYTE
*data
= CAB(outbuf
);
2132 cab_UBYTE
*dataend
= data
+ outlen
- 10;
2133 cab_LONG curpos
= LZX(intel_curpos
);
2134 cab_LONG filesize
= LZX(intel_filesize
);
2135 cab_LONG abs_off
, rel_off
;
2137 LZX(intel_curpos
) = curpos
+ outlen
;
2139 while (data
< dataend
) {
2140 if (*data
++ != 0xE8) { curpos
++; continue; }
2141 abs_off
= data
[0] | (data
[1]<<8) | (data
[2]<<16) | (data
[3]<<24);
2142 if ((abs_off
>= -curpos
) && (abs_off
< filesize
)) {
2143 rel_off
= (abs_off
>= 0) ? abs_off
- curpos
: abs_off
+ filesize
;
2144 data
[0] = (cab_UBYTE
) rel_off
;
2145 data
[1] = (cab_UBYTE
) (rel_off
>> 8);
2146 data
[2] = (cab_UBYTE
) (rel_off
>> 16);
2147 data
[3] = (cab_UBYTE
) (rel_off
>> 24);
2157 /*********************************************************
2158 * find_cabs_in_file (internal)
2160 struct cabinet
*find_cabs_in_file(LPCSTR name
)
2162 struct cabinet
*cab
, *cab2
, *firstcab
= NULL
, *linkcab
= NULL
;
2163 cab_UBYTE
*pstart
= &search_buf
[0], *pend
, *p
;
2164 cab_off_t offset
, caboff
, cablen
= 0, foffset
= 0, filelen
, length
;
2165 int state
= 0, found
= 0, ok
= 0;
2167 TRACE("(name == %s)\n", debugstr_a((char *) name
));
2169 /* open the file and search for cabinet headers */
2170 if ((cab
= (struct cabinet
*) calloc(1, sizeof(struct cabinet
)))) {
2171 cab
->filename
= name
;
2172 if (cabinet_open(cab
)) {
2173 filelen
= cab
->filelen
;
2174 for (offset
= 0; (offset
< filelen
); offset
+= length
) {
2175 /* search length is either the full length of the search buffer,
2176 * or the amount of data remaining to the end of the file,
2177 * whichever is less.
2179 length
= filelen
- offset
;
2180 if (length
> CAB_SEARCH_SIZE
) length
= CAB_SEARCH_SIZE
;
2182 /* fill the search buffer with data from disk */
2183 if (!cabinet_read(cab
, search_buf
, length
)) break;
2185 /* read through the entire buffer. */
2187 pend
= &search_buf
[length
];
2190 /* starting state */
2192 /* we spend most of our time in this while loop, looking for
2193 * a leading 'M' of the 'MSCF' signature
2195 while (*p
++ != 0x4D && p
< pend
);
2196 if (p
< pend
) state
= 1; /* if we found tht 'M', advance state */
2199 /* verify that the next 3 bytes are 'S', 'C' and 'F' */
2200 case 1: state
= (*p
++ == 0x53) ? 2 : 0; break;
2201 case 2: state
= (*p
++ == 0x43) ? 3 : 0; break;
2202 case 3: state
= (*p
++ == 0x46) ? 4 : 0; break;
2204 /* we don't care about bytes 4-7 */
2205 /* bytes 8-11 are the overall length of the cabinet */
2206 case 8: cablen
= *p
++; state
++; break;
2207 case 9: cablen
|= *p
++ << 8; state
++; break;
2208 case 10: cablen
|= *p
++ << 16; state
++; break;
2209 case 11: cablen
|= *p
++ << 24; state
++; break;
2211 /* we don't care about bytes 12-15 */
2212 /* bytes 16-19 are the offset within the cabinet of the filedata */
2213 case 16: foffset
= *p
++; state
++; break;
2214 case 17: foffset
|= *p
++ << 8; state
++; break;
2215 case 18: foffset
|= *p
++ << 16; state
++; break;
2216 case 19: foffset
|= *p
++ << 24;
2217 /* now we have recieved 20 bytes of potential cab header. */
2218 /* work out the offset in the file of this potential cabinet */
2219 caboff
= offset
+ (p
-pstart
) - 20;
2221 /* check that the files offset is less than the alleged length
2222 * of the cabinet, and that the offset + the alleged length are
2223 * 'roughly' within the end of overall file length
2225 if ((foffset
< cablen
) &&
2226 ((caboff
+ foffset
) < (filelen
+ 32)) &&
2227 ((caboff
+ cablen
) < (filelen
+ 32)) )
2229 /* found a potential result - try loading it */
2231 cab2
= load_cab_offset(name
, caboff
);
2236 /* cause the search to restart after this cab's data. */
2237 offset
= caboff
+ cablen
;
2238 if (offset
< cab
->filelen
) cabinet_seek(cab
, offset
);
2242 /* link the cab into the list */
2243 if (linkcab
== NULL
) firstcab
= cab2
;
2244 else linkcab
->next
= cab2
;
2251 p
++, state
++; break;
2260 /* if there were cabinets that were found but are not ok, point this out */
2262 WARN("%s: found %d bad cabinets\n", debugstr_a(name
), found
-ok
);
2265 /* if no cabinets were found, let the user know */
2267 WARN("%s: not a Microsoft cabinet file.\n", debugstr_a(name
));
2272 /***********************************************************************
2273 * find_cabinet_file (internal)
2275 * tries to find *cabname, from the directory path of origcab, correcting the
2276 * case of *cabname if necessary, If found, writes back to *cabname.
2278 void find_cabinet_file(char **cabname
, LPCSTR origcab
) {
2280 char *tail
, *cab
, *name
, *nextpart
, nametmp
[MAX_PATH
], *filepart
;
2283 TRACE("(*cabname == ^%p, origcab == %s)\n", cabname
? *cabname
: NULL
, debugstr_a(origcab
));
2285 /* ensure we have a cabinet name at all */
2286 if (!(name
= *cabname
)) {
2287 WARN("no cabinet name at all\n");
2290 /* find if there's a directory path in the origcab */
2291 tail
= origcab
? max(strrchr(origcab
, '/'), strrchr(origcab
, '\\')) : NULL
;
2293 if ((cab
= (char *) malloc(MAX_PATH
))) {
2294 /* add the directory path from the original cabinet name */
2296 memcpy(cab
, origcab
, tail
- origcab
);
2297 cab
[tail
- origcab
] = '\0';
2299 /* default directory path of '.' */
2305 TRACE("trying cab == %s", debugstr_a(cab
));
2307 /* we don't want null cabinet filenames */
2308 if (name
[0] == '\0') {
2309 WARN("null cab name\n");
2313 /* if there is a directory component in the cabinet name,
2314 * look for that alone first
2316 nextpart
= strchr(name
, '\\');
2317 if (nextpart
) *nextpart
= '\0';
2319 found
= SearchPathA(cab
, name
, NULL
, MAX_PATH
, nametmp
, &filepart
);
2321 /* if the component was not found, look for it in the current dir */
2323 found
= SearchPathA(".", name
, NULL
, MAX_PATH
, nametmp
, &filepart
);
2327 TRACE("found: %s\n", debugstr_a(nametmp
));
2329 TRACE("not found.\n");
2331 /* restore the real name and skip to the next directory component
2332 * or actual cabinet name
2334 if (nextpart
) *nextpart
= '\\', name
= &nextpart
[1];
2336 /* while there is another directory component, and while we
2337 * successfully found the current component
2339 } while (nextpart
&& found
);
2341 /* if we found the cabinet, change the next cabinet's name.
2342 * otherwise, pretend nothing happened
2345 free((void *) *cabname
);
2347 strncpy(cab
, nametmp
, found
+1);
2348 TRACE("result: %s\n", debugstr_a(cab
));
2351 TRACE("result: nothing\n");
2356 /************************************************************************
2357 * process_files (internal)
2359 * this does the tricky job of running through every file in the cabinet,
2360 * including spanning cabinets, and working out which file is in which
2361 * folder in which cabinet. It also throws out the duplicate file entries
2362 * that appear in spanning cabinets. There is memory leakage here because
2363 * those entries are not freed. See the XAD CAB client for an
2364 * implementation of this that correctly frees the discarded file entries.
2366 struct cab_file
*process_files(struct cabinet
*basecab
) {
2367 struct cabinet
*cab
;
2368 struct cab_file
*outfi
= NULL
, *linkfi
= NULL
, *nextfi
, *fi
, *cfi
;
2369 struct cab_folder
*fol
, *firstfol
, *lastfol
= NULL
, *predfol
;
2372 FIXME("(basecab == ^%p): Memory leak.\n", basecab
);
2374 for (cab
= basecab
; cab
; cab
= cab
->nextcab
) {
2375 /* firstfol = first folder in this cabinet */
2376 /* lastfol = last folder in this cabinet */
2377 /* predfol = last folder in previous cabinet (or NULL if first cabinet) */
2379 firstfol
= cab
->folders
;
2380 for (lastfol
= firstfol
; lastfol
->next
;) lastfol
= lastfol
->next
;
2383 for (fi
= cab
->files
; fi
; fi
= nextfi
) {
2387 if (i
< cffileCONTINUED_FROM_PREV
) {
2388 for (fol
= firstfol
; fol
&& i
--; ) fol
= fol
->next
;
2389 fi
->folder
= fol
; /* NULL if an invalid folder index */
2392 /* folder merging */
2393 if (i
== cffileCONTINUED_TO_NEXT
2394 || i
== cffileCONTINUED_PREV_AND_NEXT
) {
2395 if (cab
->nextcab
&& !lastfol
->contfile
) lastfol
->contfile
= fi
;
2398 if (i
== cffileCONTINUED_FROM_PREV
2399 || i
== cffileCONTINUED_PREV_AND_NEXT
) {
2400 /* these files are to be continued in yet another
2401 * cabinet, don't merge them in just yet */
2402 if (i
== cffileCONTINUED_PREV_AND_NEXT
) mergeok
= 0;
2404 /* only merge once per cabinet */
2406 if ((cfi
= predfol
->contfile
)
2407 && (cfi
->offset
== fi
->offset
)
2408 && (cfi
->length
== fi
->length
)
2409 && (strcmp(cfi
->filename
, fi
->filename
) == 0)
2410 && (predfol
->comp_type
== firstfol
->comp_type
)) {
2411 /* increase the number of splits */
2412 if ((i
= ++(predfol
->num_splits
)) > CAB_SPLITMAX
) {
2414 ERR("%s: internal error, increase CAB_SPLITMAX\n", debugstr_a(basecab
->filename
));
2417 /* copy information across from the merged folder */
2418 predfol
->offset
[i
] = firstfol
->offset
[0];
2419 predfol
->cab
[i
] = firstfol
->cab
[0];
2420 predfol
->next
= firstfol
->next
;
2421 predfol
->contfile
= firstfol
->contfile
;
2423 if (firstfol
== lastfol
) lastfol
= predfol
;
2425 predfol
= NULL
; /* don't merge again within this cabinet */
2429 /* if the folders won't merge, don't add their files */
2434 if (mergeok
) fi
->folder
= firstfol
;
2439 if (linkfi
) linkfi
->next
= fi
; else outfi
= fi
;
2443 } /* for (cab= ...*/
2448 /****************************************************************
2449 * convertUTF (internal)
2451 * translate UTF -> ASCII
2453 * UTF translates two-byte unicode characters into 1, 2 or 3 bytes.
2454 * %000000000xxxxxxx -> %0xxxxxxx
2455 * %00000xxxxxyyyyyy -> %110xxxxx %10yyyyyy
2456 * %xxxxyyyyyyzzzzzz -> %1110xxxx %10yyyyyy %10zzzzzz
2458 * Therefore, the inverse is as follows:
2460 * 0x00 - 0x7F = one byte char
2461 * 0x80 - 0xBF = invalid
2462 * 0xC0 - 0xDF = 2 byte char (next char only 0x80-0xBF is valid)
2463 * 0xE0 - 0xEF = 3 byte char (next 2 chars only 0x80-0xBF is valid)
2464 * 0xF0 - 0xFF = invalid
2466 * FIXME: use a winapi to do this
2468 int convertUTF(cab_UBYTE
*in
) {
2469 cab_UBYTE c
, *out
= in
, *end
= in
+ strlen((char *) in
) + 1;
2473 /* read unicode character */
2474 if ((c
= *in
++) < 0x80) x
= c
;
2476 if (c
< 0xC0) return 0;
2477 else if (c
< 0xE0) {
2478 x
= (c
& 0x1F) << 6;
2479 if ((c
= *in
++) < 0x80 || c
> 0xBF) return 0; else x
|= (c
& 0x3F);
2481 else if (c
< 0xF0) {
2482 x
= (c
& 0xF) << 12;
2483 if ((c
= *in
++) < 0x80 || c
> 0xBF) return 0; else x
|= (c
& 0x3F)<<6;
2484 if ((c
= *in
++) < 0x80 || c
> 0xBF) return 0; else x
|= (c
& 0x3F);
2489 /* terrible unicode -> ASCII conversion */
2490 if (x
> 127) x
= '_';
2492 if (in
> end
) return 0; /* just in case */
2493 } while ((*out
++ = (cab_UBYTE
) x
));
2497 /****************************************************
2498 * NONEdecompress (internal)
2500 int NONEdecompress(int inlen
, int outlen
)
2502 if (inlen
!= outlen
) return DECR_ILLEGALDATA
;
2503 memcpy(CAB(outbuf
), CAB(inbuf
), (size_t) inlen
);
2507 /**************************************************
2508 * checksum (internal)
2510 cab_ULONG
checksum(cab_UBYTE
*data
, cab_UWORD bytes
, cab_ULONG csum
) {
2514 for (len
= bytes
>> 2; len
--; data
+= 4) {
2515 csum
^= ((data
[0]) | (data
[1]<<8) | (data
[2]<<16) | (data
[3]<<24));
2518 switch (bytes
& 3) {
2519 case 3: ul
|= *data
++ << 16;
2520 case 2: ul
|= *data
++ << 8;
2521 case 1: ul
|= *data
;
2528 /**********************************************************
2529 * decompress (internal)
2531 int decompress(struct cab_file
*fi
, int savemode
, int fix
)
2533 cab_ULONG bytes
= savemode
? fi
->length
: fi
->offset
- CAB(offset
);
2534 struct cabinet
*cab
= CAB(current
)->cab
[CAB(split
)];
2535 cab_UBYTE buf
[cfdata_SIZEOF
], *data
;
2536 cab_UWORD inlen
, len
, outlen
, cando
;
2540 TRACE("(fi == ^%p, savemode == %d, fix == %d)\n", fi
, savemode
, fix
);
2543 /* cando = the max number of bytes we can do */
2544 cando
= CAB(outlen
);
2545 if (cando
> bytes
) cando
= bytes
;
2548 if (cando
&& savemode
)
2549 file_write(fi
, CAB(outpos
), cando
);
2551 CAB(outpos
) += cando
;
2552 CAB(outlen
) -= cando
;
2553 bytes
-= cando
; if (!bytes
) break;
2555 /* we only get here if we emptied the output buffer */
2557 /* read data header + data */
2559 while (outlen
== 0) {
2560 /* read the block header, skip the reserved part */
2561 if (!cabinet_read(cab
, buf
, cfdata_SIZEOF
)) return DECR_INPUT
;
2562 cabinet_skip(cab
, cab
->block_resv
);
2564 /* we shouldn't get blocks over CAB_INPUTMAX in size */
2565 data
= CAB(inbuf
) + inlen
;
2566 len
= EndGetI16(buf
+cfdata_CompressedSize
);
2568 if (inlen
> CAB_INPUTMAX
) return DECR_INPUT
;
2569 if (!cabinet_read(cab
, data
, len
)) return DECR_INPUT
;
2571 /* clear two bytes after read-in data */
2572 data
[len
+1] = data
[len
+2] = 0;
2574 /* perform checksum test on the block (if one is stored) */
2575 cksum
= EndGetI32(buf
+cfdata_CheckSum
);
2576 if (cksum
&& cksum
!= checksum(buf
+4, 4, checksum(data
, len
, 0))) {
2577 /* checksum is wrong */
2578 if (fix
&& ((fi
->folder
->comp_type
& cffoldCOMPTYPE_MASK
)
2579 == cffoldCOMPTYPE_MSZIP
))
2581 WARN("%s: checksum failed\n", debugstr_a(fi
->filename
));
2584 return DECR_CHECKSUM
;
2588 /* outlen=0 means this block was part of a split block */
2589 outlen
= EndGetI16(buf
+cfdata_UncompressedSize
);
2592 cab
= CAB(current
)->cab
[++CAB(split
)];
2593 if (!cabinet_open(cab
)) return DECR_INPUT
;
2594 cabinet_seek(cab
, CAB(current
)->offset
[CAB(split
)]);
2598 /* decompress block */
2599 if ((err
= CAB(decompress
)(inlen
, outlen
))) {
2600 if (fix
&& ((fi
->folder
->comp_type
& cffoldCOMPTYPE_MASK
)
2601 == cffoldCOMPTYPE_MSZIP
))
2603 ERR("%s: failed decrunching block\n", debugstr_a(fi
->filename
));
2609 CAB(outlen
) = outlen
;
2610 CAB(outpos
) = CAB(outbuf
);
2616 /****************************************************************
2617 * extract_file (internal)
2619 * workhorse to extract a particular file from a cab
2621 void extract_file(struct cab_file
*fi
, int lower
, int fix
, LPCSTR dir
)
2623 struct cab_folder
*fol
= fi
->folder
, *oldfol
= CAB(current
);
2624 cab_LONG err
= DECR_OK
;
2626 TRACE("(fi == ^%p, lower == %d, fix == %d, dir == %s)\n", fi
, lower
, fix
, debugstr_a(dir
));
2628 /* is a change of folder needed? do we need to reset the current folder? */
2629 if (fol
!= oldfol
|| fi
->offset
< CAB(offset
)) {
2630 cab_UWORD comptype
= fol
->comp_type
;
2631 int ct1
= comptype
& cffoldCOMPTYPE_MASK
;
2632 int ct2
= oldfol
? (oldfol
->comp_type
& cffoldCOMPTYPE_MASK
) : 0;
2634 /* if the archiver has changed, call the old archiver's free() function */
2637 case cffoldCOMPTYPE_LZX
:
2643 case cffoldCOMPTYPE_QUANTUM
:
2653 case cffoldCOMPTYPE_NONE
:
2654 CAB(decompress
) = NONEdecompress
;
2657 case cffoldCOMPTYPE_MSZIP
:
2658 CAB(decompress
) = ZIPdecompress
;
2661 case cffoldCOMPTYPE_QUANTUM
:
2662 CAB(decompress
) = QTMdecompress
;
2663 err
= QTMinit((comptype
>> 8) & 0x1f, (comptype
>> 4) & 0xF);
2666 case cffoldCOMPTYPE_LZX
:
2667 CAB(decompress
) = LZXdecompress
;
2668 err
= LZXinit((comptype
>> 8) & 0x1f);
2672 err
= DECR_DATAFORMAT
;
2674 if (err
) goto exit_handler
;
2676 /* initialisation OK, set current folder and reset offset */
2677 if (oldfol
) cabinet_close(oldfol
->cab
[CAB(split
)]);
2678 if (!cabinet_open(fol
->cab
[0])) goto exit_handler
;
2679 cabinet_seek(fol
->cab
[0], fol
->offset
[0]);
2682 CAB(outlen
) = 0; /* discard existing block */
2686 if (fi
->offset
> CAB(offset
)) {
2687 /* decode bytes and send them to /dev/null */
2688 if ((err
= decompress(fi
, 0, fix
))) goto exit_handler
;
2689 CAB(offset
) = fi
->offset
;
2692 if (!file_open(fi
, lower
, dir
)) return;
2693 err
= decompress(fi
, 1, fix
);
2694 if (err
) CAB(current
) = NULL
; else CAB(offset
) += fi
->length
;
2699 char *errmsg
, *cabname
;
2702 errmsg
= "out of memory!\n"; break;
2703 case DECR_ILLEGALDATA
:
2704 errmsg
= "%s: illegal or corrupt data\n"; break;
2705 case DECR_DATAFORMAT
:
2706 errmsg
= "%s: unsupported data format\n"; break;
2708 errmsg
= "%s: checksum error\n"; break;
2710 errmsg
= "%s: input error\n"; break;
2712 errmsg
= "%s: output error\n"; break;
2714 errmsg
= "%s: unknown error (BUG)\n";
2718 cabname
= (char *) (CAB(current
)->cab
[CAB(split
)]->filename
);
2721 cabname
= (char *) (fi
->folder
->cab
[0]->filename
);
2724 ERR(errmsg
, cabname
);
2728 /*********************************************************
2729 * print_fileinfo (internal)
2731 void print_fileinfo(struct cab_file
*fi
) {
2732 int d
= fi
->date
, t
= fi
->time
;
2735 if (fi
->attribs
& cffile_A_NAME_IS_UTF
) {
2736 fname
= malloc(strlen(fi
->filename
) + 1);
2738 strcpy(fname
, fi
->filename
);
2739 convertUTF((cab_UBYTE
*) fname
);
2743 TRACE("%9u | %02d.%02d.%04d %02d:%02d:%02d | %s\n",
2745 d
& 0x1f, (d
>>5) & 0xf, (d
>>9) + 1980,
2746 t
>> 11, (t
>>5) & 0x3f, (t
<< 1) & 0x3e,
2747 fname
? fname
: fi
->filename
2750 if (fname
) free(fname
);
2753 /****************************************************************************
2754 * process_cabinet (internal)
2756 * called to simply "extract" a cabinet file. Will find every cabinet file
2757 * in that file, search for every chained cabinet attached to those cabinets,
2758 * and will either extract the cabinets, or ? (call a callback?)
2761 * cabname [I] name of the cabinet file to extract
2762 * dir [I] directory to extract to
2763 * fix [I] attempt to process broken cabinets
2764 * lower [I] ? (lower case something or other?)
2770 BOOL
process_cabinet(LPCSTR cabname
, LPCSTR dir
, BOOL fix
, BOOL lower
)
2772 struct cabinet
*basecab
, *cab
, *cab1
, *cab2
;
2773 struct cab_file
*filelist
, *fi
;
2775 /* has the list-mode header been seen before? */
2778 ZeroMemory(&decomp_state
, sizeof(cab_decomp_state
));
2780 TRACE("Extract %s\n", debugstr_a(cabname
));
2782 /* load the file requested */
2783 basecab
= find_cabs_in_file(cabname
);
2784 if (!basecab
) return FALSE
;
2786 /* iterate over all cabinets found in that file */
2787 for (cab
= basecab
; cab
; cab
=cab
->next
) {
2789 /* bi-directionally load any spanning cabinets -- backwards */
2790 for (cab1
= cab
; cab1
->flags
& cfheadPREV_CABINET
; cab1
= cab1
->prevcab
) {
2791 TRACE("%s: extends backwards to %s (%s)\n", debugstr_a(cabname
),
2792 debugstr_a(cab1
->prevname
), debugstr_a(cab1
->previnfo
));
2793 find_cabinet_file(&(cab1
->prevname
), cabname
);
2794 if (!(cab1
->prevcab
= load_cab_offset(cab1
->prevname
, 0))) {
2795 ERR("%s: can't read previous cabinet %s\n", debugstr_a(cabname
), debugstr_a(cab1
->prevname
));
2798 cab1
->prevcab
->nextcab
= cab1
;
2801 /* bi-directionally load any spanning cabinets -- forwards */
2802 for (cab2
= cab
; cab2
->flags
& cfheadNEXT_CABINET
; cab2
= cab2
->nextcab
) {
2803 TRACE("%s: extends to %s (%s)\n", debugstr_a(cabname
),
2804 debugstr_a(cab2
->nextname
), debugstr_a(cab2
->nextinfo
));
2805 find_cabinet_file(&(cab2
->nextname
), cabname
);
2806 if (!(cab2
->nextcab
= load_cab_offset(cab2
->nextname
, 0))) {
2807 ERR("%s: can't read next cabinet %s\n", debugstr_a(cabname
), debugstr_a(cab2
->nextname
));
2810 cab2
->nextcab
->prevcab
= cab2
;
2813 filelist
= process_files(cab1
);
2814 CAB(current
) = NULL
;
2817 TRACE("File size | Date Time | Name\n");
2818 TRACE("----------+---------------------+-------------\n");
2821 for (fi
= filelist
; fi
; fi
= fi
->next
)
2823 TRACE("Beginning Extraction...\n");
2824 for (fi
= filelist
; fi
; fi
= fi
->next
) {
2825 TRACE(" extracting: %s\n", debugstr_a(fi
->filename
));
2826 extract_file(fi
, lower
, fix
, dir
);
2830 TRACE("Finished processing cabinet.\n");