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_WRITEABLE_H 17 #define DISTRIBUTEDDATAMGR_PASTEBOARD_TLV_WRITEABLE_H 18 19 #include "endian_converter.h" 20 #include "tlv_countable.h" 21 22 namespace OHOS::MiscServices { 23 24 class WriteOnlyBuffer; 25 26 class TLVWriteable : public TLVCountable { 27 public: 28 virtual ~TLVWriteable() = default; 29 30 virtual bool EncodeTLV(WriteOnlyBuffer &buffer) const = 0; 31 32 API_EXPORT bool Encode(std::vector<uint8_t> &buffer) const; 33 }; 34 35 class WriteOnlyBuffer : public TLVBuffer { 36 public: WriteOnlyBuffer(size_t len)37 explicit WriteOnlyBuffer(size_t len) : TLVBuffer(len), data_(len) 38 { 39 } 40 41 template<typename T> Write(uint16_t type,const std::vector<T> & value)42 bool Write(uint16_t type, const std::vector<T> &value) 43 { 44 if (!HasExpectBuffer(sizeof(TLVHead))) { 45 return false; 46 } 47 auto tagCursor = cursor_; 48 cursor_ += sizeof(TLVHead); // placeholder 49 auto valueCursor = cursor_; 50 bool ret = WriteValue(value); 51 WriteHead(type, tagCursor, cursor_ - valueCursor); 52 return ret; 53 } 54 55 template<typename T> Write(uint16_t type,const std::shared_ptr<T> & value)56 bool Write(uint16_t type, const std::shared_ptr<T> &value) 57 { 58 if (value == nullptr) { 59 return true; 60 } 61 return Write(type, *value); 62 } 63 64 bool Write(uint16_t type, std::monostate value); 65 bool Write(uint16_t type, const void *value); 66 bool Write(uint16_t type, bool value); 67 bool Write(uint16_t type, double value); 68 bool Write(uint16_t type, int8_t value); 69 bool Write(uint16_t type, int16_t value); 70 bool Write(uint16_t type, int32_t value); 71 bool Write(uint16_t type, int64_t value); 72 bool Write(uint16_t type, uint32_t value); 73 bool Write(uint16_t type, const std::string &value); 74 bool Write(uint16_t type, const Object &value); 75 bool Write(uint16_t type, const AAFwk::Want &value); 76 bool Write(uint16_t type, const Media::PixelMap &value); 77 bool Write(uint16_t type, const RawMem &value); 78 bool Write(uint16_t type, const TLVWriteable &value); 79 bool Write(uint16_t type, const std::vector<uint8_t> &value); 80 bool Write(uint16_t type, const std::map<std::string, std::vector<uint8_t>> &value); 81 bool Write(uint16_t type, const Details &value); 82 83 template<typename _InTp> 84 bool WriteVariant(uint16_t type, uint32_t step, const _InTp &input); 85 86 template<typename _InTp, typename _First, typename... _Rest> 87 bool WriteVariant(uint16_t type, uint32_t step, const _InTp &input); 88 89 template<typename... _Types> 90 bool Write(uint16_t type, const std::variant<_Types...> &input); 91 92 template<> 93 bool Write(uint16_t type, const EntryValue &input); 94 95 private: WriteHead(uint16_t type,size_t tagCursor,uint32_t len)96 void WriteHead(uint16_t type, size_t tagCursor, uint32_t len) 97 { 98 auto *tlvHead = reinterpret_cast<TLVHead *>(data_.data() + tagCursor); 99 tlvHead->tag = HostToNet(type); 100 tlvHead->len = HostToNet(len); 101 } 102 103 template<typename T> WriteBasic(uint16_t type,T value)104 bool WriteBasic(uint16_t type, T value) 105 { 106 if (!HasExpectBuffer(sizeof(TLVHead) + sizeof(value))) { 107 return false; 108 } 109 auto *tlvHead = reinterpret_cast<TLVHead *>(data_.data() + cursor_); 110 tlvHead->tag = HostToNet(type); 111 tlvHead->len = HostToNet((uint32_t)sizeof(value)); 112 auto valueBuff = HostToNet(value); 113 auto ret = memcpy_s(tlvHead->value, total_ - cursor_ - sizeof(TLVHead), &valueBuff, sizeof(value)); 114 if (ret != EOK) { 115 return false; 116 } 117 cursor_ += sizeof(TLVHead) + sizeof(value); 118 return true; 119 } 120 121 template<typename T> WriteValue(const std::vector<T> & value)122 bool WriteValue(const std::vector<T> &value) 123 { 124 // items iterator 125 bool ret = true; 126 for (const T &item : value) { 127 // V:item value 128 ret = ret && Write(TAG_VECTOR_ITEM, item); 129 } 130 return ret; 131 } 132 133 friend class TLVWriteable; 134 std::vector<uint8_t> data_; 135 }; 136 } // namespace OHOS::MiscServices 137 #endif // DISTRIBUTEDDATAMGR_PASTEBOARD_TLV_WRITEABLE_H 138