Release 970329
[wine/multimedia.git] / misc / lzexpand.c
blob48d67e469edf90835e87ccb8c29de589b0f5a172
1 /*
2 * LZ Decompression functions
4 * Copyright 1996 Marcus Meissner
5 */
6 /*
7 * FIXME: return values might be wrong
8 */
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <ctype.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include "windows.h"
18 #include "file.h"
19 #include "heap.h"
20 #include "ldt.h"
21 #include "lzexpand.h"
22 #include "stddebug.h"
23 #include "debug.h"
24 #include "xmalloc.h"
27 /* The readahead length of the decompressor. Reading single bytes
28 * using _lread() would be SLOW.
30 #define GETLEN 2048
32 /* Format of first 14 byte of LZ compressed file */
33 struct lzfileheader {
34 BYTE magic[8];
35 BYTE compressiontype;
36 CHAR lastchar;
37 DWORD reallength;
39 static BYTE LZMagic[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
41 static struct lzstate {
42 HFILE32 lzfd; /* the handle used by the program */
43 HFILE32 realfd; /* the real filedescriptor */
44 CHAR lastchar; /* the last char of the filename */
46 DWORD reallength; /* the decompressed length of the file */
47 DWORD realcurrent; /* the position the decompressor currently is */
48 DWORD realwanted; /* the position the user wants to read from */
50 BYTE table[0x1000]; /* the rotating LZ table */
51 UINT32 curtabent; /* CURrent TABle ENTry */
53 BYTE stringlen; /* length and position of current string */
54 DWORD stringpos; /* from stringtable */
57 WORD bytetype; /* bitmask within blocks */
59 BYTE *get; /* GETLEN bytes */
60 DWORD getcur; /* current read */
61 DWORD getlen; /* length last got */
62 } *lzstates=NULL;
63 static int nroflzstates=0;
65 /* reads one compressed byte, including buffering */
66 #define GET(lzs,b) _lzget(lzs,&b)
67 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
69 static int
70 _lzget(struct lzstate *lzs,BYTE *b) {
71 if (lzs->getcur<lzs->getlen) {
72 *b = lzs->get[lzs->getcur++];
73 return 1;
74 } else {
75 int ret = _lread32(lzs->realfd,lzs->get,GETLEN);
76 if (ret==HFILE_ERROR32)
77 return HFILE_ERROR32;
78 if (ret==0)
79 return 0;
80 lzs->getlen = ret;
81 lzs->getcur = 1;
82 *b = *(lzs->get);
83 return 1;
86 /* internal function, reads lzheader
87 * returns BADINHANDLE for non filedescriptors
88 * return 0 for file not compressed using LZ
89 * return UNKNOWNALG for unknown algorithm
90 * returns lzfileheader in *head
92 static INT32 read_header(HFILE32 fd,struct lzfileheader *head)
94 BYTE buf[14];
96 if (_llseek32(fd,0,SEEK_SET)==-1)
97 return LZERROR_BADINHANDLE;
99 /* We can't directly read the lzfileheader struct due to
100 * structure element alignment
102 if (_lread32(fd,buf,14)<14)
103 return 0;
104 memcpy(head->magic,buf,8);
105 memcpy(&(head->compressiontype),buf+8,1);
106 memcpy(&(head->lastchar),buf+9,1);
108 /* FIXME: consider endianess on non-intel architectures */
109 memcpy(&(head->reallength),buf+10,4);
111 if (memcmp(head->magic,LZMagic,8))
112 return 0;
113 if (head->compressiontype!='A')
114 return LZERROR_UNKNOWNALG;
115 return 1;
118 /***********************************************************************
119 * LZStart16 (LZEXPAND.7)
121 INT16 LZStart16(void)
123 dprintf_file(stddeb,"LZStart16(void)\n");
124 return 1;
128 /***********************************************************************
129 * LZStart32 (LZ32.6)
131 INT32 LZStart32(void)
133 dprintf_file(stddeb,"LZStart32(void)\n");
134 return 1;
138 /***********************************************************************
139 * LZInit16 (LZEXPAND.3)
141 HFILE16 LZInit16( HFILE16 hfSrc )
143 return LZInit32( hfSrc );
147 /***********************************************************************
148 * LZInit32 (LZ32.2)
150 * initializes internal decompression buffers, returns lzfiledescriptor.
151 * (return value the same as hfSrc, if hfSrc is not compressed)
152 * on failure, returns error code <0
153 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
154 * we use as much as we need, we just OR 0x400 to the passed HFILE.
156 * since _llseek uses the same types as libc.lseek, we just use the macros of
157 * libc
159 HFILE32 LZInit32( HFILE32 hfSrc )
162 struct lzfileheader head;
163 struct lzstate *lzs;
164 DWORD ret;
166 dprintf_file(stddeb,"LZInit(%d)\n",hfSrc);
167 ret=read_header(hfSrc,&head);
168 if (ret<=0) {
169 _llseek32(hfSrc,0,SEEK_SET);
170 return ret?ret:hfSrc;
172 lzstates=xrealloc(lzstates,(++nroflzstates)*sizeof(struct lzstate));
173 lzs = lzstates+(nroflzstates-1);
175 memset(lzs,'\0',sizeof(*lzs));
176 lzs->realfd = hfSrc;
177 lzs->lzfd = hfSrc | 0x400;
178 lzs->lastchar = head.lastchar;
179 lzs->reallength = head.reallength;
181 lzs->get = xmalloc(GETLEN);
182 lzs->getlen = 0;
183 lzs->getcur = 0;
185 /* Yes, preinitialize with spaces */
186 memset(lzs->table,' ',0x1000);
187 /* Yes, start 16 byte from the END of the table */
188 lzs->curtabent = 0xff0;
189 return lzs->lzfd;
193 /***********************************************************************
194 * LZDone (LZEXPAND.9) (LZ32.8)
196 void LZDone(void)
198 dprintf_file(stddeb,"LZDone()\n");
202 /***********************************************************************
203 * GetExpandedName16 (LZEXPAND.10)
205 INT16 GetExpandedName16( LPCSTR in, LPSTR out )
207 return GetExpandedName32A( in, out );
211 /***********************************************************************
212 * GetExpandedName32A (LZ32.9)
214 * gets the full filename of the compressed file 'in' by opening it
215 * and reading the header
217 * "file." is being translated to "file"
218 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
219 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
222 INT32 GetExpandedName32A( LPCSTR in, LPSTR out )
224 struct lzfileheader head;
225 HFILE32 fd;
226 OFSTRUCT ofs;
227 INT32 fnislowercased,ret,len;
228 LPSTR s,t;
230 dprintf_file(stddeb,"GetExpandedName(%s)\n",in);
231 fd=OpenFile32(in,&ofs,OF_READ);
232 if (fd==HFILE_ERROR32)
233 return LZERROR_BADINHANDLE;
234 ret=read_header(fd,&head);
235 if (ret<=0) {
236 _lclose32(fd);
237 return LZERROR_BADINHANDLE;
240 /* This line will crash if the caller hasn't allocated enough memory
241 * for us.
243 strcpy(out,in);
245 /* look for directory prefix and skip it. */
246 s=out;
247 while (NULL!=(t=strpbrk(s,"/\\:")))
248 s=t+1;
250 /* now mangle the basename */
251 if (!*s) {
252 /* FIXME: hmm. shouldn't happen? */
253 fprintf(stddeb,__FILE__":GetExpandedFileName(), specified a directory or what? (%s)\n",in);
254 _lclose32(fd);
255 return 1;
257 /* see if we should use lowercase or uppercase on the last char */
258 fnislowercased=1;
259 t=s+strlen(s)-1;
260 while (t>=out) {
261 if (!isalpha(*t)) {
262 t--;
263 continue;
265 fnislowercased=islower(*t);
266 break;
268 if (isalpha(head.lastchar)) {
269 if (fnislowercased)
270 head.lastchar=tolower(head.lastchar);
271 else
272 head.lastchar=toupper(head.lastchar);
275 /* now look where to replace the last character */
276 if (NULL!=(t=strchr(s,'.'))) {
277 if (t[1]=='\0') {
278 t[0]='\0';
279 } else {
280 len=strlen(t)-1;
281 if (t[len]=='_')
282 t[len]=head.lastchar;
284 } /* else no modification necessary */
285 _lclose32(fd);
286 return 1;
290 /***********************************************************************
291 * GetExpandedName32W (LZ32.11)
293 INT32 GetExpandedName32W( LPCWSTR in, LPWSTR out )
295 char *xin,*xout;
296 INT32 ret;
298 xout = HeapAlloc( GetProcessHeap(), 0, lstrlen32W(in)+3 );
299 xin = HEAP_strdupWtoA( GetProcessHeap(), 0, in );
300 ret = GetExpandedName16(xin,xout);
301 if (ret>0) lstrcpyAtoW(out,xout);
302 HeapFree( GetProcessHeap(), 0, xin );
303 HeapFree( GetProcessHeap(), 0, xout );
304 return ret;
308 /***********************************************************************
309 * LZRead16 (LZEXPAND.5)
311 INT16 LZRead16( HFILE16 fd, LPVOID buf, UINT16 toread )
313 return LZRead32(fd,buf,toread);
317 /***********************************************************************
318 * LZRead32 (LZ32.4)
320 INT32 LZRead32( HFILE32 fd, LPVOID vbuf, UINT32 toread )
322 int i,howmuch;
323 BYTE b,*buf;
324 struct lzstate *lzs;
326 buf=(LPBYTE)vbuf;
327 dprintf_file(stddeb,"LZRead32(%d,%p,%d)\n",fd,buf,toread);
328 howmuch=toread;
329 for (i=0;i<nroflzstates;i++)
330 if (lzstates[i].lzfd==fd)
331 break;
332 if (i==nroflzstates)
333 return _lread32(fd,buf,toread);
334 lzs=lzstates+i;
336 /* The decompressor itself is in a define, cause we need it twice
337 * in this function. (the decompressed byte will be in b)
339 #define DECOMPRESS_ONE_BYTE \
340 if (lzs->stringlen) { \
341 b = lzs->table[lzs->stringpos]; \
342 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
343 lzs->stringlen--; \
344 } else { \
345 if (!(lzs->bytetype&0x100)) { \
346 if (1!=GET(lzs,b)) \
347 return toread-howmuch; \
348 lzs->bytetype = b|0xFF00; \
350 if (lzs->bytetype & 1) { \
351 if (1!=GET(lzs,b)) \
352 return toread-howmuch; \
353 } else { \
354 BYTE b1,b2; \
356 if (1!=GET(lzs,b1)) \
357 return toread-howmuch; \
358 if (1!=GET(lzs,b2)) \
359 return toread-howmuch; \
360 /* Format: \
361 * b1 b2 \
362 * AB CD \
363 * where CAB is the stringoffset in the table\
364 * and D+3 is the len of the string \
365 */ \
366 lzs->stringpos = b1|((b2&0xf0)<<4); \
367 lzs->stringlen = (b2&0xf)+2; \
368 /* 3, but we use a byte already below ... */\
369 b = lzs->table[lzs->stringpos];\
370 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
372 lzs->bytetype>>=1; \
374 /* store b in table */ \
375 lzs->table[lzs->curtabent++]= b; \
376 lzs->curtabent &= 0xFFF; \
377 lzs->realcurrent++;
379 /* if someone has seeked, we have to bring the decompressor
380 * to that position
382 if (lzs->realcurrent!=lzs->realwanted) {
383 /* if the wanted position is before the current position
384 * I see no easy way to unroll ... We have to restart at
385 * the beginning. *sigh*
387 if (lzs->realcurrent>lzs->realwanted) {
388 /* flush decompressor state */
389 _llseek32(lzs->realfd,14,SEEK_SET);
390 GET_FLUSH(lzs);
391 lzs->realcurrent= 0;
392 lzs->bytetype = 0;
393 lzs->stringlen = 0;
394 memset(lzs->table,' ',0x1000);
395 lzs->curtabent = 0xFF0;
397 while (lzs->realcurrent<lzs->realwanted) {
398 DECOMPRESS_ONE_BYTE;
402 while (howmuch) {
403 DECOMPRESS_ONE_BYTE;
404 lzs->realwanted++;
405 *buf++ = b;
406 howmuch--;
408 return toread;
409 #undef DECOMPRESS_ONE_BYTE
413 /***********************************************************************
414 * LZSeek16 (LZEXPAND.4)
416 LONG LZSeek16( HFILE16 fd, LONG off, INT16 type )
418 return LZSeek32( fd, off, type );
422 /***********************************************************************
423 * LZSeek32 (LZ32.3)
425 LONG LZSeek32( HFILE32 fd, LONG off, INT32 type )
427 int i;
428 struct lzstate *lzs;
429 LONG newwanted;
431 dprintf_file(stddeb,"LZSeek(%d,%ld,%d)\n",fd,off,type);
432 for (i=0;i<nroflzstates;i++)
433 if (lzstates[i].lzfd==fd)
434 break;
435 /* not compressed? just use normal _llseek() */
436 if (i==nroflzstates)
437 return _llseek32(fd,off,type);
438 lzs = lzstates+i;
439 newwanted = lzs->realwanted;
440 switch (type) {
441 case 1: /* SEEK_CUR */
442 newwanted += off;
443 break;
444 case 2: /* SEEK_END */
445 newwanted = lzs->reallength-off;
446 break;
447 default:/* SEEK_SET */
448 newwanted = off;
449 break;
451 if (newwanted>lzs->reallength)
452 return LZERROR_BADVALUE;
453 if (newwanted<0)
454 return LZERROR_BADVALUE;
455 lzs->realwanted = newwanted;
456 return newwanted;
460 /***********************************************************************
461 * LZCopy16 (LZEXPAND.1)
463 LONG LZCopy16( HFILE16 src, HFILE16 dest )
465 return LZCopy32( src, dest );
469 /***********************************************************************
470 * LZCopy32 (LZ32.0)
472 * Copies everything from src to dest
473 * if src is a LZ compressed file, it will be uncompressed.
474 * will return the number of bytes written to dest or errors.
476 LONG LZCopy32( HFILE32 src, HFILE32 dest )
478 int i,ret,wret;
479 LONG len;
480 #define BUFLEN 1000
481 BYTE buf[BUFLEN];
482 INT32 (*xread)(HFILE32,LPVOID,UINT32);
484 dprintf_file(stddeb,"LZCopy(%d,%d)\n",src,dest);
485 for (i=0;i<nroflzstates;i++)
486 if (src==lzstates[i].lzfd)
487 break;
489 /* not compressed? just copy */
490 if (i==nroflzstates)
491 xread=(INT32(*)(HFILE32,LPVOID,UINT32))_lread32;
492 else
493 xread=LZRead32;
494 len=0;
495 while (1) {
496 ret=xread(src,buf,BUFLEN);
497 if (ret<=0) {
498 if (ret==0)
499 break;
500 if (ret==-1)
501 return LZERROR_READ;
502 return ret;
504 len += ret;
505 wret = _lwrite32(dest,buf,ret);
506 if (wret!=ret)
507 return LZERROR_WRITE;
509 return len;
510 #undef BUFLEN
513 /* reverses GetExpandedPathname */
514 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
516 char *p;
517 char *mfn = (char *)xmalloc( strlen(fn) + 3 ); /* "._" and \0 */
518 strcpy( mfn, fn );
519 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
520 if ((p = strchr( p, '.' )))
522 p++;
523 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
524 else p[strlen(p)-1] = '_'; /* replace last character */
526 else strcat( mfn, "._" ); /* append "._" */
527 return mfn;
531 /***********************************************************************
532 * LZOpenFile16 (LZEXPAND.2)
534 HFILE16 LZOpenFile16( LPCSTR fn, LPOFSTRUCT ofs, UINT16 mode )
536 return LZOpenFile32A( fn, ofs, mode );
540 /***********************************************************************
541 * LZOpenFile32A (LZ32.1)
543 * Opens a file. If not compressed, open it as a normal file.
545 HFILE32 LZOpenFile32A( LPCSTR fn, LPOFSTRUCT ofs, UINT32 mode )
547 HFILE32 fd,cfd;
549 dprintf_file(stddeb,"LZOpenFile(%s,%p,%d)\n",fn,ofs,mode);
550 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
551 fd=OpenFile32(fn,ofs,mode);
552 if (fd==HFILE_ERROR32)
554 LPSTR mfn = LZEXPAND_MangleName(fn);
555 fd = OpenFile32(mfn,ofs,mode);
556 free( mfn );
558 if ((mode&~0x70)!=OF_READ)
559 return fd;
560 if (fd==HFILE_ERROR32)
561 return HFILE_ERROR32;
562 cfd=LZInit32(fd);
563 if (cfd<=0)
564 return fd;
565 return cfd;
569 /***********************************************************************
570 * LZOpenFile32W (LZ32.10)
572 HFILE32 LZOpenFile32W( LPCWSTR fn, LPOFSTRUCT ofs, UINT32 mode )
574 LPSTR xfn;
575 LPWSTR yfn;
576 HFILE32 ret;
578 xfn = HEAP_strdupWtoA( GetProcessHeap(), 0, fn);
579 ret = LZOpenFile16(xfn,ofs,mode);
580 HeapFree( GetProcessHeap(), 0, xfn );
581 if (ret!=HFILE_ERROR32) {
582 /* ofs->szPathName is an array with the OFSTRUCT */
583 yfn = HEAP_strdupAtoW( GetProcessHeap(), 0, ofs->szPathName );
584 memcpy(ofs->szPathName,yfn,lstrlen32W(yfn)*2+2);
585 HeapFree( GetProcessHeap(), 0, yfn );
587 return ret;
591 /***********************************************************************
592 * LZClose16 (LZEXPAND.6)
594 void LZClose16( HFILE16 fd )
596 return LZClose32( fd );
600 /***********************************************************************
601 * LZClose32 (LZ32.5)
603 void LZClose32( HFILE32 fd )
605 int i;
607 dprintf_file(stddeb,"LZClose(%d)\n",fd);
608 for (i=0;i<nroflzstates;i++)
609 if (lzstates[i].lzfd==fd)
610 break;
611 if (i==nroflzstates) {
612 _lclose32(fd);
613 return;
615 if (lzstates[i].get)
616 free(lzstates[i].get);
617 _lclose32(lzstates[i].realfd);
618 memcpy(lzstates+i,lzstates+i+1,sizeof(struct lzstate)*(nroflzstates-i-1));
619 nroflzstates--;
620 lzstates=xrealloc(lzstates,sizeof(struct lzstate)*nroflzstates);
623 /***********************************************************************
624 * CopyLZFile16 (LZEXPAND.8)
626 LONG CopyLZFile16( HFILE16 src, HFILE16 dest )
628 dprintf_file(stddeb,"CopyLZFile16(%d,%d)\n",src,dest);
629 return LZCopy32(src,dest);
633 /***********************************************************************
634 * CopyLZFile32 (LZ32.7)
636 * Copy src to dest (including uncompressing src).
637 * NOTE: Yes. This is exactly the same function as LZCopy.
639 LONG CopyLZFile32( HFILE32 src, HFILE32 dest )
641 dprintf_file(stddeb,"CopyLZFile32(%d,%d)\n",src,dest);
642 return LZCopy32(src,dest);