2 * Convert a file image to a C define
4 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
6 * SPDX-License-Identifier: GPL-2.0+
8 * For testing EFI disk management we need an in memory image of
11 * The tool file2include converts a file to a C include. The file
12 * is separated into strings of 8 bytes. Only the non-zero strings
13 * are written to the include. The output format has been designed
14 * to maintain readability.
16 * As the disk image needed for testing contains mostly zeroes a high
17 * compression ratio can be attained.
24 /* Size of the blocks written to the compressed file */
27 int main(int argc, char *argv[])
34 /* Provide usage help */
36 printf("Usage:\n%s FILENAME\n", argv[0]);
40 file = fopen(argv[1], "r");
46 ret = fseek(file, 0, SEEK_END);
53 fprintf(stderr, "File %s has length 0\n", argv[1]);
63 count = fread(buf, 1, count, file);
67 printf(" * Non-zero %u byte strings of a disk image\n", BLOCK_SIZE);
69 printf(" * Generated with tools/file2include\n");
71 printf(" * SPDX-License-Identifier: GPL-2.0+\n");
73 printf("#define EFI_ST_DISK_IMG { 0x%08zx, { \\\n", count);
75 for (i = 0; i < count; i += BLOCK_SIZE) {
78 for (j = i; j < i + BLOCK_SIZE && j < count; ++j) {
84 printf("\t{0x%08zx, \"", i);
85 for (j = i; j < i + BLOCK_SIZE && j < count; ++j)
86 printf("\\x%02x", buf[j]);
88 for (j = i; j < i + BLOCK_SIZE && j < count; ++j) {
89 if (buf[j] >= 0x20 && buf[j] <= 0x7e)
96 printf("\t{0, NULL} } }\n");
98 /* Release resources */