• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef DISTRIBUTEDDATAMGR_PASTEBOARD_TLV_READABLE_H
17 #define DISTRIBUTEDDATAMGR_PASTEBOARD_TLV_READABLE_H
18 
19 #include "endian_converter.h"
20 #include "tlv_buffer.h"
21 #include "tlv_utils.h"
22 
23 namespace OHOS::MiscServices {
24 
25 class ReadOnlyBuffer;
26 
27 class TLVReadable {
28 public:
29     virtual ~TLVReadable() = default;
30 
31     virtual bool DecodeTLV(ReadOnlyBuffer &buffer) = 0;
32 
33     API_EXPORT bool Decode(const std::vector<uint8_t> &buffer);
34 };
35 
36 class ReadOnlyBuffer : public TLVBuffer {
37 public:
ReadOnlyBuffer(const std::vector<uint8_t> & data)38     explicit ReadOnlyBuffer(const std::vector<uint8_t> &data) : TLVBuffer(data.size()), data_(data)
39     {
40     }
41 
42     template<typename T>
ReadValue(std::vector<T> & value,const TLVHead & head)43     bool ReadValue(std::vector<T> &value, const TLVHead &head)
44     {
45         if (cursor_ > UINT32_MAX - head.len) {
46             return false;
47         }
48         auto vectorEnd = cursor_ + head.len;
49         if (vectorEnd > data_.size()) {
50             return false;
51         }
52         for (; cursor_ < vectorEnd;) {
53             // V: item value
54             TLVHead valueHead{};
55             bool ret = ReadHead(valueHead);
56             T item{};
57             ret = ret && ReadValue(item, valueHead);
58             if (!ret) {
59                 return false;
60             }
61             value.push_back(item);
62         }
63         return true;
64     }
65 
66     template<typename T>
ReadValue(std::shared_ptr<T> & value,const TLVHead & head)67     bool ReadValue(std::shared_ptr<T> &value, const TLVHead &head)
68     {
69         value = std::make_shared<T>();
70         if (value == nullptr) {
71             return false;
72         }
73         return ReadValue(*value, head);
74     }
75 
76     bool ReadHead(TLVHead &head);
77     bool ReadValue(std::monostate &value, const TLVHead &head);
78     bool ReadValue(void *value, const TLVHead &head);
79     bool ReadValue(bool &value, const TLVHead &head);
80     bool ReadValue(int8_t &value, const TLVHead &head);
81     bool ReadValue(int16_t &value, const TLVHead &head);
82     bool ReadValue(int32_t &value, const TLVHead &head);
83     bool ReadValue(int64_t &value, const TLVHead &head);
84     bool ReadValue(double &value, const TLVHead &head);
85     bool ReadValue(uint32_t &value, const TLVHead &head);
86     bool ReadValue(std::string &value, const TLVHead &head);
87     bool ReadValue(RawMem &rawMem, const TLVHead &head);
88     bool ReadValue(TLVReadable &value, const TLVHead &head);
89     bool ReadValue(std::vector<uint8_t> &value, const TLVHead &head);
90     bool ReadValue(Object &value, const TLVHead &head);
91     bool ReadValue(std::shared_ptr<OHOS::Uri> &value, const TLVHead &head);
92     bool ReadValue(std::shared_ptr<AAFwk::Want> &value, const TLVHead &head);
93     bool ReadValue(std::shared_ptr<Media::PixelMap> &value, const TLVHead &head);
94     bool ReadValue(std::map<std::string, std::vector<uint8_t>> &value, const TLVHead &head);
95     bool ReadValue(Details &value, const TLVHead &head);
96 
97     template<typename _InTp>
98     bool ReadVariant(
99         uint32_t step, uint32_t index, _InTp &input, const TLVHead &head);
100 
101     template<typename _InTp, typename _First, typename... _Rest>
102     bool ReadVariant(
103         uint32_t step, uint32_t index, _InTp &input, const TLVHead &head);
104 
105     template<typename... _Types>
106     bool ReadValue(std::variant<_Types...> &value, const TLVHead &head);
107 
108     template<>
109     bool ReadValue(EntryValue &value, const TLVHead &head);
110 
111 private:
ReadBasicValue(bool & value,const TLVHead & head)112     bool ReadBasicValue(bool &value, const TLVHead &head)
113     {
114         if (head.len != sizeof(bool) || head.len == 0) {
115             return false;
116         }
117         if (!HasExpectBuffer(head.len)) {
118             return false;
119         }
120         uint8_t rawValue = 0;
121         auto ret = memcpy_s(&rawValue, sizeof(bool), data_.data() + cursor_, sizeof(bool));
122         if (ret != EOK) {
123             return false;
124         }
125         if (rawValue > 1) {
126             return false;
127         }
128         value = NetToHost(rawValue);
129         cursor_ += sizeof(bool);
130         return true;
131     }
132 
133     template<typename T>
ReadBasicValue(T & value,const TLVHead & head)134     bool ReadBasicValue(T &value, const TLVHead &head)
135     {
136         if (head.len != sizeof(T) || head.len == 0) {
137             return false;
138         }
139         if (!HasExpectBuffer(head.len)) {
140             return false;
141         }
142         auto ret = memcpy_s(&value, sizeof(T), data_.data() + cursor_, sizeof(T));
143         if (ret != EOK) {
144             return false;
145         }
146         value = NetToHost(value);
147         cursor_ += sizeof(T);
148         return true;
149     }
150 
151     const std::vector<uint8_t> data_;
152 };
153 } // namespace OHOS::MiscServices
154 #endif // DISTRIBUTEDDATAMGR_PASTEBOARD_TLV_READABLE_H
155