Networking in C · intermediate · ~12 min

Parse an IQ-samples capture header

Read a fixed 20-byte header off the front of a software-defined-radio IQ capture file.

Overview

Bounds-check, memcmp the magic, build a u32 / u64 / u32 from byte shifts.

Why it matters

Same alignment-safe binary parse pattern as the pcap header, on a different fixture.

Lesson

Why this matters

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.

What the header looks like (our format)

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)

Your job

Implement int parse_iq_header(const uint8_t *buf, size_t n, iq_hdr_t *out). Return 0 on valid magic, -1 on:

  • NULL inputs
  • n < 20
  • Bad magic

Common mistakes

  • Reading a u64 as two u32s in the wrong order. Little-endian means the low 4 bytes come first.
  • Casting (uint64_t *)buf — alignment-unsafe.
  • Forgetting that the magic is the first 4 bytes (no NUL).

What this is NOT

  • A demodulator. We don't touch the sample data.
  • A radio. We don't transmit anything.

Summary

20 bytes, one magic check, three integers, all little-endian.

Practice with these exercises