#ifndef _H_ENDIAN
#define _H_ENDIAN
#include <machine/endian.h>
#include <libkern/OSByteOrder.h>
#include <security_utilities/utilities.h>
#include <security_utilities/memutils.h>
#include <security_utilities/debugging.h>
namespace Security {
#ifdef __LP64__
static inline unsigned long h2n(unsigned long v) { return OSSwapHostToBigInt64(v); }
static inline unsigned long n2h(unsigned long v) { return OSSwapBigToHostInt64(v); }
static inline unsigned long flip(unsigned long v) { return OSSwapInt64(v); }
static inline signed long h2n(signed long v) { return OSSwapHostToBigInt64(v); }
static inline signed long n2h(signed long v) { return OSSwapBigToHostInt64(v); }
static inline signed long flip(signed long v) { return OSSwapInt64(v); }
#else
static inline unsigned long h2n(unsigned long v) { return htonl(v); }
static inline unsigned long n2h(unsigned long v) { return ntohl(v); }
static inline unsigned long flip(unsigned long v) { return OSSwapInt32(v); }
static inline signed long h2n(signed long v) { return htonl(v); }
static inline signed long n2h(signed long v) { return ntohl(v); }
static inline signed long flip(signed long v) { return OSSwapInt32(v); }
#endif
static inline unsigned long long h2n(unsigned long long v) { return OSSwapHostToBigInt64(v); }
static inline unsigned long long n2h(unsigned long long v) { return OSSwapBigToHostInt64(v); }
static inline unsigned long long flip(unsigned long long v) { return OSSwapInt64(v); }
static inline long long h2n(long long v) { return OSSwapHostToBigInt64(v); }
static inline long long n2h(long long v) { return OSSwapBigToHostInt64(v); }
static inline long long flip(long long v) { return OSSwapInt64(v); }
static inline unsigned int h2n(unsigned int v) { return htonl(v); }
static inline unsigned int n2h(unsigned int v) { return ntohl(v); }
static inline unsigned int flip(unsigned int v) { return OSSwapInt32(v); }
static inline signed int h2n(int v) { return htonl(v); }
static inline signed int n2h(int v) { return ntohl(v); }
static inline signed int flip(int v) { return OSSwapInt32(v); }
static inline unsigned short h2n(unsigned short v) { return htons(v); }
static inline unsigned short n2h(unsigned short v) { return ntohs(v); }
static inline unsigned short flip(unsigned short v) { return OSSwapInt16(v); }
static inline signed short h2n(signed short v) { return htons(v); }
static inline signed short n2h(signed short v) { return ntohs(v); }
static inline signed short flip(signed short v) { return OSSwapInt16(v); }
static inline unsigned char h2n(unsigned char v) { return v; }
static inline unsigned char n2h(unsigned char v) { return v; }
static inline unsigned char flip(unsigned char v) { return v; }
static inline signed char h2n(signed char v) { return v; }
static inline signed char n2h(signed char v) { return v; }
static inline signed char flip(signed char v) { return v; }
template <class Base>
static inline Base *h2n(Base *p) { return (Base *)h2n(uintptr_t(p)); }
template <class Base>
static inline Base *n2h(Base *p) { return (Base *)n2h(uintptr_t(p)); }
template <class Type>
static inline void h2ni(Type &v) { v = h2n(v); }
template <class Type>
static inline void n2hi(Type &v) { v = n2h(v); }
template <class Type>
class Endian {
public:
typedef Type Value;
Endian() : mValue(Type(0)) { }
Endian(Value v) : mValue(h2n(v)) { }
Type get () const { return n2h(mValue); }
operator Value () const { return this->get(); }
Endian &operator = (Value v) { mValue = h2n(v); return *this; }
private:
Value mValue;
};
}
#endif //_H_ENDIAN