wmii/libixp2/convert.c

198 lines
4.9 KiB
C
Raw Normal View History

2005-11-18 18:54:58 +03:00
/*
2006-01-20 17:20:24 +03:00
* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
2005-11-18 18:54:58 +03:00
* See LICENSE file for license details.
*/
#include <stdlib.h>
#include <string.h>
#include "ixp.h"
/* encode/decode stuff */
2005-12-21 18:18:11 +03:00
void *
ixp_enc_u8(unsigned char *msg, unsigned char val)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
msg[0] = val;
return &msg[1];
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void *
ixp_dec_u8(unsigned char *msg, unsigned char *val)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
*val = msg[0];
return &msg[1];
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void *
ixp_enc_u16(unsigned char *msg, unsigned short val)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
msg[0] = val;
msg[1] = val >> 8;
return &msg[2];
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void *
ixp_dec_u16(unsigned char *msg, unsigned short *val)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
*val = msg[0] | (msg[1] << 8);
return &msg[2];
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void *
ixp_enc_u32(unsigned char *msg, unsigned int val)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
msg[0] = val;
msg[1] = val >> 8;
msg[2] = val >> 16;
msg[3] = val >> 24;
return &msg[4];
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void *
ixp_dec_u32(unsigned char *msg, unsigned int *val)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
*val = msg[0] | (msg[1] << 8) | (msg[2] << 16) | (msg[3] << 24);
return &msg[4];
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void *
ixp_enc_u64(unsigned char *msg, unsigned long long val)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
msg[0] = val;
msg[1] = val >> 8;
msg[2] = val >> 16;
msg[3] = val >> 24;
msg[4] = val >> 32;
msg[5] = val >> 40;
msg[6] = val >> 48;
msg[7] = val >> 56;
return &msg[8];
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void *
ixp_dec_u64(unsigned char *msg, unsigned long long *val)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
*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];
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void *
ixp_enc_string(unsigned char *msg, const char *s)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
unsigned short len = s ? strlen(s) : 0;
msg = ixp_enc_u16(msg, len);
if(s)
memcpy(msg, s, len);
return &msg[len];
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void *
ixp_dec_string(unsigned char *msg, char *string, unsigned short stringlen,
unsigned short *len)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
msg = ixp_dec_u16(msg, len);
if(!(*len))
return msg;
if(*len > stringlen - 1)
/* might never happen if stringlen == IXP_MAX_MSG */
string[0] = 0;
else {
memcpy(string, msg, *len);
string[*len] = 0;
}
return &msg[*len];
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void *
ixp_enc_data(unsigned char *msg, unsigned char *data, unsigned int datalen)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
memcpy(msg, data, datalen);
return &msg[datalen];
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void *
ixp_dec_data(unsigned char *msg, unsigned char *data, unsigned int datalen)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
memcpy(data, msg, datalen);
return &msg[datalen];
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void *
ixp_enc_prefix(unsigned char *msg, unsigned int size, unsigned char id,
unsigned short tag)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
msg = ixp_enc_u32(msg, size);
msg = ixp_enc_u8(msg, id);
return ixp_enc_u16(msg, tag);
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void *
ixp_dec_prefix(unsigned char *msg, unsigned int *size, unsigned char *id,
unsigned short *tag)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
msg = ixp_dec_u32(msg, size);
msg = ixp_dec_u8(msg, id);
return ixp_dec_u16(msg, tag);
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void *
ixp_enc_qid(unsigned char *msg, Qid * qid)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
msg = ixp_enc_u8(msg, qid->type);
msg = ixp_enc_u32(msg, qid->version);
return ixp_enc_u64(msg, qid->path);
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void *
ixp_dec_qid(unsigned char *msg, Qid * qid)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
msg = ixp_dec_u8(msg, &qid->type);
msg = ixp_dec_u32(msg, &qid->version);
return ixp_dec_u64(msg, &qid->path);
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void *
ixp_enc_stat(unsigned char *msg, Stat * stat)
2005-11-18 18:54:58 +03:00
{
msg = ixp_enc_u16(msg, ixp_sizeof_stat(stat) - sizeof(unsigned short));
2005-12-21 18:18:11 +03:00
msg = ixp_enc_u16(msg, stat->type);
msg = ixp_enc_u32(msg, stat->dev);
msg = ixp_enc_qid(msg, &stat->qid);
msg = ixp_enc_u32(msg, stat->mode);
msg = ixp_enc_u32(msg, stat->atime);
msg = ixp_enc_u32(msg, stat->mtime);
msg = ixp_enc_u64(msg, stat->length);
msg = ixp_enc_string(msg, stat->name);
msg = ixp_enc_string(msg, stat->uid);
msg = ixp_enc_string(msg, stat->gid);
return ixp_enc_string(msg, stat->muid);
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void *
ixp_dec_stat(unsigned char *msg, Stat * stat)
2005-11-18 18:54:58 +03:00
{
2006-01-07 18:50:05 +03:00
unsigned short dummy;
msg += sizeof(unsigned short);
2005-12-21 18:18:11 +03:00
msg = ixp_dec_u16(msg, &stat->type);
msg = ixp_dec_u32(msg, &stat->dev);
msg = ixp_dec_qid(msg, &stat->qid);
msg = ixp_dec_u32(msg, &stat->mode);
msg = ixp_dec_u32(msg, &stat->atime);
msg = ixp_dec_u32(msg, &stat->mtime);
msg = ixp_dec_u64(msg, &stat->length);
2006-01-07 18:50:05 +03:00
msg = ixp_dec_string(msg, stat->name, sizeof(stat->name), &dummy);
msg = ixp_dec_string(msg, stat->uid, sizeof(stat->uid), &dummy);
msg = ixp_dec_string(msg, stat->gid, sizeof(stat->gid), &dummy);
return ixp_dec_string(msg, stat->muid, sizeof(stat->muid), &dummy);
2005-11-18 18:54:58 +03:00
}