Debian Codegraph @

decoder.go

raw symbols SCIP
1 /* Go IPP - IPP core protocol implementation in pure Go
2 *
3 * Copyright (C) 2020 and up by Alexander Pevzner ([email protected])
4 * See LICENSE for license terms and conditions
5 *
6 * IPP Message decoder
7 */
8
9 package goipp
10
11 import (
12 "encoding/binary"
13 "errors"
14 "fmt"
15 "io"
16 )
17
18 // Type messageDecoder represents Message decoder
19 type messageDecoder struct {
20 in io.Reader // Input stream
21 off int // Offset of last read
22 cnt int // Count of read bytes
23 }
24
25 // Decode the message
26 func (md *messageDecoder) decode(m *Message) error {
27 // Wire format:
28 //
29 // 2 bytes: Version
30 // 2 bytes: Code (Operation or Status)
31 // 4 bytes: RequestID
32 // variable: attributes
33 // 1 byte: TagEnd
34
35 // Parse message header
36 var err error
37 m.Version, err = md.decodeVersion()
38 if err == nil {
39 m.Code, err = md.decodeCode()
40 }
41 if err == nil {
42 m.RequestID, err = md.decodeU32()
43 }
44
45 // Now parse attributes
46 done := false
47 var group *Attributes
48 var attr Attribute
49 var prev *Attribute
50
51 for err == nil && !done {
52 var tag Tag
53 tag, err = md.decodeTag()
54
55 if tag.IsDelimiter() {
56 prev = nil
57 }
58
59 switch tag {
60 case TagZero:
61 err = errors.New("Invalid tag 0")
62 case TagEnd:
63 done = true
64
65 case TagOperationGroup:
66 group = &m.Operation
67 case TagJobGroup:
68 group = &m.Job
69 case TagPrinterGroup:
70 group = &m.Printer
71 case TagUnsupportedGroup:
72 group = &m.Unsupported
73 case TagSubscriptionGroup:
74 group = &m.Subscription
75 case TagEventNotificationGroup:
76 group = &m.EventNotification
77 case TagResourceGroup:
78 group = &m.Resource
79 case TagDocumentGroup:
80 group = &m.Document
81 case TagSystemGroup:
82 group = &m.System
83 case TagFuture11Group:
84 group = &m.Future11
85 case TagFuture12Group:
86 group = &m.Future12
87 case TagFuture13Group:
88 group = &m.Future13
89 case TagFuture14Group:
90 group = &m.Future14
91 case TagFuture15Group:
92 group = &m.Future15
93
94 default:
95 // Decode attribute
96 if tag == TagMemberName || tag == TagEndCollection {
97 err = fmt.Errorf("Unexpected tag %s", tag)
98 } else {
99 attr, err = md.decodeAttribute(tag)
100 }
101
102 if err == nil && tag == TagBeginCollection {
103 attr.Values[0].V, err = md.decodeCollection()
104 }
105
106 // If everything is OK, save attribute
107 switch {
108 case err != nil:
109 case attr.Name == "":
110 if prev != nil {
111 prev.Values.Add(attr.Values[0].T, attr.Values[0].V)
112 } else {
113 err = errors.New("Additional value without preceding attribute")
114 }
115 case group != nil:
116 group.Add(attr)
117 prev = &(*group)[len(*group)-1]
118 default:
119 err = errors.New("Attribute without a group")
120 }
121 }
122 }
123
124 if err != nil {
125 err = fmt.Errorf("%s at 0x%x", err, md.off)
126 }
127
128 return err
129 }
130
131 // Decode a Collection
132 func (md *messageDecoder) decodeCollection() (Collection, error) {
133 collection := make(Collection, 0)
134 //var name string
135
136 for {
137 tag, err := md.decodeTag()
138 if err != nil {
139 return nil, err
140 }
141
142 // Delimiter cannot be inside a collection
143 if tag.IsDelimiter() {
144 err = fmt.Errorf("collection: unexpected %s", tag)
145 return nil, err
146 }
147
148 // We are about to finish with current attribute (if any),
149 // either because we've got an end of collection, or a next
150 // attribute name. Check that we are leaving the current
151 // attribute in a consistent state (i.e., with at least one value)
152 if tag == TagMemberName || tag == TagEndCollection {
153 l := len(collection)
154 if l > 0 && len(collection[l-1].Values) == 0 {
155 err = fmt.Errorf("collection: unexpected %s, expected value tag", tag)
156 return nil, err
157 }
158 }
159
160 // Fetch next attribute
161 attr, err := md.decodeAttribute(tag)
162 if err != nil {
163 return nil, err
164 }
165
166 // Process next attribute
167 switch {
168 case tag == TagEndCollection:
169 return collection, nil
170
171 case tag == TagMemberName:
172 attr.Name = string(attr.Values[0].V.(String))
173 attr.Values = nil
174
175 if attr.Name == "" {
176 err = fmt.Errorf("collection: %s contains empty attribute name", tag)
177 return nil, err
178 }
179
180 collection = append(collection, attr)
181
182 case len(collection) == 0:
183 // We've got a value without preceding TagMemberName
184 err = fmt.Errorf("collection: unexpected %s, expected %s", tag, TagMemberName)
185 return nil, err
186
187 default:
188 if tag == TagBeginCollection {
189 attr.Values[0].V, err = md.decodeCollection()
190 if err != nil {
191 return nil, err
192 }
193 }
194
195 l := len(collection)
196 collection[l-1].Values.Add(tag, attr.Values[0].V)
197 }
198 }
199 }
200
201 // Decode a tag
202 func (md *messageDecoder) decodeTag() (Tag, error) {
203 t, err := md.decodeU8()
204 return Tag(t), err
205 }
206
207 // Decode a Version
208 func (md *messageDecoder) decodeVersion() (Version, error) {
209 code, err := md.decodeU16()
210 return Version(code), err
211 }
212
213 // Decode a Code
214 func (md *messageDecoder) decodeCode() (Code, error) {
215 code, err := md.decodeU16()
216 return Code(code), err
217 }
218
219 // Decode a single attribute
220 func (md *messageDecoder) decodeAttribute(tag Tag) (Attribute, error) {
221 var attr Attribute
222 var value []byte
223 var err error
224
225 // Obtain attribute name and raw value
226 attr.Name, err = md.decodeString()
227 if err != nil {
228 goto ERROR
229 }
230
231 value, err = md.decodeBytes()
232 if err != nil {
233 goto ERROR
234 }
235
236 // Handle TagExtension
237 if tag == TagExtension {
238 if len(value) < 4 {
239 err = errors.New("Extension tag truncated")
240 goto ERROR
241 }
242
243 t := binary.BigEndian.Uint32(value[:4])
244 value = value[4:]
245
246 if t > 0x7fffffff {
247 err = errors.New("Extension tag out of range")
248 goto ERROR
249 }
250
251 tag = Tag(t)
252 }
253
254 // Unpack value
255 err = attr.unpack(tag, value)
256 if err != nil {
257 goto ERROR
258 }
259
260 return attr, nil
261
262 // Return a error
263 ERROR:
264 return Attribute{}, err
265 }
266
267 // Decode a 8-bit integer
268 func (md *messageDecoder) decodeU8() (uint8, error) {
269 buf := make([]byte, 1)
270 err := md.read(buf)
271 return buf[0], err
272 }
273
274 // Decode a 16-bit integer
275 func (md *messageDecoder) decodeU16() (uint16, error) {
276 buf := make([]byte, 2)
277 err := md.read(buf)
278 return binary.BigEndian.Uint16(buf[:]), err
279 }
280
281 // Decode a 32-bit integer
282 func (md *messageDecoder) decodeU32() (uint32, error) {
283 buf := make([]byte, 4)
284 err := md.read(buf)
285 return binary.BigEndian.Uint32(buf[:]), err
286 }
287
288 // Decode sequence of bytes
289 func (md *messageDecoder) decodeBytes() ([]byte, error) {
290 length, err := md.decodeU16()
291 if err != nil {
292 return nil, err
293 }
294
295 data := make([]byte, length)
296 err = md.read(data)
297 if err != nil {
298 return nil, err
299 }
300
301 return data, nil
302 }
303
304 // Decode string
305 func (md *messageDecoder) decodeString() (string, error) {
306 data, err := md.decodeBytes()
307 if err != nil {
308 return "", err
309 }
310
311 return string(data), nil
312 }
313
314 // Read a piece of raw data from input stream
315 func (md *messageDecoder) read(data []byte) error {
316 md.off = md.cnt
317
318 for len(data) > 0 {
319 n, err := md.in.Read(data)
320 if n > 0 {
321 md.cnt += n
322 data = data[n:]
323 } else if err != nil {
324 md.off = md.cnt
325 return err
326 }
327
328 }
329
330 return nil
331 }