udhcp: code shrink
[oweals/busybox.git] / networking / udhcp / domain_codec.c
1 /* vi: set sw=4 ts=4: */
2
3 /* RFC1035 domain compression routines (C) 2007 Gabriel Somlo <somlo at cmu.edu>
4  *
5  * Loosely based on the isc-dhcpd implementation by dhankins@isc.org
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 #if ENABLE_FEATURE_UDHCP_RFC3397
11
12 #include "common.h"
13
14 #define NS_MAXDNAME  1025       /* max domain name length */
15 #define NS_MAXCDNAME  255       /* max compressed domain name length */
16 #define NS_MAXLABEL    63       /* max label length */
17 #define NS_MAXDNSRCH    6       /* max domains in search path */
18 #define NS_CMPRSFLGS 0xc0       /* name compression pointer flag */
19
20
21 /* expand a RFC1035-compressed list of domain names "cstr", of length "clen";
22  * returns a newly allocated string containing the space-separated domains,
23  * prefixed with the contents of string pre, or NULL if an error occurs.
24  */
25 char* FAST_FUNC dname_dec(const uint8_t *cstr, int clen, const char *pre)
26 {
27         char *ret = ret; /* for compiler */
28         char *dst = NULL;
29
30         /* We make two passes over the cstr string. First, we compute
31          * how long the resulting string would be. Then we allocate a
32          * new buffer of the required length, and fill it in with the
33          * expanded content. The advantage of this approach is not
34          * having to deal with requiring callers to supply their own
35          * buffer, then having to check if it's sufficiently large, etc.
36          */
37         while (1) {
38                 /* note: "return NULL" below are leak-safe since
39                  * dst isn't yet allocated */
40                 const uint8_t *c;
41                 unsigned crtpos, retpos, depth, len;
42
43                 crtpos = retpos = depth = len = 0;
44                 while (crtpos < clen) {
45                         c = cstr + crtpos;
46
47                         if (*c & NS_CMPRSFLGS) {
48                                 /* pointer */
49                                 if (crtpos + 2 > clen) /* no offset to jump to? abort */
50                                         return NULL;
51                                 if (retpos == 0) /* toplevel? save return spot */
52                                         retpos = crtpos + 2;
53                                 depth++;
54                                 crtpos = ((c[0] & 0x3f) << 8) | (c[1] & 0xff); /* jump */
55                         } else if (*c) {
56                                 /* label */
57                                 if (crtpos + *c + 1 > clen) /* label too long? abort */
58                                         return NULL;
59                                 if (dst)
60                                         memcpy(dst + len, c + 1, *c);
61                                 len += *c + 1;
62                                 crtpos += *c + 1;
63                                 if (dst)
64                                         dst[len - 1] = '.';
65                         } else {
66                                 /* null: end of current domain name */
67                                 if (retpos == 0) {
68                                         /* toplevel? keep going */
69                                         crtpos++;
70                                 } else {
71                                         /* return to toplevel saved spot */
72                                         crtpos = retpos;
73                                         retpos = depth = 0;
74                                 }
75                                 if (dst)
76                                         dst[len - 1] = ' ';
77                         }
78
79                         if (depth > NS_MAXDNSRCH /* too many jumps? abort, it's a loop */
80                          || len > NS_MAXDNAME * NS_MAXDNSRCH /* result too long? abort */
81                         ) {
82                                 return NULL;
83                         }
84                 }
85
86                 if (!len) /* expanded string has 0 length? abort */
87                         return NULL;
88
89                 if (!dst) { /* first pass? */
90                         /* allocate dst buffer and copy pre */
91                         unsigned plen = strlen(pre);
92                         ret = dst = xmalloc(plen + len);
93                         memcpy(dst, pre, plen);
94                         dst += plen;
95                 } else {
96                         dst[len - 1] = '\0';
97                         break;
98                 }
99         }
100
101         return ret;
102 }
103
104 /* Convert a domain name (src) from human-readable "foo.blah.com" format into
105  * RFC1035 encoding "\003foo\004blah\003com\000". Return allocated string, or
106  * NULL if an error occurs.
107  */
108 static uint8_t *convert_dname(const char *src)
109 {
110         uint8_t c, *res, *lp, *rp;
111         int len;
112
113         res = xmalloc(strlen(src) + 2);
114         rp = lp = res;
115         rp++;
116
117         for (;;) {
118                 c = (uint8_t)*src++;
119                 if (c == '.' || c == '\0') {    /* end of label */
120                         len = rp - lp - 1;
121                         /* label too long, too short, or two '.'s in a row? abort */
122                         if (len > NS_MAXLABEL || len == 0 || (c == '.' && *src == '.')) {
123                                 free(res);
124                                 return NULL;
125                         }
126                         *lp = len;
127                         lp = rp++;
128                         if (c == '\0' || *src == '\0')  /* end of dname */
129                                 break;
130                 } else {
131                         if (c >= 0x41 && c <= 0x5A)             /* uppercase? convert to lower */
132                                 c += 0x20;
133                         *rp++ = c;
134                 }
135         }
136
137         *lp = 0;
138         if (rp - res > NS_MAXCDNAME) {  /* dname too long? abort */
139                 free(res);
140                 return NULL;
141         }
142         return res;
143 }
144
145 /* returns the offset within cstr at which dname can be found, or -1
146  */
147 static int find_offset(const uint8_t *cstr, int clen, const uint8_t *dname)
148 {
149         const uint8_t *c, *d;
150         int off, inc;
151
152         /* find all labels in cstr */
153         off = 0;
154         while (off < clen) {
155                 c = cstr + off;
156
157                 if ((*c & NS_CMPRSFLGS) != 0) { /* pointer, skip */
158                         off += 2;
159                 } else if (*c) {        /* label, try matching dname */
160                         inc = *c + 1;
161                         d = dname;
162                         while (*c == *d && memcmp(c + 1, d + 1, *c) == 0) {
163                                 if (*c == 0)    /* match, return offset */
164                                         return off;
165                                 d += *c + 1;
166                                 c += *c + 1;
167                                 if ((*c & NS_CMPRSFLGS) != 0)   /* pointer, jump */
168                                         c = cstr + (((*c & 0x3f) << 8) | (*(c + 1) & 0xff));
169                         }
170                         off += inc;
171                 } else {        /* null, skip */
172                         off++;
173                 }
174         }
175
176         return -1;
177 }
178
179 /* computes string to be appended to cstr so that src would be added to
180  * the compression (best case, it's a 2-byte pointer to some offset within
181  * cstr; worst case, it's all of src, converted to rfc3011 format).
182  * The computed string is returned directly; its length is returned via retlen;
183  * NULL and 0, respectively, are returned if an error occurs.
184  */
185 uint8_t* FAST_FUNC dname_enc(const uint8_t *cstr, int clen, const char *src, int *retlen)
186 {
187         uint8_t *d, *dname;
188         int off;
189
190         dname = convert_dname(src);
191         if (dname == NULL) {
192                 *retlen = 0;
193                 return NULL;
194         }
195
196         for (d = dname; *d != 0; d += *d + 1) {
197                 off = find_offset(cstr, clen, d);
198                 if (off >= 0) { /* found a match, add pointer and terminate string */
199                         *d++ = NS_CMPRSFLGS;
200                         *d = off;
201                         break;
202                 }
203         }
204
205         *retlen = d - dname + 1;
206         return dname;
207 }
208
209 #endif /* ENABLE_FEATURE_UDHCP_RFC3397 */