Advertisement
Guest User

urldecode

a guest
Jun 2nd, 2014
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * decode a percent-encoded C string with optional path normalization
  3.  *
  4.  * The buffer pointed to by @dst must be at least strlen(@src) bytes.
  5.  * Decoding stops at the first character from @src that decodes to null.
  6.  *
  7.  * @param dst       destination buffer
  8.  * @param src       source buffer
  9.  * @return          number of valid characters in @dst
  10.  * @author          Johan Lindh <johan@linkdata.se>
  11.  * @legalese        BSD licensed (http://opensource.org/licenses/BSD-2-Clause)
  12.  */
  13. ptrdiff_t urldecode(char* dst, const char* src)
  14. {
  15.     char* org_dst = dst;
  16.     char ch, a, b;
  17.     do {
  18.         ch = *src++;
  19.         if (ch == '%' && isxdigit(a = src[0]) && isxdigit(b = src[1])) {
  20.             if (a < 'A') a -= '0';
  21.             else if(a < 'a') a -= 'A' - 10;
  22.             else a -= 'a' - 10;
  23.             if (b < 'A') b -= '0';
  24.             else if(b < 'a') b -= 'A' - 10;
  25.             else b -= 'a' - 10;
  26.             ch = 16 * a + b;
  27.             src += 2;
  28.         }
  29.         *dst++ = ch;
  30.     } while(ch);
  31.     return (dst - org_dst) - 1;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement