Originally Posted by
raczzoli
Hi.
I need to parse a header read from a socket, which has the following structure.
Bit 1. indicating if this is the final fragment of a message
Bit 2,3,4. is 0
Bit 5,6,7,8. is an opcode, indicating the type of the message (text/binary/control etc)
Bit 9. indicating that the message is masked or not
Bit 10-16. If its value is < 126, denotes the actual length of the message, if = 126, than the following 16 bits will indicate the length of the message, if = 127, the following 64 bits will be the actual length.
The header is actually the websocket protocol header, and it contains some other informations, but they are irrelevant for my question.
I write the parser in C, and I don`t know how to access a specific value in the header. I created a structure and mapped the values from the header to it, and it worked, but because the length of the header itself can change in some conditions, this is no longer an option.
I saw an example where the author extracted the length of the message (starting with the second byte in the string) with bitwise operations, like this:
int message length = tmp_buff[1] & 127
It gives the value correctly, but can somebody explain to me, how this actually works and maybe give me an example, how can I extract, let`s say the opcode, or the value from the two bytes, following the length?
Thank you.