/*-1* Copyright 2007-2009 Colin Percival2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*25* This file was originally written by Colin Percival as part of the Tarsnap26* online backup system.27*/28#ifndef _SYSENDIAN_H_29#define _SYSENDIAN_H_3031/* If we don't have be64enc, the <sys/endian.h> we have isn't usable. */32#if !HAVE_DECL_BE64ENC33#undef HAVE_SYS_ENDIAN_H34#endif3536#ifdef HAVE_SYS_ENDIAN_H3738#include <sys/endian.h>3940#else4142#include <stdint.h>43444546static __inline uint64_t47be64dec(const void *pp)48{49const uint8_t *p = (uint8_t const *)pp;5051return ((uint64_t)(p[7]) + ((uint64_t)(p[6]) << 8) +52((uint64_t)(p[5]) << 16) + ((uint64_t)(p[4]) << 24) +53((uint64_t)(p[3]) << 32) + ((uint64_t)(p[2]) << 40) +54((uint64_t)(p[1]) << 48) + ((uint64_t)(p[0]) << 56));55}5657static __inline void58be64enc(void *pp, uint64_t x)59{60uint8_t * p = (uint8_t *)pp;6162p[7] = x & 0xff;63p[6] = (x >> 8) & 0xff;64p[5] = (x >> 16) & 0xff;65p[4] = (x >> 24) & 0xff;66p[3] = (x >> 32) & 0xff;67p[2] = (x >> 40) & 0xff;68p[1] = (x >> 48) & 0xff;69p[0] = (x >> 56) & 0xff;70}71727374static __inline uint64_t75le64dec(const void *pp)76{77const uint8_t *p = (uint8_t const *)pp;7879return ((uint64_t)(p[0]) + ((uint64_t)(p[1]) << 8) +80((uint64_t)(p[2]) << 16) + ((uint64_t)(p[3]) << 24) +81((uint64_t)(p[4]) << 32) + ((uint64_t)(p[5]) << 40) +82((uint64_t)(p[6]) << 48) + ((uint64_t)(p[7]) << 56));83}8485static __inline void86le64enc(void *pp, uint64_t x)87{88uint8_t * p = (uint8_t *)pp;8990p[0] = x & 0xff;91p[1] = (x >> 8) & 0xff;92p[2] = (x >> 16) & 0xff;93p[3] = (x >> 24) & 0xff;94p[4] = (x >> 32) & 0xff;95p[5] = (x >> 40) & 0xff;96p[6] = (x >> 48) & 0xff;97p[7] = (x >> 56) & 0xff;98}99100101static __inline uint32_t102be32dec(const void *pp)103{104const uint8_t *p = (uint8_t const *)pp;105106return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) +107((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24));108}109110static __inline void111be32enc(void *pp, uint32_t x)112{113uint8_t * p = (uint8_t *)pp;114115p[3] = x & 0xff;116p[2] = (x >> 8) & 0xff;117p[1] = (x >> 16) & 0xff;118p[0] = (x >> 24) & 0xff;119}120121#endif /* !HAVE_SYS_ENDIAN_H */122123#endif /* !_SYSENDIAN_H_ */124125126