tools.c: bugfixes to trim functions (Phil Slack)
authorArt Cancro <ajc@citadel.org>
Thu, 4 May 2023 22:14:10 +0000 (18:14 -0400)
committerArt Cancro <ajc@citadel.org>
Thu, 4 May 2023 22:14:10 +0000 (18:14 -0400)
libcitadel/lib/tools.c
  stripout()
    Strips too many characters.  Causes incorrect From address on inbound and probably other places.
       stripout(“Foobar<foobar@foobar.com>”) = Fooba.  Should be Foobar
    Mangles multiple boundaries. Should strip using outer boundaries.
      stripout(“ABC()(DEF)()GHI”) = ABC()(DEFGHI.  Should return ABCGHI.
  stripallbut()
    Handle outer boundaries like stripout()
      stripallbut(“ABC()(DEF)()GHI”) returns unchanged.  Should return )(DEF)(
    Code does a stderr flush.  Not sure why, but left it in.
  string_trimlen()
    Removed function.  Not used anywhere in the code and less efficient than string_trim()

libcitadel/lib/libcitadel.h
libcitadel/lib/tools.c

index d9595f5082d7b08651531c7953aa48811412b29f..48e615850a843938fa9fc485f06e17f2d48449e1 100644 (file)
@@ -430,7 +430,6 @@ void CtdlMakeTempFileName(char *name, int len);
 char *rfc2047encode(const char *line, long length);
 int is_msg_in_mset(const char *mset, long msgnum);
 int pattern2(char *search, char *patn);
-void string_trimlen(char *, int *);
 char *html_to_ascii(const char *inputmsg, int msglen, int screenwidth, int ansi);
 void LoadEntityList(char *FileName);
 void utf8ify_rfc822_string(char *buf);
index 7523b17e3647da79ff9d8d2f60f311d67684f39c..2b5066d95fcde99bbffaad31153223dca81b1989 100644 (file)
@@ -431,53 +431,60 @@ const char *cmemreadlinelen(const char *start, char *buf, int maxlen, int *retle
 
 // Strip a boundarized substring out of a string (for example, remove parentheses and anything inside them).
 int stripout(char *str, char leftboundary, char rightboundary) {
-       int a;
-        int lb = (-1);
-        int rb = (-1);
-
-        for (a = 0; a < strlen(str); ++a) {
-                if (str[a] == leftboundary) lb = a;
-                if (str[a] == rightboundary) rb = a;
-        }
-
-        if ( (lb > 0) && (rb > lb) ) {
-                strcpy(&str[lb - 1], &str[rb + 1]);
-               return 1;
-        }
-
-        else if ( (lb == 0) && (rb > lb) ) {
-                strcpy(str, &str[rb + 1]);
-               return 1;
-        }
-       return 0;
-}
+       long lb = (-1);
+       long rb = (-1);
+
+       if (!str) {
+               return 0;
+       }
 
+       for (int a = 0; str[a]; ++a) {
+               if ((lb==-1) && (str[a] == leftboundary)) {
+                       lb = a;
+               } else if (str[a] == rightboundary) {
+                       rb = a;
+               }
+       }
+
+       if ((lb==-1) || (rb <= lb)) {
+               return 0;
+       }
+
+       strcpy(str + lb, str + rb + 1);
+       return 1;
+}
 
 // Reduce a string down to a boundarized substring (for example, remove
 // parentheses and anything outside them).
 long stripallbut(char *str, char leftboundary, char rightboundary) {
-       long len = 0;
-
-       char *lb = NULL;
-       char *rb = NULL;
-
-       lb = strrchr(str, leftboundary);
-       if (lb != NULL) {
-               ++lb;
-               rb = strchr(str, rightboundary);
-               if ((rb != NULL) && (rb >= lb))  {
-                       *rb = 0;
-                       fflush(stderr);
-                       len = (long)rb - (long)lb;
-                       memmove(str, lb, len);
-                       str[len] = 0;
-                       return(len);
+       long lb = (-1);
+       long rb = (-1);
+       long orig_len = 0;
+
+       if (!str) {
+               return 0;
+       }
+
+       while (str[orig_len]) {
+               if ((lb==-1) && (str[orig_len] == leftboundary)) {
+                       lb = orig_len;
+               } else if (str[orig_len] == rightboundary) {
+                       rb = orig_len;
                }
+               orig_len++;
        }
 
-       return (long)strlen(str);
-}
+       if ((lb==-1) || (rb <= lb)) {
+               return orig_len;
+       }
 
+       fflush(stderr);
+
+       long new_len = rb - lb - 1;
+       memmove(str, str + lb + 1, new_len);
+       str[new_len] = 0;
+       return new_len;
+}
 
 char *myfgets(char *s, int size, FILE *stream) {
        char *ret = fgets(s, size, stream);
@@ -808,27 +815,6 @@ int pattern2(char *search, char *patn) {
 }
 
 
-/*
- * Strip leading and trailing spaces from a string; with premeasured and adjusted length.
- * buf - the string to modify
- * len - length of the string. 
- */
-void string_trimlen(char *buf, int *len) {
-       int delta = 0;
-       if (*len == 0) return;
-       while ((*len > delta) && (isspace(buf[delta]))){
-               delta ++;
-       }
-       memmove (buf, &buf[delta], *len - delta + 1);
-       (*len) -=delta;
-
-       if (*len == 0) return;
-       while (isspace(buf[(*len) - 1])){
-               buf[--(*len)] = '\0';
-       }
-}
-
-
 /*
  * Convert all whitespace characters in a supplied string to underscores
  */