summaryrefslogtreecommitdiff
path: root/src/cbor_wrapper/cbor_decoder.cpp
blob: a99cca0275add11a73e5589827ec029c24e179cf (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
#include "cbor_decoder.hpp"

#include <cstdint>
#include <string>

#include "cbor.h"
#include "result.hpp"

static const int kDecoderFlags = 0;

namespace cbor {

auto parse_stdstring(const CborValue* val, std::string* out) -> CborError {
  char* buf;
  size_t len;
  CborError err = cbor_value_dup_text_string(val, &buf, &len, NULL);
  if (err != CborNoError) {
    return err;
  }
  *out = std::string(buf, len);
  free(buf);
  return err;
}

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, kDecoderFlags, &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::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);
}

template <>
auto ArrayDecoder::NextValue() -> cpp::result<int64_t, CborError> {
  return NextValue(&cbor_value_is_integer, &cbor_value_get_int);
}
template <>
auto ArrayDecoder::NextValue() -> cpp::result<uint64_t, CborError> {
  return NextValue(&cbor_value_is_unsigned_integer, &cbor_value_get_uint64);
}
template <>
auto ArrayDecoder::NextValue() -> cpp::result<std::string, CborError> {
  return NextValue(&cbor_value_is_byte_string, &parse_stdstring);
}

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, kDecoderFlags, &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::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);
}

template <>
auto MapDecoder::FindValue(const std::string& key) -> std::optional<int64_t> {
  return FindValue(key, &cbor_value_is_integer, &cbor_value_get_int);
}
template <>
auto MapDecoder::FindValue(const std::string& key) -> std::optional<uint64_t> {
  return FindValue(key, &cbor_value_is_unsigned_integer,
                   &cbor_value_get_uint64);
}
template <>
auto MapDecoder::FindValue(const std::string& key)
    -> std::optional<std::string> {
  return FindValue(key, &cbor_value_is_byte_string, &parse_stdstring);
}

}  // namespace cbor