Conflicts:
	src/SpriteFileReader.cpp
This commit is contained in:
Antidote 2014-02-26 21:56:01 -08:00
commit f77b394c6b
16 changed files with 812 additions and 236 deletions

View File

@ -18,6 +18,7 @@
#include "Stream.hpp" #include "Stream.hpp"
#include <string> #include <string>
#include <functional>
namespace zelda namespace zelda
{ {
@ -64,6 +65,12 @@ public:
*/ */
std::string filepath() const; std::string filepath() const;
bool readBit();
Int8 readByte();
Uint8 readUByte();
Int8* readBytes(Int64 length);
Uint8* readUBytes(Int64 length);
/*! \brief Reads a Int16 and swaps to proper endianness depending on platform /*! \brief Reads a Int16 and swaps to proper endianness depending on platform
* and Stream settings, and advances the current position * and Stream settings, and advances the current position
* *
@ -163,7 +170,9 @@ public:
*/ */
std::string readString(); std::string readString();
void setProgressCallback(std::function<void(int)> cb);
protected: protected:
void loadData();
/*! \brief Overload of isOpenForWriting in Stream /*! \brief Overload of isOpenForWriting in Stream
* *
* \return false * \return false
@ -180,6 +189,9 @@ protected:
*/ */
void writeBytes(Int8*, Int64); void writeBytes(Int8*, Int64);
std::string m_filepath; //!< Path to the target file std::string m_filepath; //!< Path to the target file
Uint32 m_currentLength;
FILE* m_file;
std::function<void(int)> m_progressCallback;
}; };
} }
} }

View File

@ -28,6 +28,9 @@ namespace Compression
Int32 decompressZlib(Uint8* src, Uint32 srcLen, Uint8* dst, Uint32 dstLen); Int32 decompressZlib(Uint8* src, Uint32 srcLen, Uint8* dst, Uint32 dstLen);
Int32 compressZlib(const Uint8* src, Uint32 srcLen, Uint8* dst, Uint32 dstLen); Int32 compressZlib(const Uint8* src, Uint32 srcLen, Uint8* dst, Uint32 dstLen);
// lzo compression
Int32 decompressLZO(Uint8* source, Int32 sourceSize, Uint8* dest, Int32& dstSize);
// Yaz0 encoding // Yaz0 encoding
Uint32 yaz0Decode(Uint8* src, Uint8* dst, Uint32 uncompressedSize); Uint32 yaz0Decode(Uint8* src, Uint8* dst, Uint32 uncompressedSize);
Uint32 yaz0Encode(Uint8* src, Uint32 srcSize, Uint8* data); Uint32 yaz0Encode(Uint8* src, Uint32 srcSize, Uint8* data);

View File

@ -18,6 +18,10 @@
#include <string> #include <string>
#define __STRX(x) #x
#define __STR(x) __STRX(x)
#define __LINE_STRING__ __STR(__LINE__)
namespace zelda namespace zelda
{ {
namespace error namespace error
@ -53,4 +57,10 @@ protected:
} // error } // error
} // zelda } // zelda
#define THROW_EXCEPTION(msg) \
do \
{ \
throw zelda::error::Exception(__LINE_STRING__ " " __FILE__ " " msg); \
} while(0)
#endif #endif

View File

@ -40,7 +40,7 @@ public:
/*! \brief The constructor for an IOException /*! \brief The constructor for an IOException
* \param message The error message to throw * \param message The error message to throw
*/ */
IOException(const std::string& message) : inline IOException(const std::string& message) :
Exception("IOException: " + message) Exception("IOException: " + message)
{} {}
}; };

View File

@ -0,0 +1,37 @@
#ifndef INVALIDDATAEXCEPTION_HPP
#define INVALIDDATAEXCEPTION_HPP
#include "Exception.hpp"
#include <sstream>
namespace zelda
{
namespace error
{
/*! \class InvalidDataException
* \brief An exception thrown on Invalid Data calls.
*
* This should only be thrown when the library tries to
* e.g pass a NULL pointer to a function which requires a valid pointer.
* <br />
* It is <b>NOT</b> appropriate to use <b>throw new</b> so avoid doing so,
* keeping things on the stack as much as possible is very important for speed.
*/
class InvalidDataException : public Exception
{
public:
inline InvalidDataException(const std::string& error)
: Exception(error)
{
}
};
}
}
#define THROW_INVALID_DATA(msg) \
do \
{ \
throw zelda::error::InvalidDataException(__LINE_STRING__ " " __FILE__ " " msg); \
} while(0)
#endif // INVALIDDATAEXCEPTION_HPP

View File

@ -122,6 +122,14 @@ public:
* \throw IOException * \throw IOException
*/ */
virtual Int8 readByte(); virtual Int8 readByte();
/*! \brief Reads a byte at the current position and advances the current position
*
* \return Uint8 The value at the current position
* \throw IOException
*/
virtual Uint8 readUByte();
/*! \brief Reads a byte at the current position and advances the current position. /*! \brief Reads a byte at the current position and advances the current position.
* *
* \return Uint8* The buffer at the current position from the given length. * \return Uint8* The buffer at the current position from the given length.

46
include/lzo.h Normal file
View File

@ -0,0 +1,46 @@
/*
* LZO 1x decompression
* copyright (c) 2006 Reimar Doeffinger
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _LZO_H
#define LZO_H
#include "Types.hpp"
#define LZO_INPUT_DEPLETED 1
#define LZO_OUTPUT_FULL 2
#define LZO_INVALID_BACKPTR 4
#define LZO_ERROR 8
#define LZO_INPUT_PADDING 4
#define LZO_OUTPUT_PADDING 12
typedef unsigned char uint8_t;
#ifdef __cplusplus
extern "C" {
#endif
int lzo1x_decode(Uint8 *out, Int32 *outlen, Uint8 *in, Int32 *inlen);
#ifdef __cplusplus
}
#endif
#endif

100
libzelda.pri Normal file
View File

@ -0,0 +1,100 @@
INCLUDEPATH += $$PWD/include
QMAKE_CXXFLAGS = -std=c++0x
unix:LIBS += -lz
win32:LIBS += -lzlib
HEADERS += \
$$PWD/include/utility.hpp \
$$PWD/include/utf8.h \
$$PWD/include/utf8/unchecked.h \
$$PWD/include/utf8/core.h \
$$PWD/include/utf8/checked.h \
$$PWD/include/Types.hpp \
$$PWD/include/TextStream.hpp \
$$PWD/include/Stream.hpp \
$$PWD/include/Mainpage.hpp \
$$PWD/include/InvalidOperationException.hpp \
$$PWD/include/IOException.hpp \
$$PWD/include/FileNotFoundException.hpp \
$$PWD/include/Exception.hpp \
$$PWD/include/BinaryWriter.hpp \
$$PWD/include/BinaryReader.hpp \
$$PWD/include/WiiBanner.hpp \
$$PWD/include/WiiFile.hpp \
$$PWD/include/WiiSave.hpp \
$$PWD/include/WiiSaveReader.hpp \
$$PWD/include/WiiSaveWriter.hpp \
$$PWD/include/aes.h \
$$PWD/include/bn.h \
$$PWD/include/ec.h \
$$PWD/include/md5.h \
$$PWD/include/sha1.h \
$$PWD/include/ALTTPStructs.hpp \
$$PWD/include/ALTTPQuest.hpp \
$$PWD/include/ALTTPFileWriter.hpp \
$$PWD/include/ALTTPFileReader.hpp \
$$PWD/include/ALTTPFile.hpp \
$$PWD/include/ALTTPEnums.hpp \
$$PWD/include/MCFileReader.hpp \
$$PWD/include/MCFile.hpp \
$$PWD/include/MCFileWriter.hpp \
$$PWD/include/ZQuestFileWriter.hpp \
$$PWD/include/ZQuestFileReader.hpp \
$$PWD/include/Compression.hpp \
$$PWD/include/lzo.h \
$$PWD/include/WiiImage.hpp \
$$PWD/include/ZQuestFile.hpp \
$$PWD/include/Checksums.hpp \
$$PWD/include/SkywardSwordFile.hpp \
$$PWD/include/SkywardSwordFileReader.hpp \
$$PWD/include/SkywardSwordFileWriter.hpp \
$$PWD/include/SkywardSwordQuest.hpp \
$$PWD/include/Sprite.hpp \
$$PWD/include/SpriteFile.hpp \
$$PWD/include/SpriteFileReader.hpp \
$$PWD/include/SpriteFileWriter.hpp \
$$PWD/include/SpriteFrame.hpp \
$$PWD/include/SpritePart.hpp \
../libzelda/include/InvalidDataException.hpp
SOURCES += \
$$PWD/src/utility.cpp \
$$PWD/src/TextStream.cpp \
$$PWD/src/Stream.cpp \
$$PWD/src/BinaryWriter.cpp \
$$PWD/src/BinaryReader.cpp \
$$PWD/src/WiiBanner.cpp \
$$PWD/src/WiiFile.cpp \
$$PWD/src/WiiSave.cpp \
$$PWD/src/WiiSaveReader.cpp \
$$PWD/src/WiiSaveWriter.cpp \
$$PWD/src/aes.c \
$$PWD/src/bn.cpp \
$$PWD/src/ec.cpp \
$$PWD/src/md5.cpp \
$$PWD/src/sha1.cpp \
$$PWD/src/ALTTPQuest.cpp \
$$PWD/src/ALTTPFileWriter.cpp \
$$PWD/src/ALTTPFileReader.cpp \
$$PWD/src/ALTTPFile.cpp \
$$PWD/src/MCFileReader.cpp \
$$PWD/src/MCFile.cpp \
$$PWD/src/MCFileWriter.cpp \
$$PWD/src/ZQuestFileWriter.cpp \
$$PWD/src/ZQuestFileReader.cpp \
$$PWD/src/Compression.cpp \
$$PWD/src/lzo.c \
$$PWD/src/WiiImage.cpp \
$$PWD/src/ZQuestFile.cpp \
$$PWD/src/Checksums.cpp \
$$PWD/src/SkywardSwordFile.cpp \
$$PWD/src/SkywardSwordFileReader.cpp \
$$PWD/src/SkywardSwordFileWriter.cpp \
$$PWD/src/SkywardSwordQuest.cpp \
$$PWD/src/Sprite.cpp \
$$PWD/src/SpriteFile.cpp \
$$PWD/src/SpriteFileReader.cpp \
$$PWD/src/SpriteFileWriter.cpp \
$$PWD/src/SpriteFrame.cpp \
$$PWD/src/SpritePart.cpp

View File

@ -68,6 +68,7 @@ HEADERS += \
include/ZQuestFileWriter.hpp \ include/ZQuestFileWriter.hpp \
include/ZQuestFileReader.hpp \ include/ZQuestFileReader.hpp \
include/Compression.hpp \ include/Compression.hpp \
include/lzo.h \
include/WiiImage.hpp \ include/WiiImage.hpp \
include/ZQuestFile.hpp \ include/ZQuestFile.hpp \
include/Checksums.hpp \ include/Checksums.hpp \
@ -108,6 +109,7 @@ SOURCES += \
src/ZQuestFileWriter.cpp \ src/ZQuestFileWriter.cpp \
src/ZQuestFileReader.cpp \ src/ZQuestFileReader.cpp \
src/Compression.cpp \ src/Compression.cpp \
src/lzo.c \
src/WiiImage.cpp \ src/WiiImage.cpp \
src/ZQuestFile.cpp \ src/ZQuestFile.cpp \
src/Checksums.cpp \ src/Checksums.cpp \

View File

@ -49,43 +49,46 @@ BinaryReader::BinaryReader(const std::string& filename)
: m_filepath(filename) : m_filepath(filename)
{ {
Stream::setAutoResizing(false); Stream::setAutoResizing(false);
FILE* in; }
Uint32 length;
in = fopen(filename.c_str(), "rb");
if (!in) bool BinaryReader::readBit()
throw error::FileNotFoundException(filename); {
if (!m_data)
loadData();
fseek(in, 0, SEEK_END); return Stream::readBit();
length = ftell(in); }
fseek(in, 0, SEEK_SET);
#ifdef HW_RVL
m_data = (Uint8*)memalign(32, length);
#else
m_data = new Uint8[length];
#endif
Uint32 done = 0; Int8 BinaryReader::readByte()
Uint32 blocksize = BLOCKSZ; {
do if (!m_data)
{ loadData();
if (blocksize > length - done)
blocksize = length - done;
Int32 ret = fread(m_data + done, 1, blocksize, in); return Stream::readByte();
}
if (ret < 0) Uint8 BinaryReader::readUByte()
throw error::IOException("BinaryReader::BinaryReader -> reading data from disk"); {
else if (ret == 0) if (!m_data)
break; loadData();
done += blocksize; return Stream::readUByte();
} while (done < length); }
fclose(in); Int8* BinaryReader::readBytes(Int64 length)
m_length = length; {
m_position = 0; if (!m_data)
m_bitPosition = 0; loadData();
return Stream::readBytes(length);
}
Uint8* BinaryReader::readUBytes(Int64 length)
{
if (!m_data)
loadData();
return Stream::readUBytes(length);
} }
void BinaryReader::writeByte(Int8) void BinaryReader::writeByte(Int8)
@ -100,6 +103,9 @@ void BinaryReader::writeBytes(Int8*, Int64)
Int16 BinaryReader::readInt16() Int16 BinaryReader::readInt16()
{ {
if (!m_data)
loadData();
if (m_bitPosition > 0) if (m_bitPosition > 0)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -118,6 +124,9 @@ Int16 BinaryReader::readInt16()
Uint16 BinaryReader::readUInt16() Uint16 BinaryReader::readUInt16()
{ {
if (!m_data)
loadData();
if (m_bitPosition > 0) if (m_bitPosition > 0)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -136,6 +145,9 @@ Uint16 BinaryReader::readUInt16()
Int32 BinaryReader::readInt32() Int32 BinaryReader::readInt32()
{ {
if (!m_data)
loadData();
if (m_bitPosition > 0) if (m_bitPosition > 0)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -153,6 +165,9 @@ Int32 BinaryReader::readInt32()
Uint32 BinaryReader::readUInt32() Uint32 BinaryReader::readUInt32()
{ {
if (!m_data)
loadData();
if (m_bitPosition > 0) if (m_bitPosition > 0)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -171,6 +186,9 @@ Uint32 BinaryReader::readUInt32()
Int64 BinaryReader::readInt64() Int64 BinaryReader::readInt64()
{ {
if (!m_data)
loadData();
if (m_bitPosition > 0) if (m_bitPosition > 0)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -189,6 +207,9 @@ Int64 BinaryReader::readInt64()
Uint64 BinaryReader::readUInt64() Uint64 BinaryReader::readUInt64()
{ {
if (!m_data)
loadData();
if (m_bitPosition > 0) if (m_bitPosition > 0)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -206,6 +227,9 @@ Uint64 BinaryReader::readUInt64()
float BinaryReader::readFloat() float BinaryReader::readFloat()
{ {
if (!m_data)
loadData();
if (m_bitPosition > 0) if (m_bitPosition > 0)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -224,6 +248,9 @@ float BinaryReader::readFloat()
double BinaryReader::readDouble() double BinaryReader::readDouble()
{ {
if (!m_data)
loadData();
if (m_bitPosition > 0) if (m_bitPosition > 0)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -243,6 +270,9 @@ double BinaryReader::readDouble()
bool BinaryReader::readBool() bool BinaryReader::readBool()
{ {
if (!m_data)
loadData();
if (m_bitPosition > 0) if (m_bitPosition > 0)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -258,6 +288,8 @@ bool BinaryReader::readBool()
std::string BinaryReader::readUnicode() std::string BinaryReader::readUnicode()
{ {
if (!m_data)
loadData();
std::string ret; std::string ret;
std::vector<short> tmp; std::vector<short> tmp;
@ -288,6 +320,56 @@ std::string BinaryReader::readString()
return ret; return ret;
} }
void BinaryReader::setProgressCallback(std::function<void (int)> cb)
{
m_progressCallback = cb;
}
void BinaryReader::loadData()
{
FILE* in;
Uint32 length;
in = fopen(m_filepath.c_str(), "rb");
if (!in)
throw error::FileNotFoundException(m_filepath);
fseek(in, 0, SEEK_END);
length = ftell(in);
fseek(in, 0, SEEK_SET);
#ifdef HW_RVL
m_data = (Uint8*)memalign(32, length);
#else
m_data = new Uint8[length];
#endif
Uint32 done = 0;
Uint32 blocksize = BLOCKSZ;
do
{
if (blocksize > length - done)
blocksize = length - done;
Int32 ret = fread(m_data + done, 1, blocksize, in);
if (ret < 0)
throw error::IOException("BinaryReader::BinaryReader -> reading data from disk");
else if (ret == 0)
break;
done += ret;
if (m_progressCallback)
m_progressCallback((int)((float)(done* 100.f)/length));
} while (done < length);
fclose(in);
m_length = length;
m_position = 0;
m_bitPosition = 0;
}
bool BinaryReader::isOpenForWriting() bool BinaryReader::isOpenForWriting()
{ {
return false; return false;

View File

@ -15,8 +15,8 @@
#include "Compression.hpp" #include "Compression.hpp"
#include "Exception.hpp" #include "Exception.hpp"
#include "lzo.h"
#include <iostream> #include <iostream>
#include <zlib.h> #include <zlib.h>
namespace zelda namespace zelda
@ -103,6 +103,13 @@ Int32 compressZlib(const Uint8 *src, Uint32 srcLen, Uint8 *dst, Uint32 dstLen)
return ret; return ret;
} }
Int32 decompressLZO(Uint8* source, Int32 sourceSize, Uint8* dest, Int32& dstSize)
{
int size = dstSize;
int result = lzo1x_decode(dest, &size, source, &sourceSize);
dstSize = size;
return result;
}
//src points to the yaz0 source data (to the "real" source data, not at the header!) //src points to the yaz0 source data (to the "real" source data, not at the header!)
//dst points to a buffer uncompressedSize bytes large (you get uncompressedSize from //dst points to a buffer uncompressedSize bytes large (you get uncompressedSize from
@ -317,6 +324,8 @@ Uint32 simpleEnc(Uint8* src, Int32 size, Int32 pos, Uint32 *pMatchPos)
return numBytes; return numBytes;
} }
} // Compression } // Compression
} // io } // io
} // zelda } // zelda

View File

@ -24,6 +24,8 @@ SkywardSwordFileReader::SkywardSwordFileReader(const std::string& filename)
SkywardSwordFile* SkywardSwordFileReader::read() SkywardSwordFile* SkywardSwordFileReader::read()
{ {
SkywardSwordFile* file = NULL; SkywardSwordFile* file = NULL;
try
{
if (base::length() != 0xFBE0) if (base::length() != 0xFBE0)
throw zelda::error::InvalidOperationException("SSFileReader::read -> File not the expected size of 0xFBE0"); throw zelda::error::InvalidOperationException("SSFileReader::read -> File not the expected size of 0xFBE0");
@ -51,6 +53,13 @@ SkywardSwordFile* SkywardSwordFileReader::read()
base::seek(pos, base::Beginning); base::seek(pos, base::Beginning);
file->addQuest(q); file->addQuest(q);
} }
}
catch(...)
{
delete file;
file = NULL;
throw;
}
return file; return file;
} }

View File

@ -24,6 +24,9 @@ SpriteFileReader::SpriteFileReader(const std::string& filepath)
Sakura::SpriteFile* SpriteFileReader::readFile() Sakura::SpriteFile* SpriteFileReader::readFile()
{ {
Sakura::SpriteFile* ret = NULL;
try
{
Uint32 magic = base::readUInt32(); Uint32 magic = base::readUInt32();
if (magic != Sakura::SpriteFile::Magic) if (magic != Sakura::SpriteFile::Magic)
@ -49,7 +52,7 @@ Sakura::SpriteFile* SpriteFileReader::readFile()
Uint16 spriteCount = base::readUInt16(); Uint16 spriteCount = base::readUInt16();
// Lets go ahead and create or new container. // Lets go ahead and create or new container.
Sakura::SpriteFile* ret = new Sakura::SpriteFile(width, height, originX, originY); ret = new Sakura::SpriteFile(width, height, originX, originY);
// The next four bytes are reserved to keep the header 32 byte aligned. // The next four bytes are reserved to keep the header 32 byte aligned.
// This isn't necessary for most systems, but it's eventually planned // This isn't necessary for most systems, but it's eventually planned
@ -185,6 +188,13 @@ Sakura::SpriteFile* SpriteFileReader::readFile()
} }
ret->setSprites(sprites); ret->setSprites(sprites);
}
catch(...)
{
delete ret;
ret = NULL;
throw;
}
return ret; return ret;
} }

View File

@ -28,7 +28,7 @@ namespace zelda
namespace io namespace io
{ {
const Uint32 Stream::BLOCKSZ = 512; const Uint32 Stream::BLOCKSZ = (32*1024);
Stream::Stream() : Stream::Stream() :
m_bitPosition(0), m_bitPosition(0),
@ -198,6 +198,19 @@ Int8 Stream::readByte()
return *(Int8*)(m_data + m_position++); return *(Int8*)(m_data + m_position++);
} }
Uint8 Stream::readUByte()
{
if (m_bitPosition > 0)
{
m_bitPosition = 0;
m_position += sizeof(Uint8);
}
if (m_position + 1 > m_length)
throw error::IOException("Stream::readUByte -> Position passed stream bounds");
return *(Uint8*)(m_data + m_position++);
}
Uint8 *Stream::readUBytes(Int64 length) Uint8 *Stream::readUBytes(Int64 length)
{ {
return (Uint8*)readBytes(length); return (Uint8*)readBytes(length);

View File

@ -54,6 +54,8 @@ WiiSaveReader::WiiSaveReader(const std::string& filename)
WiiSave* WiiSaveReader::readSave() WiiSave* WiiSaveReader::readSave()
{ {
WiiSave* ret = new WiiSave; WiiSave* ret = new WiiSave;
try
{
if (length() < 0xF0C0) if (length() < 0xF0C0)
throw error::InvalidOperationException("WiiSaveReader::readSave -> Not a valid WiiSave"); throw error::InvalidOperationException("WiiSaveReader::readSave -> Not a valid WiiSave");
@ -98,6 +100,14 @@ WiiSave* WiiSaveReader::readSave()
ret->setFiles(files); ret->setFiles(files);
readCerts(totalSize); readCerts(totalSize);
}
catch(...)
{
delete ret;
ret = NULL;
throw;
}
return ret; return ret;
} }
@ -154,7 +164,7 @@ WiiBanner* WiiSaveReader::readBanner()
gameId = base::readUInt64(); gameId = base::readUInt64();
bannerSize = base::readUInt32(); bannerSize = base::readUInt32();
permissions = base::readByte(); permissions = base::readByte();
/* unk =*/ base::readByte(); /* unk =*/ base::readByte();
base::seek(0x10); base::seek(0x10);
// skip padding // skip padding
base::seek(2); base::seek(2);

225
src/lzo.c Normal file
View File

@ -0,0 +1,225 @@
/*
* LZO 1x decompression
* Copyright (c) 2006 Reimar Doeffinger
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
//! avoid e.g. MPlayers fast_memcpy, it slows things down here
#undef memcpy
#include <string.h>
#include "lzo.h"
//! define if we may write up to 12 bytes beyond the output buffer
//#define OUTBUF_PADDED 1
//! define if we may read up to 4 bytes beyond the input buffer
//#define INBUF_PADDED 1
typedef struct LZOContext {
Uint8 *in, *in_end;
Uint8 *out_start, *out, *out_end;
int error;
} LZOContext;
/**
* \brief read one byte from input buffer, avoiding overrun
* \return byte read
*/
static int get_byte(LZOContext *c) {
if (c->in < c->in_end)
return *c->in++;
c->error |= LZO_INPUT_DEPLETED;
return 1;
}
/**
* \brief decode a length value in the coding used by lzo
* \param x previous byte value
* \param mask bits used from x
* \return decoded length value
*/
static int get_len(LZOContext *c, int x, int mask) {
int cnt = x & mask;
if (!cnt) {
while (!(x = get_byte(c))) cnt += 255;
cnt += mask + x;
}
return cnt;
}
/**
* \brief copy bytes from input to output buffer with checking
* \param cnt number of bytes to copy, must be > 0
*/
static void copy(LZOContext *c, int cnt) {
register uint8_t *src = c->in;
register uint8_t *dst = c->out;
if (src + cnt > c->in_end) {
cnt = c->in_end - src;
c->error |= LZO_INPUT_DEPLETED;
}
if (dst + cnt > c->out_end) {
cnt = c->out_end - dst;
c->error |= LZO_OUTPUT_FULL;
}
#if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[3] = src[3];
src += 4;
dst += 4;
cnt -= 4;
if (cnt > 0)
#endif
memcpy(dst, src, cnt);
c->in = src + cnt;
c->out = dst + cnt;
}
/**
* \brief copy previously decoded bytes to current position
* \param back how many bytes back we start
* \param cnt number of bytes to copy, must be > 0
*
* cnt > back is valid, this will copy the bytes we just copied,
* thus creating a repeating pattern with a period length of back.
*/
static void copy_backptr(LZOContext *c, int back, int cnt) {
register uint8_t *src = &c->out[-back];
register uint8_t *dst = c->out;
if (src < c->out_start) {
c->error |= LZO_INVALID_BACKPTR;
return;
}
if (dst + cnt > c->out_end) {
cnt = c->out_end - dst;
c->error |= LZO_OUTPUT_FULL;
}
if (back == 1) {
memset(dst, *src, cnt);
dst += cnt;
} else {
#ifdef OUTBUF_PADDED
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[3] = src[3];
src += 4;
dst += 4;
cnt -= 4;
if (cnt > 0) {
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[3] = src[3];
dst[4] = src[4];
dst[5] = src[5];
dst[6] = src[6];
dst[7] = src[7];
src += 8;
dst += 8;
cnt -= 8;
}
#endif
if (cnt > 0) {
int blocklen = back;
while (cnt > blocklen) {
memcpy(dst, src, blocklen);
dst += blocklen;
cnt -= blocklen;
blocklen <<= 1;
}
memcpy(dst, src, cnt);
}
dst += cnt;
}
c->out = dst;
}
/**
* \brief decode LZO 1x compressed data
* \param out output buffer
* \param outlen size of output buffer, number of bytes left are returned here
* \param in input buffer
* \param inlen size of input buffer, number of bytes left are returned here
* \return 0 on success, otherwise error flags, see lzo.h
*
* make sure all buffers are appropriately padded, in must provide
* LZO_INPUT_PADDING, out must provide LZO_OUTPUT_PADDING additional bytes
*/
int lzo1x_decode(Uint8 *out, Int32 *outlen, Uint8 *in, Int32 *inlen) {
enum {COPY, BACKPTR} state = COPY;
Int32 x;
LZOContext c;
c.in = in;
c.in_end = in + *inlen;
c.out = c.out_start = out;
c.out_end = out + * outlen;
c.error = 0;
x = get_byte(&c);
if (x > 17) {
copy(&c, x - 17);
x = get_byte(&c);
if (x < 16) c.error |= LZO_ERROR;
}
while (!c.error) {
int cnt, back;
if (x >> 4) {
if (x >> 6) {
cnt = (x >> 5) - 1;
back = (get_byte(&c) << 3) + ((x >> 2) & 7) + 1;
} else if (x >> 5) {
cnt = get_len(&c, x, 31);
x = get_byte(&c);
back = (get_byte(&c) << 6) + (x >> 2) + 1;
} else {
cnt = get_len(&c, x, 7);
back = (1 << 14) + ((x & 8) << 11);
x = get_byte(&c);
back += (get_byte(&c) << 6) + (x >> 2);
if (back == (1 << 14)) {
if (cnt != 1)
c.error |= LZO_ERROR;
break;
}
}
} else
switch (state) {
case COPY:
cnt = get_len(&c, x, 15);
copy(&c, cnt + 3);
x = get_byte(&c);
if (x >> 4)
continue;
cnt = 1;
back = (1 << 11) + (get_byte(&c) << 2) + (x >> 2) + 1;
break;
case BACKPTR:
cnt = 0;
back = (get_byte(&c) << 2) + (x >> 2) + 1;
break;
}
copy_backptr(&c, back, cnt + 2);
cnt = x & 3;
state = cnt ? BACKPTR : COPY;
if (cnt)
copy(&c, cnt);
x = get_byte(&c);
}
*inlen = c.in_end - c.in;
*outlen = c.out_end - c.out;
return c.error;
}