-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.c
182 lines (154 loc) · 3.48 KB
/
scan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "scan.h"
static uint8_t xdigit(char c) {
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return -1;
}
size_t scan_uint8(const char *s, uint8_t *u) {
uint8_t d, x;
size_t i;
for (i = x = 0; d = s[i] - '0', d < 10; i++) {
if (x > UINT8_MAX / 10 || UINT8_MAX - d < 10 * x)
return 0;
x = x * 10 + d;
}
*u = x;
return i;
}
size_t scan_xint8(const char *s, uint8_t *u) {
uint8_t d, x;
size_t i;
for (i = x = 0; d = xdigit(s[i]), d < 16; i++) {
if (x > UINT8_MAX / 16 || UINT8_MAX - d < 16 * x)
return 0;
x = x * 16 + d;
}
*u = x;
return i;
}
size_t scan_uint16(const char *s, uint16_t *u) {
uint16_t x;
uint8_t d;
size_t i;
for (i = x = 0; d = s[i] - '0', d < 10; i++) {
if (x > UINT16_MAX / 10 || UINT16_MAX - d < 10 * x)
return 0;
x = x * 10 + d;
}
*u = x;
return i;
}
size_t scan_xint16(const char *s, uint16_t *u) {
uint16_t x;
uint8_t d;
size_t i;
for (i = x = 0; d = xdigit(s[i]), d < 16; i++) {
if (x > UINT16_MAX / 16 || UINT16_MAX - d < 16 * x)
return 0;
x = x * 16 + d;
}
*u = x;
return i;
}
size_t scan_uint32(const char *s, uint32_t *u) {
uint32_t x;
uint8_t d;
size_t i;
for (i = x = 0; d = s[i] - '0', d < 10; i++) {
if (x > UINT32_MAX / 10 || UINT32_MAX - d < 10 * x)
return 0;
x = x * 10 + d;
}
*u = x;
return i;
}
size_t scan_xint32(const char *s, uint32_t *u) {
uint32_t x;
uint8_t d;
size_t i;
for (i = x = 0; d = xdigit(s[i]), d < 16; i++) {
if (x > UINT32_MAX / 16 || UINT32_MAX - d < 16 * x)
return 0;
x = x * 16 + d;
}
*u = x;
return i;
}
size_t scan_uint64(const char *s, uint64_t *u) {
uint64_t x;
uint8_t d;
size_t i;
for (i = x = 0; d = s[i] - '0', d < 10; i++) {
if (x > UINT64_MAX / 10 || UINT64_MAX - d < 10 * x)
return 0;
x = x * 10 + d;
}
*u = x;
return i;
}
size_t scan_xint64(const char *s, uint64_t *u) {
uint64_t x;
uint8_t d;
size_t i;
for (i = x = 0; d = xdigit(s[i]), d < 16; i++) {
if (x > UINT64_MAX / 16 || UINT64_MAX - d < 16 * x)
return 0;
x = x * 16 + d;
}
*u = x;
return i;
}
size_t scan_ip4_prefix(const char *s, char *ip, size_t *len, size_t max) {
size_t i = 0, m, n = 0;
if (max && (n = scan_uint8(s, (uint8_t *) ip)))
while (++i < max && s[n] == '.') {
if (!(m = scan_uint8(s + n + 1, (uint8_t *) ip + i)))
break;
n += m + 1;
}
*len = i;
return n;
}
size_t scan_ip4(const char *s, char ip[4]) {
size_t i, n;
n = scan_ip4_prefix(s, ip, &i, 4);
return i == 4 ? n : 0;
}
size_t scan_ip6_prefix(const char *s, char *ip, size_t *len, size_t max) {
size_t i = 0, m, n = 0;
uint16_t u;
if (max && (n = scan_xint16(s, &u))) {
ip[i++] = u >> 8;
ip[i++] = u;
while (i < max && (s[n] == '.' || s[n] == ':')) {
if (!(m = scan_xint16(s + n + 1, &u)))
break;
ip[i++] = u >> 8;
ip[i++] = u;
n += m + 1;
}
}
*len = i;
return n;
}
size_t scan_ip6(const char *s, char ip[16]) {
size_t i, j, m, n;
m = scan_ip6_prefix(s, ip, &i, 16);
if (i == 16)
return m;
if (s[m] != '.' && s[m] != ':')
return 0;
if (s[m + 1] != '.' && s[m + 1] != ':')
return 0;
n = scan_ip6_prefix(s + m + 2, ip + i, &j, 16 - i);
memmove(ip + 16 - j, ip + i, j);
memset(ip + i, 0, 16 - i - j);
return m + n + 2;
}