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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * FIXME: return values might be wrong
28 #include <sys/types.h>
37 #include "wine/unicode.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(file);
43 /* The readahead length of the decompressor. Reading single bytes
44 * using _lread() would be SLOW.
48 /* Format of first 14 byte of LZ compressed file */
55 static BYTE LZMagic[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
58 HFILE realfd; /* the real filedescriptor */
59 CHAR lastchar; /* the last char of the filename */
61 DWORD reallength; /* the decompressed length of the file */
62 DWORD realcurrent; /* the position the decompressor currently is */
63 DWORD realwanted; /* the position the user wants to read from */
65 BYTE table[0x1000]; /* the rotating LZ table */
66 UINT curtabent; /* CURrent TABle ENTry */
68 BYTE stringlen; /* length and position of current string */
69 DWORD stringpos; /* from stringtable */
72 WORD bytetype; /* bitmask within blocks */
74 BYTE *get; /* GETLEN bytes */
75 DWORD getcur; /* current read */
76 DWORD getlen; /* length last got */
79 #define MAX_LZSTATES 16
80 static struct lzstate *lzstates[MAX_LZSTATES];
82 #define IS_LZ_HANDLE(h) (((h) >= 0x400) && ((h) < 0x400+MAX_LZSTATES))
83 #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)-0x400] : NULL)
85 /* reads one compressed byte, including buffering */
86 #define GET(lzs,b) _lzget(lzs,&b)
87 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
90 _lzget(struct lzstate *lzs,BYTE *b) {
91 if (lzs->getcur<lzs->getlen) {
92 *b = lzs->get[lzs->getcur++];
95 int ret = _lread(lzs->realfd,lzs->get,GETLEN);
106 /* internal function, reads lzheader
107 * returns BADINHANDLE for non filedescriptors
108 * return 0 for file not compressed using LZ
109 * return UNKNOWNALG for unknown algorithm
110 * returns lzfileheader in *head
112 static INT read_header(HFILE fd,struct lzfileheader *head)
116 if (_llseek(fd,0,SEEK_SET)==-1)
117 return LZERROR_BADINHANDLE;
119 /* We can't directly read the lzfileheader struct due to
120 * structure element alignment
122 if (_lread(fd,buf,14)<14)
124 memcpy(head->magic,buf,8);
125 memcpy(&(head->compressiontype),buf+8,1);
126 memcpy(&(head->lastchar),buf+9,1);
128 /* FIXME: consider endianess on non-intel architectures */
129 memcpy(&(head->reallength),buf+10,4);
131 if (memcmp(head->magic,LZMagic,8))
133 if (head->compressiontype!='A')
134 return LZERROR_UNKNOWNALG;
139 /***********************************************************************
142 INT WINAPI LZStart(void)
149 /***********************************************************************
152 * initializes internal decompression buffers, returns lzfiledescriptor.
153 * (return value the same as hfSrc, if hfSrc is not compressed)
154 * on failure, returns error code <0
155 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
157 * since _llseek uses the same types as libc.lseek, we just use the macros of
160 HFILE WINAPI LZInit( HFILE hfSrc )
163 struct lzfileheader head;
168 TRACE("(%d)\n",hfSrc);
169 ret=read_header(hfSrc,&head);
171 _llseek(hfSrc,0,SEEK_SET);
172 return ret?ret:hfSrc;
174 for (i = 0; i < MAX_LZSTATES; i++) if (!lzstates[i]) break;
175 if (i == MAX_LZSTATES) return LZERROR_GLOBALLOC;
176 lzstates[i] = lzs = HeapAlloc( GetProcessHeap(), 0, sizeof(struct lzstate) );
177 if(lzs == NULL) return LZERROR_GLOBALLOC;
179 memset(lzs,'\0',sizeof(*lzs));
181 lzs->lastchar = head.lastchar;
182 lzs->reallength = head.reallength;
184 lzs->get = HeapAlloc( GetProcessHeap(), 0, GETLEN );
188 if(lzs->get == NULL) {
189 HeapFree(GetProcessHeap(), 0, lzs);
191 return LZERROR_GLOBALLOC;
194 /* Yes, preinitialize with spaces */
195 memset(lzs->table,' ',0x1000);
196 /* Yes, start 16 byte from the END of the table */
197 lzs->curtabent = 0xff0;
202 /***********************************************************************
203 * LZDone (LZEXPAND.9)
206 void WINAPI LZDone(void)
212 /***********************************************************************
213 * GetExpandedNameA (LZ32.@)
215 * gets the full filename of the compressed file 'in' by opening it
216 * and reading the header
218 * "file." is being translated to "file"
219 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
220 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
223 INT WINAPI GetExpandedNameA( LPCSTR in, LPSTR out )
225 struct lzfileheader head;
228 INT fnislowercased,ret,len;
232 fd=OpenFile(in,&ofs,OF_READ);
234 return (INT)(INT16)LZERROR_BADINHANDLE;
236 ret=read_header(fd,&head);
238 /* not a LZ compressed file, so the expanded name is the same
239 * as the input name */
245 /* look for directory prefix and skip it. */
247 while (NULL!=(t=strpbrk(s,"/\\:")))
250 /* now mangle the basename */
252 /* FIXME: hmm. shouldn't happen? */
253 WARN("Specified a directory or what? (%s)\n",in);
257 /* see if we should use lowercase or uppercase on the last char */
265 fnislowercased=islower(*t);
268 if (isalpha(head.lastchar)) {
270 head.lastchar=tolower(head.lastchar);
272 head.lastchar=toupper(head.lastchar);
275 /* now look where to replace the last character */
276 if (NULL!=(t=strchr(s,'.'))) {
282 t[len]=head.lastchar;
284 } /* else no modification necessary */
290 /***********************************************************************
291 * GetExpandedNameW (LZ32.@)
293 INT WINAPI GetExpandedNameW( LPCWSTR in, LPWSTR out )
296 DWORD len = WideCharToMultiByte( CP_ACP, 0, in, -1, NULL, 0, NULL, NULL );
297 char *xin = HeapAlloc( GetProcessHeap(), 0, len );
298 char *xout = HeapAlloc( GetProcessHeap(), 0, len+3 );
299 WideCharToMultiByte( CP_ACP, 0, in, -1, xin, len, NULL, NULL );
300 if ((ret = GetExpandedNameA( xin, xout )) > 0)
301 MultiByteToWideChar( CP_ACP, 0, xout, -1, out, strlenW(in)+4 );
302 HeapFree( GetProcessHeap(), 0, xin );
303 HeapFree( GetProcessHeap(), 0, xout );
308 /***********************************************************************
311 INT WINAPI LZRead( HFILE fd, LPVOID vbuf, UINT toread )
318 TRACE("(%d,%p,%d)\n",fd,buf,toread);
320 if (!(lzs = GET_LZ_STATE(fd))) return _lread(fd,buf,toread);
322 /* The decompressor itself is in a define, cause we need it twice
323 * in this function. (the decompressed byte will be in b)
325 #define DECOMPRESS_ONE_BYTE \
326 if (lzs->stringlen) { \
327 b = lzs->table[lzs->stringpos]; \
328 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
331 if (!(lzs->bytetype&0x100)) { \
333 return toread-howmuch; \
334 lzs->bytetype = b|0xFF00; \
336 if (lzs->bytetype & 1) { \
338 return toread-howmuch; \
342 if (1!=GET(lzs,b1)) \
343 return toread-howmuch; \
344 if (1!=GET(lzs,b2)) \
345 return toread-howmuch; \
349 * where CAB is the stringoffset in the table\
350 * and D+3 is the len of the string \
352 lzs->stringpos = b1|((b2&0xf0)<<4); \
353 lzs->stringlen = (b2&0xf)+2; \
354 /* 3, but we use a byte already below ... */\
355 b = lzs->table[lzs->stringpos];\
356 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
360 /* store b in table */ \
361 lzs->table[lzs->curtabent++]= b; \
362 lzs->curtabent &= 0xFFF; \
365 /* if someone has seeked, we have to bring the decompressor
368 if (lzs->realcurrent!=lzs->realwanted) {
369 /* if the wanted position is before the current position
370 * I see no easy way to unroll ... We have to restart at
371 * the beginning. *sigh*
373 if (lzs->realcurrent>lzs->realwanted) {
374 /* flush decompressor state */
375 _llseek(lzs->realfd,14,SEEK_SET);
380 memset(lzs->table,' ',0x1000);
381 lzs->curtabent = 0xFF0;
383 while (lzs->realcurrent<lzs->realwanted) {
395 #undef DECOMPRESS_ONE_BYTE
399 /***********************************************************************
402 LONG WINAPI LZSeek( HFILE fd, LONG off, INT type )
407 TRACE("(%d,%ld,%d)\n",fd,off,type);
408 /* not compressed? just use normal _llseek() */
409 if (!(lzs = GET_LZ_STATE(fd))) return _llseek(fd,off,type);
410 newwanted = lzs->realwanted;
412 case 1: /* SEEK_CUR */
415 case 2: /* SEEK_END */
416 newwanted = lzs->reallength-off;
418 default:/* SEEK_SET */
422 if (newwanted>lzs->reallength)
423 return LZERROR_BADVALUE;
425 return LZERROR_BADVALUE;
426 lzs->realwanted = newwanted;
431 /***********************************************************************
434 * Copies everything from src to dest
435 * if src is a LZ compressed file, it will be uncompressed.
436 * will return the number of bytes written to dest or errors.
438 LONG WINAPI LZCopy( HFILE src, HFILE dest )
440 int usedlzinit=0,ret,wret;
445 /* we need that weird typedef, for i can't seem to get function pointer
446 * casts right. (Or they probably just do not like WINAPI in general)
448 typedef UINT (WINAPI *_readfun)(HFILE,LPVOID,UINT);
452 TRACE("(%d,%d)\n",src,dest);
453 if (!IS_LZ_HANDLE(src)) {
455 if ((INT)src <= 0) return 0;
456 if (src != oldsrc) usedlzinit=1;
459 /* not compressed? just copy */
460 if (!IS_LZ_HANDLE(src))
463 xread=(_readfun)LZRead;
466 ret=xread(src,buf,BUFLEN);
475 wret = _lwrite(dest,buf,ret);
477 return LZERROR_WRITE;
485 /* reverses GetExpandedPathname */
486 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
489 char *mfn = (char *)HeapAlloc( GetProcessHeap(), 0,
490 strlen(fn) + 3 ); /* "._" and \0 */
491 if(mfn == NULL) return NULL;
493 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
494 if ((p = strchr( p, '.' )))
497 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
498 else p[strlen(p)-1] = '_'; /* replace last character */
500 else strcat( mfn, "._" ); /* append "._" */
505 /***********************************************************************
506 * LZOpenFileA (LZ32.@)
508 * Opens a file. If not compressed, open it as a normal file.
510 HFILE WINAPI LZOpenFileA( LPCSTR fn, LPOFSTRUCT ofs, UINT mode )
514 TRACE("(%s,%p,%d)\n",fn,ofs,mode);
515 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
516 fd=OpenFile(fn,ofs,mode);
519 LPSTR mfn = LZEXPAND_MangleName(fn);
520 fd = OpenFile(mfn,ofs,mode);
521 HeapFree( GetProcessHeap(), 0, mfn );
523 if ((mode&~0x70)!=OF_READ)
528 if ((INT)cfd <= 0) return fd;
533 /***********************************************************************
534 * LZOpenFileW (LZ32.@)
536 HFILE WINAPI LZOpenFileW( LPCWSTR fn, LPOFSTRUCT ofs, UINT mode )
539 DWORD len = WideCharToMultiByte( CP_ACP, 0, fn, -1, NULL, 0, NULL, NULL );
540 LPSTR xfn = HeapAlloc( GetProcessHeap(), 0, len );
541 WideCharToMultiByte( CP_ACP, 0, fn, -1, xfn, len, NULL, NULL );
542 ret = LZOpenFileA(xfn,ofs,mode);
543 HeapFree( GetProcessHeap(), 0, xfn );
548 /***********************************************************************
551 void WINAPI LZClose( HFILE fd )
556 if (!(lzs = GET_LZ_STATE(fd))) _lclose(fd);
559 if (lzs->get) HeapFree( GetProcessHeap(), 0, lzs->get );
560 CloseHandle((HANDLE)lzs->realfd);
561 lzstates[fd - 0x400] = NULL;
562 HeapFree( GetProcessHeap(), 0, lzs );
567 /***********************************************************************
568 * CopyLZFile (LZ32.@)
570 * Copy src to dest (including uncompressing src).
571 * NOTE: Yes. This is exactly the same function as LZCopy.
573 LONG WINAPI CopyLZFile( HFILE src, HFILE dest )
575 TRACE("(%d,%d)\n",src,dest);
576 return LZCopy(src,dest);