Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / mime_sniff / resource_2.lucb

#==============================================================================================
#
#   resource_2 - MIME sniffing: the font and archive byte patterns and the rules for
#                identifying an unknown MIME type
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/MimeSniff/Resource.cpp (lines 446-667): the second half
#       of the anonymous namespace, with the tables as module constants.
#
#       Copyright (c) 2023-2024, Kemal Zebari <kemalzebra@gmail.com>.
#       Copyright (c) 2025, Ben Eidson <b.e.eidson@gmail.com>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Fonts and archives ======================================================================

## match_a_font_type_pattern's `pattern_table`.
let match_a_font_type_pattern_table: Anonymous2[6] = [
    # 34 bytes followed by the string "LP", the Embedded OpenType signature.
    Anonymous2(
        byte_pattern = b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x4C\x50",
        pattern_mask = b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF",
        ignored_leading_bytes = no_ignored_bytes,
        mime_type = "application/vnd.ms-fontobject",
    ),

    # 4 bytes representing the version number 1.0, a TrueType signature.
    Anonymous2(byte_pattern = b"\x00\x01\x00\x00", pattern_mask = b"\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "font/ttf"),

    # The string "OTTO", the OpenType signature.
    Anonymous2(byte_pattern = b"\x4F\x54\x54\x4F", pattern_mask = b"\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "font/otf"),

    # The string "ttcf", the TrueType Collection signature.
    Anonymous2(byte_pattern = b"\x74\x74\x63\x66", pattern_mask = b"\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "font/collection"),

    # The string "wOFF", the Web Open Font Format 1.0 signature.
    Anonymous2(byte_pattern = b"\x77\x4F\x46\x46", pattern_mask = b"\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "font/woff"),

    # The string "wOF2", the Web Open Font Format 2.0 signature.
    Anonymous2(byte_pattern = b"\x77\x4F\x46\x32", pattern_mask = b"\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "font/woff2"),
]

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:447 (anonymous)::match_a_font_type_pattern.
# https://mimesniff.spec.whatwg.org/#matching-a-font-type-pattern
func match_a_font_type_pattern(input: const u8[]) -> MimeType?:
    # 1. Execute the following steps for each row row in the following table:
    return mime_match_pattern_table(input, match_a_font_type_pattern_table)

## match_an_archive_type_pattern's `pattern_table`.
let match_an_archive_type_pattern_table: Anonymous2[3] = [
    # The GZIP archive signature.
    Anonymous2(byte_pattern = b"\x1F\x8B\x08", pattern_mask = b"\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "application/x-gzip"),

    # The string "PK" followed by ETX EOT, the ZIP archive signature.
    Anonymous2(byte_pattern = b"\x50\x4B\x03\x04", pattern_mask = b"\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "application/zip"),

    # The string "Rar!" followed by SUB BEL NUL, the RAR 4.x archive signature.
    Anonymous2(byte_pattern = b"\x52\x61\x72\x21\x1A\x07\x00", pattern_mask = b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "application/x-rar-compressed"),
]

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:491 (anonymous)::match_an_archive_type_pattern.
# https://mimesniff.spec.whatwg.org/#matching-an-archive-type-pattern
func match_an_archive_type_pattern(input: const u8[]) -> MimeType?:
    # 1. Execute the following steps for each row row in the following table:
    return mime_match_pattern_table(input, match_an_archive_type_pattern_table)

# mark: Unknown MIME types ======================================================================

## rules_for_identifying_an_unknown_mime_type's `ignored_whitespace_bytes`.
# https://mimesniff.spec.whatwg.org/#whitespace-byte
# A whitespace byte (abbreviated 0xWS) is any one of the following bytes: 0x09 (HT), 0x0A (LF), 0x0C (FF), 0x0D (CR), 0x20 (SP).
let rules_for_identifying_an_unknown_mime_type_ignored_whitespace_bytes: const u8[] = b"\x09\x0A\x0C\x0D\x20"

## rules_for_identifying_an_unknown_mime_type's first `pattern_table` (the scriptable types).
let rules_for_identifying_an_unknown_mime_type_scriptable_table: Anonymous2[19] = [
    # The case-insensitive string "<!DOCTYPE HTML" followed by a tag-terminating byte.
    Anonymous2(byte_pattern = b"\x3C\x21\x44\x4F\x43\x54\x59\x50\x45\x20\x48\x54\x4D\x4C\x00",
        pattern_mask = b"\xFF\xFF\xDF\xDF\xDF\xDF\xDF\xDF\xDF\xFF\xDF\xDF\xDF\xDF\xFF", ignored_leading_bytes = rules_for_identifying_an_unknown_mime_type_ignored_whitespace_bytes, mime_type = "text/html", is_tag_terminated = true),

    # The case-insensitive string "<HTML" followed by a tag-terminating byte.
    Anonymous2(byte_pattern = b"\x3C\x48\x54\x4D\x4C\x00", pattern_mask = b"\xFF\xDF\xDF\xDF\xDF\xFF", ignored_leading_bytes = rules_for_identifying_an_unknown_mime_type_ignored_whitespace_bytes, mime_type = "text/html", is_tag_terminated = true),

    # The case-insensitive string "<HEAD" followed by a tag-terminating byte.
    Anonymous2(byte_pattern = b"\x3C\x48\x45\x41\x44\x00", pattern_mask = b"\xFF\xDF\xDF\xDF\xDF\xFF", ignored_leading_bytes = rules_for_identifying_an_unknown_mime_type_ignored_whitespace_bytes, mime_type = "text/html", is_tag_terminated = true),

    # The case-insensitive string "<SCRIPT" followed by a tag-terminating byte.
    Anonymous2(byte_pattern = b"\x3C\x53\x43\x52\x49\x50\x54\x00",
        pattern_mask = b"\xFF\xDF\xDF\xDF\xDF\xDF\xDF\xFF", ignored_leading_bytes = rules_for_identifying_an_unknown_mime_type_ignored_whitespace_bytes, mime_type = "text/html", is_tag_terminated = true),

    # The case-insensitive string "<IFRAME" followed by a tag-terminating byte.
    Anonymous2(byte_pattern = b"\x3C\x49\x46\x52\x41\x4D\x45\x00",
        pattern_mask = b"\xFF\xDF\xDF\xDF\xDF\xDF\xDF\xFF", ignored_leading_bytes = rules_for_identifying_an_unknown_mime_type_ignored_whitespace_bytes, mime_type = "text/html", is_tag_terminated = true),

    # The case-insensitive string "<H1" followed by a tag-terminating byte.
    Anonymous2(byte_pattern = b"\x3C\x48\x31\x00", pattern_mask = b"\xFF\xDF\xFF\xFF", ignored_leading_bytes = rules_for_identifying_an_unknown_mime_type_ignored_whitespace_bytes, mime_type = "text/html", is_tag_terminated = true),

    # The case-insensitive string "<DIV" followed by a tag-terminating byte.
    Anonymous2(byte_pattern = b"\x3C\x44\x49\x56\x00", pattern_mask = b"\xFF\xDF\xDF\xDF\xFF", ignored_leading_bytes = rules_for_identifying_an_unknown_mime_type_ignored_whitespace_bytes, mime_type = "text/html", is_tag_terminated = true),

    # The case-insensitive string "<FONT" followed by a tag-terminating byte.
    Anonymous2(byte_pattern = b"\x3C\x46\x4F\x4E\x54\x00", pattern_mask = b"\xFF\xDF\xDF\xDF\xDF\xFF", ignored_leading_bytes = rules_for_identifying_an_unknown_mime_type_ignored_whitespace_bytes, mime_type = "text/html", is_tag_terminated = true),

    # The case-insensitive string "<TABLE" followed by a tag-terminating byte.
    Anonymous2(byte_pattern = b"\x3C\x54\x41\x42\x4C\x45\x00", pattern_mask = b"\xFF\xDF\xDF\xDF\xDF\xDF\xFF", ignored_leading_bytes = rules_for_identifying_an_unknown_mime_type_ignored_whitespace_bytes, mime_type = "text/html", is_tag_terminated = true),

    # The case-insensitive string "<A" followed by a tag-terminating byte.
    Anonymous2(byte_pattern = b"\x3C\x41\x00", pattern_mask = b"\xFF\xDF\xFF", ignored_leading_bytes = rules_for_identifying_an_unknown_mime_type_ignored_whitespace_bytes, mime_type = "text/html", is_tag_terminated = true),

    # The case-insensitive string "<STYLE" followed by a tag-terminating byte.
    Anonymous2(byte_pattern = b"\x3C\x53\x54\x59\x4C\x45\x00",
        pattern_mask = b"\xFF\xDF\xDF\xDF\xDF\xDF\xFF", ignored_leading_bytes = rules_for_identifying_an_unknown_mime_type_ignored_whitespace_bytes, mime_type = "text/html", is_tag_terminated = true),

    # The case-insensitive string "<TITLE" followed by a tag-terminating byte.
    Anonymous2(byte_pattern = b"\x3C\x54\x49\x54\x4C\x45\x00",
        pattern_mask = b"\xFF\xDF\xDF\xDF\xDF\xDF\xFF", ignored_leading_bytes = rules_for_identifying_an_unknown_mime_type_ignored_whitespace_bytes, mime_type = "text/html", is_tag_terminated = true),

    # The case-insensitive string "<B" followed by a tag-terminating byte.
    Anonymous2(byte_pattern = b"\x3C\x42\x00", pattern_mask = b"\xFF\xDF\xFF", ignored_leading_bytes = rules_for_identifying_an_unknown_mime_type_ignored_whitespace_bytes, mime_type = "text/html", is_tag_terminated = true),

    # The case-insensitive string "<BODY" followed by a tag-terminating byte.
    Anonymous2(byte_pattern = b"\x3C\x42\x4F\x44\x59\x00", pattern_mask = b"\xFF\xDF\xDF\xDF\xDF\xFF", ignored_leading_bytes = rules_for_identifying_an_unknown_mime_type_ignored_whitespace_bytes, mime_type = "text/html", is_tag_terminated = true),

    # The case-insensitive string "<BR" followed by a tag-terminating byte.
    Anonymous2(byte_pattern = b"\x3C\x42\x52\x00", pattern_mask = b"\xFF\xDF\xDF\xFF", ignored_leading_bytes = rules_for_identifying_an_unknown_mime_type_ignored_whitespace_bytes, mime_type = "text/html", is_tag_terminated = true),

    # The case-insensitive string "<P" followed by a tag-terminating byte.
    Anonymous2(byte_pattern = b"\x3C\x50\x00", pattern_mask = b"\xFF\xDF\xFF", ignored_leading_bytes = rules_for_identifying_an_unknown_mime_type_ignored_whitespace_bytes, mime_type = "text/html", is_tag_terminated = true),

    # The string "<!--" followed by a tag-terminating byte.
    Anonymous2(byte_pattern = b"\x3C\x21\x2D\x2D\x00", pattern_mask = b"\xFF\xFF\xFF\xFF\xFF", ignored_leading_bytes = rules_for_identifying_an_unknown_mime_type_ignored_whitespace_bytes, mime_type = "text/html", is_tag_terminated = true),

    # The string "<?xml".
    Anonymous2(byte_pattern = b"\x3C\x3F\x78\x6D\x6C", pattern_mask = b"\xFF\xFF\xFF\xFF\xFF", ignored_leading_bytes = rules_for_identifying_an_unknown_mime_type_ignored_whitespace_bytes, mime_type = "text/xml"),

    # The string "%PDF-", the PDF signature.
    Anonymous2(byte_pattern = b"\x25\x50\x44\x46\x2D", pattern_mask = b"\xFF\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "application/pdf"),
]

## rules_for_identifying_an_unknown_mime_type's second `pattern_table`.
let rules_for_identifying_an_unknown_mime_type_table: Anonymous2[4] = [
    # The string "%!PS-Adobe-", the PostScript signature.
    Anonymous2(byte_pattern = b"\x25\x21\x50\x53\x2D\x41\x64\x6F\x62\x65\x2D",
        pattern_mask = b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "application/postscript"),

    # UTF-16BE BOM
    Anonymous2(byte_pattern = b"\xFE\xFF\x00\x00", pattern_mask = b"\xFF\xFF\x00\x00", ignored_leading_bytes = no_ignored_bytes, mime_type = "text/plain"),

    # UTF-16LE BOM
    Anonymous2(byte_pattern = b"\xFF\xFE\x00\x00", pattern_mask = b"\xFF\xFF\x00\x00", ignored_leading_bytes = no_ignored_bytes, mime_type = "text/plain"),

    # UTF-8 BOM
    Anonymous2(byte_pattern = b"\xEF\xBB\xBF\x00", pattern_mask = b"\xFF\xFF\xFF\x00", ignored_leading_bytes = no_ignored_bytes, mime_type = "text/plain"),
]

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:521 (anonymous)::rules_for_identifying_an_unknown_mime_type.
# https://mimesniff.spec.whatwg.org/#rules-for-identifying-an-unknown-mime-type
func rules_for_identifying_an_unknown_mime_type(resource: const MimeResource*, sniff_scriptable: bool = false) -> MimeType:
    # 1. If the sniff-scriptable flag is set, execute the following steps for each row row in the following table:
    if sniff_scriptable:
        for row in rules_for_identifying_an_unknown_mime_type_scriptable_table:
            # 1. Let patternMatched be the result of the pattern matching algorithm given resource’s resource header,
            #    the value in the first column of row, the value in the second column of row, and the value in the
            #    third column of row.
            let pattern_matched = pattern_matching_algorithm(mime_resource_resource_header(resource), row.byte_pattern, row.pattern_mask, row.ignored_leading_bytes, row.is_tag_terminated)

            # 2. If patternMatched is true, return the value in the fourth column of row.
            if pattern_matched:
                if let maybe_type = mime_type_parse(ak.sv(row.mime_type)):
                    return maybe_type

    # 2. Execute the following steps for each row row in the following table:
    for row in rules_for_identifying_an_unknown_mime_type_table:
        # 1. Let patternMatched be the result of the pattern matching algorithm given resource’s resource header,
        #    the value in the first column of row, the value in the second column of row, and the value in the
        #    third column of row.
        let pattern_matched = pattern_matching_algorithm(mime_resource_resource_header(resource), row.byte_pattern, row.pattern_mask, row.ignored_leading_bytes)

        # 2. If patternMatched is true, return the value in the fourth column of row.
        if pattern_matched:
            if let maybe_type = mime_type_parse(ak.sv(row.mime_type)):
                return maybe_type

    # 3. Let matchedType be the result of executing the image type pattern matching algorithm given resource’s resource header.
    var matched_type = match_an_image_type_pattern(mime_resource_resource_header(resource))

    # 4. If matchedType is not undefined, return matchedType.
    if let type_ = matched_type:
        return type_

    # 5. Set matchedType to the result of executing the audio or video type pattern matching algorithm given resource’s resource header.
    matched_type = match_an_audio_or_video_type_pattern(mime_resource_resource_header(resource))

    # 6. If matchedType is not undefined, return matchedType.
    if let type_ = matched_type:
        return type_

    # 7. Set matchedType to the result of executing the archive type pattern matching algorithm given resource’s resource header.
    matched_type = match_an_archive_type_pattern(mime_resource_resource_header(resource))

    # 8. If matchedType is not undefined, return matchedType.
    if let type_ = matched_type:
        return type_

    # 9. If resource’s resource header contains no binary data bytes, return "text/plain".
    if not mime_bytes_contain_binary_data_byte(mime_resource_resource_header(resource)):
        return mime_type_create(ak.string_from_str("text"), ak.string_from_str("plain"))

    # 10. Return "application/octet-stream".
    return mime_type_create(ak.string_from_str("application"), ak.string_from_str("octet-stream"))