-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathrequest_decoder_state_machine.go
More file actions
162 lines (139 loc) · 4.63 KB
/
request_decoder_state_machine.go
File metadata and controls
162 lines (139 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package ipp
import (
"encoding/binary"
"fmt"
"io"
)
type requestDecoderState int
const (
requestDecoderStateInitial requestDecoderState = iota
requestDecoderStateAttributeGroup
requestDecoderStateAttribute
requestDecoderStateData
)
func (r requestDecoderState) String() string {
return requestStateName[r]
}
var requestStateName = map[requestDecoderState]string{
requestDecoderStateInitial: "initial",
requestDecoderStateAttribute: "attribute",
requestDecoderStateAttributeGroup: "attribute-group",
requestDecoderStateData: "data",
}
type requestDecoderStateMachine struct {
state requestDecoderState
currentAttributeGroupTag int8
lastAttributeGroupTag int8
currentAttributes map[string]any
currentAttributeName string
}
func newRequestStateMachine() *requestDecoderStateMachine {
return &requestDecoderStateMachine{
state: requestDecoderStateInitial,
}
}
func (r *requestDecoderStateMachine) Decode(reader io.Reader, data io.Writer) (*Request, error) {
request := &Request{
OperationAttributes: make(map[string]any),
PrinterAttributes: make(map[string]any),
JobAttributes: make(map[string]any),
}
attributeDecoder := NewAttributeDecoder(reader)
/*
-----------------------------------------------
| version-number | 2 bytes - required
-----------------------------------------------
| operation-id (request) |
| or | 2 bytes - required
| status-code (request) |
-----------------------------------------------
| request-id | 4 bytes - required
-----------------------------------------------
| attribute-group | n bytes - 0 or more
-----------------------------------------------
| end-of-attributes-tag | 1 byte - required
-----------------------------------------------
| data | q bytes - optional
-----------------------------------------------
*/
b := make([]byte, 1)
for {
switch r.state {
case requestDecoderStateInitial:
if err := binary.Read(reader, binary.BigEndian, &request.ProtocolVersionMajor); err != nil {
return nil, err
}
if err := binary.Read(reader, binary.BigEndian, &request.ProtocolVersionMinor); err != nil {
return nil, err
}
if err := binary.Read(reader, binary.BigEndian, &request.Operation); err != nil {
return nil, err
}
if err := binary.Read(reader, binary.BigEndian, &request.RequestId); err != nil {
return nil, err
}
// read first attribute group tag
if _, err := reader.Read(b); err != nil {
return nil, err
}
r.setAttributeGroupTag(int8(b[0]))
r.state = requestDecoderStateAttributeGroup
case requestDecoderStateAttributeGroup:
if r.lastAttributeGroupTag > 0 && len(r.currentAttributes) > 0 {
appendAttributeToRequest(request, r.lastAttributeGroupTag, r.currentAttributes)
}
switch r.currentAttributeGroupTag {
case TagDelimiterEnd:
r.state = requestDecoderStateData
continue
case TagDelimiterOperation, TagDelimiterPrinter, TagDelimiterJob:
r.currentAttributes = make(map[string]any)
default:
return nil, fmt.Errorf("unsupported attribute group: 0x%02x", r.currentAttributeGroupTag)
}
r.state = requestDecoderStateAttribute
case requestDecoderStateAttribute:
if _, err := reader.Read(b); err != nil {
return nil, err
}
if b[0] < 0x10 {
// new attribute group not attribute
r.setAttributeGroupTag(int8(b[0]))
r.state = requestDecoderStateAttributeGroup
continue
}
attrib, err := attributeDecoder.Decode(int8(b[0]))
if err != nil {
return nil, err
}
// save attribute name for optional additional values
if attrib.Name != "" {
r.currentAttributeName = attrib.Name
}
// TODO FIX: handle attributes with array values
// append attribute to list
r.currentAttributes[r.currentAttributeName] = attrib.Value
case requestDecoderStateData:
if data != nil {
if _, err := io.Copy(data, reader); err != nil {
return nil, err
}
}
return request, nil
}
}
}
func (r *requestDecoderStateMachine) setAttributeGroupTag(tag int8) {
r.lastAttributeGroupTag = r.currentAttributeGroupTag
r.currentAttributeGroupTag = tag
}
func appendAttributeToRequest(req *Request, tag int8, attributes map[string]any) {
switch tag {
case TagDelimiterOperation:
req.OperationAttributes = attributes
case TagDelimiterPrinter:
req.PrinterAttributes = attributes
case TagDelimiterJob:
req.JobAttributes = attributes
}
}