changelog shortlog tags branches changeset file revisions annotate raw help

Mercurial > hg > werc / bin/contrib/rc-httpd/lib/urldecode.awk

revision 667: cc53f8048ecf
     1.1--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2+++ b/bin/contrib/rc-httpd/lib/urldecode.awk	Thu Feb 11 16:23:48 2016 -0500
     1.3@@ -0,0 +1,39 @@
     1.4+# taken from werc
     1.5+BEGIN {
     1.6+    hextab ["0"] = 0; hextab ["8"] = 8;
     1.7+    hextab ["1"] = 1; hextab ["9"] = 9;
     1.8+    hextab ["2"] = 2; hextab ["A"] = hextab ["a"] = 10
     1.9+    hextab ["3"] = 3; hextab ["B"] = hextab ["b"] = 11;
    1.10+    hextab ["4"] = 4; hextab ["C"] = hextab ["c"] = 12;
    1.11+    hextab ["5"] = 5; hextab ["D"] = hextab ["d"] = 13;
    1.12+    hextab ["6"] = 6; hextab ["E"] = hextab ["e"] = 14;
    1.13+    hextab ["7"] = 7; hextab ["F"] = hextab ["f"] = 15;
    1.14+}
    1.15+{
    1.16+    decoded = ""
    1.17+    i = 1
    1.18+    len = length ($0)
    1.19+    while ( i <= len ) {
    1.20+        c = substr ($0, i, 1)
    1.21+        if ( c == "%" ) {
    1.22+            if ( i+2 <= len ) {
    1.23+                c1 = substr ($0, i+1, 1)
    1.24+                c2 = substr ($0, i+2, 1)
    1.25+                if ( hextab [c1] == "" || hextab [c2] == "" ) {
    1.26+                    print "WARNING: invalid hex encoding: %" c1 c2 | "cat >&2"
    1.27+                } else {
    1.28+                    code = 0 + hextab [c1] * 16 + hextab [c2] + 0
    1.29+                    c = sprintf ("%c", code)
    1.30+                    i = i + 2
    1.31+                }
    1.32+            } else {
    1.33+                print "WARNING: invalid % encoding: " substr ($0, i, len - i)
    1.34+            }
    1.35+        } else if ( c == "+" ) {
    1.36+            c = " "
    1.37+        }
    1.38+        decoded = decoded c
    1.39+        ++i
    1.40+    }
    1.41+    printf "%s", decoded
    1.42+}