forked from pmeulen/text-filters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base64 to HEX Dump
executable file
·56 lines (46 loc) · 998 Bytes
/
Base64 to HEX Dump
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
#!/usr/bin/php
<?php
function hex_dump($data, $newline="\n")
{
static $from = '';
static $to = '';
static $width = 16; # number of bytes per line
static $pad = '.'; # padding for non-visible characters
if ($from==='')
{
for ($i=0; $i<=0xFF; $i++)
{
$from .= chr($i);
$to .= ($i >= 0x20 && $i <= 0x7E) ? chr($i) : $pad;
}
}
$hex = str_split(bin2hex($data), $width*2);
$chars = str_split(strtr($data, $from, $to), $width);
$offset = 0;
foreach ($hex as $i => $line)
{
echo sprintf('%6X',$offset).' : '.implode(' ', str_split($line,2)) . ' [' . $chars[$i] . ']' . $newline;
$offset += $width;
}
}
$in='';
while (!feof(STDIN))
{
$res = fread(STDIN, 1024);
if (false === $res)
{
fwrite(STDERR, "error reading from stdin");
exit(3);
}
$in .= $res;
}
$bin=base64_decode($in);
if (false===$bin)
{
fwrite(STDERR, "base64_encode failed");
exit(3);
}
$result=hex_dump($bin);
fwrite(STDOUT, $result);
exit(0);
?>