2 * LZ Decompression functions
4 * Copyright 1996 Marcus Meissner
7 * FIXME: return values might be wrong
12 #include <sys/types.h>
16 #include "wine/winbase16.h"
17 #include "wine/unicode.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(file);
23 LONG WINAPI CopyLZFile16(HFILE16,HFILE16);
24 INT16 WINAPI GetExpandedName16(LPCSTR,LPSTR);
25 void WINAPI LZClose16(HFILE16);
26 LONG WINAPI LZCopy16(HFILE16,HFILE16);
27 HFILE16 WINAPI LZInit16(HFILE16);
28 HFILE16 WINAPI LZOpenFile16(LPCSTR,LPOFSTRUCT,UINT16);
29 INT16 WINAPI LZRead16(HFILE16,LPVOID,UINT16);
30 LONG WINAPI LZSeek16(HFILE16,LONG,INT16);
31 INT16 WINAPI LZStart16(void);
33 /* The readahead length of the decompressor. Reading single bytes
34 * using _lread() would be SLOW.
38 /* Format of first 14 byte of LZ compressed file */
45 static BYTE LZMagic[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
48 HFILE realfd; /* the real filedescriptor */
49 CHAR lastchar; /* the last char of the filename */
51 DWORD reallength; /* the decompressed length of the file */
52 DWORD realcurrent; /* the position the decompressor currently is */
53 DWORD realwanted; /* the position the user wants to read from */
55 BYTE table[0x1000]; /* the rotating LZ table */
56 UINT curtabent; /* CURrent TABle ENTry */
58 BYTE stringlen; /* length and position of current string */
59 DWORD stringpos; /* from stringtable */
62 WORD bytetype; /* bitmask within blocks */
64 BYTE *get; /* GETLEN bytes */
65 DWORD getcur; /* current read */
66 DWORD getlen; /* length last got */
69 #define MAX_LZSTATES 16
70 static struct lzstate *lzstates[MAX_LZSTATES];
72 #define IS_LZ_HANDLE(h) (((h) >= 0x400) && ((h) < 0x400+MAX_LZSTATES))
73 #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)-0x400] : NULL)
75 /* reads one compressed byte, including buffering */
76 #define GET(lzs,b) _lzget(lzs,&b)
77 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
80 _lzget(struct lzstate *lzs,BYTE *b) {
81 if (lzs->getcur<lzs->getlen) {
82 *b = lzs->get[lzs->getcur++];
85 int ret = _lread(lzs->realfd,lzs->get,GETLEN);
96 /* internal function, reads lzheader
97 * returns BADINHANDLE for non filedescriptors
98 * return 0 for file not compressed using LZ
99 * return UNKNOWNALG for unknown algorithm
100 * returns lzfileheader in *head
102 static INT read_header(HFILE fd,struct lzfileheader *head)
106 if (_llseek(fd,0,SEEK_SET)==-1)
107 return LZERROR_BADINHANDLE;
109 /* We can't directly read the lzfileheader struct due to
110 * structure element alignment
112 if (_lread(fd,buf,14)<14)
114 memcpy(head->magic,buf,8);
115 memcpy(&(head->compressiontype),buf+8,1);
116 memcpy(&(head->lastchar),buf+9,1);
118 /* FIXME: consider endianess on non-intel architectures */
119 memcpy(&(head->reallength),buf+10,4);
121 if (memcmp(head->magic,LZMagic,8))
123 if (head->compressiontype!='A')
124 return LZERROR_UNKNOWNALG;
128 /***********************************************************************
129 * LZStart (LZEXPAND.7)
131 INT16 WINAPI LZStart16(void)
138 /***********************************************************************
141 INT WINAPI LZStart(void)
148 /***********************************************************************
149 * LZInit (LZEXPAND.3)
151 HFILE16 WINAPI LZInit16( HFILE16 hfSrc )
153 HFILE ret = LZInit( DosFileHandleToWin32Handle(hfSrc) );
154 if (IS_LZ_HANDLE(ret)) return ret;
155 if ((INT)ret <= 0) return ret;
160 /***********************************************************************
163 * initializes internal decompression buffers, returns lzfiledescriptor.
164 * (return value the same as hfSrc, if hfSrc is not compressed)
165 * on failure, returns error code <0
166 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
168 * since _llseek uses the same types as libc.lseek, we just use the macros of
171 HFILE WINAPI LZInit( HFILE hfSrc )
174 struct lzfileheader head;
179 TRACE("(%d)\n",hfSrc);
180 ret=read_header(hfSrc,&head);
182 _llseek(hfSrc,0,SEEK_SET);
183 return ret?ret:hfSrc;
185 for (i = 0; i < MAX_LZSTATES; i++) if (!lzstates[i]) break;
186 if (i == MAX_LZSTATES) return LZERROR_GLOBALLOC;
187 lzstates[i] = lzs = HeapAlloc( GetProcessHeap(), 0, sizeof(struct lzstate) );
188 if(lzs == NULL) return LZERROR_GLOBALLOC;
190 memset(lzs,'\0',sizeof(*lzs));
192 lzs->lastchar = head.lastchar;
193 lzs->reallength = head.reallength;
195 lzs->get = HeapAlloc( GetProcessHeap(), 0, GETLEN );
199 if(lzs->get == NULL) {
200 HeapFree(GetProcessHeap(), 0, lzs);
202 return LZERROR_GLOBALLOC;
205 /* Yes, preinitialize with spaces */
206 memset(lzs->table,' ',0x1000);
207 /* Yes, start 16 byte from the END of the table */
208 lzs->curtabent = 0xff0;
213 /***********************************************************************
214 * LZDone (LZEXPAND.9)
217 void WINAPI LZDone(void)
223 /***********************************************************************
224 * GetExpandedName (LZEXPAND.10)
226 INT16 WINAPI GetExpandedName16( LPCSTR in, LPSTR out )
228 return (INT16)GetExpandedNameA( in, out );
232 /***********************************************************************
233 * GetExpandedNameA (LZ32.@)
235 * gets the full filename of the compressed file 'in' by opening it
236 * and reading the header
238 * "file." is being translated to "file"
239 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
240 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
243 INT WINAPI GetExpandedNameA( LPCSTR in, LPSTR out )
245 struct lzfileheader head;
248 INT fnislowercased,ret,len;
252 fd=OpenFile(in,&ofs,OF_READ);
254 return (INT)(INT16)LZERROR_BADINHANDLE;
256 ret=read_header(fd,&head);
258 /* not a LZ compressed file, so the expanded name is the same
259 * as the input name */
265 /* look for directory prefix and skip it. */
267 while (NULL!=(t=strpbrk(s,"/\\:")))
270 /* now mangle the basename */
272 /* FIXME: hmm. shouldn't happen? */
273 WARN("Specified a directory or what? (%s)\n",in);
277 /* see if we should use lowercase or uppercase on the last char */
285 fnislowercased=islower(*t);
288 if (isalpha(head.lastchar)) {
290 head.lastchar=tolower(head.lastchar);
292 head.lastchar=toupper(head.lastchar);
295 /* now look where to replace the last character */
296 if (NULL!=(t=strchr(s,'.'))) {
302 t[len]=head.lastchar;
304 } /* else no modification necessary */
310 /***********************************************************************
311 * GetExpandedNameW (LZ32.@)
313 INT WINAPI GetExpandedNameW( LPCWSTR in, LPWSTR out )
316 DWORD len = WideCharToMultiByte( CP_ACP, 0, in, -1, NULL, 0, NULL, NULL );
317 char *xin = HeapAlloc( GetProcessHeap(), 0, len );
318 char *xout = HeapAlloc( GetProcessHeap(), 0, len+3 );
319 WideCharToMultiByte( CP_ACP, 0, in, -1, xin, len, NULL, NULL );
320 if ((ret = GetExpandedNameA( xin, xout )) > 0)
321 MultiByteToWideChar( CP_ACP, 0, xout, -1, out, strlenW(in)+4 );
322 HeapFree( GetProcessHeap(), 0, xin );
323 HeapFree( GetProcessHeap(), 0, xout );
328 /***********************************************************************
329 * LZRead (LZEXPAND.5)
331 INT16 WINAPI LZRead16( HFILE16 fd, LPVOID buf, UINT16 toread )
333 if (IS_LZ_HANDLE(fd)) return LZRead( fd, buf, toread );
334 return _lread( DosFileHandleToWin32Handle(fd), buf, toread );
338 /***********************************************************************
341 INT WINAPI LZRead( HFILE fd, LPVOID vbuf, UINT toread )
348 TRACE("(%d,%p,%d)\n",fd,buf,toread);
350 if (!(lzs = GET_LZ_STATE(fd))) return _lread(fd,buf,toread);
352 /* The decompressor itself is in a define, cause we need it twice
353 * in this function. (the decompressed byte will be in b)
355 #define DECOMPRESS_ONE_BYTE \
356 if (lzs->stringlen) { \
357 b = lzs->table[lzs->stringpos]; \
358 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
361 if (!(lzs->bytetype&0x100)) { \
363 return toread-howmuch; \
364 lzs->bytetype = b|0xFF00; \
366 if (lzs->bytetype & 1) { \
368 return toread-howmuch; \
372 if (1!=GET(lzs,b1)) \
373 return toread-howmuch; \
374 if (1!=GET(lzs,b2)) \
375 return toread-howmuch; \
379 * where CAB is the stringoffset in the table\
380 * and D+3 is the len of the string \
382 lzs->stringpos = b1|((b2&0xf0)<<4); \
383 lzs->stringlen = (b2&0xf)+2; \
384 /* 3, but we use a byte already below ... */\
385 b = lzs->table[lzs->stringpos];\
386 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
390 /* store b in table */ \
391 lzs->table[lzs->curtabent++]= b; \
392 lzs->curtabent &= 0xFFF; \
395 /* if someone has seeked, we have to bring the decompressor
398 if (lzs->realcurrent!=lzs->realwanted) {
399 /* if the wanted position is before the current position
400 * I see no easy way to unroll ... We have to restart at
401 * the beginning. *sigh*
403 if (lzs->realcurrent>lzs->realwanted) {
404 /* flush decompressor state */
405 _llseek(lzs->realfd,14,SEEK_SET);
410 memset(lzs->table,' ',0x1000);
411 lzs->curtabent = 0xFF0;
413 while (lzs->realcurrent<lzs->realwanted) {
425 #undef DECOMPRESS_ONE_BYTE
429 /***********************************************************************
430 * LZSeek (LZEXPAND.4)
432 LONG WINAPI LZSeek16( HFILE16 fd, LONG off, INT16 type )
434 if (IS_LZ_HANDLE(fd)) return LZSeek( fd, off, type );
435 return _llseek( DosFileHandleToWin32Handle(fd), off, type );
439 /***********************************************************************
442 LONG WINAPI LZSeek( HFILE fd, LONG off, INT type )
447 TRACE("(%d,%ld,%d)\n",fd,off,type);
448 /* not compressed? just use normal _llseek() */
449 if (!(lzs = GET_LZ_STATE(fd))) return _llseek(fd,off,type);
450 newwanted = lzs->realwanted;
452 case 1: /* SEEK_CUR */
455 case 2: /* SEEK_END */
456 newwanted = lzs->reallength-off;
458 default:/* SEEK_SET */
462 if (newwanted>lzs->reallength)
463 return LZERROR_BADVALUE;
465 return LZERROR_BADVALUE;
466 lzs->realwanted = newwanted;
471 /***********************************************************************
472 * LZCopy (LZEXPAND.1)
475 LONG WINAPI LZCopy16( HFILE16 src, HFILE16 dest )
477 /* already a LZ handle? */
478 if (IS_LZ_HANDLE(src)) return LZCopy( src, DosFileHandleToWin32Handle(dest) );
480 /* no, try to open one */
482 if ((INT16)src <= 0) return 0;
483 if (IS_LZ_HANDLE(src))
485 LONG ret = LZCopy( src, DosFileHandleToWin32Handle(dest) );
489 /* it was not a compressed file */
490 return LZCopy( DosFileHandleToWin32Handle(src), DosFileHandleToWin32Handle(dest) );
494 /***********************************************************************
497 * Copies everything from src to dest
498 * if src is a LZ compressed file, it will be uncompressed.
499 * will return the number of bytes written to dest or errors.
501 LONG WINAPI LZCopy( HFILE src, HFILE dest )
503 int usedlzinit=0,ret,wret;
508 /* we need that weird typedef, for i can't seem to get function pointer
509 * casts right. (Or they probably just do not like WINAPI in general)
511 typedef UINT WINAPI (*_readfun)(HFILE,LPVOID,UINT);
515 TRACE("(%d,%d)\n",src,dest);
516 if (!IS_LZ_HANDLE(src)) {
518 if ((INT)src <= 0) return 0;
519 if (src != oldsrc) usedlzinit=1;
522 /* not compressed? just copy */
523 if (!IS_LZ_HANDLE(src))
526 xread=(_readfun)LZRead;
529 ret=xread(src,buf,BUFLEN);
538 wret = _lwrite(dest,buf,ret);
540 return LZERROR_WRITE;
548 /* reverses GetExpandedPathname */
549 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
552 char *mfn = (char *)HeapAlloc( GetProcessHeap(), 0,
553 strlen(fn) + 3 ); /* "._" and \0 */
554 if(mfn == NULL) return NULL;
556 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
557 if ((p = strchr( p, '.' )))
560 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
561 else p[strlen(p)-1] = '_'; /* replace last character */
563 else strcat( mfn, "._" ); /* append "._" */
568 /***********************************************************************
569 * LZOpenFile (LZEXPAND.2)
571 HFILE16 WINAPI LZOpenFile16( LPCSTR fn, LPOFSTRUCT ofs, UINT16 mode )
573 HFILE hfret = LZOpenFileA( fn, ofs, mode );
574 /* return errors and LZ handles unmodified */
575 if ((INT)hfret <= 0) return hfret;
576 if (IS_LZ_HANDLE(hfret)) return hfret;
577 /* but allocate a dos handle for 'normal' files */
578 return Win32HandleToDosFileHandle(hfret);
582 /***********************************************************************
583 * LZOpenFileA (LZ32.@)
585 * Opens a file. If not compressed, open it as a normal file.
587 HFILE WINAPI LZOpenFileA( LPCSTR fn, LPOFSTRUCT ofs, UINT mode )
591 TRACE("(%s,%p,%d)\n",fn,ofs,mode);
592 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
593 fd=OpenFile(fn,ofs,mode);
596 LPSTR mfn = LZEXPAND_MangleName(fn);
597 fd = OpenFile(mfn,ofs,mode);
598 HeapFree( GetProcessHeap(), 0, mfn );
600 if ((mode&~0x70)!=OF_READ)
605 if ((INT)cfd <= 0) return fd;
610 /***********************************************************************
611 * LZOpenFileW (LZ32.@)
613 HFILE WINAPI LZOpenFileW( LPCWSTR fn, LPOFSTRUCT ofs, UINT mode )
616 DWORD len = WideCharToMultiByte( CP_ACP, 0, fn, -1, NULL, 0, NULL, NULL );
617 LPSTR xfn = HeapAlloc( GetProcessHeap(), 0, len );
618 WideCharToMultiByte( CP_ACP, 0, fn, -1, xfn, len, NULL, NULL );
619 ret = LZOpenFileA(xfn,ofs,mode);
620 HeapFree( GetProcessHeap(), 0, xfn );
625 /***********************************************************************
626 * LZClose (LZEXPAND.6)
628 void WINAPI LZClose16( HFILE16 fd )
630 if (IS_LZ_HANDLE(fd)) LZClose( fd );
631 else DisposeLZ32Handle( DosFileHandleToWin32Handle(fd) );
635 /***********************************************************************
638 void WINAPI LZClose( HFILE fd )
643 if (!(lzs = GET_LZ_STATE(fd))) _lclose(fd);
646 if (lzs->get) HeapFree( GetProcessHeap(), 0, lzs->get );
647 CloseHandle(lzs->realfd);
648 lzstates[fd - 0x400] = NULL;
649 HeapFree( GetProcessHeap(), 0, lzs );
653 /***********************************************************************
654 * CopyLZFile (LZEXPAND.8)
656 LONG WINAPI CopyLZFile16( HFILE16 src, HFILE16 dest )
658 TRACE("(%d,%d)\n",src,dest);
659 return LZCopy16(src,dest);
663 /***********************************************************************
664 * CopyLZFile (LZ32.@)
666 * Copy src to dest (including uncompressing src).
667 * NOTE: Yes. This is exactly the same function as LZCopy.
669 LONG WINAPI CopyLZFile( HFILE src, HFILE dest )
671 TRACE("(%d,%d)\n",src,dest);
672 return LZCopy(src,dest);