3 * LZ Decompression functions
5 * Copyright 1996 Marcus Meissner
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * The LZ (Lempel Ziv) decompression was used in win16 installation programs.
24 * It is a simple tabledriven decompression engine, the algorithm is not
25 * documented as far as I know. WINE does not contain a compressor for
28 * The implementation is complete and there have been no reports of failures
33 * o Check whether the return values are correct
44 #define HFILE_ERROR ((HFILE)-1)
46 /* The readahead length of the decompressor. Reading single bytes
47 * using _hread() would be SLOW.
51 #define LZ_MAGIC_LEN 8
52 #define LZ_HEADER_LEN 14
54 /* Format of first 14 byte of LZ compressed file */
56 BYTE magic
[LZ_MAGIC_LEN
];
61 static const BYTE LZMagic
[LZ_MAGIC_LEN
]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
63 #define LZ_TABLE_SIZE 0x1000
66 HFILE realfd
; /* the real filedescriptor */
67 CHAR lastchar
; /* the last char of the filename */
69 DWORD reallength
; /* the decompressed length of the file */
70 DWORD realcurrent
; /* the position the decompressor currently is */
71 DWORD realwanted
; /* the position the user wants to read from */
73 BYTE table
[LZ_TABLE_SIZE
]; /* the rotating LZ table */
74 UINT curtabent
; /* CURrent TABle ENTry */
76 BYTE stringlen
; /* length and position of current string */
77 DWORD stringpos
; /* from stringtable */
80 WORD bytetype
; /* bitmask within blocks */
82 BYTE
*get
; /* GETLEN bytes */
83 DWORD getcur
; /* current read */
84 DWORD getlen
; /* length last got */
87 #define MAX_LZSTATES 16
88 static struct lzstate
*lzstates
[MAX_LZSTATES
];
90 #define LZ_MIN_HANDLE 0x400
91 #define IS_LZ_HANDLE(h) (((h) >= LZ_MIN_HANDLE) && ((h) < LZ_MIN_HANDLE+MAX_LZSTATES))
92 #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)-LZ_MIN_HANDLE] : NULL)
94 /* reads one compressed byte, including buffering */
95 #define GET(lzs,b) _lzget(lzs,&b)
96 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
99 _lzget(struct lzstate
*lzs
,BYTE
*b
) {
100 if (lzs
->getcur
<lzs
->getlen
) {
101 *b
= lzs
->get
[lzs
->getcur
++];
104 int ret
= _hread(lzs
->realfd
,lzs
->get
,GETLEN
);
105 if (ret
==HFILE_ERROR
)
115 /* internal function, reads lzheader
116 * returns BADINHANDLE for non filedescriptors
117 * return 0 for file not compressed using LZ
118 * return UNKNOWNALG for unknown algorithm
119 * returns lzfileheader in *head
121 static INT
read_header(HFILE fd
,struct lzfileheader
*head
)
123 BYTE buf
[LZ_HEADER_LEN
];
125 if (_llseek(fd
,0,SEEK_SET
)==-1)
126 return LZERROR_BADINHANDLE
;
128 /* We can't directly read the lzfileheader struct due to
129 * structure element alignment
131 if (_hread(fd
,buf
,LZ_HEADER_LEN
)<LZ_HEADER_LEN
)
133 memcpy(head
->magic
,buf
,LZ_MAGIC_LEN
);
134 memcpy(&(head
->compressiontype
),buf
+LZ_MAGIC_LEN
,1);
135 memcpy(&(head
->lastchar
),buf
+LZ_MAGIC_LEN
+1,1);
137 /* FIXME: consider endianess on non-intel architectures */
138 memcpy(&(head
->reallength
),buf
+LZ_MAGIC_LEN
+2,4);
140 if (memcmp(head
->magic
,LZMagic
,LZ_MAGIC_LEN
))
142 if (head
->compressiontype
!='A')
143 return LZERROR_UNKNOWNALG
;
148 /***********************************************************************
149 * LZStart (KERNEL32.@)
151 INT WINAPI
LZStart(void)
158 /***********************************************************************
159 * LZInit (KERNEL32.@)
161 * initializes internal decompression buffers, returns lzfiledescriptor.
162 * (return value the same as hfSrc, if hfSrc is not compressed)
163 * on failure, returns error code <0
164 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
166 * since _llseek uses the same types as libc.lseek, we just use the macros of
169 HFILE WINAPI
LZInit( HFILE hfSrc
)
172 struct lzfileheader head
;
176 DPRINT("(%d)\n",hfSrc
);
177 ret
=read_header(hfSrc
,&head
);
179 _llseek(hfSrc
,0,SEEK_SET
);
180 return ret
?ret
:hfSrc
;
182 for (i
= 0; i
< MAX_LZSTATES
; i
++) if (!lzstates
[i
]) break;
183 if (i
== MAX_LZSTATES
) return LZERROR_GLOBALLOC
;
184 lzstates
[i
] = lzs
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct lzstate
) );
185 if(lzs
== NULL
) return LZERROR_GLOBALLOC
;
188 lzs
->lastchar
= head
.lastchar
;
189 lzs
->reallength
= head
.reallength
;
191 lzs
->get
= RtlAllocateHeap( GetProcessHeap(), 0, GETLEN
);
195 if(lzs
->get
== NULL
) {
196 RtlFreeHeap(GetProcessHeap(), 0, lzs
);
198 return LZERROR_GLOBALLOC
;
201 /* Yes, preinitialize with spaces */
202 memset(lzs
->table
,' ',LZ_TABLE_SIZE
);
203 /* Yes, start 16 byte from the END of the table */
204 lzs
->curtabent
= 0xff0;
205 return LZ_MIN_HANDLE
+ i
;
209 /***********************************************************************
210 * LZDone (KERNEL32.@)
212 void WINAPI
LZDone(void)
218 /***********************************************************************
219 * GetExpandedNameA (KERNEL32.@)
221 * gets the full filename of the compressed file 'in' by opening it
222 * and reading the header
224 * "file." is being translated to "file"
225 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
226 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
229 INT WINAPI
GetExpandedNameA( LPSTR in
, LPSTR out
)
231 struct lzfileheader head
;
234 INT fnislowercased
,ret
,len
;
238 fd
=OpenFile(in
,&ofs
,OF_READ
);
240 return (INT
)(INT16
)LZERROR_BADINHANDLE
;
242 ret
=read_header(fd
,&head
);
244 /* not a LZ compressed file, so the expanded name is the same
245 * as the input name */
251 /* look for directory prefix and skip it. */
253 while (NULL
!=(t
=strpbrk(s
,"/\\:")))
256 /* now mangle the basename */
258 /* FIXME: hmm. shouldn't happen? */
259 DPRINT("Specified a directory or what? (%s)\n",in
);
263 /* see if we should use lowercase or uppercase on the last char */
271 fnislowercased
=islower(*t
);
274 if (isalpha(head
.lastchar
)) {
276 head
.lastchar
=tolower(head
.lastchar
);
278 head
.lastchar
=toupper(head
.lastchar
);
281 /* now look where to replace the last character */
282 if (NULL
!=(t
=strchr(s
,'.'))) {
288 t
[len
]=head
.lastchar
;
290 } /* else no modification necessary */
296 /***********************************************************************
297 * GetExpandedNameW (KERNEL32.@)
299 INT WINAPI
GetExpandedNameW( LPWSTR in
, LPWSTR out
)
304 len
= WideCharToMultiByte( CP_ACP
, 0, in
, -1, NULL
, 0, NULL
, NULL
);
305 xin
= RtlAllocateHeap( RtlGetProcessHeap(), 0, len
);
307 return LZERROR_BADVALUE
;
308 xout
= RtlAllocateHeap( RtlGetProcessHeap(), 0, len
+3 );
311 RtlFreeHeap( RtlGetProcessHeap(), 0, xin
);
312 return LZERROR_BADVALUE
;
314 WideCharToMultiByte( CP_ACP
, 0, in
, -1, xin
, len
, NULL
, NULL
);
315 if ((ret
= GetExpandedNameA( xin
, xout
)) > 0)
316 MultiByteToWideChar( CP_ACP
, 0, xout
, -1, out
, wcslen(in
)+4 );
317 RtlFreeHeap( RtlGetProcessHeap(), 0, xin
);
318 RtlFreeHeap( RtlGetProcessHeap(), 0, xout
);
323 /***********************************************************************
324 * LZRead (KERNEL32.@)
326 INT WINAPI
LZRead( HFILE fd
, LPSTR vbuf
, INT toread
)
333 DPRINT("(%d,%p,%d)\n",fd
,buf
,toread
);
335 if (!(lzs
= GET_LZ_STATE(fd
))) return _hread(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 _llseek(lzs
->realfd
,LZ_HEADER_LEN
,SEEK_SET
);
395 memset(lzs
->table
,' ',LZ_TABLE_SIZE
);
396 lzs
->curtabent
= 0xFF0;
398 while (lzs
->realcurrent
<lzs
->realwanted
) {
410 #undef DECOMPRESS_ONE_BYTE
414 /***********************************************************************
415 * LZSeek (KERNEL32.@)
417 LONG WINAPI
LZSeek( HFILE fd
, LONG off
, INT type
)
422 DPRINT("(%d,%ld,%d)\n",fd
,off
,type
);
423 /* not compressed? just use normal _llseek() */
424 if (!(lzs
= GET_LZ_STATE(fd
))) return _llseek(fd
,off
,type
);
425 newwanted
= lzs
->realwanted
;
427 case 1: /* SEEK_CUR */
430 case 2: /* SEEK_END */
431 newwanted
= lzs
->reallength
-off
;
433 default:/* SEEK_SET */
437 if (newwanted
>lzs
->reallength
)
438 return LZERROR_BADVALUE
;
440 return LZERROR_BADVALUE
;
441 lzs
->realwanted
= newwanted
;
446 /***********************************************************************
447 * LZCopy (KERNEL32.@)
449 * Copies everything from src to dest
450 * if src is a LZ compressed file, it will be uncompressed.
451 * will return the number of bytes written to dest or errors.
453 LONG WINAPI
LZCopy( HFILE src
, HFILE dest
)
455 int usedlzinit
= 0, ret
, wret
;
457 HFILE oldsrc
= src
, srcfd
;
462 /* we need that weird typedef, for i can't seem to get function pointer
463 * casts right. (Or they probably just do not like WINAPI in general)
465 typedef UINT (WINAPI
*_readfun
)(HFILE
,LPVOID
,UINT
);
469 DPRINT("(%d,%d)\n",src
,dest
);
470 if (!IS_LZ_HANDLE(src
)) {
472 if ((INT
)src
<= 0) return 0;
473 if (src
!= oldsrc
) usedlzinit
=1;
476 /* not compressed? just copy */
477 if (!IS_LZ_HANDLE(src
))
480 xread
=(_readfun
)LZRead
;
483 ret
=xread(src
,buf
,BUFLEN
);
492 wret
= _hwrite(dest
,buf
,ret
);
494 return LZERROR_WRITE
;
497 /* Maintain the timestamp of source file to destination file */
498 srcfd
= (!(lzs
= GET_LZ_STATE(src
))) ? src
: lzs
->realfd
;
499 GetFileTime( LongToHandle(srcfd
), NULL
, NULL
, &filetime
);
500 SetFileTime( LongToHandle(dest
), NULL
, NULL
, &filetime
);
509 /* reverses GetExpandedPathname */
510 static LPSTR
LZEXPAND_MangleName( LPCSTR fn
)
513 char *mfn
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(fn
) + 3 ); /* "._" and \0 */
514 if(mfn
== NULL
) return NULL
;
516 if (!(p
= strrchr( mfn
, '\\' ))) p
= mfn
;
517 if ((p
= strchr( p
, '.' )))
520 if (strlen(p
) < 3) strcat( p
, "_" ); /* append '_' */
521 else p
[strlen(p
)-1] = '_'; /* replace last character */
523 else strcat( mfn
, "._" ); /* append "._" */
528 /***********************************************************************
529 * LZOpenFileA (KERNEL32.@)
531 * Opens a file. If not compressed, open it as a normal file.
533 HFILE WINAPI
LZOpenFileA( LPSTR fn
, LPOFSTRUCT ofs
, WORD mode
)
537 DPRINT("(%s,%p,%d)\n",fn
,ofs
,mode
);
538 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
539 fd
=OpenFile(fn
,ofs
,mode
);
542 LPSTR mfn
= LZEXPAND_MangleName(fn
);
543 fd
= OpenFile(mfn
,ofs
,mode
);
544 RtlFreeHeap( GetProcessHeap(), 0, mfn
);
546 if ((mode
&~0x70)!=OF_READ
)
551 if ((INT
)cfd
<= 0) return fd
;
556 /***********************************************************************
557 * LZOpenFileW (KERNEL32.@)
559 HFILE WINAPI
LZOpenFileW( LPWSTR fn
, LPOFSTRUCT ofs
, WORD mode
)
562 DWORD len
= WideCharToMultiByte( CP_ACP
, 0, fn
, -1, NULL
, 0, NULL
, NULL
);
563 LPSTR xfn
= RtlAllocateHeap( GetProcessHeap(), 0, len
);
564 WideCharToMultiByte( CP_ACP
, 0, fn
, -1, xfn
, len
, NULL
, NULL
);
565 ret
= LZOpenFileA(xfn
,ofs
,mode
);
566 RtlFreeHeap( GetProcessHeap(), 0, xfn
);
571 /***********************************************************************
572 * LZClose (KERNEL32.@)
574 void WINAPI
LZClose( HFILE fd
)
579 if (!(lzs
= GET_LZ_STATE(fd
))) _lclose(fd
);
582 if (lzs
->get
) RtlFreeHeap( GetProcessHeap(), 0, lzs
->get
);
583 CloseHandle( LongToHandle(lzs
->realfd
) );
584 lzstates
[fd
- LZ_MIN_HANDLE
] = NULL
;
585 RtlFreeHeap( GetProcessHeap(), 0, lzs
);