/[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.2 by sysadm, Fri Nov 7 04:58:09 2025 UTC Revision 1.5 by sysadm, Tue Nov 11 00:28:05 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 56  int bwf_load(const char *filename) Line 60  int bwf_load(const char *filename)
60                          return -3;                          return -3;
61                  }                  }
62    
63                  if (line[len_line - 1] == '\n')                  while (len_line > 0 && (line[len_line - 1] == '\n' || line[len_line - 1] == '\r'))
64                  {                  {
65                          line[len_line - 1] = '\0';                          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)                  if (p > bwf_pattern_str)
# Line 88  int bwf_load(const char *filename) Line 98  int bwf_load(const char *filename)
98  #endif  #endif
99    
100          bwf_unload();          bwf_unload();
101            
102          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);
103          if (bwf_code == NULL)          if (bwf_code == NULL)
104          {          {
# Line 111  void bwf_unload(void) Line 121  void bwf_unload(void)
121  int check_badwords(char *str, char c_mask)  int check_badwords(char *str, char c_mask)
122  {  {
123          pcre2_match_data *match_data;          pcre2_match_data *match_data;
124            PCRE2_SIZE startoffset = 0;
125          PCRE2_SIZE *ovector;          PCRE2_SIZE *ovector;
126            uint32_t match_count;
127          int ret;          int ret;
128          int i;          int i;
129            int total_match_count = 0;
130    
131          if (bwf_code == NULL)          if (bwf_code == NULL)
132          {          {
# Line 123  int check_badwords(char *str, char c_mas Line 136  int check_badwords(char *str, char c_mas
136    
137          match_data = pcre2_match_data_create_from_pattern(bwf_code, NULL);          match_data = pcre2_match_data_create_from_pattern(bwf_code, NULL);
138    
139          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  
140          {          {
141                  ovector = pcre2_get_ovector_pointer(match_data);                  ret = pcre2_match(bwf_code, (PCRE2_SPTR)str, PCRE2_ZERO_TERMINATED, startoffset, 0, match_data, NULL);
142                  i = ret - 1;                  if (ret == PCRE2_ERROR_NOMATCH)
   
                 if (ovector[i * 2] == -1 || ovector[i * 2 + 1] == -1)  
143                  {                  {
144                          log_error("Bug: match pattern #%d with invalid offsets [%d, %d)",                          ret = total_match_count;
145                                            i, ovector[i * 2], ovector[i * 2 + 1]);                          break;
                         ret = -2;  
146                  }                  }
147                  else                  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  #ifdef _DEBUG
170                          log_error("Debug: match pattern #%d at offsets [%d, %d]\n",                                  log_error("Debug: match pattern #%d of %d at offsets [%d, %d]\n",
171                                            i, ovector[i * 2], ovector[i * 2 + 1] - ovector[i * 2]);                                                    i, match_count, ovector[i * 2], ovector[i * 2 + 1] - ovector[i * 2]);
172  #endif  #endif
173                          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]);
174                                    total_match_count++;
175                                    startoffset = ovector[i * 2 + 1];
176                            }
177                  }                  }
178          }          }
179    


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

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