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 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

View file

@ -571,7 +571,7 @@ odomainlist(uchar *w, int n, Otab *o, Req *q)
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++)

View file

@ -1591,7 +1591,7 @@ dnsnamesopt(Req *rp, int t, char *attr, Ndbtuple *nt)
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++){

View file

@ -305,7 +305,7 @@ lookupname(char *val, int len, Ndbtuple *t)
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;

View file

@ -350,7 +350,7 @@ main(int argc, char **argv)
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 @@ Ufmt(Fmt *f)
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 @@ ndb2conf(Ndb *db, uchar *myip)
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;

View file

@ -262,7 +262,7 @@ dblookup1(char *name, int type, int auth, int ttl)
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 @@ dblookup1(char *name, int type, int auth, int ttl)
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);

View file

@ -228,7 +228,7 @@ idnlookup(char *name, int class, int enter)
{
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 @@ idnname(DN *dn, char *buf, int nbuf)
char *name;
name = dnname(dn);
if(idn2utf(name, buf, nbuf) != nil)
if(idn2utf(name, buf, nbuf) >= 0)
return buf;
return name;
}

View file

@ -174,10 +174,10 @@ main(int argc, char *argv[])
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){

View file

@ -76,7 +76,7 @@ Nfmt(Fmt *f)
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 @@ Out:
if(s = u->host){
t = emalloc(Domlen);
if(idn2utf(s, t, Domlen)){
if(idn2utf(s, t, Domlen) >= 0){
u->host = estrdup(t);
free(s);
}

View file

@ -184,7 +184,7 @@ punydecode(uint input_length, char input[], uint max_out, Rune output[])
* convert punycode encoded internationalized
* domain name to unicode string
*/
char*
int
idn2utf(char *name, char *buf, int nbuf)
{
char *dp, *de, *cp;
@ -205,24 +205,24 @@ idn2utf(char *name, char *buf, int nbuf)
}
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++ = '.';
cp += nc+1;
}
*dp = 0;
return buf;
return dp - buf;
}
/*
* convert unicode string to punycode
* encoded internationalized domain name
*/
char*
int
utf2idn(char *name, char *buf, int nbuf)
{
char *dp, *de, *cp;
@ -246,17 +246,17 @@ utf2idn(char *name, char *buf, int nbuf)
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++ = '.';
cp += nc+1;
}
*dp = 0;
return buf;
return dp - buf;
}