Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which program creates a C array given any file?

Tags:

arrays

c

linux

unix

I remember seeing in the past a program that would take any file and generate a C array representing that file as output; it would prevent distribution of a separate file in some cases. Which Unix/Linux program does that?

like image 438
0x6adb015 Avatar asked Jul 20 '09 19:07

0x6adb015


2 Answers

xxd -i

like image 169
CB Bailey Avatar answered Sep 21 '22 16:09

CB Bailey


For large files, converting to text and then making the compiler parse it all over again is inefficient and unnecessary. Use objcopy instead:

objcopy -I binary -O elf32-i386 stuff stuff.o 

(Adjust the output architecture as necessary for non-x86 platforms.) Then once you link it into your program, you can access it like so:

extern char _binary_stuff_start[], _binary_stuff_end[]; #define SIZE_OF_STUFF (_binary_stuff_end - _binary_stuff_start)  ...  foo(_binary_stuff_start[i]); 
like image 35
David Avatar answered Sep 24 '22 16:09

David