1998-04-22 20:58:34 +08:00
|
|
|
/*
|
2000-03-21 23:51:30 +08:00
|
|
|
* Unaligned Data Accesses -- Generic Version, Network Order
|
1998-04-22 20:58:34 +08:00
|
|
|
*
|
2000-03-21 23:51:30 +08:00
|
|
|
* (c) 2000 Martin Mares <mj@ucw.cz>
|
1998-04-22 20:58:34 +08:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _BIRD_UNALIGNED_H_
|
|
|
|
#define _BIRD_UNALIGNED_H_
|
|
|
|
|
2000-03-21 23:51:30 +08:00
|
|
|
/*
|
|
|
|
* We don't do any clever tricks with unaligned accesses since it's
|
|
|
|
* virtually impossible to figure out what alignment does the CPU want
|
|
|
|
* (unaligned accesses can be emulated by the OS which makes them work,
|
|
|
|
* but unusably slow). We use memcpy and hope GCC will optimize it out
|
|
|
|
* if possible.
|
|
|
|
*/
|
|
|
|
|
2016-04-12 17:14:54 +08:00
|
|
|
#include "sysdep/unix/endian.h"
|
2000-04-01 07:30:21 +08:00
|
|
|
#include "lib/string.h"
|
1998-05-27 05:38:06 +08:00
|
|
|
|
|
|
|
static inline u16
|
2015-11-24 20:52:26 +08:00
|
|
|
get_u16(const void *p)
|
1998-05-27 05:38:06 +08:00
|
|
|
{
|
|
|
|
u16 x;
|
2000-03-21 23:51:30 +08:00
|
|
|
memcpy(&x, p, 2);
|
|
|
|
return ntohs(x);
|
1998-05-27 05:38:06 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 22:00:07 +08:00
|
|
|
static inline u32
|
|
|
|
get_u24(const void *P)
|
|
|
|
{
|
|
|
|
const byte *p = P;
|
|
|
|
return (p[0] << 16) + (p[1] << 8) + p[2];
|
|
|
|
}
|
|
|
|
|
1998-05-27 05:38:06 +08:00
|
|
|
static inline u32
|
2015-11-24 20:52:26 +08:00
|
|
|
get_u32(const void *p)
|
1998-05-27 05:38:06 +08:00
|
|
|
{
|
|
|
|
u32 x;
|
2000-03-21 23:51:30 +08:00
|
|
|
memcpy(&x, p, 4);
|
|
|
|
return ntohl(x);
|
|
|
|
}
|
1998-05-27 05:38:06 +08:00
|
|
|
|
2015-11-13 23:10:33 +08:00
|
|
|
static inline u64
|
|
|
|
get_u64(const void *p)
|
|
|
|
{
|
|
|
|
u32 xh, xl;
|
|
|
|
memcpy(&xh, p, 4);
|
|
|
|
memcpy(&xl, p+4, 4);
|
|
|
|
return (((u64) ntohl(xh)) << 32) | ntohl(xl);
|
|
|
|
}
|
|
|
|
|
2000-03-21 23:51:30 +08:00
|
|
|
static inline void
|
|
|
|
put_u16(void *p, u16 x)
|
|
|
|
{
|
|
|
|
x = htons(x);
|
|
|
|
memcpy(p, &x, 2);
|
|
|
|
}
|
|
|
|
|
2017-03-22 22:00:07 +08:00
|
|
|
static inline void
|
|
|
|
put_u24(void *p, u32 x)
|
|
|
|
{
|
|
|
|
x = htonl(x);
|
|
|
|
memcpy(p, ((char *) &x) + 1, 3);
|
|
|
|
}
|
|
|
|
|
2000-03-21 23:51:30 +08:00
|
|
|
static inline void
|
|
|
|
put_u32(void *p, u32 x)
|
|
|
|
{
|
|
|
|
x = htonl(x);
|
|
|
|
memcpy(p, &x, 4);
|
1998-05-27 05:38:06 +08:00
|
|
|
}
|
|
|
|
|
2015-11-13 23:10:33 +08:00
|
|
|
static inline void
|
|
|
|
put_u64(void *p, u64 x)
|
|
|
|
{
|
|
|
|
u32 xh, xl;
|
|
|
|
xh = htonl(x >> 32);
|
|
|
|
xl = htonl((u32) x);
|
|
|
|
memcpy(p, &xh, 4);
|
|
|
|
memcpy(p+4, &xl, 4);
|
|
|
|
}
|
|
|
|
|
2016-12-07 21:11:28 +08:00
|
|
|
static inline void
|
|
|
|
get_u32s(const void *p, u32 *x, int n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
memcpy(x, p, 4*n);
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
x[i] = ntohl(x[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
put_u32s(void *p, const u32 *x, int n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
put_u32((byte *) p + 4*i, x[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-04-22 20:58:34 +08:00
|
|
|
#endif
|