diff options
Diffstat (limited to 'gl/inet_pton.c')
| -rw-r--r-- | gl/inet_pton.c | 229 |
1 files changed, 113 insertions, 116 deletions
diff --git a/gl/inet_pton.c b/gl/inet_pton.c index 74d55c43..1e75c8c5 100644 --- a/gl/inet_pton.c +++ b/gl/inet_pton.c | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | /* inet_pton.c -- convert IPv4 and IPv6 addresses from text to binary form | 1 | /* inet_pton.c -- convert IPv4 and IPv6 addresses from text to binary form |
| 2 | 2 | ||
| 3 | Copyright (C) 2006, 2008-2025 Free Software Foundation, Inc. | 3 | Copyright (C) 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 |
| @@ -112,43 +112,44 @@ inet_pton (int af, const char *restrict src, void *restrict dst) | |||
| 112 | static int | 112 | static int |
| 113 | inet_pton4 (const char *restrict src, unsigned char *restrict dst) | 113 | inet_pton4 (const char *restrict src, unsigned char *restrict dst) |
| 114 | { | 114 | { |
| 115 | int saw_digit, octets, ch; | 115 | unsigned char tmp[NS_INADDRSZ]; |
| 116 | unsigned char tmp[NS_INADDRSZ], *tp; | ||
| 117 | 116 | ||
| 118 | saw_digit = 0; | 117 | { |
| 119 | octets = 0; | 118 | int saw_digit = 0; |
| 120 | *(tp = tmp) = 0; | 119 | int octets = 0; |
| 121 | while ((ch = *src++) != '\0') | 120 | unsigned char *tp = tmp; |
| 122 | { | 121 | *tp = 0; |
| 123 | 122 | int ch; | |
| 124 | if (ch >= '0' && ch <= '9') | 123 | while ((ch = *src++) != '\0') |
| 125 | { | 124 | { |
| 126 | unsigned new = *tp * 10 + (ch - '0'); | 125 | if (ch >= '0' && ch <= '9') |
| 127 | 126 | { | |
| 128 | if (saw_digit && *tp == 0) | 127 | if (saw_digit && *tp == 0) |
| 129 | return (0); | 128 | return (0); |
| 130 | if (new > 255) | 129 | unsigned new = *tp * 10 + (ch - '0'); |
| 131 | return (0); | 130 | if (new > 255) |
| 132 | *tp = new; | 131 | return (0); |
| 133 | if (!saw_digit) | 132 | *tp = new; |
| 134 | { | 133 | if (!saw_digit) |
| 135 | if (++octets > 4) | 134 | { |
| 136 | return (0); | 135 | if (++octets > 4) |
| 137 | saw_digit = 1; | 136 | return (0); |
| 138 | } | 137 | saw_digit = 1; |
| 139 | } | 138 | } |
| 140 | else if (ch == '.' && saw_digit) | 139 | } |
| 141 | { | 140 | else if (ch == '.' && saw_digit) |
| 142 | if (octets == 4) | 141 | { |
| 143 | return (0); | 142 | if (octets == 4) |
| 144 | *++tp = 0; | 143 | return (0); |
| 145 | saw_digit = 0; | 144 | *++tp = 0; |
| 146 | } | 145 | saw_digit = 0; |
| 147 | else | 146 | } |
| 148 | return (0); | 147 | else |
| 149 | } | 148 | return (0); |
| 150 | if (octets < 4) | 149 | } |
| 151 | return (0); | 150 | if (octets < 4) |
| 151 | return (0); | ||
| 152 | } | ||
| 152 | memcpy (dst, tmp, NS_INADDRSZ); | 153 | memcpy (dst, tmp, NS_INADDRSZ); |
| 153 | return (1); | 154 | return (1); |
| 154 | } | 155 | } |
| @@ -172,93 +173,89 @@ static int | |||
| 172 | inet_pton6 (const char *restrict src, unsigned char *restrict dst) | 173 | inet_pton6 (const char *restrict src, unsigned char *restrict dst) |
| 173 | { | 174 | { |
| 174 | static const char xdigits[] = "0123456789abcdef"; | 175 | static const char xdigits[] = "0123456789abcdef"; |
| 175 | unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp; | 176 | unsigned char tmp[NS_IN6ADDRSZ]; |
| 176 | const char *curtok; | ||
| 177 | int ch, saw_xdigit; | ||
| 178 | unsigned val; | ||
| 179 | 177 | ||
| 180 | tp = memset (tmp, '\0', NS_IN6ADDRSZ); | ||
| 181 | endp = tp + NS_IN6ADDRSZ; | ||
| 182 | colonp = NULL; | ||
| 183 | /* Leading :: requires some special handling. */ | 178 | /* Leading :: requires some special handling. */ |
| 184 | if (*src == ':') | 179 | if (*src == ':') |
| 185 | if (*++src != ':') | 180 | if (*++src != ':') |
| 186 | return (0); | 181 | return (0); |
| 187 | curtok = src; | ||
| 188 | saw_xdigit = 0; | ||
| 189 | val = 0; | ||
| 190 | while ((ch = c_tolower (*src++)) != '\0') | ||
| 191 | { | ||
| 192 | const char *pch; | ||
| 193 | 182 | ||
| 194 | pch = strchr (xdigits, ch); | 183 | { |
| 195 | if (pch != NULL) | 184 | unsigned char *tp = memset (tmp, '\0', NS_IN6ADDRSZ); |
| 196 | { | 185 | unsigned char *endp = tp + NS_IN6ADDRSZ; |
| 197 | val <<= 4; | 186 | unsigned char *colonp = NULL; |
| 198 | val |= (pch - xdigits); | 187 | const char *curtok = src; |
| 199 | if (val > 0xffff) | 188 | int saw_xdigit = 0; |
| 200 | return (0); | 189 | unsigned int val = 0; |
| 201 | saw_xdigit = 1; | 190 | int ch; |
| 202 | continue; | 191 | while ((ch = c_tolower (*src++)) != '\0') |
| 203 | } | 192 | { |
| 204 | if (ch == ':') | 193 | const char *pch = strchr (xdigits, ch); |
| 205 | { | 194 | if (pch != NULL) |
| 206 | curtok = src; | 195 | { |
| 207 | if (!saw_xdigit) | 196 | val <<= 4; |
| 208 | { | 197 | val |= (pch - xdigits); |
| 209 | if (colonp) | 198 | if (val > 0xffff) |
| 210 | return (0); | ||
| 211 | colonp = tp; | ||
| 212 | continue; | ||
| 213 | } | ||
| 214 | else if (*src == '\0') | ||
| 215 | { | ||
| 216 | return (0); | 199 | return (0); |
| 217 | } | 200 | saw_xdigit = 1; |
| 218 | if (tp + NS_INT16SZ > endp) | 201 | } |
| 219 | return (0); | 202 | else if (ch == ':') |
| 220 | *tp++ = (unsigned char) (val >> 8) & 0xff; | 203 | { |
| 221 | *tp++ = (unsigned char) val & 0xff; | 204 | curtok = src; |
| 222 | saw_xdigit = 0; | 205 | if (!saw_xdigit) |
| 223 | val = 0; | 206 | { |
| 224 | continue; | 207 | if (colonp) |
| 225 | } | 208 | return (0); |
| 226 | if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) && | 209 | colonp = tp; |
| 227 | inet_pton4 (curtok, tp) > 0) | 210 | } |
| 228 | { | 211 | else if (*src == '\0') |
| 229 | tp += NS_INADDRSZ; | 212 | return (0); |
| 230 | saw_xdigit = 0; | 213 | else if (tp + NS_INT16SZ > endp) |
| 231 | break; /* '\0' was seen by inet_pton4(). */ | 214 | return (0); |
| 232 | } | 215 | else |
| 216 | { | ||
| 217 | *tp++ = (unsigned char) (val >> 8) & 0xff; | ||
| 218 | *tp++ = (unsigned char) val & 0xff; | ||
| 219 | saw_xdigit = 0; | ||
| 220 | val = 0; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | else if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) && | ||
| 224 | inet_pton4 (curtok, tp) > 0) | ||
| 225 | { | ||
| 226 | tp += NS_INADDRSZ; | ||
| 227 | saw_xdigit = 0; | ||
| 228 | break; /* '\0' was seen by inet_pton4(). */ | ||
| 229 | } | ||
| 230 | else | ||
| 231 | return (0); | ||
| 232 | } | ||
| 233 | if (saw_xdigit) | ||
| 234 | { | ||
| 235 | if (tp + NS_INT16SZ > endp) | ||
| 236 | return (0); | ||
| 237 | *tp++ = (unsigned char) (val >> 8) & 0xff; | ||
| 238 | *tp++ = (unsigned char) val & 0xff; | ||
| 239 | } | ||
| 240 | if (colonp != NULL) | ||
| 241 | { | ||
| 242 | if (tp == endp) | ||
| 243 | return (0); | ||
| 244 | /* | ||
| 245 | * Since some memmove()'s erroneously fail to handle | ||
| 246 | * overlapping regions, we'll do the shift by hand. | ||
| 247 | */ | ||
| 248 | const int n = tp - colonp; | ||
| 249 | for (int i = 1; i <= n; i++) | ||
| 250 | { | ||
| 251 | endp[-i] = colonp[n - i]; | ||
| 252 | colonp[n - i] = 0; | ||
| 253 | } | ||
| 254 | tp = endp; | ||
| 255 | } | ||
| 256 | if (tp != endp) | ||
| 233 | return (0); | 257 | return (0); |
| 234 | } | 258 | } |
| 235 | if (saw_xdigit) | ||
| 236 | { | ||
| 237 | if (tp + NS_INT16SZ > endp) | ||
| 238 | return (0); | ||
| 239 | *tp++ = (unsigned char) (val >> 8) & 0xff; | ||
| 240 | *tp++ = (unsigned char) val & 0xff; | ||
| 241 | } | ||
| 242 | if (colonp != NULL) | ||
| 243 | { | ||
| 244 | /* | ||
| 245 | * Since some memmove()'s erroneously fail to handle | ||
| 246 | * overlapping regions, we'll do the shift by hand. | ||
| 247 | */ | ||
| 248 | const int n = tp - colonp; | ||
| 249 | int i; | ||
| 250 | |||
| 251 | if (tp == endp) | ||
| 252 | return (0); | ||
| 253 | for (i = 1; i <= n; i++) | ||
| 254 | { | ||
| 255 | endp[-i] = colonp[n - i]; | ||
| 256 | colonp[n - i] = 0; | ||
| 257 | } | ||
| 258 | tp = endp; | ||
| 259 | } | ||
| 260 | if (tp != endp) | ||
| 261 | return (0); | ||
| 262 | memcpy (dst, tmp, NS_IN6ADDRSZ); | 259 | memcpy (dst, tmp, NS_IN6ADDRSZ); |
| 263 | return (1); | 260 | return (1); |
| 264 | } | 261 | } |
