mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:37:34 +00:00
AK: Make LEB128 capable of reading into any type
And not just ssize_t/size_t. This is useful in cases where the output size is supposed to be larger than size_t.
This commit is contained in:
parent
48260b5054
commit
d288f6654e
1 changed files with 9 additions and 8 deletions
17
AK/LEB128.h
17
AK/LEB128.h
|
@ -12,8 +12,8 @@
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
struct LEB128 {
|
struct LEB128 {
|
||||||
template<typename StreamT>
|
template<typename StreamT, typename ValueType = size_t>
|
||||||
static bool read_unsigned(StreamT& stream, size_t& result)
|
static bool read_unsigned(StreamT& stream, ValueType& result)
|
||||||
{
|
{
|
||||||
[[maybe_unused]] size_t backup_offset = 0;
|
[[maybe_unused]] size_t backup_offset = 0;
|
||||||
if constexpr (requires { stream.offset(); })
|
if constexpr (requires { stream.offset(); })
|
||||||
|
@ -35,7 +35,7 @@ struct LEB128 {
|
||||||
if (input_stream.has_any_error())
|
if (input_stream.has_any_error())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
result = (result) | (static_cast<size_t>(byte & ~(1 << 7)) << (num_bytes * 7));
|
result = (result) | (static_cast<ValueType>(byte & ~(1 << 7)) << (num_bytes * 7));
|
||||||
if (!(byte & (1 << 7)))
|
if (!(byte & (1 << 7)))
|
||||||
break;
|
break;
|
||||||
++num_bytes;
|
++num_bytes;
|
||||||
|
@ -44,9 +44,10 @@ struct LEB128 {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename StreamT>
|
template<typename StreamT, typename ValueType = ssize_t>
|
||||||
static bool read_signed(StreamT& stream, ssize_t& result)
|
static bool read_signed(StreamT& stream, ValueType& result)
|
||||||
{
|
{
|
||||||
|
using UValueType = MakeUnsigned<ValueType>;
|
||||||
[[maybe_unused]] size_t backup_offset = 0;
|
[[maybe_unused]] size_t backup_offset = 0;
|
||||||
if constexpr (requires { stream.offset(); })
|
if constexpr (requires { stream.offset(); })
|
||||||
backup_offset = stream.offset();
|
backup_offset = stream.offset();
|
||||||
|
@ -67,13 +68,13 @@ struct LEB128 {
|
||||||
input_stream >> byte;
|
input_stream >> byte;
|
||||||
if (input_stream.has_any_error())
|
if (input_stream.has_any_error())
|
||||||
return false;
|
return false;
|
||||||
result = (result) | (static_cast<size_t>(byte & ~(1 << 7)) << (num_bytes * 7));
|
result = (result) | (static_cast<UValueType>(byte & ~(1 << 7)) << (num_bytes * 7));
|
||||||
++num_bytes;
|
++num_bytes;
|
||||||
} while (byte & (1 << 7));
|
} while (byte & (1 << 7));
|
||||||
|
|
||||||
if (num_bytes * 7 < sizeof(size_t) * 4 && (byte & 0x40)) {
|
if (num_bytes * 7 < sizeof(UValueType) * 4 && (byte & 0x40)) {
|
||||||
// sign extend
|
// sign extend
|
||||||
result |= ((size_t)(-1) << (num_bytes * 7));
|
result |= ((UValueType)(-1) << (num_bytes * 7));
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue