URL格式编码与解码
char* urlencode(const void* buf, size_t size) { _assert_(buf && size <= MEMMAXSIZ); const unsigned char* rp = (const unsigned char*)buf; char* zbuf = new char[size*3+1]; char* wp = zbuf; for (const unsigned char* ep = rp + size; rp < ep; rp++) { int32_t c = *rp; if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c != '\0' && std::strchr("_-.~", c))) { *(wp++) = c; } else { *(wp++) = '%'; int32_t num = c >> 4; if (num < 10) { *(wp++) = '0' + num; } else { *(wp++) = 'a' + num - 10; } num = c & 0x0f; if (num < 10) { *(wp++) = '0' + num; } else { *(wp++) = 'a' + num - 10; } } } *wp = '\0'; return zbuf; }
decode:
char* urldecode(const char* str, size_t* sp) { _assert_(str && sp); size_t zsiz = std::strlen(str); char* zbuf = new char[zsiz+1]; char* wp = zbuf; const char* ep = str + zsiz; while (str < ep) { int32_t c = *str; if (c == '%') { int32_t num = 0; if (++str >= ep) break; c = *str; if (c >= '0' && c <= '9') { num = c - '0'; } else if (c >= 'a' && c <= 'f') { num = c - 'a' + 10; } else if (c >= 'A' && c <= 'F') { num = c - 'A' + 10; } if (++str >= ep) break; c = *str; if (c >= '0' && c <= '9') { num = num * 0x10 + c - '0'; } else if (c >= 'a' && c <= 'f') { num = num * 0x10 + c - 'a' + 10; } else if (c >= 'A' && c <= 'F') { num = num * 0x10 + c - 'A' + 10; } *(wp++) = num; str++; } else if (c == '+') { *(wp++) = ' '; str++; } else if (c <= ' ' || c == 0x7f) { str++; } else { *(wp++) = c; str++; } } *wp = '\0'; *sp = wp - zbuf; return zbuf; }