20 #if defined(LIBC_SCCS) && !defined(lint)
21 static const char rcsid[] =
"Id: inet_net_ntop.c,v 1.1.2.2 2004/03/09 09:17:27 marka Exp $";
35 #define SPRINTF(x) strlen(sprintfx)
37 #define SPRINTF(x) ((size_t)sprintf x)
41 char *dst,
size_t size);
43 char *dst,
size_t size);
92 if (bits < 0 || bits > 32)
100 if (
size <
sizeof "0")
108 for (
b = bits / 8;
b > 0;
b--)
110 if (
size <=
sizeof "255.")
113 dst +=
SPRINTF((dst,
"%u", *src++));
119 size -= (size_t) (dst - t);
126 if (
size <=
sizeof ".255")
131 m = ((1 <<
b) - 1) << (8 -
b);
132 dst +=
SPRINTF((dst,
"%u", *src & m));
133 size -= (size_t) (dst - t);
137 if (
size <=
sizeof "/32")
139 dst +=
SPRINTF((dst,
"/%u", bits));
176 unsigned char inbuf[16];
177 char outbuf[
sizeof(
"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
182 if (bits < 0 || bits > 128)
200 memcpy(inbuf, src, p);
201 memset(inbuf + p, 0, 16 - p);
205 m = ((u_int) ~0) << (8 -
b);
212 words = (bits + 15) / 16;
217 zero_s = zero_l = tmp_zero_s = tmp_zero_l = 0;
218 for (
i = 0;
i < (words * 2);
i += 2)
220 if ((s[
i] | s[
i + 1]) == 0)
228 if (tmp_zero_l && zero_l < tmp_zero_l)
237 if (tmp_zero_l && zero_l < tmp_zero_l)
243 if (zero_l != words && zero_s == 0 && ((zero_l == 6) ||
244 ((zero_l == 5 && s[10] == 0xff && s[11] == 0xff) ||
245 ((zero_l == 7 && s[14] != 0 && s[15] != 1)))))
249 for (p = 0; p < words; p++)
251 if (zero_l != 0 && p >= zero_s && p < zero_s + zero_l)
263 if (is_ipv4 && p > 5)
265 *cp++ = (p == 6) ?
':' :
'.';
266 cp +=
SPRINTF((cp,
"%u", *s++));
268 if (p != 7 || bits > 120)
271 cp +=
SPRINTF((cp,
"%u", *s++));
278 cp +=
SPRINTF((cp,
"%x", *s * 256 + s[1]));
284 (void)
SPRINTF((cp,
"/%u", bits));
285 if (strlen(outbuf) + 1 >
size)
char * pg_inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size)
static char * inet_cidr_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size)
static char * inet_cidr_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size)
static pg_noinline void Size size