72a80e82a6f98273c33573221315f62b1daa5903
[oweals/musl.git] / src / network / dn_expand.c
1 #include <resolv.h>
2 #include "libc.h"
3
4 int __dn_expand(const unsigned char *base, const unsigned char *end, const unsigned char *src, char *dest, int space)
5 {
6         const unsigned char *p = src;
7         int len = -1, j;
8         if (space > 256) space = 256;
9         if (p==end) return -1;
10         for (;;) {
11                 if (*p & 0xc0) {
12                         if (p+1==end) return -1;
13                         j = (p[0]&1) | p[1];
14                         if (len < 0) len = p+2-src;
15                         if (j >= end-base) return -1;
16                         p = base+j;
17                 } else if (*p) {
18                         j = *p+1;
19                         if (len < 0) len = p+1-src;
20                         if (j>=end-p || j>space) return -1;
21                         while (--j) *dest++ = *p++;
22                         *dest++ = *++p ? '.' : 0;
23                 } else return len;
24         }
25 }
26
27 weak_alias(__dn_expand, dn_expand);