#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Serialization of Packages


    im trying to make my own Serialization library but I have some problems with the following.

    I have a package wich I serialize to a t_stream and then I need to serialize the t_stream to a
    char * again. This is the complete code:

    Code:
     
    
        typedef struct{
        	int16_t port;
        	int16_t priority;
        	char * ip;
        	char * code;
        }__attribute__((__packed__)) t_file_package;
        
        typedef struct {
        	int8_t packageType;
        	int32_t length;
        	char *data;
        }__attribute__((__packed__)) t_stream;
        
        
        //
        t_stream * package_file_serializer(t_file_package *self)
        {
        	char *data = malloc(  strlen(self->ip) + 1 + sizeof(int16_t) + sizeof(int16_t) + strlen(self->code)  + 1);
        	t_stream *stream = malloc(sizeof(t_stream));
        	int32_t offset = 0 , tmp_size = 0;
        
        
        	memcpy(data + offset, &self->port, tmp_size = sizeof(int16_t));
        
        	offset = tmp_size;
        	memcpy(data + offset, &self->priority, tmp_size = sizeof(int16_t));
        
        	offset += tmp_size;
        	memcpy(data + offset , self->ip , tmp_size = strlen(self->ip)+1);
        
        	offset += tmp_size;
        	memcpy(data + offset , self->code , tmp_size = strlen(self->code)+1);
        	stream->packageType = 1;
        	stream->length = offset + tmp_size;
        	stream->data = data;
        
        	return stream;
        }
        
        
        t_file_package * package_file_deserializer(t_stream *stream)
        {
        	t_file_package *self = malloc(sizeof(t_file_package));
        	int32_t offset = 0, tmp_size = 0;
        
        
        	memcpy(&self->port, stream->data + offset, tmp_size = sizeof(int16_t));
        
        	offset = tmp_size;
        	memcpy(&self->priority, stream->data + offset, tmp_size = sizeof(int16_t));
        
        	offset += tmp_size;
        	for(tmp_size = 1 ; (stream->data + offset)[tmp_size - 1] != '\0'; tmp_size++);
        	self->ip = malloc(tmp_size);
        	memcpy(self->ip, stream->data + offset, tmp_size);
        
        	offset += tmp_size;
        	for(tmp_size = 1 ; (stream->data + offset)[tmp_size - 1] != '\0'; tmp_size++);
        	self->code = malloc(tmp_size);
        	memcpy(self->code, stream->data + offset, tmp_size);
        
        	return self;
        }
        
        
        char * stream_serializer (t_stream *self)
        {
        	char *buffer= malloc(strlen(self->data)+1+sizeof(int8_t)+sizeof(int32_t));
        	int32_t offset = 0 , tmp_size = 0;
        
        	memcpy(buffer + offset,&self->packageType,tmp_size = sizeof(int8_t));
        
        	offset = tmp_size;
        	memcpy(buffer + offset,&self->length,tmp_size = sizeof(int32_t));
        
        	offset += tmp_size;
        	memcpy(buffer + offset,self->data,tmp_size = strlen(self->data)+1);
        
        	return buffer;}
        
        t_stream * stream_deserializer (char * self)
        {
        	t_stream *stream = malloc(sizeof(t_stream));
        	int32_t offset = 0, tmp_size = 0;
        
        
        	memcpy(&stream->packageType, self + offset, tmp_size = sizeof(int8_t));
        
        	offset =tmp_size;
        	memcpy(&stream->length,self + offset, tmp_size = sizeof(int32_t));
        
        	offset +=tmp_size;
        	for(tmp_size = 1 ; (self + offset)[tmp_size - 1] != '\0' ; tmp_size++);
        	stream->data=malloc(tmp_size);
        	memcpy(stream->data, self + offset, tmp_size);
        
        	return stream;
        }
        
        
        
        void free_package_file (t_file_package *self)
        {
        	free(self->ip);
        	free(self->code);
        	free(self);
        }
        
        void free_stream (t_stream *self)
        {
        	free(self->data);
        	free(self);
        }
        
        void free_buffer(char * self)
        {
        	free(self);
        }
        
        
        t_file_package * create_package_file ( int16_t port,int16_t priority,char * ip, char *code)
        {
        	t_file_package *paq = malloc(sizeof(t_file_package));
        
        	paq->priority = port;
        	paq->port =priority;
        	paq->ip = malloc(strlen(ip)+1);
        	strcpy(paq->ip,ip);
        	paq->code = malloc(strlen(code)+1);
        	strcpy(paq->code,code);
        	return paq;
        }
        
        
        
        int main(void) {
        
        			t_file_package *p1 = create_package_file(23,1,"1.2.3.4.2","SOME NICE CODE");
        			t_file_package *p2;
        			t_stream *stream,*stream2;
        			char * sStream;
        
        			stream = package_file_serializer(p1);
        			sStream = stream_serializer(stream);
        			stream2 = stream_deserializer(sStream);
        			p2 = package_file_deserializer(stream2);
        
        
        			assert(p1->port==p2->port);
        			assert(p1->priority==p2->priority);
        			assert(strcmp(p1->code,p2->code)==0);
        			assert(strcmp(p1->ip,p2->ip)==0);
        
        			free_package_file(p1);
        			free_package_file(p2);
        			free_stream(stream);
        
        
        			return 0;
        
        }
    I know that the problem is with the second serialization...


    Code:
       stream = package_file_serializer(p1);
         sStream = stream_serializer(stream);
         stream2 = stream_deserializer(sStream);
         p2 = package_file_deserializer(stream2);
    Because if you change that line to this ones it works perfect:

    Code:
          stream = package_file_serializer(p1);
          p2 = package_file_deserializer(stream);
    Id been for hours looking for the problem but i cant still find it...

    Any ideas!?

    Thanx in advanced!!!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,702
    Rep Power
    480
    What symptoms do you experience? Other than not bothering to test that memory allocation succeeds, what's the trouble?

    I inserted some header files, built, and ran the program. It produced no detectable output.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by b49P23TIvg
    What symptoms do you experience? Other than not bothering to test that memory allocation succeeds, what's the trouble?

    I inserted some header files, built, and ran the program. It produced no detectable output.
    Hi thanx for the answer, if you run it as with the first example, a message from assert should appear saying the expression is not true... That mean the two packages are not the same when they actually are
    something like this

    main: Assertion `p1->priority==p2->priority' failed.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,702
    Rep Power
    480

    Sorry, your program worked.


    I presume you're indicating the 4 lines of interesting code in main(). I tried the program with the 4 lines you showed, and with the 2 lines. No trouble.

    gcc on 64 bit 12.10 ubuntu release of linux.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by b49P23TIvg
    I presume you're indicating the 4 lines of interesting code in main(). I tried the program with the 4 lines you showed, and with the 2 lines. No trouble.

    gcc on 64 bit 12.10 ubuntu release of linux.

    lol really :S thats wierd :chomp:

    thanx anyway

IMN logo majestic logo threadwatch logo seochat tools logo