shithub: riscv

Download patch

ref: 311e3b51c6b1bde2f58968453de56a5ca2a6dbb4
parent: f18e8dfde88d00a19a184604865aa4d6383b16f5
author: cinap_lenrek <[email protected]>
date: Wed Sep 26 10:32:17 EDT 2018

libc: return number of bytes produced for idn2utf() and utf2idn()

--- a/sys/include/libc.h
+++ b/sys/include/libc.h
@@ -527,8 +527,8 @@
 extern	NetConnInfo*	getnetconninfo(char*, int);
 extern	void		freenetconninfo(NetConnInfo*);
 
-extern	char*	idn2utf(char*, char*, int);
-extern	char*	utf2idn(char*, char*, int);
+extern	int	idn2utf(char*, char*, int);
+extern	int	utf2idn(char*, char*, int);
 
 /*
  * system calls
--- a/sys/src/cmd/ip/dhcp6d.c
+++ b/sys/src/cmd/ip/dhcp6d.c
@@ -571,7 +571,7 @@
 	for(t = q->t; t != nil; t = t->entry){
 		if(strcmp(t->attr, o->q[0]) != 0)
 			continue;
-		if(utf2idn(t->val, val, sizeof(val)) == nil)
+		if(utf2idn(t->val, val, sizeof(val)) <= 0)
 			continue;
 		for(s = val; *s != 0; s++){
 			for(l = 0; *s != 0 && *s != '.'; l++)
--- a/sys/src/cmd/ip/dhcpd/dhcpd.c
+++ b/sys/src/cmd/ip/dhcpd/dhcpd.c
@@ -1591,7 +1591,7 @@
 	for(; nt != nil; nt = nt->entry){
 		if(strcmp(nt->attr, attr) != 0)
 			continue;
-		if(utf2idn(nt->val, val, sizeof(val)) == nil)
+		if(utf2idn(nt->val, val, sizeof(val)) <= 0)
 			continue;
 		d = &rp->p[2];
 		for(s = val; *s != 0; s++){
--- a/sys/src/cmd/ip/dhcpd/ndb.c
+++ b/sys/src/cmd/ip/dhcpd/ndb.c
@@ -305,7 +305,7 @@
 
 	for(nt = t; nt != nil; nt = nt->entry)
 		if(strcmp(nt->attr, "dom") == 0){
-			if(utf2idn(nt->val, val, len) == nil)
+			if(utf2idn(nt->val, val, len) < 0)
 				strncpy(val, nt->val, len-1);
 			val[len-1] = 0;
 			break;
--- a/sys/src/cmd/ip/ipconfig/main.c
+++ b/sys/src/cmd/ip/ipconfig/main.c
@@ -350,7 +350,7 @@
 		plan9 = 0;
 		break;
 	case 'h':
-		if(utf2idn(EARGF(usage()), conf.hostname, sizeof(conf.hostname)) == nil)
+		if(utf2idn(EARGF(usage()), conf.hostname, sizeof(conf.hostname)) <= 0)
 			sysfatal("bad hostname");
 		sendhostname = 1;
 		break;
@@ -980,7 +980,7 @@
 	char d[256], *s;
 
 	s = va_arg(f->args, char*);
-	if(idn2utf(s, d, sizeof(d)) != nil)
+	if(idn2utf(s, d, sizeof(d)) >= 0)
 		s = d;
 	fmtprint(f, "%s", s);
 	return 0;
@@ -1040,7 +1040,7 @@
 	t = ndbipinfo(db, "ip", val, attrs, nattr);
 	for(nt = t; nt != nil; nt = nt->entry) {
 		if(strcmp(nt->attr, "dnsdomain") == 0) {
-			if(utf2idn(nt->val, val, sizeof(val)) == nil)
+			if(utf2idn(nt->val, val, sizeof(val)) <= 0)
 				continue;
 			addnames(conf.dnsdomain, val, sizeof(conf.dnsdomain));
 			continue;
--- a/sys/src/cmd/ndb/dblookup.c
+++ b/sys/src/cmd/ndb/dblookup.c
@@ -262,7 +262,7 @@
 	for(x=0; x<4; x++){
 		switch(x){
 		case 1:	/* try unicode */
-			if(idn2utf(name, dname, sizeof dname) == nil){
+			if(idn2utf(name, dname, sizeof dname) < 0){
 				nstrcpy(dname, name, sizeof dname);
 				continue;
 			}
@@ -270,7 +270,7 @@
 				continue;
 			break;
 		case 3:	/* try ascii (lower case) */
-			if(utf2idn(name, dname, sizeof dname) == nil)
+			if(utf2idn(name, dname, sizeof dname) < 0)
 				continue;
 		case 2:
 			mklowcase(dname);
--- a/sys/src/cmd/ndb/dn.c
+++ b/sys/src/cmd/ndb/dn.c
@@ -228,7 +228,7 @@
 {
 	char dom[Domlen];
 
-	if(utf2idn(name, dom, sizeof dom) != nil)
+	if(utf2idn(name, dom, sizeof dom) >= 0)
 		name = dom;
 	return dnlookup(name, class, enter);
 }
@@ -1172,7 +1172,7 @@
 	char *name;
 
 	name = dnname(dn);
-	if(idn2utf(name, buf, nbuf) != nil)
+	if(idn2utf(name, buf, nbuf) >= 0)
 		return buf;
 	return name;
 }
--- a/sys/src/cmd/ndb/inform.c
+++ b/sys/src/cmd/ndb/inform.c
@@ -174,10 +174,10 @@
 		sysfatal("no relevant dnsdomain=");
 
 
-	if(utf2idn(dom, dn, sizeof(dn)) == nil)
+	if(utf2idn(dom, dn, sizeof(dn)) <= 0)
 		sysfatal("cannot convert dom");
 
-	if(utf2idn(dnsdomain, ds, sizeof(ds)) == nil)
+	if(utf2idn(dnsdomain, ds, sizeof(ds)) <= 0)
 		sysfatal("cannot convert dnsdomain");
 
 	if(debug){
--- a/sys/src/cmd/webfs/url.c
+++ b/sys/src/cmd/webfs/url.c
@@ -76,7 +76,7 @@
 	char d[Domlen], *s;
 
 	s = va_arg(f->args, char*);
-	if(utf2idn(s, d, sizeof(d)) != nil)
+	if(utf2idn(s, d, sizeof(d)) >= 0)
 		s = d;
 	fmtprint(f, "%s", s);
 	return 0;
@@ -320,7 +320,7 @@
 
 	if(s = u->host){
 		t = emalloc(Domlen);
-		if(idn2utf(s, t, Domlen)){
+		if(idn2utf(s, t, Domlen) >= 0){
 			u->host = estrdup(t);
 			free(s);
 		}
--- a/sys/src/libc/9sys/idn.c
+++ b/sys/src/libc/9sys/idn.c
@@ -184,7 +184,7 @@
  * convert punycode encoded internationalized
  * domain name to unicode string
  */
-char*
+int
 idn2utf(char *name, char *buf, int nbuf)
 {
 	char *dp, *de, *cp;
@@ -205,10 +205,10 @@
 		}
 		if(cistrncmp(cp, "xn--", 4) == 0)
 			if((nr = punydecode(nc-4, cp+4, nelem(rb), rb)) < 0)
-				return nil;
+				return -1;
 		dp = seprint(dp, de, "%.*S", nr, rb);
 		if(dp >= de)
-			return nil;
+			return -1;
 		if(cp[nc] == 0)
 			break;
 		*dp++ = '.';
@@ -215,7 +215,7 @@
 		cp += nc+1;
 	}
 	*dp = 0;
-	return buf;
+	return dp - buf;
 }
 
 /*
@@ -222,7 +222,7 @@
  * convert unicode string to punycode
  * encoded internationalized domain name
  */
-char*
+int
 utf2idn(char *name, char *buf, int nbuf)
 {
 	char *dp, *de, *cp;
@@ -246,11 +246,11 @@
 		else {
 			dp = seprint(dp, de, "xn--");
 			if((n = punyencode(nr, rb, de - dp, dp)) < 0)
-				return nil;
+				return -1;
 			dp += n;
 		}
 		if(dp >= de)
-			return nil;
+			return -1;
 		if(cp[nc] == 0)
 			break;
 		*dp++ = '.';
@@ -257,6 +257,6 @@
 		cp += nc+1;
 	}
 	*dp = 0;
-	return buf;
+	return dp - buf;
 }