commit ed92816fdb6cb43b3c65bb051404000d524ea958
Author: Oswald Buddenhagen <o...@users.sf.net>
Date:   Tue May 3 19:22:24 2022 +0200

    preserve stray CRs in copy_msg_convert()
    
    strip only CRs followed by LFs. this has mostly theoretical value (but
    will make a subsequent change simpler).

 src/sync_msg_cvt.c | 44 ++++++++++++++++++++++++--------------------
 src/tst_msg_cvt.c  |  2 +-
 2 files changed, 25 insertions(+), 21 deletions(-)

diff --git a/src/sync_msg_cvt.c b/src/sync_msg_cvt.c
index 93b43676..472fcf6b 100644
--- a/src/sync_msg_cvt.c
+++ b/src/sync_msg_cvt.c
@@ -13,19 +13,19 @@ copy_msg_bytes( char **out_ptr, const char *in_buf, uint 
*in_idx, uint in_len, i
        char *out = *out_ptr;
        uint idx = *in_idx;
        if (out_cr != in_cr) {
-               char c;
                if (out_cr) {
-                       for (; idx < in_len; idx++) {
-                               if ((c = in_buf[idx]) != '\r') {
-                                       if (c == '\n')
-                                               *out++ = '\r';
-                                       *out++ = c;
-                               }
+                       for (char c, pc = 0; idx < in_len; idx++) {
+                               if (((c = in_buf[idx]) == '\n') && (pc != '\r'))
+                                       *out++ = '\r';
+                               *out++ = c;
+                               pc = c;
                        }
                } else {
-                       for (; idx < in_len; idx++) {
-                               if ((c = in_buf[idx]) != '\r')
-                                       *out++ = c;
+                       for (char c, pc = 0; idx < in_len; idx++) {
+                               if (((c = in_buf[idx]) == '\n') && (pc == '\r'))
+                                       out--;
+                               *out++ = c;
+                               pc = c;
                        }
                }
        } else {
@@ -49,12 +49,13 @@ copy_msg_convert( int in_cr, int out_cr, copy_vars_t *vars )
        if (vars->srec) {
          nloop: ;
                uint start = idx;
-               uint line_crs = 0;
+               uint line_cr = 0;
+               char pc = 0;
                while (idx < in_len) {
                        char c = in_buf[idx++];
-                       if (c == '\r') {
-                               line_crs++;
-                       } else if (c == '\n') {
+                       if (c == '\n') {
+                               if (pc == '\r')
+                                       line_cr = 1;
                                if (!ebreak && starts_with_upper( in_buf + 
start, (int)(in_len - start), "X-TUID: ", 8 )) {
                                        extra = (sbreak = start) - (ebreak = 
idx);
                                        if (!vars->minimal)
@@ -67,9 +68,9 @@ copy_msg_convert( int in_cr, int out_cr, copy_vars_t *vars )
                                                        break2++;
                                        }
                                        lines++;
-                                       hdr_crs += line_crs;
+                                       hdr_crs += line_cr;
                                }
-                               if (idx - line_crs - 1 == start) {
+                               if (idx - line_cr - 1 == start) {
                                        if (!ebreak)
                                                sbreak = ebreak = start;
                                        if (vars->minimal) {
@@ -83,6 +84,7 @@ copy_msg_convert( int in_cr, int out_cr, copy_vars_t *vars )
                                }
                                goto nloop;
                        }
+                       pc = c;
                }
                free( in_buf );
                return "has incomplete header";
@@ -91,12 +93,14 @@ copy_msg_convert( int in_cr, int out_cr, copy_vars_t *vars )
                extra += 8 + TUIDL + app_cr + 1;
        }
        if (out_cr != in_cr) {
-               for (; idx < in_len; idx++) {
+               for (char pc = 0; idx < in_len; idx++) {
                        char c = in_buf[idx];
-                       if (c == '\r')
-                               bdy_crs++;
-                       else if (c == '\n')
+                       if (c == '\n') {
                                lines++;
+                               if (pc == '\r')
+                                       bdy_crs++;
+                       }
+                       pc = c;
                }
                extra -= hdr_crs + bdy_crs;
                if (out_cr)
diff --git a/src/tst_msg_cvt.c b/src/tst_msg_cvt.c
index afd437a4..399c5c2b 100644
--- a/src/tst_msg_cvt.c
+++ b/src/tst_msg_cvt.c
@@ -130,7 +130,7 @@ mintests( const char *name, const char *in, const char 
*out, int flagged )
        tests( name, in, out, ADD_TUID, MINIMAL, flagged );
 }
 
-#define FROM "From: devil\r\n"
+#define FROM "From: de\rvil\r\n"
 #define TO "To: me\r\n"
 #define IN_TUID "X-TUID: garbage\r\n"
 #define OUT_TUID "X-TUID: " TUID "\r\n"


_______________________________________________
isync-devel mailing list
isync-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/isync-devel

Reply via email to