tovis

Simple, dirty URL decode in C

Jul 8th, 2011
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. /*******************************************************************************
  2. * urlde.c
  3. * Commandline URL deocde - fast and dirty
  4. * by tovis 2011
  5. * Build: make urlde and strip urlde
  6. * Usage: cat or echo "url coded text" | urlde
  7. *******************************************************************************/
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12.  
  13. //******************************************************************************
  14. int main(int c,char *argv[])
  15. {
  16. int ichr = 0;
  17. char shex[3];
  18. //... code ...
  19. do
  20. {
  21. ichr = getchar();
  22. switch ( ichr )
  23. {
  24. case '+':
  25. ichr = ' ';
  26. break;
  27. case '%':
  28. ichr = getchar();
  29. if ( ichr != '%' )
  30. {
  31. memset(shex,0,sizeof(shex));
  32. shex[0] = (char)ichr;
  33. shex[1] = (char)getchar();
  34. if ( !isxdigit(shex[0]) || !isxdigit(shex[1]) )
  35. ichr = '?';
  36. else
  37. {
  38. ichr = 0;
  39. ichr = ( shex[0] > 0x39 ) ? (shex[0] - 0x37 ) : (shex[0] - 0x30 );
  40. ichr = ichr << 4;
  41. ichr += ( shex[1] > 0x39 ) ? (shex[1] - 0x37 ) : (shex[1] - 0x30 );
  42. if ( ichr == '\n' )
  43. {
  44. putchar(ichr);
  45. ichr = 0;
  46. continue;
  47. }
  48. }
  49. }
  50. break;
  51. default:
  52. break;
  53. }
  54. putchar(ichr);
  55. } while ( ichr != '\n' );
  56. exit ( 0 );
  57. } //end main
  58.  
  59. /*******************************************************************************
  60. * end urlde.c
  61. *******************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment