bin2c.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <string.h>
  23. #include <stdio.h>
  24. int main(int argc, char **argv)
  25. {
  26. const char *name;
  27. FILE *input, *output;
  28. unsigned int length = 0;
  29. unsigned char data;
  30. if (argc < 3 || argc > 4)
  31. return 1;
  32. input = fopen(argv[1], "rb");
  33. if (!input)
  34. return -1;
  35. output = fopen(argv[2], "wb");
  36. if (!output)
  37. return -1;
  38. if (argc == 4) {
  39. name = argv[3];
  40. } else {
  41. size_t arglen = strlen(argv[1]);
  42. name = argv[1];
  43. for (int i = 0; i < arglen; i++) {
  44. if (argv[1][i] == '.')
  45. argv[1][i] = '_';
  46. else if (argv[1][i] == '/')
  47. name = &argv[1][i+1];
  48. }
  49. }
  50. fprintf(output, "const unsigned char ff_%s_data[] = { ", name);
  51. while (fread(&data, 1, 1, input) > 0) {
  52. fprintf(output, "0x%02x, ", data);
  53. length++;
  54. }
  55. fprintf(output, "0x00 };\n");
  56. fprintf(output, "const unsigned int ff_%s_len = %u;\n", name, length);
  57. fclose(output);
  58. if (ferror(input) || !feof(input))
  59. return -1;
  60. fclose(input);
  61. return 0;
  62. }