示例#1
0
文件: beos.c 项目: Efreak/elinks
int
get_system_env(void)
{
	int env = get_common_env();
	unsigned char *term = getenv("TERM");

	if (!term || (c_toupper(term[0]) == 'B' && c_toupper(term[1]) == 'E'))
		env |= ENV_BE;

	return env;
}
示例#2
0
文件: header.c 项目: Efreak/elinks
/* Parse string param="value", return value as new string or NULL if any
 * error. */
unsigned char *
get_header_param(unsigned char *e, unsigned char *name)
{
	unsigned char *n, *start;

again:
	while (*e && c_toupper(*e++) != c_toupper(*name));
	if (!*e) return NULL;

	n = name + 1;
	while (*n && c_toupper(*e) == c_toupper(*n)) e++, n++;
	if (*n) goto again;

	skip_space(e);
	if (*e++ != '=') return NULL;

	skip_space(e);
	start = e;

	if (!isquote(*e)) {
		skip_nonspace(e);
	} else {
		unsigned char uu = *e++;

		start++;
		while (*e != uu) {
			if (!*e) return NULL;
			e++;
		}
	}

	while (start < e && *start == ' ') start++;
	while (start < e && *(e - 1) == ' ') e--;
	if (start == e) return NULL;

	n = mem_alloc(e - start + 1);
	if (n) {
		int i = 0;

		while (start < e) {
			n[i++] = (*start < ' ') ? '.' : *start;
			start++;
		}
		n[i] = '\0';
	}

	return n;
}
示例#3
0
文件: conv.c 项目: rkd77/elinks-tv
long
strtolx(unsigned char *str, unsigned char **end)
{
	long num;
	unsigned char postfix;

	errno = 0;
	num = strtol((const char *)str, (char **) end, 10);
	if (errno) return 0;
	if (!*end) return num;

	postfix = c_toupper(**end);
	if (postfix == 'K') {
		(*end)++;
		if (num < -INT_MAX / 1024) return -INT_MAX;
		if (num > INT_MAX / 1024) return INT_MAX;
		return num * 1024;
	}

	if (postfix == 'M') {
		(*end)++;
		if (num < -INT_MAX / (1024 * 1024)) return -INT_MAX;
		if (num > INT_MAX / (1024 * 1024)) return INT_MAX;
		return num * (1024 * 1024);
	}

	return num;
}
示例#4
0
static char
virshGetEscapeChar(const char *s)
{
    if (*s == '^')
        return CONTROL(c_toupper(s[1]));

    return *s;
}
示例#5
0
/**
 * oath_base32_decode:
 * @in: input string with base32 encoded data of length @inlen
 * @inlen: length of input base32 string @in
 * @out: pointer to output variable for binary data of length @outlen, or NULL
 * @outlen: pointer to output variable holding length of @out, or NULL
 *
 * Decode a base32 encoded string into binary data.
 *
 * Space characters are ignored and pad characters are added if
 * needed.  Non-base32 data are not ignored but instead will lead to
 * an %OATH_INVALID_BASE32 error.
 *
 * The @in parameter should contain @inlen bytes of base32 encoded
 * data.  The function allocates a new string in *@out to hold the
 * decoded data, and sets *@outlen to the length of the data.
 *
 * If @out is NULL, then *@outlen will be set to what would have been
 * the length of *@out on successful encoding.
 *
 * If the caller is not interested in knowing the length of the output
 * data @out, then @outlen may be set to NULL.
 *
 * It is permitted but useless to have both @out and @outlen NULL.
 *
 * Returns: On success %OATH_OK (zero) is returned,
 *   %OATH_INVALID_BASE32 is returned if the input contains non-base32
 *   characters, and %OATH_MALLOC_ERROR is returned on memory allocation
 *   errors.
 *
 * Since: 1.12.0
 **/
int
oath_base32_decode (const char *in, size_t inlen, char **out, size_t * outlen)
{
  size_t i, j, tmplen = 0;
  char *in_upcase;
  char *tmp;
  bool ok;

  in_upcase = malloc (inlen + 6);	/* leave room for up to 6 '=' */
  if (!in_upcase)
    return OATH_MALLOC_ERROR;

  for (i = 0, j = 0; i < inlen; i++)
    {
      if (in[i] != ' ')
	in_upcase[j++] = c_toupper (in[i]);
    }

  /* add pad characters if needed */
  switch (j % 8)
    {
    case 2:
      in_upcase[j++] = '=';
      in_upcase[j++] = '=';
    case 4:
      in_upcase[j++] = '=';
    case 5:
      in_upcase[j++] = '=';
      in_upcase[j++] = '=';
    case 7:
      in_upcase[j++] = '=';
    default:
    case 0:
      break;
    }

  ok = base32_decode_alloc (in_upcase, j, &tmp, &tmplen);

  free (in_upcase);

  if (ok && !tmp)
    return OATH_MALLOC_ERROR;
  else if (!ok)
    return OATH_INVALID_BASE32;

  if (outlen)
    *outlen = tmplen;

  if (out)
    *out = tmp;
  else
    free (tmp);

  return OATH_OK;
}
示例#6
0
/* compare hostname against certificate, taking account of wildcards
 * return 1 on success or 0 on error
 *
 * note: certnamesize is required as X509 certs can contain embedded NULs in
 * the strings such as CN or subjectAltName.
 *
 * @level: is used for recursion. Use 0 when you call this function.
 */
int
_gnutls_hostname_compare (const char *certname,
                          size_t certnamesize, const char *hostname, int level)
{

  if (level > 5)
    return 0;

  /* find the first different character */
  for (; *certname && *hostname && c_toupper (*certname) == c_toupper (*hostname);
       certname++, hostname++, certnamesize--)
    ;

  /* the strings are the same */
  if (certnamesize == 0 && *hostname == '\0')
    return 1;

  if (*certname == '*')
    {
      /* a wildcard certificate */

      certname++;
      certnamesize--;

      while (1)
        {
          /* Use a recursive call to allow multiple wildcards */
          if (_gnutls_hostname_compare (certname, certnamesize, hostname, level+1))
            return 1;

          /* wildcards are only allowed to match a single domain
             component or component fragment */
          if (*hostname == '\0' || *hostname == '.')
            break;
          hostname++;
        }

      return 0;
    }

  return 0;
}
示例#7
0
文件: misc.c 项目: ares89/vlc
const char *
_cdk_memistr (const char *buf, size_t buflen, const char *sub)
{
  const byte *t, *s;
  size_t n;

  for (t = (byte *) buf, n = buflen, s = (byte *) sub; n; t++, n--)
    {
      if (c_toupper (*t) == c_toupper (*s))
        {
          for (buf = t++, buflen = n--, s++;
               n && c_toupper (*t) == c_toupper ((byte) * s); t++, s++, n--)
            ;
          if (!*s)
            return buf;
          t = (byte *) buf;
          n = buflen;
          s = (byte *) sub;
        }
    }

  return NULL;
}
示例#8
0
文件: write.c 项目: libguestfs/hivex
/* Calculate the hash for a lf or lh record offset.
 */
static void
calc_hash (const char *type, const char *name, void *ret)
{
  size_t len = strlen (name);

  if (STRPREFIX (type, "lf"))
    /* Old-style, not used in current registries. */
    memcpy (ret, name, len < 4 ? len : 4);
  else {
    /* New-style for lh-records. */
    size_t i, c;
    uint32_t h = 0;
    for (i = 0; i < len; ++i) {
      c = c_toupper (name[i]);
      h *= 37;
      h += c;
    }
    *((uint32_t *) ret) = htole32 (h);
  }
}
static void
done_read (void *data_read, size_t num_bytes_read, void *private_data)
{
  struct locals *l = (struct locals *) private_data;
  const char *p = l->input + l->nread;
  const char *q = (const char *) data_read;
  size_t i;

  for (i = 0; i < num_bytes_read; i++, q++)
    {
      /* Handle conversion NL -> CRLF possibly done by the child process.  */
      if (!(O_BINARY && *q == '\r'))
        {
          char orig = *p;
          char expected = c_toupper (orig);
          ASSERT (*q == expected);
          p++;
        }
    }
  l->nread = p - l->input;
}
示例#10
0
/**
 * virStringToUpper:
 * @str: string to capitalize
 * @dst: where to store the new capitalized string
 *
 * Capitalize the string with replacement of all '-' characters for '_'
 * characters. Caller frees the result.
 *
 * Returns 0 if src is NULL, 1 if capitalization was successful, -1 on failure.
 */
int
virStringToUpper(char **dst, const char *src)
{
    char *cap = NULL;
    size_t i;

    if (!src)
        return 0;

    if (VIR_ALLOC_N(cap, strlen(src) + 1) < 0)
        return -1;

    for (i = 0; src[i]; i++) {
        cap[i] = c_toupper(src[i]);
        if (cap[i] == '-')
            cap[i] = '_';
    }

    *dst = cap;
    return 1;
}
示例#11
0
static void
test_all (void)
{
  int c;

  for (c = -0x80; c < 0x100; c++)
    {
      ASSERT (c_isascii (c) == (c >= 0 && c < 0x80));

      switch (c)
        {
        case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
        case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
        case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
        case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
        case 'Y': case 'Z':
        case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
        case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
        case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
        case 's': case 't': case 'u': case 'v': case 'w': case 'x':
        case 'y': case 'z':
        case '0': case '1': case '2': case '3': case '4': case '5':
        case '6': case '7': case '8': case '9':
          ASSERT (c_isalnum (c) == 1);
          break;
        default:
          ASSERT (c_isalnum (c) == 0);
          break;
        }

      switch (c)
        {
        case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
        case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
        case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
        case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
        case 'Y': case 'Z':
        case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
        case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
        case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
        case 's': case 't': case 'u': case 'v': case 'w': case 'x':
        case 'y': case 'z':
          ASSERT (c_isalpha (c) == 1);
          break;
        default:
          ASSERT (c_isalpha (c) == 0);
          break;
        }

      switch (c)
        {
        case '\t': case ' ':
          ASSERT (c_isblank (c) == 1);
          break;
        default:
          ASSERT (c_isblank (c) == 0);
          break;
        }

      ASSERT (c_iscntrl (c) == ((c >= 0 && c < 0x20) || c == 0x7f));

      switch (c)
        {
        case '0': case '1': case '2': case '3': case '4': case '5':
        case '6': case '7': case '8': case '9':
          ASSERT (c_isdigit (c) == 1);
          break;
        default:
          ASSERT (c_isdigit (c) == 0);
          break;
        }

      switch (c)
        {
        case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
        case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
        case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
        case 's': case 't': case 'u': case 'v': case 'w': case 'x':
        case 'y': case 'z':
          ASSERT (c_islower (c) == 1);
          break;
        default:
          ASSERT (c_islower (c) == 0);
          break;
        }

      ASSERT (c_isgraph (c) == ((c >= 0x20 && c < 0x7f) && c != ' '));

      ASSERT (c_isprint (c) == (c >= 0x20 && c < 0x7f));

      ASSERT (c_ispunct (c) == (c_isgraph (c) && !c_isalnum (c)));

      switch (c)
        {
        case ' ': case '\t': case '\n': case '\v': case '\f': case '\r':
          ASSERT (c_isspace (c) == 1);
          break;
        default:
          ASSERT (c_isspace (c) == 0);
          break;
        }

      switch (c)
        {
        case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
        case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
        case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
        case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
        case 'Y': case 'Z':
          ASSERT (c_isupper (c) == 1);
          break;
        default:
          ASSERT (c_isupper (c) == 0);
          break;
        }

      switch (c)
        {
        case '0': case '1': case '2': case '3': case '4': case '5':
        case '6': case '7': case '8': case '9':
        case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
        case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
          ASSERT (c_isxdigit (c) == 1);
          break;
        default:
          ASSERT (c_isxdigit (c) == 0);
          break;
        }

      switch (c)
        {
        case 'A':
          ASSERT (c_tolower (c) == 'a');
          ASSERT (c_toupper (c) == c);
          break;
        case 'B':
          ASSERT (c_tolower (c) == 'b');
          ASSERT (c_toupper (c) == c);
          break;
        case 'C':
          ASSERT (c_tolower (c) == 'c');
          ASSERT (c_toupper (c) == c);
          break;
        case 'D':
          ASSERT (c_tolower (c) == 'd');
          ASSERT (c_toupper (c) == c);
          break;
        case 'E':
          ASSERT (c_tolower (c) == 'e');
          ASSERT (c_toupper (c) == c);
          break;
        case 'F':
          ASSERT (c_tolower (c) == 'f');
          ASSERT (c_toupper (c) == c);
          break;
        case 'G':
          ASSERT (c_tolower (c) == 'g');
          ASSERT (c_toupper (c) == c);
          break;
        case 'H':
          ASSERT (c_tolower (c) == 'h');
          ASSERT (c_toupper (c) == c);
          break;
        case 'I':
          ASSERT (c_tolower (c) == 'i');
          ASSERT (c_toupper (c) == c);
          break;
        case 'J':
          ASSERT (c_tolower (c) == 'j');
          ASSERT (c_toupper (c) == c);
          break;
        case 'K':
          ASSERT (c_tolower (c) == 'k');
          ASSERT (c_toupper (c) == c);
          break;
        case 'L':
          ASSERT (c_tolower (c) == 'l');
          ASSERT (c_toupper (c) == c);
          break;
        case 'M':
          ASSERT (c_tolower (c) == 'm');
          ASSERT (c_toupper (c) == c);
          break;
        case 'N':
          ASSERT (c_tolower (c) == 'n');
          ASSERT (c_toupper (c) == c);
          break;
        case 'O':
          ASSERT (c_tolower (c) == 'o');
          ASSERT (c_toupper (c) == c);
          break;
        case 'P':
          ASSERT (c_tolower (c) == 'p');
          ASSERT (c_toupper (c) == c);
          break;
        case 'Q':
          ASSERT (c_tolower (c) == 'q');
          ASSERT (c_toupper (c) == c);
          break;
        case 'R':
          ASSERT (c_tolower (c) == 'r');
          ASSERT (c_toupper (c) == c);
          break;
        case 'S':
          ASSERT (c_tolower (c) == 's');
          ASSERT (c_toupper (c) == c);
          break;
        case 'T':
          ASSERT (c_tolower (c) == 't');
          ASSERT (c_toupper (c) == c);
          break;
        case 'U':
          ASSERT (c_tolower (c) == 'u');
          ASSERT (c_toupper (c) == c);
          break;
        case 'V':
          ASSERT (c_tolower (c) == 'v');
          ASSERT (c_toupper (c) == c);
          break;
        case 'W':
          ASSERT (c_tolower (c) == 'w');
          ASSERT (c_toupper (c) == c);
          break;
        case 'X':
          ASSERT (c_tolower (c) == 'x');
          ASSERT (c_toupper (c) == c);
          break;
        case 'Y':
          ASSERT (c_tolower (c) == 'y');
          ASSERT (c_toupper (c) == c);
          break;
        case 'Z':
          ASSERT (c_tolower (c) == 'z');
          ASSERT (c_toupper (c) == c);
          break;
        case 'a':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'A');
          break;
        case 'b':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'B');
          break;
        case 'c':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'C');
          break;
        case 'd':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'D');
          break;
        case 'e':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'E');
          break;
        case 'f':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'F');
          break;
        case 'g':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'G');
          break;
        case 'h':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'H');
          break;
        case 'i':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'I');
          break;
        case 'j':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'J');
          break;
        case 'k':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'K');
          break;
        case 'l':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'L');
          break;
        case 'm':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'M');
          break;
        case 'n':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'N');
          break;
        case 'o':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'O');
          break;
        case 'p':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'P');
          break;
        case 'q':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'Q');
          break;
        case 'r':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'R');
          break;
        case 's':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'S');
          break;
        case 't':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'T');
          break;
        case 'u':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'U');
          break;
        case 'v':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'V');
          break;
        case 'w':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'W');
          break;
        case 'x':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'X');
          break;
        case 'y':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'Y');
          break;
        case 'z':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'Z');
          break;
        default:
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == c);
          break;
        }
    }
}
示例#12
0
void *
virDriverLoadModule(const char *name)
{
    char *modfile = NULL, *regfunc = NULL, *fixedname = NULL;
    char *tmp;
    void *handle = NULL;
    int (*regsym)(void);

    VIR_DEBUG("Module load %s", name);

    if (!(modfile = virFileFindResourceFull(name,
                                            "libvirt_driver_",
                                            ".so",
                                            abs_topbuilddir "/src/.libs",
                                            DEFAULT_DRIVER_DIR,
                                            "LIBVIRT_DRIVER_DIR")))
        return NULL;

    if (access(modfile, R_OK) < 0) {
        VIR_INFO("Module %s not accessible", modfile);
        goto cleanup;
    }

    virUpdateSelfLastChanged(modfile);

    handle = dlopen(modfile, RTLD_NOW | RTLD_GLOBAL);
    if (!handle) {
        VIR_ERROR(_("failed to load module %s %s"), modfile, dlerror());
        goto cleanup;
    }

    if (VIR_STRDUP_QUIET(fixedname, name) < 0) {
        VIR_ERROR(_("out of memory"));
        goto cleanup;
    }

    /* convert something_like_this into somethingLikeThis */
    while ((tmp = strchr(fixedname, '_'))) {
        memmove(tmp, tmp + 1, strlen(tmp));
        *tmp = c_toupper(*tmp);
    }

    if (virAsprintfQuiet(&regfunc, "%sRegister", fixedname) < 0)
        goto cleanup;

    regsym = dlsym(handle, regfunc);
    if (!regsym) {
        VIR_ERROR(_("Missing module registration symbol %s"), regfunc);
        goto cleanup;
    }

    if ((*regsym)() < 0) {
        VIR_ERROR(_("Failed module registration %s"), regfunc);
        goto cleanup;
    }

    VIR_FREE(modfile);
    VIR_FREE(regfunc);
    VIR_FREE(fixedname);
    return handle;

 cleanup:
    VIR_FREE(modfile);
    VIR_FREE(regfunc);
    VIR_FREE(fixedname);
    if (handle)
        dlclose(handle);
    return NULL;
}
示例#13
0
文件: header.c 项目: Efreak/elinks
/** Searches for a message-header with the specified field-name.
 *
 * @param[in] head
 *   Where to start searching in the message received from the server.
 *   This function actually ignores the line to which @a head points,
 *   and starts searching from the next line.  Therefore, when parsing
 *   an HTTP message, @a head should initially point to the start-line,
 *   e.g. "HTTP/1.1 200 OK".  Alternatively, if the caller has already
 *   found a message-header and wants to know if there are any more
 *   message-headers with the same field-name, then @a head can be the
 *   pointer that a previous call stored in *@a ptr.
 * @param[in] item
 *   The field-name for which this function searches.
 * @param[out] ptr
 *   If @a ptr is not NULL, and this function finds a message-header,
 *   then this function stores in *@a ptr the address at which the
 *   field-content begins; the caller may pass that as @a head in a
 *   later call.  Otherwise, this function does not modify *@a ptr.
 * @returns
 *   NULL if not found or out of memory.  Otherwise, a copy of the
 *   field-content of the message-header; the caller must eventually
 *   mem_free() it.
 *
 * The terms message-header, field-name, start-line, and field-content
 * are defined in RFC 2616 sections 4.1 and 4.2.  */
unsigned char *
parse_header(unsigned char *head, const unsigned char *item, unsigned char **ptr)
{
	unsigned char *pos = head;

	if (!pos) return NULL;

	while (*pos) {
		unsigned char *end, *value;
		const unsigned char *itempos;
		int len;

		/* Go for a newline. */
		while (*pos && *pos != ASCII_LF) pos++;
		if (!*pos) break;
		pos++; /* Start of line now. */

		/* Does item match header line ? */
		for (itempos = item; *itempos && *pos; itempos++, pos++)
			if (c_toupper(*itempos) != c_toupper(*pos))
				break;

		if (!*pos) break; /* Nothing left to parse. */
		if (*itempos) continue; /* Do not match. */

		/* Be tolerant: we accept headers with
		 * weird syntax, since most browsers does it
		 * anyway, ie:
		 * name value
		 * name :value
		 * name = value
		 * name[TAB]:[TAB]value */

		end = pos;

		/* Skip leading whitespaces if any. */
		while (LWS(*pos)) pos++;
		if (!*pos) break; /* Nothing left to parse. */

		/* Eat ':' or '=' if any. */
		if (*pos == ':' || *pos == '=') pos++;
		if (!*pos) break; /* Nothing left to parse. */

		/* Skip whitespaces after separator if any. */
		while (LWS(*pos)) pos++;
		if (!*pos) break; /* Nothing left to parse. */

		if (pos == end) continue; /* Not an exact match (substring). */

		/* Find the end of line/string.
		 * We fail on control chars and DEL char. */
		end = pos;
		while (*end != ASCII_DEL && (*end > ' ' || LWS(*end))) end++;
		if (!*end) break; /* No end of line, nothing left to parse. */

		/* Ignore line if we encountered an unexpected char. */
		if (*end != ASCII_CR && *end != ASCII_LF) continue;

		/* Strip trailing whitespaces. */
		while (end > pos && LWS(end[-1])) end--;

		len = end - pos;
		assert(len >= 0);
		if_assert_failed break;

		if (!len) continue;	/* Empty value. */

		value = memacpy(pos, len);
		if (!value) break; /* Allocation failure, stop here. */

		if (ptr) *ptr = pos;
		return value;
	}

	return NULL;
}
示例#14
0
/** Checks whether a host name matches a pattern that may contain
 * wildcards.
 *
 * @param[in] hostname
 *   The host name to which the user wanted to connect.
 *   Should be in UTF-8 and need not be null-terminated.
 * @param[in] hostname_length
 *   The length of @a hostname, in bytes.
 * @param[in] pattern
 *   A pattern that the host name might match.
 *   Should be in UTF-8 and need not be null-terminated.
 *   The pattern may contain wildcards, as specified in
 *   RFC 2818 section 3.1.
 * @param[in] pattern_length
 *   The length of @a pattern, in bytes.
 *
 * @return
 *   Nonzero if the host name matches.  Zero if it doesn't.
 *
 * According to RFC 2818 section 3.1, '*' matches any number of
 * characters except '.'.  For example, "*r*.example.org" matches
 * "random.example.org" or "history.example.org" but not
 * "frozen.fruit.example.org".
 *
 * This function does not allocate memory, and consumes at most
 * O(@a hostname_length * @a pattern_length) time.  */
int
match_hostname_pattern(const unsigned char *hostname,
		       size_t hostname_length,
		       const unsigned char *pattern,
		       size_t pattern_length)
{
	const unsigned char *const hostname_end = hostname + hostname_length;
	const unsigned char *const pattern_end = pattern + pattern_length;

	assert(hostname <= hostname_end);
	assert(pattern <= pattern_end);
	if_assert_failed return 0;

	while (pattern < pattern_end) {
		if (*pattern == '*') {
			const unsigned char *next_wildcard;
			size_t literal_length;

			++pattern;
			next_wildcard = memchr(pattern, '*',
					       pattern_end - pattern);
			if (next_wildcard == NULL)
				literal_length = pattern_end - pattern;
			else
				literal_length = next_wildcard - pattern;

			for (;;) {
				size_t hostname_left = hostname_end - hostname;
				unicode_val_T uni;

				if (hostname_left < literal_length)
					return 0;

				/* If next_wildcard == NULL, then the
				 * literal string is at the end of the
				 * pattern, so anchor the match to the
				 * end of the hostname.  The end of
				 * this function can then easily
				 * verify that the whole hostname was
				 * matched.
				 *
				 * But do not jump directly there;
				 * first verify that there are no '.'
				 * characters in between.  */
				if ((next_wildcard != NULL
				     || hostname_left == literal_length)
				    && !c_strlcasecmp(pattern, literal_length,
						      hostname, literal_length))
					break;

				/* The literal string doesn't match here.
				 * Skip one character of the hostname and
				 * retry.  If the skipped character is '.'
				 * or one of the equivalent characters
				 * listed in RFC 3490 section 3.1
				 * requirement 1, then return 0, because
				 * '*' must not match such characters.
				 * Do the same if invalid UTF-8 is found.
				 * Cast away const.  */
				uni = utf8_to_unicode((unsigned char **) hostname,
						      hostname_end);
				if (uni == 0x002E
				    || uni == 0x3002
				    || uni == 0xFF0E
				    || uni == 0xFF61
				    || uni == UCS_NO_CHAR)
					return 0;
			}

			pattern += literal_length;
			hostname += literal_length;
		} else {
			if (hostname == hostname_end)
				return 0;

			if (c_toupper(*pattern) != c_toupper(*hostname))
				return 0;

			++pattern;
			++hostname;
		}
	}

	return hostname == hostname_end;
}
示例#15
0
// Convert token to i-code
// Return byte length or 0
unsigned char toktoi() {
	unsigned char i; // Loop counter(i-code sometime)
	unsigned char len = 0; //byte counter
	char* pkw = 0; // Temporary keyword pointer
	char* ptok; // Temporary token pointer
	char* s = lbuf; // Pointer to charactor at line buffer
	char c; // Surround the string character, " or '
	short value; //numeric
	short tmp; //numeric for overflow check

	while (*s) {
		while (c_isspace(*s)) s++; // Skip space

		//Try keyword conversion
		for (i = 0; i < SIZE_KWTBL; i++) {
			pkw = (char *)kwtbl[i]; // Point keyword
			ptok = s; // Point top of command line

			// Compare 1 keyword
			while ((*pkw != 0) && (*pkw == c_toupper(*ptok))) {
				pkw++;
				ptok++;
			}

			if (*pkw == 0) {// Case success

				if (len >= SIZE_IBUF - 1) {// List area full
					err = ERR_IBUFOF;
					return 0;
				}

				// i have i-code
				ibuf[len++] = i;
				s = ptok;
				break;
			}
		}

		// Case statement needs an argument except numeric, valiable, or strings
		if(i == I_REM) {
			while (c_isspace(*s)) s++; // Skip space
			ptok = s;
			for (i = 0; *ptok++; i++); // Get length
			if (len >= SIZE_IBUF - 2 - i) {
				err = ERR_IBUFOF;
				return 0;
			}
			ibuf[len++] = i; // Put length
			while (i--) { // Copy strings
				ibuf[len++] = *s++;
			}
			break;
		}

		if (*pkw == 0)
			continue;

		ptok = s; // Point top of command line

		// Try numeric conversion
		if (c_isdigit(*ptok)) {
			value = 0;
			tmp = 0;
			do {
				tmp = 10 * value + *ptok++ - '0';
				if (value > tmp) {
					err = ERR_VOF;
					return 0;
				}
				value = tmp;
			} while (c_isdigit(*ptok));

			if (len >= SIZE_IBUF - 3) {
				err = ERR_IBUFOF;
				return 0;
			}
			ibuf[len++] = I_NUM;
			ibuf[len++] = value & 255;
			ibuf[len++] = value >> 8;
			s = ptok;
		}
		else

		// Try string conversion
		if (*s == '\"' || *s == '\'') {// If start of string
示例#16
0
static void
tag_handle_meta (int tagid, struct taginfo *tag, struct map_context *ctx)
{
  char *name = find_attr (tag, "name", NULL);
  char *http_equiv = find_attr (tag, "http-equiv", NULL);

  if (http_equiv && 0 == strcasecmp (http_equiv, "refresh"))
    {
      /* Some pages use a META tag to specify that the page be
         refreshed by a new page after a given number of seconds.  The
         general format for this is:

           <meta http-equiv=Refresh content="NUMBER; URL=index2.html">

         So we just need to skip past the "NUMBER; URL=" garbage to
         get to the URL.  */

      struct urlpos *entry;
      int attrind;
      int timeout = 0;
      char *p;

      char *refresh = find_attr (tag, "content", &attrind);
      if (!refresh)
        return;

      for (p = refresh; c_isdigit (*p); p++)
        timeout = 10 * timeout + *p - '0';
      if (*p++ != ';')
        return;

      while (c_isspace (*p))
        ++p;
      if (!(   c_toupper (*p)       == 'U'
            && c_toupper (*(p + 1)) == 'R'
            && c_toupper (*(p + 2)) == 'L'
            &&          *(p + 3)  == '='))
        return;
      p += 4;
      while (c_isspace (*p))
        ++p;

      entry = append_url (p, ATTR_POS(tag,attrind,ctx),
                          ATTR_SIZE(tag,attrind), ctx);
      if (entry)
        {
          entry->link_refresh_p = 1;
          entry->refresh_timeout = timeout;
          entry->link_expect_html = 1;
        }
    }
  else if (http_equiv && 0 == strcasecmp (http_equiv, "content-type"))
    {
      /* Handle stuff like:
         <meta http-equiv="Content-Type" content="text/html; charset=CHARSET"> */

      char *mcharset;
      char *content = find_attr (tag, "content", NULL);
      if (!content)
        return;

      mcharset = parse_charset (content);
      if (!mcharset)
        return;

      xfree_null (meta_charset);
      meta_charset = mcharset;
    }
  else if (name && 0 == strcasecmp (name, "robots"))
    {
      /* Handle stuff like:
         <meta name="robots" content="index,nofollow"> */
      char *content = find_attr (tag, "content", NULL);
      if (!content)
        return;
      if (!strcasecmp (content, "none"))
        ctx->nofollow = true;
      else
        {
          while (*content)
            {
              char *end;
              /* Skip any initial whitespace. */
              content += strspn (content, " \f\n\r\t\v");
              /* Find the next occurrence of ',' or whitespace,
               * or the end of the string.  */
              end = content + strcspn (content, ", \f\n\r\t\v");
              if (!strncasecmp (content, "nofollow", end - content))
                ctx->nofollow = true;
              /* Skip past the next comma, if any. */
              if (*end == ',')
                ++end;
              else
                {
                  end = strchr (end, ',');
                  if (end)
                    ++end;
                  else
                    end = content + strlen (content);
                }
              content = end;
            }
        }
    }
}
示例#17
0
iconv_t
rpl_iconv_open (const char *tocode, const char *fromcode)
#undef iconv_open
{
  char fromcode_upper[32];
  char tocode_upper[32];
  char *fromcode_upper_end;
  char *tocode_upper_end;

#if REPLACE_ICONV_UTF
  /* Special handling of conversion between UTF-8 and UTF-{16,32}{BE,LE}.
     Do this here, before calling the real iconv_open(), because  OSF/1 5.1
     iconv() to these encoding inserts a BOM, which is wrong.
     We do not need to handle conversion between arbitrary encodings and
     UTF-{16,32}{BE,LE}, because the 'striconveh' module implements two-step
     conversion through UTF-8.
     The _ICONV_* constants are chosen to be disjoint from any iconv_t
     returned by the system's iconv_open() functions.  Recall that iconv_t
     is a scalar type.  */
  if (c_toupper (fromcode[0]) == 'U'
      && c_toupper (fromcode[1]) == 'T'
      && c_toupper (fromcode[2]) == 'F'
      && fromcode[3] == '-')
    {
      if (c_toupper (tocode[0]) == 'U'
          && c_toupper (tocode[1]) == 'T'
          && c_toupper (tocode[2]) == 'F'
          && tocode[3] == '-')
        {
          if (strcmp (fromcode + 4, "8") == 0)
            {
              if (c_strcasecmp (tocode + 4, "16BE") == 0)
                return _ICONV_UTF8_UTF16BE;
              if (c_strcasecmp (tocode + 4, "16LE") == 0)
                return _ICONV_UTF8_UTF16LE;
              if (c_strcasecmp (tocode + 4, "32BE") == 0)
                return _ICONV_UTF8_UTF32BE;
              if (c_strcasecmp (tocode + 4, "32LE") == 0)
                return _ICONV_UTF8_UTF32LE;
            }
          else if (strcmp (tocode + 4, "8") == 0)
            {
              if (c_strcasecmp (fromcode + 4, "16BE") == 0)
                return _ICONV_UTF16BE_UTF8;
              if (c_strcasecmp (fromcode + 4, "16LE") == 0)
                return _ICONV_UTF16LE_UTF8;
              if (c_strcasecmp (fromcode + 4, "32BE") == 0)
                return _ICONV_UTF32BE_UTF8;
              if (c_strcasecmp (fromcode + 4, "32LE") == 0)
                return _ICONV_UTF32LE_UTF8;
            }
        }
    }
#endif

  /* Do *not* add special support for 8-bit encodings like ASCII or ISO-8859-1
     here.  This would lead to programs that work in some locales (such as the
     "C" or "en_US" locales) but do not work in East Asian locales.  It is
     better if programmers make their programs depend on GNU libiconv (except
     on glibc systems), e.g. by using the AM_ICONV macro and documenting the
     dependency in an INSTALL or DEPENDENCIES file.  */

  /* Try with the original names first.
     This covers the case when fromcode or tocode is a lowercase encoding name
     that is understood by the system's iconv_open but not listed in our
     mappings table.  */
  {
    iconv_t cd = iconv_open (tocode, fromcode);
    if (cd != (iconv_t)(-1))
      return cd;
  }

  /* Convert the encodings to upper case, because
       1. in the arguments of iconv_open() on AIX, HP-UX, and OSF/1 the case
          matters,
       2. it makes searching in the table faster.  */
  {
    const char *p = fromcode;
    char *q = fromcode_upper;
    while ((*q = c_toupper (*p)) != '\0')
      {
        p++;
        q++;
        if (q == &fromcode_upper[SIZEOF (fromcode_upper)])
          {
            errno = EINVAL;
            return (iconv_t)(-1);
          }
      }
    fromcode_upper_end = q;
  }

  {
    const char *p = tocode;
    char *q = tocode_upper;
    while ((*q = c_toupper (*p)) != '\0')
      {
        p++;
        q++;
        if (q == &tocode_upper[SIZEOF (tocode_upper)])
          {
            errno = EINVAL;
            return (iconv_t)(-1);
          }
      }
    tocode_upper_end = q;
  }

#ifdef ICONV_FLAVOR
  /* Apply the mappings.  */
  {
    const struct mapping *m =
      mapping_lookup (fromcode_upper, fromcode_upper_end - fromcode_upper);

    fromcode = (m != NULL ? m->vendor_name : fromcode_upper);
  }
  {
    const struct mapping *m =
      mapping_lookup (tocode_upper, tocode_upper_end - tocode_upper);

    tocode = (m != NULL ? m->vendor_name : tocode_upper);
  }
#else
  fromcode = fromcode_upper;
  tocode = tocode_upper;
#endif

  return iconv_open (tocode, fromcode);
}
示例#18
0
文件: general.c 项目: rkd77/elinks-tv
void
html_li(struct html_context *html_context, unsigned char *a,
        unsigned char *xxx3, unsigned char *xxx4, unsigned char **xxx5)
{
	int t = par_format.flags & P_LISTMASK;

	/* When handling the code <li><li> @was_li will be 1 and it means we
	 * have to insert a line break since no list item content has done it
	 * for us. */
	if (html_context->was_li) {
		html_context->line_breax = 0;
		ln_break(html_context, 1);
	}

	/*kill_html_stack_until(html_context, 0
	                      "", "UL", "OL", NULL);*/
	if (t == P_NO_BULLET) {
		/* Print nothing. */
	} else if (!par_format.list_number) {
		if (t == P_O) /* Print U+25E6 WHITE BULLET. */
			put_chrs(html_context, (unsigned char *)"&#9702;", 7);
		else if (t == P_SQUARE) /* Print U+25AA BLACK SMALL SQUARE. */
			put_chrs(html_context, (unsigned char *)"&#9642;", 7);
		else /* Print U+2022 BULLET. */
			put_chrs(html_context, (unsigned char *)"&#8226;", 7);
		put_chrs(html_context, (unsigned char *)"&nbsp;", 6);
		par_format.leftmargin += 2;
		par_format.align = ALIGN_LEFT;

	} else {
		unsigned char c = 0;
		int nlen;
		int t = par_format.flags & P_LISTMASK;
		int s = get_num(a, (unsigned char *)"value", html_context->doc_cp);
		struct string n;

		if (!init_string(&n)) return;

		if (s != -1) par_format.list_number = s;

		if (t == P_ALPHA || t == P_alpha) {
			unsigned char n0;

			put_chrs(html_context, (unsigned char *)"&nbsp;", 6);
			c = 1;
			n0 = par_format.list_number
			       ? (par_format.list_number - 1) % 26
			         + (t == P_ALPHA ? 'A' : 'a')
			       : 0;
			if (n0) add_char_to_string(&n, n0);

		} else if (t == P_ROMAN || t == P_roman) {
			roman(&n, par_format.list_number);
			if (t == P_ROMAN) {
				unsigned char *x;

				for (x = n.source; *x; x++) *x = c_toupper(*x);
			}

		} else {
			unsigned char n0[64];
			if (par_format.list_number < 10) {
				put_chrs(html_context, (unsigned char *)"&nbsp;", 6);
				c = 1;
			}

			ulongcat(n0, NULL, par_format.list_number, (sizeof(n) - 1), 0);
			add_to_string(&n, n0);
		}

		nlen = n.length;
		put_chrs(html_context, n.source, nlen);
		put_chrs(html_context, (unsigned char *)".&nbsp;", 7);
		par_format.leftmargin += nlen + c + 2;
		par_format.align = ALIGN_LEFT;
		done_string(&n);

		{
			struct html_element *element;

			element = search_html_stack(html_context, (unsigned char *)"ol");
			if (element)
				element->parattr.list_number = par_format.list_number + 1;
		}

		par_format.list_number = 0;
	}

	html_context->putsp = HTML_SPACE_SUPPRESS;
	html_context->line_breax = 2;
	html_context->was_li = 1;
}