D
Durango
Hello I need some guidance doing serialization of data using C. I have
the following function prototype for serializing data:
int serializeData(unsigned short data_type, void *structure, unsigned
char **buffer);
What I want to be able to do is take any type of structure and of course
write the data into the buffer.
so for example if I have a struc A defined:
struct A
{
char name[32];
};
than the buffer would contain the data for x, y, and z.
I also want to prepend an opcode of type unsigned short so that I can
determine which structure type the buffer contains.
I would like to pass this to the first 2 bytes of the buffer and
afterwards pass the data within the structure.
so for example I have:
unsigned short opcode = 12;
struct A *a_data;
char *buffer
and struct A as defined above.
I would call the function in the following manner:
note: buffer gets allocated inside the function.
serializeData(opcode, a_data, &buffer);
within the function:
I try to pass the opcode to the first 2 bytes of buffer
int serializeData(unsigned short data_type, void *structure, unsigned
char **buffer)
{
struct A *a_data;
*buffer = malloc(sizeof(struct A)+2);
/* pass opcode to first 2 bytes */
(*buffer)[0] = data_type >> 8;
(*buffer)[1] = data_type;
a_data = (struct A*)structure;
strcpy(*buffer+2, start_pkt->name);
return strlen(*buffer);
}
Without trying to prepend the opcode, the code works fine and the buffer
contains the valid value from the structure passed. If I prepend the
opcode but do not use strcpy to write the structure data to the buffer
after the first 2 bytes than the buffer will contain the opcode value in
the first 2 bytes. It's the combination of both that's the issue.
I am confused in what it is, my knowledge of pointer is a bit weak so I
come here for advice.
Thank you.
the following function prototype for serializing data:
int serializeData(unsigned short data_type, void *structure, unsigned
char **buffer);
What I want to be able to do is take any type of structure and of course
write the data into the buffer.
so for example if I have a struc A defined:
struct A
{
char name[32];
};
than the buffer would contain the data for x, y, and z.
I also want to prepend an opcode of type unsigned short so that I can
determine which structure type the buffer contains.
I would like to pass this to the first 2 bytes of the buffer and
afterwards pass the data within the structure.
so for example I have:
unsigned short opcode = 12;
struct A *a_data;
char *buffer
and struct A as defined above.
I would call the function in the following manner:
note: buffer gets allocated inside the function.
serializeData(opcode, a_data, &buffer);
within the function:
I try to pass the opcode to the first 2 bytes of buffer
int serializeData(unsigned short data_type, void *structure, unsigned
char **buffer)
{
struct A *a_data;
*buffer = malloc(sizeof(struct A)+2);
/* pass opcode to first 2 bytes */
(*buffer)[0] = data_type >> 8;
(*buffer)[1] = data_type;
a_data = (struct A*)structure;
strcpy(*buffer+2, start_pkt->name);
return strlen(*buffer);
}
Without trying to prepend the opcode, the code works fine and the buffer
contains the valid value from the structure passed. If I prepend the
opcode but do not use strcpy to write the structure data to the buffer
after the first 2 bytes than the buffer will contain the opcode value in
the first 2 bytes. It's the combination of both that's the issue.
I am confused in what it is, my knowledge of pointer is a bit weak so I
come here for advice.
Thank you.