Skip to content

Commit

Permalink
AK: Move the LEB128 logic to AK and make it usable with InputStream
Browse files Browse the repository at this point in the history
  • Loading branch information
alimpfard authored and awesomekling committed May 4, 2021
1 parent 8cc6372 commit 48260b5
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 51 deletions.
85 changes: 85 additions & 0 deletions AK/LEB128.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2020-2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#pragma once

#include <AK/Stream.h>
#include <AK/Types.h>

namespace AK {

struct LEB128 {
template<typename StreamT>
static bool read_unsigned(StreamT& stream, size_t& result)
{
[[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;

result = (result) | (static_cast<size_t>(byte & ~(1 << 7)) << (num_bytes * 7));
if (!(byte & (1 << 7)))
break;
++num_bytes;
}

return true;
}

template<typename StreamT>
static bool read_signed(StreamT& stream, ssize_t& result)
{
[[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;
u8 byte = 0;

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;
result = (result) | (static_cast<size_t>(byte & ~(1 << 7)) << (num_bytes * 7));
++num_bytes;
} while (byte & (1 << 7));

if (num_bytes * 7 < sizeof(size_t) * 4 && (byte & 0x40)) {
// sign extend
result |= ((size_t)(-1) << (num_bytes * 7));
}

return true;
}
};

}

using AK::LEB128;
54 changes: 3 additions & 51 deletions AK/MemoryStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#pragma once

#include <AK/ByteBuffer.h>
#include <AK/LEB128.h>
#include <AK/MemMem.h>
#include <AK/Stream.h>
#include <AK/Vector.h>
Expand Down Expand Up @@ -73,58 +74,9 @@ class InputMemoryStream final : public InputStream {
return m_bytes[m_offset];
}

bool read_LEB128_unsigned(size_t& result)
{
const auto backup = m_offset;

result = 0;
size_t num_bytes = 0;
while (true) {
if (eof()) {
m_offset = backup;
set_recoverable_error();
return false;
}

const u8 byte = m_bytes[m_offset];
result = (result) | (static_cast<size_t>(byte & ~(1 << 7)) << (num_bytes * 7));
++m_offset;
if (!(byte & (1 << 7)))
break;
++num_bytes;
}

return true;
}

bool read_LEB128_signed(ssize_t& result)
{
const auto backup = m_offset;

result = 0;
size_t num_bytes = 0;
u8 byte = 0;

do {
if (eof()) {
m_offset = backup;
set_recoverable_error();
return false;
}

byte = m_bytes[m_offset];
result = (result) | (static_cast<size_t>(byte & ~(1 << 7)) << (num_bytes * 7));
++m_offset;
++num_bytes;
} while (byte & (1 << 7));
bool read_LEB128_unsigned(size_t& result) { return LEB128::read_unsigned(*this, result); }

if (num_bytes * 7 < sizeof(size_t) * 4 && (byte & 0x40)) {
// sign extend
result |= ((size_t)(-1) << (num_bytes * 7));
}

return true;
}
bool read_LEB128_signed(ssize_t& result) { return LEB128::read_signed(*this, result); }

ReadonlyBytes bytes() const { return m_bytes; }
size_t offset() const { return m_offset; }
Expand Down

0 comments on commit 48260b5

Please sign in to comment.