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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * FIXME: return values might be wrong
28 #include <sys/types.h>
37 #include "wine/unicode.h"
38 #include "wine/winbase16.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(file);
44 /* The readahead length of the decompressor. Reading single bytes
45 * using _lread() would be SLOW.
49 /* Format of first 14 byte of LZ compressed file */
56 static BYTE LZMagic[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
59 HFILE realfd; /* the real filedescriptor */
60 CHAR lastchar; /* the last char of the filename */
62 DWORD reallength; /* the decompressed length of the file */
63 DWORD realcurrent; /* the position the decompressor currently is */
64 DWORD realwanted; /* the position the user wants to read from */
66 BYTE table[0x1000]; /* the rotating LZ table */
67 UINT curtabent; /* CURrent TABle ENTry */
69 BYTE stringlen; /* length and position of current string */
70 DWORD stringpos; /* from stringtable */
73 WORD bytetype; /* bitmask within blocks */
75 BYTE *get; /* GETLEN bytes */
76 DWORD getcur; /* current read */
77 DWORD getlen; /* length last got */
80 #define MAX_LZSTATES 16
81 static struct lzstate *lzstates[MAX_LZSTATES];
83 #define IS_LZ_HANDLE(h) (((h) >= 0x400) && ((h) < 0x400+MAX_LZSTATES))
84 #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)-0x400] : NULL)
86 /* reads one compressed byte, including buffering */
87 #define GET(lzs,b) _lzget(lzs,&b)
88 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
91 _lzget(struct lzstate *lzs,BYTE *b) {
92 if (lzs->getcur<lzs->getlen) {
93 *b = lzs->get[lzs->getcur++];
96 int ret = _lread(lzs->realfd,lzs->get,GETLEN);
107 /* internal function, reads lzheader
108 * returns BADINHANDLE for non filedescriptors
109 * return 0 for file not compressed using LZ
110 * return UNKNOWNALG for unknown algorithm
111 * returns lzfileheader in *head
113 static INT read_header(HFILE fd,struct lzfileheader *head)
117 if (_llseek(fd,0,SEEK_SET)==-1)
118 return LZERROR_BADINHANDLE;
120 /* We can't directly read the lzfileheader struct due to
121 * structure element alignment
123 if (_lread(fd,buf,14)<14)
125 memcpy(head->magic,buf,8);
126 memcpy(&(head->compressiontype),buf+8,1);
127 memcpy(&(head->lastchar),buf+9,1);
129 /* FIXME: consider endianess on non-intel architectures */
130 memcpy(&(head->reallength),buf+10,4);
132 if (memcmp(head->magic,LZMagic,8))
134 if (head->compressiontype!='A')
135 return LZERROR_UNKNOWNALG;
139 /***********************************************************************
140 * LZStart (LZEXPAND.7)
142 INT16 WINAPI LZStart16(void)
149 /***********************************************************************
152 INT WINAPI LZStart(void)
159 /***********************************************************************
160 * LZInit (LZEXPAND.3)
162 HFILE16 WINAPI LZInit16( HFILE16 hfSrc )
164 HFILE ret = LZInit( DosFileHandleToWin32Handle(hfSrc) );
165 if (IS_LZ_HANDLE(ret)) return ret;
166 if ((INT)ret <= 0) return ret;
171 /***********************************************************************
174 * initializes internal decompression buffers, returns lzfiledescriptor.
175 * (return value the same as hfSrc, if hfSrc is not compressed)
176 * on failure, returns error code <0
177 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
179 * since _llseek uses the same types as libc.lseek, we just use the macros of
182 HFILE WINAPI LZInit( HFILE hfSrc )
185 struct lzfileheader head;
190 TRACE("(%d)\n",hfSrc);
191 ret=read_header(hfSrc,&head);
193 _llseek(hfSrc,0,SEEK_SET);
194 return ret?ret:hfSrc;
196 for (i = 0; i < MAX_LZSTATES; i++) if (!lzstates[i]) break;
197 if (i == MAX_LZSTATES) return LZERROR_GLOBALLOC;
198 lzstates[i] = lzs = HeapAlloc( GetProcessHeap(), 0, sizeof(struct lzstate) );
199 if(lzs == NULL) return LZERROR_GLOBALLOC;
201 memset(lzs,'\0',sizeof(*lzs));
203 lzs->lastchar = head.lastchar;
204 lzs->reallength = head.reallength;
206 lzs->get = HeapAlloc( GetProcessHeap(), 0, GETLEN );
210 if(lzs->get == NULL) {
211 HeapFree(GetProcessHeap(), 0, lzs);
213 return LZERROR_GLOBALLOC;
216 /* Yes, preinitialize with spaces */
217 memset(lzs->table,' ',0x1000);
218 /* Yes, start 16 byte from the END of the table */
219 lzs->curtabent = 0xff0;
224 /***********************************************************************
225 * LZDone (LZEXPAND.9)
228 void WINAPI LZDone(void)
234 /***********************************************************************
235 * GetExpandedName (LZEXPAND.10)
237 INT16 WINAPI GetExpandedName16( LPCSTR in, LPSTR out )
239 return (INT16)GetExpandedNameA( in, out );
243 /***********************************************************************
244 * GetExpandedNameA (LZ32.@)
246 * gets the full filename of the compressed file 'in' by opening it
247 * and reading the header
249 * "file." is being translated to "file"
250 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
251 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
254 INT WINAPI GetExpandedNameA( LPCSTR in, LPSTR out )
256 struct lzfileheader head;
259 INT fnislowercased,ret,len;
263 fd=OpenFile(in,&ofs,OF_READ);
265 return (INT)(INT16)LZERROR_BADINHANDLE;
267 ret=read_header(fd,&head);
269 /* not a LZ compressed file, so the expanded name is the same
270 * as the input name */
276 /* look for directory prefix and skip it. */
278 while (NULL!=(t=strpbrk(s,"/\\:")))
281 /* now mangle the basename */
283 /* FIXME: hmm. shouldn't happen? */
284 WARN("Specified a directory or what? (%s)\n",in);
288 /* see if we should use lowercase or uppercase on the last char */
296 fnislowercased=islower(*t);
299 if (isalpha(head.lastchar)) {
301 head.lastchar=tolower(head.lastchar);
303 head.lastchar=toupper(head.lastchar);
306 /* now look where to replace the last character */
307 if (NULL!=(t=strchr(s,'.'))) {
313 t[len]=head.lastchar;
315 } /* else no modification necessary */
321 /***********************************************************************
322 * GetExpandedNameW (LZ32.@)
324 INT WINAPI GetExpandedNameW( LPCWSTR in, LPWSTR out )
327 DWORD len = WideCharToMultiByte( CP_ACP, 0, in, -1, NULL, 0, NULL, NULL );
328 char *xin = HeapAlloc( GetProcessHeap(), 0, len );
329 char *xout = HeapAlloc( GetProcessHeap(), 0, len+3 );
330 WideCharToMultiByte( CP_ACP, 0, in, -1, xin, len, NULL, NULL );
331 if ((ret = GetExpandedNameA( xin, xout )) > 0)
332 MultiByteToWideChar( CP_ACP, 0, xout, -1, out, strlenW(in)+4 );
333 HeapFree( GetProcessHeap(), 0, xin );
334 HeapFree( GetProcessHeap(), 0, xout );
339 /***********************************************************************
340 * LZRead (LZEXPAND.5)
342 INT16 WINAPI LZRead16( HFILE16 fd, LPVOID buf, UINT16 toread )
344 if (IS_LZ_HANDLE(fd)) return LZRead( fd, buf, toread );
345 return _lread( DosFileHandleToWin32Handle(fd), buf, toread );
349 /***********************************************************************
352 INT WINAPI LZRead( HFILE fd, LPVOID vbuf, UINT toread )
359 TRACE("(%d,%p,%d)\n",fd,buf,toread);
361 if (!(lzs = GET_LZ_STATE(fd))) return _lread(fd,buf,toread);
363 /* The decompressor itself is in a define, cause we need it twice
364 * in this function. (the decompressed byte will be in b)
366 #define DECOMPRESS_ONE_BYTE \
367 if (lzs->stringlen) { \
368 b = lzs->table[lzs->stringpos]; \
369 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
372 if (!(lzs->bytetype&0x100)) { \
374 return toread-howmuch; \
375 lzs->bytetype = b|0xFF00; \
377 if (lzs->bytetype & 1) { \
379 return toread-howmuch; \
383 if (1!=GET(lzs,b1)) \
384 return toread-howmuch; \
385 if (1!=GET(lzs,b2)) \
386 return toread-howmuch; \
390 * where CAB is the stringoffset in the table\
391 * and D+3 is the len of the string \
393 lzs->stringpos = b1|((b2&0xf0)<<4); \
394 lzs->stringlen = (b2&0xf)+2; \
395 /* 3, but we use a byte already below ... */\
396 b = lzs->table[lzs->stringpos];\
397 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
401 /* store b in table */ \
402 lzs->table[lzs->curtabent++]= b; \
403 lzs->curtabent &= 0xFFF; \
406 /* if someone has seeked, we have to bring the decompressor
409 if (lzs->realcurrent!=lzs->realwanted) {
410 /* if the wanted position is before the current position
411 * I see no easy way to unroll ... We have to restart at
412 * the beginning. *sigh*
414 if (lzs->realcurrent>lzs->realwanted) {
415 /* flush decompressor state */
416 _llseek(lzs->realfd,14,SEEK_SET);
421 memset(lzs->table,' ',0x1000);
422 lzs->curtabent = 0xFF0;
424 while (lzs->realcurrent<lzs->realwanted) {
436 #undef DECOMPRESS_ONE_BYTE
440 /***********************************************************************
441 * LZSeek (LZEXPAND.4)
443 LONG WINAPI LZSeek16( HFILE16 fd, LONG off, INT16 type )
445 if (IS_LZ_HANDLE(fd)) return LZSeek( fd, off, type );
446 return _llseek( DosFileHandleToWin32Handle(fd), off, type );
450 /***********************************************************************
453 LONG WINAPI LZSeek( HFILE fd, LONG off, INT type )
458 TRACE("(%d,%ld,%d)\n",fd,off,type);
459 /* not compressed? just use normal _llseek() */
460 if (!(lzs = GET_LZ_STATE(fd))) return _llseek(fd,off,type);
461 newwanted = lzs->realwanted;
463 case 1: /* SEEK_CUR */
466 case 2: /* SEEK_END */
467 newwanted = lzs->reallength-off;
469 default:/* SEEK_SET */
473 if (newwanted>lzs->reallength)
474 return LZERROR_BADVALUE;
476 return LZERROR_BADVALUE;
477 lzs->realwanted = newwanted;
482 /***********************************************************************
483 * LZCopy (LZEXPAND.1)
486 LONG WINAPI LZCopy16( HFILE16 src, HFILE16 dest )
488 /* already a LZ handle? */
489 if (IS_LZ_HANDLE(src)) return LZCopy( src, DosFileHandleToWin32Handle(dest) );
491 /* no, try to open one */
493 if ((INT16)src <= 0) return 0;
494 if (IS_LZ_HANDLE(src))
496 LONG ret = LZCopy( src, DosFileHandleToWin32Handle(dest) );
500 /* it was not a compressed file */
501 return LZCopy( DosFileHandleToWin32Handle(src), DosFileHandleToWin32Handle(dest) );
505 /***********************************************************************
508 * Copies everything from src to dest
509 * if src is a LZ compressed file, it will be uncompressed.
510 * will return the number of bytes written to dest or errors.
512 LONG WINAPI LZCopy( HFILE src, HFILE dest )
514 int usedlzinit=0,ret,wret;
519 /* we need that weird typedef, for i can't seem to get function pointer
520 * casts right. (Or they probably just do not like WINAPI in general)
522 typedef UINT (WINAPI *_readfun)(HFILE,LPVOID,UINT);
526 TRACE("(%d,%d)\n",src,dest);
527 if (!IS_LZ_HANDLE(src)) {
529 if ((INT)src <= 0) return 0;
530 if (src != oldsrc) usedlzinit=1;
533 /* not compressed? just copy */
534 if (!IS_LZ_HANDLE(src))
537 xread=(_readfun)LZRead;
540 ret=xread(src,buf,BUFLEN);
549 wret = _lwrite(dest,buf,ret);
551 return LZERROR_WRITE;
559 /* reverses GetExpandedPathname */
560 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
563 char *mfn = (char *)HeapAlloc( GetProcessHeap(), 0,
564 strlen(fn) + 3 ); /* "._" and \0 */
565 if(mfn == NULL) return NULL;
567 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
568 if ((p = strchr( p, '.' )))
571 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
572 else p[strlen(p)-1] = '_'; /* replace last character */
574 else strcat( mfn, "._" ); /* append "._" */
579 /***********************************************************************
580 * LZOpenFile (LZEXPAND.2)
582 HFILE16 WINAPI LZOpenFile16( LPCSTR fn, LPOFSTRUCT ofs, UINT16 mode )
584 HFILE hfret = LZOpenFileA( fn, ofs, mode );
585 /* return errors and LZ handles unmodified */
586 if ((INT)hfret <= 0) return hfret;
587 if (IS_LZ_HANDLE(hfret)) return hfret;
588 /* but allocate a dos handle for 'normal' files */
589 return Win32HandleToDosFileHandle(hfret);
593 /***********************************************************************
594 * LZOpenFileA (LZ32.@)
596 * Opens a file. If not compressed, open it as a normal file.
598 HFILE WINAPI LZOpenFileA( LPCSTR fn, LPOFSTRUCT ofs, UINT mode )
602 TRACE("(%s,%p,%d)\n",fn,ofs,mode);
603 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
604 fd=OpenFile(fn,ofs,mode);
607 LPSTR mfn = LZEXPAND_MangleName(fn);
608 fd = OpenFile(mfn,ofs,mode);
609 HeapFree( GetProcessHeap(), 0, mfn );
611 if ((mode&~0x70)!=OF_READ)
616 if ((INT)cfd <= 0) return fd;
621 /***********************************************************************
622 * LZOpenFileW (LZ32.@)
624 HFILE WINAPI LZOpenFileW( LPCWSTR fn, LPOFSTRUCT ofs, UINT mode )
627 DWORD len = WideCharToMultiByte( CP_ACP, 0, fn, -1, NULL, 0, NULL, NULL );
628 LPSTR xfn = HeapAlloc( GetProcessHeap(), 0, len );
629 WideCharToMultiByte( CP_ACP, 0, fn, -1, xfn, len, NULL, NULL );
630 ret = LZOpenFileA(xfn,ofs,mode);
631 HeapFree( GetProcessHeap(), 0, xfn );
636 /***********************************************************************
637 * LZClose (LZEXPAND.6)
639 void WINAPI LZClose16( HFILE16 fd )
641 if (IS_LZ_HANDLE(fd)) LZClose( fd );
642 else DisposeLZ32Handle( DosFileHandleToWin32Handle(fd) );
646 /***********************************************************************
649 void WINAPI LZClose( HFILE fd )
654 if (!(lzs = GET_LZ_STATE(fd))) _lclose(fd);
657 if (lzs->get) HeapFree( GetProcessHeap(), 0, lzs->get );
658 CloseHandle(lzs->realfd);
659 lzstates[fd - 0x400] = NULL;
660 HeapFree( GetProcessHeap(), 0, lzs );
664 /***********************************************************************
665 * CopyLZFile (LZEXPAND.8)
667 LONG WINAPI CopyLZFile16( HFILE16 src, HFILE16 dest )
669 TRACE("(%d,%d)\n",src,dest);
670 return LZCopy16(src,dest);
674 /***********************************************************************
675 * CopyLZFile (LZ32.@)
677 * Copy src to dest (including uncompressing src).
678 * NOTE: Yes. This is exactly the same function as LZCopy.
680 LONG WINAPI CopyLZFile( HFILE src, HFILE dest )
682 TRACE("(%d,%d)\n",src,dest);
683 return LZCopy(src,dest);