├── LICENSE ├── README.md └── hexembed.c /LICENSE: -------------------------------------------------------------------------------- 1 | zlib License 2 | 3 | Copyright (C) 2010-2018 Lewis Van Winkle 4 | 5 | This software is provided 'as-is', without any express or implied 6 | warranty. In no event will the authors be held liable for any damages 7 | arising from the use of this software. 8 | 9 | Permission is granted to anyone to use this software for any purpose, 10 | including commercial applications, and to alter it and redistribute it 11 | freely, subject to the following restrictions: 12 | 13 | 1. The origin of this software must not be misrepresented; you must not 14 | claim that you wrote the original software. If you use this software 15 | in a product, an acknowledgement in the product documentation would be 16 | appreciated but is not required. 17 | 2. Altered source versions must be plainly marked as such, and must not be 18 | misrepresented as being the original software. 19 | 3. This notice may not be removed or altered from any source distribution. 20 | 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # hexembed 3 | 4 | hexembed is a very small utility to help embed files in C or C++ programs in an easy, cross-platform way. 5 | 6 | ## Usage 7 | 8 | ``` 9 | > gcc hexembed.c -o hexembed 10 | > hexembed some_file.jpg > some_file.c 11 | > cat some_file.c 12 | 13 | /* Embedded file: some_file.jpg */ 14 | const int fsize = 1873; 15 | const unsigned char *file = { 16 | 0x2f,0x2a,0x0a,0x20,0x2a,0x20,0x68,0x65,0x78,0x65,0x6d,0x62,0x65,0x64,0x20,0x2d, 17 | 0x20,0x61,0x20,0x73,0x69,0x6d,0x70,0x6c,0x65,0x20,0x75,0x74,0x69,0x6c,0x69,0x74, 18 | 0x79,0x20,0x74,0x6f,0x20,0x68,0x65,0x6c,0x70,0x20,0x65,0x6d,0x62,0x65,0x64,0x20, 19 | 0x66,0x69,0x6c,0x65,0x73,0x20,0x69,0x6e,0x20,0x43,0x20,0x70,0x72,0x6f,0x67,0x72, 20 | ... 21 | }; 22 | ``` 23 | 24 | Now you can simply `#include "some_file.c"` file in your program, and you have access to that file's data. 25 | 26 | 27 | You can find more info and alternative methods here: https://codeplea.com/embedding-files-in-c-programs 28 | -------------------------------------------------------------------------------- /hexembed.c: -------------------------------------------------------------------------------- 1 | /* 2 | * hexembed - a simple utility to help embed files in C programs 3 | * 4 | * Copyright (c) 2010-2018 Lewis Van Winkle 5 | * 6 | * http://CodePlea.com 7 | * 8 | * This software is provided 'as-is', without any express or implied 9 | * warranty. In no event will the authors be held liable for any damages 10 | * arising from the use of this software. 11 | * 12 | * Permission is granted to anyone to use this software for any purpose, 13 | * including commercial applications, and to alter it and redistribute it 14 | * freely, subject to the following restrictions: 15 | * 16 | * 1. The origin of this software must not be misrepresented; you must not 17 | * claim that you wrote the original software. If you use this software 18 | * in a product, an acknowledgement in the product documentation would be 19 | * appreciated but is not required. 20 | * 2. Altered source versions must be plainly marked as such, and must not be 21 | * misrepresented as being the original software. 22 | * 3. This notice may not be removed or altered from any source distribution. 23 | * 24 | */ 25 | 26 | 27 | #include 28 | #include 29 | 30 | int main(int argc, char *argv[]) { 31 | if (argc < 2) {printf("Usage:\n\thexembed \n"); return 1;} 32 | 33 | const char *fname = argv[1]; 34 | FILE *fp = fopen(fname, "rb"); 35 | if (!fp) { 36 | fprintf(stderr, "Error opening file: %s.\n", fname); 37 | return 1; 38 | } 39 | 40 | fseek(fp, 0, SEEK_END); 41 | const int fsize = ftell(fp); 42 | 43 | fseek(fp, 0, SEEK_SET); 44 | unsigned char *b = malloc(fsize); 45 | 46 | fread(b, fsize, 1, fp); 47 | fclose(fp); 48 | 49 | 50 | printf("/* Embedded file: %s */\n", fname); 51 | printf("const int fsize = %d;\n", fsize); 52 | printf("const unsigned char *file = {\n"); 53 | 54 | int i; 55 | for (i = 0; i < fsize; ++i) { 56 | printf("0x%02x%s", 57 | b[i], 58 | i == fsize-1 ? "" : ((i+1) % 16 == 0 ? ",\n" : ",")); 59 | } 60 | 61 | printf("\n};\n"); 62 | 63 | free(b); 64 | return 0; 65 | } 66 | --------------------------------------------------------------------------------