static uint32 parse_named_entity(const struct array entity) { size_t i, len; char name[16 + 2]; if (entity.size >= N_ITEMS(name) - 2) goto error; len = 0; name[len++] = '&'; for (i = 0; i < entity.size; i++) { const unsigned char c = entity.data[i]; if (!is_ascii_alnum(c)) goto error; name[len++] = c; } name[len++] = ';'; name[len] = '\0'; return html_decode_entity(name, NULL); error: return -1; }
/** * Parses the NUL-terminated string ``s'' for a host address or a hostname. * If ``s'' points to a parsable address, ``*ha'' will be set to it. Otherwise, * ``*ha'' is set to ``zero_host_addr''. If the string is a possible hostname * the function returns TRUE nonetheless and ``*endptr'' will point to the * first character after hostname. * * @param s the string to parse. * @param endptr if not NULL, it will point the first character after * the parsed host address or hostname. * @param ha if not NULL, it is set to the parsed host address or * ``zero_host_addr'' on failure. * @return TRUE if the string points to host address or is a possible * hostname. */ bool string_to_host_or_addr(const char *s, const char **endptr, host_addr_t *ha) { const char *ep; size_t len; host_addr_t addr; if (string_to_host_addr(s, endptr, &addr)) { if (ha) *ha = addr; return TRUE; } for (ep = s; '\0' != *ep; ep++) { if (!is_ascii_alnum(*ep) && '.' != *ep && '-' != *ep) break; } if (ha) *ha = zero_host_addr; if (endptr) *endptr = ep; len = ep - s; return len > 0 && len <= MAX_HOSTLEN; }
static inline const gchar * get_style_for_href(const gchar *href) { unsigned char c; g_return_val_if_fail(href, NULL); do { c = *href++; } while (is_ascii_alnum(c) || '-' == c); return ':' == c ? STYLE_TAG_ANCHOR_EXTERN : STYLE_TAG_ANCHOR; }
/** * Fetch next token from input string. */ static struct ctl_tok * ctl_next_token(struct ctl_string *s) { /* * If we have a read-ahead token, reuse it. */ if (s->unread != NULL) { struct ctl_tok *tok = s->unread; s->unread = NULL; return tok; } /* * Read next token. */ s->p = skip_ascii_blanks(s->p); if ('\0' == *s->p) return ctl_token_alloc(CTL_TOK_EOF, s->p); switch (*s->p) { case '{': s->p++; return ctl_token_alloc(CTL_TOK_LBRACE, s->p); case '}': s->p++; return ctl_token_alloc(CTL_TOK_RBRACE, s->p); case ':': s->p++; return ctl_token_alloc(CTL_TOK_COLON, s->p); case ',': s->p++; return ctl_token_alloc(CTL_TOK_COMMA, s->p); default: break; } if (is_ascii_alnum(*s->p)) { const char *start = s->p; struct ctl_tok *tok = ctl_token_alloc(CTL_TOK_ID, s->p); size_t len; s->p = skip_ascii_alnum(s->p); len = s->p - start; tok->val.s = halloc(len + 1); clamp_strncpy(tok->val.s, len + 1, start, len); return tok; } else { struct ctl_tok *tok = ctl_token_alloc(CTL_TOK_ERROR, s->p); tok->val.c = *s->p; return tok; } g_assert_not_reached(); return NULL; }
/** * Extract variable name from string `s', then fetch value from environment. * * @return variable's value, or "" if not found and set `end' to the address * of the character right after the variable name. */ static const char * get_variable(const char *s, const char **end) { const char *value, *p = s; bool end_brace = FALSE; /* * Grab variable's name. */ if (*p == '{') { p++; s++; end_brace = TRUE; } while (is_ascii_alnum(*p) || *p == '_') { p++; } if (end_brace && *p == '}') *end = &p[1]; else *end = p; /* * Get value from environment. */ { char *name; name = h_strndup(s, p - s); value = getenv(name); if (value == NULL) value = ""; if (common_dbg > 4) g_debug("variable \"%s\" is \"%s\"", name, value); HFREE_NULL(name); } return value; }
static enum html_tag parse_tag(const struct array tag) { static const struct { const char *name; enum html_tag tag; } tab[] = { #define D(x) { #x, HTML_TAG_ ## x, } D(UNKNOWN), D(A), D(B), D(BODY), D(BR), D(COL), D(CODE), D(DD), D(DIV), D(DL), D(DT), D(EM), D(H1), D(H2), D(H3), D(H4), D(H5), D(H6), D(HR), D(HEAD), D(HTML), D(I), D(IMG), D(KBD), D(LI), D(META), D(OL), D(P), D(PRE), D(Q), D(SPAN), D(STRONG), D(TABLE), D(TBODY), D(TD), D(TH), D(THEAD), D(TITLE), D(TR), D(TT), D(UL), { "!--", HTML_TAG_COMMENT }, { "!DOCTYPE", HTML_TAG_DOCTYPE }, #undef D }; size_t i, len; char name[32]; STATIC_ASSERT(N_ITEMS(tab) == NUM_HTML_TAG); len = 0; for (i = 0; i < tag.size; i++) { const unsigned char c = tag.data[i]; if (N_ITEMS(name) == len) break; if (0 == len) { if ('/' == c && 0 == i) continue; if (!is_ascii_alnum(c) && '!' != c && '?' != c) break; } else if (!is_ascii_alnum(c) && '-' != c) { break; } name[len] = ascii_toupper(c); len++; } if (len > 0 && len < N_ITEMS(name)) { name[len] = '\0'; for (i = 0; i < N_ITEMS(tab); i++) { if (0 == strcmp(name, tab[i].name)) return tab[i].tag; } g_warning("%s(): unknown tag: \"%s\"", G_STRFUNC, name); } return HTML_TAG_UNKNOWN; }