by Max_Power
Encodes a URL so that a browser can handle it. For example if you give the url
http://www.x.com/file.php?string=this is a fracking test [dood] it would return
http://www.x.com/file.php?string=thi...est+%5Bdood%5D.
This is useful if you are doing stuff with google, doing CGI in MASM, or many other things.
Code:
Encode_URL PROTO :DWORD,:DWORD
.data
szAllowed db "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz:/.?=_-$(){}~&",0
szHexFormat db "%X",0
.code
Encode_URL proc USES ecx edi esi lpSource:DWORD,lpDestination:DWORD
LOCAL szCurChar[2]:BYTE
LOCAL szCharCode[5]:BYTE
LOCAL dwLen:DWORD
invoke lstrlen,lpSource
mov dwLen,eax
xor ecx,ecx ;Offset in source string
xor edx,edx ;Offset is destination string
.REPEAT
;Get the current character
mov edi,lpSource
mov al,[edi+ecx]
lea edi,szCurChar
mov [edi],al
mov al,0
mov [edi+1],al
;Check if the character is allowed
push ecx
push edx
invoke InString,1,ADDR szAllowed,ADDR szCurChar
pop edx
pop ecx
mov edi,lpDestination
.IF eax<1
;Check if it is a space, if so replace it with
;a plus sign otherwise replace it with %hex val
mov al,szCurChar
.IF al==20h
mov al,'+'
mov [edi+edx],al
.ELSE
mov al,'%'
mov [edi+edx],al
;Convert the character code for the illegal
;char to a string
xor eax,eax
mov al,szCurChar
push ecx
push edx
invoke wsprintf,ADDR szCharCode,ADDR szHexFormat,eax
pop edx
pop ecx
inc edx
lea esi,szCharCode
mov eax,[esi]
mov [edi+edx],eax
mov eax,[esi+1]
inc edx
mov [edi+edx],eax
.ENDIF
.ELSE
mov al,szCurChar
mov [edi+edx],al
.ENDIF
inc ecx
inc edx
.UNTIL ecx==dwLen
ret
Encode_URL endp