Created
August 1, 2012 03:55
-
-
Save mueschm/3223500 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
VyMessage* parseNewMessage(unsigned char* data) | |
{ | |
VyMessage* message = malloc(sizeof(VyMessage)); | |
int offset = 2; | |
char* uuid = malloc((sizeof(char)*UUID_SIZE)); | |
int i; | |
for(i = 0;i < UUID_SIZE;i++) | |
*(uuid+i) = *(data+i+offset); | |
message->senderID = uuid; | |
message->command = cStringSubstring(data,offset+UUID_SIZE,(unsigned int)data[0]); | |
message->data = cStringSubstring(data,offset+UUID_SIZE+((unsigned int)data[0]),(unsigned int)data[1]); | |
message->messageSize = (unsigned int)data[1]; | |
return message; | |
} | |
unsigned char* unparseNewMessage(VyMessage* message) | |
{ | |
int newMessageSize = sizeof(char)*(UUID_SIZE+strlen(message->command)+strlen(message->data)+3); | |
unsigned char* newMessage = malloc(newMessageSize); | |
newMessage[0] = strlen(message->command); | |
newMessage[1] = message->messageSize; | |
int offset = 2; | |
unsigned char* uuid = message->senderID; | |
int i; | |
for(i = 0;i < UUID_SIZE;i++) | |
*(newMessage+offset+i) = *(uuid+i); | |
int commandSize = strlen(message->command); | |
for(i = 0;i < commandSize;i++) | |
*(newMessage+UUID_SIZE+i+offset) = *(message->command+i); | |
int messageSize = message->messageSize; | |
for(i = 0;i < messageSize;i++) | |
*(newMessage+UUID_SIZE+commandSize+i+offset) = *(message->data+i); | |
newMessage[UUID_SIZE+messageSize+commandSize+offset] = 0; | |
return newMessage; | |
} | |
VyMessage* generateNewMessage(char* uuid, unsigned char* command, unsigned char* message, int messageSize) | |
{ | |
VyMessage* newMessage = malloc(sizeof(VyMessage)); | |
newMessage->messageSize = messageSize; | |
int i; | |
newMessage->senderID = malloc(UUID_SIZE+1); | |
for(i = 0;i < UUID_SIZE;i++) | |
*(newMessage->senderID+i) = *(uuid+i); | |
newMessage->data = malloc(messageSize); | |
for(i = 0;i < messageSize;i++) | |
*(newMessage->data+i) = *(message+i); | |
int commandSize = sizeof(char)*(strlen(command)+1); | |
newMessage->command = malloc(commandSize); | |
for(i = 0;i < commandSize;i++) | |
*(newMessage->command+i) = *(command+i); | |
newMessage->command[strlen(command)] = 0; | |
return newMessage; | |
} | |
unsigned char* generateNewMessageChar(char* uuid,unsigned char* command, unsigned char* message, int messageSize) | |
{ | |
int newMessageSize = sizeof(char)*(UUID_SIZE+strlen(command)+messageSize+3); | |
unsigned char* newMessage = malloc(newMessageSize); | |
unsigned char* uuidString = uuid; | |
newMessage[0] = strlen(command); | |
newMessage[1] = messageSize; | |
int offset = 2; | |
int i; | |
for(i = 0;i < UUID_SIZE;i++) | |
*(newMessage+i+offset) = *(uuidString+i); | |
int commandSize = strlen(command); | |
for(i = 0;i < commandSize;i++) | |
*(newMessage+UUID_SIZE+i+offset) = *(command+i); | |
for(i = 0;i < messageSize;i++) | |
*(newMessage+UUID_SIZE+commandSize+i+offset)= *(message+i); | |
newMessage[newMessageSize-1] = 0; | |
free(uuidString); | |
return newMessage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment