udhcp: add PXELINUX config file option (code 209) definition
[oweals/busybox.git] / networking / udhcp / domain_codec.c
index c81372ad29274717b5cf69e97e75cd478c7e5eda..c1325d8be947ce6a9ba13dc88be75854dde3403b 100644 (file)
@@ -4,10 +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.
  */
-
-#include "common.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 */
@@ -16,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.
  */
@@ -141,8 +149,7 @@ static uint8_t *convert_dname(const char *src)
        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;
@@ -180,7 +187,7 @@ static int find_offset(const uint8_t *cstr, int clen, const uint8_t *dname)
        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 <4>host<3>com<0> format).
  * The computed string is returned directly; its length is returned via retlen;
@@ -213,3 +220,28 @@ uint8_t* FAST_FUNC dname_enc(const uint8_t *cstr, int clen, const char *src, int
        *retlen = d - dname + 1;
        return dname;
 }
+
+#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