Advertisement
Ham62

urlencode.asm

Jan 12th, 2020
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. org 100h
  2.  
  3. start:
  4.     lea esi, [sStr]
  5.     lea edi, [sOut]
  6.  
  7. urlEncode:
  8.   .processChar:
  9.     mov al, [esi]
  10.  
  11.     ; Null terminated string
  12.     cmp al, 00h
  13.     je .done
  14.  
  15.     ; Allowed special chars
  16.     cmp al, '-'
  17.     je .nextChar
  18.     cmp al, '.'
  19.     je .nextChar
  20.     cmp al, '_'
  21.     je .nextChar
  22.     cmp al, '~'
  23.     je .nextChar
  24.  
  25.     ; AL >= '0' && AL <= '9' is valid otherwise encode
  26.     cmp al, '0'
  27.     jb .encode    
  28.     cmp al, '9'
  29.     jbe .nextChar
  30.  
  31.     ; AL >= 'A' && AL <= 'Z' is valid otherwise encode
  32.     cmp al, 'A'
  33.     jb .encode    
  34.     cmp al, 'Z'
  35.     jbe .nextChar
  36.  
  37.     ; AL >= 'a' && AL <= 'z' is valid otherwise encode
  38.     cmp al, 'a'
  39.     jb .encode    
  40.     cmp AL, 'z'
  41.     jbe .nextChar
  42.  
  43.   .encode:
  44.     mov [edi], byte '%' ; write % (%20, %1F, etc.)
  45.     inc edi
  46.  
  47.     push esi
  48.     and eax, 0xFF       ; only use AL
  49.     call int2Hex        ; Get hex string to represent byte
  50.  
  51.     mov ax, [esi]       ; copy 2 char hex string
  52.     mov [edi], ax
  53.     add edi, 2
  54.  
  55.     pop esi
  56.  
  57.     inc esi
  58.     jmp .processChar
  59.  
  60.   .nextChar:
  61.     mov [edi], al
  62.     inc esi
  63.     inc edi
  64.     jmp .processChar
  65.  
  66.   .done:
  67.     mov [edi], byte '$' ; Add null terminator to output string
  68.  
  69.     mov bx, cs ;print output
  70.     mov ds, bx
  71.     lea dx, [sOut]
  72.     mov ah, 09h
  73.     int 21h
  74.  
  75.     ret
  76.  
  77. ; ----------------------------------------------
  78. ; Converts a 32bit integer value to a hex string
  79. ;
  80. ; input:
  81. ;    eax = input value
  82. ;
  83. ; returns:
  84. ;    eax = length of string
  85. ;    esi = address of null-terminated string
  86. ;
  87. ; trashes: none
  88. int2Hex:
  89.     push ebx
  90.     push ecx
  91.  
  92.     xor ecx, ecx      ; zero out length counter
  93.     lea esi, [cgi86.tmpStr+9] ; Get address of output
  94.  
  95.   .nextDigit:
  96.     mov ebx, eax
  97.     and bl, 0x0F  ; Go one hex digit at a time
  98.  
  99.     cmp bl, 0x0A
  100.     jb .isNum
  101.  
  102.     add bl, 'A'-0x0A ; convert to ASCII letter
  103.     jmp .addDigit
  104.  
  105.   .isNum:
  106.     add bl, '0'   ; convert to ASCII number
  107.  
  108.   .addDigit:
  109.     inc ecx       ; increment char counter
  110.     mov [esi], bl ; Write char to string
  111.  
  112.     shr eax, 4    ; Check if we're done
  113.     jz .done
  114.  
  115.     dec esi       ; move to next char
  116.     jmp .nextDigit
  117.  
  118. .done:
  119.     mov eax, ecx
  120.     pop ecx
  121.     pop ebx
  122.     ret
  123.  
  124.  
  125. .data:
  126. sStr: db "-._~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", 00h;"!@#$%^&*()", 00h;"Hello World #123", 00h
  127. sOut: resb (sOut-sStr)*3
  128.  
  129. cgi86.tmpStr: db "4294967295", '$';00h, ; largest possible number we'll return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement