Skip to content

Instantly share code, notes, and snippets.

@dbro
Created April 24, 2014 07:31
Show Gist options
  • Save dbro/11244995 to your computer and use it in GitHub Desktop.
Save dbro/11244995 to your computer and use it in GitHub Desktop.
command line url decoder
#!/usr/bin/awk -f
BEGIN {
hextab["0"] = 0; hextab["8"] = 8;
hextab["1"] = 1; hextab["9"] = 9;
hextab["2"] = 2; hextab["A"] = 10; hextab["a"] = 10;
hextab["3"] = 3; hextab["B"] = 11; hextab["b"] = 11;
hextab["4"] = 4; hextab["C"] = 12; hextab["c"] = 12;
hextab["5"] = 5; hextab["D"] = 13; hextab["d"] = 13;
hextab["6"] = 6; hextab["E"] = 14; hextab["e"] = 14;
hextab["7"] = 7; hextab["F"] = 15; hextab["f"] = 15;
}
function urldecode(inputstring) {
#print "** read line " inputstring;
decoded = "";
i = 1;
len = length(inputstring);
while ( i <= len ) {
c = substr(inputstring, i, 1);
if ( c == "%" ) {
#print "** found % at index " i;
if ( i+2 <= len ) {
c1 = substr(inputstring, i+1, 1);
c2 = substr(inputstring, i+2, 1);
if ( (hextab[c1] == "") || (hextab[c2] == "") ) {
print "WARNING: invalid hex encoding: %" c1 c2 | "cat >&2" > "/dev/stderr";
} else {
code = 0 + (hextab[c1] * 16) + hextab[c2];
c = sprintf("%c", code);
i += 2;
#print "** translated " code " into " c;
}
} else {
print "WARNING: invalid percent-sign encoding: " substr(inputstring, i, len - i) > "/dev/stderr";
}
} else if ( c == "+" ) {
#print "** found a plus at index " i;
c = " ";
}
decoded = decoded c;
i += 1;
}
return decoded;
}
{
printthis = "";
newprintthis = $0;
# some urls are repeatedly encoded, so we need to decode multiple times until the result is stable
while (length(printthis) != length(newprintthis)) {
printthis = newprintthis;
newprintthis = urldecode(printthis);
}
print newprintthis;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment