mirror of
https://github.com/AquariaOSE/Aquaria.git
synced 2025-10-03 21:01:14 +00:00
[vfs #1] Add ttvfs, miniz, and minihttp sources
This commit is contained in:
parent
99e3f5ebe2
commit
a90f57afb0
36 changed files with 10047 additions and 0 deletions
15
ExternalLibs/ttvfs_zip/CMakeLists.txt
Normal file
15
ExternalLibs/ttvfs_zip/CMakeLists.txt
Normal file
|
@ -0,0 +1,15 @@
|
|||
|
||||
set(ttvfs_zip_SRC
|
||||
VFSDirZip.cpp
|
||||
VFSDirZip.h
|
||||
VFSFileZip.cpp
|
||||
VFSFileZip.h
|
||||
VFSZipArchiveLoader.cpp
|
||||
VFSZipArchiveLoader.h
|
||||
miniz.c
|
||||
miniz.h
|
||||
)
|
||||
|
||||
include_directories(${TTVFS_INCLUDE_DIRS})
|
||||
|
||||
add_library(ttvfs_zip ${ttvfs_zip_SRC})
|
107
ExternalLibs/ttvfs_zip/VFSDirZip.cpp
Normal file
107
ExternalLibs/ttvfs_zip/VFSDirZip.cpp
Normal file
|
@ -0,0 +1,107 @@
|
|||
#include "VFSFileZip.h"
|
||||
#include "VFSDirZip.h"
|
||||
#include "VFSTools.h"
|
||||
|
||||
#include "VFSInternal.h"
|
||||
|
||||
#include "miniz.h"
|
||||
|
||||
VFS_NAMESPACE_START
|
||||
|
||||
|
||||
static size_t zip_read_func(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n)
|
||||
{
|
||||
VFSFile *vf = (VFSFile*)pOpaque;
|
||||
mz_int64 cur_ofs = vf->getpos();
|
||||
if((mz_int64)file_ofs < 0)
|
||||
return 0;
|
||||
if(cur_ofs != (mz_int64)file_ofs && !vf->seek((vfspos)file_ofs))
|
||||
return 0;
|
||||
return vf->read(pBuf, n);
|
||||
}
|
||||
|
||||
static bool zip_reader_init_vfsfile(mz_zip_archive *pZip, VFSFile *vf, mz_uint32 flags)
|
||||
{
|
||||
if(!(pZip || vf))
|
||||
return false;
|
||||
vf->open("rb");
|
||||
mz_uint64 file_size = vf->size();
|
||||
if(!file_size)
|
||||
{
|
||||
vf->close();
|
||||
return false;
|
||||
}
|
||||
pZip->m_pRead = zip_read_func;
|
||||
pZip->m_pIO_opaque = vf;
|
||||
if (!mz_zip_reader_init(pZip, file_size, flags))
|
||||
{
|
||||
vf->close();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
VFSDirZip::VFSDirZip(VFSFile *zf)
|
||||
: VFSDir(zf->fullname()), _zf(zf)
|
||||
{
|
||||
_zf->ref++;
|
||||
_setOrigin(this);
|
||||
memset(&_zip, 0, sizeof(_zip));
|
||||
}
|
||||
|
||||
VFSDirZip::~VFSDirZip()
|
||||
{
|
||||
close();
|
||||
_zf->ref--;
|
||||
}
|
||||
|
||||
bool VFSDirZip::close(void)
|
||||
{
|
||||
mz_zip_reader_end(&_zip);
|
||||
_zf->close();
|
||||
return true;
|
||||
}
|
||||
|
||||
VFSDir *VFSDirZip::createNew(const char *dir) const
|
||||
{
|
||||
return VFSDir::createNew(dir); // inside a Zip file; only the base dir can be a real VFSDirZip.
|
||||
}
|
||||
|
||||
unsigned int VFSDirZip::load(bool /*ignored*/)
|
||||
{
|
||||
close();
|
||||
|
||||
if(!zip_reader_init_vfsfile(&_zip, _zf, 0))
|
||||
return 0;
|
||||
|
||||
unsigned int files = mz_zip_reader_get_num_files(&_zip);
|
||||
|
||||
mz_zip_archive_file_stat fs;
|
||||
for (unsigned int i = 0; i < files; ++i)
|
||||
{
|
||||
// FIXME: do we want empty dirs in the tree?
|
||||
if(mz_zip_reader_is_file_a_directory(&_zip, i))
|
||||
continue;
|
||||
if(mz_zip_reader_is_file_encrypted(&_zip, i))
|
||||
continue;
|
||||
if(!mz_zip_reader_file_stat(&_zip, i, &fs))
|
||||
continue;
|
||||
if(getFile(fs.m_filename))
|
||||
continue;
|
||||
|
||||
VFSFileZip *vf = new VFSFileZip(&_zip);
|
||||
vf->_setOrigin(this);
|
||||
memcpy(vf->getZipFileStat(), &fs, sizeof(mz_zip_archive_file_stat));
|
||||
vf->_init();
|
||||
addRecursive(vf, true, VFSDir::NONE);
|
||||
vf->ref--;
|
||||
}
|
||||
|
||||
// Not necessary to keep open all the time, VFSFileZip will re-open the archive if needed
|
||||
//close();
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
VFS_NAMESPACE_END
|
32
ExternalLibs/ttvfs_zip/VFSDirZip.h
Normal file
32
ExternalLibs/ttvfs_zip/VFSDirZip.h
Normal file
|
@ -0,0 +1,32 @@
|
|||
#ifndef VFSDIR_ZIP_H
|
||||
#define VFSDIR_ZIP_H
|
||||
|
||||
#include "VFSDir.h"
|
||||
|
||||
#include "miniz.h"
|
||||
|
||||
VFS_NAMESPACE_START
|
||||
|
||||
class VFSFile;
|
||||
|
||||
class VFSDirZip : public VFSDir
|
||||
{
|
||||
public:
|
||||
VFSDirZip(VFSFile *zf);
|
||||
virtual ~VFSDirZip();
|
||||
virtual unsigned int load(bool recusive);
|
||||
virtual VFSDir *createNew(const char *dir) const;
|
||||
virtual const char *getType() const { return "VFSDirZip"; }
|
||||
virtual bool close();
|
||||
|
||||
inline mz_zip_archive *getZip() { return &_zip; }
|
||||
|
||||
protected:
|
||||
VFSFile *_zf;
|
||||
mz_zip_archive _zip;
|
||||
std::string zipfilename;
|
||||
};
|
||||
|
||||
VFS_NAMESPACE_END
|
||||
|
||||
#endif
|
197
ExternalLibs/ttvfs_zip/VFSFileZip.cpp
Normal file
197
ExternalLibs/ttvfs_zip/VFSFileZip.cpp
Normal file
|
@ -0,0 +1,197 @@
|
|||
#include "VFSFileZip.h"
|
||||
#include "VFSInternal.h"
|
||||
#include "VFSTools.h"
|
||||
#include "VFSDir.h"
|
||||
|
||||
VFS_NAMESPACE_START
|
||||
|
||||
// From miniz.c
|
||||
//#define MZ_ZIP_MODE_READING 2
|
||||
|
||||
|
||||
static bool zip_reader_reopen_vfsfile(mz_zip_archive *pZip, mz_uint32 flags)
|
||||
{
|
||||
if(!(pZip && pZip->m_pIO_opaque && pZip->m_pRead))
|
||||
return false;
|
||||
VFSFile *vf = (VFSFile*)pZip->m_pIO_opaque;
|
||||
if(!vf->isopen())
|
||||
if(!vf->open("rb"))
|
||||
return false;
|
||||
if(pZip->m_zip_mode == MZ_ZIP_MODE_READING)
|
||||
return true;
|
||||
mz_uint64 file_size = vf->size();
|
||||
if(!file_size)
|
||||
{
|
||||
vf->close();
|
||||
return false;
|
||||
}
|
||||
if (!mz_zip_reader_init(pZip, file_size, flags))
|
||||
{
|
||||
vf->close();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
VFSFileZip::VFSFileZip(mz_zip_archive *zip)
|
||||
: VFSFile(NULL), _fixedStr(NULL), _zip(zip)
|
||||
{
|
||||
_mode = "b"; // binary mode by default
|
||||
_pos = 0;
|
||||
}
|
||||
|
||||
void VFSFileZip::_init()
|
||||
{
|
||||
_setName(_zipstat.m_filename);
|
||||
}
|
||||
|
||||
VFSFileZip::~VFSFileZip()
|
||||
{
|
||||
dropBuf(true);
|
||||
}
|
||||
|
||||
bool VFSFileZip::open(const char *mode /* = NULL */)
|
||||
{
|
||||
VFS_GUARD_OPT(this);
|
||||
|
||||
_pos = 0;
|
||||
if(mode)
|
||||
{
|
||||
if(_fixedStr && _mode != mode)
|
||||
{
|
||||
delete [] _fixedStr;
|
||||
_fixedStr = NULL;
|
||||
}
|
||||
|
||||
_mode = mode;
|
||||
}
|
||||
return true; // does not have to be opened
|
||||
}
|
||||
|
||||
bool VFSFileZip::isopen(void) const
|
||||
{
|
||||
return true; // is always open
|
||||
}
|
||||
|
||||
bool VFSFileZip::iseof(void) const
|
||||
{
|
||||
VFS_GUARD_OPT(this);
|
||||
return _pos >= _zipstat.m_uncomp_size;
|
||||
}
|
||||
|
||||
bool VFSFileZip::close(void)
|
||||
{
|
||||
//return flush(); // TODO: write to zip file on close
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool VFSFileZip::seek(vfspos pos)
|
||||
{
|
||||
if(pos >= 0xFFFFFFFF) // zip files have uint32 range only
|
||||
return false;
|
||||
|
||||
VFS_GUARD_OPT(this);
|
||||
_pos = (unsigned int)pos;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool VFSFileZip::flush(void)
|
||||
{
|
||||
// FIXME: use this to actually write to zip file?
|
||||
return false;
|
||||
}
|
||||
|
||||
vfspos VFSFileZip::getpos(void) const
|
||||
{
|
||||
VFS_GUARD_OPT(this);
|
||||
return _pos;
|
||||
}
|
||||
|
||||
unsigned int VFSFileZip::read(void *dst, unsigned int bytes)
|
||||
{
|
||||
VFS_GUARD_OPT(this);
|
||||
char *mem = (char*)getBuf();
|
||||
char *startptr = mem + _pos;
|
||||
char *endptr = mem + size();
|
||||
bytes = std::min((unsigned int)(endptr - startptr), bytes); // limit in case reading over buffer size
|
||||
if(_mode.find('b') == std::string::npos)
|
||||
strnNLcpy((char*)dst, (const char*)startptr, bytes); // non-binary == text mode
|
||||
else
|
||||
memcpy(dst, startptr, bytes); // binary copy
|
||||
_pos += bytes;
|
||||
return bytes;
|
||||
}
|
||||
|
||||
unsigned int VFSFileZip::write(const void *src, unsigned int bytes)
|
||||
{
|
||||
/*VFS_GUARD_OPT(this);
|
||||
if(getpos() + bytes >= size())
|
||||
size(getpos() + bytes); // enlarge if necessary
|
||||
|
||||
memcpy(_buf + getpos(), src, bytes);
|
||||
|
||||
// TODO: implement actually writing to the Zip file.
|
||||
|
||||
return bytes;*/
|
||||
|
||||
return VFSFile::write(src, bytes);
|
||||
}
|
||||
|
||||
vfspos VFSFileZip::size(void)
|
||||
{
|
||||
VFS_GUARD_OPT(this);
|
||||
return (vfspos)_zipstat.m_uncomp_size;
|
||||
}
|
||||
|
||||
const void *VFSFileZip::getBuf(allocator_func alloc /* = NULL */, delete_func del /* = NULL */)
|
||||
{
|
||||
assert(!alloc == !del); // either both or none may be defined. Checked extra early to prevent possible errors later.
|
||||
|
||||
VFS_GUARD_OPT(this);
|
||||
// _fixedStr gets deleted on mode change, so doing this check here is fine
|
||||
if(_fixedStr)
|
||||
return _fixedStr;
|
||||
|
||||
if(!_buf)
|
||||
{
|
||||
size_t sz = (size_t)size();
|
||||
_buf = allocHelperExtra(alloc, sz, 4);
|
||||
if(!_buf)
|
||||
return NULL;
|
||||
_delfunc = del;
|
||||
|
||||
if(!zip_reader_reopen_vfsfile(_zip, 0))
|
||||
return false; // can happen if the underlying zip file was deleted
|
||||
if(!mz_zip_reader_extract_to_mem(_zip, _zipstat.m_file_index, _buf, sz, 0))
|
||||
return false; // this should not happen
|
||||
|
||||
if(_mode.find("b") == std::string::npos) // text mode?
|
||||
{
|
||||
_fixedStr = allocHelperExtra(alloc, sz, 4);
|
||||
strnNLcpy(_fixedStr, (const char*)_buf);
|
||||
|
||||
// FIXME: is this really correct?
|
||||
VFSFile::dropBuf(true);
|
||||
|
||||
return _fixedStr;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return _buf;
|
||||
}
|
||||
|
||||
void VFSFileZip::dropBuf(bool del)
|
||||
{
|
||||
VFSFile::dropBuf(del);
|
||||
|
||||
VFS_GUARD_OPT(this);
|
||||
if(del)
|
||||
delBuf(_fixedStr);
|
||||
_fixedStr = NULL;
|
||||
|
||||
}
|
||||
|
||||
VFS_NAMESPACE_END
|
41
ExternalLibs/ttvfs_zip/VFSFileZip.h
Normal file
41
ExternalLibs/ttvfs_zip/VFSFileZip.h
Normal file
|
@ -0,0 +1,41 @@
|
|||
#ifndef VFSFILE_ZIP_H
|
||||
#define VFSFILE_ZIP_H
|
||||
|
||||
#include "VFSFile.h"
|
||||
#include "miniz.h"
|
||||
|
||||
VFS_NAMESPACE_START
|
||||
|
||||
class VFSFileZip : public VFSFile
|
||||
{
|
||||
public:
|
||||
VFSFileZip(mz_zip_archive *zip);
|
||||
virtual ~VFSFileZip();
|
||||
virtual bool open(const char *mode = NULL);
|
||||
virtual bool isopen(void) const;
|
||||
virtual bool iseof(void) const;
|
||||
virtual bool close(void);
|
||||
virtual bool seek(vfspos pos);
|
||||
virtual bool flush(void);
|
||||
virtual vfspos getpos(void) const;
|
||||
virtual unsigned int read(void *dst, unsigned int bytes);
|
||||
virtual unsigned int write(const void *src, unsigned int bytes);
|
||||
virtual vfspos size(void);
|
||||
virtual const void *getBuf(allocator_func alloc = NULL, delete_func del = NULL);
|
||||
virtual void dropBuf(bool del);
|
||||
virtual const char *getType(void) const { return "Zip"; }
|
||||
|
||||
inline mz_zip_archive_file_stat *getZipFileStat(void) { return &_zipstat; }
|
||||
void _init();
|
||||
|
||||
protected:
|
||||
unsigned int _pos;
|
||||
std::string _mode;
|
||||
mz_zip_archive_file_stat _zipstat;
|
||||
mz_zip_archive *_zip;
|
||||
char *_fixedStr; // for \n fixed string in text mode. cleared when mode is changed
|
||||
};
|
||||
|
||||
VFS_NAMESPACE_END
|
||||
|
||||
#endif
|
17
ExternalLibs/ttvfs_zip/VFSZipArchiveLoader.cpp
Normal file
17
ExternalLibs/ttvfs_zip/VFSZipArchiveLoader.cpp
Normal file
|
@ -0,0 +1,17 @@
|
|||
#include "VFSInternal.h"
|
||||
#include "VFSZipArchiveLoader.h"
|
||||
#include "VFSDirZip.h"
|
||||
|
||||
VFS_NAMESPACE_START
|
||||
|
||||
VFSDir *VFSZipArchiveLoader::Load(VFSFile *arch, VFSLoader ** /*unused*/, void * /*unused*/)
|
||||
{
|
||||
VFSDirZip *vd = new VFSDirZip(arch);
|
||||
if(vd->load(true))
|
||||
return vd;
|
||||
|
||||
vd->ref--;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
VFS_NAMESPACE_END
|
17
ExternalLibs/ttvfs_zip/VFSZipArchiveLoader.h
Normal file
17
ExternalLibs/ttvfs_zip/VFSZipArchiveLoader.h
Normal file
|
@ -0,0 +1,17 @@
|
|||
#ifndef VFS_ZIP_ARCHIVE_LOADER_H
|
||||
#define VFS_ZIP_ARCHIVE_LOADER_H
|
||||
|
||||
#include "VFSArchiveLoader.h"
|
||||
|
||||
VFS_NAMESPACE_START
|
||||
|
||||
class VFSZipArchiveLoader : public VFSArchiveLoader
|
||||
{
|
||||
public:
|
||||
virtual ~VFSZipArchiveLoader() {}
|
||||
virtual VFSDir *Load(VFSFile *arch, VFSLoader **ldr, void *opaque = NULL);
|
||||
};
|
||||
|
||||
VFS_NAMESPACE_END
|
||||
|
||||
#endif
|
4834
ExternalLibs/ttvfs_zip/miniz.c
Normal file
4834
ExternalLibs/ttvfs_zip/miniz.c
Normal file
File diff suppressed because it is too large
Load diff
7
ExternalLibs/ttvfs_zip/miniz.h
Normal file
7
ExternalLibs/ttvfs_zip/miniz.h
Normal file
|
@ -0,0 +1,7 @@
|
|||
#ifndef MINIZ_H
|
||||
#define MINIZ_H
|
||||
|
||||
#define MINIZ_HEADER_FILE_ONLY
|
||||
#include "miniz.c"
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue