MCPcopy Create free account
hub / github.com/RsyncProject/rsync / inet_ntop6

Function inet_ntop6

lib/inet_ntop.c:97–185  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

95 */
96#ifdef AF_INET6
97static const char *
98inet_ntop6(const unsigned char *src, char *dst, size_t size)
99{
100 /*
101 * Note that int32_t and int16_t need only be "at least" large enough
102 * to contain a value of the specified size. On some systems, like
103 * Crays, there is no such thing as an integer variable with 16 bits.
104 * Keep this in mind if you think this function should have been coded
105 * to use pointer overlays. All the world's not a VAX.
106 */
107 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
108 struct { int base, len; } best, cur;
109 unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
110 int i, inc;
111
112 /*
113 * Preprocess:
114 * Copy the input (bytewise) array into a wordwise array.
115 * Find the longest run of 0x00's in src[] for :: shorthanding.
116 */
117 memset(words, '\0', sizeof words);
118 for (i = 0; i < NS_IN6ADDRSZ; i++)
119 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
120 best.base = -1;
121 cur.base = -1;
122 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
123 if (words[i] == 0) {
124 if (cur.base == -1)
125 cur.base = i, cur.len = 1;
126 else
127 cur.len++;
128 } else {
129 if (cur.base != -1) {
130 if (best.base == -1 || cur.len > best.len)
131 best = cur;
132 cur.base = -1;
133 }
134 }
135 }
136 if (cur.base != -1) {
137 if (best.base == -1 || cur.len > best.len)
138 best = cur;
139 }
140 if (best.base != -1 && best.len < 2)
141 best.base = -1;
142
143 /*
144 * Format the result.
145 */
146 tp = tmp;
147 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
148 /* Are we inside the best run of 0x00's? */
149 if (best.base != -1 && i >= best.base &&
150 i < (best.base + best.len)) {
151 if (i == best.base)
152 *tp++ = ':';
153 continue;
154 }

Callers 1

inet_ntopFunction · 0.85

Calls 1

inet_ntop4Function · 0.85

Tested by

no test coverage detected