cybersecurity · intermediate · ~12 min · safe pentest lab
Mixed u32/u64 little-endian binary parsing with a magic check.
Parse the 20-byte header of an IQ-samples capture — the same alignment-safe binary-parse discipline as pcap and ELF, on a different fixture format.
Implement int parse_iq_header(const uint8_t *buf, size_t n, iq_hdr_t *out) where:
typedef struct {
uint32_t sample_rate_hz;
uint64_t center_freq_hz;
uint32_t sample_count;
} iq_hdr_t;
buf, n: a fixture byte buffer and its length, baked into the harness. Layout (20 bytes):"IQHD"sample_rate_hz (u32 LE)center_freq_hz (u64 LE)sample_count (u32 LE)out: the struct to fill on success.Returns int: 0 on valid magic and n >= 20 (filling out), or -1 on NULL inputs, n < 20, or wrong magic.
20-byte buffer with magic "IQHD" -> 0, fields filled
n < 20, or wrong magic, or NULL -> -1
n < 20 returns -1.((uint64_t)hi << 32) | lo); do not cast (uint64_t *)(buf + 8) (alignment-unsafe).The same alignment-safe binary-parse discipline you used on pcap and ELF works here on a different fixture format.
A byte buffer of at least 20 bytes + the output struct pointer.
0 on success, -1 otherwise. Struct fields filled only on success.
No pointer casts; build integers from bytes.
#include <stdint.h>
#include <stddef.h>
typedef struct {
uint32_t sample_rate_hz;
uint64_t center_freq_hz;
uint32_t sample_count;
} iq_hdr_t;
int parse_iq_header(const uint8_t *buf, size_t n, iq_hdr_t *out) {
/* TODO */
(void)buf; (void)n; (void)out;
return -1;
}
Using (uint64_t *) casts. Forgetting to memcmp the magic. n bound off-by-one.
Maximum u64 value. Magic mismatch with otherwise valid sizes.
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.