Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / mime_sniff / resource_1.lucb

#==============================================================================================
#
#   resource_1 - MIME sniffing: the pattern matching algorithm, the image and audio/video
#                byte patterns and the media signatures
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/MimeSniff/Resource.cpp (lines 1-445): the first half of the
#       functions of the anonymous namespace; resource_2 has the rest. The C++ `static constexpr` tables inside the functions are module
#       constants here, named after the function that owns them.
#
#       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: Bytes ===================================================================================

## `a == b` for two byte spans (C++ Span's operator==).
func mime_bytes_equal(a: const u8[], b: const u8[]) -> bool:
    if a.length != b.length:
        return false
    for i in 0..<a.length:
        if a[i] != b[i]:
            return false
    return true

## `span.starts_with(prefix)` (C++ Span::starts_with).
func mime_bytes_start_with(span: const u8[], prefix: const u8[]) -> bool:
    if span.length < prefix.length:
        return false
    return mime_bytes_equal(span[0..<prefix.length], prefix)

## `span.contains_slow(byte)`.
func mime_bytes_contain(span: const u8[], byte: u8) -> bool:
    for b in span:
        if b == byte:
            return true
    return false

## `any_of(span, is_binary_data_byte)`.
func mime_bytes_contain_binary_data_byte(span: const u8[]) -> bool:
    for byte in span:
        if is_binary_data_byte(byte):
            return true
    return false

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:27 (anonymous)::is_tag_terminating_byte.
# https://mimesniff.spec.whatwg.org/#tag-terminating-byte
func is_tag_terminating_byte(byte: u8) -> bool:
    # A tag-terminating byte (abbreviated 0xTT) is any one of the following bytes: 0x20 (SP), 0x3E (">").
    return byte == 0x20 or byte == 0x3E

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:34 (anonymous)::is_binary_data_byte.
# https://mimesniff.spec.whatwg.org/#binary-data-byte
func is_binary_data_byte(byte: u8) -> bool:
    #  A binary data byte is a byte in the range 0x00 to 0x08 (NUL to BS), the byte 0x0B (VT), a byte in
    #  the range 0x0E to 0x1A (SO to SUB), or a byte in the range 0x1C to 0x1F (FS to US).
    return (byte <= 0x08) or byte == 0x0B or (byte >= 0x0E and byte <= 0x1A) or (byte >= 0x1C and byte <= 0x1F)

# mark: The pattern matching algorithm ==========================================================

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:42 (anonymous)::pattern_matching_algorithm.
# https://mimesniff.spec.whatwg.org/#pattern-matching-algorithm
func pattern_matching_algorithm(input: const u8[], pattern: const u8[], mask: const u8[], ignored: const u8[], is_tag_terminated: bool = false) -> bool:
    # 1. Assert: pattern’s length is equal to mask’s length.
    assert(pattern.length == mask.length)

    # 2. If input’s length is less than pattern’s length, return false.
    if input.length < pattern.length:
        return false

    # 3. Let s be 0.
    var s: usize = 0

    # 4. While s < input’s length:
    while s < input.length:
        # 1. If ignored does not contain input[s], break.
        if not mime_bytes_contain(ignored, input[s]):
            break

        # 2. Set s to s + 1.
        s += 1

    # 5. Let p be 0.
    var p: usize = 0

    # 6. While p < pattern’s length:
    while p < pattern.length:
        # 1. Let maskedData be the result of applying the bitwise AND operator to input[s] and mask[p].
        let masked_data = input[s] & mask[p]

        # NOTE: This non-standard branch exists to avoid having to create 2 byte patterns just so that
        #       they can only differ by their tag-terminating byte (which could be a 0x20 or 0x3E byte).
        if is_tag_terminated and p + 1 == pattern.length:
            return is_tag_terminating_byte(masked_data)

        # 2. If maskedData is not equal to pattern[p], return false.
        if masked_data != pattern[p]:
            return false

        # 3. Set s to s + 1.
        s += 1

        # 4. Set p to p + 1.
        p += 1

    # 7. Return true.
    return true

## The first row of `table` whose pattern matches `input`, parsed as a MIME type (the loop of
## every "match a ... type pattern" algorithm).
func mime_match_pattern_table(input: const u8[], table: const Anonymous2[]) -> MimeType?:
    for row in table:
        # 1. Let patternMatched be the result of the pattern matching algorithm given input, 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(input, 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:
            return mime_type_parse(ak.sv(row.mime_type))

    # 2. Return undefined.
    return none

# mark: Images ==================================================================================

## match_an_image_type_pattern's `pattern_table`.
let match_an_image_type_pattern_table: Anonymous2[8] = [
    # A Windows Icon signature.
    Anonymous2(byte_pattern = b"\x00\x00\x01\x00", pattern_mask = b"\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "image/x-icon"),

    # A Windows Cursor signature.
    Anonymous2(byte_pattern = b"\x00\x00\x02\x00", pattern_mask = b"\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "image/x-icon"),

    # The string "BM", a BMP signature.
    Anonymous2(byte_pattern = b"\x42\x4D", pattern_mask = b"\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "image/bmp"),

    # The string "GIF87a", a GIF signature.
    Anonymous2(byte_pattern = b"\x47\x49\x46\x38\x37\x61", pattern_mask = b"\xFF\xFF\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "image/gif"),

    # The string "GIF89a", a GIF signature.
    Anonymous2(byte_pattern = b"\x47\x49\x46\x38\x39\x61", pattern_mask = b"\xFF\xFF\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "image/gif"),

    # The string "RIFF" followed by four bytes followed by the string "WEBPVP".
    Anonymous2(byte_pattern = b"\x52\x49\x46\x46\x00\x00\x00\x00\x57\x45\x42\x50\x56\x50",
        pattern_mask = b"\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "image/webp"),

    # An error-checking byte followed by the string "PNG" followed by CR LF SUB LF, the PNG signature.
    Anonymous2(byte_pattern = b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", pattern_mask = b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "image/png"),

    # The JPEG Start of Image marker followed by the indicator byte of another marker.
    Anonymous2(byte_pattern = b"\xFF\xD8\xFF", pattern_mask = b"\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "image/jpeg"),
]

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:95 (anonymous)::match_an_image_type_pattern.
# https://mimesniff.spec.whatwg.org/#matching-an-image-type-pattern
func match_an_image_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_image_type_pattern_table)

# mark: Audio and video =========================================================================

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:141 (anonymous)::matches_mp4_signature.
# https://mimesniff.spec.whatwg.org/#signature-for-mp4
func matches_mp4_signature(sequence: const u8[]) -> bool:
    # 1. Let sequence be the byte sequence to be matched, where sequence[s] is byte s in sequence and sequence[0] is the first byte in sequence.

    # 2. Let length be the number of bytes in sequence.
    let length = sequence.length

    # 3. If length is less than 12, return false.
    if length < 12:
        return false

    # 4. Let box-size be the four bytes from sequence[0] to sequence[3], interpreted as a 32-bit unsigned big-endian integer.
    var box_size: u32 = 0
    box_size |= (u32)sequence[0] << 24
    box_size |= (u32)sequence[1] << 16
    box_size |= (u32)sequence[2] << 8
    box_size |= sequence[3]

    # 5. If length is less than box-size or if box-size modulo 4 is not equal to 0, return false.
    if (length < box_size) or (box_size % 4 != 0):
        return false

    # 6. If the four bytes from sequence[4] to sequence[7] are not equal to 0x66 0x74 0x79 0x70 ("ftyp"), return false.
    if not mime_bytes_equal(sequence[4..<8], b"\x66\x74\x79\x70"):
        return false

    # 7. If the three bytes from sequence[8] to sequence[10] are equal to 0x6D 0x70 0x34 ("mp4"), return true.
    if mime_bytes_equal(sequence[8..<11], b"\x6D\x70\x34"):
        return true

    # 8. Let bytes-read be 16.
    var bytes_read: u32 = 16

    # 9. While bytes-read is less than box-size, continuously loop through these steps:
    #      1. If the three bytes from sequence[bytes-read] to sequence[bytes-read + 2] are equal to 0x6D 0x70 0x34 ("mp4"), return true.
    #      2. Increment bytes-read by 4.
    while bytes_read < box_size:
        if mime_bytes_equal(sequence[(usize)bytes_read..<(usize)bytes_read + 3], b"\x6D\x70\x34"):
            return true
        bytes_read += 4

    # 10. Return false.
    return false

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:193 (anonymous)::parse_a_vint.
# https://mimesniff.spec.whatwg.org/#parse-a-vint
func parse_a_vint(whole_sequence: const u8[], iter: usize) -> Anonymous:
    # To parse a vint on a byte sequence sequence of size length, starting at index iter use the following steps:
    let sequence = whole_sequence[iter..]
    let length = sequence.length

    # 1. let mask be 128.
    var mask: u8 = 128

    # 2. Let max vint length be 8.
    let max_vint_length: usize = 8

    # 3. Let number size be 1.
    var number_size: usize = 1

    # 5. Let index be 0.
    # NOTE: spec declares this after step four, but it is needed before to use in the loop.
    var index: usize = 0

    # 4. While number size is less than max vint length, and less than length, continuously loop through these steps:
    while number_size < max_vint_length and number_size < length:
        # 1. If the sequence[index] & mask is not zero, abort these steps.
        if (sequence[index] & mask) != 0:
            break

        # 2. Let mask be the value of mask >> 1.
        mask >>= 1

        # 3. Increment number size by one.
        number_size += 1

    # 6. Let parsed number be sequence[index] & ~mask.
    var parsed_number: u64 = sequence[index] & ~mask

    # 7. Increment index by one.
    index += 1

    # 8. Let bytes remaining be the value of number size - 1.
    var bytes_remaining = number_size - 1

    # 9. While bytes remaining is not zero, execute there steps:
    while bytes_remaining != 0:
        # 1. Let parsed number be parsed number << 8.
        parsed_number <<= 8

        # 2. Let parsed number be parsed number | sequence[index].
        parsed_number |= sequence[index]

        # 3. Increment index by one.
        index += 1

        # 4. If index is greater or equal than length, abort these steps.
        if index >= length:
            break

        # 5. Decrement bytes remaining by one.
        bytes_remaining -= 1

    # 10. Return parsed number and number size
    return Anonymous(value = parsed_number, length = number_size)

## matches_webm_signature's `webm_signature`.
let matches_webm_signature_webm_signature: const u8[] = b"\x1A\x45\xDF\xA3"

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:258 (anonymous)::matches_webm_signature.
# https://mimesniff.spec.whatwg.org/#signature-for-webm
func matches_webm_signature(sequence: const u8[]) -> bool:
    # 1. Let sequence be the byte sequence to be matched, where sequence[s] is byte s in sequence and sequence[0] is the first byte in sequence.

    # 2. Let length be the number of bytes in sequence.
    let length = sequence.length

    # 3. If length is less than 4, return false.
    if length < 4:
        return false

    # 4. If the four bytes from sequence[0] to sequence[3], are not equal to 0x1A 0x45 0xDF 0xA3, return false.
    if not mime_bytes_start_with(sequence, matches_webm_signature_webm_signature):
        return false

    # 5. let iter be 4.
    var iter: usize = 4

    # 6. While iter is less than length and iter is less than 38, continuously loop through these steps:
    while iter < length and iter < 38:
        # before slicing 2 bytes, ensure there are at least 2 bytes
        if iter + 1 >= length:
            break

        # 1. If the two bytes from sequence[iter] to sequence[iter + 1] are equal to 0x42 0x82,
        if mime_bytes_equal(sequence[iter..<iter + 2], b"\x42\x82"):
            # 1. increment iter by 2.
            iter += 2

            # 2. If iter is greater or equal than length, abort these steps.
            if iter >= length:
                break

            # 3. Let number size be the result of parsing a vint starting at sequence[iter].

            let number_size = parse_a_vint(sequence, iter).length

            # 4. Increment iter by number size.
            iter += number_size

            # 5. If iter is greater than or equal to length - 4, abort these steps.
            if iter >= length - 4:
                break

            # 6. Let matched be the result of matching a padded sequence 0x77 0x65 0x62 0x6D ("webm") on sequence at offset iter.
            let matched = mime_bytes_equal(sequence[iter..<iter + 4], b"\x77\x65\x62\x6D")

            # 7. If matched is true, abort these steps and return true.
            if matched:
                return true

        # 2. Increment iter by 1.
        iter += 1

    # 7. Return false.
    return false

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:320 (anonymous)::match_mp3_header.
# https://mimesniff.spec.whatwg.org/#match-an-mp3-header
func match_mp3_header(sequence: const u8[], s: usize) -> bool:
    # To match an mp3 header, using a byte sequence sequence of length length at offset s execute these steps:
    let length = sequence.length

    # 1. If length is less than 4, return false.
    if length -% s < 4:
        return false

    # 2. If sequence[s] is not equal to 0xff and sequence[s + 1] & 0xe0 is not equal to 0xe0, return false.
    # NOTE: spec is wrong here, should be or.
    if sequence[s] != 0xff or (sequence[s + 1] & 0xe0) != 0xe0:
        return false

    # 3. Let layer be the result of sequence[s + 1] & 0x06 >> 1.
    let layer = (sequence[s + 1] & 0x06) >> 1

    # 4. If layer is 0, return false.
    if layer == 0:
        return false

    # 5. Let bit-rate be sequence[s + 2] & 0xf0 >> 4.
    let bit_rate = (sequence[s + 2] & 0xf0) >> 4

    # 6. If bit-rate is 15, return false.
    if bit_rate == 15:
        return false

    # 7. Let sample-rate be sequence[s + 2] & 0x0c >> 2.
    let sample_rate = (sequence[s + 2] & 0x0c) >> 2

    # 8. If sample-rate is 3, return false.
    if sample_rate == 3:
        return false

    # FIXME: 9. Let freq be the value given by sample-rate in the table sample-rate.

    # FIXME: 10. Let final-layer be the result of 4 - (sequence[s + 1]).

    # FIXME: 11. If final-layer & 0x06 >> 1 is not 3, return false.

    # 12. Return true.
    return true

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:367 (anonymous)::matches_mp3_no_id3_signature.
# https://mimesniff.spec.whatwg.org/#matches-the-signature-for-mp3-without-id3
# FIXME: Instead of implementing full spec, only matches single-frame mp3 header due to the issues stated at (https://github.com/whatwg/mimesniff/issues/70)
func matches_mp3_no_id3_signature(sequence: const u8[]) -> bool:
    # 1. Let sequence be the byte sequence to be matched, where sequence[s] is byte s in sequence and sequence[0] is the first byte in sequence.

    # FIXME: 2. Let length be the number of bytes in sequence.

    # FIXME: 3. Initialize s to 0.

    # 4. If the result of the operation match mp3 header is false, return false.
    let result = match_mp3_header(sequence, 0)
    if not result:
        return false

    # FIXME: 5. Parse an mp3 frame on sequence at offset s

    # FIXME: 6. Let skipped-bytes the return value of the execution of mp3 framesize computation

    # FIXME: 7. If skipped-bytes is less than 4, or skipped-bytes is greater than s - length, return false.

    # FIXME: 8. Increment s by skipped-bytes.

    # FIXME: 9. If the result of the operation match mp3 header operation is false, return false, else, return true.
    return true

## match_an_audio_or_video_type_pattern's `pattern_table`.
let match_an_audio_or_video_type_pattern_table: Anonymous2[6] = [
    # The string "FORM" followed by four bytes followed by the string "AIFF", the AIFF signature.
    Anonymous2(byte_pattern = b"\x46\x4F\x52\x4D\x00\x00\x00\x00\x41\x49\x46\x46",
        pattern_mask = b"\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "audio/aiff"),

    #  The string "ID3", the ID3v2-tagged MP3 signature.
    Anonymous2(byte_pattern = b"\x49\x44\x33", pattern_mask = b"\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "audio/mpeg"),

    # The string "OggS" followed by NUL, the Ogg container signature.
    Anonymous2(byte_pattern = b"\x4F\x67\x67\x53\x00", pattern_mask = b"\xFF\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "application/ogg"),

    # The string "MThd" followed by four bytes representing the number 6 in 32 bits (big-endian), the MIDI signature.
    Anonymous2(byte_pattern = b"\x4D\x54\x68\x64\x00\x00\x00\x06", pattern_mask = b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "audio/midi"),

    # The string "RIFF" followed by four bytes followed by the string "AVI ", the AVI signature.
    Anonymous2(byte_pattern = b"\x52\x49\x46\x46\x00\x00\x00\x00\x41\x56\x49\x20",
        pattern_mask = b"\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "video/avi"),

    # The string "RIFF" followed by four bytes followed by the string "WAVE", the WAVE signature.
    Anonymous2(byte_pattern = b"\x52\x49\x46\x46\x00\x00\x00\x00\x57\x41\x56\x45",
        pattern_mask = b"\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF", ignored_leading_bytes = no_ignored_bytes, mime_type = "audio/wave"),
]

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:393 (anonymous)::match_an_audio_or_video_type_pattern.
# https://mimesniff.spec.whatwg.org/#matching-an-audio-or-video-type-pattern
func match_an_audio_or_video_type_pattern(input: const u8[]) -> MimeType?:
    # 1. Execute the following steps for each row row in the following table:
    if let matched = mime_match_pattern_table(input, match_an_audio_or_video_type_pattern_table):
        return matched

    # 2. If input matches the signature for MP4, return "video/mp4".
    if matches_mp4_signature(input):
        return mime_type_create(ak.string_from_str("video"), ak.string_from_str("mp4"))

    # 3. If input matches the signature for WebM, return "video/webm".
    if matches_webm_signature(input):
        return mime_type_create(ak.string_from_str("video"), ak.string_from_str("webm"))

    # 4. If input matches the signature for MP3 without ID3, return "audio/mpeg".
    if matches_mp3_no_id3_signature(input):
        return mime_type_create(ak.string_from_str("audio"), ak.string_from_str("mpeg"))

    # 5. Return undefined.
    return none