udhcp: add PXELINUX config file option (code 209) definition
[oweals/busybox.git] / networking / udhcp / domain_codec.c
index 45354e727181c365bd51739b1c9027c0370e239f..c1325d8be947ce6a9ba13dc88be75854dde3403b 100644 (file)
@@ -4,13 +4,18 @@
  *
  * Loosely based on the isc-dhcpd implementation by dhankins@isc.org
  *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
-
-#if ENABLE_FEATURE_UDHCP_RFC3397
-
-#include "common.h"
-#include "options.h"
+#ifdef DNS_COMPR_TESTING
+# define FAST_FUNC /* nothing */
+# define xmalloc malloc
+# include <stdlib.h>
+# include <stdint.h>
+# include <string.h>
+# include <stdio.h>
+#else
+# include "common.h"
+#endif
 
 #define NS_MAXDNAME  1025      /* max domain name length */
 #define NS_MAXCDNAME  255      /* max compressed domain name length */
@@ -19,7 +24,7 @@
 #define NS_CMPRSFLGS 0xc0      /* name compression pointer flag */
 
 
-/* expand a RFC1035-compressed list of domain names "cstr", of length "clen";
+/* Expand a RFC1035-compressed list of domain names "cstr", of length "clen";
  * returns a newly allocated string containing the space-separated domains,
  * prefixed with the contents of string pre, or NULL if an error occurs.
  */
@@ -45,14 +50,14 @@ char* FAST_FUNC dname_dec(const uint8_t *cstr, int clen, const char *pre)
                while (crtpos < clen) {
                        c = cstr + crtpos;
 
-                       if (*c & NS_CMPRSFLGS) {
+                       if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
                                /* pointer */
                                if (crtpos + 2 > clen) /* no offset to jump to? abort */
                                        return NULL;
                                if (retpos == 0) /* toplevel? save return spot */
                                        retpos = crtpos + 2;
                                depth++;
-                               crtpos = ((c[0] & 0x3f) << 8) | (c[1] & 0xff); /* jump */
+                               crtpos = ((c[0] & 0x3f) << 8) | c[1]; /* jump */
                        } else if (*c) {
                                /* label */
                                if (crtpos + *c + 1 > clen) /* label too long? abort */
@@ -64,7 +69,7 @@ char* FAST_FUNC dname_dec(const uint8_t *cstr, int clen, const char *pre)
                                if (dst)
                                        dst[len - 1] = '.';
                        } else {
-                               /* null: end of current domain name */
+                               /* NUL: end of current domain name */
                                if (retpos == 0) {
                                        /* toplevel? keep going */
                                        crtpos++;
@@ -108,78 +113,83 @@ char* FAST_FUNC dname_dec(const uint8_t *cstr, int clen, const char *pre)
  */
 static uint8_t *convert_dname(const char *src)
 {
-       uint8_t c, *res, *lp, *rp;
+       uint8_t c, *res, *lenptr, *dst;
        int len;
 
        res = xmalloc(strlen(src) + 2);
-       rp = lp = res;
-       rp++;
+       dst = lenptr = res;
+       dst++;
 
        for (;;) {
                c = (uint8_t)*src++;
-               if (c == '.' || c == '\0') {    /* end of label */
-                       len = rp - lp - 1;
+               if (c == '.' || c == '\0') {  /* end of label */
+                       len = dst - lenptr - 1;
                        /* label too long, too short, or two '.'s in a row? abort */
                        if (len > NS_MAXLABEL || len == 0 || (c == '.' && *src == '.')) {
                                free(res);
                                return NULL;
                        }
-                       *lp = len;
-                       lp = rp++;
-                       if (c == '\0' || *src == '\0')  /* end of dname */
+                       *lenptr = len;
+                       if (c == '\0' || *src == '\0')  /* "" or ".": end of src */
                                break;
-               } else {
-                       if (c >= 0x41 && c <= 0x5A)             /* uppercase? convert to lower */
-                               c += 0x20;
-                       *rp++ = c;
+                       lenptr = dst++;
+                       continue;
                }
+               if (c >= 'A' && c <= 'Z')  /* uppercase? convert to lower */
+                       c += ('a' - 'A');
+               *dst++ = c;
        }
 
-       *lp = 0;
-       if (rp - res > NS_MAXCDNAME) {  /* dname too long? abort */
+       if (dst - res >= NS_MAXCDNAME) {  /* dname too long? abort */
                free(res);
                return NULL;
        }
+
+       *dst = 0;
        return res;
 }
 
-/* returns the offset within cstr at which dname can be found, or -1
- */
+/* Returns the offset within cstr at which dname can be found, or -1 */
 static int find_offset(const uint8_t *cstr, int clen, const uint8_t *dname)
 {
        const uint8_t *c, *d;
-       int off, inc;
+       int off;
 
        /* find all labels in cstr */
        off = 0;
        while (off < clen) {
                c = cstr + off;
 
-               if ((*c & NS_CMPRSFLGS) != 0) { /* pointer, skip */
+               if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) {  /* pointer, skip */
                        off += 2;
-               } else if (*c) {        /* label, try matching dname */
-                       inc = *c + 1;
+                       continue;
+               }
+               if (*c) {  /* label, try matching dname */
                        d = dname;
-                       while (*c == *d && memcmp(c + 1, d + 1, *c) == 0) {
-                               if (*c == 0)    /* match, return offset */
+                       while (1) {
+                               unsigned len1 = *c + 1;
+                               if (memcmp(c, d, len1) != 0)
+                                       break;
+                               if (len1 == 1)  /* at terminating NUL - match, return offset */
                                        return off;
-                               d += *c + 1;
-                               c += *c + 1;
-                               if ((*c & NS_CMPRSFLGS) != 0)   /* pointer, jump */
-                                       c = cstr + (((*c & 0x3f) << 8) | (*(c + 1) & 0xff));
+                               d += len1;
+                               c += len1;
+                               if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS)  /* pointer, jump */
+                                       c = cstr + (((c[0] & 0x3f) << 8) | c[1]);
                        }
-                       off += inc;
-               } else {        /* null, skip */
-                       off++;
+                       off += cstr[off] + 1;
+                       continue;
                }
+               /* NUL, skip */
+               off++;
        }
 
        return -1;
 }
 
-/* computes string to be appended to cstr so that src would be added to
+/* Computes string to be appended to cstr so that src would be added to
  * the compression (best case, it's a 2-byte pointer to some offset within
- * cstr; worst case, it's all of src, converted to rfc3011 format).
+ * cstr; worst case, it's all of src, converted to <4>host<3>com<0> format).
  * The computed string is returned directly; its length is returned via retlen;
  * NULL and 0, respectively, are returned if an error occurs.
  */
@@ -194,17 +204,44 @@ uint8_t* FAST_FUNC dname_enc(const uint8_t *cstr, int clen, const char *src, int
                return NULL;
        }
 
-       for (d = dname; *d != 0; d += *d + 1) {
-               off = find_offset(cstr, clen, d);
-               if (off >= 0) { /* found a match, add pointer and terminate string */
-                       *d++ = NS_CMPRSFLGS;
-                       *d = off;
-                       break;
+       d = dname;
+       while (*d) {
+               if (cstr) {
+                       off = find_offset(cstr, clen, d);
+                       if (off >= 0) { /* found a match, add pointer and return */
+                               *d++ = NS_CMPRSFLGS | (off >> 8);
+                               *d = off;
+                               break;
+                       }
                }
+               d += *d + 1;
        }
 
        *retlen = d - dname + 1;
        return dname;
 }
 
-#endif /* ENABLE_FEATURE_UDHCP_RFC3397 */
+#ifdef DNS_COMPR_TESTING
+/* gcc -Wall -DDNS_COMPR_TESTING domain_codec.c -o domain_codec && ./domain_codec */
+int main(int argc, char **argv)
+{
+       int len;
+       uint8_t *encoded;
+
+#define DNAME_DEC(encoded,pre) dname_dec((uint8_t*)(encoded), sizeof(encoded), (pre))
+       printf("'%s'\n",       DNAME_DEC("\4host\3com\0", "test1:"));
+       printf("test2:'%s'\n", DNAME_DEC("\4host\3com\0\4host\3com\0", ""));
+       printf("test3:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\0", ""));
+       printf("test4:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\5", ""));
+       printf("test5:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\5\1z\xC0\xA", ""));
+
+#define DNAME_ENC(cache,source,lenp) dname_enc((uint8_t*)(cache), sizeof(cache), (source), (lenp))
+       encoded = dname_enc(NULL, 0, "test.net", &len);
+       printf("test6:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
+       encoded = DNAME_ENC("\3net\0", "test.net", &len);
+       printf("test7:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
+       encoded = DNAME_ENC("\4test\3net\0", "test.net", &len);
+       printf("test8:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
+       return 0;
+}
+#endif