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 auto vectorEnd = cursor_ + head.len; 46 if (vectorEnd > data_.size()) { 47 return false; 48 } 49 for (; cursor_ < vectorEnd;) { 50 // V: item value 51 TLVHead valueHead{}; 52 bool ret = ReadHead(valueHead); 53 T item{}; 54 ret = ret && ReadValue(item, valueHead); 55 if (!ret) { 56 return false; 57 } 58 value.push_back(item); 59 } 60 return true; 61 } 62 63 template<typename T> ReadValue(std::shared_ptr<T> & value,const TLVHead & head)64 bool ReadValue(std::shared_ptr<T> &value, const TLVHead &head) 65 { 66 value = std::make_shared<T>(); 67 if (value == nullptr) { 68 return false; 69 } 70 return ReadValue(*value, head); 71 } 72 73 bool ReadHead(TLVHead &head); 74 bool ReadValue(std::monostate &value, const TLVHead &head); 75 bool ReadValue(void *value, const TLVHead &head); 76 bool ReadValue(bool &value, const TLVHead &head); 77 bool ReadValue(int8_t &value, const TLVHead &head); 78 bool ReadValue(int16_t &value, const TLVHead &head); 79 bool ReadValue(int32_t &value, const TLVHead &head); 80 bool ReadValue(int64_t &value, const TLVHead &head); 81 bool ReadValue(double &value, const TLVHead &head); 82 bool ReadValue(uint32_t &value, const TLVHead &head); 83 bool ReadValue(std::string &value, const TLVHead &head); 84 bool ReadValue(RawMem &rawMem, const TLVHead &head); 85 bool ReadValue(TLVReadable &value, const TLVHead &head); 86 bool ReadValue(std::vector<uint8_t> &value, const TLVHead &head); 87 bool ReadValue(Object &value, const TLVHead &head); 88 bool ReadValue(std::shared_ptr<OHOS::Uri> &value, const TLVHead &head); 89 bool ReadValue(std::shared_ptr<AAFwk::Want> &value, const TLVHead &head); 90 bool ReadValue(std::shared_ptr<Media::PixelMap> &value, const TLVHead &head); 91 bool ReadValue(std::map<std::string, std::vector<uint8_t>> &value, const TLVHead &head); 92 bool ReadValue(Details &value, const TLVHead &head); 93 94 template<typename _InTp> 95 bool ReadVariant( 96 uint32_t step, uint32_t index, _InTp &input, const TLVHead &head); 97 98 template<typename _InTp, typename _First, typename... _Rest> 99 bool ReadVariant( 100 uint32_t step, uint32_t index, _InTp &input, const TLVHead &head); 101 102 template<typename... _Types> 103 bool ReadValue(std::variant<_Types...> &value, const TLVHead &head); 104 105 template<> 106 bool ReadValue(EntryValue &value, const TLVHead &head); 107 108 private: ReadBasicValue(bool & value,const TLVHead & head)109 bool ReadBasicValue(bool &value, const TLVHead &head) 110 { 111 if (head.len != sizeof(bool) || head.len == 0) { 112 return false; 113 } 114 if (!HasExpectBuffer(head.len)) { 115 return false; 116 } 117 uint8_t rawValue = 0; 118 auto ret = memcpy_s(&rawValue, sizeof(bool), data_.data() + cursor_, sizeof(bool)); 119 if (ret != EOK) { 120 return false; 121 } 122 if (rawValue > 1) { 123 return false; 124 } 125 value = NetToHost(rawValue); 126 cursor_ += sizeof(bool); 127 return true; 128 } 129 130 template<typename T> ReadBasicValue(T & value,const TLVHead & head)131 bool ReadBasicValue(T &value, const TLVHead &head) 132 { 133 if (head.len != sizeof(T) || head.len == 0) { 134 return false; 135 } 136 if (!HasExpectBuffer(head.len)) { 137 return false; 138 } 139 auto ret = memcpy_s(&value, sizeof(T), data_.data() + cursor_, sizeof(T)); 140 if (ret != EOK) { 141 return false; 142 } 143 value = NetToHost(value); 144 cursor_ += sizeof(T); 145 return true; 146 } 147 148 const std::vector<uint8_t> data_; 149 }; 150 } // namespace OHOS::MiscServices 151 #endif // DISTRIBUTEDDATAMGR_PASTEBOARD_TLV_READABLE_H 152