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
|
|
@ -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