forked from sijms/go-ora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_packet.go
108 lines (101 loc) · 2.84 KB
/
data_packet.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
package network
import (
"bytes"
"encoding/binary"
"errors"
)
type DataPacket struct {
Packet
sessionCtx *SessionContext
dataFlag uint16
buffer []byte
}
func (pck *DataPacket) bytes() []byte {
output := bytes.Buffer{}
temp := make([]byte, 0xA)
if pck.sessionCtx.handshakeComplete && pck.sessionCtx.Version >= 315 {
binary.BigEndian.PutUint32(temp, pck.length)
} else {
binary.BigEndian.PutUint16(temp, uint16(pck.length))
}
temp[4] = uint8(pck.packetType)
temp[5] = pck.flag
binary.BigEndian.PutUint16(temp[8:], pck.dataFlag)
output.Write(temp)
if len(pck.buffer) > 0 {
output.Write(pck.buffer)
}
return output.Bytes()
}
func newDataPacket(initialData []byte, sessionCtx *SessionContext) (*DataPacket, error) {
var outputData []byte = initialData
var err error
if sessionCtx.AdvancedService.HashAlgo != nil {
hashData := sessionCtx.AdvancedService.HashAlgo.Compute(outputData)
outputData = append(outputData, hashData...)
}
if sessionCtx.AdvancedService.CryptAlgo != nil {
//outputData = make([]byte, len(outputData))
//copy(outputData, outputData)
outputData, err = sessionCtx.AdvancedService.CryptAlgo.Encrypt(outputData)
if err != nil {
return nil, err
}
}
if sessionCtx.AdvancedService.HashAlgo != nil || sessionCtx.AdvancedService.CryptAlgo != nil {
foldingKey := uint8(0)
outputData = append(outputData, foldingKey)
}
return &DataPacket{
Packet: Packet{
dataOffset: 0xA,
length: uint32(len(outputData)) + 0xA,
packetType: DATA,
flag: 0,
},
sessionCtx: sessionCtx,
dataFlag: 0,
buffer: outputData,
}, nil
}
func newDataPacketFromData(packetData []byte, sessionCtx *SessionContext) (*DataPacket, error) {
if len(packetData) < 0xA || PacketType(packetData[4]) != DATA {
return nil, errors.New("Not data packet")
}
pck := &DataPacket{
Packet: Packet{
dataOffset: 0xA,
//length: binary.BigEndian.Uint16(packetData),
packetType: PacketType(packetData[4]),
flag: packetData[5],
},
sessionCtx: sessionCtx,
dataFlag: binary.BigEndian.Uint16(packetData[8:]),
buffer: packetData[10:],
}
if sessionCtx.handshakeComplete && sessionCtx.Version >= 315 {
pck.length = binary.BigEndian.Uint32(packetData)
} else {
pck.length = uint32(binary.BigEndian.Uint16(packetData))
}
var err error
if sessionCtx.AdvancedService.CryptAlgo != nil || sessionCtx.AdvancedService.HashAlgo != nil {
pck.buffer = pck.buffer[:len(pck.buffer)-1]
}
if sessionCtx.AdvancedService.CryptAlgo != nil {
pck.buffer, err = sessionCtx.AdvancedService.CryptAlgo.Decrypt(pck.buffer)
if err != nil {
return nil, err
}
}
if sessionCtx.AdvancedService.HashAlgo != nil {
pck.buffer, err = sessionCtx.AdvancedService.HashAlgo.Validate(pck.buffer)
if err != nil {
return nil, err
}
}
return pck, nil
}
//func (pck *DataPacket) Data() []byte {
// return pck.buffer
//}