having this code in generator.c and receiver.c.
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));
}
}
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)
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
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);
} 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);
}