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