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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
/* 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 decoder
*/
package goipp
import (
"encoding/binary"
"errors"
"fmt"
"io"
)
// DecoderOptions represents message decoder options
type DecoderOptions struct {
// EnableWorkarounds, if set to true, enables various workarounds
// for decoding IPP messages that violate IPP protocol specification
//
// Currently it includes the following workarounds:
// * Pantum M7300FDW violates collection encoding rules.
// Instead of using TagMemberName, it uses named attributes
// within the collection
//
// The list of implemented workarounds may grow in the
// future
EnableWorkarounds bool
}
// messageDecoder represents Message decoder
type messageDecoder struct {
in io.Reader // Input stream
off int // Offset of last read
cnt int // Count of read bytes
opt DecoderOptions // Options
}
// Decode the message
func (md *messageDecoder) decode(m *Message) error {
// Wire format:
//
// 2 bytes: Version
// 2 bytes: Code (Operation or Status)
// 4 bytes: RequestID
// variable: attributes
// 1 byte: TagEnd
// Parse message header
var err error
m.Version, err = md.decodeVersion()
if err == nil {
m.Code, err = md.decodeCode()
}
if err == nil {
m.RequestID, err = md.decodeU32()
}
// Now parse attributes
done := false
var group *Attributes
var attr Attribute
var prev *Attribute
for err == nil && !done {
var tag Tag
tag, err = md.decodeTag()
if err != nil {
break
}
if tag.IsDelimiter() {
prev = nil
}
if tag.IsGroup() {
m.Groups.Add(Group{tag, nil})
}
switch tag {
case TagZero:
err = errors.New("Invalid tag 0")
case TagEnd:
done = true
case TagOperationGroup:
group = &m.Operation
case TagJobGroup:
group = &m.Job
case TagPrinterGroup:
group = &m.Printer
case TagUnsupportedGroup:
group = &m.Unsupported
case TagSubscriptionGroup:
group = &m.Subscription
case TagEventNotificationGroup:
group = &m.EventNotification
case TagResourceGroup:
group = &m.Resource
case TagDocumentGroup:
group = &m.Document
case TagSystemGroup:
group = &m.System
case TagFuture11Group:
group = &m.Future11
case TagFuture12Group:
group = &m.Future12
case TagFuture13Group:
group = &m.Future13
case TagFuture14Group:
group = &m.Future14
case TagFuture15Group:
group = &m.Future15
default:
// Decode attribute
if tag == TagMemberName || tag == TagEndCollection {
err = fmt.Errorf("Unexpected tag %s", tag)
} else {
attr, err = md.decodeAttribute(tag)
}
if err == nil && tag == TagBeginCollection {
attr.Values[0].V, err = md.decodeCollection()
}
// If everything is OK, save attribute
switch {
case err != nil:
case attr.Name == "":
if prev != nil {
prev.Values.Add(attr.Values[0].T, attr.Values[0].V)
// Append value to the last Attribute of the
// last Group in the m.Groups
//
// Note, if we are here, this last Attribute definitely exists,
// because:
// * prev != nil
// * prev is set when new named attribute is added
// * prev is reset when delimiter tag is encountered
gLast := &m.Groups[len(m.Groups)-1]
aLast := &gLast.Attrs[len(gLast.Attrs)-1]
aLast.Values.Add(attr.Values[0].T, attr.Values[0].V)
} else {
err = errors.New("Additional value without preceding attribute")
}
case group != nil:
group.Add(attr)
prev = &(*group)[len(*group)-1]
m.Groups[len(m.Groups)-1].Add(attr)
default:
err = errors.New("Attribute without a group")
}
}
}
if err != nil {
err = fmt.Errorf("%s at 0x%x", err, md.off)
}
return err
}
// Decode a Collection
//
// Collection is like a nested object - an attribute which value is a sequence
// of named attributes. Collections can be nested.
//
// Wire format:
// ATTR: Tag = TagBeginCollection, - the outer attribute that
// Name = "name", value - ignored contains the collection
//
// ATTR: Tag = TagMemberName, name = "", - member name \
// value - string, name of the next |
// member | repeated for
// | each member
// ATTR: Tag = any attribute tag, name = "", - repeated for |
// value = member value multi-value /
// members
//
// ATTR: Tag = TagEndCollection, name = "",
// value - ignored
//
// The format looks a bit baroque, but please note that it was added
// in the IPP 2.0. For IPP 1.x collection looks like a single multi-value
// TagBeginCollection attribute (attributes without names considered
// next value for the previously defined named attributes) and so
// 1.x parser silently ignores collections and doesn't get confused
// with them.
func (md *messageDecoder) decodeCollection() (Collection, error) {
collection := make(Collection, 0)
memberName := ""
for {
tag, err := md.decodeTag()
if err != nil {
return nil, err
}
// Delimiter cannot be inside a collection
if tag.IsDelimiter() {
err = fmt.Errorf("Collection: unexpected tag %s", tag)
return nil, err
}
// Check for TagMemberName without the subsequent value attribute
if (tag == TagMemberName || tag == TagEndCollection) && memberName != "" {
err = fmt.Errorf("Collection: unexpected %s, expected value tag", tag)
return nil, err
}
// Fetch next attribute
attr, err := md.decodeAttribute(tag)
if err != nil {
return nil, err
}
// Process next attribute
switch tag {
case TagEndCollection:
return collection, nil
case TagMemberName:
memberName = string(attr.Values[0].V.(String))
if memberName == "" {
err = fmt.Errorf("Collection: %s value is empty", tag)
return nil, err
}
case TagBeginCollection:
// Decode nested collection
attr.Values[0].V, err = md.decodeCollection()
if err != nil {
return nil, err
}
fallthrough
default:
if md.opt.EnableWorkarounds &&
memberName == "" && attr.Name != "" {
// Workaround for: Pantum M7300FDW
//
// This device violates collection encoding rules.
// Instead of using TagMemberName, it uses named
// attributes within the collection
memberName = attr.Name
}
if memberName != "" {
attr.Name = memberName
collection = append(collection, attr)
memberName = ""
} else if len(collection) > 0 {
l := len(collection)
collection[l-1].Values.Add(tag, attr.Values[0].V)
} else {
// We've got a value without preceding TagMemberName
err = fmt.Errorf("Collection: unexpected %s, expected %s", tag, TagMemberName)
return nil, err
}
}
}
}
// Decode a tag
func (md *messageDecoder) decodeTag() (Tag, error) {
t, err := md.decodeU8()
return Tag(t), err
}
// Decode a Version
func (md *messageDecoder) decodeVersion() (Version, error) {
code, err := md.decodeU16()
return Version(code), err
}
// Decode a Code
func (md *messageDecoder) decodeCode() (Code, error) {
code, err := md.decodeU16()
return Code(code), err
}
// Decode a single attribute
//
// Wire format:
// 1 byte: Tag
// 2+N bytes: Name length (2 bytes) + name string
// 2+N bytes: Value length (2 bytes) + value bytes
//
// For the extended tag format, Tag is encoded as TagExtension and
// 4 bytes of the actual tag value prepended to the value bytes
func (md *messageDecoder) decodeAttribute(tag Tag) (Attribute, error) {
var attr Attribute
var value []byte
var err error
// Obtain attribute name and raw value
attr.Name, err = md.decodeString()
if err != nil {
goto ERROR
}
value, err = md.decodeBytes()
if err != nil {
goto ERROR
}
// Handle TagExtension
if tag == TagExtension {
if len(value) < 4 {
err = errors.New("Extension tag truncated")
goto ERROR
}
t := binary.BigEndian.Uint32(value[:4])
value = value[4:]
if t > 0x7fffffff {
err = errors.New("Extension tag out of range")
goto ERROR
}
tag = Tag(t)
}
// Unpack value
err = attr.unpack(tag, value)
if err != nil {
goto ERROR
}
return attr, nil
// Return a error
ERROR:
return Attribute{}, err
}
// Decode a 8-bit integer
func (md *messageDecoder) decodeU8() (uint8, error) {
buf := make([]byte, 1)
err := md.read(buf)
return buf[0], err
}
// Decode a 16-bit integer
func (md *messageDecoder) decodeU16() (uint16, error) {
buf := make([]byte, 2)
err := md.read(buf)
return binary.BigEndian.Uint16(buf[:]), err
}
// Decode a 32-bit integer
func (md *messageDecoder) decodeU32() (uint32, error) {
buf := make([]byte, 4)
err := md.read(buf)
return binary.BigEndian.Uint32(buf[:]), err
}
// Decode sequence of bytes
func (md *messageDecoder) decodeBytes() ([]byte, error) {
length, err := md.decodeU16()
if err != nil {
return nil, err
}
data := make([]byte, length)
err = md.read(data)
if err != nil {
return nil, err
}
return data, nil
}
// Decode string
func (md *messageDecoder) decodeString() (string, error) {
data, err := md.decodeBytes()
if err != nil {
return "", err
}
return string(data), nil
}
// Read a piece of raw data from input stream
func (md *messageDecoder) read(data []byte) error {
md.off = md.cnt
for len(data) > 0 {
n, err := md.in.Read(data)
if n > 0 {
md.cnt += n
data = data[n:]
} else {
md.off = md.cnt
if err == nil || err == io.EOF {
err = errors.New("Message truncated")
}
return err
}
}
return nil
}
|