-
Notifications
You must be signed in to change notification settings - Fork 7
/
dns_dntop.c
57 lines (54 loc) · 1.12 KB
/
dns_dntop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* dns_dntop() produces printable ascii representation (external form)
* of a domain name
*/
#include "dns.h"
unsigned dns_dntop(const unsigned char *dn, char *dst, unsigned dstsiz) {
char *d = dst;
char *m = dst + dstsiz - 1;
unsigned n;
unsigned char c;
if (!(n = *dn++)) {
if (dstsiz < 2)
return 0;
*d++ = '.';
*d++ = '\0';
return 1;
}
do {
if (d >= m)
return 0;
if (dst != d) *d++ = '.';
do {
switch((c = *dn++)) {
case '"':
case '.':
case ';':
case '\\':
/* Special modifiers in zone files. */
case '@':
case '$':
if (d + 1 >= m)
return 0;
*d++ = '\\';
*d++ = c;
break;
default:
if (c <= 0x20 || c >= 0x7f) {
if (d + 3 >= m)
return 0;
*d++ = '\\';
*d++ = '0' + (c / 100);
*d++ = '0' + ((c % 100) / 10);
*d++ = '0' + (c % 10);
}
else {
if (d >= m)
return 0;
*d++ = c;
}
}
} while(--n);
} while((n = *dn++) != 0);
*d = '\0';
return d - dst;
}