diff --git a/Meta/Lagom/Fuzzers/FuzzTTF.cpp b/Meta/Lagom/Fuzzers/FuzzTTF.cpp index f02962a437..a4214857f2 100644 --- a/Meta/Lagom/Fuzzers/FuzzTTF.cpp +++ b/Meta/Lagom/Fuzzers/FuzzTTF.cpp @@ -8,9 +8,8 @@ #include #include -extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +extern "C" int LLVMFuzzerTestOneInput(u8 const* data, size_t size) { - ByteBuffer font_data = ByteBuffer::copy(data, size); - (void)TTF::Font::try_load_from_memory(font_data); + (void)TTF::Font::try_load_from_externally_owned_memory({ data, size }); return 0; } diff --git a/Userland/Libraries/LibTTF/Font.cpp b/Userland/Libraries/LibTTF/Font.cpp index 0405b6f696..8cc9d2c6f0 100644 --- a/Userland/Libraries/LibTTF/Font.cpp +++ b/Userland/Libraries/LibTTF/Font.cpp @@ -1,11 +1,12 @@ /* * Copyright (c) 2020, Srimanta Barua + * Copyright (c) 2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ -#include "AK/ByteBuffer.h" #include +#include #include #include #include @@ -15,6 +16,7 @@ #include #include #include +#include namespace TTF { @@ -207,19 +209,20 @@ GlyphHorizontalMetrics Hmtx::get_glyph_horizontal_metrics(u32 glyph_id) const Result, String> Font::try_load_from_file(String path, unsigned index) { - auto file_or_error = Core::File::open(move(path), Core::OpenMode::ReadOnly); + auto file_or_error = MappedFile::map(path); if (file_or_error.is_error()) - return file_or_error.error(); + return String { file_or_error.error().string() }; - auto file = file_or_error.value(); - if (!file->open(Core::OpenMode::ReadOnly)) - return String { "Could not open file" }; - - auto buffer = file->read_all(); - return try_load_from_memory(buffer, index); + auto& file = *file_or_error.value(); + auto result = try_load_from_externally_owned_memory(file.bytes(), index); + if (result.is_error()) + return result.error(); + auto& font = *result.value(); + font.m_mapped_file = file_or_error.release_value(); + return result; } -Result, String> Font::try_load_from_memory(ByteBuffer& buffer, unsigned index) +Result, String> Font::try_load_from_externally_owned_memory(ReadonlyBytes buffer, unsigned index) { if (buffer.size() < 4) return String { "Font file too small" }; @@ -243,7 +246,7 @@ Result, String> Font::try_load_from_memory(ByteBuffer& buffe } // FIXME: "loca" and "glyf" are not available for CFF fonts. -Result, String> Font::try_load_from_offset(ByteBuffer&& buffer, u32 offset) +Result, String> Font::try_load_from_offset(ReadonlyBytes buffer, u32 offset) { if (Checked::addition_would_overflow(offset, (u32)Sizes::OffsetTable)) return String { "Invalid offset in font header" }; diff --git a/Userland/Libraries/LibTTF/Font.h b/Userland/Libraries/LibTTF/Font.h index 3db965667b..f7fd922bef 100644 --- a/Userland/Libraries/LibTTF/Font.h +++ b/Userland/Libraries/LibTTF/Font.h @@ -6,7 +6,6 @@ #pragma once -#include #include #include #include @@ -47,7 +46,7 @@ class Font : public RefCounted { public: static Result, String> try_load_from_file(String path, unsigned index = 0); - static Result, String> try_load_from_memory(ByteBuffer&, unsigned index = 0); + static Result, String> try_load_from_externally_owned_memory(ReadonlyBytes bytes, unsigned index = 0); ScaledFontMetrics metrics(float x_scale, float y_scale) const; ScaledGlyphMetrics glyph_metrics(u32 glyph_id, float x_scale, float y_scale) const; @@ -72,9 +71,10 @@ private: TableRecord = 16, }; - static Result, String> try_load_from_offset(ByteBuffer&&, unsigned index = 0); - Font(ByteBuffer&& buffer, Head&& head, Name&& name, Hhea&& hhea, Maxp&& maxp, Hmtx&& hmtx, Cmap&& cmap, Loca&& loca, Glyf&& glyf) - : m_buffer(move(buffer)) + static Result, String> try_load_from_offset(ReadonlyBytes, unsigned index = 0); + + Font(ReadonlyBytes bytes, Head&& head, Name&& name, Hhea&& hhea, Maxp&& maxp, Hmtx&& hmtx, Cmap&& cmap, Loca&& loca, Glyf&& glyf) + : m_buffer(move(bytes)) , m_head(move(head)) , m_name(move(name)) , m_hhea(move(hhea)) @@ -86,8 +86,10 @@ private: { } - // This owns the font data - ByteBuffer m_buffer; + RefPtr m_mapped_file; + + ReadonlyBytes m_buffer; + // These are stateful wrappers around non-owning slices Head m_head; Name m_name;