96 lines
2.3 KiB
Go
96 lines
2.3 KiB
Go
package ipp
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"encoding/binary"
|
|
"fmt"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// Resolution represents a ipp resolution attribute
|
|
type Resolution struct {
|
|
CrossFeedResolution int32
|
|
FeedResolution int32
|
|
Unit int8 // 3 seems to mean dpi (rfc3805)
|
|
}
|
|
|
|
// SetOfResolutions represents a set ipp resolution attributes
|
|
type SetOfResolutions struct {
|
|
name string
|
|
sor []Resolution
|
|
}
|
|
|
|
// NewSetOfResolution creats a new set of ipp resolution attributes
|
|
func NewSetOfResolution(name string, resSet ...Resolution) *SetOfResolutions {
|
|
r := new(SetOfResolutions)
|
|
r.name = name
|
|
r.sor = resSet
|
|
return r
|
|
}
|
|
|
|
func (r *SetOfResolutions) unmarshal(byteStream *bufio.Reader) {
|
|
var length uint16
|
|
var res Resolution
|
|
for {
|
|
binary.Read(byteStream, binary.BigEndian, &length)
|
|
attributeName := make([]byte, length)
|
|
if length > 0 {
|
|
binary.Read(byteStream, binary.BigEndian, attributeName)
|
|
r.name = string(attributeName)
|
|
}
|
|
binary.Read(byteStream, binary.BigEndian, &length)
|
|
if length != 9 {
|
|
panic("Wrong length in resolution")
|
|
}
|
|
binary.Read(byteStream, binary.BigEndian, &res.CrossFeedResolution)
|
|
binary.Read(byteStream, binary.BigEndian, &res.FeedResolution)
|
|
binary.Read(byteStream, binary.BigEndian, &res.Unit)
|
|
r.sor = append(r.sor, res)
|
|
var t tag
|
|
err := binary.Read(byteStream, binary.BigEndian, &t)
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
}
|
|
if t != resolutionValueTag {
|
|
byteStream.UnreadByte()
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// Name returns the name of the attribute
|
|
func (r SetOfResolutions) Name() string {
|
|
return r.name
|
|
}
|
|
|
|
func (r SetOfResolutions) String() string {
|
|
return fmt.Sprintf("%v:%v", r.name, r.sor)
|
|
}
|
|
|
|
func (r *SetOfResolutions) marshal() []byte {
|
|
|
|
b := make([]byte, 0, 14+len(r.name)+14*(len(r.sor)-1))
|
|
buf := bytes.NewBuffer(b)
|
|
|
|
for i, res := range r.sor {
|
|
buf.WriteByte(byte(resolutionValueTag))
|
|
if i == 0 {
|
|
binary.Write(buf, binary.BigEndian, uint16(len(r.name)))
|
|
buf.WriteString(r.name)
|
|
} else {
|
|
binary.Write(buf, binary.BigEndian, uint16(0))
|
|
}
|
|
binary.Write(buf, binary.BigEndian, uint16(9))
|
|
binary.Write(buf, binary.BigEndian, int32(res.CrossFeedResolution))
|
|
binary.Write(buf, binary.BigEndian, int32(res.FeedResolution))
|
|
buf.WriteByte(byte(res.Unit))
|
|
}
|
|
return buf.Bytes()
|
|
}
|
|
|
|
func (r *SetOfResolutions) valueTag() tag {
|
|
return resolutionValueTag
|
|
}
|