Refactor client into separate files
Add a main loop, move I/O code into io.go, move configuration into config.go. Use shared.Decode24BitLength instead of manually decoding block lengths. Fix response block decoding and checksum validation. Add constants for commonly used byte values and use these in the signer and the client.
This commit is contained in:
parent
65855152ce
commit
42d1e6e991
8 changed files with 322 additions and 241 deletions
|
@ -6,6 +6,15 @@ type Action uint8
|
|||
|
||||
const ActionNul = Action(0)
|
||||
|
||||
func (a Action) String() string {
|
||||
switch a {
|
||||
case ActionNul:
|
||||
return "NUL"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
func encode24BitLength(data []byte) []byte {
|
||||
lengthBytes := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(lengthBytes, uint32(len(data)))
|
||||
|
@ -13,7 +22,7 @@ func encode24BitLength(data []byte) []byte {
|
|||
}
|
||||
|
||||
// calculate length from 24 bits of data in network byte order
|
||||
func decode24BitLength(bytes []byte) int {
|
||||
func Decode24BitLength(bytes []byte) int {
|
||||
return int(binary.BigEndian.Uint32([]byte{0x0, bytes[0], bytes[1], bytes[2]}))
|
||||
}
|
||||
|
||||
|
|
|
@ -25,19 +25,19 @@ type SignerRequest struct {
|
|||
const protocolVersion = 1
|
||||
|
||||
func SignerRequestFromData(lengthBytes []byte, blockData []byte, checkSum byte) (*SignerRequest, error) {
|
||||
headerLength := decode24BitLength(blockData[0:3])
|
||||
headerLength := Decode24BitLength(blockData[0:3])
|
||||
headerBytes := blockData[3 : 3+headerLength]
|
||||
|
||||
contentBytes := blockData[3+headerLength:]
|
||||
content1Length := decode24BitLength(contentBytes[0:3])
|
||||
content1Length := Decode24BitLength(contentBytes[0:3])
|
||||
content1 := string(contentBytes[3 : 3+content1Length])
|
||||
|
||||
content2Offset := 3 + content1Length
|
||||
content2Length := decode24BitLength(contentBytes[content2Offset : content2Offset+3])
|
||||
content2Length := Decode24BitLength(contentBytes[content2Offset : content2Offset+3])
|
||||
content2 := string(contentBytes[3+content2Offset : 3+content2Offset+content2Length])
|
||||
|
||||
content3Offset := 3 + content2Offset + content2Length
|
||||
content3Length := decode24BitLength(contentBytes[content3Offset : content3Offset+3])
|
||||
content3Length := Decode24BitLength(contentBytes[content3Offset : content3Offset+3])
|
||||
content3 := string(contentBytes[3+content3Offset : 3+content3Offset+content3Length])
|
||||
|
||||
calculated := CalculateXorCheckSum([][]byte{lengthBytes, blockData})
|
||||
|
@ -79,8 +79,8 @@ func (r SignerRequest) Serialize() []byte {
|
|||
|
||||
func NewNulRequest() *SignerRequest {
|
||||
return &SignerRequest{
|
||||
Version: protocolVersion,
|
||||
Action: ActionNul,
|
||||
Version: protocolVersion,
|
||||
Action: ActionNul,
|
||||
Content1: time.Now().UTC().Format("010203042006.05"),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,20 +17,26 @@ type SignerResponse struct {
|
|||
}
|
||||
|
||||
func SignerResponseFromData(lengthBytes []byte, blockData []byte, checkSum byte) (*SignerResponse, error) {
|
||||
headerLength := decode24BitLength(lengthBytes)
|
||||
headerBytes := blockData[3 : 3+headerLength]
|
||||
if len(blockData) < 3 {
|
||||
return nil, errors.New("begin of structure corrupt")
|
||||
}
|
||||
|
||||
contentBytes := blockData[3+headerLength:]
|
||||
content1Length := decode24BitLength(contentBytes[0:3])
|
||||
content1 := string(contentBytes[3 : 3+content1Length])
|
||||
offset := 0
|
||||
headerLength := Decode24BitLength(blockData[offset : offset+3])
|
||||
offset += 3
|
||||
headerBytes := blockData[offset : offset+headerLength]
|
||||
offset += headerLength
|
||||
|
||||
content2Offset := 3 + content1Length
|
||||
content2Length := decode24BitLength(contentBytes[content2Offset : content2Offset+3])
|
||||
content2 := string(contentBytes[3+content2Offset : 3+content2Offset+content2Length])
|
||||
|
||||
content3Offset := 3 + content2Offset + content2Length
|
||||
content3Length := decode24BitLength(contentBytes[content3Offset : content3Offset+3])
|
||||
content3 := string(contentBytes[3+content3Offset : 3+content3Offset+content3Length])
|
||||
content := make([]string, 3)
|
||||
for offset < len(blockData) {
|
||||
dataLength := Decode24BitLength(blockData[offset : offset+3])
|
||||
if len(blockData)-3 < dataLength {
|
||||
return nil, errors.New("structure cut off")
|
||||
}
|
||||
offset += 3
|
||||
content = append(content, string(blockData[offset:offset+dataLength]))
|
||||
offset += dataLength
|
||||
}
|
||||
|
||||
calculated := CalculateXorCheckSum([][]byte{lengthBytes, blockData})
|
||||
if checkSum != calculated {
|
||||
|
@ -42,9 +48,9 @@ func SignerResponseFromData(lengthBytes []byte, blockData []byte, checkSum byte)
|
|||
Action: Action(headerBytes[1]),
|
||||
Reserved1: headerBytes[2],
|
||||
Reserved2: headerBytes[3],
|
||||
Content1: content1,
|
||||
Content2: content2,
|
||||
Content3: content3,
|
||||
Content1: content[0],
|
||||
Content2: content[1],
|
||||
Content3: content[2],
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue