From owner-FreeBSD-tech-jp@jp.freebsd.org  Fri Nov 27 00:22:56 1998
Received: (from daemon@localhost)
	by jaz.jp.freebsd.org (8.9.1+3.1W/8.7.3) id AAA10105;
	Fri, 27 Nov 1998 00:22:56 +0900 (JST)
	(envelope-from owner-FreeBSD-tech-jp@jp.FreeBSD.org)
Received: from mana.s.notwork.org (tok135.airnet.ne.jp [210.159.88.135])
	by jaz.jp.freebsd.org (8.9.1+3.1W/8.7.3) with SMTP id AAA10095
	for <FreeBSD-tech-jp@jp.freebsd.org>; Fri, 27 Nov 1998 00:22:52 +0900 (JST)
	(envelope-from mrt@notwork.org)
Received: (qmail 23526 invoked from network); 26 Nov 1998 15:22:27 -0000
Received: from kotonoha.s.notwork.org (192.168.1.1)
  by mana.s.notwork.org with SMTP; 26 Nov 1998 15:22:27 -0000
To: FreeBSD-tech-jp@jp.freebsd.org
X-cite: xcite 1.20
References: <199811261111.UAA00535@lavender.yy.cs.keio.ac.jp>
MIME-Version: 1.0 (generated by WEMI 1.11.1 - "Shimizu")
Content-Type: text/plain; charset=ISO-2022-JP
From: Murata Shuuichirou <mrt@notwork.org>
Date: 27 Nov 1998 00:22:52 +0900
In-Reply-To: <199811261111.UAA00535@lavender.yy.cs.keio.ac.jp>
 (MIHIRA Yoshiro's message of "Thu, 26 Nov 1998 20:11:59 +0900 (JST)")
Message-ID: <873e76xyur.fsf@kotonoha.s.notwork.org>
Lines: 342
User-Agent: T-gnus/6.10.040 (based on Pterodactyl Gnus 0.54) WEMI/1.11.1 (Shimizu) FLIM/1.12.0 (Amagatsuji) MULE XEmacs/21.2 (Aglaia) (i386-unknown-freebsd3.0)
Reply-To: FreeBSD-tech-jp@jp.freebsd.org
Precedence: list
X-Distribute: distribute version 2.1 (Alpha) patchlevel 24e+981115
X-Sequence: FreeBSD-tech-jp 1919
Subject: [FreeBSD-tech-jp 1919] Re: ansi.c patch for ja-netscape
Errors-To: owner-FreeBSD-tech-jp@jp.freebsd.org
Sender: owner-FreeBSD-tech-jp@jp.freebsd.org
X-Originator: mrt@kotonoha.s.notwork.org

In message <199811261111.UAA00535@lavender.yy.cs.keio.ac.jp>,
 `sanpei@sanpei.org' wrote:
>   Netscape $B$,IT0BDj$K$J$kLdBj$G$9$,(B. 

$B$=$&$$$($P!"(Blocale $B$K<j$r$$$l$?$N$r30$K=P$9$N$r$9$C$+$jK:$l$F$$(B
$B$^$7$?!#$3$N%a%$%k$K$D$1$k(B diff $B$O(B

1. mbstowcs(), wcstombs() $B$"$?$j$,$^$H$b$C$]$/F0$/(B
2. netscape navigator/communicator $B$,0BDj$9$k5$$,$9$k(B

$B$N(B*$B$I$A$i$+(B*$B$r<B8=$9$k$b$N$G$9!#%G%U%)%k%H$G$O(B 2 $B$K$J$C$F$$$^$9(B
$B$,!"%^%/%m(B NETSCAPE_BUG $B$r(B 0 $B$K(B define $B$7$F$*$/$H(B 1 $B$NF0:n$K$J(B
$B$j$^$9!#(Bdiff $B$rE,MQ$7$?$N$A$K!"(Blibxpg4 $B$H(B libc $B$r:n$j$J$*$7$F$/(B
$B$@$5$$!#(B

-current $B$+$i$N(B diff $B$K$J$C$F$$$^$9$N$G!"(B-stable $B$K$O$=$N$^$^$G(B
$B$O$"$?$j$^$;$s!#(Bansi.c $B$@$1$"$i$+$8$a(B -current $B$+$i$b$C$F$-$F$*(B
$B$1$PBg>fIW$@$H;W$$$^$9!#(B

-- 
$B$`$i$?$7$e$&$$$A$m$&(B

Index: locale/ansi.c
===================================================================
RCS file: /usr/home/cvs/src/lib/libc/locale/ansi.c,v
retrieving revision 1.3
diff -u -r1.3 ansi.c
--- ansi.c	1998/10/25 05:06:42	1.3
+++ ansi.c	1998/11/26 14:59:26
@@ -43,6 +43,10 @@
 #include <stddef.h>
 #include <rune.h>
 
+#ifndef NETSCAPE_BUG
+#define NETSCAPE_BUG 1
+#endif
+
 int
 mblen(s, n)
 	const char *s;
@@ -53,8 +57,10 @@
 	if (s == 0 || *s == 0)
 		return (0);	/* No support for state dependent encodings. */
 
-	if (sgetrune(s, n, &e) == _INVALID_RUNE)
-		return (s - e);
+	if (sgetrune(s, n > MB_CUR_MAX? MB_CUR_MAX: n, &e) == _INVALID_RUNE) {
+		/* errno = EILSEQ; */
+		return (-1);
+	}
 	return (e - s);
 }
 
@@ -70,8 +76,10 @@
 	if (s == 0 || *s == 0)
 		return (0);	/* No support for state dependent encodings. */
 
-	if ((r = sgetrune(s, n, &e)) == _INVALID_RUNE)
-		return (s - e);
+	if ((r = sgetrune(s, n > MB_CUR_MAX? MB_CUR_MAX: n, &e)) == _INVALID_RUNE) {
+		/* errno = EILSEQ; */
+		return (-1);
+	}
 	if (pwc)
 		*pwc = r;
 	return (e - s);
@@ -103,16 +111,25 @@
 	size_t n;
 {
 	char const *e;
+	rune_t r;
 	int cnt = 0;
 
-	if (!pwcs || !s)
+	if (!s)
 		return (-1);
 
+#if NETSCAPE_BUG
 	while (n-- > 0) {
-		*pwcs = sgetrune(s, MB_LEN_MAX, &e);
-		if (*pwcs == _INVALID_RUNE)
+#else
+	while (n-- > 0 || !pwcs) {
+#endif
+		r = sgetrune(s, MB_CUR_MAX, &e);
+		if (r == _INVALID_RUNE) {
+			/* errno = EILSEQ; */
 			return (-1);
-		if (*pwcs++ == 0)
+		}
+		if (pwcs)
+			*pwcs++ = r;
+		if (r == 0)
 			break;
 		s = e;
 		++cnt;
@@ -127,25 +144,47 @@
 	size_t n;
 {
 	char *e;
-	int cnt, nb;
+	int cnt = 0;
 
-	if (!pwcs || !s || n > INT_MAX)
+	if (!pwcs)
 		return (-1);
+
+#if NETSCAPE_BUG
+	if (s && n >= 3)
+		s[2] = 0;
+	if (s && n >= 4)
+		s[3] = 0;
+	if (*pwcs & ~0xffff) {
+		fprintf(stderr, "Oops!  netscape bug is encountered.\n");
+		fprintf(stderr, "wcs %x\n", *pwcs);
+		strncpy(s, (char *)pwcs, n);
+		return s[n - 1]? n: strlen(s);
+	}
+#endif
 
-	nb = n;
-	cnt = 0;
-	while (nb > 0) {
+#if NETSCAPE_BUG
+	while (n > 0) {
+#else
+	while (n > 0 || !s) {
+#endif
 		if (*pwcs == 0) {
-			*s = 0;
+			if (s)
+				*s = 0;
 			break;
 		}
-		if (!sputrune(*pwcs++, s, nb, &e))
+		if (!sputrune(*pwcs++, s, s? n: MB_CUR_MAX, &e)) {
+			/* errno = EILSEQ; */
 			return (-1);		/* encoding error */
-		if (!e)			/* too long */
+		}
+		if (!e) {		/* too long */
+			if (s)
+				*s = 0;
 			return (cnt);
+		}
 		cnt += e - s;
-		nb -= e - s;
-		s = e;
+		n -= e - s;
+		if (s)
+			s = e;
 	}
 	return (cnt);
 }
Index: locale/big5.c
===================================================================
RCS file: /usr/home/cvs/src/lib/libc/locale/big5.c,v
retrieving revision 1.2
diff -u -r1.2 big5.c
--- big5.c	1998/08/15 20:53:34	1.2
+++ big5.c	1998/11/23 12:25:45
@@ -75,14 +75,14 @@
 	char const **result;
 {
 	rune_t rune = 0;
-	int len;
+	size_t len;
 
 	if (n < 1 || (len = _big5_check(*string)) > n) {
 		if (result)
 			*result = string;
 		return (_INVALID_RUNE);
 	}
-	while (len-- >= 0)
+	while (len-- > 0)
 		rune = (rune << 8) | ((u_int)(*string++) & 0xff);
 	if (result)
 		*result = string + len;
@@ -95,26 +95,22 @@
 	char *string, **result;
 	size_t n;
 {
-	if (c & 0x8000) {
-		if (n >= 2) {
-			string[0] = (c >> 8) & 0xff;
-			string[1] = c & 0xff;
-			if (result)
-				*result = string + 2;
-			return (2);
-		}
-	}
-	else {
-		if (n >= 1) {
-			*string = c & 0xff;
-			if (result)
-				*result = string + 1;
-			return (1);
-		}
+	size_t i, len;
+	
+	if (c & 0x8000)
+		i = len = 2;
+	else
+		i = len =1;
+	if (n >= len) {
+		if (string)
+			while (i-- > 0)
+				*string++ = (c >> (i << 3)) & 0xff;
+		if (result)
+			*result = string? string: (char *)len;
+		return len;
 	}
 	if (result)
-		*result = string;
-	return (0);
-	
+		*result = NULL;
+	return len;
 }
 #endif /* XPG4 */
Index: locale/euc.c
===================================================================
RCS file: /usr/home/cvs/src/lib/libc/locale/euc.c,v
retrieving revision 1.3
diff -u -r1.3 euc.c
--- euc.c	1998/01/14 08:14:56	1.3
+++ euc.c	1998/11/23 17:26:37
@@ -169,55 +169,61 @@
 {
 	rune_t m = c & CEI->mask;
 	rune_t nm = c & ~m;
-	int i, len;
+	size_t i, len;
 
 	if (m == CEI->bits[1]) {
 CodeSet1:
 		/* Codeset 1: The first byte must have 0x80 in it. */
 		i = len = CEI->count[1];
-		if (n >= len) {
+		if (n < len) {
 			if (result)
-				*result = string + len;
+				*result = NULL;
+			return (len);
+		}
+		if (string)
 			while (i-- > 0)
-				*string++ = (nm >> (i << 3)) | 0x80;
-		} else
+				*string++ = ((nm >> (i << 3)) & 0xff) | 0x80;
+	} else if (m == CEI->bits[0]) {
+		i = len = CEI->count[0];
+		if (n < len) {
 			if (result)
-				*result = (char *) 0;
+				*result = NULL;
+			return (len);
+		}
+		if (string)
+			while (i-- > 0)
+				*string++ = (nm >> (i << 3)) & 0xff;
 	} else {
-		if (m == CEI->bits[0]) {
-			i = len = CEI->count[0];
+		if (m == CEI->bits[2]) {
+			i = len = CEI->count[2];
 			if (n < len) {
 				if (result)
 					*result = NULL;
 				return (len);
 			}
-		} else
-			if (m == CEI->bits[2]) {
-				i = len = CEI->count[2];
-				if (n < len) {
-					if (result)
-						*result = NULL;
-					return (len);
-				}
+			if (string) {
 				*string++ = _SS2;
 				--i;
-			} else
-				if (m == CEI->bits[3]) {
-					i = len = CEI->count[3];
-					if (n < len) {
-						if (result)
-							*result = NULL;
-						return (len);
-					}
-					*string++ = _SS3;
-					--i;
-				} else
-					goto CodeSet1;	/* Bletch */
-		while (i-- > 0)
-			*string++ = (nm >> (i << 3)) & 0xff;
-		if (result)
-			*result = string;
+			}
+		} else if (m == CEI->bits[3]) {
+			i = len = CEI->count[3];
+			if (n < len) {
+				if (result)
+					*result = NULL;
+				return (len);
+			}
+			if (string) {
+				*string++ = _SS3;
+				--i;
+			}
+		} else
+			goto CodeSet1;	/* Bletch */
+		if (string)
+			while (i-- > 0)
+				*string++ = ((nm >> (i << 3)) & 0xff) | 0x80;
 	}
+	if (result)
+		*result = string? string: (char *)len;
 	return (len);
 }
 #endif  /* XPG4 */
Index: locale/mskanji.c
===================================================================
RCS file: /usr/home/cvs/src/lib/libc/locale/mskanji.c,v
retrieving revision 1.2
diff -u -r1.2 mskanji.c
--- mskanji.c	1997/09/25 23:20:26	1.2
+++ mskanji.c	1998/11/22 17:12:05
@@ -98,9 +98,10 @@
 		if ( result ) *result = (char *) 0;
 	} else {
 		if ( result ) *result = string + len;
-		for ( i = len; i-- > 0; ) {
-			*( string++ ) = c >> ( i << 3 );
-		}
+		if (string)
+			for ( i = len; i-- > 0; ) {
+				*( string++ ) = c >> ( i << 3 );
+			}
 	}
 	return len;
 }
