-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathattribute_decoder.go
More file actions
283 lines (239 loc) · 6.02 KB
/
attribute_decoder.go
File metadata and controls
283 lines (239 loc) · 6.02 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
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
package ipp
import (
"encoding/binary"
"fmt"
"io"
)
// AttributeDecoder reads and decodes ipp from an input stream
type AttributeDecoder struct {
reader io.Reader
}
// NewAttributeDecoder returns a new decoder that reads from r
func NewAttributeDecoder(r io.Reader) *AttributeDecoder {
return &AttributeDecoder{r}
}
// Decode reads the next ipp attribute into a attribute struct. the type is identified by a tag passed as an argument
func (d *AttributeDecoder) Decode(tag int8) (*Attribute, error) {
attr := Attribute{Tag: tag}
name, err := d.decodeString()
if err != nil {
return nil, err
}
attr.Name = name
switch attr.Tag {
case TagEnum, TagInteger:
val, err := d.decodeInteger()
if err != nil {
return nil, err
}
attr.Value = val
case TagBoolean:
val, err := d.decodeBool()
if err != nil {
return nil, err
}
attr.Value = val
case TagDate:
val, err := d.decodeDate()
if err != nil {
return nil, err
}
attr.Value = val
case TagRange:
val, err := d.decodeRange()
if err != nil {
return nil, err
}
attr.Value = val
case TagResolution:
val, err := d.decodeResolution()
if err != nil {
return nil, err
}
attr.Value = val
case TagBeginCollection:
val, err := d.decodeCollection()
if err != nil {
return nil, err
}
attr.Value = val
default:
val, err := d.decodeString()
if err != nil {
return nil, err
}
attr.Value = val
}
return &attr, nil
}
func (d *AttributeDecoder) decodeBool() (b bool, err error) {
if _, err = d.readValueLength(); err != nil {
return
}
if err = binary.Read(d.reader, binary.BigEndian, &b); err != nil {
return
}
return
}
func (d *AttributeDecoder) decodeInteger() (i int, err error) {
if _, err = d.readValueLength(); err != nil {
return
}
var reti int32
if err = binary.Read(d.reader, binary.BigEndian, &reti); err != nil {
return
}
return int(reti), nil
}
func (d *AttributeDecoder) decodeString() (string, error) {
length, err := d.readValueLength()
if err != nil {
return "", err
}
if length == 0 {
return "", nil
}
bs := make([]byte, length)
if _, err := d.reader.Read(bs); err != nil {
return "", nil
}
return string(bs), nil
}
func (d *AttributeDecoder) decodeDate() ([]int, error) {
length, err := d.readValueLength()
if err != nil {
return nil, err
}
is := make([]int, length)
var ti int8
for i := int16(0); i < length; i++ {
if err = binary.Read(d.reader, binary.BigEndian, &ti); err != nil {
return nil, err
}
is[i] = int(ti)
}
return is, nil
}
func (d *AttributeDecoder) decodeRange() ([]int32, error) {
length, err := d.readValueLength()
if err != nil {
return nil, err
}
// initialize range element count (c) and range slice (r)
c := length / 4
r := make([]int32, c)
for i := int16(0); i < c; i++ {
var ti int32
if err = binary.Read(d.reader, binary.BigEndian, &ti); err != nil {
return nil, err
}
r[i] = ti
}
return r, nil
}
func (d *AttributeDecoder) decodeResolution() (res Resolution, err error) {
_, err = d.readValueLength()
if err != nil {
return
}
if err = binary.Read(d.reader, binary.BigEndian, &res.Height); err != nil {
return
}
if err = binary.Read(d.reader, binary.BigEndian, &res.Width); err != nil {
return
}
if err = binary.Read(d.reader, binary.BigEndian, &res.Depth); err != nil {
return
}
return
}
func (d *AttributeDecoder) readValueLength() (length int16, err error) {
err = binary.Read(d.reader, binary.BigEndian, &length)
return
}
func (d *AttributeDecoder) decodeCollection() (Collection, error) {
// Read the value length (should be 0 for beginCollection)
_, err := d.readValueLength()
if err != nil {
return nil, err
}
collection := make(Collection)
// Read collection members until we hit endCollection
for {
// Read the next tag
var tagByte int8
if err := binary.Read(d.reader, binary.BigEndian, &tagByte); err != nil {
return nil, fmt.Errorf("failed to read tag: %w - was collection %+v", err, collection)
}
// Check if we've reached the end of the collection
if tagByte == TagEndCollection {
// Read and discard the name length and value length for endCollection
if _, err := d.readValueLength(); err != nil {
return nil, err
}
if _, err := d.readValueLength(); err != nil {
return nil, err
}
break
}
// If it's a member name tag, read the member name and value
if tagByte == TagMemberName {
// Read the name length (should be 0 per RFC 3382 Section 7.1)
nameLen, err := d.readValueLength()
if err != nil {
return nil, err
}
if nameLen != 0 {
return nil, fmt.Errorf("memberAttrName should have zero name length, got %d", nameLen)
}
// Read the value length (this is the length of the member attribute name)
memberNameLen, err := d.readValueLength()
if err != nil {
return nil, err
}
// Read the member attribute name from the value field
memberNameBytes := make([]byte, memberNameLen)
if _, err := d.reader.Read(memberNameBytes); err != nil {
return nil, err
}
memberName := string(memberNameBytes)
// Read the next tag which contains the actual value
if err := binary.Read(d.reader, binary.BigEndian, &tagByte); err != nil {
return nil, err
}
// Read the name length (should be 0 after memberName)
if _, err := d.readValueLength(); err != nil {
return nil, err
}
// Decode the value based on its tag
var value interface{}
switch tagByte {
case TagEnum, TagInteger:
value, err = d.decodeInteger()
case TagBoolean:
value, err = d.decodeBool()
case TagDate:
value, err = d.decodeDate()
case TagRange:
value, err = d.decodeRange()
case TagResolution:
value, err = d.decodeResolution()
case TagBeginCollection:
value, err = d.decodeCollection()
default:
value, err = d.decodeString()
}
if err != nil {
return nil, err
}
// Add the attribute to the collection
attr := Attribute{
Tag: tagByte,
Name: memberName,
Value: value,
}
collection[memberName] = append(collection[memberName], attr)
}
}
return collection, nil
}