| 1 | /* $Id: lz32.cpp,v 1.6 1999-08-17 19:30:49 phaller Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 5 | * | 
|---|
| 6 | * LZ Decompression functions | 
|---|
| 7 | * | 
|---|
| 8 | * Copyright 1996 Marcus Meissner | 
|---|
| 9 | * Copyright 1999 Patrick Haller | 
|---|
| 10 | * | 
|---|
| 11 | * FIXME: return values might be wrong | 
|---|
| 12 | */ | 
|---|
| 13 |  | 
|---|
| 14 | #include <string.h> | 
|---|
| 15 | #include <ctype.h> | 
|---|
| 16 | #include <odin.h> | 
|---|
| 17 | #include <os2sel.h> | 
|---|
| 18 | #include <odinwrap.h> | 
|---|
| 19 | #include <windef.h> | 
|---|
| 20 | #include <winbase.h> | 
|---|
| 21 | #include <heap.h> | 
|---|
| 22 | #include <heapstring.h> | 
|---|
| 23 | #include <lz32.h> | 
|---|
| 24 | #include "debugtools.h" | 
|---|
| 25 |  | 
|---|
| 26 |  | 
|---|
| 27 | ODINDEBUGCHANNEL(LZ32) | 
|---|
| 28 |  | 
|---|
| 29 | // @@@PH WINE compatibility macros | 
|---|
| 30 | LPWSTR WINAPI lstrcpyAtoW    (LPWSTR unicode, LPSTR ascii); | 
|---|
| 31 | #define SystemHeap GetProcessHeap() | 
|---|
| 32 | #define dprintf(a) | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | /**************************************************************************** | 
|---|
| 36 | * Defines                                                                  * | 
|---|
| 37 | ****************************************************************************/ | 
|---|
| 38 |  | 
|---|
| 39 | /* The readahead length of the decompressor. Reading single bytes | 
|---|
| 40 | * using _lread() would be SLOW. | 
|---|
| 41 | */ | 
|---|
| 42 | #define  GETLEN   2048 | 
|---|
| 43 |  | 
|---|
| 44 |  | 
|---|
| 45 | /**************************************************************************** | 
|---|
| 46 | * Structures                                                               * | 
|---|
| 47 | ****************************************************************************/ | 
|---|
| 48 |  | 
|---|
| 49 | /* Format of first 14 byte of LZ compressed file */ | 
|---|
| 50 | struct lzfileheader { | 
|---|
| 51 | BYTE  magic[8]; | 
|---|
| 52 | BYTE  compressiontype; | 
|---|
| 53 | CHAR  lastchar; | 
|---|
| 54 | DWORD reallength; | 
|---|
| 55 | }; | 
|---|
| 56 | static BYTE LZMagic[8]={'S','Z','D','D',0x88,0xf0,0x27,0x33}; | 
|---|
| 57 |  | 
|---|
| 58 | struct lzstate { | 
|---|
| 59 | HFILE realfd;                         /* the real filedescriptor */ | 
|---|
| 60 | CHAR  lastchar;                       /* the last char of the filename */ | 
|---|
| 61 |  | 
|---|
| 62 | DWORD reallength;                     /* the decompressed length of the file */ | 
|---|
| 63 | DWORD realcurrent;                    /* the position the decompressor currently is */ | 
|---|
| 64 | DWORD realwanted;                     /* the position the user wants to read from */ | 
|---|
| 65 |  | 
|---|
| 66 | BYTE  table[0x1000];                  /* the rotating LZ table */ | 
|---|
| 67 | UINT  curtabent;                      /* CURrent TABle ENTry */ | 
|---|
| 68 |  | 
|---|
| 69 | BYTE  stringlen;                      /* length and position of current string */ | 
|---|
| 70 | DWORD stringpos;                      /* from stringtable */ | 
|---|
| 71 |  | 
|---|
| 72 |  | 
|---|
| 73 | WORD  bytetype;                       /* bitmask within blocks */ | 
|---|
| 74 |  | 
|---|
| 75 | BYTE  *get;    /* GETLEN bytes */ | 
|---|
| 76 | DWORD getcur;                         /* current read */ | 
|---|
| 77 | DWORD getlen;                         /* length last got */ | 
|---|
| 78 | }; | 
|---|
| 79 |  | 
|---|
| 80 | #define MAX_LZSTATES 16 | 
|---|
| 81 | static struct lzstate *lzstates[MAX_LZSTATES]; | 
|---|
| 82 |  | 
|---|
| 83 | #define IS_LZ_HANDLE(h) (((h) >= 0x400) && ((h) < 0x400+MAX_LZSTATES)) | 
|---|
| 84 | #define GET_LZ_STATE(h) (IS_LZ_HANDLE(h) ? lzstates[(h)-0x400] : NULL) | 
|---|
| 85 |  | 
|---|
| 86 | /* reads one compressed byte, including buffering */ | 
|---|
| 87 | #define GET(lzs,b)                       _lzget(lzs,&b) | 
|---|
| 88 | #define GET_FLUSH(lzs)                   lzs->getcur=lzs->getlen; | 
|---|
| 89 |  | 
|---|
| 90 |  | 
|---|
| 91 | /**************************************************************************** | 
|---|
| 92 | * Internal Prototypes                                                      * | 
|---|
| 93 | ****************************************************************************/ | 
|---|
| 94 |  | 
|---|
| 95 | static int _lzget(struct lzstate *lzs,BYTE *b); | 
|---|
| 96 | static INT read_header(HFILE fd,struct lzfileheader *head); | 
|---|
| 97 |  | 
|---|
| 98 |  | 
|---|
| 99 | /**************************************************************************** | 
|---|
| 100 | * Implementation                                                           * | 
|---|
| 101 | ****************************************************************************/ | 
|---|
| 102 |  | 
|---|
| 103 | static int _lzget(struct lzstate *lzs,BYTE *b) | 
|---|
| 104 | { | 
|---|
| 105 | if (lzs->getcur<lzs->getlen) { | 
|---|
| 106 | *b    = lzs->get[lzs->getcur++]; | 
|---|
| 107 | return      1; | 
|---|
| 108 | } else { | 
|---|
| 109 | int ret = _lread(lzs->realfd,lzs->get,GETLEN); | 
|---|
| 110 | if (ret==HFILE_ERROR) | 
|---|
| 111 | return HFILE_ERROR; | 
|---|
| 112 | if (ret==0) | 
|---|
| 113 | return 0; | 
|---|
| 114 | lzs->getlen = ret; | 
|---|
| 115 | lzs->getcur = 1; | 
|---|
| 116 | *b    = *(lzs->get); | 
|---|
| 117 | return 1; | 
|---|
| 118 | } | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 |  | 
|---|
| 122 | /* internal function, reads lzheader | 
|---|
| 123 | * returns BADINHANDLE for non filedescriptors | 
|---|
| 124 | * return 0 for file not compressed using LZ | 
|---|
| 125 | * return UNKNOWNALG for unknown algorithm | 
|---|
| 126 | * returns lzfileheader in *head | 
|---|
| 127 | */ | 
|---|
| 128 | static INT read_header(HFILE fd,struct lzfileheader *head) | 
|---|
| 129 | { | 
|---|
| 130 | BYTE  buf[14]; | 
|---|
| 131 |  | 
|---|
| 132 | if (_llseek(fd,0,SEEK_SET)==-1) | 
|---|
| 133 | return LZERROR_BADINHANDLE; | 
|---|
| 134 |  | 
|---|
| 135 | /* We can't directly read the lzfileheader struct due to | 
|---|
| 136 | * structure element alignment | 
|---|
| 137 | */ | 
|---|
| 138 | if (_lread(fd,buf,14)<14) | 
|---|
| 139 | return 0; | 
|---|
| 140 | memcpy(head->magic,buf,8); | 
|---|
| 141 | memcpy(&(head->compressiontype),buf+8,1); | 
|---|
| 142 | memcpy(&(head->lastchar),buf+9,1); | 
|---|
| 143 |  | 
|---|
| 144 | /* FIXME: consider endianess on non-intel architectures */ | 
|---|
| 145 | memcpy(&(head->reallength),buf+10,4); | 
|---|
| 146 |  | 
|---|
| 147 | if (memcmp(head->magic,LZMagic,8)) | 
|---|
| 148 | return 0; | 
|---|
| 149 | if (head->compressiontype!='A') | 
|---|
| 150 | return LZERROR_UNKNOWNALG; | 
|---|
| 151 | return 1; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 |  | 
|---|
| 155 | /*********************************************************************** | 
|---|
| 156 | *           LZStart32   (LZ32.6) | 
|---|
| 157 | */ | 
|---|
| 158 |  | 
|---|
| 159 | ODINFUNCTION0(INT,LZStart) | 
|---|
| 160 | { | 
|---|
| 161 | dprintf(("LZ32: LZStart32()\n")); | 
|---|
| 162 | return 1; | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 |  | 
|---|
| 166 | /*********************************************************************** | 
|---|
| 167 | *           LZInit32   (LZ32.2) | 
|---|
| 168 | * | 
|---|
| 169 | * initializes internal decompression buffers, returns lzfiledescriptor. | 
|---|
| 170 | * (return value the same as hfSrc, if hfSrc is not compressed) | 
|---|
| 171 | * on failure, returns error code <0 | 
|---|
| 172 | * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process) | 
|---|
| 173 | * | 
|---|
| 174 | * since _llseek uses the same types as libc.lseek, we just use the macros of | 
|---|
| 175 | *  libc | 
|---|
| 176 | */ | 
|---|
| 177 | ODINFUNCTION1(HFILE,LZInit,HFILE,hfSrc) | 
|---|
| 178 | { | 
|---|
| 179 | struct   lzfileheader                 head; | 
|---|
| 180 | struct   lzstate                         *lzs; | 
|---|
| 181 | DWORD ret; | 
|---|
| 182 | int i; | 
|---|
| 183 |  | 
|---|
| 184 | dprintf(("LZ32: LZInit(%08xh)\n", | 
|---|
| 185 | hfSrc)); | 
|---|
| 186 |  | 
|---|
| 187 | ret=read_header(hfSrc,&head); | 
|---|
| 188 | if (ret<=0) { | 
|---|
| 189 | _llseek(hfSrc,0,SEEK_SET); | 
|---|
| 190 | return ret?ret:hfSrc; | 
|---|
| 191 | } | 
|---|
| 192 | for (i = 0; i < MAX_LZSTATES; i++) if (!lzstates[i]) break; | 
|---|
| 193 | if (i == MAX_LZSTATES) return LZERROR_GLOBALLOC; | 
|---|
| 194 |  | 
|---|
| 195 | lzstates[i] = lzs = (lzstate*)HeapAlloc( SystemHeap, 0, sizeof(struct lzstate) ); | 
|---|
| 196 |  | 
|---|
| 197 | memset(lzs,'\0',sizeof(*lzs)); | 
|---|
| 198 | lzs->realfd = hfSrc; | 
|---|
| 199 | lzs->lastchar  = head.lastchar; | 
|---|
| 200 | lzs->reallength = head.reallength; | 
|---|
| 201 |  | 
|---|
| 202 | lzs->get = (BYTE*)HEAP_xalloc( GetProcessHeap(), 0, GETLEN ); | 
|---|
| 203 | lzs->getlen = 0; | 
|---|
| 204 | lzs->getcur = 0; | 
|---|
| 205 |  | 
|---|
| 206 | /* Yes, preinitialize with spaces */ | 
|---|
| 207 | memset(lzs->table,' ',0x1000); | 
|---|
| 208 | /* Yes, start 16 byte from the END of the table */ | 
|---|
| 209 | lzs->curtabent = 0xff0; | 
|---|
| 210 | return 0x400 + i; | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 |  | 
|---|
| 214 | /*********************************************************************** | 
|---|
| 215 | *           LZDone   (LZEXPAND.9) (LZ32.8) | 
|---|
| 216 | */ | 
|---|
| 217 | ODINPROCEDURE0(LZDone) | 
|---|
| 218 | { | 
|---|
| 219 | TRACE("(void)\n"); | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 | /*********************************************************************** | 
|---|
| 223 | *           GetExpandedName32A   (LZ32.9) | 
|---|
| 224 | * | 
|---|
| 225 | * gets the full filename of the compressed file 'in' by opening it | 
|---|
| 226 | * and reading the header | 
|---|
| 227 | * | 
|---|
| 228 | * "file." is being translated to "file" | 
|---|
| 229 | * "file.bl_" (with lastchar 'a') is being translated to "file.bla" | 
|---|
| 230 | * "FILE.BL_" (with lastchar 'a') is being translated to "FILE.BLA" | 
|---|
| 231 | */ | 
|---|
| 232 |  | 
|---|
| 233 | ODINFUNCTION2(INT,GetExpandedNameA,LPCSTR,in,LPSTR,out) | 
|---|
| 234 | { | 
|---|
| 235 | struct lzfileheader                   head; | 
|---|
| 236 | HFILE    fd; | 
|---|
| 237 | OFSTRUCT ofs; | 
|---|
| 238 | INT      fnislowercased,ret,len; | 
|---|
| 239 | LPSTR    s,t; | 
|---|
| 240 |  | 
|---|
| 241 | dprintf(("LZ32: GetExpandedNameA(%s,%08xh)\n", | 
|---|
| 242 | in, | 
|---|
| 243 | out)); | 
|---|
| 244 |  | 
|---|
| 245 | fd=OpenFile(in,&ofs,OF_READ); | 
|---|
| 246 | if (fd==HFILE_ERROR) | 
|---|
| 247 | return (INT)(INT16)LZERROR_BADINHANDLE; | 
|---|
| 248 | strcpy(out,in); | 
|---|
| 249 | ret=read_header(fd,&head); | 
|---|
| 250 | if (ret<=0) { | 
|---|
| 251 | /* not a LZ compressed file, so the expanded name is the same | 
|---|
| 252 | * as the input name */ | 
|---|
| 253 | _lclose(fd); | 
|---|
| 254 | return 1; | 
|---|
| 255 | } | 
|---|
| 256 |  | 
|---|
| 257 |  | 
|---|
| 258 | /* look for directory prefix and skip it. */ | 
|---|
| 259 | s=out; | 
|---|
| 260 | while (NULL!=(t=strpbrk(s,"/\\:"))) | 
|---|
| 261 | s=t+1; | 
|---|
| 262 |  | 
|---|
| 263 | /* now mangle the basename */ | 
|---|
| 264 | if (!*s) { | 
|---|
| 265 | /* FIXME: hmm. shouldn't happen? */ | 
|---|
| 266 | WARN("Specified a directory or what? (%s)\n",in); | 
|---|
| 267 | _lclose(fd); | 
|---|
| 268 | return 1; | 
|---|
| 269 | } | 
|---|
| 270 | /* see if we should use lowercase or uppercase on the last char */ | 
|---|
| 271 | fnislowercased=1; | 
|---|
| 272 | t=s+strlen(s)-1; | 
|---|
| 273 | while (t>=out) { | 
|---|
| 274 | if (!isalpha(*t)) { | 
|---|
| 275 | t--; | 
|---|
| 276 | continue; | 
|---|
| 277 | } | 
|---|
| 278 | fnislowercased=islower(*t); | 
|---|
| 279 | break; | 
|---|
| 280 | } | 
|---|
| 281 | if (isalpha(head.lastchar)) { | 
|---|
| 282 | if (fnislowercased) | 
|---|
| 283 | head.lastchar=tolower(head.lastchar); | 
|---|
| 284 | else | 
|---|
| 285 | head.lastchar=toupper(head.lastchar); | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 | /* now look where to replace the last character */ | 
|---|
| 289 | if (NULL!=(t=strchr(s,'.'))) { | 
|---|
| 290 | if (t[1]=='\0') { | 
|---|
| 291 | t[0]='\0'; | 
|---|
| 292 | } else { | 
|---|
| 293 | len=strlen(t)-1; | 
|---|
| 294 | if (t[len]=='_') | 
|---|
| 295 | t[len]=head.lastchar; | 
|---|
| 296 | } | 
|---|
| 297 | } /* else no modification necessary */ | 
|---|
| 298 | _lclose(fd); | 
|---|
| 299 | return 1; | 
|---|
| 300 | } | 
|---|
| 301 |  | 
|---|
| 302 |  | 
|---|
| 303 | /*********************************************************************** | 
|---|
| 304 | *           GetExpandedName32W   (LZ32.11) | 
|---|
| 305 | */ | 
|---|
| 306 | ODINFUNCTION2(INT,GetExpandedNameW,LPCWSTR,in,LPWSTR,out) | 
|---|
| 307 | { | 
|---|
| 308 | char  *xin,*xout; | 
|---|
| 309 | INT   ret; | 
|---|
| 310 |  | 
|---|
| 311 | dprintf(("LZ32: GetExpandedNameW(%08xh,%08xh)\n", | 
|---|
| 312 | in, | 
|---|
| 313 | out)); | 
|---|
| 314 |  | 
|---|
| 315 | xout  = (char*)HeapAlloc( GetProcessHeap(), 0, lstrlenW(in)+3 ); | 
|---|
| 316 | xin   = HEAP_strdupWtoA( GetProcessHeap(), 0, in ); | 
|---|
| 317 | ret   = GetExpandedNameA(xin,xout); | 
|---|
| 318 | if (ret>0) lstrcpyAtoW(out,xout); | 
|---|
| 319 | HeapFree( GetProcessHeap(), 0, xin ); | 
|---|
| 320 | HeapFree( GetProcessHeap(), 0, xout ); | 
|---|
| 321 | return   ret; | 
|---|
| 322 | } | 
|---|
| 323 |  | 
|---|
| 324 |  | 
|---|
| 325 | /*********************************************************************** | 
|---|
| 326 | *           LZRead32   (LZ32.4) | 
|---|
| 327 | */ | 
|---|
| 328 | ODINFUNCTION3(INT,LZRead,HFILE,fd,LPVOID,vbuf,UINT,toread) | 
|---|
| 329 | { | 
|---|
| 330 | int   howmuch; | 
|---|
| 331 | BYTE  b,*buf; | 
|---|
| 332 | struct   lzstate                      *lzs; | 
|---|
| 333 |  | 
|---|
| 334 | dprintf(("LZ32: LZRead(%08xh,%08xh,%08h)\n", | 
|---|
| 335 | fd, | 
|---|
| 336 | vbuf, | 
|---|
| 337 | toread)); | 
|---|
| 338 |  | 
|---|
| 339 | buf=(LPBYTE)vbuf; | 
|---|
| 340 | howmuch=toread; | 
|---|
| 341 | if (!(lzs = GET_LZ_STATE(fd))) return _lread(fd,buf,toread); | 
|---|
| 342 |  | 
|---|
| 343 | /* The decompressor itself is in a define, cause we need it twice | 
|---|
| 344 | * in this function. (the decompressed byte will be in b) | 
|---|
| 345 | */ | 
|---|
| 346 | #define DECOMPRESS_ONE_BYTE                             \ | 
|---|
| 347 | if (lzs->stringlen) {                          \ | 
|---|
| 348 | b     = lzs->table[lzs->stringpos];   \ | 
|---|
| 349 | lzs->stringpos                  = (lzs->stringpos+1)&0xFFF;   \ | 
|---|
| 350 | lzs->stringlen--;                        \ | 
|---|
| 351 | } else {                                    \ | 
|---|
| 352 | if (!(lzs->bytetype&0x100)) {         \ | 
|---|
| 353 | if (1!=GET(lzs,b))                 \ | 
|---|
| 354 | return toread-howmuch;       \ | 
|---|
| 355 | lzs->bytetype = b|0xFF00;       \ | 
|---|
| 356 | }                                     \ | 
|---|
| 357 | if (lzs->bytetype & 1) {              \ | 
|---|
| 358 | if (1!=GET(lzs,b))                 \ | 
|---|
| 359 | return toread-howmuch;       \ | 
|---|
| 360 | } else {                                 \ | 
|---|
| 361 | BYTE  b1,b2;                          \ | 
|---|
| 362 | \ | 
|---|
| 363 | if (1!=GET(lzs,b1))                \ | 
|---|
| 364 | return toread-howmuch;       \ | 
|---|
| 365 | if (1!=GET(lzs,b2))                \ | 
|---|
| 366 | return toread-howmuch;       \ | 
|---|
| 367 | /* Format:                            \ | 
|---|
| 368 | * b1 b2                              \ | 
|---|
| 369 | * AB CD                              \ | 
|---|
| 370 | * where CAB is the stringoffset in the table\ | 
|---|
| 371 | * and D+3 is the len of the string   \ | 
|---|
| 372 | */                                   \ | 
|---|
| 373 | lzs->stringpos               = b1|((b2&0xf0)<<4); \ | 
|---|
| 374 | lzs->stringlen               = (b2&0xf)+2;     \ | 
|---|
| 375 | /* 3, but we use a  byte already below ... */\ | 
|---|
| 376 | b     = lzs->table[lzs->stringpos];\ | 
|---|
| 377 | lzs->stringpos               = (lzs->stringpos+1)&0xFFF;\ | 
|---|
| 378 | }                                     \ | 
|---|
| 379 | lzs->bytetype>>=1;                       \ | 
|---|
| 380 | }                                        \ | 
|---|
| 381 | /* store b in table */                         \ | 
|---|
| 382 | lzs->table[lzs->curtabent++]= b;         \ | 
|---|
| 383 | lzs->curtabent                     &= 0xFFF;            \ | 
|---|
| 384 | lzs->realcurrent++; | 
|---|
| 385 |  | 
|---|
| 386 | /* if someone has seeked, we have to bring the decompressor | 
|---|
| 387 | * to that position | 
|---|
| 388 | */ | 
|---|
| 389 | if (lzs->realcurrent!=lzs->realwanted) { | 
|---|
| 390 | /* if the wanted position is before the current position | 
|---|
| 391 | * I see no easy way to unroll ... We have to restart at | 
|---|
| 392 | * the beginning. *sigh* | 
|---|
| 393 | */ | 
|---|
| 394 | if (lzs->realcurrent>lzs->realwanted) { | 
|---|
| 395 | /* flush decompressor state */ | 
|---|
| 396 | _llseek(lzs->realfd,14,SEEK_SET); | 
|---|
| 397 | GET_FLUSH(lzs); | 
|---|
| 398 | lzs->realcurrent= 0; | 
|---|
| 399 | lzs->bytetype                   = 0; | 
|---|
| 400 | lzs->stringlen                  = 0; | 
|---|
| 401 | memset(lzs->table,' ',0x1000); | 
|---|
| 402 | lzs->curtabent                  = 0xFF0; | 
|---|
| 403 | } | 
|---|
| 404 | while (lzs->realcurrent<lzs->realwanted) { | 
|---|
| 405 | DECOMPRESS_ONE_BYTE; | 
|---|
| 406 | } | 
|---|
| 407 | } | 
|---|
| 408 |  | 
|---|
| 409 | while (howmuch) { | 
|---|
| 410 | DECOMPRESS_ONE_BYTE; | 
|---|
| 411 | lzs->realwanted++; | 
|---|
| 412 | *buf++      = b; | 
|---|
| 413 | howmuch--; | 
|---|
| 414 | } | 
|---|
| 415 | return   toread; | 
|---|
| 416 | #undef DECOMPRESS_ONE_BYTE | 
|---|
| 417 | } | 
|---|
| 418 |  | 
|---|
| 419 |  | 
|---|
| 420 | /*********************************************************************** | 
|---|
| 421 | *           LZSeek32   (LZ32.3) | 
|---|
| 422 | */ | 
|---|
| 423 |  | 
|---|
| 424 | ODINFUNCTION3(LONG,LZSeek,HFILE,fd,LONG,off,INT,type) | 
|---|
| 425 | { | 
|---|
| 426 | struct   lzstate                      *lzs; | 
|---|
| 427 | LONG  newwanted; | 
|---|
| 428 |  | 
|---|
| 429 | dprintf(("LZ32: LZSeek(%08xh,%08xh,%08xh)\n", | 
|---|
| 430 | fd, | 
|---|
| 431 | off, | 
|---|
| 432 | type)); | 
|---|
| 433 |  | 
|---|
| 434 | /* not compressed? just use normal _llseek() */ | 
|---|
| 435 | if (!(lzs = GET_LZ_STATE(fd))) return _llseek(fd,off,type); | 
|---|
| 436 | newwanted = lzs->realwanted; | 
|---|
| 437 | switch (type) { | 
|---|
| 438 | case 1:  /* SEEK_CUR */ | 
|---|
| 439 | newwanted      += off; | 
|---|
| 440 | break; | 
|---|
| 441 | case 2:  /* SEEK_END */ | 
|---|
| 442 | newwanted   = lzs->reallength-off; | 
|---|
| 443 | break; | 
|---|
| 444 | default:/* SEEK_SET */ | 
|---|
| 445 | newwanted   = off; | 
|---|
| 446 | break; | 
|---|
| 447 | } | 
|---|
| 448 | if (newwanted>lzs->reallength) | 
|---|
| 449 | return LZERROR_BADVALUE; | 
|---|
| 450 | if (newwanted<0) | 
|---|
| 451 | return LZERROR_BADVALUE; | 
|---|
| 452 | lzs->realwanted                       = newwanted; | 
|---|
| 453 | return newwanted; | 
|---|
| 454 | } | 
|---|
| 455 |  | 
|---|
| 456 |  | 
|---|
| 457 | /*********************************************************************** | 
|---|
| 458 | *           LZCopy32   (LZ32.0) | 
|---|
| 459 | * | 
|---|
| 460 | * Copies everything from src to dest | 
|---|
| 461 | * if src is a LZ compressed file, it will be uncompressed. | 
|---|
| 462 | * will return the number of bytes written to dest or errors. | 
|---|
| 463 | */ | 
|---|
| 464 |  | 
|---|
| 465 | typedef  UINT (WINAPI *_readfun)(HFILE,LPVOID,UINT); | 
|---|
| 466 |  | 
|---|
| 467 | ODINFUNCTION2(LONG,LZCopy,HFILE,src,HFILE,dest) | 
|---|
| 468 | { | 
|---|
| 469 | int   usedlzinit=0,ret,wret; | 
|---|
| 470 | LONG  len; | 
|---|
| 471 | HFILE oldsrc = src; | 
|---|
| 472 | #define BUFLEN 1000 | 
|---|
| 473 | BYTE  buf[BUFLEN]; | 
|---|
| 474 | /* we need that weird typedef, for i can't seem to get function pointer | 
|---|
| 475 | * casts right. (Or they probably just do not like WINAPI in general) | 
|---|
| 476 | */ | 
|---|
| 477 |  | 
|---|
| 478 | _readfun xread; | 
|---|
| 479 |  | 
|---|
| 480 | dprintf(("LZ32: LZCopy(%08x,h%08xh)\n", | 
|---|
| 481 | src, | 
|---|
| 482 | dest)); | 
|---|
| 483 |  | 
|---|
| 484 | if (!IS_LZ_HANDLE(src)) { | 
|---|
| 485 | src = LZInit(src); | 
|---|
| 486 | if ((INT)src <= 0) return 0; | 
|---|
| 487 | if (src != oldsrc) usedlzinit=1; | 
|---|
| 488 | } | 
|---|
| 489 |  | 
|---|
| 490 | /* not compressed? just copy */ | 
|---|
| 491 | if (!IS_LZ_HANDLE(src)) | 
|---|
| 492 | xread=_lread; | 
|---|
| 493 | else | 
|---|
| 494 | xread=(_readfun)LZRead; | 
|---|
| 495 | len=0; | 
|---|
| 496 | while (1) { | 
|---|
| 497 | ret=xread(src,buf,BUFLEN); | 
|---|
| 498 | if (ret<=0) { | 
|---|
| 499 | if (ret==0) | 
|---|
| 500 | break; | 
|---|
| 501 | if (ret==-1) | 
|---|
| 502 | return LZERROR_READ; | 
|---|
| 503 | return ret; | 
|---|
| 504 | } | 
|---|
| 505 | len    += ret; | 
|---|
| 506 | wret  = _lwrite(dest,(LPCSTR)buf,ret); | 
|---|
| 507 | if (wret!=ret) | 
|---|
| 508 | return LZERROR_WRITE; | 
|---|
| 509 | } | 
|---|
| 510 | if (usedlzinit) | 
|---|
| 511 | LZClose(src); | 
|---|
| 512 | return len; | 
|---|
| 513 | #undef BUFLEN | 
|---|
| 514 | } | 
|---|
| 515 |  | 
|---|
| 516 | /* reverses GetExpandedPathname */ | 
|---|
| 517 | static LPSTR LZEXPAND_MangleName( LPCSTR fn ) | 
|---|
| 518 | { | 
|---|
| 519 | char *p; | 
|---|
| 520 | char *mfn = (char *)HEAP_xalloc( GetProcessHeap(), 0, | 
|---|
| 521 | strlen(fn) + 3 ); /* "._" and \0 */ | 
|---|
| 522 | strcpy( mfn, fn ); | 
|---|
| 523 | if (!(p = strrchr( mfn, '\\' ))) p = mfn; | 
|---|
| 524 | if ((p = strchr( p, '.' )) != NULL) | 
|---|
| 525 | { | 
|---|
| 526 | p++; | 
|---|
| 527 | if (strlen(p) < 3) strcat( p, "_" );  /* append '_' */ | 
|---|
| 528 | else p[strlen(p)-1] = '_';  /* replace last character */ | 
|---|
| 529 | } | 
|---|
| 530 | else strcat( mfn, "._" );            /* append "._" */ | 
|---|
| 531 | return mfn; | 
|---|
| 532 | } | 
|---|
| 533 |  | 
|---|
| 534 |  | 
|---|
| 535 | /*********************************************************************** | 
|---|
| 536 | *           LZOpenFile32A   (LZ32.1) | 
|---|
| 537 | * | 
|---|
| 538 | * Opens a file. If not compressed, open it as a normal file. | 
|---|
| 539 | */ | 
|---|
| 540 |  | 
|---|
| 541 | ODINFUNCTION3(HFILE,LZOpenFileA,LPCSTR,fn,LPOFSTRUCT,ofs,UINT,mode) | 
|---|
| 542 | { | 
|---|
| 543 | HFILE fd,cfd; | 
|---|
| 544 |  | 
|---|
| 545 | dprintf(("LZ32: LZOpenFileA(%s,%08xh,%08xh)\n", | 
|---|
| 546 | fn, | 
|---|
| 547 | ofs, | 
|---|
| 548 | mode)); | 
|---|
| 549 |  | 
|---|
| 550 | /* 0x70 represents all OF_SHARE_* flags, ignore them for the check */ | 
|---|
| 551 | fd=OpenFile(fn,ofs,mode); | 
|---|
| 552 | if (fd==HFILE_ERROR) | 
|---|
| 553 | { | 
|---|
| 554 | LPSTR mfn = LZEXPAND_MangleName(fn); | 
|---|
| 555 | fd = OpenFile(mfn,ofs,mode); | 
|---|
| 556 | HeapFree( GetProcessHeap(), 0, mfn ); | 
|---|
| 557 | } | 
|---|
| 558 | if ((mode&~0x70)!=OF_READ) | 
|---|
| 559 | return fd; | 
|---|
| 560 | if (fd==HFILE_ERROR) | 
|---|
| 561 | return HFILE_ERROR; | 
|---|
| 562 | cfd=LZInit(fd); | 
|---|
| 563 | if ((INT)cfd <= 0) return fd; | 
|---|
| 564 | return cfd; | 
|---|
| 565 | } | 
|---|
| 566 |  | 
|---|
| 567 |  | 
|---|
| 568 | /*********************************************************************** | 
|---|
| 569 | *           LZOpenFile32W   (LZ32.10) | 
|---|
| 570 | */ | 
|---|
| 571 | ODINFUNCTION3(HFILE,LZOpenFileW,LPCWSTR,fn,LPOFSTRUCT,ofs,UINT,mode) | 
|---|
| 572 | { | 
|---|
| 573 | LPSTR xfn; | 
|---|
| 574 | LPWSTR   yfn; | 
|---|
| 575 | HFILE ret; | 
|---|
| 576 |  | 
|---|
| 577 | dprintf(("LZ32: LZOpenFileW(%08xh,%08xh,%08xh)\n", | 
|---|
| 578 | fn, | 
|---|
| 579 | ofs, | 
|---|
| 580 | mode)); | 
|---|
| 581 |  | 
|---|
| 582 | xfn   = HEAP_strdupWtoA( GetProcessHeap(), 0, fn); | 
|---|
| 583 | ret   = LZOpenFileA(xfn,ofs,mode); | 
|---|
| 584 | HeapFree( GetProcessHeap(), 0, xfn ); | 
|---|
| 585 | if (ret!=HFILE_ERROR) { | 
|---|
| 586 | /* ofs->szPathName is an array with the OFSTRUCT */ | 
|---|
| 587 | yfn = HEAP_strdupAtoW( GetProcessHeap(), 0, (LPCSTR)ofs->szPathName ); | 
|---|
| 588 | memcpy(ofs->szPathName,yfn,lstrlenW(yfn)*2+2); | 
|---|
| 589 | HeapFree( GetProcessHeap(), 0, yfn ); | 
|---|
| 590 | } | 
|---|
| 591 | return   ret; | 
|---|
| 592 | } | 
|---|
| 593 |  | 
|---|
| 594 |  | 
|---|
| 595 | /*********************************************************************** | 
|---|
| 596 | *           LZClose32   (LZ32.5) | 
|---|
| 597 | */ | 
|---|
| 598 |  | 
|---|
| 599 | ODINPROCEDURE1(LZClose,HFILE,fd) | 
|---|
| 600 | { | 
|---|
| 601 | struct lzstate *lzs; | 
|---|
| 602 |  | 
|---|
| 603 | dprintf(("LZ32: LZClose(%08xh)\n", | 
|---|
| 604 | fd)); | 
|---|
| 605 |  | 
|---|
| 606 | if (!(lzs = GET_LZ_STATE(fd))) _lclose(fd); | 
|---|
| 607 | else | 
|---|
| 608 | { | 
|---|
| 609 | if (lzs->get) HeapFree( GetProcessHeap(), 0, lzs->get ); | 
|---|
| 610 | CloseHandle(lzs->realfd); | 
|---|
| 611 | lzstates[fd - 0x400] = NULL; | 
|---|
| 612 | HeapFree( SystemHeap, 0, lzs ); | 
|---|
| 613 | } | 
|---|
| 614 | } | 
|---|
| 615 |  | 
|---|
| 616 |  | 
|---|
| 617 | /*********************************************************************** | 
|---|
| 618 | *           CopyLZFile32  (LZ32.7) | 
|---|
| 619 | * | 
|---|
| 620 | * Copy src to dest (including uncompressing src). | 
|---|
| 621 | * NOTE: Yes. This is exactly the same function as LZCopy. | 
|---|
| 622 | */ | 
|---|
| 623 |  | 
|---|
| 624 | ODINFUNCTION2(LONG,CopyLZFile,HFILE,src,HFILE,dest) | 
|---|
| 625 | { | 
|---|
| 626 | dprintf(("LZ32: CopyLZFile(%08xh,%08xh)\n", | 
|---|
| 627 | src, | 
|---|
| 628 | dest)); | 
|---|
| 629 |  | 
|---|
| 630 | return LZCopy(src,dest); | 
|---|
| 631 | } | 
|---|