2 * LZ Decompression functions
4 * Copyright 1996 Marcus Meissner
7 * FIXME: return values might be wrong
19 /* The readahead length of the decompressor. Reading single bytes
20 * using _lread() would be SLOW.
24 /* Format of first 14 byte of LZ compressed file */
31 static BYTE LZMagic[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
34 HFILE32 realfd; /* the real filedescriptor */
35 CHAR lastchar; /* the last char of the filename */
37 DWORD reallength; /* the decompressed length of the file */
38 DWORD realcurrent; /* the position the decompressor currently is */
39 DWORD realwanted; /* the position the user wants to read from */
41 BYTE table[0x1000]; /* the rotating LZ table */
42 UINT32 curtabent; /* CURrent TABle ENTry */
44 BYTE stringlen; /* length and position of current string */
45 DWORD stringpos; /* from stringtable */
48 WORD bytetype; /* bitmask within blocks */
50 BYTE *get; /* GETLEN bytes */
51 DWORD getcur; /* current read */
52 DWORD getlen; /* length last got */
55 #define MAX_LZSTATES 16
56 static struct lzstate *lzstates[MAX_LZSTATES];
58 #define IS_LZ_HANDLE(h) (((h) >= 0x400) && ((h) < 0x400+MAX_LZSTATES))
59 #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)] : NULL)
61 /* reads one compressed byte, including buffering */
62 #define GET(lzs,b) _lzget(lzs,&b)
63 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
66 _lzget(struct lzstate *lzs,BYTE *b) {
67 if (lzs->getcur<lzs->getlen) {
68 *b = lzs->get[lzs->getcur++];
71 int ret = _lread32(lzs->realfd,lzs->get,GETLEN);
72 if (ret==HFILE_ERROR32)
82 /* internal function, reads lzheader
83 * returns BADINHANDLE for non filedescriptors
84 * return 0 for file not compressed using LZ
85 * return UNKNOWNALG for unknown algorithm
86 * returns lzfileheader in *head
88 static INT32 read_header(HFILE32 fd,struct lzfileheader *head)
92 if (_llseek32(fd,0,SEEK_SET)==-1)
93 return LZERROR_BADINHANDLE;
95 /* We can't directly read the lzfileheader struct due to
96 * structure element alignment
98 if (_lread32(fd,buf,14)<14)
100 memcpy(head->magic,buf,8);
101 memcpy(&(head->compressiontype),buf+8,1);
102 memcpy(&(head->lastchar),buf+9,1);
104 /* FIXME: consider endianess on non-intel architectures */
105 memcpy(&(head->reallength),buf+10,4);
107 if (memcmp(head->magic,LZMagic,8))
109 if (head->compressiontype!='A')
110 return LZERROR_UNKNOWNALG;
114 /***********************************************************************
115 * LZStart16 (LZEXPAND.7)
117 INT16 WINAPI LZStart16(void)
119 TRACE(file,"(void)\n");
124 /***********************************************************************
127 INT32 WINAPI LZStart32(void)
129 TRACE(file,"(void)\n");
134 /***********************************************************************
135 * LZInit16 (LZEXPAND.3)
137 HFILE16 WINAPI LZInit16( HFILE16 hfSrc )
139 return LZInit32( FILE_GetHandle32(hfSrc) );
143 /***********************************************************************
146 * initializes internal decompression buffers, returns lzfiledescriptor.
147 * (return value the same as hfSrc, if hfSrc is not compressed)
148 * on failure, returns error code <0
149 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
151 * since _llseek uses the same types as libc.lseek, we just use the macros of
154 HFILE32 WINAPI LZInit32( HFILE32 hfSrc )
157 struct lzfileheader head;
162 TRACE(file,"(%d)\n",hfSrc);
163 ret=read_header(hfSrc,&head);
165 _llseek32(hfSrc,0,SEEK_SET);
166 return ret?ret:hfSrc;
168 for (i = 0; i < MAX_LZSTATES; i++) if (!lzstates[i]) break;
169 if (i == MAX_LZSTATES) return LZERROR_GLOBALLOC;
170 lzstates[i] = lzs = HeapAlloc( SystemHeap, 0, sizeof(struct lzstate) );
172 memset(lzs,'\0',sizeof(*lzs));
174 lzs->lastchar = head.lastchar;
175 lzs->reallength = head.reallength;
177 lzs->get = HEAP_xalloc( GetProcessHeap(), 0, GETLEN );
181 /* Yes, preinitialize with spaces */
182 memset(lzs->table,' ',0x1000);
183 /* Yes, start 16 byte from the END of the table */
184 lzs->curtabent = 0xff0;
189 /***********************************************************************
190 * LZDone (LZEXPAND.9) (LZ32.8)
192 void WINAPI LZDone(void)
194 TRACE(file,"(void)\n");
198 /***********************************************************************
199 * GetExpandedName16 (LZEXPAND.10)
201 INT16 WINAPI GetExpandedName16( LPCSTR in, LPSTR out )
203 return (INT16)GetExpandedName32A( in, out );
207 /***********************************************************************
208 * GetExpandedName32A (LZ32.9)
210 * gets the full filename of the compressed file 'in' by opening it
211 * and reading the header
213 * "file." is being translated to "file"
214 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
215 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
218 INT32 WINAPI GetExpandedName32A( LPCSTR in, LPSTR out )
220 struct lzfileheader head;
223 INT32 fnislowercased,ret,len;
226 TRACE(file,"(%s)\n",in);
227 fd=OpenFile32(in,&ofs,OF_READ);
228 if (fd==HFILE_ERROR32)
229 return (INT32)(INT16)LZERROR_BADINHANDLE;
231 ret=read_header(fd,&head);
233 /* not a LZ compressed file, so the expanded name is the same
234 * as the input name */
240 /* look for directory prefix and skip it. */
242 while (NULL!=(t=strpbrk(s,"/\\:")))
245 /* now mangle the basename */
247 /* FIXME: hmm. shouldn't happen? */
248 WARN(file,"Specified a directory or what? (%s)\n",in);
252 /* see if we should use lowercase or uppercase on the last char */
260 fnislowercased=islower(*t);
263 if (isalpha(head.lastchar)) {
265 head.lastchar=tolower(head.lastchar);
267 head.lastchar=toupper(head.lastchar);
270 /* now look where to replace the last character */
271 if (NULL!=(t=strchr(s,'.'))) {
277 t[len]=head.lastchar;
279 } /* else no modification necessary */
285 /***********************************************************************
286 * GetExpandedName32W (LZ32.11)
288 INT32 WINAPI GetExpandedName32W( LPCWSTR in, LPWSTR out )
293 xout = HeapAlloc( GetProcessHeap(), 0, lstrlen32W(in)+3 );
294 xin = HEAP_strdupWtoA( GetProcessHeap(), 0, in );
295 ret = GetExpandedName16(xin,xout);
296 if (ret>0) lstrcpyAtoW(out,xout);
297 HeapFree( GetProcessHeap(), 0, xin );
298 HeapFree( GetProcessHeap(), 0, xout );
303 /***********************************************************************
304 * LZRead16 (LZEXPAND.5)
306 INT16 WINAPI LZRead16( HFILE16 fd, LPVOID buf, UINT16 toread )
308 if (IS_LZ_HANDLE(fd)) return LZRead32( fd, buf, toread );
309 return _lread16( fd, buf, toread );
313 /***********************************************************************
316 INT32 WINAPI LZRead32( HFILE32 fd, LPVOID vbuf, UINT32 toread )
323 TRACE(file,"(%d,%p,%d)\n",fd,buf,toread);
325 if (!(lzs = GET_LZ_STATE(fd))) return _lread32(fd,buf,toread);
327 /* The decompressor itself is in a define, cause we need it twice
328 * in this function. (the decompressed byte will be in b)
330 #define DECOMPRESS_ONE_BYTE \
331 if (lzs->stringlen) { \
332 b = lzs->table[lzs->stringpos]; \
333 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
336 if (!(lzs->bytetype&0x100)) { \
338 return toread-howmuch; \
339 lzs->bytetype = b|0xFF00; \
341 if (lzs->bytetype & 1) { \
343 return toread-howmuch; \
347 if (1!=GET(lzs,b1)) \
348 return toread-howmuch; \
349 if (1!=GET(lzs,b2)) \
350 return toread-howmuch; \
354 * where CAB is the stringoffset in the table\
355 * and D+3 is the len of the string \
357 lzs->stringpos = b1|((b2&0xf0)<<4); \
358 lzs->stringlen = (b2&0xf)+2; \
359 /* 3, but we use a byte already below ... */\
360 b = lzs->table[lzs->stringpos];\
361 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
365 /* store b in table */ \
366 lzs->table[lzs->curtabent++]= b; \
367 lzs->curtabent &= 0xFFF; \
370 /* if someone has seeked, we have to bring the decompressor
373 if (lzs->realcurrent!=lzs->realwanted) {
374 /* if the wanted position is before the current position
375 * I see no easy way to unroll ... We have to restart at
376 * the beginning. *sigh*
378 if (lzs->realcurrent>lzs->realwanted) {
379 /* flush decompressor state */
380 _llseek32(lzs->realfd,14,SEEK_SET);
385 memset(lzs->table,' ',0x1000);
386 lzs->curtabent = 0xFF0;
388 while (lzs->realcurrent<lzs->realwanted) {
400 #undef DECOMPRESS_ONE_BYTE
404 /***********************************************************************
405 * LZSeek16 (LZEXPAND.4)
407 LONG WINAPI LZSeek16( HFILE16 fd, LONG off, INT16 type )
409 if (IS_LZ_HANDLE(fd)) return LZSeek32( fd, off, type );
410 return _llseek16( fd, off, type );
414 /***********************************************************************
417 LONG WINAPI LZSeek32( HFILE32 fd, LONG off, INT32 type )
422 TRACE(file,"(%d,%ld,%d)\n",fd,off,type);
423 /* not compressed? just use normal _llseek() */
424 if (!(lzs = GET_LZ_STATE(fd))) return _llseek32(fd,off,type);
425 newwanted = lzs->realwanted;
427 case 1: /* SEEK_CUR */
430 case 2: /* SEEK_END */
431 newwanted = lzs->reallength-off;
433 default:/* SEEK_SET */
437 if (newwanted>lzs->reallength)
438 return LZERROR_BADVALUE;
440 return LZERROR_BADVALUE;
441 lzs->realwanted = newwanted;
446 /***********************************************************************
447 * LZCopy16 (LZEXPAND.1)
450 LONG WINAPI LZCopy16( HFILE16 src, HFILE16 dest )
452 HFILE32 oldsrc = src;
456 if (!IS_LZ_HANDLE(src))
459 if (src!=oldsrc) usedlzinit=1;
460 if (src>0xfff0) return 0;
462 ret = LZCopy32( src, FILE_GetHandle32(dest) );
463 if (usedlzinit) LZClose32(src);
468 /***********************************************************************
471 * Copies everything from src to dest
472 * if src is a LZ compressed file, it will be uncompressed.
473 * will return the number of bytes written to dest or errors.
475 LONG WINAPI LZCopy32( HFILE32 src, HFILE32 dest )
477 int usedlzinit=0,ret,wret;
479 HFILE32 oldsrc = src;
482 UINT32 WINAPI (*xread)(HFILE32,LPVOID,UINT32);
484 TRACE(file,"(%d,%d)\n",src,dest);
485 if (!IS_LZ_HANDLE(src)) {
493 /* not compressed? just copy */
494 if (!IS_LZ_HANDLE(src))
496 else /* Note: Ignore warning, just mismatched INT/UINT */
500 ret=xread(src,buf,BUFLEN);
509 wret = _lwrite32(dest,buf,ret);
511 return LZERROR_WRITE;
519 /* reverses GetExpandedPathname */
520 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
523 char *mfn = (char *)HEAP_xalloc( GetProcessHeap(), 0,
524 strlen(fn) + 3 ); /* "._" and \0 */
526 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
527 if ((p = strchr( p, '.' )))
530 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
531 else p[strlen(p)-1] = '_'; /* replace last character */
533 else strcat( mfn, "._" ); /* append "._" */
538 /***********************************************************************
539 * LZOpenFile16 (LZEXPAND.2)
541 HFILE16 WINAPI LZOpenFile16( LPCSTR fn, LPOFSTRUCT ofs, UINT16 mode )
545 hfret = LZOpenFile32A( fn, ofs, mode );
546 /* return errors and LZ handles unmodified */
547 if (IS_LZ_HANDLE(hfret) || (hfret>=0xfff0) || (hfret<=0))
549 /* but allocate a dos handle for 'normal' files */
550 return FILE_AllocDosHandle(hfret);
554 /***********************************************************************
555 * LZOpenFile32A (LZ32.1)
557 * Opens a file. If not compressed, open it as a normal file.
559 HFILE32 WINAPI LZOpenFile32A( LPCSTR fn, LPOFSTRUCT ofs, UINT32 mode )
563 TRACE(file,"(%s,%p,%d)\n",fn,ofs,mode);
564 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
565 fd=OpenFile32(fn,ofs,mode);
566 if (fd==HFILE_ERROR32)
568 LPSTR mfn = LZEXPAND_MangleName(fn);
569 fd = OpenFile32(mfn,ofs,mode);
570 HeapFree( GetProcessHeap(), 0, mfn );
572 if ((mode&~0x70)!=OF_READ)
574 if (fd==HFILE_ERROR32)
575 return HFILE_ERROR32;
583 /***********************************************************************
584 * LZOpenFile32W (LZ32.10)
586 HFILE32 WINAPI LZOpenFile32W( LPCWSTR fn, LPOFSTRUCT ofs, UINT32 mode )
592 xfn = HEAP_strdupWtoA( GetProcessHeap(), 0, fn);
593 ret = LZOpenFile16(xfn,ofs,mode);
594 HeapFree( GetProcessHeap(), 0, xfn );
595 if (ret!=HFILE_ERROR32) {
596 /* ofs->szPathName is an array with the OFSTRUCT */
597 yfn = HEAP_strdupAtoW( GetProcessHeap(), 0, ofs->szPathName );
598 memcpy(ofs->szPathName,yfn,lstrlen32W(yfn)*2+2);
599 HeapFree( GetProcessHeap(), 0, yfn );
605 /***********************************************************************
606 * LZClose16 (LZEXPAND.6)
608 void WINAPI LZClose16( HFILE16 fd )
610 if (IS_LZ_HANDLE(fd)) LZClose32( fd );
611 else _lclose16( fd );
615 /***********************************************************************
618 void WINAPI LZClose32( HFILE32 fd )
622 TRACE(file,"(%d)\n",fd);
623 if (!(lzs = GET_LZ_STATE(fd))) _lclose32(fd);
626 if (lzs->get) HeapFree( GetProcessHeap(), 0, lzs->get );
627 CloseHandle(lzs->realfd);
628 lzstates[fd - 0x400] = NULL;
629 HeapFree( SystemHeap, 0, lzs );
633 /***********************************************************************
634 * CopyLZFile16 (LZEXPAND.8)
636 LONG WINAPI CopyLZFile16( HFILE16 src, HFILE16 dest )
638 TRACE(file,"(%d,%d)\n",src,dest);
639 return LZCopy16(src,dest);
643 /***********************************************************************
644 * CopyLZFile32 (LZ32.7)
646 * Copy src to dest (including uncompressing src).
647 * NOTE: Yes. This is exactly the same function as LZCopy.
649 LONG WINAPI CopyLZFile32( HFILE32 src, HFILE32 dest )
651 TRACE(file,"(%d,%d)\n",src,dest);
652 return LZCopy32(src,dest);