bin2h.c 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * mkbootimg/bin2h.c
  3. *
  4. * Copyright (C) 2020 bzt (bztsrc@gitlab)
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * This file is part of the BOOTBOOT Protocol package.
  27. * @brief Small utility to convert binary to C header, because MacOSX does not support ld -b binary!
  28. *
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <stdint.h>
  34. #include "zlib.c"
  35. int main(int argc, char **argv)
  36. {
  37. FILE *f, *c, *h;
  38. unsigned long int size, len;
  39. unsigned char *buff = NULL, *buff2 = NULL;
  40. char *fn, name[255];
  41. int i, file;
  42. if(argc < 2){
  43. printf("bin2h <bin file> [bin file2...]\r\n");
  44. exit(1);
  45. }
  46. c = fopen("data.c", "w"); if(!c) { fprintf(stderr, "bin2h: unable to open data.c\r\n"); return 1; }
  47. h = fopen("data.h", "w"); if(!h) { fprintf(stderr, "bin2h: unable to open data.h\r\n"); return 1; }
  48. fprintf(c, "/* generated by bin2h, do not edit */\n\n");
  49. fprintf(h, "/* generated by bin2h, do not edit */\n\n");
  50. for(file = 1; file < argc; file++){
  51. size = 0;
  52. f = fopen(argv[file],"rb");
  53. if(f) {
  54. fseek(f, 0, SEEK_END);
  55. size = ftell(f);
  56. fseek(f, 0, SEEK_SET);
  57. buff = (unsigned char*)malloc(size);
  58. if(!buff) {
  59. fprintf(stderr, "bin2h: memory allocation error\r\n");
  60. exit(2);
  61. }
  62. fread(buff, 1, size, f);
  63. fclose(f);
  64. fn = strrchr(argv[file], '/');
  65. if(!fn) fn = strrchr(argv[file], '\\');
  66. if(!fn) fn = argv[file]; else fn++;
  67. for(i = 0; fn[i]; i++)
  68. name[i] = fn[i] == '.' || fn[i] <= ' ' ? '_' : fn[i];
  69. name[i] = 0;
  70. if(size > 512) {
  71. len = compressBound(size);
  72. buff2 = (unsigned char*)malloc(len);
  73. if(!buff2) {
  74. fprintf(stderr, "bin2h: memory allocation error\r\n");
  75. exit(2);
  76. }
  77. compress2(buff2, &len, buff, size, 9);
  78. free(buff); buff = buff2;
  79. } else len = size;
  80. fprintf(h, "#define sizeof_%s %ld\nextern unsigned char binary_%s[%ld];\n", name, size, name, len);
  81. fprintf(c, "unsigned char binary_%s[%ld] = { ", name, len);
  82. for(i = 0; i < len; i++)
  83. fprintf(c,"%s%d", i?",":"", buff[i]);
  84. fprintf(c," };\n");
  85. free(buff);
  86. buff = NULL;
  87. } else
  88. fprintf(stderr, "bin2h: unable to open input file: %s\r\n", argv[file]);
  89. }
  90. fclose(h);
  91. fclose(c);
  92. }