2 * LZ Decompression functions
4 * Copyright 1996 Marcus Meissner
7 * FIXME: return values might be wrong
12 #include <sys/types.h>
16 #include "wine/winbase16.h"
17 #include "wine/winestring.h"
21 #include "debugtools.h"
23 DEFAULT_DEBUG_CHANNEL(file);
25 LONG WINAPI CopyLZFile16(HFILE16,HFILE16);
26 INT16 WINAPI GetExpandedName16(LPCSTR,LPSTR);
27 void WINAPI LZClose16(HFILE16);
28 LONG WINAPI LZCopy16(HFILE16,HFILE16);
29 HFILE16 WINAPI LZInit16(HFILE16);
30 HFILE16 WINAPI LZOpenFile16(LPCSTR,LPOFSTRUCT,UINT16);
31 INT16 WINAPI LZRead16(HFILE16,LPVOID,UINT16);
32 LONG WINAPI LZSeek16(HFILE16,LONG,INT16);
33 INT16 WINAPI LZStart16(void);
35 /* The readahead length of the decompressor. Reading single bytes
36 * using _lread() would be SLOW.
40 /* Format of first 14 byte of LZ compressed file */
47 static BYTE LZMagic[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
50 HFILE realfd; /* the real filedescriptor */
51 CHAR lastchar; /* the last char of the filename */
53 DWORD reallength; /* the decompressed length of the file */
54 DWORD realcurrent; /* the position the decompressor currently is */
55 DWORD realwanted; /* the position the user wants to read from */
57 BYTE table[0x1000]; /* the rotating LZ table */
58 UINT curtabent; /* CURrent TABle ENTry */
60 BYTE stringlen; /* length and position of current string */
61 DWORD stringpos; /* from stringtable */
64 WORD bytetype; /* bitmask within blocks */
66 BYTE *get; /* GETLEN bytes */
67 DWORD getcur; /* current read */
68 DWORD getlen; /* length last got */
71 #define MAX_LZSTATES 16
72 static struct lzstate *lzstates[MAX_LZSTATES];
74 #define IS_LZ_HANDLE(h) (((h) >= 0x400) && ((h) < 0x400+MAX_LZSTATES))
75 #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)-0x400] : NULL)
77 /* reads one compressed byte, including buffering */
78 #define GET(lzs,b) _lzget(lzs,&b)
79 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
82 _lzget(struct lzstate *lzs,BYTE *b) {
83 if (lzs->getcur<lzs->getlen) {
84 *b = lzs->get[lzs->getcur++];
87 int ret = _lread(lzs->realfd,lzs->get,GETLEN);
98 /* internal function, reads lzheader
99 * returns BADINHANDLE for non filedescriptors
100 * return 0 for file not compressed using LZ
101 * return UNKNOWNALG for unknown algorithm
102 * returns lzfileheader in *head
104 static INT read_header(HFILE fd,struct lzfileheader *head)
108 if (_llseek(fd,0,SEEK_SET)==-1)
109 return LZERROR_BADINHANDLE;
111 /* We can't directly read the lzfileheader struct due to
112 * structure element alignment
114 if (_lread(fd,buf,14)<14)
116 memcpy(head->magic,buf,8);
117 memcpy(&(head->compressiontype),buf+8,1);
118 memcpy(&(head->lastchar),buf+9,1);
120 /* FIXME: consider endianess on non-intel architectures */
121 memcpy(&(head->reallength),buf+10,4);
123 if (memcmp(head->magic,LZMagic,8))
125 if (head->compressiontype!='A')
126 return LZERROR_UNKNOWNALG;
130 /***********************************************************************
131 * LZStart16 (LZEXPAND.7)
133 INT16 WINAPI LZStart16(void)
140 /***********************************************************************
143 INT WINAPI LZStart(void)
150 /***********************************************************************
151 * LZInit16 (LZEXPAND.3)
153 HFILE16 WINAPI LZInit16( HFILE16 hfSrc )
155 HFILE ret = LZInit( FILE_GetHandle(hfSrc) );
156 if (IS_LZ_HANDLE(ret)) return ret;
157 if ((INT)ret <= 0) return ret;
162 /***********************************************************************
165 * initializes internal decompression buffers, returns lzfiledescriptor.
166 * (return value the same as hfSrc, if hfSrc is not compressed)
167 * on failure, returns error code <0
168 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
170 * since _llseek uses the same types as libc.lseek, we just use the macros of
173 HFILE WINAPI LZInit( HFILE hfSrc )
176 struct lzfileheader head;
181 TRACE("(%d)\n",hfSrc);
182 ret=read_header(hfSrc,&head);
184 _llseek(hfSrc,0,SEEK_SET);
185 return ret?ret:hfSrc;
187 for (i = 0; i < MAX_LZSTATES; i++) if (!lzstates[i]) break;
188 if (i == MAX_LZSTATES) return LZERROR_GLOBALLOC;
189 lzstates[i] = lzs = HeapAlloc( GetProcessHeap(), 0, sizeof(struct lzstate) );
190 if(lzs == NULL) return LZERROR_GLOBALLOC;
192 memset(lzs,'\0',sizeof(*lzs));
194 lzs->lastchar = head.lastchar;
195 lzs->reallength = head.reallength;
197 lzs->get = HeapAlloc( GetProcessHeap(), 0, GETLEN );
201 if(lzs->get == NULL) {
202 HeapFree(GetProcessHeap(), 0, lzs);
204 return LZERROR_GLOBALLOC;
207 /* Yes, preinitialize with spaces */
208 memset(lzs->table,' ',0x1000);
209 /* Yes, start 16 byte from the END of the table */
210 lzs->curtabent = 0xff0;
215 /***********************************************************************
216 * LZDone (LZEXPAND.9) (LZ32.8)
218 void WINAPI LZDone(void)
224 /***********************************************************************
225 * GetExpandedName16 (LZEXPAND.10)
227 INT16 WINAPI GetExpandedName16( LPCSTR in, LPSTR out )
229 return (INT16)GetExpandedNameA( in, out );
233 /***********************************************************************
234 * GetExpandedNameA (LZ32.9)
236 * gets the full filename of the compressed file 'in' by opening it
237 * and reading the header
239 * "file." is being translated to "file"
240 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
241 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
244 INT WINAPI GetExpandedNameA( LPCSTR in, LPSTR out )
246 struct lzfileheader head;
249 INT fnislowercased,ret,len;
253 fd=OpenFile(in,&ofs,OF_READ);
255 return (INT)(INT16)LZERROR_BADINHANDLE;
257 ret=read_header(fd,&head);
259 /* not a LZ compressed file, so the expanded name is the same
260 * as the input name */
266 /* look for directory prefix and skip it. */
268 while (NULL!=(t=strpbrk(s,"/\\:")))
271 /* now mangle the basename */
273 /* FIXME: hmm. shouldn't happen? */
274 WARN("Specified a directory or what? (%s)\n",in);
278 /* see if we should use lowercase or uppercase on the last char */
286 fnislowercased=islower(*t);
289 if (isalpha(head.lastchar)) {
291 head.lastchar=tolower(head.lastchar);
293 head.lastchar=toupper(head.lastchar);
296 /* now look where to replace the last character */
297 if (NULL!=(t=strchr(s,'.'))) {
303 t[len]=head.lastchar;
305 } /* else no modification necessary */
311 /***********************************************************************
312 * GetExpandedNameW (LZ32.11)
314 INT WINAPI GetExpandedNameW( LPCWSTR in, LPWSTR out )
319 xout = HeapAlloc( GetProcessHeap(), 0, lstrlenW(in)+3 );
320 xin = HEAP_strdupWtoA( GetProcessHeap(), 0, in );
321 ret = GetExpandedName16(xin,xout);
322 if (ret>0) lstrcpyAtoW(out,xout);
323 HeapFree( GetProcessHeap(), 0, xin );
324 HeapFree( GetProcessHeap(), 0, xout );
329 /***********************************************************************
330 * LZRead16 (LZEXPAND.5)
332 INT16 WINAPI LZRead16( HFILE16 fd, LPVOID buf, UINT16 toread )
334 if (IS_LZ_HANDLE(fd)) return LZRead( fd, buf, toread );
335 return _lread( FILE_GetHandle(fd), buf, toread );
339 /***********************************************************************
342 INT WINAPI LZRead( HFILE fd, LPVOID vbuf, UINT toread )
349 TRACE("(%d,%p,%d)\n",fd,buf,toread);
351 if (!(lzs = GET_LZ_STATE(fd))) return _lread(fd,buf,toread);
353 /* The decompressor itself is in a define, cause we need it twice
354 * in this function. (the decompressed byte will be in b)
356 #define DECOMPRESS_ONE_BYTE \
357 if (lzs->stringlen) { \
358 b = lzs->table[lzs->stringpos]; \
359 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
362 if (!(lzs->bytetype&0x100)) { \
364 return toread-howmuch; \
365 lzs->bytetype = b|0xFF00; \
367 if (lzs->bytetype & 1) { \
369 return toread-howmuch; \
373 if (1!=GET(lzs,b1)) \
374 return toread-howmuch; \
375 if (1!=GET(lzs,b2)) \
376 return toread-howmuch; \
380 * where CAB is the stringoffset in the table\
381 * and D+3 is the len of the string \
383 lzs->stringpos = b1|((b2&0xf0)<<4); \
384 lzs->stringlen = (b2&0xf)+2; \
385 /* 3, but we use a byte already below ... */\
386 b = lzs->table[lzs->stringpos];\
387 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
391 /* store b in table */ \
392 lzs->table[lzs->curtabent++]= b; \
393 lzs->curtabent &= 0xFFF; \
396 /* if someone has seeked, we have to bring the decompressor
399 if (lzs->realcurrent!=lzs->realwanted) {
400 /* if the wanted position is before the current position
401 * I see no easy way to unroll ... We have to restart at
402 * the beginning. *sigh*
404 if (lzs->realcurrent>lzs->realwanted) {
405 /* flush decompressor state */
406 _llseek(lzs->realfd,14,SEEK_SET);
411 memset(lzs->table,' ',0x1000);
412 lzs->curtabent = 0xFF0;
414 while (lzs->realcurrent<lzs->realwanted) {
426 #undef DECOMPRESS_ONE_BYTE
430 /***********************************************************************
431 * LZSeek16 (LZEXPAND.4)
433 LONG WINAPI LZSeek16( HFILE16 fd, LONG off, INT16 type )
435 if (IS_LZ_HANDLE(fd)) return LZSeek( fd, off, type );
436 return _llseek( FILE_GetHandle(fd), off, type );
440 /***********************************************************************
443 LONG WINAPI LZSeek( HFILE fd, LONG off, INT type )
448 TRACE("(%d,%ld,%d)\n",fd,off,type);
449 /* not compressed? just use normal _llseek() */
450 if (!(lzs = GET_LZ_STATE(fd))) return _llseek(fd,off,type);
451 newwanted = lzs->realwanted;
453 case 1: /* SEEK_CUR */
456 case 2: /* SEEK_END */
457 newwanted = lzs->reallength-off;
459 default:/* SEEK_SET */
463 if (newwanted>lzs->reallength)
464 return LZERROR_BADVALUE;
466 return LZERROR_BADVALUE;
467 lzs->realwanted = newwanted;
472 /***********************************************************************
473 * LZCopy16 (LZEXPAND.1)
476 LONG WINAPI LZCopy16( HFILE16 src, HFILE16 dest )
478 /* already a LZ handle? */
479 if (IS_LZ_HANDLE(src)) return LZCopy( src, FILE_GetHandle(dest) );
481 /* no, try to open one */
483 if ((INT16)src <= 0) return 0;
484 if (IS_LZ_HANDLE(src))
486 LONG ret = LZCopy( src, FILE_GetHandle(dest) );
490 /* it was not a compressed file */
491 return LZCopy( FILE_GetHandle(src), FILE_GetHandle(dest) );
495 /***********************************************************************
498 * Copies everything from src to dest
499 * if src is a LZ compressed file, it will be uncompressed.
500 * will return the number of bytes written to dest or errors.
502 LONG WINAPI LZCopy( HFILE src, HFILE dest )
504 int usedlzinit=0,ret,wret;
509 /* we need that weird typedef, for i can't seem to get function pointer
510 * casts right. (Or they probably just do not like WINAPI in general)
512 typedef UINT WINAPI (*_readfun)(HFILE,LPVOID,UINT);
516 TRACE("(%d,%d)\n",src,dest);
517 if (!IS_LZ_HANDLE(src)) {
519 if ((INT)src <= 0) return 0;
520 if (src != oldsrc) usedlzinit=1;
523 /* not compressed? just copy */
524 if (!IS_LZ_HANDLE(src))
527 xread=(_readfun)LZRead;
530 ret=xread(src,buf,BUFLEN);
539 wret = _lwrite(dest,buf,ret);
541 return LZERROR_WRITE;
549 /* reverses GetExpandedPathname */
550 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
553 char *mfn = (char *)HeapAlloc( GetProcessHeap(), 0,
554 strlen(fn) + 3 ); /* "._" and \0 */
555 if(mfn == NULL) return NULL;
557 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
558 if ((p = strchr( p, '.' )))
561 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
562 else p[strlen(p)-1] = '_'; /* replace last character */
564 else strcat( mfn, "._" ); /* append "._" */
569 /***********************************************************************
570 * LZOpenFile16 (LZEXPAND.2)
572 HFILE16 WINAPI LZOpenFile16( LPCSTR fn, LPOFSTRUCT ofs, UINT16 mode )
574 HFILE hfret = LZOpenFileA( fn, ofs, mode );
575 /* return errors and LZ handles unmodified */
576 if ((INT)hfret <= 0) return hfret;
577 if (IS_LZ_HANDLE(hfret)) return hfret;
578 /* but allocate a dos handle for 'normal' files */
579 return FILE_AllocDosHandle(hfret);
583 /***********************************************************************
584 * LZOpenFileA (LZ32.1)
586 * Opens a file. If not compressed, open it as a normal file.
588 HFILE WINAPI LZOpenFileA( LPCSTR fn, LPOFSTRUCT ofs, UINT mode )
592 TRACE("(%s,%p,%d)\n",fn,ofs,mode);
593 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
594 fd=OpenFile(fn,ofs,mode);
597 LPSTR mfn = LZEXPAND_MangleName(fn);
598 fd = OpenFile(mfn,ofs,mode);
599 HeapFree( GetProcessHeap(), 0, mfn );
601 if ((mode&~0x70)!=OF_READ)
606 if ((INT)cfd <= 0) return fd;
611 /***********************************************************************
612 * LZOpenFileW (LZ32.10)
614 HFILE WINAPI LZOpenFileW( LPCWSTR fn, LPOFSTRUCT ofs, UINT mode )
620 xfn = HEAP_strdupWtoA( GetProcessHeap(), 0, fn);
621 ret = LZOpenFile16(xfn,ofs,mode);
622 HeapFree( GetProcessHeap(), 0, xfn );
623 if (ret!=HFILE_ERROR) {
624 /* ofs->szPathName is an array with the OFSTRUCT */
625 yfn = HEAP_strdupAtoW( GetProcessHeap(), 0, ofs->szPathName );
626 memcpy(ofs->szPathName,yfn,lstrlenW(yfn)*2+2);
627 HeapFree( GetProcessHeap(), 0, yfn );
633 /***********************************************************************
634 * LZClose16 (LZEXPAND.6)
636 void WINAPI LZClose16( HFILE16 fd )
638 if (IS_LZ_HANDLE(fd)) LZClose( fd );
639 else _lclose16( fd );
643 /***********************************************************************
646 void WINAPI LZClose( HFILE fd )
651 if (!(lzs = GET_LZ_STATE(fd))) _lclose(fd);
654 if (lzs->get) HeapFree( GetProcessHeap(), 0, lzs->get );
655 CloseHandle(lzs->realfd);
656 lzstates[fd - 0x400] = NULL;
657 HeapFree( GetProcessHeap(), 0, lzs );
661 /***********************************************************************
662 * CopyLZFile16 (LZEXPAND.8)
664 LONG WINAPI CopyLZFile16( HFILE16 src, HFILE16 dest )
666 TRACE("(%d,%d)\n",src,dest);
667 return LZCopy16(src,dest);
671 /***********************************************************************
672 * CopyLZFile (LZ32.7)
674 * Copy src to dest (including uncompressing src).
675 * NOTE: Yes. This is exactly the same function as LZCopy.
677 LONG WINAPI CopyLZFile( HFILE src, HFILE dest )
679 TRACE("(%d,%d)\n",src,dest);
680 return LZCopy(src,dest);