// File: night-shift[P]Srl/src/lib/ToolsBtsConv.cpp
// Function: Srl::Tools::bytes_to_base64(unsigned char const*, unsigned long, std::vector<unsigned char, std::allocator<unsigned char>>&)
size_t Tools::bytes_to_base64(const uint8_t* bytes, size_t nbytes, vector<uint8_t>& out)
{
    auto min_size = 4 * ((nbytes + 2) / 3);

    if(out.size() < min_size) {
        out.resize(min_size);
    }

    Base64Table table;
    uint8_t b0 = 0, b1 = 0, b2 = 0;

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

        b0 = bytes[in_idx];
        b1 = in_idx + 1 < nbytes ? bytes[in_idx + 1] : 0;
        b2 = in_idx + 2 < nbytes ? bytes[in_idx + 2] : 0;

        /* [BBBBBB]BB BBBBBBBB BBBBBBBB */
        out[out_idx]     = table.encode[b0 >> 2];
        /* PPPPPP [BB BBBB]BBBB BBBBBBBB */
        out[out_idx + 1] = table.encode[((b0 << 4) | (b1 >> 4)) & 0x3F];
        /* PPPPPP PPPPPP [BBBB BB]BBBBBB */
        out[out_idx + 2] = table.encode[((b1 << 2) | (b2 >> 6)) & 0x3F];
        /* PPPPPP PPPPPP PPPPPP [BBBBBB] */
        out[out_idx + 3] = table.encode[b2 & 0x3F];
    }

    auto rem = nbytes % 3;
    /* rem == 1 -> 2 equals signs, rem == 2 -> 1 equals sign */
    if(rem > 0) {
        out[min_size - 1] = '=';
    }
    if(rem == 1) {
        out[min_size - 2] = '=';
    }

    return min_size;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rax
addq $0x2, %rax
movl $0x3, %ecx
xorl %edx, %edx
divq %rcx
shlq $0x2, %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
callq 0x37840
cmpq -0x20(%rbp), %rax
jae 0x61add
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x35f00
leaq -0x162(%rbp), %rdi
callq 0x381b0
movb $0x0, -0x163(%rbp)
movb $0x0, -0x164(%rbp)
movb $0x0, -0x165(%rbp)
movq $0x0, -0x170(%rbp)
movq $0x0, -0x178(%rbp)
movq -0x170(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x61cce
movq -0x8(%rbp), %rax
movq -0x170(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x163(%rbp)
movq -0x170(%rbp), %rax
addq $0x1, %rax
cmpq -0x10(%rbp), %rax
jae 0x61b62
movq -0x8(%rbp), %rax
movq -0x170(%rbp), %rcx
movzbl 0x1(%rax,%rcx), %eax
movl %eax, -0x184(%rbp)
jmp 0x61b6c
xorl %eax, %eax
movl %eax, -0x184(%rbp)
jmp 0x61b6c
movl -0x184(%rbp), %eax
movb %al, -0x164(%rbp)
movq -0x170(%rbp), %rax
addq $0x2, %rax
cmpq -0x10(%rbp), %rax
jae 0x61ba1
movq -0x8(%rbp), %rax
movq -0x170(%rbp), %rcx
movzbl 0x2(%rax,%rcx), %eax
movl %eax, -0x188(%rbp)
jmp 0x61bab
xorl %eax, %eax
movl %eax, -0x188(%rbp)
jmp 0x61bab
movl -0x188(%rbp), %eax
movb %al, -0x165(%rbp)
movzbl -0x163(%rbp), %eax
sarl $0x2, %eax
cltq
movb -0x160(%rbp,%rax), %al
movb %al, -0x18c(%rbp)
movq -0x18(%rbp), %rdi
movq -0x178(%rbp), %rsi
callq 0x33120
movb -0x18c(%rbp), %cl
movb %cl, (%rax)
movzbl -0x163(%rbp), %eax
shll $0x4, %eax
movzbl -0x164(%rbp), %ecx
sarl $0x4, %ecx
orl %ecx, %eax
andl $0x3f, %eax
cltq
movb -0x160(%rbp,%rax), %al
movb %al, -0x18b(%rbp)
movq -0x18(%rbp), %rdi
movq -0x178(%rbp), %rsi
addq $0x1, %rsi
callq 0x33120
movb -0x18b(%rbp), %cl
movb %cl, (%rax)
movzbl -0x164(%rbp), %eax
shll $0x2, %eax
movzbl -0x165(%rbp), %ecx
sarl $0x6, %ecx
orl %ecx, %eax
andl $0x3f, %eax
cltq
movb -0x160(%rbp,%rax), %al
movb %al, -0x18a(%rbp)
movq -0x18(%rbp), %rdi
movq -0x178(%rbp), %rsi
addq $0x2, %rsi
callq 0x33120
movb -0x18a(%rbp), %cl
movb %cl, (%rax)
movzbl -0x165(%rbp), %eax
andl $0x3f, %eax
cltq
movb -0x160(%rbp,%rax), %al
movb %al, -0x189(%rbp)
movq -0x18(%rbp), %rdi
movq -0x178(%rbp), %rsi
addq $0x3, %rsi
callq 0x33120
movb -0x189(%rbp), %cl
movb %cl, (%rax)
movq -0x170(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x170(%rbp)
movq -0x178(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x178(%rbp)
jmp 0x61b14
movq -0x10(%rbp), %rax
movl $0x3, %ecx
xorl %edx, %edx
divq %rcx
movq %rdx, -0x180(%rbp)
cmpq $0x0, -0x180(%rbp)
jbe 0x61d01
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
subq $0x1, %rsi
callq 0x33120
movb $0x3d, (%rax)
cmpq $0x1, -0x180(%rbp)
jne 0x61d1f
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
subq $0x2, %rsi
callq 0x33120
movb $0x3d, (%rax)
movq -0x20(%rbp), %rax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
nopl (%rax)