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
|
#pragma once
#include <stdint.h>
#include <cstdint>
#include "cbor.h"
namespace cbor {
static auto parse_stdstring(CborValue* val, std::string* out) -> CborError {
uint8_t* buf;
size_t len;
CborError err = cbor_value_dup_byte_string(val, &buf, &len, NULL);
if (err != CborNoError) {
return err;
}
*out = std::move(std::string(buf, len));
free(buf);
return err
}
class ArrayDecoder {
public:
static auto Create(uint8_t* buffer, size_t buffer_len)
-> cpp::result<std::unique_ptr<ArrayDecoder>, CborError>;
static auto Create(CborValue& root)
-> cpp::result<std::unique_ptr<ArrayDecoder>, CborError>;
template <typename T>
auto NextValue() -> cpp::result<T, CborError>;
template <>
auto NextValue() -> cpp::result<int64_t, CborError> {
return NextValue(&cbor_value_is_integer, &cbor_value_get_int);
}
template <>
auto NextValue() -> cpp::result<uint64_t, CborError> {
return NextValue(&cbor_value_is_unsigned_integer, &cbor_value_get_uint64);
}
template <>
auto NextValue() -> cpp::result<std::string, CborError> {
return NextValue(&cbor_value_is_byte_string, &parse_stdstring);
}
template <typename T>
auto NextValue(bool (*is_valid)(CborValue*),
CborError (*parse)(CborValue*, T*))
-> cpp::result<T, CborError> {
if (error_ != CborNoError) {
return cpp::fail(error_);
}
if (!is_valid(&it_)) {
error_ = CborErrorIllegalType;
return cpp::fail(error_);
}
T ret;
error_ = parse(&it_, &ret);
if (error_ != CborNoError) {
return cpp::fail(error_);
}
error_ = cbor_value_advance(&it_);
if (error_ != CborNoError) {
return cpp::fail(error_);
}
return ret;
}
auto Failed() -> CborError { return error_; }
auto Iterator() -> CborValue& { return it_; }
ArrayDecoder(const ArrayDecoder&) = delete;
ArrayDecoder& operator=(const ArrayDecoder&) = delete;
private:
CborParser parser_;
CborValue root_;
CborValue it_;
CborError error_ = CborNoError;
};
class MapDecoder {
public:
static auto Create(uint8_t* buffer, size_t buffer_len)
-> cpp::result<std::unique_ptr<MapDecoder>, CborError>;
static auto Create(CborValue& root)
-> cpp::result<std::unique_ptr<MapDecoder>, CborError>;
template <typename T>
auto FindValue(const std::string& key) -> std::optional<T>;
template <>
auto FindValue(const std::string& key) -> std::optional<int64_t> {
return FindValue(key, &cbor_value_is_integer, &cbor_value_get_int);
}
template <>
auto FindValue(const std::string& key) -> std::optional<uint64_t> {
return FindValue(key, &cbor_value_is_unsigned_integer,
&cbor_value_get_uint64);
}
template <>
auto FindValue(const std::string& key) -> std::optional<std::string> {
return FindValue(key, &cbor_value_is_byte_string, &parse_stdstring);
}
template <typename T>
auto FindValue(const std::string& key,
bool (*is_valid)(CborValue*),
CborError (*parse)(CborValue*, T*)) -> std::optional<T> {
if (error_ != CborNoError) {
return {};
}
if (cbor_value_map_find_value(&it_, key.c_str(), &val) != CborNoError) {
return {};
}
if (!is_valid(&val)) {
error_ = CborErrorIllegalType;
return {};
}
T ret;
error_ = parse(&val, &ret);
if (error_ != CborNoError) {
return cpp::fail(error_);
}
return ret;
}
auto Failed() -> CborError { return error_; }
MapDecoder(const MapDecoder&) = delete;
MapDecoder& operator=(const MapDecoder&) = delete;
private:
CborParser parser_;
CborValue root_;
CborValue it_;
CborError error_ = CborNoError;
};
} // namespace cbor
|