⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 url.c

📁 是一个手机功能的模拟程序
💻 C
📖 第 1 页 / 共 5 页
字号:
  bs = wip_wchar2byte (pchUrl, &err);
  if ((bs != NULL) && !err && URL_Parse (bs, &url)) {
    *scheme = url.scheme_type;
  }
  else
    ok = FALSE;
  DEALLOC (&bs);

  return ok;
}

BOOL
b_GetScheme (const BYTE* pbUrl, BYTE **scheme)
{
  return b_GetPart (pbUrl, SCHEME_PART, scheme);
}

BOOL
b_GetSchemeType (const BYTE* pbUrl, Scheme *scheme)
{
  URL url;

  if ((pbUrl == NULL) || (scheme == NULL))
    return FALSE;

  if (!URL_Parse ((BYTE*)pbUrl, &url))
    return FALSE;
  *scheme = url.scheme_type;

  return TRUE;
}


/*
 * Return the host part of a URL.
 * Returns NULL in case of error, or if the URL does not have a host part.
 * NOTE: it is the responsibility of the caller to deallocate the string.
 */
BYTE*
URL_GetHost (URL *url)
{
  if (url == NULL)
    return NULL;

  return newstring (url->s[HOST_PART], url->len[HOST_PART]);
}

/*
 * Extract the host part of a URL.
 * Returns FALSE in case of error, including that the URL is not valid.
 * Sets the out-parameter to NULL if the URL does not have a host component.
 * NOTE: it is the responsibility of the caller to deallocate
 * the returned string.
 */
BOOL
w_GetHost (const WCHAR* pchUrl, WCHAR **host)
{
  return w_GetPart (pchUrl, HOST_PART, host);
}

BOOL
b_GetHost (const BYTE* pchUrl, BYTE **host)
{
  return b_GetPart (pchUrl, HOST_PART, host);
}


/*
 * Return the port number of a URL.
 * Returns NULL in case of error, or if the URL does not have a port number.
 * NOTE: it is the responsibility of the caller to deallocate the string.
 */
BYTE*
URL_GetPort (URL *url)
{
  if (url == NULL)
    return NULL;

  return newstring (url->s[PORT_PART], url->len[PORT_PART]);
}

/*
 * Extract the port number of a URL.
 * Returns FALSE in case of error, including that the URL is not valid.
 * Sets the out-parameter to NULL if the URL does not have a port number.
 * NOTE: it is the responsibility of the caller to deallocate
 * the returned string.
 */
BOOL
w_GetPort (const WCHAR* pchUrl, WCHAR **port)
{
  return w_GetPart (pchUrl, PORT_PART, port);
}

BOOL
b_GetPort (const BYTE* pbUrl, BYTE **port)
{
  return b_GetPart (pbUrl, PORT_PART, port);
}


/*
 * Return the path component of a URL.
 * Returns NULL in case of error, or if the URL does not have a path component.
 * NOTE: it is the responsibility of the caller to deallocate the string.
 */
BYTE*
URL_GetPath (URL *url)
{
  BYTE  *bs, *p, *q;
  INT16 i, len;
  INT16 inparam;

  if ((url == NULL) || (url->len[PATH_PART] == 0)) {
    return NULL;
  }

  q = url->s[PATH_PART];
  len = 0;
  inparam = 0;
  for (i = 0; i < url->len[PATH_PART]; i++) {
    if (inparam && (q[i] == '/')) {
      inparam = 0;
    }
    else if (!inparam && (q[i] == ';')) {
      inparam = 1;
    }
    if (!inparam) {
      len++;
    }
  }

  bs = NEWARRAY (BYTE, len + 1);
  if (bs == NULL)
    return NULL;

  p = bs;
  q = url->s[PATH_PART];
  inparam = 0;
  for (i = 0; i < url->len[PATH_PART]; i++) {
    if (inparam && (q[i] == '/')) {
      inparam = 0;
    }
    else if (!inparam && (q[i] == ';')) {
      inparam = 1;
    }
    if (!inparam) {
      *p++ = q[i];
    }
  }
  *p = '\0';

  return bs;
}

/*
 * Extract the path component of a URL.
 * Returns FALSE in case of error, including that the URL is not valid.
 * Sets the out-parameter to NULL if the URL does not have a path component.
 * NOTE: it is the responsibility of the caller to deallocate
 * the returned string.
 */
BOOL
w_GetPath (const WCHAR* pchUrl, WCHAR **path)
{
  URL  url;
  BYTE *bs = NULL;
  BOOL err = FALSE;
  BYTE *p;

  if ((pchUrl == NULL) || (path == NULL))
    return FALSE;
  *path = NULL;

  bs = wip_wchar2byte (pchUrl, &err);
  if ((bs == NULL) || err) {
    return FALSE;
  }
  if (!URL_Parse (bs, &url)) {
    DEALLOC (&bs);
    return FALSE;
  }
  p = URL_GetPath (&url);
  *path = wip_byte2wchar (p);

  DEALLOC (&bs);
  DEALLOC (&p);

  return TRUE;
}

BOOL
b_GetPath (const BYTE* pbUrl, BYTE **path)
{
  URL url;

  if ((pbUrl == NULL) || (path == NULL))
    return FALSE;
  *path = NULL;

  if (!URL_Parse ((BYTE*)pbUrl, &url)) {
    return FALSE;
  }
  *path = URL_GetPath (&url);

  return TRUE;
}


/*
 * Return the parameter component of a URL.
 * Returns NULL in case of error, or if the URL does not have any parameters.
 * NOTE: it is the responsibility of the caller to deallocate the string.
 */
BYTE *
URL_GetParameters (URL *url)
{
  INT16  i;
  UINT16 len, plen;
  BYTE   *p, *bs;

  if ((url == NULL) || (url->len[PATH_PART] == 0)) {
    return NULL;
  }

  len = url->len[PATH_PART];
  p = url->s[PATH_PART];

  /* Find the right-most '/', if any. */
  for (i = len - 1; (i >= 0) && (p[i] != '/'); i--);
  i++;

  /* Find the first ';' after the last '/', if any. */
  for (; (i < len) && (p[i] != ';'); i++);
  if (i == len) {
    /* The last path segment has no parameter part */
    return NULL;
  }
  p += i + 1;
  plen = len - i - 1;
  if ((bs = NEWARRAY (BYTE, plen + 1)) == NULL) {
    return NULL;
  }
  B_COPYSTRINGN (bs, p, plen);
  bs[plen] = '\0';

  return bs;
}

/*
 * Extract the parameter component of a URL.
 * Returns FALSE in case of error, including that the URL is not valid.
 * Sets the out-parameter to NULL if the URL does not have any parameters.
 * NOTE: it is the responsibility of the caller to deallocate
 * the returned string.
 */
BOOL
w_GetParameters (const WCHAR* pchUrl, WCHAR **param)
{
  URL  url;
  BYTE *bs = NULL;
  BOOL err = FALSE;
  BYTE *p;

  if ((pchUrl == NULL) || (param == NULL))
    return FALSE;
  *param = NULL;

  bs = wip_wchar2byte (pchUrl, &err);
  if ((bs == NULL) || err) {
    return FALSE;
  }
  if (!URL_Parse (bs, &url)) {
    DEALLOC (&bs);
    return FALSE;
  }
  p = URL_GetParameters (&url);
  if (p != NULL) {
    *param = wip_byte2wchar (p);
  }

  DEALLOC (&bs);
  DEALLOC (&p);

  return TRUE;
}

BOOL
b_GetParameters (const BYTE* pbUrl, BYTE **param)
{
  URL url;

  if ((pbUrl == NULL) || (param == NULL))
    return FALSE;
  *param = NULL;

  if (!URL_Parse ((BYTE*)pbUrl, &url)) {
    return FALSE;
  }
  *param = URL_GetParameters (&url);

  return TRUE;
}


/*
 * Return the query part of a URL.
 * Returns NULL in case of error, or if the URL does not have a query part.
 * NOTE: it is the responsibility of the caller to deallocate the string.
 */
BYTE *
URL_GetQuery (URL *url)
{
  if (url == NULL)
    return NULL;

  return newstring (url->s[QUERY_PART], url->len[QUERY_PART]);
}

/*
 * Extract the query part of a URL.
 * Returns FALSE in case of error, including that the URL is not valid.
 * Sets the out-parameter to NULL if the URL does not have a query part.
 * NOTE: it is the responsibility of the caller to deallocate
 * the returned string.
 */
BOOL
w_GetQuery (const WCHAR* pchUrl, WCHAR **query)
{
  return w_GetPart (pchUrl, QUERY_PART, query);
}

BOOL
b_GetQuery (const BYTE* pbUrl, BYTE **query)
{
  return b_GetPart (pbUrl, QUERY_PART, query);
}

void
URL_RemoveQuery (URL *url)
{
  if (url == NULL)
    return;
  url->s[QUERY_PART] = NULL;
  url->len[QUERY_PART] = 0;
}

/*
 * Return the fragment part of a URL.
 * Returns NULL in case of error, or if the URL does not have a fragment part.
 * NOTE: it is the responsibility of the caller to deallocate the string.
 */
BYTE *
URL_GetFragment (URL *url)
{
  if (url == NULL)
    return NULL;

  return newstring (url->s[FRAGMENT_PART], url->len[FRAGMENT_PART]);
}

/*
 * Extract the fragment part of a URL.
 * Returns FALSE in case of error, including that the URL is not valid.
 * Sets the out-parameter to NULL if the URL does not have a fragment part.
 * NOTE: it is the responsibility of the caller to deallocate
 * the returned string.
 */
BOOL
w_GetFragment (const WCHAR* pchUrl, WCHAR **frag)
{
  return w_GetPart (pchUrl, FRAGMENT_PART, frag);
}

BOOL
b_GetFragment (const BYTE* pbUrl, BYTE **frag)
{
  return b_GetPart (pbUrl, FRAGMENT_PART, frag);
}


/*
 * Other utility routines
 */

/*
 * Return a copy of 'pchString' where each character belonging to the set of
 * so called "special characters" or being in the range 0x80-0xff,
 * has been replaced by a hexadecimal esacape sequence of the form "%xy".
 * Returns NULL in case of error or if any character in the input
 * has a character code > 0xff.
 * NOTE: it is the callers responsibility to deallocate the returned string.
 */
WCHAR *
w_WMLS_EscapeString (const WCHAR* pchString)
{
  return w_EscapeString (pchString);
}

/*
 * Return a copy of 'pchString' where each character belonging to the set of
 * so called "special characters" or being in the range 0x80-0xff,
 * has been replaced by a hexadecimal esacape sequence of the form "%xy".
 * Returns NULL in case of error or if any character in the input
 * has a character code > 0xff.
 * NOTE: it is the callers responsibility to deallocate the returned string.
 */
WCHAR*
w_EscapeString (const WCHAR* pchString)
{
  const WCHAR *p;
  WCHAR       *q, *s;
  BYTE        tmp[2];
  UINT16      l = 0;
  UINT16      r = 0;

  if (pchString == NULL)
    return NULL;

  for (p = pchString; *p; p++) {
    BYTE b = (BYTE)(*p & 0xff);
    if (*p > 0xff) {
      return NULL;
    }
    if (wae_isspecial (b) || (b > 0x7f))
      r++;
    else
      l++;
  }

  if ((s = NEWARRAY (WCHAR, l + 3 * r + 1)) == NULL)
    return NULL;

  for (p = pchString, q = s; *p; p++) {
    BYTE b = (BYTE)(*p & 0xff);
    if (wae_isspecial (b) || (b > 0x7f)) {
      *q++ = '%';
      ByteToHex (b, tmp);
      *q++ = (WCHAR)tmp[0];
      *q++ = (WCHAR)tmp[1];
    }
    else
      *q++ = *p;
  }
  *q = '\0';

  return s;
}

/*
 * Return a copy of 'pbString' where each character belonging to the set of
 * so called "special characters" or being in the range 0x80-0xff,
 * has been replaced by a hexadecimal esacape sequence of the form "%xy".
 * Returns NULL in case of error.
 * NOTE: it is the callers responsibility to deallocate the returned string.
 */
BYTE*
b_EscapeString (const BYTE* pbString)
{
  const BYTE *p;
  BYTE       *q, *s;
  UINT16     l = 0;
  UINT16     r = 0;

  if (pbString == NULL)
    return NULL;

  for (p = pbString; *p; p++) {
    if (wae_isspecial (*p) || (*p > 0x7f))
      r++;
    else
      l++;
  }

  if ((s = NEWARRAY (BYTE, l + 3 * r + 1)) == NULL)
    return NULL;

  for (p = pbString, q = s; *p; p++) {
    if (wae_isspecial (*p) || (*p > 0x7f)) {
      *q++ = '%';
      ByteToHex (*p, q);
      q += 2;
    }
    else
      *q++ = *p;
  }
  *q = '\0';

  return s;
}

/*
 * Return a copy of 'ws', where every 16-bit character has been
 * replaced by two escape sequences of the form "%xy", representing
 * the two bytes in the 16-bit character. The parts of each 16-bit
 * character are read in big-endian order.
 * Returns NULL in case of error.
 * NOTE: it is the callers responsibility to deallocate the returned string.
 */
BYTE *
b_UnicodeEscape (WCHAR *ws)
{
  WCHAR  *wp;
  BYTE   *p, *bs;
  UINT16 len;

  if (ws == NULL)
    return NULL;

  len = STRINGLENGTH (ws);
  bs = NEWARRAY (BYTE, 6 * len + 1);
  if (bs == NULL)
    return NULL;

  p = bs;
  for (wp = ws; *wp; wp++) {
    BYTE b1 = (BYTE)((*wp >> 8) & 0xff);
    BYTE b2 = (BYTE)(*wp & 0xff);
    *p++ = '%';
    ByteToHex (b1, p);
    p += 2;
    *p++ = '%';
    ByteToHex (b2, p);
    p += 2;
  }
  *p = '\0';

  return bs;
}

/*
 * Return a copy of 'pchString' where each character belonging to the set of
 * so called "special characters" or being in the range 0x80-0xff,
 * has been replaced by a hexadecimal esacape sequence of the form "%xy".
 * Characters that are greater then 0xff are replaced with a blank character.
 * Returns NULL in case of error.
 * NOTE: it is the callers responsibility to deallocate the returned string.
 */
WCHAR*
w_wmlVariableEscape (const WCHAR* pchString)
{
  const WCHAR *p;
  WCHAR       *q, *s;
  BYTE        tmp[2];
  UINT16      l = 0;
  UINT16      r = 0;

  if (pchString == NULL)
    return NULL;

  for (p = pchString; *p; p++) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -