From: Ian Jackson Date: Thu, 24 Jul 2014 00:46:09 +0000 (+0100) Subject: Introduce adns__labels_equal and ctype_toupper X-Git-Tag: make-bug.2014-07-26~4 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=adns.git;a=commitdiff_plain;h=e734125afd36224ccbf7e7208f7c3d9aea4fa9be Introduce adns__labels_equal and ctype_toupper This not only makes adns__findrr_anychk less confusing, but it also makes it possible to reuse this functionality. Signed-off-by: Ian Jackson --- diff --git a/src/internal.h b/src/internal.h index c2656c8..55da2b9 100644 --- a/src/internal.h +++ b/src/internal.h @@ -851,6 +851,8 @@ void adns__update_expires(adns_query qu, unsigned long ttl, int vbuf__append_quoted1035(vbuf *vb, const byte *buf, int len); +bool adns__labels_equal(const byte *a, int al, const byte *b, int bl); + /* From event.c: */ void adns__tcp_broken(adns_state ads, const char *what, const char *why); @@ -898,6 +900,9 @@ static inline int ctype_digit(int c) { return c>='0' && c<='9'; } static inline int ctype_alpha(int c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } +static inline int ctype_toupper(int c) { + return ctype_alpha(c) ? (c & ~32) : c; +} static inline int ctype_822special(int c) { return strchr("()<>@,;:\\\".[]",c) != 0; } diff --git a/src/parse.c b/src/parse.c index 1d5a993..20853dd 100644 --- a/src/parse.c +++ b/src/parse.c @@ -160,7 +160,17 @@ adns_status adns__parse_domain_more(findlabel_state *fls, adns_state ads, if (!adns__vbuf_append(vb,"",1)) return adns_s_nomemory; return adns_s_ok; } - + +bool adns__labels_equal(const byte *a, int al, const byte *b, int bl) { + if (al != bl) return 0; + while (al-- > 0) { + int ac= ctype_toupper(*a++); + int bc= ctype_toupper(*b++); + if (ac != bc) return 0; + } + return 1; +} + adns_status adns__findrr_anychk(adns_query qu, int serv, const byte *dgram, int dglen, int *cbyte_io, int *type_r, int *class_r, @@ -174,8 +184,8 @@ adns_status adns__findrr_anychk(adns_query qu, int serv, int tmp, rdlen; unsigned long ttl; - int lablen, labstart, ch; - int eo_lablen, eo_labstart, eo_ch; + int lablen, labstart; + int eo_lablen, eo_labstart; adns_status st; cbyte= *cbyte_io; @@ -197,12 +207,9 @@ adns_status adns__findrr_anychk(adns_query qu, int serv, if (eo_fls) { st= adns__findlabel_next(eo_fls,&eo_lablen,&eo_labstart); assert(!st); assert(eo_lablen>=0); - if (lablen != eo_lablen) eo_fls= 0; - while (eo_fls && eo_lablen-- > 0) { - ch= dgram[labstart++]; if (ctype_alpha(ch)) ch &= ~32; - eo_ch= eo_dgram[eo_labstart++]; if (ctype_alpha(eo_ch)) eo_ch &= ~32; - if (ch != eo_ch) eo_fls= 0; - } + if (!adns__labels_equal(dgram+labstart, lablen, + eo_dgram+eo_labstart, eo_lablen)) + eo_fls= 0; } if (!lablen) break; }