From ae7684e6be29173059a953e020be6c4b7aa69312 Mon Sep 17 00:00:00 2001 From: chaoskagami Date: Mon, 23 May 2016 16:12:21 -0400 Subject: [PATCH] Rename the 0282builder ID and split out lzss --- external/loader/loader.rsf | 6 ++-- external/loader/source/loader.c | 59 +-------------------------------- external/loader/source/lzss.c | 55 ++++++++++++++++++++++++++++++ external/loader/source/lzss.h | 3 ++ 4 files changed, 63 insertions(+), 60 deletions(-) create mode 100644 external/loader/source/lzss.c create mode 100644 external/loader/source/lzss.h diff --git a/external/loader/loader.rsf b/external/loader/loader.rsf index dfeeb0e..338bac9 100644 --- a/external/loader/loader.rsf +++ b/external/loader/loader.rsf @@ -1,7 +1,9 @@ BasicInfo: Title : loader CompanyCode : "00" - ProductCode : 0828builder + ProductCode : injectload +# The system doesn't actually care about this, 0282builder was +# likely an artifact of their toolchain. We change it just because. ContentType : Application Logo : None @@ -112,4 +114,4 @@ AccessControlInfo: SystemControlInfo: SaveDataSize: 0KB # It doesn't use any save data. RemasterVersion: 0 - StackSize: 0x1000 \ No newline at end of file + StackSize: 0x1000 diff --git a/external/loader/source/loader.c b/external/loader/source/loader.c index 6fc32d2..3bb93b6 100644 --- a/external/loader/source/loader.c +++ b/external/loader/source/loader.c @@ -6,6 +6,7 @@ #include "fsreg.h" #include "pxipm.h" #include "srvsys.h" +#include "lzss.h" #include "internal.h" // TODO - a lot of this is unecessarily verbose and shitty. Clean it up to be tidy. @@ -31,64 +32,6 @@ static u64 g_cached_prog_handle; static exheader_header g_exheader; static char g_ret_buf[1024]; -// TODO - This doesn't belong in loader.c - It's a decompression function, stuff it in its own file. -// Not to mention, it is nigh unreadable. Replace it with a more readable version of LZSS. -// This decompresses in-place. - -// end should be 'buffer' -static int lzss_decompress(u8 *buffer) -{ - // This WAS originally a decompilation in @yifan_lu's repo; it was rewritten for readability following ctrtool's namings. - unsigned int decompSize, v15; - u8 *compressEndOff, *index, *stopIndex; - char control; - int v9, v13, v14, v16; - int ret = 0; - - if ( !buffer ) // Return immediately when buffer is invalid. - return 0; - - // v1=decompressedSize, v2=compressedSize, v3=index, v4=stopIndex - decompSize = *((u32 *)buffer - 2); - compressEndOff = &buffer[*((u32 *)buffer - 1)]; - index = &buffer[-(decompSize >> 24)]; // FIXME - This is not correct code. It's optimized, but incorrect here. Fix it. - stopIndex = &buffer[-(decompSize & 0xFFFFFF)]; - - while ( index > stopIndex ) // index > stopIndex - { - control = *(index-- - 1); // control (just scoping though) - for (int i=0; i<8; i++) - { - if ( control & 0x80 ) // control & 0x80 - { - v13 = *(index - 1); - v14 = *(index - 2); - index -= 2; - v15 = ((v14 | (v13 << 8)) & 0xFFFF0FFF) + 2; - v16 = v13 + 32; - do - { - ret = compressEndOff[v15]; - *(compressEndOff-- - 1) = ret; - v16 -= 16; - } - while ( !(v16 < 0) ); - } - else - { - v9 = *(index-- - 1); - ret = v9; - *(compressEndOff-- - 1) = v9; - } - control *= 2; - if ( index <= stopIndex ) - return ret; - } - } - - return ret; -} - static Result allocate_shared_mem(prog_addrs_t *shared, prog_addrs_t *vaddr, int flags) { // Somehow, we need to allow reallocating. diff --git a/external/loader/source/lzss.c b/external/loader/source/lzss.c new file mode 100644 index 0000000..1b1bc0a --- /dev/null +++ b/external/loader/source/lzss.c @@ -0,0 +1,55 @@ +#include <3ds.h> + +int lzss_decompress(u8 *buffer) +{ + // This WAS originally a decompilation in @yifan_lu's repo; it was rewritten for readability following ctrtool's namings. + // You can thank me for making it more readable if you'd like; I don't really care. Did it for myself. + unsigned int decompSize, v15; + u8 *compressEndOff, *index, *stopIndex; + char control; + int v9, v13, v14, v16; + int ret = 0; + + if ( !buffer ) // Return immediately when buffer is invalid. + return 0; + + // v1=decompressedSize, v2=compressedSize, v3=index, v4=stopIndex + decompSize = *((u32 *)buffer - 2); + compressEndOff = &buffer[*((u32 *)buffer - 1)]; + index = &buffer[-(decompSize >> 24)]; // FIXME - The integer negation is due to a compiler optimization. It's probably okay, but... + stopIndex = &buffer[-(decompSize & 0xFFFFFF)]; + + while ( index > stopIndex ) // index > stopIndex + { + control = *(index-- - 1); // control (just scoping though) + for (int i=0; i<8; i++) + { + if ( control & 0x80 ) // control & 0x80 + { + v13 = *(index - 1); + v14 = *(index - 2); + index -= 2; + v15 = ((v14 | (v13 << 8)) & 0xFFFF0FFF) + 2; + v16 = v13 + 32; + do + { + ret = compressEndOff[v15]; + *(compressEndOff-- - 1) = ret; + v16 -= 16; + } + while ( !(v16 < 0) ); + } + else + { + v9 = *(index-- - 1); + ret = v9; + *(compressEndOff-- - 1) = v9; + } + control *= 2; + if ( index <= stopIndex ) + return ret; + } + } + + return ret; +} diff --git a/external/loader/source/lzss.h b/external/loader/source/lzss.h new file mode 100644 index 0000000..9e99725 --- /dev/null +++ b/external/loader/source/lzss.h @@ -0,0 +1,3 @@ +#pragma once + +int lzss_decompress(u8 *buffer); -- 2.39.5