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

Diff of /lbbs/src/lml.c

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

Revision 1.6 by sysadm, Tue Jun 3 03:41:19 2025 UTC Revision 1.35 by sysadm, Thu Oct 30 03:18:56 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #include "common.h"
18  #include "lml.h"  #include "lml.h"
19  #include "log.h"  #include "log.h"
 #include "common.h"  
20  #include <stdio.h>  #include <stdio.h>
21  #include <string.h>  #include <string.h>
22  #include <sys/param.h>  #include <sys/param.h>
# Line 24  Line 24 
24  #define LML_TAG_PARAM_BUF_LEN 256  #define LML_TAG_PARAM_BUF_LEN 256
25  #define LML_TAG_OUTPUT_BUF_LEN 1024  #define LML_TAG_OUTPUT_BUF_LEN 1024
26    
27  typedef int (*lml_tag_filter_cb)(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len);  typedef int (*lml_tag_filter_cb)(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len, int quote_mode);
28    
29  static int lml_tag_color_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len)  static int lml_tag_color_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len, int quote_mode)
30  {  {
31          if (strcasecmp(tag_name, "color") == 0)          if (strcasecmp(tag_name, "color") == 0)
32          {          {
# Line 63  static int lml_tag_color_filter(const ch Line 63  static int lml_tag_color_filter(const ch
63  }  }
64    
65  #define LML_TAG_QUOTE_MAX_LEVEL 10  #define LML_TAG_QUOTE_MAX_LEVEL 10
 #define LML_TAG_QUOTE_LEVEL_LOOP 3  
66    
67  static const char *lml_tag_quote_color[] = {  static const char *lml_tag_quote_color[] = {
68          "\033[33m", // yellow          "\033[33m", // yellow
# Line 71  static const char *lml_tag_quote_color[] Line 70  static const char *lml_tag_quote_color[]
70          "\033[35m", // magenta          "\033[35m", // magenta
71  };  };
72    
73    static const int LML_TAG_QUOTE_LEVEL_LOOP = (int)(sizeof(lml_tag_quote_color) / sizeof(const char *));
74    
75  static int lml_tag_quote_level = 0;  static int lml_tag_quote_level = 0;
76    
77  static int lml_tag_quote_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len)  static int lml_tag_quote_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len, int quote_mode)
78  {  {
   
79          if (strcasecmp(tag_name, "quote") == 0)          if (strcasecmp(tag_name, "quote") == 0)
80          {          {
81                  if (lml_tag_quote_level <= LML_TAG_QUOTE_MAX_LEVEL)                  if (lml_tag_quote_level <= LML_TAG_QUOTE_MAX_LEVEL)
82                  {                  {
83                          lml_tag_quote_level++;                          lml_tag_quote_level++;
84                  }                  }
85                  return snprintf(tag_output_buf, tag_output_buf_len, lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP]);                  return snprintf(tag_output_buf, tag_output_buf_len, "%s",
86                                                    lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP]);
87          }          }
88          else if (strcasecmp(tag_name, "/quote") == 0)          else if (strcasecmp(tag_name, "/quote") == 0)
89          {          {
# Line 90  static int lml_tag_quote_filter(const ch Line 91  static int lml_tag_quote_filter(const ch
91                  {                  {
92                          lml_tag_quote_level--;                          lml_tag_quote_level--;
93                  }                  }
94                  return snprintf(tag_output_buf, tag_output_buf_len,                  return snprintf(tag_output_buf, tag_output_buf_len, "%s",
95                                                  (lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m"));                                                  (lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m"));
96          }          }
97    
98          return 0;          return 0;
99  }  }
100    
101  const static char *LML_tag_def[][3] = {  static int lml_tag_disabled = 0;
102          {"left", "[", ""},  
103          {"right", "]", NULL},  static int lml_tag_disable_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len, int quote_mode)
104          {"bold", "\033[1m", ""}, // does not work in Fterm  {
105          {"/bold", "\033[22m", NULL},          lml_tag_disabled = 1;
106          {"b", "\033[1m", ""},  
107          {"/b", "\033[22m", NULL},          return snprintf(tag_output_buf, tag_output_buf_len, "%s", (quote_mode ? "[plain]" : ""));
108          {"italic", "\033[5m", ""}, // use blink instead  }
109          {"/italic", "\033[m", NULL}, // \033[25m does not work in Fterm  
110          {"i", "\033[5m", ""},  typedef struct lml_tag_def_t
111          {"/i", "\033[m", NULL},  {
112          {"underline", "\033[4m", ""},          const char *tag_name;                    // tag name
113          {"/underline", "\033[m", NULL}, // \033[24m does not work in Fterm          const char *tag_output;                  // output string
114          {"u", "\033[4m", ""},          const char *default_param;               // default param string
115          {"/u", "\033[m", NULL},          const char *quote_mode_output;   // output string in quote mode
116          {"color", NULL, (const char *)lml_tag_color_filter},          lml_tag_filter_cb tag_filter_cb; // tag filter callback
117          {"/color", "\033[m", NULL},  } LML_TAG_DEF;
118          {"quote", NULL, (const char *)lml_tag_quote_filter},  
119          {"/quote", NULL, (const char *)lml_tag_quote_filter},  const LML_TAG_DEF lml_tag_def[] = {
120          {"url", "", ""},          // Definition of tuple: {lml_tag, lml_output, default_param, quote_mode_output, lml_filter_cb}
121          {"/url", "(Á´½Ó: %s)", NULL},          {"plain", NULL, NULL, NULL, lml_tag_disable_filter},
122          {"link", "", ""},          {"nolml", "", NULL, "", NULL}, // deprecated
123          {"/link", "(Á´½Ó: %s)", NULL},          {"lml", "", NULL, "", NULL},   // deprecated
124          {"email", "", ""},          {"align", "", "", "", NULL},   // N/A
125          {"/email", "(Email: %s)", NULL},          {"/align", "", "", "", NULL},  // N/A
126          {"user", "", ""},          {"size", "", "", "", NULL},        // N/A
127          {"/user", "(Óû§: %s)", NULL},          {"/size", "", "", "", NULL},   // N/A
128          {"article", "", ""},          {"left", "[", "", "[left]", NULL},
129          {"/article", "(ÎÄÕÂ: %s)", NULL},          {"right", "]", "", "[right]", NULL},
130          {"image", "(ͼƬ: %s)", ""},          {"bold", "\033[1m", "", "", NULL}, // does not work in Fterm
131          {"flash", "(Flash: %s)", ""},          {"/bold", "\033[22m", NULL, "", NULL},
132          {"bwf", "\033[1;31m****\033[m", ""},          {"b", "\033[1m", "", "", NULL},
133            {"/b", "\033[22m", NULL, "", NULL},
134            {"italic", "\033[5m", "", "", NULL},   // use blink instead
135            {"/italic", "\033[m", NULL, "", NULL}, // \033[25m does not work in Fterm
136            {"i", "\033[5m", "", "", NULL},
137            {"/i", "\033[m", NULL, "", NULL},
138            {"underline", "\033[4m", "", "", NULL},
139            {"/underline", "\033[m", NULL, "", NULL}, // \033[24m does not work in Fterm
140            {"u", "\033[4m", "", "", NULL},
141            {"/u", "\033[m", NULL, "", NULL},
142            {"color", NULL, NULL, "", lml_tag_color_filter},
143            {"/color", "\033[m", NULL, "", NULL},
144            {"quote", NULL, NULL, "", lml_tag_quote_filter},
145            {"/quote", NULL, NULL, "", lml_tag_quote_filter},
146            {"url", "", "", "", NULL},
147            {"/url", "(链接: %s)", NULL, "", NULL},
148            {"link", "", "", "", NULL},
149            {"/link", "(链接: %s)", NULL, "", NULL},
150            {"email", "", "", "", NULL},
151            {"/email", "(Email: %s)", NULL, "", NULL},
152            {"user", "", "", "", NULL},
153            {"/user", "(用户: %s)", NULL, "", NULL},
154            {"article", "", "", "", NULL},
155            {"/article", "(文章: %s)", NULL, "", NULL},
156            {"image", "(图片: %s)", "", "%s", NULL},
157            {"flash", "(Flash: %s)", "", "", NULL},
158            {"bwf", "\033[1;31m****\033[m", "", "****", NULL},
159  };  };
160    
161  #define LML_TAG_COUNT 31  #define LML_TAG_COUNT (sizeof(lml_tag_def) / sizeof(LML_TAG_DEF))
162    
163  static int LML_tag_name_len[LML_TAG_COUNT];  static int lml_tag_name_len[LML_TAG_COUNT];
164  static int LML_init = 0;  static int lml_ready = 0;
165    
166  inline static void lml_init(void)  inline static void lml_init(void)
167  {  {
168          int i;          int i;
169    
170          if (!LML_init)          if (!lml_ready)
171          {          {
172                  for (i = 0; i < LML_TAG_COUNT; i++)                  for (i = 0; i < LML_TAG_COUNT; i++)
173                  {                  {
174                          LML_tag_name_len[i] = (int)strlen(LML_tag_def[i][0]);                          lml_tag_name_len[i] = (int)strlen(lml_tag_def[i].tag_name);
175                  }                  }
176    
177                  LML_init = 1;                  lml_ready = 1;
178          }          }
179  }  }
180    
181  int lml_plain(const char *str_in, char *str_out, int buf_len)  #define CHECK_AND_APPEND_OUTPUT(out_buf, out_buf_len, out_buf_offset, tag_out, tag_out_len)                                         \
182            {                                                                                                                               \
183                    if ((out_buf_offset) + (tag_out_len) >= (out_buf_len))                                                                      \
184                    {                                                                                                                           \
185                            log_error("Buffer is not longer enough for output string %d >= %d\n", (out_buf_offset) + (tag_out_len), (out_buf_len)); \
186                            out_buf[out_buf_offset] = '\0';                                                                                         \
187                            return (out_buf_offset);                                                                                                \
188                    }                                                                                                                           \
189                    memcpy((out_buf) + (out_buf_offset), (tag_out), (size_t)(tag_out_len));                                                     \
190                    (out_buf_offset) += (tag_out_len);                                                                                          \
191            }
192    
193    int lml_render(const char *str_in, char *str_out, int buf_len, int quote_mode)
194  {  {
195            char c;
196          char tag_param_buf[LML_TAG_PARAM_BUF_LEN];          char tag_param_buf[LML_TAG_PARAM_BUF_LEN];
197          char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN];          char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN];
198          int i;          int i;
199          int j = 0;          int j = 0;
200          int k;          int k;
201          int tag_start_pos = -1;          int tag_start_pos = -1;
202            int tag_name_pos = -1;
203          int tag_end_pos = -1;          int tag_end_pos = -1;
204          int tag_param_pos = -1;          int tag_param_pos = -1;
205          int tag_output_len;          int tag_output_len;
206          int new_line = 1;          int new_line = 1;
207          int fb_quote_level = 0;          int fb_quote_level = 0;
208            int tag_name_found;
209    
210          lml_init();          lml_init();
211    
212            lml_tag_disabled = 0;
213          lml_tag_quote_level = 0;          lml_tag_quote_level = 0;
214    
215          for (i = 0; str_in[i] != '\0'; i++)          for (i = 0; str_in[i] != '\0'; i++)
216          {          {
217                  if (new_line)                  if (!quote_mode && !lml_tag_disabled && new_line)
218                  {                  {
219                          if (fb_quote_level > 0)                          fb_quote_level = 0;
                         {  
                                 lml_tag_quote_level -= fb_quote_level;  
   
                                 tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN,  
                                                                                   (lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m"));  
                                 if (j + tag_output_len >= buf_len - 1)  
                                 {  
                                         log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len - 1);  
                                         str_out[j] = '\0';  
                                         return j;  
                                 }  
                                 memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);  
                                 j += tag_output_len;  
   
                                 fb_quote_level = 0;  
                         }  
220    
221                          while (str_in[i + fb_quote_level * 2] == ':' && str_in[i + fb_quote_level * 2 + 1] == ' ') // FB2000 quote leading str                          while (str_in[i + fb_quote_level * 2] == ':' && str_in[i + fb_quote_level * 2 + 1] == ' ') // FB2000 quote leading str
222                          {                          {
223                                  fb_quote_level++;                                  fb_quote_level++;
224                          }                          }
225    
226                          if (fb_quote_level > 0)                          lml_tag_quote_level += fb_quote_level;
                         {  
                                 lml_tag_quote_level += fb_quote_level;  
227    
228                                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN,                          if (lml_tag_quote_level > 0)
229                                                                                    lml_tag_quote_color[(lml_tag_quote_level) % LML_TAG_QUOTE_LEVEL_LOOP]);                          {
230                                  if (j + tag_output_len >= buf_len - 1)                                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s",
231                                  {                                                                                    lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP]);
232                                          log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len - 1);                                  CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len);
                                         str_out[j] = '\0';  
                                         return j;  
                                 }  
                                 memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);  
                                 j += tag_output_len;  
233                          }                          }
234    
235                          new_line = 0;                          new_line = 0;
236                  }                  }
237    
238                  if (str_in[i] == '\n')                  if (str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence -- copy directly
239                  {                  {
240                          tag_start_pos = -1; // jump out of tag at end of line                          for (k = i + 2; str_in[k] != '\0' && str_in[k] != 'm' && str_in[k] != '\033'; k++)
241                                    ;
242    
243                            if (str_in[k] != 'm') // invalid
244                            {
245                                    k--;
246                            }
247    
248                            CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + i, k - i + 1);
249                            i = k;
250                            continue;
251                    }
252    
253                    if (str_in[i] == '\n') // jump out of tag at end of line
254                    {
255                            if (tag_start_pos != -1) // tag is not closed
256                            {
257                                    tag_end_pos = i - 1;
258                                    tag_output_len = tag_end_pos - tag_start_pos + 1;
259                                    CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len);
260                            }
261    
262                            if (fb_quote_level > 0)
263                            {
264                                    lml_tag_quote_level -= fb_quote_level;
265    
266                                    tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m");
267                                    CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len);
268                            }
269    
270                            tag_start_pos = -1;
271                            tag_name_pos = -1;
272                          new_line = 1;                          new_line = 1;
273                  }                  }
274                  else if (str_in[i] == '\r')                  else if (str_in[i] == '\r')
# Line 225  int lml_plain(const char *str_in, char * Line 276  int lml_plain(const char *str_in, char *
276                          continue; // ignore '\r'                          continue; // ignore '\r'
277                  }                  }
278    
279                  if (str_in[i] == '[')                  if (!lml_tag_disabled && str_in[i] == '[')
280                  {                  {
281                          tag_start_pos = i + 1;                          if (tag_start_pos != -1) // tag is not closed
282                            {
283                                    tag_end_pos = i - 1;
284                                    tag_output_len = tag_end_pos - tag_start_pos + 1;
285                                    CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len);
286                            }
287    
288                            tag_start_pos = i;
289                            tag_name_pos = i + 1;
290                  }                  }
291                  else if (str_in[i] == ']')                  else if (!lml_tag_disabled && str_in[i] == ']' && tag_name_pos >= 0)
292                  {                  {
293                          if (tag_start_pos >= 0)                          tag_end_pos = i;
                         {  
                                 tag_end_pos = i;  
294    
295                                  // Skip space characters                          // Skip space characters
296                                  while (str_in[tag_start_pos] == ' ')                          while (str_in[tag_name_pos] == ' ')
297                                  {                          {
298                                          tag_start_pos++;                                  tag_name_pos++;
299                                  }                          }
300    
301                                  for (k = 0; k < LML_TAG_COUNT; k++)                          for (tag_name_found = 0, k = 0; k < LML_TAG_COUNT; k++)
302                            {
303                                    if (strncasecmp(lml_tag_def[k].tag_name, str_in + tag_name_pos, (size_t)lml_tag_name_len[k]) == 0)
304                                  {                                  {
305                                          if (strncasecmp(LML_tag_def[k][0], str_in + tag_start_pos, (size_t)LML_tag_name_len[k]) == 0)                                          tag_param_pos = -1;
306                                            switch (str_in[tag_name_pos + lml_tag_name_len[k]])
307                                          {                                          {
308                                                  tag_param_pos = -1;                                          case ' ':
309                                                  switch (str_in[tag_start_pos + LML_tag_name_len[k]])                                                  tag_name_found = 1;
310                                                    tag_param_pos = tag_name_pos + lml_tag_name_len[k] + 1;
311                                                    while (str_in[tag_param_pos] == ' ')
312                                                  {                                                  {
313                                                  case ' ':                                                          tag_param_pos++;
314                                                          tag_param_pos = tag_start_pos + LML_tag_name_len[k] + 1;                                                  }
315                                                          while (str_in[tag_param_pos] == ' ')                                                  strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN));
316                                                          {                                                  tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0';
317                                                                  tag_param_pos++;                                          case ']':
318                                                          }                                                  tag_name_found = 1;
319                                                          strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN));                                                  if (tag_param_pos == -1 && lml_tag_def[k].tag_output != NULL && lml_tag_def[k].default_param != NULL) // Apply default param if not defined
320                                                          tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0';                                                  {
321                                                  case ']':                                                          strncpy(tag_param_buf, lml_tag_def[k].default_param, LML_TAG_PARAM_BUF_LEN - 1);
322                                                          if (tag_param_pos == -1 && LML_tag_def[k][1] != NULL && LML_tag_def[k][2] != NULL) // Apply default param if not defined                                                          tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';
323                                                    }
324                                                    tag_output_len = 0;
325                                                    if (!quote_mode)
326                                                    {
327                                                            if (lml_tag_def[k].tag_output != NULL)
328                                                          {                                                          {
329                                                                  strncpy(tag_param_buf, LML_tag_def[k][2], LML_TAG_PARAM_BUF_LEN - 1);                                                                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].tag_output, tag_param_buf);
                                                                 tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';  
330                                                          }                                                          }
331                                                          if (LML_tag_def[k][1] != NULL)                                                          else if (lml_tag_def[k].tag_filter_cb != NULL)
332                                                          {                                                          {
333                                                                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, LML_tag_def[k][1], tag_param_buf);                                                                  tag_output_len = lml_tag_def[k].tag_filter_cb(
334                                                                            lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 0);
335                                                          }                                                          }
336                                                          else                                                  }
337                                                    else // if (quote_mode)
338                                                    {
339                                                            if (lml_tag_def[k].quote_mode_output != NULL)
340                                                          {                                                          {
341                                                                  tag_output_len = ((lml_tag_filter_cb)LML_tag_def[k][2])(                                                                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].quote_mode_output, tag_param_buf);
                                                                         LML_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN);  
342                                                          }                                                          }
343                                                          if (j + tag_output_len >= buf_len - 1)                                                          else if (lml_tag_def[k].tag_filter_cb != NULL)
344                                                          {                                                          {
345                                                                  log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len - 1);                                                                  tag_output_len = lml_tag_def[k].tag_filter_cb(
346                                                                  str_out[j] = '\0';                                                                          lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 1);
                                                                 return j;  
347                                                          }                                                          }
                                                         memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);  
                                                         j += tag_output_len;  
                                                         break;  
                                                 default: // tag_name not match  
                                                         continue;  
348                                                  }                                                  }
349                                                    CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len);
350                                                  break;                                                  break;
351                                            default: // tag_name not match
352                                                    continue;
353                                          }                                          }
354                                            break;
355                                  }                                  }
356                            }
357    
358                                  tag_start_pos = -1;                          if (!tag_name_found)
359                            {
360                                    tag_output_len = tag_end_pos - tag_start_pos + 1;
361                                    CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len);
362                          }                          }
363    
364                            tag_start_pos = -1;
365                            tag_name_pos = -1;
366                  }                  }
367                  else if (tag_start_pos == -1) // not in LML tag                  else if (lml_tag_disabled || tag_name_pos == -1) // not in LML tag
368                  {                  {
369                          if (str_in[i] < 0 || str_in[i] > 127) // GBK chinese character                          if (str_in[i] & 0x80) // head of multi-byte character
370                          {                          {
371                                  if (j + 2 >= buf_len - 1)                                  if (j + 4 >= buf_len) // Assuming UTF-8 CJK characters use 4 bytes, though most of them actually use 3 bytes
372                                  {                                  {
373                                          log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 2, buf_len - 1);                                          log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 4, buf_len);
374                                          str_out[j] = '\0';                                          str_out[j] = '\0';
375                                          return j;                                          return j;
376                                  }                                  }
377                                  str_out[j++] = str_in[i++];  
378                                  if (str_in[i] == '\0')                                  c = (str_in[i] & 0x70) << 1;
379                                    while (c & 0x80)
380                                  {                                  {
381                                          str_out[j] = '\0';                                          str_out[j++] = str_in[i++];
382                                          return j;                                          if (str_in[i] == '\0')
383                                            {
384                                                    str_out[j] = '\0';
385                                                    return j;
386                                            }
387                                            c = (c & 0x7f) << 1;
388                                  }                                  }
389                          }                          }
390    
391                          if (j + 1 >= buf_len - 1)                          if (j + 1 >= buf_len)
392                          {                          {
393                                  log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 1, buf_len - 1);                                  log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 1, buf_len);
394                                  str_out[j] = '\0';                                  str_out[j] = '\0';
395                                  return j;                                  return j;
396                          }                          }
# Line 322  int lml_plain(const char *str_in, char * Line 402  int lml_plain(const char *str_in, char *
402                  }                  }
403          }          }
404    
405          if (lml_tag_quote_level > 0)          if (tag_start_pos != -1) // tag is not closed
406            {
407                    tag_end_pos = i - 1;
408                    tag_output_len = tag_end_pos - tag_start_pos + 1;
409                    CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len);
410            }
411    
412            if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0)
413          {          {
414                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m");                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m");
415                  if (j + tag_output_len >= buf_len - 1)                  CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len);
                 {  
                         log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len - 1);  
                         str_out[j] = '\0';  
                         return j;  
                 }  
                 memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);  
                 j += tag_output_len;  
416          }          }
417    
418          str_out[j] = '\0';          str_out[j] = '\0';


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

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