Esempio n. 1
0
int extractSearchSpecData(struct SearchSpecLine *s,char **tokenarray) {

  // 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 = malloc(MAX_SUFFIX_LENGTH*sizeof(char));
  s->begin  = malloc(MAX_STRING_LENGTH*sizeof(char));
  s->end    = malloc(MAX_STRING_LENGTH*sizeof(char));    
  
  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

  s->length = strtoull(tokenarray[2], 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;
  }

  s->beginlength = translate(tokenarray[3]);
  memcpy(s->begin,tokenarray[3],s->beginlength);
  s->endlength   = translate(tokenarray[4]);
  memcpy(s->end,tokenarray[4],s->endlength);  
 
  init_bm_table(s->begin,s->begin_bm_table,s->beginlength, 
		s->casesensitive);
  init_bm_table(s->end,s->end_bm_table,s->endlength,
		s->casesensitive);
  return SCALPEL_OK;
}
Esempio n. 2
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;
}