mirror of https://github.com/AxioDL/metaforce.git
Merge pull request #35 from AxioDL/bintoc-compress
Add bintoc --compress
This commit is contained in:
commit
2149af68fa
|
@ -1,5 +1,7 @@
|
||||||
if(NOT CMAKE_CROSSCOMPILING)
|
if(NOT CMAKE_CROSSCOMPILING)
|
||||||
|
add_subdirectory(../extern/athena/extern/zlib zlib)
|
||||||
add_executable(bintoc bintoc.c)
|
add_executable(bintoc bintoc.c)
|
||||||
|
target_link_libraries(bintoc ${ZLIB_LIBRARIES})
|
||||||
function(bintoc out in sym)
|
function(bintoc out in sym)
|
||||||
if(IS_ABSOLUTE ${out})
|
if(IS_ABSOLUTE ${out})
|
||||||
set(theOut ${out})
|
set(theOut ${out})
|
||||||
|
@ -17,6 +19,23 @@ function(bintoc out in sym)
|
||||||
COMMAND $<TARGET_FILE:bintoc> ARGS ${theIn} ${theOut} ${sym}
|
COMMAND $<TARGET_FILE:bintoc> ARGS ${theIn} ${theOut} ${sym}
|
||||||
DEPENDS ${theIn} bintoc)
|
DEPENDS ${theIn} bintoc)
|
||||||
endfunction()
|
endfunction()
|
||||||
|
function(bintoc_compress out in sym)
|
||||||
|
if(IS_ABSOLUTE ${out})
|
||||||
|
set(theOut ${out})
|
||||||
|
else()
|
||||||
|
set(theOut ${CMAKE_CURRENT_BINARY_DIR}/${out})
|
||||||
|
endif()
|
||||||
|
if(IS_ABSOLUTE ${in})
|
||||||
|
set(theIn ${in})
|
||||||
|
else()
|
||||||
|
set(theIn ${CMAKE_CURRENT_SOURCE_DIR}/${in})
|
||||||
|
endif()
|
||||||
|
get_filename_component(outDir ${theOut} DIRECTORY)
|
||||||
|
file(MAKE_DIRECTORY ${outDir})
|
||||||
|
add_custom_command(OUTPUT ${theOut}
|
||||||
|
COMMAND $<TARGET_FILE:bintoc> ARGS --compress ${theIn} ${theOut} ${sym}
|
||||||
|
DEPENDS ${theIn} bintoc)
|
||||||
|
endfunction()
|
||||||
|
|
||||||
##################
|
##################
|
||||||
# Package Export #
|
# Package Export #
|
||||||
|
|
|
@ -1,39 +1,94 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <zlib.h>
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
#define CHUNK 16384
|
||||||
{
|
#define LINE_BREAK 32
|
||||||
if (argc < 4)
|
static uint8_t buf[CHUNK];
|
||||||
{
|
static uint8_t zbuf[CHUNK];
|
||||||
fprintf(stderr, "Usage: bintoc <in> <out> <symbol>\n");
|
|
||||||
|
void print_usage() { fprintf(stderr, "Usage: bintoc [--compress] <in> <out> <symbol>\n"); }
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
if (argc < 4) {
|
||||||
|
print_usage();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
FILE* fin = fopen(argv[1], "rb");
|
char* input = argv[1];
|
||||||
if (!fin)
|
char* output = argv[2];
|
||||||
{
|
char* symbol = argv[3];
|
||||||
fprintf(stderr, "Unable to open %s for reading\n", argv[1]);
|
bool compress = false;
|
||||||
|
if (strcmp(input, "--compress") == 0) {
|
||||||
|
if (argc < 5) {
|
||||||
|
print_usage();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
FILE* fout = fopen(argv[2], "wb");
|
input = argv[2];
|
||||||
if (!fout)
|
output = argv[3];
|
||||||
{
|
symbol = argv[4];
|
||||||
fprintf(stderr, "Unable to open %s for writing\n", argv[2]);
|
compress = true;
|
||||||
|
}
|
||||||
|
FILE* fin = fopen(input, "rb");
|
||||||
|
if (!fin) {
|
||||||
|
fprintf(stderr, "Unable to open %s for reading\n", input);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
FILE* fout = fopen(output, "wb");
|
||||||
|
if (!fout) {
|
||||||
|
fprintf(stderr, "Unable to open %s for writing\n", output);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
fprintf(fout, "#include <cstdint>\n#include <cstddef>\n");
|
fprintf(fout, "#include <cstdint>\n#include <cstddef>\n");
|
||||||
fprintf(fout, "extern \"C\" const uint8_t %s[] =\n{\n", argv[3]);
|
fprintf(fout, "extern \"C\" const uint8_t %s[] =\n{\n ", symbol);
|
||||||
size_t totalSz = 0;
|
size_t totalSz = 0;
|
||||||
size_t readSz;
|
size_t readSz;
|
||||||
uint8_t buf[32];
|
if (compress) {
|
||||||
while ((readSz = fread(buf, 1, 32, fin)))
|
size_t compressedSz = 0;
|
||||||
{
|
z_stream strm = {.zalloc = Z_NULL, .zfree = Z_NULL, .opaque = Z_NULL};
|
||||||
fprintf(fout, " ");
|
int ret = deflateInit2(&strm, Z_BEST_COMPRESSION, Z_DEFLATED, MAX_WBITS | 16, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
|
||||||
totalSz += readSz;
|
if (ret != Z_OK) {
|
||||||
for (int b=0 ; b<readSz ; ++b)
|
fprintf(stderr, "zlib initialization failed %d\n", ret);
|
||||||
fprintf(fout, "0x%02X, ", buf[b]);
|
return 1;
|
||||||
|
}
|
||||||
|
while ((strm.avail_in = fread(buf, 1, sizeof(buf), fin))) {
|
||||||
|
totalSz += strm.avail_in;
|
||||||
|
strm.next_in = buf;
|
||||||
|
int eof = feof(fin);
|
||||||
|
do {
|
||||||
|
strm.next_out = zbuf;
|
||||||
|
strm.avail_out = sizeof(zbuf);
|
||||||
|
ret = deflate(&strm, eof ? Z_FINISH : Z_NO_FLUSH);
|
||||||
|
if (ret == Z_STREAM_ERROR) {
|
||||||
|
fprintf(stderr, "zlib compression failed %d\n", ret);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
size_t sz = sizeof(zbuf) - strm.avail_out;
|
||||||
|
if (sz > 0) {
|
||||||
|
for (int b = 0; b < sz; ++b) {
|
||||||
|
fprintf(fout, "0x%02X, ", zbuf[b]);
|
||||||
|
if ((compressedSz + b + 1) % LINE_BREAK == 0)
|
||||||
fprintf(fout, "\n ");
|
fprintf(fout, "\n ");
|
||||||
}
|
}
|
||||||
fprintf(fout, "0x0};\nextern \"C\" const size_t %s_SZ = %zu;\n", argv[3], totalSz);
|
compressedSz += sz;
|
||||||
|
}
|
||||||
|
} while (strm.avail_out == 0 || (eof && (ret == Z_OK || ret == Z_BUF_ERROR)));
|
||||||
|
}
|
||||||
|
deflateEnd(&strm);
|
||||||
|
fprintf(fout, "0x00};\nextern \"C\" const size_t %s_SZ = %zu;\n", symbol, compressedSz);
|
||||||
|
fprintf(fout, "extern \"C\" const size_t %s_DECOMPRESSED_SZ = %zu;\n", symbol, totalSz);
|
||||||
|
} else {
|
||||||
|
while ((readSz = fread(buf, 1, sizeof(buf), fin))) {
|
||||||
|
for (int b = 0; b < readSz; ++b) {
|
||||||
|
fprintf(fout, "0x%02X, ", buf[b]);
|
||||||
|
if ((totalSz + b + 1) % LINE_BREAK == 0)
|
||||||
|
fprintf(fout, "\n ");
|
||||||
|
}
|
||||||
|
totalSz += readSz;
|
||||||
|
}
|
||||||
|
fprintf(fout, "0x0};\nextern \"C\" const size_t %s_SZ = %zu;\n", symbol, totalSz);
|
||||||
|
}
|
||||||
fclose(fin);
|
fclose(fin);
|
||||||
fclose(fout);
|
fclose(fout);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -25,3 +25,21 @@ function(bintoc out in sym)
|
||||||
COMMAND $<TARGET_FILE:bintoc> ARGS ${theIn} ${theOut} ${sym}
|
COMMAND $<TARGET_FILE:bintoc> ARGS ${theIn} ${theOut} ${sym}
|
||||||
DEPENDS ${theIn})
|
DEPENDS ${theIn})
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
|
function(bintoc_compress out in sym)
|
||||||
|
if(IS_ABSOLUTE ${out})
|
||||||
|
set(theOut ${out})
|
||||||
|
else()
|
||||||
|
set(theOut ${CMAKE_CURRENT_BINARY_DIR}/${out})
|
||||||
|
endif()
|
||||||
|
if(IS_ABSOLUTE ${in})
|
||||||
|
set(theIn ${in})
|
||||||
|
else()
|
||||||
|
set(theIn ${CMAKE_CURRENT_SOURCE_DIR}/${in})
|
||||||
|
endif()
|
||||||
|
get_filename_component(outDir ${theOut} DIRECTORY)
|
||||||
|
file(MAKE_DIRECTORY ${outDir})
|
||||||
|
add_custom_command(OUTPUT ${theOut}
|
||||||
|
COMMAND $<TARGET_FILE:bintoc> ARGS --compress ${theIn} ${theOut} ${sym}
|
||||||
|
DEPENDS ${theIn})
|
||||||
|
endfunction()
|
||||||
|
|
Loading…
Reference in New Issue