incbin(): Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
(new page) |
(→See also: add link) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 48: | Line 48: | ||
It should also work for C with some minor changes. |
It should also work for C with some minor changes. |
||
== See also == |
|||
* https://gist.github.com/mmozeiko/ed9655cf50341553d282 |
|||
* https://github.com/graphitemaster/incbin |
|||
* http://smackerelofopinion.blogspot.com/2015/12/incorporating-and-accesses-binary-data.html |
|||
[[Category:Programming]] |
[[Category:Programming]] |
||
[[Category:C]] |
[[Category:C]] |
||
[[Category:C++]] |
[[Category:C++]] |
||
[[Category:C preprocessor snippets]] |
Latest revision as of 11:33, 28 April 2020
To include binary files directly in a C++ program without using an external tool you can use this handy macro (probably only works with the GNU assembler, though):
#include <stdio.h>
struct binfile {
char *data;
size_t size;
binfile(char *data, size_t size):
data(data),
size(size)
{
}
};
#define _CONCAT(x, y) x##y
#define CONCAT(x, y) _CONCAT(x, y)
#define _STRINGIFY(x) #x
#define STRINGIFY(x) _STRINGIFY(x)
#define incbin(filename) \
({ \
extern char CONCAT(incbin_data, __LINE__)[]; \
extern unsigned long CONCAT(incbin_data_size, __LINE__); \
asm (".pushsection .rodata, \"a\" ;" \
".global incbin_data" STRINGIFY(__LINE__) " ;" \
"incbin_data" STRINGIFY(__LINE__) ": ;" \
".incbin " STRINGIFY(filename) " ;" \
"incbin_data_end" STRINGIFY(__LINE__) ": ;" \
".byte 0 ;" \
".global incbin_data_size" STRINGIFY(__LINE__) " ;" \
"incbin_data_size" STRINGIFY(__LINE__) ": ;" \
".quad (incbin_data_end" STRINGIFY(__LINE__) " - incbin_data" STRINGIFY(__LINE__) ") ;" \
".popsection"); \
binfile(CONCAT(incbin_data, __LINE__), \
CONCAT(incbin_data_size, __LINE__)); \
})
int main(int argc, char *argv[])
{
auto v = incbin("/etc/passwd");
fwrite(v.data, 1, v.size, stdout);
return 0;
}
It should also work for C with some minor changes.