2021-04-26 19:39:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020-2021, the SerenityOS developers.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-05-30 22:12:11 +00:00
|
|
|
#include <AK/NumericLimits.h>
|
2023-01-29 12:09:25 +00:00
|
|
|
#include <AK/Stream.h>
|
2021-04-26 19:39:02 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2023-01-29 23:02:38 +00:00
|
|
|
template<typename ValueType>
|
|
|
|
class [[gnu::packed]] LEB128 {
|
|
|
|
public:
|
|
|
|
constexpr LEB128() = default;
|
|
|
|
|
|
|
|
constexpr LEB128(ValueType value)
|
|
|
|
: m_value(value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr operator ValueType() const { return m_value; }
|
|
|
|
|
2023-02-10 00:00:18 +00:00
|
|
|
static ErrorOr<LEB128<ValueType>> read_from_stream(Stream& stream)
|
2023-01-29 23:02:38 +00:00
|
|
|
requires(Unsigned<ValueType>)
|
2021-04-26 19:39:02 +00:00
|
|
|
{
|
2024-07-26 21:53:21 +00:00
|
|
|
// First byte is unrolled for speed
|
|
|
|
auto byte = TRY(stream.read_value<u8>());
|
|
|
|
if ((byte & 0x80) == 0)
|
|
|
|
return LEB128<ValueType> { byte };
|
2023-01-29 12:09:25 +00:00
|
|
|
|
2024-07-26 21:53:21 +00:00
|
|
|
ValueType result = byte & 0x7F;
|
|
|
|
size_t num_bytes = 1;
|
|
|
|
while (true) {
|
2023-01-29 12:09:25 +00:00
|
|
|
auto byte = TRY(stream.read_value<u8>());
|
2024-07-26 21:53:21 +00:00
|
|
|
ValueType masked = byte & 0x7F;
|
|
|
|
result |= masked << (num_bytes * 7);
|
|
|
|
if (num_bytes * 7 >= sizeof(ValueType) * 8 - 7 && (byte >> (sizeof(ValueType) * 8 - (num_bytes * 7))) != 0) {
|
|
|
|
if ((byte & 0x80) != 0)
|
|
|
|
return Error::from_string_literal("Read value contains more bits than fit the chosen ValueType");
|
2023-01-29 12:09:25 +00:00
|
|
|
return Error::from_string_literal("Read byte is too large to fit the chosen ValueType");
|
2024-07-26 21:53:21 +00:00
|
|
|
}
|
2021-04-26 19:39:02 +00:00
|
|
|
++num_bytes;
|
2024-07-26 21:53:21 +00:00
|
|
|
if ((byte & 0x80) == 0)
|
|
|
|
return LEB128<ValueType> { result };
|
2021-04-26 19:39:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-10 00:00:18 +00:00
|
|
|
static ErrorOr<LEB128<ValueType>> read_from_stream(Stream& stream)
|
2023-01-29 23:02:38 +00:00
|
|
|
requires(Signed<ValueType>)
|
2021-04-26 19:39:02 +00:00
|
|
|
{
|
2024-06-16 14:40:30 +00:00
|
|
|
constexpr auto BITS = sizeof(ValueType) * 8;
|
2021-04-26 19:39:02 +00:00
|
|
|
|
2024-06-16 14:40:30 +00:00
|
|
|
ValueType result = 0;
|
|
|
|
u32 shift = 0;
|
2021-04-26 19:39:02 +00:00
|
|
|
u8 byte = 0;
|
|
|
|
|
|
|
|
do {
|
2023-01-29 12:09:25 +00:00
|
|
|
if (stream.is_eof())
|
|
|
|
return Error::from_string_literal("Stream reached end-of-file while reading LEB128 value");
|
|
|
|
byte = TRY(stream.read_value<u8>());
|
2024-06-16 14:40:30 +00:00
|
|
|
result |= (ValueType)(byte & 0x7F) << shift;
|
|
|
|
|
|
|
|
if (shift >= BITS - 7) {
|
|
|
|
bool has_continuation = (byte & 0x80);
|
|
|
|
ValueType sign_and_unused = (i8)(byte << 1) >> (BITS - shift);
|
|
|
|
if (has_continuation)
|
|
|
|
return Error::from_string_literal("Read value contains more bits than fit the chosen ValueType");
|
|
|
|
if (sign_and_unused != 0 && sign_and_unused != -1)
|
|
|
|
return Error::from_string_literal("Read byte is too large to fit the chosen ValueType");
|
|
|
|
return LEB128<ValueType> { result };
|
|
|
|
}
|
|
|
|
|
|
|
|
shift += 7;
|
|
|
|
} while (byte & 0x80);
|
|
|
|
|
|
|
|
// Sign extend
|
|
|
|
if (shift < BITS && (byte & 0x40))
|
|
|
|
result |= ((ValueType)~0 << shift);
|
2021-05-30 22:12:11 +00:00
|
|
|
|
2023-01-29 23:02:38 +00:00
|
|
|
return LEB128<ValueType> { result };
|
2021-04-26 19:39:02 +00:00
|
|
|
}
|
2023-01-29 23:02:38 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
ValueType m_value { 0 };
|
2021-04-26 19:39:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2021-04-26 19:39:02 +00:00
|
|
|
using AK::LEB128;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|