1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:17:45 +00:00

AK: Rename span() to bytes() when appropriate.

I originally defined the bytes() method for the String class, because it
made it obvious that it's a span of bytes instead of span of characters.

This commit makes this more consistent by defining a bytes() method when
the type of the span is known to be u8.

Additionaly, the cast operator to Bytes is overloaded for ByteBuffer and
such.
This commit is contained in:
asynts 2020-08-15 18:38:24 +02:00 committed by Andreas Kling
parent 525d51bbb5
commit fff581cd72
18 changed files with 51 additions and 45 deletions

View file

@ -55,7 +55,7 @@ public:
virtual const ByteBuffer& data() const override { return m_data; };
virtual void overwrite(const ReadonlyBytes&) override;
virtual void overwrite(const ByteBuffer& buffer) override { overwrite(buffer.span()); }
virtual void overwrite(const ByteBuffer& buffer) override { overwrite(buffer.bytes()); }
virtual void overwrite(const u8* data, size_t size) override { overwrite({ data, size }); }
virtual void apply_initialization_vector(const u8* ivec) override

View file

@ -65,7 +65,7 @@ public:
virtual const ByteBuffer& data() const = 0;
virtual void overwrite(const ReadonlyBytes&) = 0;
virtual void overwrite(const ByteBuffer& buffer) { overwrite(buffer.span()); }
virtual void overwrite(const ByteBuffer& buffer) { overwrite(buffer.bytes()); }
virtual void overwrite(const u8* data, size_t size) { overwrite({ data, size }); }
virtual void apply_initialization_vector(const u8* ivec) = 0;

View file

@ -125,7 +125,7 @@ void RSA::encrypt(const ByteBuffer& in, ByteBuffer& out)
return;
}
auto exp = NumberTheory::ModularPower(in_integer, m_public_key.public_exponent(), m_public_key.modulus());
auto size = exp.export_data(out.span());
auto size = exp.export_data(out);
auto outsize = out.size();
if (size != outsize) {
dbg() << "POSSIBLE RSA BUG!!! Size mismatch: " << outsize << " requested but " << size << " bytes generated";
@ -139,7 +139,7 @@ void RSA::decrypt(const ByteBuffer& in, ByteBuffer& out)
auto in_integer = UnsignedBigInteger::import_data(in.data(), in.size());
auto exp = NumberTheory::ModularPower(in_integer, m_private_key.private_exponent(), m_private_key.modulus());
auto size = exp.export_data(out.span());
auto size = exp.export_data(out);
auto align = m_private_key.length();
auto aligned_size = (size + align - 1) / align * align;
@ -153,7 +153,7 @@ void RSA::sign(const ByteBuffer& in, ByteBuffer& out)
{
auto in_integer = UnsignedBigInteger::import_data(in.data(), in.size());
auto exp = NumberTheory::ModularPower(in_integer, m_private_key.private_exponent(), m_private_key.modulus());
auto size = exp.export_data(out.span());
auto size = exp.export_data(out);
out = out.slice(out.size() - size, size);
}
@ -161,7 +161,7 @@ void RSA::verify(const ByteBuffer& in, ByteBuffer& out)
{
auto in_integer = UnsignedBigInteger::import_data(in.data(), in.size());
auto exp = NumberTheory::ModularPower(in_integer, m_public_key.public_exponent(), m_public_key.modulus());
auto size = exp.export_data(out.span());
auto size = exp.export_data(out);
out = out.slice(out.size() - size, size);
}
@ -170,7 +170,7 @@ void RSA::import_private_key(ReadonlyBytes bytes, bool pem)
ByteBuffer buffer;
if (pem) {
buffer = decode_pem(bytes);
bytes = buffer.span();
bytes = buffer;
}
auto key = parse_rsa_key(bytes);
@ -186,7 +186,7 @@ void RSA::import_public_key(ReadonlyBytes bytes, bool pem)
ByteBuffer buffer;
if (pem) {
buffer = decode_pem(bytes);
bytes = buffer.span();
bytes = buffer;
}
auto key = parse_rsa_key(bytes);

View file

@ -160,8 +160,8 @@ public:
RSA(const ByteBuffer& publicKeyPEM, const ByteBuffer& privateKeyPEM)
{
import_public_key(publicKeyPEM.span());
import_private_key(privateKeyPEM.span());
import_public_key(publicKeyPEM);
import_private_key(privateKeyPEM);
}
RSA(const StringView& privKeyPEM)

View file

@ -105,7 +105,7 @@ void DebugInfo::prepare_lines()
return;
auto buffer = section.wrapping_byte_buffer();
InputMemoryStream stream { buffer.span() };
InputMemoryStream stream { buffer };
Vector<LineProgram::LineInfo> all_lines;
while (!stream.eof()) {

View file

@ -40,7 +40,7 @@ AbbreviationsMap::AbbreviationsMap(const DwarfInfo& dwarf_info, u32 offset)
void AbbreviationsMap::populate_map()
{
InputMemoryStream abbreviation_stream(m_dwarf_info.abbreviation_data().span());
InputMemoryStream abbreviation_stream(m_dwarf_info.abbreviation_data());
abbreviation_stream.discard_or_error(m_offset);
while (!abbreviation_stream.eof()) {

View file

@ -36,7 +36,7 @@ DIE::DIE(const CompilationUnit& unit, u32 offset)
: m_compilation_unit(unit)
, m_offset(offset)
{
InputMemoryStream stream(m_compilation_unit.dwarf_info().debug_info_data().span());
InputMemoryStream stream(m_compilation_unit.dwarf_info().debug_info_data());
stream.discard_or_error(m_offset);
stream.read_LEB128_unsigned(m_abbreviation_code);
m_data_offset = stream.offset();
@ -181,7 +181,7 @@ DIE::AttributeValue DIE::get_attribute_value(AttributeDataForm form,
Optional<DIE::AttributeValue> DIE::get_attribute(const Attribute& attribute) const
{
InputMemoryStream stream { m_compilation_unit.dwarf_info().debug_info_data().span() };
InputMemoryStream stream { m_compilation_unit.dwarf_info().debug_info_data() };
stream.discard_or_error(m_data_offset);
auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code);

View file

@ -53,7 +53,7 @@ void DwarfInfo::populate_compilation_units()
if (m_debug_info_data.is_null())
return;
InputMemoryStream stream(m_debug_info_data.span());
InputMemoryStream stream{ m_debug_info_data };
while (!stream.eof()) {
auto unit_offset = stream.offset();
CompilationUnitHeader compilation_unit_header {};

View file

@ -92,7 +92,7 @@ void TLSv12::update_packet(ByteBuffer& packet)
buffer_position += packet.size() - header_size;
// get the appropricate HMAC value for the entire packet
auto mac = hmac_message(packet.span(), {}, mac_size, true);
auto mac = hmac_message(packet, {}, mac_size, true);
// write the MAC
buffer.overwrite(buffer_position, mac.data(), mac.size());
@ -114,8 +114,8 @@ void TLSv12::update_packet(ByteBuffer& packet)
ASSERT(length % block_size == 0);
// get a block to encrypt into
auto view = ct.span().slice(header_size + iv_size, length);
m_aes_local->encrypt(buffer.span(), view, iv.span());
auto view = ct.bytes().slice(header_size + iv_size, length);
m_aes_local->encrypt(buffer, view, iv);
// store the correct ciphertext length into the packet
u16 ct_length = (u16)ct.size() - header_size;
@ -215,8 +215,8 @@ ssize_t TLSv12::handle_message(const ByteBuffer& buffer)
auto decrypted = m_aes_remote->create_aligned_buffer(length - iv_size);
auto iv = buffer.slice_view(header_size, iv_size);
Bytes decrypted_span = decrypted.span();
m_aes_remote->decrypt(buffer.span().slice(header_size + iv_size, length - iv_size), decrypted_span, iv.span());
Bytes decrypted_span = decrypted;
m_aes_remote->decrypt(buffer.bytes().slice(header_size + iv_size, length - iv_size), decrypted_span, iv);
length = decrypted_span.size();

View file

@ -727,7 +727,7 @@ bool TLSv12::add_client_key(const ByteBuffer& certificate_pem_buffer, const Byte
if (certificate_pem_buffer.is_empty() || rsa_key.is_empty()) {
return true;
}
auto decoded_certificate = decode_pem(certificate_pem_buffer.span(), 0);
auto decoded_certificate = decode_pem(certificate_pem_buffer, 0);
if (decoded_certificate.is_empty()) {
dbg() << "Certificate not PEM";
return false;