From 9250cfdf7c6b846b94a2d9790c1b3935035d53c0 Mon Sep 17 00:00:00 2001 From: Jon Feldman Date: Sun, 22 Jan 2017 11:37:56 -0500 Subject: [PATCH] Move common sources to a dedicated directory --- boot/Makefile.am | 2 +- {include => common}/lzss.c | 26 ++++++++++++++++---------- include/lzss.h | 6 ++++++ loader/Makefile | 2 +- loader/source/loader.c | 2 +- 5 files changed, 25 insertions(+), 13 deletions(-) rename {include => common}/lzss.c (70%) create mode 100644 include/lzss.h diff --git a/boot/Makefile.am b/boot/Makefile.am index b8e1e77..6e7ae4e 100644 --- a/boot/Makefile.am +++ b/boot/Makefile.am @@ -25,4 +25,4 @@ corbenik_SOURCES = \ patch/reboot.c patch/svc.c patch/module.c patch/emunand.c \ std/fs.c std/draw.c std/memory.c std/abort.c \ firm/util.c firm/keys.c firm/firmlaunch.c firm/version.c firm/firm.c firm/decryptor.c \ - configback/file-dat.c + configback/file-dat.c ../common/lzss.c diff --git a/include/lzss.c b/common/lzss.c similarity index 70% rename from include/lzss.c rename to common/lzss.c index c2f02de..b0418fa 100644 --- a/include/lzss.c +++ b/common/lzss.c @@ -1,13 +1,21 @@ -#ifndef __LZSS_H -#define __LZSS_H +#include -static int -lzss_decompress(u8 *buffer) + +int +lzss_decompress(uint8_t *buffer) { // This WAS originally a decompilation in @yifan_lu's repo; it was rewritten // for readability following ctrtool's namings. + + // This implementation is particularly interesting in that it's an in-place + // decompressor. + + // LZSS is performed in the reverse direction, + // and the last four bytes are the decompressed size. + // Presumably, Nintendo did as such so decompression can occur in-place without need for temporary buffers. + unsigned int decompSize, v15; - u8 *compressEndOff, *index, *stopIndex; + uint8_t *compressEndOff, *index, *stopIndex; char control; int v9, v13, v14, v16; int ret = 0; @@ -16,8 +24,8 @@ lzss_decompress(u8 *buffer) return 0; // v1=decompressedSize, v2=compressedSize, v3=index, v4=stopIndex - decompSize = *((u32 *)buffer - 2); - compressEndOff = &buffer[*((u32 *)buffer - 1)]; + decompSize = *((uint32_t *)buffer - 2); + compressEndOff = &buffer[*((uint32_t *)buffer - 1)]; index = &buffer[-(decompSize >> 24)]; // FIXME - The integer negation is due // to a compiler optimization. It's // probably okay, but... @@ -32,7 +40,7 @@ lzss_decompress(u8 *buffer) v13 = *(index - 1); v14 = *(index - 2); index -= 2; - v15 = ((v14 | (v13 << 8)) & 0xFFFF0FFF) + 2; + v15 = (((unsigned int)v14 | ((unsigned int)v13 << 8)) & 0xFFFF0FFF) + 2; v16 = v13 + 32; do { ret = compressEndOff[v15]; @@ -52,5 +60,3 @@ lzss_decompress(u8 *buffer) return ret; } - -#endif diff --git a/include/lzss.h b/include/lzss.h new file mode 100644 index 0000000..ab3cbde --- /dev/null +++ b/include/lzss.h @@ -0,0 +1,6 @@ +#ifndef __LZSS_H +#define __LZSS_H + +int lzss_decompress(uint8_t *buffer); + +#endif diff --git a/loader/Makefile b/loader/Makefile index e3dbd85..5db3812 100644 --- a/loader/Makefile +++ b/loader/Makefile @@ -33,7 +33,7 @@ endif #--------------------------------------------------------------------------------- TARGET := loader BUILD := build -SOURCES := source source/patch +SOURCES := source ../common DATA := data INCLUDES := include diff --git a/loader/source/loader.c b/loader/source/loader.c index 1573084..f0fdf34 100644 --- a/loader/source/loader.c +++ b/loader/source/loader.c @@ -1,6 +1,6 @@ #include <3ds.h> #include "patcher.h" -#include +#include #include #include #include -- 2.39.5