urldecode.awk (1265B)
1 # taken from werc 2 BEGIN { 3 hextab ["0"] = 0; hextab ["8"] = 8; 4 hextab ["1"] = 1; hextab ["9"] = 9; 5 hextab ["2"] = 2; hextab ["A"] = hextab ["a"] = 10 6 hextab ["3"] = 3; hextab ["B"] = hextab ["b"] = 11; 7 hextab ["4"] = 4; hextab ["C"] = hextab ["c"] = 12; 8 hextab ["5"] = 5; hextab ["D"] = hextab ["d"] = 13; 9 hextab ["6"] = 6; hextab ["E"] = hextab ["e"] = 14; 10 hextab ["7"] = 7; hextab ["F"] = hextab ["f"] = 15; 11 } 12 { 13 decoded = "" 14 i = 1 15 len = length ($0) 16 while ( i <= len ) { 17 c = substr ($0, i, 1) 18 if ( c == "%" ) { 19 if ( i+2 <= len ) { 20 c1 = substr ($0, i+1, 1) 21 c2 = substr ($0, i+2, 1) 22 if ( hextab [c1] == "" || hextab [c2] == "" ) { 23 print "WARNING: invalid hex encoding: %" c1 c2 | "cat >&2" 24 } else { 25 code = 0 + hextab [c1] * 16 + hextab [c2] + 0 26 c = sprintf ("%c", code) 27 i = i + 2 28 } 29 } else { 30 print "WARNING: invalid % encoding: " substr ($0, i, len - i) 31 } 32 } else if ( c == "+" ) { 33 c = " " 34 } 35 decoded = decoded c 36 ++i 37 } 38 printf "%s", decoded 39 }