-
Notifications
You must be signed in to change notification settings - Fork 6
/
unmarshal.go
143 lines (119 loc) · 2.7 KB
/
unmarshal.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
package putty
import (
"bytes"
"crypto/aes"
"encoding/binary"
"fmt"
"io"
"math/big"
"reflect"
)
func unmarshal(data []byte, val interface{}, enc bool) error {
v := reflect.ValueOf(val).Elem()
buf := bytes.NewReader(data)
err := parseField(v, buf)
if err != nil {
return err
}
// check key block size
err = checkGarbage(buf, enc)
if err != nil {
return fmt.Errorf("wrong key size: %s", err)
}
return nil
}
func parseField(v reflect.Value, src *bytes.Reader) error {
fieldType := v.Type()
switch fieldType.Kind() {
case reflect.Struct:
for i := 0; i < fieldType.NumField(); i++ {
if fieldType.Field(i).PkgPath != "" {
return fmt.Errorf("struct contains unexported fields")
}
err := parseField(v.Field(i), src)
if err != nil {
return err
}
}
return nil
default:
}
switch fieldType {
case reflect.TypeOf(string("")):
parsedString, err := readString(src)
if err != nil {
return err
}
v.Set(reflect.ValueOf(parsedString))
case reflect.TypeOf([]byte(nil)):
parsedBytes, err := readBytes(src)
if err != nil {
return err
}
v.Set(reflect.ValueOf(parsedBytes))
case reflect.TypeOf(new(big.Int)):
parsedInt, err := readBigInt(src)
if err != nil {
return err
}
v.Set(reflect.ValueOf(parsedInt))
default:
return fmt.Errorf("unknown type %s", fieldType)
}
return nil
}
func readBytes(src *bytes.Reader) ([]byte, error) {
var length uint32
// read 4 bytes (uint32 size) of the next element size
err := binary.Read(src, binary.BigEndian, &length)
if err != nil {
return nil, err
}
// get the current reader position
pos, err := src.Seek(0, io.SeekCurrent)
if err != nil {
return nil, err
}
// check next element size
if int64(length)+pos > src.Size() {
return nil, fmt.Errorf("the element length %d is out of range", length)
}
buf := make([]byte, length)
n, err := io.ReadFull(src, buf)
if err != nil {
return nil, err
}
if n != int(length) {
return nil, fmt.Errorf("expected to read %d, but read %d", length, n)
}
return buf, nil
}
func readString(src *bytes.Reader) (string, error) {
b, err := readBytes(src)
if err != nil {
return "", err
}
return string(b), nil
}
func readBigInt(src *bytes.Reader) (*big.Int, error) {
b, err := readBytes(src)
if err != nil {
return nil, err
}
return new(big.Int).SetBytes(b), nil
}
func checkGarbage(src *bytes.Reader, encrypted bool) error {
pos, err := src.Seek(0, io.SeekCurrent)
if err != nil {
return err
}
if encrypted {
// normalize the size of the decrypted part (should be % aes.BlockSize)
pos = pos + aes.BlockSize - pos&(aes.BlockSize-1)
}
// check key size
if src.Size() != pos {
return fmt.Errorf("expected %d, got %d", pos, src.Size())
}
return nil
}