// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x170, %rsp            # imm = 0x170
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
leaq 0x2e(%rsp), %rdi
callq 0x12980
testb $0x3, %bl
jne 0x1ed91
movb 0x2e(%rsp), %al
leaq 0x3(%r14), %rcx
xorl %edx, %edx
cmpq %r15, %rdx
jae 0x1ed2b
movsbq -0x3(%rcx), %rsi
movb 0x70(%rsp,%rsi), %r9b
cmpb %al, %r9b
je 0x1ed46
movsbq -0x2(%rcx), %rsi
movb 0x70(%rsp,%rsi), %r8b
cmpb %al, %r8b
je 0x1ed46
movsbq -0x1(%rcx), %rsi
movb 0x70(%rsp,%rsi), %sil
cmpb %al, %sil
je 0x1ed46
movsbq (%rcx), %rdi
movb 0x70(%rsp,%rdi), %dil
cmpb %al, %dil
je 0x1ed46
shlb $0x2, %r9b
movl %r8d, %r10d
shrb $0x4, %r10b
orb %r9b, %r10b
movb %r10b, (%r12,%rdx)
leaq 0x1(%rdx), %r9
cmpq %r15, %r9
jae 0x1ed08
shlb $0x4, %r8b
movl %esi, %r10d
shrb $0x2, %r10b
orb %r8b, %r10b
movb %r10b, 0x1(%r12,%rdx)
incq %r9
cmpq %r15, %r9
jae 0x1ed1c
shlb $0x6, %sil
orb %sil, %dil
movb %dil, 0x2(%r12,%rdx)
addq $0x4, %rcx
incq %r9
movq %r9, %rdx
jmp 0x1ec8e
movq %r14, %rdi
movq %rbx, %rsi
callq 0x126e0
addq $0x170, %rsp            # imm = 0x170
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
pushq $0x28
popq %rdi
callq 0x115e0
movq %rax, %rbx
leaq 0x30eb(%rip), %rsi      # 0x21e43
leaq 0x8(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x128f0
movb $0x1, %bpl
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x11300
xorl %ebp, %ebp
movq 0xd268(%rip), %rsi      # 0x2bfe8
movq 0xd1c1(%rip), %rdx      # 0x2bf48
movq %rbx, %rdi
callq 0x12920
jmp 0x1edda
pushq $0x28
popq %rdi
callq 0x115e0
movq %rax, %rbx
leaq 0x3048(%rip), %rsi      # 0x21deb
leaq 0x8(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x128f0
movb $0x1, %bpl
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x11300
xorl %ebp, %ebp
movq 0xd21d(%rip), %rsi      # 0x2bfe8
movq 0xd176(%rip), %rdx      # 0x2bf48
movq %rbx, %rdi
callq 0x12920
jmp 0x1edde
jmp 0x1edf2
movq %rax, %r14
leaq 0x8(%rsp), %rdi
callq 0x12cd8
testb %bpl, %bpl
jne 0x1edf5
jmp 0x1edfd
movq %rax, %r14
movq %rbx, %rdi
callq 0x119b0
movq %r14, %rdi
callq 0x12990