Merge documentation/status/lzexpand into the lzexpand source.
[wine/hacks.git] / dlls / lzexpand / lzexpand_main.c
blob0eb39e6822774f7e2126809ef7a13c7ed7f83859
1 /*
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
20 * NOTES
22 * The LZ (Lempel Ziv) decompression was used in win16 installation programs.
23 * It is a simple tabledriven decompression engine, the algorithm is not
24 * documented as far as I know. WINE does not contain a compressor for
25 * this format.
27 * The implementation is complete and there have been no reports of failures
28 * for some time.
30 * TODO:
32 * o Check whether the return values are correct
36 #include "config.h"
38 #include <string.h>
39 #include <ctype.h>
40 #include <sys/types.h>
41 #include <stdarg.h>
42 #include <stdio.h>
43 #ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif
47 #include "windef.h"
48 #include "winbase.h"
49 #include "lzexpand.h"
51 #include "wine/unicode.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(file);
56 /* The readahead length of the decompressor. Reading single bytes
57 * using _lread() would be SLOW.
59 #define GETLEN 2048
61 /* Format of first 14 byte of LZ compressed file */
62 struct lzfileheader {
63 BYTE magic[8];
64 BYTE compressiontype;
65 CHAR lastchar;
66 DWORD reallength;
68 static BYTE LZMagic[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
70 struct lzstate {
71 HFILE realfd; /* the real filedescriptor */
72 CHAR lastchar; /* the last char of the filename */
74 DWORD reallength; /* the decompressed length of the file */
75 DWORD realcurrent; /* the position the decompressor currently is */
76 DWORD realwanted; /* the position the user wants to read from */
78 BYTE table[0x1000]; /* the rotating LZ table */
79 UINT curtabent; /* CURrent TABle ENTry */
81 BYTE stringlen; /* length and position of current string */
82 DWORD stringpos; /* from stringtable */
85 WORD bytetype; /* bitmask within blocks */
87 BYTE *get; /* GETLEN bytes */
88 DWORD getcur; /* current read */
89 DWORD getlen; /* length last got */
92 #define MAX_LZSTATES 16
93 static struct lzstate *lzstates[MAX_LZSTATES];
95 #define IS_LZ_HANDLE(h) (((h) >= 0x400) && ((h) < 0x400+MAX_LZSTATES))
96 #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)-0x400] : NULL)
98 /* reads one compressed byte, including buffering */
99 #define GET(lzs,b) _lzget(lzs,&b)
100 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
102 static int
103 _lzget(struct lzstate *lzs,BYTE *b) {
104 if (lzs->getcur<lzs->getlen) {
105 *b = lzs->get[lzs->getcur++];
106 return 1;
107 } else {
108 int ret = _lread(lzs->realfd,lzs->get,GETLEN);
109 if (ret==HFILE_ERROR)
110 return HFILE_ERROR;
111 if (ret==0)
112 return 0;
113 lzs->getlen = ret;
114 lzs->getcur = 1;
115 *b = *(lzs->get);
116 return 1;
119 /* internal function, reads lzheader
120 * returns BADINHANDLE for non filedescriptors
121 * return 0 for file not compressed using LZ
122 * return UNKNOWNALG for unknown algorithm
123 * returns lzfileheader in *head
125 static INT read_header(HFILE fd,struct lzfileheader *head)
127 BYTE buf[14];
129 if (_llseek(fd,0,SEEK_SET)==-1)
130 return LZERROR_BADINHANDLE;
132 /* We can't directly read the lzfileheader struct due to
133 * structure element alignment
135 if (_lread(fd,buf,14)<14)
136 return 0;
137 memcpy(head->magic,buf,8);
138 memcpy(&(head->compressiontype),buf+8,1);
139 memcpy(&(head->lastchar),buf+9,1);
141 /* FIXME: consider endianess on non-intel architectures */
142 memcpy(&(head->reallength),buf+10,4);
144 if (memcmp(head->magic,LZMagic,8))
145 return 0;
146 if (head->compressiontype!='A')
147 return LZERROR_UNKNOWNALG;
148 return 1;
152 /***********************************************************************
153 * LZStart (LZ32.@)
155 INT WINAPI LZStart(void)
157 TRACE("(void)\n");
158 return 1;
162 /***********************************************************************
163 * LZInit (LZ32.@)
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
171 * libc
173 HFILE WINAPI LZInit( HFILE hfSrc )
176 struct lzfileheader head;
177 struct lzstate *lzs;
178 DWORD ret;
179 int i;
181 TRACE("(%d)\n",hfSrc);
182 ret=read_header(hfSrc,&head);
183 if (ret<=0) {
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));
193 lzs->realfd = hfSrc;
194 lzs->lastchar = head.lastchar;
195 lzs->reallength = head.reallength;
197 lzs->get = HeapAlloc( GetProcessHeap(), 0, GETLEN );
198 lzs->getlen = 0;
199 lzs->getcur = 0;
201 if(lzs->get == NULL) {
202 HeapFree(GetProcessHeap(), 0, lzs);
203 lzstates[i] = NULL;
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;
211 return 0x400 + i;
215 /***********************************************************************
216 * LZDone (LZEXPAND.9)
217 * LZDone (LZ32.@)
219 void WINAPI LZDone(void)
221 TRACE("(void)\n");
225 /***********************************************************************
226 * GetExpandedNameA (LZ32.@)
228 * gets the full filename of the compressed file 'in' by opening it
229 * and reading the header
231 * "file." is being translated to "file"
232 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
233 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
236 INT WINAPI GetExpandedNameA( LPSTR in, LPSTR out )
238 struct lzfileheader head;
239 HFILE fd;
240 OFSTRUCT ofs;
241 INT fnislowercased,ret,len;
242 LPSTR s,t;
244 TRACE("(%s)\n",in);
245 fd=OpenFile(in,&ofs,OF_READ);
246 if (fd==HFILE_ERROR)
247 return (INT)(INT16)LZERROR_BADINHANDLE;
248 strcpy(out,in);
249 ret=read_header(fd,&head);
250 if (ret<=0) {
251 /* not a LZ compressed file, so the expanded name is the same
252 * as the input name */
253 _lclose(fd);
254 return 1;
258 /* look for directory prefix and skip it. */
259 s=out;
260 while (NULL!=(t=strpbrk(s,"/\\:")))
261 s=t+1;
263 /* now mangle the basename */
264 if (!*s) {
265 /* FIXME: hmm. shouldn't happen? */
266 WARN("Specified a directory or what? (%s)\n",in);
267 _lclose(fd);
268 return 1;
270 /* see if we should use lowercase or uppercase on the last char */
271 fnislowercased=1;
272 t=s+strlen(s)-1;
273 while (t>=out) {
274 if (!isalpha(*t)) {
275 t--;
276 continue;
278 fnislowercased=islower(*t);
279 break;
281 if (isalpha(head.lastchar)) {
282 if (fnislowercased)
283 head.lastchar=tolower(head.lastchar);
284 else
285 head.lastchar=toupper(head.lastchar);
288 /* now look where to replace the last character */
289 if (NULL!=(t=strchr(s,'.'))) {
290 if (t[1]=='\0') {
291 t[0]='\0';
292 } else {
293 len=strlen(t)-1;
294 if (t[len]=='_')
295 t[len]=head.lastchar;
297 } /* else no modification necessary */
298 _lclose(fd);
299 return 1;
303 /***********************************************************************
304 * GetExpandedNameW (LZ32.@)
306 INT WINAPI GetExpandedNameW( LPWSTR in, LPWSTR out )
308 INT ret;
309 DWORD len = WideCharToMultiByte( CP_ACP, 0, in, -1, NULL, 0, NULL, NULL );
310 char *xin = HeapAlloc( GetProcessHeap(), 0, len );
311 char *xout = HeapAlloc( GetProcessHeap(), 0, len+3 );
312 WideCharToMultiByte( CP_ACP, 0, in, -1, xin, len, NULL, NULL );
313 if ((ret = GetExpandedNameA( xin, xout )) > 0)
314 MultiByteToWideChar( CP_ACP, 0, xout, -1, out, strlenW(in)+4 );
315 HeapFree( GetProcessHeap(), 0, xin );
316 HeapFree( GetProcessHeap(), 0, xout );
317 return ret;
321 /***********************************************************************
322 * LZRead (LZ32.@)
324 INT WINAPI LZRead( HFILE fd, LPSTR vbuf, INT toread )
326 int howmuch;
327 BYTE b,*buf;
328 struct lzstate *lzs;
330 buf=(LPBYTE)vbuf;
331 TRACE("(%d,%p,%d)\n",fd,buf,toread);
332 howmuch=toread;
333 if (!(lzs = GET_LZ_STATE(fd))) return _lread(fd,buf,toread);
335 /* The decompressor itself is in a define, cause we need it twice
336 * in this function. (the decompressed byte will be in b)
338 #define DECOMPRESS_ONE_BYTE \
339 if (lzs->stringlen) { \
340 b = lzs->table[lzs->stringpos]; \
341 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
342 lzs->stringlen--; \
343 } else { \
344 if (!(lzs->bytetype&0x100)) { \
345 if (1!=GET(lzs,b)) \
346 return toread-howmuch; \
347 lzs->bytetype = b|0xFF00; \
349 if (lzs->bytetype & 1) { \
350 if (1!=GET(lzs,b)) \
351 return toread-howmuch; \
352 } else { \
353 BYTE b1,b2; \
355 if (1!=GET(lzs,b1)) \
356 return toread-howmuch; \
357 if (1!=GET(lzs,b2)) \
358 return toread-howmuch; \
359 /* Format: \
360 * b1 b2 \
361 * AB CD \
362 * where CAB is the stringoffset in the table\
363 * and D+3 is the len of the string \
364 */ \
365 lzs->stringpos = b1|((b2&0xf0)<<4); \
366 lzs->stringlen = (b2&0xf)+2; \
367 /* 3, but we use a byte already below ... */\
368 b = lzs->table[lzs->stringpos];\
369 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
371 lzs->bytetype>>=1; \
373 /* store b in table */ \
374 lzs->table[lzs->curtabent++]= b; \
375 lzs->curtabent &= 0xFFF; \
376 lzs->realcurrent++;
378 /* if someone has seeked, we have to bring the decompressor
379 * to that position
381 if (lzs->realcurrent!=lzs->realwanted) {
382 /* if the wanted position is before the current position
383 * I see no easy way to unroll ... We have to restart at
384 * the beginning. *sigh*
386 if (lzs->realcurrent>lzs->realwanted) {
387 /* flush decompressor state */
388 _llseek(lzs->realfd,14,SEEK_SET);
389 GET_FLUSH(lzs);
390 lzs->realcurrent= 0;
391 lzs->bytetype = 0;
392 lzs->stringlen = 0;
393 memset(lzs->table,' ',0x1000);
394 lzs->curtabent = 0xFF0;
396 while (lzs->realcurrent<lzs->realwanted) {
397 DECOMPRESS_ONE_BYTE;
401 while (howmuch) {
402 DECOMPRESS_ONE_BYTE;
403 lzs->realwanted++;
404 *buf++ = b;
405 howmuch--;
407 return toread;
408 #undef DECOMPRESS_ONE_BYTE
412 /***********************************************************************
413 * LZSeek (LZ32.@)
415 LONG WINAPI LZSeek( HFILE fd, LONG off, INT type )
417 struct lzstate *lzs;
418 LONG newwanted;
420 TRACE("(%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;
424 switch (type) {
425 case 1: /* SEEK_CUR */
426 newwanted += off;
427 break;
428 case 2: /* SEEK_END */
429 newwanted = lzs->reallength-off;
430 break;
431 default:/* SEEK_SET */
432 newwanted = off;
433 break;
435 if (newwanted>lzs->reallength)
436 return LZERROR_BADVALUE;
437 if (newwanted<0)
438 return LZERROR_BADVALUE;
439 lzs->realwanted = newwanted;
440 return newwanted;
444 /***********************************************************************
445 * LZCopy (LZ32.@)
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.
451 LONG WINAPI LZCopy( HFILE src, HFILE dest )
453 int usedlzinit = 0, ret, wret;
454 LONG len;
455 HFILE oldsrc = src, srcfd;
456 FILETIME filetime;
457 struct lzstate *lzs;
458 #define BUFLEN 1000
459 BYTE buf[BUFLEN];
460 /* we need that weird typedef, for i can't seem to get function pointer
461 * casts right. (Or they probably just do not like WINAPI in general)
463 typedef UINT (WINAPI *_readfun)(HFILE,LPVOID,UINT);
465 _readfun xread;
467 TRACE("(%d,%d)\n",src,dest);
468 if (!IS_LZ_HANDLE(src)) {
469 src = LZInit(src);
470 if ((INT)src <= 0) return 0;
471 if (src != oldsrc) usedlzinit=1;
474 /* not compressed? just copy */
475 if (!IS_LZ_HANDLE(src))
476 xread=_lread;
477 else
478 xread=(_readfun)LZRead;
479 len=0;
480 while (1) {
481 ret=xread(src,buf,BUFLEN);
482 if (ret<=0) {
483 if (ret==0)
484 break;
485 if (ret==-1)
486 return LZERROR_READ;
487 return ret;
489 len += ret;
490 wret = _lwrite(dest,buf,ret);
491 if (wret!=ret)
492 return LZERROR_WRITE;
495 /* Maintain the timestamp of source file to destination file */
496 srcfd = (!(lzs = GET_LZ_STATE(src))) ? src : lzs->realfd;
497 GetFileTime((HANDLE)srcfd, NULL, NULL, &filetime);
498 SetFileTime((HANDLE)dest, NULL, NULL, &filetime);
500 /* close handle */
501 if (usedlzinit)
502 LZClose(src);
503 return len;
504 #undef BUFLEN
507 /* reverses GetExpandedPathname */
508 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
510 char *p;
511 char *mfn = (char *)HeapAlloc( GetProcessHeap(), 0,
512 strlen(fn) + 3 ); /* "._" and \0 */
513 if(mfn == NULL) return NULL;
514 strcpy( mfn, fn );
515 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
516 if ((p = strchr( p, '.' )))
518 p++;
519 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
520 else p[strlen(p)-1] = '_'; /* replace last character */
522 else strcat( mfn, "._" ); /* append "._" */
523 return mfn;
527 /***********************************************************************
528 * LZOpenFileA (LZ32.@)
530 * Opens a file. If not compressed, open it as a normal file.
532 HFILE WINAPI LZOpenFileA( LPSTR fn, LPOFSTRUCT ofs, WORD mode )
534 HFILE fd,cfd;
536 TRACE("(%s,%p,%d)\n",fn,ofs,mode);
537 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
538 fd=OpenFile(fn,ofs,mode);
539 if (fd==HFILE_ERROR)
541 LPSTR mfn = LZEXPAND_MangleName(fn);
542 fd = OpenFile(mfn,ofs,mode);
543 HeapFree( GetProcessHeap(), 0, mfn );
545 if ((mode&~0x70)!=OF_READ)
546 return fd;
547 if (fd==HFILE_ERROR)
548 return HFILE_ERROR;
549 cfd=LZInit(fd);
550 if ((INT)cfd <= 0) return fd;
551 return cfd;
555 /***********************************************************************
556 * LZOpenFileW (LZ32.@)
558 HFILE WINAPI LZOpenFileW( LPWSTR fn, LPOFSTRUCT ofs, WORD mode )
560 HFILE ret;
561 DWORD len = WideCharToMultiByte( CP_ACP, 0, fn, -1, NULL, 0, NULL, NULL );
562 LPSTR xfn = HeapAlloc( GetProcessHeap(), 0, len );
563 WideCharToMultiByte( CP_ACP, 0, fn, -1, xfn, len, NULL, NULL );
564 ret = LZOpenFileA(xfn,ofs,mode);
565 HeapFree( GetProcessHeap(), 0, xfn );
566 return ret;
570 /***********************************************************************
571 * LZClose (LZ32.@)
573 void WINAPI LZClose( HFILE fd )
575 struct lzstate *lzs;
577 TRACE("(%d)\n",fd);
578 if (!(lzs = GET_LZ_STATE(fd))) _lclose(fd);
579 else
581 if (lzs->get) HeapFree( GetProcessHeap(), 0, lzs->get );
582 CloseHandle((HANDLE)lzs->realfd);
583 lzstates[fd - 0x400] = NULL;
584 HeapFree( GetProcessHeap(), 0, lzs );
589 /***********************************************************************
590 * CopyLZFile (LZ32.@)
592 * Copy src to dest (including uncompressing src).
593 * NOTE: Yes. This is exactly the same function as LZCopy.
595 LONG WINAPI CopyLZFile( HFILE src, HFILE dest )
597 TRACE("(%d,%d)\n",src,dest);
598 return LZCopy(src,dest);