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

Diff of /lbbs/src/bwf.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.1 by sysadm, Fri Nov 7 04:51:06 2025 UTC Revision 1.6 by sysadm, Fri Nov 21 10:34:10 2025 UTC
# Line 6  Line 6 
6   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>   * 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  #define PCRE2_CODE_UNIT_WIDTH 8
14    
15  #include "bwf.h"  #include "bwf.h"
# Line 31  int bwf_load(const char *filename) Line 35  int bwf_load(const char *filename)
35          size_t len_line;          size_t len_line;
36          char *p = bwf_pattern_str;          char *p = bwf_pattern_str;
37          int line_id = 0;          int line_id = 0;
         int errorcode;  
         PCRE2_SIZE erroroffset;  
38    
39          if (filename == NULL)          if (filename == NULL)
40          {          {
# Line 56  int bwf_load(const char *filename) Line 58  int bwf_load(const char *filename)
58                          return -3;                          return -3;
59                  }                  }
60    
61                  if (line[len_line - 1] == '\n')                  while (len_line > 0 && (line[len_line - 1] == '\n' || line[len_line - 1] == '\r'))
62                  {                  {
63                          line[len_line - 1] = '\0';                          line[len_line - 1] = '\0';
64                            len_line--;
65                    }
66    
67                    if (len_line == 0)
68                    {
69                            continue;
70                  }                  }
71    
72                  if (p > bwf_pattern_str)                  if (p > bwf_pattern_str)
# Line 87  int bwf_load(const char *filename) Line 95  int bwf_load(const char *filename)
95          log_error("Debug: bwf_pattern_str: %s\n", bwf_pattern_str);          log_error("Debug: bwf_pattern_str: %s\n", bwf_pattern_str);
96  #endif  #endif
97    
98            return 0;
99    }
100    
101    int bwf_compile(void)
102    {
103            int errorcode;
104            PCRE2_SIZE erroroffset;
105    
106            bwf_cleanup();
107    
108          bwf_code = pcre2_compile((PCRE2_SPTR)bwf_pattern_str, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &errorcode, &erroroffset, NULL);          bwf_code = pcre2_compile((PCRE2_SPTR)bwf_pattern_str, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &errorcode, &erroroffset, NULL);
109          if (bwf_code == NULL)          if (bwf_code == NULL)
110          {          {
111                  log_error("pcre2_compile() error: %d", errorcode);                  log_error("pcre2_compile() error: %d", errorcode);
112                  return -4;                  return -1;
113          }          }
114    
115          return 0;          return 0;
116  }  }
117    
118  void bwf_unload(void)  void bwf_cleanup(void)
119  {  {
120          if (bwf_code != NULL)          if (bwf_code != NULL)
121          {          {
# Line 109  void bwf_unload(void) Line 127  void bwf_unload(void)
127  int check_badwords(char *str, char c_mask)  int check_badwords(char *str, char c_mask)
128  {  {
129          pcre2_match_data *match_data;          pcre2_match_data *match_data;
130            PCRE2_SIZE startoffset = 0;
131          PCRE2_SIZE *ovector;          PCRE2_SIZE *ovector;
132            uint32_t match_count;
133          int ret;          int ret;
134          int i;          int i;
135            int total_match_count = 0;
136    
137          if (bwf_code == NULL)          if (bwf_code == NULL)
138          {          {
# Line 121  int check_badwords(char *str, char c_mas Line 142  int check_badwords(char *str, char c_mas
142    
143          match_data = pcre2_match_data_create_from_pattern(bwf_code, NULL);          match_data = pcre2_match_data_create_from_pattern(bwf_code, NULL);
144    
145          ret = pcre2_match(bwf_code, (PCRE2_SPTR)str, PCRE2_ZERO_TERMINATED, 0, 0, match_data, NULL);          while (1)
         if (ret == PCRE2_ERROR_NOMATCH)  
         {  
                 ret = 0;  
         }  
         else if (ret < 0)  
         {  
                 log_error("pcre2_match() error: %d\n", ret);  
         }  
         else if (ret == 0)  
         {  
                 log_error("Vector of offsets is too small\n");  
         }  
         else // ret >= 1  
146          {          {
147                  ovector = pcre2_get_ovector_pointer(match_data);                  ret = pcre2_match(bwf_code, (PCRE2_SPTR)str, PCRE2_ZERO_TERMINATED, startoffset, 0, match_data, NULL);
148                  i = ret - 1;                  if (ret == PCRE2_ERROR_NOMATCH)
149                    {
150                  if (ovector[i * 2] == -1 || ovector[i * 2 + 1] == -1)                          ret = total_match_count;
151                            break;
152                    }
153                    else if (ret < 0)
154                  {                  {
155                          log_error("Bug: match pattern #%d with invalid offsets [%d, %d)",                          log_error("pcre2_match() error: %d\n", ret);
                                           i, ovector[i * 2], ovector[i * 2 + 1]);  
                         ret = -2;  
156                  }                  }
157                  else                  else if (ret == 0)
158                  {                  {
159                            log_error("Vector of offsets is too small\n");
160                    }
161                    else // ret >= 1
162                    {
163                            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  #ifdef _DEBUG  #ifdef _DEBUG
176                          log_error("Debug: match pattern #%d at offsets [%d, %d]\n",                                  log_error("Debug: match pattern #%d of %d at offsets [%d, %d]\n",
177                                            i, ovector[i * 2], ovector[i * 2 + 1] - ovector[i * 2]);                                                    i, match_count, ovector[i * 2], ovector[i * 2 + 1] - ovector[i * 2]);
178  #endif  #endif
179                          memset(str + ovector[i * 2], c_mask, ovector[i * 2 + 1] - ovector[i * 2]);                                  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                  }                  }
184          }          }
185    


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

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