Forgive me if this is completely obvious, but I am sending data over the network using a byte array, and I need to stuff an integer into it and then get it back out at the other side.
Type definitions in the example:
uint8_t *dest;
uint8_t *ciphertext;
size_t cbytes; // length of ciphertext
uint8_t iv[16];
uint8_t tag[16];
Relevant portion of the writer:
size_t bytes = 0;
memcpy(&dest[bytes], iv, sizeof(iv));
bytes = sizeof(iv);
memcpy(&dest[bytes], (void*)htonl(cbytes), sizeof(uint32_t));
bytes += sizeof(uint32_t);
memcpy(&dest[bytes], ciphertext, cbytes);
bytes += cbytes;
memcpy(&dest[bytes], tag, sizeof(tag));
bytes += sizeof(tag);
Is this a correct way of stuffing cbytes
, as an integer into the byte array? If not, what is a better way of doing it?
Now, with this byte array, how do I read cbytes
back out into an integer (or size_t)? The rest of it can just be copied back out, but not sure what to do about the integer.