// File: /workspace/github_cpp_makefile_star2_O1/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 0x4483(%rip), %xmm0     # 0x1ff80
movups %xmm0, -0x40(%r13)
movups 0x4487(%rip), %xmm0     # 0x1ff90
movups %xmm0, -0x30(%r13)
movups 0x448b(%rip), %xmm0     # 0x1ffa0
movups %xmm0, -0x20(%r13)
movups 0x448f(%rip), %xmm0     # 0x1ffb0
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 0x1bb3f
testb $0x3, %bl
jne 0x1bca6
testq %r15, %r15
je 0x1bc12
movb 0x26(%rsp), %al
leaq 0x3(%r14), %rcx
movl $0x2, %edx
movsbq -0x3(%rcx), %rsi
movb 0x68(%rsp,%rsi), %r9b
cmpb %al, %r9b
je 0x1bc52
movsbq -0x2(%rcx), %rsi
movb 0x68(%rsp,%rsi), %dil
cmpb %al, %dil
je 0x1bc52
movsbq -0x1(%rcx), %rsi
movb 0x68(%rsp,%rsi), %sil
cmpb %al, %sil
je 0x1bc52
movsbq (%rcx), %r8
movb 0x68(%rsp,%r8), %r8b
cmpb %al, %r8b
je 0x1bc52
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 0x1bbeb
shlb $0x4, %dil
movl %esi, %r9d
shrb $0x2, %r9b
orb %dil, %r9b
movb %r9b, -0x1(%r12,%rdx)
cmpq %r15, %rdx
jae 0x1bbfb
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 0x1bb71
cmpq $0x4, %rbx
jae 0x1bc1c
xorl %eax, %eax
jmp 0x1bc40
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 0x41b4(%rip), %rsi      # 0x1fe23
leaq 0x41ee(%rip), %rdx      # 0x1fe64
movq %rsp, %rdi
callq 0xb2e0
movb $0x1, %bpl
movq %rsp, %rsi
movq %rbx, %rdi
callq 0xb1d0
xorl %ebp, %ebp
movq 0xc353(%rip), %rsi      # 0x27fe8
movq 0xc2ac(%rip), %rdx      # 0x27f48
movq %rbx, %rdi
callq 0xbe60
jmp 0x1bcf8
movl $0x28, %edi
callq 0xb3b0
movq %rax, %rbx
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x4108(%rip), %rsi      # 0x1fdcb
leaq 0x4158(%rip), %rdx      # 0x1fe22
movq %rsp, %rdi
callq 0xb2e0
movb $0x1, %bpl
movq %rsp, %rsi
movq %rbx, %rdi
callq 0xb1d0
xorl %ebp, %ebp
movq 0xc2ff(%rip), %rsi      # 0x27fe8
movq 0xc258(%rip), %rdx      # 0x27f48
movq %rbx, %rdi
callq 0xbe60
jmp 0x1bcfc
jmp 0x1bd17
movq %rax, %r14
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x1bd1d
movq 0x10(%rsp), %rsi
incq %rsi
callq 0xb940
jmp 0x1bd1d
movq %rax, %r14
movb $0x1, %bpl
testb %bpl, %bpl
je 0x1bd2a
movq %rbx, %rdi
callq 0xb610
movq %r14, %rdi
callq 0xbe90