Hello guys,


I have to read a 16 byte file header that will be piped into my program.
Normally, I just read the header, process it and continue reading the rest of the file later on.


Problem is when the file I’m receiving is encrypted. In that case, I have to decrypt the header first, and then the rest of the file. Separating the information like this results in the incorrect decryption of the rest of the file though, as the previous bytes affect the decryption of the subsequent bytes.


I could modify my decryption code to try to account for this data split, but it would be messy and require a lot of work. A simpler solution -which is what I do with regular, encrypted files, not piped from the stdin- is to read and decrypt the header, and then reset the file offset to the start of the file and later read everything again.


I can’t do this with the stdin though. lseek returns -1 and errno is set to “Illegal Seek”. I can’t open and close the file either as I don’t have a file name for stdin, only a file descriptor. Playing around with the dup() functions has been unsuccessful as well, as the cloned descriptor shares its file offset with the original; meaning reading from it moves the offset in stdin and using lseek has the same result.


So my question, is there an established “proper” way to reset the file offset to the beginning of the stdin? If not, is there a “hack” to accomplish the same (needs to work in Linux and OSX).


Thank you.