• 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_UTILS_H
17 #define DISTRIBUTEDDATAMGR_PASTEBOARD_TLV_UTILS_H
18 
19 #include "parcel.h"
20 #include "securec.h"
21 #include "unified_meta.h"
22 
23 namespace OHOS::MiscServices {
24 
25 struct RawMem {
26     uintptr_t buffer;
27     size_t bufferLen;
28     // notice:Keep the parcel reference to prevent the memory in the parcel from being destructed
29     std::shared_ptr<OHOS::Parcel> parcel;
30 };
31 
32 class TLVUtils {
33 public:
34     // parcelable to buffer
35     static RawMem Parcelable2Raw(const Parcelable *value);
36 
37     // buffer to parcelable
38     template<typename ParcelableType>
Raw2Parcelable(const RawMem & rawMem)39     static std::shared_ptr<ParcelableType> Raw2Parcelable(const RawMem &rawMem)
40     {
41         Parcel parcel(nullptr);
42         if (!Raw2Parcel(rawMem, parcel)) {
43             return nullptr;
44         }
45         return std::shared_ptr<ParcelableType>(ParcelableType::Unmarshalling(parcel));
46     }
47     // buffer to parcelable
48     API_EXPORT static bool Raw2Parcel(const RawMem &rawMem, Parcel &parcel);
49 
50     static std::shared_ptr<Media::PixelMap> Vector2PixelMap(std::vector<std::uint8_t> &value);
51 
52     static std::vector<std::uint8_t> PixelMap2Vector(std::shared_ptr<Media::PixelMap> pixelMap);
53 };
54 } // namespace OHOS::MiscServices
55 #endif //DISTRIBUTEDDATAMGR_PASTEBOARD_TLV_UTILS_H
56