summaryrefslogtreecommitdiff
path: root/src/cbor/cbor_decoder.cpp
blob: eb43e1633b876241d6ac2e8e9043d4b9f427a8df (plain)
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
#include "cbor_decoder.hpp"
#include <cstdint>
#include "esp-idf/components/cbor/tinycbor/src/cbor.h"
#include "include/cbor_decoder.hpp"

namespace cbor {

static auto ArrayDecoder::Create(uint8_t *buffer, size_t buffer_len) -> cpp::result<std::unique_ptr<ArrayDecoder>, CborError> {
  auto decoder = std::make_unique<ArrayDecoder>();
  cbor_parser_init(buffer, buffer_len, &decoder->parser_, &decoder->root_);
  if (!cbor_value_is_array(&decoder->root_)) {
    return cpp::fail(CborErrorIllegalType);
  }
  CborError err = cbor_value_enter_container(&decoder->root_, &decoder->it_);
  if (err != CborNoError) {
    return cpp::fail(err);
  }
  return std::move(decoder);
}


auto ArrayDecoder::ParseString() -> cpp::result<std::string, CborError> {
  if (error_ != CborNoError) {
    return cpp::fail(error_);
  }

  if (!cbor_value_is_byte_string(&it_)) {
    error_ = CborErrorIllegalType;
    return cpp::fail(error_);
  }
  uint8_t *buf; size_t len; CborValue new_val;
  error_ = cbor_value_dup_byte_string(&it_, &buf, &len, &new_val);
  if (error_ != CborNoError) {
    return cpp::fail(error_);
  }
  std::string ret(buf, len);
  free(buf);
  val_ = new_val;
  return ret;
}

auto ArrayDecoder::ParseUnsigned() -> cpp::result<uint32_t, CborError> {
  if (error_ != CborNoError) {
    return cpp::fail(error_);
  }

  if (!cbor_value_is_unsigned_integer(&it_)) {
    error_ = CborErrorIllegalType;
    return cpp::fail(error_);
  }
  uint64_t ret;
  error_ = cbor_value_get_uint64(&it_, &ret);
  if (error_ != CborNoError) {
    return cpp::fail(error_);
  }
  error_ = cbor_value_advance(&it_);
  if (error_ != CborNoError) {
    return cpp::fail(error_);
  }
  return ret;
}

auto ArrayDecoder::ParseSigned() -> cpp::result<int32_t, CborError> {
  if (error_ != CborNoError) {
    return cpp::fail(error_);
  }
  if (!cbor_value_is_unsigned_integer(&it_)) {
    error_ = CborErrorIllegalType;
    return cpp::fail(error_);
  }
  uint64_t ret;
  error_ = cbor_value_get_uint64(&it_, &ret);
  if (error_ != CborNoError) {
    return cpp::fail(error_);
  }
  error_ = cbor_value_advance(&it_);
  if (error_ != CborNoError) {
    return cpp::fail(error_);
  }
  return ret;
}

static auto MapDecoder::Create(uint8_t *buffer, size_t buffer_len) -> cpp::result<std::unique_ptr<MapDecoder>, CborError> {
  auto decoder = std::make_unique<MapDecoder>();
  cbor_parser_init(buffer, buffer_len, &decoder->parser_, &decoder->root_);
  if (!cbor_value_is_map(&decoder->root_)) {
    return cpp::fail(CborErrorIllegalType);
  }
  CborError err = cbor_value_enter_container(&decoder->root_, &decoder->it_);
  if (err != CborNoError) {
    return cpp::fail(err);
  }
  return std::move(decoder);
}

auto MapDecoder::FindString(const std::string &key) -> std::optional<std::string> {
  CborValue val;
  if (error_ != CborNoError) {
    return {};
  }
  if (cbor_value_map_find_value(&it_, key.c_str(), &val) != CborNoError) {
    return {};
  }
  if (!cbor_value_is_byte_string(&val)) {
    error_ = CborErrorIllegalType;
    return {};
  }
  uint8_t *buf; size_t len;
  error_ = cbor_value_dup_byte_string(&val, &buf, &len, NULL);
  if (error_ != CborNoError) {
    return cpp::fail(error_);
  }
  std::string ret(buf, len);
  free(buf);
  return ret;
}

auto MapDecoder::FindUnsigned(const std::string &key) -> std::optional<uint32_t> {
  CborValue val;
  if (error_ != CborNoError) {
    return {};
  }
  if (cbor_value_map_find_value(&it_, key.c_str(), &val) != CborNoError) {
    return {};
  }
  if (!cbor_value_is_unsigned_integer(&val)) {
    error_ = CborErrorIllegalType;
    return {};
  }
  uint64_t ret;
  error_ = cbor_value_get_uint64(&val, &ret);
  if (error_ != CborNoError) {
    return cpp::fail(error_);
  }
  return ret;
}

auto MapDecoder::FindSigned(const std::string &key) -> std::optional<int32_t> {
  CborValue val;
  if (error_ != CborNoError) {
    return {};
  }
  if (cbor_value_map_find_value(&it_, key.c_str(), &val) != CborNoError) {
    return {};
  }
  if (!cbor_value_is_integer(&val)) {
    error_ = CborErrorIllegalType;
    return {};
  }
  int32_t ret;
  error_ = cbor_value_get_int(&val, &ret);
  if (error_ != CborNoError) {
    return cpp::fail(error_);
  }
  return ret;
}

} // namespace cbor