| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
package goipp |
| 10 |
|
| 11 |
import ( |
| 12 |
"encoding/binary" |
| 13 |
"errors" |
| 14 |
"fmt" |
| 15 |
"io" |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
type messageDecoder struct { |
| 20 |
in io.Reader |
| 21 |
off int |
| 22 |
cnt int |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
func (md *messageDecoder) decode(m *Message) error { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
var err error |
| 37 |
m.Version, err = md.decodeVersion() |
| 38 |
if err == nil { |
| 39 |
m.Code, err = md.decodeCode() |
| 40 |
} |
| 41 |
if err == nil { |
| 42 |
m.RequestID, err = md.decodeU32() |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
done := false |
| 47 |
var group *Attributes |
| 48 |
var attr Attribute |
| 49 |
var prev *Attribute |
| 50 |
|
| 51 |
for err == nil && !done { |
| 52 |
var tag Tag |
| 53 |
tag, err = md.decodeTag() |
| 54 |
|
| 55 |
if tag.IsDelimiter() { |
| 56 |
prev = nil |
| 57 |
} |
| 58 |
|
| 59 |
switch tag { |
| 60 |
case TagZero: |
| 61 |
err = errors.New("Invalid tag 0") |
| 62 |
case TagEnd: |
| 63 |
done = true |
| 64 |
|
| 65 |
case TagOperationGroup: |
| 66 |
group = &m.Operation |
| 67 |
case TagJobGroup: |
| 68 |
group = &m.Job |
| 69 |
case TagPrinterGroup: |
| 70 |
group = &m.Printer |
| 71 |
case TagUnsupportedGroup: |
| 72 |
group = &m.Unsupported |
| 73 |
case TagSubscriptionGroup: |
| 74 |
group = &m.Subscription |
| 75 |
case TagEventNotificationGroup: |
| 76 |
group = &m.EventNotification |
| 77 |
case TagResourceGroup: |
| 78 |
group = &m.Resource |
| 79 |
case TagDocumentGroup: |
| 80 |
group = &m.Document |
| 81 |
case TagSystemGroup: |
| 82 |
group = &m.System |
| 83 |
case TagFuture11Group: |
| 84 |
group = &m.Future11 |
| 85 |
case TagFuture12Group: |
| 86 |
group = &m.Future12 |
| 87 |
case TagFuture13Group: |
| 88 |
group = &m.Future13 |
| 89 |
case TagFuture14Group: |
| 90 |
group = &m.Future14 |
| 91 |
case TagFuture15Group: |
| 92 |
group = &m.Future15 |
| 93 |
|
| 94 |
default: |
| 95 |
|
| 96 |
if tag == TagMemberName || tag == TagEndCollection { |
| 97 |
err = fmt.Errorf("Unexpected tag %s", tag) |
| 98 |
} else { |
| 99 |
attr, err = md.decodeAttribute(tag) |
| 100 |
} |
| 101 |
|
| 102 |
if err == nil && tag == TagBeginCollection { |
| 103 |
attr.Values[0].V, err = md.decodeCollection() |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
switch { |
| 108 |
case err != nil: |
| 109 |
case attr.Name == "": |
| 110 |
if prev != nil { |
| 111 |
prev.Values.Add(attr.Values[0].T, attr.Values[0].V) |
| 112 |
} else { |
| 113 |
err = errors.New("Additional value without preceding attribute") |
| 114 |
} |
| 115 |
case group != nil: |
| 116 |
group.Add(attr) |
| 117 |
prev = &(*group)[len(*group)-1] |
| 118 |
default: |
| 119 |
err = errors.New("Attribute without a group") |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
if err != nil { |
| 125 |
err = fmt.Errorf("%s at 0x%x", err, md.off) |
| 126 |
} |
| 127 |
|
| 128 |
return err |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
func (md *messageDecoder) decodeCollection() (Collection, error) { |
| 133 |
collection := make(Collection, 0) |
| 134 |
|
| 135 |
|
| 136 |
for { |
| 137 |
tag, err := md.decodeTag() |
| 138 |
if err != nil { |
| 139 |
return nil, err |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
if tag.IsDelimiter() { |
| 144 |
err = fmt.Errorf("collection: unexpected %s", tag) |
| 145 |
return nil, err |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
if tag == TagMemberName || tag == TagEndCollection { |
| 153 |
l := len(collection) |
| 154 |
if l > 0 && len(collection[l-1].Values) == 0 { |
| 155 |
err = fmt.Errorf("collection: unexpected %s, expected value tag", tag) |
| 156 |
return nil, err |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
attr, err := md.decodeAttribute(tag) |
| 162 |
if err != nil { |
| 163 |
return nil, err |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
switch { |
| 168 |
case tag == TagEndCollection: |
| 169 |
return collection, nil |
| 170 |
|
| 171 |
case tag == TagMemberName: |
| 172 |
attr.Name = string(attr.Values[0].V.(String)) |
| 173 |
attr.Values = nil |
| 174 |
|
| 175 |
if attr.Name == "" { |
| 176 |
err = fmt.Errorf("collection: %s contains empty attribute name", tag) |
| 177 |
return nil, err |
| 178 |
} |
| 179 |
|
| 180 |
collection = append(collection, attr) |
| 181 |
|
| 182 |
case len(collection) == 0: |
| 183 |
|
| 184 |
err = fmt.Errorf("collection: unexpected %s, expected %s", tag, TagMemberName) |
| 185 |
return nil, err |
| 186 |
|
| 187 |
default: |
| 188 |
if tag == TagBeginCollection { |
| 189 |
attr.Values[0].V, err = md.decodeCollection() |
| 190 |
if err != nil { |
| 191 |
return nil, err |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
l := len(collection) |
| 196 |
collection[l-1].Values.Add(tag, attr.Values[0].V) |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
func (md *messageDecoder) decodeTag() (Tag, error) { |
| 203 |
t, err := md.decodeU8() |
| 204 |
return Tag(t), err |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
func (md *messageDecoder) decodeVersion() (Version, error) { |
| 209 |
code, err := md.decodeU16() |
| 210 |
return Version(code), err |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
func (md *messageDecoder) decodeCode() (Code, error) { |
| 215 |
code, err := md.decodeU16() |
| 216 |
return Code(code), err |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
func (md *messageDecoder) decodeAttribute(tag Tag) (Attribute, error) { |
| 221 |
var attr Attribute |
| 222 |
var value []byte |
| 223 |
var err error |
| 224 |
|
| 225 |
|
| 226 |
attr.Name, err = md.decodeString() |
| 227 |
if err != nil { |
| 228 |
goto ERROR |
| 229 |
} |
| 230 |
|
| 231 |
value, err = md.decodeBytes() |
| 232 |
if err != nil { |
| 233 |
goto ERROR |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
if tag == TagExtension { |
| 238 |
if len(value) < 4 { |
| 239 |
err = errors.New("Extension tag truncated") |
| 240 |
goto ERROR |
| 241 |
} |
| 242 |
|
| 243 |
t := binary.BigEndian.Uint32(value[:4]) |
| 244 |
value = value[4:] |
| 245 |
|
| 246 |
if t > 0x7fffffff { |
| 247 |
err = errors.New("Extension tag out of range") |
| 248 |
goto ERROR |
| 249 |
} |
| 250 |
|
| 251 |
tag = Tag(t) |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
err = attr.unpack(tag, value) |
| 256 |
if err != nil { |
| 257 |
goto ERROR |
| 258 |
} |
| 259 |
|
| 260 |
return attr, nil |
| 261 |
|
| 262 |
|
| 263 |
ERROR: |
| 264 |
return Attribute{}, err |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
func (md *messageDecoder) decodeU8() (uint8, error) { |
| 269 |
buf := make([]byte, 1) |
| 270 |
err := md.read(buf) |
| 271 |
return buf[0], err |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
func (md *messageDecoder) decodeU16() (uint16, error) { |
| 276 |
buf := make([]byte, 2) |
| 277 |
err := md.read(buf) |
| 278 |
return binary.BigEndian.Uint16(buf[:]), err |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
func (md *messageDecoder) decodeU32() (uint32, error) { |
| 283 |
buf := make([]byte, 4) |
| 284 |
err := md.read(buf) |
| 285 |
return binary.BigEndian.Uint32(buf[:]), err |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
func (md *messageDecoder) decodeBytes() ([]byte, error) { |
| 290 |
length, err := md.decodeU16() |
| 291 |
if err != nil { |
| 292 |
return nil, err |
| 293 |
} |
| 294 |
|
| 295 |
data := make([]byte, length) |
| 296 |
err = md.read(data) |
| 297 |
if err != nil { |
| 298 |
return nil, err |
| 299 |
} |
| 300 |
|
| 301 |
return data, nil |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
func (md *messageDecoder) decodeString() (string, error) { |
| 306 |
data, err := md.decodeBytes() |
| 307 |
if err != nil { |
| 308 |
return "", err |
| 309 |
} |
| 310 |
|
| 311 |
return string(data), nil |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
func (md *messageDecoder) read(data []byte) error { |
| 316 |
md.off = md.cnt |
| 317 |
|
| 318 |
for len(data) > 0 { |
| 319 |
n, err := md.in.Read(data) |
| 320 |
if n > 0 { |
| 321 |
md.cnt += n |
| 322 |
data = data[n:] |
| 323 |
} else if err != nil { |
| 324 |
md.off = md.cnt |
| 325 |
return err |
| 326 |
} |
| 327 |
|
| 328 |
} |
| 329 |
|
| 330 |
return nil |
| 331 |
} |