Release 961222
[wine/multimedia.git] / misc / lzexpand.c
bloba48fa0de57dbf585f5b269534ae4696223ae1c6d
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 HFILE lzfd; /* the handle used by the program */
43 HFILE 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 UINT 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_ERROR)
77 return HFILE_ERROR;
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 INT
93 read_header(HFILE fd,struct lzfileheader *head) {
94 BYTE buf[14];
96 if (_llseek(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 * LZStart [LZEXPAND.7] [LZ32.6]
120 INT16 LZStart(void)
122 dprintf_file(stddeb,"LZStart(void)\n");
123 return 1;
127 * LZInit [LZEXPAND.3] [LZ32.2]
129 * initializes internal decompression buffers, returns lzfiledescriptor.
130 * (return value the same as hfSrc, if hfSrc is not compressed)
131 * on failure, returns error code <0
132 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
133 * we use as much as we need, we just OR 0x400 to the passed HFILE.
135 * since _llseek uses the same types as libc.lseek, we just use the macros of
136 * libc
138 HFILE
139 LZInit(HFILE hfSrc) {
140 struct lzfileheader head;
141 struct lzstate *lzs;
142 DWORD ret;
144 dprintf_file(stddeb,"LZInit(%d)\n",hfSrc);
145 ret=read_header(hfSrc,&head);
146 if (ret<=0) {
147 _llseek(hfSrc,0,SEEK_SET);
148 return ret?ret:hfSrc;
150 lzstates=xrealloc(lzstates,(++nroflzstates)*sizeof(struct lzstate));
151 lzs = lzstates+(nroflzstates-1);
153 memset(lzs,'\0',sizeof(*lzs));
154 lzs->realfd = hfSrc;
155 lzs->lzfd = hfSrc | 0x400;
156 lzs->lastchar = head.lastchar;
157 lzs->reallength = head.reallength;
159 lzs->get = xmalloc(GETLEN);
160 lzs->getlen = 0;
161 lzs->getcur = 0;
163 /* Yes, preinitialize with spaces */
164 memset(lzs->table,' ',0x1000);
165 /* Yes, start 16 byte from the END of the table */
166 lzs->curtabent = 0xff0;
167 return lzs->lzfd;
171 * LZDone [LZEXPAND.9] [LZ32.8]
174 void
175 LZDone(void) {
176 dprintf_file(stddeb,"LZDone()\n");
180 * GetExpandedName [LZEXPAND.10]
182 * gets the full filename of the compressed file 'in' by opening it
183 * and reading the header
185 * "file." is being translated to "file"
186 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
187 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
190 INT16
191 GetExpandedName16(LPCSTR in,LPSTR out) {
192 struct lzfileheader head;
193 HFILE fd;
194 OFSTRUCT ofs;
195 INT fnislowercased,ret,len;
196 LPSTR s,t;
198 dprintf_file(stddeb,"GetExpandedName(%s)\n",in);
199 fd=OpenFile(in,&ofs,OF_READ);
200 if (fd==HFILE_ERROR)
201 return LZERROR_BADINHANDLE;
202 ret=read_header(fd,&head);
203 if (ret<=0) {
204 _lclose(fd);
205 return LZERROR_BADINHANDLE;
208 /* This line will crash if the caller hasn't allocated enough memory
209 * for us.
211 strcpy(out,in);
213 /* look for directory prefix and skip it. */
214 s=out;
215 while (NULL!=(t=strpbrk(s,"/\\:")))
216 s=t+1;
218 /* now mangle the basename */
219 if (!*s) {
220 /* FIXME: hmm. shouldn't happen? */
221 fprintf(stddeb,__FILE__":GetExpandedFileName(), specified a directory or what? (%s)\n",in);
222 _lclose(fd);
223 return 1;
225 /* see if we should use lowercase or uppercase on the last char */
226 fnislowercased=1;
227 t=s+strlen(s)-1;
228 while (t>=out) {
229 if (!isalpha(*t)) {
230 t--;
231 continue;
233 fnislowercased=islower(*t);
234 break;
236 if (isalpha(head.lastchar)) {
237 if (fnislowercased)
238 head.lastchar=tolower(head.lastchar);
239 else
240 head.lastchar=toupper(head.lastchar);
243 /* now look where to replace the last character */
244 if (NULL!=(t=strchr(s,'.'))) {
245 if (t[1]=='\0') {
246 t[0]='\0';
247 } else {
248 len=strlen(t)-1;
249 if (t[len]=='_')
250 t[len]=head.lastchar;
252 } /* else no modification necessary */
253 _lclose(fd);
254 return 1;
258 * GetExpandedNameW [LZ32.11]
260 INT32
261 GetExpandedName32W(LPCWSTR in,LPWSTR out) {
262 char *xin,*xout;
263 INT32 ret;
265 xout = HeapAlloc( GetProcessHeap(), 0, lstrlen32W(in)+3 );
266 xin = HEAP_strdupWtoA( GetProcessHeap(), 0, in );
267 ret = GetExpandedName16(xin,xout);
268 if (ret>0) lstrcpyAtoW(out,xout);
269 HeapFree( GetProcessHeap(), 0, xin );
270 HeapFree( GetProcessHeap(), 0, xout );
271 return ret;
275 * GetExpandedNameA [LZ32.9]
277 INT32
278 GetExpandedName32A(LPCSTR in,LPSTR out) {
279 return GetExpandedName16(in,out);
283 * LZRead [LZEXPAND.5] [LZ32.4]
284 * just as normal read, but reads from LZ special fd and uncompresses.
286 INT16
287 LZRead16(HFILE fd,SEGPTR segbuf,UINT16 toread) {
288 dprintf_file(stddeb,"LZRead16(%d,%08lx,%d)\n",fd,(DWORD)segbuf,toread);
289 return LZRead32(fd,(LPBYTE)PTR_SEG_TO_LIN(segbuf),toread);
292 INT32
293 LZRead32(HFILE fd,LPVOID vbuf,UINT32 toread) {
294 int i,howmuch;
295 BYTE b,*buf;
296 struct lzstate *lzs;
298 buf=(LPBYTE)vbuf;
299 dprintf_file(stddeb,"LZRead32(%d,%p,%d)\n",fd,buf,toread);
300 howmuch=toread;
301 for (i=0;i<nroflzstates;i++)
302 if (lzstates[i].lzfd==fd)
303 break;
304 if (i==nroflzstates)
305 return _lread32(fd,buf,toread);
306 lzs=lzstates+i;
308 /* The decompressor itself is in a define, cause we need it twice
309 * in this function. (the decompressed byte will be in b)
311 #define DECOMPRESS_ONE_BYTE \
312 if (lzs->stringlen) { \
313 b = lzs->table[lzs->stringpos]; \
314 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
315 lzs->stringlen--; \
316 } else { \
317 if (!(lzs->bytetype&0x100)) { \
318 if (1!=GET(lzs,b)) \
319 return toread-howmuch; \
320 lzs->bytetype = b|0xFF00; \
322 if (lzs->bytetype & 1) { \
323 if (1!=GET(lzs,b)) \
324 return toread-howmuch; \
325 } else { \
326 BYTE b1,b2; \
328 if (1!=GET(lzs,b1)) \
329 return toread-howmuch; \
330 if (1!=GET(lzs,b2)) \
331 return toread-howmuch; \
332 /* Format: \
333 * b1 b2 \
334 * AB CD \
335 * where CAB is the stringoffset in the table\
336 * and D+3 is the len of the string \
337 */ \
338 lzs->stringpos = b1|((b2&0xf0)<<4); \
339 lzs->stringlen = (b2&0xf)+2; \
340 /* 3, but we use a byte already below ... */\
341 b = lzs->table[lzs->stringpos];\
342 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
344 lzs->bytetype>>=1; \
346 /* store b in table */ \
347 lzs->table[lzs->curtabent++]= b; \
348 lzs->curtabent &= 0xFFF; \
349 lzs->realcurrent++;
351 /* if someone has seeked, we have to bring the decompressor
352 * to that position
354 if (lzs->realcurrent!=lzs->realwanted) {
355 /* if the wanted position is before the current position
356 * I see no easy way to unroll ... We have to restart at
357 * the beginning. *sigh*
359 if (lzs->realcurrent>lzs->realwanted) {
360 /* flush decompressor state */
361 _llseek(lzs->realfd,14,SEEK_SET);
362 GET_FLUSH(lzs);
363 lzs->realcurrent= 0;
364 lzs->bytetype = 0;
365 lzs->stringlen = 0;
366 memset(lzs->table,' ',0x1000);
367 lzs->curtabent = 0xFF0;
369 while (lzs->realcurrent<lzs->realwanted) {
370 DECOMPRESS_ONE_BYTE;
374 while (howmuch) {
375 DECOMPRESS_ONE_BYTE;
376 lzs->realwanted++;
377 *buf++ = b;
378 howmuch--;
380 return toread;
381 #undef DECOMPRESS_ONE_BYTE
385 * LZSeek [LZEXPAND.4] [LZ32.3]
387 * works as the usual _llseek
390 LONG
391 LZSeek(HFILE fd,LONG off,INT32 type) {
392 int i;
393 struct lzstate *lzs;
394 LONG newwanted;
396 dprintf_file(stddeb,"LZSeek(%d,%ld,%d)\n",fd,off,type);
397 for (i=0;i<nroflzstates;i++)
398 if (lzstates[i].lzfd==fd)
399 break;
400 /* not compressed? just use normal _llseek() */
401 if (i==nroflzstates)
402 return _llseek(fd,off,type);
403 lzs = lzstates+i;
404 newwanted = lzs->realwanted;
405 switch (type) {
406 case 1: /* SEEK_CUR */
407 newwanted += off;
408 break;
409 case 2: /* SEEK_END */
410 newwanted = lzs->reallength-off;
411 break;
412 default:/* SEEK_SET */
413 newwanted = off;
414 break;
416 if (newwanted>lzs->reallength)
417 return LZERROR_BADVALUE;
418 if (newwanted<0)
419 return LZERROR_BADVALUE;
420 lzs->realwanted = newwanted;
421 return newwanted;
425 * LZCopy [LZEXPAND.1] [LZ32.0]
427 * Copies everything from src to dest
428 * if src is a LZ compressed file, it will be uncompressed.
429 * will return the number of bytes written to dest or errors.
431 LONG
432 LZCopy(HFILE src,HFILE dest) {
433 int i,ret,wret;
434 LONG len;
435 #define BUFLEN 1000
436 BYTE buf[BUFLEN];
437 INT32 (*xread)(HFILE,LPVOID,UINT32);
439 dprintf_file(stddeb,"LZCopy(%d,%d)\n",src,dest);
440 for (i=0;i<nroflzstates;i++)
441 if (src==lzstates[i].lzfd)
442 break;
444 /* not compressed? just copy */
445 if (i==nroflzstates)
446 xread=(INT32(*)(HFILE,LPVOID,UINT32))_lread32;
447 else
448 xread=LZRead32;
449 len=0;
450 while (1) {
451 ret=xread(src,buf,BUFLEN);
452 if (ret<=0) {
453 if (ret==0)
454 break;
455 if (ret==-1)
456 return LZERROR_READ;
457 return ret;
459 len += ret;
460 wret = _lwrite32(dest,buf,ret);
461 if (wret!=ret)
462 return LZERROR_WRITE;
464 return len;
465 #undef BUFLEN
468 /* reverses GetExpandedPathname */
469 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
471 char *p;
472 char *mfn = (char *)xmalloc( strlen(fn) + 3 ); /* "._" and \0 */
473 strcpy( mfn, fn );
474 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
475 if ((p = strchr( p, '.' )))
477 p++;
478 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
479 else p[strlen(p)-1] = '_'; /* replace last character */
481 else strcat( mfn, "._" ); /* append "._" */
482 return mfn;
486 * LZOpenFile [LZEXPAND.2]
487 * Opens a file. If not compressed, open it as a normal file.
489 HFILE
490 LZOpenFile16(LPCSTR fn,LPOFSTRUCT ofs,UINT16 mode) {
491 HFILE fd,cfd;
493 dprintf_file(stddeb,"LZOpenFile(%s,%p,%d)\n",fn,ofs,mode);
494 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
495 fd=OpenFile(fn,ofs,mode);
496 if (fd==HFILE_ERROR)
498 LPSTR mfn = LZEXPAND_MangleName(fn);
499 fd = OpenFile(mfn,ofs,mode);
500 free( mfn );
502 if ((mode&~0x70)!=OF_READ)
503 return fd;
504 if (fd==HFILE_ERROR)
505 return HFILE_ERROR;
506 cfd=LZInit(fd);
507 if (cfd<=0)
508 return fd;
509 return cfd;
513 * LZOpenFileA [LZ32.1]
515 HFILE
516 LZOpenFile32A(LPCSTR fn,LPOFSTRUCT ofs,UINT32 mode) {
517 return LZOpenFile16(fn,ofs,mode);
521 * LZOpenFileW [LZ32.10]
523 HFILE
524 LZOpenFile32W(LPCWSTR fn,LPOFSTRUCT ofs,UINT32 mode) {
525 LPSTR xfn;
526 LPWSTR yfn;
527 HFILE ret;
529 xfn = HEAP_strdupWtoA( GetProcessHeap(), 0, fn);
530 ret = LZOpenFile16(xfn,ofs,mode);
531 HeapFree( GetProcessHeap(), 0, xfn );
532 if (ret!=HFILE_ERROR) {
533 /* ofs->szPathName is an array with the OFSTRUCT */
534 yfn = HEAP_strdupAtoW( GetProcessHeap(), 0, ofs->szPathName );
535 memcpy(ofs->szPathName,yfn,lstrlen32W(yfn)*2+2);
536 HeapFree( GetProcessHeap(), 0, yfn );
538 return ret;
542 * LZClose [LZEXPAND.6] [LZ32.5]
544 void
545 LZClose(HFILE fd) {
546 int i;
548 dprintf_file(stddeb,"LZClose(%d)\n",fd);
549 for (i=0;i<nroflzstates;i++)
550 if (lzstates[i].lzfd==fd)
551 break;
552 if (i==nroflzstates) {
553 _lclose(fd);
554 return;
556 if (lzstates[i].get)
557 free(lzstates[i].get);
558 _lclose(lzstates[i].realfd);
559 memcpy(lzstates+i,lzstates+i+1,sizeof(struct lzstate)*(nroflzstates-i-1));
560 nroflzstates--;
561 lzstates=xrealloc(lzstates,sizeof(struct lzstate)*nroflzstates);
565 * CopyLZFile [LZEXPAND.8] [LZ32.7]
567 * Copy src to dest (including uncompressing src).
568 * NOTE: Yes. This is exactly the same function as LZCopy.
570 LONG
571 CopyLZFile(HFILE src,HFILE dest) {
572 dprintf_file(stddeb,"CopyLZFile(%d,%d)\n",src,dest);
573 return LZCopy(src,dest);