localedata: dz_BT, bo_CN: convert to UTF-8
[glibc.git] / resolv / ns_name_ntop.c
blob4dab3a3ce0286891d87dbfdaad1dc5c937ad2986
1 /* Convert DNS domain names from network format to textual presentation format.
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <arpa/nameser.h>
19 #include <errno.h>
20 #include <shlib-compat.h>
21 #include <stdbool.h>
23 /* Thinking in noninternationalized US-ASCII (per the DNS spec), is
24 this character special ("in need of quoting")? */
25 static inline bool
26 special (int ch)
28 switch (ch)
30 case '"':
31 case '.':
32 case ';':
33 case '\\':
34 case '(':
35 case ')':
36 /* Special modifiers in zone files. */
37 case '@':
38 case '$':
39 return true;
40 default:
41 return false;
45 /* Thinking in noninternationalized US-ASCII (per the DNS spec), is
46 this character visible and not a space when printed? */
47 static inline bool
48 printable (int ch)
50 return ch > 0x20 && ch < 0x7f;
53 /* Converts an uncompressed, encoded domain name to printable ASCII as
54 per RFC1035. Returns the number of bytes written to buffer, or -1
55 (with errno set). The root is returned as "." All other domains
56 are returned in non absolute form. */
57 int
58 ___ns_name_ntop (const unsigned char *src, char *dst, size_t dstsiz)
60 const unsigned char *cp;
61 char *dn, *eom;
62 unsigned char c;
63 int l;
65 cp = src;
66 dn = dst;
67 eom = dst + dstsiz;
69 while ((l = *cp++) != 0)
71 if (l >= 64)
73 /* Some kind of compression pointer. */
74 __set_errno (EMSGSIZE);
75 return -1;
77 if (dn != dst)
79 if (dn >= eom)
81 __set_errno (EMSGSIZE);
82 return -1;
84 *dn++ = '.';
86 for (; l > 0; l--)
88 c = *cp++;
89 if (special (c))
91 if (eom - dn < 2)
93 __set_errno (EMSGSIZE);
94 return -1;
96 *dn++ = '\\';
97 *dn++ = c;
99 else if (!printable (c))
101 if (eom - dn < 4)
103 __set_errno (EMSGSIZE);
104 return -1;
106 *dn++ = '\\';
107 *dn++ = '0' + (c / 100);
108 *dn++ = '0' + ((c % 100) / 10);
109 *dn++ = '0' + (c % 10);
111 else
113 if (eom - dn < 2)
115 __set_errno (EMSGSIZE);
116 return -1;
118 *dn++ = c;
122 if (dn == dst)
124 if (dn >= eom)
126 __set_errno (EMSGSIZE);
127 return -1;
129 *dn++ = '.';
131 if (dn >= eom)
133 __set_errno (EMSGSIZE);
134 return -1;
136 *dn++ = '\0';
137 return dn - dst;
139 versioned_symbol (libc, ___ns_name_ntop, ns_name_ntop, GLIBC_2_34);
140 versioned_symbol (libc, ___ns_name_ntop, __ns_name_ntop, GLIBC_PRIVATE);
141 libc_hidden_ver (___ns_name_ntop, __ns_name_ntop)
143 #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_9, GLIBC_2_34)
144 compat_symbol (libresolv, ___ns_name_ntop, ns_name_ntop, GLIBC_2_9);
145 #endif