Skip to content

Instantly share code, notes, and snippets.

@dash17291
Created November 21, 2012 20:01
Show Gist options
  • Save dash17291/4127293 to your computer and use it in GitHub Desktop.
Save dash17291/4127293 to your computer and use it in GitHub Desktop.
/*******************************************************************************
* urlde.c
* Commandline URL decode - fast and dirty
* by tovis 2011
* Build: make urlde and strip urlde
* Usage: cat or echo "url coded text" | urlde
*******************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
//******************************************************************************
int main(int c,char *argv[])
{
int ichr = 0;
char shex[3];
//... code ...
do
{
ichr = getchar();
switch ( ichr )
{
case '+':
ichr = ' ';
break;
case '%':
ichr = getchar();
if ( ichr != '%' )
{
memset(shex,0,sizeof(shex));
shex[0] = (char)ichr;
shex[1] = (char)getchar();
if ( !isxdigit(shex[0]) || !isxdigit(shex[1]) )
ichr = '?';
else
{
ichr = 0;
ichr = ( shex[0] > 0x39 ) ? (shex[0] - 0x37 ) : (shex[0] - 0x30 );
ichr = ichr << 4;
ichr += ( shex[1] > 0x39 ) ? (shex[1] - 0x37 ) : (shex[1] - 0x30 );
if ( ichr == '\n' )
{
putchar(ichr);
ichr = 0;
continue;
}
}
}
break;
default:
break;
}
putchar(ichr);
} while ( ichr != '\n' );
exit ( 0 );
} //end main
/*******************************************************************************
* end urlde.c
*******************************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment