binarydump.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stdbool.h>
  8. #include <string.h>
  9. static unsigned char *
  10. read_file_to_buffer(const char *filename, int *ret_size)
  11. {
  12. unsigned char *buffer;
  13. FILE *file;
  14. int file_size, read_size;
  15. if (!(file = fopen(filename, "rb")))
  16. return NULL;
  17. fseek(file, 0, SEEK_END);
  18. file_size = ftell(file);
  19. fseek(file, 0, SEEK_SET);
  20. if (!(buffer = malloc(file_size))) {
  21. fclose(file);
  22. return NULL;
  23. }
  24. read_size = fread(buffer, 1, file_size, file);
  25. fclose(file);
  26. if (read_size < file_size) {
  27. free(buffer);
  28. return NULL;
  29. }
  30. *ret_size = file_size;
  31. return buffer;
  32. }
  33. static int
  34. print_help()
  35. {
  36. printf("Usage: binarydump -o <file> -n <name> input_file\n");
  37. printf("Options:\n");
  38. printf(" -o <file> Place the output into <file>\n");
  39. printf(" -n <name> The name of array <file>\n");
  40. return -1;
  41. }
  42. static bool
  43. bin_file_dump(const unsigned char *file, int size, const char *bin_file_output,
  44. const char *array_name)
  45. {
  46. unsigned i = 0;
  47. const unsigned char *p = file, *p_end = file + size;
  48. FILE *file_output = fopen(bin_file_output, "wb+");
  49. if (!file_output)
  50. return false;
  51. fprintf(file_output, "\nunsigned char __aligned(4) %s[] = {\n ",
  52. array_name);
  53. while (p < p_end) {
  54. fprintf(file_output, "0x%02X", *p++);
  55. if (p == p_end)
  56. break;
  57. fprintf(file_output, ",");
  58. if ((++i % 12) != 0)
  59. fprintf(file_output, " ");
  60. else
  61. fprintf(file_output, "\n ");
  62. }
  63. fprintf(file_output, "\n};\n");
  64. fclose(file_output);
  65. return true;
  66. }
  67. int
  68. main(int argc, char *argv[])
  69. {
  70. unsigned char *file;
  71. int size;
  72. bool ret;
  73. const char *bin_file_input, *array_file_output = NULL, *array_name = NULL;
  74. for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  75. if (!strcmp(argv[0], "-o")) {
  76. ++argv;
  77. if (--argc == 0)
  78. return print_help();
  79. array_file_output = *argv;
  80. }
  81. else if (!strcmp(argv[0], "-n")) {
  82. ++argv;
  83. if (--argc == 0)
  84. return print_help();
  85. array_name = *argv;
  86. }
  87. else
  88. return print_help();
  89. }
  90. if (!array_file_output || !array_name)
  91. return print_help();
  92. bin_file_input = *argv;
  93. if (!(file = read_file_to_buffer(bin_file_input, &size)))
  94. return -1;
  95. ret = bin_file_dump(file, size, array_file_output, array_name);
  96. free(file);
  97. return ret ? 0 : -1;
  98. }