Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. encode_base64:          # (number of opened bytes, input buffer, output buffer)
  2.     pushq %rbp          # handle standard function stuff, %rbp is callee-save
  3.     movq %rsp, %rbp
  4.     movq $0, %rcx       # initialize our offset counter for the Input Buffer
  5.     movq $0, %r11       # initialize a counter  to keep track of our progress in the output buffer
  6.     movq $BUFFER_INPUT, %r9
  7.     movq $BUFFER_OUTPUT, %r10     # save the pointer to the output buffer
  8.     movq $0, %rdx
  9.     movq %rdi, %rax     # check if number of read bytes is divisible by 3
  10.     movq $3, %r13
  11.     div %r13
  12.     pushq %rdx          # save the remainder for later
  13.     cmpq $1, %rdx        # compare the remainder to 1
  14.     je encode_2_buffer  # if the remainder is 1 we need to adjust the length of our loop
  15.     jl encode_loop      # if it is divisible, remainder smaller than 1, by 3 we can move rigth ahead to encoding
  16.     subq $2, %rdi       # if the remainder is greater than 1, we need to prepare for 1 padding symbol
  17.     jmp encode_loop
  18.  
  19. encode_2_buffer:        # if neither 1 or 0 padding is needed it defaults to 2 padding
  20.     subq $1, %rdi        # if the remainder is 1 we prepare for 2 padding symbols
  21.  
  22. encode_loop:
  23.     cmpq %rcx, %rdi             # check if the loop has ended
  24.     jle end_encode_loop
  25.     movl (%r9,%rcx, 1), %ebx    # move the 4 bytes of the INPUT_BUFFER in %r9, offset by %rcx * 1, to %ebx
  26.     bswapl %ebx                 # fix endianess
  27.     movq $0, %rax               # clear %rax so we get propper offsets
  28.     movq $0, %r12                # we introduce a counter for the following loop that encodes the 3 byte segemnts
  29.  
  30. internal_encode_loop:
  31.     cmpq $3, %r12               # check if the loop has run 3 times already
  32.     jg internal_encode_loop_end # end the loop
  33.     roll $6, %ebx               # rotate the first 8 Bit to the bottom of the register
  34.     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
  35.     movb %bl, %al               # copy the lowest 8 bit to %rax so we get a register that's empty except forthe last 6 Bit
  36.    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
  37.    movb %dl, (%r10,%r11,1)   # move the byte at position %al of table64 to position %r11 of the Output Buffer stored in %r10
  38.    incq %r11
  39.    incq %r12
  40.    jmp internal_encode_loop
  41.  
  42. internal_encode_loop_end:
  43.    addq $3, %rcx               # prepare the counter of the encode loop to get the next 3 bytes
  44.    movq $0, %rbx
  45.    jmp encode_loop
  46.  
  47. end_encode_loop:
  48.    movq %rsp, %rbp
  49.    popq %rdx
  50.    popq %rbp
  51.    movq %r11, %rax
  52.    ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement