OpenVMS Source Code Demos

WCSM_URL_DECODE.FUN

	!================================================================================
	! title  : wcsm_url_decode.fun
	! author : Neil Rieck
	! purpose: remove so-called percent encoding
	! ver who when   what
	! --- --- ------ ----------------------------------------------------------------
	! 100 NSR 110419 1. original effort
	!================================================================================
	function string wcsm_url_decode(string inbound$)			!
	option type=explicit							!
	declare string	constant hex_string$ = "0123456789ABCDEF"		!
	declare long	x%, y%, i%, j%,	handler%,				&
		string	in$, uc$, out$						!
	!
	out$ = ""								! init
	in$ = inbound$								!
	!
	!       replace plus signs with spaces
	!
	strip_plus_sign:
	x% = pos(in$, '+', 1)							! locate '+'
	if x% > 0 then								! if we found one...
	    mid$(in$, x%, 1) = ' '						! replace with a space
	    goto strip_plus_sign						! look for more
	end if                                                                  !
	!
	!	dehexify (eg. %0d -> chr$(13)	)
	!
	!	in$ = "12345%0d67890%0d12345"
	!	      ^     ^
	!	      |     +--- y%
	!	      +--------- x%
	!
	uc$ = edit$(in$, 32)							! upcase for hex scan
	x% = 0									! previous position of '%'
	!
	dexify_loop:								!
	y% = pos(uc$, '%', x% + 1)						! locate '%'
	if y% = 0 then								! if no more found...
	    out$ = out$ + seg$(in$, x%+1, len(in$))				! scoop up thru to end
	else									! if found...
	    out$ = out$ + seg$(in$, x%+1, y%-1%)				! scoop up data up to '%'
	    when error in							!
		i% = pos(hex_string$, mid$(uc$, y%+1, 1), 1)			! isolate char 1 and enumerate
		j% = pos(hex_string$, mid$(uc$, y%+2, 1), 1)			! isolate char 2 and enumerate
		if (i% = 0) or (j% = 0) then					!
		   out$ = out$ + '%'						!
		   x% = y%							! slide pointer forward (1-1=0)
		   goto dexify_loop						!
		end if								!
		i% = (i% - 1) * 16						! adjust tens digit
		j% = (j% - 1)							! adjust ones digit
		out$ = out$ + chr$(i% + j%)					! replace %xy with equiv replacement character
		x% = y%	+ 2							! slide pointer forward (3-1=2)
		goto dexify_loop						!
	    use									!
		handler% = err							! oops
		print "-e-url_decode-error: "+str$(err)				!
	    end when								!
	end if									!
	wcsm_url_decode = out$							!
	end function								!

Back to Home
Neil Rieck
Waterloo, Ontario, Canada.