/
marshall.go
211 lines (179 loc) · 5.88 KB
/
marshall.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2017 Thales e-Security
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package module
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"reflect"
"github.com/pkg/errors"
)
const (
seeJobResponse_OK = iota
seeJobResponse_Error = iota
seeJobResponse_ProcessingError = iota
wordSize = 4
)
// marshallAll marshalls all items and returns a buffer. Supported
// types are []byte, int, int32, uint8 and string. All integer types
// are treated as 32-bit values.
func marshallAll(items ...interface{}) (io.Reader, error) {
buffer := new(bytes.Buffer)
var err error
for _, item := range items {
switch i := item.(type) {
case []byte:
err = marshallBytes(i, buffer)
case int32:
err = marshallInt(i, buffer)
case int:
err = marshallInt(int32(i), buffer)
case uint8:
err = marshallInt(int32(i), buffer)
case int64:
err = marshallInt64(i, buffer)
case string:
err = marshallString(i, buffer)
default:
err = errors.Errorf("Unknown type: %s", reflect.TypeOf(item))
}
if err != nil {
break
}
}
return buffer, err
}
// unmarshallAll will consume data from reader and unmarshall into the supplied
// objects.
func unmarshallAll(reader io.Reader, destinations ...interface{}) error {
var err error
for _, dest := range destinations {
if dp, ok := dest.(*[]byte); ok {
*dp, err = unmarshallBytes(reader)
} else if dp, ok := dest.(*int32); ok {
*dp, err = unmarshallInt(reader)
} else if dp, ok := dest.(*string); ok {
*dp, err = unmarshallString(reader)
}
if err != nil {
break
}
}
return err
}
// unmarshallInt reads an int32 from the input data.
func unmarshallInt(in io.Reader) (int32, error) {
var result int32
err := binary.Read(in, binary.LittleEndian, &result)
return result, err
}
// marshallInt writes an int32 to the output buffer.
func marshallInt(i int32, out io.Writer) error {
return binary.Write(out, binary.LittleEndian, i)
}
// marshallInt64 writes an int64 to the output buffer.
func marshallInt64(i int64, out io.Writer) error {
return binary.Write(out, binary.LittleEndian, i)
}
// marshallString writes a string to the output buffer.
func marshallString(s string, out io.Writer) error {
return marshallBytes(append([]byte(s), 0), out)
}
// unmarshallString reads a string from the input data.
func unmarshallString(in io.Reader) (string, error) {
s, err := unmarshallBytes(in)
return string(s[:len(s)-1]), err
}
// marshallBytes writes a slice to the output buffer.
func marshallBytes(b []byte, out io.Writer) error {
err := marshallInt(int32(len(b)), out)
if err != nil {
return err
}
_, err = out.Write(b)
if err != nil {
return err
}
numPadBytesNeeded := getPaddingForLength(len(b))
if numPadBytesNeeded > 0 {
padding := make([]byte, numPadBytesNeeded)
_, err = out.Write(padding)
}
return err
}
// getPaddingForLength calculates the amount of padding required
// to align with the word boundary.
func getPaddingForLength(length int) int {
return (wordSize - (length % wordSize)) % wordSize
}
// unmarshallBytes reads a slice from the input data.
func unmarshallBytes(in io.Reader) ([]byte, error) {
length, err := unmarshallInt(in)
if err != nil {
return nil, err
}
result := make([]byte, length)
n, err := io.ReadFull(in, result)
if err != nil {
return result, errors.Wrap(err, fmt.Sprintf("Tried to read %d bytes, found %d", length, n))
}
paddingToDiscard := getPaddingForLength(int(length))
_, err = io.CopyN(ioutil.Discard, in, int64(paddingToDiscard))
return result, errors.Wrap(err, fmt.Sprintf("Failed to discard %d padding bytes", paddingToDiscard))
}
// unmarshallModuleReponse unpicks the response from a module. If the response
// indicates an error then an appropriate error string is returned. Otherwise
// the job response data is returned.
func unmarshallModuleReponse(in io.Reader) ([]byte, error) {
// Skip the first four bytes, since they contain a redundant
// length indicator
_, err := io.CopyN(ioutil.Discard, in, 4)
if err != nil {
return nil, err
}
responseCode, err := unmarshallInt(in)
if err != nil {
return nil, err
}
switch responseCode {
case seeJobResponse_OK:
return unmarshallBytes(in)
case seeJobResponse_Error:
errorString, err := unmarshallString(in)
if err != nil {
return nil, errors.WithMessage(err, "Failed to unmarshal error string")
}
return nil, errors.New("Error from module: " + errorString)
case seeJobResponse_ProcessingError:
errorString, err := unmarshallString(in)
if err != nil {
return nil, errors.WithMessage(err, "Failed to unmarshal error string")
}
errorCode, err := unmarshallInt(in)
if err != nil {
return nil, errors.WithMessage(err, "Failed to unmarshal error code")
}
return nil, errors.New(fmt.Sprintf("Error from module (code=%d): %s", errorCode, errorString))
default:
return nil, errors.Errorf("Unknown response code: %d", responseCode)
}
}