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