Example #1
0
static void test6()
{
    mpm_re *re;
    int i;
    char * subjects1[] = { "maab", "aabb", "aa", "a", NULL };
    char * subjects2[] = { "maab", "aabb", "aa", "a", "m\naa", "\r\naa", "a\ra", "\raa\n", NULL };
    char * subjects3[] = { "m\xab", "\n\xab", "\xab\n", NULL };

    printf("Test6: Testing multiline and ^ assertion.\n\n");

    test_single_match("^aa", MPM_ADD_VERBOSE, MPM_COMPILE_VERBOSE | MPM_COMPILE_VERBOSE_STATS, subjects1);
    test_single_match("^aa", MPM_ADD_MULTILINE | MPM_ADD_VERBOSE, MPM_COMPILE_VERBOSE | MPM_COMPILE_VERBOSE_STATS, subjects2);
    test_single_match("^\\xab", MPM_ADD_MULTILINE | MPM_ADD_VERBOSE, MPM_COMPILE_VERBOSE | MPM_COMPILE_VERBOSE_STATS, subjects3);
    test_single_match("^[^\\xab]", MPM_ADD_MULTILINE | MPM_ADD_VERBOSE, MPM_COMPILE_VERBOSE | MPM_COMPILE_VERBOSE_STATS, subjects3);

    re = test_mpm_create();
    if (!re)
        return;

    test_mpm_add_fail(re, "^(?:a|a*)", MPM_ADD_VERBOSE, MPM_EMPTY_PATTERN);
    test_mpm_add_fail(re, "^(?:a|a*)", MPM_ADD_MULTILINE | MPM_ADD_VERBOSE, MPM_EMPTY_PATTERN);
    test_mpm_add_fail(re, "^a|a", MPM_ADD_VERBOSE, MPM_UNSUPPORTED_PATTERN);

    for (i = 0; i < 32; i++)
        test_mpm_add(re, "A", 0);
    test_mpm_add_fail(re, "B", 0, MPM_PATTERN_LIMIT);
    mpm_free(re);
}
Example #2
0
static void test2()
{
    mpm_re *re;

    printf("Test2: Testing iterators.\n\n");

    re = test_mpm_create();
    if (!re)
        return;

    test_mpm_add(re, "#a+?#b*#c??#d{3,6}#e{0,3}?#f{2,}#", MPM_ADD_VERBOSE);
    test_mpm_add(re, "#a+#b*?#c?#d{3,6}?#e{0,3}#f{2,}?#", MPM_ADD_CASELESS | MPM_ADD_VERBOSE);
    test_mpm_add(re, "#[^a]+?#[^b]*#[^c]??#[^d]{3,6}#[^e]{0,3}?#[^f]{2,}#", MPM_ADD_VERBOSE);
    test_mpm_add(re, "#[^a]+#[^b]*?#[^c]?#[^d]{3,6}?#[^e]{0,3}#[^f]{2,}?#", MPM_ADD_CASELESS | MPM_ADD_VERBOSE);
    test_mpm_add(re, "#\\s+?#\\w*#\\d??#\\h{3,6}#\\w{0,3}?#.{2,}#", MPM_ADD_CASELESS | MPM_ADD_VERBOSE);
    test_mpm_add(re, "#\\S+?#\\W*#\\D??#\\H{6,9}#\\W{0,7}?#.{6,}#", MPM_ADD_DOTALL | MPM_ADD_VERBOSE);
    test_mpm_add(re, "#[a-z]+?#[a-z]*#[a-z]??#[a-z]{3,6}#[a-z]{0,3}?#[a-z]{2,}#", MPM_ADD_VERBOSE);
    test_mpm_add(re, "aa|bb(cc(?:dd|ee)|ff)", MPM_ADD_VERBOSE);
    test_mpm_add(re, "a.+b*?", MPM_ADD_VERBOSE | MPM_ADD_FIXED(6));
    test_mpm_add(re, "x[Bm]*Y?", MPM_ADD_VERBOSE | MPM_ADD_CASELESS | MPM_ADD_FIXED(6));
    test_mpm_add_fail(re, "(ab|cd(mn|op)+|ef(gh)?)*", MPM_ADD_VERBOSE, MPM_EMPTY_PATTERN);
    test_mpm_add_fail(re, "a?b?", MPM_ADD_VERBOSE, MPM_EMPTY_PATTERN);
    test_mpm_add_fail(re, "a|b?", MPM_ADD_VERBOSE, MPM_EMPTY_PATTERN);
    test_mpm_add_fail(re, "", MPM_ADD_VERBOSE, MPM_EMPTY_PATTERN);
    test_mpm_add_fail(re, "(.)\\1", MPM_ADD_VERBOSE, MPM_UNSUPPORTED_PATTERN);
    test_mpm_add_fail(re, "(?", MPM_ADD_VERBOSE, MPM_INVALID_PATTERN);

    mpm_free(re);
}
Example #3
0
static void test7()
{
    mpm_re *re;

    printf("Test7: Testing offsets.\n\n");

    re = test_mpm_create();
    if (!re)
        return;

    printf("\nTest1:\n");
    test_mpm_add(re, "^a", MPM_ADD_MULTILINE);
    test_mpm_add(re, "^a", 0);
    test_mpm_add(re, "\\na", 0);
    test_mpm_compile(re, NULL, 0);
    test_mpm_exec(re, "a\na", 0);
    test_mpm_exec(re, "a\na", 2);
    test_mpm_exec(re, "a\na\na", 2);
    mpm_free(re);

    re = test_mpm_create();
    if (!re)
        return;

    printf("\nTest2:\n");
    test_mpm_add(re, "^a", 0);
    test_mpm_add(re, "\\na", 0);
    test_mpm_compile(re, NULL, 0);
    test_mpm_exec(re, "a\na", 0);
    test_mpm_exec(re, "a\na\n", 2);
    test_mpm_exec(re, "a\na\na", 2);
    mpm_free(re);

    re = test_mpm_create();
    if (!re)
        return;

    printf("\nTest3:\n");
    test_mpm_add(re, "^a", MPM_ADD_MULTILINE);
    test_mpm_add(re, "\\na", 0);
    test_mpm_compile(re, NULL, 0);
    test_mpm_exec(re, "a\na", 0);
    test_mpm_exec(re, "a\na\nb", 2);
    test_mpm_exec(re, "a\na\na", 2);
    mpm_free(re);
}
Example #4
0
static void test_multiple_match(mpm_re *re, int compile_flags, char **subject)
{
    test_mpm_compile(re, NULL, compile_flags);
    while (subject[0]) {
        test_mpm_exec(re, subject[0], 0);
        subject++;
    }
    puts("");
    mpm_free(re);
}
Example #5
0
void decodeSigHandler(int sig) {
	if (sig == SIGCHLD) {
		int status;
		if (mpm_get_id(MPM_DECODE) && mpm_get_id(MPM_DECODE)
	    			== wait3(&status, WNOHANG, NULL)) {
			if (WIFSIGNALED(status) && WTERMSIG(status) != SIGTERM
					&& WTERMSIG(status) != SIGINT) {
				ERROR("decode process died from signal: %i\n",
				      WTERMSIG(status));
			}
			mpm_set_id(MPM_DECODE,0);
			mpm_free(MPM_DECODE);
		}
	} else if (sig == SIGTERM) {
		if (mpm_get_id(MPM_DECODE) > 0)
			kill(mpm_get_id(MPM_DECODE), SIGTERM);
		mpm_free(MPM_DECODE);
		exit(EXIT_SUCCESS);
	}
}
Example #6
0
static void test3()
{
    mpm_re *re;

    printf("Test3: A large set.\n\n");

    re = test_mpm_create();
    if (!re)
        return;

    test_mpm_add(re, "\\x3Cobject[^\\x3E]+?data\\s*\\x3D\\s*\\x22\\x22", MPM_ADD_VERBOSE);
    test_mpm_add(re, "^[^\\s]{256}", MPM_ADD_VERBOSE);
    test_mpm_compile(re, NULL, MPM_COMPILE_VERBOSE | MPM_COMPILE_VERBOSE_STATS);

    mpm_free(re);
}
Example #7
0
static void test1()
{
    mpm_re *re;

    printf("Test1: Testing character classes.\n\n");

    re = test_mpm_create();
    if (!re)
        return;

    test_mpm_add(re, "aB#.\\x00\\x01\\xff\\xfe", MPM_ADD_VERBOSE);
    test_mpm_add(re, ".[^c][^\\x00][^\\x01][^\\xfe][^\\xff]", MPM_ADD_DOTALL | MPM_ADD_VERBOSE);
    test_mpm_add(re, "aB[^c][^D]#[^#]", MPM_ADD_CASELESS | MPM_ADD_VERBOSE);
    test_mpm_add(re, " [a-z] [\\x00-\\x05y-\\xff]  (?i)[c-fMX] ", MPM_ADD_EXTENDED | MPM_ADD_VERBOSE);
    test_mpm_add(re, " [\\x01\\x02def\\xfd\\xfe]  (?i)[cd\\s]  [\\vedcb \\d] ", MPM_ADD_EXTENDED | MPM_ADD_VERBOSE);
    test_mpm_add(re, "\\d\\D\\w\\W\\s\\S\\h\\H\\v\\V", MPM_ADD_CASELESS | MPM_ADD_VERBOSE);

    mpm_free(re);
}
Example #8
0
static void new_feature(void)
{
#if 0

    mpm_re *re;

    re = test_mpm_create();
    if (!re)
        return;

    test_mpm_add(re, "aa.b*", MPM_ADD_VERBOSE);
    test_mpm_add(re, "ma?", MPM_ADD_VERBOSE);
    test_mpm_add(re, "aa", MPM_ADD_VERBOSE | MPM_ADD_FIXED(2));
    printf("Distance: %d\n", mpm_distance(re, 0, re, 1));
    test_mpm_compile(re, NULL, MPM_COMPILE_VERBOSE | MPM_COMPILE_VERBOSE_STATS);
    test_mpm_exec(re, "mmaa bb", 0);
    test_mpm_exec(re, "aa", 0);
    test_mpm_exec(re, "aax", 0);

    mpm_free(re);

#elif 0

    mpm_re *re;
    int i;

    load_pattern_list("../../patterns3.txt");
    if (!loaded_items)
        return;

    mpm_clustering(loaded_items, loaded_items_size, MPM_CLUSTERING_VERBOSE);

    printf("Group 0:\n  %s\n", (char *)loaded_items[0].data);
    re = loaded_items[0].re;
    for (i = 1; i < loaded_items_size; i++) {
        if (loaded_items[i].group_id != loaded_items[i - 1].group_id) {
            if (mpm_compile(re, NULL, MPM_COMPILE_VERBOSE_STATS) != MPM_NO_ERROR)
                printf("WARNING: mpm_compile failed\n");
            printf("\nGroup: %d\n", loaded_items[i].group_id);
            re = loaded_items[i].re;
        } else {
            if (mpm_combine(&re, loaded_items[i].re, 0) != MPM_NO_ERROR)
                printf("WARNING: mpm_combine failed\n");
        }

        printf("  %s\n", (char *)loaded_items[i].data);
    }

    mpm_compile(re, NULL, MPM_COMPILE_VERBOSE_STATS);

#elif 1

    mpm_rule_list *rule_list;
    mpm_uint32 result[2] = { 0, 0 };
    int error_code;
    mpm_compile_rules_args args = {
        2,     /* no_selected_patterns */
        0,     /* minimum_no_new_cover */
        -1.0,  /* rule_strength_scale */
        -1.0,  /* inner_distance_scale */
        -1.0,  /* outer_distance_scale */
        -1.0   /* length_scale */
    };

    mpm_rule_pattern rules[] = {
        { (mpm_char8 *)"ab{4,17}c*d+xyz|h", MPM_RULE_NEW },
        { (mpm_char8 *)"ab{4,17}c*d+xyz", MPM_RULE_NEW },
        { (mpm_char8 *)"(cccc|dddd){4,8}b{4,17}c*d+xyze{4,}", MPM_RULE_NEW },
        { (mpm_char8 *)"(ccc)*(ddd)+(eee)?xyz", MPM_RULE_NEW },
#if 0
        { (mpm_char8 *)"(eee)?x?y?z?abc", MPM_RULE_NEW },
        { (mpm_char8 *)"(eee)?x?y?z?def", MPM_RULE_NEW },
        { (mpm_char8 *)"(eee)?x?y?z?ghi", MPM_RULE_NEW },
#endif
    };
    char *subject = "bbbbdxyz h";

    error_code = mpm_compile_rules(rules, sizeof(rules) / sizeof(mpm_rule_pattern), &rule_list,
        NULL, &args, MPM_COMPILE_RULES_VERBOSE | MPM_COMPILE_RULES_VERBOSE_STATS);
    printf("mpm_compile_rules: %s\n", mpm_error_to_string(error_code));
    if (rule_list) {
        mpm_exec_list(rule_list, (mpm_char8 *)subject, strlen(subject), 0, result);
        mpm_rule_list_free(rule_list);
    }

    printf("\nResult: 0x%x 0x%x\n", result[0], result[1]);

#elif 0

#include "../../uri/patterns.txt"

    mpm_rule_list *rule_list;
    mpm_size consumed_memory;
    mpm_compile_rules_args args = {
        20,   /* no_selected_patterns */
        4,    /* minimum_no_new_cover */
        0.2,  /* rule_strength_scale */
        0.15, /* inner_distance_scale */
        0.3,  /* outer_distance_scale */
        0.2   /* length_scale */
    };

    printf("Processing %d rules:\n", (int)(sizeof(rules_global) / sizeof(mpm_rule_pattern)));

    mpm_compile_rules(rules_global, sizeof(rules_global) / sizeof(mpm_rule_pattern), &rule_list,
        &consumed_memory, &args, 0 | MPM_COMPILE_RULES_VERBOSE | MPM_COMPILE_RULES_VERBOSE_STATS | MPM_COMPILE_RULES_IGNORE_FIXED);
    if (!rule_list)
        return;

    printf("MPM State machine size: %ld\n", (long)consumed_memory);

    mpm_rule_list_free(rule_list);

#else

    /* Ignore this case. */
    printf("No test selected!\n\n");

#endif
}
Example #9
0
static void load_pattern_list(char* file_name)
{
    FILE *f = fopen(file_name, "rt");
    char data[MAX_LINE_LENGTH];
    int line, count, skipped_count, unsupported_count, flags, len;
    compiled_pattern *first_pattern, *last_pattern;
    compiled_pattern *current_pattern;
    mpm_re *current_re;
    mpm_cluster_item *loaded_item_ptr;
    char *ptr;

    if (!f) {
        printf("Cannot open file: %s\n", file_name);
        return;
    }

    line = 1;
    count = 0;
    skipped_count = 0;
    unsupported_count = 0;
    first_pattern = NULL;
    last_pattern = NULL;
    while (1) {
        if (!fgets(data, MAX_LINE_LENGTH, f))
            break;

        current_re = mpm_create();
        if (!current_re) {
            printf("WARNING: mpm_create is failed: %s\n", mpm_error_to_string(MPM_NO_MEMORY));
            return;
        }

        if (memcmp(data, "regex \"/", 8) == 0 || memcmp(data, "regex !\"/", 9) == 0) {
            flags = process_regex(data);
            /* An error happened. */
            if (flags == -1)
                continue;

            ptr = data + ((data[6] == '!') ? 9 : 8);
            flags = mpm_add(current_re, (mpm_char8*)ptr, flags | MPM_ADD_TEST_RATING);
            if (flags != MPM_NO_ERROR) {
                printf("Warning: mpm_add returned with '%s' in line:%d '%s'\n", mpm_error_to_string(flags), line, ptr);
                ptr = NULL;
            }

            len = strlen(data);
            data[len] = '/';
            len += strlen(data + len);
            if (data[len - 1] == '\n')
                data[len - 1] = '\0';
        } else if (memcmp(data, "pattern ", 8) == 0) {
            ptr = process_fixed_string(data);
            flags = mpm_add(current_re, (mpm_char8*)(data + 8), MPM_ADD_FIXED(ptr - (data + 8)) | MPM_ADD_TEST_RATING);
            if (flags != MPM_NO_ERROR) {
                printf("Warning: mpm_add returned with '%s' in line:%d '%s'\n", mpm_error_to_string(flags), line, data + 8);
                ptr = NULL;
            } else
                ptr = data + 8;
        } else {
            flags = MPM_NO_ERROR;
            ptr = NULL;
            printf("Warning: Unknown type: line:%d %s\n", line, data);
        }

        if (flags == MPM_TOO_LOW_RATING)
            skipped_count++;
        else if (flags != MPM_NO_ERROR)
            unsupported_count++;

        line++;
        if (!ptr) {
            mpm_free(current_re);
            continue;
        }

        ptr = data;
        current_pattern = (compiled_pattern *)malloc(sizeof(compiled_pattern));
        if (!current_pattern) {
            printf("WARNING: out of memory\n");
            return;
        }

        len = strlen(ptr);
        current_pattern->pattern = (char *)malloc(len + 1);
        if (!current_pattern->pattern) {
            printf("WARNING: out of memory\n");
            return;
        }
        memcpy(current_pattern->pattern, ptr, len + 1);

        current_pattern->next = NULL;
        current_pattern->re = current_re;
        if (!first_pattern)
            first_pattern = current_pattern;
        else
            last_pattern->next = current_pattern;
        last_pattern = current_pattern;

        count++;
    }

    fclose(f);

    if (!count)
        return;

    loaded_items = (mpm_cluster_item *)malloc(count * sizeof(mpm_cluster_item));
    if (!loaded_items) {
        printf("WARNING: out of memory\n");
        return;
    }

    loaded_items_size = count;

    /* Copy the data. */
    loaded_item_ptr = loaded_items;
    while (first_pattern) {
        loaded_item_ptr->re = first_pattern->re;
        loaded_item_ptr->data = first_pattern->pattern;
        loaded_item_ptr++;

        last_pattern = first_pattern->next;
        free(first_pattern);
        first_pattern = last_pattern;
    }

    line = count + skipped_count + unsupported_count;
    printf("%d patterns are processed\n  %d (%d%%) successfully loaded\n"
           "  %d (%d%%) ignored because of low rating\n  %d (%d%%) ignored because they are unsupported\n\n",
           line, count, count * 100 / line, skipped_count, skipped_count * 100 / line,
           unsupported_count, unsupported_count * 100 / line);
}