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 <stdint.h>
20
21#include <memory>
22#include <string>
23#include <vector>
24
25#include "cbor.h"
26
27namespace cuttlefish {
28namespace confui {
29
38class Cbor {
39 enum class Error : uint32_t {
40 OK = 0,
41 OUT_OF_DATA = 1,
42 MALFORMED = 2,
43 MALFORMED_UTF8 = 3,
44 };
45
46 enum class MessageSize : uint32_t { MAX = 6144u };
47
48 enum class Type : uint8_t {
49 NUMBER = 0,
50 NEGATIVE = 1,
51 BYTE_STRING = 2,
52 TEXT_STRING = 3,
53 ARRAY = 4,
54 MAP = 5,
55 TAG = 6,
56 FLOAT = 7,
57 };
58
59 public:
60 Cbor(const std::string& prompt_text, const std::vector<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<uint8_t>&& GetMessage();
76
80 const uint32_t kMax = static_cast<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<uint8_t> extra_data_;
92 std::vector<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:38
Error GetErrorCode() const
Definition: confui_cbor.h:69
std::vector< uint8_t > && GetMessage()
Definition: confui_cbor.cc:77
std::vector< uint8_t > buffer_
Definition: confui_cbor.h:92
bool IsMessageTooLong() const
Definition: confui_cbor.h:70
Cbor(const std::string &prompt_text, const std::vector< uint8_t > &extra_data)
Definition: confui_cbor.h:60
Type
Definition: confui_cbor.h:48
void Init()
Definition: confui_cbor.cc:34
Error
Definition: confui_cbor.h:39
MessageSize
Definition: confui_cbor.h:46
bool IsMalformedUtf8() const
Definition: confui_cbor.h:71
const uint32_t kMax
Definition: confui_cbor.h:80
Error buffer_status_
Definition: confui_cbor.h:91
std::unique_ptr< cbor_item_t, CborDeleter > cb_map_
Definition: confui_cbor.h:88
std::vector< uint8_t > extra_data_
Definition: confui_cbor.h:90
Error CheckUTF8Copy(const std::string &text)
Definition: confui_cbor.cc:79
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_driver.h:20