-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathencoder.go
239 lines (199 loc) · 4.8 KB
/
encoder.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
/* Go IPP - IPP core protocol implementation in pure Go
*
* Copyright (C) 2020 and up by Alexander Pevzner (pzz@apevzner.com)
* See LICENSE for license terms and conditions
*
* IPP Message encoder
*/
package goipp
import (
"errors"
"fmt"
"io"
"math"
)
// Type messageEncoder represents Message encoder
type messageEncoder struct {
out io.Writer // Output stream
}
// Encode the message
func (me *messageEncoder) encode(m *Message) error {
// Wire format:
//
// 2 bytes: Version
// 2 bytes: Code (Operation or Status)
// 4 bytes: RequestID
// variable: attributes
// 1 byte: TagEnd
// Encode message header
var err error
err = me.encodeU16(uint16(m.Version))
if err == nil {
err = me.encodeU16(uint16(m.Code))
}
if err == nil {
err = me.encodeU32(uint32(m.RequestID))
}
// Encode attributes
for _, grp := range m.attrGroups() {
err = me.encodeTag(grp.Tag)
if err == nil {
for _, attr := range grp.Attrs {
if attr.Name == "" {
err = errors.New("Attribute without name")
} else {
err = me.encodeAttr(attr, true)
}
}
}
if err != nil {
break
}
}
if err == nil {
err = me.encodeTag(TagEnd)
}
return err
}
// Encode attribute
func (me *messageEncoder) encodeAttr(attr Attribute, checkTag bool) error {
// Wire format
// 1 byte: Tag
// 2 bytes: len(Name)
// variable: name
// 2 bytes: len(Value)
// variable Value
//
// And each additional value comes as attribute
// without name
if len(attr.Values) == 0 {
return errors.New("Attribute without value")
}
name := attr.Name
for _, val := range attr.Values {
tag := val.T
if checkTag {
if tag.IsDelimiter() || tag == TagMemberName || tag == TagEndCollection {
return fmt.Errorf("Tag %s cannot be used with value", tag)
}
if uint(tag)&0x80000000 != 0 {
return fmt.Errorf("Tag %s exceeds extension tag range", tag)
}
}
var err error
if tag >= 0x100 {
err = me.encodeTag(TagExtension)
} else {
err = me.encodeTag(tag)
}
if err != nil {
return err
}
err = me.encodeName(name)
if err != nil {
return err
}
err = me.encodeValue(val.T, val.V)
if err != nil {
return err
}
name = "" // Each additional value comes without name
}
return nil
}
// Encode 8-bit integer
func (me *messageEncoder) encodeU8(v uint8) error {
return me.write([]byte{v})
}
// Encode 16-bit integer
func (me *messageEncoder) encodeU16(v uint16) error {
return me.write([]byte{byte(v >> 8), byte(v)})
}
// Encode 32-bit integer
func (me *messageEncoder) encodeU32(v uint32) error {
return me.write([]byte{byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)})
}
// Encode Tag
func (me *messageEncoder) encodeTag(tag Tag) error {
return me.encodeU8(byte(tag))
}
// Encode Attribute name
func (me *messageEncoder) encodeName(name string) error {
if len(name) > math.MaxInt16 {
return fmt.Errorf("Attribute name exceeds %d bytes",
math.MaxInt16)
}
err := me.encodeU16(uint16(len(name)))
if err == nil {
err = me.write([]byte(name))
}
return err
}
// Encode Attribute value
func (me *messageEncoder) encodeValue(tag Tag, v Value) error {
// Check Value type vs the Tag
tagType := tag.Type()
if tagType == TypeVoid {
v = Void{} // Ignore supplied value
} else if tagType != v.Type() {
return fmt.Errorf("Tag %s: %s value required, %s present",
tag, tagType, v.Type())
}
// Encode the value
//
// If tag >= 0x100, tag is replaced with TagExtension, and actual
// tag value prepended to the data bytes. See RFC 8010, 3.5.2 for
// details
data, err := v.encode()
if err != nil {
return err
}
valueLen := len(data)
if tag >= 0x100 {
valueLen += 4 // Prepend extension tag value to the data
}
if valueLen > math.MaxInt16 {
return fmt.Errorf("Attribute value exceeds %d bytes",
math.MaxInt16)
}
err = me.encodeU16(uint16(valueLen))
if err == nil && tag >= 0x100 {
err = me.encodeU32(uint32(tag))
}
if err == nil {
err = me.write(data)
}
// Handle collection
if collection, ok := v.(Collection); ok {
return me.encodeCollection(tag, collection)
}
return err
}
// Encode collection
func (me *messageEncoder) encodeCollection(tag Tag, collection Collection) error {
for _, attr := range collection {
if attr.Name == "" {
return errors.New("Collection member without name")
}
attrName := MakeAttribute("", TagMemberName, String(attr.Name))
err := me.encodeAttr(attrName, false)
if err == nil {
err = me.encodeAttr(Attribute{Name: "", Values: attr.Values}, true)
}
if err != nil {
return err
}
}
return me.encodeAttr(MakeAttribute("", TagEndCollection, Void{}), false)
}
// Write a piece of raw data to output stream
func (me *messageEncoder) write(data []byte) error {
for len(data) > 0 {
n, err := me.out.Write(data)
if err != nil {
return err
}
data = data[n:]
}
return nil
}