-
August 31st, 2011, 10:32 PM
#1
String Parsing in C
It has been a while since I did some parsing in C, so I am trying to get it all back
I have a specific string that I need to parse. Basically get the specific string of length M starting at position X.
Here is a sample of data format:
Code:
1 TestTeamCisco96279 68:7f:74:56:64:ec WPA2PSK/AES 68 11b/g/n NONE In
I have a structure to to store this data.
Right now I have parse until WPAPSK/AES and I wanted to separate the string before and after "/" .
So here is what I did.
Code:
int get_data(char *ssid, char *mac , char *auth, char * enc, const char *data)
{
char p[150];
char tmp[30];
memset(p, 0, strlen(data) );
memcpy(p, data, strlen(data));
// get data starting at pos =57 length = 23
memmove(tmp, p+57, 23);
int pos = strcspn(tmp , "/");
memmove(enc, tmp+pos+1, strlen(tmp)-pos);
memmove(auth, p+57, pos);
printf("Auth :: %s \n", auth);
printf("Encryption :: %s \n", enc);
}
The output is these:
Code:
Auth :: WPA2PSK
Encryption :: AES WPA2PSK
If I changed the lenght of Encryption to 10, then the data is corrrectly displayed.
This is something wrong with how I manipulated the pointers?
englighten me please
-
September 1st, 2011, 04:44 AM
#2
Re: String Parsing in C
strcspn() works with null terminated strings.
But, there is no guarantee your strings are null terminated.
Code:
memset(p, 0, strlen(data) );
memcpy(p, data, strlen(data));
Let's suppose data is a string containing A, B, C, \0.
strlen() will return 3, not 4 because strlen does not take into account the last null character.
So memset(p, 0, strlen(data) ); will only write three zeros.
Then memcpy(p, data, strlen(data)); will only copy A, B, and C.
You end up with a string which is not null terminated.
A possible solution is simply:
-
September 1st, 2011, 07:19 AM
#3
-
September 1st, 2011, 07:21 AM
#4
Re: String Parsing in C
I don't know why you're using memcpy or memmove here. You could iterate the string looking for the slashes and use strncpy to extract the portion you want, you could use strtok to find the slashes, or you could use strstr to find the slashes and strncpy to extract the data between them.
-
September 1st, 2011, 07:27 PM
#5
Re: String Parsing in C
@ olivthill2 , thanks for the tip. strcspn , gave me the correct position of the character. I was not able to include it in the post, but the temporary variable "tmp" was initialized using memset and its size is greater than the actual number of size I extracted from the original string, so I think "tmp" is null terminated. correct me if am wrong.
@aamir121a , thanks for the suggestion. I was actually looking at it too. I am making this program on a busybox so there is very limited libraries and functions available. I am not sure if regular expression included in the platform. I will check.
@GCDEF .. strtok works its wonders again. thanks.
thank you all for the suggestions.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|