#ifndef _H_ENDIAN
#define _H_ENDIAN
#include <Security/utilities.h>
#include <Security/memutils.h>
#include <Security/debugging.h>
namespace Security {
inline uint32 h2n(uint32 v) { return htonl(v); }
inline sint32 h2n(sint32 v) { return htonl(v); }
inline uint16 h2n(uint16 v) { return htons(v); }
inline sint16 h2n(sint16 v) { return htons(v); }
inline uint8 h2n(uint8 v) { return v; }
inline sint8 h2n(sint8 v) { return v; }
inline uint32 n2h(uint32 v) { return ntohl(v); }
inline sint32 n2h(sint32 v) { return ntohl(v); }
inline uint16 n2h(uint16 v) { return ntohs(v); }
inline sint16 n2h(sint16 v) { return ntohs(v); }
inline uint8 n2h(uint8 v) { return v; }
inline sint8 n2h(sint8 v) { return v; }
template <class Base>
inline Base *h2n(Base *p) { return (Base *)h2n(LowLevelMemoryUtilities::PointerInt(p)); }
template <class Base>
inline Base *n2h(Base *p) { return (Base *)n2h(LowLevelMemoryUtilities::PointerInt(p)); }
template <class Type>
inline const Type &h2n(const Type &v)
{
secdebug("endian", "generic h2n called for type %s", Debug::typeName(v).c_str());
return v;
}
template <class Type>
inline const Type &n2h(const Type &v)
{
secdebug("endian", "generic n2h called for type %s", Debug::typeName(v).c_str());
return v;
}
template <class Type>
inline void h2ni(Type &v) { v = h2n(v); }
template <class Type>
inline void n2hi(Type &v) { v = n2h(v); }
void n2hi(CssmKey::Header &key);
void h2ni(CssmKey::Header &key);
inline void n2hi(CSSM_KEYHEADER &key) { n2hi(CssmKey::Header::overlay (key));}
inline void h2ni(CSSM_KEYHEADER &key) { h2ni(CssmKey::Header::overlay (key));}
template <class Type>
class Endian {
public:
typedef Type Value;
Endian() : mValue(0) { }
Endian(Value v) : mValue(h2n(v)) { }
operator Value () const { return n2h(mValue); }
Endian &operator = (Value v) { mValue = h2n(v); return *this; }
private:
Value mValue;
};
}
#endif //_H_ENDIAN