xref: /lighttpd1.4/src/data_config.c (revision 94edf7e8)
1 #include "first.h"
2 
3 #include "array.h"
4 #include "configfile.h"
5 
6 #include <string.h>
7 #include <stdlib.h>
8 
9 #ifdef HAVE_PCRE_H
10 #include <pcre.h>
11 #ifndef PCRE_STUDY_JIT_COMPILE
12 #define PCRE_STUDY_JIT_COMPILE 0
13 #define pcre_free_study(x) pcre_free(x)
14 #endif
15 #endif
16 
17 __attribute_cold__
18 static data_unset *data_config_copy(const data_unset *s) {
19 	data_config *src = (data_config *)s;
20 	data_config *ds = data_config_init();
21 
22 	ds->comp = src->comp;
23 	if (!buffer_is_unset(&src->key)) {
24 		buffer_copy_buffer(&ds->key, &src->key);
25 		ds->comp_key = ds->key.ptr + (src->comp_key - src->key.ptr);
26 	}
27 	buffer_copy_buffer(&ds->comp_tag, &src->comp_tag);
28 	array_copy_array(ds->value, src->value);
29 	return (data_unset *)ds;
30 }
31 
32 __attribute_cold__
33 static void data_config_free(data_unset *d) {
34 	data_config *ds = (data_config *)d;
35 
36 	free(ds->key.ptr);
37 	free(ds->comp_tag.ptr);
38 
39 	array_free(ds->value);
40 	vector_config_weak_clear(&ds->children);
41 
42 	free(ds->string.ptr);
43 #ifdef HAVE_PCRE_H
44 	if (ds->regex) pcre_free(ds->regex);
45 	if (ds->regex_study) pcre_free_study(ds->regex_study);
46 #endif
47 
48 	free(d);
49 }
50 
51 data_config *data_config_init(void) {
52 	static const struct data_methods fn = {
53 		data_config_copy,
54 		data_config_free,
55 		NULL
56 	};
57 	data_config *ds;
58 
59 	ds = calloc(1, sizeof(*ds));
60 	force_assert(ds);
61 
62 	ds->comp_key = "";
63 	ds->value = array_init(4);
64 	vector_config_weak_init(&ds->children);
65 
66 	ds->type = TYPE_CONFIG;
67 	ds->fn = &fn;
68 
69 	return ds;
70 }
71 
72 #include "log.h"
73 
74 int data_config_pcre_compile(data_config * const dc, const int pcre_jit, log_error_st * const errh) {
75 #ifdef HAVE_PCRE_H
76     const char *errptr;
77     int erroff, captures;
78 
79     dc->regex = pcre_compile(dc->string.ptr, 0, &errptr, &erroff, NULL);
80     if (NULL == dc->regex) {
81         log_error(errh, __FILE__, __LINE__,
82                   "parsing regex failed: %s -> %s at offset %d\n",
83                   dc->string.ptr, errptr, erroff);
84         return 0;
85     }
86 
87     const int study_options = pcre_jit ? PCRE_STUDY_JIT_COMPILE : 0;
88     dc->regex_study = pcre_study(dc->regex, study_options, &errptr);
89     if (NULL == dc->regex_study && errptr != NULL) {
90         log_error(errh, __FILE__, __LINE__,
91                   "studying regex failed: %s -> %s\n",
92                   dc->string.ptr, errptr);
93         return 0;
94     }
95 
96     erroff = pcre_fullinfo(dc->regex, dc->regex_study, PCRE_INFO_CAPTURECOUNT,
97                            &captures);
98     if (0 != erroff) {
99         log_error(errh, __FILE__, __LINE__,
100                   "getting capture count for regex failed: %s\n",
101                   dc->string.ptr);
102         return 0;
103     }
104     else if (captures > 9) {
105         log_error(errh, __FILE__, __LINE__,
106                   "Too many captures in regex, use (?:...) instead of (...): %s\n",
107                   dc->string.ptr);
108         return 0;
109     }
110     return 1;
111 #else
112     UNUSED(pcre_jit);
113     log_error(errh, __FILE__, __LINE__,
114               "can't handle '%s' as you compiled without pcre support. \n"
115               "(perhaps just a missing pcre-devel package ?) \n",
116               dc->comp_key);
117     return 0;
118 #endif
119 }
120