From 163cd9a17b5b55f3bc885933a4b532ab869aac28 Mon Sep 17 00:00:00 2001 From: CrashOveride95 Date: Sun, 3 Jan 2021 15:48:35 -0500 Subject: [PATCH] Append size of uncomp data to end of gzip --- Makefile | 4 +++- src/game/memory.c | 21 ++++++++++++++++++++- tools/.gitignore | 1 + tools/Makefile | 4 +++- tools/bfsize.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 tools/bfsize.c diff --git a/Makefile b/Makefile index fdf24135..6c7fad09 100644 --- a/Makefile +++ b/Makefile @@ -405,6 +405,7 @@ export LANG := C # N64 tools YAY0TOOL := $(TOOLS_DIR)/slienc ROMALIGN := $(TOOLS_DIR)/romalign +BFSIZE := $(TOOLS_DIR)/bfsize N64CKSUM := $(TOOLS_DIR)/n64cksum N64GRAPHICS := $(TOOLS_DIR)/n64graphics N64GRAPHICS_CI := $(TOOLS_DIR)/n64graphics_ci @@ -581,12 +582,13 @@ ifeq ($(COMPRESS),gzip) $(BUILD_DIR)/%.gz: $(BUILD_DIR)/%.bin $(call print,Compressing:,$<,$@) $(V)gzip -c -9 -n $< > $@ + $(V)dd if=/dev/zero bs=1 count=4 >> $@ + perl -e 'printf ("%x", -s "$<")' $< >> $@ # Strip gzip header $(BUILD_DIR)/%.szp: $(BUILD_DIR)/%.gz $(call print,Converting:,$<,$@) $(V)dd bs=10 skip=1 if=$< of=$@ - $(V)$(ROMALIGN) $@ 16 # convert binary szp to object file $(BUILD_DIR)/%.szp.o: $(BUILD_DIR)/%.szp diff --git a/src/game/memory.c b/src/game/memory.c index 82a1f71a..c2d2f33b 100644 --- a/src/game/memory.c +++ b/src/game/memory.c @@ -329,19 +329,33 @@ void *load_to_fixed_pool_addr(u8 *destAddr, u8 *srcStart, u8 *srcEnd) { void *load_segment_decompress(s32 segment, u8 *srcStart, u8 *srcEnd) { void *dest = NULL; +#ifdef GZIP + u32 compSize = (srcEnd - 8 - srcStart); +#else u32 compSize = ALIGN16(srcEnd - srcStart); +#endif + u8 *compressed = main_pool_alloc(compSize, MEMORY_POOL_RIGHT); +#ifdef GZIP + // Decompressed size from end of gzip + u32 *size = (u32 *) (compressed + compSize - 4); +#else // Decompressed size from mio0 header u32 *size = (u32 *) (compressed + 4); +#endif +#ifdef GZIP if (compressed != NULL) { dma_read(compressed, srcStart, srcEnd); dest = main_pool_alloc(*size, MEMORY_POOL_LEFT); if (dest != NULL) { -#ifdef GZIP slidma(compressed, dest, compSize); #else + if (compressed != NULL) { + dma_read(compressed, srcStart, srcEnd); + dest = main_pool_alloc(*size, MEMORY_POOL_LEFT); + if (dest != NULL) { slidstart(compressed, dest); #endif set_segment_base_addr(segment, dest); @@ -355,7 +369,12 @@ void *load_segment_decompress(s32 segment, u8 *srcStart, u8 *srcEnd) { void *load_segment_decompress_heap(u32 segment, u8 *srcStart, u8 *srcEnd) { UNUSED void *dest = NULL; + +#ifdef GZIP + u32 compSize = (srcEnd - 8 - srcStart); +#else u32 compSize = ALIGN16(srcEnd - srcStart); +#endif u8 *compressed = main_pool_alloc(compSize, MEMORY_POOL_RIGHT); UNUSED u32 *pUncSize = (u32 *) (compressed + 4); diff --git a/tools/.gitignore b/tools/.gitignore index 4038282c..f5177800 100644 --- a/tools/.gitignore +++ b/tools/.gitignore @@ -1,6 +1,7 @@ /aifc_decode /aiff_extract_codebook /armips +/bfsize /extract_data_for_mio /mio0 /n64cksum diff --git a/tools/Makefile b/tools/Makefile index 1761c641..d148500b 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -7,7 +7,7 @@ CC := gcc CXX := g++ CFLAGS := -I . -Wall -Wextra -Wno-unused-parameter -pedantic -O2 -s LDFLAGS := -lm -ALL_PROGRAMS := armips n64graphics n64graphics_ci mio0 slienc n64cksum textconv patch_libultra_math aifc_decode aiff_extract_codebook vadpcm_enc tabledesign extract_data_for_mio skyconv +ALL_PROGRAMS := armips bfsize n64graphics n64graphics_ci mio0 slienc n64cksum textconv patch_libultra_math aifc_decode aiff_extract_codebook vadpcm_enc tabledesign extract_data_for_mio skyconv LIBAUDIOFILE := audiofile/libaudiofile.a # Only build armips from tools if it is not found on the system @@ -19,6 +19,8 @@ endif default: all +bfsize_SOURCES := bfsize.c + n64graphics_SOURCES := n64graphics.c utils.c n64graphics_CFLAGS := -DN64GRAPHICS_STANDALONE diff --git a/tools/bfsize.c b/tools/bfsize.c new file mode 100644 index 00000000..7b2e9224 --- /dev/null +++ b/tools/bfsize.c @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2011 + * + * This is free software, licensed under the GNU General Public License v2. + * See /LICENSE for more information. + * + */ + +#include +#include +#include + +static void print_usage(const char *name) +{ + printf("Usage: %s file\n", name); +} + +int main(int argc, char **argv) +{ + FILE *fd; + long size; + + if (argc != 2) + { + print_usage(argv[0]); + exit(1); + } + + fd = fopen(argv[1], "rb"); + if (fd == NULL) + { + printf("Could not read the file '%s'\n", argv[1]); + print_usage(argv[0]); + exit(1); + } + + size = fseek(fd, 0L, SEEK_END); + size = ftell(fd); + fclose(fd); + + printf("%ld\n", size); + + exit(0); +}