• 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_BUFFER_H
17 #define DISTRIBUTEDDATAMGR_PASTEBOARD_TLV_BUFFER_H
18 
19 #include <vector>
20 
21 #include "unified_meta.h"
22 
23 namespace OHOS::MiscServices {
24 #pragma pack(1)
25 struct TLVHead {
26     uint16_t tag;
27     uint32_t len;
28     std::uint8_t value[0];
29 };
30 #pragma pack()
31 
32 /*
33  * Common tag definitions.
34  * Product should use after TAG_BUFF
35  **/
36 enum COMMON_TAG : uint16_t {
37     TAG_VECTOR_ITEM = 0x0000,
38     TAG_MAP_KEY,
39     TAG_MAP_VALUE, // std::vector<uint8_t>
40     TAG_MAP_VALUE_TYPE,
41     TAG_VARIANT_INDEX,
42     TAG_VARIANT_VALUE,
43     TAG_BUFF = 0x0100,
44 };
45 
46 using ValueType = std::variant<int32_t, int64_t, bool, double, std::string, std::vector<uint8_t>>;
47 using Details = std::map<std::string, ValueType>;
48 
49 using EntryValue = UDMF::ValueType;
50 using Object = UDMF::Object;
51 
52 struct TLVBuffer {
53 public:
TLVBufferTLVBuffer54     TLVBuffer() : total_(0), cursor_(0) {}
TLVBufferTLVBuffer55     explicit TLVBuffer(size_t total) : total_(total), cursor_(0) {}
56     virtual ~TLVBuffer() = default;
57 
SkipTLVBuffer58     inline bool Skip(size_t len)
59     {
60         if (len > total_ - cursor_) {
61             return false;
62         }
63         cursor_ += len;
64         return true;
65     }
66 
IsEnoughTLVBuffer67     inline bool IsEnough() const
68     {
69         return cursor_ < total_;
70     }
71 
HasExpectBufferTLVBuffer72     inline bool HasExpectBuffer(uint32_t expectLen) const
73     {
74         return total_ >= cursor_ && total_ - cursor_ >= expectLen;
75     }
76 
77 protected:
78     size_t total_ = 0;
79     size_t cursor_ = 0;
80 };
81 } // namespace OHOS::MiscServices
82 #endif // DISTRIBUTEDDATAMGR_PASTEBOARD_TLV_BUFFER_H
83