2 * LZ Decompression functions
4 * Copyright 1996 Marcus Meissner
7 * FIXME: return values might be wrong
14 #include "wine/winbase16.h"
15 #include "wine/winestring.h"
21 DEFAULT_DEBUG_CHANNEL(file
)
24 /* The readahead length of the decompressor. Reading single bytes
25 * using _lread() would be SLOW.
29 /* Format of first 14 byte of LZ compressed file */
36 static BYTE LZMagic
[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
39 HFILE realfd
; /* the real filedescriptor */
40 CHAR lastchar
; /* the last char of the filename */
42 DWORD reallength
; /* the decompressed length of the file */
43 DWORD realcurrent
; /* the position the decompressor currently is */
44 DWORD realwanted
; /* the position the user wants to read from */
46 BYTE table
[0x1000]; /* the rotating LZ table */
47 UINT curtabent
; /* CURrent TABle ENTry */
49 BYTE stringlen
; /* length and position of current string */
50 DWORD stringpos
; /* from stringtable */
53 WORD bytetype
; /* bitmask within blocks */
55 BYTE
*get
; /* GETLEN bytes */
56 DWORD getcur
; /* current read */
57 DWORD getlen
; /* length last got */
60 #define MAX_LZSTATES 16
61 static struct lzstate
*lzstates
[MAX_LZSTATES
];
63 #define IS_LZ_HANDLE(h) (((h) >= 0x400) && ((h) < 0x400+MAX_LZSTATES))
64 #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)-0x400] : NULL)
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
= _lread(lzs
->realfd
,lzs
->get
,GETLEN
);
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 INT
read_header(HFILE fd
,struct lzfileheader
*head
)
97 if (_llseek(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 (_lread(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 WINAPI
LZStart16(void)
124 TRACE(file
,"(void)\n");
129 /***********************************************************************
132 INT WINAPI
LZStart(void)
134 TRACE(file
,"(void)\n");
139 /***********************************************************************
140 * LZInit16 (LZEXPAND.3)
142 HFILE16 WINAPI
LZInit16( HFILE16 hfSrc
)
144 HFILE ret
= LZInit( FILE_GetHandle(hfSrc
) );
145 if (IS_LZ_HANDLE(ret
)) return ret
;
146 if ((INT
)ret
<= 0) return ret
;
151 /***********************************************************************
154 * initializes internal decompression buffers, returns lzfiledescriptor.
155 * (return value the same as hfSrc, if hfSrc is not compressed)
156 * on failure, returns error code <0
157 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
159 * since _llseek uses the same types as libc.lseek, we just use the macros of
162 HFILE WINAPI
LZInit( HFILE hfSrc
)
165 struct lzfileheader head
;
170 TRACE(file
,"(%d)\n",hfSrc
);
171 ret
=read_header(hfSrc
,&head
);
173 _llseek(hfSrc
,0,SEEK_SET
);
174 return ret
?ret
:hfSrc
;
176 for (i
= 0; i
< MAX_LZSTATES
; i
++) if (!lzstates
[i
]) break;
177 if (i
== MAX_LZSTATES
) return LZERROR_GLOBALLOC
;
178 lzstates
[i
] = lzs
= HeapAlloc( SystemHeap
, 0, sizeof(struct lzstate
) );
180 memset(lzs
,'\0',sizeof(*lzs
));
182 lzs
->lastchar
= head
.lastchar
;
183 lzs
->reallength
= head
.reallength
;
185 lzs
->get
= HEAP_xalloc( GetProcessHeap(), 0, GETLEN
);
189 /* Yes, preinitialize with spaces */
190 memset(lzs
->table
,' ',0x1000);
191 /* Yes, start 16 byte from the END of the table */
192 lzs
->curtabent
= 0xff0;
197 /***********************************************************************
198 * LZDone (LZEXPAND.9) (LZ32.8)
200 void WINAPI
LZDone(void)
202 TRACE(file
,"(void)\n");
206 /***********************************************************************
207 * GetExpandedName16 (LZEXPAND.10)
209 INT16 WINAPI
GetExpandedName16( LPCSTR in
, LPSTR out
)
211 return (INT16
)GetExpandedNameA( in
, out
);
215 /***********************************************************************
216 * GetExpandedName32A (LZ32.9)
218 * gets the full filename of the compressed file 'in' by opening it
219 * and reading the header
221 * "file." is being translated to "file"
222 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
223 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
226 INT WINAPI
GetExpandedNameA( LPCSTR in
, LPSTR out
)
228 struct lzfileheader head
;
231 INT fnislowercased
,ret
,len
;
234 TRACE(file
,"(%s)\n",in
);
235 fd
=OpenFile(in
,&ofs
,OF_READ
);
237 return (INT
)(INT16
)LZERROR_BADINHANDLE
;
239 ret
=read_header(fd
,&head
);
241 /* not a LZ compressed file, so the expanded name is the same
242 * as the input name */
248 /* look for directory prefix and skip it. */
250 while (NULL
!=(t
=strpbrk(s
,"/\\:")))
253 /* now mangle the basename */
255 /* FIXME: hmm. shouldn't happen? */
256 WARN(file
,"Specified a directory or what? (%s)\n",in
);
260 /* see if we should use lowercase or uppercase on the last char */
268 fnislowercased
=islower(*t
);
271 if (isalpha(head
.lastchar
)) {
273 head
.lastchar
=tolower(head
.lastchar
);
275 head
.lastchar
=toupper(head
.lastchar
);
278 /* now look where to replace the last character */
279 if (NULL
!=(t
=strchr(s
,'.'))) {
285 t
[len
]=head
.lastchar
;
287 } /* else no modification necessary */
293 /***********************************************************************
294 * GetExpandedName32W (LZ32.11)
296 INT WINAPI
GetExpandedNameW( LPCWSTR in
, LPWSTR out
)
301 xout
= HeapAlloc( GetProcessHeap(), 0, lstrlenW(in
)+3 );
302 xin
= HEAP_strdupWtoA( GetProcessHeap(), 0, in
);
303 ret
= GetExpandedName16(xin
,xout
);
304 if (ret
>0) lstrcpyAtoW(out
,xout
);
305 HeapFree( GetProcessHeap(), 0, xin
);
306 HeapFree( GetProcessHeap(), 0, xout
);
311 /***********************************************************************
312 * LZRead16 (LZEXPAND.5)
314 INT16 WINAPI
LZRead16( HFILE16 fd
, LPVOID buf
, UINT16 toread
)
316 if (IS_LZ_HANDLE(fd
)) return LZRead( fd
, buf
, toread
);
317 return _lread16( fd
, buf
, toread
);
321 /***********************************************************************
324 INT WINAPI
LZRead( HFILE fd
, LPVOID vbuf
, UINT toread
)
331 TRACE(file
,"(%d,%p,%d)\n",fd
,buf
,toread
);
333 if (!(lzs
= GET_LZ_STATE(fd
))) return _lread(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 _llseek(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 WINAPI
LZSeek16( HFILE16 fd
, LONG off
, INT16 type
)
417 if (IS_LZ_HANDLE(fd
)) return LZSeek( fd
, off
, type
);
418 return _llseek16( fd
, off
, type
);
422 /***********************************************************************
425 LONG WINAPI
LZSeek( HFILE fd
, LONG off
, INT type
)
430 TRACE(file
,"(%d,%ld,%d)\n",fd
,off
,type
);
431 /* not compressed? just use normal _llseek() */
432 if (!(lzs
= GET_LZ_STATE(fd
))) return _llseek(fd
,off
,type
);
433 newwanted
= lzs
->realwanted
;
435 case 1: /* SEEK_CUR */
438 case 2: /* SEEK_END */
439 newwanted
= lzs
->reallength
-off
;
441 default:/* SEEK_SET */
445 if (newwanted
>lzs
->reallength
)
446 return LZERROR_BADVALUE
;
448 return LZERROR_BADVALUE
;
449 lzs
->realwanted
= newwanted
;
454 /***********************************************************************
455 * LZCopy16 (LZEXPAND.1)
458 LONG WINAPI
LZCopy16( HFILE16 src
, HFILE16 dest
)
460 /* already a LZ handle? */
461 if (IS_LZ_HANDLE(src
)) return LZCopy( src
, FILE_GetHandle(dest
) );
463 /* no, try to open one */
465 if ((INT16
)src
<= 0) return 0;
466 if (IS_LZ_HANDLE(src
))
468 LONG ret
= LZCopy( src
, FILE_GetHandle(dest
) );
472 /* it was not a compressed file */
473 return LZCopy( FILE_GetHandle(src
), FILE_GetHandle(dest
) );
477 /***********************************************************************
480 * Copies everything from src to dest
481 * if src is a LZ compressed file, it will be uncompressed.
482 * will return the number of bytes written to dest or errors.
484 LONG WINAPI
LZCopy( HFILE src
, HFILE dest
)
486 int usedlzinit
=0,ret
,wret
;
491 /* we need that weird typedef, for i can't seem to get function pointer
492 * casts right. (Or they probably just do not like WINAPI in general)
494 typedef UINT
WINAPI (*_readfun
)(HFILE
,LPVOID
,UINT
);
498 TRACE(file
,"(%d,%d)\n",src
,dest
);
499 if (!IS_LZ_HANDLE(src
)) {
501 if ((INT
)src
<= 0) return 0;
502 if (src
!= oldsrc
) usedlzinit
=1;
505 /* not compressed? just copy */
506 if (!IS_LZ_HANDLE(src
))
509 xread
=(_readfun
)LZRead
;
512 ret
=xread(src
,buf
,BUFLEN
);
521 wret
= _lwrite(dest
,buf
,ret
);
523 return LZERROR_WRITE
;
531 /* reverses GetExpandedPathname */
532 static LPSTR
LZEXPAND_MangleName( LPCSTR fn
)
535 char *mfn
= (char *)HEAP_xalloc( GetProcessHeap(), 0,
536 strlen(fn
) + 3 ); /* "._" and \0 */
538 if (!(p
= strrchr( mfn
, '\\' ))) p
= mfn
;
539 if ((p
= strchr( p
, '.' )))
542 if (strlen(p
) < 3) strcat( p
, "_" ); /* append '_' */
543 else p
[strlen(p
)-1] = '_'; /* replace last character */
545 else strcat( mfn
, "._" ); /* append "._" */
550 /***********************************************************************
551 * LZOpenFile16 (LZEXPAND.2)
553 HFILE16 WINAPI
LZOpenFile16( LPCSTR fn
, LPOFSTRUCT ofs
, UINT16 mode
)
555 HFILE hfret
= LZOpenFileA( fn
, ofs
, mode
);
556 /* return errors and LZ handles unmodified */
557 if ((INT
)hfret
<= 0) return hfret
;
558 if (IS_LZ_HANDLE(hfret
)) return hfret
;
559 /* but allocate a dos handle for 'normal' files */
560 return FILE_AllocDosHandle(hfret
);
564 /***********************************************************************
565 * LZOpenFile32A (LZ32.1)
567 * Opens a file. If not compressed, open it as a normal file.
569 HFILE WINAPI
LZOpenFileA( LPCSTR fn
, LPOFSTRUCT ofs
, UINT mode
)
573 TRACE(file
,"(%s,%p,%d)\n",fn
,ofs
,mode
);
574 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
575 fd
=OpenFile(fn
,ofs
,mode
);
578 LPSTR mfn
= LZEXPAND_MangleName(fn
);
579 fd
= OpenFile(mfn
,ofs
,mode
);
580 HeapFree( GetProcessHeap(), 0, mfn
);
582 if ((mode
&~0x70)!=OF_READ
)
587 if ((INT
)cfd
<= 0) return fd
;
592 /***********************************************************************
593 * LZOpenFile32W (LZ32.10)
595 HFILE WINAPI
LZOpenFileW( LPCWSTR fn
, LPOFSTRUCT ofs
, UINT mode
)
601 xfn
= HEAP_strdupWtoA( GetProcessHeap(), 0, fn
);
602 ret
= LZOpenFile16(xfn
,ofs
,mode
);
603 HeapFree( GetProcessHeap(), 0, xfn
);
604 if (ret
!=HFILE_ERROR
) {
605 /* ofs->szPathName is an array with the OFSTRUCT */
606 yfn
= HEAP_strdupAtoW( GetProcessHeap(), 0, ofs
->szPathName
);
607 memcpy(ofs
->szPathName
,yfn
,lstrlenW(yfn
)*2+2);
608 HeapFree( GetProcessHeap(), 0, yfn
);
614 /***********************************************************************
615 * LZClose16 (LZEXPAND.6)
617 void WINAPI
LZClose16( HFILE16 fd
)
619 if (IS_LZ_HANDLE(fd
)) LZClose( fd
);
620 else _lclose16( fd
);
624 /***********************************************************************
627 void WINAPI
LZClose( HFILE fd
)
631 TRACE(file
,"(%d)\n",fd
);
632 if (!(lzs
= GET_LZ_STATE(fd
))) _lclose(fd
);
635 if (lzs
->get
) HeapFree( GetProcessHeap(), 0, lzs
->get
);
636 CloseHandle(lzs
->realfd
);
637 lzstates
[fd
- 0x400] = NULL
;
638 HeapFree( SystemHeap
, 0, lzs
);
642 /***********************************************************************
643 * CopyLZFile16 (LZEXPAND.8)
645 LONG WINAPI
CopyLZFile16( HFILE16 src
, HFILE16 dest
)
647 TRACE(file
,"(%d,%d)\n",src
,dest
);
648 return LZCopy16(src
,dest
);
652 /***********************************************************************
653 * CopyLZFile32 (LZ32.7)
655 * Copy src to dest (including uncompressing src).
656 * NOTE: Yes. This is exactly the same function as LZCopy.
658 LONG WINAPI
CopyLZFile( HFILE src
, HFILE dest
)
660 TRACE(file
,"(%d,%d)\n",src
,dest
);
661 return LZCopy(src
,dest
);