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>
34 #include "wine/unicode.h"
35 #include "wine/winbase16.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(file);
41 /* The readahead length of the decompressor. Reading single bytes
42 * using _lread() would be SLOW.
46 /* Format of first 14 byte of LZ compressed file */
53 static BYTE LZMagic[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
56 HFILE realfd; /* the real filedescriptor */
57 CHAR lastchar; /* the last char of the filename */
59 DWORD reallength; /* the decompressed length of the file */
60 DWORD realcurrent; /* the position the decompressor currently is */
61 DWORD realwanted; /* the position the user wants to read from */
63 BYTE table[0x1000]; /* the rotating LZ table */
64 UINT curtabent; /* CURrent TABle ENTry */
66 BYTE stringlen; /* length and position of current string */
67 DWORD stringpos; /* from stringtable */
70 WORD bytetype; /* bitmask within blocks */
72 BYTE *get; /* GETLEN bytes */
73 DWORD getcur; /* current read */
74 DWORD getlen; /* length last got */
77 #define MAX_LZSTATES 16
78 static struct lzstate *lzstates[MAX_LZSTATES];
80 #define IS_LZ_HANDLE(h) (((h) >= 0x400) && ((h) < 0x400+MAX_LZSTATES))
81 #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)-0x400] : NULL)
83 /* reads one compressed byte, including buffering */
84 #define GET(lzs,b) _lzget(lzs,&b)
85 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
88 _lzget(struct lzstate *lzs,BYTE *b) {
89 if (lzs->getcur<lzs->getlen) {
90 *b = lzs->get[lzs->getcur++];
93 int ret = _lread(lzs->realfd,lzs->get,GETLEN);
104 /* internal function, reads lzheader
105 * returns BADINHANDLE for non filedescriptors
106 * return 0 for file not compressed using LZ
107 * return UNKNOWNALG for unknown algorithm
108 * returns lzfileheader in *head
110 static INT read_header(HFILE fd,struct lzfileheader *head)
114 if (_llseek(fd,0,SEEK_SET)==-1)
115 return LZERROR_BADINHANDLE;
117 /* We can't directly read the lzfileheader struct due to
118 * structure element alignment
120 if (_lread(fd,buf,14)<14)
122 memcpy(head->magic,buf,8);
123 memcpy(&(head->compressiontype),buf+8,1);
124 memcpy(&(head->lastchar),buf+9,1);
126 /* FIXME: consider endianess on non-intel architectures */
127 memcpy(&(head->reallength),buf+10,4);
129 if (memcmp(head->magic,LZMagic,8))
131 if (head->compressiontype!='A')
132 return LZERROR_UNKNOWNALG;
136 /***********************************************************************
137 * LZStart (LZEXPAND.7)
139 INT16 WINAPI LZStart16(void)
146 /***********************************************************************
149 INT WINAPI LZStart(void)
156 /***********************************************************************
157 * LZInit (LZEXPAND.3)
159 HFILE16 WINAPI LZInit16( HFILE16 hfSrc )
161 HFILE ret = LZInit( DosFileHandleToWin32Handle(hfSrc) );
162 if (IS_LZ_HANDLE(ret)) return ret;
163 if ((INT)ret <= 0) return ret;
168 /***********************************************************************
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,' ',0x1000);
215 /* Yes, start 16 byte from the END of the table */
216 lzs->curtabent = 0xff0;
221 /***********************************************************************
222 * LZDone (LZEXPAND.9)
225 void WINAPI LZDone(void)
231 /***********************************************************************
232 * GetExpandedName (LZEXPAND.10)
234 INT16 WINAPI GetExpandedName16( LPCSTR in, LPSTR out )
236 return (INT16)GetExpandedNameA( in, out );
240 /***********************************************************************
241 * GetExpandedNameA (LZ32.@)
243 * gets the full filename of the compressed file 'in' by opening it
244 * and reading the header
246 * "file." is being translated to "file"
247 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
248 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
251 INT WINAPI GetExpandedNameA( LPCSTR in, LPSTR out )
253 struct lzfileheader head;
256 INT fnislowercased,ret,len;
260 fd=OpenFile(in,&ofs,OF_READ);
262 return (INT)(INT16)LZERROR_BADINHANDLE;
264 ret=read_header(fd,&head);
266 /* not a LZ compressed file, so the expanded name is the same
267 * as the input name */
273 /* look for directory prefix and skip it. */
275 while (NULL!=(t=strpbrk(s,"/\\:")))
278 /* now mangle the basename */
280 /* FIXME: hmm. shouldn't happen? */
281 WARN("Specified a directory or what? (%s)\n",in);
285 /* see if we should use lowercase or uppercase on the last char */
293 fnislowercased=islower(*t);
296 if (isalpha(head.lastchar)) {
298 head.lastchar=tolower(head.lastchar);
300 head.lastchar=toupper(head.lastchar);
303 /* now look where to replace the last character */
304 if (NULL!=(t=strchr(s,'.'))) {
310 t[len]=head.lastchar;
312 } /* else no modification necessary */
318 /***********************************************************************
319 * GetExpandedNameW (LZ32.@)
321 INT WINAPI GetExpandedNameW( LPCWSTR in, LPWSTR out )
324 DWORD len = WideCharToMultiByte( CP_ACP, 0, in, -1, NULL, 0, NULL, NULL );
325 char *xin = HeapAlloc( GetProcessHeap(), 0, len );
326 char *xout = HeapAlloc( GetProcessHeap(), 0, len+3 );
327 WideCharToMultiByte( CP_ACP, 0, in, -1, xin, len, NULL, NULL );
328 if ((ret = GetExpandedNameA( xin, xout )) > 0)
329 MultiByteToWideChar( CP_ACP, 0, xout, -1, out, strlenW(in)+4 );
330 HeapFree( GetProcessHeap(), 0, xin );
331 HeapFree( GetProcessHeap(), 0, xout );
336 /***********************************************************************
337 * LZRead (LZEXPAND.5)
339 INT16 WINAPI LZRead16( HFILE16 fd, LPVOID buf, UINT16 toread )
341 if (IS_LZ_HANDLE(fd)) return LZRead( fd, buf, toread );
342 return _lread( DosFileHandleToWin32Handle(fd), buf, toread );
346 /***********************************************************************
349 INT WINAPI LZRead( HFILE fd, LPVOID vbuf, UINT toread )
356 TRACE("(%d,%p,%d)\n",fd,buf,toread);
358 if (!(lzs = GET_LZ_STATE(fd))) return _lread(fd,buf,toread);
360 /* The decompressor itself is in a define, cause we need it twice
361 * in this function. (the decompressed byte will be in b)
363 #define DECOMPRESS_ONE_BYTE \
364 if (lzs->stringlen) { \
365 b = lzs->table[lzs->stringpos]; \
366 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
369 if (!(lzs->bytetype&0x100)) { \
371 return toread-howmuch; \
372 lzs->bytetype = b|0xFF00; \
374 if (lzs->bytetype & 1) { \
376 return toread-howmuch; \
380 if (1!=GET(lzs,b1)) \
381 return toread-howmuch; \
382 if (1!=GET(lzs,b2)) \
383 return toread-howmuch; \
387 * where CAB is the stringoffset in the table\
388 * and D+3 is the len of the string \
390 lzs->stringpos = b1|((b2&0xf0)<<4); \
391 lzs->stringlen = (b2&0xf)+2; \
392 /* 3, but we use a byte already below ... */\
393 b = lzs->table[lzs->stringpos];\
394 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
398 /* store b in table */ \
399 lzs->table[lzs->curtabent++]= b; \
400 lzs->curtabent &= 0xFFF; \
403 /* if someone has seeked, we have to bring the decompressor
406 if (lzs->realcurrent!=lzs->realwanted) {
407 /* if the wanted position is before the current position
408 * I see no easy way to unroll ... We have to restart at
409 * the beginning. *sigh*
411 if (lzs->realcurrent>lzs->realwanted) {
412 /* flush decompressor state */
413 _llseek(lzs->realfd,14,SEEK_SET);
418 memset(lzs->table,' ',0x1000);
419 lzs->curtabent = 0xFF0;
421 while (lzs->realcurrent<lzs->realwanted) {
433 #undef DECOMPRESS_ONE_BYTE
437 /***********************************************************************
438 * LZSeek (LZEXPAND.4)
440 LONG WINAPI LZSeek16( HFILE16 fd, LONG off, INT16 type )
442 if (IS_LZ_HANDLE(fd)) return LZSeek( fd, off, type );
443 return _llseek( DosFileHandleToWin32Handle(fd), off, type );
447 /***********************************************************************
450 LONG WINAPI LZSeek( HFILE fd, LONG off, INT type )
455 TRACE("(%d,%ld,%d)\n",fd,off,type);
456 /* not compressed? just use normal _llseek() */
457 if (!(lzs = GET_LZ_STATE(fd))) return _llseek(fd,off,type);
458 newwanted = lzs->realwanted;
460 case 1: /* SEEK_CUR */
463 case 2: /* SEEK_END */
464 newwanted = lzs->reallength-off;
466 default:/* SEEK_SET */
470 if (newwanted>lzs->reallength)
471 return LZERROR_BADVALUE;
473 return LZERROR_BADVALUE;
474 lzs->realwanted = newwanted;
479 /***********************************************************************
480 * LZCopy (LZEXPAND.1)
483 LONG WINAPI LZCopy16( HFILE16 src, HFILE16 dest )
485 /* already a LZ handle? */
486 if (IS_LZ_HANDLE(src)) return LZCopy( src, DosFileHandleToWin32Handle(dest) );
488 /* no, try to open one */
490 if ((INT16)src <= 0) return 0;
491 if (IS_LZ_HANDLE(src))
493 LONG ret = LZCopy( src, DosFileHandleToWin32Handle(dest) );
497 /* it was not a compressed file */
498 return LZCopy( DosFileHandleToWin32Handle(src), DosFileHandleToWin32Handle(dest) );
502 /***********************************************************************
505 * Copies everything from src to dest
506 * if src is a LZ compressed file, it will be uncompressed.
507 * will return the number of bytes written to dest or errors.
509 LONG WINAPI LZCopy( HFILE src, HFILE dest )
511 int usedlzinit=0,ret,wret;
516 /* we need that weird typedef, for i can't seem to get function pointer
517 * casts right. (Or they probably just do not like WINAPI in general)
519 typedef UINT (WINAPI *_readfun)(HFILE,LPVOID,UINT);
523 TRACE("(%d,%d)\n",src,dest);
524 if (!IS_LZ_HANDLE(src)) {
526 if ((INT)src <= 0) return 0;
527 if (src != oldsrc) usedlzinit=1;
530 /* not compressed? just copy */
531 if (!IS_LZ_HANDLE(src))
534 xread=(_readfun)LZRead;
537 ret=xread(src,buf,BUFLEN);
546 wret = _lwrite(dest,buf,ret);
548 return LZERROR_WRITE;
556 /* reverses GetExpandedPathname */
557 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
560 char *mfn = (char *)HeapAlloc( GetProcessHeap(), 0,
561 strlen(fn) + 3 ); /* "._" and \0 */
562 if(mfn == NULL) return NULL;
564 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
565 if ((p = strchr( p, '.' )))
568 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
569 else p[strlen(p)-1] = '_'; /* replace last character */
571 else strcat( mfn, "._" ); /* append "._" */
576 /***********************************************************************
577 * LZOpenFile (LZEXPAND.2)
579 HFILE16 WINAPI LZOpenFile16( LPCSTR fn, LPOFSTRUCT ofs, UINT16 mode )
581 HFILE hfret = LZOpenFileA( fn, ofs, mode );
582 /* return errors and LZ handles unmodified */
583 if ((INT)hfret <= 0) return hfret;
584 if (IS_LZ_HANDLE(hfret)) return hfret;
585 /* but allocate a dos handle for 'normal' files */
586 return Win32HandleToDosFileHandle(hfret);
590 /***********************************************************************
591 * LZOpenFileA (LZ32.@)
593 * Opens a file. If not compressed, open it as a normal file.
595 HFILE WINAPI LZOpenFileA( LPCSTR fn, LPOFSTRUCT ofs, UINT mode )
599 TRACE("(%s,%p,%d)\n",fn,ofs,mode);
600 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
601 fd=OpenFile(fn,ofs,mode);
604 LPSTR mfn = LZEXPAND_MangleName(fn);
605 fd = OpenFile(mfn,ofs,mode);
606 HeapFree( GetProcessHeap(), 0, mfn );
608 if ((mode&~0x70)!=OF_READ)
613 if ((INT)cfd <= 0) return fd;
618 /***********************************************************************
619 * LZOpenFileW (LZ32.@)
621 HFILE WINAPI LZOpenFileW( LPCWSTR fn, LPOFSTRUCT ofs, UINT mode )
624 DWORD len = WideCharToMultiByte( CP_ACP, 0, fn, -1, NULL, 0, NULL, NULL );
625 LPSTR xfn = HeapAlloc( GetProcessHeap(), 0, len );
626 WideCharToMultiByte( CP_ACP, 0, fn, -1, xfn, len, NULL, NULL );
627 ret = LZOpenFileA(xfn,ofs,mode);
628 HeapFree( GetProcessHeap(), 0, xfn );
633 /***********************************************************************
634 * LZClose (LZEXPAND.6)
636 void WINAPI LZClose16( HFILE16 fd )
638 if (IS_LZ_HANDLE(fd)) LZClose( fd );
639 else DisposeLZ32Handle( DosFileHandleToWin32Handle(fd) );
643 /***********************************************************************
646 void WINAPI LZClose( HFILE fd )
651 if (!(lzs = GET_LZ_STATE(fd))) _lclose(fd);
654 if (lzs->get) HeapFree( GetProcessHeap(), 0, lzs->get );
655 CloseHandle(lzs->realfd);
656 lzstates[fd - 0x400] = NULL;
657 HeapFree( GetProcessHeap(), 0, lzs );
661 /***********************************************************************
662 * CopyLZFile (LZEXPAND.8)
664 LONG WINAPI CopyLZFile16( HFILE16 src, HFILE16 dest )
666 TRACE("(%d,%d)\n",src,dest);
667 return LZCopy16(src,dest);
671 /***********************************************************************
672 * CopyLZFile (LZ32.@)
674 * Copy src to dest (including uncompressing src).
675 * NOTE: Yes. This is exactly the same function as LZCopy.
677 LONG WINAPI CopyLZFile( HFILE src, HFILE dest )
679 TRACE("(%d,%d)\n",src,dest);
680 return LZCopy(src,dest);