mkdata.pl 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. #ifndef __HTTP_FILES_H__
  24. #define __HTTP_FILES_H__
  25. #include <stddef.h>
  26. #include <string.h>
  27. #include <sys/types.h>
  28. static const struct embedded_file {
  29. const char *name;
  30. const unsigned char *data;
  31. const char *mimetype;
  32. size_t size;
  33. } embedded_files[] = {
  34. EOS
  35. foreach my $i (0 .. $#ARGV) {
  36. my $mime = mimetype($ARGV[$i]);
  37. $ARGV[$i] =~ s/htdocs//;
  38. print " {\"$ARGV[$i]\", v$i, \"$mime\", sizeof(v$i) - 1},\n";
  39. }
  40. print <<EOS;
  41. {NULL, NULL, NULL, 0}
  42. };
  43. const struct embedded_file *find_embedded_file(const char *name) {
  44. const struct embedded_file *p;
  45. for (p = embedded_files; p->name != NULL; p++)
  46. if (!strcmp(p->name, name))
  47. return p;
  48. return NULL;
  49. }
  50. #endif
  51. EOS