2 * LZ Decompression functions
4 * Copyright 1996 Marcus Meissner
7 * FIXME: return values might be wrong
14 #include <sys/types.h>
24 /* The readahead length of the decompressor. Reading single bytes
25 * using _lread() would be SLOW.
29 /* Format of first 14 byte of LZ compressed file */
36 static BYTE LZMagic[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
38 static struct lzstate {
39 HFILE32 lzfd; /* the handle used by the program */
40 HFILE32 realfd; /* the real filedescriptor */
41 CHAR lastchar; /* the last char of the filename */
43 DWORD reallength; /* the decompressed length of the file */
44 DWORD realcurrent; /* the position the decompressor currently is */
45 DWORD realwanted; /* the position the user wants to read from */
47 BYTE table[0x1000]; /* the rotating LZ table */
48 UINT32 curtabent; /* CURrent TABle ENTry */
50 BYTE stringlen; /* length and position of current string */
51 DWORD stringpos; /* from stringtable */
54 WORD bytetype; /* bitmask within blocks */
56 BYTE *get; /* GETLEN bytes */
57 DWORD getcur; /* current read */
58 DWORD getlen; /* length last got */
60 static int nroflzstates=0;
62 /* reads one compressed byte, including buffering */
63 #define GET(lzs,b) _lzget(lzs,&b)
64 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
67 _lzget(struct lzstate *lzs,BYTE *b) {
68 if (lzs->getcur<lzs->getlen) {
69 *b = lzs->get[lzs->getcur++];
72 int ret = _lread32(lzs->realfd,lzs->get,GETLEN);
73 if (ret==HFILE_ERROR32)
83 /* internal function, reads lzheader
84 * returns BADINHANDLE for non filedescriptors
85 * return 0 for file not compressed using LZ
86 * return UNKNOWNALG for unknown algorithm
87 * returns lzfileheader in *head
89 static INT32 read_header(HFILE32 fd,struct lzfileheader *head)
93 if (_llseek32(fd,0,SEEK_SET)==-1)
94 return LZERROR_BADINHANDLE;
96 /* We can't directly read the lzfileheader struct due to
97 * structure element alignment
99 if (_lread32(fd,buf,14)<14)
101 memcpy(head->magic,buf,8);
102 memcpy(&(head->compressiontype),buf+8,1);
103 memcpy(&(head->lastchar),buf+9,1);
105 /* FIXME: consider endianess on non-intel architectures */
106 memcpy(&(head->reallength),buf+10,4);
108 if (memcmp(head->magic,LZMagic,8))
110 if (head->compressiontype!='A')
111 return LZERROR_UNKNOWNALG;
115 /***********************************************************************
116 * LZStart16 (LZEXPAND.7)
118 INT16 WINAPI LZStart16(void)
120 TRACE(file,"(void)\n");
125 /***********************************************************************
128 INT32 WINAPI LZStart32(void)
130 TRACE(file,"(void)\n");
135 /***********************************************************************
136 * LZInit16 (LZEXPAND.3)
138 HFILE16 WINAPI LZInit16( HFILE16 hfSrc )
140 return HFILE32_TO_HFILE16( LZInit32( HFILE16_TO_HFILE32(hfSrc) ) );
144 /***********************************************************************
147 * initializes internal decompression buffers, returns lzfiledescriptor.
148 * (return value the same as hfSrc, if hfSrc is not compressed)
149 * on failure, returns error code <0
150 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
151 * we use as much as we need, we just OR 0x400 to the passed HFILE.
153 * since _llseek uses the same types as libc.lseek, we just use the macros of
156 HFILE32 WINAPI LZInit32( HFILE32 hfSrc )
159 struct lzfileheader head;
163 TRACE(file,"(%d)\n",hfSrc);
164 ret=read_header(hfSrc,&head);
166 _llseek32(hfSrc,0,SEEK_SET);
167 return ret?ret:hfSrc;
169 lzstates = HeapReAlloc( SystemHeap, 0, lzstates,
170 (++nroflzstates)*sizeof(struct lzstate) );
171 lzs = lzstates+(nroflzstates-1);
173 memset(lzs,'\0',sizeof(*lzs));
175 lzs->lzfd = hfSrc | 0x400;
176 lzs->lastchar = head.lastchar;
177 lzs->reallength = head.reallength;
179 lzs->get = HEAP_xalloc( GetProcessHeap(), 0, GETLEN );
183 /* Yes, preinitialize with spaces */
184 memset(lzs->table,' ',0x1000);
185 /* Yes, start 16 byte from the END of the table */
186 lzs->curtabent = 0xff0;
191 /***********************************************************************
192 * LZDone (LZEXPAND.9) (LZ32.8)
194 void WINAPI LZDone(void)
196 TRACE(file,"(void)\n");
200 /***********************************************************************
201 * GetExpandedName16 (LZEXPAND.10)
203 INT16 WINAPI GetExpandedName16( LPCSTR in, LPSTR out )
205 return (INT16)GetExpandedName32A( in, out );
209 /***********************************************************************
210 * GetExpandedName32A (LZ32.9)
212 * gets the full filename of the compressed file 'in' by opening it
213 * and reading the header
215 * "file." is being translated to "file"
216 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
217 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
220 INT32 WINAPI GetExpandedName32A( LPCSTR in, LPSTR out )
222 struct lzfileheader head;
225 INT32 fnislowercased,ret,len;
228 TRACE(file,"(%s)\n",in);
229 fd=OpenFile32(in,&ofs,OF_READ);
230 if (fd==HFILE_ERROR32)
231 return (INT32)(INT16)LZERROR_BADINHANDLE;
233 ret=read_header(fd,&head);
235 /* not a LZ compressed file, so the expanded name is the same
236 * as the input name */
242 /* look for directory prefix and skip it. */
244 while (NULL!=(t=strpbrk(s,"/\\:")))
247 /* now mangle the basename */
249 /* FIXME: hmm. shouldn't happen? */
250 WARN(file,"Specified a directory or what? (%s)\n",in);
254 /* see if we should use lowercase or uppercase on the last char */
262 fnislowercased=islower(*t);
265 if (isalpha(head.lastchar)) {
267 head.lastchar=tolower(head.lastchar);
269 head.lastchar=toupper(head.lastchar);
272 /* now look where to replace the last character */
273 if (NULL!=(t=strchr(s,'.'))) {
279 t[len]=head.lastchar;
281 } /* else no modification necessary */
287 /***********************************************************************
288 * GetExpandedName32W (LZ32.11)
290 INT32 WINAPI GetExpandedName32W( LPCWSTR in, LPWSTR out )
295 xout = HeapAlloc( GetProcessHeap(), 0, lstrlen32W(in)+3 );
296 xin = HEAP_strdupWtoA( GetProcessHeap(), 0, in );
297 ret = GetExpandedName16(xin,xout);
298 if (ret>0) lstrcpyAtoW(out,xout);
299 HeapFree( GetProcessHeap(), 0, xin );
300 HeapFree( GetProcessHeap(), 0, xout );
305 /***********************************************************************
306 * LZRead16 (LZEXPAND.5)
308 INT16 WINAPI LZRead16( HFILE16 fd, LPVOID buf, UINT16 toread )
310 return LZRead32(HFILE16_TO_HFILE32(fd),buf,toread);
314 /***********************************************************************
317 INT32 WINAPI LZRead32( HFILE32 fd, LPVOID vbuf, UINT32 toread )
324 TRACE(file,"(%d,%p,%d)\n",fd,buf,toread);
326 for (i=0;i<nroflzstates;i++)
327 if (lzstates[i].lzfd==fd)
330 return _lread32(fd,buf,toread);
333 /* The decompressor itself is in a define, cause we need it twice
334 * in this function. (the decompressed byte will be in b)
336 #define DECOMPRESS_ONE_BYTE \
337 if (lzs->stringlen) { \
338 b = lzs->table[lzs->stringpos]; \
339 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
342 if (!(lzs->bytetype&0x100)) { \
344 return toread-howmuch; \
345 lzs->bytetype = b|0xFF00; \
347 if (lzs->bytetype & 1) { \
349 return toread-howmuch; \
353 if (1!=GET(lzs,b1)) \
354 return toread-howmuch; \
355 if (1!=GET(lzs,b2)) \
356 return toread-howmuch; \
360 * where CAB is the stringoffset in the table\
361 * and D+3 is the len of the string \
363 lzs->stringpos = b1|((b2&0xf0)<<4); \
364 lzs->stringlen = (b2&0xf)+2; \
365 /* 3, but we use a byte already below ... */\
366 b = lzs->table[lzs->stringpos];\
367 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
371 /* store b in table */ \
372 lzs->table[lzs->curtabent++]= b; \
373 lzs->curtabent &= 0xFFF; \
376 /* if someone has seeked, we have to bring the decompressor
379 if (lzs->realcurrent!=lzs->realwanted) {
380 /* if the wanted position is before the current position
381 * I see no easy way to unroll ... We have to restart at
382 * the beginning. *sigh*
384 if (lzs->realcurrent>lzs->realwanted) {
385 /* flush decompressor state */
386 _llseek32(lzs->realfd,14,SEEK_SET);
391 memset(lzs->table,' ',0x1000);
392 lzs->curtabent = 0xFF0;
394 while (lzs->realcurrent<lzs->realwanted) {
406 #undef DECOMPRESS_ONE_BYTE
410 /***********************************************************************
411 * LZSeek16 (LZEXPAND.4)
413 LONG WINAPI LZSeek16( HFILE16 fd, LONG off, INT16 type )
415 return LZSeek32( HFILE16_TO_HFILE32(fd), off, type );
419 /***********************************************************************
422 LONG WINAPI LZSeek32( HFILE32 fd, LONG off, INT32 type )
428 TRACE(file,"(%d,%ld,%d)\n",fd,off,type);
429 for (i=0;i<nroflzstates;i++)
430 if (lzstates[i].lzfd==fd)
432 /* not compressed? just use normal _llseek() */
434 return _llseek32(fd,off,type);
436 newwanted = lzs->realwanted;
438 case 1: /* SEEK_CUR */
441 case 2: /* SEEK_END */
442 newwanted = lzs->reallength-off;
444 default:/* SEEK_SET */
448 if (newwanted>lzs->reallength)
449 return LZERROR_BADVALUE;
451 return LZERROR_BADVALUE;
452 lzs->realwanted = newwanted;
457 /***********************************************************************
458 * LZCopy16 (LZEXPAND.1)
461 LONG WINAPI LZCopy16( HFILE16 src, HFILE16 dest )
463 return LZCopy32( HFILE16_TO_HFILE32(src), HFILE16_TO_HFILE32(dest) );
467 /***********************************************************************
470 * Copies everything from src to dest
471 * if src is a LZ compressed file, it will be uncompressed.
472 * will return the number of bytes written to dest or errors.
474 LONG WINAPI LZCopy32( HFILE32 src, HFILE32 dest )
476 int usedlzinit=0,i,ret,wret;
478 HFILE32 oldsrc = src;
481 INT32 WINAPI (*xread)(HFILE32,LPVOID,UINT32);
483 TRACE(file,"(%d,%d)\n",src,dest);
492 for (i=0;i<nroflzstates;i++)
493 if (src==lzstates[i].lzfd)
495 /* not compressed? just copy */
497 xread=(INT32(*)(HFILE32,LPVOID,UINT32))_lread32;
502 ret=xread(src,buf,BUFLEN);
511 wret = _lwrite32(dest,buf,ret);
513 return LZERROR_WRITE;
521 /* reverses GetExpandedPathname */
522 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
525 char *mfn = (char *)HEAP_xalloc( GetProcessHeap(), 0,
526 strlen(fn) + 3 ); /* "._" and \0 */
528 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
529 if ((p = strchr( p, '.' )))
532 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
533 else p[strlen(p)-1] = '_'; /* replace last character */
535 else strcat( mfn, "._" ); /* append "._" */
540 /***********************************************************************
541 * LZOpenFile16 (LZEXPAND.2)
543 HFILE16 WINAPI LZOpenFile16( LPCSTR fn, LPOFSTRUCT ofs, UINT16 mode )
545 return HFILE32_TO_HFILE16 ( LZOpenFile32A( fn, ofs, mode ) );
549 /***********************************************************************
550 * LZOpenFile32A (LZ32.1)
552 * Opens a file. If not compressed, open it as a normal file.
554 HFILE32 WINAPI LZOpenFile32A( LPCSTR fn, LPOFSTRUCT ofs, UINT32 mode )
558 TRACE(file,"(%s,%p,%d)\n",fn,ofs,mode);
559 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
560 fd=OpenFile32(fn,ofs,mode);
561 if (fd==HFILE_ERROR32)
563 LPSTR mfn = LZEXPAND_MangleName(fn);
564 fd = OpenFile32(mfn,ofs,mode);
565 HeapFree( GetProcessHeap(), 0, mfn );
567 if ((mode&~0x70)!=OF_READ)
569 if (fd==HFILE_ERROR32)
570 return HFILE_ERROR32;
578 /***********************************************************************
579 * LZOpenFile32W (LZ32.10)
581 HFILE32 WINAPI LZOpenFile32W( LPCWSTR fn, LPOFSTRUCT ofs, UINT32 mode )
587 xfn = HEAP_strdupWtoA( GetProcessHeap(), 0, fn);
588 ret = LZOpenFile16(xfn,ofs,mode);
589 HeapFree( GetProcessHeap(), 0, xfn );
590 if (ret!=HFILE_ERROR32) {
591 /* ofs->szPathName is an array with the OFSTRUCT */
592 yfn = HEAP_strdupAtoW( GetProcessHeap(), 0, ofs->szPathName );
593 memcpy(ofs->szPathName,yfn,lstrlen32W(yfn)*2+2);
594 HeapFree( GetProcessHeap(), 0, yfn );
600 /***********************************************************************
601 * LZClose16 (LZEXPAND.6)
603 void WINAPI LZClose16( HFILE16 fd )
605 return LZClose32( HFILE16_TO_HFILE32 (fd) );
609 /***********************************************************************
612 void WINAPI LZClose32( HFILE32 fd )
616 TRACE(file,"(%d)\n",fd);
617 for (i=0;i<nroflzstates;i++)
618 if (lzstates[i].lzfd==fd)
620 if (i==nroflzstates) {
625 HeapFree( GetProcessHeap(), 0, lzstates[i].get );
626 _lclose32(lzstates[i].realfd);
627 memmove(lzstates+i,lzstates+i+1,
628 sizeof(struct lzstate)*(nroflzstates-i-1));
630 lzstates = HeapReAlloc( SystemHeap, 0, lzstates,
631 sizeof(struct lzstate)*nroflzstates );
634 /***********************************************************************
635 * CopyLZFile16 (LZEXPAND.8)
637 LONG WINAPI CopyLZFile16( HFILE16 src, HFILE16 dest )
639 TRACE(file,"(%d,%d)\n",src,dest);
640 return LZCopy32(HFILE16_TO_HFILE32(src),HFILE16_TO_HFILE32(dest));
644 /***********************************************************************
645 * CopyLZFile32 (LZ32.7)
647 * Copy src to dest (including uncompressing src).
648 * NOTE: Yes. This is exactly the same function as LZCopy.
650 LONG WINAPI CopyLZFile32( HFILE32 src, HFILE32 dest )
652 TRACE(file,"(%d,%d)\n",src,dest);
653 return LZCopy32(src,dest);