-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbin2c.pl
More file actions
executable file
·43 lines (35 loc) · 922 Bytes
/
Copy pathbin2c.pl
File metadata and controls
executable file
·43 lines (35 loc) · 922 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/perl -w
use strict;
use warnings;
my ($file_name, $content_type, $var_name) = @ARGV;
if (!$file_name || !$content_type || !$var_name) {
die "usage: bin2c.pl <local file name> <http content type> " .
"<c variable name>";
}
open(my $fh, "<", $file_name) || die "$file_name: $!";
binmode($fh) || die "$file_name: binmode(): $!";
my $data;
{
local $/;
$data = <$fh>;
}
close($fh);
my $content_len = length($data);
$data = "Content-Type: $content_type\r\n" .
"Content-Length: $content_len\r\n" .
"\r\n" .
$data;
my $hfile = "$var_name.h";
open($fh, ">", $hfile) || die "$hfile: $!";
binmode($fh) || die "$hfile: binmode(): $!";
print $fh "char $var_name\[\] = { ";
while ($data =~ s/^(.)//ms) {
my $char = $1;
print $fh sprintf("0x%02hx", ord($char));
if ($data ne "") {
print $fh ",";
print $fh "\n" if ($char eq "\n");
}
}
print $fh " };\n";
close($fh);