E
erbm
Hello people. I'm learning C and I'm doing a simple client-server
aplication, and so I'm using 2 fuctions to serialize and deserialize a
struct to/from an array of bytes and send it to the server.
struct product_t {
int size_n; // name size
char* name;
int price;
};
and the serialization format is:
NAMESIZE NAME PRICE
[4byte] [NAMESIZE bytes] [4 byte]
The functions are:
int product_to_buffer(struct product_t *prod, char **buffer)
{
char *buf = *buffer;
uint32_t size = sizeof(uint32_t) + strlen(prod->name) + sizeof
(uint32_t);
buf = malloc (size);
*buffer = buf;
char* pos = buf;
uint32_t name_size = htonl(prod->size_n);
unsigned char* bytes = (unsigned char*) &name_size;
memcpy(pos, bytes, sizeof (size));
pos+= sizeof (size);
char auxName[prod->size_n];
strcpy(auxName, prod->name);
memcpy(pos, auxName, prod->size_n);
pos+= prod->size_n;
uint32_t price = htonl(prod->price);
bytes = (unsigned char*) &price;
memcpy(pos, bytes, sizeof (uint32_t));
pos+= sizeof (size);
return size; //number of bytes allocated
}
struct product_t *buffer_to_product(int buflen, char *buffer)
{
if (buflen < 2 * sizeof(uint32_t) || buffer == NULL)
return NULL;
struct product_t* prod = (struct product_t*) malloc(sizeof (struct
product_t));
char* pos = buffer;
uint32_t price, name_size;
memcpy(&name_size, pos, sizeof (uint32_t));
prod->size_n = ntohl(name_size);
prod->name = malloc(ntohl(name_size));
pos += sizeof (uint32_t);
char name[prod->size_n];
memcpy(name, pos, prod->size_n);
strcpy(prod->name, name);
prod->name[prod->size_n] = '\0' ;
pos += prod->size_n;
memcpy(&price, pos, sizeof (uint32_t));
prod->price = ntohl(price);
return prod;
}
It looks like they work correctly. For example, If I call:
char* buffer;
int size = product_to_buffer(&prod, &buffer);
and :
struct product_t* prod2 = buffer_to_product(size, buffer);
i am able to restore the original product successfuly.
The problem is finding the size of se buffer after the serialization.
int buf_size = strlen(buffer);
return 0, and so i cant copy the buffer and find the number of bytes.
Yes, the variable size contains the length, but in some situations i
dont have this information but I still need to find the length of the
buffer.
Any ideia of what is happening? Returnig 0 on strlen is the same as
telling me the buffer is empty, isn't it? But, it works when I use the
buffer to deserialize.
Goog night
aplication, and so I'm using 2 fuctions to serialize and deserialize a
struct to/from an array of bytes and send it to the server.
struct product_t {
int size_n; // name size
char* name;
int price;
};
and the serialization format is:
NAMESIZE NAME PRICE
[4byte] [NAMESIZE bytes] [4 byte]
The functions are:
int product_to_buffer(struct product_t *prod, char **buffer)
{
char *buf = *buffer;
uint32_t size = sizeof(uint32_t) + strlen(prod->name) + sizeof
(uint32_t);
buf = malloc (size);
*buffer = buf;
char* pos = buf;
uint32_t name_size = htonl(prod->size_n);
unsigned char* bytes = (unsigned char*) &name_size;
memcpy(pos, bytes, sizeof (size));
pos+= sizeof (size);
char auxName[prod->size_n];
strcpy(auxName, prod->name);
memcpy(pos, auxName, prod->size_n);
pos+= prod->size_n;
uint32_t price = htonl(prod->price);
bytes = (unsigned char*) &price;
memcpy(pos, bytes, sizeof (uint32_t));
pos+= sizeof (size);
return size; //number of bytes allocated
}
struct product_t *buffer_to_product(int buflen, char *buffer)
{
if (buflen < 2 * sizeof(uint32_t) || buffer == NULL)
return NULL;
struct product_t* prod = (struct product_t*) malloc(sizeof (struct
product_t));
char* pos = buffer;
uint32_t price, name_size;
memcpy(&name_size, pos, sizeof (uint32_t));
prod->size_n = ntohl(name_size);
prod->name = malloc(ntohl(name_size));
pos += sizeof (uint32_t);
char name[prod->size_n];
memcpy(name, pos, prod->size_n);
strcpy(prod->name, name);
prod->name[prod->size_n] = '\0' ;
pos += prod->size_n;
memcpy(&price, pos, sizeof (uint32_t));
prod->price = ntohl(price);
return prod;
}
It looks like they work correctly. For example, If I call:
char* buffer;
int size = product_to_buffer(&prod, &buffer);
and :
struct product_t* prod2 = buffer_to_product(size, buffer);
i am able to restore the original product successfuly.
The problem is finding the size of se buffer after the serialization.
int buf_size = strlen(buffer);
return 0, and so i cant copy the buffer and find the number of bytes.
Yes, the variable size contains the length, but in some situations i
dont have this information but I still need to find the length of the
buffer.
Any ideia of what is happening? Returnig 0 on strlen is the same as
telling me the buffer is empty, isn't it? But, it works when I use the
buffer to deserialize.
Goog night