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
Notes: sideshowbarker 2024-07-19 03:34:56 +09:00
18 changed files with 51 additions and 45 deletions

View File

@ -71,8 +71,8 @@ public:
u8* data() { return m_data; }
const u8* data() const { return m_data; }
Bytes span() { return { data(), size() }; }
ReadonlyBytes span() const { return { data(), size() }; }
Bytes bytes() { return { data(), size() }; }
ReadonlyBytes bytes() const { return { data(), size() }; }
u8* offset_pointer(int offset) { return m_data + offset; }
const u8* offset_pointer(int offset) const { return m_data + offset; }
@ -163,8 +163,8 @@ public:
u8* data() { return m_impl ? m_impl->data() : nullptr; }
const u8* data() const { return m_impl ? m_impl->data() : nullptr; }
Bytes span() { return m_impl ? m_impl->span() : nullptr; }
ReadonlyBytes span() const { return m_impl ? m_impl->span() : nullptr; }
Bytes bytes() { return m_impl ? m_impl->bytes() : nullptr; }
ReadonlyBytes bytes() const { return m_impl ? m_impl->bytes() : nullptr; }
u8* offset_pointer(int offset) { return m_impl ? m_impl->offset_pointer(offset) : nullptr; }
const u8* offset_pointer(int offset) const { return m_impl ? m_impl->offset_pointer(offset) : nullptr; }
@ -233,6 +233,9 @@ public:
__builtin_memcpy(this->data() + offset, data, data_size);
}
operator Bytes() { return bytes(); }
operator ReadonlyBytes() const { return bytes(); }
private:
explicit ByteBuffer(RefPtr<ByteBufferImpl>&& impl)
: m_impl(move(impl))

View File

@ -243,6 +243,9 @@ public:
return !(*this == other);
}
operator Span<T>() { return span(); }
operator Span<const T>() const { return span(); }
bool contains_slow(const T& value) const
{
for (size_t i = 0; i < size(); ++i) {

View File

@ -285,7 +285,7 @@ void handle_icmp(const EthernetFrameHeader& eth, const IPv4Packet& ipv4_packet)
memcpy(response.payload(), request.payload(), icmp_payload_size);
response.header.set_checksum(internet_checksum(&response, icmp_packet_size));
// FIXME: What is the right TTL value here? Is 64 ok? Should we use the same TTL as the echo request?
adapter->send_ipv4(eth.source(), ipv4_packet.source(), IPv4Protocol::ICMP, buffer.span(), 64);
adapter->send_ipv4(eth.source(), ipv4_packet.source(), IPv4Protocol::ICMP, buffer, 64);
}
}

View File

@ -218,7 +218,7 @@ void TCPSocket::send_tcp_packet(u16 flags, const void* payload, size_t payload_s
routing_decision.adapter->send_ipv4(
routing_decision.next_hop, peer_address(), IPv4Protocol::TCP,
buffer.span(), ttl());
buffer, ttl());
m_packets_out++;
m_bytes_out += buffer.size();
@ -246,7 +246,7 @@ void TCPSocket::send_outgoing_packets()
#endif
routing_decision.adapter->send_ipv4(
routing_decision.next_hop, peer_address(), IPv4Protocol::TCP,
packet.buffer.span(), ttl());
packet.buffer, ttl());
m_packets_out++;
m_bytes_out += packet.buffer.size();

View File

@ -102,7 +102,7 @@ KResultOr<size_t> UDPSocket::protocol_send(const void* data, size_t data_length)
udp_packet.set_length(sizeof(UDPPacket) + data_length);
memcpy(udp_packet.payload(), data, data_length);
klog() << "sending as udp packet from " << routing_decision.adapter->ipv4_address().to_string().characters() << ":" << local_port() << " to " << peer_address().to_string().characters() << ":" << peer_port() << "!";
routing_decision.adapter->send_ipv4(routing_decision.next_hop, peer_address(), IPv4Protocol::UDP, buffer.span(), ttl());
routing_decision.adapter->send_ipv4(routing_decision.next_hop, peer_address(), IPv4Protocol::UDP, buffer, ttl());
return data_length;
}

View File

@ -69,11 +69,11 @@ public:
typename CipherType::CTRMode cipher(m_key, KeySize);
Bytes buffer_span { buffer, n };
auto counter_span = m_counter.span();
auto counter_span = m_counter.bytes();
cipher.key_stream(buffer_span, counter_span, &counter_span);
// Extract a new key from the prng stream.
Bytes key_span = m_key.span();
Bytes key_span = m_key.bytes();
cipher.key_stream(key_span, counter_span, &counter_span);
}

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;

View File

@ -75,6 +75,6 @@ int main(int argc, char** argv)
return 0;
}
auto encoded = encode_base64(buffer.span());
auto encoded = encode_base64(buffer);
printf("%s\n", encoded.characters());
}

View File

@ -201,8 +201,8 @@ static void aes_cbc(const char* message, size_t len)
Crypto::Cipher::Intent::Encryption);
auto enc = cipher.create_aligned_buffer(buffer.size());
auto enc_span = enc.span();
cipher.encrypt(buffer.span(), enc_span, iv.span());
auto enc_span = enc.bytes();
cipher.encrypt(buffer, enc_span, iv);
if (binary)
printf("%.*s", (int)enc_span.size(), enc_span.data());
@ -214,8 +214,8 @@ static void aes_cbc(const char* message, size_t len)
key_bits,
Crypto::Cipher::Intent::Decryption);
auto dec = cipher.create_aligned_buffer(buffer.size());
auto dec_span = dec.span();
cipher.decrypt(buffer.span(), dec_span, iv.span());
auto dec_span = dec.bytes();
cipher.decrypt(buffer, dec_span, iv);
printf("%.*s\n", (int)dec_span.size(), dec_span.data());
}
}
@ -588,8 +588,8 @@ static void aes_cbc_test_encrypt()
auto in = "This is a test! This is another test!"_b;
auto out = cipher.create_aligned_buffer(in.size());
auto iv = ByteBuffer::create_zeroed(Crypto::Cipher::AESCipher::block_size());
auto out_span = out.span();
cipher.encrypt(in.span(), out_span, iv.span());
auto out_span = out.bytes();
cipher.encrypt(in, out_span, iv);
if (out.size() != sizeof(result))
FAIL(size mismatch);
else if (memcmp(out_span.data(), result, out_span.size()) != 0) {
@ -652,8 +652,8 @@ static void aes_cbc_test_decrypt()
auto in = ByteBuffer::copy(result, result_len);
auto out = cipher.create_aligned_buffer(in.size());
auto iv = ByteBuffer::create_zeroed(Crypto::Cipher::AESCipher::block_size());
auto out_span = out.span();
cipher.decrypt(in.span(), out_span, iv.span());
auto out_span = out.bytes();
cipher.decrypt(in, out_span, iv);
if (out_span.size() != strlen(true_value)) {
FAIL(size mismatch);
printf("Expected %zu bytes but got %zu\n", strlen(true_value), out_span.size());
@ -728,8 +728,8 @@ static void aes_ctr_test_encrypt()
// nonce is already included in ivec.
Crypto::Cipher::AESCipher::CTRMode cipher(key, 8 * key.size(), Crypto::Cipher::Intent::Encryption);
ByteBuffer out_actual = ByteBuffer::create_zeroed(in.size());
Bytes out_span = out_actual.span();
cipher.encrypt(in.span(), out_span, ivec.span());
Bytes out_span = out_actual.bytes();
cipher.encrypt(in, out_span, ivec);
if (out_expected.size() != out_actual.size()) {
FAIL(size mismatch);
printf("Expected %zu bytes but got %zu\n", out_expected.size(), out_span.size());
@ -923,8 +923,8 @@ static void aes_ctr_test_decrypt()
// nonce is already included in ivec.
Crypto::Cipher::AESCipher::CTRMode cipher(key, 8 * key.size(), Crypto::Cipher::Intent::Decryption);
ByteBuffer out_actual = ByteBuffer::create_zeroed(in.size());
auto out_span = out_actual.span();
cipher.decrypt(in.span(), out_span, ivec.span());
auto out_span = out_actual.bytes();
cipher.decrypt(in, out_span, ivec);
if (out_expected.size() != out_span.size()) {
FAIL(size mismatch);
printf("Expected %zu bytes but got %zu\n", out_expected.size(), out_span.size());
@ -1442,7 +1442,7 @@ static void rsa_test_encrypt()
rsa.encrypt(data, buf);
if (memcmp(result, buf.data(), buf.size())) {
FAIL(Invalid encryption result);
print_buffer(buf.span(), 16);
print_buffer(buf, 16);
} else {
PASS;
}