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 captures store complex (I/Q) baseband samples in flat files. Every tool — GNU Radio, SDR#, rtl_sdr — uses a header in front of the samples that pins the sample rate, the centre frequency, and the count.
We're not capturing any RF here. We just 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)
Implement int parse_iq_header(const uint8_t *buf, size_t n, iq_hdr_t *out).
Return 0 on valid magic, -1 on:
n < 20(uint64_t *)buf — alignment-unsafe.20 bytes, one magic check, three integers, all little-endian.