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( LPSTR 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( LPWSTR 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, LPSTR vbuf, INT 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;
442 HFILE oldsrc = src, srcfd;
447 /* we need that weird typedef, for i can't seem to get function pointer
448 * casts right. (Or they probably just do not like WINAPI in general)
450 typedef UINT (WINAPI *_readfun)(HFILE,LPVOID,UINT);
454 TRACE("(%d,%d)\n",src,dest);
455 if (!IS_LZ_HANDLE(src)) {
457 if ((INT)src <= 0) return 0;
458 if (src != oldsrc) usedlzinit=1;
461 /* not compressed? just copy */
462 if (!IS_LZ_HANDLE(src))
465 xread=(_readfun)LZRead;
468 ret=xread(src,buf,BUFLEN);
477 wret = _lwrite(dest,buf,ret);
479 return LZERROR_WRITE;
482 /* Maintain the timestamp of source file to destination file */
483 srcfd = (!(lzs = GET_LZ_STATE(src))) ? src : lzs->realfd;
484 GetFileTime((HANDLE)srcfd, NULL, NULL, &filetime);
485 SetFileTime((HANDLE)dest, NULL, NULL, &filetime);
494 /* reverses GetExpandedPathname */
495 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
498 char *mfn = (char *)HeapAlloc( GetProcessHeap(), 0,
499 strlen(fn) + 3 ); /* "._" and \0 */
500 if(mfn == NULL) return NULL;
502 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
503 if ((p = strchr( p, '.' )))
506 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
507 else p[strlen(p)-1] = '_'; /* replace last character */
509 else strcat( mfn, "._" ); /* append "._" */
514 /***********************************************************************
515 * LZOpenFileA (LZ32.@)
517 * Opens a file. If not compressed, open it as a normal file.
519 HFILE WINAPI LZOpenFileA( LPSTR fn, LPOFSTRUCT ofs, WORD mode )
523 TRACE("(%s,%p,%d)\n",fn,ofs,mode);
524 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
525 fd=OpenFile(fn,ofs,mode);
528 LPSTR mfn = LZEXPAND_MangleName(fn);
529 fd = OpenFile(mfn,ofs,mode);
530 HeapFree( GetProcessHeap(), 0, mfn );
532 if ((mode&~0x70)!=OF_READ)
537 if ((INT)cfd <= 0) return fd;
542 /***********************************************************************
543 * LZOpenFileW (LZ32.@)
545 HFILE WINAPI LZOpenFileW( LPWSTR fn, LPOFSTRUCT ofs, WORD mode )
548 DWORD len = WideCharToMultiByte( CP_ACP, 0, fn, -1, NULL, 0, NULL, NULL );
549 LPSTR xfn = HeapAlloc( GetProcessHeap(), 0, len );
550 WideCharToMultiByte( CP_ACP, 0, fn, -1, xfn, len, NULL, NULL );
551 ret = LZOpenFileA(xfn,ofs,mode);
552 HeapFree( GetProcessHeap(), 0, xfn );
557 /***********************************************************************
560 void WINAPI LZClose( HFILE fd )
565 if (!(lzs = GET_LZ_STATE(fd))) _lclose(fd);
568 if (lzs->get) HeapFree( GetProcessHeap(), 0, lzs->get );
569 CloseHandle((HANDLE)lzs->realfd);
570 lzstates[fd - 0x400] = NULL;
571 HeapFree( GetProcessHeap(), 0, lzs );
576 /***********************************************************************
577 * CopyLZFile (LZ32.@)
579 * Copy src to dest (including uncompressing src).
580 * NOTE: Yes. This is exactly the same function as LZCopy.
582 LONG WINAPI CopyLZFile( HFILE src, HFILE dest )
584 TRACE("(%d,%d)\n",src,dest);
585 return LZCopy(src,dest);