+ SIVAL(s, 0, x);
+ if (x <= 0x7FFFFFFF && x >= 0) {
+ writefd(f, s, 4);
+ return;
+ }
+
+#if SIZEOF_INT64 < 8
+ rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
+ exit_cleanup(RERR_UNSUPPORTED);
+#else
+ memset(b, 0xFF, 4);
+ SIVAL(s, 4, x >> 32);
+ writefd(f, b, 12);
+#endif
+}
+
+void write_buf(int f, const char *buf, size_t len)
+{
+ writefd(f,buf,len);
+}
+
+/** Write a string to the connection */
+void write_sbuf(int f, const char *buf)
+{
+ writefd(f, buf, strlen(buf));
+}
+
+void write_byte(int f, uchar c)
+{
+ writefd(f, (char *)&c, 1);
+}
+
+void write_vstring(int f, const char *str, int len)
+{
+ uchar lenbuf[3], *lb = lenbuf;
+
+ if (len > 0x7F) {
+ if (len > 0x7FFF) {
+ rprintf(FERROR,
+ "attempting to send over-long vstring (%d > %d)\n",
+ len, 0x7FFF);
+ exit_cleanup(RERR_PROTOCOL);
+ }
+ *lb++ = len / 0x100 + 0x80;
+ }
+ *lb = len;
+
+ writefd(f, (char*)lenbuf, lb - lenbuf + 1);
+ if (len)
+ writefd(f, str, len);
+}
+
+/* Send a file-list index using a byte-reduction method. */
+void write_ndx(int f, int32 ndx)
+{
+ static int32 prev_positive = -1, prev_negative = 1;
+ int32 diff, cnt = 0;
+ char b[6];
+
+ if (protocol_version < 30 || read_batch) {
+ write_int(f, ndx);
+ return;
+ }
+
+ /* Send NDX_DONE as a single-byte 0 with no side effects. Send
+ * negative nums as a positive after sending a leading 0xFF. */
+ if (ndx >= 0) {
+ diff = ndx - prev_positive;
+ prev_positive = ndx;
+ } else if (ndx == NDX_DONE) {
+ *b = 0;
+ writefd(f, b, 1);
+ return;
+ } else {
+ b[cnt++] = (char)0xFF;
+ ndx = -ndx;
+ diff = ndx - prev_negative;
+ prev_negative = ndx;
+ }
+
+ /* A diff of 1 - 253 is sent as a one-byte diff; a diff of 254 - 32767
+ * or 0 is sent as a 0xFE + a two-byte diff; otherwise we send 0xFE
+ * & all 4 bytes of the (non-negative) num with the high-bit set. */
+ if (diff < 0xFE && diff > 0)
+ b[cnt++] = (char)diff;
+ else if (diff < 0 || diff > 0x7FFF) {
+ b[cnt++] = (char)0xFE;
+ b[cnt++] = (char)((ndx >> 24) | 0x80);
+ b[cnt++] = (char)ndx;
+ b[cnt++] = (char)(ndx >> 8);
+ b[cnt++] = (char)(ndx >> 16);
+ } else {
+ b[cnt++] = (char)0xFE;
+ b[cnt++] = (char)(diff >> 8);
+ b[cnt++] = (char)diff;
+ }
+ writefd(f, b, cnt);
+}
+
+/* Receive a file-list index using a byte-reduction method. */
+int32 read_ndx(int f)
+{
+ static int32 prev_positive = -1, prev_negative = 1;
+ int32 *prev_ptr, num;
+ char b[4];
+
+ if (protocol_version < 30)
+ return read_int(f);
+
+ readfd(f, b, 1);
+ if (CVAL(b, 0) == 0xFF) {
+ readfd(f, b, 1);
+ prev_ptr = &prev_negative;
+ } else if (CVAL(b, 0) == 0)
+ return NDX_DONE;
+ else
+ prev_ptr = &prev_positive;
+ if (CVAL(b, 0) == 0xFE) {
+ readfd(f, b, 2);
+ if (CVAL(b, 0) & 0x80) {
+ b[3] = CVAL(b, 0) & ~0x80;
+ b[0] = b[1];
+ readfd(f, b+1, 2);
+ num = IVAL(b, 0);
+ } else
+ num = (UVAL(b,0)<<8) + UVAL(b,1) + *prev_ptr;
+ } else
+ num = UVAL(b, 0) + *prev_ptr;
+ *prev_ptr = num;
+ if (prev_ptr == &prev_negative)
+ num = -num;
+ return num;
+}
+
+/**
+ * Read a line of up to @p maxlen characters into @p buf (not counting
+ * the trailing null). Strips the (required) trailing newline and all
+ * carriage returns.
+ *
+ * @return 1 for success; 0 for I/O error or truncation.
+ **/
+int read_line(int f, char *buf, size_t maxlen)
+{
+ while (maxlen) {
+ buf[0] = 0;
+ read_buf(f, buf, 1);
+ if (buf[0] == 0)
+ return 0;
+ if (buf[0] == '\n')
+ break;
+ if (buf[0] != '\r') {
+ buf++;
+ maxlen--;
+ }
+ }
+ *buf = '\0';
+ return maxlen > 0;
+}
+
+void io_printf(int fd, const char *format, ...)
+{
+ va_list ap;
+ char buf[BIGPATHBUFLEN];
+ int len;
+
+ va_start(ap, format);
+ len = vsnprintf(buf, sizeof buf, format, ap);
+ va_end(ap);
+
+ if (len < 0)
+ exit_cleanup(RERR_STREAMIO);
+
+ if (len > (int)sizeof buf) {
+ rprintf(FERROR, "io_printf() was too long for the buffer.\n");
+ exit_cleanup(RERR_STREAMIO);
+ }
+
+ write_sbuf(fd, buf);
+}
+
+/** Setup for multiplexing a MSG_* stream with the data stream. */
+void io_start_multiplex_out(void)
+{
+ io_flush(NORMAL_FLUSH);
+ io_start_buffering_out(sock_f_out);
+ io_multiplexing_out = 1;
+}
+
+/** Setup for multiplexing a MSG_* stream with the data stream. */
+void io_start_multiplex_in(void)
+{
+ io_flush(NORMAL_FLUSH);
+ io_start_buffering_in(sock_f_in);
+ io_multiplexing_in = 1;
+}
+
+/** Write an message to the multiplexed data stream. */
+int io_multiplex_write(enum msgcode code, const char *buf, size_t len, int convert)
+{
+ if (!io_multiplexing_out)
+ return 0;
+ io_flush(NORMAL_FLUSH);
+ stats.total_written += (len+4);
+ mplex_write(sock_f_out, code, buf, len, convert);
+ return 1;
+}
+
+void io_end_multiplex_in(void)
+{
+ io_multiplexing_in = 0;
+ io_end_buffering_in();
+}
+
+/** Stop output multiplexing. */
+void io_end_multiplex_out(void)
+{
+ io_multiplexing_out = 0;
+ io_end_buffering_out();
+}
+
+void start_write_batch(int fd)
+{
+ /* Some communication has already taken place, but we don't
+ * enable batch writing until here so that we can write a
+ * canonical record of the communication even though the
+ * actual communication so far depends on whether a daemon
+ * is involved. */
+ write_int(batch_fd, protocol_version);
+ write_int(batch_fd, checksum_seed);
+
+ if (am_sender)
+ write_batch_monitor_out = fd;
+ else
+ write_batch_monitor_in = fd;
+}
+
+void stop_write_batch(void)
+{
+ write_batch_monitor_out = -1;
+ write_batch_monitor_in = -1;
+}