• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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 LIBPANDAFILE_HELPERS_H_
17 #define LIBPANDAFILE_HELPERS_H_
18 
19 #include "macros.h"
20 #include "utils/bit_helpers.h"
21 #include "utils/leb128.h"
22 #include "utils/logger.h"
23 #include "utils/span.h"
24 
25 #include <cstdint>
26 
27 #include <limits>
28 #include <optional>
29 
30 namespace ark::panda_file::helpers {
31 
32 constexpr size_t UINT_BYTE2_SHIFT = 8U;
33 constexpr size_t UINT_BYTE3_SHIFT = 16U;
34 constexpr size_t UINT_BYTE4_SHIFT = 24U;
35 
36 template <size_t WIDTH>
Read(Span<const uint8_t> * sp)37 inline auto Read(Span<const uint8_t> *sp)
38 {
39     constexpr size_t BYTE_WIDTH = std::numeric_limits<uint8_t>::digits;
40     constexpr size_t BITWIDTH = BYTE_WIDTH * WIDTH;
41     using UnsignedType = ark::helpers::TypeHelperT<BITWIDTH, false>;
42 
43     UnsignedType result = 0;
44     for (size_t i = 0; i < WIDTH; i++) {
45         UnsignedType tmp = static_cast<UnsignedType>((*sp)[i]) << (i * BYTE_WIDTH);
46         result |= tmp;
47     }
48     *sp = sp->SubSpan(WIDTH);
49     return result;
50 }
51 
52 template <>
53 inline auto Read<sizeof(uint16_t)>(Span<const uint8_t> *sp)
54 {
55     auto *p = sp->data();
56     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
57     uint16_t result = *(p++);
58     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic, hicpp-signed-bitwise)
59     result |= static_cast<uint16_t>(*p) << UINT_BYTE2_SHIFT;
60     *sp = sp->SubSpan(sizeof(uint16_t));
61     return result;
62 }
63 
64 template <>
65 inline auto Read<sizeof(uint32_t)>(Span<const uint8_t> *sp)
66 {
67     auto *p = sp->data();
68     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
69     uint32_t result = *(p++);
70     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
71     result |= static_cast<uint32_t>(*(p++)) << UINT_BYTE2_SHIFT;
72     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
73     result |= static_cast<uint32_t>(*(p++)) << UINT_BYTE3_SHIFT;
74     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
75     result |= static_cast<uint32_t>(*p) << UINT_BYTE4_SHIFT;
76     *sp = sp->SubSpan(sizeof(uint32_t));
77     return result;
78 }
79 
80 template <size_t WIDTH>
Read(Span<const uint8_t> sp)81 inline auto Read(Span<const uint8_t> sp)
82 {
83     return Read<WIDTH>(&sp);
84 }
85 
ReadULeb128(Span<const uint8_t> * sp)86 inline uint32_t ReadULeb128(Span<const uint8_t> *sp)
87 {
88     uint32_t result;
89     size_t n;
90     [[maybe_unused]] bool isFull;
91     std::tie(result, n, isFull) = leb128::DecodeUnsigned<uint32_t>(sp->data());
92     if (!isFull) {
93         LOG(FATAL, PANDAFILE) << "ULEB128 decode failed: input truncated or malformed";
94     }
95     *sp = sp->SubSpan(n);
96     return result;
97 }
98 
99 // CC-OFFNXT(G.FUD.06) solid logic
SkipULeb128(Span<const uint8_t> * sp)100 inline void SkipULeb128(Span<const uint8_t> *sp)
101 {
102     if ((*sp)[0U] <= leb128::PAYLOAD_MASK) {
103         *sp = sp->SubSpan(1U);
104         return;
105     }
106 
107     if ((*sp)[1U] <= leb128::PAYLOAD_MASK) {
108         *sp = sp->SubSpan(2U);
109         return;
110     }
111 
112     if ((*sp)[2U] <= leb128::PAYLOAD_MASK) {
113         *sp = sp->SubSpan(3U);
114         return;
115     }
116 
117     ASSERT((*sp)[3U] <= leb128::PAYLOAD_MASK);
118     *sp = sp->SubSpan(4U);
119 }
120 
ReadLeb128(Span<const uint8_t> * sp)121 inline int32_t ReadLeb128(Span<const uint8_t> *sp)
122 {
123     int32_t result;
124     size_t n;
125     [[maybe_unused]] bool isFull;
126     std::tie(result, n, isFull) = leb128::DecodeSigned<int32_t>(sp->data());
127     ASSERT(isFull);
128     *sp = sp->SubSpan(n);
129     return result;
130 }
131 
132 template <size_t ALIGNMENT>
Align(const uint8_t * ptr)133 inline const uint8_t *Align(const uint8_t *ptr)
134 {
135     auto intptr = reinterpret_cast<uintptr_t>(ptr);
136     uintptr_t aligned = (intptr - 1U + ALIGNMENT) & -ALIGNMENT;
137     return reinterpret_cast<const uint8_t *>(aligned);
138 }
139 
140 template <size_t ALIGNMENT, class T>
Align(T n)141 inline T Align(T n)
142 {
143     return (n - 1U + ALIGNMENT) & -ALIGNMENT;
144 }
145 
146 template <class T, class E>
GetOptionalTaggedValue(Span<const uint8_t> sp,E tag,Span<const uint8_t> * next)147 inline std::optional<T> GetOptionalTaggedValue(Span<const uint8_t> sp, E tag, Span<const uint8_t> *next)
148 {
149     if (sp[0] == static_cast<uint8_t>(tag)) {
150         sp = sp.SubSpan(1);
151         T value = static_cast<T>(Read<sizeof(T)>(&sp));
152         *next = sp;
153         return value;
154     }
155     *next = sp;
156 
157     // NB! This is a workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635
158     // which fails Release builds for GCC 8 and 9.
159     std::optional<T> novalue = {};
160     return novalue;
161 }
162 
163 template <class T, class E, class Callback>
EnumerateTaggedValues(Span<const uint8_t> sp,E tag,Callback cb,Span<const uint8_t> * next)164 inline void EnumerateTaggedValues(Span<const uint8_t> sp, E tag, Callback cb, Span<const uint8_t> *next)
165 {
166     while (sp[0] == static_cast<uint8_t>(tag)) {
167         sp = sp.SubSpan(1);
168         T value(Read<sizeof(T)>(&sp));
169         cb(value);
170     }
171 
172     if (next == nullptr) {
173         return;
174     }
175 
176     *next = sp;
177 }
178 
179 template <class T, class E, class Callback>
EnumerateTaggedValuesWithEarlyStop(Span<const uint8_t> sp,E tag,Callback cb)180 inline bool EnumerateTaggedValuesWithEarlyStop(Span<const uint8_t> sp, E tag, Callback cb)
181 {
182     while (sp[0] == static_cast<uint8_t>(tag)) {
183         sp = sp.SubSpan(1);
184         T value(Read<sizeof(T)>(&sp));
185         if (cb(value)) {
186             return true;
187         }
188     }
189 
190     return false;
191 }
192 
193 }  // namespace ark::panda_file::helpers
194 
195 #endif  // LIBPANDAFILE_HELPERS_H_
196