3 * LZ Decompression functions
5 * Copyright 1996 Marcus Meissner
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * FIXME: return values might be wrong
28 #include <kernel32/kernel32.h>
31 #include <sys/types.h>
40 #define HFILE_ERROR ((HFILE)-1)
42 /* The readahead length of the decompressor. Reading single bytes
43 * using _hread() would be SLOW.
47 /* Format of first 14 byte of LZ compressed file */
54 static BYTE LZMagic[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
57 HFILE realfd; /* the real filedescriptor */
58 CHAR lastchar; /* the last char of the filename */
60 DWORD reallength; /* the decompressed length of the file */
61 DWORD realcurrent; /* the position the decompressor currently is */
62 DWORD realwanted; /* the position the user wants to read from */
64 BYTE table[0x1000]; /* the rotating LZ table */
65 UINT curtabent; /* CURrent TABle ENTry */
67 BYTE stringlen; /* length and position of current string */
68 DWORD stringpos; /* from stringtable */
71 WORD bytetype; /* bitmask within blocks */
73 BYTE *get; /* GETLEN bytes */
74 DWORD getcur; /* current read */
75 DWORD getlen; /* length last got */
78 #define MAX_LZSTATES 16
79 static struct lzstate *lzstates[MAX_LZSTATES];
81 #define IS_LZ_HANDLE(h) (((h) >= 0x400) && ((h) < 0x400+MAX_LZSTATES))
82 #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)-0x400] : NULL)
84 /* reads one compressed byte, including buffering */
85 #define GET(lzs,b) _lzget(lzs,&b)
86 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
89 _lzget(struct lzstate *lzs,BYTE *b) {
90 if (lzs->getcur<lzs->getlen) {
91 *b = lzs->get[lzs->getcur++];
94 int ret = _hread(lzs->realfd,lzs->get,GETLEN);
105 /* internal function, reads lzheader
106 * returns BADINHANDLE for non filedescriptors
107 * return 0 for file not compressed using LZ
108 * return UNKNOWNALG for unknown algorithm
109 * returns lzfileheader in *head
111 static INT read_header(HFILE fd,struct lzfileheader *head)
115 if (_llseek(fd,0,SEEK_SET)==-1)
116 return LZERROR_BADINHANDLE;
118 /* We can't directly read the lzfileheader struct due to
119 * structure element alignment
121 if (_hread(fd,buf,14)<14)
123 memcpy(head->magic,buf,8);
124 memcpy(&(head->compressiontype),buf+8,1);
125 memcpy(&(head->lastchar),buf+9,1);
127 /* FIXME: consider endianess on non-intel architectures */
128 memcpy(&(head->reallength),buf+10,4);
130 if (memcmp(head->magic,LZMagic,8))
132 if (head->compressiontype!='A')
133 return LZERROR_UNKNOWNALG;
138 /***********************************************************************
143 INT WINAPI LZStart(void)
150 /***********************************************************************
153 * initializes internal decompression buffers, returns lzfiledescriptor.
154 * (return value the same as hfSrc, if hfSrc is not compressed)
155 * on failure, returns error code <0
156 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
158 * since _llseek uses the same types as libc.lseek, we just use the macros of
163 HFILE WINAPI LZInit( HFILE hfSrc )
166 struct lzfileheader head;
171 DPRINT("(%d)\n",hfSrc);
172 ret=read_header(hfSrc,&head);
174 _llseek(hfSrc,0,SEEK_SET);
175 return ret?ret:hfSrc;
177 for (i = 0; i < MAX_LZSTATES; i++) if (!lzstates[i]) break;
178 if (i == MAX_LZSTATES) return LZERROR_GLOBALLOC;
179 lzstates[i] = lzs = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(struct lzstate) );
180 if(lzs == NULL) return LZERROR_GLOBALLOC;
182 memset(lzs,'\0',sizeof(*lzs));
184 lzs->lastchar = head.lastchar;
185 lzs->reallength = head.reallength;
187 lzs->get = RtlAllocateHeap( GetProcessHeap(), 0, GETLEN );
191 if(lzs->get == NULL) {
192 RtlFreeHeap(GetProcessHeap(), 0, lzs);
194 return LZERROR_GLOBALLOC;
197 /* Yes, preinitialize with spaces */
198 memset(lzs->table,' ',0x1000);
199 /* Yes, start 16 byte from the END of the table */
200 lzs->curtabent = 0xff0;
205 /***********************************************************************
206 * LZDone (LZEXPAND.9)
211 void WINAPI LZDone(void)
217 /***********************************************************************
218 * GetExpandedNameA (LZ32.@)
220 * gets the full filename of the compressed file 'in' by opening it
221 * and reading the header
223 * "file." is being translated to "file"
224 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
225 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
230 INT WINAPI GetExpandedNameA( LPSTR in, LPSTR out )
232 struct lzfileheader head;
235 INT fnislowercased,ret,len;
239 fd=OpenFile(in,&ofs,OF_READ);
241 return (INT)(INT16)LZERROR_BADINHANDLE;
243 ret=read_header(fd,&head);
245 /* not a LZ compressed file, so the expanded name is the same
246 * as the input name */
252 /* look for directory prefix and skip it. */
254 while (NULL!=(t=strpbrk(s,"/\\:")))
257 /* now mangle the basename */
259 /* FIXME: hmm. shouldn't happen? */
260 DPRINT("Specified a directory or what? (%s)\n",in);
264 /* see if we should use lowercase or uppercase on the last char */
272 fnislowercased=islower(*t);
275 if (isalpha(head.lastchar)) {
277 head.lastchar=tolower(head.lastchar);
279 head.lastchar=toupper(head.lastchar);
282 /* now look where to replace the last character */
283 if (NULL!=(t=strchr(s,'.'))) {
289 t[len]=head.lastchar;
291 } /* else no modification necessary */
297 /***********************************************************************
298 * GetExpandedNameW (LZ32.@)
302 INT WINAPI GetExpandedNameW( LPWSTR in, LPWSTR out )
305 DWORD len = WideCharToMultiByte( CP_ACP, 0, in, -1, NULL, 0, NULL, NULL );
306 char *xin = RtlAllocateHeap( GetProcessHeap(), 0, len );
307 char *xout = RtlAllocateHeap( GetProcessHeap(), 0, len+3 );
308 WideCharToMultiByte( CP_ACP, 0, in, -1, xin, len, NULL, NULL );
309 if ((ret = GetExpandedNameA( xin, xout )) > 0)
310 MultiByteToWideChar( CP_ACP, 0, xout, -1, out, wcslen(in)+4 );
311 RtlFreeHeap( GetProcessHeap(), 0, xin );
312 RtlFreeHeap( GetProcessHeap(), 0, xout );
317 /***********************************************************************
322 INT WINAPI LZRead( HFILE fd, LPSTR vbuf, INT toread )
329 DPRINT("(%d,%p,%d)\n",fd,buf,toread);
331 if (!(lzs = GET_LZ_STATE(fd))) return _hread(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 /***********************************************************************
415 LONG WINAPI LZSeek( HFILE fd, LONG off, INT type )
420 DPRINT("(%d,%ld,%d)\n",fd,off,type);
421 /* not compressed? just use normal _llseek() */
422 if (!(lzs = GET_LZ_STATE(fd))) return _llseek(fd,off,type);
423 newwanted = lzs->realwanted;
425 case 1: /* SEEK_CUR */
428 case 2: /* SEEK_END */
429 newwanted = lzs->reallength-off;
431 default:/* SEEK_SET */
435 if (newwanted>lzs->reallength)
436 return LZERROR_BADVALUE;
438 return LZERROR_BADVALUE;
439 lzs->realwanted = newwanted;
444 /***********************************************************************
447 * Copies everything from src to dest
448 * if src is a LZ compressed file, it will be uncompressed.
449 * will return the number of bytes written to dest or errors.
453 LONG WINAPI LZCopy( HFILE src, HFILE dest )
455 int usedlzinit=0,ret,wret;
457 HFILE oldsrc = src, srcfd;
463 /* we need that weird typedef, for i can't seem to get function pointer
464 * casts right. (Or they probably just do not like WINAPI in general)
466 typedef UINT (WINAPI *_readfun)(HFILE,LPVOID,UINT);
470 DPRINT("(%d,%d)\n",src,dest);
471 if (!IS_LZ_HANDLE(src)) {
473 if ((INT)src <= 0) return 0;
474 if (src != oldsrc) usedlzinit=1;
477 /* not compressed? just copy */
478 if (!IS_LZ_HANDLE(src))
479 xread=(_readfun)_hread;
481 xread=(_readfun)LZRead;
484 ret=xread(src,buf,BUFLEN);
493 wret = _hwrite(dest,buf,ret);
495 return LZERROR_WRITE;
498 /* Maintain the timestamp of source file to destination file */
499 srcfd = (!(lzs = GET_LZ_STATE(src))) ? src : lzs->realfd;
500 GetFileTime((HANDLE)srcfd, NULL, NULL, &filetime);
501 SetFileTime((HANDLE)dest, NULL, NULL, &filetime);
510 /* reverses GetExpandedPathname */
511 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
514 char *mfn = (char *)RtlAllocateHeap( GetProcessHeap(), 0,
515 strlen(fn) + 3 ); /* "._" and \0 */
516 if(mfn == NULL) return NULL;
518 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
519 if ((p = strchr( p, '.' )))
522 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
523 else p[strlen(p)-1] = '_'; /* replace last character */
525 else strcat( mfn, "._" ); /* append "._" */
529 /***********************************************************************
530 * LZOpenFileA (LZ32.@)
532 * Opens a file. If not compressed, open it as a normal file.
536 HFILE WINAPI LZOpenFileA( LPSTR fn, LPOFSTRUCT ofs, WORD mode )
540 DPRINT("(%s,%p,%d)\n",fn,ofs,mode);
541 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
542 fd=OpenFile(fn,ofs,mode);
545 LPSTR mfn = LZEXPAND_MangleName(fn);
546 fd = OpenFile(mfn,ofs,mode);
547 RtlFreeHeap( GetProcessHeap(), 0, mfn );
549 if ((mode&~0x70)!=OF_READ)
554 if ((INT)cfd <= 0) return fd;
559 /***********************************************************************
560 * LZOpenFileW (LZ32.@)
564 HFILE WINAPI LZOpenFileW( LPWSTR fn, LPOFSTRUCT ofs, WORD mode )
567 DWORD len = WideCharToMultiByte( CP_ACP, 0, fn, -1, NULL, 0, NULL, NULL );
568 LPSTR xfn = RtlAllocateHeap( GetProcessHeap(), 0, len );
569 WideCharToMultiByte( CP_ACP, 0, fn, -1, xfn, len, NULL, NULL );
570 ret = LZOpenFileA(xfn,ofs,mode);
571 RtlFreeHeap( GetProcessHeap(), 0, xfn );
576 /***********************************************************************
581 void WINAPI LZClose( HFILE fd )
586 if (!(lzs = GET_LZ_STATE(fd))) _lclose(fd);
589 if (lzs->get) RtlFreeHeap( GetProcessHeap(), 0, lzs->get );
590 CloseHandle((HANDLE)lzs->realfd);
591 lzstates[fd - 0x400] = NULL;
592 RtlFreeHeap( GetProcessHeap(), 0, lzs );
597 /***********************************************************************
598 * CopyLZFile (LZ32.@)
600 * Copy src to dest (including uncompressing src).
601 * NOTE: Yes. This is exactly the same function as LZCopy.
605 LONG WINAPI CopyLZFile( HFILE src, HFILE dest )
607 DPRINT("(%d,%d)\n",src,dest);
608 return LZCopy(src,dest);