-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
133 lines (121 loc) · 2.44 KB
/
parser.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
#include "parser.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
extern int errno;
main(int argc, char ** argv){
if (argc != 3)
return 1;
char buf[3][atoi(argv[1])];
struct parse_output p = {
.size = sizeof(buf[0]),
.desc = buf[0],
.mask = buf[1],
.limit = buf[2],
};
printf("len = %d\n", parse_int(argv[2], &p));
printf("p.desc = %u\n", *(int32_t *)p.desc);
printf("p.mask = %u\n", *(int32_t *)p.mask);
printf("p.limit = %u\n", *(int32_t *)p.limit);
return 0;
}
int
char_to_value(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 0xff;
}
int
parse_int (const char *str, struct parse_output *out)
{
int i;
char *end = NULL;
char operation = 0;
intmax_t spec;
intmax_t tmp[2];
uintmax_t mask = UINTMAX_MAX;
char *start = (char *)&str[0];
for (i = 0; i < 2; i++) {
errno = 0;
spec = strtoimax(start, &end, 0);
if (errno || (end == start))
return -1;
switch (out->size) {
case sizeof(int8_t):
if (spec > INT8_MAX ||
spec < INT8_MIN)
return -1;
tmp[i] = (int8_t)spec;
break;
case sizeof(int16_t):
if (spec > INT16_MAX ||
spec < INT16_MIN)
return -1;
tmp[i] = (int16_t)spec;
break;
case sizeof(int32_t):
if (spec > INT32_MAX ||
spec < INT32_MIN)
return -1;
tmp[i] = (int32_t)spec;
break;
case sizeof(int64_t):
if (spec > INT64_MAX ||
spec < INT64_MIN)
return -1;
tmp[i] = (int64_t)spec;
break;
default:
return -1;
}
if (isspace((char)*end) || !(*end))
break;
if ((*end != '/' && *end != ',') ||
operation)
return -1;
start = end + 1;
operation = *end;
}
if (out->desc)
memcpy(out->desc, &tmp[0], out->size);
switch (operation) {
case '/':
if (out->mask)
memcpy(out->mask, &tmp[1], out->size);
if (out->limit)
memcpy(out->limit, &tmp[0], out->size);
break;
case ',':
if (out->mask)
memcpy(out->mask, &mask, out->size);
if (out->limit)
memcpy(out->limit, &tmp[1], out->size);
break;
default:
if (out->mask)
memcpy(out->mask, &mask, out->size);
if (out->limit)
memcpy(out->limit, &tmp[0], out->size);
break;
}
return end - str;
}
int
parse_ipv4(const char *str, struct parse_output *out) {
return 0;
}
int
parse_ipv6(const char *str, struct parse_output *out) {
return 0;
}
int
parse_mac(const char *str, struct parse_output *out) {
return 0;
}