diff --git a/src/dcc.c b/src/dcc.c index 3ce979ee5..d8473a9bc 100644 --- a/src/dcc.c +++ b/src/dcc.c @@ -89,22 +89,6 @@ static int detect_telnet(unsigned char *buf) return 0; } -/* Escape telnet IAC and prepend CR to LF */ -static char *escape_telnet(char *s) -{ - static char buf[1024]; - char *p; - - for (p = buf; *s && (p < (buf + sizeof(buf) - 2)); *p++ = *s++) - if ((unsigned char) *s == TLN_IAC) - *p++ = *s; - else if (*s == '\n') - *p++ = '\r'; - *p = 0; - - return buf; -} - static void strip_telnet(int sock, char *buf, int *len) { unsigned char *p = (unsigned char *) buf, *o = (unsigned char *) buf; @@ -969,7 +953,7 @@ static void out_dcc_general(int idx, char *buf, void *x) strip_mirc_codes(p->strip_flags, buf); if (dcc[idx].status & STAT_TELNET) - y = escape_telnet(buf); + y = add_cr(buf, 1); if (dcc[idx].status & STAT_PAGE) append_line(idx, y); else @@ -1827,7 +1811,7 @@ static void dcc_telnet_pass(int idx, int atr) /* Turn off remote telnet echo (send IAC WILL ECHO). */ if (dcc[idx].status & STAT_TELNET) { char buf[1030]; - egg_snprintf(buf, sizeof buf, "\n%s%s\r\n", escape_telnet(DCC_ENTERPASS), + egg_snprintf(buf, sizeof buf, "\n%s%s\r\n", add_cr(DCC_ENTERPASS, 1), TLN_IAC_C TLN_WILL_C TLN_ECHO_C); tputs(dcc[idx].sock, buf, strlen(buf)); } else diff --git a/src/dccutil.c b/src/dccutil.c index 24cb43cfd..322dd70f1 100644 --- a/src/dccutil.c +++ b/src/dccutil.c @@ -129,21 +129,6 @@ int findanyidx(int z) return -1; } -/* Replace \n with \r\n */ -char *add_cr(char *buf) -{ - static char WBUF[1024]; - char *p, *q; - - for (p = buf, q = WBUF; *p; p++, q++) { - if (*p == '\n') - *q++ = '\r'; - *q = *p; - } - *q = *p; - return WBUF; -} - extern void (*qserver) (int, char *, int); ATTRIBUTE_FORMAT(printf,2,3) @@ -204,7 +189,7 @@ void dprint(int idx, char *buf, int len) len = LOGLINEMAX-10; } if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) { - char *p = add_cr(buf); + char *p = add_cr(buf, 0); tputs(dcc[idx].sock, p, strlen(p)); } else if (dcc[idx].type && dcc[idx].type->output) diff --git a/src/misc.c b/src/misc.c index 1c521475e..f51d1af75 100644 --- a/src/misc.c +++ b/src/misc.c @@ -1590,3 +1590,28 @@ int crypto_verify(const char *x_, const char *y_) } return (1 & ((d - 1) >> 8)) - 1; } + +/* Prepend CR to LF (Replace \n with \r\n) and escape telnet IAC if + * escape_telnet is set + */ +char *add_cr(const char *p, const int escape_telnet) +{ + size_t len; + static size_t buf_size = 0; + static char *buf; + char *q; + + len = (strlen(p) << 1) + 1; + if (len > buf_size) { + buf_size = len; + buf = nrealloc(buf, buf_size); + } + for (q = buf; *p; *q++ = *p++) { + if (escape_telnet && ((unsigned char) *p == TLN_IAC)) + *q++ = *p; + else if (*p == '\n') + *q++ = '\r'; + } + *q = 0; + return buf; +} diff --git a/src/proto.h b/src/proto.h index 0d95ae488..d84069e1a 100644 --- a/src/proto.h +++ b/src/proto.h @@ -141,7 +141,6 @@ int dcc_fingerprint(int); int increase_socks_max(void); int findidx(int); int findanyidx(int); -char *add_cr(char *); void dprint(int, char *, int); void dprintf (int, const char *format, ...) ATTRIBUTE_FORMAT(printf,2,3); void chatout (const char *format, ...) ATTRIBUTE_FORMAT(printf,1,2); @@ -267,6 +266,7 @@ void maskaddr(const char *, char *, int); #define maskhost(a,b) maskaddr((a),(b),3) #define maskban(a,b) maskaddr((a),(b),3) int crypto_verify(const char *, const char *); +char *add_cr(const char *, const int); /* net.c */ IP my_atoul(char *);