bin2header.c 812 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /* bin2header.c - program to convert binary file into a C structure
  2. * definition to be included in a header file.
  3. *
  4. * (C) Copyright 2008 by Harald Welte <laforge@openmoko.org>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <unistd.h>
  11. int main(int argc, char **argv)
  12. {
  13. if (argc < 2) {
  14. fprintf(stderr, "%s needs one argument: the structure name\n",
  15. argv[0]);
  16. exit(1);
  17. }
  18. printf("/* bin2header output - automatically generated */\n");
  19. printf("unsigned char %s[] = {\n", argv[1]);
  20. while (1) {
  21. int i, nread;
  22. unsigned char buf[10];
  23. nread = read(0, buf, sizeof(buf));
  24. if (nread <= 0)
  25. break;
  26. printf("\t");
  27. for (i = 0; i < nread - 1; i++)
  28. printf("0x%02x, ", buf[i]);
  29. printf("0x%02x,\n", buf[nread-1]);
  30. }
  31. printf("};\n");
  32. exit(0);
  33. }