* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DISTRIBUTEDDATAMGR_PASTEBOARD_TLV_UTILS_H
#define DISTRIBUTEDDATAMGR_PASTEBOARD_TLV_UTILS_H
#include <cinttypes>
#include "parcel.h"
#include "securec.h"
#include "unified_meta.h"
namespace OHOS::MiscServices {
struct RawMem {
uintptr_t buffer;
size_t bufferLen;
std::shared_ptr<OHOS::Parcel> parcel;
};
class TLVUtils {
public:
static RawMem Parcelable2Raw(const Parcelable *value);
template<typename ParcelableType>
static std::shared_ptr<ParcelableType> Raw2Parcelable(const RawMem &rawMem)
{
Parcel parcel(nullptr);
if (!Raw2Parcel(rawMem, parcel)) {
return nullptr;
}
return std::shared_ptr<ParcelableType>(ParcelableType::Unmarshalling(parcel));
}
API_EXPORT static bool Raw2Parcel(const RawMem &rawMem, Parcel &parcel);
static std::shared_ptr<Media::PixelMap> Vector2PixelMap(std::vector<std::uint8_t> &value);
static std::vector<std::uint8_t> PixelMap2Vector(std::shared_ptr<Media::PixelMap> pixelMap);
};
class RecursiveGuard {
public:
RecursiveGuard()
{
depth_++;
}
~RecursiveGuard()
{
depth_--;
}
bool IsValid()
{
return depth_ <= MAX_DEPTH;
}
private:
static constexpr uint32_t MAX_DEPTH = 10;
static thread_local inline uint32_t depth_ = 0;
};
}
#endif