Some checksum improvements
[rsync.git] / io.c
diff --git a/io.c b/io.c
index a868fa9caa4307e839821f1e84de1bab2a3bdcd8..189bc23237916b35b68d799db9a9e1da5354fd10 100644 (file)
--- a/io.c
+++ b/io.c
@@ -4,7 +4,7 @@
  * Copyright (C) 1996-2001 Andrew Tridgell
  * Copyright (C) 1996 Paul Mackerras
  * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
- * Copyright (C) 2003-2014 Wayne Davison
+ * Copyright (C) 2003-2019 Wayne Davison
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -2013,20 +2013,20 @@ void write_varint(int f, int32 x)
 {
        char b[5];
        uchar bit;
-       int cnt = 4;
+       int cnt;
 
        SIVAL(b, 1, x);
 
-       while (cnt > 1 && b[cnt] == 0)
-               cnt--;
+       for (cnt = 4; cnt > 1 && b[cnt] == 0; cnt--) {}
        bit = ((uchar)1<<(7-cnt+1));
+
        if (CVAL(b, cnt) >= bit) {
                cnt++;
                *b = ~(bit-1);
        } else if (cnt > 1)
                *b = b[cnt] | ~(bit*2-1);
        else
-               *b = b[cnt];
+               *b = b[1];
 
        write_buf(f, b, cnt);
 }
@@ -2283,7 +2283,7 @@ void io_printf(int fd, const char *format, ...)
        if (len < 0)
                exit_cleanup(RERR_PROTOCOL);
 
-       if (len > (int)sizeof buf) {
+       if (len >= (int)sizeof buf) {
                rprintf(FERROR, "io_printf() was too long for the buffer.\n");
                exit_cleanup(RERR_PROTOCOL);
        }
@@ -2368,7 +2368,8 @@ void start_write_batch(int fd)
         * is involved. */
        write_int(batch_fd, protocol_version);
        if (protocol_version >= 30)
-               write_byte(batch_fd, compat_flags);
+               write_varint(batch_fd, compat_flags);
+       maybe_write_checksum(batch_fd);
        write_int(batch_fd, checksum_seed);
 
        if (am_sender)