| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 | 
							- /*
 
-  * MinizipExtensions.cpp, part of VCMI engine
 
-  *
 
-  * Authors: listed in file AUTHORS in main folder
 
-  *
 
-  * License: GNU General Public License v2.0 or later
 
-  * Full text of license available in license.txt file, in main folder
 
-  *
 
-  */
 
- #include "StdInc.h"
 
- #include "MinizipExtensions.h"
 
- #include "CMemoryBuffer.h"
 
- #include "FileStream.h"
 
- VCMI_LIB_NAMESPACE_BEGIN
 
- template<class Stream>
 
- inline uLong streamRead(voidpf opaque, voidpf stream, void * buf, uLong size)
 
- {
 
- 	assert(opaque != nullptr);
 
- 	assert(stream != nullptr);
 
- 	auto * actualStream = static_cast<Stream *>(stream);
 
- 	return static_cast<uLong>(actualStream->read(static_cast<ui8 *>(buf), size));
 
- }
 
- template<class Stream>
 
- inline ZPOS64_T streamTell(voidpf opaque, voidpf stream)
 
- {
 
- 	assert(opaque != nullptr);
 
- 	assert(stream != nullptr);
 
- 	auto * actualStream = static_cast<Stream *>(stream);
 
- 	return actualStream->tell();
 
- }
 
- template<class Stream>
 
- inline long streamSeek(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
 
- {
 
- 	assert(opaque != nullptr);
 
- 	assert(stream != nullptr);
 
- 	auto * actualStream = static_cast<Stream *>(stream);
 
- 	long ret = 0;
 
- 	switch(origin)
 
- 	{
 
- 		case ZLIB_FILEFUNC_SEEK_CUR:
 
- 			if(actualStream->skip(offset) != offset)
 
- 				ret = -1;
 
- 			break;
 
- 		case ZLIB_FILEFUNC_SEEK_END:
 
- 		{
 
- 			const si64 pos = actualStream->getSize() - offset;
 
- 			if(actualStream->seek(pos) != pos)
 
- 				ret = -1;
 
- 		}
 
- 		break;
 
- 		case ZLIB_FILEFUNC_SEEK_SET:
 
- 			if(actualStream->seek(offset) != offset)
 
- 				ret = -1;
 
- 			break;
 
- 		default:
 
- 			ret = -1;
 
- 	}
 
- 	if(ret == -1)
 
- 		logGlobal->error("Stream seek failed");
 
- 	return 0;
 
- }
 
- template<class Stream>
 
- inline int streamProxyClose(voidpf opaque, voidpf stream)
 
- {
 
- 	assert(opaque != nullptr);
 
- 	assert(stream != nullptr);
 
- 	auto * actualStream = static_cast<Stream *>(stream);
 
- 	logGlobal->trace("Proxy stream closed");
 
- 	actualStream->seek(0);
 
-     return 0;
 
- }
 
- ///CDefaultIOApi
 
- zlib_filefunc64_def CDefaultIOApi::getApiStructure()
 
- {
 
- 	return * FileStream::GetMinizipFilefunc();
 
- }
 
- ///CProxyIOApi
 
- CProxyIOApi::CProxyIOApi(CInputOutputStream * buffer):
 
- 	data(buffer)
 
- {
 
- }
 
- //must be instantiated in .cpp file for access to complete types of all member fields
 
- CProxyIOApi::~CProxyIOApi() = default;
 
- zlib_filefunc64_def CProxyIOApi::getApiStructure()
 
- {
 
- 	zlib_filefunc64_def api;
 
- 	api.opaque = this;
 
- 	api.zopen64_file = &openFileProxy;
 
- 	api.zread_file = &readFileProxy;
 
- 	api.zwrite_file = &writeFileProxy;
 
- 	api.ztell64_file = &tellFileProxy;
 
- 	api.zseek64_file = &seekFileProxy;
 
- 	api.zclose_file = &closeFileProxy;
 
- 	api.zerror_file = &errorFileProxy;
 
- 	return api;
 
- }
 
- voidpf ZCALLBACK CProxyIOApi::openFileProxy(voidpf opaque, const void * filename, int mode)
 
- {
 
- 	assert(opaque != nullptr);
 
- 	boost::filesystem::path path;
 
- 	if(filename != nullptr)
 
- 		path =  static_cast<const boost::filesystem::path::value_type *>(filename);
 
- 	return (static_cast<CProxyIOApi *>(opaque))->openFile(path, mode);
 
- }
 
- uLong ZCALLBACK CProxyIOApi::readFileProxy(voidpf opaque, voidpf stream, void * buf, uLong size)
 
- {
 
- 	return streamRead<CInputOutputStream>(opaque, stream, buf, size);
 
- }
 
- uLong ZCALLBACK CProxyIOApi::writeFileProxy(voidpf opaque, voidpf stream, const void * buf, uLong size)
 
- {
 
- 	assert(opaque != nullptr);
 
- 	assert(stream != nullptr);
 
- 	auto * actualStream = static_cast<CInputOutputStream *>(stream);
 
- 	return static_cast<uLong>(actualStream->write(static_cast<const ui8 *>(buf), size));
 
- }
 
- ZPOS64_T ZCALLBACK CProxyIOApi::tellFileProxy(voidpf opaque, voidpf stream)
 
- {
 
- 	return streamTell<CInputOutputStream>(opaque, stream);
 
- }
 
- long ZCALLBACK CProxyIOApi::seekFileProxy(voidpf  opaque, voidpf stream, ZPOS64_T offset, int origin)
 
- {
 
- 	return streamSeek<CInputOutputStream>(opaque, stream, offset, origin);
 
- }
 
- int ZCALLBACK CProxyIOApi::closeFileProxy(voidpf opaque, voidpf stream)
 
- {
 
- 	return streamProxyClose<CInputOutputStream>(opaque, stream);
 
- }
 
- int ZCALLBACK CProxyIOApi::errorFileProxy(voidpf opaque, voidpf stream)
 
- {
 
-     return 0;
 
- }
 
- CInputOutputStream * CProxyIOApi::openFile(const boost::filesystem::path & filename, int mode)
 
- {
 
- 	logGlobal->trace("CProxyIOApi: stream opened for %s with mode %d", filename.string(), mode);
 
- 	data->seek(0);
 
- 	return data;
 
- }
 
- ///CProxyROIOApi
 
- CProxyROIOApi::CProxyROIOApi(CInputStream * buffer):
 
- 	data(buffer)
 
- {
 
- }
 
- //must be instantiated in .cpp file for access to complete types of all member fields
 
- CProxyROIOApi::~CProxyROIOApi() = default;
 
- zlib_filefunc64_def CProxyROIOApi::getApiStructure()
 
- {
 
- 	zlib_filefunc64_def api;
 
- 	api.opaque = this;
 
- 	api.zopen64_file = &openFileProxy;
 
- 	api.zread_file = &readFileProxy;
 
- 	api.zwrite_file = &writeFileProxy;
 
- 	api.ztell64_file = &tellFileProxy;
 
- 	api.zseek64_file = &seekFileProxy;
 
- 	api.zclose_file = &closeFileProxy;
 
- 	api.zerror_file = &errorFileProxy;
 
- 	return api;
 
- }
 
- CInputStream * CProxyROIOApi::openFile(const boost::filesystem::path& filename, int mode)
 
- {
 
- 	logGlobal->trace("CProxyROIOApi: stream opened for %s with mode %d", filename.string(), mode);
 
- 	data->seek(0);
 
- 	return data;
 
- }
 
- voidpf ZCALLBACK CProxyROIOApi::openFileProxy(voidpf opaque, const void* filename, int mode)
 
- {
 
- 	assert(opaque != nullptr);
 
- 	boost::filesystem::path path;
 
- 	if(filename != nullptr)
 
- 		path =  static_cast<const boost::filesystem::path::value_type *>(filename);
 
- 	return (static_cast<CProxyROIOApi *>(opaque))->openFile(path, mode);
 
- }
 
- uLong ZCALLBACK CProxyROIOApi::readFileProxy(voidpf opaque, voidpf stream, void * buf, uLong size)
 
- {
 
- 	return streamRead<CInputStream>(opaque, stream, buf, size);
 
- }
 
- uLong ZCALLBACK CProxyROIOApi::writeFileProxy(voidpf opaque, voidpf stream, const void* buf, uLong size)
 
- {
 
- 	logGlobal->error("Attempt to write to read-only stream");
 
- 	return 0;
 
- }
 
- ZPOS64_T ZCALLBACK CProxyROIOApi::tellFileProxy(voidpf opaque, voidpf stream)
 
- {
 
- 	return streamTell<CInputStream>(opaque, stream);
 
- }
 
- long ZCALLBACK CProxyROIOApi::seekFileProxy(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
 
- {
 
- 	return streamSeek<CInputStream>(opaque, stream, offset, origin);
 
- }
 
- int ZCALLBACK CProxyROIOApi::closeFileProxy(voidpf opaque, voidpf stream)
 
- {
 
- 	return streamProxyClose<CInputStream>(opaque, stream);
 
- }
 
- int ZCALLBACK CProxyROIOApi::errorFileProxy(voidpf opaque, voidpf stream)
 
- {
 
- 	return 0;
 
- }
 
- VCMI_LIB_NAMESPACE_END
 
 
  |