Move string compression to Python

... so that the script can know its uncompressed size and calculate the
appropriate buffer size.
This commit is contained in:
Alvin Wong
2021-04-16 17:55:30 +08:00
parent 90426b2b22
commit 6f4f4d9733
4 changed files with 111 additions and 68 deletions

View File

@@ -1,48 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include "lzfx.h"
/* Program to demonstrate file compression. Don't use it in the real world! */
int main(int argc, char **argv) {
if (argc == 3) {
/* open input */
FILE *f = fopen(argv[1], "rb");
if (!f) {
printf("Error: %s\n", argv[1]);
return 1;
}
/* get size */
fseek(f, 0, SEEK_END);
int inputsize = ftell(f);
fseek(f, 0, SEEK_SET);
/* read */
char *input = malloc(inputsize);
fread(input, inputsize, 1, f);
fclose(f);
/* compress */
int outputsize = inputsize + 1; /* buffer overflow */
char *output = malloc(outputsize);
lzfx_compress(input, inputsize, output, &outputsize);
/* open output */
f = fopen(argv[2], "wb");
if (!f) {
printf("Error: %s\n", argv[1]);
return 1;
}
/* write */
fwrite(output, outputsize, 1, f);
fclose(f);
return 0;
} else {
printf("Compresses a file.\n\nUsage: lzfx-raw input output\n");
return 1;
}
}

View File

@@ -364,10 +364,10 @@ $(OUTPUT_DIR)/Core/Gen/translation.files/%.o: Core/Gen/Translation.%.cpp
@echo Generating $@
@$(CPP) -c $(filter-out -flto -g3,$(CXXFLAGS)) $< -o $@
$(HOST_OUTPUT_DIR)/lzfx/lzfx-host-compress: Core/lzfx/lzfx-host-compress.c Core/lzfx/lzfx.c
$(HOST_OUTPUT_DIR)/lzfx/liblzfx.so: Core/lzfx/lzfx.c
@test -d $(@D) || mkdir -p $(@D)
@echo Building host lzfx tool $@
@$(HOST_CC) -Wno-unused-result -O $^ -o $@
@echo Building host lzfx shared library $@
@$(HOST_CC) -Wno-unused-result -fPIC -shared -O $^ -o $@
$(OUTPUT_DIR)/Core/Gen/translation.files/%.strings.bin: $(OUTPUT_DIR)/Core/Gen/translation.files/%.o
@echo Dumping translation strings data from $<
@@ -378,18 +378,13 @@ $(OUTPUT_DIR)/Core/Gen/translation.files/%.strings.bin: $(OUTPUT_DIR)/Core/Gen/t
@test -s $(@D)/$*.data.TranslationStrings.bin || (rm $(@D)/$*.data.TranslationStrings.bin; echo 'ERROR: Output for .rodata._ZL22TranslationStringsData is empty!' >&2; false)
@cat $(@D)/$*.data.TranslationIndices.bin $(@D)/$*.data.TranslationStrings.bin > $@
$(OUTPUT_DIR)/Core/Gen/translation.files/%.strings.lzfx: $(OUTPUT_DIR)/Core/Gen/translation.files/%.strings.bin $(HOST_OUTPUT_DIR)/lzfx/lzfx-host-compress
@echo Compressing translation strings data for $*
@$(HOST_OUTPUT_DIR)/lzfx/lzfx-host-compress $< $@
@echo Compressed from $$(stat --printf="%s" $<) to $$(stat --printf="%s" $@) bytes
Core/Gen/Translation_lzfx.%.cpp: $(OUTPUT_DIR)/Core/Gen/translation.files/%.strings.lzfx $(OUTPUT_DIR)/Core/Gen/translation.files/%.pickle
Core/Gen/Translation_lzfx.%.cpp: $(OUTPUT_DIR)/Core/Gen/translation.files/%.strings.bin $(OUTPUT_DIR)/Core/Gen/translation.files/%.pickle $(HOST_OUTPUT_DIR)/lzfx/liblzfx.so
@test -d $(@D) || mkdir -p $(@D)
@echo Generating lzfx compressed translation for $*
@python3 ../Translations/make_translation.py \
-o $(PWD)/Core/Gen/Translation_lzfx.$*.cpp \
--input-pickled $(OUTPUT_DIR)/Core/Gen/translation.files/$*.pickle \
--lzfx-strings $(OUTPUT_DIR)/Core/Gen/translation.files/$*.strings.lzfx \
--strings-bin $(OUTPUT_DIR)/Core/Gen/translation.files/$*.strings.bin \
$*