2015-08-11 13:44:47 +02:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2015-08-11 20:20:13 +02:00
|
|
|
#include "CMemoryBuffer.h"
|
2016-02-05 08:28:36 +02:00
|
|
|
#include "FileStream.h"
|
2015-08-11 20:20:13 +02:00
|
|
|
|
|
|
|
///CIOApi
|
2015-08-11 15:56:07 +02:00
|
|
|
voidpf ZCALLBACK CIOApi::openFileProxy(voidpf opaque, const void * filename, int mode)
|
2015-08-11 13:44:47 +02:00
|
|
|
{
|
|
|
|
assert(opaque != nullptr);
|
2016-02-05 08:28:36 +02:00
|
|
|
|
|
|
|
boost::filesystem::path path;
|
|
|
|
|
2015-08-11 13:44:47 +02:00
|
|
|
if(filename != nullptr)
|
2016-02-05 08:28:36 +02:00
|
|
|
path = static_cast<const boost::filesystem::path::value_type *>(filename);
|
|
|
|
|
|
|
|
return ((CIOApi *)opaque)->openFile(path, mode);
|
2015-08-11 13:44:47 +02:00
|
|
|
}
|
|
|
|
|
2015-08-11 15:56:07 +02:00
|
|
|
uLong ZCALLBACK CIOApi::readFileProxy(voidpf opaque, voidpf stream, void * buf, uLong size)
|
2015-08-11 13:44:47 +02:00
|
|
|
{
|
|
|
|
assert(opaque != nullptr);
|
|
|
|
assert(stream != nullptr);
|
2016-02-05 08:28:36 +02:00
|
|
|
|
2015-08-11 13:44:47 +02:00
|
|
|
CInputOutputStream * actualStream = static_cast<CInputOutputStream *>(stream);
|
2016-02-05 08:28:36 +02:00
|
|
|
|
2015-08-11 13:44:47 +02:00
|
|
|
return actualStream->read((ui8 *)buf, size);
|
|
|
|
}
|
|
|
|
|
2015-08-11 15:56:07 +02:00
|
|
|
uLong ZCALLBACK CIOApi::writeFileProxy(voidpf opaque, voidpf stream, const void * buf, uLong size)
|
2015-08-11 13:44:47 +02:00
|
|
|
{
|
|
|
|
assert(opaque != nullptr);
|
|
|
|
assert(stream != nullptr);
|
|
|
|
|
|
|
|
CInputOutputStream * actualStream = static_cast<CInputOutputStream *>(stream);
|
|
|
|
return (uLong)actualStream->write((const ui8 *)buf, size);
|
|
|
|
}
|
|
|
|
|
2015-08-11 15:56:07 +02:00
|
|
|
ZPOS64_T ZCALLBACK CIOApi::tellFileProxy(voidpf opaque, voidpf stream)
|
2015-08-11 13:44:47 +02:00
|
|
|
{
|
|
|
|
assert(opaque != nullptr);
|
|
|
|
assert(stream != nullptr);
|
2016-02-05 08:28:36 +02:00
|
|
|
|
|
|
|
CInputOutputStream * actualStream = static_cast<CInputOutputStream *>(stream);
|
2015-08-11 13:44:47 +02:00
|
|
|
return actualStream->tell();
|
|
|
|
}
|
|
|
|
|
2015-08-11 15:56:07 +02:00
|
|
|
long ZCALLBACK CIOApi::seekFileProxy(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
|
2015-08-11 13:44:47 +02:00
|
|
|
{
|
|
|
|
assert(opaque != nullptr);
|
|
|
|
assert(stream != nullptr);
|
2016-02-05 08:28:36 +02:00
|
|
|
|
|
|
|
CInputOutputStream * actualStream = static_cast<CInputOutputStream *>(stream);
|
2015-08-11 13:44:47 +02:00
|
|
|
|
|
|
|
long ret = 0;
|
|
|
|
switch (origin)
|
|
|
|
{
|
|
|
|
case ZLIB_FILEFUNC_SEEK_CUR :
|
2015-08-13 23:16:31 +02:00
|
|
|
if(actualStream->skip(offset) != offset)
|
|
|
|
ret = -1;
|
2015-08-11 13:44:47 +02:00
|
|
|
break;
|
2015-08-13 23:16:31 +02:00
|
|
|
case ZLIB_FILEFUNC_SEEK_END:
|
|
|
|
{
|
|
|
|
const si64 pos = actualStream->getSize() - offset;
|
|
|
|
if(actualStream->seek(pos) != pos)
|
|
|
|
ret = -1;
|
2016-02-05 08:28:36 +02:00
|
|
|
}
|
2015-08-11 13:44:47 +02:00
|
|
|
break;
|
|
|
|
case ZLIB_FILEFUNC_SEEK_SET :
|
2015-08-13 23:16:31 +02:00
|
|
|
if(actualStream->seek(offset) != offset)
|
|
|
|
ret = -1;
|
2015-08-11 13:44:47 +02:00
|
|
|
break;
|
|
|
|
default: ret = -1;
|
|
|
|
}
|
2015-08-13 23:16:31 +02:00
|
|
|
if(ret == -1)
|
2016-02-05 08:28:36 +02:00
|
|
|
logGlobal->error("CIOApi::seekFileProxy: seek failed");
|
2015-08-11 13:44:47 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-11 15:56:07 +02:00
|
|
|
int ZCALLBACK CIOApi::closeFileProxy(voidpf opaque, voidpf stream)
|
2015-08-11 13:44:47 +02:00
|
|
|
{
|
|
|
|
assert(opaque != nullptr);
|
|
|
|
assert(stream != nullptr);
|
2016-02-05 08:28:36 +02:00
|
|
|
|
2015-08-11 13:44:47 +02:00
|
|
|
CInputOutputStream * actualStream = static_cast<CInputOutputStream *>(stream);
|
2016-02-05 08:28:36 +02:00
|
|
|
|
2015-08-11 20:20:13 +02:00
|
|
|
((CIOApi *)opaque)->closeFile(actualStream);
|
2016-02-05 08:28:36 +02:00
|
|
|
|
2015-08-11 13:44:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-11 15:56:07 +02:00
|
|
|
int ZCALLBACK CIOApi::errorFileProxy(voidpf opaque, voidpf stream)
|
2015-08-11 13:44:47 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-11 15:56:07 +02:00
|
|
|
zlib_filefunc64_def CIOApi::getApiStructure() const
|
2015-08-11 13:44:47 +02:00
|
|
|
{
|
2015-08-11 15:56:07 +02:00
|
|
|
zlib_filefunc64_def api;
|
|
|
|
api.opaque = (void *) this;
|
2015-08-11 13:44:47 +02:00
|
|
|
api.zopen64_file = &openFileProxy;
|
|
|
|
api.zread_file = &readFileProxy;
|
|
|
|
api.zwrite_file = &writeFileProxy;
|
|
|
|
api.ztell64_file = &tellFileProxy;
|
|
|
|
api.zseek64_file = &seekFileProxy;
|
|
|
|
api.zclose_file = &closeFileProxy;
|
2016-02-05 08:28:36 +02:00
|
|
|
api.zerror_file = &errorFileProxy;
|
|
|
|
|
2015-08-11 15:56:07 +02:00
|
|
|
return api;
|
|
|
|
}
|
|
|
|
|
2015-08-11 20:20:13 +02:00
|
|
|
void CIOApi::closeFile(CInputOutputStream * stream) const
|
|
|
|
{
|
|
|
|
delete stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
///CDefaultIOApi
|
2015-08-11 15:56:07 +02:00
|
|
|
CDefaultIOApi::CDefaultIOApi()
|
|
|
|
{
|
2016-02-05 08:28:36 +02:00
|
|
|
|
2015-08-11 15:56:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CDefaultIOApi::~CDefaultIOApi()
|
|
|
|
{
|
2016-02-05 08:28:36 +02:00
|
|
|
|
2015-08-11 15:56:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
zlib_filefunc64_def CDefaultIOApi::getApiStructure() const
|
|
|
|
{
|
2016-02-05 08:28:36 +02:00
|
|
|
return * FileStream::GetMinizipFilefunc();
|
2015-08-11 15:56:07 +02:00
|
|
|
}
|
|
|
|
|
2016-02-05 08:28:36 +02:00
|
|
|
CInputOutputStream * CDefaultIOApi::openFile(const boost::filesystem::path & filename, int mode) const
|
2015-08-11 15:56:07 +02:00
|
|
|
{
|
2016-02-05 08:28:36 +02:00
|
|
|
throw new std::runtime_error("CDefaultIOApi::openFile call not expected.");
|
2015-08-11 15:56:07 +02:00
|
|
|
}
|
|
|
|
|
2015-08-11 20:20:13 +02:00
|
|
|
///CProxyIOApi
|
|
|
|
CProxyIOApi::CProxyIOApi(CInputOutputStream * buffer):
|
|
|
|
data(buffer)
|
2015-08-11 15:56:07 +02:00
|
|
|
{
|
2016-02-05 08:28:36 +02:00
|
|
|
|
2015-08-11 13:44:47 +02:00
|
|
|
}
|
|
|
|
|
2015-08-11 20:20:13 +02:00
|
|
|
CProxyIOApi::~CProxyIOApi()
|
2015-08-11 15:56:07 +02:00
|
|
|
{
|
2016-02-05 08:28:36 +02:00
|
|
|
|
2015-08-11 15:56:07 +02:00
|
|
|
}
|
2015-08-11 20:20:13 +02:00
|
|
|
|
2016-02-05 08:28:36 +02:00
|
|
|
CInputOutputStream * CProxyIOApi::openFile(const boost::filesystem::path & filename, int mode) const
|
2015-08-11 20:20:13 +02:00
|
|
|
{
|
2016-02-05 08:28:36 +02:00
|
|
|
logGlobal->traceStream() << "CProxyIOApi: stream opened for " <<filename.string() <<" with mode "<<mode;
|
|
|
|
|
2015-08-11 20:20:13 +02:00
|
|
|
data->seek(0);
|
2015-08-18 00:56:16 +02:00
|
|
|
return data;
|
2015-08-11 20:20:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CProxyIOApi::closeFile(CInputOutputStream * stream) const
|
|
|
|
{
|
|
|
|
logGlobal->traceStream() << "CProxyIOApi: stream closed";
|
|
|
|
stream->seek(0);//stream is local singleton and even not owned, just seek
|
|
|
|
}
|