Advertisement
Guest User

urldecode

a guest
Jun 2nd, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1.  
  2. /**
  3.  * decode a percent-encoded C string with optional path normalization
  4.  *
  5.  * The buffer pointed to by @dst must be at least strlen(@src) bytes.
  6.  * Decoding stops at the first character from @src that decodes to null.
  7.  * Path normalization will remove redundant slashes and slash+dot sequences,
  8.  * as well as removing path components when slash+dot+dot is found. It will
  9.  * keep the root slash (if one was present) and will stop normalization
  10.  * at the first questionmark found (so query parameters won't be normalized).
  11.  *
  12.  * @param dst       destination buffer
  13.  * @param src       source buffer
  14.  * @return          number of valid characters in @dst
  15.  * @author          Johan Lindh <[email protected]>
  16.  * @legalese        BSD licensed (http://opensource.org/licenses/BSD-2-Clause)
  17.  */
  18. ptrdiff_t urldecode(char* dst, const char* src)
  19. {
  20.     char* org_dst = dst;
  21.     char ch, a, b;
  22.     do {
  23.         ch = *src++;
  24.         if (ch == '%' && isxdigit(a = src[0]) && isxdigit(b = src[1])) {
  25.             if (a < 'A') a -= '0';
  26.             else if(a < 'a') a -= 'A' - 10;
  27.             else a -= 'a' - 10;
  28.             if (b < 'A') b -= '0';
  29.             else if(b < 'a') b -= 'A' - 10;
  30.             else b -= 'a' - 10;
  31.             ch = 16 * a + b;
  32.             src += 2;
  33.         }
  34.         *dst++ = ch;
  35.     } while(ch);
  36.     return (dst - org_dst) - 1;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement