2 * LZ Decompression functions
4 * Copyright 1996 Marcus Meissner
7 * FIXME: return values might be wrong
15 #include <sys/types.h>
20 #include "stackframe.h"
26 #define strdupW2A(x) STRING32_DupUniToAnsi(x)
27 #define strdupA2W(x) STRING32_DupAnsiToUni(x)
28 #define strcpyAW(a,b) STRING32_AnsiToUni(a,b)
30 /* The readahead length of the decompressor. Reading single bytes
31 * using _lread() would be SLOW.
35 /* Format of first 14 byte of LZ compressed file */
42 static BYTE LZMagic
[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
44 static struct lzstate
{
45 HFILE lzfd
; /* the handle used by the program */
46 HFILE realfd
; /* the real filedescriptor */
47 CHAR lastchar
; /* the last char of the filename */
49 DWORD reallength
; /* the decompressed length of the file */
50 DWORD realcurrent
; /* the position the decompressor currently is */
51 DWORD realwanted
; /* the position the user wants to read from */
53 BYTE table
[0x1000]; /* the rotating LZ table */
54 UINT curtabent
; /* CURrent TABle ENTry */
56 BYTE stringlen
; /* length and position of current string */
57 DWORD stringpos
; /* from stringtable */
60 WORD bytetype
; /* bitmask within blocks */
62 BYTE
*get
; /* GETLEN bytes */
63 DWORD getcur
; /* current read */
64 DWORD getlen
; /* length last got */
66 static int nroflzstates
=0;
68 /* reads one compressed byte, including buffering */
69 #define GET(lzs,b) _lzget(lzs,&b)
70 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
73 _lzget(struct lzstate
*lzs
,BYTE
*b
) {
74 if (lzs
->getcur
<lzs
->getlen
) {
75 *b
= lzs
->get
[lzs
->getcur
++];
78 int ret
= FILE_Read(lzs
->realfd
,lzs
->get
,GETLEN
);
89 /* internal function, reads lzheader
90 * returns BADINHANDLE for non filedescriptors
91 * return 0 for file not compressed using LZ
92 * return UNKNOWNALG for unknown algorithm
93 * returns lzfileheader in *head
96 read_header(HFILE fd
,struct lzfileheader
*head
) {
99 if (_llseek(fd
,0,SEEK_SET
)==-1)
100 return LZERROR_BADINHANDLE
;
102 /* We can't directly read the lzfileheader struct due to
103 * structure element alignment
105 if (FILE_Read(fd
,buf
,14)<14)
107 memcpy(head
->magic
,buf
,8);
108 memcpy(&(head
->compressiontype
),buf
+8,1);
109 memcpy(&(head
->lastchar
),buf
+9,1);
111 /* FIXME: consider endianess on non-intel architectures */
112 memcpy(&(head
->reallength
),buf
+10,4);
114 if (memcmp(head
->magic
,LZMagic
,8))
116 if (head
->compressiontype
!='A')
117 return LZERROR_UNKNOWNALG
;
121 * LZStart [LZEXPAND.7] [LZ32.6]
125 dprintf_file(stddeb
,"LZStart(void)\n");
130 * LZInit [LZEXPAND.3] [LZ32.2]
132 * initializes internal decompression buffers, returns lzfiledescriptor.
133 * (return value the same as hfSrc, if hfSrc is not compressed)
134 * on failure, returns error code <0
135 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
136 * we use as much as we need, we just OR 0x400 to the passed HFILE.
138 * since _llseek uses the same types as libc.lseek, we just use the macros of
142 LZInit(HFILE hfSrc
) {
143 struct lzfileheader head
;
147 dprintf_file(stddeb
,"LZInit(%d)\n",hfSrc
);
148 ret
=read_header(hfSrc
,&head
);
150 _llseek(hfSrc
,0,SEEK_SET
);
151 return ret
?ret
:hfSrc
;
153 lzstates
=xrealloc(lzstates
,(++nroflzstates
)*sizeof(struct lzstate
));
154 lzs
= lzstates
+(nroflzstates
-1);
156 memset(lzs
,'\0',sizeof(*lzs
));
158 lzs
->lzfd
= hfSrc
| 0x400;
159 lzs
->lastchar
= head
.lastchar
;
160 lzs
->reallength
= head
.reallength
;
162 lzs
->get
= xmalloc(GETLEN
);
166 /* Yes, preinitialize with spaces */
167 memset(lzs
->table
,' ',0x1000);
168 /* Yes, start 16 byte from the END of the table */
169 lzs
->curtabent
= 0xff0;
174 * LZDone [LZEXPAND.9] [LZ32.8]
179 dprintf_file(stddeb
,"LZDone()\n");
183 * GetExpandedName [LZEXPAND.10]
185 * gets the full filename of the compressed file 'in' by opening it
186 * and reading the header
188 * "file." is being translated to "file"
189 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
190 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
194 GetExpandedName16(LPCSTR in
,LPSTR out
) {
195 struct lzfileheader head
;
198 INT fnislowercased
,ret
,len
;
201 dprintf_file(stddeb
,"GetExpandedName(%s)\n",in
);
202 fd
=OpenFile(in
,&ofs
,OF_READ
);
204 return LZERROR_BADINHANDLE
;
205 ret
=read_header(fd
,&head
);
208 return LZERROR_BADINHANDLE
;
211 /* This line will crash if the caller hasn't allocated enough memory
216 /* look for directory prefix and skip it. */
218 while (NULL
!=(t
=strpbrk(s
,"/\\:")))
221 /* now mangle the basename */
223 /* FIXME: hmm. shouldn't happen? */
224 fprintf(stddeb
,__FILE__
":GetExpandedFileName(), specified a directory or what? (%s)\n",in
);
228 /* see if we should use lowercase or uppercase on the last char */
236 fnislowercased
=islower(*t
);
239 if (isalpha(head
.lastchar
)) {
241 head
.lastchar
=tolower(head
.lastchar
);
243 head
.lastchar
=toupper(head
.lastchar
);
246 /* now look where to replace the last character */
247 if (NULL
!=(t
=strchr(s
,'.'))) {
253 t
[len
]=head
.lastchar
;
255 } /* else no modification necessary */
261 * GetExpandedNameW [LZ32.11]
264 GetExpandedName32W(LPCWSTR in
,LPWSTR out
) {
268 xout
= malloc(lstrlen32W(in
)+3);
270 ret
= GetExpandedName16(xin
,xout
);
279 * GetExpandedNameA [LZ32.9]
282 GetExpandedName32A(LPCSTR in
,LPSTR out
) {
283 return GetExpandedName16(in
,out
);
287 * LZRead [LZEXPAND.5] [LZ32.4]
288 * just as normal read, but reads from LZ special fd and uncompresses.
291 LZRead16(HFILE fd
,SEGPTR segbuf
,UINT16 toread
) {
292 dprintf_file(stddeb
,"LZRead16(%d,%08lx,%d)\n",fd
,(DWORD
)segbuf
,toread
);
293 return LZRead32(fd
,(LPBYTE
)PTR_SEG_TO_LIN(segbuf
),toread
);
297 LZRead32(HFILE fd
,LPVOID vbuf
,UINT32 toread
) {
303 dprintf_file(stddeb
,"LZRead32(%d,%p,%d)\n",fd
,buf
,toread
);
305 for (i
=0;i
<nroflzstates
;i
++)
306 if (lzstates
[i
].lzfd
==fd
)
309 return FILE_Read(fd
,buf
,toread
);
312 /* The decompressor itself is in a define, cause we need it twice
313 * in this function. (the decompressed byte will be in b)
315 #define DECOMPRESS_ONE_BYTE \
316 if (lzs->stringlen) { \
317 b = lzs->table[lzs->stringpos]; \
318 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
321 if (!(lzs->bytetype&0x100)) { \
323 return toread-howmuch; \
324 lzs->bytetype = b|0xFF00; \
326 if (lzs->bytetype & 1) { \
328 return toread-howmuch; \
332 if (1!=GET(lzs,b1)) \
333 return toread-howmuch; \
334 if (1!=GET(lzs,b2)) \
335 return toread-howmuch; \
339 * where CAB is the stringoffset in the table\
340 * and D+3 is the len of the string \
342 lzs->stringpos = b1|((b2&0xf0)<<4); \
343 lzs->stringlen = (b2&0xf)+2; \
344 /* 3, but we use a byte already below ... */\
345 b = lzs->table[lzs->stringpos];\
346 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
350 /* store b in table */ \
351 lzs->table[lzs->curtabent++]= b; \
352 lzs->curtabent &= 0xFFF; \
355 /* if someone has seeked, we have to bring the decompressor
358 if (lzs
->realcurrent
!=lzs
->realwanted
) {
359 /* if the wanted position is before the current position
360 * I see no easy way to unroll ... We have to restart at
361 * the beginning. *sigh*
363 if (lzs
->realcurrent
>lzs
->realwanted
) {
364 /* flush decompressor state */
365 _llseek(lzs
->realfd
,14,SEEK_SET
);
370 memset(lzs
->table
,' ',0x1000);
371 lzs
->curtabent
= 0xFF0;
373 while (lzs
->realcurrent
<lzs
->realwanted
) {
385 #undef DECOMPRESS_ONE_BYTE
389 * LZSeek [LZEXPAND.4] [LZ32.3]
391 * works as the usual _llseek
395 LZSeek(HFILE fd
,LONG off
,INT32 type
) {
400 dprintf_file(stddeb
,"LZSeek(%d,%ld,%d)\n",fd
,off
,type
);
401 for (i
=0;i
<nroflzstates
;i
++)
402 if (lzstates
[i
].lzfd
==fd
)
404 /* not compressed? just use normal _llseek() */
406 return _llseek(fd
,off
,type
);
408 newwanted
= lzs
->realwanted
;
410 case 1: /* SEEK_CUR */
413 case 2: /* SEEK_END */
414 newwanted
= lzs
->reallength
-off
;
416 default:/* SEEK_SET */
420 if (newwanted
>lzs
->reallength
)
421 return LZERROR_BADVALUE
;
423 return LZERROR_BADVALUE
;
424 lzs
->realwanted
= newwanted
;
429 * LZCopy [LZEXPAND.1] [LZ32.0]
431 * Copies everything from src to dest
432 * if src is a LZ compressed file, it will be uncompressed.
433 * will return the number of bytes written to dest or errors.
436 LZCopy(HFILE src
,HFILE dest
) {
441 INT32 (*xread
)(HFILE
,LPVOID
,UINT32
);
443 dprintf_file(stddeb
,"LZCopy(%d,%d)\n",src
,dest
);
444 for (i
=0;i
<nroflzstates
;i
++)
445 if (src
==lzstates
[i
].lzfd
)
448 /* not compressed? just copy */
455 ret
=xread(src
,buf
,BUFLEN
);
464 wret
= _lwrite(dest
,buf
,ret
);
466 return LZERROR_WRITE
;
472 /* reverses GetExpandedPathname */
473 static LPSTR
LZEXPAND_MangleName( LPCSTR fn
)
476 char *mfn
= (char *)xmalloc( strlen(fn
) + 3 ); /* "._" and \0 */
478 if (!(p
= strrchr( mfn
, '\\' ))) p
= mfn
;
479 if ((p
= strchr( p
, '.' )))
482 if (strlen(p
) < 3) strcat( p
, "_" ); /* append '_' */
483 else p
[strlen(p
)-1] = '_'; /* replace last character */
485 else strcat( mfn
, "._" ); /* append "._" */
490 * LZOpenFile [LZEXPAND.2]
491 * Opens a file. If not compressed, open it as a normal file.
494 LZOpenFile16(LPCSTR fn
,LPOFSTRUCT ofs
,UINT16 mode
) {
497 dprintf_file(stddeb
,"LZOpenFile(%s,%p,%d)\n",fn
,ofs
,mode
);
498 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
499 fd
=OpenFile(fn
,ofs
,mode
);
502 LPSTR mfn
= LZEXPAND_MangleName(fn
);
503 fd
= OpenFile(mfn
,ofs
,mode
);
506 if ((mode
&~0x70)!=OF_READ
)
517 * LZOpenFileA [LZ32.1]
520 LZOpenFile32A(LPCSTR fn
,LPOFSTRUCT ofs
,UINT32 mode
) {
521 return LZOpenFile16(fn
,ofs
,mode
);
525 * LZOpenFileW [LZ32.10]
528 LZOpenFile32W(LPCWSTR fn
,LPOFSTRUCT ofs
,UINT32 mode
) {
534 ret
= LZOpenFile16(xfn
,ofs
,mode
);
536 if (ret
!=HFILE_ERROR
) {
537 /* ofs->szPathName is an array with the OFSTRUCT */
538 yfn
= strdupA2W(ofs
->szPathName
);
539 memcpy(ofs
->szPathName
,yfn
,lstrlen32W(yfn
)*2+2);
546 * LZClose [LZEXPAND.6] [LZ32.5]
552 dprintf_file(stddeb
,"LZClose(%d)\n",fd
);
553 for (i
=0;i
<nroflzstates
;i
++)
554 if (lzstates
[i
].lzfd
==fd
)
556 if (i
==nroflzstates
) {
561 free(lzstates
[i
].get
);
562 _lclose(lzstates
[i
].realfd
);
563 memcpy(lzstates
+i
,lzstates
+i
+1,sizeof(struct lzstate
)*(nroflzstates
-i
-1));
565 lzstates
=xrealloc(lzstates
,sizeof(struct lzstate
)*nroflzstates
);
569 * CopyLZFile [LZEXPAND.8] [LZ32.7]
571 * Copy src to dest (including uncompressing src).
572 * NOTE: Yes. This is exactly the same function as LZCopy.
575 CopyLZFile(HFILE src
,HFILE dest
) {
576 dprintf_file(stddeb
,"CopyLZFile(%d,%d)\n",src
,dest
);
577 return LZCopy(src
,dest
);