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

This commit is contained in:
cinap_lenrek 2018-09-26 14:32:17 +02:00
parent f18e8dfde8
commit 311e3b51c6
10 changed files with 24 additions and 24 deletions

View file

@ -527,8 +527,8 @@ struct NetConnInfo
extern NetConnInfo* getnetconninfo(char*, int); extern NetConnInfo* getnetconninfo(char*, int);
extern void freenetconninfo(NetConnInfo*); extern void freenetconninfo(NetConnInfo*);
extern char* idn2utf(char*, char*, int); extern int idn2utf(char*, char*, int);
extern char* utf2idn(char*, char*, int); extern int utf2idn(char*, char*, int);
/* /*
* system calls * system calls

View file

@ -571,7 +571,7 @@ odomainlist(uchar *w, int n, Otab *o, Req *q)
for(t = q->t; t != nil; t = t->entry){ for(t = q->t; t != nil; t = t->entry){
if(strcmp(t->attr, o->q[0]) != 0) if(strcmp(t->attr, o->q[0]) != 0)
continue; continue;
if(utf2idn(t->val, val, sizeof(val)) == nil) if(utf2idn(t->val, val, sizeof(val)) <= 0)
continue; continue;
for(s = val; *s != 0; s++){ for(s = val; *s != 0; s++){
for(l = 0; *s != 0 && *s != '.'; l++) for(l = 0; *s != 0 && *s != '.'; l++)

View file

@ -1591,7 +1591,7 @@ dnsnamesopt(Req *rp, int t, char *attr, Ndbtuple *nt)
for(; nt != nil; nt = nt->entry){ for(; nt != nil; nt = nt->entry){
if(strcmp(nt->attr, attr) != 0) if(strcmp(nt->attr, attr) != 0)
continue; continue;
if(utf2idn(nt->val, val, sizeof(val)) == nil) if(utf2idn(nt->val, val, sizeof(val)) <= 0)
continue; continue;
d = &rp->p[2]; d = &rp->p[2];
for(s = val; *s != 0; s++){ for(s = val; *s != 0; s++){

View file

@ -305,7 +305,7 @@ lookupname(char *val, int len, Ndbtuple *t)
for(nt = t; nt != nil; nt = nt->entry) for(nt = t; nt != nil; nt = nt->entry)
if(strcmp(nt->attr, "dom") == 0){ 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); strncpy(val, nt->val, len-1);
val[len-1] = 0; val[len-1] = 0;
break; break;

View file

@ -350,7 +350,7 @@ main(int argc, char **argv)
plan9 = 0; plan9 = 0;
break; break;
case 'h': 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"); sysfatal("bad hostname");
sendhostname = 1; sendhostname = 1;
break; break;
@ -980,7 +980,7 @@ Ufmt(Fmt *f)
char d[256], *s; char d[256], *s;
s = va_arg(f->args, char*); s = va_arg(f->args, char*);
if(idn2utf(s, d, sizeof(d)) != nil) if(idn2utf(s, d, sizeof(d)) >= 0)
s = d; s = d;
fmtprint(f, "%s", s); fmtprint(f, "%s", s);
return 0; return 0;
@ -1040,7 +1040,7 @@ ndb2conf(Ndb *db, uchar *myip)
t = ndbipinfo(db, "ip", val, attrs, nattr); t = ndbipinfo(db, "ip", val, attrs, nattr);
for(nt = t; nt != nil; nt = nt->entry) { for(nt = t; nt != nil; nt = nt->entry) {
if(strcmp(nt->attr, "dnsdomain") == 0) { if(strcmp(nt->attr, "dnsdomain") == 0) {
if(utf2idn(nt->val, val, sizeof(val)) == nil) if(utf2idn(nt->val, val, sizeof(val)) <= 0)
continue; continue;
addnames(conf.dnsdomain, val, sizeof(conf.dnsdomain)); addnames(conf.dnsdomain, val, sizeof(conf.dnsdomain));
continue; continue;

View file

@ -262,7 +262,7 @@ dblookup1(char *name, int type, int auth, int ttl)
for(x=0; x<4; x++){ for(x=0; x<4; x++){
switch(x){ switch(x){
case 1: /* try unicode */ case 1: /* try unicode */
if(idn2utf(name, dname, sizeof dname) == nil){ if(idn2utf(name, dname, sizeof dname) < 0){
nstrcpy(dname, name, sizeof dname); nstrcpy(dname, name, sizeof dname);
continue; continue;
} }
@ -270,7 +270,7 @@ dblookup1(char *name, int type, int auth, int ttl)
continue; continue;
break; break;
case 3: /* try ascii (lower case) */ case 3: /* try ascii (lower case) */
if(utf2idn(name, dname, sizeof dname) == nil) if(utf2idn(name, dname, sizeof dname) < 0)
continue; continue;
case 2: case 2:
mklowcase(dname); mklowcase(dname);

View file

@ -228,7 +228,7 @@ idnlookup(char *name, int class, int enter)
{ {
char dom[Domlen]; char dom[Domlen];
if(utf2idn(name, dom, sizeof dom) != nil) if(utf2idn(name, dom, sizeof dom) >= 0)
name = dom; name = dom;
return dnlookup(name, class, enter); return dnlookup(name, class, enter);
} }
@ -1172,7 +1172,7 @@ idnname(DN *dn, char *buf, int nbuf)
char *name; char *name;
name = dnname(dn); name = dnname(dn);
if(idn2utf(name, buf, nbuf) != nil) if(idn2utf(name, buf, nbuf) >= 0)
return buf; return buf;
return name; return name;
} }

View file

@ -174,10 +174,10 @@ main(int argc, char *argv[])
sysfatal("no relevant dnsdomain="); sysfatal("no relevant dnsdomain=");
if(utf2idn(dom, dn, sizeof(dn)) == nil) if(utf2idn(dom, dn, sizeof(dn)) <= 0)
sysfatal("cannot convert dom"); sysfatal("cannot convert dom");
if(utf2idn(dnsdomain, ds, sizeof(ds)) == nil) if(utf2idn(dnsdomain, ds, sizeof(ds)) <= 0)
sysfatal("cannot convert dnsdomain"); sysfatal("cannot convert dnsdomain");
if(debug){ if(debug){

View file

@ -76,7 +76,7 @@ Nfmt(Fmt *f)
char d[Domlen], *s; char d[Domlen], *s;
s = va_arg(f->args, char*); s = va_arg(f->args, char*);
if(utf2idn(s, d, sizeof(d)) != nil) if(utf2idn(s, d, sizeof(d)) >= 0)
s = d; s = d;
fmtprint(f, "%s", s); fmtprint(f, "%s", s);
return 0; return 0;
@ -320,7 +320,7 @@ Out:
if(s = u->host){ if(s = u->host){
t = emalloc(Domlen); t = emalloc(Domlen);
if(idn2utf(s, t, Domlen)){ if(idn2utf(s, t, Domlen) >= 0){
u->host = estrdup(t); u->host = estrdup(t);
free(s); free(s);
} }

View file

@ -184,7 +184,7 @@ punydecode(uint input_length, char input[], uint max_out, Rune output[])
* convert punycode encoded internationalized * convert punycode encoded internationalized
* domain name to unicode string * domain name to unicode string
*/ */
char* int
idn2utf(char *name, char *buf, int nbuf) idn2utf(char *name, char *buf, int nbuf)
{ {
char *dp, *de, *cp; char *dp, *de, *cp;
@ -205,24 +205,24 @@ idn2utf(char *name, char *buf, int nbuf)
} }
if(cistrncmp(cp, "xn--", 4) == 0) if(cistrncmp(cp, "xn--", 4) == 0)
if((nr = punydecode(nc-4, cp+4, nelem(rb), rb)) < 0) if((nr = punydecode(nc-4, cp+4, nelem(rb), rb)) < 0)
return nil; return -1;
dp = seprint(dp, de, "%.*S", nr, rb); dp = seprint(dp, de, "%.*S", nr, rb);
if(dp >= de) if(dp >= de)
return nil; return -1;
if(cp[nc] == 0) if(cp[nc] == 0)
break; break;
*dp++ = '.'; *dp++ = '.';
cp += nc+1; cp += nc+1;
} }
*dp = 0; *dp = 0;
return buf; return dp - buf;
} }
/* /*
* convert unicode string to punycode * convert unicode string to punycode
* encoded internationalized domain name * encoded internationalized domain name
*/ */
char* int
utf2idn(char *name, char *buf, int nbuf) utf2idn(char *name, char *buf, int nbuf)
{ {
char *dp, *de, *cp; char *dp, *de, *cp;
@ -246,17 +246,17 @@ utf2idn(char *name, char *buf, int nbuf)
else { else {
dp = seprint(dp, de, "xn--"); dp = seprint(dp, de, "xn--");
if((n = punyencode(nr, rb, de - dp, dp)) < 0) if((n = punyencode(nr, rb, de - dp, dp)) < 0)
return nil; return -1;
dp += n; dp += n;
} }
if(dp >= de) if(dp >= de)
return nil; return -1;
if(cp[nc] == 0) if(cp[nc] == 0)
break; break;
*dp++ = '.'; *dp++ = '.';
cp += nc+1; cp += nc+1;
} }
*dp = 0; *dp = 0;
return buf; return dp - buf;
} }