-
Notifications
You must be signed in to change notification settings - Fork 0
/
myoplnetparser.c
134 lines (121 loc) · 4.54 KB
/
myoplnetparser.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
#include <stdlib.h>
#include <tamtypes.h>
#include <debug.h>
#include "myoplnetparser.h"
const char* PS2_IP = "ps2_ip_addr=";
const char* PS2_NM = "ps2_netmask=";
const char* PS2_GW = "ps2_gateway=";
const char* PS2_DNS = "ps2_dns=";
const char* PS2_DHCP = "ps2_ip_use_dhcp=";
const char* SMB_IP = "smb_ip=";
const char* SMB_PORT = "smb_port=";
s32 parse_s32(s32 *number, const char* text);
s32 parse_u16(u16 *number, const char* text);
s32 parse_u8(u8 *number, const char* text);
s32 parse_ip(struct quadroctets *ip, const char* text);
s32 starts_with(const char* longer, const char* shorter);
s32 skip_until_past_newline(const char* text);
s32 skip_until_past_char(const char* text, char c);
struct oplnetconf *parse_opl(const char* text) {
struct oplnetconf *nc = calloc(sizeof(struct oplnetconf), 1);
if(nc==NULL) return NULL;
const char* p = text;
while(p[0] != 0){
if(starts_with(p, PS2_IP)){
p+=skip_until_past_char(p, '=');
p+=parse_ip(&nc->ps2ip.spread, p);
}
else if(starts_with(p, PS2_NM)){
p+=skip_until_past_char(p, '=');
p+=parse_ip(&nc->ps2nm.spread, p);
}
else if(starts_with(p, PS2_GW)){
p+=skip_until_past_char(p, '=');
p+=parse_ip(&nc->ps2gw.spread, p);
}
else if(starts_with(p, PS2_DNS)){
p+=skip_until_past_char(p, '=');
p+=parse_ip(&nc->ps2dns.spread, p);
}
else if(starts_with(p, SMB_IP)){
p+=skip_until_past_char(p, '=');
p+=parse_ip(&nc->pcip.spread, p);
}
else if(starts_with(p, SMB_PORT)){
p+=skip_until_past_char(p, '=');
u16 t = nc->pcport;
p+=parse_u16(&t, p);
nc->pcport = t;
}
else if(starts_with(p, PS2_DHCP)){
p+=skip_until_past_char(p, '=');
s32 t = nc->usedhcp;
p+=parse_s32(&t, p);
nc->usedhcp = t;
}
else{
p+=skip_until_past_newline(p);
}
}
return nc;
}
s32 parse_s32(s32 *number, const char* text) {
int i;
for(i=0; '0' <= text[i] && text[i] <= '9'; i++)
*number = (*number)*10 + (text[i]-'0');
return i;
}
s32 parse_u16(u16 *number, const char* text) {
int i;
for(i=0; '0' <= text[i] && text[i] <= '9'; i++)
*number = (*number)*10 + (text[i]-'0');
return i;
}
s32 parse_u8(u8 *number, const char* text) {
int i;
for(i=0; '0' <= text[i] && text[i] <= '9'; i++)
*number = (*number)*10 + (text[i]-'0');
return i;
}
s32 parse_ip(struct quadroctets *ip, const char* text) {
const char *p = text;
p+=parse_u8(&ip->octet1, p);
if(p[0]=='.')
p+=1+parse_u8(&ip->octet2, &p[1]);
if(p[0]=='.')
p+=1+parse_u8(&ip->octet3, &p[1]);
if(p[0]=='.')
p+=1+parse_u8(&ip->octet4, &p[1]);
return p-text;
}
s32 starts_with(const char* longer, const char* shorter) {
int i;
for(i = 0; longer[i] == shorter[i] && longer[i] != 0 && shorter[i] != 0; i++);
if(shorter[i] == 0) return -1;
return 0;
}
s32 skip_until_past_newline(const char* text) {
int i = 0;
while(text[i] != 0 && (text[i] != '\n' && text[i] != '\r')) i++;
while(text[i] != 0 && (text[i] == '\n' || text[i] == '\r')) i++;
return i;
}
s32 skip_until_past_char(const char* text, char c){
int i = 0;
while(text[i] != 0 && text[i] != c) i++;
while(text[i] != 0 && text[i] == c) i++;
return i;
}
void print_opl_net_bind(struct oplnetconf * netconf) {
if(netconf==NULL) return;
scr_printf("DHCP: %d\n", netconf->usedhcp);
scr_printf("PS2 IP: %03d.%03d.%03d.%03d\n", netconf->ps2ip.spread.octet1, netconf->ps2ip.spread.octet2, netconf->ps2ip.spread.octet3, netconf->ps2ip.spread.octet4);
scr_printf("PS2 NM: %03d.%03d.%03d.%03d\n", netconf->ps2nm.spread.octet1, netconf->ps2nm.spread.octet2, netconf->ps2nm.spread.octet3, netconf->ps2nm.spread.octet4);
scr_printf("PS2 GW: %03d.%03d.%03d.%03d\n", netconf->ps2gw.spread.octet1, netconf->ps2gw.spread.octet2, netconf->ps2gw.spread.octet3, netconf->ps2gw.spread.octet4);
scr_printf("PS2 DNS: %03d.%03d.%03d.%03d\n", netconf->ps2dns.spread.octet1, netconf->ps2dns.spread.octet2, netconf->ps2dns.spread.octet3, netconf->ps2dns.spread.octet4);
}
void print_opl_net_target(struct oplnetconf * netconf) {
if(netconf==NULL) return;
scr_printf("PC IP: %03d.%03d.%03d.%03d\n", netconf->pcip.spread.octet1, netconf->pcip.spread.octet2, netconf->pcip.spread.octet3, netconf->pcip.spread.octet4);
scr_printf("PC PORT: %05d\n", netconf->pcport);
}