1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "regexp.h"
#if defined(GIT_REGEX_BUILTIN) || defined(GIT_REGEX_PCRE)
int git_regexp_compile(git_regexp *r, const char *pattern, int flags)
{
int erroffset, cflags = 0;
const char *error = NULL;
if (flags & GIT_REGEXP_ICASE)
cflags |= PCRE_CASELESS;
if ((*r = pcre_compile(pattern, cflags, &error, &erroffset, NULL)) == NULL) {
git_error_set_str(GIT_ERROR_REGEX, error);
return GIT_EINVALIDSPEC;
}
return 0;
}
void git_regexp_dispose(git_regexp *r)
{
pcre_free(*r);
*r = NULL;
}
int git_regexp_match(const git_regexp *r, const char *string)
{
int error;
if ((error = pcre_exec(*r, NULL, string, (int) strlen(string), 0, 0, NULL, 0)) < 0)
return (error == PCRE_ERROR_NOMATCH) ? GIT_ENOTFOUND : GIT_EINVALIDSPEC;
return 0;
}
int git_regexp_search(const git_regexp *r, const char *string, size_t nmatches, git_regmatch *matches)
{
int static_ovec[9] = {0}, *ovec;
int error;
size_t i;
/* The ovec array always needs to be a multiple of three */
if (nmatches <= ARRAY_SIZE(static_ovec) / 3)
ovec = static_ovec;
else
ovec = git__calloc(nmatches * 3, sizeof(*ovec));
GIT_ERROR_CHECK_ALLOC(ovec);
if ((error = pcre_exec(*r, NULL, string, (int) strlen(string), 0, 0, ovec, (int) nmatches * 3)) < 0)
goto out;
if (error == 0)
error = (int) nmatches;
for (i = 0; i < (unsigned int) error; i++) {
matches[i].start = (ovec[i * 2] < 0) ? -1 : ovec[i * 2];
matches[i].end = (ovec[i * 2 + 1] < 0) ? -1 : ovec[i * 2 + 1];
}
for (i = (unsigned int) error; i < nmatches; i++)
matches[i].start = matches[i].end = -1;
out:
if (nmatches > ARRAY_SIZE(static_ovec) / 3)
git__free(ovec);
if (error < 0)
return (error == PCRE_ERROR_NOMATCH) ? GIT_ENOTFOUND : GIT_EINVALIDSPEC;
return 0;
}
#elif defined(GIT_REGEX_PCRE2)
int git_regexp_compile(git_regexp *r, const char *pattern, int flags)
{
unsigned char errmsg[1024];
PCRE2_SIZE erroff;
int error, cflags = 0;
if (flags & GIT_REGEXP_ICASE)
cflags |= PCRE2_CASELESS;
if ((*r = pcre2_compile((const unsigned char *) pattern, PCRE2_ZERO_TERMINATED,
cflags, &error, &erroff, NULL)) == NULL) {
pcre2_get_error_message(error, errmsg, sizeof(errmsg));
git_error_set_str(GIT_ERROR_REGEX, (char *) errmsg);
return GIT_EINVALIDSPEC;
}
return 0;
}
void git_regexp_dispose(git_regexp *r)
{
pcre2_code_free(*r);
*r = NULL;
}
int git_regexp_match(const git_regexp *r, const char *string)
{
pcre2_match_data *data;
int error;
data = pcre2_match_data_create(1, NULL);
GIT_ERROR_CHECK_ALLOC(data);
if ((error = pcre2_match(*r, (const unsigned char *) string, strlen(string),
0, 0, data, NULL)) < 0)
return (error == PCRE2_ERROR_NOMATCH) ? GIT_ENOTFOUND : GIT_EINVALIDSPEC;
pcre2_match_data_free(data);
return 0;
}
int git_regexp_search(const git_regexp *r, const char *string, size_t nmatches, git_regmatch *matches)
{
pcre2_match_data *data = NULL;
PCRE2_SIZE *ovec;
int error;
size_t i;
if ((data = pcre2_match_data_create(nmatches, NULL)) == NULL) {
git_error_set_oom();
goto out;
}
if ((error = pcre2_match(*r, (const unsigned char *) string, strlen(string),
0, 0, data, NULL)) < 0)
goto out;
if (error == 0 || (unsigned int) error > nmatches)
error = nmatches;
ovec = pcre2_get_ovector_pointer(data);
for (i = 0; i < (unsigned int) error; i++) {
matches[i].start = (ovec[i * 2] == PCRE2_UNSET) ? -1 : (ssize_t) ovec[i * 2];
matches[i].end = (ovec[i * 2 + 1] == PCRE2_UNSET) ? -1 : (ssize_t) ovec[i * 2 + 1];
}
for (i = (unsigned int) error; i < nmatches; i++)
matches[i].start = matches[i].end = -1;
out:
pcre2_match_data_free(data);
if (error < 0)
return (error == PCRE2_ERROR_NOMATCH) ? GIT_ENOTFOUND : GIT_EINVALIDSPEC;
return 0;
}
#elif defined(GIT_REGEX_REGCOMP) || defined(GIT_REGEX_REGCOMP_L)
#if defined(GIT_REGEX_REGCOMP_L)
# include <xlocale.h>
#endif
int git_regexp_compile(git_regexp *r, const char *pattern, int flags)
{
int cflags = REG_EXTENDED, error;
char errmsg[1024];
if (flags & GIT_REGEXP_ICASE)
cflags |= REG_ICASE;
# if defined(GIT_REGEX_REGCOMP)
if ((error = regcomp(r, pattern, cflags)) != 0)
# else
if ((error = regcomp_l(r, pattern, cflags, (locale_t) 0)) != 0)
# endif
{
regerror(error, r, errmsg, sizeof(errmsg));
git_error_set_str(GIT_ERROR_REGEX, errmsg);
return GIT_EINVALIDSPEC;
}
return 0;
}
void git_regexp_dispose(git_regexp *r)
{
regfree(r);
}
int git_regexp_match(const git_regexp *r, const char *string)
{
int error;
if ((error = regexec(r, string, 0, NULL, 0)) != 0)
return (error == REG_NOMATCH) ? GIT_ENOTFOUND : GIT_EINVALIDSPEC;
return 0;
}
int git_regexp_search(const git_regexp *r, const char *string, size_t nmatches, git_regmatch *matches)
{
regmatch_t static_m[3], *m;
int error;
size_t i;
if (nmatches <= ARRAY_SIZE(static_m))
m = static_m;
else
m = git__calloc(nmatches, sizeof(*m));
if ((error = regexec(r, string, nmatches, m, 0)) != 0)
goto out;
for (i = 0; i < nmatches; i++) {
matches[i].start = (m[i].rm_so < 0) ? -1 : m[i].rm_so;
matches[i].end = (m[i].rm_eo < 0) ? -1 : m[i].rm_eo;
}
out:
if (nmatches > ARRAY_SIZE(static_m))
git__free(m);
if (error)
return (error == REG_NOMATCH) ? GIT_ENOTFOUND : GIT_EINVALIDSPEC;
return 0;
}
#endif