Add a kind of hacky Frankenstein C version of doing the validation

This is based on bits of code I had lying around from other projects. It can probably be cleaned up and made much more canonical - I'm very out of practice with C so someone is likely to vomit from how bad this is (especially since it's gratuitously not C89).