|
19 | 19 | #include <arpa/inet.h> |
20 | 20 |
|
21 | 21 | using namespace std; |
22 | | -void cbor_variant::construct_from_into(const std::vector<uint8_t>& in, cbor_variant* dest) |
| 22 | +cbor_variant cbor_variant::construct_from(const std::vector<uint8_t>& in) |
23 | 23 | { |
24 | 24 | unsigned int dummy_offset=0; |
25 | | - return construct_from_into(in, dest, &dummy_offset); |
| 25 | + return construct_from(in, &dummy_offset); |
26 | 26 | } |
27 | 27 |
|
28 | | -void cbor_variant::construct_from_into(const std::vector<uint8_t>& in, cbor_variant* dest, unsigned int* offset) |
| 28 | +cbor_variant cbor_variant::construct_from(const std::vector<uint8_t>& in, unsigned int* offset) |
29 | 29 | { |
30 | 30 | // nothing to read? |
31 | 31 | if (in.size()<=*offset) throw length_error("No header byte while decoding cbor"); |
@@ -35,16 +35,8 @@ void cbor_variant::construct_from_into(const std::vector<uint8_t>& in, cbor_vari |
35 | 35 |
|
36 | 36 | // integers |
37 | 37 | switch (h->major) { |
38 | | - case 0: { |
39 | | - dest->emplace<integer>(read_integer_header(in, h, offset)); // +ve integer |
40 | | - return; |
41 | | - } |
42 | | - |
43 | | - case 1: { |
44 | | - dest->emplace<integer>(-1-read_integer_header(in, h, offset)); // -ve integer |
45 | | - return; |
46 | | - } |
47 | | - |
| 38 | + case 0: return cbor_variant { read_integer_header(in, h, offset) }; |
| 39 | + case 1: return cbor_variant { -1-read_integer_header(in, h, offset) }; |
48 | 40 | case 2: // bytes and strings |
49 | 41 | case 3: { |
50 | 42 | int length=read_integer_header(in, h, offset); |
@@ -53,26 +45,22 @@ void cbor_variant::construct_from_into(const std::vector<uint8_t>& in, cbor_vari |
53 | 45 | *offset+=static_cast<unsigned int>(length); |
54 | 46 | if (in.size()<*offset) throw length_error("Insufficient data bytes while decoding cbor"); |
55 | 47 | if (h->major==2) |
56 | | - dest->emplace<bytes>(vector<uint8_t>(&in[offset_at_begin], &in[offset_at_begin+static_cast<unsigned int>(length)])); |
| 48 | + return cbor_variant { vector<uint8_t>(&in[offset_at_begin], &in[offset_at_begin+static_cast<unsigned int>(length)]) }; |
57 | 49 | else |
58 | | - dest->emplace<unicode_string>(&in[offset_at_begin], &in[offset_at_begin+static_cast<unsigned int>(length)]); |
59 | | - return; |
| 50 | + return cbor_variant { string(&in[offset_at_begin], &in[offset_at_begin+static_cast<unsigned int>(length)]) }; |
60 | 51 | } |
61 | 52 |
|
62 | 53 | case 4: { // arrays |
63 | 54 | int total_items=read_integer_header(in, h, offset); |
64 | | - dest->emplace<array>(static_cast<unsigned>(total_items), cbor_variant()); |
65 | | - cbor_array* current_array=&get<array>(*dest); |
| 55 | + cbor_variant rtn=cbor_variant { cbor_array(static_cast<unsigned>(total_items), cbor_variant()) }; |
66 | 56 | for (unsigned int this_item=0; this_item<static_cast<unsigned>(total_items); this_item++) { |
67 | | - cbor_variant* value_dest=¤t_array->at(this_item); |
68 | | - construct_from_into(in, value_dest, offset); |
| 57 | + get<cbor_array>(rtn)[this_item]=construct_from(in, offset); |
69 | 58 | } |
70 | | - return; |
| 59 | + return rtn; |
71 | 60 | } |
72 | 61 |
|
73 | 62 | case 5: { // maps |
74 | | - dest->emplace<map>(); |
75 | | - cbor_map* current_map=&get<map>(*dest); |
| 63 | + cbor_variant rtn=cbor_variant { cbor_map() }; |
76 | 64 | for (int pending_items=read_integer_header(in, h, offset); pending_items>0; pending_items--) { |
77 | 65 | // get the key |
78 | 66 | h=reinterpret_cast<const header*>(&in[*offset]); |
@@ -83,38 +71,33 @@ void cbor_variant::construct_from_into(const std::vector<uint8_t>& in, cbor_vari |
83 | 71 | *offset+=static_cast<unsigned int>(key_length); |
84 | 72 |
|
85 | 73 | // create the variant |
86 | | - current_map->emplace(key, cbor_variant()); |
87 | | - cbor_variant* value_dest=¤t_map->at(key); |
88 | | - construct_from_into(in, value_dest, offset); |
| 74 | + get<cbor_map>(rtn)[key]=construct_from(in, offset); |
89 | 75 | } |
90 | | - return; |
| 76 | + return rtn; |
91 | 77 | } |
92 | 78 |
|
93 | 79 | case 6: { // tags (are ignored) |
94 | | - read_integer_header(in, h, offset); |
95 | | - return; |
| 80 | + read_integer_header(in, h, offset); // skip |
| 81 | + return construct_from(in, offset); |
96 | 82 | } |
97 | 83 |
|
98 | 84 | case 7: { // floats and none |
99 | 85 | const uint8_t* first_data_byte=in.data()+*offset+1; |
100 | 86 | if (h->additional==26) { // single precision |
101 | 87 | *offset+=5; |
102 | 88 | float rtn; |
103 | | - float_to_big_endian(first_data_byte, reinterpret_cast<uint8_t*>(&rtn), 4); |
104 | | - dest->emplace<floating_point>(rtn); |
105 | | - return; |
| 89 | + float_to_big_endian(first_data_byte, reinterpret_cast<uint8_t*>(&rtn)); |
| 90 | + return cbor_variant { rtn }; |
106 | 91 | } |
107 | | - if (h->additional==27) { // double precision, gets cast down to single |
| 92 | + if (h->additional==27) { // double precision |
108 | 93 | *offset+=9; |
109 | 94 | double rtn; |
110 | | - float_to_big_endian(first_data_byte, reinterpret_cast<uint8_t*>(&rtn), 8); |
111 | | - dest->emplace<floating_point>(static_cast<float>(rtn)); |
112 | | - return; |
| 95 | + double_to_big_endian(first_data_byte, reinterpret_cast<uint8_t*>(&rtn)); |
| 96 | + return cbor_variant { rtn }; |
113 | 97 | } |
114 | 98 | if (h->additional==22) { |
115 | 99 | *offset+=1; |
116 | | - dest->emplace<none>(); |
117 | | - return; |
| 100 | + return cbor_variant { monostate() }; |
118 | 101 | } |
119 | 102 | throw runtime_error("Asked to process a major type 7 that is neither a float nor a double"); |
120 | 103 | } |
@@ -138,13 +121,13 @@ void cbor_variant::encode_onto(std::vector<uint8_t>* in) const |
138 | 121 |
|
139 | 122 | // https://tools.ietf.org/html/rfc7049#section-2.3 |
140 | 123 | case floating_point: { // floats |
141 | | - header h(7, 26); |
| 124 | + header h(7, 27); |
142 | 125 | h.append_onto(in); |
143 | | - float val=get<floating_point>(*this); |
144 | | - float big_endian; |
| 126 | + double val=get<floating_point>(*this); |
| 127 | + double big_endian; |
145 | 128 | uint8_t* p_big_endian=reinterpret_cast<uint8_t*>(&big_endian); |
146 | | - float_to_big_endian(reinterpret_cast<uint8_t*>(&val), p_big_endian, sizeof(float)); |
147 | | - in->insert(in->end(), p_big_endian, p_big_endian+sizeof(float)); |
| 129 | + double_to_big_endian(reinterpret_cast<uint8_t*>(&val), p_big_endian); |
| 130 | + in->insert(in->end(), p_big_endian, p_big_endian+sizeof(double)); |
148 | 131 | return; |
149 | 132 | } |
150 | 133 |
|
@@ -268,23 +251,40 @@ int cbor_variant::read_integer_header(const std::vector<uint8_t>& in, const head |
268 | 251 | } |
269 | 252 | } |
270 | 253 |
|
271 | | -void cbor_variant::float_to_big_endian(const uint8_t* p_src, uint8_t* p_dest, unsigned int bytes) |
| 254 | +void cbor_variant::float_to_big_endian(const uint8_t* p_src, uint8_t* p_dest) |
272 | 255 | { |
273 | 256 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
274 | | - p_dest+=bytes-1; |
275 | | - while (bytes>0) { |
276 | | - *p_dest=*p_src; |
277 | | - ++p_src; |
278 | | - --p_dest; |
279 | | - --bytes; |
280 | | - } |
| 257 | + *p_dest++=p_src[3]; |
| 258 | + *p_dest++=p_src[2]; |
| 259 | + *p_dest++=p_src[1]; |
| 260 | + *p_dest =p_src[0]; |
281 | 261 | #else |
282 | | - while (bytes>0) { |
283 | | - *p_dest=*p_src; |
284 | | - ++p_src; |
285 | | - ++p_dest; |
286 | | - --bytes; |
287 | | - } |
288 | | -} |
| 262 | + *p_dest++=*p_src++; |
| 263 | + *p_dest++=*p_src++; |
| 264 | + *p_dest++=*p_src++; |
| 265 | + *p_dest =*p_src; |
| 266 | +#endif |
| 267 | +}; |
| 268 | + |
| 269 | +void cbor_variant::double_to_big_endian(const uint8_t* p_src, uint8_t* p_dest) |
| 270 | +{ |
| 271 | +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 272 | + *p_dest++=p_src[7]; |
| 273 | + *p_dest++=p_src[6]; |
| 274 | + *p_dest++=p_src[5]; |
| 275 | + *p_dest++=p_src[4]; |
| 276 | + *p_dest++=p_src[3]; |
| 277 | + *p_dest++=p_src[2]; |
| 278 | + *p_dest++=p_src[1]; |
| 279 | + *p_dest =p_src[0]; |
| 280 | +#else |
| 281 | + *p_dest++=*p_src++; |
| 282 | + *p_dest++=*p_src++; |
| 283 | + *p_dest++=*p_src++; |
| 284 | + *p_dest++=*p_src++; |
| 285 | + *p_dest++=*p_src++; |
| 286 | + *p_dest++=*p_src++; |
| 287 | + *p_dest++=*p_src++; |
| 288 | + *p_dest =*p_src; |
289 | 289 | #endif |
290 | 290 | }; |