]> git.proxmox.com Git - wasi-libc.git/blob - libc-bottom-half/cloudlibc/src/libc/arpa/inet/inet_aton.c
42f4c2f299d78f24c6da8f36ce8fcd792fb98b83
[wasi-libc.git] / libc-bottom-half / cloudlibc / src / libc / arpa / inet / inet_aton.c
1 // Copyright (c) 2016 Nuxi, https://nuxi.nl/
2 //
3 // SPDX-License-Identifier: BSD-2-Clause
4
5 #include <arpa/inet.h>
6
7 #include <stdbool.h>
8 #include <stdint.h>
9
10 int inet_aton(const char *cp, struct in_addr *inp) {
11 uint32_t max = UINT32_MAX;
12 uint32_t leading = 0;
13 int shift = 24;
14 for (;;) {
15 // Parse next part of the IPv4 address.
16 typedef uint32_t int_t;
17 uint32_t min = 0;
18 int base = 0;
19 bool allow_negative = false;
20 #define PEEK(n) cp[n]
21 #define SKIP(n) \
22 do { \
23 cp += (n); \
24 } while (0)
25 #include <common/parser_strtoint.h>
26 #undef PEEK
27 #undef SKIP
28 if (!have_number || have_overflow)
29 return 0;
30
31 if (*cp == '\0') {
32 // End of string. Return the IPv4 address, combining the
33 // previously parsed leading bytes with the trailing number.
34 inp->s_addr = htonl(leading | number);
35 return 1;
36 } else if (shift > 0 && number <= UINT8_MAX && *cp++ == '.') {
37 // More components follow.
38 leading |= number << shift;
39 shift -= 8;
40 max >>= 8;
41 } else {
42 // Parse error.
43 return 0;
44 }
45 }
46 }