cybersecurity · intermediate · ~20 min
Robust binary-header parsing with bounds + sentinel checks.
Extract three identity fields from a mock ELF64 header — the universal Linux binary fingerprint that every reverse-engineering tool reads first.
Implement int parse_elf64_ident(const unsigned char *buf, int len, int *class_, int *data, int *machine) that fills the three output fields from the header.
buf, len: a fixture byte buffer (the header's first bytes) and its length, baked into the harness. Fields:class = buf[4]: 1 = 32-bit, 2 = 64-bitdata = buf[5]: 1 = little-endian (LSB), 2 = big-endian (MSB)machine = buf[18..19] little-endian (only LSB binaries are tested)class_, data, machine: output pointers, written only on success.Returns int: 1 on success (filling the three outputs), or 0 on failure.
ELF64 LSB x86_64 header -> 1, class=2, data=1, machine=0x3E
short buffer / bad magic -> 0
buf is NULL, len < 20, the first 4 bytes are not the ELF magic, or any output pointer is NULL.The ELF header is the universal Linux binary fingerprint. Knowing how to extract its fields makes you fluent in any reverse-engineering tool.
A byte buffer buf (>= 20 bytes) and its length, plus three output int pointers.
An int: 1 on success with class_/data/machine filled, else 0.
Bounds-check every read. Validate the ELF magic and non-NULL outputs.
#include <stddef.h>
int parse_elf64_ident(const unsigned char *buf, int len, int *class_, int *data, int *machine) { /* TODO */ (void)buf; (void)len; (void)class_; (void)data; (void)machine; return 0; }
Indexing buf[18] without confirming len >= 20.
Magic mismatch; short buffer; NULL outputs.
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.