Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7982451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:41:04+00:00 2026-06-04T10:41:04+00:00

here is something I have been working on for quite a while and can’t

  • 0

here is something I have been working on for quite a while and can’t solve why it is not working properly, hope you can help me work something out!. I’ll try to be as descriptive as possible.

The idea is to serialize a structure (NIPC packet structure), to send via TCP to another process, then deserialize and re-build this structure. But somehow I can’t get it to work properly, just partialy. Here are the structures.

typedef struct {
    char pct_type;
    short int payload_lenght;
}__attribute__((__packed__)) t_header;

typedef struct {
    char* path;
    long unsigned int size;
    long int offset;
}__attribute__ ((__packed__)) t_payload_read;

typedef struct {
    t_header header;
    t_payload_read payload;
}__attribute__ ((__packed__)) t_pct_read;

Then, I serialized the payload first (offset,size and path), to calculate the payload’s length, then the entire packet, including the header to send it. And then Deserialized, but there is no way to get it working, on the other side i get the header OK and the path field “OK”, but i get weird numbers on offset and size, for example, using this hardcoded values on the structure:

type: 4  
offset: 5  
size: 15  
path: ~/Desarrollo/Workspace

i get:

pct->header.pct_type: 4
pct->header.payload_length: 28
pct->payload.path: ~/Desarrollo/Workspac
pct->payload.size: 143994937
pct->payload.offset: 143994941

Here I append the serializing and deserializing functions I designed, I tried to be as descriptive and careful as possible.

char* serialize_read(t_pct_read* packet) {
    char* buffer = malloc(
            sizeof(t_header) + sizeof(packet->payload.offset)
                    + sizeof(packet->payload.size)
                    + strlen(packet->payload.path + 1));
    char* payload = malloc(
            sizeof(packet->payload.offset) + sizeof(packet->payload.size)
                    + strlen(packet->payload.path) + 1);
    int offset;

    // First I serialize the Payload, in order to calculate It's lenght 

    memcpy(payload, packet->payload.path, strlen(packet->payload.path) + 1);

    offset = strlen(packet->payload.path)+1;

    memcpy(payload + offset, &packet->payload.size, sizeof(long unsigned int));

    offset += (sizeof(long unsigned int));

    memcpy(payload + offset, &packet->payload.offset, sizeof(long int));

    packet->header.payload_lenght = offset+1; // Here I get payload's length

    offset = 0;

    // Same procedure here, but for the entire packet, including the header.    

    memcpy(buffer, &packet->header.pct_type, sizeof(char));

    offset = sizeof(char);

    memcpy(buffer + offset, &packet->header.payload_lenght, sizeof(short int));

    offset += sizeof(short int);

    memcpy(buffer + offset, packet->payload.path,
            strlen(packet->payload.path) + 1);

    offset += strlen(packet->payload.path + 1);

    memcpy(buffer + offset, &packet->payload.size, sizeof(long unsigned int));

    offset += (sizeof(long unsigned int));

    memcpy(buffer + offset, &packet->payload.offset, sizeof(long int));

    return buffer;

}

t_pct_read* deserialize_read(char* stream) {
    t_pct_read* packet = malloc(sizeof(t_pct_read));

    int offset;
    int alloc_size;

    memcpy(&packet->header.pct_type, stream, sizeof(char));

    offset = sizeof(char);

    memcpy(&packet->header.payload_lenght, stream + offset, sizeof(short int));

    offset += sizeof(short int);

    for (alloc_size = 1; (stream + offset)[alloc_size - 1] != '\0';
            alloc_size++) {
        packet->payload.path = malloc(alloc_size)+1;
    }

    memcpy(packet->payload.path, stream + offset, alloc_size+1);

    offset += strlen(packet->payload.path);

    memcpy(&packet->payload.size, stream + offset, sizeof(long unsigned int));

    offset += (sizeof(long unsigned int));

    memcpy(&packet->payload.offset, stream + offset, sizeof(long int));

    return packet;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:41:05+00:00Added an answer on June 4, 2026 at 10:41 am

    strlen(packet->payload.path + 1) looks suspicious, especially compared to strlen(packet->payload.path) + 1).
    Are your sure this is what you mean?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Its quite possible I'm missing something obvious here; I hope so. I have been
Here's something I've been thinking about: suppose you have a number, x, that can
I've been working quite a bit on something I can only describe as a
I have been working on XNA for quite some time, but was asked to
I have been working on something which a friend asked me. He is using
I have been working with Core Data in an iPad app and I can
I have been working on JUNG2 for a while now, trying to visualize a
I have been working with my rails application and since today I have not
I have been working on something that checks an MySQL Database to check something
I've been staring at this for quite a while and I'm sure it's something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.