I needed this last week! Building an embedded firmware image for a dashboard display, with lots of PNG files for icons etc. 61 of them. The original developer wrote a tool to expand the PNGs to BMPs (arrays of 32-bit pixel values) and generate a C array definition as a text file. Which is lots bigger than the original PNG (13K => 100K sort of thing). Then included that C source in the build. Used up 700K of my firmwa…
Embedding Binary Objects in C
81–90 of 141 posts
Re: Embedding Binary Objects in C
#82My favorite is simply using a tool to create a C file with your binary data: static uint8_t mydata[] = {0xDE, 0xAD, 0xBE, 0xEF, ... }; The advantage is that it works anywhere, with any compiler. The disadvantage is that it can increase compile time. I would limit each .c file to 10MB; it seems there's a quadratic increase in build time with file size, at least with gcc. Also, instead of "0x%02x" I use decimal notatio…
For those wondering, the easiest way to do this is with: xxd -i filename.bin
Re: Embedding Binary Objects in C
#83I needed this last week! Building an embedded firmware image for a dashboard display, with lots of PNG files for icons etc. 61 of them. The original developer wrote a tool to expand the PNGs to BMPs (arrays of 32-bit pixel values) and generate a C array definition as a text file. Which is lots bigger than the original PNG (13K => 100K sort of thing). Then included that C source in the build. Used up 700K of my firmwa…
There are even smaller png libs than libpng, try a stripped down stb_image for example. Wouldn't use that for user-supplied images, but since you control them all it should be fine.
Re: Embedding Binary Objects in C
#84My favorite is simply using a tool to create a C file with your binary data: static uint8_t mydata[] = {0xDE, 0xAD, 0xBE, 0xEF, ... }; The advantage is that it works anywhere, with any compiler. The disadvantage is that it can increase compile time. I would limit each .c file to 10MB; it seems there's a quadratic increase in build time with file size, at least with gcc. Also, instead of "0x%02x" I use decimal notatio…
For those wondering, the easiest way to do this is with: xxd -i filename.bin
Re: Embedding Binary Objects in C
#85In Rust: `let foo = include_bytes!("path/to/file")`. And people wonder why I think C is difficult.
Re: Embedding Binary Objects in C
#86Is it possible to automate this with cmake?
Re: Embedding Binary Objects in C
#87One of the Capture the Flag challenges at CCC last year was to figure out a way to leak the contents of a file on a remote compiler server. The server would accept some C code and just give you a boolean true/false value whether the code compiled or not---without ever executing it. Others came up with a solution abusing the C preprocessor by defining macros that would make the known structure of the file valid C and…
"""
_Pragma("clang diagnostic push")
_Pragma("clang diagnostic ignored \\"-Wtrigraphs\\"")
??= define STRINGIZE(...) ??=__VA_ARGS__
??= define EXPAND_AND_STRINGIZE(...) STRINGIZE(__VA_ARGS__)
??= define hxp EXPAND_AND_STRINGIZE(
??= define BUILD_BUG_ON_ZERO(e) (sizeof(struct ))
??= define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
const char flag =
??= include "flag"
)[{}];
BUILD_BUG_ON(flag == {});
_Pragma("clang diagnostic pop")
"""
(The {}, of course, being Python format parameters from our script.)Re: Embedding Binary Objects in C
#88Earlier quoted context omitted.
Can you elaborate on this? Does "unrelated" have any special meaning in this context? Why is this operation undefined?
In C, accessing data outside the bounds of an object is undefined (like, you can’t legally “go past the end” of an array and end up in some other object). The “start” and “end” pointers, as far as the compiler is concerned, are totally different objects, so it may optimize a loop from one pointer to the other out since it’s impossible to increment an address so it’ll go from pounding at one thing to another.
Re: Embedding Binary Objects in C
#89If we're in the realm of "non-standard linker tricks"... Compilers will concatenate sections of the same name. You can use this trick to produce a concatenation of arrays across several files: $ cat t1.c __attribute__((section("some_array"))) int a[] = {1, 2, 3}; $ cat t2.c __attribute__((section("some_array"))) int b[] = {4, 5, 6}; $ cat t.c #include extern const int __start_some_array; extern const int __stop_some_…
Can you give an example of a use case where this is needed?
Re: Embedding Binary Objects in C
#90According to https://news.ycombinator.com/item?id=22865842 #embed proposal is in the review process http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2499.pdf . This similar (same?) proposal is also presented and in the review at WG21 to be included in C++ standard. The author puts a lot of work towards making this proposal a reality (as far as I as a casual twitter/slack observer can see) and I'm looking forward to i…