Release 960324
[wine.git] / misc / lzexpand.c
blob4b4b500590a286ebe799d4df09131a070c9bdb14
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 "lzexpand.h"
20 #include "stackframe.h"
21 #include "stddebug.h"
22 #include "debug.h"
23 #include "xmalloc.h"
25 /* The readahead length of the decompressor. Reading single bytes
26 * using _lread() would be SLOW.
28 #define GETLEN 2048
30 /* Format of first 14 byte of LZ compressed file */
31 struct lzfileheader {
32 BYTE magic[8];
33 BYTE compressiontype;
34 CHAR lastchar;
35 DWORD reallength;
37 static BYTE LZMagic[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
39 static struct lzstate {
40 HFILE lzfd; /* the handle used by the program */
41 HFILE realfd; /* the real filedescriptor */
42 CHAR lastchar; /* the last char of the filename */
44 DWORD reallength; /* the decompressed length of the file */
45 DWORD realcurrent; /* the position the decompressor currently is */
46 DWORD realwanted; /* the position the user wants to read from */
48 BYTE table[0x1000]; /* the rotating LZ table */
49 UINT curtabent; /* CURrent TABle ENTry */
51 BYTE stringlen; /* length and position of current string */
52 DWORD stringpos; /* from stringtable */
55 WORD bytetype; /* bitmask within blocks */
57 BYTE *get; /* GETLEN bytes */
58 DWORD getcur; /* current read */
59 DWORD getlen; /* length last got */
60 } *lzstates=NULL;
61 static int nroflzstates=0;
63 /* reads one compressed byte, including buffering */
64 #define GET(lzs,b) _lzget(lzs,&b)
65 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
67 int
68 _lzget(struct lzstate *lzs,BYTE *b) {
69 if (lzs->getcur<lzs->getlen) {
70 *b = lzs->get[lzs->getcur++];
71 return 1;
72 } else {
73 int ret = FILE_Read(lzs->realfd,lzs->get,GETLEN);
74 if (ret==HFILE_ERROR)
75 return HFILE_ERROR;
76 if (ret==0)
77 return 0;
78 lzs->getlen = ret;
79 lzs->getcur = 1;
80 *b = *(lzs->get);
81 return 1;
84 /* internal function, reads lzheader
85 * returns BADINHANDLE for non filedescriptors
86 * return 0 for file not compressed using LZ
87 * return UNKNOWNALG for unknown algorithm
88 * returns lzfileheader in *head
90 static INT
91 read_header(HFILE fd,struct lzfileheader *head) {
92 BYTE buf[14];
94 if (_llseek(fd,0,SEEK_SET)==-1)
95 return LZERROR_BADINHANDLE;
97 /* We can't directly read the lzfileheader struct due to
98 * structure element alignment
100 if (FILE_Read(fd,buf,14)<14)
101 return 0;
102 memcpy(head->magic,buf,8);
103 memcpy(&(head->compressiontype),buf+8,1);
104 memcpy(&(head->lastchar),buf+9,1);
106 /* FIXME: consider endianess on non-intel architectures */
107 memcpy(&(head->reallength),buf+10,4);
109 if (memcmp(head->magic,LZMagic,8))
110 return 0;
111 if (head->compressiontype!='A')
112 return LZERROR_UNKNOWNALG;
113 return 1;
116 * LZSTART [LZEXPAND.7]
119 LZStart(void) {
120 dprintf_file(stddeb,"LZStart(void)\n");
121 return 1;
125 * LZINIT [LZEXPAND.3]
127 * initializes internal decompression buffers, returns lzfiledescriptor.
128 * (return value the same as hfSrc, if hfSrc is not compressed)
129 * on failure, returns error code <0
130 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
131 * we use as much as we need, we just OR 0x400 to the passed HFILE.
133 * since _llseek uses the same types as libc.lseek, we just use the macros of
134 * libc
136 HFILE
137 LZInit(HFILE hfSrc) {
138 struct lzfileheader head;
139 struct lzstate *lzs;
140 DWORD ret;
142 dprintf_file(stddeb,"LZInit(%d)\n",hfSrc);
143 ret=read_header(hfSrc,&head);
144 if (ret<=0) {
145 _llseek(hfSrc,0,SEEK_SET);
146 return ret?ret:hfSrc;
148 lzstates=xrealloc(lzstates,(++nroflzstates)*sizeof(struct lzstate));
149 lzs = lzstates+(nroflzstates-1);
151 memset(lzs,'\0',sizeof(*lzs));
152 lzs->realfd = hfSrc;
153 lzs->lzfd = hfSrc | 0x400;
154 lzs->lastchar = head.lastchar;
155 lzs->reallength = head.reallength;
157 lzs->get = xmalloc(GETLEN);
158 lzs->getlen = 0;
159 lzs->getcur = 0;
161 /* Yes, preinitialize with spaces */
162 memset(lzs->table,' ',0x1000);
163 /* Yes, start 16 byte from the END of the table */
164 lzs->curtabent = 0xff0;
165 return lzs->lzfd;
169 * LZDone [LZEXPAND.9]
172 void
173 LZDone(void) {
174 dprintf_file(stddeb,"LZDone()\n");
178 * GetExpandedName [LZEXPAND.10]
180 * gets the full filename of the compressed file 'in' by opening it
181 * and reading the header
183 * "file." is being translated to "file"
184 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
185 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
189 GetExpandedName(LPCSTR in,LPSTR out) {
190 struct lzfileheader head;
191 HFILE fd;
192 OFSTRUCT ofs;
193 INT fnislowercased,ret,len;
194 LPSTR s,t;
196 dprintf_file(stddeb,"GetExpandedName(%s)\n",in);
197 fd=OpenFile(in,&ofs,OF_READ);
198 if (fd==HFILE_ERROR)
199 return LZERROR_BADINHANDLE;
200 ret=read_header(fd,&head);
201 if (ret<=0) {
202 _lclose(fd);
203 return LZERROR_BADINHANDLE;
206 /* This line will crash if the caller hasn't allocated enough memory
207 * for us.
209 strcpy(out,in);
211 /* look for directory prefix and skip it. */
212 s=out;
213 while (NULL!=(t=strpbrk(s,"/\\:")))
214 s=t+1;
216 /* now mangle the basename */
217 if (!*s) {
218 /* FIXME: hmm. shouldn't happen? */
219 fprintf(stddeb,__FILE__":GetExpandedFileName(), specified a directory or what? (%s)\n",in);
220 _lclose(fd);
221 return 1;
223 /* see if we should use lowercase or uppercase on the last char */
224 fnislowercased=1;
225 t=s+strlen(s)-1;
226 while (t>=out) {
227 if (!isalpha(*t)) {
228 t--;
229 continue;
231 fnislowercased=islower(*t);
232 break;
234 if (isalpha(head.lastchar)) {
235 if (fnislowercased)
236 head.lastchar=tolower(head.lastchar);
237 else
238 head.lastchar=toupper(head.lastchar);
241 /* now look where to replace the last character */
242 if (NULL!=(t=strchr(s,'.'))) {
243 if (t[1]=='\0') {
244 t[0]='\0';
245 } else {
246 len=strlen(t)-1;
247 if (t[len]=='_')
248 t[len]=head.lastchar;
250 } /* else no modification necessary */
251 _lclose(fd);
252 return 1;
256 * LZRead [LZEXPAND.5]
257 * just as normal read, but reads from LZ special fd and uncompresses.
260 LZRead(HFILE fd,SEGPTR segbuf,WORD toread) {
261 int i,howmuch;
262 BYTE b;
263 BYTE *buf;
264 struct lzstate *lzs;
266 dprintf_file(stddeb,"LZRead(%d,%08lx,%d)\n",fd,(DWORD)segbuf,toread);
267 howmuch=toread;
268 for (i=0;i<nroflzstates;i++)
269 if (lzstates[i].lzfd==fd)
270 break;
271 if (i==nroflzstates)
272 return _lread(fd,segbuf,toread);
273 lzs=lzstates+i;
276 /* The decompressor itself is in a define, cause we need it twice
277 * in this function. (the decompressed byte will be in b)
279 #define DECOMPRESS_ONE_BYTE \
280 if (lzs->stringlen) { \
281 b = lzs->table[lzs->stringpos]; \
282 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
283 lzs->stringlen--; \
284 } else { \
285 if (!(lzs->bytetype&0x100)) { \
286 if (1!=GET(lzs,b)) \
287 return toread-howmuch; \
288 lzs->bytetype = b|0xFF00; \
290 if (lzs->bytetype & 1) { \
291 if (1!=GET(lzs,b)) \
292 return toread-howmuch; \
293 } else { \
294 BYTE b1,b2; \
296 if (1!=GET(lzs,b1)) \
297 return toread-howmuch; \
298 if (1!=GET(lzs,b2)) \
299 return toread-howmuch; \
300 /* Format: \
301 * b1 b2 \
302 * AB CD \
303 * where CAB is the stringoffset in the table\
304 * and D+3 is the len of the string \
305 */ \
306 lzs->stringpos = b1|((b2&0xf0)<<4); \
307 lzs->stringlen = (b2&0xf)+2; \
308 /* 3, but we use a byte already below ... */\
309 b = lzs->table[lzs->stringpos];\
310 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
312 lzs->bytetype>>=1; \
314 /* store b in table */ \
315 lzs->table[lzs->curtabent++]= b; \
316 lzs->curtabent &= 0xFFF; \
317 lzs->realcurrent++;
319 /* if someone has seeked, we have to bring the decompressor
320 * to that position
322 if (lzs->realcurrent!=lzs->realwanted) {
323 /* if the wanted position is before the current position
324 * I see no easy way to unroll ... We have to restart at
325 * the beginning. *sigh*
327 if (lzs->realcurrent>lzs->realwanted) {
328 /* flush decompressor state */
329 _llseek(lzs->realfd,14,SEEK_SET);
330 GET_FLUSH(lzs);
331 lzs->realcurrent= 0;
332 lzs->bytetype = 0;
333 lzs->stringlen = 0;
334 memset(lzs->table,' ',0x1000);
335 lzs->curtabent = 0xFF0;
337 while (lzs->realcurrent<lzs->realwanted) {
338 DECOMPRESS_ONE_BYTE;
342 buf=PTR_SEG_TO_LIN(segbuf);
343 while (howmuch) {
344 DECOMPRESS_ONE_BYTE;
345 lzs->realwanted++;
346 *buf++ = b;
347 howmuch--;
349 return toread;
350 #undef DECOMPRESS_ONE_BYTE
354 * LZSeek [LZEXPAND.4]
356 * works as the usual _llseek
359 LONG
360 LZSeek(HFILE fd,LONG off,INT type) {
361 int i;
362 struct lzstate *lzs;
363 LONG lastwanted,newwanted;
365 dprintf_file(stddeb,"LZSeek(%d,%ld,%d)\n",fd,off,type);
366 for (i=0;i<nroflzstates;i++)
367 if (lzstates[i].lzfd==fd)
368 break;
369 /* not compressed? just use normal _llseek() */
370 if (i==nroflzstates)
371 return _llseek(fd,off,type);
372 lzs = lzstates+i;
373 lastwanted = lzs->realwanted;
374 newwanted = lzs->realwanted;
375 switch (type) {
376 case 1: /* SEEK_CUR */
377 newwanted += off;
378 break;
379 case 2: /* SEEK_END */
380 newwanted = lzs->reallength-off;
381 break;
382 default:/* SEEK_SET */
383 newwanted = off;
384 break;
386 if (newwanted>lzs->reallength)
387 return LZERROR_BADVALUE;
388 if (newwanted<0)
389 return LZERROR_BADVALUE;
390 lzs->realwanted = newwanted;
391 return lastwanted;
395 * LZCopy [LZEXPAND.1]
397 * Copies everything from src to dest
398 * if src is a LZ compressed file, it will be uncompressed.
399 * will return the number of bytes written to dest or errors.
401 LONG
402 LZCopy(HFILE src,HFILE dest) {
403 int i,ret,wret;
404 LONG len;
405 #define BUFLEN 1000
406 BYTE buf[BUFLEN];
407 INT (*xread)(HFILE,SEGPTR,WORD);
409 dprintf_file(stddeb,"LZCopy(%d,%d)\n",src,dest);
410 for (i=0;i<nroflzstates;i++)
411 if (src==lzstates[i].lzfd)
412 break;
414 /* not compressed? just copy */
415 if (i==nroflzstates)
416 xread=_lread;
417 else
418 xread=LZRead;
419 len=0;
420 while (1) {
421 ret=xread(src,MAKE_SEGPTR(buf),BUFLEN);
422 if (ret<=0) {
423 if (ret==0)
424 break;
425 if (ret==-1)
426 return LZERROR_READ;
427 return ret;
429 len += ret;
430 wret = _lwrite(dest,buf,ret);
431 if (wret!=ret)
432 return LZERROR_WRITE;
434 return len;
435 #undef BUFLEN
439 * LZOpenFile [LZEXPAND.2]
440 * Opens a file. If not compressed, open it as a normal file.
442 HFILE
443 LZOpenFile(LPCSTR fn,LPOFSTRUCT ofs,UINT mode) {
444 HFILE fd,cfd;
446 dprintf_file(stddeb,"LZOpenFile(%s,%p,%d)\n",fn,ofs,mode);
447 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
448 fd=OpenFile(fn,ofs,mode);
449 if ((mode&~0x70)!=OF_READ)
450 return fd;
451 if (fd==HFILE_ERROR)
452 return HFILE_ERROR;
453 cfd=LZInit(fd);
454 if (cfd<=0)
455 return fd;
456 return cfd;
460 * LZClose [LZEXPAND.6]
462 void
463 LZClose(HFILE fd) {
464 int i;
466 dprintf_file(stddeb,"LZClose(%d)\n",fd);
467 for (i=0;i<nroflzstates;i++)
468 if (lzstates[i].lzfd==fd)
469 break;
470 if (i==nroflzstates) {
471 _lclose(fd);
472 return;
474 if (lzstates[i].get)
475 free(lzstates[i].get);
476 _lclose(lzstates[i].realfd);
477 memcpy(lzstates+i,lzstates+i+1,sizeof(struct lzstate)*(nroflzstates-i-1));
478 nroflzstates--;
479 lzstates=xrealloc(lzstates,sizeof(struct lzstate)*nroflzstates);
483 * CopyLZFile [LZEXPAND.8]
485 * Copy src to dest (including uncompressing src).
486 * NOTE: Yes. This is exactly the same function as LZCopy.
488 LONG
489 CopyLZFile(HFILE src,HFILE dest) {
490 dprintf_file(stddeb,"CopyLZFile(%d,%d)\n",src,dest);
491 return LZCopy(src,dest);