I am trying to work out the difference (in bytes) between two members of a packed structure.
The structure is defined like this:
Code:
struct __attribute__((packed)) jcamsys_msg_image
{
unsigned char magic[2];
uint32_t msglen;
uint16_t msgtype; // JC_MSG_... or JC_MSG_...
uint16_t reqid;
uint16_t cam;
uint16_t image_type; // at the moment only JC_IMAGE_TYPE_JPEG
char crypted; // TRUE if data that follows is encrypted
uint32_t datalen;
uint16_t width;
uint16_t height;
unsigned char data[JC_MAX_MSGLEN-JC_MAX_HEADERLEN]; // image data
};
Code:
int jc_msg_image(struct jcamsys_key* key, int *fd, int reqid, int crypted, int cam, unsigned char*data, int datasize, int datalen, int image_type, int width, int height)
{
struct jcamsys_msg_image* msgi =(struct jcamsys_msg_image*)sendbuf;
int w=0;
msgi->magic[0] = MAGIC0;
msgi->magic[1] = MAGIC0;
!!! msgi->msglen = ( ((uintptr_t)msgi->data - (uintptr_t)msgi->msgtype) ) + datalen; // len = remaining header size + data size
msgi->msgtype = JC_MSG_IMAGE;
msgi->reqid = jc_nextreqid(reqid);
msgi->cam = cam;
msgi->image_type= JC_IMAGE_TYPE_JPEG;
msgi->crypted = crypted;
msgi->datalen = datalen;
msgi->width = width;
msgi->height = height;
if (crypted==TRUE)
jcam_crypt_buf_copy(key, msgi->data, data, datasize, datalen); // copy as we crypt
else memcpy(msgi->data, data, datalen); // else just copy
printf("sending image message msgi->msglen=%d\n",msgi->msglen); fflush(stdout);
if (jc_sockwrite(fd, (unsigned char*)&sendbuf, msgi->msglen)<0)
return(JC_ERR);
else return(msgi->reqid);
}
The issue is that:
((uintptr_t)msgi->data - (uintptr_t)msgi->msgtype)
gives some huge number, it should be 15 ?
What I am trying to say is "as an integer" what the difference between the address of structure member data and address of structure member msgtype.
Anyone know what I am doing wrong?
Many Thanks,
Jon