Пример #1
0
int
main(int argc, char **argv)
{
  int len, i, flags, n;
  char regex[50];
  char *buf;
  regex_t preg;
  int status, seed;

  seed = time(NULL);
  seed = 1028358583;
  printf("seed = %d\n", seed);
  srand(seed);
  n = 0;

  for (n = 0; n < 0; n++)
    rand();

  while (1)
    {
      printf("*");
      fflush(stdout);

      printf("n = %d\n", n);
      len = 1 + (int)(REGEXP_MAX_LEN * (rand() / (RAND_MAX + 1.0)));
      n++;

      for (i = 0; i < len; i++)
	{
	  regex[i] = 1 + (int)(255 * (rand() / (RAND_MAX + 1.0)));
	  n++;
	}
      regex[i] = L'\0';

      printf("len = %d, regexp = \"%s\"\n", len, regex);

      for (flags = 0;
	   flags < (REG_EXTENDED | REG_ICASE | REG_NEWLINE | REG_NOSUB);
	   flags++)
	{
	  buf = malloc(sizeof(*buf) * len);
	  strncpy(buf, regex, len - 1);
	  status = regncomp(&preg, buf, len, flags);
	  if (status == REG_OK)
	    regfree(&preg);
	}
      printf("\n");
    }

  return 0;
}
Пример #2
0
GLOBAL (void _p_CNewRegEx (RegExType *r, const char *Expression, int ExpressionLength,
                           Boolean ExtendedRegEx, Boolean CaseInsensitive, Boolean NewLines))
{
  int result;
  r->ErrorInternal = NULL;
  r->Error = NULL;
  r->From = 1;
  r->Length = 0;
  r->SubExpressions = 0;
  r->RegMatch = 0;
  r->RegEx = (regex_t *) malloc (sizeof (regex_t));
  result = regncomp (r->RegEx, Expression, ExpressionLength,
            (ExtendedRegEx    ? REG_EXTENDED : 0) |
            (CaseInsensitive  ? REG_ICASE    : 0) |
            (NewLines         ? REG_NEWLINE  : 0));
  if (result)
    {
      if (r->ErrorInternal)
        {
          free (r->ErrorInternal);
          r->ErrorInternal = 0;
        }
      if (result)
        {
          size_t size = regerror (result, r->RegEx, 0, 0);
          r->ErrorInternal = (char *) malloc (size);
          regerror (result, r->RegEx, r->ErrorInternal, size);
        }
      free (r->RegEx);
      r->RegEx = 0;
    }
  else
    {
      r->SubExpressions = r->RegEx->re_nsub - 1;
      r->RegMatch = (regmatch_t *) malloc ((r->SubExpressions + 1) * sizeof (regmatch_t));
    }
}
Пример #3
0
Файл: regexp.c Проект: uarka/sed
static void
compile_regex_1 (struct regex *new_regex, int needed_sub)
{
#ifdef REG_PERL
  int errcode;
  errcode = regncomp(&new_regex->pattern, new_regex->re, new_regex->sz,
                     (needed_sub ? 0 : REG_NOSUB)
                     | new_regex->flags
                     | extended_regexp_flags);

  if (errcode)
    {
      char errorbuf[200];
      regerror(errcode, NULL, errorbuf, 200);
      bad_prog(gettext(errorbuf));
    }
#else
  const char *error;
  int syntax = ((extended_regexp_flags & REG_EXTENDED)
                 ? RE_SYNTAX_POSIX_EXTENDED
                 : RE_SYNTAX_POSIX_BASIC);

  syntax &= ~RE_DOT_NOT_NULL;
  syntax |= RE_NO_POSIX_BACKTRACKING;

  switch (posixicity)
    {
    case POSIXLY_EXTENDED:
      syntax &= ~RE_UNMATCHED_RIGHT_PAREN_ORD;
      break;
    case POSIXLY_CORRECT:
      syntax |= RE_UNMATCHED_RIGHT_PAREN_ORD;
      break;
    case POSIXLY_BASIC:
      syntax |= RE_UNMATCHED_RIGHT_PAREN_ORD | RE_LIMITED_OPS | RE_NO_GNU_OPS;
      break;
    }

#ifdef RE_ICASE
  syntax |= (new_regex->flags & REG_ICASE) ? RE_ICASE : 0;
#endif
#ifdef RE_NO_SUB
  syntax |= needed_sub ? 0 : RE_NO_SUB;
#endif

  new_regex->pattern.fastmap = malloc (1 << (sizeof (char) * 8));

  /* If REG_NEWLINE is set, newlines are treated differently.  */
  if (new_regex->flags & REG_NEWLINE)
    {
      /* REG_NEWLINE implies neither . nor [^...] match newline.  */
      syntax &= ~RE_DOT_NEWLINE;
      syntax |= RE_HAT_LISTS_NOT_NEWLINE;
    }

  re_set_syntax (syntax);
  error = re_compile_pattern (new_regex->re, new_regex->sz,
                              &new_regex->pattern);
  new_regex->pattern.newline_anchor = (new_regex->flags & REG_NEWLINE) != 0;

  new_regex->pattern.translate = NULL;
#ifndef RE_ICASE
  if (new_regex->flags & REG_ICASE)
    {
      static char translate[1 << (sizeof(char) * 8)];
      int i;
      for (i = 0; i < sizeof(translate) / sizeof(char); i++)
        translate[i] = tolower (i);

      new_regex->pattern.translate = translate;
    }
#endif

  if (error)
    bad_prog(error);
#endif

  /* Just to be sure, I mark this as not POSIXLY_CORRECT behavior */
  if (needed_sub
      && new_regex->pattern.re_nsub < needed_sub - 1
      && posixicity == POSIXLY_EXTENDED)
    {
      char buf[200];
      sprintf(buf, _("invalid reference \\%d on `s' command's RHS"),
              needed_sub - 1);
      bad_prog(buf);
    }
}
Пример #4
0
int extractSearchSpecData(struct scalpelState *state, 
                          struct SearchSpecLine *s,
                          char **tokenarray) {

    int err = 0;

    // process one line from config file:
    //     token[0] = suffix
    //     token[1] = case sensitive?
    //     token[2] = maximum carve size
    //     token[3] = begintag
    //     token[4] = endtag
    //     token[5] = search type (optional)

    s->suffix = (char *) malloc(MAX_SUFFIX_LENGTH * sizeof(char));
    checkMemoryAllocation(state, s->suffix, __LINE__, __FILE__, "s->suffix");
    s->begin = (char *) malloc(MAX_STRING_LENGTH * sizeof(char));
    checkMemoryAllocation(state, s->begin, __LINE__, __FILE__, "s->begin");
    s->end = (char *) malloc(MAX_STRING_LENGTH * sizeof(char));
    checkMemoryAllocation(state, s->end, __LINE__, __FILE__, "s->end");
    s->begintext = (char *) malloc(MAX_STRING_LENGTH * sizeof(char));
    checkMemoryAllocation(state, s->begintext, __LINE__, __FILE__,
        "s->begintext");
    s->endtext = (char *) malloc(MAX_STRING_LENGTH * sizeof(char));
    checkMemoryAllocation(state, s->endtext, __LINE__, __FILE__, "s->endtext");

    if (!strncasecmp(tokenarray[0], SCALPEL_NOEXTENSION_SUFFIX,
        strlen(SCALPEL_NOEXTENSION_SUFFIX))) {
            s->suffix[0] = SCALPEL_NOEXTENSION;
            s->suffix[1] = 0;
    } else {
        memcpy(s->suffix, tokenarray[0], MAX_SUFFIX_LENGTH);
    }

    // case sensitivity check
    s->casesensitive = (!strncasecmp(tokenarray[1], "y", 1)
        || !strncasecmp(tokenarray[1], "yes", 3));

    //#ifdef _WIN32
    //    s->length = _atoi64(tokenarray[2]);
    //#else
    //  s->length = atoull(tokenarray[2]);
    //#endif

    char split[MAX_STRING_LENGTH];
    char *maxcarvelength;

    strcpy(split, tokenarray[2]);
    maxcarvelength = strchr(split, ':');
    if (!maxcarvelength) {
        s->minlength = 0;
        s->length = strtoull(split, 0, 10);
    } else {
        *maxcarvelength = 0;
        maxcarvelength++;
        s->minlength = strtoull(split, 0, 10);
        s->length = strtoull(maxcarvelength, 0, 10);
    }

    // determine search type for this needle
    s->searchtype = SEARCHTYPE_FORWARD;
    if (!strncasecmp(tokenarray[5], "REVERSE", strlen("REVERSE"))) {
        s->searchtype = SEARCHTYPE_REVERSE;
    } else if (!strncasecmp(tokenarray[5], "NEXT", strlen("NEXT"))) {
        s->searchtype = SEARCHTYPE_FORWARD_NEXT;
    }
    // FORWARD is the default, but OK if the user defines it explicitly
    else if (!strncasecmp(tokenarray[5], "FORWARD", strlen("FORWARD"))) {
        s->searchtype = SEARCHTYPE_FORWARD;
    }

    // regular expressions must be handled separately

    if (isRegularExpression(tokenarray[3])) {

#ifdef GPU_THREADING
        // GPU execution does not support regex needles.
        std::stringstream ss;
        ss << "ERROR: GPU search for regex headers is not supported!\n";
        ss << "Please modify the config file for non-regex headers only.\n";
        std::string msg = ss.str();
        fprintf(stderr, msg.c_str());
        throw std::runtime_error(msg);
#endif

        // copy RE, zap leading/training '/' and prepare for regular expression compilation
        s->beginisRE = 1;
        strcpy(s->begin, tokenarray[3]);
        strcpy(s->begintext, tokenarray[3]);
        s->beginlength = strlen(tokenarray[3]);
        s->begin[s->beginlength] = 0;
        // compile regular expression
        err = regncomp(&(s->beginstate.re), s->begin + 1, s->beginlength - 2,
            REG_EXTENDED | (REG_ICASE * (!s->casesensitive)));
        if (err) {
            return SCALPEL_ERROR_BAD_HEADER_REGEX;
        }
    } else {
        // non-regular expression header
        s->beginisRE = 0;
        strcpy(s->begintext, tokenarray[3]);
        s->beginlength = translate(tokenarray[3]);
        memcpy(s->begin, tokenarray[3], s->beginlength);
        init_bm_table(s->begin, s->beginstate.bm_table, s->beginlength,
            s->casesensitive);
    }

    if (isRegularExpression(tokenarray[4])) {

#ifdef GPU_THREADING
        // GPU execution does not support regex needles.
        std::stringstream ss;
        ss << "ERROR: GPU search for regex footers is not supported!\n";
        ss << "Please modify the config file for non-regex footers only.\n";
        std::string msg = ss.str();
        fprintf(stderr, msg.c_str());
        throw std::runtime_error(msg);
#endif  

        // copy RE, zap leading/training '/' and prepare for for regular expression compilation
        s->endisRE = 1;
        strcpy(s->end, tokenarray[4]);
        strcpy(s->endtext, tokenarray[4]);
        s->endlength = strlen(tokenarray[4]);
        s->end[s->endlength] = 0;
        // compile regular expression
        err = regncomp(&(s->endstate.re), s->end + 1, s->endlength - 2,
            REG_EXTENDED | (REG_ICASE * (!s->casesensitive)));

        if (err) {
            return SCALPEL_ERROR_BAD_FOOTER_REGEX;
        }
    } else {
        s->endisRE = 0;
        strcpy(s->endtext, tokenarray[4]);
        s->endlength = translate(tokenarray[4]);
        memcpy(s->end, tokenarray[4], s->endlength);
        init_bm_table(s->end, s->endstate.bm_table, s->endlength,
            s->casesensitive);
    }

    return SCALPEL_OK;
}