Small fixes.
[wine/multimedia.git] / misc / lzexpand.c
blobd968b1936096d758ff99a6deb1cafee8f6e8cf29
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 <string.h>
12 #include <unistd.h>
13 #include <ctype.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include "windows.h"
17 #include "file.h"
18 #include "heap.h"
19 #include "ldt.h"
20 #include "lzexpand.h"
21 #include "debug.h"
24 /* The readahead length of the decompressor. Reading single bytes
25 * using _lread() would be SLOW.
27 #define GETLEN 2048
29 /* Format of first 14 byte of LZ compressed file */
30 struct lzfileheader {
31 BYTE magic[8];
32 BYTE compressiontype;
33 CHAR lastchar;
34 DWORD reallength;
36 static BYTE LZMagic[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
38 static struct lzstate {
39 HFILE32 lzfd; /* the handle used by the program */
40 HFILE32 realfd; /* the real filedescriptor */
41 CHAR lastchar; /* the last char of the filename */
43 DWORD reallength; /* the decompressed length of the file */
44 DWORD realcurrent; /* the position the decompressor currently is */
45 DWORD realwanted; /* the position the user wants to read from */
47 BYTE table[0x1000]; /* the rotating LZ table */
48 UINT32 curtabent; /* CURrent TABle ENTry */
50 BYTE stringlen; /* length and position of current string */
51 DWORD stringpos; /* from stringtable */
54 WORD bytetype; /* bitmask within blocks */
56 BYTE *get; /* GETLEN bytes */
57 DWORD getcur; /* current read */
58 DWORD getlen; /* length last got */
59 } *lzstates=NULL;
60 static int nroflzstates=0;
62 /* reads one compressed byte, including buffering */
63 #define GET(lzs,b) _lzget(lzs,&b)
64 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
66 static int
67 _lzget(struct lzstate *lzs,BYTE *b) {
68 if (lzs->getcur<lzs->getlen) {
69 *b = lzs->get[lzs->getcur++];
70 return 1;
71 } else {
72 int ret = _lread32(lzs->realfd,lzs->get,GETLEN);
73 if (ret==HFILE_ERROR32)
74 return HFILE_ERROR32;
75 if (ret==0)
76 return 0;
77 lzs->getlen = ret;
78 lzs->getcur = 1;
79 *b = *(lzs->get);
80 return 1;
83 /* internal function, reads lzheader
84 * returns BADINHANDLE for non filedescriptors
85 * return 0 for file not compressed using LZ
86 * return UNKNOWNALG for unknown algorithm
87 * returns lzfileheader in *head
89 static INT32 read_header(HFILE32 fd,struct lzfileheader *head)
91 BYTE buf[14];
93 if (_llseek32(fd,0,SEEK_SET)==-1)
94 return LZERROR_BADINHANDLE;
96 /* We can't directly read the lzfileheader struct due to
97 * structure element alignment
99 if (_lread32(fd,buf,14)<14)
100 return 0;
101 memcpy(head->magic,buf,8);
102 memcpy(&(head->compressiontype),buf+8,1);
103 memcpy(&(head->lastchar),buf+9,1);
105 /* FIXME: consider endianess on non-intel architectures */
106 memcpy(&(head->reallength),buf+10,4);
108 if (memcmp(head->magic,LZMagic,8))
109 return 0;
110 if (head->compressiontype!='A')
111 return LZERROR_UNKNOWNALG;
112 return 1;
115 /***********************************************************************
116 * LZStart16 (LZEXPAND.7)
118 INT16 WINAPI LZStart16(void)
120 TRACE(file,"(void)\n");
121 return 1;
125 /***********************************************************************
126 * LZStart32 (LZ32.6)
128 INT32 WINAPI LZStart32(void)
130 TRACE(file,"(void)\n");
131 return 1;
135 /***********************************************************************
136 * LZInit16 (LZEXPAND.3)
138 HFILE16 WINAPI LZInit16( HFILE16 hfSrc )
140 return HFILE32_TO_HFILE16( LZInit32( HFILE16_TO_HFILE32(hfSrc) ) );
144 /***********************************************************************
145 * LZInit32 (LZ32.2)
147 * initializes internal decompression buffers, returns lzfiledescriptor.
148 * (return value the same as hfSrc, if hfSrc is not compressed)
149 * on failure, returns error code <0
150 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
151 * we use as much as we need, we just OR 0x400 to the passed HFILE.
153 * since _llseek uses the same types as libc.lseek, we just use the macros of
154 * libc
156 HFILE32 WINAPI LZInit32( HFILE32 hfSrc )
159 struct lzfileheader head;
160 struct lzstate *lzs;
161 DWORD ret;
163 TRACE(file,"(%d)\n",hfSrc);
164 ret=read_header(hfSrc,&head);
165 if (ret<=0) {
166 _llseek32(hfSrc,0,SEEK_SET);
167 return ret?ret:hfSrc;
169 lzstates = HeapReAlloc( SystemHeap, 0, lzstates,
170 (++nroflzstates)*sizeof(struct lzstate) );
171 lzs = lzstates+(nroflzstates-1);
173 memset(lzs,'\0',sizeof(*lzs));
174 lzs->realfd = hfSrc;
175 lzs->lzfd = hfSrc | 0x400;
176 lzs->lastchar = head.lastchar;
177 lzs->reallength = head.reallength;
179 lzs->get = HEAP_xalloc( GetProcessHeap(), 0, GETLEN );
180 lzs->getlen = 0;
181 lzs->getcur = 0;
183 /* Yes, preinitialize with spaces */
184 memset(lzs->table,' ',0x1000);
185 /* Yes, start 16 byte from the END of the table */
186 lzs->curtabent = 0xff0;
187 return lzs->lzfd;
191 /***********************************************************************
192 * LZDone (LZEXPAND.9) (LZ32.8)
194 void WINAPI LZDone(void)
196 TRACE(file,"(void)\n");
200 /***********************************************************************
201 * GetExpandedName16 (LZEXPAND.10)
203 INT16 WINAPI GetExpandedName16( LPCSTR in, LPSTR out )
205 return (INT16)GetExpandedName32A( in, out );
209 /***********************************************************************
210 * GetExpandedName32A (LZ32.9)
212 * gets the full filename of the compressed file 'in' by opening it
213 * and reading the header
215 * "file." is being translated to "file"
216 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
217 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
220 INT32 WINAPI GetExpandedName32A( LPCSTR in, LPSTR out )
222 struct lzfileheader head;
223 HFILE32 fd;
224 OFSTRUCT ofs;
225 INT32 fnislowercased,ret,len;
226 LPSTR s,t;
228 TRACE(file,"(%s)\n",in);
229 fd=OpenFile32(in,&ofs,OF_READ);
230 if (fd==HFILE_ERROR32)
231 return (INT32)(INT16)LZERROR_BADINHANDLE;
232 strcpy(out,in);
233 ret=read_header(fd,&head);
234 if (ret<=0) {
235 /* not a LZ compressed file, so the expanded name is the same
236 * as the input name */
237 _lclose32(fd);
238 return 1;
242 /* look for directory prefix and skip it. */
243 s=out;
244 while (NULL!=(t=strpbrk(s,"/\\:")))
245 s=t+1;
247 /* now mangle the basename */
248 if (!*s) {
249 /* FIXME: hmm. shouldn't happen? */
250 WARN(file,"Specified a directory or what? (%s)\n",in);
251 _lclose32(fd);
252 return 1;
254 /* see if we should use lowercase or uppercase on the last char */
255 fnislowercased=1;
256 t=s+strlen(s)-1;
257 while (t>=out) {
258 if (!isalpha(*t)) {
259 t--;
260 continue;
262 fnislowercased=islower(*t);
263 break;
265 if (isalpha(head.lastchar)) {
266 if (fnislowercased)
267 head.lastchar=tolower(head.lastchar);
268 else
269 head.lastchar=toupper(head.lastchar);
272 /* now look where to replace the last character */
273 if (NULL!=(t=strchr(s,'.'))) {
274 if (t[1]=='\0') {
275 t[0]='\0';
276 } else {
277 len=strlen(t)-1;
278 if (t[len]=='_')
279 t[len]=head.lastchar;
281 } /* else no modification necessary */
282 _lclose32(fd);
283 return 1;
287 /***********************************************************************
288 * GetExpandedName32W (LZ32.11)
290 INT32 WINAPI GetExpandedName32W( LPCWSTR in, LPWSTR out )
292 char *xin,*xout;
293 INT32 ret;
295 xout = HeapAlloc( GetProcessHeap(), 0, lstrlen32W(in)+3 );
296 xin = HEAP_strdupWtoA( GetProcessHeap(), 0, in );
297 ret = GetExpandedName16(xin,xout);
298 if (ret>0) lstrcpyAtoW(out,xout);
299 HeapFree( GetProcessHeap(), 0, xin );
300 HeapFree( GetProcessHeap(), 0, xout );
301 return ret;
305 /***********************************************************************
306 * LZRead16 (LZEXPAND.5)
308 INT16 WINAPI LZRead16( HFILE16 fd, LPVOID buf, UINT16 toread )
310 return LZRead32(HFILE16_TO_HFILE32(fd),buf,toread);
314 /***********************************************************************
315 * LZRead32 (LZ32.4)
317 INT32 WINAPI LZRead32( HFILE32 fd, LPVOID vbuf, UINT32 toread )
319 int i,howmuch;
320 BYTE b,*buf;
321 struct lzstate *lzs;
323 buf=(LPBYTE)vbuf;
324 TRACE(file,"(%d,%p,%d)\n",fd,buf,toread);
325 howmuch=toread;
326 for (i=0;i<nroflzstates;i++)
327 if (lzstates[i].lzfd==fd)
328 break;
329 if (i==nroflzstates)
330 return _lread32(fd,buf,toread);
331 lzs=lzstates+i;
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; \
340 lzs->stringlen--; \
341 } else { \
342 if (!(lzs->bytetype&0x100)) { \
343 if (1!=GET(lzs,b)) \
344 return toread-howmuch; \
345 lzs->bytetype = b|0xFF00; \
347 if (lzs->bytetype & 1) { \
348 if (1!=GET(lzs,b)) \
349 return toread-howmuch; \
350 } else { \
351 BYTE b1,b2; \
353 if (1!=GET(lzs,b1)) \
354 return toread-howmuch; \
355 if (1!=GET(lzs,b2)) \
356 return toread-howmuch; \
357 /* Format: \
358 * b1 b2 \
359 * AB CD \
360 * where CAB is the stringoffset in the table\
361 * and D+3 is the len of the string \
362 */ \
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;\
369 lzs->bytetype>>=1; \
371 /* store b in table */ \
372 lzs->table[lzs->curtabent++]= b; \
373 lzs->curtabent &= 0xFFF; \
374 lzs->realcurrent++;
376 /* if someone has seeked, we have to bring the decompressor
377 * to that position
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 _llseek32(lzs->realfd,14,SEEK_SET);
387 GET_FLUSH(lzs);
388 lzs->realcurrent= 0;
389 lzs->bytetype = 0;
390 lzs->stringlen = 0;
391 memset(lzs->table,' ',0x1000);
392 lzs->curtabent = 0xFF0;
394 while (lzs->realcurrent<lzs->realwanted) {
395 DECOMPRESS_ONE_BYTE;
399 while (howmuch) {
400 DECOMPRESS_ONE_BYTE;
401 lzs->realwanted++;
402 *buf++ = b;
403 howmuch--;
405 return toread;
406 #undef DECOMPRESS_ONE_BYTE
410 /***********************************************************************
411 * LZSeek16 (LZEXPAND.4)
413 LONG WINAPI LZSeek16( HFILE16 fd, LONG off, INT16 type )
415 return LZSeek32( HFILE16_TO_HFILE32(fd), off, type );
419 /***********************************************************************
420 * LZSeek32 (LZ32.3)
422 LONG WINAPI LZSeek32( HFILE32 fd, LONG off, INT32 type )
424 int i;
425 struct lzstate *lzs;
426 LONG newwanted;
428 TRACE(file,"(%d,%ld,%d)\n",fd,off,type);
429 for (i=0;i<nroflzstates;i++)
430 if (lzstates[i].lzfd==fd)
431 break;
432 /* not compressed? just use normal _llseek() */
433 if (i==nroflzstates)
434 return _llseek32(fd,off,type);
435 lzs = lzstates+i;
436 newwanted = lzs->realwanted;
437 switch (type) {
438 case 1: /* SEEK_CUR */
439 newwanted += off;
440 break;
441 case 2: /* SEEK_END */
442 newwanted = lzs->reallength-off;
443 break;
444 default:/* SEEK_SET */
445 newwanted = off;
446 break;
448 if (newwanted>lzs->reallength)
449 return LZERROR_BADVALUE;
450 if (newwanted<0)
451 return LZERROR_BADVALUE;
452 lzs->realwanted = newwanted;
453 return newwanted;
457 /***********************************************************************
458 * LZCopy16 (LZEXPAND.1)
461 LONG WINAPI LZCopy16( HFILE16 src, HFILE16 dest )
463 return LZCopy32( HFILE16_TO_HFILE32(src), HFILE16_TO_HFILE32(dest) );
467 /***********************************************************************
468 * LZCopy32 (LZ32.0)
470 * Copies everything from src to dest
471 * if src is a LZ compressed file, it will be uncompressed.
472 * will return the number of bytes written to dest or errors.
474 LONG WINAPI LZCopy32( HFILE32 src, HFILE32 dest )
476 int usedlzinit=0,i,ret,wret;
477 LONG len;
478 HFILE32 oldsrc = src;
479 #define BUFLEN 1000
480 BYTE buf[BUFLEN];
481 INT32 WINAPI (*xread)(HFILE32,LPVOID,UINT32);
483 TRACE(file,"(%d,%d)\n",src,dest);
484 if (src<0x400) {
485 src = LZInit32(src);
486 if (src!=oldsrc)
487 usedlzinit=1;
488 if (src>0xfff0)
489 return 0;
492 for (i=0;i<nroflzstates;i++)
493 if (src==lzstates[i].lzfd)
494 break;
495 /* not compressed? just copy */
496 if (i==nroflzstates)
497 xread=(INT32(*)(HFILE32,LPVOID,UINT32))_lread32;
498 else
499 xread=LZRead32;
500 len=0;
501 while (1) {
502 ret=xread(src,buf,BUFLEN);
503 if (ret<=0) {
504 if (ret==0)
505 break;
506 if (ret==-1)
507 return LZERROR_READ;
508 return ret;
510 len += ret;
511 wret = _lwrite32(dest,buf,ret);
512 if (wret!=ret)
513 return LZERROR_WRITE;
515 if (usedlzinit)
516 LZClose32(src);
517 return len;
518 #undef BUFLEN
521 /* reverses GetExpandedPathname */
522 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
524 char *p;
525 char *mfn = (char *)HEAP_xalloc( GetProcessHeap(), 0,
526 strlen(fn) + 3 ); /* "._" and \0 */
527 strcpy( mfn, fn );
528 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
529 if ((p = strchr( p, '.' )))
531 p++;
532 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
533 else p[strlen(p)-1] = '_'; /* replace last character */
535 else strcat( mfn, "._" ); /* append "._" */
536 return mfn;
540 /***********************************************************************
541 * LZOpenFile16 (LZEXPAND.2)
543 HFILE16 WINAPI LZOpenFile16( LPCSTR fn, LPOFSTRUCT ofs, UINT16 mode )
545 return HFILE32_TO_HFILE16 ( LZOpenFile32A( fn, ofs, mode ) );
549 /***********************************************************************
550 * LZOpenFile32A (LZ32.1)
552 * Opens a file. If not compressed, open it as a normal file.
554 HFILE32 WINAPI LZOpenFile32A( LPCSTR fn, LPOFSTRUCT ofs, UINT32 mode )
556 HFILE32 fd,cfd;
558 TRACE(file,"(%s,%p,%d)\n",fn,ofs,mode);
559 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
560 fd=OpenFile32(fn,ofs,mode);
561 if (fd==HFILE_ERROR32)
563 LPSTR mfn = LZEXPAND_MangleName(fn);
564 fd = OpenFile32(mfn,ofs,mode);
565 HeapFree( GetProcessHeap(), 0, mfn );
567 if ((mode&~0x70)!=OF_READ)
568 return fd;
569 if (fd==HFILE_ERROR32)
570 return HFILE_ERROR32;
571 cfd=LZInit32(fd);
572 if (cfd<=0)
573 return fd;
574 return cfd;
578 /***********************************************************************
579 * LZOpenFile32W (LZ32.10)
581 HFILE32 WINAPI LZOpenFile32W( LPCWSTR fn, LPOFSTRUCT ofs, UINT32 mode )
583 LPSTR xfn;
584 LPWSTR yfn;
585 HFILE32 ret;
587 xfn = HEAP_strdupWtoA( GetProcessHeap(), 0, fn);
588 ret = LZOpenFile16(xfn,ofs,mode);
589 HeapFree( GetProcessHeap(), 0, xfn );
590 if (ret!=HFILE_ERROR32) {
591 /* ofs->szPathName is an array with the OFSTRUCT */
592 yfn = HEAP_strdupAtoW( GetProcessHeap(), 0, ofs->szPathName );
593 memcpy(ofs->szPathName,yfn,lstrlen32W(yfn)*2+2);
594 HeapFree( GetProcessHeap(), 0, yfn );
596 return ret;
600 /***********************************************************************
601 * LZClose16 (LZEXPAND.6)
603 void WINAPI LZClose16( HFILE16 fd )
605 return LZClose32( HFILE16_TO_HFILE32 (fd) );
609 /***********************************************************************
610 * LZClose32 (LZ32.5)
612 void WINAPI LZClose32( HFILE32 fd )
614 int i;
616 TRACE(file,"(%d)\n",fd);
617 for (i=0;i<nroflzstates;i++)
618 if (lzstates[i].lzfd==fd)
619 break;
620 if (i==nroflzstates) {
621 _lclose32(fd);
622 return;
624 if (lzstates[i].get)
625 HeapFree( GetProcessHeap(), 0, lzstates[i].get );
626 _lclose32(lzstates[i].realfd);
627 memmove(lzstates+i,lzstates+i+1,
628 sizeof(struct lzstate)*(nroflzstates-i-1));
629 nroflzstates--;
630 lzstates = HeapReAlloc( SystemHeap, 0, lzstates,
631 sizeof(struct lzstate)*nroflzstates );
634 /***********************************************************************
635 * CopyLZFile16 (LZEXPAND.8)
637 LONG WINAPI CopyLZFile16( HFILE16 src, HFILE16 dest )
639 TRACE(file,"(%d,%d)\n",src,dest);
640 return LZCopy32(HFILE16_TO_HFILE32(src),HFILE16_TO_HFILE32(dest));
644 /***********************************************************************
645 * CopyLZFile32 (LZ32.7)
647 * Copy src to dest (including uncompressing src).
648 * NOTE: Yes. This is exactly the same function as LZCopy.
650 LONG WINAPI CopyLZFile32( HFILE32 src, HFILE32 dest )
652 TRACE(file,"(%d,%d)\n",src,dest);
653 return LZCopy32(src,dest);