zoukankan      html  css  js  c++  java
  • 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 != '' && 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 = ''; 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 = '';
      *sp = wp - zbuf;
      return zbuf;
    }
  • 相关阅读:
    Android中的httpclient框架发送get请求
    成员函数的重载&amp;&amp;隐藏&amp;&amp;覆盖
    子墨庖丁Android的ActionBar源代码分析 (一)实例化
    Hadoop2.x介绍与源代码编译
    NFS 服务器的配置
    tftp 服务器的配置
    LINUX内核及应用程序移植工作
    u-boot 移植工作目录
    Linux 下工作用户及环境
    zless
  • 原文地址:https://www.cnblogs.com/feika/p/3575123.html
Copyright © 2011-2022 走看看