-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibParseMessage.c
81 lines (76 loc) · 1.83 KB
/
libParseMessage.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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "libParseMessage.h"
#include "protocol.h"
int isAlphaNumeric(char *s){
while(*s!='\0'){
if(!isalnum(*s))return(0);
s++;
}
return(1);
}
void clearPart(char *part[]){
for(int i=0;i<4;i++)part[i]=NULL;
}
/**
* Split buffer into at most 4 null terminated parts,
* part[0]...part[3] by placing '\0' inside buffer replacing the
* first 3 ':'. We return pointers to the pieces. We also
* validate the message at a high level.
*
* params
* char *buffer: a null terminated string
* char *part[]: array of 4 pointers to char
* return
* the number of pieces parsed
* 0 if this is not a valid message
*/
int parseMessage(char *buffer, char *part[]){
clearPart(part);
char *s=buffer;
int numParts=0;
part[0]=buffer;
numParts++;
while(*s!='\0' && numParts<4){
if(*s==':'){
*s='\0';
part[numParts]=(s+1);
numParts++;
}
s++;
}
if(strcmp(part[0], "register")==0){ // register:USER
if(numParts!=2 || strlen(part[1])>MAX_USER_LEN){
clearPart(part);
return(0);
}
} else if(strcmp(part[0], "getMessage")==0){ // getMessage
if(numParts!=1){
clearPart(part);
return(0);
}
} else if(strcmp(part[0], "list")==0){ // list
if(numParts!=1){
clearPart(part);
return(0);
}
} else if(strcmp(part[0], "quit")==0){ // quit
if(numParts!=1){
clearPart(part);
return(0);
}
} else if(strcmp(part[0], "message")==0){ // message:FROM_USER:TO_USER:CHAT_MESSAGE
if(numParts!=4
|| strlen(part[1])>MAX_USER_LEN || !isAlphaNumeric(part[1])
|| strlen(part[2])>MAX_USER_LEN || !isAlphaNumeric(part[2])
|| strlen(part[3])>MAX_CHAT_MESSAGE_LEN){
clearPart(part);
return(0);
}
} else {
clearPart(part);
return(0);
}
return(numParts);
}