1 /* $Id: lzexpand.c 52854 2011-07-24 23:42:09Z ion $
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
37 #define HFILE_ERROR ((HFILE)-1)
42 #include <sys/types.h>
53 #include "wine/unicode.h"
54 #include "wine/debug.h"
56 #define HeapAlloc RtlAllocateHeap
57 #define HeapReAlloc RtlReAllocateHeap
58 #define HeapFree RtlFreeHeap
59 #define _lread(a, b, c) (long)(_hread(a, b, (long)c))
60 #define _lwrite(a, b, c) (long)(_hwrite(a, b, (long)c))
61 WINE_DEFAULT_DEBUG_CHANNEL(file
);
63 /* The readahead length of the decompressor. Reading single bytes
64 * using _lread() would be SLOW.
68 #define LZ_MAGIC_LEN 8
69 #define LZ_HEADER_LEN 14
71 /* Format of first 14 byte of LZ compressed file */
73 BYTE magic
[LZ_MAGIC_LEN
];
78 static const BYTE LZMagic
[LZ_MAGIC_LEN
]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
80 #define LZ_TABLE_SIZE 0x1000
83 HFILE realfd
; /* the real filedescriptor */
84 CHAR lastchar
; /* the last char of the filename */
86 DWORD reallength
; /* the decompressed length of the file */
87 DWORD realcurrent
; /* the position the decompressor currently is */
88 DWORD realwanted
; /* the position the user wants to read from */
90 BYTE table
[LZ_TABLE_SIZE
]; /* the rotating LZ table */
91 UINT curtabent
; /* CURrent TABle ENTry */
93 BYTE stringlen
; /* length and position of current string */
94 DWORD stringpos
; /* from stringtable */
97 WORD bytetype
; /* bitmask within blocks */
99 BYTE
*get
; /* GETLEN bytes */
100 DWORD getcur
; /* current read */
101 DWORD getlen
; /* length last got */
104 #define MAX_LZSTATES 16
105 static struct lzstate
*lzstates
[MAX_LZSTATES
];
107 #define LZ_MIN_HANDLE 0x400
108 #define IS_LZ_HANDLE(h) (((h) >= LZ_MIN_HANDLE) && ((h) < LZ_MIN_HANDLE+MAX_LZSTATES))
109 #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)-LZ_MIN_HANDLE] : NULL)
111 /* reads one compressed byte, including buffering */
112 #define GET(lzs,b) _lzget(lzs,&b)
113 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
116 _lzget(struct lzstate
*lzs
,BYTE
*b
) {
117 if (lzs
->getcur
<lzs
->getlen
) {
118 *b
= lzs
->get
[lzs
->getcur
++];
121 int ret
= _lread(lzs
->realfd
,lzs
->get
,GETLEN
);
122 if (ret
==HFILE_ERROR
)
132 /* internal function, reads lzheader
133 * returns BADINHANDLE for non filedescriptors
134 * return 0 for file not compressed using LZ
135 * return UNKNOWNALG for unknown algorithm
136 * returns lzfileheader in *head
138 static INT
read_header(HFILE fd
,struct lzfileheader
*head
)
140 BYTE buf
[LZ_HEADER_LEN
];
142 if (_llseek(fd
,0,SEEK_SET
)==-1)
143 return LZERROR_BADINHANDLE
;
145 /* We can't directly read the lzfileheader struct due to
146 * structure element alignment
148 if (_lread(fd
,buf
,LZ_HEADER_LEN
)<LZ_HEADER_LEN
)
150 memcpy(head
->magic
,buf
,LZ_MAGIC_LEN
);
151 memcpy(&(head
->compressiontype
),buf
+LZ_MAGIC_LEN
,1);
152 memcpy(&(head
->lastchar
),buf
+LZ_MAGIC_LEN
+1,1);
154 /* FIXME: consider endianess on non-intel architectures */
155 memcpy(&(head
->reallength
),buf
+LZ_MAGIC_LEN
+2,4);
157 if (memcmp(head
->magic
,LZMagic
,LZ_MAGIC_LEN
))
159 if (head
->compressiontype
!='A')
160 return LZERROR_UNKNOWNALG
;
165 /***********************************************************************
166 * LZStart (KERNEL32.@)
168 INT WINAPI
LZStart(void)
175 /***********************************************************************
176 * LZInit (KERNEL32.@)
178 * initializes internal decompression buffers, returns lzfiledescriptor.
179 * (return value the same as hfSrc, if hfSrc is not compressed)
180 * on failure, returns error code <0
181 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
183 * since _llseek uses the same types as libc.lseek, we just use the macros of
186 HFILE WINAPI
LZInit( HFILE hfSrc
)
189 struct lzfileheader head
;
193 TRACE("(%d)\n",hfSrc
);
194 ret
=read_header(hfSrc
,&head
);
196 _llseek(hfSrc
,0,SEEK_SET
);
197 return ret
?ret
:hfSrc
;
199 for (i
= 0; i
< MAX_LZSTATES
; i
++) if (!lzstates
[i
]) break;
200 if (i
== MAX_LZSTATES
) return LZERROR_GLOBALLOC
;
201 lzstates
[i
] = lzs
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*lzs
) );
202 if(lzs
== NULL
) return LZERROR_GLOBALLOC
;
205 lzs
->lastchar
= head
.lastchar
;
206 lzs
->reallength
= head
.reallength
;
208 lzs
->get
= HeapAlloc( GetProcessHeap(), 0, GETLEN
);
212 if(lzs
->get
== NULL
) {
213 HeapFree(GetProcessHeap(), 0, lzs
);
215 return LZERROR_GLOBALLOC
;
218 /* Yes, preinitialize with spaces */
219 memset(lzs
->table
,' ',LZ_TABLE_SIZE
);
220 /* Yes, start 16 byte from the END of the table */
221 lzs
->curtabent
= 0xff0;
222 return LZ_MIN_HANDLE
+ i
;
226 /***********************************************************************
227 * LZDone (KERNEL32.@)
229 void WINAPI
LZDone(void)
235 /***********************************************************************
236 * GetExpandedNameA (KERNEL32.@)
238 * gets the full filename of the compressed file 'in' by opening it
239 * and reading the header
241 * "file." is being translated to "file"
242 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
243 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
246 INT WINAPI
GetExpandedNameA( LPSTR in
, LPSTR out
)
248 struct lzfileheader head
;
251 INT fnislowercased
,ret
,len
;
255 fd
=OpenFile(in
,&ofs
,OF_READ
);
257 return (INT
)(INT16
)LZERROR_BADINHANDLE
;
259 ret
=read_header(fd
,&head
);
261 /* not a LZ compressed file, so the expanded name is the same
262 * as the input name */
268 /* look for directory prefix and skip it. */
270 while (NULL
!=(t
=strpbrk(s
,"/\\:")))
273 /* now mangle the basename */
275 /* FIXME: hmm. shouldn't happen? */
276 WARN("Specified a directory or what? (%s)\n",in
);
280 /* see if we should use lowercase or uppercase on the last char */
288 fnislowercased
=islower(*t
);
291 if (isalpha(head
.lastchar
)) {
293 head
.lastchar
=tolower(head
.lastchar
);
295 head
.lastchar
=toupper(head
.lastchar
);
298 /* now look where to replace the last character */
299 if (NULL
!=(t
=strchr(s
,'.'))) {
305 t
[len
]=head
.lastchar
;
307 } /* else no modification necessary */
313 /***********************************************************************
314 * GetExpandedNameW (KERNEL32.@)
316 INT WINAPI
GetExpandedNameW( LPWSTR in
, LPWSTR out
)
319 DWORD len
= WideCharToMultiByte( CP_ACP
, 0, in
, -1, NULL
, 0, NULL
, NULL
);
320 char *xin
= HeapAlloc( GetProcessHeap(), 0, len
);
321 char *xout
= HeapAlloc( GetProcessHeap(), 0, len
+3 );
322 WideCharToMultiByte( CP_ACP
, 0, in
, -1, xin
, len
, NULL
, NULL
);
323 if ((ret
= GetExpandedNameA( xin
, xout
)) > 0)
324 MultiByteToWideChar( CP_ACP
, 0, xout
, -1, out
, strlenW(in
)+4 );
325 HeapFree( GetProcessHeap(), 0, xin
);
326 HeapFree( GetProcessHeap(), 0, xout
);
331 /***********************************************************************
332 * LZRead (KERNEL32.@)
334 INT WINAPI
LZRead( HFILE fd
, LPSTR vbuf
, INT toread
)
341 TRACE("(%d,%p,%d)\n",fd
,buf
,toread
);
343 if (!(lzs
= GET_LZ_STATE(fd
))) return _lread(fd
,buf
,toread
);
345 /* The decompressor itself is in a define, cause we need it twice
346 * in this function. (the decompressed byte will be in b)
348 #define DECOMPRESS_ONE_BYTE \
349 if (lzs->stringlen) { \
350 b = lzs->table[lzs->stringpos]; \
351 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
354 if (!(lzs->bytetype&0x100)) { \
356 return toread-howmuch; \
357 lzs->bytetype = b|0xFF00; \
359 if (lzs->bytetype & 1) { \
361 return toread-howmuch; \
365 if (1!=GET(lzs,b1)) \
366 return toread-howmuch; \
367 if (1!=GET(lzs,b2)) \
368 return toread-howmuch; \
372 * where CAB is the stringoffset in the table\
373 * and D+3 is the len of the string \
375 lzs->stringpos = b1|((b2&0xf0)<<4); \
376 lzs->stringlen = (b2&0xf)+2; \
377 /* 3, but we use a byte already below ... */\
378 b = lzs->table[lzs->stringpos];\
379 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
383 /* store b in table */ \
384 lzs->table[lzs->curtabent++]= b; \
385 lzs->curtabent &= 0xFFF; \
388 /* if someone has seeked, we have to bring the decompressor
391 if (lzs
->realcurrent
!=lzs
->realwanted
) {
392 /* if the wanted position is before the current position
393 * I see no easy way to unroll ... We have to restart at
394 * the beginning. *sigh*
396 if (lzs
->realcurrent
>lzs
->realwanted
) {
397 /* flush decompressor state */
398 _llseek(lzs
->realfd
,LZ_HEADER_LEN
,SEEK_SET
);
403 memset(lzs
->table
,' ',LZ_TABLE_SIZE
);
404 lzs
->curtabent
= 0xFF0;
406 while (lzs
->realcurrent
<lzs
->realwanted
) {
418 #undef DECOMPRESS_ONE_BYTE
422 /***********************************************************************
423 * LZSeek (KERNEL32.@)
425 LONG WINAPI
LZSeek( HFILE fd
, LONG off
, INT type
)
430 TRACE("(%d,%d,%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 * LZCopy (KERNEL32.@)
457 * Copies everything from src to dest
458 * if src is a LZ compressed file, it will be uncompressed.
459 * will return the number of bytes written to dest or errors.
461 LONG WINAPI
LZCopy( HFILE src
, HFILE dest
)
463 int usedlzinit
= 0, ret
, wret
;
465 HFILE oldsrc
= src
, srcfd
;
470 /* we need that weird typedef, for i can't seem to get function pointer
471 * casts right. (Or they probably just do not like WINAPI in general)
473 typedef UINT (WINAPI
*_readfun
)(HFILE
,LPVOID
,UINT
);
477 TRACE("(%d,%d)\n",src
,dest
);
478 if (!IS_LZ_HANDLE(src
)) {
480 if ((INT
)src
<= 0) return 0;
481 if (src
!= oldsrc
) usedlzinit
=1;
484 /* not compressed? just copy */
485 if (!IS_LZ_HANDLE(src
))
486 xread
=(_readfun
)_hread
; // ROSHACK
488 xread
=(_readfun
)LZRead
;
491 ret
=xread(src
,buf
,BUFLEN
);
500 wret
= _lwrite(dest
,buf
,ret
);
502 return LZERROR_WRITE
;
505 /* Maintain the timestamp of source file to destination file */
506 srcfd
= (!(lzs
= GET_LZ_STATE(src
))) ? src
: lzs
->realfd
;
507 GetFileTime( LongToHandle(srcfd
), NULL
, NULL
, &filetime
);
508 SetFileTime( LongToHandle(dest
), NULL
, NULL
, &filetime
);
517 /* reverses GetExpandedPathname */
518 static LPSTR
LZEXPAND_MangleName( LPCSTR fn
)
521 char *mfn
= HeapAlloc( GetProcessHeap(), 0, strlen(fn
) + 3 ); /* "._" and \0 */
522 if(mfn
== NULL
) return NULL
;
524 if (!(p
= strrchr( mfn
, '\\' ))) p
= mfn
;
525 if ((p
= strchr( p
, '.' )))
528 if (strlen(p
) < 3) strcat( p
, "_" ); /* append '_' */
529 else p
[strlen(p
)-1] = '_'; /* replace last character */
531 else strcat( mfn
, "._" ); /* append "._" */
536 /***********************************************************************
537 * LZOpenFileA (KERNEL32.@)
539 * Opens a file. If not compressed, open it as a normal file.
541 HFILE WINAPI
LZOpenFileA( LPSTR fn
, LPOFSTRUCT ofs
, WORD mode
)
545 TRACE("(%s,%p,%d)\n",fn
,ofs
,mode
);
546 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
547 fd
=OpenFile(fn
,ofs
,mode
);
550 LPSTR mfn
= LZEXPAND_MangleName(fn
);
551 fd
= OpenFile(mfn
,ofs
,mode
);
552 HeapFree( GetProcessHeap(), 0, mfn
);
554 if ((mode
&~0x70)!=OF_READ
)
559 if ((INT
)cfd
<= 0) return fd
;
564 /***********************************************************************
565 * LZOpenFileW (KERNEL32.@)
567 HFILE WINAPI
LZOpenFileW( LPWSTR fn
, LPOFSTRUCT ofs
, WORD mode
)
570 DWORD len
= WideCharToMultiByte( CP_ACP
, 0, fn
, -1, NULL
, 0, NULL
, NULL
);
571 LPSTR xfn
= HeapAlloc( GetProcessHeap(), 0, len
);
572 WideCharToMultiByte( CP_ACP
, 0, fn
, -1, xfn
, len
, NULL
, NULL
);
573 ret
= LZOpenFileA(xfn
,ofs
,mode
);
574 HeapFree( GetProcessHeap(), 0, xfn
);
579 /***********************************************************************
580 * LZClose (KERNEL32.@)
582 void WINAPI
LZClose( HFILE fd
)
587 if (!(lzs
= GET_LZ_STATE(fd
))) _lclose(fd
);
590 HeapFree( GetProcessHeap(), 0, lzs
->get
);
591 CloseHandle( LongToHandle(lzs
->realfd
) );
592 lzstates
[fd
- LZ_MIN_HANDLE
] = NULL
;
593 HeapFree( GetProcessHeap(), 0, lzs
);
602 LZCloseFile(IN HFILE FileHandle
)
604 /* One function uses _lclose, the other CloseHandle -- same thing */
613 LZCreateFileW(IN LPCWSTR FileName
,
614 IN DWORD dwDesiredAccess
,
615 IN DWORD dwShareMode
,
616 IN DWORD dwCreationDisposition
,
619 WARN(" LZCreateFileW Not implemented!\n");
620 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
621 return ERROR_CALL_NOT_IMPLEMENTED
;