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

Annotation of /lbbs/src/bwf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (hide annotations)
Fri Nov 21 10:34:10 2025 UTC (3 months, 3 weeks ago) by sysadm
Branch: MAIN
Changes since 1.5: +11 -5 lines
Content type: text/x-csrc
Refine BWF

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

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