Correct errors with move to kernel time functions.
[wine/multimedia.git] / dlls / cabinet / cabextract.c
blob566cccd162f6de84f02b1208086a47ee44d34034
1 /*
2 * cabextract.c
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.
29 #include "config.h"
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winerror.h"
39 #include "cabinet.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(cabinet);
45 THOSE_ZIP_CONSTS;
47 /* all the file IO is abstracted into these routines:
48 * cabinet_(open|close|read|seek|skip|getoffset)
49 * file_(open|close|write)
52 /* try to open a cabinet file, returns success */
53 BOOL cabinet_open(struct cabinet *cab)
55 char *name = (char *)cab->filename;
56 HANDLE fh;
58 TRACE("(cab == ^%p)\n", cab);
60 if ((fh = CreateFileA( name, GENERIC_READ, FILE_SHARE_READ,
61 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL )) == INVALID_HANDLE_VALUE) {
62 ERR("Couldn't open %s\n", debugstr_a(name));
63 return FALSE;
66 /* seek to end of file and get the length */
67 if ((cab->filelen = SetFilePointer(fh, 0, NULL, FILE_END)) == INVALID_SET_FILE_POINTER) {
68 if (GetLastError() != NO_ERROR) {
69 ERR("Seek END failed: %s\n", debugstr_a(name));
70 CloseHandle(fh);
71 return FALSE;
75 /* return to the start of the file */
76 if (SetFilePointer(fh, 0, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
77 ERR("Seek BEGIN failed: %s\n", debugstr_a(name));
78 CloseHandle(fh);
79 return FALSE;
82 cab->fh = fh;
83 return TRUE;
86 /*******************************************************************
87 * cabinet_close (internal)
89 * close the file handle in a struct cabinet.
91 void cabinet_close(struct cabinet *cab) {
92 TRACE("(cab == ^%p)\n", cab);
93 if (cab->fh) CloseHandle(cab->fh);
94 cab->fh = 0;
97 /*******************************************************
98 * ensure_filepath2 (internal)
100 BOOL ensure_filepath2(char *path) {
101 BOOL ret = TRUE;
102 int len;
103 char *new_path;
105 new_path = HeapAlloc(GetProcessHeap(), 0, (strlen(path) + 1));
106 strcpy(new_path, path);
108 while((len = strlen(new_path)) && new_path[len - 1] == '\\')
109 new_path[len - 1] = 0;
111 TRACE("About to try to create directory %s\n", debugstr_a(new_path));
112 while(!CreateDirectoryA(new_path, NULL)) {
113 char *slash;
114 DWORD last_error = GetLastError();
116 if(last_error == ERROR_ALREADY_EXISTS)
117 break;
119 if(last_error != ERROR_PATH_NOT_FOUND) {
120 ret = FALSE;
121 break;
124 if(!(slash = strrchr(new_path, '\\'))) {
125 ret = FALSE;
126 break;
129 len = slash - new_path;
130 new_path[len] = 0;
131 if(! ensure_filepath2(new_path)) {
132 ret = FALSE;
133 break;
135 new_path[len] = '\\';
136 TRACE("New path in next iteration: %s\n", debugstr_a(new_path));
139 HeapFree(GetProcessHeap(), 0, new_path);
140 return ret;
144 /**********************************************************************
145 * ensure_filepath (internal)
147 * ensure_filepath("a\b\c\d.txt") ensures a, a\b and a\b\c exist as dirs
149 BOOL ensure_filepath(char *path) {
150 char new_path[MAX_PATH];
151 int len, i, lastslashpos = -1;
153 TRACE("(path == %s)\n", debugstr_a(path));
155 strcpy(new_path, path);
156 /* remove trailing slashes (shouldn't need to but wth...) */
157 while ((len = strlen(new_path)) && new_path[len - 1] == '\\')
158 new_path[len - 1] = 0;
159 /* find the position of the last '\\' */
160 for (i=0; i<MAX_PATH; i++) {
161 if (new_path[i] == 0) break;
162 if (new_path[i] == '\\')
163 lastslashpos = i;
165 if (lastslashpos > 0) {
166 new_path[lastslashpos] = 0;
167 /* may be trailing slashes but ensure_filepath2 will chop them */
168 return ensure_filepath2(new_path);
169 } else
170 return TRUE; /* ? */
173 /*******************************************************************
174 * file_open (internal)
176 * opens a file for output, returns success
178 BOOL file_open(struct cab_file *fi, BOOL lower, LPCSTR dir)
180 char c, *s, *d, *name;
181 BOOL ok = FALSE;
183 TRACE("(fi == ^%p, lower == %s, dir == %s)\n", fi, lower ? "TRUE" : "FALSE", debugstr_a(dir));
185 if (!(name = malloc(strlen(fi->filename) + (dir ? strlen(dir) : 0) + 2))) {
186 ERR("out of memory!\n");
187 return FALSE;
190 /* start with blank name */
191 *name = 0;
193 /* add output directory if needed */
194 if (dir) {
195 strcpy(name, dir);
196 strcat(name, "\\");
199 /* remove leading slashes */
200 s = (char *) fi->filename;
201 while (*s == '\\') s++;
203 /* copy from fi->filename to new name.
204 * lowercases characters if needed.
206 d = &name[strlen(name)];
207 do {
208 c = *s++;
209 *d++ = (lower ? tolower((unsigned char) c) : c);
210 } while (c);
212 /* create directories if needed, attempt to write file */
213 if (ensure_filepath(name)) {
214 fi->fh = CreateFileA(name, GENERIC_WRITE, 0, NULL,
215 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
216 if (fi->fh != INVALID_HANDLE_VALUE)
217 ok = TRUE;
218 else {
219 ERR("CreateFileA returned INVALID_HANDLE_VALUE\n");
220 fi->fh = 0;
222 } else
223 ERR("Couldn't ensure filepath for %s\n", debugstr_a(name));
225 if (!ok) {
226 ERR("Couldn't open file %s for writing\n", debugstr_a(name));
229 /* as full filename is no longer needed, free it */
230 free(name);
232 return ok;
235 /********************************************************
236 * close_file (internal)
238 * closes a completed file
240 void file_close(struct cab_file *fi)
242 TRACE("(fi == ^%p)\n", fi);
244 if (fi->fh) {
245 CloseHandle(fi->fh);
247 fi->fh = 0;
250 /******************************************************************
251 * file_write (internal)
253 * writes from buf to a file specified as a cab_file struct.
254 * returns success/failure
256 BOOL file_write(struct cab_file *fi, cab_UBYTE *buf, cab_off_t length)
258 DWORD bytes_written;
260 TRACE("(fi == ^%p, buf == ^%p, length == %u)\n", fi, buf, length);
262 if ((!WriteFile( fi->fh, (LPCVOID) buf, length, &bytes_written, FALSE) ||
263 (bytes_written != length))) {
264 ERR("Error writing file: %s\n", debugstr_a(fi->filename));
265 return FALSE;
267 return TRUE;
271 /*******************************************************************
272 * cabinet_skip (internal)
274 * advance the file pointer associated with the cab structure
275 * by distance bytes
277 void cabinet_skip(struct cabinet *cab, cab_off_t distance)
279 TRACE("(cab == ^%p, distance == %u)\n", cab, distance);
280 if (SetFilePointer(cab->fh, distance, NULL, FILE_CURRENT) == INVALID_SET_FILE_POINTER) {
281 if (distance != INVALID_SET_FILE_POINTER)
282 ERR("%s\n", debugstr_a((char *) cab->filename));
286 /*******************************************************************
287 * cabinet_seek (internal)
289 * seek to the specified absolute offset in a cab
291 void cabinet_seek(struct cabinet *cab, cab_off_t offset) {
292 TRACE("(cab == ^%p, offset == %u)\n", cab, offset);
293 if (SetFilePointer(cab->fh, offset, NULL, FILE_BEGIN) != offset)
294 ERR("%s seek failure\n", debugstr_a((char *)cab->filename));
297 /*******************************************************************
298 * cabinet_getoffset (internal)
300 * returns the file pointer position of a cab
302 cab_off_t cabinet_getoffset(struct cabinet *cab)
304 return SetFilePointer(cab->fh, 0, NULL, FILE_CURRENT);
307 /*******************************************************************
308 * cabinet_read (internal)
310 * read data from a cabinet, returns success
312 BOOL cabinet_read(struct cabinet *cab, cab_UBYTE *buf, cab_off_t length)
314 DWORD bytes_read;
315 cab_off_t avail = cab->filelen - cabinet_getoffset(cab);
317 TRACE("(cab == ^%p, buf == ^%p, length == %u)\n", cab, buf, length);
319 if (length > avail) {
320 WARN("%s: WARNING; cabinet is truncated\n", debugstr_a((char *)cab->filename));
321 length = avail;
324 if (! ReadFile( cab->fh, (LPVOID) buf, length, &bytes_read, NULL )) {
325 ERR("%s read error\n", debugstr_a((char *) cab->filename));
326 return FALSE;
327 } else if (bytes_read != length) {
328 ERR("%s read size mismatch\n", debugstr_a((char *) cab->filename));
329 return FALSE;
332 return TRUE;
335 /**********************************************************************
336 * cabinet_read_string (internal)
338 * allocate and read an aribitrarily long string from the cabinet
340 char *cabinet_read_string(struct cabinet *cab)
342 cab_off_t len=256, base = cabinet_getoffset(cab), maxlen = cab->filelen - base;
343 BOOL ok = FALSE;
344 int i;
345 cab_UBYTE *buf = NULL;
347 TRACE("(cab == ^%p)\n", cab);
349 do {
350 if (len > maxlen) len = maxlen;
351 if (!(buf = realloc(buf, (size_t) len))) break;
352 if (!cabinet_read(cab, buf, (size_t) len)) break;
354 /* search for a null terminator in what we've just read */
355 for (i=0; i < len; i++) {
356 if (!buf[i]) {ok=TRUE; break;}
359 if (!ok) {
360 if (len == maxlen) {
361 ERR("%s: WARNING; cabinet is truncated\n", debugstr_a((char *) cab->filename));
362 break;
364 len += 256;
365 cabinet_seek(cab, base);
367 } while (!ok);
369 if (!ok) {
370 if (buf)
371 free(buf);
372 else
373 ERR("out of memory!\n");
374 return NULL;
377 /* otherwise, set the stream to just after the string and return */
378 cabinet_seek(cab, base + ((cab_off_t) strlen((char *) buf)) + 1);
380 return (char *) buf;
383 /******************************************************************
384 * cabinet_read_entries (internal)
386 * reads the header and all folder and file entries in this cabinet
388 BOOL cabinet_read_entries(struct cabinet *cab)
390 int num_folders, num_files, header_resv, folder_resv = 0, i;
391 struct cab_folder *fol, *linkfol = NULL;
392 struct cab_file *file, *linkfile = NULL;
393 cab_off_t base_offset;
394 cab_UBYTE buf[64];
396 TRACE("(cab == ^%p)\n", cab);
398 /* read in the CFHEADER */
399 base_offset = cabinet_getoffset(cab);
400 if (!cabinet_read(cab, buf, cfhead_SIZEOF)) {
401 return FALSE;
404 /* check basic MSCF signature */
405 if (EndGetI32(buf+cfhead_Signature) != 0x4643534d) {
406 ERR("%s: not a Microsoft cabinet file\n", debugstr_a((char *) cab->filename));
407 return FALSE;
410 /* get the number of folders */
411 num_folders = EndGetI16(buf+cfhead_NumFolders);
412 if (num_folders == 0) {
413 ERR("%s: no folders in cabinet\n", debugstr_a((char *) cab->filename));
414 return FALSE;
417 /* get the number of files */
418 num_files = EndGetI16(buf+cfhead_NumFiles);
419 if (num_files == 0) {
420 ERR("%s: no files in cabinet\n", debugstr_a((char *) cab->filename));
421 return FALSE;
424 /* just check the header revision */
425 if ((buf[cfhead_MajorVersion] > 1) ||
426 (buf[cfhead_MajorVersion] == 1 && buf[cfhead_MinorVersion] > 3))
428 WARN("%s: WARNING; cabinet format version > 1.3\n", debugstr_a((char *) cab->filename));
431 /* read the reserved-sizes part of header, if present */
432 cab->flags = EndGetI16(buf+cfhead_Flags);
433 if (cab->flags & cfheadRESERVE_PRESENT) {
434 if (!cabinet_read(cab, buf, cfheadext_SIZEOF)) return FALSE;
435 header_resv = EndGetI16(buf+cfheadext_HeaderReserved);
436 folder_resv = buf[cfheadext_FolderReserved];
437 cab->block_resv = buf[cfheadext_DataReserved];
439 if (header_resv > 60000) {
440 WARN("%s: WARNING; header reserved space > 60000\n", debugstr_a((char *) cab->filename));
443 /* skip the reserved header */
444 if (header_resv)
445 if (SetFilePointer(cab->fh, (cab_off_t) header_resv, NULL, FILE_CURRENT) == INVALID_SET_FILE_POINTER)
446 ERR("seek failure: %s\n", debugstr_a((char *) cab->filename));
449 if (cab->flags & cfheadPREV_CABINET) {
450 cab->prevname = cabinet_read_string(cab);
451 if (!cab->prevname) return FALSE;
452 cab->previnfo = cabinet_read_string(cab);
455 if (cab->flags & cfheadNEXT_CABINET) {
456 cab->nextname = cabinet_read_string(cab);
457 if (!cab->nextname) return FALSE;
458 cab->nextinfo = cabinet_read_string(cab);
461 /* read folders */
462 for (i = 0; i < num_folders; i++) {
463 if (!cabinet_read(cab, buf, cffold_SIZEOF)) return FALSE;
464 if (folder_resv) cabinet_skip(cab, folder_resv);
466 fol = (struct cab_folder *) calloc(1, sizeof(struct cab_folder));
467 if (!fol) {
468 ERR("out of memory!\n");
469 return FALSE;
472 fol->cab[0] = cab;
473 fol->offset[0] = base_offset + (cab_off_t) EndGetI32(buf+cffold_DataOffset);
474 fol->num_blocks = EndGetI16(buf+cffold_NumBlocks);
475 fol->comp_type = EndGetI16(buf+cffold_CompType);
477 if (!linkfol)
478 cab->folders = fol;
479 else
480 linkfol->next = fol;
482 linkfol = fol;
485 /* read files */
486 for (i = 0; i < num_files; i++) {
487 if (!cabinet_read(cab, buf, cffile_SIZEOF))
488 return FALSE;
490 file = (struct cab_file *) calloc(1, sizeof(struct cab_file));
491 if (!file) {
492 ERR("out of memory!\n");
493 return FALSE;
496 file->length = EndGetI32(buf+cffile_UncompressedSize);
497 file->offset = EndGetI32(buf+cffile_FolderOffset);
498 file->index = EndGetI16(buf+cffile_FolderIndex);
499 file->time = EndGetI16(buf+cffile_Time);
500 file->date = EndGetI16(buf+cffile_Date);
501 file->attribs = EndGetI16(buf+cffile_Attribs);
502 file->filename = cabinet_read_string(cab);
504 if (!file->filename) {
505 free(file);
506 return FALSE;
509 if (!linkfile)
510 cab->files = file;
511 else
512 linkfile->next = file;
514 linkfile = file;
516 return TRUE;
519 /***********************************************************
520 * load_cab_offset (internal)
522 * validates and reads file entries from a cabinet at offset [offset] in
523 * file [name]. Returns a cabinet structure if successful, or NULL
524 * otherwise.
526 struct cabinet *load_cab_offset(LPCSTR name, cab_off_t offset)
528 struct cabinet *cab = (struct cabinet *) calloc(1, sizeof(struct cabinet));
529 int ok;
531 TRACE("(name == %s, offset == %u)\n", debugstr_a((char *) name), offset);
533 if (!cab) return NULL;
535 cab->filename = name;
536 if ((ok = cabinet_open(cab))) {
537 cabinet_seek(cab, offset);
538 ok = cabinet_read_entries(cab);
539 cabinet_close(cab);
542 if (ok) return cab;
543 free(cab);
544 return NULL;
547 /* MSZIP decruncher */
549 /* Dirk Stoecker wrote the ZIP decoder, based on the InfoZip deflate code */
551 /********************************************************
552 * Ziphuft_free (internal)
554 void Ziphuft_free(struct Ziphuft *t)
556 register struct Ziphuft *p, *q;
558 /* Go through linked list, freeing from the allocated (t[-1]) address. */
559 p = t;
560 while (p != (struct Ziphuft *)NULL)
562 q = (--p)->v.t;
563 free(p);
564 p = q;
568 /*********************************************************
569 * Ziphuft_build (internal)
571 cab_LONG Ziphuft_build(cab_ULONG *b, cab_ULONG n, cab_ULONG s, cab_UWORD *d, cab_UWORD *e,
572 struct Ziphuft **t, cab_LONG *m, cab_decomp_state *decomp_state)
574 cab_ULONG a; /* counter for codes of length k */
575 cab_ULONG el; /* length of EOB code (value 256) */
576 cab_ULONG f; /* i repeats in table every f entries */
577 cab_LONG g; /* maximum code length */
578 cab_LONG h; /* table level */
579 register cab_ULONG i; /* counter, current code */
580 register cab_ULONG j; /* counter */
581 register cab_LONG k; /* number of bits in current code */
582 cab_LONG *l; /* stack of bits per table */
583 register cab_ULONG *p; /* pointer into ZIP(c)[],ZIP(b)[],ZIP(v)[] */
584 register struct Ziphuft *q; /* points to current table */
585 struct Ziphuft r; /* table entry for structure assignment */
586 register cab_LONG w; /* bits before this table == (l * h) */
587 cab_ULONG *xp; /* pointer into x */
588 cab_LONG y; /* number of dummy codes added */
589 cab_ULONG z; /* number of entries in current table */
591 l = ZIP(lx)+1;
593 /* Generate counts for each bit length */
594 el = n > 256 ? b[256] : ZIPBMAX; /* set length of EOB code, if any */
596 for(i = 0; i < ZIPBMAX+1; ++i)
597 ZIP(c)[i] = 0;
598 p = b; i = n;
601 ZIP(c)[*p]++; p++; /* assume all entries <= ZIPBMAX */
602 } while (--i);
603 if (ZIP(c)[0] == n) /* null input--all zero length codes */
605 *t = (struct Ziphuft *)NULL;
606 *m = 0;
607 return 0;
610 /* Find minimum and maximum length, bound *m by those */
611 for (j = 1; j <= ZIPBMAX; j++)
612 if (ZIP(c)[j])
613 break;
614 k = j; /* minimum code length */
615 if ((cab_ULONG)*m < j)
616 *m = j;
617 for (i = ZIPBMAX; i; i--)
618 if (ZIP(c)[i])
619 break;
620 g = i; /* maximum code length */
621 if ((cab_ULONG)*m > i)
622 *m = i;
624 /* Adjust last length count to fill out codes, if needed */
625 for (y = 1 << j; j < i; j++, y <<= 1)
626 if ((y -= ZIP(c)[j]) < 0)
627 return 2; /* bad input: more codes than bits */
628 if ((y -= ZIP(c)[i]) < 0)
629 return 2;
630 ZIP(c)[i] += y;
632 /* Generate starting offsets LONGo the value table for each length */
633 ZIP(x)[1] = j = 0;
634 p = ZIP(c) + 1; xp = ZIP(x) + 2;
635 while (--i)
636 { /* note that i == g from above */
637 *xp++ = (j += *p++);
640 /* Make a table of values in order of bit lengths */
641 p = b; i = 0;
643 if ((j = *p++) != 0)
644 ZIP(v)[ZIP(x)[j]++] = i;
645 } while (++i < n);
648 /* Generate the Huffman codes and for each, make the table entries */
649 ZIP(x)[0] = i = 0; /* first Huffman code is zero */
650 p = ZIP(v); /* grab values in bit order */
651 h = -1; /* no tables yet--level -1 */
652 w = l[-1] = 0; /* no bits decoded yet */
653 ZIP(u)[0] = (struct Ziphuft *)NULL; /* just to keep compilers happy */
654 q = (struct Ziphuft *)NULL; /* ditto */
655 z = 0; /* ditto */
657 /* go through the bit lengths (k already is bits in shortest code) */
658 for (; k <= g; k++)
660 a = ZIP(c)[k];
661 while (a--)
663 /* here i is the Huffman code of length k bits for value *p */
664 /* make tables up to required level */
665 while (k > w + l[h])
667 w += l[h++]; /* add bits already decoded */
669 /* compute minimum size table less than or equal to *m bits */
670 z = (z = g - w) > (cab_ULONG)*m ? *m : z; /* upper limit */
671 if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
672 { /* too few codes for k-w bit table */
673 f -= a + 1; /* deduct codes from patterns left */
674 xp = ZIP(c) + k;
675 while (++j < z) /* try smaller tables up to z bits */
677 if ((f <<= 1) <= *++xp)
678 break; /* enough codes to use up j bits */
679 f -= *xp; /* else deduct codes from patterns */
682 if ((cab_ULONG)w + j > el && (cab_ULONG)w < el)
683 j = el - w; /* make EOB code end at table */
684 z = 1 << j; /* table entries for j-bit table */
685 l[h] = j; /* set table size in stack */
687 /* allocate and link in new table */
688 if (!(q = (struct Ziphuft *) malloc((z + 1)*sizeof(struct Ziphuft))))
690 if(h)
691 Ziphuft_free(ZIP(u)[0]);
692 return 3; /* not enough memory */
694 *t = q + 1; /* link to list for Ziphuft_free() */
695 *(t = &(q->v.t)) = (struct Ziphuft *)NULL;
696 ZIP(u)[h] = ++q; /* table starts after link */
698 /* connect to last table, if there is one */
699 if (h)
701 ZIP(x)[h] = i; /* save pattern for backing up */
702 r.b = (cab_UBYTE)l[h-1]; /* bits to dump before this table */
703 r.e = (cab_UBYTE)(16 + j); /* bits in this table */
704 r.v.t = q; /* pointer to this table */
705 j = (i & ((1 << w) - 1)) >> (w - l[h-1]);
706 ZIP(u)[h-1][j] = r; /* connect to last table */
710 /* set up table entry in r */
711 r.b = (cab_UBYTE)(k - w);
712 if (p >= ZIP(v) + n)
713 r.e = 99; /* out of values--invalid code */
714 else if (*p < s)
716 r.e = (cab_UBYTE)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
717 r.v.n = *p++; /* simple code is just the value */
719 else
721 r.e = (cab_UBYTE)e[*p - s]; /* non-simple--look up in lists */
722 r.v.n = d[*p++ - s];
725 /* fill code-like entries with r */
726 f = 1 << (k - w);
727 for (j = i >> w; j < z; j += f)
728 q[j] = r;
730 /* backwards increment the k-bit code i */
731 for (j = 1 << (k - 1); i & j; j >>= 1)
732 i ^= j;
733 i ^= j;
735 /* backup over finished tables */
736 while ((i & ((1 << w) - 1)) != ZIP(x)[h])
737 w -= l[--h]; /* don't need to update q */
741 /* return actual size of base table */
742 *m = l[0];
744 /* Return true (1) if we were given an incomplete table */
745 return y != 0 && g != 1;
748 /*********************************************************
749 * Zipinflate_codes (internal)
751 cab_LONG Zipinflate_codes(struct Ziphuft *tl, struct Ziphuft *td,
752 cab_LONG bl, cab_LONG bd, cab_decomp_state *decomp_state)
754 register cab_ULONG e; /* table entry flag/number of extra bits */
755 cab_ULONG n, d; /* length and index for copy */
756 cab_ULONG w; /* current window position */
757 struct Ziphuft *t; /* pointer to table entry */
758 cab_ULONG ml, md; /* masks for bl and bd bits */
759 register cab_ULONG b; /* bit buffer */
760 register cab_ULONG k; /* number of bits in bit buffer */
762 /* make local copies of globals */
763 b = ZIP(bb); /* initialize bit buffer */
764 k = ZIP(bk);
765 w = ZIP(window_posn); /* initialize window position */
767 /* inflate the coded data */
768 ml = Zipmask[bl]; /* precompute masks for speed */
769 md = Zipmask[bd];
771 for(;;)
773 ZIPNEEDBITS((cab_ULONG)bl)
774 if((e = (t = tl + ((cab_ULONG)b & ml))->e) > 16)
777 if (e == 99)
778 return 1;
779 ZIPDUMPBITS(t->b)
780 e -= 16;
781 ZIPNEEDBITS(e)
782 } while ((e = (t = t->v.t + ((cab_ULONG)b & Zipmask[e]))->e) > 16);
783 ZIPDUMPBITS(t->b)
784 if (e == 16) /* then it's a literal */
785 CAB(outbuf)[w++] = (cab_UBYTE)t->v.n;
786 else /* it's an EOB or a length */
788 /* exit if end of block */
789 if(e == 15)
790 break;
792 /* get length of block to copy */
793 ZIPNEEDBITS(e)
794 n = t->v.n + ((cab_ULONG)b & Zipmask[e]);
795 ZIPDUMPBITS(e);
797 /* decode distance of block to copy */
798 ZIPNEEDBITS((cab_ULONG)bd)
799 if ((e = (t = td + ((cab_ULONG)b & md))->e) > 16)
800 do {
801 if (e == 99)
802 return 1;
803 ZIPDUMPBITS(t->b)
804 e -= 16;
805 ZIPNEEDBITS(e)
806 } while ((e = (t = t->v.t + ((cab_ULONG)b & Zipmask[e]))->e) > 16);
807 ZIPDUMPBITS(t->b)
808 ZIPNEEDBITS(e)
809 d = w - t->v.n - ((cab_ULONG)b & Zipmask[e]);
810 ZIPDUMPBITS(e)
813 n -= (e = (e = ZIPWSIZE - ((d &= ZIPWSIZE-1) > w ? d : w)) > n ?n:e);
816 CAB(outbuf)[w++] = CAB(outbuf)[d++];
817 } while (--e);
818 } while (n);
822 /* restore the globals from the locals */
823 ZIP(window_posn) = w; /* restore global window pointer */
824 ZIP(bb) = b; /* restore global bit buffer */
825 ZIP(bk) = k;
827 /* done */
828 return 0;
831 /***********************************************************
832 * Zipinflate_stored (internal)
834 cab_LONG Zipinflate_stored(cab_decomp_state *decomp_state)
835 /* "decompress" an inflated type 0 (stored) block. */
837 cab_ULONG n; /* number of bytes in block */
838 cab_ULONG w; /* current window position */
839 register cab_ULONG b; /* bit buffer */
840 register cab_ULONG k; /* number of bits in bit buffer */
842 /* make local copies of globals */
843 b = ZIP(bb); /* initialize bit buffer */
844 k = ZIP(bk);
845 w = ZIP(window_posn); /* initialize window position */
847 /* go to byte boundary */
848 n = k & 7;
849 ZIPDUMPBITS(n);
851 /* get the length and its complement */
852 ZIPNEEDBITS(16)
853 n = ((cab_ULONG)b & 0xffff);
854 ZIPDUMPBITS(16)
855 ZIPNEEDBITS(16)
856 if (n != (cab_ULONG)((~b) & 0xffff))
857 return 1; /* error in compressed data */
858 ZIPDUMPBITS(16)
860 /* read and output the compressed data */
861 while(n--)
863 ZIPNEEDBITS(8)
864 CAB(outbuf)[w++] = (cab_UBYTE)b;
865 ZIPDUMPBITS(8)
868 /* restore the globals from the locals */
869 ZIP(window_posn) = w; /* restore global window pointer */
870 ZIP(bb) = b; /* restore global bit buffer */
871 ZIP(bk) = k;
872 return 0;
875 /******************************************************
876 * Zipinflate_fixed (internal)
878 cab_LONG Zipinflate_fixed(cab_decomp_state *decomp_state)
880 struct Ziphuft *fixed_tl;
881 struct Ziphuft *fixed_td;
882 cab_LONG fixed_bl, fixed_bd;
883 cab_LONG i; /* temporary variable */
884 cab_ULONG *l;
886 l = ZIP(ll);
888 /* literal table */
889 for(i = 0; i < 144; i++)
890 l[i] = 8;
891 for(; i < 256; i++)
892 l[i] = 9;
893 for(; i < 280; i++)
894 l[i] = 7;
895 for(; i < 288; i++) /* make a complete, but wrong code set */
896 l[i] = 8;
897 fixed_bl = 7;
898 if((i = Ziphuft_build(l, 288, 257, (cab_UWORD *) Zipcplens,
899 (cab_UWORD *) Zipcplext, &fixed_tl, &fixed_bl, decomp_state)))
900 return i;
902 /* distance table */
903 for(i = 0; i < 30; i++) /* make an incomplete code set */
904 l[i] = 5;
905 fixed_bd = 5;
906 if((i = Ziphuft_build(l, 30, 0, (cab_UWORD *) Zipcpdist, (cab_UWORD *) Zipcpdext,
907 &fixed_td, &fixed_bd, decomp_state)) > 1)
909 Ziphuft_free(fixed_tl);
910 return i;
913 /* decompress until an end-of-block code */
914 i = Zipinflate_codes(fixed_tl, fixed_td, fixed_bl, fixed_bd, decomp_state);
916 Ziphuft_free(fixed_td);
917 Ziphuft_free(fixed_tl);
918 return i;
921 /**************************************************************
922 * Zipinflate_dynamic (internal)
924 cab_LONG Zipinflate_dynamic(cab_decomp_state *decomp_state)
925 /* decompress an inflated type 2 (dynamic Huffman codes) block. */
927 cab_LONG i; /* temporary variables */
928 cab_ULONG j;
929 cab_ULONG *ll;
930 cab_ULONG l; /* last length */
931 cab_ULONG m; /* mask for bit lengths table */
932 cab_ULONG n; /* number of lengths to get */
933 struct Ziphuft *tl; /* literal/length code table */
934 struct Ziphuft *td; /* distance code table */
935 cab_LONG bl; /* lookup bits for tl */
936 cab_LONG bd; /* lookup bits for td */
937 cab_ULONG nb; /* number of bit length codes */
938 cab_ULONG nl; /* number of literal/length codes */
939 cab_ULONG nd; /* number of distance codes */
940 register cab_ULONG b; /* bit buffer */
941 register cab_ULONG k; /* number of bits in bit buffer */
943 /* make local bit buffer */
944 b = ZIP(bb);
945 k = ZIP(bk);
946 ll = ZIP(ll);
948 /* read in table lengths */
949 ZIPNEEDBITS(5)
950 nl = 257 + ((cab_ULONG)b & 0x1f); /* number of literal/length codes */
951 ZIPDUMPBITS(5)
952 ZIPNEEDBITS(5)
953 nd = 1 + ((cab_ULONG)b & 0x1f); /* number of distance codes */
954 ZIPDUMPBITS(5)
955 ZIPNEEDBITS(4)
956 nb = 4 + ((cab_ULONG)b & 0xf); /* number of bit length codes */
957 ZIPDUMPBITS(4)
958 if(nl > 288 || nd > 32)
959 return 1; /* bad lengths */
961 /* read in bit-length-code lengths */
962 for(j = 0; j < nb; j++)
964 ZIPNEEDBITS(3)
965 ll[Zipborder[j]] = (cab_ULONG)b & 7;
966 ZIPDUMPBITS(3)
968 for(; j < 19; j++)
969 ll[Zipborder[j]] = 0;
971 /* build decoding table for trees--single level, 7 bit lookup */
972 bl = 7;
973 if((i = Ziphuft_build(ll, 19, 19, NULL, NULL, &tl, &bl, decomp_state)) != 0)
975 if(i == 1)
976 Ziphuft_free(tl);
977 return i; /* incomplete code set */
980 /* read in literal and distance code lengths */
981 n = nl + nd;
982 m = Zipmask[bl];
983 i = l = 0;
984 while((cab_ULONG)i < n)
986 ZIPNEEDBITS((cab_ULONG)bl)
987 j = (td = tl + ((cab_ULONG)b & m))->b;
988 ZIPDUMPBITS(j)
989 j = td->v.n;
990 if (j < 16) /* length of code in bits (0..15) */
991 ll[i++] = l = j; /* save last length in l */
992 else if (j == 16) /* repeat last length 3 to 6 times */
994 ZIPNEEDBITS(2)
995 j = 3 + ((cab_ULONG)b & 3);
996 ZIPDUMPBITS(2)
997 if((cab_ULONG)i + j > n)
998 return 1;
999 while (j--)
1000 ll[i++] = l;
1002 else if (j == 17) /* 3 to 10 zero length codes */
1004 ZIPNEEDBITS(3)
1005 j = 3 + ((cab_ULONG)b & 7);
1006 ZIPDUMPBITS(3)
1007 if ((cab_ULONG)i + j > n)
1008 return 1;
1009 while (j--)
1010 ll[i++] = 0;
1011 l = 0;
1013 else /* j == 18: 11 to 138 zero length codes */
1015 ZIPNEEDBITS(7)
1016 j = 11 + ((cab_ULONG)b & 0x7f);
1017 ZIPDUMPBITS(7)
1018 if ((cab_ULONG)i + j > n)
1019 return 1;
1020 while (j--)
1021 ll[i++] = 0;
1022 l = 0;
1026 /* free decoding table for trees */
1027 Ziphuft_free(tl);
1029 /* restore the global bit buffer */
1030 ZIP(bb) = b;
1031 ZIP(bk) = k;
1033 /* build the decoding tables for literal/length and distance codes */
1034 bl = ZIPLBITS;
1035 if((i = Ziphuft_build(ll, nl, 257, (cab_UWORD *) Zipcplens, (cab_UWORD *) Zipcplext,
1036 &tl, &bl, decomp_state)) != 0)
1038 if(i == 1)
1039 Ziphuft_free(tl);
1040 return i; /* incomplete code set */
1042 bd = ZIPDBITS;
1043 Ziphuft_build(ll + nl, nd, 0, (cab_UWORD *) Zipcpdist, (cab_UWORD *) Zipcpdext,
1044 &td, &bd, decomp_state);
1046 /* decompress until an end-of-block code */
1047 if(Zipinflate_codes(tl, td, bl, bd, decomp_state))
1048 return 1;
1050 /* free the decoding tables, return */
1051 Ziphuft_free(tl);
1052 Ziphuft_free(td);
1053 return 0;
1056 /*****************************************************
1057 * Zipinflate_block (internal)
1059 cab_LONG Zipinflate_block(cab_LONG *e, cab_decomp_state *decomp_state) /* e == last block flag */
1060 { /* decompress an inflated block */
1061 cab_ULONG t; /* block type */
1062 register cab_ULONG b; /* bit buffer */
1063 register cab_ULONG k; /* number of bits in bit buffer */
1065 /* make local bit buffer */
1066 b = ZIP(bb);
1067 k = ZIP(bk);
1069 /* read in last block bit */
1070 ZIPNEEDBITS(1)
1071 *e = (cab_LONG)b & 1;
1072 ZIPDUMPBITS(1)
1074 /* read in block type */
1075 ZIPNEEDBITS(2)
1076 t = (cab_ULONG)b & 3;
1077 ZIPDUMPBITS(2)
1079 /* restore the global bit buffer */
1080 ZIP(bb) = b;
1081 ZIP(bk) = k;
1083 /* inflate that block type */
1084 if(t == 2)
1085 return Zipinflate_dynamic(decomp_state);
1086 if(t == 0)
1087 return Zipinflate_stored(decomp_state);
1088 if(t == 1)
1089 return Zipinflate_fixed(decomp_state);
1090 /* bad block type */
1091 return 2;
1094 /****************************************************
1095 * ZIPdecompress (internal)
1097 int ZIPdecompress(int inlen, int outlen, cab_decomp_state *decomp_state)
1099 cab_LONG e; /* last block flag */
1101 TRACE("(inlen == %d, outlen == %d)\n", inlen, outlen);
1103 ZIP(inpos) = CAB(inbuf);
1104 ZIP(bb) = ZIP(bk) = ZIP(window_posn) = 0;
1105 if(outlen > ZIPWSIZE)
1106 return DECR_DATAFORMAT;
1108 /* CK = Chris Kirmse, official Microsoft purloiner */
1109 if(ZIP(inpos)[0] != 0x43 || ZIP(inpos)[1] != 0x4B)
1110 return DECR_ILLEGALDATA;
1111 ZIP(inpos) += 2;
1115 if(Zipinflate_block(&e, decomp_state))
1116 return DECR_ILLEGALDATA;
1117 } while(!e);
1119 /* return success */
1120 return DECR_OK;
1123 /* Quantum decruncher */
1125 /* This decruncher was researched and implemented by Matthew Russoto. */
1126 /* It has since been tidied up by Stuart Caie */
1128 /******************************************************************
1129 * QTMinitmodel (internal)
1131 * Initialise a model which decodes symbols from [s] to [s]+[n]-1
1133 void QTMinitmodel(struct QTMmodel *m, struct QTMmodelsym *sym, int n, int s) {
1134 int i;
1135 m->shiftsleft = 4;
1136 m->entries = n;
1137 m->syms = sym;
1138 memset(m->tabloc, 0xFF, sizeof(m->tabloc)); /* clear out look-up table */
1139 for (i = 0; i < n; i++) {
1140 m->tabloc[i+s] = i; /* set up a look-up entry for symbol */
1141 m->syms[i].sym = i+s; /* actual symbol */
1142 m->syms[i].cumfreq = n-i; /* current frequency of that symbol */
1144 m->syms[n].cumfreq = 0;
1147 /******************************************************************
1148 * QTMinit (internal)
1150 int QTMinit(int window, int level, cab_decomp_state *decomp_state) {
1151 int wndsize = 1 << window, msz = window * 2, i;
1152 cab_ULONG j;
1154 /* QTM supports window sizes of 2^10 (1Kb) through 2^21 (2Mb) */
1155 /* if a previously allocated window is big enough, keep it */
1156 if (window < 10 || window > 21) return DECR_DATAFORMAT;
1157 if (QTM(actual_size) < wndsize) {
1158 if (QTM(window)) free(QTM(window));
1159 QTM(window) = NULL;
1161 if (!QTM(window)) {
1162 if (!(QTM(window) = malloc(wndsize))) return DECR_NOMEMORY;
1163 QTM(actual_size) = wndsize;
1165 QTM(window_size) = wndsize;
1166 QTM(window_posn) = 0;
1168 /* initialise static slot/extrabits tables */
1169 for (i = 0, j = 0; i < 27; i++) {
1170 CAB(q_length_extra)[i] = (i == 26) ? 0 : (i < 2 ? 0 : i - 2) >> 2;
1171 CAB(q_length_base)[i] = j; j += 1 << ((i == 26) ? 5 : CAB(q_length_extra)[i]);
1173 for (i = 0, j = 0; i < 42; i++) {
1174 CAB(q_extra_bits)[i] = (i < 2 ? 0 : i-2) >> 1;
1175 CAB(q_position_base)[i] = j; j += 1 << CAB(q_extra_bits)[i];
1178 /* initialise arithmetic coding models */
1180 QTMinitmodel(&QTM(model7), &QTM(m7sym)[0], 7, 0);
1182 QTMinitmodel(&QTM(model00), &QTM(m00sym)[0], 0x40, 0x00);
1183 QTMinitmodel(&QTM(model40), &QTM(m40sym)[0], 0x40, 0x40);
1184 QTMinitmodel(&QTM(model80), &QTM(m80sym)[0], 0x40, 0x80);
1185 QTMinitmodel(&QTM(modelC0), &QTM(mC0sym)[0], 0x40, 0xC0);
1187 /* model 4 depends on table size, ranges from 20 to 24 */
1188 QTMinitmodel(&QTM(model4), &QTM(m4sym)[0], (msz < 24) ? msz : 24, 0);
1189 /* model 5 depends on table size, ranges from 20 to 36 */
1190 QTMinitmodel(&QTM(model5), &QTM(m5sym)[0], (msz < 36) ? msz : 36, 0);
1191 /* model 6pos depends on table size, ranges from 20 to 42 */
1192 QTMinitmodel(&QTM(model6pos), &QTM(m6psym)[0], msz, 0);
1193 QTMinitmodel(&QTM(model6len), &QTM(m6lsym)[0], 27, 0);
1195 return DECR_OK;
1198 /****************************************************************
1199 * QTMupdatemodel (internal)
1201 void QTMupdatemodel(struct QTMmodel *model, int sym) {
1202 struct QTMmodelsym temp;
1203 int i, j;
1205 for (i = 0; i < sym; i++) model->syms[i].cumfreq += 8;
1207 if (model->syms[0].cumfreq > 3800) {
1208 if (--model->shiftsleft) {
1209 for (i = model->entries - 1; i >= 0; i--) {
1210 /* -1, not -2; the 0 entry saves this */
1211 model->syms[i].cumfreq >>= 1;
1212 if (model->syms[i].cumfreq <= model->syms[i+1].cumfreq) {
1213 model->syms[i].cumfreq = model->syms[i+1].cumfreq + 1;
1217 else {
1218 model->shiftsleft = 50;
1219 for (i = 0; i < model->entries ; i++) {
1220 /* no -1, want to include the 0 entry */
1221 /* this converts cumfreqs into frequencies, then shifts right */
1222 model->syms[i].cumfreq -= model->syms[i+1].cumfreq;
1223 model->syms[i].cumfreq++; /* avoid losing things entirely */
1224 model->syms[i].cumfreq >>= 1;
1227 /* now sort by frequencies, decreasing order -- this must be an
1228 * inplace selection sort, or a sort with the same (in)stability
1229 * characteristics
1231 for (i = 0; i < model->entries - 1; i++) {
1232 for (j = i + 1; j < model->entries; j++) {
1233 if (model->syms[i].cumfreq < model->syms[j].cumfreq) {
1234 temp = model->syms[i];
1235 model->syms[i] = model->syms[j];
1236 model->syms[j] = temp;
1241 /* then convert frequencies back to cumfreq */
1242 for (i = model->entries - 1; i >= 0; i--) {
1243 model->syms[i].cumfreq += model->syms[i+1].cumfreq;
1245 /* then update the other part of the table */
1246 for (i = 0; i < model->entries; i++) {
1247 model->tabloc[model->syms[i].sym] = i;
1253 /*******************************************************************
1254 * QTMdecompress (internal)
1256 int QTMdecompress(int inlen, int outlen, cab_decomp_state *decomp_state)
1258 cab_UBYTE *inpos = CAB(inbuf);
1259 cab_UBYTE *window = QTM(window);
1260 cab_UBYTE *runsrc, *rundest;
1262 cab_ULONG window_posn = QTM(window_posn);
1263 cab_ULONG window_size = QTM(window_size);
1265 /* used by bitstream macros */
1266 register int bitsleft, bitrun, bitsneed;
1267 register cab_ULONG bitbuf;
1269 /* used by GET_SYMBOL */
1270 cab_ULONG range;
1271 cab_UWORD symf;
1272 int i;
1274 int extra, togo = outlen, match_length = 0, copy_length;
1275 cab_UBYTE selector, sym;
1276 cab_ULONG match_offset = 0;
1278 cab_UWORD H = 0xFFFF, L = 0, C;
1280 TRACE("(inlen == %d, outlen == %d)\n", inlen, outlen);
1282 /* read initial value of C */
1283 Q_INIT_BITSTREAM;
1284 Q_READ_BITS(C, 16);
1286 /* apply 2^x-1 mask */
1287 window_posn &= window_size - 1;
1288 /* runs can't straddle the window wraparound */
1289 if ((window_posn + togo) > window_size) {
1290 TRACE("straddled run\n");
1291 return DECR_DATAFORMAT;
1294 while (togo > 0) {
1295 GET_SYMBOL(model7, selector);
1296 switch (selector) {
1297 case 0:
1298 GET_SYMBOL(model00, sym); window[window_posn++] = sym; togo--;
1299 break;
1300 case 1:
1301 GET_SYMBOL(model40, sym); window[window_posn++] = sym; togo--;
1302 break;
1303 case 2:
1304 GET_SYMBOL(model80, sym); window[window_posn++] = sym; togo--;
1305 break;
1306 case 3:
1307 GET_SYMBOL(modelC0, sym); window[window_posn++] = sym; togo--;
1308 break;
1310 case 4:
1311 /* selector 4 = fixed length of 3 */
1312 GET_SYMBOL(model4, sym);
1313 Q_READ_BITS(extra, CAB(q_extra_bits)[sym]);
1314 match_offset = CAB(q_position_base)[sym] + extra + 1;
1315 match_length = 3;
1316 break;
1318 case 5:
1319 /* selector 5 = fixed length of 4 */
1320 GET_SYMBOL(model5, sym);
1321 Q_READ_BITS(extra, CAB(q_extra_bits)[sym]);
1322 match_offset = CAB(q_position_base)[sym] + extra + 1;
1323 match_length = 4;
1324 break;
1326 case 6:
1327 /* selector 6 = variable length */
1328 GET_SYMBOL(model6len, sym);
1329 Q_READ_BITS(extra, CAB(q_length_extra)[sym]);
1330 match_length = CAB(q_length_base)[sym] + extra + 5;
1331 GET_SYMBOL(model6pos, sym);
1332 Q_READ_BITS(extra, CAB(q_extra_bits)[sym]);
1333 match_offset = CAB(q_position_base)[sym] + extra + 1;
1334 break;
1336 default:
1337 TRACE("Selector is bogus\n");
1338 return DECR_ILLEGALDATA;
1341 /* if this is a match */
1342 if (selector >= 4) {
1343 rundest = window + window_posn;
1344 togo -= match_length;
1346 /* copy any wrapped around source data */
1347 if (window_posn >= match_offset) {
1348 /* no wrap */
1349 runsrc = rundest - match_offset;
1350 } else {
1351 runsrc = rundest + (window_size - match_offset);
1352 copy_length = match_offset - window_posn;
1353 if (copy_length < match_length) {
1354 match_length -= copy_length;
1355 window_posn += copy_length;
1356 while (copy_length-- > 0) *rundest++ = *runsrc++;
1357 runsrc = window;
1360 window_posn += match_length;
1362 /* copy match data - no worries about destination wraps */
1363 while (match_length-- > 0) *rundest++ = *runsrc++;
1365 } /* while (togo > 0) */
1367 if (togo != 0) {
1368 TRACE("Frame overflow, this_run = %d\n", togo);
1369 return DECR_ILLEGALDATA;
1372 memcpy(CAB(outbuf), window + ((!window_posn) ? window_size : window_posn) -
1373 outlen, outlen);
1375 QTM(window_posn) = window_posn;
1376 return DECR_OK;
1379 /* LZX decruncher */
1381 /* Microsoft's LZX document and their implementation of the
1382 * com.ms.util.cab Java package do not concur.
1384 * In the LZX document, there is a table showing the correlation between
1385 * window size and the number of position slots. It states that the 1MB
1386 * window = 40 slots and the 2MB window = 42 slots. In the implementation,
1387 * 1MB = 42 slots, 2MB = 50 slots. The actual calculation is 'find the
1388 * first slot whose position base is equal to or more than the required
1389 * window size'. This would explain why other tables in the document refer
1390 * to 50 slots rather than 42.
1392 * The constant NUM_PRIMARY_LENGTHS used in the decompression pseudocode
1393 * is not defined in the specification.
1395 * The LZX document does not state the uncompressed block has an
1396 * uncompressed length field. Where does this length field come from, so
1397 * we can know how large the block is? The implementation has it as the 24
1398 * bits following after the 3 blocktype bits, before the alignment
1399 * padding.
1401 * The LZX document states that aligned offset blocks have their aligned
1402 * offset huffman tree AFTER the main and length trees. The implementation
1403 * suggests that the aligned offset tree is BEFORE the main and length
1404 * trees.
1406 * The LZX document decoding algorithm states that, in an aligned offset
1407 * block, if an extra_bits value is 1, 2 or 3, then that number of bits
1408 * should be read and the result added to the match offset. This is
1409 * correct for 1 and 2, but not 3, where just a huffman symbol (using the
1410 * aligned tree) should be read.
1412 * Regarding the E8 preprocessing, the LZX document states 'No translation
1413 * may be performed on the last 6 bytes of the input block'. This is
1414 * correct. However, the pseudocode provided checks for the *E8 leader*
1415 * up to the last 6 bytes. If the leader appears between -10 and -7 bytes
1416 * from the end, this would cause the next four bytes to be modified, at
1417 * least one of which would be in the last 6 bytes, which is not allowed
1418 * according to the spec.
1420 * The specification states that the huffman trees must always contain at
1421 * least one element. However, many CAB files contain blocks where the
1422 * length tree is completely empty (because there are no matches), and
1423 * this is expected to succeed.
1427 /* LZX uses what it calls 'position slots' to represent match offsets.
1428 * What this means is that a small 'position slot' number and a small
1429 * offset from that slot are encoded instead of one large offset for
1430 * every match.
1431 * - lzx_position_base is an index to the position slot bases
1432 * - lzx_extra_bits states how many bits of offset-from-base data is needed.
1435 /************************************************************
1436 * LZXinit (internal)
1438 int LZXinit(int window, cab_decomp_state *decomp_state) {
1439 cab_ULONG wndsize = 1 << window;
1440 int i, j, posn_slots;
1442 /* LZX supports window sizes of 2^15 (32Kb) through 2^21 (2Mb) */
1443 /* if a previously allocated window is big enough, keep it */
1444 if (window < 15 || window > 21) return DECR_DATAFORMAT;
1445 if (LZX(actual_size) < wndsize) {
1446 if (LZX(window)) free(LZX(window));
1447 LZX(window) = NULL;
1449 if (!LZX(window)) {
1450 if (!(LZX(window) = malloc(wndsize))) return DECR_NOMEMORY;
1451 LZX(actual_size) = wndsize;
1453 LZX(window_size) = wndsize;
1455 /* initialise static tables */
1456 for (i=0, j=0; i <= 50; i += 2) {
1457 CAB(extra_bits)[i] = CAB(extra_bits)[i+1] = j; /* 0,0,0,0,1,1,2,2,3,3... */
1458 if ((i != 0) && (j < 17)) j++; /* 0,0,1,2,3,4...15,16,17,17,17,17... */
1460 for (i=0, j=0; i <= 50; i++) {
1461 CAB(lzx_position_base)[i] = j; /* 0,1,2,3,4,6,8,12,16,24,32,... */
1462 j += 1 << CAB(extra_bits)[i]; /* 1,1,1,1,2,2,4,4,8,8,16,16,32,32,... */
1465 /* calculate required position slots */
1466 if (window == 20) posn_slots = 42;
1467 else if (window == 21) posn_slots = 50;
1468 else posn_slots = window << 1;
1470 /*posn_slots=i=0; while (i < wndsize) i += 1 << CAB(extra_bits)[posn_slots++]; */
1472 LZX(R0) = LZX(R1) = LZX(R2) = 1;
1473 LZX(main_elements) = LZX_NUM_CHARS + (posn_slots << 3);
1474 LZX(header_read) = 0;
1475 LZX(frames_read) = 0;
1476 LZX(block_remaining) = 0;
1477 LZX(block_type) = LZX_BLOCKTYPE_INVALID;
1478 LZX(intel_curpos) = 0;
1479 LZX(intel_started) = 0;
1480 LZX(window_posn) = 0;
1482 /* initialise tables to 0 (because deltas will be applied to them) */
1483 for (i = 0; i < LZX_MAINTREE_MAXSYMBOLS; i++) LZX(MAINTREE_len)[i] = 0;
1484 for (i = 0; i < LZX_LENGTH_MAXSYMBOLS; i++) LZX(LENGTH_len)[i] = 0;
1486 return DECR_OK;
1489 /*************************************************************************
1490 * make_decode_table (internal)
1492 * This function was coded by David Tritscher. It builds a fast huffman
1493 * decoding table out of just a canonical huffman code lengths table.
1495 * PARAMS
1496 * nsyms: total number of symbols in this huffman tree.
1497 * nbits: any symbols with a code length of nbits or less can be decoded
1498 * in one lookup of the table.
1499 * length: A table to get code lengths from [0 to syms-1]
1500 * table: The table to fill up with decoded symbols and pointers.
1502 * RETURNS
1503 * OK: 0
1504 * error: 1
1506 int make_decode_table(cab_ULONG nsyms, cab_ULONG nbits, cab_UBYTE *length, cab_UWORD *table) {
1507 register cab_UWORD sym;
1508 register cab_ULONG leaf;
1509 register cab_UBYTE bit_num = 1;
1510 cab_ULONG fill;
1511 cab_ULONG pos = 0; /* the current position in the decode table */
1512 cab_ULONG table_mask = 1 << nbits;
1513 cab_ULONG bit_mask = table_mask >> 1; /* don't do 0 length codes */
1514 cab_ULONG next_symbol = bit_mask; /* base of allocation for long codes */
1516 /* fill entries for codes short enough for a direct mapping */
1517 while (bit_num <= nbits) {
1518 for (sym = 0; sym < nsyms; sym++) {
1519 if (length[sym] == bit_num) {
1520 leaf = pos;
1522 if((pos += bit_mask) > table_mask) return 1; /* table overrun */
1524 /* fill all possible lookups of this symbol with the symbol itself */
1525 fill = bit_mask;
1526 while (fill-- > 0) table[leaf++] = sym;
1529 bit_mask >>= 1;
1530 bit_num++;
1533 /* if there are any codes longer than nbits */
1534 if (pos != table_mask) {
1535 /* clear the remainder of the table */
1536 for (sym = pos; sym < table_mask; sym++) table[sym] = 0;
1538 /* give ourselves room for codes to grow by up to 16 more bits */
1539 pos <<= 16;
1540 table_mask <<= 16;
1541 bit_mask = 1 << 15;
1543 while (bit_num <= 16) {
1544 for (sym = 0; sym < nsyms; sym++) {
1545 if (length[sym] == bit_num) {
1546 leaf = pos >> 16;
1547 for (fill = 0; fill < bit_num - nbits; fill++) {
1548 /* if this path hasn't been taken yet, 'allocate' two entries */
1549 if (table[leaf] == 0) {
1550 table[(next_symbol << 1)] = 0;
1551 table[(next_symbol << 1) + 1] = 0;
1552 table[leaf] = next_symbol++;
1554 /* follow the path and select either left or right for next bit */
1555 leaf = table[leaf] << 1;
1556 if ((pos >> (15-fill)) & 1) leaf++;
1558 table[leaf] = sym;
1560 if ((pos += bit_mask) > table_mask) return 1; /* table overflow */
1563 bit_mask >>= 1;
1564 bit_num++;
1568 /* full table? */
1569 if (pos == table_mask) return 0;
1571 /* either erroneous table, or all elements are 0 - let's find out. */
1572 for (sym = 0; sym < nsyms; sym++) if (length[sym]) return 1;
1573 return 0;
1576 /************************************************************
1577 * lzx_read_lens (internal)
1579 int lzx_read_lens(cab_UBYTE *lens, cab_ULONG first, cab_ULONG last, struct lzx_bits *lb,
1580 cab_decomp_state *decomp_state) {
1581 cab_ULONG i,j, x,y;
1582 int z;
1584 register cab_ULONG bitbuf = lb->bb;
1585 register int bitsleft = lb->bl;
1586 cab_UBYTE *inpos = lb->ip;
1587 cab_UWORD *hufftbl;
1589 for (x = 0; x < 20; x++) {
1590 READ_BITS(y, 4);
1591 LENTABLE(PRETREE)[x] = y;
1593 BUILD_TABLE(PRETREE);
1595 for (x = first; x < last; ) {
1596 READ_HUFFSYM(PRETREE, z);
1597 if (z == 17) {
1598 READ_BITS(y, 4); y += 4;
1599 while (y--) lens[x++] = 0;
1601 else if (z == 18) {
1602 READ_BITS(y, 5); y += 20;
1603 while (y--) lens[x++] = 0;
1605 else if (z == 19) {
1606 READ_BITS(y, 1); y += 4;
1607 READ_HUFFSYM(PRETREE, z);
1608 z = lens[x] - z; if (z < 0) z += 17;
1609 while (y--) lens[x++] = z;
1611 else {
1612 z = lens[x] - z; if (z < 0) z += 17;
1613 lens[x++] = z;
1617 lb->bb = bitbuf;
1618 lb->bl = bitsleft;
1619 lb->ip = inpos;
1620 return 0;
1623 /*******************************************************
1624 * LZXdecompress (internal)
1626 int LZXdecompress(int inlen, int outlen, cab_decomp_state *decomp_state) {
1627 cab_UBYTE *inpos = CAB(inbuf);
1628 cab_UBYTE *endinp = inpos + inlen;
1629 cab_UBYTE *window = LZX(window);
1630 cab_UBYTE *runsrc, *rundest;
1631 cab_UWORD *hufftbl; /* used in READ_HUFFSYM macro as chosen decoding table */
1633 cab_ULONG window_posn = LZX(window_posn);
1634 cab_ULONG window_size = LZX(window_size);
1635 cab_ULONG R0 = LZX(R0);
1636 cab_ULONG R1 = LZX(R1);
1637 cab_ULONG R2 = LZX(R2);
1639 register cab_ULONG bitbuf;
1640 register int bitsleft;
1641 cab_ULONG match_offset, i,j,k; /* ijk used in READ_HUFFSYM macro */
1642 struct lzx_bits lb; /* used in READ_LENGTHS macro */
1644 int togo = outlen, this_run, main_element, aligned_bits;
1645 int match_length, copy_length, length_footer, extra, verbatim_bits;
1647 TRACE("(inlen == %d, outlen == %d)\n", inlen, outlen);
1649 INIT_BITSTREAM;
1651 /* read header if necessary */
1652 if (!LZX(header_read)) {
1653 i = j = 0;
1654 READ_BITS(k, 1); if (k) { READ_BITS(i,16); READ_BITS(j,16); }
1655 LZX(intel_filesize) = (i << 16) | j; /* or 0 if not encoded */
1656 LZX(header_read) = 1;
1659 /* main decoding loop */
1660 while (togo > 0) {
1661 /* last block finished, new block expected */
1662 if (LZX(block_remaining) == 0) {
1663 if (LZX(block_type) == LZX_BLOCKTYPE_UNCOMPRESSED) {
1664 if (LZX(block_length) & 1) inpos++; /* realign bitstream to word */
1665 INIT_BITSTREAM;
1668 READ_BITS(LZX(block_type), 3);
1669 READ_BITS(i, 16);
1670 READ_BITS(j, 8);
1671 LZX(block_remaining) = LZX(block_length) = (i << 8) | j;
1673 switch (LZX(block_type)) {
1674 case LZX_BLOCKTYPE_ALIGNED:
1675 for (i = 0; i < 8; i++) { READ_BITS(j, 3); LENTABLE(ALIGNED)[i] = j; }
1676 BUILD_TABLE(ALIGNED);
1677 /* rest of aligned header is same as verbatim */
1679 case LZX_BLOCKTYPE_VERBATIM:
1680 READ_LENGTHS(MAINTREE, 0, 256, lzx_read_lens);
1681 READ_LENGTHS(MAINTREE, 256, LZX(main_elements), lzx_read_lens);
1682 BUILD_TABLE(MAINTREE);
1683 if (LENTABLE(MAINTREE)[0xE8] != 0) LZX(intel_started) = 1;
1685 READ_LENGTHS(LENGTH, 0, LZX_NUM_SECONDARY_LENGTHS, lzx_read_lens);
1686 BUILD_TABLE(LENGTH);
1687 break;
1689 case LZX_BLOCKTYPE_UNCOMPRESSED:
1690 LZX(intel_started) = 1; /* because we can't assume otherwise */
1691 ENSURE_BITS(16); /* get up to 16 pad bits into the buffer */
1692 if (bitsleft > 16) inpos -= 2; /* and align the bitstream! */
1693 R0 = inpos[0]|(inpos[1]<<8)|(inpos[2]<<16)|(inpos[3]<<24);inpos+=4;
1694 R1 = inpos[0]|(inpos[1]<<8)|(inpos[2]<<16)|(inpos[3]<<24);inpos+=4;
1695 R2 = inpos[0]|(inpos[1]<<8)|(inpos[2]<<16)|(inpos[3]<<24);inpos+=4;
1696 break;
1698 default:
1699 return DECR_ILLEGALDATA;
1703 /* buffer exhaustion check */
1704 if (inpos > endinp) {
1705 /* it's possible to have a file where the next run is less than
1706 * 16 bits in size. In this case, the READ_HUFFSYM() macro used
1707 * in building the tables will exhaust the buffer, so we should
1708 * allow for this, but not allow those accidentally read bits to
1709 * be used (so we check that there are at least 16 bits
1710 * remaining - in this boundary case they aren't really part of
1711 * the compressed data)
1713 if (inpos > (endinp+2) || bitsleft < 16) return DECR_ILLEGALDATA;
1716 while ((this_run = LZX(block_remaining)) > 0 && togo > 0) {
1717 if (this_run > togo) this_run = togo;
1718 togo -= this_run;
1719 LZX(block_remaining) -= this_run;
1721 /* apply 2^x-1 mask */
1722 window_posn &= window_size - 1;
1723 /* runs can't straddle the window wraparound */
1724 if ((window_posn + this_run) > window_size)
1725 return DECR_DATAFORMAT;
1727 switch (LZX(block_type)) {
1729 case LZX_BLOCKTYPE_VERBATIM:
1730 while (this_run > 0) {
1731 READ_HUFFSYM(MAINTREE, main_element);
1733 if (main_element < LZX_NUM_CHARS) {
1734 /* literal: 0 to LZX_NUM_CHARS-1 */
1735 window[window_posn++] = main_element;
1736 this_run--;
1738 else {
1739 /* match: LZX_NUM_CHARS + ((slot<<3) | length_header (3 bits)) */
1740 main_element -= LZX_NUM_CHARS;
1742 match_length = main_element & LZX_NUM_PRIMARY_LENGTHS;
1743 if (match_length == LZX_NUM_PRIMARY_LENGTHS) {
1744 READ_HUFFSYM(LENGTH, length_footer);
1745 match_length += length_footer;
1747 match_length += LZX_MIN_MATCH;
1749 match_offset = main_element >> 3;
1751 if (match_offset > 2) {
1752 /* not repeated offset */
1753 if (match_offset != 3) {
1754 extra = CAB(extra_bits)[match_offset];
1755 READ_BITS(verbatim_bits, extra);
1756 match_offset = CAB(lzx_position_base)[match_offset]
1757 - 2 + verbatim_bits;
1759 else {
1760 match_offset = 1;
1763 /* update repeated offset LRU queue */
1764 R2 = R1; R1 = R0; R0 = match_offset;
1766 else if (match_offset == 0) {
1767 match_offset = R0;
1769 else if (match_offset == 1) {
1770 match_offset = R1;
1771 R1 = R0; R0 = match_offset;
1773 else /* match_offset == 2 */ {
1774 match_offset = R2;
1775 R2 = R0; R0 = match_offset;
1778 rundest = window + window_posn;
1779 this_run -= match_length;
1781 /* copy any wrapped around source data */
1782 if (window_posn >= match_offset) {
1783 /* no wrap */
1784 runsrc = rundest - match_offset;
1785 } else {
1786 runsrc = rundest + (window_size - match_offset);
1787 copy_length = match_offset - window_posn;
1788 if (copy_length < match_length) {
1789 match_length -= copy_length;
1790 window_posn += copy_length;
1791 while (copy_length-- > 0) *rundest++ = *runsrc++;
1792 runsrc = window;
1795 window_posn += match_length;
1797 /* copy match data - no worries about destination wraps */
1798 while (match_length-- > 0) *rundest++ = *runsrc++;
1801 break;
1803 case LZX_BLOCKTYPE_ALIGNED:
1804 while (this_run > 0) {
1805 READ_HUFFSYM(MAINTREE, main_element);
1807 if (main_element < LZX_NUM_CHARS) {
1808 /* literal: 0 to LZX_NUM_CHARS-1 */
1809 window[window_posn++] = main_element;
1810 this_run--;
1812 else {
1813 /* match: LZX_NUM_CHARS + ((slot<<3) | length_header (3 bits)) */
1814 main_element -= LZX_NUM_CHARS;
1816 match_length = main_element & LZX_NUM_PRIMARY_LENGTHS;
1817 if (match_length == LZX_NUM_PRIMARY_LENGTHS) {
1818 READ_HUFFSYM(LENGTH, length_footer);
1819 match_length += length_footer;
1821 match_length += LZX_MIN_MATCH;
1823 match_offset = main_element >> 3;
1825 if (match_offset > 2) {
1826 /* not repeated offset */
1827 extra = CAB(extra_bits)[match_offset];
1828 match_offset = CAB(lzx_position_base)[match_offset] - 2;
1829 if (extra > 3) {
1830 /* verbatim and aligned bits */
1831 extra -= 3;
1832 READ_BITS(verbatim_bits, extra);
1833 match_offset += (verbatim_bits << 3);
1834 READ_HUFFSYM(ALIGNED, aligned_bits);
1835 match_offset += aligned_bits;
1837 else if (extra == 3) {
1838 /* aligned bits only */
1839 READ_HUFFSYM(ALIGNED, aligned_bits);
1840 match_offset += aligned_bits;
1842 else if (extra > 0) { /* extra==1, extra==2 */
1843 /* verbatim bits only */
1844 READ_BITS(verbatim_bits, extra);
1845 match_offset += verbatim_bits;
1847 else /* extra == 0 */ {
1848 /* ??? */
1849 match_offset = 1;
1852 /* update repeated offset LRU queue */
1853 R2 = R1; R1 = R0; R0 = match_offset;
1855 else if (match_offset == 0) {
1856 match_offset = R0;
1858 else if (match_offset == 1) {
1859 match_offset = R1;
1860 R1 = R0; R0 = match_offset;
1862 else /* match_offset == 2 */ {
1863 match_offset = R2;
1864 R2 = R0; R0 = match_offset;
1867 rundest = window + window_posn;
1868 this_run -= match_length;
1870 /* copy any wrapped around source data */
1871 if (window_posn >= match_offset) {
1872 /* no wrap */
1873 runsrc = rundest - match_offset;
1874 } else {
1875 runsrc = rundest + (window_size - match_offset);
1876 copy_length = match_offset - window_posn;
1877 if (copy_length < match_length) {
1878 match_length -= copy_length;
1879 window_posn += copy_length;
1880 while (copy_length-- > 0) *rundest++ = *runsrc++;
1881 runsrc = window;
1884 window_posn += match_length;
1886 /* copy match data - no worries about destination wraps */
1887 while (match_length-- > 0) *rundest++ = *runsrc++;
1890 break;
1892 case LZX_BLOCKTYPE_UNCOMPRESSED:
1893 if ((inpos + this_run) > endinp) return DECR_ILLEGALDATA;
1894 memcpy(window + window_posn, inpos, (size_t) this_run);
1895 inpos += this_run; window_posn += this_run;
1896 break;
1898 default:
1899 return DECR_ILLEGALDATA; /* might as well */
1905 if (togo != 0) return DECR_ILLEGALDATA;
1906 memcpy(CAB(outbuf), window + ((!window_posn) ? window_size : window_posn) -
1907 outlen, (size_t) outlen);
1909 LZX(window_posn) = window_posn;
1910 LZX(R0) = R0;
1911 LZX(R1) = R1;
1912 LZX(R2) = R2;
1914 /* intel E8 decoding */
1915 if ((LZX(frames_read)++ < 32768) && LZX(intel_filesize) != 0) {
1916 if (outlen <= 6 || !LZX(intel_started)) {
1917 LZX(intel_curpos) += outlen;
1919 else {
1920 cab_UBYTE *data = CAB(outbuf);
1921 cab_UBYTE *dataend = data + outlen - 10;
1922 cab_LONG curpos = LZX(intel_curpos);
1923 cab_LONG filesize = LZX(intel_filesize);
1924 cab_LONG abs_off, rel_off;
1926 LZX(intel_curpos) = curpos + outlen;
1928 while (data < dataend) {
1929 if (*data++ != 0xE8) { curpos++; continue; }
1930 abs_off = data[0] | (data[1]<<8) | (data[2]<<16) | (data[3]<<24);
1931 if ((abs_off >= -curpos) && (abs_off < filesize)) {
1932 rel_off = (abs_off >= 0) ? abs_off - curpos : abs_off + filesize;
1933 data[0] = (cab_UBYTE) rel_off;
1934 data[1] = (cab_UBYTE) (rel_off >> 8);
1935 data[2] = (cab_UBYTE) (rel_off >> 16);
1936 data[3] = (cab_UBYTE) (rel_off >> 24);
1938 data += 4;
1939 curpos += 5;
1943 return DECR_OK;
1946 /*********************************************************
1947 * find_cabs_in_file (internal)
1949 struct cabinet *find_cabs_in_file(LPCSTR name, cab_UBYTE search_buf[])
1951 struct cabinet *cab, *cab2, *firstcab = NULL, *linkcab = NULL;
1952 cab_UBYTE *pstart = &search_buf[0], *pend, *p;
1953 cab_off_t offset, caboff, cablen = 0, foffset = 0, filelen, length;
1954 int state = 0, found = 0, ok = 0;
1956 TRACE("(name == %s)\n", debugstr_a((char *) name));
1958 /* open the file and search for cabinet headers */
1959 if ((cab = (struct cabinet *) calloc(1, sizeof(struct cabinet)))) {
1960 cab->filename = name;
1961 if (cabinet_open(cab)) {
1962 filelen = cab->filelen;
1963 for (offset = 0; (offset < filelen); offset += length) {
1964 /* search length is either the full length of the search buffer,
1965 * or the amount of data remaining to the end of the file,
1966 * whichever is less.
1968 length = filelen - offset;
1969 if (length > CAB_SEARCH_SIZE) length = CAB_SEARCH_SIZE;
1971 /* fill the search buffer with data from disk */
1972 if (!cabinet_read(cab, search_buf, length)) break;
1974 /* read through the entire buffer. */
1975 p = pstart;
1976 pend = &search_buf[length];
1977 while (p < pend) {
1978 switch (state) {
1979 /* starting state */
1980 case 0:
1981 /* we spend most of our time in this while loop, looking for
1982 * a leading 'M' of the 'MSCF' signature
1984 while (*p++ != 0x4D && p < pend);
1985 if (p < pend) state = 1; /* if we found tht 'M', advance state */
1986 break;
1988 /* verify that the next 3 bytes are 'S', 'C' and 'F' */
1989 case 1: state = (*p++ == 0x53) ? 2 : 0; break;
1990 case 2: state = (*p++ == 0x43) ? 3 : 0; break;
1991 case 3: state = (*p++ == 0x46) ? 4 : 0; break;
1993 /* we don't care about bytes 4-7 */
1994 /* bytes 8-11 are the overall length of the cabinet */
1995 case 8: cablen = *p++; state++; break;
1996 case 9: cablen |= *p++ << 8; state++; break;
1997 case 10: cablen |= *p++ << 16; state++; break;
1998 case 11: cablen |= *p++ << 24; state++; break;
2000 /* we don't care about bytes 12-15 */
2001 /* bytes 16-19 are the offset within the cabinet of the filedata */
2002 case 16: foffset = *p++; state++; break;
2003 case 17: foffset |= *p++ << 8; state++; break;
2004 case 18: foffset |= *p++ << 16; state++; break;
2005 case 19: foffset |= *p++ << 24;
2006 /* now we have received 20 bytes of potential cab header. */
2007 /* work out the offset in the file of this potential cabinet */
2008 caboff = offset + (p-pstart) - 20;
2010 /* check that the files offset is less than the alleged length
2011 * of the cabinet, and that the offset + the alleged length are
2012 * 'roughly' within the end of overall file length
2014 if ((foffset < cablen) &&
2015 ((caboff + foffset) < (filelen + 32)) &&
2016 ((caboff + cablen) < (filelen + 32)) )
2018 /* found a potential result - try loading it */
2019 found++;
2020 cab2 = load_cab_offset(name, caboff);
2021 if (cab2) {
2022 /* success */
2023 ok++;
2025 /* cause the search to restart after this cab's data. */
2026 offset = caboff + cablen;
2027 if (offset < cab->filelen) cabinet_seek(cab, offset);
2028 length = 0;
2029 p = pend;
2031 /* link the cab into the list */
2032 if (linkcab == NULL) firstcab = cab2;
2033 else linkcab->next = cab2;
2034 linkcab = cab2;
2037 state = 0;
2038 break;
2039 default:
2040 p++, state++; break;
2044 cabinet_close(cab);
2046 free(cab);
2049 /* if there were cabinets that were found but are not ok, point this out */
2050 if (found > ok) {
2051 WARN("%s: found %d bad cabinets\n", debugstr_a(name), found-ok);
2054 /* if no cabinets were found, let the user know */
2055 if (!firstcab) {
2056 WARN("%s: not a Microsoft cabinet file.\n", debugstr_a(name));
2058 return firstcab;
2061 /***********************************************************************
2062 * find_cabinet_file (internal)
2064 * tries to find *cabname, from the directory path of origcab, correcting the
2065 * case of *cabname if necessary, If found, writes back to *cabname.
2067 void find_cabinet_file(char **cabname, LPCSTR origcab) {
2069 char *tail, *cab, *name, *nextpart, nametmp[MAX_PATH], *filepart;
2070 int found = 0;
2072 TRACE("(*cabname == ^%p, origcab == %s)\n", cabname ? *cabname : NULL, debugstr_a(origcab));
2074 /* ensure we have a cabinet name at all */
2075 if (!(name = *cabname)) {
2076 WARN("no cabinet name at all\n");
2079 /* find if there's a directory path in the origcab */
2080 tail = origcab ? max(strrchr(origcab, '/'), strrchr(origcab, '\\')) : NULL;
2082 if ((cab = (char *) malloc(MAX_PATH))) {
2083 /* add the directory path from the original cabinet name */
2084 if (tail) {
2085 memcpy(cab, origcab, tail - origcab);
2086 cab[tail - origcab] = '\0';
2087 } else {
2088 /* default directory path of '.' */
2089 cab[0] = '.';
2090 cab[1] = '\0';
2093 do {
2094 TRACE("trying cab == %s\n", debugstr_a(cab));
2096 /* we don't want null cabinet filenames */
2097 if (name[0] == '\0') {
2098 WARN("null cab name\n");
2099 break;
2102 /* if there is a directory component in the cabinet name,
2103 * look for that alone first
2105 nextpart = strchr(name, '\\');
2106 if (nextpart) *nextpart = '\0';
2108 found = SearchPathA(cab, name, NULL, MAX_PATH, nametmp, &filepart);
2110 /* if the component was not found, look for it in the current dir */
2111 if (!found) {
2112 found = SearchPathA(".", name, NULL, MAX_PATH, nametmp, &filepart);
2115 if (found)
2116 TRACE("found: %s\n", debugstr_a(nametmp));
2117 else
2118 TRACE("not found.\n");
2120 /* restore the real name and skip to the next directory component
2121 * or actual cabinet name
2123 if (nextpart) *nextpart = '\\', name = &nextpart[1];
2125 /* while there is another directory component, and while we
2126 * successfully found the current component
2128 } while (nextpart && found);
2130 /* if we found the cabinet, change the next cabinet's name.
2131 * otherwise, pretend nothing happened
2133 if (found) {
2134 free((void *) *cabname);
2135 *cabname = cab;
2136 strncpy(cab, nametmp, found+1);
2137 TRACE("result: %s\n", debugstr_a(cab));
2138 } else {
2139 free((void *) cab);
2140 TRACE("result: nothing\n");
2145 /************************************************************************
2146 * process_files (internal)
2148 * this does the tricky job of running through every file in the cabinet,
2149 * including spanning cabinets, and working out which file is in which
2150 * folder in which cabinet. It also throws out the duplicate file entries
2151 * that appear in spanning cabinets. There is memory leakage here because
2152 * those entries are not freed. See the XAD CAB client (function CAB_GetInfo
2153 * in CAB.c) for an implementation of this that correctly frees the discarded
2154 * file entries.
2156 struct cab_file *process_files(struct cabinet *basecab) {
2157 struct cabinet *cab;
2158 struct cab_file *outfi = NULL, *linkfi = NULL, *nextfi, *fi, *cfi;
2159 struct cab_folder *fol, *firstfol, *lastfol = NULL, *predfol;
2160 int i, mergeok;
2162 FIXME("(basecab == ^%p): Memory leak.\n", basecab);
2164 for (cab = basecab; cab; cab = cab->nextcab) {
2165 /* firstfol = first folder in this cabinet */
2166 /* lastfol = last folder in this cabinet */
2167 /* predfol = last folder in previous cabinet (or NULL if first cabinet) */
2168 predfol = lastfol;
2169 firstfol = cab->folders;
2170 for (lastfol = firstfol; lastfol->next;) lastfol = lastfol->next;
2171 mergeok = 1;
2173 for (fi = cab->files; fi; fi = nextfi) {
2174 i = fi->index;
2175 nextfi = fi->next;
2177 if (i < cffileCONTINUED_FROM_PREV) {
2178 for (fol = firstfol; fol && i--; ) fol = fol->next;
2179 fi->folder = fol; /* NULL if an invalid folder index */
2181 else {
2182 /* folder merging */
2183 if (i == cffileCONTINUED_TO_NEXT
2184 || i == cffileCONTINUED_PREV_AND_NEXT) {
2185 if (cab->nextcab && !lastfol->contfile) lastfol->contfile = fi;
2188 if (i == cffileCONTINUED_FROM_PREV
2189 || i == cffileCONTINUED_PREV_AND_NEXT) {
2190 /* these files are to be continued in yet another
2191 * cabinet, don't merge them in just yet */
2192 if (i == cffileCONTINUED_PREV_AND_NEXT) mergeok = 0;
2194 /* only merge once per cabinet */
2195 if (predfol) {
2196 if ((cfi = predfol->contfile)
2197 && (cfi->offset == fi->offset)
2198 && (cfi->length == fi->length)
2199 && (strcmp(cfi->filename, fi->filename) == 0)
2200 && (predfol->comp_type == firstfol->comp_type)) {
2201 /* increase the number of splits */
2202 if ((i = ++(predfol->num_splits)) > CAB_SPLITMAX) {
2203 mergeok = 0;
2204 ERR("%s: internal error: CAB_SPLITMAX exceeded. please report this to wine-devel@winehq.org)\n",
2205 debugstr_a(basecab->filename));
2207 else {
2208 /* copy information across from the merged folder */
2209 predfol->offset[i] = firstfol->offset[0];
2210 predfol->cab[i] = firstfol->cab[0];
2211 predfol->next = firstfol->next;
2212 predfol->contfile = firstfol->contfile;
2214 if (firstfol == lastfol) lastfol = predfol;
2215 firstfol = predfol;
2216 predfol = NULL; /* don't merge again within this cabinet */
2219 else {
2220 /* if the folders won't merge, don't add their files */
2221 mergeok = 0;
2225 if (mergeok) fi->folder = firstfol;
2229 if (fi->folder) {
2230 if (linkfi) linkfi->next = fi; else outfi = fi;
2231 linkfi = fi;
2233 } /* for (fi= .. */
2234 } /* for (cab= ...*/
2236 return outfi;
2239 /****************************************************************
2240 * convertUTF (internal)
2242 * translate UTF -> ASCII
2244 * UTF translates two-byte unicode characters into 1, 2 or 3 bytes.
2245 * %000000000xxxxxxx -> %0xxxxxxx
2246 * %00000xxxxxyyyyyy -> %110xxxxx %10yyyyyy
2247 * %xxxxyyyyyyzzzzzz -> %1110xxxx %10yyyyyy %10zzzzzz
2249 * Therefore, the inverse is as follows:
2250 * First char:
2251 * 0x00 - 0x7F = one byte char
2252 * 0x80 - 0xBF = invalid
2253 * 0xC0 - 0xDF = 2 byte char (next char only 0x80-0xBF is valid)
2254 * 0xE0 - 0xEF = 3 byte char (next 2 chars only 0x80-0xBF is valid)
2255 * 0xF0 - 0xFF = invalid
2257 * FIXME: use a winapi to do this
2259 int convertUTF(cab_UBYTE *in) {
2260 cab_UBYTE c, *out = in, *end = in + strlen((char *) in) + 1;
2261 cab_ULONG x;
2263 do {
2264 /* read unicode character */
2265 if ((c = *in++) < 0x80) x = c;
2266 else {
2267 if (c < 0xC0) return 0;
2268 else if (c < 0xE0) {
2269 x = (c & 0x1F) << 6;
2270 if ((c = *in++) < 0x80 || c > 0xBF) return 0; else x |= (c & 0x3F);
2272 else if (c < 0xF0) {
2273 x = (c & 0xF) << 12;
2274 if ((c = *in++) < 0x80 || c > 0xBF) return 0; else x |= (c & 0x3F)<<6;
2275 if ((c = *in++) < 0x80 || c > 0xBF) return 0; else x |= (c & 0x3F);
2277 else return 0;
2280 /* terrible unicode -> ASCII conversion */
2281 if (x > 127) x = '_';
2283 if (in > end) return 0; /* just in case */
2284 } while ((*out++ = (cab_UBYTE) x));
2285 return 1;
2288 /****************************************************
2289 * NONEdecompress (internal)
2291 int NONEdecompress(int inlen, int outlen, cab_decomp_state *decomp_state)
2293 if (inlen != outlen) return DECR_ILLEGALDATA;
2294 memcpy(CAB(outbuf), CAB(inbuf), (size_t) inlen);
2295 return DECR_OK;
2298 /**************************************************
2299 * checksum (internal)
2301 cab_ULONG checksum(cab_UBYTE *data, cab_UWORD bytes, cab_ULONG csum) {
2302 int len;
2303 cab_ULONG ul = 0;
2305 for (len = bytes >> 2; len--; data += 4) {
2306 csum ^= ((data[0]) | (data[1]<<8) | (data[2]<<16) | (data[3]<<24));
2309 switch (bytes & 3) {
2310 case 3: ul |= *data++ << 16;
2311 case 2: ul |= *data++ << 8;
2312 case 1: ul |= *data;
2314 csum ^= ul;
2316 return csum;
2319 /**********************************************************
2320 * decompress (internal)
2322 int decompress(struct cab_file *fi, int savemode, int fix, cab_decomp_state *decomp_state)
2324 cab_ULONG bytes = savemode ? fi->length : fi->offset - CAB(offset);
2325 struct cabinet *cab = CAB(current)->cab[CAB(split)];
2326 cab_UBYTE buf[cfdata_SIZEOF], *data;
2327 cab_UWORD inlen, len, outlen, cando;
2328 cab_ULONG cksum;
2329 cab_LONG err;
2331 TRACE("(fi == ^%p, savemode == %d, fix == %d)\n", fi, savemode, fix);
2333 while (bytes > 0) {
2334 /* cando = the max number of bytes we can do */
2335 cando = CAB(outlen);
2336 if (cando > bytes) cando = bytes;
2338 /* if cando != 0 */
2339 if (cando && savemode)
2340 file_write(fi, CAB(outpos), cando);
2342 CAB(outpos) += cando;
2343 CAB(outlen) -= cando;
2344 bytes -= cando; if (!bytes) break;
2346 /* we only get here if we emptied the output buffer */
2348 /* read data header + data */
2349 inlen = outlen = 0;
2350 while (outlen == 0) {
2351 /* read the block header, skip the reserved part */
2352 if (!cabinet_read(cab, buf, cfdata_SIZEOF)) return DECR_INPUT;
2353 cabinet_skip(cab, cab->block_resv);
2355 /* we shouldn't get blocks over CAB_INPUTMAX in size */
2356 data = CAB(inbuf) + inlen;
2357 len = EndGetI16(buf+cfdata_CompressedSize);
2358 inlen += len;
2359 if (inlen > CAB_INPUTMAX) return DECR_INPUT;
2360 if (!cabinet_read(cab, data, len)) return DECR_INPUT;
2362 /* clear two bytes after read-in data */
2363 data[len+1] = data[len+2] = 0;
2365 /* perform checksum test on the block (if one is stored) */
2366 cksum = EndGetI32(buf+cfdata_CheckSum);
2367 if (cksum && cksum != checksum(buf+4, 4, checksum(data, len, 0))) {
2368 /* checksum is wrong */
2369 if (fix && ((fi->folder->comp_type & cffoldCOMPTYPE_MASK)
2370 == cffoldCOMPTYPE_MSZIP))
2372 WARN("%s: checksum failed\n", debugstr_a(fi->filename));
2374 else {
2375 return DECR_CHECKSUM;
2379 /* outlen=0 means this block was part of a split block */
2380 outlen = EndGetI16(buf+cfdata_UncompressedSize);
2381 if (outlen == 0) {
2382 cabinet_close(cab);
2383 cab = CAB(current)->cab[++CAB(split)];
2384 if (!cabinet_open(cab)) return DECR_INPUT;
2385 cabinet_seek(cab, CAB(current)->offset[CAB(split)]);
2389 /* decompress block */
2390 if ((err = CAB(decompress)(inlen, outlen, decomp_state))) {
2391 if (fix && ((fi->folder->comp_type & cffoldCOMPTYPE_MASK)
2392 == cffoldCOMPTYPE_MSZIP))
2394 ERR("%s: failed decrunching block\n", debugstr_a(fi->filename));
2396 else {
2397 return err;
2400 CAB(outlen) = outlen;
2401 CAB(outpos) = CAB(outbuf);
2404 return DECR_OK;
2407 /****************************************************************
2408 * extract_file (internal)
2410 * workhorse to extract a particular file from a cab
2412 void extract_file(struct cab_file *fi, int lower, int fix, LPCSTR dir, cab_decomp_state *decomp_state)
2414 struct cab_folder *fol = fi->folder, *oldfol = CAB(current);
2415 cab_LONG err = DECR_OK;
2417 TRACE("(fi == ^%p, lower == %d, fix == %d, dir == %s)\n", fi, lower, fix, debugstr_a(dir));
2419 /* is a change of folder needed? do we need to reset the current folder? */
2420 if (fol != oldfol || fi->offset < CAB(offset)) {
2421 cab_UWORD comptype = fol->comp_type;
2422 int ct1 = comptype & cffoldCOMPTYPE_MASK;
2423 int ct2 = oldfol ? (oldfol->comp_type & cffoldCOMPTYPE_MASK) : 0;
2425 /* if the archiver has changed, call the old archiver's free() function */
2426 if (ct1 != ct2) {
2427 switch (ct2) {
2428 case cffoldCOMPTYPE_LZX:
2429 if (LZX(window)) {
2430 free(LZX(window));
2431 LZX(window) = NULL;
2433 break;
2434 case cffoldCOMPTYPE_QUANTUM:
2435 if (QTM(window)) {
2436 free(QTM(window));
2437 QTM(window) = NULL;
2439 break;
2443 switch (ct1) {
2444 case cffoldCOMPTYPE_NONE:
2445 CAB(decompress) = NONEdecompress;
2446 break;
2448 case cffoldCOMPTYPE_MSZIP:
2449 CAB(decompress) = ZIPdecompress;
2450 break;
2452 case cffoldCOMPTYPE_QUANTUM:
2453 CAB(decompress) = QTMdecompress;
2454 err = QTMinit((comptype >> 8) & 0x1f, (comptype >> 4) & 0xF, decomp_state);
2455 break;
2457 case cffoldCOMPTYPE_LZX:
2458 CAB(decompress) = LZXdecompress;
2459 err = LZXinit((comptype >> 8) & 0x1f, decomp_state);
2460 break;
2462 default:
2463 err = DECR_DATAFORMAT;
2465 if (err) goto exit_handler;
2467 /* initialisation OK, set current folder and reset offset */
2468 if (oldfol) cabinet_close(oldfol->cab[CAB(split)]);
2469 if (!cabinet_open(fol->cab[0])) goto exit_handler;
2470 cabinet_seek(fol->cab[0], fol->offset[0]);
2471 CAB(current) = fol;
2472 CAB(offset) = 0;
2473 CAB(outlen) = 0; /* discard existing block */
2474 CAB(split) = 0;
2477 if (fi->offset > CAB(offset)) {
2478 /* decode bytes and send them to /dev/null */
2479 if ((err = decompress(fi, 0, fix, decomp_state))) goto exit_handler;
2480 CAB(offset) = fi->offset;
2483 if (!file_open(fi, lower, dir)) return;
2484 err = decompress(fi, 1, fix, decomp_state);
2485 if (err) CAB(current) = NULL; else CAB(offset) += fi->length;
2486 file_close(fi);
2488 exit_handler:
2489 if (err) {
2490 const char *errmsg;
2491 char *cabname;
2492 switch (err) {
2493 case DECR_NOMEMORY:
2494 errmsg = "out of memory!\n"; break;
2495 case DECR_ILLEGALDATA:
2496 errmsg = "%s: illegal or corrupt data\n"; break;
2497 case DECR_DATAFORMAT:
2498 errmsg = "%s: unsupported data format\n"; break;
2499 case DECR_CHECKSUM:
2500 errmsg = "%s: checksum error\n"; break;
2501 case DECR_INPUT:
2502 errmsg = "%s: input error\n"; break;
2503 case DECR_OUTPUT:
2504 errmsg = "%s: output error\n"; break;
2505 default:
2506 errmsg = "%s: unknown error (BUG)\n";
2509 if (CAB(current)) {
2510 cabname = (char *) (CAB(current)->cab[CAB(split)]->filename);
2512 else {
2513 cabname = (char *) (fi->folder->cab[0]->filename);
2516 ERR(errmsg, cabname);
2520 /*********************************************************
2521 * print_fileinfo (internal)
2523 void print_fileinfo(struct cab_file *fi) {
2524 int d = fi->date, t = fi->time;
2525 char *fname = NULL;
2527 if (fi->attribs & cffile_A_NAME_IS_UTF) {
2528 fname = malloc(strlen(fi->filename) + 1);
2529 if (fname) {
2530 strcpy(fname, fi->filename);
2531 convertUTF((cab_UBYTE *) fname);
2535 TRACE("%9u | %02d.%02d.%04d %02d:%02d:%02d | %s\n",
2536 fi->length,
2537 d & 0x1f, (d>>5) & 0xf, (d>>9) + 1980,
2538 t >> 11, (t>>5) & 0x3f, (t << 1) & 0x3e,
2539 fname ? fname : fi->filename
2542 if (fname) free(fname);
2545 /****************************************************************************
2546 * process_cabinet (internal)
2548 * called to simply "extract" a cabinet file. Will find every cabinet file
2549 * in that file, search for every chained cabinet attached to those cabinets,
2550 * and will either extract the cabinets, or ? (call a callback?)
2552 * PARAMS
2553 * cabname [I] name of the cabinet file to extract
2554 * dir [I] directory to extract to
2555 * fix [I] attempt to process broken cabinets
2556 * lower [I] ? (lower case something or other?)
2557 * dest [O]
2559 * RETURNS
2560 * Success: TRUE
2561 * Failure: FALSE
2563 BOOL process_cabinet(LPCSTR cabname, LPCSTR dir, BOOL fix, BOOL lower, EXTRACTdest *dest)
2565 struct cabinet *basecab, *cab, *cab1, *cab2;
2566 struct cab_file *filelist, *fi;
2567 struct ExtractFileList **destlistptr = &(dest->filelist);
2569 /* The first result of a search will be returned, and
2570 * the remaining results will be chained to it via the cab->next structure
2571 * member.
2573 cab_UBYTE search_buf[CAB_SEARCH_SIZE];
2575 cab_decomp_state decomp_state_local;
2576 cab_decomp_state *decomp_state = &decomp_state_local;
2578 /* has the list-mode header been seen before? */
2579 int viewhdr = 0;
2581 ZeroMemory(decomp_state, sizeof(cab_decomp_state));
2583 TRACE("Extract %s\n", debugstr_a(cabname));
2585 /* load the file requested */
2586 basecab = find_cabs_in_file(cabname, search_buf);
2587 if (!basecab) return FALSE;
2589 /* iterate over all cabinets found in that file */
2590 for (cab = basecab; cab; cab=cab->next) {
2592 /* bi-directionally load any spanning cabinets -- backwards */
2593 for (cab1 = cab; cab1->flags & cfheadPREV_CABINET; cab1 = cab1->prevcab) {
2594 TRACE("%s: extends backwards to %s (%s)\n", debugstr_a(cabname),
2595 debugstr_a(cab1->prevname), debugstr_a(cab1->previnfo));
2596 find_cabinet_file(&(cab1->prevname), cabname);
2597 if (!(cab1->prevcab = load_cab_offset(cab1->prevname, 0))) {
2598 ERR("%s: can't read previous cabinet %s\n", debugstr_a(cabname), debugstr_a(cab1->prevname));
2599 break;
2601 cab1->prevcab->nextcab = cab1;
2604 /* bi-directionally load any spanning cabinets -- forwards */
2605 for (cab2 = cab; cab2->flags & cfheadNEXT_CABINET; cab2 = cab2->nextcab) {
2606 TRACE("%s: extends to %s (%s)\n", debugstr_a(cabname),
2607 debugstr_a(cab2->nextname), debugstr_a(cab2->nextinfo));
2608 find_cabinet_file(&(cab2->nextname), cabname);
2609 if (!(cab2->nextcab = load_cab_offset(cab2->nextname, 0))) {
2610 ERR("%s: can't read next cabinet %s\n", debugstr_a(cabname), debugstr_a(cab2->nextname));
2611 break;
2613 cab2->nextcab->prevcab = cab2;
2616 filelist = process_files(cab1);
2617 CAB(current) = NULL;
2619 if (!viewhdr) {
2620 TRACE("File size | Date Time | Name\n");
2621 TRACE("----------+---------------------+-------------\n");
2622 viewhdr = 1;
2624 for (fi = filelist; fi; fi = fi->next) {
2625 print_fileinfo(fi);
2626 dest->filecount++;
2628 TRACE("Beginning Extraction...\n");
2629 for (fi = filelist; fi; fi = fi->next) {
2630 TRACE(" extracting: %s\n", debugstr_a(fi->filename));
2631 extract_file(fi, lower, fix, dir, decomp_state);
2632 sprintf(dest->lastfile, "%s%s%s",
2633 strlen(dest->directory) ? dest->directory : "",
2634 strlen(dest->directory) ? "\\": "",
2635 fi->filename);
2636 *destlistptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2637 sizeof(struct ExtractFileList));
2638 if(*destlistptr) {
2639 (*destlistptr)->unknown = TRUE; /* FIXME: were do we get the value? */
2640 (*destlistptr)->filename = HeapAlloc(GetProcessHeap(), 0, (
2641 strlen(fi->filename)+1));
2642 if((*destlistptr)->filename)
2643 lstrcpyA((*destlistptr)->filename, fi->filename);
2644 destlistptr = &((*destlistptr)->next);
2649 TRACE("Finished processing cabinet.\n");
2651 return TRUE;