/
type_bin.go
132 lines (107 loc) · 2.73 KB
/
type_bin.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package bmnumbers
import (
"errors"
"regexp"
"strconv"
)
type Bin struct{}
func (d Bin) GetName() string {
return "bin"
}
func (d Bin) getInfo() string {
return ""
}
func (d Bin) GetSize() int {
return -1 // Any size
}
func (d Bin) importMatchers() map[string]ImportFunc {
result := make(map[string]ImportFunc)
result["^0b(?P<bin>[0-1]+)$"] = binImportNoSize
result["^0b<(?P<bits>[0-9]+)>(?P<bin>[0-1]+)$"] = binImportWithSize
return result
}
func (d Bin) Convert(n *BMNumber) error {
convertFrom := n.nType.GetName()
switch convertFrom {
default:
return errors.New("cannot convert from " + convertFrom + " to " + d.GetName())
}
return nil
}
func binImportNoSize(re *regexp.Regexp, input string) (*BMNumber, error) {
binNum := re.ReplaceAllString(input, "${bin}")
var nextByte string
newNumber := BMNumber{}
newNumber.number = make([]byte, (len(binNum)-1)/8+1)
newNumber.bits = len(binNum)
newNumber.nType = Bin{}
for i := 0; ; i++ {
if len(binNum) > 8 {
nextByte = binNum[len(binNum)-8:]
binNum = binNum[:len(binNum)-8]
} else if len(binNum) > 0 {
nextByte = binNum
binNum = ""
} else {
break
}
if val, err := strconv.ParseUint(nextByte, 2, 8); err == nil {
decoded := byte(val)
newNumber.number[i] = decoded
} else {
return nil, errors.New("invalid binary number" + input)
}
}
return &newNumber, nil
}
func binImportWithSize(re *regexp.Regexp, input string) (*BMNumber, error) {
binNum := re.ReplaceAllString(input, "${bin}")
binSizeS := re.ReplaceAllString(input, "${bits}")
var binSize int
if val, err := strconv.Atoi(binSizeS); err != nil {
return nil, errors.New("invalid hex number, wrong bits value" + input)
} else {
binSize = val
}
if len(binNum) > binSize {
return nil, errors.New("invalid binary number, the specified number if greater than the bits size " + input)
}
var nextByte string
newNumber := BMNumber{}
newNumber.number = make([]byte, (binSize-1)/8+1)
newNumber.bits = binSize
newNumber.nType = Bin{}
for i := 0; ; i++ {
if len(binNum) > 8 {
nextByte = binNum[len(binNum)-8:]
binNum = binNum[:len(binNum)-8]
} else if len(binNum) > 0 {
nextByte = binNum
binNum = ""
} else {
if i*8 < binSize {
nextByte = "00000000"
} else {
break
}
}
if val, err := strconv.ParseUint(nextByte, 2, 8); err == nil {
decoded := byte(val)
newNumber.number[i] = decoded
} else {
return nil, errors.New("invalid binary number" + input)
}
}
return &newNumber, nil
}
func (d Bin) ExportString(n *BMNumber) (string, error) {
return n.ExportBinary(true)
}
func (d Bin) ShowInstructions() map[string]string {
result := make(map[string]string)
result["addop"] = "add"
return result
}
func (d Bin) ShowPrefix() string {
return "0b"
}