From 36b8776e3788b80fa07f4e43288bc60ab1d69b13 Mon Sep 17 00:00:00 2001
From: scw <scw@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>
Date: Wed, 24 Dec 2003 06:58:25 +0000
Subject: Rewrite strip_ansi() (also move from vote.c to io.c). Using STRIP_ALL
 instead of 0 when calling strip_ansi. Fix some buffer overflow problem.

git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@1428 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
---
 mbbsd/bbs.c    |  4 ++--
 mbbsd/gamble.c |  2 +-
 mbbsd/io.c     | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 mbbsd/kaede.c  |  2 ++
 mbbsd/mail.c   |  2 +-
 mbbsd/talk.c   |  2 +-
 mbbsd/vote.c   | 34 -------------------------------
 7 files changed, 69 insertions(+), 40 deletions(-)

(limited to 'mbbsd')

diff --git a/mbbsd/bbs.c b/mbbsd/bbs.c
index b6f509f4..6ce918c7 100644
--- a/mbbsd/bbs.c
+++ b/mbbsd/bbs.c
@@ -603,7 +603,7 @@ do_general(int isbid)
            }
 	}
 	getdata_buf(22, 0, "���D�G", save_title, TTLEN, DOECHO);
-	strip_ansi(save_title, save_title, 0);
+	strip_ansi(save_title, save_title, STRIP_ALL);
     }
     if (save_title[0] == '\0')
 	return FULLUPDATE;
@@ -2191,7 +2191,7 @@ bh_title_edit()
 
 	if (!genbuf[0])
 	    return 0;
-	strip_ansi(genbuf, genbuf, 0);
+	strip_ansi(genbuf, genbuf, STRIP_ALL);
 	strlcpy(bp->title + 7, genbuf, sizeof(bp->title) - 7);
 	substitute_record(fn_board, bp, sizeof(boardheader_t), currbid);
 	log_usies("SetBoard", currboard);
diff --git a/mbbsd/gamble.c b/mbbsd/gamble.c
index 9b9939df..c19f1e5a 100644
--- a/mbbsd/gamble.c
+++ b/mbbsd/gamble.c
@@ -287,7 +287,7 @@ openticket(int bid)
     setbfile(buf, bh->brdname, FN_TICKET_USER);
     if ((bet == 98 || ticket[bet]) && (fp1 = fopen(buf, "r"))) {
 	int             mybet, uid;
-	char            userid[IDLEN];
+	char            userid[IDLEN + 1];
 
 	while (fscanf(fp1, "%s %d %d\n", userid, &mybet, &i) != EOF) {
 	    if (bet == 98 && mybet >= 0 && mybet < count) {
diff --git a/mbbsd/io.c b/mbbsd/io.c
index f31d5f30..1d1677a1 100644
--- a/mbbsd/io.c
+++ b/mbbsd/io.c
@@ -386,6 +386,67 @@ igetch()
     return 0;
 }
 
+int
+strip_ansi(char *buf, char *str, int mode)
+{
+    register int    count = 0;
+    static char EscapeFlag[] = {
+	/*  0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+	/* 10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0,
+	/* 20 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+	/* 30 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, /* 0~9 ;= */
+	/* 40 */ 0, 2, 2, 2, 2, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, /* ABCDHIJK */
+	/* 50 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, /* [ */
+	/* 60 */ 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 2, 0, 0, /* fhlm */
+	/* 70 */ 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* su */
+	/* 80 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+	/* 90 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+	/* A0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+	/* B0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+	/* C0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+	/* D0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+	/* E0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+	/* F0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    };
+#define isEscapeParam(X) (EscapeFlag[(X)] & 1)
+#define isEscapeCommand(X) (EscapeFlag[(X)] & 2)
+
+    for(; *str; ++str)
+	if( *str != '\033' ){
+	    if( buf )
+		*buf++ = *str;
+	    ++count;
+	}else{
+	    register char* p = str;
+	    while(isEscapeParam(*++p));
+	    if( (mode == NO_RELOAD && isEscapeCommand(*p)) ||
+		(mode == ONLY_COLOR && *p == 'm' )){
+		register int len = p - str + 1;
+		if( buf ){
+		    strncpy(buf, str, len);
+		    buf += len;
+		}
+		count += len;
+	    }
+	    str = p;
+	}
+    if( buf )
+	*buf = 0;
+    return count;
+
+
+    /* Rewritten by scw.
+     * Moved from vote.c (here is a better place for this function).
+     * register int    ansi, count = 0;
+     * for (ansi = 0; *str ; str++) { if (*str == 27) { if (mode) { if (buf)
+     * *buf++ = *str; count++; } ansi = 1; } else if (ansi && strchr(
+     * "[;1234567890mfHABCDnsuJKc=n", *str)) { if ((mode == NO_RELOAD &&
+     * !strchr("c=n", *str)) || (mode == ONLY_COLOR && strchr("[;1234567890m",
+     * *str))) { if (buf) *buf++ = *str; count++; } if (strchr("mHn ", *str))
+     * ansi = 0; } else { ansi = 0; if (buf) *buf++ = *str; count++; } } if
+     * (buf) *buf = '\0'; return count; */
+}
+
 int
 oldgetdata(int line, int col, char *prompt, char *buf, int len, int echo)
 {
@@ -401,7 +462,7 @@ oldgetdata(int line, int col, char *prompt, char *buf, int len, int echo)
 	move(line, col);
 	clrtoeol();
 	outs(prompt);
-	x += strip_ansi(NULL, prompt, 0);
+	x += strip_ansi(NULL, prompt, STRIP_ALL);
     }
 
     if (!echo) {
diff --git a/mbbsd/kaede.c b/mbbsd/kaede.c
index f166eb73..42730c1f 100644
--- a/mbbsd/kaede.c
+++ b/mbbsd/kaede.c
@@ -49,9 +49,11 @@ Ptt_prints(char *str, int mode)
 				  "%d", cuser.money);
 		    break;
 		default:
+		    /* It's saver not to send these undefined escape string. 
 		    strbuf[w++] = '\033';
 		    strbuf[w++] = '*';
 		    strbuf[w++] = str[r];
+		    */
 		}
 	    }
 	}
diff --git a/mbbsd/mail.c b/mbbsd/mail.c
index 8db98d2a..171269af 100644
--- a/mbbsd/mail.c
+++ b/mbbsd/mail.c
@@ -233,7 +233,7 @@ do_send(char *userid, char *title)
 {
     fileheader_t    mhdr;
     char            fpath[STRLEN];
-    char            receiver[IDLEN];
+    char            receiver[IDLEN + 1];
     char            genbuf[200];
     int             internet_mail, i;
 
diff --git a/mbbsd/talk.c b/mbbsd/talk.c
index 827a9406..5bba046b 100644
--- a/mbbsd/talk.c
+++ b/mbbsd/talk.c
@@ -602,7 +602,7 @@ my_write(pid_t pid, char *prompt, char *id, int flag, userinfo_t * puin)
 	len = strlen(msg);
     }
 
-    strip_ansi(msg, msg, 0);
+    strip_ansi(msg, msg, STRIP_ALL);
     if (uin && *uin->userid && (flag == 0 || flag == 4)) {
 	snprintf(buf, sizeof(buf), "�ᵹ %s : %s [Y/n]?", uin->userid, msg);
 	getdata(0, 0, buf, genbuf, 3, LCECHO);
diff --git a/mbbsd/vote.c b/mbbsd/vote.c
index 8213ea69..16455e7f 100644
--- a/mbbsd/vote.c
+++ b/mbbsd/vote.c
@@ -19,40 +19,6 @@ static char     STR_new_comments[] = "comments0\0";	/* 
 static char     STR_new_limited[] = "limited0\0";	/* �p�H�벼 */
 static char     STR_new_title[] = "vtitle0\0";
 
-int
-strip_ansi(char *buf, char *str, int mode)
-{
-    register int    ansi, count = 0;
-
-    for (ansi = 0; *str /* && *str != '\n' */ ; str++) {
-	if (*str == 27) {
-	    if (mode) {
-		if (buf)
-		    *buf++ = *str;
-		count++;
-	    }
-	    ansi = 1;
-	} else if (ansi && strchr("[;1234567890mfHABCDnsuJKc=n", *str)) {
-	    if ((mode == NO_RELOAD && !strchr("c=n", *str)) ||
-		(mode == ONLY_COLOR && strchr("[;1234567890m", *str))) {
-		if (buf)
-		    *buf++ = *str;
-		count++;
-	    }
-	    if (strchr("mHn ", *str))
-		ansi = 0;
-	} else {
-	    ansi = 0;
-	    if (buf)
-		*buf++ = *str;
-	    count++;
-	}
-    }
-    if (buf)
-	*buf = '\0';
-    return count;
-}
-
 void
 b_suckinfile(FILE * fp, char *fname)
 {
-- 
cgit v1.2.3