#!/bin/bash

# Copyright (c) 2025 Cloudflare, Inc.
# Licensed under the Apache 2.0 license found in the LICENSE file or at:
#     https://opensource.org/licenses/Apache-2.0

die() {
    echo "[!] bpf_asm not found. Please put it in your \$PATH"
    echo "[!] you can typically find it in linux/tools/bpf/bpf_asm"
    exit 1
}

which bpf_asm > /dev/null || die

THISFILE=$0
CUTLINENUMBER=$(awk '/^# -- [A]UTOGENERATED/ {print FNR}' $THISFILE)
sed -i "$[CUTLINENUMBER]q" "$THISFILE"

cat <<EOF | bpf_asm -c >> $THISFILE

            ; load first byte to figure out the app number, ASCII 0-3
            ldb [0]
            jlt #0x30, parse_udpgrm_cookie
            jgt #0x33, parse_udpgrm_cookie
            sub #0x30
            or #0x80000000
            ret a

    parse_udpgrm_cookie:
           ; the cookie is starting at offset 1
           ldh [1]
           ret a
EOF
exit
# -- AUTOGENERATED FROM HERE --
{ 0x30,  0,  0, 0000000000 },
{ 0x35,  0,  4, 0x00000030 },
{ 0x25,  3,  0, 0x00000033 },
{ 0x14,  0,  0, 0x00000030 },
{ 0x44,  0,  0, 0x80000000 },
{ 0x16,  0,  0, 0000000000 },
{ 0x28,  0,  0, 0x00000001 },
{ 0x16,  0,  0, 0000000000 },
