/*********************************************************************** * LZSeek (KERNEL32.@) */ LONG WINAPI LZSeek( HFILE fd, LONG off, INT type ) { struct lzstate *lzs; LONG newwanted; TRACE("(%d,%d,%d)\n",fd,off,type); /* not compressed? just use normal _llseek() */ if (!(lzs = GET_LZ_STATE(fd))) return _llseek(fd,off,type); newwanted = lzs->realwanted; switch (type) { case 1: /* SEEK_CUR */ newwanted += off; break; case 2: /* SEEK_END */ newwanted = lzs->reallength-off; break; default:/* SEEK_SET */ newwanted = off; break; } if (newwanted>lzs->reallength) return LZERROR_BADVALUE; if (newwanted<0) return LZERROR_BADVALUE; lzs->realwanted = newwanted; return newwanted; }
/*********************************************************************** * LZCopy (KERNEL32.@) * * Copies everything from src to dest * if src is a LZ compressed file, it will be uncompressed. * will return the number of bytes written to dest or errors. */ LONG WINAPI LZCopy( HFILE src, HFILE dest ) { int usedlzinit = 0, ret, wret; LONG len; HFILE oldsrc = src, srcfd; FILETIME filetime; struct lzstate *lzs; #define BUFLEN 1000 CHAR buf[BUFLEN]; /* we need that weird typedef, for i can't seem to get function pointer * casts right. (Or they probably just do not like WINAPI in general) */ typedef UINT (WINAPI *_readfun)(HFILE,LPVOID,UINT); _readfun xread; DPRINT("(%d,%d)\n",src,dest); if (!IS_LZ_HANDLE(src)) { src = LZInit(src); if ((INT)src <= 0) return 0; if (src != oldsrc) usedlzinit=1; } /* not compressed? just copy */ if (!IS_LZ_HANDLE(src)) xread=_lread; else xread=(_readfun)LZRead; len=0; while (1) { ret=xread(src,buf,BUFLEN); if (ret<=0) { if (ret==0) break; if (ret==-1) return LZERROR_READ; return ret; } len += ret; wret = _hwrite(dest,buf,ret); if (wret!=ret) return LZERROR_WRITE; } /* Maintain the timestamp of source file to destination file */ srcfd = (!(lzs = GET_LZ_STATE(src))) ? src : lzs->realfd; GetFileTime( LongToHandle(srcfd), NULL, NULL, &filetime ); SetFileTime( LongToHandle(dest), NULL, NULL, &filetime ); /* close handle */ if (usedlzinit) LZClose(src); return len; #undef BUFLEN }
/*********************************************************************** * LZClose (KERNEL32.@) */ void WINAPI LZClose( HFILE fd ) { struct lzstate *lzs; DPRINT("(%d)\n",fd); if (!(lzs = GET_LZ_STATE(fd))) _lclose(fd); else { if (lzs->get) RtlFreeHeap( GetProcessHeap(), 0, lzs->get ); CloseHandle( LongToHandle(lzs->realfd) ); lzstates[fd - LZ_MIN_HANDLE] = NULL; RtlFreeHeap( GetProcessHeap(), 0, lzs ); } }
/*********************************************************************** * LZRead (KERNEL32.@) */ INT WINAPI LZRead( HFILE fd, LPSTR vbuf, INT toread ) { int howmuch; BYTE b,*buf; struct lzstate *lzs; buf=(LPBYTE)vbuf; TRACE("(%d,%p,%d)\n",fd,buf,toread); howmuch=toread; if (!(lzs = GET_LZ_STATE(fd))) return _lread(fd,buf,toread); /* The decompressor itself is in a define, cause we need it twice * in this function. (the decompressed byte will be in b) */ #define DECOMPRESS_ONE_BYTE \ if (lzs->stringlen) { \ b = lzs->table[lzs->stringpos]; \ lzs->stringpos = (lzs->stringpos+1)&0xFFF; \ lzs->stringlen--; \ } else { \ if (!(lzs->bytetype&0x100)) { \ if (1!=GET(lzs,b)) \ return toread-howmuch; \ lzs->bytetype = b|0xFF00; \ } \ if (lzs->bytetype & 1) { \ if (1!=GET(lzs,b)) \ return toread-howmuch; \ } else { \ BYTE b1,b2; \ \ if (1!=GET(lzs,b1)) \ return toread-howmuch; \ if (1!=GET(lzs,b2)) \ return toread-howmuch; \ /* Format: \ * b1 b2 \ * AB CD \ * where CAB is the stringoffset in the table\ * and D+3 is the len of the string \ */ \ lzs->stringpos = b1|((b2&0xf0)<<4); \ lzs->stringlen = (b2&0xf)+2; \ /* 3, but we use a byte already below ... */\ b = lzs->table[lzs->stringpos];\ lzs->stringpos = (lzs->stringpos+1)&0xFFF;\ } \ lzs->bytetype>>=1; \ } \ /* store b in table */ \ lzs->table[lzs->curtabent++]= b; \ lzs->curtabent &= 0xFFF; \ lzs->realcurrent++; /* if someone has seeked, we have to bring the decompressor * to that position */ if (lzs->realcurrent!=lzs->realwanted) { /* if the wanted position is before the current position * I see no easy way to unroll ... We have to restart at * the beginning. *sigh* */ if (lzs->realcurrent>lzs->realwanted) { /* flush decompressor state */ _llseek(lzs->realfd,LZ_HEADER_LEN,SEEK_SET); GET_FLUSH(lzs); lzs->realcurrent= 0; lzs->bytetype = 0; lzs->stringlen = 0; memset(lzs->table,' ',LZ_TABLE_SIZE); lzs->curtabent = 0xFF0; } while (lzs->realcurrent<lzs->realwanted) { DECOMPRESS_ONE_BYTE; } } while (howmuch) { DECOMPRESS_ONE_BYTE; lzs->realwanted++; *buf++ = b; howmuch--; } return toread; #undef DECOMPRESS_ONE_BYTE }