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
|
|
|
{
|
2023-01-29 23:02:38 +00:00
|
|
|
ValueType result {};
|
2021-04-26 19:39:02 +00:00
|
|
|
size_t num_bytes = 0;
|
|
|
|
while (true) {
|
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");
|
|
|
|
|
|
|
|
auto byte = TRY(stream.read_value<u8>());
|
2021-04-26 19:39:02 +00:00
|
|
|
|
2021-05-30 22:12:11 +00:00
|
|
|
ValueType masked_byte = byte & ~(1 << 7);
|
2022-07-08 21:27:24 +00:00
|
|
|
bool const shift_too_large_for_result = num_bytes * 7 > sizeof(ValueType) * 8;
|
2021-08-31 10:30:12 +00:00
|
|
|
if (shift_too_large_for_result)
|
2023-01-29 12:09:25 +00:00
|
|
|
return Error::from_string_literal("Read value contains more bits than fit the chosen ValueType");
|
2021-08-31 10:30:12 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
bool const shift_too_large_for_byte = ((masked_byte << (num_bytes * 7)) >> (num_bytes * 7)) != masked_byte;
|
2021-08-31 10:30:12 +00:00
|
|
|
if (shift_too_large_for_byte)
|
2023-01-29 12:09:25 +00:00
|
|
|
return Error::from_string_literal("Read byte is too large to fit the chosen ValueType");
|
2021-05-30 22:12:11 +00:00
|
|
|
|
|
|
|
result = (result) | (masked_byte << (num_bytes * 7));
|
2021-04-26 19:39:02 +00:00
|
|
|
if (!(byte & (1 << 7)))
|
|
|
|
break;
|
|
|
|
++num_bytes;
|
|
|
|
}
|
|
|
|
|
2023-01-29 23:02:38 +00:00
|
|
|
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
|
|
|
{
|
2021-05-30 22:12:11 +00:00
|
|
|
// Note: We read into a u64 to simplify the parsing logic;
|
|
|
|
// result is range checked into ValueType after parsing.
|
|
|
|
static_assert(sizeof(ValueType) <= sizeof(u64), "Error checking logic assumes 64 bits or less!");
|
2021-04-26 19:39:02 +00:00
|
|
|
|
2021-05-30 22:12:11 +00:00
|
|
|
i64 temp = 0;
|
2021-04-26 19:39:02 +00:00
|
|
|
size_t num_bytes = 0;
|
|
|
|
u8 byte = 0;
|
2023-01-29 23:02:38 +00:00
|
|
|
ValueType result {};
|
2021-04-26 19:39:02 +00:00
|
|
|
|
|
|
|
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");
|
2021-04-26 19:39:02 +00:00
|
|
|
|
2023-01-29 12:09:25 +00:00
|
|
|
byte = TRY(stream.read_value<u8>());
|
2021-05-30 22:12:11 +00:00
|
|
|
|
|
|
|
// note: 64 bit assumptions!
|
|
|
|
u64 masked_byte = byte & ~(1 << 7);
|
2022-07-08 21:27:24 +00:00
|
|
|
bool const shift_too_large_for_result = num_bytes * 7 >= 64;
|
2021-08-31 10:30:12 +00:00
|
|
|
if (shift_too_large_for_result)
|
2023-01-29 12:09:25 +00:00
|
|
|
return Error::from_string_literal("Read value contains more bits than fit the chosen ValueType");
|
2021-05-30 22:12:11 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
bool const shift_too_large_for_byte = (num_bytes * 7) == 63 && masked_byte != 0x00 && masked_byte != 0x7Fu;
|
2021-08-31 10:30:12 +00:00
|
|
|
if (shift_too_large_for_byte)
|
2023-01-29 12:09:25 +00:00
|
|
|
return Error::from_string_literal("Read byte is too large to fit the chosen ValueType");
|
2021-05-30 22:12:11 +00:00
|
|
|
|
|
|
|
temp = (temp) | (masked_byte << (num_bytes * 7));
|
2021-04-26 19:39:02 +00:00
|
|
|
++num_bytes;
|
|
|
|
} while (byte & (1 << 7));
|
|
|
|
|
2021-05-30 22:12:11 +00:00
|
|
|
if ((num_bytes * 7) < 64 && (byte & 0x40)) {
|
2021-04-26 19:39:02 +00:00
|
|
|
// sign extend
|
2021-05-30 22:12:11 +00:00
|
|
|
temp |= ((u64)(-1) << (num_bytes * 7));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we've accumulated into an i64, make sure it fits into result
|
|
|
|
if constexpr (sizeof(ValueType) < sizeof(u64)) {
|
|
|
|
if (temp > NumericLimits<ValueType>::max() || temp < NumericLimits<ValueType>::min())
|
2023-01-29 12:09:25 +00:00
|
|
|
return Error::from_string_literal("Temporary value does not fit the result type");
|
2021-04-26 19:39:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-30 22:12:11 +00:00
|
|
|
result = static_cast<ValueType>(temp);
|
|
|
|
|
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
|