1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:37:34 +00:00

Everywhere: Split Error::from_string_literal and Error::from_string_view

Error::from_string_literal now takes direct char const*s, while
Error::from_string_view does what Error::from_string_literal used to do:
taking StringViews. This change will remove the need to insert `sv`
after error strings when returning string literal errors once
StringView(char const*) is removed.

No functional changes.
This commit is contained in:
sin-ack 2022-07-11 17:57:32 +00:00 committed by Andreas Kling
parent c70f45ff44
commit e5f09ea170
51 changed files with 282 additions and 261 deletions

View file

@ -168,22 +168,22 @@ Optional<Name> Name::from_slice(ReadonlyBytes slice)
ErrorOr<Kern> Kern::from_slice(ReadonlyBytes slice)
{
if (slice.size() < sizeof(u32))
return Error::from_string_literal("Invalid kern table header"sv);
return Error::from_string_literal("Invalid kern table header");
// We only support the old (2x u16) version of the header
auto version = be_u16(slice.data());
auto number_of_subtables = be_u16(slice.offset(sizeof(u16)));
if (version != 0)
return Error::from_string_literal("Unsupported kern table version"sv);
return Error::from_string_literal("Unsupported kern table version");
if (number_of_subtables == 0)
return Error::from_string_literal("Kern table does not contain any subtables"sv);
return Error::from_string_literal("Kern table does not contain any subtables");
// Read all subtable offsets
auto subtable_offsets = TRY(FixedArray<size_t>::try_create(number_of_subtables));
size_t offset = 2 * sizeof(u16);
for (size_t i = 0; i < number_of_subtables; ++i) {
if (slice.size() < offset + Sizes::SubtableHeader)
return Error::from_string_literal("Invalid kern subtable header"sv);
return Error::from_string_literal("Invalid kern subtable header");
subtable_offsets[i] = offset;
auto subtable_size = be_u16(slice.offset(offset + sizeof(u16)));
@ -365,22 +365,22 @@ ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_file(String path, unsigned inde
ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_externally_owned_memory(ReadonlyBytes buffer, unsigned index)
{
if (buffer.size() < 4)
return Error::from_string_literal("Font file too small"sv);
return Error::from_string_literal("Font file too small");
u32 tag = be_u32(buffer.data());
if (tag == tag_from_str("ttcf")) {
// It's a font collection
if (buffer.size() < (u32)Sizes::TTCHeaderV1 + sizeof(u32) * (index + 1))
return Error::from_string_literal("Font file too small"sv);
return Error::from_string_literal("Font file too small");
u32 offset = be_u32(buffer.offset_pointer((u32)Sizes::TTCHeaderV1 + sizeof(u32) * index));
return try_load_from_offset(buffer, offset);
}
if (tag == tag_from_str("OTTO"))
return Error::from_string_literal("CFF fonts not supported yet"sv);
return Error::from_string_literal("CFF fonts not supported yet");
if (tag != 0x00010000)
return Error::from_string_literal("Not a valid font"sv);
return Error::from_string_literal("Not a valid font");
return try_load_from_offset(buffer, 0);
}
@ -389,10 +389,10 @@ ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_externally_owned_memory(Readonl
ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_offset(ReadonlyBytes buffer, u32 offset)
{
if (Checked<u32>::addition_would_overflow(offset, (u32)Sizes::OffsetTable))
return Error::from_string_literal("Invalid offset in font header"sv);
return Error::from_string_literal("Invalid offset in font header");
if (buffer.size() < offset + (u32)Sizes::OffsetTable)
return Error::from_string_literal("Font file too small"sv);
return Error::from_string_literal("Font file too small");
Optional<ReadonlyBytes> opt_head_slice = {};
Optional<ReadonlyBytes> opt_name_slice = {};
@ -417,7 +417,7 @@ ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_offset(ReadonlyBytes buffer, u3
auto num_tables = be_u16(buffer.offset_pointer(offset + (u32)Offsets::NumTables));
if (buffer.size() < offset + (u32)Sizes::OffsetTable + num_tables * (u32)Sizes::TableRecord)
return Error::from_string_literal("Font file too small"sv);
return Error::from_string_literal("Font file too small");
for (auto i = 0; i < num_tables; i++) {
u32 record_offset = offset + (u32)Sizes::OffsetTable + i * (u32)Sizes::TableRecord;
@ -426,10 +426,10 @@ ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_offset(ReadonlyBytes buffer, u3
u32 table_length = be_u32(buffer.offset_pointer(record_offset + (u32)Offsets::TableRecord_Length));
if (Checked<u32>::addition_would_overflow(table_offset, table_length))
return Error::from_string_literal("Invalid table offset or length in font"sv);
return Error::from_string_literal("Invalid table offset or length in font");
if (buffer.size() < table_offset + table_length)
return Error::from_string_literal("Font file too small"sv);
return Error::from_string_literal("Font file too small");
auto buffer_here = ReadonlyBytes(buffer.offset_pointer(table_offset), table_length);
@ -458,39 +458,39 @@ ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_offset(ReadonlyBytes buffer, u3
}
if (!opt_head_slice.has_value() || !(opt_head = Head::from_slice(opt_head_slice.value())).has_value())
return Error::from_string_literal("Could not load Head"sv);
return Error::from_string_literal("Could not load Head");
auto head = opt_head.value();
if (!opt_name_slice.has_value() || !(opt_name = Name::from_slice(opt_name_slice.value())).has_value())
return Error::from_string_literal("Could not load Name"sv);
return Error::from_string_literal("Could not load Name");
auto name = opt_name.value();
if (!opt_hhea_slice.has_value() || !(opt_hhea = Hhea::from_slice(opt_hhea_slice.value())).has_value())
return Error::from_string_literal("Could not load Hhea"sv);
return Error::from_string_literal("Could not load Hhea");
auto hhea = opt_hhea.value();
if (!opt_maxp_slice.has_value() || !(opt_maxp = Maxp::from_slice(opt_maxp_slice.value())).has_value())
return Error::from_string_literal("Could not load Maxp"sv);
return Error::from_string_literal("Could not load Maxp");
auto maxp = opt_maxp.value();
if (!opt_hmtx_slice.has_value() || !(opt_hmtx = Hmtx::from_slice(opt_hmtx_slice.value(), maxp.num_glyphs(), hhea.number_of_h_metrics())).has_value())
return Error::from_string_literal("Could not load Hmtx"sv);
return Error::from_string_literal("Could not load Hmtx");
auto hmtx = opt_hmtx.value();
if (!opt_cmap_slice.has_value() || !(opt_cmap = Cmap::from_slice(opt_cmap_slice.value())).has_value())
return Error::from_string_literal("Could not load Cmap"sv);
return Error::from_string_literal("Could not load Cmap");
auto cmap = opt_cmap.value();
if (!opt_loca_slice.has_value() || !(opt_loca = Loca::from_slice(opt_loca_slice.value(), maxp.num_glyphs(), head.index_to_loc_format())).has_value())
return Error::from_string_literal("Could not load Loca"sv);
return Error::from_string_literal("Could not load Loca");
auto loca = opt_loca.value();
if (!opt_glyf_slice.has_value())
return Error::from_string_literal("Could not load Glyf"sv);
return Error::from_string_literal("Could not load Glyf");
auto glyf = Glyf(opt_glyf_slice.value());
if (!opt_os2_slice.has_value())
return Error::from_string_literal("Could not load OS/2"sv);
return Error::from_string_literal("Could not load OS/2");
auto os2 = OS2(opt_os2_slice.value());
Optional<Kern> kern {};
@ -507,7 +507,7 @@ ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_offset(ReadonlyBytes buffer, u3
auto subtable = opt_subtable.value();
auto platform = subtable.platform_id();
if (!platform.has_value())
return Error::from_string_literal("Invalid Platform ID"sv);
return Error::from_string_literal("Invalid Platform ID");
if (platform.value() == Cmap::Subtable::Platform::Windows) {
if (subtable.encoding_id() == (u16)Cmap::Subtable::WindowsEncoding::UnicodeFullRepertoire) {

View file

@ -60,12 +60,12 @@ ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_externally_owned_memory(Readonl
{
// https://www.w3.org/TR/WOFF/#WOFFHeader
if (buffer.size() < WOFF_HEADER_SIZE)
return Error::from_string_literal("WOFF file too small"sv);
return Error::from_string_literal("WOFF file too small");
// The signature field in the WOFF header MUST contain the "magic number" 0x774F4646. If the field does not contain this value, user agents MUST reject the file as invalid.
u32 signature = be_u32(buffer.data());
if (signature != WOFF_SIGNATURE)
return Error::from_string_literal("Invalid WOFF signature"sv);
return Error::from_string_literal("Invalid WOFF signature");
// The flavor field corresponds to the "sfnt version" field found at the beginning of an sfnt file,
// indicating the type of font data contained. Although only fonts of type 0x00010000 (the version number 1.0 as a 16.16 fixed-point value, indicating TrueType glyph data)
// and 0x4F54544F (the tag 'OTTO', indicating CFF glyph data) are widely supported at present,
@ -85,17 +85,17 @@ ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_externally_owned_memory(Readonl
u32 priv_offset = be_u32(buffer.offset(36)); // Offset to private data block, from beginning of WOFF file.
u32 priv_length = be_u32(buffer.offset(40)); // Length of private data block.
if (length > buffer.size())
return Error::from_string_literal("Invalid WOFF length"sv);
return Error::from_string_literal("Invalid WOFF length");
if (reserved != 0)
return Error::from_string_literal("Invalid WOFF reserved field"sv);
return Error::from_string_literal("Invalid WOFF reserved field");
if (meta_length == 0 && meta_offset != 0)
return Error::from_string_literal("Invalid WOFF meta block offset"sv);
return Error::from_string_literal("Invalid WOFF meta block offset");
if (priv_length == 0 && priv_offset != 0)
return Error::from_string_literal("Invalid WOFF private block offset"sv);
return Error::from_string_literal("Invalid WOFF private block offset");
if (WOFF_HEADER_SIZE + num_tables * WOFF_TABLE_SIZE > length)
return Error::from_string_literal("Truncated WOFF table directory"sv);
return Error::from_string_literal("Truncated WOFF table directory");
if (total_sfnt_size > 10 * MiB)
return Error::from_string_literal("Uncompressed font is more than 10 MiB"sv);
return Error::from_string_literal("Uncompressed font is more than 10 MiB");
auto font_buffer = TRY(ByteBuffer::create_zeroed(total_sfnt_size));
// ISO-IEC 14496-22:2019 4.5.1 Offset table
@ -116,19 +116,19 @@ ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_externally_owned_memory(Readonl
u32 orig_checksum = be_u32(buffer.offset(base_offset + 16));
if ((size_t)offset + comp_length > length)
return Error::from_string_literal("Truncated WOFF table"sv);
return Error::from_string_literal("Truncated WOFF table");
if (font_buffer_offset + orig_length > font_buffer.size())
return Error::from_string_literal("Uncompressed WOFF table too big"sv);
return Error::from_string_literal("Uncompressed WOFF table too big");
if (comp_length < orig_length) {
auto decompressed = Compress::Zlib::decompress_all(buffer.slice(offset, comp_length));
if (!decompressed.has_value())
return Error::from_string_literal("Could not decompress WOFF table"sv);
return Error::from_string_literal("Could not decompress WOFF table");
if (orig_length != decompressed->size())
return Error::from_string_literal("Invalid decompressed WOFF table length"sv);
return Error::from_string_literal("Invalid decompressed WOFF table length");
font_buffer.overwrite(font_buffer_offset, decompressed->data(), orig_length);
} else {
if (comp_length != orig_length)
return Error::from_string_literal("Invalid uncompressed WOFF table length"sv);
return Error::from_string_literal("Invalid uncompressed WOFF table length");
font_buffer.overwrite(font_buffer_offset, buffer.data() + offset, orig_length);
}