• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "decoded/raw_data_decoder.h"
17 
18 #include "securec.h"
19 
20 namespace OHOS {
21 namespace HiviewDFX {
22 namespace EventRaw {
23 
FloatingNumberDecoded(uint8_t * rawData,const size_t maxLen,size_t & pos,double & dest)24 bool RawDataDecoder::FloatingNumberDecoded(uint8_t* rawData, const size_t maxLen, size_t& pos, double& dest)
25 {
26     if (rawData == nullptr || pos >= maxLen) {
27         return false;
28     }
29     uint64_t valByteCnt = 0; // default 0
30     if (!UnsignedVarintDecoded(rawData, maxLen, pos, valByteCnt)) {
31         return false;
32     }
33     if ((pos + valByteCnt) > maxLen) {
34         return false;
35     }
36     if (valByteCnt == sizeof(float)) {
37         float tmpf;
38         if (memcpy_s(reinterpret_cast<uint8_t*>(&tmpf), valByteCnt, rawData + pos, valByteCnt) != EOK) {
39             return false;
40         }
41         dest = static_cast<double>(tmpf);
42     } else if (valByteCnt == sizeof(double)) {
43         double tmpd;
44         if (memcpy_s(reinterpret_cast<uint8_t*>(&tmpd), valByteCnt, rawData + pos, valByteCnt) != EOK) {
45             return false;
46         }
47         dest = tmpd;
48     } else {
49         return false;
50     }
51     pos += valByteCnt;
52     return true;
53 }
54 
SignedVarintDecoded(uint8_t * rawData,const size_t maxLen,size_t & pos,int64_t & dest)55 bool RawDataDecoder::SignedVarintDecoded(uint8_t* rawData, const size_t maxLen, size_t& pos, int64_t& dest)
56 {
57     if (rawData == nullptr || pos >= maxLen) {
58         return false;
59     }
60     uint64_t uval = 0;
61     if (!UnsignedVarintDecoded(rawData, maxLen, pos, uval)) {
62         return false;
63     }
64     // unzigzag
65     dest = (uval >> 1) ^ -static_cast<int64_t>(uval & 1);
66     return true;
67 }
68 
StringValueDecoded(uint8_t * rawData,const size_t maxLen,size_t & pos,std::string & dest)69 bool RawDataDecoder::StringValueDecoded(uint8_t* rawData, const size_t maxLen, size_t& pos, std::string& dest)
70 {
71     if (rawData == nullptr || pos >= maxLen) {
72         return false;
73     }
74     uint64_t valByteCnt = 0; // default 0
75     if (!UnsignedVarintDecoded(rawData, maxLen, pos, valByteCnt) ||
76         valByteCnt > maxLen || // for value flip
77         ((pos + valByteCnt) > maxLen)) {
78         return false;
79     }
80     if (valByteCnt == 0) { // no need to copy
81         return true;
82     }
83     dest = std::string(reinterpret_cast<char*>(rawData + pos), valByteCnt);
84     pos += valByteCnt;
85     return true;
86 }
87 
UnsignedVarintDecoded(uint8_t * rawData,const size_t maxLen,size_t & pos,uint64_t & dest)88 bool RawDataDecoder::UnsignedVarintDecoded(uint8_t* rawData, const size_t maxLen, size_t& pos, uint64_t& dest)
89 {
90     if (rawData == nullptr || pos >= maxLen) {
91         return false;
92     }
93     uint64_t val = rawData[pos] & TAG_BYTE_MASK;
94     uint32_t offset = TAG_BYTE_OFFSET;
95     if ((pos + 1) > maxLen) {
96         return false;
97     }
98     if (rawData[pos++] & TAG_BYTE_BOUND) {
99         do {
100             if (pos >= maxLen) {
101                 return false;
102             }
103             val |= (static_cast<uint64_t>(rawData[pos] & NON_TAG_BYTE_MASK) << offset);
104             offset += NON_TAG_BYTE_OFFSET;
105         } while (rawData[pos++] & NON_TAG_BYTE_BOUND);
106     }
107     dest = val;
108     return true;
109 }
110 
ValueTypeDecoded(uint8_t * rawData,const size_t maxLen,size_t & pos,struct ParamValueType & dest)111 bool RawDataDecoder::ValueTypeDecoded(uint8_t* rawData, const size_t maxLen, size_t& pos,
112     struct ParamValueType& dest)
113 {
114     if (rawData == nullptr || pos >= maxLen) {
115         return false;
116     }
117     if ((pos + sizeof(struct ParamValueType)) > maxLen) {
118         return false;
119     }
120     dest = *(reinterpret_cast<struct ParamValueType*>(rawData + pos));
121     pos += sizeof(struct ParamValueType);
122     return true;
123 }
124 } // namespace EventRaw
125 } // namespace HiviewDFX
126 } // namespace OHOS