Example #1
0
static int match_gend(struct ExecCtx *ctx, const struct Op *f_op, const char *str, struct GMatch *gm)
{
	int err = REG_NOMATCH;
	const struct Op *op = gm->owner;
	bool zeromatch = (str == gm->start);
	bool gotmatch = false;

	/* ignore follow-up empty matches, unless it has backrefs */
	if (zeromatch && gm->count > 0 && gm->count >= op->mincnt && !gm->owner->gdata.has_refs)
		return REG_NOMATCH;

	/* tag as matched */
	gm->end = str;

	/* try more repeats, stop if count full or last match was zero-length */
	if (gm->count + 1 < op->maxcnt && !zeromatch) {
		err = match_group(ctx, op, str, gm);
		if (err == 0 && STRICT)
			gotmatch = true;
		else if (err != REG_NOMATCH)
			return err;
	}

	/* fail if not enough repeats */
	if (!zeromatch && gm->count + 1 < op->mincnt)
		return err;

	/* continue with parent branch */
	err = do_match(ctx, op->next, str, gm->parent);
	if (err == REG_NOMATCH && gotmatch)
		err = 0;
	return err;
}
Example #2
0
char* match_named_group(match_t* match, char* grname) {
   assert(match);
   int* gr = NULL;
   if (match->names)
      gr = (int*) obhash_find(match->names, grname);
   if (!gr)
      return NULL;
   return match_group(match, *gr);
}
Example #3
0
void do_test() {
Start:
   if (isatty(fileno(stdin)))
      printf("Enter a regular expression:\n\n");
   if (fgets(buf, BUFFER, stdin) == NULL)
      return;
   trim(buf);
   if ((shre = shre_compile(buf)) == NULL) {
      printf("error: %s\n", shre_strerror(shre_er));
      goto Start;
   }
   if (isatty(fileno(stdin)))
      printf("\nEnter text to test the regular expression:\n\n");
   while (fgets(buf, BUFFER, stdin) != NULL) {
      trim(buf);
      if (strcmp(buf, "NEW") == 0) {
         goto Start;
      }
      printf("Pattern:  '%s'\n", shre_expression(shre));
      printf("String:   '%s'\n", buf);
      printf("Match:    ");
      if ((match = shre_search(shre, buf)) == NULL) {
         printf(" None\n\n");
      } else {
         printf("'%s'\n", match_get(match));
         for (int i = 1; i < match_num_groups(match); ++i) {
            printf("Group %2d: ", i);
            char* str = match_group(match, i);
            if (!str) {
               printf(" NULL\n");
            } else {
               printf("'%s'\n", str);
               free(str);
            }
         }
         printf("\n");
         match_free(match);
      }
   }
}
Example #4
0
	/*
	** Determines whether the directory entry's name matches the glob
	** pattern.
	*/
	bool operator()(const directory_entry& ent)
	const noexcept
	{
		#ifndef NDEBUG
			assert(m_pat.length() > 0);
		#endif

		// Offset into pattern that we are matching.
		auto i = 0u;
		// Offset into the file name.
		auto j = 0u;
		auto s = ent.name();

		do switch(m_pat[i]) {
		default:   if (m_pat[i++] != s[j++])         return false; continue;
		case '*':  ++i; return match_wildcard(s, i, j);
		case '?':  ++i; ++j;                                       continue;
		case '[':  ++i; if (!match_group(s, i, j))   return false; continue;
		case '\\': ++i; if (m_pat[i++] != s[j++])    return false; continue;
		} while (i != m_pat.length() && j != s.length());
		return (i == m_pat.length() || m_pat[i] == '*') && j == s.length();
	}
Example #5
0
	/*
	** After this method is called, we must iteratively test each character
	** of `s` beginning at `s[j]` to check whether it matches the characters
	** after the wildcards beginning at `m_pat[i - 1]`.
	*/
	bool match_wildcard(const boost::string_ref s, unsigned i, unsigned j)
	const noexcept
	{
		/*
		** We keep track of the index `pi` after each wildcard that we
		** are trying to match, and the index `pj` into `s` at which we
		** are looking for a match. If the characters after the wildcard
		** fail to match the string beginning at `s[j]`, then we reset
		** `i` to `pi` and `j` to `pj`.
		*/

		// First index of current subpattern for which we are looking
		// for a match. A subpattern is a pattern between a wildcard and
		// another wildcard or null character.
		auto pi = i;
		// Current index at which we are looking for a match.
		auto pj = j;

		while (s[j] != '\0') switch (m_pat[i]) {
			default:
				if (m_pat[i] != s[j]) {
					++pj;
					i = pi;
					j = pj;
					continue;
				}
				else {
					++i;
					++j;
					continue;
				}
			case '*':
				++i;
				pi = i;
				pj = j;
				continue;
			case '?':
				++i;
				++j;
				continue;
			case '[':
				++i;
				if (!match_group(s, i, j)) {
					++pj;
					i = pi;
					j = pj;
				}
				continue;
			case '\\':
				/*
				** If we increment `i` by one, then we will
				** interpret the following escaped character as
				** a special character at the next iteration. To
				** prevent this, we must anticipate what should
				** happen in the next iteration.
				*/
				if (m_pat[i + 1] != s[j]) {
					++pj;
					i = pi;
					j = pj;
					continue;
				}
				else {
					i += 2;
					++j;
					continue;
				}
		}
		return i == m_pat.length();
	}
Example #6
0
						// We handle 2 same as 3 as all of our destinations are
						// multicast

						// XXX send icmp parameter problem, code 2 to the source

						return;
					}
				}
			}
		}
	}

	/* Prepare the packet to be forwarded, decrementing the hop limit. */
	hdr->ip6_hlim--;

	us_mfa_group *grp = match_group(hdr->ip6_dst);

	if (grp) {
		grp->route(dev, hdr, len);
	} else {
		discovered_source(dev, hdr->ip6_dst, hdr->ip6_src);
	}
}

void us_mfa::forward(interface *intf, ip6_hdr *hdr, uint16_t len) const {
	if (len > intf->mtu()) {
		send_icmpv6_toobig(intf, hdr, len);
		return;
	}

	/* Any socket will do. */
Example #7
0
static void parse_config_value(am_xml_parser_ctx_t *x, const char *prm, int type,
        int *itm_sz, void *itm, const char *val, int len) {

    if (itm == NULL || val == NULL || len <= 0 || strcmp(x->current_name, prm) != 0) {
        return;
    }

    switch (type) {
        case CONF_STRING:
        {
            char **value = (char **) itm;
            *value = strndup(val, len);
            if (x->log_enable) {
                if (strstr(prm, "password") != NULL) {
                    AM_LOG_DEBUG(x->conf->instance_id, "am_parse_config_xml() %s is set to '********'",
                            prm);
                    break;
                }
                AM_LOG_DEBUG(x->conf->instance_id, "am_parse_config_xml() %s is set to '%s'",
                        prm, LOGEMPTY(*value));
            }
        }
            break;
        case CONF_NUMBER:
        {
            int *value = (int *) itm;
            if (strncasecmp(val, "on", len) == 0 || strncasecmp(val, "true", len) == 0 ||
                    strncasecmp(val, "local", len) == 0) {
                *value = 1;
            } else if (strncasecmp(val, "off", len) == 0 || strncasecmp(val, "false", len) == 0 ||
                    strncasecmp(val, "centralized", len) == 0) {
                *value = 0;
            } else {
                char *t = strndup(val, len);
                if (t != NULL) {
                    *value = strtol(t, NULL, AM_BASE_TEN);
                    free(t);
                }
            }
            if (x->log_enable) {
                AM_LOG_DEBUG(x->conf->instance_id, "am_parse_config_xml() %s is set to '%d'", prm, *value);
            }
        }
            break;
        case CONF_DEBUG_LEVEL:
        {
            int *value = (int *) itm;
            *value = AM_LOG_LEVEL_NONE;
            if (strncasecmp(val, "all", 3) == 0) {
                *value = AM_LOG_LEVEL_DEBUG;
            } else if (strncasecmp(val, "error", len) == 0) {
                *value = AM_LOG_LEVEL_ERROR;
            } else if (strncasecmp(val, "info", len) == 0) {
                *value = AM_LOG_LEVEL_INFO;
            } else if (strncasecmp(val, "message", len) == 0) {
                *value = AM_LOG_LEVEL_WARNING;
            } else if (strncasecmp(val, "warning", len) == 0) {
                *value = AM_LOG_LEVEL_WARNING;
            }
            if (x->log_enable) {
                AM_LOG_DEBUG(x->conf->instance_id, "am_parse_config_xml() %s is set to '%d'",
                        prm, *value);
            }
        }
            break;
        case CONF_ATTR_MODE:
        {
            int *value = (int *) itm;
            *value = AM_SET_ATTRS_NONE;
            if (strncasecmp(val, "HTTP_HEADER", len) == 0) {
                *value = AM_SET_ATTRS_AS_HEADER;
            } else if (strncasecmp(val, "HTTP_COOKIE", len) == 0) {
                *value = AM_SET_ATTRS_AS_COOKIE;
            }
            if (x->log_enable) {
                AM_LOG_DEBUG(x->conf->instance_id, "am_parse_config_xml() %s is set to '%d'",
                        prm, *value);
            }
        }
            break;
        case CONF_AUDIT_LEVEL:
        {
            int *value = (int *) itm;
            *value = 0;
            if (strncasecmp(val, "LOG_ALLOW", len) == 0) {
                *value |= AM_LOG_LEVEL_AUDIT_ALLOW;
            } else if (strncasecmp(val, "LOG_BOTH", len) == 0) {
                *value |= AM_LOG_LEVEL_AUDIT_ALLOW;
                *value |= AM_LOG_LEVEL_AUDIT_DENY;
            } else if (strncasecmp(val, "LOG_DENY", len) == 0) {
                *value |= AM_LOG_LEVEL_AUDIT_DENY;
            } else if (strncasecmp(val, "ALL", len) == 0) {
                *value |= AM_LOG_LEVEL_AUDIT;
                *value |= AM_LOG_LEVEL_AUDIT_REMOTE;
            } else if (strncasecmp(val, "LOCAL", len) == 0) {
                *value |= AM_LOG_LEVEL_AUDIT;
            } else if (strncasecmp(val, "REMOTE", len) == 0) {
                *value |= AM_LOG_LEVEL_AUDIT_REMOTE;
            }
            if (x->log_enable) {
                AM_LOG_DEBUG(x->conf->instance_id, "am_parse_config_xml() %s is set to '%d'",
                        prm, *value);
            }
        }
            break;
        case CONF_STRING_LIST:
        {
            int old_sz = *itm_sz;
            char ***value = (char ***) itm;
            char **value_tmp;
            char *v = strndup(val, len);
            if (v == NULL) break;
            value_tmp = (char **) realloc(*value, sizeof (char *) *(++(*itm_sz)));
            if (value_tmp == NULL) {//TODO: realloc failure?
                (*itm_sz)--;
                if (*itm_sz < 0) *itm_sz = 0;
                free(v);
                break;
            }
            *value = value_tmp;
            (*value)[old_sz] = v;
            if (x->log_enable) {
                AM_LOG_DEBUG(x->conf->instance_id, "am_parse_config_xml() %s is set to %d value(s)",
                        prm, *itm_sz);
            }
        }
            break;
        case CONF_NUMBER_LIST:
        {
            int old_sz = *itm_sz;
            int **value = (int **) itm;
            int *value_tmp;
            char *v = strndup(val, len);
            if (v == NULL) break;
            value_tmp = (int *) realloc(*value, sizeof (int) *(++(*itm_sz)));
            if (value_tmp == NULL) {
                (*itm_sz)--;
                if (*itm_sz < 0) *itm_sz = 0;
                free(v);
                break;
            }
            *value = value_tmp;
            (*value)[old_sz] = strtol(v, NULL, AM_BASE_TEN);
            free(v);
            if (x->log_enable) {
                AM_LOG_DEBUG(x->conf->instance_id, "am_parse_config_xml() %s is set to %d value(s)",
                        prm, *itm_sz);
            }
        }
            break;
        case CONF_STRING_MAP:
        {
            int old_sz = *itm_sz;
            am_config_map_t **value = (am_config_map_t **) itm;
            am_config_map_t *value_tmp;
            size_t slen = (size_t) len;
            char *v, *mv;

            if (val[0] != '[') break;
            v = strndup(val, len);
            if (v == NULL) break;

            mv = match_group(x->rgx, 2 /* groups in [key]=value */, v, &slen);
            free(v);
            if (mv == NULL) break;

            value_tmp = (am_config_map_t *) realloc(*value, sizeof (am_config_map_t) *(++(*itm_sz)));
            if (value_tmp == NULL) {
                (*itm_sz)--;
                if (*itm_sz < 0) *itm_sz = 0;
                free(mv);
                break;
            }
            *value = value_tmp;
            (&(*value)[old_sz])->name = mv;
            (&(*value)[old_sz])->value = mv + strlen(mv) + 1;
            if (x->log_enable) {
                AM_LOG_DEBUG(x->conf->instance_id, "am_parse_config_xml() %s is set to %d value(s)",
                        prm, *itm_sz);
            }
        }
            break;
        default:
            AM_LOG_WARNING(x->conf->instance_id, "am_parse_config_xml() unknown type value %d setting %s", type, prm);
            break;
    }
}