-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathencoder.go
More file actions
208 lines (169 loc) · 4.77 KB
/
encoder.go
File metadata and controls
208 lines (169 loc) · 4.77 KB
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
/*
Copyright 2021 Hewlett Packard Enterprise Development LP
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 structex
import (
"fmt"
"io"
"math"
"math/bits"
"reflect"
)
type encoder struct {
writer io.ByteWriter
currentByte uint8
byteOffset uint64
bitOffset uint64
transcoder *transcoder
}
func (e *encoder) write(value uint64, nbits uint64) error {
if nbits > 1 && value > math.MaxUint64 {
return fmt.Errorf("Value %d (%#x) will overflow bitfield of %d bits", value, value, nbits)
}
// Write any bits that might be part of previous bitfield definitions
if e.bitOffset != 0 {
e.currentByte |= uint8(value << e.bitOffset)
remainingBits := 8 - e.bitOffset
if nbits < remainingBits {
e.bitOffset += nbits
return nil
} else {
value = value >> remainingBits
nbits -= remainingBits
e.writeByte(e.currentByte)
e.currentByte = uint8(value)
e.bitOffset = 0
}
}
for nbits != 0 {
e.currentByte = uint8(value & ((1 << nbits)-1))
if nbits < 8 {
e.bitOffset += nbits
return nil
} else {
e.writeByte(e.currentByte)
value = value >> 8
nbits -= 8
}
}
return nil
}
func (e *encoder) writeByte(value uint8) error {
if err := e.writer.WriteByte(value); err != nil {
return err
}
e.byteOffset++
return nil
}
func (e *encoder) align(val alignment) error {
if e.bitOffset != 0 {
if err := e.write(0, 8-e.bitOffset); err != nil {
return err
}
}
for e.byteOffset%uint64(val) != 0 {
if err := e.write(0, 8); err != nil {
return err
}
}
return nil
}
func (e *encoder) field(val reflect.Value, tags *tags) error {
v := getValue(val)
nbits := tags.bitfield.nbits
if nbits == 0 {
nbits = uint64(val.Type().Bits())
}
if (tags != nil && tags.endian == big) || (e.transcoder.defaultEndianness == big && (tags != nil && tags.endian != little)) {
switch val.Kind() {
case reflect.Uint16, reflect.Int16:
v = uint64(bits.ReverseBytes16(uint16(v)))
case reflect.Uint32, reflect.Int32, reflect.Uint, reflect.Int:
v = uint64(bits.ReverseBytes32(uint32(v)))
case reflect.Uint64, reflect.Int64:
v = bits.ReverseBytes64(v)
}
}
return e.write(v, nbits)
}
func (e *encoder) layout(val reflect.Value, ref *tagReference) error {
value := uint64(0)
if ref.value.IsZero() {
switch ref.tags.layout.format {
case sizeOf:
sz, err := size(val.Index(0))
if err != nil {
return err
}
value = uint64(val.Len()) * sz
if ref.tags.layout.relative {
value -= e.byteOffset
}
case countOf:
value = uint64(val.Len())
}
} else {
value = getValue(ref.value)
}
return e.write(value, ref.tags.bitfield.nbits)
}
func (e *encoder) array(t *transcoder, arr reflect.Value, tags *tags, ref *tagReference) error {
l := arr.Len()
if ref != nil && !ref.value.IsZero() {
l = int(ref.value.Uint())
}
for i := 0; i < l; i++ {
if err := t.transcode(arr.Index(i), tags); err != nil {
return err
}
}
return nil
}
func (e *encoder) slice(t *transcoder, arr reflect.Value, tags *tags, ref *tagReference) error {
return e.array(t, arr, tags, ref)
}
/*
Encode serializes the data structure defined by 's' into the available
io.ByteWriter stream. Annotation rules are as defined in the Decode
function.
*/
func Encode(writer io.ByteWriter, s interface{}) error {
e := encoder{
writer: writer,
currentByte: 0,
byteOffset: 0,
bitOffset: 0,
}
t := newTranscoder(&e)
e.transcoder = t
return t.transcode(reflect.ValueOf(s), nil)
}
/*
EncodeByteBuffer serializes the provided data structure 's' into a new byte
buffer. Bytes are packed according to the annotation rules defined for 's'.
*/
func EncodeByteBuffer(s interface{}) ([]byte, error) {
buf := NewBuffer(s)
if buf == nil {
return nil, fmt.Errorf("Could not allocate byte buffer")
}
if err := Encode(buf, s); err != nil {
return nil, err
}
return buf.Bytes(), nil
}