-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregexp.c
81 lines (75 loc) · 2.91 KB
/
regexp.c
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
/*
* Copyright (c) 2010 Russell Harmon <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <string.h>
#include <assert.h>
#include <pcre.h>
#include <sqlite3ext.h>
SQLITE_EXTENSION_INIT1
static void sqlite3_regexp( sqlite3_context *context, int argc, sqlite3_value **argv ) {
assert(argc == 2);
char *pcre_err = NULL;
int pcre_erroffset, pcre_errcode;
pcre *pattern = pcre_compile2((const char *) sqlite3_value_text(argv[0]), PCRE_UTF8, &pcre_errcode, (const char **) &pcre_err, &pcre_erroffset, NULL);
if( pattern == NULL ) {
// pcre error code 21 is out of memory as per pcreapi(3)
if( pcre_errcode == 21 ) {
sqlite3_result_error_nomem(context);
} else {
sqlite3_result_error(context, pcre_err, strlen(pcre_err));
}
} else {
const unsigned char *subject = sqlite3_value_text(argv[1]);
pcre_errcode = pcre_exec(pattern, NULL, (const char *) subject, strlen((const char *) subject), 0, 0, NULL, 0);
if( pcre_errcode < 0 ) {
char *errmsg;
switch( pcre_errcode ) {
case PCRE_ERROR_NOMATCH:
sqlite3_result_int(context, 0);
break;
case PCRE_ERROR_NOMEMORY:
sqlite3_result_error_nomem(context);
break;
default:
errmsg = sqlite3_mprintf("pcre_exec: Error code %d\n", pcre_errcode);
if( errmsg == NULL ) {
sqlite3_result_error_nomem(context);
} else {
sqlite3_result_error(context, errmsg, strlen(errmsg));
}
sqlite3_free(errmsg);
}
} else {
sqlite3_result_int(context, 1);
}
pcre_free(pattern);
}
}
int sqlite3_extension_init( sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi ) {
SQLITE_EXTENSION_INIT2(pApi)
int code;
if( pcre_config(PCRE_CONFIG_UTF8, &code) != 0 ) assert(0);
if( code != 1 ) {
if( pzErrMsg ) *pzErrMsg = sqlite3_mprintf("PCRE does not support UTF-8");
return SQLITE_ERROR;
}
return sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, 0, sqlite3_regexp, 0, 0);
}