* Add LZO support

* Add qmake include file
This commit is contained in:
Antidote 2014-02-11 17:38:50 -08:00
parent 9c64071bc0
commit 5552a24628
5 changed files with 383 additions and 1 deletions

View File

@ -28,6 +28,9 @@ namespace Compression
Int32 decompressZlib(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
Uint32 yaz0Decode(Uint8* src, Uint8* dst, Uint32 uncompressedSize);
Uint32 yaz0Encode(Uint8* src, Uint32 srcSize, Uint8* data);

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

99
libzelda.pri Normal file
View File

@ -0,0 +1,99 @@
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
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

@ -15,8 +15,8 @@
#include "Compression.hpp"
#include "Exception.hpp"
#include "lzo.h"
#include <iostream>
#include <zlib.h>
namespace zelda
@ -103,6 +103,13 @@ Int32 compressZlib(const Uint8 *src, Uint32 srcLen, Uint8 *dst, Uint32 dstLen)
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!)
//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;
}
} // Compression
} // io
} // zelda

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;
}