-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBinaryString.inc.sh
executable file
·81 lines (64 loc) · 1.38 KB
/
BinaryString.inc.sh
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
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env bash
BASEDIR=$(cd `dirname "${BASH_SOURCE[0]}"` && pwd)
. ${BASEDIR}/includer.inc.sh upvars classes
## Definition of class BinaryString
class BinaryString
function BinaryString.__construct
{
scope
var $this.pos=0
local buf=""
local array=""
if [[ $1 == stdin ]]
then
while read; do
array+=$REPLY
done < <( xxd -p )
fi
var $this.buf="$array"
}
function BinaryString.tostring
{
scope
local buf
put $this.buf into buf
echo -n "$buf" | xxd -p -r
}
function BinaryString.NextByte
{
scope
local buf
local pos
put $this.buf into buf
put $this.pos into pos
local "$1" && upvar $1 "$(( 0x${buf[@]:$(( ( pos ) * 2)):2} ))"
pset $this.pos=$(( pos + 1 ))
}
function BinaryString.NextWord
{
scope
local buf
local pos
put $this.buf into buf
put $this.pos into pos
local val="$(( 0x${buf[@]:$(( ( pos ) * 2)):4} ))"
# echo "val: '$val'"
# local "$1" && upvar $1 "$(( 0x${buf[@]:$(( ( pos ) * 2)):4} ))"
local "$1" && upvar $1 "$val"
pset $this.pos=$(( pos + 2 ))
}
function BinaryString.test
{
## Instantiate "buf" as a BinaryString, and call constructor with argument "stdin"
## (and pipe in some input)
new BinaryString buf stdin < <( echo -e 'ABCDEF\n\x1b[1mHello\n\x1b[0mHow are you\n' )
## Check everything has worked
buf.tostring
## Check NextByte
for i in {1..20}; do
buf.NextWord b
printf "%04x " $b
done
}
endclass
# BinaryString.test