2021-04-26 22:39:02 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020-2021, the SerenityOS developers.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-05-31 01:12:11 +03:00
|
|
|
#include <AK/NumericLimits.h>
|
2021-04-26 22:39:02 +03:00
|
|
|
#include <AK/Stream.h>
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
struct LEB128 {
|
2021-04-27 16:28:02 +03:00
|
|
|
template<typename StreamT, typename ValueType = size_t>
|
|
|
|
static bool read_unsigned(StreamT& stream, ValueType& result)
|
2021-04-26 22:39:02 +03:00
|
|
|
{
|
|
|
|
[[maybe_unused]] size_t backup_offset = 0;
|
|
|
|
if constexpr (requires { stream.offset(); })
|
|
|
|
backup_offset = stream.offset();
|
|
|
|
InputStream& input_stream { stream };
|
|
|
|
|
|
|
|
result = 0;
|
|
|
|
size_t num_bytes = 0;
|
|
|
|
while (true) {
|
|
|
|
if (input_stream.unreliable_eof()) {
|
|
|
|
if constexpr (requires { stream.seek(backup_offset); })
|
|
|
|
stream.seek(backup_offset);
|
|
|
|
input_stream.set_fatal_error();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
u8 byte = 0;
|
|
|
|
input_stream >> byte;
|
|
|
|
if (input_stream.has_any_error())
|
|
|
|
return false;
|
|
|
|
|
2021-05-31 01:12:11 +03:00
|
|
|
ValueType masked_byte = byte & ~(1 << 7);
|
2022-07-09 00:27:24 +03:00
|
|
|
bool const shift_too_large_for_result = num_bytes * 7 > sizeof(ValueType) * 8;
|
2021-08-31 13:30:12 +03:00
|
|
|
if (shift_too_large_for_result)
|
|
|
|
return false;
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool const shift_too_large_for_byte = ((masked_byte << (num_bytes * 7)) >> (num_bytes * 7)) != masked_byte;
|
2021-08-31 13:30:12 +03:00
|
|
|
if (shift_too_large_for_byte)
|
2021-05-31 01:12:11 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
result = (result) | (masked_byte << (num_bytes * 7));
|
2021-04-26 22:39:02 +03:00
|
|
|
if (!(byte & (1 << 7)))
|
|
|
|
break;
|
|
|
|
++num_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-27 16:28:02 +03:00
|
|
|
template<typename StreamT, typename ValueType = ssize_t>
|
|
|
|
static bool read_signed(StreamT& stream, ValueType& result)
|
2021-04-26 22:39:02 +03:00
|
|
|
{
|
2021-05-31 01:12:11 +03: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 22:39:02 +03:00
|
|
|
[[maybe_unused]] size_t backup_offset = 0;
|
|
|
|
if constexpr (requires { stream.offset(); })
|
|
|
|
backup_offset = stream.offset();
|
|
|
|
InputStream& input_stream { stream };
|
|
|
|
|
2021-05-31 01:12:11 +03:00
|
|
|
i64 temp = 0;
|
2021-04-26 22:39:02 +03:00
|
|
|
size_t num_bytes = 0;
|
|
|
|
u8 byte = 0;
|
2021-05-31 01:12:11 +03:00
|
|
|
result = 0;
|
2021-04-26 22:39:02 +03:00
|
|
|
|
|
|
|
do {
|
|
|
|
if (input_stream.unreliable_eof()) {
|
|
|
|
if constexpr (requires { stream.seek(backup_offset); })
|
|
|
|
stream.seek(backup_offset);
|
|
|
|
input_stream.set_fatal_error();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
input_stream >> byte;
|
|
|
|
if (input_stream.has_any_error())
|
|
|
|
return false;
|
2021-05-31 01:12:11 +03:00
|
|
|
|
|
|
|
// note: 64 bit assumptions!
|
|
|
|
u64 masked_byte = byte & ~(1 << 7);
|
2022-07-09 00:27:24 +03:00
|
|
|
bool const shift_too_large_for_result = num_bytes * 7 >= 64;
|
2021-08-31 13:30:12 +03:00
|
|
|
if (shift_too_large_for_result)
|
|
|
|
return false;
|
2021-05-31 01:12:11 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool const shift_too_large_for_byte = (num_bytes * 7) == 63 && masked_byte != 0x00 && masked_byte != 0x7Fu;
|
2021-08-31 13:30:12 +03:00
|
|
|
if (shift_too_large_for_byte)
|
2021-05-31 01:12:11 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
temp = (temp) | (masked_byte << (num_bytes * 7));
|
2021-04-26 22:39:02 +03:00
|
|
|
++num_bytes;
|
|
|
|
} while (byte & (1 << 7));
|
|
|
|
|
2021-05-31 01:12:11 +03:00
|
|
|
if ((num_bytes * 7) < 64 && (byte & 0x40)) {
|
2021-04-26 22:39:02 +03:00
|
|
|
// sign extend
|
2021-05-31 01:12:11 +03: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())
|
|
|
|
return false;
|
2021-04-26 22:39:02 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 01:12:11 +03:00
|
|
|
result = static_cast<ValueType>(temp);
|
|
|
|
|
2021-04-26 22:39:02 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::LEB128;
|