mirror of
https://foundry.openuru.org/gitblit/r/CWE-ou-minkata.git
synced 2025-07-14 02:27:40 -04:00
CWE Directory Reorganization
Rearrange directory structure of CWE to be loosely equivalent to the H'uru Plasma repository. Part 1: Movement of directories and files.
This commit is contained in:
67
Sources/Plasma/PubUtilLib/plCompression/plCompress.h
Normal file
67
Sources/Plasma/PubUtilLib/plCompression/plCompress.h
Normal file
@ -0,0 +1,67 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools
|
||||
Copyright (C) 2011 Cyan Worlds, Inc.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Additional permissions under GNU GPL version 3 section 7
|
||||
|
||||
If you modify this Program, or any covered work, by linking or
|
||||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
|
||||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
|
||||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
|
||||
(or a modified version of those libraries),
|
||||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
|
||||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
|
||||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
|
||||
licensors of this Program grant you additional
|
||||
permission to convey the resulting work. Corresponding Source for a
|
||||
non-source form of such a combination shall include the source code for
|
||||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
|
||||
work.
|
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
or by snail mail at:
|
||||
Cyan Worlds, Inc.
|
||||
14617 N Newport Hwy
|
||||
Mead, WA 99021
|
||||
|
||||
*==LICENSE==*/
|
||||
#ifndef plCompress_h
|
||||
#define plCompress_h
|
||||
|
||||
#include "hsTypes.h"
|
||||
|
||||
//
|
||||
// Abstract base class for compression code
|
||||
//
|
||||
class plCompress
|
||||
{
|
||||
public:
|
||||
plCompress() {}
|
||||
virtual ~plCompress() {}
|
||||
|
||||
// return true if successful
|
||||
virtual hsBool Uncompress(UInt8* bufOut, UInt32* bufLenOut, const UInt8* bufIn, UInt32 bufLenIn) = 0;
|
||||
virtual hsBool Compress(UInt8* bufOut, UInt32* bufLenOut, const UInt8* bufIn, UInt32 bufLenIn) = 0;
|
||||
|
||||
// in place versions
|
||||
virtual hsBool Uncompress(UInt8** bufIn, UInt32* bufLenIn, UInt32 maxBufLenOut, int offset=0) = 0;
|
||||
virtual hsBool Compress(UInt8** bufIn, UInt32* bufLenIn, int offset=0) = 0;
|
||||
};
|
||||
|
||||
|
||||
#endif // plCompress_h
|
||||
|
284
Sources/Plasma/PubUtilLib/plCompression/plZlibCompress.cpp
Normal file
284
Sources/Plasma/PubUtilLib/plCompression/plZlibCompress.cpp
Normal file
@ -0,0 +1,284 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools
|
||||
Copyright (C) 2011 Cyan Worlds, Inc.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Additional permissions under GNU GPL version 3 section 7
|
||||
|
||||
If you modify this Program, or any covered work, by linking or
|
||||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
|
||||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
|
||||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
|
||||
(or a modified version of those libraries),
|
||||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
|
||||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
|
||||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
|
||||
licensors of this Program grant you additional
|
||||
permission to convey the resulting work. Corresponding Source for a
|
||||
non-source form of such a combination shall include the source code for
|
||||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
|
||||
work.
|
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
or by snail mail at:
|
||||
Cyan Worlds, Inc.
|
||||
14617 N Newport Hwy
|
||||
Mead, WA 99021
|
||||
|
||||
*==LICENSE==*/
|
||||
#include "plZlibCompress.h"
|
||||
#include "zlib.h"
|
||||
#include "hsMemory.h"
|
||||
#include "hsStream.h"
|
||||
|
||||
hsBool plZlibCompress::Uncompress(UInt8* bufOut, UInt32* bufLenOut, const UInt8* bufIn, UInt32 bufLenIn)
|
||||
{
|
||||
return (uncompress(bufOut, bufLenOut, bufIn, bufLenIn) == Z_OK);
|
||||
}
|
||||
|
||||
hsBool plZlibCompress::Compress(UInt8* bufOut, UInt32* bufLenOut, const UInt8* bufIn, UInt32 bufLenIn)
|
||||
{
|
||||
// according to compress doc, the bufOut buffer should be at least .1% larger than source buffer, plus 12 bytes.
|
||||
hsAssert(*bufLenOut>=(int)(bufLenIn*1.1+12), "bufOut compress buffer is not large enough");
|
||||
return (compress(bufOut, bufLenOut, bufIn, bufLenIn) == Z_OK);
|
||||
}
|
||||
|
||||
//
|
||||
// copy bufOut to bufIn, set bufLenIn=bufLenOut
|
||||
//
|
||||
hsBool plZlibCompress::ICopyBuffers(UInt8** bufIn, UInt32* bufLenIn, char* bufOut, UInt32 bufLenOut, int offset, bool ok)
|
||||
{
|
||||
if (ok)
|
||||
{
|
||||
*bufLenIn = bufLenOut+offset;
|
||||
UInt8* newBuf = TRACKED_NEW UInt8[*bufLenIn]; // alloc new buffer
|
||||
HSMemory::BlockMove(*bufIn, newBuf, offset); // copy offset (uncompressed) part
|
||||
delete [] *bufIn; // delete original buffer
|
||||
|
||||
HSMemory::BlockMove(bufOut, newBuf+offset, bufLenOut); // copy compressed part
|
||||
delete [] bufOut;
|
||||
*bufIn = newBuf;
|
||||
return true;
|
||||
}
|
||||
delete [] bufOut;
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
// In place version
|
||||
// offset is how much to skip over when compressing
|
||||
//
|
||||
hsBool plZlibCompress::Compress(UInt8** bufIn, UInt32* bufLenIn, int offset)
|
||||
{
|
||||
UInt32 adjBufLenIn = *bufLenIn - offset;
|
||||
UInt8* adjBufIn = *bufIn + offset;
|
||||
|
||||
// according to compress doc, the bufOut buffer should be at least .1% larger than source buffer, plus 12 bytes.
|
||||
UInt32 bufLenOut = (int)(adjBufLenIn*1.1+12);
|
||||
char* bufOut = TRACKED_NEW char[bufLenOut];
|
||||
|
||||
bool ok=(Compress((UInt8*)bufOut, &bufLenOut, (UInt8*)adjBufIn, adjBufLenIn) &&
|
||||
bufLenOut < adjBufLenIn);
|
||||
return ICopyBuffers(bufIn, bufLenIn, bufOut, bufLenOut, offset, ok);
|
||||
}
|
||||
|
||||
//
|
||||
// In place version
|
||||
//
|
||||
hsBool plZlibCompress::Uncompress(UInt8** bufIn, UInt32* bufLenIn, UInt32 bufLenOut, int offset)
|
||||
{
|
||||
UInt32 adjBufLenIn = *bufLenIn - offset;
|
||||
UInt8* adjBufIn = *bufIn + offset;
|
||||
|
||||
char* bufOut = TRACKED_NEW char[bufLenOut];
|
||||
|
||||
bool ok=Uncompress((UInt8*)bufOut, &bufLenOut, (UInt8*)adjBufIn, adjBufLenIn) ? true : false;
|
||||
return ICopyBuffers(bufIn, bufLenIn, bufOut, bufLenOut, offset, ok);
|
||||
}
|
||||
|
||||
//// .gz File Versions ///////////////////////////////////////////////////////
|
||||
|
||||
#define kGzBufferSize 64 * 1024
|
||||
#if 1
|
||||
|
||||
hsBool plZlibCompress::UncompressFile( const char *compressedPath, const char *destPath )
|
||||
{
|
||||
gzFile inFile;
|
||||
FILE *outFile;
|
||||
hsBool worked = false;
|
||||
int length, err;
|
||||
|
||||
UInt8 buffer[ kGzBufferSize ];
|
||||
|
||||
|
||||
outFile = fopen( destPath, "wb" );
|
||||
if( outFile != nil )
|
||||
{
|
||||
inFile = gzopen( compressedPath, "rb" );
|
||||
if( inFile != nil )
|
||||
{
|
||||
for( ;; )
|
||||
{
|
||||
length = gzread( inFile, buffer, sizeof( buffer ) );
|
||||
if( length < 0 )
|
||||
{
|
||||
gzerror( inFile, &err );
|
||||
break;
|
||||
}
|
||||
if( length == 0 )
|
||||
{
|
||||
worked = true;
|
||||
break;
|
||||
}
|
||||
if( fwrite( buffer, 1, length, outFile ) != length )
|
||||
break;
|
||||
}
|
||||
if( gzclose( inFile ) != Z_OK )
|
||||
worked = false;
|
||||
}
|
||||
fclose( outFile );
|
||||
}
|
||||
|
||||
return worked;
|
||||
}
|
||||
|
||||
|
||||
hsBool plZlibCompress::CompressFile( const char *uncompressedPath, const char *destPath )
|
||||
{
|
||||
FILE *inFile;
|
||||
gzFile outFile;
|
||||
hsBool worked = false;
|
||||
int length, err;
|
||||
|
||||
UInt8 buffer[ kGzBufferSize ];
|
||||
|
||||
|
||||
inFile = fopen( uncompressedPath, "rb" );
|
||||
if( inFile != nil )
|
||||
{
|
||||
outFile = gzopen( destPath, "wb" );
|
||||
if( outFile != nil )
|
||||
{
|
||||
for( ;; )
|
||||
{
|
||||
length = fread( buffer, 1, sizeof( buffer ), inFile );
|
||||
if( ferror( inFile ) )
|
||||
break;
|
||||
|
||||
if( length == 0 )
|
||||
{
|
||||
worked = true;
|
||||
break;
|
||||
}
|
||||
if( gzwrite( outFile, buffer, (unsigned)length ) != length )
|
||||
{
|
||||
gzerror( outFile, &err );
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( gzclose( outFile ) != Z_OK )
|
||||
worked = false;
|
||||
}
|
||||
fclose( inFile );
|
||||
}
|
||||
|
||||
return worked;
|
||||
}
|
||||
|
||||
|
||||
//// file <-> stream ///////////////////////////////////////////////////////
|
||||
|
||||
hsBool plZlibCompress::UncompressToStream( const char * filename, hsStream * s )
|
||||
{
|
||||
gzFile inFile;
|
||||
hsBool worked = false;
|
||||
int length, err;
|
||||
|
||||
UInt8 buffer[ kGzBufferSize ];
|
||||
|
||||
|
||||
inFile = gzopen( filename, "rb" );
|
||||
if( inFile != nil )
|
||||
{
|
||||
for( ;; )
|
||||
{
|
||||
length = gzread( inFile, buffer, sizeof( buffer ) );
|
||||
if( length < 0 )
|
||||
{
|
||||
gzerror( inFile, &err );
|
||||
break;
|
||||
}
|
||||
if( length == 0 )
|
||||
{
|
||||
worked = true;
|
||||
break;
|
||||
}
|
||||
s->Write( length, buffer );
|
||||
}
|
||||
if( gzclose( inFile ) != Z_OK )
|
||||
worked = false;
|
||||
}
|
||||
|
||||
return worked;
|
||||
}
|
||||
|
||||
|
||||
hsBool plZlibCompress::CompressToFile( hsStream * s, const char * filename )
|
||||
{
|
||||
gzFile outFile;
|
||||
hsBool worked = false;
|
||||
int length, err;
|
||||
|
||||
UInt8 buffer[ kGzBufferSize ];
|
||||
|
||||
|
||||
outFile = gzopen( filename, "wb" );
|
||||
if( outFile != nil )
|
||||
{
|
||||
for( ;; )
|
||||
{
|
||||
int avail = s->GetEOF()-s->GetPosition();
|
||||
int n = ( avail>sizeof( buffer ) ) ? sizeof( buffer ) : avail;
|
||||
|
||||
if( n == 0 )
|
||||
{
|
||||
worked = true;
|
||||
break;
|
||||
}
|
||||
|
||||
length = s->Read( n, buffer );
|
||||
|
||||
if( length == 0 )
|
||||
{
|
||||
worked = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if( gzwrite( outFile, buffer, (unsigned)length ) != length )
|
||||
{
|
||||
gzerror( outFile, &err );
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( gzclose( outFile ) != Z_OK )
|
||||
worked = false;
|
||||
}
|
||||
|
||||
return worked;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
70
Sources/Plasma/PubUtilLib/plCompression/plZlibCompress.h
Normal file
70
Sources/Plasma/PubUtilLib/plCompression/plZlibCompress.h
Normal file
@ -0,0 +1,70 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools
|
||||
Copyright (C) 2011 Cyan Worlds, Inc.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Additional permissions under GNU GPL version 3 section 7
|
||||
|
||||
If you modify this Program, or any covered work, by linking or
|
||||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
|
||||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
|
||||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
|
||||
(or a modified version of those libraries),
|
||||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
|
||||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
|
||||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
|
||||
licensors of this Program grant you additional
|
||||
permission to convey the resulting work. Corresponding Source for a
|
||||
non-source form of such a combination shall include the source code for
|
||||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
|
||||
work.
|
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
or by snail mail at:
|
||||
Cyan Worlds, Inc.
|
||||
14617 N Newport Hwy
|
||||
Mead, WA 99021
|
||||
|
||||
*==LICENSE==*/
|
||||
#ifndef plZlibCompress_h
|
||||
#define plZlibCompress_h
|
||||
|
||||
#include "plCompress.h"
|
||||
|
||||
class hsStream;
|
||||
|
||||
class plZlibCompress : public plCompress
|
||||
{
|
||||
protected:
|
||||
hsBool ICopyBuffers(UInt8** bufIn, UInt32* bufLenIn, char* bufOut, UInt32 bufLenOut, int offset, bool ok );
|
||||
public:
|
||||
hsBool Uncompress(UInt8* bufOut, UInt32* bufLenOut, const UInt8* bufIn, UInt32 bufLenIn);
|
||||
hsBool Compress(UInt8* bufOut, UInt32* bufLenOut, const UInt8* bufIn, UInt32 bufLenIn);
|
||||
|
||||
// in place versions
|
||||
hsBool Uncompress(UInt8** bufIn, UInt32* bufLenIn, UInt32 maxBufLenOut, int offset=0);
|
||||
hsBool Compress(UInt8** bufIn, UInt32* bufLenIn, int offset=0);
|
||||
|
||||
// .gz versions
|
||||
static hsBool UncompressFile( const char *compressedPath, const char *destPath );
|
||||
static hsBool CompressFile( const char *uncompressedPath, const char *destPath );
|
||||
|
||||
// file <-> stream
|
||||
static hsBool UncompressToStream( const char * filename, hsStream * s );
|
||||
static hsBool CompressToFile( hsStream * s, const char * filename );
|
||||
};
|
||||
|
||||
#endif // plZlibCompress_h
|
315
Sources/Plasma/PubUtilLib/plCompression/plZlibStream.cpp
Normal file
315
Sources/Plasma/PubUtilLib/plCompression/plZlibStream.cpp
Normal file
@ -0,0 +1,315 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools
|
||||
Copyright (C) 2011 Cyan Worlds, Inc.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Additional permissions under GNU GPL version 3 section 7
|
||||
|
||||
If you modify this Program, or any covered work, by linking or
|
||||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
|
||||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
|
||||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
|
||||
(or a modified version of those libraries),
|
||||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
|
||||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
|
||||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
|
||||
licensors of this Program grant you additional
|
||||
permission to convey the resulting work. Corresponding Source for a
|
||||
non-source form of such a combination shall include the source code for
|
||||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
|
||||
work.
|
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
or by snail mail at:
|
||||
Cyan Worlds, Inc.
|
||||
14617 N Newport Hwy
|
||||
Mead, WA 99021
|
||||
|
||||
*==LICENSE==*/
|
||||
#include "plZlibStream.h"
|
||||
#include "zlib.h"
|
||||
|
||||
voidpf ZlibAlloc(voidpf opaque, uInt items, uInt size)
|
||||
{
|
||||
return ALLOC(items*size);
|
||||
}
|
||||
|
||||
void ZlibFree(voidpf opaque, voidpf address)
|
||||
{
|
||||
FREE(address);
|
||||
}
|
||||
|
||||
plZlibStream::plZlibStream() : fOutput(nil), fZStream(nil), fHeader(kNeedMoreData), fDecompressedOk(false)
|
||||
{
|
||||
}
|
||||
|
||||
plZlibStream::~plZlibStream()
|
||||
{
|
||||
hsAssert(!fOutput && !fZStream, "plZlibStream not closed");
|
||||
}
|
||||
|
||||
hsBool plZlibStream::Open(const char* filename, const char* mode)
|
||||
{
|
||||
wchar* wFilename = hsStringToWString(filename);
|
||||
wchar* wMode = hsStringToWString(mode);
|
||||
hsBool ret = Open(wFilename, wMode);
|
||||
delete [] wFilename;
|
||||
delete [] wMode;
|
||||
return ret;
|
||||
}
|
||||
|
||||
hsBool plZlibStream::Open(const wchar* filename, const wchar* mode)
|
||||
{
|
||||
fFilename = filename;
|
||||
fMode = mode;
|
||||
|
||||
fOutput = NEW(hsUNIXStream);
|
||||
return fOutput->Open(filename, L"wb");
|
||||
}
|
||||
|
||||
hsBool plZlibStream::Close()
|
||||
{
|
||||
if (fOutput)
|
||||
{
|
||||
fOutput->Close();
|
||||
DEL(fOutput);
|
||||
fOutput = nil;
|
||||
}
|
||||
if (fZStream)
|
||||
{
|
||||
z_streamp zstream = (z_streamp)fZStream;
|
||||
inflateEnd(zstream);
|
||||
DEL(zstream);
|
||||
fZStream = nil;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
UInt32 plZlibStream::Write(UInt32 byteCount, const void* buffer)
|
||||
{
|
||||
UInt8* byteBuf = (UInt8*)buffer;
|
||||
|
||||
// Check if we've read in the full gzip header yet
|
||||
if (fHeader == kNeedMoreData)
|
||||
{
|
||||
int cutAmt = IValidateGzHeader(byteCount, buffer);
|
||||
|
||||
// Once we've read in the whole header, cut out whatever part of it is
|
||||
// in this buffer, leaving raw compressed data
|
||||
if (cutAmt != 0)
|
||||
{
|
||||
byteCount -= cutAmt;
|
||||
byteBuf += cutAmt;
|
||||
}
|
||||
}
|
||||
|
||||
if (fHeader == kInvalidHeader)
|
||||
return 0;
|
||||
|
||||
if (fHeader == kValidHeader)
|
||||
{
|
||||
ASSERT(fOutput);
|
||||
ASSERT(fZStream);
|
||||
z_streamp zstream = (z_streamp)fZStream;
|
||||
zstream->avail_in = byteCount;
|
||||
zstream->next_in = byteBuf;
|
||||
|
||||
char outBuf[2048];
|
||||
while (zstream->avail_in != 0)
|
||||
{
|
||||
zstream->avail_out = sizeof(outBuf);
|
||||
zstream->next_out = (UInt8*)outBuf;
|
||||
|
||||
UInt32 amtWritten = zstream->total_out;
|
||||
|
||||
int ret = inflate(zstream, Z_NO_FLUSH);
|
||||
|
||||
bool inflateErr = (ret == Z_NEED_DICT || ret == Z_DATA_ERROR ||
|
||||
ret == Z_STREAM_ERROR || ret == Z_MEM_ERROR || ret == Z_BUF_ERROR);
|
||||
// If we have a decompression error, just fail
|
||||
if (inflateErr)
|
||||
{
|
||||
hsAssert(!inflateErr, "Error in inflate");
|
||||
fHeader = kInvalidHeader;
|
||||
break;
|
||||
}
|
||||
|
||||
amtWritten = zstream->total_out - amtWritten;
|
||||
fOutput->Write(amtWritten, outBuf);
|
||||
|
||||
// If zlib says we hit the end of the stream, ignore avail_in
|
||||
if (ret == Z_STREAM_END)
|
||||
{
|
||||
fDecompressedOk = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return byteCount;
|
||||
}
|
||||
|
||||
int plZlibStream::IValidateGzHeader(UInt32 byteCount, const void* buffer)
|
||||
{
|
||||
// Ripped from gzio.cpp
|
||||
#define HEAD_CRC 0x02
|
||||
#define EXTRA_FIELD 0x04
|
||||
#define ORIG_NAME 0x08
|
||||
#define COMMENT 0x10
|
||||
#define RESERVED 0xE0
|
||||
static int gz_magic[2] = {0x1f, 0x8b}; // gzip magic header
|
||||
|
||||
#define CheckForEnd() if (s.AtEnd()) { fHeader = kNeedMoreData; return 0; }
|
||||
|
||||
int i;
|
||||
|
||||
int initCacheSize = fHeaderCache.size();
|
||||
for (i = 0; i < byteCount; i++)
|
||||
fHeaderCache.push_back(((UInt8*)buffer)[i]);
|
||||
hsReadOnlyStream s(fHeaderCache.size(), &fHeaderCache[0]);
|
||||
|
||||
// Check the gzip magic header
|
||||
for (i = 0; i < 2; i++)
|
||||
{
|
||||
UInt8 c = s.ReadByte();
|
||||
if (c != gz_magic[i])
|
||||
{
|
||||
CheckForEnd();
|
||||
fHeader = kInvalidHeader;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int method = s.ReadByte();
|
||||
int flags = s.ReadByte();
|
||||
if (method != Z_DEFLATED || (flags & RESERVED) != 0)
|
||||
{
|
||||
CheckForEnd();
|
||||
fHeader = kInvalidHeader;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Discard time, xflags and OS code:
|
||||
for (i = 0; i < 6; i++)
|
||||
s.ReadByte();
|
||||
CheckForEnd();
|
||||
|
||||
if ((flags & EXTRA_FIELD) != 0)
|
||||
{
|
||||
// skip the extra field
|
||||
UInt16 len = s.ReadSwap16();
|
||||
while (len-- != 0 && s.ReadByte())
|
||||
{
|
||||
CheckForEnd();
|
||||
}
|
||||
}
|
||||
|
||||
int c;
|
||||
|
||||
// skip the original file name
|
||||
if ((flags & ORIG_NAME) != 0)
|
||||
{
|
||||
while ((c = s.ReadByte()) != 0)
|
||||
{
|
||||
CheckForEnd();
|
||||
}
|
||||
}
|
||||
|
||||
// skip the .gz file comment
|
||||
if ((flags & COMMENT) != 0)
|
||||
{
|
||||
while ((c = s.ReadByte()) != 0)
|
||||
{
|
||||
CheckForEnd();
|
||||
}
|
||||
}
|
||||
|
||||
// skip the header crc
|
||||
if ((flags & HEAD_CRC) != 0)
|
||||
{
|
||||
s.ReadSwap16();
|
||||
CheckForEnd();
|
||||
}
|
||||
|
||||
CheckForEnd();
|
||||
|
||||
UInt32 headerSize = s.GetPosition();
|
||||
UInt32 clipBuffer = headerSize - initCacheSize;
|
||||
|
||||
// Initialize the zlib stream
|
||||
z_streamp zstream = NEW(z_stream_s);
|
||||
memset(zstream, 0, sizeof(z_stream_s));
|
||||
zstream->zalloc = ZlibAlloc;
|
||||
zstream->zfree = ZlibFree;
|
||||
zstream->opaque = nil;
|
||||
zstream->avail_in = byteCount - clipBuffer;
|
||||
zstream->next_in = (UInt8*)&fHeaderCache[clipBuffer];
|
||||
// Gotta use inflateInit2, because there's no header for zlib to look at.
|
||||
// The -15 tells it to not try and find a header
|
||||
bool initOk = (inflateInit2(zstream, -15) == Z_OK);
|
||||
fZStream = zstream;
|
||||
|
||||
fHeaderCache.clear();
|
||||
|
||||
if (!initOk)
|
||||
{
|
||||
hsAssert(0, "Zip init failed");
|
||||
fHeader = kInvalidHeader;
|
||||
return 0;
|
||||
}
|
||||
ASSERT(fZStream);
|
||||
|
||||
fHeader = kValidHeader;
|
||||
return clipBuffer;
|
||||
}
|
||||
|
||||
hsBool plZlibStream::AtEnd()
|
||||
{
|
||||
hsAssert(0, "AtEnd not supported");
|
||||
return true;
|
||||
}
|
||||
|
||||
UInt32 plZlibStream::Read(UInt32 byteCount, void* buffer)
|
||||
{
|
||||
hsAssert(0, "Read not supported");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void plZlibStream::Skip(UInt32 deltaByteCount)
|
||||
{
|
||||
hsAssert(0, "Skip not supported");
|
||||
}
|
||||
|
||||
void plZlibStream::Rewind()
|
||||
{
|
||||
// hack so rewind will work (someone thought it would be funny to not implement base class functions)
|
||||
Close();
|
||||
Open(fFilename.c_str(), fMode.c_str());
|
||||
fHeader = kNeedMoreData;
|
||||
fDecompressedOk = false;
|
||||
}
|
||||
|
||||
void plZlibStream::FastFwd()
|
||||
{
|
||||
hsAssert(0, "FastFwd not supported");
|
||||
}
|
||||
|
||||
UInt32 plZlibStream::GetEOF()
|
||||
{
|
||||
hsAssert(0, "GetEOF not supported");
|
||||
return 0;
|
||||
}
|
90
Sources/Plasma/PubUtilLib/plCompression/plZlibStream.h
Normal file
90
Sources/Plasma/PubUtilLib/plCompression/plZlibStream.h
Normal file
@ -0,0 +1,90 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools
|
||||
Copyright (C) 2011 Cyan Worlds, Inc.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Additional permissions under GNU GPL version 3 section 7
|
||||
|
||||
If you modify this Program, or any covered work, by linking or
|
||||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
|
||||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
|
||||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
|
||||
(or a modified version of those libraries),
|
||||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
|
||||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
|
||||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
|
||||
licensors of this Program grant you additional
|
||||
permission to convey the resulting work. Corresponding Source for a
|
||||
non-source form of such a combination shall include the source code for
|
||||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
|
||||
work.
|
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
or by snail mail at:
|
||||
Cyan Worlds, Inc.
|
||||
14617 N Newport Hwy
|
||||
Mead, WA 99021
|
||||
|
||||
*==LICENSE==*/
|
||||
#ifndef plZlibStream_h_inc
|
||||
#define plZlibStream_h_inc
|
||||
|
||||
#include "hsStream.h"
|
||||
#include "hsStlUtils.h"
|
||||
|
||||
//
|
||||
// This is for reading a .gz file from a buffer, and writing the uncompressed data to a file.
|
||||
// Call open with the name of the uncompressed file, then call write with the compressed data.
|
||||
//
|
||||
class plZlibStream : public hsStream
|
||||
{
|
||||
protected:
|
||||
hsStream* fOutput;
|
||||
void* fZStream;
|
||||
bool fDecompressedOk;
|
||||
|
||||
enum Validate { kNeedMoreData, kInvalidHeader, kValidHeader };
|
||||
Validate fHeader;
|
||||
std::vector<UInt8> fHeaderCache;
|
||||
|
||||
std::wstring fFilename, fMode; // needed for rewind function
|
||||
|
||||
int IValidateGzHeader(UInt32 byteCount, const void* buffer);
|
||||
|
||||
public:
|
||||
plZlibStream();
|
||||
virtual ~plZlibStream();
|
||||
|
||||
virtual hsBool Open(const char* filename, const char* mode);
|
||||
virtual hsBool Open(const wchar* filename, const wchar* mode);
|
||||
virtual hsBool Close();
|
||||
virtual UInt32 Write(UInt32 byteCount, const void* buffer);
|
||||
|
||||
// Since most functions don't check the return value from Write, you can
|
||||
// call this after you've passed in all your data to determine if it
|
||||
// decompressed ok
|
||||
bool DecompressedOk() { return fDecompressedOk; }
|
||||
|
||||
// You can't use these
|
||||
virtual hsBool AtEnd();
|
||||
virtual UInt32 Read(UInt32 byteCount, void* buffer);
|
||||
virtual void Skip(UInt32 deltaByteCount);
|
||||
virtual void Rewind();
|
||||
virtual void FastFwd();
|
||||
virtual UInt32 GetEOF();
|
||||
};
|
||||
|
||||
#endif // plZlibStream_h_inc
|
Reference in New Issue
Block a user