2014-12-11 10:50:15 +03:00
|
|
|
// Copyright (c) 2012-2014 Konstantin Isakov <ikm@zbackup.org> and ZBackup contributors, see CONTRIBUTORS
|
2014-10-10 13:50:55 +04:00
|
|
|
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE
|
2013-07-18 21:33:25 +04:00
|
|
|
|
|
|
|
#ifndef ENDIAN_HH_INCLUDED__
|
|
|
|
#define ENDIAN_HH_INCLUDED__
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <arpa/inet.h>
|
2013-07-20 22:32:41 +04:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <machine/endian.h>
|
|
|
|
#else
|
2013-07-18 21:33:25 +04:00
|
|
|
#include <endian.h>
|
2013-07-20 22:32:41 +04:00
|
|
|
#endif
|
2013-07-18 21:33:25 +04:00
|
|
|
|
2014-01-15 21:48:05 +04:00
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2013-07-18 21:33:25 +04:00
|
|
|
|
|
|
|
/// Converts the given host-order value to big-endian value
|
|
|
|
inline uint32_t toBigEndian( uint32_t v ) { return htonl( v ); }
|
|
|
|
/// Converts the given host-order value to little-endian value
|
|
|
|
inline uint32_t toLittleEndian( uint32_t v ) { return v; }
|
|
|
|
inline uint64_t toLittleEndian( uint64_t v ) { return v; }
|
|
|
|
/// Converts the given little-endian value to host-order value
|
|
|
|
inline uint32_t fromLittleEndian( uint32_t v ) { return v; }
|
|
|
|
inline uint64_t fromLittleEndian( uint64_t v ) { return v; }
|
|
|
|
|
2014-01-15 21:48:05 +04:00
|
|
|
#elif __BYTE_ORDER == __BIG_ENDIAN
|
|
|
|
|
|
|
|
// Note: the functions used are non-standard. Add more ifdefs if needed
|
|
|
|
|
|
|
|
/// Converts the given host-order value to big-endian value
|
|
|
|
inline uint32_t toBigEndian( uint32_t v ) { return v; }
|
|
|
|
/// Converts the given host-order value to little-endian value
|
|
|
|
inline uint32_t toLittleEndian( uint32_t v ) { return htole32( v ); }
|
|
|
|
inline uint64_t toLittleEndian( uint64_t v ) { return htole64( v ); }
|
|
|
|
/// Converts the given little-endian value to host-order value
|
|
|
|
inline uint32_t fromLittleEndian( uint32_t v ) { return le32toh( v ); }
|
|
|
|
inline uint64_t fromLittleEndian( uint64_t v ) { return le64toh( v ); }
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#error Please add support for architectures different from little-endian and\
|
|
|
|
big-endian.
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-07-18 21:33:25 +04:00
|
|
|
#endif
|