|
| 1 | +#!/usr/bin/perl |
| 2 | +# This program is used to embed arbitrary data into a C binary. It takes |
| 3 | +# a list of files as an input, and produces a .c data file that contains |
| 4 | +# contents of all these files as collection of char arrays. |
| 5 | +# |
| 6 | +# Usage: perl <this_file> <file1> [file2, ...] > embedded_data.c |
| 7 | + |
| 8 | +use File::Basename; |
| 9 | + |
| 10 | +%mimetypes = ( |
| 11 | + js => 'application/javascript', |
| 12 | + css => 'text/css', |
| 13 | + ico => 'image/vnd.microsoft.icon', |
| 14 | + woff => 'application/font-woff', |
| 15 | + ttf => 'application/x-font-ttf', |
| 16 | + eot => 'application/octet-stream', |
| 17 | + svg => 'image/svg+xml', |
| 18 | + html => 'text/html' |
| 19 | +); |
| 20 | + |
| 21 | +foreach my $i (0 .. $#ARGV) { |
| 22 | + open FD, '<:raw', $ARGV[$i] or die "Cannot open $ARGV[$i]: $!\n"; |
| 23 | + printf("static const unsigned char v%d[] = {", $i); |
| 24 | + my $byte; |
| 25 | + my $j = 0; |
| 26 | + while (read(FD, $byte, 1)) { |
| 27 | + if (($j % 12) == 0) { |
| 28 | + print "\n"; |
| 29 | + } |
| 30 | + printf ' %#04x,', ord($byte); |
| 31 | + $j++; |
| 32 | + } |
| 33 | + print " 0x00\n};\n"; |
| 34 | + close FD; |
| 35 | +} |
| 36 | + |
| 37 | +print <<EOS; |
| 38 | +#include <stddef.h> |
| 39 | +#include <string.h> |
| 40 | +#include <sys/types.h> |
| 41 | +#include "src/http_server.h" |
| 42 | +
|
| 43 | +static const struct embedded_file embedded_files[] = { |
| 44 | +EOS |
| 45 | + |
| 46 | +foreach my $i (0 .. $#ARGV) { |
| 47 | + my ($ext) = $ARGV[$i] =~ /([^.]+)$/; |
| 48 | + my $mime = $mimetypes{$ext}; |
| 49 | + $ARGV[$i] =~ s/htdocs//; |
| 50 | + print " {\"$ARGV[$i]\", v$i, \"$mime\", sizeof(v$i) - 1},\n"; |
| 51 | +} |
| 52 | + |
| 53 | +print <<EOS; |
| 54 | + {NULL, NULL, NULL, 0} |
| 55 | +}; |
| 56 | +
|
| 57 | +const struct embedded_file *find_embedded_file(const char *name) { |
| 58 | + const struct embedded_file *p; |
| 59 | + for (p = embedded_files; p->name != NULL; p++) |
| 60 | + if (!strcmp(p->name, name)) |
| 61 | + return p; |
| 62 | + return NULL; |
| 63 | +} |
| 64 | +
|
| 65 | +EOS |
0 commit comments