Simplify unmarshalling.
This commit is contained in:
56
packages/ipp/setofintegers.go
Normal file
56
packages/ipp/setofintegers.go
Normal file
@@ -0,0 +1,56 @@
|
||||
// Copyright 2021, Henrik Sölver henrik.solver@gmail.com
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
package ipp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// setOfIntegers is a helper type that is used to handle integer types
|
||||
// which only differs in tag value.
|
||||
type setOfIntegers struct {
|
||||
name string
|
||||
values []int32
|
||||
}
|
||||
|
||||
func unmarshalIntegers(byteStream *bufio.Reader, valueTag tag) (*setOfIntegers, error) {
|
||||
|
||||
s := new(setOfIntegers)
|
||||
s.values = make([]int32, 0, 1)
|
||||
|
||||
var length uint16
|
||||
binary.Read(byteStream, binary.BigEndian, &length)
|
||||
name := make([]byte, length)
|
||||
if length > 0 {
|
||||
binary.Read(byteStream, binary.BigEndian, name)
|
||||
}
|
||||
s.name = string(name)
|
||||
binary.Read(byteStream, binary.BigEndian, &length)
|
||||
if length != 4 {
|
||||
return nil, fmt.Errorf("wrong value-length of integer attribute %v", length)
|
||||
}
|
||||
var value int32 //valueBytes := make([]byte, length)
|
||||
for length > 0 {
|
||||
err := binary.Read(byteStream, binary.BigEndian, &value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.values = append(s.values, value)
|
||||
next, err := byteStream.Peek(3)
|
||||
if err != nil {
|
||||
// end of byte stream
|
||||
break
|
||||
}
|
||||
if next[0] != byte(valueTag) || next[1] != 0x00 || next[2] != 0x00 {
|
||||
// End of attribute
|
||||
break
|
||||
}
|
||||
// Remove the value tag with the zero length from the stream
|
||||
byteStream.Discard(3)
|
||||
binary.Read(byteStream, binary.BigEndian, &length)
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
Reference in New Issue
Block a user