/[LeafOK_CVS]/lbbs/src/bwf.c
ViewVC logotype

Contents of /lbbs/src/bwf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (show annotations)
Tue Nov 11 00:28:05 2025 UTC (4 months ago) by sysadm
Branch: MAIN
Changes since 1.4: +4 -0 lines
Content type: text/x-csrc
Use config.h

1 /* SPDX-License-Identifier: GPL-3.0-or-later */
2 /*
3 * bwf
4 * - bad words filter
5 *
6 * Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com>
7 */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #define PCRE2_CODE_UNIT_WIDTH 8
14
15 #include "bwf.h"
16 #include "log.h"
17 #include <errno.h>
18 #include <pcre2.h>
19 #include <stdio.h>
20 #include <string.h>
21
22 enum _bwf_constant_t
23 {
24 BWF_max_pattern_len = 64,
25 BWF_max_pattern_cnt = 1000,
26 };
27
28 static char bwf_pattern_str[(BWF_max_pattern_len + 1) * BWF_max_pattern_cnt];
29 static pcre2_code *bwf_code = NULL;
30
31 int bwf_load(const char *filename)
32 {
33 FILE *fp;
34 char line[BWF_max_pattern_len + 1];
35 size_t len_line;
36 char *p = bwf_pattern_str;
37 int line_id = 0;
38 int errorcode;
39 PCRE2_SIZE erroroffset;
40
41 if (filename == NULL)
42 {
43 log_error("NULL pointer error\n");
44 return -1;
45 }
46
47 if ((fp = fopen(filename, "r")) == NULL)
48 {
49 log_error("fopen(%s) error: %d\n", filename, errno);
50 return -2;
51 }
52
53 while (fgets(line, sizeof(line), fp) != NULL)
54 {
55 len_line = strnlen(line, sizeof(line) - 1);
56 if (!feof(fp) && line[len_line - 1] != '\n')
57 {
58 log_error("Data line %d (len=%d) is truncated\n", line_id, len_line);
59 bwf_pattern_str[0] = '\0';
60 return -3;
61 }
62
63 while (len_line > 0 && (line[len_line - 1] == '\n' || line[len_line - 1] == '\r'))
64 {
65 line[len_line - 1] = '\0';
66 len_line--;
67 }
68
69 if (len_line == 0)
70 {
71 continue;
72 }
73
74 if (p > bwf_pattern_str)
75 {
76 *p++ = '|';
77 }
78
79 if (len_line + 2 > sizeof(bwf_pattern_str) - 1 - (size_t)(p - bwf_pattern_str))
80 {
81 log_error("Data in %s exceed length limit %d\n", filename, sizeof(bwf_pattern_str) - 1);
82 bwf_pattern_str[0] = '\0';
83 return -3;
84 }
85
86 *p++ = '(';
87 p = mempcpy(p, line, strnlen(line, sizeof(bwf_pattern_str) - 1 - (size_t)(p - bwf_pattern_str)));
88 *p++ = ')';
89 line_id++;
90 }
91
92 *p = '\0';
93
94 fclose(fp);
95
96 #ifdef _DEBUG
97 log_error("Debug: bwf_pattern_str: %s\n", bwf_pattern_str);
98 #endif
99
100 bwf_unload();
101
102 bwf_code = pcre2_compile((PCRE2_SPTR)bwf_pattern_str, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &errorcode, &erroroffset, NULL);
103 if (bwf_code == NULL)
104 {
105 log_error("pcre2_compile() error: %d", errorcode);
106 return -4;
107 }
108
109 return 0;
110 }
111
112 void bwf_unload(void)
113 {
114 if (bwf_code != NULL)
115 {
116 pcre2_code_free(bwf_code);
117 bwf_code = NULL;
118 }
119 }
120
121 int check_badwords(char *str, char c_mask)
122 {
123 pcre2_match_data *match_data;
124 PCRE2_SIZE startoffset = 0;
125 PCRE2_SIZE *ovector;
126 uint32_t match_count;
127 int ret;
128 int i;
129 int total_match_count = 0;
130
131 if (bwf_code == NULL)
132 {
133 log_error("BWF not loaded\n");
134 return -1;
135 }
136
137 match_data = pcre2_match_data_create_from_pattern(bwf_code, NULL);
138
139 while (1)
140 {
141 ret = pcre2_match(bwf_code, (PCRE2_SPTR)str, PCRE2_ZERO_TERMINATED, startoffset, 0, match_data, NULL);
142 if (ret == PCRE2_ERROR_NOMATCH)
143 {
144 ret = total_match_count;
145 break;
146 }
147 else if (ret < 0)
148 {
149 log_error("pcre2_match() error: %d\n", ret);
150 }
151 else if (ret == 0)
152 {
153 log_error("Vector of offsets is too small\n");
154 }
155 else // ret >= 1
156 {
157 ovector = pcre2_get_ovector_pointer(match_data);
158 match_count = pcre2_get_ovector_count(match_data);
159
160 i = ret - 1;
161 if (ovector[i * 2] == -1 || ovector[i * 2 + 1] == -1)
162 {
163 log_error("Bug: match pattern #%d of %d with invalid offsets [%d, %d)",
164 i, match_count, ovector[i * 2], ovector[i * 2 + 1]);
165 ret = -2;
166 }
167 else
168 {
169 #ifdef _DEBUG
170 log_error("Debug: match pattern #%d of %d at offsets [%d, %d]\n",
171 i, match_count, ovector[i * 2], ovector[i * 2 + 1] - ovector[i * 2]);
172 #endif
173 memset(str + ovector[i * 2], c_mask, ovector[i * 2 + 1] - ovector[i * 2]);
174 total_match_count++;
175 startoffset = ovector[i * 2 + 1];
176 }
177 }
178 }
179
180 pcre2_match_data_free(match_data);
181
182 return ret;
183 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1