I would investigate a function like
void *memchr(const void *s, int c, size_t n);
The memchr() function scans the first n bytes of the memory area
pointed to by s for the character c. The first byte to match c
(interpreted as an unsigned character) stops the operation.
The memchr() and memrchr() functions return a pointer to the match‐
ing byte or NULL if the character does not occur in the given mem‐
ory area.
Copied from the manpages on GNU/Linux.
With this function you can leap through valid areas of data to store.