Fix last review findings and finish windows bits

pull/4381/head
Kim Kulling 2022-02-14 20:25:18 +01:00
parent 52b6c4f7c0
commit c718500c55
6 changed files with 48 additions and 51 deletions

View File

@ -175,7 +175,7 @@ void BlenderImporter::InternReadFile(const std::string &pFile,
size_t total = 0; size_t total = 0;
Compression compression; Compression compression;
if (compression.open(Compression::Format::Binary, Compression::FlushMode::NoFlush, 16 + MAX_WBITS)) { if (compression.open(Compression::Format::Binary, Compression::FlushMode::NoFlush, 16 + Compression::MaxWBits)) {
total = compression.decompress((unsigned char *)reader->GetPtr(), reader->GetRemainingSize(), uncompressed); total = compression.decompress((unsigned char *)reader->GetPtr(), reader->GetRemainingSize(), uncompressed);
compression.close(); compression.close();
} }

View File

@ -571,30 +571,6 @@ void ReadBinaryDataArray(char type, uint32_t count, const char*& data, const cha
compress.decompress(data, comp_len, buff); compress.decompress(data, comp_len, buff);
compress.close(); compress.close();
} }
/* z_stream zstream = {};
zstream.opaque = Z_NULL;
zstream.zalloc = Z_NULL;
zstream.zfree = Z_NULL;
zstream.data_type = Z_BINARY;
// http://hewgill.com/journal/entries/349-how-to-decompress-gzip-stream-with-zlib
if(Z_OK != inflateInit(&zstream)) {
ParseError("failure initializing zlib");
}
zstream.next_in = reinterpret_cast<Bytef *>(const_cast<char *>(data));
zstream.avail_in = comp_len;
zstream.avail_out = static_cast<uInt>(buff.size());
zstream.next_out = reinterpret_cast<Bytef*>(&*buff.begin());
const int ret = inflate(&zstream, Z_FINISH);
if (ret != Z_STREAM_END && ret != Z_OK) {
ParseError("failure decompressing compressed data section");
}
// terminate zlib
inflateEnd(&zstream);*/
} }
#ifdef ASSIMP_BUILD_DEBUG #ifdef ASSIMP_BUILD_DEBUG
else { else {

View File

@ -117,13 +117,13 @@ XFileParser::XFileParser(const std::vector<char> &pBuffer) :
mIsBinaryFormat = true; mIsBinaryFormat = true;
compressed = true; compressed = true;
} else } else
ThrowException("Unsupported xfile format '", mP[8], mP[9], mP[10], mP[11], "'"); ThrowException("Unsupported x-file format '", mP[8], mP[9], mP[10], mP[11], "'");
// float size // float size
mBinaryFloatSize = (unsigned int)(mP[12] - 48) * 1000 + (unsigned int)(mP[13] - 48) * 100 + (unsigned int)(mP[14] - 48) * 10 + (unsigned int)(mP[15] - 48); mBinaryFloatSize = (unsigned int)(mP[12] - 48) * 1000 + (unsigned int)(mP[13] - 48) * 100 + (unsigned int)(mP[14] - 48) * 10 + (unsigned int)(mP[15] - 48);
if (mBinaryFloatSize != 32 && mBinaryFloatSize != 64) if (mBinaryFloatSize != 32 && mBinaryFloatSize != 64)
ThrowException("Unknown float size ", mBinaryFloatSize, " specified in xfile header."); ThrowException("Unknown float size ", mBinaryFloatSize, " specified in x-file header.");
// The x format specifies size in bits, but we work in bytes // The x format specifies size in bits, but we work in bytes
mBinaryFloatSize /= 8; mBinaryFloatSize /= 8;
@ -188,7 +188,7 @@ XFileParser::XFileParser(const std::vector<char> &pBuffer) :
Compression compression; Compression compression;
uncompressed.resize(est_out + 1); uncompressed.resize(est_out + 1);
char *out = &uncompressed.front(); char *out = &uncompressed.front();
if (compression.open(mIsBinaryFormat ? Compression::Format::Binary : Compression::Format::ASCII, Compression::FlushMode::SyncFlush, -MAX_WBITS)) { if (compression.open(mIsBinaryFormat ? Compression::Format::Binary : Compression::Format::ASCII, Compression::FlushMode::SyncFlush, -Compression::MAX_WBITS)) {
while (mP + 3 < mEnd) { while (mP + 3 < mEnd) {
uint16_t ofs = *((uint16_t *)mP); uint16_t ofs = *((uint16_t *)mP);
AI_SWAP2(ofs); AI_SWAP2(ofs);

View File

@ -53,8 +53,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <assimp/importerdesc.h> #include <assimp/importerdesc.h>
#include <assimp/mesh.h> #include <assimp/mesh.h>
#include <assimp/scene.h> #include <assimp/scene.h>
#include <cctype> //#include <cctype>
#include <memory> //#include <memory>
using namespace Assimp; using namespace Assimp;
@ -132,7 +132,7 @@ void XGLImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSy
Compression compression; Compression compression;
size_t total = 0l; size_t total = 0l;
if (compression.open(Compression::Format::Binary, Compression::FlushMode::NoFlush, -MAX_WBITS)) { if (compression.open(Compression::Format::Binary, Compression::FlushMode::NoFlush, -Compression::MaxWBits)) {
// skip two extra bytes, zgl files do carry a crc16 upfront (I think) // skip two extra bytes, zgl files do carry a crc16 upfront (I think)
raw_reader->IncPtr(2); raw_reader->IncPtr(2);
total = compression.decompress((unsigned char *)raw_reader->GetPtr(), raw_reader->GetRemainingSize(), uncompressed); total = compression.decompress((unsigned char *)raw_reader->GetPtr(), raw_reader->GetRemainingSize(), uncompressed);
@ -200,7 +200,7 @@ void XGLImporter::ReadWorld(XmlNode &node, TempScope &scope) {
if (!nd) { if (!nd) {
ThrowException("failure reading <world>"); ThrowException("failure reading <world>");
} }
if (!nd->mName.length) { if (nd->mName.length == 0) {
nd->mName.Set("WORLD"); nd->mName.Set("WORLD");
} }
@ -784,4 +784,4 @@ aiColor3D XGLImporter::ReadCol3(XmlNode &node) {
return aiColor3D(v.x, v.y, v.z); return aiColor3D(v.x, v.y, v.z);
} }
#endif #endif // ASSIMP_BUILD_NO_XGL_IMPORTER

View File

@ -43,15 +43,16 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <assimp/ai_assert.h> #include <assimp/ai_assert.h>
#include <assimp/Exceptional.h> #include <assimp/Exceptional.h>
namespace Assimp { namespace Assimp {
struct Compression::impl { struct Compression::impl {
bool mOpen; bool mOpen;
z_stream mZSstream; z_stream mZSstream;
FlushMode mFlushMode; FlushMode mFlushMode;
impl() : impl() :
mOpen(false), mOpen(false),
mZSstream(),
mFlushMode(Compression::FlushMode::NoFlush) { mFlushMode(Compression::FlushMode::NoFlush) {
// empty // empty
} }
@ -97,8 +98,6 @@ bool Compression::open(Format format, FlushMode flush, int windowBits) {
return mImpl->mOpen; return mImpl->mOpen;
} }
constexpr size_t MYBLOCK = 32786;
static int getFlushMode(Compression::FlushMode flush) { static int getFlushMode(Compression::FlushMode flush) {
int z_flush = 0; int z_flush = 0;
switch (flush) { switch (flush) {
@ -119,15 +118,19 @@ static int getFlushMode(Compression::FlushMode flush) {
return z_flush; return z_flush;
} }
constexpr size_t MYBLOCK = 32786;
size_t Compression::decompress(const void *data, size_t in, std::vector<char> &uncompressed) { size_t Compression::decompress(const void *data, size_t in, std::vector<char> &uncompressed) {
ai_assert(mImpl != nullptr); ai_assert(mImpl != nullptr);
if (data == nullptr || in == 0) {
return 0l;
}
mImpl->mZSstream.next_in = (Bytef*)(data); mImpl->mZSstream.next_in = (Bytef*)(data);
mImpl->mZSstream.avail_in = (uInt)in; mImpl->mZSstream.avail_in = (uInt)in;
int ret = 0; int ret = 0;
size_t total = 0l; size_t total = 0l;
const int flushMode = getFlushMode(mImpl->mFlushMode); const int flushMode = getFlushMode(mImpl->mFlushMode);
if (flushMode == Z_FINISH) { if (flushMode == Z_FINISH) {
mImpl->mZSstream.avail_out = static_cast<uInt>(uncompressed.size()); mImpl->mZSstream.avail_out = static_cast<uInt>(uncompressed.size());
@ -160,6 +163,10 @@ size_t Compression::decompress(const void *data, size_t in, std::vector<char> &u
} }
size_t Compression::decompressBlock(const void *data, size_t in, char *out, size_t availableOut) { size_t Compression::decompressBlock(const void *data, size_t in, char *out, size_t availableOut) {
ai_assert(mImpl != nullptr);
if (data == nullptr || in == 0 || out == nullptr || availableOut == 0) {
return 0l;
}
// push data to the stream // push data to the stream
mImpl->mZSstream.next_in = (Bytef *)data; mImpl->mZSstream.next_in = (Bytef *)data;
@ -169,8 +176,9 @@ size_t Compression::decompressBlock(const void *data, size_t in, char *out, size
// and decompress the data .... // and decompress the data ....
int ret = ::inflate(&mImpl->mZSstream, Z_SYNC_FLUSH); int ret = ::inflate(&mImpl->mZSstream, Z_SYNC_FLUSH);
if (ret != Z_OK && ret != Z_STREAM_END) if (ret != Z_OK && ret != Z_STREAM_END) {
throw DeadlyImportError("X: Failed to decompress MSZIP-compressed data"); throw DeadlyImportError("X: Failed to decompress MSZIP-compressed data");
}
::inflateReset(&mImpl->mZSstream); ::inflateReset(&mImpl->mZSstream);
::inflateSetDictionary(&mImpl->mZSstream, (const Bytef *)out, (uInt)availableOut - mImpl->mZSstream.avail_out); ::inflateSetDictionary(&mImpl->mZSstream, (const Bytef *)out, (uInt)availableOut - mImpl->mZSstream.avail_out);

View File

@ -54,21 +54,25 @@ namespace Assimp {
/// @brief This class provides the decompression of zlib-compressed data. /// @brief This class provides the decompression of zlib-compressed data.
class Compression { class Compression {
public: public:
enum class Format { static const int MaxWBits = MAX_WBITS;
InvalidFormat = -1,
Binary = 0,
ASCII,
NumFormats /// @brief Describes the format data type
enum class Format {
InvalidFormat = -1, ///< Invalid enum type.
Binary = 0, ///< Binary format.
ASCII, ///< ASCII format.
NumFormats ///< The number of supported formats.
}; };
/// @brief The supported flush mode, used for blocked access.
enum class FlushMode { enum class FlushMode {
InvalidFormat = -1, InvalidFormat = -1, ///< Invalid enum type.
NoFlush = 0, NoFlush = 0, ///< No flush, will be done on inflate end.
SyncFlush, SyncFlush, ///< Synced flush mode.
Finish, Finish, ///< Finish mode, all in once, no block access.
NumModes NumModes ///< The number of supported modes.
}; };
/// @brief The class constructor. /// @brief The class constructor.
@ -78,6 +82,9 @@ public:
~Compression(); ~Compression();
/// @brief Will open the access to the compression. /// @brief Will open the access to the compression.
/// @param[in] format The format type
/// @param[in] flush The flush mode.
/// @param[in] windowBits The windows history working size, shall be between 8 and 15.
/// @return true if close was successful, false if not. /// @return true if close was successful, false if not.
bool open(Format format, FlushMode flush, int windowBits); bool open(Format format, FlushMode flush, int windowBits);
@ -89,12 +96,18 @@ public:
/// @return true if close was successful, false if not. /// @return true if close was successful, false if not.
bool close(); bool close();
/// @brief Will decompress the data buffer. /// @brief Will decompress the data buffer in one step.
/// @param[in] data The data to decompress /// @param[in] data The data to decompress
/// @param[in] in The size of the data. /// @param[in] in The size of the data.
/// @param[out uncompressed A std::vector containing the decompressed data. /// @param[out uncompressed A std::vector containing the decompressed data.
size_t decompress(const void *data, size_t in, std::vector<char> &uncompressed); size_t decompress(const void *data, size_t in, std::vector<char> &uncompressed);
/// @brief Will decompress the data buffer block-wise.
/// @param[in] data The compressed data
/// @param[in] in The size of the data buffer
/// @param[out] out The output buffer
/// @param[out] availableOut The upper limit of the output buffer.
/// @return The size of the decompressed data buffer.
size_t decompressBlock(const void *data, size_t in, char *out, size_t availableOut); size_t decompressBlock(const void *data, size_t in, char *out, size_t availableOut);
private: private: