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

Contents of /lbbs/src/bwf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Fri Nov 7 06:32:50 2025 UTC (4 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.2: +36 -26 lines
Content type: text/x-csrc
Find and mask all matched substrings until the end of input str

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 if (line[len_line - 1] == '\n')
60 {
61 line[len_line - 1] = '\0';
62 }
63
64 if (p > bwf_pattern_str)
65 {
66 *p++ = '|';
67 }
68
69 if (len_line + 2 > sizeof(bwf_pattern_str) - 1 - (size_t)(p - bwf_pattern_str))
70 {
71 log_error("Data in %s exceed length limit %d\n", filename, sizeof(bwf_pattern_str) - 1);
72 bwf_pattern_str[0] = '\0';
73 return -3;
74 }
75
76 *p++ = '(';
77 p = mempcpy(p, line, strnlen(line, sizeof(bwf_pattern_str) - 1 - (size_t)(p - bwf_pattern_str)));
78 *p++ = ')';
79 line_id++;
80 }
81
82 *p = '\0';
83
84 fclose(fp);
85
86 #ifdef _DEBUG
87 log_error("Debug: bwf_pattern_str: %s\n", bwf_pattern_str);
88 #endif
89
90 bwf_unload();
91
92 bwf_code = pcre2_compile((PCRE2_SPTR)bwf_pattern_str, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &errorcode, &erroroffset, NULL);
93 if (bwf_code == NULL)
94 {
95 log_error("pcre2_compile() error: %d", errorcode);
96 return -4;
97 }
98
99 return 0;
100 }
101
102 void bwf_unload(void)
103 {
104 if (bwf_code != NULL)
105 {
106 pcre2_code_free(bwf_code);
107 bwf_code = NULL;
108 }
109 }
110
111 int check_badwords(char *str, char c_mask)
112 {
113 pcre2_match_data *match_data;
114 PCRE2_SIZE startoffset = 0;
115 PCRE2_SIZE *ovector;
116 uint32_t match_count;
117 int ret;
118 int i;
119 int total_match_count = 0;
120
121 if (bwf_code == NULL)
122 {
123 log_error("BWF not loaded\n");
124 return -1;
125 }
126
127 match_data = pcre2_match_data_create_from_pattern(bwf_code, NULL);
128
129 while (1)
130 {
131 ret = pcre2_match(bwf_code, (PCRE2_SPTR)str, PCRE2_ZERO_TERMINATED, startoffset, 0, match_data, NULL);
132 if (ret == PCRE2_ERROR_NOMATCH)
133 {
134 ret = total_match_count;
135 break;
136 }
137 else if (ret < 0)
138 {
139 log_error("pcre2_match() error: %d\n", ret);
140 }
141 else if (ret == 0)
142 {
143 log_error("Vector of offsets is too small\n");
144 }
145 else // ret >= 1
146 {
147 ovector = pcre2_get_ovector_pointer(match_data);
148 match_count = pcre2_get_ovector_count(match_data);
149
150 i = ret - 1;
151 if (ovector[i * 2] == -1 || ovector[i * 2 + 1] == -1)
152 {
153 log_error("Bug: match pattern #%d of %d with invalid offsets [%d, %d)",
154 i, match_count, ovector[i * 2], ovector[i * 2 + 1]);
155 ret = -2;
156 }
157 else
158 {
159 #ifdef _DEBUG
160 log_error("Debug: match pattern #%d of %d at offsets [%d, %d]\n",
161 i, match_count, ovector[i * 2], ovector[i * 2 + 1] - ovector[i * 2]);
162 #endif
163 memset(str + ovector[i * 2], c_mask, ovector[i * 2 + 1] - ovector[i * 2]);
164 total_match_count++;
165 startoffset = ovector[i * 2 + 1];
166 }
167 }
168 }
169
170 pcre2_match_data_free(match_data);
171
172 return ret;
173 }

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