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

Contents of /lbbs/src/bwf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Fri Nov 7 09:18:58 2025 UTC (4 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.3: +7 -1 lines
Content type: text/x-csrc
Trim \r and \n
Skip empty line

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

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