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)
add_subdirectory(../extern/athena/extern/zlib zlib)
add_executable(bintoc bintoc.c)
target_link_libraries(bintoc ${ZLIB_LIBRARIES})
function(bintoc out in sym)
if(IS_ABSOLUTE ${out})
set(theOut ${out})
@ -17,6 +19,23 @@ function(bintoc out in sym)
COMMAND $<TARGET_FILE:bintoc> ARGS ${theIn} ${theOut} ${sym}
DEPENDS ${theIn} bintoc)
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 #

View File

@ -1,40 +1,95 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <zlib.h>
int main(int argc, char** argv)
{
if (argc < 4)
{
fprintf(stderr, "Usage: bintoc <in> <out> <symbol>\n");
return 1;
#define CHUNK 16384
#define LINE_BREAK 32
static uint8_t buf[CHUNK];
static uint8_t zbuf[CHUNK];
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");
if (!fin)
{
fprintf(stderr, "Unable to open %s for reading\n", argv[1]);
return 1;
input = argv[2];
output = argv[3];
symbol = argv[4];
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;
}
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");
if (!fout)
{
fprintf(stderr, "Unable to open %s for writing\n", argv[2]);
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 ");
}
compressedSz += sz;
}
} while (strm.avail_out == 0 || (eof && (ret == Z_OK || ret == Z_BUF_ERROR)));
}
fprintf(fout, "#include <cstdint>\n#include <cstddef>\n");
fprintf(fout, "extern \"C\" const uint8_t %s[] =\n{\n", argv[3]);
size_t totalSz = 0;
size_t readSz;
uint8_t buf[32];
while ((readSz = fread(buf, 1, 32, fin)))
{
fprintf(fout, " ");
totalSz += readSz;
for (int b=0 ; b<readSz ; ++b)
fprintf(fout, "0x%02X, ", buf[b]);
fprintf(fout, "\n");
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", argv[3], totalSz);
fclose(fin);
fclose(fout);
return 0;
fprintf(fout, "0x0};\nextern \"C\" const size_t %s_SZ = %zu;\n", symbol, totalSz);
}
fclose(fin);
fclose(fout);
return 0;
}

View File

@ -1,8 +1,8 @@
# - Config file for the bintoc package
# Compute paths
get_filename_component(BINTOC_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
# Our library dependencies (contains definitions for IMPORTED targets)
if(NOT TARGET bintoc AND NOT bintoc_BINARY_DIR)
include("${BINTOC_CMAKE_DIR}/hecl-bintocTargets.cmake")
@ -25,3 +25,21 @@ function(bintoc out in sym)
COMMAND $<TARGET_FILE:bintoc> ARGS ${theIn} ${theOut} ${sym}
DEPENDS ${theIn})
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()