8a05d4f240b9f45bb4f24f06c0127891bbdfa297
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * FIXME: return values might be wrong
29 #include "../include/debug.h"
32 #define HFILE_ERROR ((HFILE)-1)
34 /* The readahead length of the decompressor. Reading single bytes
35 * using _hread() would be SLOW.
39 /* Format of first 14 byte of LZ compressed file */
46 static BYTE LZMagic
[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
49 HFILE realfd
; /* the real filedescriptor */
50 CHAR lastchar
; /* the last char of the filename */
52 DWORD reallength
; /* the decompressed length of the file */
53 DWORD realcurrent
; /* the position the decompressor currently is */
54 DWORD realwanted
; /* the position the user wants to read from */
56 BYTE table
[0x1000]; /* the rotating LZ table */
57 UINT curtabent
; /* CURrent TABle ENTry */
59 BYTE stringlen
; /* length and position of current string */
60 DWORD stringpos
; /* from stringtable */
63 WORD bytetype
; /* bitmask within blocks */
65 BYTE
*get
; /* GETLEN bytes */
66 DWORD getcur
; /* current read */
67 DWORD getlen
; /* length last got */
70 #define MAX_LZSTATES 16
71 static struct lzstate
*lzstates
[MAX_LZSTATES
];
73 #define IS_LZ_HANDLE(h) (((h) >= 0x400) && ((h) < 0x400+MAX_LZSTATES))
74 #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)-0x400] : NULL)
76 /* reads one compressed byte, including buffering */
77 #define GET(lzs,b) _lzget(lzs,&b)
78 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
81 _lzget(struct lzstate
*lzs
,BYTE
*b
) {
82 if (lzs
->getcur
<lzs
->getlen
) {
83 *b
= lzs
->get
[lzs
->getcur
++];
86 int ret
= _hread(lzs
->realfd
,lzs
->get
,GETLEN
);
97 /* internal function, reads lzheader
98 * returns BADINHANDLE for non filedescriptors
99 * return 0 for file not compressed using LZ
100 * return UNKNOWNALG for unknown algorithm
101 * returns lzfileheader in *head
103 static INT
read_header(HFILE fd
,struct lzfileheader
*head
)
107 if (_llseek(fd
,0,SEEK_SET
)==-1)
108 return LZERROR_BADINHANDLE
;
110 /* We can't directly read the lzfileheader struct due to
111 * structure element alignment
113 if (_hread(fd
,buf
,14)<14)
115 memcpy(head
->magic
,buf
,8);
116 memcpy(&(head
->compressiontype
),buf
+8,1);
117 memcpy(&(head
->lastchar
),buf
+9,1);
119 /* FIXME: consider endianess on non-intel architectures */
120 memcpy(&(head
->reallength
),buf
+10,4);
122 if (memcmp(head
->magic
,LZMagic
,8))
124 if (head
->compressiontype
!='A')
125 return LZERROR_UNKNOWNALG
;
130 /***********************************************************************
135 INT WINAPI
LZStart(void)
142 /***********************************************************************
145 * initializes internal decompression buffers, returns lzfiledescriptor.
146 * (return value the same as hfSrc, if hfSrc is not compressed)
147 * on failure, returns error code <0
148 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
150 * since _llseek uses the same types as libc.lseek, we just use the macros of
155 HFILE WINAPI
LZInit( HFILE hfSrc
)
158 struct lzfileheader head
;
163 DPRINT("(%d)\n",hfSrc
);
164 ret
=read_header(hfSrc
,&head
);
166 _llseek(hfSrc
,0,SEEK_SET
);
167 return ret
?ret
:hfSrc
;
169 for (i
= 0; i
< MAX_LZSTATES
; i
++) if (!lzstates
[i
]) break;
170 if (i
== MAX_LZSTATES
) return LZERROR_GLOBALLOC
;
171 lzstates
[i
] = lzs
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(struct lzstate
) );
172 if(lzs
== NULL
) return LZERROR_GLOBALLOC
;
174 memset(lzs
,'\0',sizeof(*lzs
));
176 lzs
->lastchar
= head
.lastchar
;
177 lzs
->reallength
= head
.reallength
;
179 lzs
->get
= RtlAllocateHeap( GetProcessHeap(), 0, GETLEN
);
183 if(lzs
->get
== NULL
) {
184 RtlFreeHeap(GetProcessHeap(), 0, lzs
);
186 return LZERROR_GLOBALLOC
;
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)
203 void WINAPI
LZDone(void)
209 /***********************************************************************
210 * GetExpandedNameA (LZ32.@)
212 * gets the full filename of the compressed file 'in' by opening it
213 * and reading the header
215 * "file." is being translated to "file"
216 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
217 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
222 INT WINAPI
GetExpandedNameA( LPSTR in
, LPSTR out
)
224 struct lzfileheader head
;
227 INT fnislowercased
,ret
,len
;
231 fd
=OpenFile(in
,&ofs
,OF_READ
);
233 return (INT
)(INT16
)LZERROR_BADINHANDLE
;
235 ret
=read_header(fd
,&head
);
237 /* not a LZ compressed file, so the expanded name is the same
238 * as the input name */
244 /* look for directory prefix and skip it. */
246 while (NULL
!=(t
=strpbrk(s
,"/\\:")))
249 /* now mangle the basename */
251 /* FIXME: hmm. shouldn't happen? */
252 DPRINT("Specified a directory or what? (%s)\n",in
);
256 /* see if we should use lowercase or uppercase on the last char */
264 fnislowercased
=islower(*t
);
267 if (isalpha(head
.lastchar
)) {
269 head
.lastchar
=tolower(head
.lastchar
);
271 head
.lastchar
=toupper(head
.lastchar
);
274 /* now look where to replace the last character */
275 if (NULL
!=(t
=strchr(s
,'.'))) {
281 t
[len
]=head
.lastchar
;
283 } /* else no modification necessary */
289 /***********************************************************************
290 * GetExpandedNameW (LZ32.@)
294 INT WINAPI
GetExpandedNameW( LPWSTR in
, LPWSTR out
)
297 DWORD len
= WideCharToMultiByte( CP_ACP
, 0, in
, -1, NULL
, 0, NULL
, NULL
);
298 char *xin
= RtlAllocateHeap( GetProcessHeap(), 0, len
);
299 char *xout
= RtlAllocateHeap( GetProcessHeap(), 0, len
+3 );
300 WideCharToMultiByte( CP_ACP
, 0, in
, -1, xin
, len
, NULL
, NULL
);
301 if ((ret
= GetExpandedNameA( xin
, xout
)) > 0)
302 MultiByteToWideChar( CP_ACP
, 0, xout
, -1, out
, wcslen(in
)+4 );
303 RtlFreeHeap( GetProcessHeap(), 0, xin
);
304 RtlFreeHeap( GetProcessHeap(), 0, xout
);
309 /***********************************************************************
314 INT WINAPI
LZRead( HFILE fd
, LPSTR vbuf
, INT toread
)
321 DPRINT("(%d,%p,%d)\n",fd
,buf
,toread
);
323 if (!(lzs
= GET_LZ_STATE(fd
))) return _hread(fd
,buf
,toread
);
325 /* The decompressor itself is in a define, cause we need it twice
326 * in this function. (the decompressed byte will be in b)
328 #define DECOMPRESS_ONE_BYTE \
329 if (lzs->stringlen) { \
330 b = lzs->table[lzs->stringpos]; \
331 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
334 if (!(lzs->bytetype&0x100)) { \
336 return toread-howmuch; \
337 lzs->bytetype = b|0xFF00; \
339 if (lzs->bytetype & 1) { \
341 return toread-howmuch; \
345 if (1!=GET(lzs,b1)) \
346 return toread-howmuch; \
347 if (1!=GET(lzs,b2)) \
348 return toread-howmuch; \
352 * where CAB is the stringoffset in the table\
353 * and D+3 is the len of the string \
355 lzs->stringpos = b1|((b2&0xf0)<<4); \
356 lzs->stringlen = (b2&0xf)+2; \
357 /* 3, but we use a byte already below ... */\
358 b = lzs->table[lzs->stringpos];\
359 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
363 /* store b in table */ \
364 lzs->table[lzs->curtabent++]= b; \
365 lzs->curtabent &= 0xFFF; \
368 /* if someone has seeked, we have to bring the decompressor
371 if (lzs
->realcurrent
!=lzs
->realwanted
) {
372 /* if the wanted position is before the current position
373 * I see no easy way to unroll ... We have to restart at
374 * the beginning. *sigh*
376 if (lzs
->realcurrent
>lzs
->realwanted
) {
377 /* flush decompressor state */
378 _llseek(lzs
->realfd
,14,SEEK_SET
);
383 memset(lzs
->table
,' ',0x1000);
384 lzs
->curtabent
= 0xFF0;
386 while (lzs
->realcurrent
<lzs
->realwanted
) {
398 #undef DECOMPRESS_ONE_BYTE
402 /***********************************************************************
407 LONG WINAPI
LZSeek( HFILE fd
, LONG off
, INT type
)
412 DPRINT("(%d,%ld,%d)\n",fd
,off
,type
);
413 /* not compressed? just use normal _llseek() */
414 if (!(lzs
= GET_LZ_STATE(fd
))) return _llseek(fd
,off
,type
);
415 newwanted
= lzs
->realwanted
;
417 case 1: /* SEEK_CUR */
420 case 2: /* SEEK_END */
421 newwanted
= lzs
->reallength
-off
;
423 default:/* SEEK_SET */
427 if (newwanted
>lzs
->reallength
)
428 return LZERROR_BADVALUE
;
430 return LZERROR_BADVALUE
;
431 lzs
->realwanted
= newwanted
;
436 /***********************************************************************
439 * Copies everything from src to dest
440 * if src is a LZ compressed file, it will be uncompressed.
441 * will return the number of bytes written to dest or errors.
445 LONG WINAPI
LZCopy( HFILE src
, HFILE dest
)
447 int usedlzinit
=0,ret
,wret
;
449 HFILE oldsrc
= src
, srcfd
;
455 /* we need that weird typedef, for i can't seem to get function pointer
456 * casts right. (Or they probably just do not like WINAPI in general)
458 typedef UINT (WINAPI
*_readfun
)(HFILE
,LPVOID
,UINT
);
462 DPRINT("(%d,%d)\n",src
,dest
);
463 if (!IS_LZ_HANDLE(src
)) {
465 if ((INT
)src
<= 0) return 0;
466 if (src
!= oldsrc
) usedlzinit
=1;
469 /* not compressed? just copy */
470 if (!IS_LZ_HANDLE(src
))
471 xread
=(_readfun
)_hread
;
473 xread
=(_readfun
)LZRead
;
476 ret
=xread(src
,buf
,BUFLEN
);
485 wret
= _hwrite(dest
,buf
,ret
);
487 return LZERROR_WRITE
;
490 /* Maintain the timestamp of source file to destination file */
491 srcfd
= (!(lzs
= GET_LZ_STATE(src
))) ? src
: lzs
->realfd
;
492 GetFileTime((HANDLE
)srcfd
, NULL
, NULL
, &filetime
);
493 SetFileTime((HANDLE
)dest
, NULL
, NULL
, &filetime
);
502 /* reverses GetExpandedPathname */
503 static LPSTR
LZEXPAND_MangleName( LPCSTR fn
)
506 char *mfn
= (char *)RtlAllocateHeap( GetProcessHeap(), 0,
507 strlen(fn
) + 3 ); /* "._" and \0 */
508 if(mfn
== NULL
) return NULL
;
510 if (!(p
= strrchr( mfn
, '\\' ))) p
= mfn
;
511 if ((p
= strchr( p
, '.' )))
514 if (strlen(p
) < 3) strcat( p
, "_" ); /* append '_' */
515 else p
[strlen(p
)-1] = '_'; /* replace last character */
517 else strcat( mfn
, "._" ); /* append "._" */
521 /***********************************************************************
522 * LZOpenFileA (LZ32.@)
524 * Opens a file. If not compressed, open it as a normal file.
528 HFILE WINAPI
LZOpenFileA( LPSTR fn
, LPOFSTRUCT ofs
, WORD mode
)
532 DPRINT("(%s,%p,%d)\n",fn
,ofs
,mode
);
533 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
534 fd
=OpenFile(fn
,ofs
,mode
);
537 LPSTR mfn
= LZEXPAND_MangleName(fn
);
538 fd
= OpenFile(mfn
,ofs
,mode
);
539 RtlFreeHeap( GetProcessHeap(), 0, mfn
);
541 if ((mode
&~0x70)!=OF_READ
)
546 if ((INT
)cfd
<= 0) return fd
;
551 /***********************************************************************
552 * LZOpenFileW (LZ32.@)
556 HFILE WINAPI
LZOpenFileW( LPWSTR fn
, LPOFSTRUCT ofs
, WORD mode
)
559 DWORD len
= WideCharToMultiByte( CP_ACP
, 0, fn
, -1, NULL
, 0, NULL
, NULL
);
560 LPSTR xfn
= RtlAllocateHeap( GetProcessHeap(), 0, len
);
561 WideCharToMultiByte( CP_ACP
, 0, fn
, -1, xfn
, len
, NULL
, NULL
);
562 ret
= LZOpenFileA(xfn
,ofs
,mode
);
563 RtlFreeHeap( GetProcessHeap(), 0, xfn
);
568 /***********************************************************************
573 void WINAPI
LZClose( HFILE fd
)
578 if (!(lzs
= GET_LZ_STATE(fd
))) _lclose(fd
);
581 if (lzs
->get
) RtlFreeHeap( GetProcessHeap(), 0, lzs
->get
);
582 CloseHandle((HANDLE
)lzs
->realfd
);
583 lzstates
[fd
- 0x400] = NULL
;
584 RtlFreeHeap( GetProcessHeap(), 0, lzs
);
589 /***********************************************************************
590 * CopyLZFile (LZ32.@)
592 * Copy src to dest (including uncompressing src).
593 * NOTE: Yes. This is exactly the same function as LZCopy.
597 LONG WINAPI
CopyLZFile( HFILE src
, HFILE dest
)
599 DPRINT("(%d,%d)\n",src
,dest
);
600 return LZCopy(src
,dest
);