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 HFILE lzfd
; /* the handle used by the program */
43 HFILE 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 UINT 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
);
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
93 read_header(HFILE fd
,struct lzfileheader
*head
) {
96 if (_llseek(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 * LZStart [LZEXPAND.7] [LZ32.6]
122 dprintf_file(stddeb
,"LZStart(void)\n");
127 * LZInit [LZEXPAND.3] [LZ32.2]
129 * initializes internal decompression buffers, returns lzfiledescriptor.
130 * (return value the same as hfSrc, if hfSrc is not compressed)
131 * on failure, returns error code <0
132 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
133 * we use as much as we need, we just OR 0x400 to the passed HFILE.
135 * since _llseek uses the same types as libc.lseek, we just use the macros of
139 LZInit(HFILE hfSrc
) {
140 struct lzfileheader head
;
144 dprintf_file(stddeb
,"LZInit(%d)\n",hfSrc
);
145 ret
=read_header(hfSrc
,&head
);
147 _llseek(hfSrc
,0,SEEK_SET
);
148 return ret
?ret
:hfSrc
;
150 lzstates
=xrealloc(lzstates
,(++nroflzstates
)*sizeof(struct lzstate
));
151 lzs
= lzstates
+(nroflzstates
-1);
153 memset(lzs
,'\0',sizeof(*lzs
));
155 lzs
->lzfd
= hfSrc
| 0x400;
156 lzs
->lastchar
= head
.lastchar
;
157 lzs
->reallength
= head
.reallength
;
159 lzs
->get
= xmalloc(GETLEN
);
163 /* Yes, preinitialize with spaces */
164 memset(lzs
->table
,' ',0x1000);
165 /* Yes, start 16 byte from the END of the table */
166 lzs
->curtabent
= 0xff0;
171 * LZDone [LZEXPAND.9] [LZ32.8]
176 dprintf_file(stddeb
,"LZDone()\n");
180 * GetExpandedName [LZEXPAND.10]
182 * gets the full filename of the compressed file 'in' by opening it
183 * and reading the header
185 * "file." is being translated to "file"
186 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
187 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
191 GetExpandedName16(LPCSTR in
,LPSTR out
) {
192 struct lzfileheader head
;
195 INT fnislowercased
,ret
,len
;
198 dprintf_file(stddeb
,"GetExpandedName(%s)\n",in
);
199 fd
=OpenFile(in
,&ofs
,OF_READ
);
201 return LZERROR_BADINHANDLE
;
202 ret
=read_header(fd
,&head
);
205 return LZERROR_BADINHANDLE
;
208 /* This line will crash if the caller hasn't allocated enough memory
213 /* look for directory prefix and skip it. */
215 while (NULL
!=(t
=strpbrk(s
,"/\\:")))
218 /* now mangle the basename */
220 /* FIXME: hmm. shouldn't happen? */
221 fprintf(stddeb
,__FILE__
":GetExpandedFileName(), specified a directory or what? (%s)\n",in
);
225 /* see if we should use lowercase or uppercase on the last char */
233 fnislowercased
=islower(*t
);
236 if (isalpha(head
.lastchar
)) {
238 head
.lastchar
=tolower(head
.lastchar
);
240 head
.lastchar
=toupper(head
.lastchar
);
243 /* now look where to replace the last character */
244 if (NULL
!=(t
=strchr(s
,'.'))) {
250 t
[len
]=head
.lastchar
;
252 } /* else no modification necessary */
258 * GetExpandedNameW [LZ32.11]
261 GetExpandedName32W(LPCWSTR in
,LPWSTR out
) {
265 xout
= HeapAlloc( GetProcessHeap(), 0, lstrlen32W(in
)+3 );
266 xin
= HEAP_strdupWtoA( GetProcessHeap(), 0, in
);
267 ret
= GetExpandedName16(xin
,xout
);
268 if (ret
>0) lstrcpyAtoW(out
,xout
);
269 HeapFree( GetProcessHeap(), 0, xin
);
270 HeapFree( GetProcessHeap(), 0, xout
);
275 * GetExpandedNameA [LZ32.9]
278 GetExpandedName32A(LPCSTR in
,LPSTR out
) {
279 return GetExpandedName16(in
,out
);
283 * LZRead [LZEXPAND.5] [LZ32.4]
284 * just as normal read, but reads from LZ special fd and uncompresses.
287 LZRead16(HFILE fd
,SEGPTR segbuf
,UINT16 toread
) {
288 dprintf_file(stddeb
,"LZRead16(%d,%08lx,%d)\n",fd
,(DWORD
)segbuf
,toread
);
289 return LZRead32(fd
,(LPBYTE
)PTR_SEG_TO_LIN(segbuf
),toread
);
293 LZRead32(HFILE fd
,LPVOID vbuf
,UINT32 toread
) {
299 dprintf_file(stddeb
,"LZRead32(%d,%p,%d)\n",fd
,buf
,toread
);
301 for (i
=0;i
<nroflzstates
;i
++)
302 if (lzstates
[i
].lzfd
==fd
)
305 return _lread32(fd
,buf
,toread
);
308 /* The decompressor itself is in a define, cause we need it twice
309 * in this function. (the decompressed byte will be in b)
311 #define DECOMPRESS_ONE_BYTE \
312 if (lzs->stringlen) { \
313 b = lzs->table[lzs->stringpos]; \
314 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
317 if (!(lzs->bytetype&0x100)) { \
319 return toread-howmuch; \
320 lzs->bytetype = b|0xFF00; \
322 if (lzs->bytetype & 1) { \
324 return toread-howmuch; \
328 if (1!=GET(lzs,b1)) \
329 return toread-howmuch; \
330 if (1!=GET(lzs,b2)) \
331 return toread-howmuch; \
335 * where CAB is the stringoffset in the table\
336 * and D+3 is the len of the string \
338 lzs->stringpos = b1|((b2&0xf0)<<4); \
339 lzs->stringlen = (b2&0xf)+2; \
340 /* 3, but we use a byte already below ... */\
341 b = lzs->table[lzs->stringpos];\
342 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
346 /* store b in table */ \
347 lzs->table[lzs->curtabent++]= b; \
348 lzs->curtabent &= 0xFFF; \
351 /* if someone has seeked, we have to bring the decompressor
354 if (lzs
->realcurrent
!=lzs
->realwanted
) {
355 /* if the wanted position is before the current position
356 * I see no easy way to unroll ... We have to restart at
357 * the beginning. *sigh*
359 if (lzs
->realcurrent
>lzs
->realwanted
) {
360 /* flush decompressor state */
361 _llseek(lzs
->realfd
,14,SEEK_SET
);
366 memset(lzs
->table
,' ',0x1000);
367 lzs
->curtabent
= 0xFF0;
369 while (lzs
->realcurrent
<lzs
->realwanted
) {
381 #undef DECOMPRESS_ONE_BYTE
385 * LZSeek [LZEXPAND.4] [LZ32.3]
387 * works as the usual _llseek
391 LZSeek(HFILE fd
,LONG off
,INT32 type
) {
396 dprintf_file(stddeb
,"LZSeek(%d,%ld,%d)\n",fd
,off
,type
);
397 for (i
=0;i
<nroflzstates
;i
++)
398 if (lzstates
[i
].lzfd
==fd
)
400 /* not compressed? just use normal _llseek() */
402 return _llseek(fd
,off
,type
);
404 newwanted
= lzs
->realwanted
;
406 case 1: /* SEEK_CUR */
409 case 2: /* SEEK_END */
410 newwanted
= lzs
->reallength
-off
;
412 default:/* SEEK_SET */
416 if (newwanted
>lzs
->reallength
)
417 return LZERROR_BADVALUE
;
419 return LZERROR_BADVALUE
;
420 lzs
->realwanted
= newwanted
;
425 * LZCopy [LZEXPAND.1] [LZ32.0]
427 * Copies everything from src to dest
428 * if src is a LZ compressed file, it will be uncompressed.
429 * will return the number of bytes written to dest or errors.
432 LZCopy(HFILE src
,HFILE dest
) {
437 INT32 (*xread
)(HFILE
,LPVOID
,UINT32
);
439 dprintf_file(stddeb
,"LZCopy(%d,%d)\n",src
,dest
);
440 for (i
=0;i
<nroflzstates
;i
++)
441 if (src
==lzstates
[i
].lzfd
)
444 /* not compressed? just copy */
446 xread
=(INT32(*)(HFILE
,LPVOID
,UINT32
))_lread32
;
451 ret
=xread(src
,buf
,BUFLEN
);
460 wret
= _lwrite32(dest
,buf
,ret
);
462 return LZERROR_WRITE
;
468 /* reverses GetExpandedPathname */
469 static LPSTR
LZEXPAND_MangleName( LPCSTR fn
)
472 char *mfn
= (char *)xmalloc( strlen(fn
) + 3 ); /* "._" and \0 */
474 if (!(p
= strrchr( mfn
, '\\' ))) p
= mfn
;
475 if ((p
= strchr( p
, '.' )))
478 if (strlen(p
) < 3) strcat( p
, "_" ); /* append '_' */
479 else p
[strlen(p
)-1] = '_'; /* replace last character */
481 else strcat( mfn
, "._" ); /* append "._" */
486 * LZOpenFile [LZEXPAND.2]
487 * Opens a file. If not compressed, open it as a normal file.
490 LZOpenFile16(LPCSTR fn
,LPOFSTRUCT ofs
,UINT16 mode
) {
493 dprintf_file(stddeb
,"LZOpenFile(%s,%p,%d)\n",fn
,ofs
,mode
);
494 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
495 fd
=OpenFile(fn
,ofs
,mode
);
498 LPSTR mfn
= LZEXPAND_MangleName(fn
);
499 fd
= OpenFile(mfn
,ofs
,mode
);
502 if ((mode
&~0x70)!=OF_READ
)
513 * LZOpenFileA [LZ32.1]
516 LZOpenFile32A(LPCSTR fn
,LPOFSTRUCT ofs
,UINT32 mode
) {
517 return LZOpenFile16(fn
,ofs
,mode
);
521 * LZOpenFileW [LZ32.10]
524 LZOpenFile32W(LPCWSTR fn
,LPOFSTRUCT ofs
,UINT32 mode
) {
529 xfn
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn
);
530 ret
= LZOpenFile16(xfn
,ofs
,mode
);
531 HeapFree( GetProcessHeap(), 0, xfn
);
532 if (ret
!=HFILE_ERROR
) {
533 /* ofs->szPathName is an array with the OFSTRUCT */
534 yfn
= HEAP_strdupAtoW( GetProcessHeap(), 0, ofs
->szPathName
);
535 memcpy(ofs
->szPathName
,yfn
,lstrlen32W(yfn
)*2+2);
536 HeapFree( GetProcessHeap(), 0, yfn
);
542 * LZClose [LZEXPAND.6] [LZ32.5]
548 dprintf_file(stddeb
,"LZClose(%d)\n",fd
);
549 for (i
=0;i
<nroflzstates
;i
++)
550 if (lzstates
[i
].lzfd
==fd
)
552 if (i
==nroflzstates
) {
557 free(lzstates
[i
].get
);
558 _lclose(lzstates
[i
].realfd
);
559 memcpy(lzstates
+i
,lzstates
+i
+1,sizeof(struct lzstate
)*(nroflzstates
-i
-1));
561 lzstates
=xrealloc(lzstates
,sizeof(struct lzstate
)*nroflzstates
);
565 * CopyLZFile [LZEXPAND.8] [LZ32.7]
567 * Copy src to dest (including uncompressing src).
568 * NOTE: Yes. This is exactly the same function as LZCopy.
571 CopyLZFile(HFILE src
,HFILE dest
) {
572 dprintf_file(stddeb
,"CopyLZFile(%d,%d)\n",src
,dest
);
573 return LZCopy(src
,dest
);