-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathipp_frame_builder.cc
368 lines (347 loc) · 11.9 KB
/
ipp_frame_builder.cc
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "libipp/ipp_frame_builder.h"
#include <algorithm>
#include "libipp/ipp_attribute.h"
#include "libipp/ipp_encoding.h"
namespace ipp {
namespace {
// Saves 1-,2- or 4-bytes integer to buf using two's-complement binary encoding.
// The "buf" parameter is always resized to BytesCount. Returns false when
// given integer is out of range. In this case 0 is saved to buf.
template <size_t BytesCount>
bool SaveInteger(int v, std::vector<uint8_t>* buf) {
buf->resize(BytesCount);
uint8_t* ptr = buf->data();
if (!WriteInteger<BytesCount>(&ptr, v)) {
buf->clear();
buf->resize(BytesCount, 0);
return false;
}
return true;
}
// Saves simple string to buf, buf is resized accordingly.
void SaveOctetString(const std::string& s, std::vector<uint8_t>* buf) {
buf->assign(s.begin(), s.end());
}
// Writes textWithLanguage/nameWithLanguage (see [rfc8010]) to buf, which is
// resized accordingly. Returns false if given string(s) is too long. In this
// case an empty string is saved to the buffer.
bool SaveStringWithLanguage(const ipp::StringWithLanguage& s,
std::vector<uint8_t>* buf) {
buf->clear();
buf->resize(4 + s.value.size() + s.language.size());
uint8_t* ptr = buf->data();
if (!WriteInteger<2>(&ptr, s.language.size())) {
std::vector<uint8_t> empty_buffer(4, 0);
buf->swap(empty_buffer);
return false;
}
ptr = std::copy(s.language.begin(), s.language.end(), ptr);
if (!WriteInteger<2>(&ptr, s.value.size())) {
std::vector<uint8_t> empty_buffer(4, 0);
buf->swap(empty_buffer);
return false;
}
std::copy(s.value.begin(), s.value.end(), ptr);
return true;
}
// Saves dateTime (see [rfc8010]) to buf, which is resized accordingly. Returns
// false when the given dateTime is incorrect; in this case |buf| is set to
// 2000/1/1 00:00:00 +00:00.
bool SaveDateTime(const ipp::DateTime& v, std::vector<uint8_t>* buf) {
buf->resize(11);
uint8_t* ptr = buf->data();
if (WriteInteger<2>(&ptr, v.year) && WriteInteger<1>(&ptr, v.month) &&
WriteInteger<1>(&ptr, v.day) && WriteInteger<1>(&ptr, v.hour) &&
WriteInteger<1>(&ptr, v.minutes) && WriteInteger<1>(&ptr, v.seconds) &&
WriteInteger<1>(&ptr, v.deci_seconds) &&
WriteInteger<1>(&ptr, v.UTC_direction) &&
WriteInteger<1>(&ptr, v.UTC_hours) &&
WriteInteger<1>(&ptr, v.UTC_minutes))
return true;
buf->clear();
buf->resize(11, 0);
ptr = buf->data();
WriteInteger<int16_t>(&ptr, 2000);
(*buf)[8] = '+';
return false;
}
// Writes resolution (see [rfc8010]) to buf, which is resized accordingly.
// Returns false when given value is incorrect; in this case |buf| is set to
// 256x256dpi.
bool SaveResolution(const ipp::Resolution& v, std::vector<uint8_t>* buf) {
buf->resize(9);
uint8_t* ptr = buf->data();
if (WriteInteger<4>(&ptr, v.xres) && WriteInteger<4>(&ptr, v.yres) &&
(v.units == ipp::Resolution::kDotsPerCentimeter ||
v.units == ipp::Resolution::kDotsPerInch)) {
WriteInteger<int8_t>(&ptr, v.units);
return true;
}
buf->clear();
// set to 256x256 dpi
buf->resize(9, 0);
(*buf)[2] = (*buf)[6] = 1;
(*buf)[8] = ipp::Resolution::kDotsPerInch;
return false;
}
// Writes rangeOfInteger (see [rfc8010]) to buf, which is resized accordingly.
// Returns false when given value is incorrect; in this case |buf| is set to 0.
bool SaveRangeOfInteger(const ipp::RangeOfInteger& v,
std::vector<uint8_t>* buf) {
buf->resize(8);
uint8_t* ptr = buf->data();
if (WriteInteger<4>(&ptr, v.min_value) && WriteInteger<4>(&ptr, v.max_value))
return true;
buf->clear();
buf->resize(8, 0);
return false;
}
} // namespace
void FrameBuilder::LogFrameBuilderError(const std::string& message) {
Log l;
l.message = "Error when building frame: " + message + ".";
errors_->push_back(l);
}
void FrameBuilder::SaveAttrValue(Attribute* attr,
size_t index,
uint8_t* tag,
std::vector<uint8_t>* buf) {
*tag = static_cast<uint8_t>(attr->GetType());
bool result = true;
switch (attr->GetType()) {
case AttrType::boolean: {
int v = 0;
attr->GetValue(&v, index);
if (v != 0)
v = 1;
result = SaveInteger<1>(v, buf);
break;
}
case AttrType::integer:
case AttrType::enum_: {
int v = 0;
attr->GetValue(&v, index);
result = SaveInteger<4>(v, buf);
break;
}
case AttrType::dateTime: {
DateTime v;
attr->GetValue(&v, index);
result = SaveDateTime(v, buf);
break;
}
case AttrType::resolution: {
Resolution v;
attr->GetValue(&v, index);
result = SaveResolution(v, buf);
break;
}
case AttrType::rangeOfInteger: {
RangeOfInteger v;
attr->GetValue(&v, index);
result = SaveRangeOfInteger(v, buf);
break;
}
case AttrType::text: {
StringWithLanguage s;
attr->GetValue(&s, index);
if (s.language.empty()) {
*tag = textWithoutLanguage_value_tag;
SaveOctetString(s, buf);
} else {
result = SaveStringWithLanguage(s, buf);
}
} break;
case AttrType::name: {
StringWithLanguage s;
attr->GetValue(&s, index);
if (s.language.empty()) {
*tag = nameWithoutLanguage_value_tag;
SaveOctetString(s, buf);
} else {
result = SaveStringWithLanguage(s, buf);
}
} break;
case AttrType::octetString:
case AttrType::keyword:
case AttrType::uri:
case AttrType::uriScheme:
case AttrType::charset:
case AttrType::naturalLanguage:
case AttrType::mimeMediaType: {
std::string s = "";
attr->GetValue(&s, index);
SaveOctetString(s, buf);
break;
}
default:
LogFrameBuilderError(
"Internal error: cannot recognize value type of the attribute " +
attr->GetName());
buf->clear();
break;
}
if (!result)
LogFrameBuilderError("Incorrect value of the attribute " + attr->GetName() +
". Default value was written instead");
}
void FrameBuilder::SaveCollection(Collection* coll,
std::list<TagNameValue>* data_chunks) {
// get list of all attributes
std::vector<Attribute*> attrs = coll->GetAllAttributes();
// save the attributes
for (Attribute* attr : attrs) {
if (attr->GetState() == AttrState::unset)
continue;
TagNameValue tnv;
tnv.tag = memberAttrName_value_tag;
tnv.name.clear();
SaveOctetString(attr->GetName(), &tnv.value);
data_chunks->push_back(tnv);
if (attr->GetState() != AttrState::set) {
// out-of-band value (tag)
tnv.tag = static_cast<uint8_t>(attr->GetState());
tnv.value.clear();
data_chunks->push_back(tnv);
} else {
// standard values (one or more)
for (size_t val_index = 0; val_index < attr->GetSize(); ++val_index) {
if (attr->GetType() == AttrType::collection) {
tnv.tag = begCollection_value_tag;
tnv.value.clear();
data_chunks->push_back(tnv);
SaveCollection(attr->GetCollection(val_index), data_chunks);
tnv.tag = endCollection_value_tag;
tnv.value.clear();
} else {
SaveAttrValue(attr, val_index, &tnv.tag, &tnv.value);
}
data_chunks->push_back(tnv);
}
}
}
}
void FrameBuilder::SaveGroup(Collection* coll,
std::list<TagNameValue>* data_chunks) {
// get list of all attributes
std::vector<Attribute*> attrs = coll->GetAllAttributes();
// save the attributes
for (Attribute* attr : attrs) {
if (attr->GetState() == AttrState::unset)
continue;
TagNameValue tnv;
SaveOctetString(attr->GetName(), &tnv.name);
if (attr->GetState() != AttrState::set) {
tnv.tag = static_cast<uint8_t>(attr->GetState());
tnv.value.clear();
data_chunks->push_back(tnv);
continue;
}
for (size_t val_index = 0; val_index < attr->GetSize(); ++val_index) {
if (attr->GetType() == AttrType::collection) {
tnv.tag = begCollection_value_tag;
tnv.value.clear();
data_chunks->push_back(tnv);
SaveCollection(attr->GetCollection(val_index), data_chunks);
tnv.tag = endCollection_value_tag;
tnv.name.clear();
tnv.value.clear();
} else {
SaveAttrValue(attr, val_index, &tnv.tag, &tnv.value);
}
data_chunks->push_back(tnv);
tnv.name.clear();
}
}
}
void FrameBuilder::BuildFrameFromPackage(Package* package) {
frame_->groups_tags_.clear();
frame_->groups_content_.clear();
// save frame data
std::vector<Group*> groups = package->GetAllGroups();
for (Group* grp : groups) {
for (size_t i = 0; true; ++i) {
Collection* coll = grp->GetCollection(i);
if (coll == nullptr)
break;
frame_->groups_tags_.push_back(static_cast<uint8_t>(grp->GetName()));
frame_->groups_content_.resize(frame_->groups_tags_.size());
SaveGroup(coll, &(frame_->groups_content_.back()));
// skip if it this is a single empty group
if (frame_->groups_content_.back().empty() && !grp->IsASet()) {
frame_->groups_tags_.pop_back();
frame_->groups_content_.pop_back();
}
}
}
frame_->data_ = package->Data();
}
bool FrameBuilder::WriteFrameToBuffer(uint8_t* ptr) {
bool error_in_header = true;
if (!WriteInteger<1>(&ptr, frame_->major_version_number_)) {
LogFrameBuilderError("major-version-number is out of range");
} else if (!WriteInteger<1>(&ptr, frame_->minor_version_number_)) {
LogFrameBuilderError("minor-version-number is out of range");
} else if (!WriteInteger<2>(&ptr, frame_->operation_id_or_status_code_)) {
LogFrameBuilderError("operation-id or status-code is out of range");
} else if (!WriteInteger<4>(&ptr, frame_->request_id_)) {
LogFrameBuilderError("request-id is out of range");
} else {
error_in_header = false;
}
if (error_in_header)
return false;
for (size_t i = 0; i < frame_->groups_tags_.size(); ++i) {
if (frame_->groups_tags_[i] > max_begin_attribute_group_tag) {
LogFrameBuilderError("begin-attribute-group-tag is out of range");
return false;
}
// write a group to the buffer
if (!WriteInteger<1>(&ptr, frame_->groups_tags_[i]) ||
!WriteTNVsToBuffer(frame_->groups_content_[i], &ptr))
return false;
}
WriteInteger<int8_t>(&ptr, end_of_attributes_tag);
std::copy(frame_->data_.begin(), frame_->data_.end(), ptr);
return true;
}
bool FrameBuilder::WriteTNVsToBuffer(const std::list<TagNameValue>& tnvs,
uint8_t** ptr) {
for (auto& tnv : tnvs) {
if (!WriteInteger<1>(ptr, tnv.tag)) {
LogFrameBuilderError("value-tag is out of range");
return false;
}
if (!WriteInteger<2>(ptr, tnv.name.size())) {
LogFrameBuilderError("name-length is out of range");
return false;
}
*ptr = std::copy(tnv.name.begin(), tnv.name.end(), *ptr);
if (!WriteInteger<2>(ptr, tnv.value.size())) {
LogFrameBuilderError("value-length is out of range");
return false;
}
*ptr = std::copy(tnv.value.begin(), tnv.value.end(), *ptr);
}
return true;
}
std::size_t FrameBuilder::GetFrameLength() const {
// Header has always 8 bytes (ipp_version + operation_id/status + request_id).
size_t length = 8;
// The header is followed by a list of groups.
for (const auto& tnvs : frame_->groups_content_) {
// Each group starts with 1-byte group-tag ...
length += 1;
// ... and consists of list of tag-name-value.
for (const auto& tnv : tnvs)
// Tag + name_size + name + value_size + value.
length += (1 + 2 + tnv.name.size() + 2 + tnv.value.size());
}
// end-of-attributes-tag + blob_with_data.
length += 1 + frame_->data_.size();
return length;
}
} // namespace ipp