2 * LZ Decompression functions
4 * Copyright 1996 Marcus Meissner
7 * FIXME: return values might be wrong
14 #include "wine/winbase16.h"
15 #include "wine/winestring.h"
22 /* The readahead length of the decompressor. Reading single bytes
23 * using _lread() would be SLOW.
27 /* Format of first 14 byte of LZ compressed file */
34 static BYTE LZMagic[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
37 HFILE realfd; /* the real filedescriptor */
38 CHAR lastchar; /* the last char of the filename */
40 DWORD reallength; /* the decompressed length of the file */
41 DWORD realcurrent; /* the position the decompressor currently is */
42 DWORD realwanted; /* the position the user wants to read from */
44 BYTE table[0x1000]; /* the rotating LZ table */
45 UINT curtabent; /* CURrent TABle ENTry */
47 BYTE stringlen; /* length and position of current string */
48 DWORD stringpos; /* from stringtable */
51 WORD bytetype; /* bitmask within blocks */
53 BYTE *get; /* GETLEN bytes */
54 DWORD getcur; /* current read */
55 DWORD getlen; /* length last got */
58 #define MAX_LZSTATES 16
59 static struct lzstate *lzstates[MAX_LZSTATES];
61 #define IS_LZ_HANDLE(h) (((h) >= 0x400) && ((h) < 0x400+MAX_LZSTATES))
62 #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)-0x400] : NULL)
64 /* reads one compressed byte, including buffering */
65 #define GET(lzs,b) _lzget(lzs,&b)
66 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
69 _lzget(struct lzstate *lzs,BYTE *b) {
70 if (lzs->getcur<lzs->getlen) {
71 *b = lzs->get[lzs->getcur++];
74 int ret = _lread(lzs->realfd,lzs->get,GETLEN);
85 /* internal function, reads lzheader
86 * returns BADINHANDLE for non filedescriptors
87 * return 0 for file not compressed using LZ
88 * return UNKNOWNALG for unknown algorithm
89 * returns lzfileheader in *head
91 static INT read_header(HFILE fd,struct lzfileheader *head)
95 if (_llseek(fd,0,SEEK_SET)==-1)
96 return LZERROR_BADINHANDLE;
98 /* We can't directly read the lzfileheader struct due to
99 * structure element alignment
101 if (_lread(fd,buf,14)<14)
103 memcpy(head->magic,buf,8);
104 memcpy(&(head->compressiontype),buf+8,1);
105 memcpy(&(head->lastchar),buf+9,1);
107 /* FIXME: consider endianess on non-intel architectures */
108 memcpy(&(head->reallength),buf+10,4);
110 if (memcmp(head->magic,LZMagic,8))
112 if (head->compressiontype!='A')
113 return LZERROR_UNKNOWNALG;
117 /***********************************************************************
118 * LZStart16 (LZEXPAND.7)
120 INT16 WINAPI LZStart16(void)
122 TRACE(file,"(void)\n");
127 /***********************************************************************
130 INT WINAPI LZStart(void)
132 TRACE(file,"(void)\n");
137 /***********************************************************************
138 * LZInit16 (LZEXPAND.3)
140 HFILE16 WINAPI LZInit16( HFILE16 hfSrc )
142 HFILE ret = LZInit( FILE_GetHandle(hfSrc) );
143 if (IS_LZ_HANDLE(ret)) return ret;
144 if ((INT)ret <= 0) return ret;
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(file,"(%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( SystemHeap, 0, sizeof(struct lzstate) );
178 memset(lzs,'\0',sizeof(*lzs));
180 lzs->lastchar = head.lastchar;
181 lzs->reallength = head.reallength;
183 lzs->get = HEAP_xalloc( GetProcessHeap(), 0, GETLEN );
187 /* Yes, preinitialize with spaces */
188 memset(lzs->table,' ',0x1000);
189 /* Yes, start 16 byte from the END of the table */
190 lzs->curtabent = 0xff0;
195 /***********************************************************************
196 * LZDone (LZEXPAND.9) (LZ32.8)
198 void WINAPI LZDone(void)
200 TRACE(file,"(void)\n");
204 /***********************************************************************
205 * GetExpandedName16 (LZEXPAND.10)
207 INT16 WINAPI GetExpandedName16( LPCSTR in, LPSTR out )
209 return (INT16)GetExpandedNameA( in, out );
213 /***********************************************************************
214 * GetExpandedName32A (LZ32.9)
216 * gets the full filename of the compressed file 'in' by opening it
217 * and reading the header
219 * "file." is being translated to "file"
220 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
221 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
224 INT WINAPI GetExpandedNameA( LPCSTR in, LPSTR out )
226 struct lzfileheader head;
229 INT fnislowercased,ret,len;
232 TRACE(file,"(%s)\n",in);
233 fd=OpenFile(in,&ofs,OF_READ);
235 return (INT)(INT16)LZERROR_BADINHANDLE;
237 ret=read_header(fd,&head);
239 /* not a LZ compressed file, so the expanded name is the same
240 * as the input name */
246 /* look for directory prefix and skip it. */
248 while (NULL!=(t=strpbrk(s,"/\\:")))
251 /* now mangle the basename */
253 /* FIXME: hmm. shouldn't happen? */
254 WARN(file,"Specified a directory or what? (%s)\n",in);
258 /* see if we should use lowercase or uppercase on the last char */
266 fnislowercased=islower(*t);
269 if (isalpha(head.lastchar)) {
271 head.lastchar=tolower(head.lastchar);
273 head.lastchar=toupper(head.lastchar);
276 /* now look where to replace the last character */
277 if (NULL!=(t=strchr(s,'.'))) {
283 t[len]=head.lastchar;
285 } /* else no modification necessary */
291 /***********************************************************************
292 * GetExpandedName32W (LZ32.11)
294 INT WINAPI GetExpandedNameW( LPCWSTR in, LPWSTR out )
299 xout = HeapAlloc( GetProcessHeap(), 0, lstrlenW(in)+3 );
300 xin = HEAP_strdupWtoA( GetProcessHeap(), 0, in );
301 ret = GetExpandedName16(xin,xout);
302 if (ret>0) lstrcpyAtoW(out,xout);
303 HeapFree( GetProcessHeap(), 0, xin );
304 HeapFree( GetProcessHeap(), 0, xout );
309 /***********************************************************************
310 * LZRead16 (LZEXPAND.5)
312 INT16 WINAPI LZRead16( HFILE16 fd, LPVOID buf, UINT16 toread )
314 if (IS_LZ_HANDLE(fd)) return LZRead( fd, buf, toread );
315 return _lread16( fd, buf, toread );
319 /***********************************************************************
322 INT WINAPI LZRead( HFILE fd, LPVOID vbuf, UINT toread )
329 TRACE(file,"(%d,%p,%d)\n",fd,buf,toread);
331 if (!(lzs = GET_LZ_STATE(fd))) return _lread(fd,buf,toread);
333 /* The decompressor itself is in a define, cause we need it twice
334 * in this function. (the decompressed byte will be in b)
336 #define DECOMPRESS_ONE_BYTE \
337 if (lzs->stringlen) { \
338 b = lzs->table[lzs->stringpos]; \
339 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
342 if (!(lzs->bytetype&0x100)) { \
344 return toread-howmuch; \
345 lzs->bytetype = b|0xFF00; \
347 if (lzs->bytetype & 1) { \
349 return toread-howmuch; \
353 if (1!=GET(lzs,b1)) \
354 return toread-howmuch; \
355 if (1!=GET(lzs,b2)) \
356 return toread-howmuch; \
360 * where CAB is the stringoffset in the table\
361 * and D+3 is the len of the string \
363 lzs->stringpos = b1|((b2&0xf0)<<4); \
364 lzs->stringlen = (b2&0xf)+2; \
365 /* 3, but we use a byte already below ... */\
366 b = lzs->table[lzs->stringpos];\
367 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
371 /* store b in table */ \
372 lzs->table[lzs->curtabent++]= b; \
373 lzs->curtabent &= 0xFFF; \
376 /* if someone has seeked, we have to bring the decompressor
379 if (lzs->realcurrent!=lzs->realwanted) {
380 /* if the wanted position is before the current position
381 * I see no easy way to unroll ... We have to restart at
382 * the beginning. *sigh*
384 if (lzs->realcurrent>lzs->realwanted) {
385 /* flush decompressor state */
386 _llseek(lzs->realfd,14,SEEK_SET);
391 memset(lzs->table,' ',0x1000);
392 lzs->curtabent = 0xFF0;
394 while (lzs->realcurrent<lzs->realwanted) {
406 #undef DECOMPRESS_ONE_BYTE
410 /***********************************************************************
411 * LZSeek16 (LZEXPAND.4)
413 LONG WINAPI LZSeek16( HFILE16 fd, LONG off, INT16 type )
415 if (IS_LZ_HANDLE(fd)) return LZSeek( fd, off, type );
416 return _llseek16( fd, off, type );
420 /***********************************************************************
423 LONG WINAPI LZSeek( HFILE fd, LONG off, INT type )
428 TRACE(file,"(%d,%ld,%d)\n",fd,off,type);
429 /* not compressed? just use normal _llseek() */
430 if (!(lzs = GET_LZ_STATE(fd))) return _llseek(fd,off,type);
431 newwanted = lzs->realwanted;
433 case 1: /* SEEK_CUR */
436 case 2: /* SEEK_END */
437 newwanted = lzs->reallength-off;
439 default:/* SEEK_SET */
443 if (newwanted>lzs->reallength)
444 return LZERROR_BADVALUE;
446 return LZERROR_BADVALUE;
447 lzs->realwanted = newwanted;
452 /***********************************************************************
453 * LZCopy16 (LZEXPAND.1)
456 LONG WINAPI LZCopy16( HFILE16 src, HFILE16 dest )
458 /* already a LZ handle? */
459 if (IS_LZ_HANDLE(src)) return LZCopy( src, FILE_GetHandle(dest) );
461 /* no, try to open one */
463 if ((INT16)src <= 0) return 0;
464 if (IS_LZ_HANDLE(src))
466 LONG ret = LZCopy( src, FILE_GetHandle(dest) );
470 /* it was not a compressed file */
471 return LZCopy( FILE_GetHandle(src), FILE_GetHandle(dest) );
475 /***********************************************************************
478 * Copies everything from src to dest
479 * if src is a LZ compressed file, it will be uncompressed.
480 * will return the number of bytes written to dest or errors.
482 LONG WINAPI LZCopy( HFILE src, HFILE dest )
484 int usedlzinit=0,ret,wret;
489 UINT WINAPI (*xread)(HFILE,LPVOID,UINT);
491 TRACE(file,"(%d,%d)\n",src,dest);
492 if (!IS_LZ_HANDLE(src)) {
494 if ((INT)src <= 0) return 0;
495 if (src != oldsrc) usedlzinit=1;
498 /* not compressed? just copy */
499 if (!IS_LZ_HANDLE(src))
501 else /* Note: Ignore warning, just mismatched INT/UINT */
505 ret=xread(src,buf,BUFLEN);
514 wret = _lwrite(dest,buf,ret);
516 return LZERROR_WRITE;
524 /* reverses GetExpandedPathname */
525 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
528 char *mfn = (char *)HEAP_xalloc( GetProcessHeap(), 0,
529 strlen(fn) + 3 ); /* "._" and \0 */
531 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
532 if ((p = strchr( p, '.' )))
535 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
536 else p[strlen(p)-1] = '_'; /* replace last character */
538 else strcat( mfn, "._" ); /* append "._" */
543 /***********************************************************************
544 * LZOpenFile16 (LZEXPAND.2)
546 HFILE16 WINAPI LZOpenFile16( LPCSTR fn, LPOFSTRUCT ofs, UINT16 mode )
548 HFILE hfret = LZOpenFileA( fn, ofs, mode );
549 /* return errors and LZ handles unmodified */
550 if ((INT)hfret <= 0) return hfret;
551 if (IS_LZ_HANDLE(hfret)) return hfret;
552 /* but allocate a dos handle for 'normal' files */
553 return FILE_AllocDosHandle(hfret);
557 /***********************************************************************
558 * LZOpenFile32A (LZ32.1)
560 * Opens a file. If not compressed, open it as a normal file.
562 HFILE WINAPI LZOpenFileA( LPCSTR fn, LPOFSTRUCT ofs, UINT mode )
566 TRACE(file,"(%s,%p,%d)\n",fn,ofs,mode);
567 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
568 fd=OpenFile(fn,ofs,mode);
571 LPSTR mfn = LZEXPAND_MangleName(fn);
572 fd = OpenFile(mfn,ofs,mode);
573 HeapFree( GetProcessHeap(), 0, mfn );
575 if ((mode&~0x70)!=OF_READ)
580 if ((INT)cfd <= 0) return fd;
585 /***********************************************************************
586 * LZOpenFile32W (LZ32.10)
588 HFILE WINAPI LZOpenFileW( LPCWSTR fn, LPOFSTRUCT ofs, UINT mode )
594 xfn = HEAP_strdupWtoA( GetProcessHeap(), 0, fn);
595 ret = LZOpenFile16(xfn,ofs,mode);
596 HeapFree( GetProcessHeap(), 0, xfn );
597 if (ret!=HFILE_ERROR) {
598 /* ofs->szPathName is an array with the OFSTRUCT */
599 yfn = HEAP_strdupAtoW( GetProcessHeap(), 0, ofs->szPathName );
600 memcpy(ofs->szPathName,yfn,lstrlenW(yfn)*2+2);
601 HeapFree( GetProcessHeap(), 0, yfn );
607 /***********************************************************************
608 * LZClose16 (LZEXPAND.6)
610 void WINAPI LZClose16( HFILE16 fd )
612 if (IS_LZ_HANDLE(fd)) LZClose( fd );
613 else _lclose16( fd );
617 /***********************************************************************
620 void WINAPI LZClose( HFILE fd )
624 TRACE(file,"(%d)\n",fd);
625 if (!(lzs = GET_LZ_STATE(fd))) _lclose(fd);
628 if (lzs->get) HeapFree( GetProcessHeap(), 0, lzs->get );
629 CloseHandle(lzs->realfd);
630 lzstates[fd - 0x400] = NULL;
631 HeapFree( SystemHeap, 0, lzs );
635 /***********************************************************************
636 * CopyLZFile16 (LZEXPAND.8)
638 LONG WINAPI CopyLZFile16( HFILE16 src, HFILE16 dest )
640 TRACE(file,"(%d,%d)\n",src,dest);
641 return LZCopy16(src,dest);
645 /***********************************************************************
646 * CopyLZFile32 (LZ32.7)
648 * Copy src to dest (including uncompressing src).
649 * NOTE: Yes. This is exactly the same function as LZCopy.
651 LONG WINAPI CopyLZFile( HFILE src, HFILE dest )
653 TRACE(file,"(%d,%d)\n",src,dest);
654 return LZCopy(src,dest);