Skip to content

halloweeks/net-rw

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

net_rw.h

Lightweight low-level binary read/write helpers for C.

net_rw.h provides endian-safe primitive serialization functions for working with binary protocols, packet formats, file formats, embedded systems, SDR/radio protocols, and raw byte buffers.

The library is header-only, allocation-free, alignment-safe, and designed for explicit manual buffer management.


Features

  • Header-only implementation
  • Little-endian serialization format
  • Alignment-safe memory access using memcpy
  • Portable fixed-width integer encoding
  • IEEE754 float/double serialization
  • No hidden state or allocations
  • Minimal and low-overhead API
  • Suitable for embedded and systems programming

Supported Types

Unsigned Integers

  • u8
  • u16
  • u32
  • u64

Signed Integers

  • i8
  • i16
  • i32
  • i64

Floating Point

  • f32
  • f64

Raw Data

  • byte arrays
  • binary blobs

Design Philosophy

net_rw.h is intentionally low-level.

The library only performs primitive binary serialization/deserialization operations.

It does NOT provide:

  • automatic buffer management
  • bounds checking
  • stream abstractions
  • offset tracking
  • dynamic allocations
  • protocol definitions

The caller is responsible for managing offsets, buffer sizes, and protocol structures.

Example:

uint8_t buffer[256];
size_t offset = 0;

write_u32(buffer + offset, 0x12345678);
offset += 4;

write_f32(buffer + offset, 3.14f);
offset += 4;

This explicit design keeps the library:

  • lightweight
  • deterministic
  • flexible
  • easy to integrate into low-level systems

Endianness

All serialized values are encoded in little-endian format.

This ensures deterministic and portable binary encoding across architectures.


Why Not Write Raw Structs?

Writing raw C structs directly to files or networks is often unsafe because of:

  • compiler padding
  • alignment differences
  • endianness issues
  • compiler/platform ABI differences

net_rw.h avoids these issues by explicitly encoding primitive values into raw byte buffers.


Usage

Include

#include "net_rw.h"

Writing Example

#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include "net_rw.h"

int main(void)
{
    uint8_t buffer[256];
    size_t offset = 0;

    write_u32(buffer + offset, 0xABCD1234);
    offset += 4;

    write_u16(buffer + offset, 1337);
    offset += 2;

    write_f32(buffer + offset, 3.14f);
    offset += 4;

    write_bytes(buffer + offset, "hello", 5);
    offset += 5;

    return 0;
}

Reading Example

#include <stdio.h>
#include <stdint.h>

#include "net_rw.h"

int main(void)
{
    uint8_t buffer[256];
    size_t offset = 0;

    uint32_t magic = read_u32(buffer + offset);
    offset += 4;

    uint16_t port = read_u16(buffer + offset);
    offset += 2;

    float value = read_f32(buffer + offset);
    offset += 4;

    return 0;
}

Supported Functions

Write Functions

write_u8()
write_u16()
write_u32()
write_u64()

write_i8()
write_i16()
write_i32()
write_i64()

write_f32()
write_f64()

write_bytes()

Read Functions

read_u8()
read_u16()
read_u32()
read_u64()

read_i8()
read_i16()
read_i32()
read_i64()

read_f32()
read_f64()

read_bytes()

Example Use Cases

net_rw.h is suitable for:

  • TCP/UDP protocols
  • SDR/radio packet formats
  • custom binary protocols
  • binary file formats
  • game networking
  • embedded firmware protocols
  • IPC/shared memory
  • serialization systems
  • packet framing systems
  • custom archive/container formats

Safety Notes

The library assumes:

  • valid pointers
  • sufficient buffer size
  • proper offset management by caller

No bounds checking is performed internally.


Requirements

  • C11 compatible compiler
  • Compiler support for:
    • __builtin_memcpy
    • __builtin_bswap16
    • __builtin_bswap32
    • __builtin_bswap64

Tested with:

  • GCC
  • Clang

License

MIT License

About

Lightweight low-level binary read/write helpers for C with endian-safe serialization. Header-only library for portable binary encoding of integers, floating-point values, and raw byte buffers. Suitable for binary file formats, network protocols, SDR/radio packets, embedded systems, and custom serialization.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages