/* const char *
* inet_ntop6(src, dst, size)
* convert IPv6 binary address into presentation (printable) format
- * author:
+ * original author:
* Paul Vixie, 1996.
*/
static const char *
* Keep this in mind if you think this function should have been coded
* to use pointer overlays. All the world's not a VAX.
*/
- char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
- struct { int base, len; } best, cur;
- u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
- int i;
+ struct {
+ int base = -1;
+ int len = 0;
+ } best, cur;
+ unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ] = {};
/*
* Preprocess:
* Copy the input (bytewise) array into a wordwise array.
* Find the longest run of 0x00's in src[] for :: shorthanding.
*/
- memset(words, '\0', sizeof words);
- for (i = 0; i < NS_IN6ADDRSZ; i++)
+ for (int i = 0; i < NS_IN6ADDRSZ; ++i)
words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
- best.base = -1;
- best.len = 0;
- cur.base = -1;
- cur.len = 0;
- for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
+ for (int i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); ++i) {
if (words[i] == 0) {
if (cur.base == -1)
cur.base = i, cur.len = 1;
/*
* Format the result.
*/
- tp = tmp;
- for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
+ const auto bufSize =
+ sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255");
+ char buf[bufSize];
+ char *tp = buf;
+ for (int i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); ++i) {
/* Are we inside the best run of 0x00's? */
if (best.base != -1 && i >= best.base &&
i < (best.base + best.len)) {
if (i == 6 && best.base == 0 && (best.len == 6 ||
(best.len == 7 && words[7] != 0x0001) ||
(best.len == 5 && words[5] == 0xffff))) {
- if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
+ if (!inet_ntop4(src+12, tp, sizeof buf - (tp - buf)))
return nullptr;
tp += strlen(tp);
break;
}
- tp += snprintf(tp, (tmp + sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") - tp), "%x", words[i]);
+ tp += snprintf(tp, (buf + bufSize - tp), "%x", words[i]);
}
/* Was it a trailing run of 0x00's? */
if (best.base != -1 && (best.base + best.len) ==
/*
* Check for overflow, copy, and we're done.
*/
- if ((size_t)(tp - tmp) > size) {
+ if ((size_t)(tp - buf) > size) {
errno = ENOSPC;
return nullptr;
}
- strcpy(dst, tmp);
+ strcpy(dst, buf);
return (dst);
}