From 416ef5e2bf59bb2e45066a1d5d91ac677c0f48e5 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Wed, 10 Dec 2014 03:05:47 +0000 Subject: Initial commit --- src/libs6dns/s6dns_message_get_domain_internal.c | 47 ++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/libs6dns/s6dns_message_get_domain_internal.c (limited to 'src/libs6dns/s6dns_message_get_domain_internal.c') diff --git a/src/libs6dns/s6dns_message_get_domain_internal.c b/src/libs6dns/s6dns_message_get_domain_internal.c new file mode 100644 index 0000000..357797d --- /dev/null +++ b/src/libs6dns/s6dns_message_get_domain_internal.c @@ -0,0 +1,47 @@ +/* ISC license. */ + +#include +#include +#include +#include "s6dns-message-internal.h" + +unsigned int s6dns_message_get_domain_internal (char *out, unsigned int outmax, char const *s, unsigned int len, unsigned int *pos) +{ + unsigned int w = 0 ; /* writing head */ + unsigned int r = *pos ; /* reading head */ + unsigned int jumps = 0 ; + register int hasjumped = 0 ; + for (;;) + { + unsigned char c ; + if (r >= len) return (errno = EPROTO, 0) ; + c = s[r] ; + if (c < 64) /* normal label */ + { + if (r + ++c > len) return (errno = EPROTO, 0) ; + if (out) + { + if (w + c > outmax) return (errno = ENAMETOOLONG, 0) ; + byte_copy(out + w, c, s + r) ; + } + w += c ; r += c ; if (!hasjumped) *pos += c ; + if (c == 1) break ; + } + else if (c >= 192) /* pointer */ + { + if (r + 1 >= len) return (errno = EPROTO, 0) ; + if (hasjumped) + { + if (++jumps > 1000) return (errno = EPROTO, 0) ; + } + else + { + *pos += 2 ; + hasjumped = 1 ; + } + r = (((unsigned int)c & 63) << 8) | (unsigned char)(s[r + 1]) ; + } + else return (errno = EPROTONOSUPPORT, 0) ; /* unsupported extension */ + } + return w ; +} -- cgit v1.3.1