// File: /workspace/github_cpp_makefile_star2_O3/night-shift[P]Srl/src/lib/ToolsBtsConv.cpp
// Function: Srl::Tools::base64_to_bytes(unsigned char*, unsigned long, char const*, unsigned long)
size_t Tools::base64_to_bytes(uint8_t* dest, size_t dest_size, const char* str, size_t str_len)
{
    Base64Table table;

    if(str_len > 0 && str_len % 4 != 0) {
        throw Exception("Unable to convert base64 string to binary data." \
                        " Size of source must be a multiple of 4." );
    }

    for(size_t in_idx = 0, out_idx = 0; out_idx < dest_size;
               out_idx += 3, in_idx += 4) {

        uint8_t b0 = table.decode[ (size_t) str[in_idx] ],
                b1 = table.decode[ (size_t) str[in_idx + 1] ],
                b2 = table.decode[ (size_t) str[in_idx + 2] ],
                b3 = table.decode[ (size_t) str[in_idx + 3] ];

        if(b0 == table.val_invalid || b1 == table.val_invalid ||
           b2 == table.val_invalid || b3 == table.val_invalid) {
            throw Exception("Unable to convert base64 string to binary data. String malformed.");
        }
        /* [BBBBBB BB]BBBB BBBBBB BBBBBB */
        dest[out_idx] = b0 << 2 | b1 >> 4;
        /* PPPPPP PP[BBBB BBBB]BB BBBBBB */
        if(out_idx + 1 < dest_size) {
            dest[out_idx + 1] = b1 << 4 | b2 >> 2;
        }
        /* PPPPPP PPPPPP PPPP[BB BBBBBB] */
        if(out_idx + 2 < dest_size) {
            dest[out_idx + 2] = b2 << 6 | b3;
        }
    }

    return get_base64_decoded_size(str, str_len);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
leaq 0x68(%rsp), %r13
movw $0xfeff, -0x42(%r13)    # imm = 0xFEFF
movups 0x387e(%rip), %xmm0     # 0x1ef80
movups %xmm0, -0x40(%r13)
movups 0x3882(%rip), %xmm0     # 0x1ef90
movups %xmm0, -0x30(%r13)
movups 0x3886(%rip), %xmm0     # 0x1efa0
movups %xmm0, -0x20(%r13)
movups 0x388a(%rip), %xmm0     # 0x1efb0
movups %xmm0, -0x10(%r13)
movl $0x100, %edx            # imm = 0x100
movq %r13, %rdi
movl $0xff, %esi
callq 0xb420
movb $-0x2, 0x3d(%r13)
xorl %eax, %eax
movsbq 0x28(%rsp,%rax), %rcx
movb %al, 0x68(%rsp,%rcx)
incq %rax
cmpq $0x40, %rax
jne 0x1b744
testb $0x3, %bl
jne 0x1b8ab
testq %r15, %r15
je 0x1b817
movb 0x26(%rsp), %al
leaq 0x3(%r14), %rcx
movl $0x2, %edx
movsbq -0x3(%rcx), %rsi
movb 0x68(%rsp,%rsi), %r9b
cmpb %al, %r9b
je 0x1b857
movsbq -0x2(%rcx), %rsi
movb 0x68(%rsp,%rsi), %dil
cmpb %al, %dil
je 0x1b857
movsbq -0x1(%rcx), %rsi
movb 0x68(%rsp,%rsi), %sil
cmpb %al, %sil
je 0x1b857
movsbq (%rcx), %r8
movb 0x68(%rsp,%r8), %r8b
cmpb %al, %r8b
je 0x1b857
shlb $0x2, %r9b
movl %edi, %r10d
shrb $0x4, %r10b
orb %r9b, %r10b
movb %r10b, -0x2(%r12,%rdx)
leaq -0x1(%rdx), %r9
cmpq %r15, %r9
jae 0x1b7f0
shlb $0x4, %dil
movl %esi, %r9d
shrb $0x2, %r9b
orb %dil, %r9b
movb %r9b, -0x1(%r12,%rdx)
cmpq %r15, %rdx
jae 0x1b800
shlb $0x6, %sil
orb %sil, %r8b
movb %r8b, (%r12,%rdx)
addq $0x4, %rcx
leaq 0x3(%rdx), %rsi
incq %rdx
cmpq %r15, %rdx
movq %rsi, %rdx
jb 0x1b776
cmpq $0x4, %rbx
jae 0x1b821
xorl %eax, %eax
jmp 0x1b845
xorl %ecx, %ecx
cmpb $0x3d, -0x1(%r14,%rbx)
sete %cl
xorl %edx, %edx
cmpb $0x3d, -0x2(%r14,%rbx)
sete %dl
shrq $0x2, %rbx
leaq (%rbx,%rbx,2), %rax
subq %rcx, %rax
subq %rdx, %rax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x28, %edi
callq 0xb3b0
movq %rax, %rbx
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x35af(%rip), %rsi      # 0x1ee23
leaq 0x35e9(%rip), %rdx      # 0x1ee64
movq %rsp, %rdi
callq 0xb2e0
movb $0x1, %bpl
movq %rsp, %rsi
movq %rbx, %rdi
callq 0xb1d0
xorl %ebp, %ebp
movq 0xb74e(%rip), %rsi      # 0x26fe8
movq 0xb6a7(%rip), %rdx      # 0x26f48
movq %rbx, %rdi
callq 0xbe60
jmp 0x1b8fd
movl $0x28, %edi
callq 0xb3b0
movq %rax, %rbx
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x3503(%rip), %rsi      # 0x1edcb
leaq 0x3553(%rip), %rdx      # 0x1ee22
movq %rsp, %rdi
callq 0xb2e0
movb $0x1, %bpl
movq %rsp, %rsi
movq %rbx, %rdi
callq 0xb1d0
xorl %ebp, %ebp
movq 0xb6fa(%rip), %rsi      # 0x26fe8
movq 0xb653(%rip), %rdx      # 0x26f48
movq %rbx, %rdi
callq 0xbe60
jmp 0x1b901
jmp 0x1b921
movq %rax, %r14
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x1b91a
movq 0x10(%rsp), %rsi
incq %rsi
callq 0xb940
testb %bpl, %bpl
jne 0x1b924
jmp 0x1b92c
movq %rax, %r14
movq %rbx, %rdi
callq 0xb610
movq %r14, %rdi
callq 0xbe90