2 * LZ Decompression functions
4 * Copyright 1996 Marcus Meissner
7 * FIXME: return values might be wrong
10 #define NO_TRANSITION_TYPES /* This file is Win32-clean */
16 #include <sys/types.h>
28 /* The readahead length of the decompressor. Reading single bytes
29 * using _lread() would be SLOW.
33 /* Format of first 14 byte of LZ compressed file */
40 static BYTE LZMagic[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
42 static struct lzstate {
43 HFILE32 lzfd; /* the handle used by the program */
44 HFILE32 realfd; /* the real filedescriptor */
45 CHAR lastchar; /* the last char of the filename */
47 DWORD reallength; /* the decompressed length of the file */
48 DWORD realcurrent; /* the position the decompressor currently is */
49 DWORD realwanted; /* the position the user wants to read from */
51 BYTE table[0x1000]; /* the rotating LZ table */
52 UINT32 curtabent; /* CURrent TABle ENTry */
54 BYTE stringlen; /* length and position of current string */
55 DWORD stringpos; /* from stringtable */
58 WORD bytetype; /* bitmask within blocks */
60 BYTE *get; /* GETLEN bytes */
61 DWORD getcur; /* current read */
62 DWORD getlen; /* length last got */
64 static int nroflzstates=0;
66 /* reads one compressed byte, including buffering */
67 #define GET(lzs,b) _lzget(lzs,&b)
68 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
71 _lzget(struct lzstate *lzs,BYTE *b) {
72 if (lzs->getcur<lzs->getlen) {
73 *b = lzs->get[lzs->getcur++];
76 int ret = _lread32(lzs->realfd,lzs->get,GETLEN);
77 if (ret==HFILE_ERROR32)
87 /* internal function, reads lzheader
88 * returns BADINHANDLE for non filedescriptors
89 * return 0 for file not compressed using LZ
90 * return UNKNOWNALG for unknown algorithm
91 * returns lzfileheader in *head
93 static INT32 read_header(HFILE32 fd,struct lzfileheader *head)
97 if (_llseek32(fd,0,SEEK_SET)==-1)
98 return LZERROR_BADINHANDLE;
100 /* We can't directly read the lzfileheader struct due to
101 * structure element alignment
103 if (_lread32(fd,buf,14)<14)
105 memcpy(head->magic,buf,8);
106 memcpy(&(head->compressiontype),buf+8,1);
107 memcpy(&(head->lastchar),buf+9,1);
109 /* FIXME: consider endianess on non-intel architectures */
110 memcpy(&(head->reallength),buf+10,4);
112 if (memcmp(head->magic,LZMagic,8))
114 if (head->compressiontype!='A')
115 return LZERROR_UNKNOWNALG;
119 /***********************************************************************
120 * LZStart16 (LZEXPAND.7)
122 INT16 LZStart16(void)
124 dprintf_file(stddeb,"LZStart16(void)\n");
129 /***********************************************************************
132 INT32 LZStart32(void)
134 dprintf_file(stddeb,"LZStart32(void)\n");
139 /***********************************************************************
140 * LZInit16 (LZEXPAND.3)
142 HFILE16 LZInit16( HFILE16 hfSrc )
144 return LZInit32( hfSrc );
148 /***********************************************************************
151 * initializes internal decompression buffers, returns lzfiledescriptor.
152 * (return value the same as hfSrc, if hfSrc is not compressed)
153 * on failure, returns error code <0
154 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
155 * we use as much as we need, we just OR 0x400 to the passed HFILE.
157 * since _llseek uses the same types as libc.lseek, we just use the macros of
160 HFILE32 LZInit32( HFILE32 hfSrc )
163 struct lzfileheader head;
167 dprintf_file(stddeb,"LZInit(%d)\n",hfSrc);
168 ret=read_header(hfSrc,&head);
170 _llseek32(hfSrc,0,SEEK_SET);
171 return ret?ret:hfSrc;
173 lzstates=xrealloc(lzstates,(++nroflzstates)*sizeof(struct lzstate));
174 lzs = lzstates+(nroflzstates-1);
176 memset(lzs,'\0',sizeof(*lzs));
178 lzs->lzfd = hfSrc | 0x400;
179 lzs->lastchar = head.lastchar;
180 lzs->reallength = head.reallength;
182 lzs->get = xmalloc(GETLEN);
186 /* Yes, preinitialize with spaces */
187 memset(lzs->table,' ',0x1000);
188 /* Yes, start 16 byte from the END of the table */
189 lzs->curtabent = 0xff0;
194 /***********************************************************************
195 * LZDone (LZEXPAND.9) (LZ32.8)
199 dprintf_file(stddeb,"LZDone()\n");
203 /***********************************************************************
204 * GetExpandedName16 (LZEXPAND.10)
206 INT16 GetExpandedName16( LPCSTR in, LPSTR out )
208 return GetExpandedName32A( in, out );
212 /***********************************************************************
213 * GetExpandedName32A (LZ32.9)
215 * gets the full filename of the compressed file 'in' by opening it
216 * and reading the header
218 * "file." is being translated to "file"
219 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
220 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
223 INT32 GetExpandedName32A( LPCSTR in, LPSTR out )
225 struct lzfileheader head;
228 INT32 fnislowercased,ret,len;
231 dprintf_file(stddeb,"GetExpandedName(%s)\n",in);
232 fd=OpenFile32(in,&ofs,OF_READ);
233 if (fd==HFILE_ERROR32)
234 return LZERROR_BADINHANDLE;
235 ret=read_header(fd,&head);
238 return LZERROR_BADINHANDLE;
241 /* This line will crash if the caller hasn't allocated enough memory
246 /* look for directory prefix and skip it. */
248 while (NULL!=(t=strpbrk(s,"/\\:")))
251 /* now mangle the basename */
253 /* FIXME: hmm. shouldn't happen? */
254 fprintf(stddeb,__FILE__":GetExpandedFileName(), specified a directory or what? (%s)\n",in);
258 /* see if we should use lowercase or uppercase on the last char */
266 fnislowercased=islower(*t);
269 if (isalpha(head.lastchar)) {
271 head.lastchar=tolower(head.lastchar);
273 head.lastchar=toupper(head.lastchar);
276 /* now look where to replace the last character */
277 if (NULL!=(t=strchr(s,'.'))) {
283 t[len]=head.lastchar;
285 } /* else no modification necessary */
291 /***********************************************************************
292 * GetExpandedName32W (LZ32.11)
294 INT32 GetExpandedName32W( LPCWSTR in, LPWSTR out )
299 xout = HeapAlloc( GetProcessHeap(), 0, lstrlen32W(in)+3 );
300 xin = HEAP_strdupWtoA( GetProcessHeap(), 0, in );
301 ret = GetExpandedName16(xin,xout);
302 if (ret>0) lstrcpyAtoW(out,xout);
303 HeapFree( GetProcessHeap(), 0, xin );
304 HeapFree( GetProcessHeap(), 0, xout );
309 /***********************************************************************
310 * LZRead16 (LZEXPAND.5)
312 INT16 LZRead16( HFILE16 fd, LPVOID buf, UINT16 toread )
314 return LZRead32(fd,buf,toread);
318 /***********************************************************************
321 INT32 LZRead32( HFILE32 fd, LPVOID vbuf, UINT32 toread )
328 dprintf_file(stddeb,"LZRead32(%d,%p,%d)\n",fd,buf,toread);
330 for (i=0;i<nroflzstates;i++)
331 if (lzstates[i].lzfd==fd)
334 return _lread32(fd,buf,toread);
337 /* The decompressor itself is in a define, cause we need it twice
338 * in this function. (the decompressed byte will be in b)
340 #define DECOMPRESS_ONE_BYTE \
341 if (lzs->stringlen) { \
342 b = lzs->table[lzs->stringpos]; \
343 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
346 if (!(lzs->bytetype&0x100)) { \
348 return toread-howmuch; \
349 lzs->bytetype = b|0xFF00; \
351 if (lzs->bytetype & 1) { \
353 return toread-howmuch; \
357 if (1!=GET(lzs,b1)) \
358 return toread-howmuch; \
359 if (1!=GET(lzs,b2)) \
360 return toread-howmuch; \
364 * where CAB is the stringoffset in the table\
365 * and D+3 is the len of the string \
367 lzs->stringpos = b1|((b2&0xf0)<<4); \
368 lzs->stringlen = (b2&0xf)+2; \
369 /* 3, but we use a byte already below ... */\
370 b = lzs->table[lzs->stringpos];\
371 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
375 /* store b in table */ \
376 lzs->table[lzs->curtabent++]= b; \
377 lzs->curtabent &= 0xFFF; \
380 /* if someone has seeked, we have to bring the decompressor
383 if (lzs->realcurrent!=lzs->realwanted) {
384 /* if the wanted position is before the current position
385 * I see no easy way to unroll ... We have to restart at
386 * the beginning. *sigh*
388 if (lzs->realcurrent>lzs->realwanted) {
389 /* flush decompressor state */
390 _llseek32(lzs->realfd,14,SEEK_SET);
395 memset(lzs->table,' ',0x1000);
396 lzs->curtabent = 0xFF0;
398 while (lzs->realcurrent<lzs->realwanted) {
410 #undef DECOMPRESS_ONE_BYTE
414 /***********************************************************************
415 * LZSeek16 (LZEXPAND.4)
417 LONG LZSeek16( HFILE16 fd, LONG off, INT16 type )
419 return LZSeek32( fd, off, type );
423 /***********************************************************************
426 LONG LZSeek32( HFILE32 fd, LONG off, INT32 type )
432 dprintf_file(stddeb,"LZSeek(%d,%ld,%d)\n",fd,off,type);
433 for (i=0;i<nroflzstates;i++)
434 if (lzstates[i].lzfd==fd)
436 /* not compressed? just use normal _llseek() */
438 return _llseek32(fd,off,type);
440 newwanted = lzs->realwanted;
442 case 1: /* SEEK_CUR */
445 case 2: /* SEEK_END */
446 newwanted = lzs->reallength-off;
448 default:/* SEEK_SET */
452 if (newwanted>lzs->reallength)
453 return LZERROR_BADVALUE;
455 return LZERROR_BADVALUE;
456 lzs->realwanted = newwanted;
461 /***********************************************************************
462 * LZCopy16 (LZEXPAND.1)
464 LONG LZCopy16( HFILE16 src, HFILE16 dest )
466 return LZCopy32( src, dest );
470 /***********************************************************************
473 * Copies everything from src to dest
474 * if src is a LZ compressed file, it will be uncompressed.
475 * will return the number of bytes written to dest or errors.
477 LONG LZCopy32( HFILE32 src, HFILE32 dest )
483 INT32 (*xread)(HFILE32,LPVOID,UINT32);
485 dprintf_file(stddeb,"LZCopy(%d,%d)\n",src,dest);
486 for (i=0;i<nroflzstates;i++)
487 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;
514 /* reverses GetExpandedPathname */
515 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
518 char *mfn = (char *)xmalloc( strlen(fn) + 3 ); /* "._" and \0 */
520 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
521 if ((p = strchr( p, '.' )))
524 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
525 else p[strlen(p)-1] = '_'; /* replace last character */
527 else strcat( mfn, "._" ); /* append "._" */
532 /***********************************************************************
533 * LZOpenFile16 (LZEXPAND.2)
535 HFILE16 LZOpenFile16( LPCSTR fn, LPOFSTRUCT ofs, UINT16 mode )
537 return LZOpenFile32A( fn, ofs, mode );
541 /***********************************************************************
542 * LZOpenFile32A (LZ32.1)
544 * Opens a file. If not compressed, open it as a normal file.
546 HFILE32 LZOpenFile32A( LPCSTR fn, LPOFSTRUCT ofs, UINT32 mode )
550 dprintf_file(stddeb,"LZOpenFile(%s,%p,%d)\n",fn,ofs,mode);
551 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
552 fd=OpenFile32(fn,ofs,mode);
553 if (fd==HFILE_ERROR32)
555 LPSTR mfn = LZEXPAND_MangleName(fn);
556 fd = OpenFile32(mfn,ofs,mode);
559 if ((mode&~0x70)!=OF_READ)
561 if (fd==HFILE_ERROR32)
562 return HFILE_ERROR32;
570 /***********************************************************************
571 * LZOpenFile32W (LZ32.10)
573 HFILE32 LZOpenFile32W( LPCWSTR fn, LPOFSTRUCT ofs, UINT32 mode )
579 xfn = HEAP_strdupWtoA( GetProcessHeap(), 0, fn);
580 ret = LZOpenFile16(xfn,ofs,mode);
581 HeapFree( GetProcessHeap(), 0, xfn );
582 if (ret!=HFILE_ERROR32) {
583 /* ofs->szPathName is an array with the OFSTRUCT */
584 yfn = HEAP_strdupAtoW( GetProcessHeap(), 0, ofs->szPathName );
585 memcpy(ofs->szPathName,yfn,lstrlen32W(yfn)*2+2);
586 HeapFree( GetProcessHeap(), 0, yfn );
592 /***********************************************************************
593 * LZClose16 (LZEXPAND.6)
595 void LZClose16( HFILE16 fd )
597 return LZClose32( fd );
601 /***********************************************************************
604 void LZClose32( HFILE32 fd )
608 dprintf_file(stddeb,"LZClose(%d)\n",fd);
609 for (i=0;i<nroflzstates;i++)
610 if (lzstates[i].lzfd==fd)
612 if (i==nroflzstates) {
617 free(lzstates[i].get);
618 _lclose32(lzstates[i].realfd);
619 memcpy(lzstates+i,lzstates+i+1,sizeof(struct lzstate)*(nroflzstates-i-1));
621 lzstates=xrealloc(lzstates,sizeof(struct lzstate)*nroflzstates);
624 /***********************************************************************
625 * CopyLZFile16 (LZEXPAND.8)
627 LONG CopyLZFile16( HFILE16 src, HFILE16 dest )
629 dprintf_file(stddeb,"CopyLZFile16(%d,%d)\n",src,dest);
630 return LZCopy32(src,dest);
634 /***********************************************************************
635 * CopyLZFile32 (LZ32.7)
637 * Copy src to dest (including uncompressing src).
638 * NOTE: Yes. This is exactly the same function as LZCopy.
640 LONG CopyLZFile32( HFILE32 src, HFILE32 dest )
642 dprintf_file(stddeb,"CopyLZFile32(%d,%d)\n",src,dest);
643 return LZCopy32(src,dest);