-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.go
344 lines (310 loc) · 8.66 KB
/
io.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* ****************************************************************************
* Copyright 2020 51 Degrees Mobile Experts Limited (51degrees.com)
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
* ***************************************************************************/
package common
import (
"bytes"
"encoding"
"encoding/binary"
"fmt"
"math"
"time"
)
// WriteStrings one dimensional array of strings.
func WriteStrings(b *bytes.Buffer, v []string) error {
err := WriteUint16(b, uint16(len(v)))
if err != nil {
return err
}
for _, i := range v {
err = WriteString(b, i)
if err != nil {
return err
}
}
return nil
}
// ReadStrings into one dimensional array of strings.
func ReadStrings(b *bytes.Buffer) ([]string, error) {
c, err := ReadUint16(b)
if err != nil {
return nil, err
}
v := make([]string, c, c)
for i := uint16(0); i < c; i++ {
v[i], err = ReadString(b)
if err != nil {
return nil, err
}
}
return v, nil
}
// WriteByteArrayArray two dimensional array of bytes.
func WriteByteArrayArray(b *bytes.Buffer, v [][]byte) error {
err := WriteUint16(b, uint16(len(v)))
if err != nil {
return err
}
for _, i := range v {
err = WriteByteArray(b, i)
if err != nil {
return err
}
}
return nil
}
// ReadByteArrayArray from two dimensional array of bytes.
func ReadByteArrayArray(b *bytes.Buffer) ([][]byte, error) {
c, err := ReadUint16(b)
if err != nil {
return nil, err
}
v := make([][]byte, c, c)
for i := uint16(0); i < c; i++ {
v[i], err = ReadByteArray(b)
if err != nil {
return nil, err
}
}
return v, nil
}
// WriteFloat32 to the buffer.
func WriteFloat32(b *bytes.Buffer, f float32) error {
return WriteUint32(b, math.Float32bits(f))
}
// ReadFloat32 from the buffer.
func ReadFloat32(b *bytes.Buffer) (float32, error) {
f, err := ReadUint32(b)
if err != nil {
return 0, err
}
return math.Float32frombits(f), nil
}
// ReadTime from the next binary object.
func ReadTime(b *bytes.Buffer) (time.Time, error) {
var t time.Time
d, err := ReadByteArray(b)
if err == nil {
t.GobDecode(d)
}
return t, err
}
// WriteTime as a binary object.
func WriteTime(b *bytes.Buffer, t time.Time) error {
d, err := t.GobEncode()
if err != nil {
return err
}
return WriteByteArray(b, d)
}
// ReadDate reads the date from the unsigned 16 bit integer and then determines
// the date by adding this to the IoDateBase epoch.
func ReadDate(b *bytes.Buffer) (time.Time, error) {
d, err := ReadUint16(b)
if err != nil {
return time.Time{}, err
}
return IoDateBase.Add(time.Duration(d) * time.Hour * 24), nil
}
// WriteDate writes the date as the number of hours since the IoDateBase epoch.
// Uses an unsigned 16 bit integer.
func WriteDate(b *bytes.Buffer, t time.Time) error {
return WriteUint16(b, uint16(t.Sub(IoDateBase).Hours()/24))
}
// ReadMarshaller reads the content into the unmarshaler instance.
func ReadMarshaller(b *bytes.Buffer, m encoding.BinaryUnmarshaler) error {
v, err := ReadByteArray(b)
if err != nil {
return err
}
return m.UnmarshalBinary(v)
}
// WriteMarshaller writes the result of marshal binary call to the buffer.
func WriteMarshaller(b *bytes.Buffer, m encoding.BinaryMarshaler) error {
v, err := m.MarshalBinary()
if err != nil {
return err
}
return WriteByteArray(b, v)
}
// ReadString reads a null (zero) terminated string from the byte buffer.
func ReadString(b *bytes.Buffer) (string, error) {
s, err := b.ReadBytes(0)
if err == nil {
return string(s[0 : len(s)-1]), err
}
return "", err
}
// WriteString writes a null (zero) terminated string to the byte buffer.
func WriteString(b *bytes.Buffer, s string) error {
l, err := b.WriteString(s)
if err == nil {
// Validate the number of bytes written matches the number of bytes in
// the string.
if l != len(s) {
return fmt.Errorf(
"mismatched lengths '%d' and '%d'",
l,
len(s))
}
// Write the null terminator.
b.WriteByte(0)
}
return err
}
// ReadByteArray reads the first 4 bytes as an unsigned 32 bit integer to
// determine the length of the byte array contained in the following bytes.
func ReadByteArray(b *bytes.Buffer) ([]byte, error) {
l, err := ReadUint32(b)
if err != nil {
return nil, err
}
return b.Next(int(l)), err
}
// ReadByteArray writes the length of the byte array as an unsigned 32 bit
// integer followed by the bytes.
func WriteByteArray(b *bytes.Buffer, v []byte) error {
err := WriteUint32(b, uint32(len(v)))
if err != nil {
return err
}
return WriteByteArrayNoLength(b, v)
}
// ReadByteArrayNoLength reads the number of bytes specified into a new byte
// array.
func ReadByteArrayNoLength(b *bytes.Buffer, l int) ([]byte, error) {
v := b.Next(l)
if len(v) != l {
return nil, fmt.Errorf("read '%d' bytes but expected '%d'", len(v), l)
}
return v, nil
}
// WriteByteArrayNoLength writes the byte array to the buffer without recording
// the length. Used with fixed length data.
func WriteByteArrayNoLength(b *bytes.Buffer, v []byte) error {
l, err := b.Write(v)
if err == nil {
if l != len(v) {
return fmt.Errorf(
"mismatched lengths '%d' and '%d'",
l,
len(v))
}
}
return err
}
// GetDateInMinutes returns the number of minutes that have elapsed since the
// IoDateBase epoch.
func GetDateInMinutes(t time.Time) uint32 {
return uint32(t.Sub(IoDateBase).Minutes())
}
// GetTimeFromMinutes returns the date time from the minutes provided.
func GetDateFromMinutes(t uint32) time.Time {
return IoDateBase.Add(time.Minute * time.Duration(t))
}
// ReadDateFromUInt32 reads the date from the buffer where the date is stored
// as the number of minutes as an unsigned 32 bit integer that have elapsed
// since the IoDateBase epoch.
func ReadDateFromUInt32(b *bytes.Buffer) (time.Time, error) {
i, err := ReadUint32(b)
if err != nil {
return time.Time{}, err
}
return IoDateBase.Add(time.Duration(i) * time.Minute), nil
}
// WriteDateToUInt32 writes the date to the buffer as an unsigned 32 bit
// representing the number of minutes that have elapsed since the IoDateBase
// epoch.
func WriteDateToUInt32(b *bytes.Buffer, t time.Time) error {
return WriteUint32(b, GetDateInMinutes(t))
}
// ReadByte reads the next byte from the buffer.
func ReadByte(b *bytes.Buffer) (byte, error) {
d := b.Next(1)
if len(d) != 1 {
return 0, fmt.Errorf("'%d' bytes incorrect for byte", len(d))
}
return d[0], nil
}
// WriteByte writes the byte provided to the buffer.
func WriteByte(b *bytes.Buffer, i byte) error {
return b.WriteByte(i)
}
// Read the next byte as a bool and returns the value.
func ReadBool(b *bytes.Buffer) (bool, error) {
d := b.Next(1)
if len(d) != 1 {
return false, fmt.Errorf("'%d' bytes incorrect for bool", len(d))
}
return d[0] != 0, nil
}
// WriteBool writes the boolean value to the buffer as a byte.
func WriteBool(b *bytes.Buffer, v bool) error {
var d byte
if v {
d = 1
}
return b.WriteByte(d)
}
// ReadUint32 reads an unsigned 32 bit integer from the buffer. The integer is
// stored in little endian format.
func ReadUint32(b *bytes.Buffer) (uint32, error) {
d := b.Next(4)
if len(d) != 4 {
return 0, fmt.Errorf("'%d' bytes incorrect for Uint32", len(d))
}
return binary.LittleEndian.Uint32(d), nil
}
// ReadUint16 reads an unsigned 16 bit integer from the buffer. The integer is
// stored in little endian format.
func ReadUint16(b *bytes.Buffer) (uint16, error) {
d := b.Next(2)
if len(d) != 2 {
return 0, fmt.Errorf("'%d' bytes incorrect for Uint16", len(d))
}
return binary.LittleEndian.Uint16(d), nil
}
// WriteUint16 reads an unsigned 16 bit integer from the buffer where the
// integer is stored in little endian format.
func WriteUint16(b *bytes.Buffer, i uint16) error {
v := make([]byte, 2)
binary.LittleEndian.PutUint16(v, i)
l, err := b.Write(v)
if err == nil {
if l != len(v) {
return fmt.Errorf(
"Mismatched lengths '%d' and '%d'",
l,
len(v))
}
}
return err
}
// WriteUint32 reads an unsigned 32 bit integer from the buffer where the
// integer is stored in little endian format.
func WriteUint32(b *bytes.Buffer, i uint32) error {
v := make([]byte, 4)
binary.LittleEndian.PutUint32(v, i)
l, err := b.Write(v)
if err == nil {
if l != len(v) {
return fmt.Errorf(
"mismatched lengths '%d' and '%d'",
l,
len(v))
}
}
return err
}