Implement payload compression

This commit is contained in:
shchmue 2021-05-12 16:47:06 -06:00
parent a7712b173c
commit 918b00ce07
20 changed files with 2305 additions and 28 deletions

16
tools/bin2c/Makefile Normal file
View file

@ -0,0 +1,16 @@
NATIVE_CC ?= gcc
ifeq (, $(shell which $(NATIVE_CC) 2>/dev/null))
$(error "Native GCC is missing. Please install it first. If it's path is custom, set it with export NATIVE_CC=<path to native gcc toolchain>")
endif
.PHONY: all clean
all: bin2c
@echo > /dev/null
clean:
@rm -f bin2c
bin2c: bin2c.c
@$(NATIVE_CC) -o $@ bin2c.c

83
tools/bin2c/bin2c.c Normal file
View file

@ -0,0 +1,83 @@
/*
* This is bin2c program, which allows you to convert binary file to
* C language array, for use as embedded resource, for instance you can
* embed graphics or audio file directly into your program.
* This is public domain software, use it on your own risk.
* Contact Serge Fukanchik at fuxx@mail.ru if you have any questions.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
/* Replace . with _ */
char*
make_ident ( char* name )
{
char* ret;
char* p;
ret = strdup ( name );
for ( p = ret; p[0]; p++ )
{
if ( !isalnum ( p[0] ) ) p[0] = '_';
}
return ret;
}
int
main ( int argc, char* argv[] )
{
unsigned char buf[BUFSIZ];
char* ident;
FILE *fd;
size_t size, i, total, blksize = BUFSIZ;
int need_comma = 0;
if ( argc != 2 )
{
fprintf ( stderr, "Usage: %s binary_file > output_file\n", argv[0] );
return -1;
}
fd = fopen ( argv[1], "rb" );
if ( fd == NULL )
{
fprintf ( stderr, "%s: can't open %s for reading\n", argv[0], argv[1] );
return -1;
}
fseek(fd, 0, SEEK_END);
size = ftell(fd);
rewind(fd);
ident = make_ident ( argv[1] );
printf ( "static const unsigned char __attribute__((section (\"._%s\"))) %s[] = {", ident, ident );
for ( total = 0; total < size; )
{
if ( size - total < blksize ) blksize = size - total;
if ( fread ( buf, 1, blksize, fd ) != blksize )
{
fprintf ( stderr, "%s: file read error\n", argv[0] );
return -1;
}
for ( i = 0; i < blksize; i++ )
{
if ( need_comma ) printf ( ", " );
else need_comma = 1;
if ( ( total % 11 ) == 0 ) printf ( "\n\t" );
printf ( "0x%.2x", buf[i] );
total++;
}
}
printf ( "\n};\n" );
fclose ( fd );
free ( ident );
return 0;
}