new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / os_regex / os_pcre2_execute.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All right reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 #include "defs.h"
11 #include "os_regex.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 const char *OSPcre2_Execute(const char *str, OSPcre2 *reg)
17 {
18     /* Check for references not initialized */
19     if (str == NULL) {
20         reg->error = OS_REGEX_STR_NULL;
21         return (NULL);
22     }
23
24     return reg->exec_function(str, reg);
25 }
26
27 const char *OSPcre2_Execute_pcre2_match(const char *str, OSPcre2 *reg)
28 {
29     int rc = 0, nbs = 0, i = 0;
30     PCRE2_SIZE *ov = NULL;
31
32     /* Execute the reg */
33 #ifdef USE_PCRE2_JIT
34     rc = pcre2_jit_match(reg->regex, (PCRE2_SPTR)str, strlen(str), 0, 0, reg->match_data, NULL);
35 #else
36     rc = pcre2_match(reg->regex, (PCRE2_SPTR)str, strlen(str), 0, 0, reg->match_data, NULL);
37 #endif
38
39     /* Check execution result */
40     if (rc <= 0) {
41         return NULL;
42     }
43
44     /* get the offsets informations for the match */
45     ov = pcre2_get_ovector_pointer(reg->match_data);
46
47     /* get the substrings if required */
48     for (i = 1; i < rc; i++) {
49         PCRE2_SIZE sub_string_start = ov[2 * i];
50         PCRE2_SIZE sub_string_end = ov[2 * i + 1];
51         PCRE2_SIZE sub_string_len = sub_string_end - sub_string_start;
52         if (sub_string_end > sub_string_start) {
53             reg->sub_strings[nbs] = (char *)calloc(sub_string_len + 1, sizeof(char));
54             strncpy(reg->sub_strings[nbs], &str[sub_string_start], sub_string_len);
55             nbs++;
56         }
57     }
58     reg->sub_strings[nbs] = NULL;
59
60     return &str[ov[1]];
61 }
62
63 const char *OSPcre2_Execute_strcmp(const char *subject, OSPcre2 *reg)
64 {
65     if (!strcmp(reg->pattern, subject)) {
66         return &subject[reg->pattern_len];
67     }
68     return NULL;
69 }
70
71 const char *OSPcre2_Execute_strncmp(const char *subject, OSPcre2 *reg)
72 {
73     if (!strncmp(reg->pattern, subject, reg->pattern_len)) {
74         return &subject[reg->pattern_len];
75     }
76     return NULL;
77 }
78
79 const char *OSPcre2_Execute_strrcmp(const char *subject, OSPcre2 *reg)
80 {
81     size_t len = strlen(subject);
82     if (len >= reg->pattern_len && !strcmp(reg->pattern, &subject[len - reg->pattern_len])) {
83         return &subject[len];
84     }
85     return NULL;
86 }
87
88 const char *OSPcre2_Execute_strcasecmp(const char *subject, OSPcre2 *reg)
89 {
90     if (!strcasecmp(reg->pattern, subject)) {
91         return &subject[reg->pattern_len];
92     }
93     return NULL;
94 }
95
96 const char *OSPcre2_Execute_strncasecmp(const char *subject, OSPcre2 *reg)
97 {
98     if (!strncasecmp(reg->pattern, subject, reg->pattern_len)) {
99         return &subject[reg->pattern_len];
100     }
101     return NULL;
102 }
103
104 const char *OSPcre2_Execute_strrcasecmp(const char *subject, OSPcre2 *reg)
105 {
106     size_t len = strlen(subject);
107     if (len >= reg->pattern_len &&
108         !strcasecmp(reg->pattern, &subject[len - reg->pattern_len])) {
109         return &subject[len];
110     }
111     return NULL;
112 }