winex11: Create a global vulkan instance for xrandr.
[wine.git] / dlls / kernel32 / lzexpand.c
blob4e5de516553fce1e62bc5f47b12dc230798748c5
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 <string.h>
37 #include <ctype.h>
38 #include <sys/types.h>
39 #include <stdarg.h>
40 #include <stdio.h>
42 #include "ntstatus.h"
43 #define WIN32_NO_STATUS
44 #include "windef.h"
45 #include "winbase.h"
46 #include "winnls.h"
47 #include "winternl.h"
48 #include "ddk/ntddk.h"
49 #include "lzexpand.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(file);
55 /* The readahead length of the decompressor. Reading single bytes
56 * using _lread() would be SLOW.
58 #define GETLEN 2048
60 #define LZ_MAGIC_LEN 8
61 #define LZ_HEADER_LEN 14
63 /* Format of first 14 byte of LZ compressed file */
64 struct lzfileheader {
65 BYTE magic[LZ_MAGIC_LEN];
66 BYTE compressiontype;
67 CHAR lastchar;
68 DWORD reallength;
70 static const BYTE LZMagic[LZ_MAGIC_LEN]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
72 #define LZ_TABLE_SIZE 0x1000
74 struct lzstate {
75 HFILE realfd; /* the real filedescriptor */
76 CHAR lastchar; /* the last char of the filename */
78 DWORD reallength; /* the decompressed length of the file */
79 DWORD realcurrent; /* the position the decompressor currently is */
80 DWORD realwanted; /* the position the user wants to read from */
82 BYTE table[LZ_TABLE_SIZE]; /* the rotating LZ table */
83 UINT curtabent; /* CURrent TABle ENTry */
85 BYTE stringlen; /* length and position of current string */
86 DWORD stringpos; /* from stringtable */
89 WORD bytetype; /* bitmask within blocks */
91 BYTE *get; /* GETLEN bytes */
92 DWORD getcur; /* current read */
93 DWORD getlen; /* length last got */
96 #define MAX_LZSTATES 16
97 static struct lzstate *lzstates[MAX_LZSTATES];
99 #define LZ_MIN_HANDLE 0x400
100 #define IS_LZ_HANDLE(h) (((h) >= LZ_MIN_HANDLE) && ((h) < LZ_MIN_HANDLE+MAX_LZSTATES))
101 #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)-LZ_MIN_HANDLE] : NULL)
103 /* reads one compressed byte, including buffering */
104 #define GET(lzs,b) _lzget(lzs,&b)
105 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
107 static int
108 _lzget(struct lzstate *lzs,BYTE *b) {
109 if (lzs->getcur<lzs->getlen) {
110 *b = lzs->get[lzs->getcur++];
111 return 1;
112 } else {
113 int ret = _lread(lzs->realfd,lzs->get,GETLEN);
114 if (ret==HFILE_ERROR)
115 return HFILE_ERROR;
116 if (ret==0)
117 return 0;
118 lzs->getlen = ret;
119 lzs->getcur = 1;
120 *b = *(lzs->get);
121 return 1;
124 /* internal function, reads lzheader
125 * returns BADINHANDLE for non filedescriptors
126 * return 0 for file not compressed using LZ
127 * return UNKNOWNALG for unknown algorithm
128 * returns lzfileheader in *head
130 static INT read_header(HFILE fd,struct lzfileheader *head)
132 BYTE buf[LZ_HEADER_LEN];
134 if (_llseek(fd,0,SEEK_SET)==-1)
135 return LZERROR_BADINHANDLE;
137 /* We can't directly read the lzfileheader struct due to
138 * structure element alignment
140 if (_lread(fd,buf,LZ_HEADER_LEN) != LZ_HEADER_LEN)
141 return 0;
142 memcpy(head->magic,buf,LZ_MAGIC_LEN);
143 memcpy(&(head->compressiontype),buf+LZ_MAGIC_LEN,1);
144 memcpy(&(head->lastchar),buf+LZ_MAGIC_LEN+1,1);
146 /* FIXME: consider endianness on non-intel architectures */
147 memcpy(&(head->reallength),buf+LZ_MAGIC_LEN+2,4);
149 if (memcmp(head->magic,LZMagic,LZ_MAGIC_LEN))
150 return 0;
151 if (head->compressiontype!='A')
152 return LZERROR_UNKNOWNALG;
153 return 1;
157 /***********************************************************************
158 * LZStart (KERNEL32.@)
160 INT WINAPI LZStart(void)
162 TRACE("(void)\n");
163 return 1;
167 /***********************************************************************
168 * LZInit (KERNEL32.@)
170 * initializes internal decompression buffers, returns lzfiledescriptor.
171 * (return value the same as hfSrc, if hfSrc is not compressed)
172 * on failure, returns error code <0
173 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
175 * since _llseek uses the same types as libc.lseek, we just use the macros of
176 * libc
178 HFILE WINAPI LZInit( HFILE hfSrc )
181 struct lzfileheader head;
182 struct lzstate *lzs;
183 int i, ret;
185 TRACE("(%d)\n",hfSrc);
186 ret=read_header(hfSrc,&head);
187 if (ret<=0) {
188 _llseek(hfSrc,0,SEEK_SET);
189 return ret?ret:hfSrc;
191 for (i = 0; i < MAX_LZSTATES; i++) if (!lzstates[i]) break;
192 if (i == MAX_LZSTATES) return LZERROR_GLOBALLOC;
193 lzstates[i] = lzs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*lzs) );
194 if(lzs == NULL) return LZERROR_GLOBALLOC;
196 lzs->realfd = hfSrc;
197 lzs->lastchar = head.lastchar;
198 lzs->reallength = head.reallength;
200 lzs->get = HeapAlloc( GetProcessHeap(), 0, GETLEN );
201 lzs->getlen = 0;
202 lzs->getcur = 0;
204 if(lzs->get == NULL) {
205 HeapFree(GetProcessHeap(), 0, lzs);
206 lzstates[i] = NULL;
207 return LZERROR_GLOBALLOC;
210 /* Yes, preinitialize with spaces */
211 memset(lzs->table,' ',LZ_TABLE_SIZE);
212 /* Yes, start 16 byte from the END of the table */
213 lzs->curtabent = 0xff0;
214 return LZ_MIN_HANDLE + i;
218 /***********************************************************************
219 * LZDone (KERNEL32.@)
221 void WINAPI LZDone(void)
223 TRACE("(void)\n");
227 /***********************************************************************
228 * GetExpandedNameA (KERNEL32.@)
230 * gets the full filename of the compressed file 'in' by opening it
231 * and reading the header
233 * "file." is being translated to "file"
234 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
235 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
238 INT WINAPI GetExpandedNameA( LPSTR in, LPSTR out )
240 struct lzfileheader head;
241 HFILE fd;
242 OFSTRUCT ofs;
243 INT fnislowercased,ret,len;
244 LPSTR s,t;
246 TRACE("(%s)\n",in);
247 fd=OpenFile(in,&ofs,OF_READ);
248 if (fd==HFILE_ERROR)
249 return (INT)(INT16)LZERROR_BADINHANDLE;
250 strcpy(out,in);
251 ret=read_header(fd,&head);
252 if (ret<=0) {
253 /* not a LZ compressed file, so the expanded name is the same
254 * as the input name */
255 _lclose(fd);
256 return 1;
260 /* look for directory prefix and skip it. */
261 s=out;
262 while (NULL!=(t=strpbrk(s,"/\\:")))
263 s=t+1;
265 /* now mangle the basename */
266 if (!*s) {
267 /* FIXME: hmm. shouldn't happen? */
268 WARN("Specified a directory or what? (%s)\n",in);
269 _lclose(fd);
270 return 1;
272 /* see if we should use lowercase or uppercase on the last char */
273 fnislowercased=1;
274 t=s+strlen(s)-1;
275 while (t>=out) {
276 if (!isalpha(*t)) {
277 t--;
278 continue;
280 fnislowercased=islower(*t);
281 break;
283 if (isalpha(head.lastchar)) {
284 if (fnislowercased)
285 head.lastchar=tolower(head.lastchar);
286 else
287 head.lastchar=RtlUpperChar(head.lastchar);
290 /* now look where to replace the last character */
291 if (NULL!=(t=strchr(s,'.'))) {
292 if (t[1]=='\0') {
293 t[0]='\0';
294 } else {
295 len=strlen(t)-1;
296 if (t[len]=='_')
297 t[len]=head.lastchar;
299 } /* else no modification necessary */
300 _lclose(fd);
301 return 1;
305 /***********************************************************************
306 * GetExpandedNameW (KERNEL32.@)
308 INT WINAPI GetExpandedNameW( LPWSTR in, LPWSTR out )
310 INT ret;
311 DWORD len = WideCharToMultiByte( CP_ACP, 0, in, -1, NULL, 0, NULL, NULL );
312 char *xin = HeapAlloc( GetProcessHeap(), 0, len );
313 char *xout = HeapAlloc( GetProcessHeap(), 0, len+3 );
314 WideCharToMultiByte( CP_ACP, 0, in, -1, xin, len, NULL, NULL );
315 if ((ret = GetExpandedNameA( xin, xout )) > 0)
316 MultiByteToWideChar( CP_ACP, 0, xout, -1, out, lstrlenW(in)+4 );
317 HeapFree( GetProcessHeap(), 0, xin );
318 HeapFree( GetProcessHeap(), 0, xout );
319 return ret;
323 /***********************************************************************
324 * LZRead (KERNEL32.@)
326 INT WINAPI LZRead( HFILE fd, LPSTR vbuf, INT toread )
328 int howmuch;
329 BYTE b,*buf;
330 struct lzstate *lzs;
332 buf=(LPBYTE)vbuf;
333 TRACE("(%d,%p,%d)\n",fd,buf,toread);
334 howmuch=toread;
335 if (!(lzs = GET_LZ_STATE(fd))) return _lread(fd,buf,toread);
337 /* The decompressor itself is in a define, cause we need it twice
338 * in this function. (the decompressed byte will be in b)
340 #define DECOMPRESS_ONE_BYTE \
341 if (lzs->stringlen) { \
342 b = lzs->table[lzs->stringpos]; \
343 lzs->stringpos = (lzs->stringpos+1)&0xFFF; \
344 lzs->stringlen--; \
345 } else { \
346 if (!(lzs->bytetype&0x100)) { \
347 if (1!=GET(lzs,b)) \
348 return toread-howmuch; \
349 lzs->bytetype = b|0xFF00; \
351 if (lzs->bytetype & 1) { \
352 if (1!=GET(lzs,b)) \
353 return toread-howmuch; \
354 } else { \
355 BYTE b1,b2; \
357 if (1!=GET(lzs,b1)) \
358 return toread-howmuch; \
359 if (1!=GET(lzs,b2)) \
360 return toread-howmuch; \
361 /* Format: \
362 * b1 b2 \
363 * AB CD \
364 * where CAB is the stringoffset in the table\
365 * and D+3 is the len of the string \
366 */ \
367 lzs->stringpos = b1|((b2&0xf0)<<4); \
368 lzs->stringlen = (b2&0xf)+2; \
369 /* 3, but we use a byte already below ... */\
370 b = lzs->table[lzs->stringpos];\
371 lzs->stringpos = (lzs->stringpos+1)&0xFFF;\
373 lzs->bytetype>>=1; \
375 /* store b in table */ \
376 lzs->table[lzs->curtabent++]= b; \
377 lzs->curtabent &= 0xFFF; \
378 lzs->realcurrent++;
380 /* if someone has seeked, we have to bring the decompressor
381 * to that position
383 if (lzs->realcurrent!=lzs->realwanted) {
384 /* if the wanted position is before the current position
385 * I see no easy way to unroll ... We have to restart at
386 * the beginning. *sigh*
388 if (lzs->realcurrent>lzs->realwanted) {
389 /* flush decompressor state */
390 _llseek(lzs->realfd,LZ_HEADER_LEN,SEEK_SET);
391 GET_FLUSH(lzs);
392 lzs->realcurrent= 0;
393 lzs->bytetype = 0;
394 lzs->stringlen = 0;
395 memset(lzs->table,' ',LZ_TABLE_SIZE);
396 lzs->curtabent = 0xFF0;
398 while (lzs->realcurrent<lzs->realwanted) {
399 DECOMPRESS_ONE_BYTE;
403 while (howmuch) {
404 DECOMPRESS_ONE_BYTE;
405 lzs->realwanted++;
406 *buf++ = b;
407 howmuch--;
409 return toread;
410 #undef DECOMPRESS_ONE_BYTE
414 /***********************************************************************
415 * LZSeek (KERNEL32.@)
417 LONG WINAPI LZSeek( HFILE fd, LONG off, INT type )
419 struct lzstate *lzs;
420 LONG newwanted;
422 TRACE("(%d,%ld,%d)\n",fd,off,type);
423 /* not compressed? just use normal _llseek() */
424 if (!(lzs = GET_LZ_STATE(fd))) return _llseek(fd,off,type);
425 newwanted = lzs->realwanted;
426 switch (type) {
427 case 1: /* SEEK_CUR */
428 newwanted += off;
429 break;
430 case 2: /* SEEK_END */
431 newwanted = lzs->reallength-off;
432 break;
433 default:/* SEEK_SET */
434 newwanted = off;
435 break;
437 if (newwanted>lzs->reallength)
438 return LZERROR_BADVALUE;
439 if (newwanted<0)
440 return LZERROR_BADVALUE;
441 lzs->realwanted = newwanted;
442 return newwanted;
446 /***********************************************************************
447 * LZCopy (KERNEL32.@)
449 * Copies everything from src to dest
450 * if src is a LZ compressed file, it will be uncompressed.
451 * will return the number of bytes written to dest or errors.
453 LONG WINAPI LZCopy( HFILE src, HFILE dest )
455 int usedlzinit = 0, ret, wret;
456 LONG len;
457 HFILE oldsrc = src, srcfd;
458 FILETIME filetime;
459 struct lzstate *lzs;
460 #define BUFLEN 1000
461 CHAR buf[BUFLEN];
462 /* we need that weird typedef, for i can't seem to get function pointer
463 * casts right. (Or they probably just do not like WINAPI in general)
465 typedef UINT (WINAPI *_readfun)(HFILE,LPVOID,UINT);
467 _readfun xread;
469 TRACE("(%d,%d)\n",src,dest);
470 if (!IS_LZ_HANDLE(src)) {
471 src = LZInit(src);
472 if ((INT)src <= 0) return 0;
473 if (src != oldsrc) usedlzinit=1;
476 /* not compressed? just copy */
477 if (!IS_LZ_HANDLE(src))
478 xread=_lread;
479 else
480 xread=(_readfun)LZRead;
481 len=0;
482 while (1) {
483 ret=xread(src,buf,BUFLEN);
484 if (ret<=0) {
485 if (ret==0)
486 break;
487 if (ret==-1)
488 return LZERROR_READ;
489 return ret;
491 len += ret;
492 wret = _lwrite(dest,buf,ret);
493 if (wret!=ret)
494 return LZERROR_WRITE;
497 /* Maintain the timestamp of source file to destination file */
498 srcfd = (!(lzs = GET_LZ_STATE(src))) ? src : lzs->realfd;
499 GetFileTime( LongToHandle(srcfd), NULL, NULL, &filetime );
500 SetFileTime( LongToHandle(dest), NULL, NULL, &filetime );
502 /* close handle */
503 if (usedlzinit)
504 LZClose(src);
505 return len;
506 #undef BUFLEN
509 /* reverses GetExpandedPathname */
510 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
512 char *p;
513 char *mfn = HeapAlloc( GetProcessHeap(), 0, strlen(fn) + 3 ); /* "._" and \0 */
514 if(mfn == NULL) return NULL;
515 strcpy( mfn, fn );
516 if (!(p = strrchr( mfn, '\\' ))) p = mfn;
517 if ((p = strchr( p, '.' )))
519 p++;
520 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
521 else p[strlen(p)-1] = '_'; /* replace last character */
523 else strcat( mfn, "._" ); /* append "._" */
524 return mfn;
528 /***********************************************************************
529 * LZOpenFileA (KERNEL32.@)
531 * Opens a file. If not compressed, open it as a normal file.
533 HFILE WINAPI LZOpenFileA( LPSTR fn, LPOFSTRUCT ofs, WORD mode )
535 HFILE fd,cfd;
536 BYTE ofs_cBytes = ofs->cBytes;
538 TRACE("(%s,%p,%d)\n",fn,ofs,mode);
539 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
540 fd=OpenFile(fn,ofs,mode);
541 if (fd==HFILE_ERROR)
543 LPSTR mfn = LZEXPAND_MangleName(fn);
544 fd = OpenFile(mfn,ofs,mode);
545 HeapFree( GetProcessHeap(), 0, mfn );
547 if (fd==HFILE_ERROR)
548 ofs->cBytes = ofs_cBytes;
549 if ((mode&~0x70)!=OF_READ)
550 return fd;
551 if (fd==HFILE_ERROR)
552 return HFILE_ERROR;
553 cfd=LZInit(fd);
554 if ((INT)cfd <= 0) return fd;
555 return cfd;
559 /***********************************************************************
560 * LZOpenFileW (KERNEL32.@)
562 HFILE WINAPI LZOpenFileW( LPWSTR fn, LPOFSTRUCT ofs, WORD mode )
564 HFILE ret;
565 DWORD len = WideCharToMultiByte( CP_ACP, 0, fn, -1, NULL, 0, NULL, NULL );
566 LPSTR xfn = HeapAlloc( GetProcessHeap(), 0, len );
567 WideCharToMultiByte( CP_ACP, 0, fn, -1, xfn, len, NULL, NULL );
568 ret = LZOpenFileA(xfn,ofs,mode);
569 HeapFree( GetProcessHeap(), 0, xfn );
570 return ret;
574 /***********************************************************************
575 * LZClose (KERNEL32.@)
577 void WINAPI LZClose( HFILE fd )
579 struct lzstate *lzs;
581 TRACE("(%d)\n",fd);
582 if (!(lzs = GET_LZ_STATE(fd))) _lclose(fd);
583 else
585 HeapFree( GetProcessHeap(), 0, lzs->get );
586 CloseHandle( LongToHandle(lzs->realfd) );
587 lzstates[fd - LZ_MIN_HANDLE] = NULL;
588 HeapFree( GetProcessHeap(), 0, lzs );