Networking in C · intermediate · ~12 min
Read a fixed 20-byte header off the front of a software-defined-radio IQ capture file.
Bounds-check, memcmp the magic, build a u32 / u64 / u32 from byte shifts.
Same alignment-safe binary parse pattern as the pcap header, on a different fixture.
Software-defined radio (SDR) captures store complex baseband samples in flat binary files. These are called I/Q samples — the in-phase (I) and quadrature (Q) components of a radio signal.
Every SDR tool — GNU Radio, SDR#, rtl_sdr — writes a small header in front of the samples. The header records:
We are not capturing any RF here. We only decode the header bytes.
offset size field
0 4 magic "IQHD"
4 4 sample_rate_hz (u32 LE)
8 8 center_freq_hz (u64 LE)
16 4 sample_count (u32 LE)
The header is exactly 20 bytes. All integers are little-endian (LE): the lowest-value byte comes first in the file.
Implement:
int parse_iq_header(const uint8_t *buf, size_t n, iq_hdr_t *out);
Return 0 when the magic is valid. Return -1 on any of these:
n < 20 (not enough bytes)(uint64_t *)buf. This is alignment-unsafe and may crash or misread on some platforms. Build the value from individual byte shifts instead.IQHD), with no terminating NUL.memcmp the magic, then build each integer from byte shifts.