File: hextool

package info (click to toggle)
xa 2.4.1-0.1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,356 kB
  • sloc: ansic: 8,585; asm: 845; makefile: 755; perl: 116; sh: 53
file content (57 lines) | stat: -rwxr-xr-x 1,251 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl -s

# This tool either emits hex in a machine or human parseable format, or
# compares two binaries in the form of cmp. It is mostly to deal with
# systems with an unreliable hexdump, that *lack* hexdump (or od), and/or
# emit non-standard output. 

# "use bytes"
BEGIN { $^H |= 0x00000008 unless ($] < 5.006); }


if ($output || $noutput) {
	# check output mode. If $output, there must be output. If there is
	# $noutput there must NOT be output. Feed tee or something to this.
	$wasoutput = 0;
	while(<>) {
		$wasoutput++;
	}
	exit (($wasoutput && $output) ? 0 :
		($wasoutput && $noutput) ? 1 :
		(!$wasoutput && !$noutput) ? 0 :
		1);
}

if ($cmp) {
	# compare mode. both files must fit in memory. to eliminate any
	# weirdness about endianness, encoding, etc., we unpack them to
	# hex bytes and just compare strings.
	undef $/;
	open(W, "$cmp") || die("can't open $cmp: $!\n");
	$cmp = unpack("H*", <W>);
	close(W);
	$std = unpack("H*", <>);
	if ($cmp ne $std) {
		print <<"EOF";
FAILED!

received from stdin
-------------------
$std

expected to equal
-----------------
$cmp

FAILED!
EOF
		exit 255;
	}
	exit 0;
}

# hexdump mode. accept data on stdin, spew a stream of hex bytes.

while(<>) {
	print STDOUT unpack("H*", $_);
}