From 46e99b09b980f3d67dc3f92f07b27db754981662 Mon Sep 17 00:00:00 2001 From: Wayne Davison Date: Fri, 4 Mar 2005 15:38:58 +0000 Subject: [PATCH] Added read_vstring() and write_vstring() to io.c instead of having this code in generator.c and receiver.c. --- generator.c | 15 ++------------- io.c | 39 +++++++++++++++++++++++++++++++++++++++ receiver.c | 18 +----------------- 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/generator.c b/generator.c index c1a86f49..b8888f9d 100644 --- a/generator.c +++ b/generator.c @@ -1021,19 +1021,8 @@ notify_others: if (f_out_name >= 0) { write_byte(f_out_name, fnamecmp_type); if (fnamecmp_type == FNAMECMP_FUZZY) { - uchar lenbuf[3], *lb = lenbuf; - int len = strlen(fuzzy_file->basename); - if (len > 0x7F) { -#if MAXPATHLEN > 0x7FFF - *lb++ = len / 0x10000 + 0x80; - *lb++ = len / 0x100; -#else - *lb++ = len / 0x100 + 0x80; -#endif - } - *lb = len; - write_buf(f_out_name, (char*)lenbuf, lb - lenbuf + 1); - write_buf(f_out_name, fuzzy_file->basename, len); + write_vstring(f_out_name, fuzzy_file->basename, + strlen(fuzzy_file->basename)); } } 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 diff --git a/receiver.c b/receiver.c index b71d5996..656f2a25 100644 --- a/receiver.c +++ b/receiver.c @@ -306,17 +306,6 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r, static void read_gen_name(int fd, char *dirname, char *buf) { int dlen; - int len = read_byte(fd); - - if (len & 0x80) { -#if MAXPATHLEN > 32767 - uchar lenbuf[2]; - read_buf(fd, (char *)lenbuf, 2); - len = (len & ~0x80) * 0x10000 + lenbuf[0] * 0x100 + lenbuf[1]; -#else - len = (len & ~0x80) * 0x100 + read_byte(fd); -#endif - } if (dirname) { dlen = strlcpy(buf, dirname, MAXPATHLEN); @@ -324,12 +313,7 @@ static void read_gen_name(int fd, char *dirname, char *buf) } else dlen = 0; - if (dlen + len >= MAXPATHLEN) { - rprintf(FERROR, "bogus data on generator name pipe\n"); - exit_cleanup(RERR_PROTOCOL); - } - - read_sbuf(fd, buf + dlen, len); + read_vstring(fd, buf + dlen, MAXPATHLEN - dlen); } -- 2.34.1