2 * LZ Decompression functions
4 * Copyright 1996 Marcus Meissner
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * The LZ (Lempel Ziv) decompression was used in win16 installation programs.
23 * It is a simple tabledriven decompression engine, the algorithm is not
24 * documented as far as I know. WINE does not contain a compressor for
27 * The implementation is complete and there have been no reports of failures
32 * o Check whether the return values are correct
40 #include <sys/types.h>
51 #include "wine/unicode.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(file);
56 /* The readahead length of the decompressor. Reading single bytes
57 * using _lread() would be SLOW.
61 #define LZ_MAGIC_LEN 8
62 #define LZ_HEADER_LEN 14
64 /* Format of first 14 byte of LZ compressed file */
66 BYTE magic[LZ_MAGIC_LEN];
71 static const BYTE LZMagic[LZ_MAGIC_LEN]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
73 #define LZ_TABLE_SIZE 0x1000
76 HFILE realfd; /* the real filedescriptor */
77 CHAR lastchar; /* the last char of the filename */
79 DWORD reallength; /* the decompressed length of the file */
80 DWORD realcurrent; /* the position the decompressor currently is */
81 DWORD realwanted; /* the position the user wants to read from */
83 BYTE table[LZ_TABLE_SIZE]; /* the rotating LZ table */
84 UINT curtabent; /* CURrent TABle ENTry */
86 BYTE stringlen; /* length and position of current string */
87 DWORD stringpos; /* from stringtable */
90 WORD bytetype; /* bitmask within blocks */
92 BYTE *get; /* GETLEN bytes */
93 DWORD getcur; /* current read */
94 DWORD getlen; /* length last got */
97 #define MAX_LZSTATES 16
98 static struct lzstate *lzstates[MAX_LZSTATES];
100 #define LZ_MIN_HANDLE 0x400
101 #define IS_LZ_HANDLE(h) (((h) >= LZ_MIN_HANDLE) && ((h) < LZ_MIN_HANDLE+MAX_LZSTATES))
102 #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)-LZ_MIN_HANDLE] : NULL)
104 /* reads one compressed byte, including buffering */
105 #define GET(lzs,b) _lzget(lzs,&b)
106 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
109 _lzget(struct lzstate *lzs,BYTE *b) {
110 if (lzs->getcur<lzs->getlen) {
111 *b = lzs->get[lzs->getcur++];
114 int ret = _lread(lzs->realfd,lzs->get,GETLEN);
115 if (ret==HFILE_ERROR)
125 /* internal function, reads lzheader
126 * returns BADINHANDLE for non filedescriptors
127 * return 0 for file not compressed using LZ
128 * return UNKNOWNALG for unknown algorithm
129 * returns lzfileheader in *head
131 static INT read_header(HFILE fd,struct lzfileheader *head)
133 BYTE buf[LZ_HEADER_LEN];
135 if (_llseek(fd,0,SEEK_SET)==-1)
136 return LZERROR_BADINHANDLE;
138 /* We can't directly read the lzfileheader struct due to
139 * structure element alignment
141 if (_lread(fd,buf,LZ_HEADER_LEN)<LZ_HEADER_LEN)
143 memcpy(head->magic,buf,LZ_MAGIC_LEN);
144 memcpy(&(head->compressiontype),buf+LZ_MAGIC_LEN,1);
145 memcpy(&(head->lastchar),buf+LZ_MAGIC_LEN+1,1);
147 /* FIXME: consider endianess on non-intel architectures */
148 memcpy(&(head->reallength),buf+LZ_MAGIC_LEN+2,4);
150 if (memcmp(head->magic,LZMagic,LZ_MAGIC_LEN))
152 if (head->compressiontype!='A')
153 return LZERROR_UNKNOWNALG;
158 /***********************************************************************
159 * LZStart (KERNEL32.@)
161 INT WINAPI LZStart(void)
168 /***********************************************************************
169 * LZInit (KERNEL32.@)
171 * initializes internal decompression buffers, returns lzfiledescriptor.
172 * (return value the same as hfSrc, if hfSrc is not compressed)
173 * on failure, returns error code <0
174 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
176 * since _llseek uses the same types as libc.lseek, we just use the macros of
179 HFILE WINAPI LZInit( HFILE hfSrc )
182 struct lzfileheader head;
187 TRACE("(%d)\n",hfSrc);
188 ret=read_header(hfSrc,&head);
190 _llseek(hfSrc,0,SEEK_SET);
191 return ret?ret:hfSrc;
193 for (i = 0; i < MAX_LZSTATES; i++) if (!lzstates[i]) break;
194 if (i == MAX_LZSTATES) return LZERROR_GLOBALLOC;
195 lzstates[i] = lzs = HeapAlloc( GetProcessHeap(), 0, sizeof(struct lzstate) );
196 if(lzs == NULL) return LZERROR_GLOBALLOC;
198 memset(lzs,'\0',sizeof(*lzs));
200 lzs->lastchar = head.lastchar;
201 lzs->reallength = head.reallength;
203 lzs->get = HeapAlloc( GetProcessHeap(), 0, GETLEN );
207 if(lzs->get == NULL) {
208 HeapFree(GetProcessHeap(), 0, lzs);
210 return LZERROR_GLOBALLOC;
213 /* Yes, preinitialize with spaces */
214 memset(lzs->table,' ',LZ_TABLE_SIZE);
215 /* Yes, start 16 byte from the END of the table */
216 lzs->curtabent = 0xff0;
217 return LZ_MIN_HANDLE + i;
221 /***********************************************************************
222 * LZDone (KERNEL32.@)
224 void WINAPI LZDone(void)
230 /***********************************************************************
231 * GetExpandedNameA (KERNEL32.@)
233 * gets the full filename of the compressed file 'in' by opening it
234 * and reading the header
236 * "file." is being translated to "file"
237 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
238 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
241 INT WINAPI GetExpandedNameA( LPSTR in, LPSTR out )
243 struct lzfileheader head;
246 INT fnislowercased,ret,len;
250 fd=OpenFile(in,&ofs,OF_READ);
252 return (INT)(INT16)LZERROR_BADINHANDLE;
254 ret=read_header(fd,&head);
256 /* not a LZ compressed file, so the expanded name is the same
257 * as the input name */
263 /* look for directory prefix and skip it. */
265 while (NULL!=(t=strpbrk(s,"/\\:")))
268 /* now mangle the basename */
270 /* FIXME: hmm. shouldn't happen? */
271 WARN("Specified a directory or what? (%s)\n",in);
275 /* see if we should use lowercase or uppercase on the last char */
283 fnislowercased=islower(*t);
286 if (isalpha(head.lastchar)) {
288 head.lastchar=tolower(head.lastchar);
290 head.lastchar=toupper(head.lastchar);
293 /* now look where to replace the last character */
294 if (NULL!=(t=strchr(s,'.'))) {
300 t[len]=head.lastchar;
302 } /* else no modification necessary */
308 /***********************************************************************
309 * GetExpandedNameW (KERNEL32.@)
311 INT WINAPI GetExpandedNameW( LPWSTR in, LPWSTR out )
314 DWORD len = WideCharToMultiByte( CP_ACP, 0, in, -1, NULL, 0, NULL, NULL );
315 char *xin = HeapAlloc( GetProcessHeap(), 0, len );
316 char *xout = HeapAlloc( GetProcessHeap(), 0, len+3 );
317 WideCharToMultiByte( CP_ACP, 0, in, -1, xin, len, NULL, NULL );
318 if ((ret = GetExpandedNameA( xin, xout )) > 0)
319 MultiByteToWideChar( CP_ACP, 0, xout, -1, out, strlenW(in)+4 );
320 HeapFree( GetProcessHeap(), 0, xin );
321 HeapFree( GetProcessHeap(), 0, xout );
326 /***********************************************************************
327 * LZRead (KERNEL32.@)
329 INT WINAPI LZRead( HFILE fd, LPSTR vbuf, INT toread )
336 TRACE("(%d,%p,%d)\n",fd,buf,toread);
338 if (!(lzs = GET_LZ_STATE(fd))) return _lread(fd,buf,toread);
340 /* The decompressor itself is in a define, cause we need it twice
341 * in this function. (the decompressed byte will be in b)
343 #define DECOMPRESS_ONE_BYTE \
344 if (lzs->stringlen) { \
345 b = lzs->table[lzs->stringpos]; \
346 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
349 if (!(lzs->bytetype&0x100)) { \
351 return toread-howmuch; \
352 lzs->bytetype = b|0xFF00; \
354 if (lzs->bytetype & 1) { \
356 return toread-howmuch; \
360 if (1!=GET(lzs,b1)) \
361 return toread-howmuch; \
362 if (1!=GET(lzs,b2)) \
363 return toread-howmuch; \
367 * where CAB is the stringoffset in the table\
368 * and D+3 is the len of the string \
370 lzs->stringpos = b1|((b2&0xf0)<<4); \
371 lzs->stringlen = (b2&0xf)+2; \
372 /* 3, but we use a byte already below ... */\
373 b = lzs->table[lzs->stringpos];\
374 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
378 /* store b in table */ \
379 lzs->table[lzs->curtabent++]= b; \
380 lzs->curtabent &= 0xFFF; \
383 /* if someone has seeked, we have to bring the decompressor
386 if (lzs->realcurrent!=lzs->realwanted) {
387 /* if the wanted position is before the current position
388 * I see no easy way to unroll ... We have to restart at
389 * the beginning. *sigh*
391 if (lzs->realcurrent>lzs->realwanted) {
392 /* flush decompressor state */
393 _llseek(lzs->realfd,LZ_HEADER_LEN,SEEK_SET);
398 memset(lzs->table,' ',LZ_TABLE_SIZE);
399 lzs->curtabent = 0xFF0;
401 while (lzs->realcurrent<lzs->realwanted) {
413 #undef DECOMPRESS_ONE_BYTE
417 /***********************************************************************
418 * LZSeek (KERNEL32.@)
420 LONG WINAPI LZSeek( HFILE fd, LONG off, INT type )
425 TRACE("(%d,%d,%d)\n",fd,off,type);
426 /* not compressed? just use normal _llseek() */
427 if (!(lzs = GET_LZ_STATE(fd))) return _llseek(fd,off,type);
428 newwanted = lzs->realwanted;
430 case 1: /* SEEK_CUR */
433 case 2: /* SEEK_END */
434 newwanted = lzs->reallength-off;
436 default:/* SEEK_SET */
440 if (newwanted>lzs->reallength)
441 return LZERROR_BADVALUE;
443 return LZERROR_BADVALUE;
444 lzs->realwanted = newwanted;
449 /***********************************************************************
450 * LZCopy (KERNEL32.@)
452 * Copies everything from src to dest
453 * if src is a LZ compressed file, it will be uncompressed.
454 * will return the number of bytes written to dest or errors.
456 LONG WINAPI LZCopy( HFILE src, HFILE dest )
458 int usedlzinit = 0, ret, wret;
460 HFILE oldsrc = src, srcfd;
465 /* we need that weird typedef, for i can't seem to get function pointer
466 * casts right. (Or they probably just do not like WINAPI in general)
468 typedef UINT (WINAPI *_readfun)(HFILE,LPVOID,UINT);
472 TRACE("(%d,%d)\n",src,dest);
473 if (!IS_LZ_HANDLE(src)) {
475 if ((INT)src <= 0) return 0;
476 if (src != oldsrc) usedlzinit=1;
479 /* not compressed? just copy */
480 if (!IS_LZ_HANDLE(src))
483 xread=(_readfun)LZRead;
486 ret=xread(src,buf,BUFLEN);
495 wret = _lwrite(dest,buf,ret);
497 return LZERROR_WRITE;
500 /* Maintain the timestamp of source file to destination file */
501 srcfd = (!(lzs = GET_LZ_STATE(src))) ? src : lzs->realfd;
502 GetFileTime((HANDLE)srcfd, NULL, NULL, &filetime);
503 SetFileTime((HANDLE)dest, NULL, NULL, &filetime);
512 /* reverses GetExpandedPathname */
513 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
516 char *mfn = HeapAlloc( GetProcessHeap(), 0, strlen(fn) + 3 ); /* "._" and \0 */
517 if(mfn == NULL) return NULL;
519 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
520 if ((p = strchr( p, '.' )))
523 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
524 else p[strlen(p)-1] = '_'; /* replace last character */
526 else strcat( mfn, "._" ); /* append "._" */
531 /***********************************************************************
532 * LZOpenFileA (KERNEL32.@)
534 * Opens a file. If not compressed, open it as a normal file.
536 HFILE WINAPI LZOpenFileA( LPSTR fn, LPOFSTRUCT ofs, WORD mode )
540 TRACE("(%s,%p,%d)\n",fn,ofs,mode);
541 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
542 fd=OpenFile(fn,ofs,mode);
545 LPSTR mfn = LZEXPAND_MangleName(fn);
546 fd = OpenFile(mfn,ofs,mode);
547 HeapFree( GetProcessHeap(), 0, mfn );
549 if ((mode&~0x70)!=OF_READ)
554 if ((INT)cfd <= 0) return fd;
559 /***********************************************************************
560 * LZOpenFileW (KERNEL32.@)
562 HFILE WINAPI LZOpenFileW( LPWSTR fn, LPOFSTRUCT ofs, WORD mode )
565 DWORD len = WideCharToMultiByte( CP_ACP, 0, fn, -1, NULL, 0, NULL, NULL );
566 LPSTR xfn = HeapAlloc( GetProcessHeap(), 0, len );
567 WideCharToMultiByte( CP_ACP, 0, fn, -1, xfn, len, NULL, NULL );
568 ret = LZOpenFileA(xfn,ofs,mode);
569 HeapFree( GetProcessHeap(), 0, xfn );
574 /***********************************************************************
575 * LZClose (KERNEL32.@)
577 void WINAPI LZClose( HFILE fd )
582 if (!(lzs = GET_LZ_STATE(fd))) _lclose(fd);
585 HeapFree( GetProcessHeap(), 0, lzs->get );
586 CloseHandle((HANDLE)lzs->realfd);
587 lzstates[fd - LZ_MIN_HANDLE] = NULL;
588 HeapFree( GetProcessHeap(), 0, lzs );