ixp_unpack_* functions cleanup

This commit is contained in:
Mikhail Gusarov 2006-06-12 18:34:59 +07:00
parent e02481607e
commit 185ed448a6
5 changed files with 119 additions and 123 deletions

View File

@ -150,21 +150,21 @@ static void
xls(void *result, unsigned int msize, int details)
{
unsigned int n = 0, i = 0;
void *p = result;
unsigned char *p = result;
Stat *dir;
static Stat stat;
do {
p = ixp_unpack_stat(p, &stat);
ixp_unpack_stat(&p, &stat);
n++;
}
while(p - result < msize);
while(p - (unsigned char*)result < msize);
dir = (Stat *)cext_emallocz(sizeof(Stat) * n);
p = result;
do {
p = ixp_unpack_stat(p, &dir[i++]);
ixp_unpack_stat(&p, &dir[i++]);
}
while(p - result < msize);
while(p - (unsigned char*)result < msize);
qsort(dir, n, sizeof(Stat), comp_stat);
for(i = 0; i < n; i++)
print_stat(&dir[i], details);

View File

@ -16,11 +16,10 @@ ixp_pack_u8(unsigned char **msg, int *msize, unsigned char val)
*(*msg)++ = val;
}
void *
ixp_unpack_u8(unsigned char *msg, unsigned char *val)
void
ixp_unpack_u8(unsigned char **msg, unsigned char *val)
{
*val = msg[0];
return &msg[1];
*val = *(*msg)++;
}
void
@ -32,11 +31,11 @@ ixp_pack_u16(unsigned char **msg, int *msize, unsigned short val)
}
}
void *
ixp_unpack_u16(unsigned char *msg, unsigned short *val)
void
ixp_unpack_u16(unsigned char **msg, unsigned short *val)
{
*val = msg[0] | (msg[1] << 8);
return &msg[2];
*val = *(*msg)++;
*val |= *(*msg)++ << 8;
}
void
@ -50,11 +49,13 @@ ixp_pack_u32(unsigned char **msg, int *msize, unsigned int val)
}
}
void *
ixp_unpack_u32(unsigned char *msg, unsigned int *val)
void
ixp_unpack_u32(unsigned char **msg, unsigned int *val)
{
*val = msg[0] | (msg[1] << 8) | (msg[2] << 16) | (msg[3] << 24);
return &msg[4];
*val = *(*msg)++;
*val |= *(*msg)++ << 8;
*val |= *(*msg)++ << 16;
*val |= *(*msg)++ << 24;
}
void
@ -72,18 +73,13 @@ ixp_pack_u64(unsigned char **msg, int *msize, unsigned long long val)
}
}
void *
ixp_unpack_u64(unsigned char *msg, unsigned long long *val)
void
ixp_unpack_u64(unsigned char **msg, unsigned long long *val)
{
*val = (unsigned long long) msg[0] |
((unsigned long long) msg[1] << 8) |
((unsigned long long) msg[2] << 16) |
((unsigned long long) msg[3] << 24) |
((unsigned long long) msg[4] << 32) |
((unsigned long long) msg[5] << 40) |
((unsigned long long) msg[6] << 48) |
((unsigned long long) msg[7] << 56);
return &msg[8];
int i;
*val = 0;
for (i = 0; i < 8; ++i)
*val |= (unsigned long long) *(*msg)++ << (8 * i);
}
void
@ -95,21 +91,21 @@ ixp_pack_string(unsigned char **msg, int *msize, const char *s)
ixp_pack_data(msg, msize, (void *)s, len);
}
void *
ixp_unpack_string(unsigned char *msg, char *string, unsigned short stringlen,
void
ixp_unpack_string(unsigned char **msg, char *string, unsigned short stringlen,
unsigned short *len)
{
msg = ixp_unpack_u16(msg, len);
if(!(*len))
return msg;
if(*len > stringlen - 1)
ixp_unpack_u16(msg, len);
if (!*len)
return;
if (*len > stringlen - 1) {
/* might never happen if stringlen == IXP_MAX_MSG */
string[0] = 0;
else {
memcpy(string, msg, *len);
} else {
memcpy(string, *msg, *len);
string[*len] = 0;
*msg += *len;
}
return &msg[*len];
}
void
@ -121,31 +117,31 @@ ixp_pack_data(unsigned char **msg, int *msize, unsigned char *data, unsigned int
}
}
void *
ixp_unpack_data(unsigned char *msg, unsigned char *data, unsigned int datalen)
void
ixp_unpack_data(unsigned char **msg, unsigned char *data, unsigned int datalen)
{
memcpy(data, msg, datalen);
return &msg[datalen];
memcpy(data, *msg, datalen);
*msg += datalen;
}
void
ixp_pack_prefix(unsigned char *msg, unsigned int size, unsigned char id,
unsigned short tag)
{
unsigned int dummy = sizeof(unsigned char) +
int dummy = sizeof(unsigned char) +
sizeof(unsigned short) + sizeof(unsigned int);
ixp_pack_u32(&msg, &dummy, size);
ixp_pack_u8(&msg, &dummy, id);
ixp_pack_u16(&msg, &dummy, tag);
}
void *
ixp_unpack_prefix(unsigned char *msg, unsigned int *size, unsigned char *id,
void
ixp_unpack_prefix(unsigned char **msg, unsigned int *size, unsigned char *id,
unsigned short *tag)
{
msg = ixp_unpack_u32(msg, size);
msg = ixp_unpack_u8(msg, id);
return ixp_unpack_u16(msg, tag);
ixp_unpack_u32(msg, size);
ixp_unpack_u8(msg, id);
ixp_unpack_u16(msg, tag);
}
void
@ -156,12 +152,12 @@ ixp_pack_qid(unsigned char **msg, int *msize, Qid * qid)
ixp_pack_u64(msg, msize, qid->path);
}
void *
ixp_unpack_qid(unsigned char *msg, Qid * qid)
void
ixp_unpack_qid(unsigned char **msg, Qid * qid)
{
msg = ixp_unpack_u8(msg, &qid->type);
msg = ixp_unpack_u32(msg, &qid->version);
return ixp_unpack_u64(msg, &qid->path);
ixp_unpack_u8(msg, &qid->type);
ixp_unpack_u32(msg, &qid->version);
ixp_unpack_u64(msg, &qid->path);
}
void
@ -181,20 +177,20 @@ ixp_pack_stat(unsigned char **msg, int *msize, Stat * stat)
ixp_pack_string(msg, msize, stat->muid);
}
void *
ixp_unpack_stat(unsigned char *msg, Stat * stat)
void
ixp_unpack_stat(unsigned char **msg, Stat * stat)
{
unsigned short dummy;
msg += sizeof(unsigned short);
msg = ixp_unpack_u16(msg, &stat->type);
msg = ixp_unpack_u32(msg, &stat->dev);
msg = ixp_unpack_qid(msg, &stat->qid);
msg = ixp_unpack_u32(msg, &stat->mode);
msg = ixp_unpack_u32(msg, &stat->atime);
msg = ixp_unpack_u32(msg, &stat->mtime);
msg = ixp_unpack_u64(msg, &stat->length);
msg = ixp_unpack_string(msg, stat->name, sizeof(stat->name), &dummy);
msg = ixp_unpack_string(msg, stat->uid, sizeof(stat->uid), &dummy);
msg = ixp_unpack_string(msg, stat->gid, sizeof(stat->gid), &dummy);
return ixp_unpack_string(msg, stat->muid, sizeof(stat->muid), &dummy);
*msg += sizeof(unsigned short);
ixp_unpack_u16(msg, &stat->type);
ixp_unpack_u32(msg, &stat->dev);
ixp_unpack_qid(msg, &stat->qid);
ixp_unpack_u32(msg, &stat->mode);
ixp_unpack_u32(msg, &stat->atime);
ixp_unpack_u32(msg, &stat->mtime);
ixp_unpack_u64(msg, &stat->length);
ixp_unpack_string(msg, stat->name, sizeof(stat->name), &dummy);
ixp_unpack_string(msg, stat->uid, sizeof(stat->uid), &dummy);
ixp_unpack_string(msg, stat->gid, sizeof(stat->gid), &dummy);
ixp_unpack_string(msg, stat->muid, sizeof(stat->muid), &dummy);
}

View File

@ -202,28 +202,28 @@ int ixp_client_do_fcall(IXPClient * c);
/* convert.c */
void ixp_pack_u8(unsigned char **msg, int *msize, unsigned char val);
void *ixp_unpack_u8(unsigned char *msg, unsigned char *val);
void ixp_unpack_u8(unsigned char **msg, unsigned char *val);
void ixp_pack_u16(unsigned char **msg, int *msize, unsigned short val);
void *ixp_unpack_u16(unsigned char *msg, unsigned short *val);
void ixp_unpack_u16(unsigned char **msg, unsigned short *val);
void ixp_pack_u32(unsigned char **msg, int *msize, unsigned int val);
void *ixp_unpack_u32(unsigned char *msg, unsigned int *val);
void ixp_unpack_u32(unsigned char **msg, unsigned int *val);
void ixp_pack_u64(unsigned char **msg, int *msize, unsigned long long val);
void *ixp_unpack_u64(unsigned char *msg, unsigned long long *val);
void ixp_unpack_u64(unsigned char **msg, unsigned long long *val);
void ixp_pack_string(unsigned char **msg, int *msize, const char *s);
void *ixp_unpack_string(unsigned char *msg, char *string,
void ixp_unpack_string(unsigned char **msg, char *string,
unsigned short stringlen, unsigned short *len);
void ixp_pack_data(unsigned char **msg, int *msize, unsigned char *data,
unsigned int datalen);
void *ixp_unpack_data(unsigned char *msg, unsigned char *data,
void ixp_unpack_data(unsigned char **msg, unsigned char *data,
unsigned int datalen);
void ixp_pack_prefix(unsigned char *msg, unsigned int size,
unsigned char id, unsigned short tag);
void *ixp_unpack_prefix(unsigned char *msg, unsigned int *size,
void ixp_unpack_prefix(unsigned char **msg, unsigned int *size,
unsigned char *id, unsigned short *tag);
void ixp_pack_qid(unsigned char **msg, int *msize, Qid *qid);
void *ixp_unpack_qid(unsigned char *msg, Qid *qid);
void ixp_unpack_qid(unsigned char **msg, Qid *qid);
void ixp_pack_stat(unsigned char **msg, int *msize, Stat *stat);
void *ixp_unpack_stat(unsigned char *msg, Stat *stat);
void ixp_unpack_stat(unsigned char **msg, Stat *stat);
/* message.c */
unsigned short ixp_sizeof_stat(Stat *stat);

View File

@ -141,97 +141,98 @@ ixp_msg2fcall(Fcall *fcall, void *msg, unsigned int msglen)
{
unsigned int i, msize;
unsigned short len;
void *p = ixp_unpack_prefix(msg, &msize, &fcall->id, &fcall->tag);
unsigned char *p = msg;
ixp_unpack_prefix(&p, &msize, &fcall->id, &fcall->tag);
if(msize > msglen) /* bad message */
return 0;
switch (fcall->id) {
case TVERSION:
case RVERSION:
p = ixp_unpack_u32(p, &fcall->maxmsg);
p = ixp_unpack_string(p, fcall->version, sizeof(fcall->version), &len);
ixp_unpack_u32(&p, &fcall->maxmsg);
ixp_unpack_string(&p, fcall->version, sizeof(fcall->version), &len);
break;
case TAUTH:
p = ixp_unpack_u32(p, &fcall->afid);
p = ixp_unpack_string(p, fcall->uname, sizeof(fcall->uname), &len);
p = ixp_unpack_string(p, fcall->aname, sizeof(fcall->aname), &len);
ixp_unpack_u32(&p, &fcall->afid);
ixp_unpack_string(&p, fcall->uname, sizeof(fcall->uname), &len);
ixp_unpack_string(&p, fcall->aname, sizeof(fcall->aname), &len);
break;
case RAUTH:
p = ixp_unpack_qid(p, &fcall->aqid);
ixp_unpack_qid(&p, &fcall->aqid);
break;
case RATTACH:
p = ixp_unpack_qid(p, &fcall->qid);
ixp_unpack_qid(&p, &fcall->qid);
break;
case TATTACH:
p = ixp_unpack_u32(p, &fcall->fid);
p = ixp_unpack_u32(p, &fcall->afid);
p = ixp_unpack_string(p, fcall->uname, sizeof(fcall->uname), &len);
p = ixp_unpack_string(p, fcall->aname, sizeof(fcall->aname), &len);
ixp_unpack_u32(&p, &fcall->fid);
ixp_unpack_u32(&p, &fcall->afid);
ixp_unpack_string(&p, fcall->uname, sizeof(fcall->uname), &len);
ixp_unpack_string(&p, fcall->aname, sizeof(fcall->aname), &len);
break;
case RERROR:
p = ixp_unpack_string(p, fcall->errstr, sizeof(fcall->errstr), &len);
ixp_unpack_string(&p, fcall->errstr, sizeof(fcall->errstr), &len);
break;
case TFLUSH:
p = ixp_unpack_u16(p, &fcall->oldtag);
ixp_unpack_u16(&p, &fcall->oldtag);
break;
case TWALK:
p = ixp_unpack_u32(p, &fcall->fid);
p = ixp_unpack_u32(p, &fcall->newfid);
p = ixp_unpack_u16(p, &fcall->nwname);
ixp_unpack_u32(&p, &fcall->fid);
ixp_unpack_u32(&p, &fcall->newfid);
ixp_unpack_u16(&p, &fcall->nwname);
for(i = 0; i < fcall->nwname; i++)
p = ixp_unpack_string(p, fcall->wname[i], IXP_MAX_FLEN, &len);
ixp_unpack_string(&p, fcall->wname[i], IXP_MAX_FLEN, &len);
break;
case RWALK:
p = ixp_unpack_u16(p, &fcall->nwqid);
ixp_unpack_u16(&p, &fcall->nwqid);
for(i = 0; i < fcall->nwqid; i++)
p = ixp_unpack_qid(p, &fcall->wqid[i]);
ixp_unpack_qid(&p, &fcall->wqid[i]);
break;
case TOPEN:
p = ixp_unpack_u32(p, &fcall->fid);
p = ixp_unpack_u8(p, &fcall->mode);
ixp_unpack_u32(&p, &fcall->fid);
ixp_unpack_u8(&p, &fcall->mode);
break;
case ROPEN:
case RCREATE:
p = ixp_unpack_qid(p, &fcall->qid);
p = ixp_unpack_u32(p, &fcall->iounit);
ixp_unpack_qid(&p, &fcall->qid);
ixp_unpack_u32(&p, &fcall->iounit);
break;
case TCREATE:
p = ixp_unpack_u32(p, &fcall->fid);
p = ixp_unpack_string(p, fcall->name, sizeof(fcall->name), &len);
p = ixp_unpack_u32(p, &fcall->perm);
p = ixp_unpack_u8(p, &fcall->mode);
ixp_unpack_u32(&p, &fcall->fid);
ixp_unpack_string(&p, fcall->name, sizeof(fcall->name), &len);
ixp_unpack_u32(&p, &fcall->perm);
ixp_unpack_u8(&p, &fcall->mode);
break;
case TREAD:
p = ixp_unpack_u32(p, &fcall->fid);
p = ixp_unpack_u64(p, &fcall->offset);
p = ixp_unpack_u32(p, &fcall->count);
ixp_unpack_u32(&p, &fcall->fid);
ixp_unpack_u64(&p, &fcall->offset);
ixp_unpack_u32(&p, &fcall->count);
break;
case RREAD:
p = ixp_unpack_u32(p, &fcall->count);
p = ixp_unpack_data(p, fcall->data, fcall->count);
ixp_unpack_u32(&p, &fcall->count);
ixp_unpack_data(&p, fcall->data, fcall->count);
break;
case TWRITE:
p = ixp_unpack_u32(p, &fcall->fid);
p = ixp_unpack_u64(p, &fcall->offset);
p = ixp_unpack_u32(p, &fcall->count);
p = ixp_unpack_data(p, fcall->data, fcall->count);
ixp_unpack_u32(&p, &fcall->fid);
ixp_unpack_u64(&p, &fcall->offset);
ixp_unpack_u32(&p, &fcall->count);
ixp_unpack_data(&p, fcall->data, fcall->count);
break;
case RWRITE:
p = ixp_unpack_u32(p, &fcall->count);
ixp_unpack_u32(&p, &fcall->count);
break;
case TCLUNK:
case TREMOVE:
case TSTAT:
p = ixp_unpack_u32(p, &fcall->fid);
ixp_unpack_u32(&p, &fcall->fid);
break;
case RSTAT:
p = ixp_unpack_u16(p, &len);
p = ixp_unpack_stat(p, &fcall->stat);
ixp_unpack_u16(&p, &len);
ixp_unpack_stat(&p, &fcall->stat);
break;
case TWSTAT:
p = ixp_unpack_u32(p, &fcall->fid);
p = ixp_unpack_u16(p, &len);
p = ixp_unpack_stat(p, &fcall->stat);
ixp_unpack_u32(&p, &fcall->fid);
ixp_unpack_u16(&p, &len);
ixp_unpack_stat(&p, &fcall->stat);
break;
}

View File

@ -64,15 +64,14 @@ ixp_recv_message(int fd, void *msg, unsigned int msglen, char **errstr)
if(ixp_recv_data(fd, msg, sizeof(unsigned int), errstr) !=
sizeof(unsigned int))
return 0;
ixp_unpack_u32(msg, &msize);
ixp_unpack_u32((unsigned char**)&msg, &msize);
if(msize > msglen) {
*errstr = "invalid message header";
return 0;
}
/* receive message */
if(ixp_recv_data(fd, msg + sizeof(unsigned int),
msize - sizeof(unsigned int), errstr)
!= msize - sizeof(unsigned int))
if(ixp_recv_data(fd, msg, msize - sizeof(unsigned int), errstr)
!= msize - sizeof(unsigned int))
return 0;
return msize;
}