// File: night-shift[P]Srl/src/lib/ToolsBtsConv.cpp
// Function: Srl::Tools::hex_to_bytes(unsigned char*, unsigned long, unsigned char const*, unsigned long)
void Tools::hex_to_bytes(uint8_t* dest, size_t dest_size, const uint8_t* str, size_t str_len)
{
    if(str_len % 2 != 0) {
        throw Exception("Unable to convert hex string to binary data. Size of source must be even.");
    }

    if(dest_size < str_len / 2) {
        throw Exception("Unable to convert hex string to binary data. Destination buffer too small.");
    }

    const auto conv = [](uint8_t& c) {
        if(c >= '0' && c <= '9') {
            c -= '0';

        } else if(c >= 'A' && c <= 'F') {
            c = (c - 'A') + 10;

        } else if(c >= 'a' && c <= 'f') {
            c = (c - 'a') + 10;

        } else {
            throw Exception("Unable to convert hex string to binary data. String malformed.");
        }
    };

    uint8_t low = 0, high = 0;
    uint8_t packed = 0;

    for (size_t in_idx = 0, out_idx = 0; in_idx < str_len;
                in_idx += 2, out_idx++) {

        high = str[in_idx];
        low  = str[in_idx + 1];

        conv(high);
        conv(low);

        packed = (high << 4U) | low;
        dest[out_idx] = packed;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x20(%rbp), %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x62565
movb $0x1, -0x55(%rbp)
movl $0x28, %edi
callq 0x34160
movq %rax, -0xa0(%rbp)
leaq -0x41(%rbp), %rdi
movq %rdi, -0x98(%rbp)
callq 0x38240
movq -0x98(%rbp), %rdx
leaq 0x7bbe(%rip), %rsi      # 0x6a09e
leaq -0x40(%rbp), %rdi
callq 0x37f40
jmp 0x624eb
movq -0xa0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x33910
jmp 0x624fd
movq -0xa0(%rbp), %rdi
movb $0x0, -0x55(%rbp)
movq 0x20ad9(%rip), %rsi     # 0x82fe8
movq 0x20a22(%rip), %rdx     # 0x82f38
callq 0x37fc0
jmp 0x626fd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x62543
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x38ce0
leaq -0x41(%rbp), %rdi
callq 0x35f90
testb $0x1, -0x55(%rbp)
jne 0x62554
jmp 0x62560
movq -0xa0(%rbp), %rdi
callq 0x34ce0
jmp 0x626f4
movq -0x10(%rbp), %rax
movq -0x20(%rbp), %rcx
shrq %rcx
cmpq %rcx, %rax
jae 0x62631
movb $0x1, -0x7a(%rbp)
movl $0x28, %edi
callq 0x34160
movq %rax, -0xb0(%rbp)
leaq -0x79(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x38240
movq -0xa8(%rbp), %rdx
leaq 0x7b3c(%rip), %rsi      # 0x6a0e8
leaq -0x78(%rbp), %rdi
callq 0x37f40
jmp 0x625b7
movq -0xb0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x33910
jmp 0x625c9
movq -0xb0(%rbp), %rdi
movb $0x0, -0x7a(%rbp)
movq 0x20a0d(%rip), %rsi     # 0x82fe8
movq 0x20956(%rip), %rdx     # 0x82f38
callq 0x37fc0
jmp 0x626fd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x6260f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x38ce0
leaq -0x79(%rbp), %rdi
callq 0x35f90
testb $0x1, -0x7a(%rbp)
jne 0x62620
jmp 0x6262c
movq -0xb0(%rbp), %rdi
callq 0x34ce0
jmp 0x626f4
movb $0x0, -0x7c(%rbp)
movb $0x0, -0x7d(%rbp)
movb $0x0, -0x7e(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x626eb
movq -0x18(%rbp), %rax
movq -0x88(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x7d(%rbp)
movq -0x18(%rbp), %rax
movq -0x88(%rbp), %rcx
movb 0x1(%rax,%rcx), %al
movb %al, -0x7c(%rbp)
leaq -0x7b(%rbp), %rdi
leaq -0x7d(%rbp), %rsi
callq 0x62700
leaq -0x7b(%rbp), %rdi
leaq -0x7c(%rbp), %rsi
callq 0x62700
movzbl -0x7d(%rbp), %eax
shll $0x4, %eax
movzbl -0x7c(%rbp), %ecx
orl %ecx, %eax
movb %al, -0x7e(%rbp)
movb -0x7e(%rbp), %dl
movq -0x8(%rbp), %rax
movq -0x90(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x88(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x88(%rbp)
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0x62653
addq $0xb0, %rsp
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x38210
nopl (%rax)