file2include.c 2.3 KB

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