summaryrefslogtreecommitdiffstats
path: root/gl/inet_ntop.c
diff options
context:
space:
mode:
Diffstat (limited to 'gl/inet_ntop.c')
-rw-r--r--gl/inet_ntop.c44
1 files changed, 21 insertions, 23 deletions
diff --git a/gl/inet_ntop.c b/gl/inet_ntop.c
index df3d9512..599ca4fb 100644
--- a/gl/inet_ntop.c
+++ b/gl/inet_ntop.c
@@ -1,6 +1,6 @@
1/* inet_ntop.c -- convert IPv4 and IPv6 addresses from binary to text form 1/* inet_ntop.c -- convert IPv4 and IPv6 addresses from binary to text form
2 2
3 Copyright (C) 2005-2006, 2008-2025 Free Software Foundation, Inc. 3 Copyright (C) 2005-2006, 2008-2026 Free Software Foundation, Inc.
4 4
5 This file is free software: you can redistribute it and/or modify 5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as 6 it under the terms of the GNU Lesser General Public License as
@@ -125,9 +125,7 @@ static const char *
125inet_ntop4 (const unsigned char *src, char *dst, socklen_t size) 125inet_ntop4 (const unsigned char *src, char *dst, socklen_t size)
126{ 126{
127 char tmp[sizeof "255.255.255.255"]; 127 char tmp[sizeof "255.255.255.255"];
128 int len; 128 int len = sprintf (tmp, "%u.%u.%u.%u", src[0], src[1], src[2], src[3]);
129
130 len = sprintf (tmp, "%u.%u.%u.%u", src[0], src[1], src[2], src[3]);
131 if (len < 0) 129 if (len < 0)
132 return NULL; 130 return NULL;
133 131
@@ -152,33 +150,25 @@ static const char *
152inet_ntop6 (const unsigned char *src, char *dst, socklen_t size) 150inet_ntop6 (const unsigned char *src, char *dst, socklen_t size)
153{ 151{
154 /* 152 /*
155 * Note that int32_t and int16_t need only be "at least" large enough
156 * to contain a value of the specified size. On some systems, like
157 * Crays, there is no such thing as an integer variable with 16 bits.
158 * Keep this in mind if you think this function should have been coded
159 * to use pointer overlays. All the world's not a VAX.
160 */
161 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
162 struct
163 {
164 int base, len;
165 } best, cur;
166 unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
167 int i;
168
169 /*
170 * Preprocess: 153 * Preprocess:
171 * Copy the input (bytewise) array into a wordwise array. 154 * Copy the input (bytewise) array into a wordwise array.
172 * Find the longest run of 0x00's in src[] for :: shorthanding. 155 * Find the longest run of 0x00's in src[] for :: shorthanding.
173 */ 156 */
157 unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
174 memset (words, '\0', sizeof words); 158 memset (words, '\0', sizeof words);
175 for (i = 0; i < NS_IN6ADDRSZ; i += 2) 159 for (int i = 0; i < NS_IN6ADDRSZ; i += 2)
176 words[i / 2] = (src[i] << 8) | src[i + 1]; 160 words[i / 2] = (src[i] << 8) | src[i + 1];
161
162 struct
163 {
164 int base, len;
165 } best, cur;
177 best.base = -1; 166 best.base = -1;
178 cur.base = -1; 167 cur.base = -1;
179 IF_LINT(best.len = 0); 168 IF_LINT(best.len = 0);
180 IF_LINT(cur.len = 0); 169 IF_LINT(cur.len = 0);
181 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) 170
171 for (int i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
182 { 172 {
183 if (words[i] == 0) 173 if (words[i] == 0)
184 { 174 {
@@ -208,8 +198,16 @@ inet_ntop6 (const unsigned char *src, char *dst, socklen_t size)
208 /* 198 /*
209 * Format the result. 199 * Format the result.
210 */ 200 */
211 tp = tmp; 201 /*
212 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) 202 * Note that int32_t and int16_t need only be "at least" large enough
203 * to contain a value of the specified size. On some systems, like
204 * Crays, there is no such thing as an integer variable with 16 bits.
205 * Keep this in mind if you think this function should have been coded
206 * to use pointer overlays. All the world's not a VAX.
207 */
208 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
209 char *tp = tmp;
210 for (int i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
213 { 211 {
214 /* Are we inside the best run of 0x00's? */ 212 /* Are we inside the best run of 0x00's? */
215 if (best.base != -1 && i >= best.base && i < (best.base + best.len)) 213 if (best.base != -1 && i >= best.base && i < (best.base + best.len))