u/E-Vex

Can we write a generic byte-swap function for any struct in C?

Can we write a generic byte-swap function for any struct in C?

Hello, I am not a native speaker so please forgive my language level.

Today I was working on my packet analyzer project. I was refactoring the code and I realized something: there are a lot of functions just to swap the byte order for every header (struct).

So I have a question: Can I build a function/program in C that can automatically swap the byte order of every field in any struct?

thank you all and this my project link : vex-packet-analyzer

u/E-Vex — 21 hours ago

HOW?

I am not English native speaker so sorry for my language

I am an IT student and before few months I started learn C language and low level programming but my way of learning it by using AI

and after I feel comfortable with the pointers and understand how things really work I started a project its a packet analyzer

its a project to learn not a perfect thing it is my first real project so I decided to share my work here in r/C_Programming community so I was shocked that people say 'AI-slop' or something like that

but my project is too bad to have been written by AI I document everything on GitHub and in a YouTube streams

It was my mistake that I used AI to write the post because I was afraid of failing to write raw English without revision because my English level is not high

how I should learn? can someone help me please?

reddit.com
u/E-Vex — 8 days ago

The Extraction Loop & The Phantom 16 Bytes

Today, I was building a function to read packets continuously from a PCAP file. You can check out my project’s progress on GitHub: vex-packet-analyzer.

My goal was to build the read_packets(); function. Here is exactly how my thought process went:

*”Okay, let’s think about it. I want to build a loop that reads the packet header, grabs the payload.

Read 16 bytes (Packet Header).

Determine the incl_len.

Read the Data Link Type header.

Determine the protocol.

Jump to the next packet.”*

I decided to call this The Extraction Loop: looping through the entire file, extracting each header, decapsulating the link layer, and jumping to the next packet.

After a lot of suffering, I finally built the function — but it gave me completely unexpected, corrupted results. I spent so much time reviewing the read_packets(); logic, convinced the bug was inside it. But what happened next was a classic programming plot twist.

Why were the results corrupted? The problem was not the function itself. It was a leftover block of code in my main() function! Before building the loop, I was reading the packets manually. When I implemented read_packets(), I forgot to delete that old manual read.

So, my C code was executing like this:

Read 24 bytes (Global Header)

Read 16 bytes (Manual Packet Header — the leftover code!)

Read 16 bytes (Inside read_packets() function)

Read 20 bytes (Payload for the SLL2 header, since the network field was 0x114)

How did I detect the problem? I used the ftell() function to track the exact byte offset in the file. After the payload read, I expected ftell() to return 60 (24 + 16 + 20).

Instead, it returned 76.

That number was completely illogical. The math proved there were 16 phantom bytes sneaking in, which desynchronized my file pointer and broke the byte alignment for the entire file. I deleted the leftover code in main(), and boom—The Extraction Loop works perfectly.

You can view the complete work at: https://github.com/E-Vex/vex-packet-analyzer

u/E-Vex — 9 days ago

A painful reminder of low-level work: How a single flipped assumption corrupted my entire PCAP parser

Hey everyone,

I wanted to share a quick post-mortem of a bug that blocked me for a bit, mostly to document my journey and hopefully help anyone working on binary parsing in C.

For the past 17 days, I hadn't updated my repository. Not because I stopped learning, but because I was deep in the weeds trying to truly understand how pointers behave during low-level parsing work, rather than just reading theory.

I'm currently building a custom network packet analyzer from scratch called Vexor (GitHub:https://github.com/E-Vex/vex-packet-analyzer). Today, I finally got back into the PCAP parser itself and hit a fascinating bug in my check_magic_number function.

The Bug

The logic that determines the byte order (Endianness) from the PCAP global header was completely inverted. I had the mapping flipped in my head:

  • I was treating 0xa1b2c3d4 as Little-Endian (In reality, it represents Big-Endian/Identical).
  • I was treating 0xd4c3b2a1 as Big-Endian (In reality, it represents Little-Endian/Swapped).

Why It Mattered (The Domino Effect)

This wasn't just a minor logic bug. In low-level systems programming, one wrong assumption at the byte stage doesn't just break a single function—it corrupts the downstream data entirely.

Because the byte-order mapping was inverted, every single packet header that followed was interpreted incorrectly. Just look at what happened to the Major and Minor versions when the endianness was flipped (shifting by 8 bits without a proper byte swap):

Corrupted Output (Before the fix):

Magic Number : 0xD4C3B2A1
Major Version : 512
Minor Version : 1024
This Zone : 0
Sigfigs : 0
Snaplen : 1024
Network : 0x14010000

Correct Output (After the fix):

Magic Number : 0xA1B2C3D4
Major Version : 2
Minor Version : 4
This Zone : 0
Sigfigs : 0
Snaplen : 262144
Network : 0x114

(Major version successfully returned to 2, and Minor to 4).

The Fix

I corrected the mapping logic to accurately detect the Magic Number. Instead of relying on host endianness, I cast the pointer to uint8_t and read the memory byte-by-byte, which is a much safer approach.

Here is the snippet of the fix:

int check_magic_number(uint32_t *M)
{
    uint8_t *b = (uint8_t *)M;

    if (b[0] == 0xa1 && b[1] == 0xb2 && b[2] == 0xc3 && b[3] == 0xd4)
    {
        return BIG;
    }
    else if (b[0] == 0xd4 && b[1] == 0xc3 && b[2] == 0xb2 && b[3] == 0xa1)
    {
        return LITTLE;
    }
    else
    {
        printf("Error: the file is corrupted or is not a valid PCAP file\n");
        exit(1);
    }
}

Lesson learned: In C and low-level engineering, verify your foundational assumptions twice. One flipped bit or byte mapping can ruin the whole architecture.

Would love to hear your thoughts or if you've hit similar silent corruptions when parsing binary files!

u/E-Vex — 11 days ago