X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/blobdiff_plain/af436313a05bd40ccc08df05dbf22c46e51882bf..46e99b09b980f3d67dc3f92f07b27db754981662:/io.c diff --git a/io.c b/io.c index a53b347c..64c19007 100644 --- a/io.c +++ b/io.c @@ -850,6 +850,25 @@ uchar read_byte(int f) return c; } +int read_vstring(int f, char *buf, int bufsize) +{ + int len = read_byte(f); + + if (len & 0x80) + len = (len & ~0x80) * 0x100 + read_byte(f); + + if (len >= bufsize) { + rprintf(FERROR, "over-long vstring received (%d > %d)\n", + len, bufsize - 1); + exit_cleanup(RERR_PROTOCOL); + } + + if (len) + readfd(f, buf, len); + buf[len] = '\0'; + return len; +} + /* Populate a sum_struct with values from the socket. This is * called by both the sender and the receiver. */ void read_sum_head(int f, struct sum_struct *sum) @@ -1203,6 +1222,26 @@ void write_byte(int f, uchar c) writefd(f, (char *)&c, 1); } +void write_vstring(int f, 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); +} + /** * Read a line of up to @p maxlen characters into @p buf (not counting