Add collection type, use buffered bytestream.

Fixed some more attribute types.
This commit is contained in:
2021-01-09 17:31:06 +01:00
parent 0805cec129
commit 48aa81238d
5 changed files with 112 additions and 19 deletions

View File

@@ -0,0 +1,74 @@
package ipp
import (
"bufio"
"encoding/binary"
log "github.com/sirupsen/logrus"
)
// Currently the collection data is just consumed and dropped from the bytestream
func consumeCollection(byteStream *bufio.Reader) {
// RFC8010 Section 3.1.6
var length uint16
binary.Read(byteStream, binary.BigEndian, &length)
collectionName := make([]byte, length)
if length > 0 {
binary.Read(byteStream, binary.BigEndian, collectionName)
}
log.Info("collection name " + string(collectionName))
err := binary.Read(byteStream, binary.BigEndian, &length) //Always zero ??
if err != nil {
log.Fatal("error ", err.Error())
}
if length != 0 {
log.Fatal("Should be zero")
}
// Member attributes
done:
for {
var t tag
binary.Read(byteStream, binary.BigEndian, &t)
log.Debug("Collection tag ", t)
switch t {
case endCollectionValueTag:
binary.Read(byteStream, binary.BigEndian, &length)
if length != 0 {
log.Fatal("Should be zero")
}
binary.Read(byteStream, binary.BigEndian, &length)
if length != 0 {
log.Fatal("Should be zero")
}
case memberAttrNameValueTag:
// RFC8010 Section 3.7.1
binary.Read(byteStream, binary.BigEndian, &length) //Always zero ??
if length != 0 {
log.Fatal("Should be zero")
}
binary.Read(byteStream, binary.BigEndian, &length) // Value length
memberName := make([]byte, length)
binary.Read(byteStream, binary.BigEndian, memberName)
log.Debugf("Member name: %v", string(memberName))
var memberValueTag tag
binary.Read(byteStream, binary.BigEndian, &memberValueTag)
log.Debug("Member value tag: ", memberValueTag)
binary.Read(byteStream, binary.BigEndian, &length) //Always zero ??
if length != 0 {
log.Fatal("Should be zero")
}
var memberValueLength uint16
binary.Read(byteStream, binary.BigEndian, &memberValueLength)
memberValue := make([]byte, memberValueLength)
binary.Read(byteStream, binary.BigEndian, memberValue)
log.Debugf("Member Value: % x", memberValue)
default:
// Next tag is one that can not be handled in the collection
// Put it back in the byte stream and return to main loop
byteStream.UnreadByte()
break done
}
}
log.Debug("Collection done")
}

View File

@@ -2,6 +2,7 @@
package ipp package ipp
import ( import (
"bufio"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io" "io"
@@ -81,6 +82,7 @@ const (
type printerState int32 type printerState int32
// printerstate defenitions
const ( const (
Idle printerState = 3 Idle printerState = 3
Processing printerState = 4 Processing printerState = 4
@@ -89,6 +91,7 @@ const (
type statusCode uint16 type statusCode uint16
// status code defenitions
const ( const (
SuccessfulOk statusCode = 0x0000 SuccessfulOk statusCode = 0x0000
ClientErrorBadRequest statusCode = 0x0400 ClientErrorBadRequest statusCode = 0x0400
@@ -180,11 +183,11 @@ func (a *attributes) addAttribute(group tag, attr Attribute) {
} }
} }
func UnMarshalAttributues(body io.Reader) *attributes { func UnMarshalAttributes(bytestream *bufio.Reader) *attributes {
a := new(attributes) a := new(attributes)
var t tag var t tag
err := binary.Read(body, binary.BigEndian, &t) err := binary.Read(bytestream, binary.BigEndian, &t)
if err != nil { if err != nil {
log.Error(err.Error()) log.Error(err.Error())
} }
@@ -197,9 +200,9 @@ func UnMarshalAttributues(body io.Reader) *attributes {
var lastAddValuer AddValuer var lastAddValuer AddValuer
for { for {
err = binary.Read(body, binary.BigEndian, &t) err = binary.Read(bytestream, binary.BigEndian, &t)
if err != nil { if err != nil {
log.Errorf("End of input before end of attributes tag (%v)", err.Error()) log.Fatal("End of input before end of attributes tag (%v)", err.Error())
} }
log.Debugf("Value tag - %v", t) log.Debugf("Value tag - %v", t)
switch t { switch t {
@@ -207,21 +210,25 @@ func UnMarshalAttributues(body io.Reader) *attributes {
return a return a
case charsetValueTag: case charsetValueTag:
c := NewCharSetValue("", "") c := NewCharSetValue("", "")
c.unmarshal(body) c.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, c) a.addAttribute(currentAttributeGroup, c)
log.Debugf("%v %v", c.name, c.value) log.Debugf("%v %v", c.name, c.value)
case booleanValueTag:
na := NewBoolean("", false)
na.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, na)
case uriValueTag: case uriValueTag:
u := NewURIValue("", "") u := NewURIValue("", "")
u.unmarshal(body) u.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, u) a.addAttribute(currentAttributeGroup, u)
log.Debugf("%v %v", u.name, u.value) log.Debugf("%v %v", u.name, u.value)
case naturalLanguageValueTag: case naturalLanguageValueTag:
n := NewNaturalLanguage("", "") n := NewNaturalLanguage("", "")
n.unmarshal(body) n.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, n) a.addAttribute(currentAttributeGroup, n)
log.Debugf("%v %v", n.name, n.value) log.Debugf("%v %v", n.name, n.value)
case keyWordValueTag: case keyWordValueTag:
name, value := unmarshalSingleValue(body) name, value := unmarshalSingleValue(bytestream)
if name == "" { if name == "" {
lastAddValuer.addValue(value) lastAddValuer.addValue(value)
} else { } else {
@@ -232,11 +239,16 @@ func UnMarshalAttributues(body io.Reader) *attributes {
log.Debugf("%v : %v", name, value) log.Debugf("%v : %v", name, value)
case nameWithoutLanguageValueTag: case nameWithoutLanguageValueTag:
n := NewNameWithoutLanguage("", "") n := NewNameWithoutLanguage("", "")
n.unmarshal(body) n.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, n) a.addAttribute(currentAttributeGroup, n)
log.Debugf("%v %v", n.name, n.value) log.Debugf("%v %v", n.name, n.value)
case textWithoutLanguageValueTag:
attr := NewtextWithoutLanguage("", "")
attr.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, attr)
log.Debugf("%v %v", attr.name, attr.value)
case mimeMediaTypeValueTag: case mimeMediaTypeValueTag:
name, value := unmarshalSingleValue(body) name, value := unmarshalSingleValue(bytestream)
if name == "" { if name == "" {
lastAddValuer.addValue(value) lastAddValuer.addValue(value)
} else { } else {
@@ -246,7 +258,7 @@ func UnMarshalAttributues(body io.Reader) *attributes {
} }
log.Debugf("%v : %v", name, value) log.Debugf("%v : %v", name, value)
case integerValueTag: case integerValueTag:
name, value := unmarshalSingleInteger(body) name, value := unmarshalSingleInteger(bytestream)
if name == "" { if name == "" {
lastAddValuer.addValue(value) lastAddValuer.addValue(value)
} else { } else {
@@ -256,7 +268,7 @@ func UnMarshalAttributues(body io.Reader) *attributes {
} }
log.Debugf("%v : %v", name, value) log.Debugf("%v : %v", name, value)
case rangeOfIntegerValueTag: case rangeOfIntegerValueTag:
name, value := unmarshalSingleRangeOfInteger(body) name, value := unmarshalSingleRangeOfInteger(bytestream)
if name == "" { if name == "" {
lastAddValuer.addValue(value) lastAddValuer.addValue(value)
} else { } else {
@@ -266,7 +278,7 @@ func UnMarshalAttributues(body io.Reader) *attributes {
} }
log.Debugf("%v : %v", name, value) log.Debugf("%v : %v", name, value)
case enumValueTag: case enumValueTag:
name, value := unmarshalSingleInteger(body) name, value := unmarshalSingleInteger(bytestream)
if name == "" { if name == "" {
lastAddValuer.addValue(value) lastAddValuer.addValue(value)
} else { } else {
@@ -275,6 +287,9 @@ func UnMarshalAttributues(body io.Reader) *attributes {
lastAddValuer = e lastAddValuer = e
} }
log.Debugf("%v : %v", name, value) log.Debugf("%v : %v", name, value)
case begCollectionValueTag:
// For now just consume the collection
consumeCollection(bytestream)
case jobAttributes: case jobAttributes:
log.Debug("Start job attributes") log.Debug("Start job attributes")
currentAttributeGroup = jobAttributes currentAttributeGroup = jobAttributes
@@ -286,11 +301,11 @@ func UnMarshalAttributues(body io.Reader) *attributes {
currentAttributeGroup = operationAttributes currentAttributeGroup = operationAttributes
case resolutionValueTag: case resolutionValueTag:
res := NewResolution("", 0, 0) res := NewResolution("", 0, 0)
res.unmarshal(body) res.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, res) a.addAttribute(currentAttributeGroup, res)
log.Debugf("Resolution %v", res) log.Debugf("Resolution %v", res)
default: default:
log.Errorf("Unsupported tag %v", t) log.Errorf("Unsupported tag %v (%x)", t, uint8(t))
} }
} }
} }

View File

@@ -1,6 +1,7 @@
package ipp package ipp
import ( import (
"bufio"
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
@@ -55,9 +56,10 @@ func (r Request) String() string {
} }
func (r *Request) UnMarshal(body io.Reader) { func (r *Request) UnMarshal(body io.Reader) {
r.header.unmarshal(body) buffbody := bufio.NewReader(body)
r.header.unmarshal(buffbody)
//log.Printf("Header %v", r.header) //log.Printf("Header %v", r.header)
r.a = UnMarshalAttributues(body) r.a = UnMarshalAttributes(buffbody)
} }
func (r *Request) RequestID() uint32 { func (r *Request) RequestID() uint32 {

View File

@@ -1,6 +1,7 @@
package ipp package ipp
import ( import (
"bufio"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io" "io"
@@ -75,9 +76,10 @@ func (r *Response) Marshal() []byte {
} }
func (r *Response) UnMarshal(body io.Reader) { func (r *Response) UnMarshal(body io.Reader) {
r.header.unmarshal(body) buffbody := bufio.NewReader(body)
r.header.unmarshal(buffbody)
//log.Printf("Header %v", r.header) //log.Printf("Header %v", r.header)
r.a = UnMarshalAttributues(body) r.a = UnMarshalAttributes(buffbody)
} }
func (r *Response) AddPrinterAttribute(a Attribute) { func (r *Response) AddPrinterAttribute(a Attribute) {