Below is the code for decoding URLs. It changes + to ' ' (space) and fixes those characters starting with %. For example if URL constains scandinavic charater ? its in the URL like this %F6. This parses it back to ?.

UrlDecode takes only one parameter, which is the address of the string to parse. It replaces original string with parsed one. Return values in eax are 0 if no errors and 1 if something went wrong.

I use this code in my cgi projects, because string posted from form are sameway coded.



;-------------------------------------------------------------------------------
;UrlDecode
;-------------------------------------------------------------------------------
UrlDecode proc uses edx ebx String :DWORD
LOCAL bRetVal :DWORD
LOCAL srcpos :DWORD
LOCAL despos :DWORD
LOCAL strtemp :DWORD
LOCAL pTemp :DWORD

mov bRetVal, 0
mov srcpos, 0
mov despos, 0
mov pTemp, 0

invoke lnstr, String
inc eax
push eax
invoke GlobalAlloc, GPTR, eax
.if eax == 0
mov bRetVal, 1
pop eax
jmp Exitus
.endif
mov pTemp, eax

pop eax
.while srcpos < eax
push eax
mov eax, String
add eax, srcpos
.if BYTE PTR [eax] == '+'
mov edx, pTemp
add edx, despos
mov BYTE PTR [edx], ' '
add despos, 1
.elseif BYTE PTR [eax] == '%'
add srcpos, 1
mov eax, String
add eax, srcpos
mov dl, BYTE PTR [eax]
lea eax, strtemp
mov BYTE PTR [eax], dl
invoke htodw, ADDR strtemp
shl eax, 4
mov ebx, eax
add srcpos, 1
mov eax, String
add eax, srcpos
mov dl, BYTE PTR [eax]
lea eax, strtemp
mov BYTE PTR [eax], dl
invoke htodw, ADDR strtemp
add eax, ebx
mov edx, pTemp
add edx, despos
mov BYTE PTR [edx], al
add despos, 1
.else
mov edx, pTemp
add edx, despos
mov al, BYTE PTR [eax]
mov BYTE PTR [edx], al
add despos, 1
.endif
add srcpos, 1
pop eax
.endw
invoke lnstr, pTemp
inc eax
invoke MemCopy, pTemp, String, eax
mov bRetVal, 0
Exitus:
.if pTemp != 0
invoke GlobalFree, pTemp
.endif
mov eax, bRetVal
ret
UrlDecode endp
Posted on 2001-12-03 14:45:56 by SamiP