file2include.c 2.4 KB

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