update for HEAD-2003091401
[reactos.git] / lib / kernel32 / misc / lzexpand_main.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 /*
22  * FIXME: return values might be wrong
23  */
24
25 //#include "config.h"
26
27 #define NDEBUG
28 #include <kernel32/kernel32.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <sys/types.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37
38 #include "windows.h"
39 #include "lzexpand.h"
40 #define HFILE_ERROR ((HFILE)-1)
41
42 /* The readahead length of the decompressor. Reading single bytes
43  * using _hread() would be SLOW.
44  */
45 #define GETLEN  2048
46
47 /* Format of first 14 byte of LZ compressed file */
48 struct lzfileheader {
49         BYTE    magic[8];
50         BYTE    compressiontype;
51         CHAR    lastchar;
52         DWORD   reallength;
53 };
54 static BYTE LZMagic[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33};
55
56 struct lzstate {
57         HFILE   realfd;         /* the real filedescriptor */
58         CHAR    lastchar;       /* the last char of the filename */
59
60         DWORD   reallength;     /* the decompressed length of the file */
61         DWORD   realcurrent;    /* the position the decompressor currently is */
62         DWORD   realwanted;     /* the position the user wants to read from */
63
64         BYTE    table[0x1000];  /* the rotating LZ table */
65         UINT    curtabent;      /* CURrent TABle ENTry */
66
67         BYTE    stringlen;      /* length and position of current string */
68         DWORD   stringpos;      /* from stringtable */
69
70
71         WORD    bytetype;       /* bitmask within blocks */
72
73         BYTE    *get;           /* GETLEN bytes */
74         DWORD   getcur;         /* current read */
75         DWORD   getlen;         /* length last got */
76 };
77
78 #define MAX_LZSTATES 16
79 static struct lzstate *lzstates[MAX_LZSTATES];
80
81 #define IS_LZ_HANDLE(h) (((h) >= 0x400) && ((h) < 0x400+MAX_LZSTATES))
82 #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)-0x400] : NULL)
83
84 /* reads one compressed byte, including buffering */
85 #define GET(lzs,b)      _lzget(lzs,&b)
86 #define GET_FLUSH(lzs)  lzs->getcur=lzs->getlen;
87
88 static int
89 _lzget(struct lzstate *lzs,BYTE *b) {
90         if (lzs->getcur<lzs->getlen) {
91                 *b              = lzs->get[lzs->getcur++];
92                 return          1;
93         } else {
94                 int ret = _hread(lzs->realfd,lzs->get,GETLEN);
95                 if (ret==HFILE_ERROR)
96                         return HFILE_ERROR;
97                 if (ret==0)
98                         return 0;
99                 lzs->getlen     = ret;
100                 lzs->getcur     = 1;
101                 *b              = *(lzs->get);
102                 return 1;
103         }
104 }
105 /* internal function, reads lzheader
106  * returns BADINHANDLE for non filedescriptors
107  * return 0 for file not compressed using LZ
108  * return UNKNOWNALG for unknown algorithm
109  * returns lzfileheader in *head
110  */
111 static INT read_header(HFILE fd,struct lzfileheader *head)
112 {
113         BYTE    buf[14];
114
115         if (_llseek(fd,0,SEEK_SET)==-1)
116                 return LZERROR_BADINHANDLE;
117
118         /* We can't directly read the lzfileheader struct due to
119          * structure element alignment
120          */
121         if (_hread(fd,buf,14)<14)
122                 return 0;
123         memcpy(head->magic,buf,8);
124         memcpy(&(head->compressiontype),buf+8,1);
125         memcpy(&(head->lastchar),buf+9,1);
126
127         /* FIXME: consider endianess on non-intel architectures */
128         memcpy(&(head->reallength),buf+10,4);
129
130         if (memcmp(head->magic,LZMagic,8))
131                 return 0;
132         if (head->compressiontype!='A')
133                 return LZERROR_UNKNOWNALG;
134         return 1;
135 }
136
137
138 /***********************************************************************
139  *           LZStart   (LZ32.@)
140  *
141  * @unimplemented
142  */
143 INT WINAPI LZStart(void)
144 {
145     DPRINT("(void)\n");
146     return 1;
147 }
148
149
150 /***********************************************************************
151  *           LZInit   (LZ32.@)
152  *
153  * initializes internal decompression buffers, returns lzfiledescriptor.
154  * (return value the same as hfSrc, if hfSrc is not compressed)
155  * on failure, returns error code <0
156  * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process)
157  *
158  * since _llseek uses the same types as libc.lseek, we just use the macros of
159  *  libc
160  *
161  * @implemented
162  */
163 HFILE WINAPI LZInit( HFILE hfSrc )
164 {
165
166         struct  lzfileheader    head;
167         struct  lzstate         *lzs;
168         DWORD   ret;
169         int i;
170
171         DPRINT("(%d)\n",hfSrc);
172         ret=read_header(hfSrc,&head);
173         if (ret<=0) {
174                 _llseek(hfSrc,0,SEEK_SET);
175                 return ret?ret:hfSrc;
176         }
177         for (i = 0; i < MAX_LZSTATES; i++) if (!lzstates[i]) break;
178         if (i == MAX_LZSTATES) return LZERROR_GLOBALLOC;
179         lzstates[i] = lzs = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(struct lzstate) );
180         if(lzs == NULL) return LZERROR_GLOBALLOC;
181
182         memset(lzs,'\0',sizeof(*lzs));
183         lzs->realfd     = hfSrc;
184         lzs->lastchar   = head.lastchar;
185         lzs->reallength = head.reallength;
186
187         lzs->get        = RtlAllocateHeap( GetProcessHeap(), 0, GETLEN );
188         lzs->getlen     = 0;
189         lzs->getcur     = 0;
190
191         if(lzs->get == NULL) {
192                 RtlFreeHeap(GetProcessHeap(), 0, lzs);
193                 lzstates[i] = NULL;
194                 return LZERROR_GLOBALLOC;
195         }
196
197         /* Yes, preinitialize with spaces */
198         memset(lzs->table,' ',0x1000);
199         /* Yes, start 16 byte from the END of the table */
200         lzs->curtabent  = 0xff0;
201         return 0x400 + i;
202 }
203
204
205 /***********************************************************************
206  *           LZDone   (LZEXPAND.9)
207  *           LZDone   (LZ32.@)
208  *
209  * @unimplemented
210  */
211 void WINAPI LZDone(void)
212 {
213     DPRINT("(void)\n");
214 }
215
216
217 /***********************************************************************
218  *           GetExpandedNameA   (LZ32.@)
219  *
220  * gets the full filename of the compressed file 'in' by opening it
221  * and reading the header
222  *
223  * "file." is being translated to "file"
224  * "file.bl_" (with lastchar 'a') is being translated to "file.bla"
225  * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA"
226  *
227  * @implemented
228  */
229
230 INT WINAPI GetExpandedNameA( LPSTR in, LPSTR out )
231 {
232         struct lzfileheader     head;
233         HFILE           fd;
234         OFSTRUCT        ofs;
235         INT             fnislowercased,ret,len;
236         LPSTR           s,t;
237
238         DPRINT("(%s)\n",in);
239         fd=OpenFile(in,&ofs,OF_READ);
240         if (fd==HFILE_ERROR)
241                 return (INT)(INT16)LZERROR_BADINHANDLE;
242         strcpy(out,in);
243         ret=read_header(fd,&head);
244         if (ret<=0) {
245                 /* not a LZ compressed file, so the expanded name is the same
246                  * as the input name */
247                 _lclose(fd);
248                 return 1;
249         }
250
251
252         /* look for directory prefix and skip it. */
253         s=out;
254         while (NULL!=(t=strpbrk(s,"/\\:")))
255                 s=t+1;
256
257         /* now mangle the basename */
258         if (!*s) {
259                 /* FIXME: hmm. shouldn't happen? */
260                 DPRINT("Specified a directory or what? (%s)\n",in);
261                 _lclose(fd);
262                 return 1;
263         }
264         /* see if we should use lowercase or uppercase on the last char */
265         fnislowercased=1;
266         t=s+strlen(s)-1;
267         while (t>=out) {
268                 if (!isalpha(*t)) {
269                         t--;
270                         continue;
271                 }
272                 fnislowercased=islower(*t);
273                 break;
274         }
275         if (isalpha(head.lastchar)) {
276                 if (fnislowercased)
277                         head.lastchar=tolower(head.lastchar);
278                 else
279                         head.lastchar=toupper(head.lastchar);
280         }
281
282         /* now look where to replace the last character */
283         if (NULL!=(t=strchr(s,'.'))) {
284                 if (t[1]=='\0') {
285                         t[0]='\0';
286                 } else {
287                         len=strlen(t)-1;
288                         if (t[len]=='_')
289                                 t[len]=head.lastchar;
290                 }
291         } /* else no modification necessary */
292         _lclose(fd);
293         return 1;
294 }
295
296
297 /***********************************************************************
298  *           GetExpandedNameW   (LZ32.@)
299  *
300  * @implemented
301  */
302 INT WINAPI GetExpandedNameW( LPWSTR in, LPWSTR out )
303 {
304     INT ret;
305     DWORD len = WideCharToMultiByte( CP_ACP, 0, in, -1, NULL, 0, NULL, NULL );
306     char *xin = RtlAllocateHeap( GetProcessHeap(), 0, len );
307     char *xout = RtlAllocateHeap( GetProcessHeap(), 0, len+3 );
308     WideCharToMultiByte( CP_ACP, 0, in, -1, xin, len, NULL, NULL );
309     if ((ret = GetExpandedNameA( xin, xout )) > 0)
310         MultiByteToWideChar( CP_ACP, 0, xout, -1, out, wcslen(in)+4 );
311     RtlFreeHeap( GetProcessHeap(), 0, xin );
312     RtlFreeHeap( GetProcessHeap(), 0, xout );
313     return ret;
314 }
315
316
317 /***********************************************************************
318  *           LZRead   (LZ32.@)
319  *
320  * @implemented
321  */
322 INT WINAPI LZRead( HFILE fd, LPSTR vbuf, INT toread )
323 {
324         int     howmuch;
325         BYTE    b,*buf;
326         struct  lzstate *lzs;
327
328         buf=(LPBYTE)vbuf;
329         DPRINT("(%d,%p,%d)\n",fd,buf,toread);
330         howmuch=toread;
331         if (!(lzs = GET_LZ_STATE(fd))) return _hread(fd,buf,toread);
332
333 /* The decompressor itself is in a define, cause we need it twice
334  * in this function. (the decompressed byte will be in b)
335  */
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;               \
346                         }                                               \
347                         if (lzs->bytetype & 1) {                        \
348                                 if (1!=GET(lzs,b))                      \
349                                         return toread-howmuch;          \
350                         } else {                                        \
351                                 BYTE    b1,b2;                          \
352                                                                         \
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;\
368                         }                                               \
369                         lzs->bytetype>>=1;                              \
370                 }                                                       \
371                 /* store b in table */                                  \
372                 lzs->table[lzs->curtabent++]= b;                        \
373                 lzs->curtabent  &= 0xFFF;                               \
374                 lzs->realcurrent++;
375
376         /* if someone has seeked, we have to bring the decompressor
377          * to that position
378          */
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*
383                  */
384                 if (lzs->realcurrent>lzs->realwanted) {
385                         /* flush decompressor state */
386                         _llseek(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;
393                 }
394                 while (lzs->realcurrent<lzs->realwanted) {
395                         DECOMPRESS_ONE_BYTE;
396                 }
397         }
398
399         while (howmuch) {
400                 DECOMPRESS_ONE_BYTE;
401                 lzs->realwanted++;
402                 *buf++          = b;
403                 howmuch--;
404         }
405         return  toread;
406 #undef DECOMPRESS_ONE_BYTE
407 }
408
409
410 /***********************************************************************
411  *           LZSeek   (LZ32.@)
412  *
413  * @implemented
414  */
415 LONG WINAPI LZSeek( HFILE fd, LONG off, INT type )
416 {
417         struct  lzstate *lzs;
418         LONG    newwanted;
419
420         DPRINT("(%d,%ld,%d)\n",fd,off,type);
421         /* not compressed? just use normal _llseek() */
422         if (!(lzs = GET_LZ_STATE(fd))) return _llseek(fd,off,type);
423         newwanted = lzs->realwanted;
424         switch (type) {
425         case 1: /* SEEK_CUR */
426                 newwanted      += off;
427                 break;
428         case 2: /* SEEK_END */
429                 newwanted       = lzs->reallength-off;
430                 break;
431         default:/* SEEK_SET */
432                 newwanted       = off;
433                 break;
434         }
435         if (newwanted>lzs->reallength)
436                 return LZERROR_BADVALUE;
437         if (newwanted<0)
438                 return LZERROR_BADVALUE;
439         lzs->realwanted = newwanted;
440         return newwanted;
441 }
442
443
444 /***********************************************************************
445  *           LZCopy   (LZ32.@)
446  *
447  * Copies everything from src to dest
448  * if src is a LZ compressed file, it will be uncompressed.
449  * will return the number of bytes written to dest or errors.
450  *
451  * @implemented
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
461 #define BUFLEN  1000
462         BYTE    buf[BUFLEN];
463         /* we need that weird typedef, for i can't seem to get function pointer
464          * casts right. (Or they probably just do not like WINAPI in general)
465          */
466         typedef UINT    (WINAPI *_readfun)(HFILE,LPVOID,UINT);
467
468         _readfun        xread;
469
470         DPRINT("(%d,%d)\n",src,dest);
471         if (!IS_LZ_HANDLE(src)) {
472                 src = LZInit(src);
473                 if ((INT)src <= 0) return 0;
474                 if (src != oldsrc) usedlzinit=1;
475         }
476
477         /* not compressed? just copy */
478         if (!IS_LZ_HANDLE(src))
479                 xread=(_readfun)_hread;
480         else
481                 xread=(_readfun)LZRead;
482         len=0;
483         while (1) {
484                 ret=xread(src,buf,BUFLEN);
485                 if (ret<=0) {
486                         if (ret==0)
487                                 break;
488                         if (ret==-1)
489                                 return LZERROR_READ;
490                         return ret;
491                 }
492                 len    += ret;
493                 wret    = _hwrite(dest,buf,ret);
494                 if (wret!=ret)
495                         return LZERROR_WRITE;
496         }
497
498         /* Maintain the timestamp of source file to destination file */
499         srcfd = (!(lzs = GET_LZ_STATE(src))) ? src : lzs->realfd;
500         GetFileTime((HANDLE)srcfd, NULL, NULL, &filetime);
501         SetFileTime((HANDLE)dest, NULL, NULL, &filetime);
502  
503         /* close handle */
504         if (usedlzinit)
505                 LZClose(src);
506         return len;
507 #undef BUFLEN
508 }
509
510 /* reverses GetExpandedPathname */
511 static LPSTR LZEXPAND_MangleName( LPCSTR fn )
512 {
513     char *p;
514     char *mfn = (char *)RtlAllocateHeap( GetProcessHeap(), 0,
515                                    strlen(fn) + 3 ); /* "._" and \0 */
516     if(mfn == NULL) return NULL;
517     strcpy( mfn, fn );
518     if (!(p = strrchr( mfn, '\\' ))) p = mfn;
519     if ((p = strchr( p, '.' )))
520     {
521         p++;
522         if (strlen(p) < 3) strcat( p, "_" );  /* append '_' */
523         else p[strlen(p)-1] = '_';  /* replace last character */
524     }
525     else strcat( mfn, "._" );   /* append "._" */
526     return mfn;
527 }
528
529 /***********************************************************************
530  *           LZOpenFileA   (LZ32.@)
531  *
532  * Opens a file. If not compressed, open it as a normal file.
533  *
534  * @implemented
535  */
536 HFILE WINAPI LZOpenFileA( LPSTR fn, LPOFSTRUCT ofs, WORD mode )
537 {
538         HFILE   fd,cfd;
539
540         DPRINT("(%s,%p,%d)\n",fn,ofs,mode);
541         /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */
542         fd=OpenFile(fn,ofs,mode);
543         if (fd==HFILE_ERROR)
544         {
545             LPSTR mfn = LZEXPAND_MangleName(fn);
546             fd = OpenFile(mfn,ofs,mode);
547             RtlFreeHeap( GetProcessHeap(), 0, mfn );
548         }
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;
556 }
557
558
559 /***********************************************************************
560  *           LZOpenFileW   (LZ32.@)
561  *
562  * @implemented
563  */
564 HFILE WINAPI LZOpenFileW( LPWSTR fn, LPOFSTRUCT ofs, WORD mode )
565 {
566     HFILE ret;
567     DWORD len = WideCharToMultiByte( CP_ACP, 0, fn, -1, NULL, 0, NULL, NULL );
568     LPSTR xfn = RtlAllocateHeap( GetProcessHeap(), 0, len );
569     WideCharToMultiByte( CP_ACP, 0, fn, -1, xfn, len, NULL, NULL );
570     ret = LZOpenFileA(xfn,ofs,mode);
571     RtlFreeHeap( GetProcessHeap(), 0, xfn );
572     return ret;
573 }
574
575
576 /***********************************************************************
577  *           LZClose   (LZ32.@)
578  *
579  * @implemented
580  */
581 void WINAPI LZClose( HFILE fd )
582 {
583         struct lzstate *lzs;
584
585         DPRINT("(%d)\n",fd);
586         if (!(lzs = GET_LZ_STATE(fd))) _lclose(fd);
587         else
588         {
589             if (lzs->get) RtlFreeHeap( GetProcessHeap(), 0, lzs->get );
590             CloseHandle((HANDLE)lzs->realfd);
591             lzstates[fd - 0x400] = NULL;
592             RtlFreeHeap( GetProcessHeap(), 0, lzs );
593         }
594 }
595
596
597 /***********************************************************************
598  *           CopyLZFile  (LZ32.@)
599  *
600  * Copy src to dest (including uncompressing src).
601  * NOTE: Yes. This is exactly the same function as LZCopy.
602  *
603  * @implemented
604  */
605 LONG WINAPI CopyLZFile( HFILE src, HFILE dest )
606 {
607     DPRINT("(%d,%d)\n",src,dest);
608     return LZCopy(src,dest);
609 }