Data (De)serialization in C

1

I was wondering if someone could possibly help me with my problem. In client side, I have a linked list which each item contains an integer number. Suppose I appended 5 integer (let's say 1, 2, 3, 4, 5). I checked and the routine for creating and appending of integers to the linked list is OK. After that, I want to serialize the linked list via below code:

typedef struct ser_buff_ {
  void *b;
  int size; // total size of the buffer
  int next; // length of the contained data 
}ser_buff_t;

void serialize_linkedlist(table_t *table, ser_buff_t *b){
  table_entry_t *head = table->next;
  while(head){
    int temp = head->data;
    serialize_data(b, (char*)&temp, sizeof(int));
    head = head->next;
  }
  unsigned int sentinel = 0xFFFFFFFF;
  serialize_data(b, (char*)&sentinel, sizeof(unsigned int)); // mark the tail
}

table refers to the first item in the linked list and b is the buffer in which I want to serialize the linked list. As you may see, the function serialize_data() is invoked. The function is like below:

void serialize_data(ser_buff_t *b, char *data, int nbytes){
  if(b == NULL) assert(0);
  ser_buff_t *buff = b;
  int available_size = buff->size - buff->next;
  int isResize = 0;

  while(available_size < nbytes){
    buff->size *= 2;
    available_size = buff->size - buff->next;
    isResize = 1;
  }
  if(isResize == 0){
    memcpy((char*)buff->b + buff->next, data, nbytes);
    buff->next += nbytes;
    return;
  }
  buff->b = realloc(buff->b , buff->size);
  memcpy((char*)buff->b + buff->next, data, nbytes);
  buff->next += nbytes;
  return;
}

After serializing the linked list, I send the buffer to the server via below code:

ret = write(data_socket, b, b->next);

Here in server side, I receive the serialized buffer via socket.

ret = read(data_socket, b, 128);

I am sure that b->next in client side is less than 128. After receiving the serialized data from the client, the server passes it to de-serializaion function:

void de_serialize_linkedlist(ser_buff_t *b, table_t *table){
  int i = 1;
  unsigned int sentinel = 0;
  while(i){
    de_serialize_data((char*)&sentinel, b, sizeof(unsigned int));
    if(sentinel == 0xFFFFFFFF){
      break;
    }
    else{
      serialize_buffer_skip(b, sizeof(unsigned int)* (-1));
      de_serialize_data((char*)&i, b, sizeof(int));
      add(table, i);
    }
  }
}

inside this function, there is another function de_serialize_data() is called:

void de_serialize_data(char *dest, ser_buff_t *b, int nbytes){
  if(!b || !b->b) assert(0);
  if(!nbytes) return;
  if((b->size - b->next) < nbytes) assert(0);
  memcpy((char*)dest, (char*)b->b + b->next, nbytes);
  b->next += nbytes;
}

But I got set fault when I reach the line

memcpy((char*)dest, (char*)b->b + b->next, nbytes);

I assume b->b is not accessible which is not my expectation. Could anyone please help me.

c
serialization
rpc
asked on Stack Overflow Apr 11, 2020 by Ben Eslami

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0