summaryrefslogtreecommitdiff
path: root/src/cbor/cbor_decoder.cpp
blob: 20696afb4666781d87c9349cf54d9f2ec9e59386 (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
#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);
}

static auto ArrayDecoder::Create(CborValue& root)
    -> cpp::result<std::unique_ptr<ArrayDecoder>, CborError> {
  auto decoder = std::make_unique<ArrayDecoder>();
  decoder->root_ = 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);
}

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);
}

static auto MapDecoder::Create(CborValue& root)
    -> cpp::result<std::unique_ptr<MapDecoder>, CborError> {
  auto decoder = std::make_unique<MapDecoder>();
  decoder->root_ = 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) {
    // Don't store this as an error; missing keys are fine.
    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