2 * LZ Decompression functions
4 * Copyright 1996 Marcus Meissner
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * The LZ (Lempel Ziv) decompression was used in win16 installation programs.
23 * It is a simple tabledriven decompression engine, the algorithm is not
24 * documented as far as I know. WINE does not contain a compressor for
27 * The implementation is complete and there have been no reports of failures
32 * o Check whether the return values are correct
38 #include <sys/types.h>
43 #define WIN32_NO_STATUS
48 #include "ddk/ntddk.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(file
);
55 /* The readahead length of the decompressor. Reading single bytes
56 * using _lread() would be SLOW.
60 #define LZ_MAGIC_LEN 8
61 #define LZ_HEADER_LEN 14
63 /* Format of first 14 byte of LZ compressed file */
65 BYTE magic
[LZ_MAGIC_LEN
];
70 static const BYTE LZMagic
[LZ_MAGIC_LEN
]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
72 #define LZ_TABLE_SIZE 0x1000
75 HFILE realfd
; /* the real filedescriptor */
76 CHAR lastchar
; /* the last char of the filename */
78 DWORD reallength
; /* the decompressed length of the file */
79 DWORD realcurrent
; /* the position the decompressor currently is */
80 DWORD realwanted
; /* the position the user wants to read from */
82 BYTE table
[LZ_TABLE_SIZE
]; /* the rotating LZ table */
83 UINT curtabent
; /* CURrent TABle ENTry */
85 BYTE stringlen
; /* length and position of current string */
86 DWORD stringpos
; /* from stringtable */
89 WORD bytetype
; /* bitmask within blocks */
91 BYTE
*get
; /* GETLEN bytes */
92 DWORD getcur
; /* current read */
93 DWORD getlen
; /* length last got */
96 #define MAX_LZSTATES 16
97 static struct lzstate
*lzstates
[MAX_LZSTATES
];
99 #define LZ_MIN_HANDLE 0x400
100 #define IS_LZ_HANDLE(h) (((h) >= LZ_MIN_HANDLE) && ((h) < LZ_MIN_HANDLE+MAX_LZSTATES))
101 #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)-LZ_MIN_HANDLE] : NULL)
103 /* reads one compressed byte, including buffering */
104 #define GET(lzs,b) _lzget(lzs,&b)
105 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
108 _lzget(struct lzstate
*lzs
,BYTE
*b
) {
109 if (lzs
->getcur
<lzs
->getlen
) {
110 *b
= lzs
->get
[lzs
->getcur
++];
113 int ret
= _lread(lzs
->realfd
,lzs
->get
,GETLEN
);
114 if (ret
==HFILE_ERROR
)
124 /* internal function, reads lzheader
125 * returns BADINHANDLE for non filedescriptors
126 * return 0 for file not compressed using LZ
127 * return UNKNOWNALG for unknown algorithm
128 * returns lzfileheader in *head
130 static INT
read_header(HFILE fd
,struct lzfileheader
*head
)
132 BYTE buf
[LZ_HEADER_LEN
];
134 if (_llseek(fd
,0,SEEK_SET
)==-1)
135 return LZERROR_BADINHANDLE
;
137 /* We can't directly read the lzfileheader struct due to
138 * structure element alignment
140 if (_lread(fd
,buf
,LZ_HEADER_LEN
) != LZ_HEADER_LEN
)
142 memcpy(head
->magic
,buf
,LZ_MAGIC_LEN
);
143 memcpy(&(head
->compressiontype
),buf
+LZ_MAGIC_LEN
,1);
144 memcpy(&(head
->lastchar
),buf
+LZ_MAGIC_LEN
+1,1);
146 /* FIXME: consider endianness on non-intel architectures */
147 memcpy(&(head
->reallength
),buf
+LZ_MAGIC_LEN
+2,4);
149 if (memcmp(head
->magic
,LZMagic
,LZ_MAGIC_LEN
))
151 if (head
->compressiontype
!='A')
152 return LZERROR_UNKNOWNALG
;
157 /***********************************************************************
158 * LZStart (KERNEL32.@)
160 INT WINAPI
LZStart(void)
167 /***********************************************************************
168 * LZInit (KERNEL32.@)
170 * initializes internal decompression buffers, returns lzfiledescriptor.
171 * (return value the same as hfSrc, if hfSrc is not compressed)
172 * on failure, returns error code <0
173 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
175 * since _llseek uses the same types as libc.lseek, we just use the macros of
178 HFILE WINAPI
LZInit( HFILE hfSrc
)
181 struct lzfileheader head
;
185 TRACE("(%d)\n",hfSrc
);
186 ret
=read_header(hfSrc
,&head
);
188 _llseek(hfSrc
,0,SEEK_SET
);
189 return ret
?ret
:hfSrc
;
191 for (i
= 0; i
< MAX_LZSTATES
; i
++) if (!lzstates
[i
]) break;
192 if (i
== MAX_LZSTATES
) return LZERROR_GLOBALLOC
;
193 lzstates
[i
] = lzs
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*lzs
) );
194 if(lzs
== NULL
) return LZERROR_GLOBALLOC
;
197 lzs
->lastchar
= head
.lastchar
;
198 lzs
->reallength
= head
.reallength
;
200 lzs
->get
= HeapAlloc( GetProcessHeap(), 0, GETLEN
);
204 if(lzs
->get
== NULL
) {
205 HeapFree(GetProcessHeap(), 0, lzs
);
207 return LZERROR_GLOBALLOC
;
210 /* Yes, preinitialize with spaces */
211 memset(lzs
->table
,' ',LZ_TABLE_SIZE
);
212 /* Yes, start 16 byte from the END of the table */
213 lzs
->curtabent
= 0xff0;
214 return LZ_MIN_HANDLE
+ i
;
218 /***********************************************************************
219 * LZDone (KERNEL32.@)
221 void WINAPI
LZDone(void)
227 /***********************************************************************
228 * GetExpandedNameA (KERNEL32.@)
230 * gets the full filename of the compressed file 'in' by opening it
231 * and reading the header
233 * "file." is being translated to "file"
234 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
235 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
238 INT WINAPI
GetExpandedNameA( LPSTR in
, LPSTR out
)
240 struct lzfileheader head
;
243 INT fnislowercased
,ret
,len
;
247 fd
=OpenFile(in
,&ofs
,OF_READ
);
249 return (INT
)(INT16
)LZERROR_BADINHANDLE
;
251 ret
=read_header(fd
,&head
);
253 /* not a LZ compressed file, so the expanded name is the same
254 * as the input name */
260 /* look for directory prefix and skip it. */
262 while (NULL
!=(t
=strpbrk(s
,"/\\:")))
265 /* now mangle the basename */
267 /* FIXME: hmm. shouldn't happen? */
268 WARN("Specified a directory or what? (%s)\n",in
);
272 /* see if we should use lowercase or uppercase on the last char */
280 fnislowercased
=islower(*t
);
283 if (isalpha(head
.lastchar
)) {
285 head
.lastchar
=tolower(head
.lastchar
);
287 head
.lastchar
=RtlUpperChar(head
.lastchar
);
290 /* now look where to replace the last character */
291 if (NULL
!=(t
=strchr(s
,'.'))) {
297 t
[len
]=head
.lastchar
;
299 } /* else no modification necessary */
305 /***********************************************************************
306 * GetExpandedNameW (KERNEL32.@)
308 INT WINAPI
GetExpandedNameW( LPWSTR in
, LPWSTR out
)
311 DWORD len
= WideCharToMultiByte( CP_ACP
, 0, in
, -1, NULL
, 0, NULL
, NULL
);
312 char *xin
= HeapAlloc( GetProcessHeap(), 0, len
);
313 char *xout
= HeapAlloc( GetProcessHeap(), 0, len
+3 );
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
, lstrlenW(in
)+4 );
317 HeapFree( GetProcessHeap(), 0, xin
);
318 HeapFree( GetProcessHeap(), 0, xout
);
323 /***********************************************************************
324 * LZRead (KERNEL32.@)
326 INT WINAPI
LZRead( HFILE fd
, LPSTR vbuf
, INT toread
)
333 TRACE("(%d,%p,%d)\n",fd
,buf
,toread
);
335 if (!(lzs
= GET_LZ_STATE(fd
))) return _lread(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 TRACE("(%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 TRACE("(%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
= _lwrite(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
= HeapAlloc( 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
)
536 BYTE ofs_cBytes
= ofs
->cBytes
;
538 TRACE("(%s,%p,%d)\n",fn
,ofs
,mode
);
539 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
540 fd
=OpenFile(fn
,ofs
,mode
);
543 LPSTR mfn
= LZEXPAND_MangleName(fn
);
544 fd
= OpenFile(mfn
,ofs
,mode
);
545 HeapFree( GetProcessHeap(), 0, mfn
);
548 ofs
->cBytes
= ofs_cBytes
;
549 if ((mode
&~0x70)!=OF_READ
)
554 if ((INT
)cfd
<= 0) return fd
;
559 /***********************************************************************
560 * LZOpenFileW (KERNEL32.@)
562 HFILE WINAPI
LZOpenFileW( LPWSTR fn
, LPOFSTRUCT ofs
, WORD mode
)
565 DWORD len
= WideCharToMultiByte( CP_ACP
, 0, fn
, -1, NULL
, 0, NULL
, NULL
);
566 LPSTR xfn
= HeapAlloc( GetProcessHeap(), 0, len
);
567 WideCharToMultiByte( CP_ACP
, 0, fn
, -1, xfn
, len
, NULL
, NULL
);
568 ret
= LZOpenFileA(xfn
,ofs
,mode
);
569 HeapFree( GetProcessHeap(), 0, xfn
);
574 /***********************************************************************
575 * LZClose (KERNEL32.@)
577 void WINAPI
LZClose( HFILE fd
)
582 if (!(lzs
= GET_LZ_STATE(fd
))) _lclose(fd
);
585 HeapFree( GetProcessHeap(), 0, lzs
->get
);
586 CloseHandle( LongToHandle(lzs
->realfd
) );
587 lzstates
[fd
- LZ_MIN_HANDLE
] = NULL
;
588 HeapFree( GetProcessHeap(), 0, lzs
);