bin2c.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. fclose(input);
  38. return -1;
  39. }
  40. if (argc == 4) {
  41. name = argv[3];
  42. } else {
  43. size_t arglen = strlen(argv[1]);
  44. name = argv[1];
  45. for (int i = 0; i < arglen; i++) {
  46. if (argv[1][i] == '.')
  47. argv[1][i] = '_';
  48. else if (argv[1][i] == '/')
  49. name = &argv[1][i+1];
  50. }
  51. }
  52. fprintf(output, "const unsigned char ff_%s_data[] = { ", name);
  53. while (fread(&data, 1, 1, input) > 0) {
  54. fprintf(output, "0x%02x, ", data);
  55. length++;
  56. }
  57. fprintf(output, "0x00 };\n");
  58. fprintf(output, "const unsigned int ff_%s_len = %u;\n", name, length);
  59. fclose(output);
  60. if (ferror(input) || !feof(input)) {
  61. fclose(input);
  62. return -1;
  63. }
  64. fclose(input);
  65. return 0;
  66. }