Blame view
tools/file2include.c
2.33 KB
83d290c56 SPDX: Convert all... |
1 |
// SPDX-License-Identifier: GPL-2.0+ |
ac0201961 tools: provide a ... |
2 3 4 5 6 |
/* * Convert a file image to a C define * * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de> * |
ac0201961 tools: provide a ... |
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
* For testing EFI disk management we need an in memory image of * a disk. * * The tool file2include converts a file to a C include. The file * is separated into strings of 8 bytes. Only the non-zero strings * are written to the include. The output format has been designed * to maintain readability. * * As the disk image needed for testing contains mostly zeroes a high * compression ratio can be attained. */ #include <stdio.h> #include <stdlib.h> #include <stdint.h> |
ac0201961 tools: provide a ... |
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
/* Size of the blocks written to the compressed file */ #define BLOCK_SIZE 8 int main(int argc, char *argv[]) { FILE *file; int ret; unsigned char *buf; size_t count, i, j; /* Provide usage help */ if (argc != 2) { printf("Usage: %s FILENAME ", argv[0]); return EXIT_FAILURE; } /* Open file */ file = fopen(argv[1], "r"); if (!file) { perror("fopen"); return EXIT_FAILURE; } /* Get file length */ ret = fseek(file, 0, SEEK_END); if (ret < 0) { perror("fseek"); return EXIT_FAILURE; } count = ftell(file); if (!count) { fprintf(stderr, "File %s has length 0 ", argv[1]); return EXIT_FAILURE; } rewind(file); /* Read file */ buf = malloc(count); if (!buf) { perror("calloc"); return EXIT_FAILURE; } count = fread(buf, 1, count, file); /* Generate output */ |
e75ac7039 tools/file2includ... |
67 68 |
printf("/* SPDX-License-Identifier: GPL-2.0+ */ "); |
ac0201961 tools: provide a ... |
69 70 71 72 73 74 75 76 |
printf("/* "); printf(" * Non-zero %u byte strings of a disk image ", BLOCK_SIZE); printf(" * "); printf(" * Generated with tools/file2include "); |
ac0201961 tools: provide a ... |
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
printf(" */ "); printf("#define EFI_ST_DISK_IMG { 0x%08zx, { \\ ", count); for (i = 0; i < count; i += BLOCK_SIZE) { int c = 0; for (j = i; j < i + BLOCK_SIZE && j < count; ++j) { if (buf[j]) c = 1; } if (!c) continue; printf("\t{0x%08zx, \"", i); for (j = i; j < i + BLOCK_SIZE && j < count; ++j) printf("\\x%02x", buf[j]); printf("\"}, /* "); for (j = i; j < i + BLOCK_SIZE && j < count; ++j) { |
b14619ba6 tools/file2includ... |
97 |
if (buf[j] != '*' && buf[j] >= 0x20 && buf[j] <= 0x7e) |
ac0201961 tools: provide a ... |
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
printf("%c", buf[j]); else printf("."); } printf(" */ \\ "); } printf("\t{0, NULL} } } "); /* Release resources */ free(buf); ret = fclose(file); if (ret) { perror("fclose"); return EXIT_FAILURE; } return EXIT_SUCCESS; } |