mkdata.pl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # This program is used to embed arbitrary data into a C binary. It takes
  2. # a list of files as an input, and produces a .c data file that contains
  3. # contents of all these files as collection of char arrays.
  4. #
  5. # Usage: perl <this_file> <file1> [file2, ...] > embedded_data.c
  6. use File::MimeInfo;
  7. foreach my $i (0 .. $#ARGV) {
  8. open FD, '<:raw', $ARGV[$i] or die "Cannot open $ARGV[$i]: $!\n";
  9. printf("static const unsigned char v%d[] = {", $i);
  10. my $byte;
  11. my $j = 0;
  12. while (read(FD, $byte, 1)) {
  13. if (($j % 12) == 0) {
  14. print "\n";
  15. }
  16. printf ' %#04x,', ord($byte);
  17. $j++;
  18. }
  19. print " 0x00\n};\n";
  20. close FD;
  21. }
  22. print <<EOS;
  23. /* ympd
  24. (c) 2013-2014 Andrew Karpow <andy\@ympd.org>
  25. This project's homepage is: http://www.ympd.org
  26. Redistribution and use in source and binary forms, with or without
  27. modification, are permitted provided that the following conditions
  28. are met:
  29. - Redistributions of source code must retain the above copyright
  30. notice, this list of conditions and the following disclaimer.
  31. - Redistributions in binary form must reproduce the above copyright
  32. notice, this list of conditions and the following disclaimer in the
  33. documentation and/or other materials provided with the distribution.
  34. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  38. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  39. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  40. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  41. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  42. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  43. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  44. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. #ifndef __HTTP_FILES_H__
  47. #define __HTTP_FILES_H__
  48. #include <stddef.h>
  49. #include <string.h>
  50. #include <sys/types.h>
  51. static const struct embedded_file {
  52. const char *name;
  53. const unsigned char *data;
  54. const char *mimetype;
  55. size_t size;
  56. } embedded_files[] = {
  57. EOS
  58. foreach my $i (0 .. $#ARGV) {
  59. my $mime = mimetype($ARGV[$i]);
  60. print " {\"/$ARGV[$i]\", v$i, \"$mime\", sizeof(v$i) - 1},\n";
  61. }
  62. print <<EOS;
  63. {NULL, NULL, NULL, 0}
  64. };
  65. const struct embedded_file *find_embedded_file(const char *name) {
  66. const struct embedded_file *p;
  67. for (p = embedded_files; p->name != NULL; p++)
  68. if (!strcmp(p->name, name))
  69. return p;
  70. return NULL;
  71. }
  72. #endif
  73. EOS