Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- org 100h
- start:
- lea esi, [sStr]
- lea edi, [sOut]
- urlEncode:
- .processChar:
- mov al, [esi]
- ; Null terminated string
- cmp al, 00h
- je .done
- ; Allowed special chars
- cmp al, '-'
- je .nextChar
- cmp al, '.'
- je .nextChar
- cmp al, '_'
- je .nextChar
- cmp al, '~'
- je .nextChar
- ; AL >= '0' && AL <= '9' is valid otherwise encode
- cmp al, '0'
- jb .encode
- cmp al, '9'
- jbe .nextChar
- ; AL >= 'A' && AL <= 'Z' is valid otherwise encode
- cmp al, 'A'
- jb .encode
- cmp al, 'Z'
- jbe .nextChar
- ; AL >= 'a' && AL <= 'z' is valid otherwise encode
- cmp al, 'a'
- jb .encode
- cmp AL, 'z'
- jbe .nextChar
- .encode:
- mov [edi], byte '%' ; write % (%20, %1F, etc.)
- inc edi
- push esi
- and eax, 0xFF ; only use AL
- call int2Hex ; Get hex string to represent byte
- mov ax, [esi] ; copy 2 char hex string
- mov [edi], ax
- add edi, 2
- pop esi
- inc esi
- jmp .processChar
- .nextChar:
- mov [edi], al
- inc esi
- inc edi
- jmp .processChar
- .done:
- mov [edi], byte '$' ; Add null terminator to output string
- mov bx, cs ;print output
- mov ds, bx
- lea dx, [sOut]
- mov ah, 09h
- int 21h
- ret
- ; ----------------------------------------------
- ; Converts a 32bit integer value to a hex string
- ;
- ; input:
- ; eax = input value
- ;
- ; returns:
- ; eax = length of string
- ; esi = address of null-terminated string
- ;
- ; trashes: none
- int2Hex:
- push ebx
- push ecx
- xor ecx, ecx ; zero out length counter
- lea esi, [cgi86.tmpStr+9] ; Get address of output
- .nextDigit:
- mov ebx, eax
- and bl, 0x0F ; Go one hex digit at a time
- cmp bl, 0x0A
- jb .isNum
- add bl, 'A'-0x0A ; convert to ASCII letter
- jmp .addDigit
- .isNum:
- add bl, '0' ; convert to ASCII number
- .addDigit:
- inc ecx ; increment char counter
- mov [esi], bl ; Write char to string
- shr eax, 4 ; Check if we're done
- jz .done
- dec esi ; move to next char
- jmp .nextDigit
- .done:
- mov eax, ecx
- pop ecx
- pop ebx
- ret
- .data:
- sStr: db "-._~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", 00h;"!@#$%^&*()", 00h;"Hello World #123", 00h
- sOut: resb (sOut-sStr)*3
- cgi86.tmpStr: db "4294967295", '$';00h, ; largest possible number we'll return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement