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 #ifndef BASE_EVENT_RAW_ENCODE_INCLUDE_RAW_DATA_ENCODER_H 17 #define BASE_EVENT_RAW_ENCODE_INCLUDE_RAW_DATA_ENCODER_H 18 19 #include <cstdarg> 20 #include <cstddef> 21 #include <cstdint> 22 #include <string> 23 #include <vector> 24 25 #include "base/raw_data_base_def.h" 26 #include "base/raw_data.h" 27 28 namespace OHOS { 29 namespace HiviewDFX { 30 namespace EventRaw { 31 class RawDataEncoder { 32 public: 33 static bool ValueTypeEncoded(RawData& data, bool isArray, ValueType valueType, 34 uint8_t count); 35 static bool StringValueEncoded(RawData& data, const std::string& val); 36 37 public: 38 // uintx_t -> uint64_t 39 template<typename T> UnsignedVarintEncoded(RawData & data,const EncodeType type,T val)40 static bool UnsignedVarintEncoded(RawData& data, const EncodeType type, T val) 41 { 42 uint8_t cpyVal = EncodedTag(static_cast<uint8_t>(type)) | ((val < TAG_BYTE_BOUND) ? 0 : TAG_BYTE_BOUND) | 43 static_cast<uint8_t>(val & TAG_BYTE_MASK); 44 if (!data.Append(reinterpret_cast<uint8_t*>(&cpyVal), 1)) { 45 return false; 46 } 47 val >>= TAG_BYTE_OFFSET; 48 while (val > 0) { 49 cpyVal = ((val < NON_TAG_BYTE_BOUND) ? 0 : NON_TAG_BYTE_BOUND) | 50 static_cast<uint8_t>(val & NON_TAG_BYTE_MASK); 51 if (!data.Append(reinterpret_cast<uint8_t*>(&cpyVal), 1)) { 52 return false; 53 } 54 val >>= NON_TAG_BYTE_OFFSET; 55 } 56 return true; 57 } 58 59 // bool, intx_t -> int64_t 60 template<typename T> SignedVarintEncoded(RawData & data,const EncodeType type,T val)61 static bool SignedVarintEncoded(RawData& data, const EncodeType type, T val) 62 { 63 int64_t valInt64 = static_cast<int64_t>(val); 64 // zigzag encode 65 uint64_t uValInt64 = static_cast<uint64_t>((valInt64 << 1) ^ (valInt64 >> ((sizeof(valInt64) << 3) - 1))); 66 return UnsignedVarintEncoded(data, type, uValInt64); 67 } 68 69 // float, double => double 70 template<typename T> FloatingNumberEncoded(RawData & data,T val)71 static bool FloatingNumberEncoded(RawData& data, T val) 72 { 73 T valFdl = static_cast<T>(val); 74 if (!UnsignedVarintEncoded(data, EncodeType::LENGTH_DELIMITED, sizeof(T))) { 75 return false; 76 } 77 if (!data.Append(reinterpret_cast<uint8_t*>(&valFdl), sizeof(T))) { 78 return false; 79 } 80 return true; 81 } 82 83 private: 84 static uint8_t EncodedTag(uint8_t type); 85 86 private: 87 static constexpr int TAG_BYTE_OFFSET = 5; 88 static constexpr int TAG_BYTE_BOUND = (1 << TAG_BYTE_OFFSET); 89 static constexpr int TAG_BYTE_MASK = (TAG_BYTE_BOUND - 1); 90 91 static constexpr int NON_TAG_BYTE_OFFSET = 7; 92 static constexpr int NON_TAG_BYTE_BOUND = (1 << NON_TAG_BYTE_OFFSET); 93 static constexpr int NON_TAG_BYTE_MASK = (NON_TAG_BYTE_BOUND - 1); 94 }; 95 } // namespace EventRaw 96 } // namespace HiviewDFX 97 } // namespace OHOS 98 99 #endif // BASE_EVENT_RAW_ENCODE_INCLUDE_RAW_DATA_ENCODER_H