-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack.go
160 lines (137 loc) · 3.41 KB
/
pack.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
package hsm
import (
"encoding/binary"
"encoding/hex"
"fmt"
"golib/gerror"
"golib/modules/logs"
"strings"
)
/*请求报文*/
type HsmRequest struct {
buf []byte
len int
}
func NewHsmRequest() *HsmRequest {
req := new(HsmRequest)
req.buf = make([]byte, MaxReqLen)
req.len = 0
return req
}
func (req *HsmRequest) SetByte(b byte) {
req.buf[req.len] = b
req.len++
}
func (req *HsmRequest) SetInt8(i int) {
req.buf[req.len] = byte(i)
req.len += 1
}
func (req *HsmRequest) SetInt16(i int) {
binary.BigEndian.PutUint16(req.buf[req.len:], uint16(i))
req.len += 2
}
func (req *HsmRequest) SetHex(hex []byte) {
copy(req.buf[req.len:], hex)
req.len += len(hex)
}
func (req *HsmRequest) SetAsc(asc string) {
bt, _ := hex.DecodeString(asc)
copy(req.buf[req.len:], bt)
req.len += len(bt)
}
func (req *HsmRequest) String() string {
req.Byte()
return fmt.Sprintf("[%04d] [% 02X]", req.len, req.buf[:req.len])
}
func (req *HsmRequest) Byte() []byte {
return req.buf[:req.len:req.len]
}
type HsmResponse struct {
respBody []byte
retCd byte
len int
currBuf []byte
currLen int
}
func NewHsmResponse(buf []byte) (*HsmResponse, error) {
if len(buf) < 1 {
logs.Error("应答报文为空;")
return nil, gerror.NewR(9000, nil, "应答报文为空")
}
hsmResp := new(HsmResponse)
hsmResp.respBody = buf
hsmResp.len = len(buf)
hsmResp.retCd = hsmResp.respBody[0]
hsmResp.currBuf = hsmResp.respBody[1:]
hsmResp.currLen = hsmResp.len - 1
return hsmResp, nil
}
func (rsp *HsmResponse) GetRetCd() byte {
return rsp.retCd
}
func (rsp *HsmResponse) GetByte() (byte, error) {
if rsp.currLen >= 1 {
bt := rsp.currBuf[0]
rsp.currBuf = rsp.currBuf[1:]
rsp.currLen--
return bt, nil
} else {
logs.Error("报文解析完毕,剩余为空;")
return 0, gerror.NewR(9001, nil, "报文解析完毕,剩余为空;")
}
}
func (rsp *HsmResponse) GetInt16() (uint16, error) {
if rsp.currLen >= 2 {
i := binary.BigEndian.Uint16(rsp.currBuf[:2])
rsp.currBuf = rsp.currBuf[2:]
rsp.currLen -= 2
return i, nil
} else {
logs.Error("报文解析完毕,剩余为空;")
return 0, gerror.NewR(9002, nil, "报文解析完毕,剩余为空;")
}
}
func (rsp *HsmResponse) GetInt8() (int, error) {
if rsp.currLen >= 1 {
i := int(rsp.currBuf[0])
rsp.currBuf = rsp.currBuf[1:]
rsp.currLen -= 1
return i, nil
} else {
logs.Error("报文解析完毕,剩余为空;")
return 0, gerror.NewR(9002, nil, "报文解析完毕,剩余为空;")
}
}
func (rsp *HsmResponse) GetHex(len int) ([]byte, error) {
if rsp.currLen >= len {
hex := rsp.currBuf[:len]
rsp.currBuf = rsp.currBuf[len:]
rsp.currLen -= len
return hex, nil
} else {
logs.Error("报文解析完毕,剩余为空;")
return nil, gerror.NewR(9002, nil, "报文解析完毕,剩余为空;")
}
}
func (rsp *HsmResponse) GetAsc(len int) (string, error) {
if rsp.currLen >= len {
bin := rsp.currBuf[:len]
rsp.currBuf = rsp.currBuf[len:]
rsp.currLen -= len
asc := strings.ToUpper(hex.EncodeToString(bin))
return asc, nil
} else {
logs.Error("报文解析完毕,剩余为空;")
return "", gerror.NewR(9002, nil, "报文解析完毕,剩余为空;")
}
}
func (rsp *HsmResponse) GetLByte() []byte {
return rsp.currBuf
}
func (rsp *HsmResponse) CheckSucc() bool {
return rsp.retCd == success
}
func (rsp *HsmResponse) String() string {
return fmt.Sprintf("应答[% 02X][%c][%d][% 02X]",
rsp.respBody, rsp.retCd, rsp.currLen, rsp.currBuf)
}