;; Extracts every CVE ID in UTF-8 text as a CVE.org record URI.
;;
;; A match is `CVE-YYYY-NNNN...` with a case-insensitive prefix, a four-digit
;; year of 1999 or later, and a 4 to 19 digit sequence number, as in the CVE
;; JSON 5 `cveId` pattern. ASCII letters, digits, and `_` must not touch either
;; end of the match. Each match becomes one `text/uri-list` line ending in CRLF,
;; in input order, with duplicates kept. Text without matches gives empty
;; output.
(module $CveExtractAsCveOrgUris
  (memory (export "memory") 6 6)
  (data (i32.const 0) "https://www.cve.org/CVERecord?id=")
  (data (i32.const 64) "text/uri-list")
  (global $prefix_size i32 (i32.const 33))
  (global $input_ptr i32 (i32.const 0x10000))
  (func (export "input_ptr") (result i32)
    (global.get $input_ptr))
  (global $input_utf8_cap i32 (i32.const 0x10000))
  (func (export "input_utf8_cap") (result i32)
    (global.get $input_utf8_cap))
  (global $output_ptr i32 (i32.const 0x20000))
  ;; The densest input is `CVE-1999-0001 ` repeated: 14 input bytes give
  ;; 48 output bytes, so 64 KiB of input needs less than 256 KiB of output.
  (global $output_utf8_cap i32 (i32.const 0x40000))
  (func (export "output_utf8_cap") (result i32)
    (global.get $output_utf8_cap))
  (func (export "output_content_type_ptr") (result i32)
    (i32.const 64))
  (func (export "output_content_type_size") (result i32)
    (i32.const 13))

  (func $byte (param $offset i32) (result i32)
    (i32.load8_u (i32.add (global.get $input_ptr) (local.get $offset))))

  (func $is_digit (param $c i32) (result i32)
    (i32.lt_u (i32.sub (local.get $c) (i32.const 48)) (i32.const 10)))

  (func $is_word (param $c i32) (result i32)
    (i32.or
      (call $is_digit (local.get $c))
      (i32.or
        (i32.lt_u
          (i32.sub (i32.and (local.get $c) (i32.const 0xDF)) (i32.const 65))
          (i32.const 26))
        (i32.eq (local.get $c) (i32.const 95)))))

  (func $digit_at (param $offset i32) (result i32)
    (i32.sub (call $byte (local.get $offset)) (i32.const 48)))

  ;; Returns the sequence-number length of a CVE ID starting at $at, or 0.
  (func $match_length (param $at i32) (param $input_size i32) (result i32)
    (local $j i32)
    (local $limit i32)
    (local $year i32)

    (if (local.get $at)
      (then
        (if (call $is_word (call $byte (i32.sub (local.get $at) (i32.const 1))))
          (then (return (i32.const 0))))))
    (if (i32.ne (i32.and (call $byte (local.get $at)) (i32.const 0xDF)) (i32.const 67))
      (then (return (i32.const 0))))
    (if (i32.ne (i32.and (call $byte (i32.add (local.get $at) (i32.const 1))) (i32.const 0xDF)) (i32.const 86))
      (then (return (i32.const 0))))
    (if (i32.ne (i32.and (call $byte (i32.add (local.get $at) (i32.const 2))) (i32.const 0xDF)) (i32.const 69))
      (then (return (i32.const 0))))
    (if (i32.ne (call $byte (i32.add (local.get $at) (i32.const 3))) (i32.const 45))
      (then (return (i32.const 0))))
    (if (i32.ne (call $byte (i32.add (local.get $at) (i32.const 8))) (i32.const 45))
      (then (return (i32.const 0))))
    (if (i32.eqz
          (i32.and
            (i32.and
              (call $is_digit (call $byte (i32.add (local.get $at) (i32.const 4))))
              (call $is_digit (call $byte (i32.add (local.get $at) (i32.const 5)))))
            (i32.and
              (call $is_digit (call $byte (i32.add (local.get $at) (i32.const 6))))
              (call $is_digit (call $byte (i32.add (local.get $at) (i32.const 7)))))))
      (then (return (i32.const 0))))
    (local.set $year
      (i32.add
        (i32.add
          (i32.mul (call $digit_at (i32.add (local.get $at) (i32.const 4))) (i32.const 1000))
          (i32.mul (call $digit_at (i32.add (local.get $at) (i32.const 5))) (i32.const 100)))
        (i32.add
          (i32.mul (call $digit_at (i32.add (local.get $at) (i32.const 6))) (i32.const 10))
          (call $digit_at (i32.add (local.get $at) (i32.const 7))))))
    (if (i32.lt_u (local.get $year) (i32.const 1999))
      (then (return (i32.const 0))))

    ;; Count at most 20 sequence digits: 20 means too long.
    (local.set $j (i32.add (local.get $at) (i32.const 9)))
    (local.set $limit (i32.add (local.get $at) (i32.const 29)))
    (if (i32.gt_u (local.get $limit) (local.get $input_size))
      (then (local.set $limit (local.get $input_size))))
    (block $done
      (loop $digits
        (br_if $done (i32.ge_u (local.get $j) (local.get $limit)))
        (br_if $done (i32.eqz (call $is_digit (call $byte (local.get $j)))))
        (local.set $j (i32.add (local.get $j) (i32.const 1)))
        (br $digits)))
    (local.set $j (i32.sub (local.get $j) (i32.add (local.get $at) (i32.const 9))))

    (if (i32.or
          (i32.lt_u (local.get $j) (i32.const 4))
          (i32.gt_u (local.get $j) (i32.const 19)))
      (then (return (i32.const 0))))
    (if (i32.lt_u (i32.add (i32.add (local.get $at) (i32.const 9)) (local.get $j)) (local.get $input_size))
      (then
        (if (call $is_word
              (call $byte (i32.add (i32.add (local.get $at) (i32.const 9)) (local.get $j))))
          (then (return (i32.const 0))))))
    (local.get $j))

  (func (export "render") (param $input_size i32) (result i64)
    (local $i i32)
    (local $last_start i32)
    (local $seq_len i32)
    (local $k i32)
    (local $id_size i32)
    (local $out_i i32)
    (local $out_addr i32)

    (if (i32.gt_u (local.get $input_size) (global.get $input_utf8_cap))
      (then unreachable))

    ;; `CVE-YYYY-NNNN` is 13 bytes, so no match can start after this offset.
    (if (i32.ge_u (local.get $input_size) (i32.const 13))
      (then
        (local.set $last_start (i32.sub (local.get $input_size) (i32.const 13)))
        (block $done_scan
          (loop $scan
            (br_if $done_scan (i32.gt_u (local.get $i) (local.get $last_start)))
            (local.set $seq_len (call $match_length (local.get $i) (local.get $input_size)))
            (if (local.get $seq_len)
              (then
                (local.set $id_size (i32.add (i32.const 9) (local.get $seq_len)))
                (if (i32.gt_u
                      (i32.add
                        (local.get $out_i)
                        (i32.add (i32.add (global.get $prefix_size) (local.get $id_size)) (i32.const 2)))
                      (global.get $output_utf8_cap))
                  (then unreachable))
                (local.set $out_addr (i32.add (global.get $output_ptr) (local.get $out_i)))

                (local.set $k (i32.const 0))
                (block $done_prefix
                  (loop $prefix
                    (br_if $done_prefix (i32.ge_u (local.get $k) (global.get $prefix_size)))
                    (i32.store8
                      (i32.add (local.get $out_addr) (local.get $k))
                      (i32.load8_u (local.get $k)))
                    (local.set $k (i32.add (local.get $k) (i32.const 1)))
                    (br $prefix)))
                (local.set $out_addr (i32.add (local.get $out_addr) (global.get $prefix_size)))

                ;; Uppercase `CVE`, then copy `-YYYY-NNNN...` unchanged.
                (i32.store8 (local.get $out_addr) (i32.const 67))
                (i32.store8 (i32.add (local.get $out_addr) (i32.const 1)) (i32.const 86))
                (i32.store8 (i32.add (local.get $out_addr) (i32.const 2)) (i32.const 69))
                (local.set $k (i32.const 3))
                (block $done_id
                  (loop $id
                    (br_if $done_id (i32.ge_u (local.get $k) (local.get $id_size)))
                    (i32.store8
                      (i32.add (local.get $out_addr) (local.get $k))
                      (call $byte (i32.add (local.get $i) (local.get $k))))
                    (local.set $k (i32.add (local.get $k) (i32.const 1)))
                    (br $id)))
                (local.set $out_addr (i32.add (local.get $out_addr) (local.get $id_size)))
                (i32.store8 (local.get $out_addr) (i32.const 13))
                (i32.store8 (i32.add (local.get $out_addr) (i32.const 1)) (i32.const 10))
                (local.set $out_i
                  (i32.add
                    (local.get $out_i)
                    (i32.add (i32.add (global.get $prefix_size) (local.get $id_size)) (i32.const 2))))))
            ;; A match holds only `CVE`, digits, and hyphens, so no other match
            ;; can start inside it. Stepping by one keeps the loop bound simple.
            (local.set $i (i32.add (local.get $i) (i32.const 1)))
            (br $scan)))))

    (if (i32.gt_u (local.get $out_i) (global.get $output_utf8_cap))
      (then unreachable))
    (i64.or
      (i64.shl (i64.extend_i32_u (global.get $output_ptr)) (i64.const 32))
      (i64.extend_i32_u (local.get $out_i))))
)
