Sync to trunk (r44371)
[reactos.git] / reactos / dll / win32 / kernel32 / misc / lzexpand.c
1 /* $Id$
2 *
3 * LZ Decompression functions
4 *
5 * Copyright 1996 Marcus Meissner
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 *
21 * NOTES
22 *
23 * The LZ (Lempel Ziv) decompression was used in win16 installation programs.
24 * It is a simple tabledriven decompression engine, the algorithm is not
25 * documented as far as I know. WINE does not contain a compressor for
26 * this format.
27 *
28 * The implementation is complete and there have been no reports of failures
29 * for some time.
30 *
31 * TODO:
32 *
33 * o Check whether the return values are correct
34 *
35 */
36
37 //#include "config.h"
38
39 #include <k32.h>
40 #define NDEBUG
41 #include <debug.h>
42 #include "lzexpand.h"
43
44 #define HFILE_ERROR ((HFILE)-1)
45
46 /* The readahead length of the decompressor. Reading single bytes
47 * using _hread() would be SLOW.
48 */
49 #define GETLEN 2048
50
51 #define LZ_MAGIC_LEN 8
52 #define LZ_HEADER_LEN 14
53
54 /* Format of first 14 byte of LZ compressed file */
55 struct lzfileheader {
56 BYTE magic[LZ_MAGIC_LEN];
57 BYTE compressiontype;
58 CHAR lastchar;
59 DWORD reallength;
60 };
61 static const BYTE LZMagic[LZ_MAGIC_LEN]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
62
63 #define LZ_TABLE_SIZE 0x1000
64
65 struct lzstate {
66 HFILE realfd; /* the real filedescriptor */
67 CHAR lastchar; /* the last char of the filename */
68
69 DWORD reallength; /* the decompressed length of the file */
70 DWORD realcurrent; /* the position the decompressor currently is */
71 DWORD realwanted; /* the position the user wants to read from */
72
73 BYTE table[LZ_TABLE_SIZE]; /* the rotating LZ table */
74 UINT curtabent; /* CURrent TABle ENTry */
75
76 BYTE stringlen; /* length and position of current string */
77 DWORD stringpos; /* from stringtable */
78
79
80 WORD bytetype; /* bitmask within blocks */
81
82 BYTE *get; /* GETLEN bytes */
83 DWORD getcur; /* current read */
84 DWORD getlen; /* length last got */
85 };
86
87 #define MAX_LZSTATES 16
88 static struct lzstate *lzstates[MAX_LZSTATES];
89
90 #define LZ_MIN_HANDLE 0x400
91 #define IS_LZ_HANDLE(h) (((h) >= LZ_MIN_HANDLE) && ((h) < LZ_MIN_HANDLE+MAX_LZSTATES))
92 #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)-LZ_MIN_HANDLE] : NULL)
93
94 /* reads one compressed byte, including buffering */
95 #define GET(lzs,b) _lzget(lzs,&b)
96 #define GET_FLUSH(lzs) lzs->getcur=lzs->getlen;
97
98 static int
99 _lzget(struct lzstate *lzs,BYTE *b) {
100 if (lzs->getcur<lzs->getlen) {
101 *b = lzs->get[lzs->getcur++];
102 return 1;
103 } else {
104 int ret = _hread(lzs->realfd,lzs->get,GETLEN);
105 if (ret==HFILE_ERROR)
106 return HFILE_ERROR;
107 if (ret==0)
108 return 0;
109 lzs->getlen = ret;
110 lzs->getcur = 1;
111 *b = *(lzs->get);
112 return 1;
113 }
114 }
115 /* internal function, reads lzheader
116 * returns BADINHANDLE for non filedescriptors
117 * return 0 for file not compressed using LZ
118 * return UNKNOWNALG for unknown algorithm
119 * returns lzfileheader in *head
120 */
121 static INT read_header(HFILE fd,struct lzfileheader *head)
122 {
123 BYTE buf[LZ_HEADER_LEN];
124
125 if (_llseek(fd,0,SEEK_SET)==-1)
126 return LZERROR_BADINHANDLE;
127
128 /* We can't directly read the lzfileheader struct due to
129 * structure element alignment
130 */
131 if (_hread(fd,buf,LZ_HEADER_LEN)<LZ_HEADER_LEN)
132 return 0;
133 memcpy(head->magic,buf,LZ_MAGIC_LEN);
134 memcpy(&(head->compressiontype),buf+LZ_MAGIC_LEN,1);
135 memcpy(&(head->lastchar),buf+LZ_MAGIC_LEN+1,1);
136
137 /* FIXME: consider endianess on non-intel architectures */
138 memcpy(&(head->reallength),buf+LZ_MAGIC_LEN+2,4);
139
140 if (memcmp(head->magic,LZMagic,LZ_MAGIC_LEN))
141 return 0;
142 if (head->compressiontype!='A')
143 return LZERROR_UNKNOWNALG;
144 return 1;
145 }
146
147
148 /***********************************************************************
149 * LZStart (KERNEL32.@)
150 */
151 INT WINAPI LZStart(void)
152 {
153 DPRINT("(void)\n");
154 return 1;
155 }
156
157
158 /***********************************************************************
159 * LZInit (KERNEL32.@)
160 *
161 * initializes internal decompression buffers, returns lzfiledescriptor.
162 * (return value the same as hfSrc, if hfSrc is not compressed)
163 * on failure, returns error code <0
164 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
165 *
166 * since _llseek uses the same types as libc.lseek, we just use the macros of
167 * libc
168 */
169 HFILE WINAPI LZInit( HFILE hfSrc )
170 {
171
172 struct lzfileheader head;
173 struct lzstate *lzs;
174 int i, ret;
175
176 DPRINT("(%d)\n",hfSrc);
177 ret=read_header(hfSrc,&head);
178 if (ret<=0) {
179 _llseek(hfSrc,0,SEEK_SET);
180 return ret?ret:hfSrc;
181 }
182 for (i = 0; i < MAX_LZSTATES; i++) if (!lzstates[i]) break;
183 if (i == MAX_LZSTATES) return LZERROR_GLOBALLOC;
184 lzstates[i] = lzs = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct lzstate) );
185 if(lzs == NULL) return LZERROR_GLOBALLOC;
186
187 lzs->realfd = hfSrc;
188 lzs->lastchar = head.lastchar;
189 lzs->reallength = head.reallength;
190
191 lzs->get = RtlAllocateHeap( GetProcessHeap(), 0, GETLEN );
192 lzs->getlen = 0;
193 lzs->getcur = 0;
194
195 if(lzs->get == NULL) {
196 RtlFreeHeap(GetProcessHeap(), 0, lzs);
197 lzstates[i] = NULL;
198 return LZERROR_GLOBALLOC;
199 }
200
201 /* Yes, preinitialize with spaces */
202 memset(lzs->table,' ',LZ_TABLE_SIZE);
203 /* Yes, start 16 byte from the END of the table */
204 lzs->curtabent = 0xff0;
205 return LZ_MIN_HANDLE + i;
206 }
207
208
209 /***********************************************************************
210 * LZDone (KERNEL32.@)
211 */
212 void WINAPI LZDone(void)
213 {
214 DPRINT("(void)\n");
215 }
216
217
218 /***********************************************************************
219 * GetExpandedNameA (KERNEL32.@)
220 *
221 * gets the full filename of the compressed file 'in' by opening it
222 * and reading the header
223 *
224 * "file." is being translated to "file"
225 * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
226 * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
227 */
228
229 INT WINAPI GetExpandedNameA( LPSTR in, LPSTR out )
230 {
231 struct lzfileheader head;
232 HFILE fd;
233 OFSTRUCT ofs;
234 INT fnislowercased,ret,len;
235 LPSTR s,t;
236
237 DPRINT("(%s)\n",in);
238 fd=OpenFile(in,&ofs,OF_READ);
239 if (fd==HFILE_ERROR)
240 return (INT)(INT16)LZERROR_BADINHANDLE;
241 strcpy(out,in);
242 ret=read_header(fd,&head);
243 if (ret<=0) {
244 /* not a LZ compressed file, so the expanded name is the same
245 * as the input name */
246 _lclose(fd);
247 return 1;
248 }
249
250
251 /* look for directory prefix and skip it. */
252 s=out;
253 while (NULL!=(t=strpbrk(s,"/\\:")))
254 s=t+1;
255
256 /* now mangle the basename */
257 if (!*s) {
258 /* FIXME: hmm. shouldn't happen? */
259 DPRINT("Specified a directory or what? (%s)\n",in);
260 _lclose(fd);
261 return 1;
262 }
263 /* see if we should use lowercase or uppercase on the last char */
264 fnislowercased=1;
265 t=s+strlen(s)-1;
266 while (t>=out) {
267 if (!isalpha(*t)) {
268 t--;
269 continue;
270 }
271 fnislowercased=islower(*t);
272 break;
273 }
274 if (isalpha(head.lastchar)) {
275 if (fnislowercased)
276 head.lastchar=tolower(head.lastchar);
277 else
278 head.lastchar=toupper(head.lastchar);
279 }
280
281 /* now look where to replace the last character */
282 if (NULL!=(t=strchr(s,'.'))) {
283 if (t[1]=='\0') {
284 t[0]='\0';
285 } else {
286 len=strlen(t)-1;
287 if (t[len]=='_')
288 t[len]=head.lastchar;
289 }
290 } /* else no modification necessary */
291 _lclose(fd);
292 return 1;
293 }
294
295
296 /***********************************************************************
297 * GetExpandedNameW (KERNEL32.@)
298 */
299 INT WINAPI GetExpandedNameW( LPWSTR in, LPWSTR out )
300 {
301 INT ret;
302 DWORD len;
303 char *xin, *xout;
304 len = WideCharToMultiByte( CP_ACP, 0, in, -1, NULL, 0, NULL, NULL );
305 xin = RtlAllocateHeap( RtlGetProcessHeap(), 0, len );
306 if (xin == NULL)
307 return LZERROR_BADVALUE;
308 xout = RtlAllocateHeap( RtlGetProcessHeap(), 0, len+3 );
309 if (xout == NULL)
310 {
311 RtlFreeHeap( RtlGetProcessHeap(), 0, xin );
312 return LZERROR_BADVALUE;
313 }
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, wcslen(in)+4 );
317 RtlFreeHeap( RtlGetProcessHeap(), 0, xin );
318 RtlFreeHeap( RtlGetProcessHeap(), 0, xout );
319 return ret;
320 }
321
322
323 /***********************************************************************
324 * LZRead (KERNEL32.@)
325 */
326 INT WINAPI LZRead( HFILE fd, LPSTR vbuf, INT toread )
327 {
328 int howmuch;
329 BYTE b,*buf;
330 struct lzstate *lzs;
331
332 buf=(LPBYTE)vbuf;
333 DPRINT("(%d,%p,%d)\n",fd,buf,toread);
334 howmuch=toread;
335 if (!(lzs = GET_LZ_STATE(fd))) return _hread(fd,buf,toread);
336
337 /* The decompressor itself is in a define, cause we need it twice
338 * in this function. (the decompressed byte will be in b)
339 */
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; \
350 } \
351 if (lzs->bytetype & 1) { \
352 if (1!=GET(lzs,b)) \
353 return toread-howmuch; \
354 } else { \
355 BYTE b1,b2; \
356 \
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;\
372 } \
373 lzs->bytetype>>=1; \
374 } \
375 /* store b in table */ \
376 lzs->table[lzs->curtabent++]= b; \
377 lzs->curtabent &= 0xFFF; \
378 lzs->realcurrent++;
379
380 /* if someone has seeked, we have to bring the decompressor
381 * to that position
382 */
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*
387 */
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;
397 }
398 while (lzs->realcurrent<lzs->realwanted) {
399 DECOMPRESS_ONE_BYTE;
400 }
401 }
402
403 while (howmuch) {
404 DECOMPRESS_ONE_BYTE;
405 lzs->realwanted++;
406 *buf++ = b;
407 howmuch--;
408 }
409 return toread;
410 #undef DECOMPRESS_ONE_BYTE
411 }
412
413
414 /***********************************************************************
415 * LZSeek (KERNEL32.@)
416 */
417 LONG WINAPI LZSeek( HFILE fd, LONG off, INT type )
418 {
419 struct lzstate *lzs;
420 LONG newwanted;
421
422 DPRINT("(%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;
436 }
437 if (newwanted>lzs->reallength)
438 return LZERROR_BADVALUE;
439 if (newwanted<0)
440 return LZERROR_BADVALUE;
441 lzs->realwanted = newwanted;
442 return newwanted;
443 }
444
445
446 /***********************************************************************
447 * LZCopy (KERNEL32.@)
448 *
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.
452 */
453 LONG WINAPI LZCopy( HFILE src, HFILE dest )
454 {
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)
464 */
465 typedef UINT (WINAPI *_readfun)(HFILE,LPVOID,UINT);
466
467 _readfun xread;
468
469 DPRINT("(%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;
474 }
475
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;
490 }
491 len += ret;
492 wret = _hwrite(dest,buf,ret);
493 if (wret!=ret)
494 return LZERROR_WRITE;
495 }
496
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 );
501
502 /* close handle */
503 if (usedlzinit)
504 LZClose(src);
505 return len;
506 #undef BUFLEN
507 }
508
509 /* reverses GetExpandedPathname */
510 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
511 {
512 char *p;
513 char *mfn = RtlAllocateHeap( 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, '.' )))
518 {
519 p++;
520 if (strlen(p) < 3) strcat( p, "_" ); /* append '_' */
521 else p[strlen(p)-1] = '_'; /* replace last character */
522 }
523 else strcat( mfn, "._" ); /* append "._" */
524 return mfn;
525 }
526
527
528 /***********************************************************************
529 * LZOpenFileA (KERNEL32.@)
530 *
531 * Opens a file. If not compressed, open it as a normal file.
532 */
533 HFILE WINAPI LZOpenFileA( LPSTR fn, LPOFSTRUCT ofs, WORD mode )
534 {
535 HFILE fd,cfd;
536
537 DPRINT("(%s,%p,%d)\n",fn,ofs,mode);
538 /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
539 fd=OpenFile(fn,ofs,mode);
540 if (fd==HFILE_ERROR)
541 {
542 LPSTR mfn = LZEXPAND_MangleName(fn);
543 fd = OpenFile(mfn,ofs,mode);
544 RtlFreeHeap( GetProcessHeap(), 0, mfn );
545 }
546 if ((mode&~0x70)!=OF_READ)
547 return fd;
548 if (fd==HFILE_ERROR)
549 return HFILE_ERROR;
550 cfd=LZInit(fd);
551 if ((INT)cfd <= 0) return fd;
552 return cfd;
553 }
554
555
556 /***********************************************************************
557 * LZOpenFileW (KERNEL32.@)
558 */
559 HFILE WINAPI LZOpenFileW( LPWSTR fn, LPOFSTRUCT ofs, WORD mode )
560 {
561 HFILE ret;
562 DWORD len = WideCharToMultiByte( CP_ACP, 0, fn, -1, NULL, 0, NULL, NULL );
563 LPSTR xfn = RtlAllocateHeap( GetProcessHeap(), 0, len );
564 WideCharToMultiByte( CP_ACP, 0, fn, -1, xfn, len, NULL, NULL );
565 ret = LZOpenFileA(xfn,ofs,mode);
566 RtlFreeHeap( GetProcessHeap(), 0, xfn );
567 return ret;
568 }
569
570
571 /***********************************************************************
572 * LZClose (KERNEL32.@)
573 */
574 void WINAPI LZClose( HFILE fd )
575 {
576 struct lzstate *lzs;
577
578 DPRINT("(%d)\n",fd);
579 if (!(lzs = GET_LZ_STATE(fd))) _lclose(fd);
580 else
581 {
582 if (lzs->get) RtlFreeHeap( GetProcessHeap(), 0, lzs->get );
583 CloseHandle( LongToHandle(lzs->realfd) );
584 lzstates[fd - LZ_MIN_HANDLE] = NULL;
585 RtlFreeHeap( GetProcessHeap(), 0, lzs );
586 }
587 }
588