// Copyright 2021, Henrik Sölver henrik.solver@gmail.com // SPDX-License-Identifier: BSD-3-Clause package ipp import ( "bytes" "encoding/binary" "fmt" "io" ) // Boolean is the ipp attribute boolean type Boolean struct { name string value bool } // NewBoolean creates a nre boolean attribute func NewBoolean(name string, value bool) *Boolean { b := new(Boolean) b.name = name b.value = value return b } // Name gets tha name of the boolean attribute func (b Boolean) Name() string { return b.name } func (b Boolean) String() string { return b.name + ":" + fmt.Sprint(b.value) } func (b *Boolean) valueTag() tag { return booleanValueTag } func unmarshalSingleAttribute(byteStream io.Reader) (string, []byte) { var length uint16 binary.Read(byteStream, binary.BigEndian, &length) attributeName := make([]byte, length) if length > 0 { binary.Read(byteStream, binary.BigEndian, attributeName) } binary.Read(byteStream, binary.BigEndian, &length) attributeValue := make([]byte, length) binary.Read(byteStream, binary.BigEndian, attributeValue) return string(attributeName), attributeValue } func (b *Boolean) unmarshal(byteStream io.Reader) { var length uint16 binary.Read(byteStream, binary.BigEndian, &length) attributeName := make([]byte, length) if length > 0 { binary.Read(byteStream, binary.BigEndian, attributeName) } b.name = string(attributeName) binary.Read(byteStream, binary.BigEndian, &length) data := make([]byte, length) binary.Read(byteStream, binary.BigEndian, data) //name, data := unmarshalSingleAttribute(byteStream) if data[0] == 0 { b.value = false return } b.value = true } func (b *Boolean) marshal() []byte { l := 5 + len(b.name) ba := make([]byte, 0, l) buf := bytes.NewBuffer(ba) buf.WriteByte(byte(booleanValueTag)) binary.Write(buf, binary.BigEndian, uint16(len(b.name))) buf.WriteString(b.name) binary.Write(buf, binary.BigEndian, uint16(1)) if b.value { buf.WriteByte(byte(1)) } else { buf.WriteByte(byte(0)) } return buf.Bytes() }