Android-cuttlefish cvd tool
confui_cbor.h
Go to the documentation of this file.
1/*
2 * Copyright 2021, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <cstdint>
20#include <memory>
21#include <string>
22#include <vector>
23
24#include "cbor.h"
25
26namespace cuttlefish {
27namespace confui {
28
37class Cbor {
38 enum class Error : uint32_t {
39 OK = 0,
40 OUT_OF_DATA = 1,
41 MALFORMED = 2,
42 MALFORMED_UTF8 = 3,
43 };
44
45 enum class MessageSize : uint32_t { MAX = 6144u };
46
47 enum class Type : uint8_t {
48 NUMBER = 0,
49 NEGATIVE = 1,
50 BYTE_STRING = 2,
51 TEXT_STRING = 3,
52 ARRAY = 4,
53 MAP = 5,
54 TAG = 6,
55 FLOAT = 7,
56 };
57
58 public:
59 Cbor(const std::string& prompt_text,
60 const std::vector<std::uint8_t>& extra_data)
61 : prompt_text_(prompt_text),
62 extra_data_(extra_data),
64 buffer_(kMax + 1) {
65 Init();
66 }
67
68 bool IsOk() const { return buffer_status_ == Error::OK; }
69 Error GetErrorCode() const { return buffer_status_; }
71 bool IsMalformedUtf8() const {
73 }
74 // call this only when IsOk() returns true
75 std::vector<std::uint8_t>&& GetMessage();
76
80 const std::uint32_t kMax = static_cast<std::uint32_t>(MessageSize::MAX);
81
82 private:
84 public:
85 void operator()(cbor_item_t* ptr) { cbor_decref(&ptr); }
86 };
87
88 std::unique_ptr<cbor_item_t, CborDeleter> cb_map_;
89 std::string prompt_text_;
90 std::vector<std::uint8_t> extra_data_;
92 std::vector<std::uint8_t> buffer_;
93
94 void Init();
95 Error CheckUTF8Copy(const std::string& text);
96};
97
98} // namespace confui
99} // end of namespace cuttlefish
Definition: confui_cbor.h:83
void operator()(cbor_item_t *ptr)
Definition: confui_cbor.h:85
Definition: confui_cbor.h:37
Error GetErrorCode() const
Definition: confui_cbor.h:69
const std::uint32_t kMax
Definition: confui_cbor.h:80
bool IsMessageTooLong() const
Definition: confui_cbor.h:70
Type
Definition: confui_cbor.h:47
void Init()
Definition: confui_cbor.cc:30
std::vector< std::uint8_t > extra_data_
Definition: confui_cbor.h:90
Error
Definition: confui_cbor.h:38
MessageSize
Definition: confui_cbor.h:45
bool IsMalformedUtf8() const
Definition: confui_cbor.h:71
std::vector< std::uint8_t > && GetMessage()
Definition: confui_cbor.cc:71
std::vector< std::uint8_t > buffer_
Definition: confui_cbor.h:92
Cbor(const std::string &prompt_text, const std::vector< std::uint8_t > &extra_data)
Definition: confui_cbor.h:59
Error buffer_status_
Definition: confui_cbor.h:91
std::unique_ptr< cbor_item_t, CborDeleter > cb_map_
Definition: confui_cbor.h:88
Error CheckUTF8Copy(const std::string &text)
Definition: confui_cbor.cc:73
std::string prompt_text_
Definition: confui_cbor.h:89
bool IsOk() const
Definition: confui_cbor.h:68
static const char *const text[]
Definition: ext2_err.c:10
Definition: alloc_utils.cpp:23