-some small compression corrections

This commit is contained in:
fix94.1 2013-01-06 18:33:29 +00:00
parent e44ec9cda1
commit 7cda297245
10 changed files with 230 additions and 255 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -22,7 +22,7 @@ static inline u32 packBytes(int a, int b, int c, int d)
return (d << 24) | (c << 16) | (b << 8) | (a); return (d << 24) | (c << 16) | (b << 8) | (a);
} }
static int __decompressLZ77_11(u8 *in, u32 inputLen, u8 **output, u32 *outputLen) static int __decompressLZ77_11(const u8 *in, u32 inputLen, u8 **output, u32 *outputLen)
{ {
int x = 0; int x = 0;
u32 y = 0; u32 y = 0;
@ -98,7 +98,7 @@ static int __decompressLZ77_11(u8 *in, u32 inputLen, u8 **output, u32 *outputLen
return 0; return 0;
} }
static int __decompressLZ77_10(u8 *in, u8 **output, u32 *outputLen) static int __decompressLZ77_10(const u8 *in, u8 **output, u32 *outputLen)
{ {
int x = 0; int x = 0;
u32 y = 0; u32 y = 0;
@ -148,14 +148,14 @@ static int __decompressLZ77_10(u8 *in, u8 **output, u32 *outputLen)
return 0; return 0;
} }
int isLZ77compressed(u8 *buffer) int isLZ77compressed(const u8 *buffer)
{ {
if((buffer[0] == LZ77_0x10_FLAG) || (buffer[0] == LZ77_0x11_FLAG)) if((buffer[0] == LZ77_0x10_FLAG) || (buffer[0] == LZ77_0x11_FLAG))
return 1; return 1;
return 0; return 0;
} }
int decompressLZ77content(u8 *buffer, u32 length, u8 **output, u32 *outputLen) int decompressLZ77content(const u8 *buffer, u32 length, u8 **output, u32 *outputLen)
{ {
int ret = 0; int ret = 0;
switch(buffer[0]) switch(buffer[0])

View File

@ -23,8 +23,8 @@
extern "C" { extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
int isLZ77compressed(u8 *buffer); int isLZ77compressed(const u8 *buffer);
int decompressLZ77content(u8 *buffer, u32 length, u8 **output, u32 *outputLen); int decompressLZ77content(const u8 *buffer, u32 length, u8 **output, u32 *outputLen);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -49,7 +49,7 @@ protected:
u8 *sysFont2; u8 *sysFont2;
}; };
u8 *DecompressCopy(const u8 *stuff, const u32 len, u32 *size); u8 *DecompressCopy(const u8 *stuff, u32 len, u32 *size);
extern AnimatedBanner gameBanner; extern AnimatedBanner gameBanner;
#endif #endif

View File

@ -21,7 +21,7 @@
#include "gecko/gecko.hpp" #include "gecko/gecko.hpp"
#include "loader/fs.h" #include "loader/fs.h"
#include "loader/sys.h" #include "loader/sys.h"
#include "unzip/lz77.h" #include "banner/AnimatedBanner.h"
#include "unzip/U8Archive.h" #include "unzip/U8Archive.h"
extern const u8 save_bin[]; extern const u8 save_bin[];
@ -65,7 +65,7 @@ bool NandSave::CheckSave()
goto done; goto done;
} }
/* extract our archive */ /* extract our archive */
decompressLZ77content(save_bin, save_bin_size, &u8_bin, &u8_bin_size); u8_bin = DecompressCopy(save_bin, save_bin_size, &u8_bin_size);
if(u8_bin == NULL || u8_bin_size == 0) if(u8_bin == NULL || u8_bin_size == 0)
goto error; goto error;
/* grab cert.sys */ /* grab cert.sys */

View File

@ -7,6 +7,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include "homebrew.h" #include "homebrew.h"
#include "banner/AnimatedBanner.h"
#include "gecko/gecko.hpp" #include "gecko/gecko.hpp"
#define EXECUTE_ADDR ((u8 *)0x91000000) #define EXECUTE_ADDR ((u8 *)0x91000000)
@ -110,9 +111,16 @@ void writeStub()
/* Clear potential homebrew channel stub */ /* Clear potential homebrew channel stub */
memset((void*)0x80001800, 0, 0x1800); memset((void*)0x80001800, 0, 0x1800);
/* Extract our stub */
u32 StubSize = 0;
u8 *Stub = DecompressCopy(stub_bin, stub_bin_size, &StubSize);
/* Copy our own stub into memory */ /* Copy our own stub into memory */
memcpy((void*)0x80001800, stub_bin, stub_bin_size); memcpy((void*)0x80001800, Stub, StubSize);
DCFlushRange((void*)0x80001800, stub_bin_size); DCFlushRange((void*)0x80001800, StubSize);
/* And free the memory again */
free(Stub);
} }
void BootHomebrew() void BootHomebrew()

View File

@ -1,210 +1,177 @@
/******************************************************************************* /*******************************************************************************
* lz77.c * lz77.c
* *
* Copyright (c) 2009 The Lemon Man * Copyright (c) 2009 The Lemon Man
* Copyright (c) 2009 Nicksasa * Copyright (c) 2009 Nicksasa
* Copyright (c) 2009 WiiPower * Copyright (c) 2009 WiiPower
* *
* Distributed under the terms of the GNU General Public License (v2) * Distributed under the terms of the GNU General Public License (v2)
* See http://www.gnu.org/licenses/gpl-2.0.txt for more info. * See http://www.gnu.org/licenses/gpl-2.0.txt for more info.
* *
* Description: * Description:
* ----------- * -----------
* *
******************************************************************************/ ******************************************************************************/
#include <gccore.h> #include <gccore.h>
#include <stdio.h> #include <stdlib.h>
#include <unistd.h> #include "lz77.h"
#include <string.h> #include "memory/mem2.hpp"
#include "lz77.h"
#include "loader/utils.h" static inline u32 packBytes(int a, int b, int c, int d)
#include "memory/mem2.hpp" {
return (d << 24) | (c << 16) | (b << 8) | (a);
u32 packBytes(int a, int b, int c, int d) }
{
return (d << 24) | (c << 16) | (b << 8) | (a); static int __decompressLZ77_11(const u8 *in, u32 inputLen, u8 **output, u32 *outputLen)
} {
int x = 0;
s32 __decompressLZ77_11(const u8 *in, const u32 inputLen, u8 **output, u32 *outputLen) u32 y = 0;
{ u32 compressedPos = 0x4;
int x, y; u32 decompressedPos = 0;
u32 decompressedSize = packBytes(in[0], in[1], in[2], in[3]) >> 8;
u8 *out = NULL; if(decompressedSize == 0)
{
u32 compressedPos = 0x4; decompressedSize = packBytes(in[4], in[5], in[6], in[7]);
u32 decompressedPos = 0x0; compressedPos += 0x4;
u32 decompressedSize = 0; }
//printf("Decompressed size : %i\n", decompressedSize);
decompressedSize = packBytes(in[0], in[1], in[2], in[3]) >> 8;
u8 *out = (u8*)MEM2_alloc(decompressedSize);
if (!decompressedSize) if(out == NULL)
{ {
decompressedSize = packBytes(in[4], in[5], in[6], in[7]); //printf("Out of memory\n");
compressedPos += 0x4; return -1;
} }
//printf("Decompressed size : %i\n", decompressedSize); while(compressedPos < inputLen && decompressedPos < decompressedSize)
{
out = MEM2_memalign(32, decompressedSize); u8 byteFlag = in[compressedPos];
if (out == NULL) compressedPos++;
{
printf("Out of memory\n"); for(x = 7; x >= 0; x--)
return -1; {
} if((byteFlag & (1 << x)) > 0)
{
while (compressedPos < inputLen && decompressedPos < decompressedSize) u8 first = in[compressedPos];
{ u8 second = in[compressedPos + 1];
u8 byteFlag = in[compressedPos];
compressedPos++; u32 pos, copyLen;
if(first < 0x20)
for (x = 7; x >= 0; x--) {
{ u8 third = in[compressedPos + 2];
if ((byteFlag & (1 << x)) > 0) if(first >= 0x10)
{ {
u8 first = in[compressedPos]; u32 fourth = in[compressedPos + 3];
u8 second = in[compressedPos + 1]; pos = (u32)(((third & 0xF) << 8) | fourth) + 1;
copyLen = (u32)((second << 4) | ((first & 0xF) << 12) | (third >> 4)) + 273;
u32 pos, copyLen; compressedPos += 4;
}
if (first < 0x20) else
{ {
u8 third = in[compressedPos + 2]; pos = (u32)(((second & 0xF) << 8) | third) + 1;
copyLen = (u32)(((first & 0xF) << 4) | (second >> 4)) + 17;
if (first >= 0x10) compressedPos += 3;
{ }
u32 fourth = in[compressedPos + 3]; }
else
pos = (u32)(((third & 0xF) << 8) | fourth) + 1; {
copyLen = (u32)((second << 4) | ((first & 0xF) << 12) | (third >> 4)) + 273; pos = (u32)(((first & 0xF) << 8) | second) + 1;
copyLen = (u32)(first >> 4) + 1;
compressedPos += 4; compressedPos += 2;
} else }
{ for(y = 0; y < copyLen; y++)
pos = (u32)(((second & 0xF) << 8) | third) + 1; out[decompressedPos + y] = out[decompressedPos - pos + y];
copyLen = (u32)(((first & 0xF) << 4) | (second >> 4)) + 17; decompressedPos += copyLen;
}
compressedPos += 3; else
} {
} else out[decompressedPos] = in[compressedPos];
{ decompressedPos++;
pos = (u32)(((first & 0xF) << 8) | second) + 1; compressedPos++;
copyLen = (u32)(first >> 4) + 1; }
if(compressedPos >= inputLen || decompressedPos >= decompressedSize)
compressedPos += 2; break;
} }
}
for (y = 0; y < (int)copyLen; y++) *output = out;
{ *outputLen = decompressedSize;
out[decompressedPos + y] = out[decompressedPos - pos + y]; return 0;
} }
decompressedPos += copyLen; static int __decompressLZ77_10(const u8 *in, u8 **output, u32 *outputLen)
} else {
{ int x = 0;
out[decompressedPos] = in[compressedPos]; u32 y = 0;
u32 compressedPos = 0x4;
decompressedPos++; u32 decompressedPos = 0;
compressedPos++; u32 decompressedSize = packBytes(in[0], in[1], in[2], in[3]) >> 8;
} //printf("Decompressed size : %i\n", decompressedSize);
if (compressedPos >= inputLen || decompressedPos >= decompressedSize) u8 *out = (u8*)MEM2_alloc(decompressedSize);
break; if(out == NULL)
} {
} //printf("Out of memory\n");
*output = out; return -1;
*outputLen = decompressedSize; }
return 0;
} while(decompressedPos < decompressedSize)
{
s32 __decompressLZ77_10(const u8 *in, u8 **output, u32 *outputLen) u8 byteFlag = in[compressedPos];
{ compressedPos ++;
int x, y;
u8 *out = NULL; for(x = 0; x < 8; ++x)
{
u32 compressedPos = 0; if(byteFlag & 0x80)
u32 decompressedSize = 0x4; {
u32 decompressedPos = 0; u8 first = in[compressedPos];
u8 second = in[compressedPos + 1];
decompressedSize = packBytes(in[0], in[1], in[2], in[3]) >> 8; u16 pos = (u16)((((first << 8) + second) & 0xFFF) + 1);
u8 copyLen = (u8)(3 + ((first >> 4) & 0xF));
//int compressionType = (packBytes(in[0], in[1], in[2], in[3]) >> 4) & 0xF; for(y = 0; y < copyLen; y++)
out[decompressedPos + y] = out[decompressedPos - pos + (y % pos)];
//printf("Decompressed size : %i\n", decompressedSize); compressedPos += 2;
decompressedPos += copyLen;
out = MEM2_memalign(32, decompressedSize); }
if (out == NULL) else
{ {
printf("Out of memory\n"); out[decompressedPos] = in[compressedPos];
return -1; compressedPos += 1;
} decompressedPos += 1;
}
compressedPos += 0x4; byteFlag <<= 1;
if(decompressedPos >= decompressedSize)
while (decompressedPos < decompressedSize) break;
{ }
u8 flag = *(u8*)(in + compressedPos); }
compressedPos += 1; *output = out;
*outputLen = decompressedSize;
for (x = 0; x < 8; x++) return 0;
{ }
if (flag & 0x80)
{ int isLZ77compressed(const u8 *buffer)
u8 first = in[compressedPos]; {
u8 second = in[compressedPos + 1]; if((buffer[0] == LZ77_0x10_FLAG) || (buffer[0] == LZ77_0x11_FLAG))
return 1;
u16 pos = (u16)((((first << 8) + second) & 0xFFF) + 1); return 0;
u8 copyLen = (u8)(3 + ((first >> 4) & 0xF)); }
for (y = 0; y < copyLen; y++) int decompressLZ77content(const u8 *buffer, u32 length, u8 **output, u32 *outputLen)
{ {
out[decompressedPos + y] = out[decompressedPos - pos + (y % pos)]; int ret = 0;
} switch(buffer[0])
{
compressedPos += 2; case LZ77_0x10_FLAG:
decompressedPos += copyLen; //printf("LZ77 variant 0x10 compressed content...unpacking may take a while...\n");
} else ret = __decompressLZ77_10(buffer, output, outputLen);
{ break;
out[decompressedPos] = in[compressedPos]; case LZ77_0x11_FLAG:
compressedPos += 1; //printf("LZ77 variant 0x11 compressed content...unpacking may take a while...\n");
decompressedPos += 1; ret = __decompressLZ77_11(buffer, length, output, outputLen);
} break;
default:
flag <<= 1; //printf("Not compressed ...\n");
ret = -1;
if (decompressedPos >= decompressedSize) break;
break; }
} return ret;
} }
*output = out;
*outputLen = decompressedSize;
return 0;
}
int isLZ77compressed(const u8 *buffer)
{
if((buffer[0] == LZ77_0x10_FLAG) || (buffer[0] == LZ77_0x11_FLAG))
return 1;
return 0;
}
int decompressLZ77content(const u8 *buffer, const u32 length, u8 **output, u32 *outputLen)
{
int ret;
switch (buffer[0])
{
case LZ77_0x10_FLAG:
//printf("LZ77 variant 0x10 compressed content...unpacking may take a while...\n");
ret = __decompressLZ77_10(buffer, output, outputLen);
break;
case LZ77_0x11_FLAG:
//printf("LZ77 variant 0x11 compressed content...unpacking may take a while...\n");
ret = __decompressLZ77_11(buffer, length, output, outputLen);
break;
default:
//printf("Not compressed ...\n");
ret = -1;
break;
}
return ret;
}

View File

@ -1,34 +1,34 @@
/******************************************************************************* /*******************************************************************************
* lz77.h * lz77.h
* *
* Copyright (c) 2009 The Lemon Man * Copyright (c) 2009 The Lemon Man
* Copyright (c) 2009 Nicksasa * Copyright (c) 2009 Nicksasa
* Copyright (c) 2009 WiiPower * Copyright (c) 2009 WiiPower
* *
* Distributed under the terms of the GNU General Public License (v2) * Distributed under the terms of the GNU General Public License (v2)
* See http://www.gnu.org/licenses/gpl-2.0.txt for more info. * See http://www.gnu.org/licenses/gpl-2.0.txt for more info.
* *
* Description: * Description:
* ----------- * -----------
* *
******************************************************************************/ ******************************************************************************/
#ifndef _LZ77_MODULE #ifndef _LZ77_MODULE
#define _LZ77_MODULE #define _LZ77_MODULE
#define LZ77_0x10_FLAG 0x10 #define LZ77_0x10_FLAG 0x10
#define LZ77_0x11_FLAG 0x11 #define LZ77_0x11_FLAG 0x11
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
int isLZ77compressed(const u8 *buffer); int isLZ77compressed(const u8 *buffer);
int decompressLZ77content(const u8 *buffer, const u32 length, u8 **output, u32 *outputLen); int decompressLZ77content(const u8 *buffer, u32 length, u8 **output, u32 *outputLen);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif /* __cplusplus */ #endif /* __cplusplus */
#endif #endif