-
Notifications
You must be signed in to change notification settings - Fork 2
/
hex.go
168 lines (151 loc) · 3.11 KB
/
hex.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package hex
import (
"encoding/hex"
"errors"
"fmt"
"math"
"math/bits"
"strconv"
"strings"
"sync/atomic"
"unicode"
)
const hextable_l = "0123456789abcdef"
const hextable_u = "0123456789ABCDEF"
var (
lowercase = uint32(1)
empty = errors.New("Empty string")
)
func OutputLowerCase(enabled bool) {
if enabled {
atomic.StoreUint32(&lowercase, 1)
return
}
atomic.StoreUint32(&lowercase, 0)
}
func EncodedLenWithSeq(n int) int {
return (n * 3) - 1
}
func Encode(dst, src []byte, sep ...byte) (n int) {
if 0 == len(sep) || 2 > len(src) {
if hex.EncodedLen(len(src)) <= len(dst) {
return hex.Encode(dst, src)
}
return 0
}
if 0x20 <= sep[0] && 0x7A >= sep[0] {
if _n := (len(dst) + 1) / 3; 0 < _n {
//
table := []byte(hextable_u)
//
if 0x0 == atomic.LoadUint32(&lowercase) {
table = []byte(hextable_l)
}
//
if _n < len(src) {
src = src[:_n]
}
//
for i, item := range src {
if 0 < i {
dst[n] = sep[0]
n++
}
dst[n] = table[item>>4]
dst[n+1] = table[item&0x0f]
n += 2
}
}
}
return
}
func EncodeToString(src []byte, sep ...byte) string {
if 0 < len(src) {
var s strings.Builder
var r byte
//
table := []byte(hextable_u)
//
if 0x0 == atomic.LoadUint32(&lowercase) {
table = []byte(hextable_l)
}
//
if 0 < len(sep) && 0x20 <= sep[0] && 0x7A >= sep[0] {
r = sep[0]
}
//
s.Grow(EncodedLenWithSeq(len(src)))
//
for i, item := range src {
if 0 < r && 0 < i {
s.WriteByte(r)
}
s.WriteByte(table[item>>4])
s.WriteByte(table[item&0x0f])
}
//
return s.String()
}
//
return ""
}
func EncodeToStringWithSeq(src []byte, sep rune) string {
if s := byte(sep); 0x20 <= s && 0x7A >= s {
return EncodeToString(src, s)
}
return EncodeToString(src)
}
func EncodeNumberToStringWithSeq(v interface{}, sep rune, le bool, n ...int) string {
var value uint64
switch result := v.(type) {
case string:
if s, err := strconv.ParseUint(result, 10, 64); err == nil {
value = s
}
case int8, uint8:
return fmt.Sprintf("%02X", result)
case int16:
return fmt.Sprintf("%02X %02X", (uint16(math.Abs(float64(result)))>>8)&0xFF, uint16(math.Abs(float64(result)))&0xFF)
case uint16:
return fmt.Sprintf("%02X %02X", (result>>8)&0xFF, result&0xFF)
case int:
value = uint64(math.Abs(float64(result)))
case int32:
value = uint64(math.Abs(float64(result)))
case uint32:
value = uint64(result)
case int64:
value = uint64(math.Abs(float64(result)))
case uint64:
value = uint64(result)
}
// 修正max
max := bits.Len64(value)/8 + 1
if 0 < len(n) && 0 < n[0] && max != n[0] {
max = n[0]
}
data := make([]byte, max)
for i, _ := range data {
if le {
data[i] = byte(value >> (i * 8))
} else {
data[i] = byte(value >> ((max - i - 1) * 8))
}
}
return EncodeToStringWithSeq(data, sep)
}
func DecodeStringWithSeq(s string) ([]byte, error) {
list := strings.FieldsFunc(s, func(r rune) bool {
if unicode.IsNumber(r) {
return false
} else if unicode.IsLetter(r) {
return false
}
return true
})
if 0 < len(list) {
s = strings.Join(list, "")
return hex.DecodeString(s)
}
return nil, empty
}