shithub: apl10

ref: 7b65afc1ad13f3859eca6eadaa2c45d864320304
dir: /util.c/

View raw version
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "dat.h"
#include "fns.h"

int
write1u(u8int *d, u8int v)
{
	d[0] = v;
	return 1;
}

int
write2u(u8int *d, u16int v)
{
	d[0] = (v >> 8) & 0xFF;
	d[1] = (v >> 0) & 0xFF;
	return 2;
}

int
write4u(u8int *d, u32int v)
{
	d[0] = (v >> 24) & 0xFF;
	d[1] = (v >> 16) & 0xFF;
	d[2] = (v >> 8) & 0xFF;
	d[3] = (v >> 0) & 0xFF;
	return 4;
}

int
write8u(u8int *d, u64int v)
{
	d[0] = (v >> 56) & 0xFF;
	d[1] = (v >> 48) & 0xFF;
	d[2] = (v >> 40) & 0xFF;
	d[3] = (v >> 32) & 0xFF;
	d[4] = (v >> 24) & 0xFF;
	d[5] = (v >> 16) & 0xFF;
	d[6] = (v >> 8) & 0xFF;
	d[7] = (v >> 0) & 0xFF;
	return 8;
}

int
write1s(u8int *d, s8int v)
{
	s8int *s = (s8int *)d;
	s[0] = v;
	return 1;
}

int
write2s(u8int *d, s16int v)
{
	s8int *s = (s8int *)d;
	s[0] = (v >> 8) & 0xFF;
	s[1] = (v >> 0) & 0xFF;
	return 2;
}

int
write4s(u8int *d, s32int v)
{
	s8int *s = (s8int *)d;
	s[0] = (v >> 24) & 0xFF;
	s[1] = (v >> 16) & 0xFF;
	s[2] = (v >> 8) & 0xFF;
	s[3] = (v >> 0) & 0xFF;
	return 4;
}

int
write8s(u8int *d, s64int v)
{
	s8int *s = (s8int *)d;
	s[0] = (v >> 56) & 0xFF;
	s[1] = (v >> 48) & 0xFF;
	s[2] = (v >> 40) & 0xFF;
	s[3] = (v >> 32) & 0xFF;
	s[4] = (v >> 24) & 0xFF;
	s[5] = (v >> 16) & 0xFF;
	s[6] = (v >> 8) & 0xFF;
	s[7] = (v >> 0) & 0xFF;
	return 8;
}

u8int
read1u(u8int *d)
{
	u8int v = 0;
	v |= ((u8int)d[0]) << 0;
	return v;
}

u16int
read2u(u8int *d)
{
	u16int v = 0;
	v |= ((u16int)d[0]) << 8;
	v |= ((u16int)d[1]) << 0;
	return v;
}

u32int
read4u(u8int *d)
{
	u32int v = 0;
	v |= ((u32int)d[0]) << 24;
	v |= ((u32int)d[1]) << 16;
	v |= ((u32int)d[2]) << 8;
	v |= ((u32int)d[3]) << 0;
	return v;
}

u64int
read8u(u8int *d)
{
	u64int v = 0;
	v |= ((u64int)d[0]) << 56;
	v |= ((u64int)d[1]) << 48;
	v |= ((u64int)d[2]) << 40;
	v |= ((u64int)d[3]) << 32;
	v |= ((u64int)d[4]) << 24;
	v |= ((u64int)d[5]) << 16;
	v |= ((u64int)d[6]) << 8;
	v |= ((u64int)d[7]) << 0;
	return v;
}

s8int
read1s(u8int *d)
{
	s8int v = 0;
	v |= ((s8int)d[0]) << 0;
	return v;
}

s16int
read2s(u8int *d)
{
	s16int v = 0;
	v |= ((s16int)d[0]) << 8;
	v |= ((s16int)d[1]) << 0;
	return v;
}

s32int
read4s(u8int *d)
{
	s32int v = 0;
	v |= ((s32int)d[0]) << 24;
	v |= ((s32int)d[1]) << 16;
	v |= ((s32int)d[2]) << 8;
	v |= ((s32int)d[3]) << 0;
	return v;
}

s64int
read8s(u8int *d)
{
	s64int v = 0;
	v |= ((s64int)d[0]) << 56;
	v |= ((s64int)d[1]) << 48;
	v |= ((s64int)d[2]) << 40;
	v |= ((s64int)d[3]) << 32;
	v |= ((s64int)d[4]) << 24;
	v |= ((s64int)d[5]) << 16;
	v |= ((s64int)d[6]) << 8;
	v |= ((s64int)d[7]) << 0;
	return v;
}