-
Notifications
You must be signed in to change notification settings - Fork 8
/
payload.go
74 lines (61 loc) · 1.19 KB
/
payload.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
package ntlmssp
import (
"bytes"
"fmt"
"io"
)
type payload struct {
Len uint16
MaxLen uint16
Offset uint32
}
func newPayload(length int, offset *int) payload {
p := payload{
Len: uint16(length),
MaxLen: uint16(length),
Offset: uint32(*offset),
}
*offset += length
return p
}
func (p *payload) ReadValue(reader *bytes.Reader) ([]byte, error) {
// Remember where we are
pos, err := reader.Seek(0, io.SeekCurrent)
if err != nil {
return nil, err
}
// Seek to where the payload is
_, err = reader.Seek(int64(p.Offset), io.SeekStart)
if err != nil {
return nil, err
}
// Read the value
b := make([]byte, p.Len)
n, err := reader.Read(b)
if err != nil {
return nil, err
}
if n != int(p.Len) {
return nil, fmt.Errorf("expected %d bytes, only read %d", p.Len, n)
}
// Seek back to where we were
_, err = reader.Seek(pos, io.SeekStart)
if err != nil {
return nil, err
}
return b, nil
}
func (p *payload) ReadString(reader *bytes.Reader, utf16 bool) (string, error) {
b, err := p.ReadValue(reader)
if err != nil {
return "", err
}
if utf16 {
s, err := utf16ToString(b)
if err != nil {
return "", err
}
return s, nil
}
return string(b), nil
}