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>
39 #include "wine/unicode.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(file);
45 /* The readahead length of the decompressor. Reading single bytes
46 * using _lread() would be SLOW.
50 /* Format of first 14 byte of LZ compressed file */
57 static BYTE LZMagic[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
60 HFILE realfd; /* the real filedescriptor */
61 CHAR lastchar; /* the last char of the filename */
63 DWORD reallength; /* the decompressed length of the file */
64 DWORD realcurrent; /* the position the decompressor currently is */
65 DWORD realwanted; /* the position the user wants to read from */
67 BYTE table[0x1000]; /* the rotating LZ table */
68 UINT curtabent; /* CURrent TABle ENTry */
70 BYTE stringlen; /* length and position of current string */
71 DWORD stringpos; /* from stringtable */
74 WORD bytetype; /* bitmask within blocks */
76 BYTE *get; /* GETLEN bytes */
77 DWORD getcur; /* current read */
78 DWORD getlen; /* length last got */
81 #define MAX_LZSTATES 16
82 static struct lzstate *lzstates[MAX_LZSTATES];
84 #define IS_LZ_HANDLE(h) (((h) >= 0x400) && ((h) < 0x400+MAX_LZSTATES))
85 #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)-0x400] : NULL)
87 /* reads one compressed byte, including buffering */
88 #define GET(lzs,b) _lzget(lzs,&b)
89 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
92 _lzget(struct lzstate *lzs,BYTE *b) {
93 if (lzs->getcur<lzs->getlen) {
94 *b = lzs->get[lzs->getcur++];
97 int ret = _lread(lzs->realfd,lzs->get,GETLEN);
108 /* internal function, reads lzheader
109 * returns BADINHANDLE for non filedescriptors
110 * return 0 for file not compressed using LZ
111 * return UNKNOWNALG for unknown algorithm
112 * returns lzfileheader in *head
114 static INT read_header(HFILE fd,struct lzfileheader *head)
118 if (_llseek(fd,0,SEEK_SET)==-1)
119 return LZERROR_BADINHANDLE;
121 /* We can't directly read the lzfileheader struct due to
122 * structure element alignment
124 if (_lread(fd,buf,14)<14)
126 memcpy(head->magic,buf,8);
127 memcpy(&(head->compressiontype),buf+8,1);
128 memcpy(&(head->lastchar),buf+9,1);
130 /* FIXME: consider endianess on non-intel architectures */
131 memcpy(&(head->reallength),buf+10,4);
133 if (memcmp(head->magic,LZMagic,8))
135 if (head->compressiontype!='A')
136 return LZERROR_UNKNOWNALG;
141 /***********************************************************************
144 INT WINAPI LZStart(void)
151 /***********************************************************************
154 * initializes internal decompression buffers, returns lzfiledescriptor.
155 * (return value the same as hfSrc, if hfSrc is not compressed)
156 * on failure, returns error code <0
157 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
159 * since _llseek uses the same types as libc.lseek, we just use the macros of
162 HFILE WINAPI LZInit( HFILE hfSrc )
165 struct lzfileheader head;
170 TRACE("(%d)\n",hfSrc);
171 ret=read_header(hfSrc,&head);
173 _llseek(hfSrc,0,SEEK_SET);
174 return ret?ret:hfSrc;
176 for (i = 0; i < MAX_LZSTATES; i++) if (!lzstates[i]) break;
177 if (i == MAX_LZSTATES) return LZERROR_GLOBALLOC;
178 lzstates[i] = lzs = HeapAlloc( GetProcessHeap(), 0, sizeof(struct lzstate) );
179 if(lzs == NULL) return LZERROR_GLOBALLOC;
181 memset(lzs,'\0',sizeof(*lzs));
183 lzs->lastchar = head.lastchar;
184 lzs->reallength = head.reallength;
186 lzs->get = HeapAlloc( GetProcessHeap(), 0, GETLEN );
190 if(lzs->get == NULL) {
191 HeapFree(GetProcessHeap(), 0, lzs);
193 return LZERROR_GLOBALLOC;
196 /* Yes, preinitialize with spaces */
197 memset(lzs->table,' ',0x1000);
198 /* Yes, start 16 byte from the END of the table */
199 lzs->curtabent = 0xff0;
204 /***********************************************************************
205 * LZDone (LZEXPAND.9)
208 void WINAPI LZDone(void)
214 /***********************************************************************
215 * GetExpandedNameA (LZ32.@)
217 * gets the full filename of the compressed file 'in' by opening it
218 * and reading the header
220 * "file." is being translated to "file"
221 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
222 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
225 INT WINAPI GetExpandedNameA( LPSTR in, LPSTR out )
227 struct lzfileheader head;
230 INT fnislowercased,ret,len;
234 fd=OpenFile(in,&ofs,OF_READ);
236 return (INT)(INT16)LZERROR_BADINHANDLE;
238 ret=read_header(fd,&head);
240 /* not a LZ compressed file, so the expanded name is the same
241 * as the input name */
247 /* look for directory prefix and skip it. */
249 while (NULL!=(t=strpbrk(s,"/\\:")))
252 /* now mangle the basename */
254 /* FIXME: hmm. shouldn't happen? */
255 WARN("Specified a directory or what? (%s)\n",in);
259 /* see if we should use lowercase or uppercase on the last char */
267 fnislowercased=islower(*t);
270 if (isalpha(head.lastchar)) {
272 head.lastchar=tolower(head.lastchar);
274 head.lastchar=toupper(head.lastchar);
277 /* now look where to replace the last character */
278 if (NULL!=(t=strchr(s,'.'))) {
284 t[len]=head.lastchar;
286 } /* else no modification necessary */
292 /***********************************************************************
293 * GetExpandedNameW (LZ32.@)
295 INT WINAPI GetExpandedNameW( LPWSTR in, LPWSTR out )
298 DWORD len = WideCharToMultiByte( CP_ACP, 0, in, -1, NULL, 0, NULL, NULL );
299 char *xin = HeapAlloc( GetProcessHeap(), 0, len );
300 char *xout = HeapAlloc( GetProcessHeap(), 0, len+3 );
301 WideCharToMultiByte( CP_ACP, 0, in, -1, xin, len, NULL, NULL );
302 if ((ret = GetExpandedNameA( xin, xout )) > 0)
303 MultiByteToWideChar( CP_ACP, 0, xout, -1, out, strlenW(in)+4 );
304 HeapFree( GetProcessHeap(), 0, xin );
305 HeapFree( GetProcessHeap(), 0, xout );
310 /***********************************************************************
313 INT WINAPI LZRead( HFILE fd, LPSTR vbuf, INT toread )
320 TRACE("(%d,%p,%d)\n",fd,buf,toread);
322 if (!(lzs = GET_LZ_STATE(fd))) return _lread(fd,buf,toread);
324 /* The decompressor itself is in a define, cause we need it twice
325 * in this function. (the decompressed byte will be in b)
327 #define DECOMPRESS_ONE_BYTE \
328 if (lzs->stringlen) { \
329 b = lzs->table[lzs->stringpos]; \
330 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
333 if (!(lzs->bytetype&0x100)) { \
335 return toread-howmuch; \
336 lzs->bytetype = b|0xFF00; \
338 if (lzs->bytetype & 1) { \
340 return toread-howmuch; \
344 if (1!=GET(lzs,b1)) \
345 return toread-howmuch; \
346 if (1!=GET(lzs,b2)) \
347 return toread-howmuch; \
351 * where CAB is the stringoffset in the table\
352 * and D+3 is the len of the string \
354 lzs->stringpos = b1|((b2&0xf0)<<4); \
355 lzs->stringlen = (b2&0xf)+2; \
356 /* 3, but we use a byte already below ... */\
357 b = lzs->table[lzs->stringpos];\
358 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
362 /* store b in table */ \
363 lzs->table[lzs->curtabent++]= b; \
364 lzs->curtabent &= 0xFFF; \
367 /* if someone has seeked, we have to bring the decompressor
370 if (lzs->realcurrent!=lzs->realwanted) {
371 /* if the wanted position is before the current position
372 * I see no easy way to unroll ... We have to restart at
373 * the beginning. *sigh*
375 if (lzs->realcurrent>lzs->realwanted) {
376 /* flush decompressor state */
377 _llseek(lzs->realfd,14,SEEK_SET);
382 memset(lzs->table,' ',0x1000);
383 lzs->curtabent = 0xFF0;
385 while (lzs->realcurrent<lzs->realwanted) {
397 #undef DECOMPRESS_ONE_BYTE
401 /***********************************************************************
404 LONG WINAPI LZSeek( HFILE fd, LONG off, INT type )
409 TRACE("(%d,%ld,%d)\n",fd,off,type);
410 /* not compressed? just use normal _llseek() */
411 if (!(lzs = GET_LZ_STATE(fd))) return _llseek(fd,off,type);
412 newwanted = lzs->realwanted;
414 case 1: /* SEEK_CUR */
417 case 2: /* SEEK_END */
418 newwanted = lzs->reallength-off;
420 default:/* SEEK_SET */
424 if (newwanted>lzs->reallength)
425 return LZERROR_BADVALUE;
427 return LZERROR_BADVALUE;
428 lzs->realwanted = newwanted;
433 /***********************************************************************
436 * Copies everything from src to dest
437 * if src is a LZ compressed file, it will be uncompressed.
438 * will return the number of bytes written to dest or errors.
440 LONG WINAPI LZCopy( HFILE src, HFILE dest )
442 int usedlzinit = 0, ret, wret;
444 HFILE oldsrc = src, srcfd;
449 /* we need that weird typedef, for i can't seem to get function pointer
450 * casts right. (Or they probably just do not like WINAPI in general)
452 typedef UINT (WINAPI *_readfun)(HFILE,LPVOID,UINT);
456 TRACE("(%d,%d)\n",src,dest);
457 if (!IS_LZ_HANDLE(src)) {
459 if ((INT)src <= 0) return 0;
460 if (src != oldsrc) usedlzinit=1;
463 /* not compressed? just copy */
464 if (!IS_LZ_HANDLE(src))
467 xread=(_readfun)LZRead;
470 ret=xread(src,buf,BUFLEN);
479 wret = _lwrite(dest,buf,ret);
481 return LZERROR_WRITE;
484 /* Maintain the timestamp of source file to destination file */
485 srcfd = (!(lzs = GET_LZ_STATE(src))) ? src : lzs->realfd;
486 GetFileTime((HANDLE)srcfd, NULL, NULL, &filetime);
487 SetFileTime((HANDLE)dest, NULL, NULL, &filetime);
496 /* reverses GetExpandedPathname */
497 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
500 char *mfn = (char *)HeapAlloc( GetProcessHeap(), 0,
501 strlen(fn) + 3 ); /* "._" and \0 */
502 if(mfn == NULL) return NULL;
504 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
505 if ((p = strchr( p, '.' )))
508 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
509 else p[strlen(p)-1] = '_'; /* replace last character */
511 else strcat( mfn, "._" ); /* append "._" */
516 /***********************************************************************
517 * LZOpenFileA (LZ32.@)
519 * Opens a file. If not compressed, open it as a normal file.
521 HFILE WINAPI LZOpenFileA( LPSTR fn, LPOFSTRUCT ofs, WORD mode )
525 TRACE("(%s,%p,%d)\n",fn,ofs,mode);
526 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
527 fd=OpenFile(fn,ofs,mode);
530 LPSTR mfn = LZEXPAND_MangleName(fn);
531 fd = OpenFile(mfn,ofs,mode);
532 HeapFree( GetProcessHeap(), 0, mfn );
534 if ((mode&~0x70)!=OF_READ)
539 if ((INT)cfd <= 0) return fd;
544 /***********************************************************************
545 * LZOpenFileW (LZ32.@)
547 HFILE WINAPI LZOpenFileW( LPWSTR fn, LPOFSTRUCT ofs, WORD mode )
550 DWORD len = WideCharToMultiByte( CP_ACP, 0, fn, -1, NULL, 0, NULL, NULL );
551 LPSTR xfn = HeapAlloc( GetProcessHeap(), 0, len );
552 WideCharToMultiByte( CP_ACP, 0, fn, -1, xfn, len, NULL, NULL );
553 ret = LZOpenFileA(xfn,ofs,mode);
554 HeapFree( GetProcessHeap(), 0, xfn );
559 /***********************************************************************
562 void WINAPI LZClose( HFILE fd )
567 if (!(lzs = GET_LZ_STATE(fd))) _lclose(fd);
570 if (lzs->get) HeapFree( GetProcessHeap(), 0, lzs->get );
571 CloseHandle((HANDLE)lzs->realfd);
572 lzstates[fd - 0x400] = NULL;
573 HeapFree( GetProcessHeap(), 0, lzs );
578 /***********************************************************************
579 * CopyLZFile (LZ32.@)
581 * Copy src to dest (including uncompressing src).
582 * NOTE: Yes. This is exactly the same function as LZCopy.
584 LONG WINAPI CopyLZFile( HFILE src, HFILE dest )
586 TRACE("(%d,%d)\n",src,dest);
587 return LZCopy(src,dest);