2 * LZ Decompression functions
4 * Copyright 1996 Marcus Meissner
7 * FIXME: return values might be wrong
15 #include <sys/types.h>
27 /* The readahead length of the decompressor. Reading single bytes
28 * using _lread() would be SLOW.
32 /* Format of first 14 byte of LZ compressed file */
39 static BYTE LZMagic[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
41 static struct lzstate {
42 HFILE32 lzfd; /* the handle used by the program */
43 HFILE32 realfd; /* the real filedescriptor */
44 CHAR lastchar; /* the last char of the filename */
46 DWORD reallength; /* the decompressed length of the file */
47 DWORD realcurrent; /* the position the decompressor currently is */
48 DWORD realwanted; /* the position the user wants to read from */
50 BYTE table[0x1000]; /* the rotating LZ table */
51 UINT32 curtabent; /* CURrent TABle ENTry */
53 BYTE stringlen; /* length and position of current string */
54 DWORD stringpos; /* from stringtable */
57 WORD bytetype; /* bitmask within blocks */
59 BYTE *get; /* GETLEN bytes */
60 DWORD getcur; /* current read */
61 DWORD getlen; /* length last got */
63 static int nroflzstates=0;
65 /* reads one compressed byte, including buffering */
66 #define GET(lzs,b) _lzget(lzs,&b)
67 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
70 _lzget(struct lzstate *lzs,BYTE *b) {
71 if (lzs->getcur<lzs->getlen) {
72 *b = lzs->get[lzs->getcur++];
75 int ret = _lread32(lzs->realfd,lzs->get,GETLEN);
76 if (ret==HFILE_ERROR32)
86 /* internal function, reads lzheader
87 * returns BADINHANDLE for non filedescriptors
88 * return 0 for file not compressed using LZ
89 * return UNKNOWNALG for unknown algorithm
90 * returns lzfileheader in *head
92 static INT32 read_header(HFILE32 fd,struct lzfileheader *head)
96 if (_llseek32(fd,0,SEEK_SET)==-1)
97 return LZERROR_BADINHANDLE;
99 /* We can't directly read the lzfileheader struct due to
100 * structure element alignment
102 if (_lread32(fd,buf,14)<14)
104 memcpy(head->magic,buf,8);
105 memcpy(&(head->compressiontype),buf+8,1);
106 memcpy(&(head->lastchar),buf+9,1);
108 /* FIXME: consider endianess on non-intel architectures */
109 memcpy(&(head->reallength),buf+10,4);
111 if (memcmp(head->magic,LZMagic,8))
113 if (head->compressiontype!='A')
114 return LZERROR_UNKNOWNALG;
118 /***********************************************************************
119 * LZStart16 (LZEXPAND.7)
121 INT16 LZStart16(void)
123 dprintf_file(stddeb,"LZStart16(void)\n");
128 /***********************************************************************
131 INT32 LZStart32(void)
133 dprintf_file(stddeb,"LZStart32(void)\n");
138 /***********************************************************************
139 * LZInit16 (LZEXPAND.3)
141 HFILE16 LZInit16( HFILE16 hfSrc )
143 return LZInit32( hfSrc );
147 /***********************************************************************
150 * initializes internal decompression buffers, returns lzfiledescriptor.
151 * (return value the same as hfSrc, if hfSrc is not compressed)
152 * on failure, returns error code <0
153 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
154 * we use as much as we need, we just OR 0x400 to the passed HFILE.
156 * since _llseek uses the same types as libc.lseek, we just use the macros of
159 HFILE32 LZInit32( HFILE32 hfSrc )
162 struct lzfileheader head;
166 dprintf_file(stddeb,"LZInit(%d)\n",hfSrc);
167 ret=read_header(hfSrc,&head);
169 _llseek32(hfSrc,0,SEEK_SET);
170 return ret?ret:hfSrc;
172 lzstates=xrealloc(lzstates,(++nroflzstates)*sizeof(struct lzstate));
173 lzs = lzstates+(nroflzstates-1);
175 memset(lzs,'\0',sizeof(*lzs));
177 lzs->lzfd = hfSrc | 0x400;
178 lzs->lastchar = head.lastchar;
179 lzs->reallength = head.reallength;
181 lzs->get = xmalloc(GETLEN);
185 /* Yes, preinitialize with spaces */
186 memset(lzs->table,' ',0x1000);
187 /* Yes, start 16 byte from the END of the table */
188 lzs->curtabent = 0xff0;
193 /***********************************************************************
194 * LZDone (LZEXPAND.9) (LZ32.8)
198 dprintf_file(stddeb,"LZDone()\n");
202 /***********************************************************************
203 * GetExpandedName16 (LZEXPAND.10)
205 INT16 GetExpandedName16( LPCSTR in, LPSTR out )
207 return (INT16)GetExpandedName32A( in, out );
211 /***********************************************************************
212 * GetExpandedName32A (LZ32.9)
214 * gets the full filename of the compressed file 'in' by opening it
215 * and reading the header
217 * "file." is being translated to "file"
218 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
219 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
222 INT32 GetExpandedName32A( LPCSTR in, LPSTR out )
224 struct lzfileheader head;
227 INT32 fnislowercased,ret,len;
230 dprintf_file(stddeb,"GetExpandedName(%s)\n",in);
231 fd=OpenFile32(in,&ofs,OF_READ);
232 if (fd==HFILE_ERROR32)
233 return (INT32)(INT16)LZERROR_BADINHANDLE;
235 ret=read_header(fd,&head);
237 /* not a LZ compressed file, so the expanded name is the same
238 * as the input name */
244 /* look for directory prefix and skip it. */
246 while (NULL!=(t=strpbrk(s,"/\\:")))
249 /* now mangle the basename */
251 /* FIXME: hmm. shouldn't happen? */
252 fprintf(stddeb,__FILE__":GetExpandedFileName(), specified a directory or what? (%s)\n",in);
256 /* see if we should use lowercase or uppercase on the last char */
264 fnislowercased=islower(*t);
267 if (isalpha(head.lastchar)) {
269 head.lastchar=tolower(head.lastchar);
271 head.lastchar=toupper(head.lastchar);
274 /* now look where to replace the last character */
275 if (NULL!=(t=strchr(s,'.'))) {
281 t[len]=head.lastchar;
283 } /* else no modification necessary */
289 /***********************************************************************
290 * GetExpandedName32W (LZ32.11)
292 INT32 GetExpandedName32W( LPCWSTR in, LPWSTR out )
297 xout = HeapAlloc( GetProcessHeap(), 0, lstrlen32W(in)+3 );
298 xin = HEAP_strdupWtoA( GetProcessHeap(), 0, in );
299 ret = GetExpandedName16(xin,xout);
300 if (ret>0) lstrcpyAtoW(out,xout);
301 HeapFree( GetProcessHeap(), 0, xin );
302 HeapFree( GetProcessHeap(), 0, xout );
307 /***********************************************************************
308 * LZRead16 (LZEXPAND.5)
310 INT16 LZRead16( HFILE16 fd, LPVOID buf, UINT16 toread )
312 return LZRead32(fd,buf,toread);
316 /***********************************************************************
319 INT32 LZRead32( HFILE32 fd, LPVOID vbuf, UINT32 toread )
326 dprintf_file(stddeb,"LZRead32(%d,%p,%d)\n",fd,buf,toread);
328 for (i=0;i<nroflzstates;i++)
329 if (lzstates[i].lzfd==fd)
332 return _lread32(fd,buf,toread);
335 /* The decompressor itself is in a define, cause we need it twice
336 * in this function. (the decompressed byte will be in b)
338 #define DECOMPRESS_ONE_BYTE \
339 if (lzs->stringlen) { \
340 b = lzs->table[lzs->stringpos]; \
341 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
344 if (!(lzs->bytetype&0x100)) { \
346 return toread-howmuch; \
347 lzs->bytetype = b|0xFF00; \
349 if (lzs->bytetype & 1) { \
351 return toread-howmuch; \
355 if (1!=GET(lzs,b1)) \
356 return toread-howmuch; \
357 if (1!=GET(lzs,b2)) \
358 return toread-howmuch; \
362 * where CAB is the stringoffset in the table\
363 * and D+3 is the len of the string \
365 lzs->stringpos = b1|((b2&0xf0)<<4); \
366 lzs->stringlen = (b2&0xf)+2; \
367 /* 3, but we use a byte already below ... */\
368 b = lzs->table[lzs->stringpos];\
369 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
373 /* store b in table */ \
374 lzs->table[lzs->curtabent++]= b; \
375 lzs->curtabent &= 0xFFF; \
378 /* if someone has seeked, we have to bring the decompressor
381 if (lzs->realcurrent!=lzs->realwanted) {
382 /* if the wanted position is before the current position
383 * I see no easy way to unroll ... We have to restart at
384 * the beginning. *sigh*
386 if (lzs->realcurrent>lzs->realwanted) {
387 /* flush decompressor state */
388 _llseek32(lzs->realfd,14,SEEK_SET);
393 memset(lzs->table,' ',0x1000);
394 lzs->curtabent = 0xFF0;
396 while (lzs->realcurrent<lzs->realwanted) {
408 #undef DECOMPRESS_ONE_BYTE
412 /***********************************************************************
413 * LZSeek16 (LZEXPAND.4)
415 LONG LZSeek16( HFILE16 fd, LONG off, INT16 type )
417 return LZSeek32( fd, off, type );
421 /***********************************************************************
424 LONG LZSeek32( HFILE32 fd, LONG off, INT32 type )
430 dprintf_file(stddeb,"LZSeek(%d,%ld,%d)\n",fd,off,type);
431 for (i=0;i<nroflzstates;i++)
432 if (lzstates[i].lzfd==fd)
434 /* not compressed? just use normal _llseek() */
436 return _llseek32(fd,off,type);
438 newwanted = lzs->realwanted;
440 case 1: /* SEEK_CUR */
443 case 2: /* SEEK_END */
444 newwanted = lzs->reallength-off;
446 default:/* SEEK_SET */
450 if (newwanted>lzs->reallength)
451 return LZERROR_BADVALUE;
453 return LZERROR_BADVALUE;
454 lzs->realwanted = newwanted;
459 /***********************************************************************
460 * LZCopy16 (LZEXPAND.1)
462 LONG LZCopy16( HFILE16 src, HFILE16 dest )
464 return LZCopy32( src, dest );
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 LZCopy32( HFILE32 src, HFILE32 dest )
481 INT32 (*xread)(HFILE32,LPVOID,UINT32);
483 dprintf_file(stddeb,"LZCopy(%d,%d)\n",src,dest);
484 for (i=0;i<nroflzstates;i++)
485 if (src==lzstates[i].lzfd)
488 /* not compressed? just copy */
490 xread=(INT32(*)(HFILE32,LPVOID,UINT32))_lread32;
495 ret=xread(src,buf,BUFLEN);
504 wret = _lwrite32(dest,buf,ret);
506 return LZERROR_WRITE;
512 /* reverses GetExpandedPathname */
513 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
516 char *mfn = (char *)xmalloc( strlen(fn) + 3 ); /* "._" and \0 */
518 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
519 if ((p = strchr( p, '.' )))
522 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
523 else p[strlen(p)-1] = '_'; /* replace last character */
525 else strcat( mfn, "._" ); /* append "._" */
530 /***********************************************************************
531 * LZOpenFile16 (LZEXPAND.2)
533 HFILE16 LZOpenFile16( LPCSTR fn, LPOFSTRUCT ofs, UINT16 mode )
535 return LZOpenFile32A( fn, ofs, mode );
539 /***********************************************************************
540 * LZOpenFile32A (LZ32.1)
542 * Opens a file. If not compressed, open it as a normal file.
544 HFILE32 LZOpenFile32A( LPCSTR fn, LPOFSTRUCT ofs, UINT32 mode )
548 dprintf_file(stddeb,"LZOpenFile(%s,%p,%d)\n",fn,ofs,mode);
549 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
550 fd=OpenFile32(fn,ofs,mode);
551 if (fd==HFILE_ERROR32)
553 LPSTR mfn = LZEXPAND_MangleName(fn);
554 fd = OpenFile32(mfn,ofs,mode);
557 if ((mode&~0x70)!=OF_READ)
559 if (fd==HFILE_ERROR32)
560 return HFILE_ERROR32;
568 /***********************************************************************
569 * LZOpenFile32W (LZ32.10)
571 HFILE32 LZOpenFile32W( LPCWSTR fn, LPOFSTRUCT ofs, UINT32 mode )
577 xfn = HEAP_strdupWtoA( GetProcessHeap(), 0, fn);
578 ret = LZOpenFile16(xfn,ofs,mode);
579 HeapFree( GetProcessHeap(), 0, xfn );
580 if (ret!=HFILE_ERROR32) {
581 /* ofs->szPathName is an array with the OFSTRUCT */
582 yfn = HEAP_strdupAtoW( GetProcessHeap(), 0, ofs->szPathName );
583 memcpy(ofs->szPathName,yfn,lstrlen32W(yfn)*2+2);
584 HeapFree( GetProcessHeap(), 0, yfn );
590 /***********************************************************************
591 * LZClose16 (LZEXPAND.6)
593 void LZClose16( HFILE16 fd )
595 return LZClose32( fd );
599 /***********************************************************************
602 void LZClose32( HFILE32 fd )
606 dprintf_file(stddeb,"LZClose(%d)\n",fd);
607 for (i=0;i<nroflzstates;i++)
608 if (lzstates[i].lzfd==fd)
610 if (i==nroflzstates) {
615 free(lzstates[i].get);
616 _lclose32(lzstates[i].realfd);
617 memcpy(lzstates+i,lzstates+i+1,sizeof(struct lzstate)*(nroflzstates-i-1));
619 lzstates=xrealloc(lzstates,sizeof(struct lzstate)*nroflzstates);
622 /***********************************************************************
623 * CopyLZFile16 (LZEXPAND.8)
625 LONG CopyLZFile16( HFILE16 src, HFILE16 dest )
627 dprintf_file(stddeb,"CopyLZFile16(%d,%d)\n",src,dest);
628 return LZCopy32(src,dest);
632 /***********************************************************************
633 * CopyLZFile32 (LZ32.7)
635 * Copy src to dest (including uncompressing src).
636 * NOTE: Yes. This is exactly the same function as LZCopy.
638 LONG CopyLZFile32( HFILE32 src, HFILE32 dest )
640 dprintf_file(stddeb,"CopyLZFile32(%d,%d)\n",src,dest);
641 return LZCopy32(src,dest);