bin2h.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2008,2010 Free Software Foundation, Inc.
  3. *
  4. * GRUB is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * GRUB is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <config.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #define _GNU_SOURCE 1
  21. #include <getopt.h>
  22. #include "progname.h"
  23. static struct option options[] =
  24. {
  25. {"help", no_argument, 0, 'h' },
  26. {"version", no_argument, 0, 'V' },
  27. {0, 0, 0, 0 }
  28. };
  29. static void
  30. usage (int status)
  31. {
  32. if (status)
  33. fprintf (stderr,
  34. "Try ``%s --help'' for more information.\n", program_name);
  35. else
  36. printf ("\
  37. Usage: %s [OPTIONS] SYMBOL-NAME\n\
  38. \n\
  39. Convert a binary file to a C header.\n\
  40. \n\
  41. -h, --help display this message and exit\n\
  42. -V, --version print version information and exit\n\
  43. \n\
  44. Report bugs to <%s>.\n\
  45. ", program_name, PACKAGE_BUGREPORT);
  46. exit (status);
  47. }
  48. int
  49. main (int argc, char *argv[])
  50. {
  51. int b, i;
  52. char *sym;
  53. set_program_name (argv[0]);
  54. /* Check for options. */
  55. while (1)
  56. {
  57. int c = getopt_long (argc, argv, "snm:r:hVv", options, 0);
  58. if (c == -1)
  59. break;
  60. else
  61. switch (c)
  62. {
  63. case 'h':
  64. usage (0);
  65. break;
  66. case 'V':
  67. printf ("%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
  68. return 0;
  69. default:
  70. usage (1);
  71. break;
  72. }
  73. }
  74. if (optind >= argc)
  75. usage (1);
  76. if (optind + 1 != argc)
  77. usage (1);
  78. sym = argv[optind];
  79. b = getchar ();
  80. if (b == EOF)
  81. goto abort;
  82. printf ("/* THIS CHUNK OF BYTES IS AUTOMATICALY GENERATED */\n"
  83. "unsigned char %s[] =\n{\n", sym);
  84. while (1)
  85. {
  86. printf ("0x%02x", b);
  87. b = getchar ();
  88. if (b == EOF)
  89. goto end;
  90. for (i = 0; i < 16 - 1; i++)
  91. {
  92. printf (", 0x%02x", b);
  93. b = getchar ();
  94. if (b == EOF)
  95. goto end;
  96. }
  97. printf (",\n");
  98. }
  99. end:
  100. printf ("\n};\n");
  101. abort:
  102. exit (0);
  103. }