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 <>
Read(Span<const uint8_t> *sp)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 <>
Read(Span<const uint8_t> *sp)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
96 // CC-OFFNXT(G.FUD.06) solid logic
SkipULeb128(Span<const uint8_t> *sp)97 inline void SkipULeb128(Span<const uint8_t> *sp)
98 {
99 if ((*sp)[0U] <= leb128::PAYLOAD_MASK) {
100 *sp = sp->SubSpan(1U);
101 return;
102 }
103
104 if ((*sp)[1U] <= leb128::PAYLOAD_MASK) {
105 *sp = sp->SubSpan(2U);
106 return;
107 }
108
109 if ((*sp)[2U] <= leb128::PAYLOAD_MASK) {
110 *sp = sp->SubSpan(3U);
111 return;
112 }
113
114 ASSERT((*sp)[3U] <= leb128::PAYLOAD_MASK);
115 *sp = sp->SubSpan(4U);
116 }
117
ReadLeb128(Span<const uint8_t> *sp)118 inline int32_t ReadLeb128(Span<const uint8_t> *sp)
119 {
120 int32_t result;
121 size_t n;
122 [[maybe_unused]] bool isFull;
123 std::tie(result, n, isFull) = leb128::DecodeSigned<int32_t>(sp->data());
124 ASSERT(isFull);
125 *sp = sp->SubSpan(n);
126 return result;
127 }
128
129 template <size_t ALIGNMENT>
Align(const uint8_t *ptr)130 inline const uint8_t *Align(const uint8_t *ptr)
131 {
132 auto intptr = reinterpret_cast<uintptr_t>(ptr);
133 uintptr_t aligned = (intptr - 1U + ALIGNMENT) & -ALIGNMENT;
134 return reinterpret_cast<const uint8_t *>(aligned);
135 }
136
137 template <size_t ALIGNMENT, class T>
Align(T n)138 inline T Align(T n)
139 {
140 return (n - 1U + ALIGNMENT) & -ALIGNMENT;
141 }
142
143 template <class T, class E>
GetOptionalTaggedValue(Span<const uint8_t> sp, E tag, Span<const uint8_t> *next)144 inline std::optional<T> GetOptionalTaggedValue(Span<const uint8_t> sp, E tag, Span<const uint8_t> *next)
145 {
146 if (sp[0] == static_cast<uint8_t>(tag)) {
147 sp = sp.SubSpan(1);
148 T value = static_cast<T>(Read<sizeof(T)>(&sp));
149 *next = sp;
150 return value;
151 }
152 *next = sp;
153
154 // NB! This is a workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635
155 // which fails Release builds for GCC 8 and 9.
156 std::optional<T> novalue = {};
157 return novalue;
158 }
159
160 template <class T, class E, class Callback>
EnumerateTaggedValues(Span<const uint8_t> sp, E tag, Callback cb, Span<const uint8_t> *next)161 inline void EnumerateTaggedValues(Span<const uint8_t> sp, E tag, Callback cb, Span<const uint8_t> *next)
162 {
163 while (sp[0] == static_cast<uint8_t>(tag)) {
164 sp = sp.SubSpan(1);
165 T value(Read<sizeof(T)>(&sp));
166 cb(value);
167 }
168
169 if (next == nullptr) {
170 return;
171 }
172
173 *next = sp;
174 }
175
176 template <class T, class E, class Callback>
EnumerateTaggedValuesWithEarlyStop(Span<const uint8_t> sp, E tag, Callback cb)177 inline bool EnumerateTaggedValuesWithEarlyStop(Span<const uint8_t> sp, E tag, Callback cb)
178 {
179 while (sp[0] == static_cast<uint8_t>(tag)) {
180 sp = sp.SubSpan(1);
181 T value(Read<sizeof(T)>(&sp));
182 if (cb(value)) {
183 return true;
184 }
185 }
186
187 return false;
188 }
189
190 } // namespace ark::panda_file::helpers
191
192 #endif // LIBPANDAFILE_HELPERS_H_
193