forked from LiBwrt/openwrt-6.x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xxdi.pl
executable file
·66 lines (53 loc) · 1.56 KB
/
xxdi.pl
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env perl
#
# xxdi.pl - perl implementation of 'xxd -i' mode
#
# Copyright 2013 Greg Kroah-Hartman <[email protected]>
# Copyright 2013 Linux Foundation
#
# Released under the GPLv2.
#
# Implements the "basic" functionality of 'xxd -i' in perl to keep build
# systems from having to build/install/rely on vim-core, which not all
# distros want to do. But everyone has perl, so use it instead.
#
use strict;
use warnings;
my $indata;
my $var_name = "stdin";
my $full_output = (@ARGV > 0 && $ARGV[0] eq '-i') ? shift @ARGV : undef;
{
local $/;
my $fh;
if (@ARGV) {
$var_name = $ARGV[0];
open($fh, '<:raw', $var_name) || die("xxdi.pl: Unable to open $var_name: $!\n");
} elsif (! -t STDIN) {
$fh = \*STDIN;
undef $full_output;
} else {
die "usage: xxdi.pl [-i] [infile]\n";
}
$indata = readline $fh;
close $fh;
}
my $len_data = length($indata);
my $num_digits_per_line = 12;
my $outdata = "";
# Use the variable name of the file we read from, converting '/' and '.
# to '_', or, if this is stdin, just use "stdin" as the name.
$var_name =~ s/\//_/g;
$var_name =~ s/\./_/g;
$var_name = "__$var_name" if $var_name =~ /^\d/;
$outdata = "unsigned char $var_name\[] = { " if $full_output;
for (my $key= 0; $key < $len_data; $key++) {
if ($key % $num_digits_per_line == 0) {
$outdata = substr($outdata, 0, -1)."\n ";
}
$outdata .= sprintf("0x%.2x, ", ord(substr($indata, $key, 1)));
}
$outdata = substr($outdata, 0, -2);
$outdata .= "\n";
$outdata .= "};\nunsigned int $var_name\_len = $len_data;\n" if $full_output;
binmode STDOUT;
print $outdata;