2
3
mirror of https://foundry.openuru.org/gitblit/r/CWE-ou-minkata.git synced 2025-07-14 02:27:40 -04:00

Fix line endings and tabs

This commit is contained in:
Branan Purvine-Riley
2011-04-11 16:27:55 -07:00
parent d4250e19b5
commit 908aaeb6f6
2738 changed files with 702562 additions and 702562 deletions

View File

@ -1,20 +1,20 @@
include_directories("../../CoreLib")
include_directories(${ZLIB_INCLUDE_DIR})
set(plCompression_SOURCES
plZlibCompress.cpp
plZlibStream.cpp
)
set(plCompression_HEADERS
plCompress.h
plZlibCompress.h
plZlibStream.h
)
add_library(plCompression STATIC ${plCompression_SOURCES} ${plCompression_HEADERS})
target_link_libraries(plCompression ${ZLIB_LIBRARIES})
source_group("Source Files" FILES ${plCompression_SOURCES})
source_group("Header Files" FILES ${plCompression_HEADERS})
include_directories("../../CoreLib")
include_directories(${ZLIB_INCLUDE_DIR})
set(plCompression_SOURCES
plZlibCompress.cpp
plZlibStream.cpp
)
set(plCompression_HEADERS
plCompress.h
plZlibCompress.h
plZlibStream.h
)
add_library(plCompression STATIC ${plCompression_SOURCES} ${plCompression_HEADERS})
target_link_libraries(plCompression ${ZLIB_LIBRARIES})
source_group("Source Files" FILES ${plCompression_SOURCES})
source_group("Header Files" FILES ${plCompression_HEADERS})

View File

@ -1,51 +1,51 @@
/*==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/>.
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
/*==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/>.
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

View File

@ -1,268 +1,268 @@
/*==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/>.
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
/*==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/>.
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

View File

@ -1,54 +1,54 @@
/*==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/>.
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
/*==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/>.
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

View File

@ -1,299 +1,299 @@
/*==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/>.
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;
}
/*==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/>.
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;
}

View File

@ -1,74 +1,74 @@
/*==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/>.
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();
};
/*==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/>.
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