Guest User

Untitled

a guest
Mar 2nd, 2025
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASM (NASM) 6.66 KB | Source Code | 0 0
  1. SECTION .data
  2. base64_map db "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  3.  
  4. %define SYS_EXIT 60
  5. %define SYS_READ 0
  6. %define SYS_WRITE 1
  7. %define STDOUT 1
  8. %define STDIN 0
  9. %define EQUALS 0x3D
  10. %define IBUF_SIZE 49152
  11. %define OBUF_SIZE 65536
  12.  
  13. SECTION .bss
  14. input_buffer resb IBUF_SIZE   ; Buffer for input (48 KB)
  15. output_buffer resb OBUF_SIZE   ; Buffer for output (64 KB)
  16.  
  17. SECTION .text
  18.    
  19. global main
  20.  
  21. main:
  22.     mov rbp, rsp
  23.     call base64_encode
  24.  
  25.     mov rdi, 0          ; set exit status = 0
  26.     mov rax, SYS_EXIT   ; SYSCALL number for EXIT
  27.     syscall             ; make the syscall
  28.  
  29.  
  30. base64_encode:
  31.     push rax
  32.     push rbx
  33.     push rcx
  34.     push rdx
  35.     push rsi
  36.     push rdi
  37.     push r8
  38.     push r9
  39.    
  40.     xor r9, r9                              ; store number of bytes in output_buffer in r9, used to calculate offset
  41.     xor r8, r8                              ; store bytes left in read buffer
  42.     jmp read_buffer                         ; on first loop, we have nothing to write
  43.    
  44.     write_buffer:
  45.         mov rbx, rsi                        ; store read address (required for remainder)
  46.         mov rdi, STDOUT                     ; file descriptor = stout
  47.         mov rdx, r9                         ; number of bytes to write
  48.         mov rax, SYS_WRITE                  ; SYSCALL number to write to STDOUT
  49.         lea rsi, output_buffer              
  50.         syscall                             ; make the syscall
  51.         xor r9, r9                          ; reset write buffer count
  52.         mov rsi, rbx                        ; load back read address
  53.    
  54.     read_buffer:
  55.         mov ax, [rsi]
  56.         mov [input_buffer], ax              ; put remainder at the start of the buffer
  57.         lea rsi, [input_buffer + r8]        ; load address
  58.         mov rdi, STDIN                      ; file descriptor = stdin
  59.         mov rdx, IBUF_SIZE
  60.         sub rax, r8                         ; max number of bytes to read
  61.         mov rax, SYS_READ                   ; SYSCALL number for reading from STDIN
  62.         syscall
  63.         add r8, rax                         ; store read byte count in r8, as rax will be used
  64.         lea rsi, input_buffer
  65.         cmp rax, 0
  66.         je handle_remainder                 ; if no bytes were read, eof was reached --> encode remainder
  67.  
  68.     base64_encode_loop:
  69.         cmp r8, 0x3                         ; if number of bystes < 3, write output buffer (and read new input buffer)
  70.         jl  write_buffer
  71.        
  72.     encoding_start:
  73.         mov eax, [rsi]                      ; read memory into eax
  74.         sub r8, 3
  75.         add rsi, 3
  76.        
  77.         ; initial state aex: ???????? ABCDEFGH IJKLMNOP QRSTUVWX
  78.         ; encoding order:    00CDEFGH 00MNOPAB 00WXIJKL 00QRSTUV
  79.         ; encode last byte
  80.         mov ebx, eax
  81.         shr ebx, 16                         ; 00000000 00000000 ???????? ABCDEFGH
  82.         and ebx, 0x0000003F                 ; 00000000 00000000 00000000 00CDEFGH
  83.         mov ch, [base64_map + ebx]          ; ???????? ???????? 33333333 ????????
  84.         mov ebx, eax
  85.         shr ebx, 16                         ; 00000000 00000000 ???????? ABCDEFGH
  86.         mov bh, ah                          ; 00000000 00000000 IJKLMNOP ABCDEFGH
  87.         shr bx, 6                           ; 00000000 00000000 000000IJ KLMNOPAB
  88.         and ebx, 0x0000003F                 ; 00000000 00000000 00000000 00MNOPAB
  89.         mov cl, [base64_map + ebx]          ; ???????? ???????? 33333333 22222222
  90.         shl ecx, 16                         ; 33333333 22222222 00000000 00000000
  91.         mov bh, al                          ; 00000000 00000000 QRSTUVWX 00MNOPAB
  92.         mov bl, ah                          ; 00000000 00000000 QRSTUVWX IJKLMNOP
  93.         shr bx, 4                           ; 00000000 00000000 0000QRST UVWXIJKL
  94.         and ebx, 0x0000003F                 ; 00000000 00000000 00000000 00WXIJKL
  95.         mov ch, [base64_map + ebx]          ; 33333333 22222222 11111111 00000000
  96.         shr eax, 2                          ; 00?????? ??ABCDEF GHIJKLMN OPQRSTUV
  97.         and eax, 0x0000003F                 ; 00000000 00000000 00000000 00QRSTUV
  98.         mov cl, [base64_map + eax]          ; 33333333 22222222 11111111 00000000
  99.        
  100.         lea rax, [output_buffer + r9]       ; load offset address in output_buffer
  101.         mov [rax], ecx                      ; write to output_buffer
  102.         add r9, 4                           ; increase number of bytes written
  103.        
  104.         ; jump back to the start of the loop
  105.         jmp base64_encode_loop
  106.        
  107.     write_buffer_end:
  108.         mov rbx, rsi                        ; store rsi, it will be needed
  109.         mov rdi, STDOUT                     ; file descriptor = stout
  110.         mov rdx, r9                         ; number of bytes to write
  111.         mov rax, SYS_WRITE                  ; SYSCALL number to write to STDOUT
  112.         lea rsi, output_buffer              
  113.         syscall                             ; make the syscall
  114.         mov rsi, rbx
  115.  
  116.  
  117.     handle_remainder:
  118.         mov eax, [rsi]             ; read memory into eax
  119.         cmp r8, 0
  120.         je routine_end                      ; if no bytes were read, jump to end
  121.         cmp r8, 2
  122.         je two_bytes
  123.         and eax, 0x000000FF
  124.        
  125.         mov ch, EQUALS
  126.         mov cl, EQUALS
  127.         shl ecx, 16
  128.         mov bh, al
  129.         mov bl, ah
  130.         shr bx, 4
  131.         and ebx, 0x0000003F
  132.         mov ch, [base64_map + ebx]
  133.         shr eax, 2
  134.         and eax, 0x0000003F
  135.         mov cl, [base64_map + eax]
  136.         jmp write_end
  137.     two_bytes:
  138.         and eax, 0x0000FFFF
  139.         mov ch, EQUALS
  140.         mov ebx, eax
  141.         shr ebx, 16
  142.         mov bh, ah
  143.         shr bx, 6
  144.         and ebx, 0x0000003F
  145.         mov cl, [base64_map + ebx]
  146.         shl ecx, 16
  147.         mov bh, al
  148.         mov bl, ah
  149.         shr bx, 4
  150.         and ebx, 0x0000003F
  151.         mov ch, [base64_map + ebx]
  152.         shr eax, 2
  153.         and eax, 0x0000003F
  154.         mov cl, [base64_map + eax]
  155.        
  156.        
  157.     write_end:
  158.         lea rsi, [output_buffer]            ; load offset address in output_buffer
  159.         mov [rsi], ecx                      ; write to output_buffer
  160.         ; write remainder in ecx to stdout
  161.         mov rdi, STDOUT                     ; file descriptor = stout
  162.         mov rdx, 0x4                        ; number of bytes to write
  163.         mov rax, SYS_WRITE                  ; SYSCALL number to write to STDOUT
  164.         syscall                             ; make the syscall
  165.         jmp routine_end
  166.        
  167.     routine_end:
  168.         pop r8
  169.         pop r9
  170.         pop rdi
  171.         pop rsi
  172.         pop rdx
  173.         pop rcx
  174.         pop rbx
  175.         pop rax
Add Comment
Please, Sign In to add comment