file2include.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include <malloc.h>
  22. /* Size of the blocks written to the compressed file */
  23. #define BLOCK_SIZE 8
  24. int main(int argc, char *argv[])
  25. {
  26. FILE *file;
  27. int ret;
  28. unsigned char *buf;
  29. size_t count, i, j;
  30. /* Provide usage help */
  31. if (argc != 2) {
  32. printf("Usage:\n%s FILENAME\n", argv[0]);
  33. return EXIT_FAILURE;
  34. }
  35. /* Open file */
  36. file = fopen(argv[1], "r");
  37. if (!file) {
  38. perror("fopen");
  39. return EXIT_FAILURE;
  40. }
  41. /* Get file length */
  42. ret = fseek(file, 0, SEEK_END);
  43. if (ret < 0) {
  44. perror("fseek");
  45. return EXIT_FAILURE;
  46. }
  47. count = ftell(file);
  48. if (!count) {
  49. fprintf(stderr, "File %s has length 0\n", argv[1]);
  50. return EXIT_FAILURE;
  51. }
  52. rewind(file);
  53. /* Read file */
  54. buf = malloc(count);
  55. if (!buf) {
  56. perror("calloc");
  57. return EXIT_FAILURE;
  58. }
  59. count = fread(buf, 1, count, file);
  60. /* Generate output */
  61. printf("/* SPDX-License-Identifier: GPL-2.0+ */\n");
  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\n");
  67. printf("#define EFI_ST_DISK_IMG { 0x%08zx, { \\\n", count);
  68. for (i = 0; i < count; i += BLOCK_SIZE) {
  69. int c = 0;
  70. for (j = i; j < i + BLOCK_SIZE && j < count; ++j) {
  71. if (buf[j])
  72. c = 1;
  73. }
  74. if (!c)
  75. continue;
  76. printf("\t{0x%08zx, \"", i);
  77. for (j = i; j < i + BLOCK_SIZE && j < count; ++j)
  78. printf("\\x%02x", buf[j]);
  79. printf("\"}, /* ");
  80. for (j = i; j < i + BLOCK_SIZE && j < count; ++j) {
  81. if (buf[j] != '*' && buf[j] >= 0x20 && buf[j] <= 0x7e)
  82. printf("%c", buf[j]);
  83. else
  84. printf(".");
  85. }
  86. printf(" */ \\\n");
  87. }
  88. printf("\t{0, NULL} } }\n");
  89. /* Release resources */
  90. free(buf);
  91. ret = fclose(file);
  92. if (ret) {
  93. perror("fclose");
  94. return EXIT_FAILURE;
  95. }
  96. return EXIT_SUCCESS;
  97. }