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};
33 static struct lzstate {
34 HFILE32 lzfd; /* the handle used by the program */
35 HFILE32 realfd; /* the real filedescriptor */
36 CHAR lastchar; /* the last char of the filename */
38 DWORD reallength; /* the decompressed length of the file */
39 DWORD realcurrent; /* the position the decompressor currently is */
40 DWORD realwanted; /* the position the user wants to read from */
42 BYTE table[0x1000]; /* the rotating LZ table */
43 UINT32 curtabent; /* CURrent TABle ENTry */
45 BYTE stringlen; /* length and position of current string */
46 DWORD stringpos; /* from stringtable */
49 WORD bytetype; /* bitmask within blocks */
51 BYTE *get; /* GETLEN bytes */
52 DWORD getcur; /* current read */
53 DWORD getlen; /* length last got */
55 static int nroflzstates=0;
57 /* reads one compressed byte, including buffering */
58 #define GET(lzs,b) _lzget(lzs,&b)
59 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
62 _lzget(struct lzstate *lzs,BYTE *b) {
63 if (lzs->getcur<lzs->getlen) {
64 *b = lzs->get[lzs->getcur++];
67 int ret = _lread32(lzs->realfd,lzs->get,GETLEN);
68 if (ret==HFILE_ERROR32)
78 /* internal function, reads lzheader
79 * returns BADINHANDLE for non filedescriptors
80 * return 0 for file not compressed using LZ
81 * return UNKNOWNALG for unknown algorithm
82 * returns lzfileheader in *head
84 static INT32 read_header(HFILE32 fd,struct lzfileheader *head)
88 if (_llseek32(fd,0,SEEK_SET)==-1)
89 return LZERROR_BADINHANDLE;
91 /* We can't directly read the lzfileheader struct due to
92 * structure element alignment
94 if (_lread32(fd,buf,14)<14)
96 memcpy(head->magic,buf,8);
97 memcpy(&(head->compressiontype),buf+8,1);
98 memcpy(&(head->lastchar),buf+9,1);
100 /* FIXME: consider endianess on non-intel architectures */
101 memcpy(&(head->reallength),buf+10,4);
103 if (memcmp(head->magic,LZMagic,8))
105 if (head->compressiontype!='A')
106 return LZERROR_UNKNOWNALG;
110 /***********************************************************************
111 * LZStart16 (LZEXPAND.7)
113 INT16 WINAPI LZStart16(void)
115 TRACE(file,"(void)\n");
120 /***********************************************************************
123 INT32 WINAPI LZStart32(void)
125 TRACE(file,"(void)\n");
130 /***********************************************************************
131 * LZInit16 (LZEXPAND.3)
133 HFILE16 WINAPI LZInit16( HFILE16 hfSrc )
135 return HFILE32_TO_HFILE16( LZInit32( HFILE16_TO_HFILE32(hfSrc) ) );
139 /***********************************************************************
142 * initializes internal decompression buffers, returns lzfiledescriptor.
143 * (return value the same as hfSrc, if hfSrc is not compressed)
144 * on failure, returns error code <0
145 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
146 * we use as much as we need, we just OR 0x400 to the passed HFILE.
148 * since _llseek uses the same types as libc.lseek, we just use the macros of
151 HFILE32 WINAPI LZInit32( HFILE32 hfSrc )
154 struct lzfileheader head;
158 TRACE(file,"(%d)\n",hfSrc);
159 ret=read_header(hfSrc,&head);
161 _llseek32(hfSrc,0,SEEK_SET);
162 return ret?ret:hfSrc;
164 lzstates = HeapReAlloc( SystemHeap, 0, lzstates,
165 (++nroflzstates)*sizeof(struct lzstate) );
166 lzs = lzstates+(nroflzstates-1);
168 memset(lzs,'\0',sizeof(*lzs));
170 lzs->lzfd = hfSrc | 0x400;
171 lzs->lastchar = head.lastchar;
172 lzs->reallength = head.reallength;
174 lzs->get = HEAP_xalloc( GetProcessHeap(), 0, GETLEN );
178 /* Yes, preinitialize with spaces */
179 memset(lzs->table,' ',0x1000);
180 /* Yes, start 16 byte from the END of the table */
181 lzs->curtabent = 0xff0;
186 /***********************************************************************
187 * LZDone (LZEXPAND.9) (LZ32.8)
189 void WINAPI LZDone(void)
191 TRACE(file,"(void)\n");
195 /***********************************************************************
196 * GetExpandedName16 (LZEXPAND.10)
198 INT16 WINAPI GetExpandedName16( LPCSTR in, LPSTR out )
200 return (INT16)GetExpandedName32A( in, out );
204 /***********************************************************************
205 * GetExpandedName32A (LZ32.9)
207 * gets the full filename of the compressed file 'in' by opening it
208 * and reading the header
210 * "file." is being translated to "file"
211 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
212 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
215 INT32 WINAPI GetExpandedName32A( LPCSTR in, LPSTR out )
217 struct lzfileheader head;
220 INT32 fnislowercased,ret,len;
223 TRACE(file,"(%s)\n",in);
224 fd=OpenFile32(in,&ofs,OF_READ);
225 if (fd==HFILE_ERROR32)
226 return (INT32)(INT16)LZERROR_BADINHANDLE;
228 ret=read_header(fd,&head);
230 /* not a LZ compressed file, so the expanded name is the same
231 * as the input name */
237 /* look for directory prefix and skip it. */
239 while (NULL!=(t=strpbrk(s,"/\\:")))
242 /* now mangle the basename */
244 /* FIXME: hmm. shouldn't happen? */
245 WARN(file,"Specified a directory or what? (%s)\n",in);
249 /* see if we should use lowercase or uppercase on the last char */
257 fnislowercased=islower(*t);
260 if (isalpha(head.lastchar)) {
262 head.lastchar=tolower(head.lastchar);
264 head.lastchar=toupper(head.lastchar);
267 /* now look where to replace the last character */
268 if (NULL!=(t=strchr(s,'.'))) {
274 t[len]=head.lastchar;
276 } /* else no modification necessary */
282 /***********************************************************************
283 * GetExpandedName32W (LZ32.11)
285 INT32 WINAPI GetExpandedName32W( LPCWSTR in, LPWSTR out )
290 xout = HeapAlloc( GetProcessHeap(), 0, lstrlen32W(in)+3 );
291 xin = HEAP_strdupWtoA( GetProcessHeap(), 0, in );
292 ret = GetExpandedName16(xin,xout);
293 if (ret>0) lstrcpyAtoW(out,xout);
294 HeapFree( GetProcessHeap(), 0, xin );
295 HeapFree( GetProcessHeap(), 0, xout );
300 /***********************************************************************
301 * LZRead16 (LZEXPAND.5)
303 INT16 WINAPI LZRead16( HFILE16 fd, LPVOID buf, UINT16 toread )
305 return LZRead32(HFILE16_TO_HFILE32(fd),buf,toread);
309 /***********************************************************************
312 INT32 WINAPI LZRead32( HFILE32 fd, LPVOID vbuf, UINT32 toread )
319 TRACE(file,"(%d,%p,%d)\n",fd,buf,toread);
321 for (i=0;i<nroflzstates;i++)
322 if (lzstates[i].lzfd==fd)
325 return _lread32(fd,buf,toread);
328 /* The decompressor itself is in a define, cause we need it twice
329 * in this function. (the decompressed byte will be in b)
331 #define DECOMPRESS_ONE_BYTE \
332 if (lzs->stringlen) { \
333 b = lzs->table[lzs->stringpos]; \
334 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
337 if (!(lzs->bytetype&0x100)) { \
339 return toread-howmuch; \
340 lzs->bytetype = b|0xFF00; \
342 if (lzs->bytetype & 1) { \
344 return toread-howmuch; \
348 if (1!=GET(lzs,b1)) \
349 return toread-howmuch; \
350 if (1!=GET(lzs,b2)) \
351 return toread-howmuch; \
355 * where CAB is the stringoffset in the table\
356 * and D+3 is the len of the string \
358 lzs->stringpos = b1|((b2&0xf0)<<4); \
359 lzs->stringlen = (b2&0xf)+2; \
360 /* 3, but we use a byte already below ... */\
361 b = lzs->table[lzs->stringpos];\
362 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
366 /* store b in table */ \
367 lzs->table[lzs->curtabent++]= b; \
368 lzs->curtabent &= 0xFFF; \
371 /* if someone has seeked, we have to bring the decompressor
374 if (lzs->realcurrent!=lzs->realwanted) {
375 /* if the wanted position is before the current position
376 * I see no easy way to unroll ... We have to restart at
377 * the beginning. *sigh*
379 if (lzs->realcurrent>lzs->realwanted) {
380 /* flush decompressor state */
381 _llseek32(lzs->realfd,14,SEEK_SET);
386 memset(lzs->table,' ',0x1000);
387 lzs->curtabent = 0xFF0;
389 while (lzs->realcurrent<lzs->realwanted) {
401 #undef DECOMPRESS_ONE_BYTE
405 /***********************************************************************
406 * LZSeek16 (LZEXPAND.4)
408 LONG WINAPI LZSeek16( HFILE16 fd, LONG off, INT16 type )
410 return LZSeek32( HFILE16_TO_HFILE32(fd), off, type );
414 /***********************************************************************
417 LONG WINAPI LZSeek32( HFILE32 fd, LONG off, INT32 type )
423 TRACE(file,"(%d,%ld,%d)\n",fd,off,type);
424 for (i=0;i<nroflzstates;i++)
425 if (lzstates[i].lzfd==fd)
427 /* not compressed? just use normal _llseek() */
429 return _llseek32(fd,off,type);
431 newwanted = lzs->realwanted;
433 case 1: /* SEEK_CUR */
436 case 2: /* SEEK_END */
437 newwanted = lzs->reallength-off;
439 default:/* SEEK_SET */
443 if (newwanted>lzs->reallength)
444 return LZERROR_BADVALUE;
446 return LZERROR_BADVALUE;
447 lzs->realwanted = newwanted;
452 /***********************************************************************
453 * LZCopy16 (LZEXPAND.1)
456 LONG WINAPI LZCopy16( HFILE16 src, HFILE16 dest )
458 return LZCopy32( HFILE16_TO_HFILE32(src), HFILE16_TO_HFILE32(dest) );
462 /***********************************************************************
465 * Copies everything from src to dest
466 * if src is a LZ compressed file, it will be uncompressed.
467 * will return the number of bytes written to dest or errors.
469 LONG WINAPI LZCopy32( HFILE32 src, HFILE32 dest )
471 int usedlzinit=0,i,ret,wret;
473 HFILE32 oldsrc = src;
476 INT32 WINAPI (*xread)(HFILE32,LPVOID,UINT32);
478 TRACE(file,"(%d,%d)\n",src,dest);
487 for (i=0;i<nroflzstates;i++)
488 if (src==lzstates[i].lzfd)
490 /* not compressed? just copy */
492 xread=(INT32(*)(HFILE32,LPVOID,UINT32))_lread32;
497 ret=xread(src,buf,BUFLEN);
506 wret = _lwrite32(dest,buf,ret);
508 return LZERROR_WRITE;
516 /* reverses GetExpandedPathname */
517 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
520 char *mfn = (char *)HEAP_xalloc( GetProcessHeap(), 0,
521 strlen(fn) + 3 ); /* "._" and \0 */
523 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
524 if ((p = strchr( p, '.' )))
527 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
528 else p[strlen(p)-1] = '_'; /* replace last character */
530 else strcat( mfn, "._" ); /* append "._" */
535 /***********************************************************************
536 * LZOpenFile16 (LZEXPAND.2)
538 HFILE16 WINAPI LZOpenFile16( LPCSTR fn, LPOFSTRUCT ofs, UINT16 mode )
540 return HFILE32_TO_HFILE16 ( LZOpenFile32A( fn, ofs, mode ) );
544 /***********************************************************************
545 * LZOpenFile32A (LZ32.1)
547 * Opens a file. If not compressed, open it as a normal file.
549 HFILE32 WINAPI LZOpenFile32A( LPCSTR fn, LPOFSTRUCT ofs, UINT32 mode )
553 TRACE(file,"(%s,%p,%d)\n",fn,ofs,mode);
554 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
555 fd=OpenFile32(fn,ofs,mode);
556 if (fd==HFILE_ERROR32)
558 LPSTR mfn = LZEXPAND_MangleName(fn);
559 fd = OpenFile32(mfn,ofs,mode);
560 HeapFree( GetProcessHeap(), 0, mfn );
562 if ((mode&~0x70)!=OF_READ)
564 if (fd==HFILE_ERROR32)
565 return HFILE_ERROR32;
573 /***********************************************************************
574 * LZOpenFile32W (LZ32.10)
576 HFILE32 WINAPI LZOpenFile32W( LPCWSTR fn, LPOFSTRUCT ofs, UINT32 mode )
582 xfn = HEAP_strdupWtoA( GetProcessHeap(), 0, fn);
583 ret = LZOpenFile16(xfn,ofs,mode);
584 HeapFree( GetProcessHeap(), 0, xfn );
585 if (ret!=HFILE_ERROR32) {
586 /* ofs->szPathName is an array with the OFSTRUCT */
587 yfn = HEAP_strdupAtoW( GetProcessHeap(), 0, ofs->szPathName );
588 memcpy(ofs->szPathName,yfn,lstrlen32W(yfn)*2+2);
589 HeapFree( GetProcessHeap(), 0, yfn );
595 /***********************************************************************
596 * LZClose16 (LZEXPAND.6)
598 void WINAPI LZClose16( HFILE16 fd )
600 return LZClose32( HFILE16_TO_HFILE32 (fd) );
604 /***********************************************************************
607 void WINAPI LZClose32( HFILE32 fd )
611 TRACE(file,"(%d)\n",fd);
612 for (i=0;i<nroflzstates;i++)
613 if (lzstates[i].lzfd==fd)
615 if (i==nroflzstates) {
620 HeapFree( GetProcessHeap(), 0, lzstates[i].get );
621 _lclose32(lzstates[i].realfd);
622 memmove(lzstates+i,lzstates+i+1,
623 sizeof(struct lzstate)*(nroflzstates-i-1));
625 lzstates = HeapReAlloc( SystemHeap, 0, lzstates,
626 sizeof(struct lzstate)*nroflzstates );
629 /***********************************************************************
630 * CopyLZFile16 (LZEXPAND.8)
632 LONG WINAPI CopyLZFile16( HFILE16 src, HFILE16 dest )
634 TRACE(file,"(%d,%d)\n",src,dest);
635 return LZCopy32(HFILE16_TO_HFILE32(src),HFILE16_TO_HFILE32(dest));
639 /***********************************************************************
640 * CopyLZFile32 (LZ32.7)
642 * Copy src to dest (including uncompressing src).
643 * NOTE: Yes. This is exactly the same function as LZCopy.
645 LONG WINAPI CopyLZFile32( HFILE32 src, HFILE32 dest )
647 TRACE(file,"(%d,%d)\n",src,dest);
648 return LZCopy32(src,dest);