chiark / gitweb /
Introduce adns__labels_equal and ctype_toupper
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 24 Jul 2014 00:46:09 +0000 (01:46 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 19 Oct 2014 20:14:55 +0000 (21:14 +0100)
This not only makes adns__findrr_anychk less confusing, but it also
makes it possible to reuse this functionality.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/internal.h
src/parse.c

index 0b39418433c859b464aa2c19606769e5dd728e0c..18aabf48b3fab177ee6ea7565cbd6e865c8e97d7 100644 (file)
@@ -39,6 +39,7 @@ typedef unsigned char byte;
 #include <errno.h>
 #include <string.h>
 #include <stdlib.h>
+#include <stdbool.h>
 
 #include <sys/time.h>
 
@@ -865,6 +866,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);
@@ -913,6 +916,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;
 }
index 1d5a993a09c17b46e7388eebd41ba9628aea7908..20853dda22abfa73aef53827d5df84200bbe9415 100644 (file)
@@ -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;
   }