Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- encode_base64: # (number of opened bytes, input buffer, output buffer)
- pushq %rbp # handle standard function stuff, %rbp is callee-save
- movq %rsp, %rbp
- movq $0, %rcx # initialize our offset counter for the Input Buffer
- movq $0, %r11 # initialize a counter to keep track of our progress in the output buffer
- movq $BUFFER_INPUT, %r9
- movq $BUFFER_OUTPUT, %r10 # save the pointer to the output buffer
- movq $0, %rdx
- movq %rdi, %rax # check if number of read bytes is divisible by 3
- movq $3, %r13
- div %r13
- pushq %rdx # save the remainder for later
- cmpq $1, %rdx # compare the remainder to 1
- je encode_2_buffer # if the remainder is 1 we need to adjust the length of our loop
- jl encode_loop # if it is divisible, remainder smaller than 1, by 3 we can move rigth ahead to encoding
- subq $2, %rdi # if the remainder is greater than 1, we need to prepare for 1 padding symbol
- jmp encode_loop
- encode_2_buffer: # if neither 1 or 0 padding is needed it defaults to 2 padding
- subq $1, %rdi # if the remainder is 1 we prepare for 2 padding symbols
- encode_loop:
- cmpq %rcx, %rdi # check if the loop has ended
- jle end_encode_loop
- movl (%r9,%rcx, 1), %ebx # move the 4 bytes of the INPUT_BUFFER in %r9, offset by %rcx * 1, to %ebx
- bswapl %ebx # fix endianess
- movq $0, %rax # clear %rax so we get propper offsets
- movq $0, %r12 # we introduce a counter for the following loop that encodes the 3 byte segemnts
- internal_encode_loop:
- cmpq $3, %r12 # check if the loop has run 3 times already
- jg internal_encode_loop_end # end the loop
- roll $6, %ebx # rotate the first 8 Bit to the bottom of the register
- andb $0b00111111, %bl # mask the first 2 bits of %al to get the 6 bits we just shifted left, they are from the 4th byte in the qword in %rax so we can dismiss them
- movb %bl, %al # copy the lowest 8 bit to %rax so we get a register that's empty except forthe last 6 Bit
- movb table64(%rax), %dl # use the 6 bit in %rax as a multiplier for the 1 byte steps we use as offset to get to our position in the lookup table
- movb %dl, (%r10,%r11,1) # move the byte at position %al of table64 to position %r11 of the Output Buffer stored in %r10
- incq %r11
- incq %r12
- jmp internal_encode_loop
- internal_encode_loop_end:
- addq $3, %rcx # prepare the counter of the encode loop to get the next 3 bytes
- movq $0, %rbx
- jmp encode_loop
- end_encode_loop:
- movq %rsp, %rbp
- popq %rdx
- popq %rbp
- movq %r11, %rax
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement