--- pdns-2.9.4-stable/pdns/dnspacket.cc	Wed Jan  8 17:07:25 2003
+++ pdns-2.9.4-mark5/pdns/dnspacket.cc	Mon Jan 20 00:00:57 2003
@@ -1352,7 +1352,7 @@
 	break;
       }
       else {
-	if (strcmp(p, label.data()) == 0)
+	if (strncmp(p, label.data(), label.size()) == 0)
 	  return (p - data);
 	p += (*p + 1);
       }
@@ -1379,7 +1379,7 @@
 	}
       else
 	{
-	  if (strcmp(p, label.data()) == 0)
+	  if (strncmp(p, label.data(), label.size()) == 0)
 	    {
 	      return (p - data);
 	    }
@@ -1418,7 +1418,7 @@
 	}
 	else {
 
-	  if (strcmp(p, label.data()) == 0) {
+	  if (strncmp(p, label.data(), label.size()) == 0) {
 	    return (p - data);
 	  }
 		   
@@ -1447,28 +1447,59 @@
     //  ns1.norad.org
     //  norad.org
     //  org
+    //
+    //	recursive function:	( N = qname.length() )
+    //	G.N	= ""
+    //	G.i	= offset( qname[i..N) )
+    //				if offset(qname[i..N)) >= 0
+    //	G.i	= qname[i..i+qname[i]+1) ++ G.(i + qname[i]+1)
+    //				if offset(qname[i..N)) == -1
+    //	G.i	= qname[i..N)	if qname[i] = "offset"
+    //	
+    //	Q: G.0 = qname
+    //	
+    //  (tail)invariants:
+    //  P0: G.0 = qname[0..i) ++ G.i
+    //  P1: 0 <= i <= qname.length()
     
-    int i = 0;
+    int i = 0;	/* P0 /\ P1 */
+    bool containsptr = false;
     
-    while (qname[i] != 0x00) {
+    while (qname[i] != 0x00 &&	/* qname[i] == 0x00 => i == qname.length */
+	(qname[i] & 0xC0) != 0xC0) {	// no use to try to compress offsets
       // Get a portion of the name
       
-      string s = qname.substr(i);
+      // qname must include an extra trailing '\0' if it's prefix
+      // is not an offset ptr
+      string s = qname.substr(i);	/* s == qname[i..N) */
+      if (!containsptr) s = s + '\0';
 
       // Did we see this before?
       int offset = findlabel(s);
+      /* offset == offset(qname[i..N)) \/ offset == -1 */
       
-      if ( offset != -1) {
+      if ( offset != -1) {		/* offset != -1 */
 
 	qname[i + 0] = (char) (((offset | 0xC000) & 0x0000FF00) >> 8);
 	qname[i + 1] = (char)  ((offset | 0xC000) & 0x000000FF);
 	qname = qname.substr(0, i + 2); // XX setlength() ?
+	containsptr = true;
 	// qname now consists of unique prefix+known suffix (on location 'offset')
-	break;
+	
+	// we managed to make qname shorter, maybe we can do that again
+	
+	/* P0(i := 0) /\ P1 */
+	i = 0;
+	/* P0 /\ P1 */
       }
-      // Move to the next label
-      i += (qname[i] + 1); // doesn't quite handle very long labels
-    }
+      else {				/* offset == -1 */
+      	// Move to the next label
+	
+	/* P0(i := i + qname[i] + 1) /\ P1) */
+      	i += (qname[i] + 1); // doesn't quite handle very long labels
+	/* P0 /\ P1 */
+      }
+    } /* P0 /\ P1 /\ (i == qname.length() \/ qname[i] == "offset") => Q */
   }
   
   return qname.length();

