1/*
2 * Copyright (c) 2023 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#include "encoded/raw_data_encoder.h"
17
18#include "hiview_logger.h"
19
20namespace OHOS {
21namespace HiviewDFX {
22namespace EventRaw {
23DEFINE_LOG_TAG("HiView-RawDataEncoder");
24uint8_t RawDataEncoder::EncodedTag(uint8_t type)
25{
26    return (type << (TAG_BYTE_OFFSET + 1));
27}
28
29bool RawDataEncoder::StringValueEncoded(RawData& data, const std::string& val)
30{
31    if (!UnsignedVarintEncoded(data, EncodeType::LENGTH_DELIMITED, val.length())) {
32        return false;
33    }
34    if (!data.Append(reinterpret_cast<uint8_t*>(const_cast<char*>(val.c_str())),
35        val.length())) {
36        HIVIEW_LOGE("string value copy failed.");
37        return false;
38    }
39    return true;
40}
41
42bool RawDataEncoder::ValueTypeEncoded(RawData& data, bool isArray, ValueType type, uint8_t count)
43{
44    struct ParamValueType kvType {
45        .isArray = isArray ? 1 : 0,
46        .valueType = static_cast<uint8_t>(type),
47        .valueByteCnt = count,
48    };
49    if (!data.Append(reinterpret_cast<uint8_t*>(&kvType), sizeof(struct ParamValueType))) {
50        return false;
51    }
52    return true;
53}
54} // namespace EventRaw
55} // namespace HiviewDFX
56} // namespace OHOS
57