domain_codec: optimize dname_dec and convert_dname
dname_dec: now iterates over the packet only once. convert_dname: remove redundant checks and code shrink. While testing I've noticed that some of the tests didn't compile properly, so I fixed them. function old new delta dname_dec 286 267 -19 dname_enc 166 143 -23 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-42) Total: -42 bytes Signed-off-by: Martin Lewis <martin.lewis.x84@gmail.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
051665ef69
commit
4468c569f7
@ -10,10 +10,14 @@
|
|||||||
# define _GNU_SOURCE
|
# define _GNU_SOURCE
|
||||||
# define FAST_FUNC /* nothing */
|
# define FAST_FUNC /* nothing */
|
||||||
# define xmalloc malloc
|
# define xmalloc malloc
|
||||||
|
# define xzalloc(s) calloc(s, 1)
|
||||||
|
# define xstrdup strdup
|
||||||
|
# define xrealloc realloc
|
||||||
# include <stdlib.h>
|
# include <stdlib.h>
|
||||||
# include <stdint.h>
|
# include <stdint.h>
|
||||||
# include <string.h>
|
# include <string.h>
|
||||||
# include <stdio.h>
|
# include <stdio.h>
|
||||||
|
# include <ctype.h>
|
||||||
#else
|
#else
|
||||||
# include "common.h"
|
# include "common.h"
|
||||||
#endif
|
#endif
|
||||||
@ -26,48 +30,44 @@
|
|||||||
|
|
||||||
|
|
||||||
/* 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,
|
* return a newly allocated string containing the space-separated domains,
|
||||||
* prefixed with the contents of string pre, or NULL if an error occurs.
|
* prefixed with the contents of string pre, or NULL if an error occurs.
|
||||||
*/
|
*/
|
||||||
char* FAST_FUNC dname_dec(const uint8_t *cstr, int clen, const char *pre)
|
char* FAST_FUNC dname_dec(const uint8_t *cstr, int clen, const char *pre)
|
||||||
{
|
{
|
||||||
char *ret = ret; /* for compiler */
|
char *ret, *end;
|
||||||
char *dst = NULL;
|
unsigned len, crtpos, retpos, depth;
|
||||||
|
|
||||||
/* We make two passes over the cstr string. First, we compute
|
crtpos = retpos = depth = 0;
|
||||||
* how long the resulting string would be. Then we allocate a
|
len = strlen(pre);
|
||||||
* new buffer of the required length, and fill it in with the
|
end = ret = xstrdup(pre);
|
||||||
* expanded content. The advantage of this approach is not
|
|
||||||
* having to deal with requiring callers to supply their own
|
|
||||||
* buffer, then having to check if it's sufficiently large, etc.
|
|
||||||
*/
|
|
||||||
while (1) {
|
|
||||||
/* note: "return NULL" below are leak-safe since
|
|
||||||
* dst isn't allocated yet */
|
|
||||||
const uint8_t *c;
|
|
||||||
unsigned crtpos, retpos, depth, len;
|
|
||||||
|
|
||||||
crtpos = retpos = depth = len = 0;
|
/* Scan the string once, allocating new memory as needed */
|
||||||
while (crtpos < clen) {
|
while (crtpos < clen) {
|
||||||
|
const uint8_t *c;
|
||||||
c = cstr + crtpos;
|
c = cstr + crtpos;
|
||||||
|
|
||||||
if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
|
if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
|
||||||
/* pointer */
|
/* pointer */
|
||||||
if (crtpos + 2 > clen) /* no offset to jump to? abort */
|
if (crtpos + 2 > clen) /* no offset to jump to? abort */
|
||||||
return NULL;
|
goto error;
|
||||||
if (retpos == 0) /* toplevel? save return spot */
|
if (retpos == 0) /* toplevel? save return spot */
|
||||||
retpos = crtpos + 2;
|
retpos = crtpos + 2;
|
||||||
depth++;
|
depth++;
|
||||||
crtpos = ((c[0] & 0x3f) << 8) | c[1]; /* jump */
|
crtpos = ((c[0] << 8) | c[1]) & 0x3fff; /* jump */
|
||||||
} else if (*c) {
|
} else if (*c) {
|
||||||
|
unsigned label_len;
|
||||||
/* label */
|
/* label */
|
||||||
if (crtpos + *c + 1 > clen) /* label too long? abort */
|
if (crtpos + *c + 1 > clen) /* label too long? abort */
|
||||||
return NULL;
|
goto error;
|
||||||
if (dst)
|
ret = xrealloc(ret, len + *c + 1);
|
||||||
/* \3com ---> "com." */
|
/* \3com ---> "com." */
|
||||||
((char*)mempcpy(dst + len, c + 1, *c))[0] = '.';
|
end = (char *)mempcpy(ret + len, c + 1, *c);
|
||||||
len += *c + 1;
|
*end = '.';
|
||||||
crtpos += *c + 1;
|
|
||||||
|
label_len = *c + 1;
|
||||||
|
len += label_len;
|
||||||
|
crtpos += label_len;
|
||||||
} else {
|
} else {
|
||||||
/* NUL: end of current domain name */
|
/* NUL: end of current domain name */
|
||||||
if (retpos == 0) {
|
if (retpos == 0) {
|
||||||
@ -78,34 +78,29 @@ char* FAST_FUNC dname_dec(const uint8_t *cstr, int clen, const char *pre)
|
|||||||
crtpos = retpos;
|
crtpos = retpos;
|
||||||
retpos = depth = 0;
|
retpos = depth = 0;
|
||||||
}
|
}
|
||||||
if (dst && len != 0)
|
|
||||||
|
if (len != 0) {
|
||||||
/* \4host\3com\0\4host and we are at \0:
|
/* \4host\3com\0\4host and we are at \0:
|
||||||
* \3com was converted to "com.", change dot to space.
|
* \3com was converted to "com.", change dot to space.
|
||||||
*/
|
*/
|
||||||
dst[len - 1] = ' ';
|
ret[len - 1] = ' ';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (depth > NS_MAXDNSRCH /* too many jumps? abort, it's a loop */
|
if (depth > NS_MAXDNSRCH /* too many jumps? abort, it's a loop */
|
||||||
|| len > NS_MAXDNAME * NS_MAXDNSRCH /* result too long? abort */
|
|| len > NS_MAXDNAME * NS_MAXDNSRCH /* result too long? abort */
|
||||||
) {
|
) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret == end) { /* expanded string is empty? abort */
|
||||||
|
error:
|
||||||
|
free(ret);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!len) /* expanded string has 0 length? abort */
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if (!dst) { /* first pass? */
|
|
||||||
/* allocate dst buffer and copy pre */
|
|
||||||
unsigned plen = strlen(pre);
|
|
||||||
ret = xmalloc(plen + len);
|
|
||||||
dst = stpcpy(ret, pre);
|
|
||||||
} else {
|
|
||||||
dst[len - 1] = '\0';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
*end = '\0';
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,42 +110,40 @@ char* FAST_FUNC dname_dec(const uint8_t *cstr, int clen, const char *pre)
|
|||||||
*/
|
*/
|
||||||
static uint8_t *convert_dname(const char *src, int *retlen)
|
static uint8_t *convert_dname(const char *src, int *retlen)
|
||||||
{
|
{
|
||||||
uint8_t c, *res, *lenptr, *dst;
|
uint8_t *res, *lenptr, *dst;
|
||||||
int len;
|
|
||||||
|
|
||||||
res = xmalloc(strlen(src) + 2);
|
res = xzalloc(strlen(src) + 2);
|
||||||
dst = lenptr = res;
|
dst = lenptr = res;
|
||||||
dst++;
|
dst++;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
uint8_t c;
|
||||||
|
int len;
|
||||||
|
|
||||||
c = (uint8_t)*src++;
|
c = (uint8_t)*src++;
|
||||||
if (c == '.' || c == '\0') { /* end of label */
|
if (c == '.' || c == '\0') { /* end of label */
|
||||||
len = dst - lenptr - 1;
|
len = dst - lenptr - 1;
|
||||||
/* label too long, too short, or two '.'s in a row? abort */
|
/* label too long, too short, or two '.'s in a row (len will be 0) */
|
||||||
if (len > NS_MAXLABEL || len == 0 || (c == '.' && *src == '.')) {
|
if (len > NS_MAXLABEL || len == 0)
|
||||||
free(res);
|
goto error;
|
||||||
*retlen = 0;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
*lenptr = len;
|
*lenptr = len;
|
||||||
if (c == '\0' || *src == '\0') /* "" or ".": end of src */
|
if (c == '\0' || *src == '\0') /* "" or ".": end of src */
|
||||||
break;
|
break;
|
||||||
lenptr = dst++;
|
lenptr = dst++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (c >= 'A' && c <= 'Z') /* uppercase? convert to lower */
|
*dst++ = tolower(c);
|
||||||
c += ('a' - 'A');
|
|
||||||
*dst++ = c;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dst - res >= NS_MAXCDNAME) { /* dname too long? abort */
|
*retlen = dst + 1 - res;
|
||||||
|
if (*retlen > NS_MAXCDNAME) { /* dname too long? abort */
|
||||||
|
error:
|
||||||
free(res);
|
free(res);
|
||||||
*retlen = 0;
|
*retlen = 0;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
*dst++ = 0;
|
|
||||||
*retlen = dst - res;
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,6 +238,7 @@ int main(int argc, char **argv)
|
|||||||
printf("test4:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\5", ""));
|
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", ""));
|
printf("test5:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\5\1z\xC0\xA", ""));
|
||||||
|
|
||||||
|
#if 0
|
||||||
#define DNAME_ENC(cache,source,lenp) dname_enc((uint8_t*)(cache), sizeof(cache), (source), (lenp))
|
#define DNAME_ENC(cache,source,lenp) dname_enc((uint8_t*)(cache), sizeof(cache), (source), (lenp))
|
||||||
encoded = dname_enc(NULL, 0, "test.net", &len);
|
encoded = dname_enc(NULL, 0, "test.net", &len);
|
||||||
printf("test6:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
|
printf("test6:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
|
||||||
@ -252,6 +246,13 @@ int main(int argc, char **argv)
|
|||||||
printf("test7:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
|
printf("test7:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
|
||||||
encoded = DNAME_ENC("\4test\3net\0", "test.net", &len);
|
encoded = DNAME_ENC("\4test\3net\0", "test.net", &len);
|
||||||
printf("test8:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
|
printf("test8:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
encoded = dname_enc("test.net", &len);
|
||||||
|
printf("test6:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
|
||||||
|
encoded = dname_enc("test.host.com", &len);
|
||||||
|
printf("test7:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user