Merge pull request #35 from AxioDL/bintoc-compress

Add bintoc --compress
This commit is contained in:
Phillip Stephens 2020-03-03 16:47:09 -08:00 committed by GitHub
commit 2149af68fa
3 changed files with 126 additions and 34 deletions

View File

@ -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 #

View File

@ -1,40 +1,95 @@
#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");
return 1; 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;
}
char* input = argv[1];
char* output = argv[2];
char* symbol = argv[3];
bool compress = false;
if (strcmp(input, "--compress") == 0) {
if (argc < 5) {
print_usage();
return 1;
} }
FILE* fin = fopen(argv[1], "rb"); input = argv[2];
if (!fin) output = argv[3];
{ symbol = argv[4];
fprintf(stderr, "Unable to open %s for reading\n", argv[1]); compress = true;
return 1; }
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;
}
fprintf(fout, "#include <cstdint>\n#include <cstddef>\n");
fprintf(fout, "extern \"C\" const uint8_t %s[] =\n{\n ", symbol);
size_t totalSz = 0;
size_t readSz;
if (compress) {
size_t compressedSz = 0;
z_stream strm = {.zalloc = Z_NULL, .zfree = Z_NULL, .opaque = Z_NULL};
int ret = deflateInit2(&strm, Z_BEST_COMPRESSION, Z_DEFLATED, MAX_WBITS | 16, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
if (ret != Z_OK) {
fprintf(stderr, "zlib initialization failed %d\n", ret);
return 1;
} }
FILE* fout = fopen(argv[2], "wb"); while ((strm.avail_in = fread(buf, 1, sizeof(buf), fin))) {
if (!fout) totalSz += strm.avail_in;
{ strm.next_in = buf;
fprintf(stderr, "Unable to open %s for writing\n", argv[2]); int eof = feof(fin);
return 1; 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 ");
}
compressedSz += sz;
}
} while (strm.avail_out == 0 || (eof && (ret == Z_OK || ret == Z_BUF_ERROR)));
} }
fprintf(fout, "#include <cstdint>\n#include <cstddef>\n"); deflateEnd(&strm);
fprintf(fout, "extern \"C\" const uint8_t %s[] =\n{\n", argv[3]); fprintf(fout, "0x00};\nextern \"C\" const size_t %s_SZ = %zu;\n", symbol, compressedSz);
size_t totalSz = 0; fprintf(fout, "extern \"C\" const size_t %s_DECOMPRESSED_SZ = %zu;\n", symbol, totalSz);
size_t readSz; } else {
uint8_t buf[32]; while ((readSz = fread(buf, 1, sizeof(buf), fin))) {
while ((readSz = fread(buf, 1, 32, fin))) for (int b = 0; b < readSz; ++b) {
{ fprintf(fout, "0x%02X, ", buf[b]);
fprintf(fout, " "); if ((totalSz + b + 1) % LINE_BREAK == 0)
totalSz += readSz; fprintf(fout, "\n ");
for (int b=0 ; b<readSz ; ++b) }
fprintf(fout, "0x%02X, ", buf[b]); totalSz += readSz;
fprintf(fout, "\n");
} }
fprintf(fout, "0x0};\nextern \"C\" const size_t %s_SZ = %zu;\n", argv[3], totalSz); fprintf(fout, "0x0};\nextern \"C\" const size_t %s_SZ = %zu;\n", symbol, totalSz);
fclose(fin); }
fclose(fout); fclose(fin);
return 0; fclose(fout);
return 0;
} }

View File

@ -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()