-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_validate_util.c
339 lines (282 loc) · 9.69 KB
/
command_validate_util.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "command_validate_util.h"
/*
* 토크나이징이 적절하게 되었는지 검증한다.
* 예를들어 du , 1 1 과 같이
* 파라미터 사이에 콤마가 없거나 이상한 위치에 콤마가 있는 등의 문제를 잡아낸다.
*
* @return true or false
*/
bool validate_tokenizing(char *str, int token_cnt, int max_token_num) {
assert(str);
int length = (int)strlen(str);
int i=0, comma_cnt = 0, flag = 0;
char cm;
// 토큰의 개수를 검증한다.
if(token_cnt > max_token_num)
return false;
if(token_cnt <= 0)
return false;
// 콤마의 개수를 계산한다.
for(i=0;i<length; i++){
if(str[i] == ',')
comma_cnt++;
}
// 토큰의 개수가 두개이면서 콤마가 없는 경우는 적절한 경우다.
if(token_cnt <= 2 && comma_cnt == 0)
return true;
// 예시와 같은 경우의 에러를 잡아낸다.
// ex) du , 1 1 [x]
// ex) , du
flag = 0;
for(i=0;i<length;i++){
cm = str[i];
if(flag == 0){
// 첫번째 토큰 이전 문자열.
if(cm == ',') return false;
if(cm != ' ' && cm != '\t') flag = 1;
continue;
} else if(flag == 1){
// 첫번째 토큰을 지나가는 중
if(cm == ',') return false;
if(cm == ' ' || cm == '\t') flag = 2;
continue;
}
// 첫번째 토큰과 두번째 토큰 사이
if(str[i] == ' ') continue;
if(str[i] == '\t') continue;
if(str[i] != ',') break;
else return false;
}
// 예시와 같은 경우의 에러를 잡아낸다.
// ex) du 1 1 , [x]
for(i=length-2;i>=0;i--){
if(str[i] == ' ') continue;
if(str[i] == '\t') continue;
if(str[i] != ',') break;
else return false;
}
// 토큰의 개수와 콤마의 개수를 비교한다.
if(token_cnt != comma_cnt + 2)
return false;
// ex) f 1 ,, 2 3 [x]
// , 다음에는 [ ] [\t]이 나오다가 [ ] [\t] [,]이 아닌 값이 나와야한다.
flag = 0; // 콤마가 나오면 flag 는 1 로 놓자.
for(i=0;i<length;i++) {
cm = str[i];
if (flag == 1) {
if (cm == ',') return false;
if (cm == ' ' || cm == '\t') continue;
flag = 0;
continue;
}
if (cm == ',') {
flag = 1;
continue;
}
}
return true;
}
bool validate_tokenizing_for_not_comma(char *str, int token_cnt, int max_token_num){
assert(str);
int length = (int)strlen(str);
int comma_cnt = 0;
// 토큰의 개수를 검증한다.
if(token_cnt > max_token_num)
return false;
if(token_cnt <= 0)
return false;
// 콤마의 개수를 계산한다.
for(int i=0;i<length; i++){
if(str[i] == ',')
comma_cnt++;
}
if(comma_cnt != 0)
return false;
return true;
}
/*
* 사용자가 입력한 파라미터가 적절한 파라미터 값인지 검증한다.
* (명령어에 따른 파라미터 개수, 크기, 범위 등)
*
* @return VALID_PARAMETERS or INVALID_PARAMETERS
*/
shell_status validate_parameters(Command *user_command){
assert(user_command);
if((user_command->type == TYPE_QUIT ||
user_command->type == TYPE_HELP ||
user_command->type == TYPE_HISTORY ||
user_command->type == TYPE_DIR ||
user_command->type == TYPE_RESET ||
user_command->type == TYPE_OPCODELIST ||
user_command->type == TYPE_SYMBOL ||
user_command->type == TYPE_RUN ||
user_command->type == TYPE_BP_LIST
) &&
user_command->token_cnt > 1)
return INVALID_PARAMETERS;
if(user_command->type == TYPE_TYPE && user_command->token_cnt != 2)
return INVALID_PARAMETERS;
if(user_command->type == TYPE_OPCODE)
return validate_opcode_parameters(user_command);
if(user_command->type == TYPE_EDIT)
return validate_edit_parameters(user_command);
if(user_command->type == TYPE_FILL)
return validate_fill_parameters(user_command);
if(user_command->type == TYPE_DUMP)
return validate_dump_parameters(user_command);
if(user_command->type == TYPE_ASSEMBLE)
return validate_assemble_parameters(user_command);
if(user_command->type == TYPE_PROGADDR)
return validate_progaddr_parameters(user_command);
if(user_command->type == TYPE_BP)
return validate_bp_parameters(user_command);
if(user_command->type == TYPE_BP_CLEAR)
return validate_bp_clear_parameters(user_command);
if(user_command->type == TYPE_LOADER)
return validate_loader_parameters(user_command);
return VALID_PARAMETERS;
}
/*
* dump 명령어의 파라미터를 검증한다.
*
* @return VALID_PARAMETERS or INVALID_PARAMETERS
*/
shell_status validate_dump_parameters(Command *user_command){
assert(user_command);
assert(user_command->type == TYPE_DUMP);
int tok1, tok2;
if(user_command->token_cnt > 1) {
// 적절한 주소값인지 확인한다.
if (!is_valid_address(user_command->tokens[1], MB))
return INVALID_PARAMETERS;
else if (user_command->token_cnt == 2)
return VALID_PARAMETERS;
// 적절한 주소값인지 확인한다.
if (!is_valid_address(user_command->tokens[2], MB))
return INVALID_PARAMETERS;
tok1 = (int) strtol(user_command->tokens[1], NULL, 16);
tok2 = (int) strtol(user_command->tokens[2], NULL, 16);
// invalid area
if (tok1 > tok2) return INVALID_PARAMETERS;
}
// dump 1, 2, 3과 같이 파라미터가 세개 이상이 되는 경우는 에러이다.
if(user_command->token_cnt > 3)
return INVALID_PARAMETERS;
return VALID_PARAMETERS;
}
/*
* opcode 명령어의 파라미터를 검증한다.
*
* @return VALID_PARAMETERS or INVALID_PARAMETERS
*/
shell_status validate_opcode_parameters(Command *user_command){
assert(user_command);
assert(user_command->type == TYPE_OPCODE);
if(user_command->token_cnt != 2) return INVALID_PARAMETERS;
if(strlen(user_command->tokens[1]) > 14) return INVALID_PARAMETERS;
return VALID_PARAMETERS;
}
/*
* edit 명령어의 파라미터를 검증한다.
*
* @return VALID_PARAMETERS or INVALID_PARAMETERS
*/
shell_status validate_edit_parameters(Command *user_command){
assert(user_command);
assert(user_command->type == TYPE_EDIT);
int value;
if(user_command->token_cnt != 3)
return INVALID_PARAMETERS;
// 적절한 주소값인지 검증
if(!is_valid_address(user_command->tokens[1], MB))
return INVALID_PARAMETERS;
// 적절한 16진수 값인지 검증한다.
if(!is_valid_hex(user_command->tokens[2]))
return INVALID_PARAMETERS;
value = (int) strtol(user_command->tokens[2], NULL, 16);
// 범위 확인
if(!(0 <= value && value <= 0xFF))
return INVALID_PARAMETERS;
return VALID_PARAMETERS;
}
/*
* fill 명령어의 파라미터를 검증한다.
*
* @return VALID_PARAMETERS or INVALID_PARAMETERS
*/
shell_status validate_fill_parameters(Command *user_command){
assert(user_command);
assert(user_command->type == TYPE_FILL);
int value, start, end;
if(user_command->token_cnt != 4)
return INVALID_PARAMETERS;
if(!is_valid_address(user_command->tokens[1], MB))
return INVALID_PARAMETERS;
if(!is_valid_address(user_command->tokens[2], MB))
return INVALID_PARAMETERS;
start = (int) strtol(user_command->tokens[1], NULL, 16);
end = (int) strtol(user_command->tokens[2], NULL, 16);
if (start > end) return INVALID_PARAMETERS;
value = (int) strtol(user_command->tokens[3], NULL, 16);
if(!is_valid_hex(user_command->tokens[3]))
return INVALID_PARAMETERS;
if(!(0 <= value && value <= 0xFF)) return INVALID_PARAMETERS;
return VALID_PARAMETERS;
}
/*
* assemble 명령어의 파라미터를 검증한다.
*
* @return VALID_PARAMETERS or INVALID_PARAMETERS
*/
shell_status validate_assemble_parameters(Command *user_command){
assert(user_command);
assert(user_command->type == TYPE_ASSEMBLE);
if(user_command->token_cnt != 2)
return INVALID_PARAMETERS;
return VALID_PARAMETERS;
}
/*
* progaddr 명령어의 파라미터를 검증한다
*
* @return VALID_PARAMETERS or INVALID_PARAMETERS
*/
shell_status validate_progaddr_parameters(Command *user_command){
assert(user_command);
assert(user_command->type == TYPE_PROGADDR);
if(user_command->token_cnt != 2)
return INVALID_PARAMETERS;
// 적절한 주소 값인지 확인한다.
if(!is_valid_address(user_command->tokens[1], MB))
return INVALID_PARAMETERS;
return VALID_PARAMETERS;
}
/*
* bp 명령어의 파라미터를 검증한다
*
* @return VALID_PARAMETERS or INVALID_PARAMETERS
*/
shell_status validate_bp_parameters(Command* user_command){
assert(user_command);
assert(user_command->type == TYPE_BP);
if(user_command->token_cnt != 2) {
return INVALID_PARAMETERS;
}
// 적절한 주소 값인지 확인한다.
if(!is_valid_address(user_command->tokens[1], MB))
return INVALID_PARAMETERS;
return VALID_PARAMETERS;
}
shell_status validate_bp_clear_parameters(Command* user_command){
assert(user_command);
assert(user_command->type == TYPE_BP_CLEAR);
if(user_command->token_cnt != 2)
return INVALID_PARAMETERS;
return VALID_PARAMETERS;
}
shell_status validate_loader_parameters(Command* user_command){
assert(user_command);
assert(user_command->type == TYPE_LOADER);
if(user_command->token_cnt > 4 || user_command->token_cnt == 1)
return INVALID_PARAMETERS;
return VALID_PARAMETERS;
}