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