endian_enc.h revision 243730
18870Srgrimes/*- 250476Speter * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org> 38870Srgrimes * Copyright (c) 2005 Robert N. M. Watson 42116Sjkh * All rights reserved. 52116Sjkh * 68870Srgrimes * Redistribution and use in source and binary forms, with or without 72116Sjkh * modification, are permitted provided that the following conditions 82116Sjkh * are met: 98870Srgrimes * 1. Redistributions of source code must retain the above copyright 102116Sjkh * notice, this list of conditions and the following disclaimer. 112116Sjkh * 2. Redistributions in binary form must reproduce the above copyright 128870Srgrimes * notice, this list of conditions and the following disclaimer in the 138870Srgrimes * documentation and/or other materials provided with the distribution. 142116Sjkh * 15212518Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16131001Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17141281Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18212518Simp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1933662Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 202116Sjkh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21141281Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22130149Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23180581Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24251404Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25251404Sdas * SUCH DAMAGE. 26251404Sdas * 27251404Sdas * Derived from FreeBSD src/sys/sys/endian.h:1.6. 282116Sjkh * $P4: //depot/projects/trustedbsd/openbsm/compat/endian_enc.h#1 $ 29174684Sdas */ 30174684Sdas 31174684Sdas#ifndef _COMPAT_ENDIAN_ENC_H_ 32181074Sdas#define _COMPAT_ENDIAN_ENC_H_ 33174684Sdas 34174684Sdas/* 35181074Sdas * Some systems will have the uint/int types defined here already, others 36174684Sdas * will need stdint.h. 37174684Sdas */ 38284289Ssjg#ifdef HAVE_STDINT_H 39284289Ssjg#include <stdint.h> 40180581Sdas#endif 41180581Sdas 42180581Sdas/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */ 43180581Sdas 442116Sjkhstatic __inline uint16_t 45284421Sbaptbe16dec(const void *pp) 46169807Sdeischen{ 47181064Sdas unsigned char const *p = (unsigned char const *)pp; 48262613Sdim 4993211Sbde return ((p[0] << 8) | p[1]); 5093211Sbde} 512116Sjkh 522116Sjkhstatic __inline uint32_t 532116Sjkhbe32dec(const void *pp) 542116Sjkh{ 55216211Sdas unsigned char const *p = (unsigned char const *)pp; 56216211Sdas 572116Sjkh return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); 58130149Sdas} 59255294Stheraven 60226597Sdasstatic __inline uint64_t 61141280Sdasbe64dec(const void *pp) 62181074Sdas{ 63176243Sbde unsigned char const *p = (unsigned char const *)pp; 64174617Sdas 65174617Sdas return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4)); 66144650Sdas} 67144650Sdas 68176243Sbdestatic __inline uint16_t 69140609Sdasle16dec(const void *pp) 70131320Sdas{ 71143222Sdas unsigned char const *p = (unsigned char const *)pp; 72144772Sdas 73140088Sdas return ((p[1] << 8) | p[0]); 74144772Sdas} 75174684Sdas 76144091Sdasstatic __inline uint32_t 77257818Skarglle32dec(const void *pp) 78132382Sdas{ 79176243Sbde unsigned char const *p = (unsigned char const *)pp; 80176388Sdas 81141280Sdas return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]); 822116Sjkh} 83121497Sdes 84121497Sdesstatic __inline uint64_t 85211934Snwhitehornle64dec(const void *pp) 86211934Snwhitehorn{ 87211934Snwhitehorn unsigned char const *p = (unsigned char const *)pp; 88211934Snwhitehorn 89211934Snwhitehorn return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p)); 90176361Sdas} 91211934Snwhitehorn 92157196Sdeischenstatic __inline void 93121497Sdesbe16enc(void *pp, uint16_t u) 94157196Sdeischen{ 95157196Sdeischen unsigned char *p = (unsigned char *)pp; 96157196Sdeischen 97121497Sdes p[0] = (u >> 8) & 0xff; 98175309Sdas p[1] = u & 0xff; 99143222Sdas} 100143222Sdas 101251599Sdasstatic __inline void 102260067Skarglbe32enc(void *pp, uint32_t u) 103271651Skargl{ 104260067Skargl unsigned char *p = (unsigned char *)pp; 105181074Sdas 106251599Sdas p[0] = (u >> 24) & 0xff; 107268593Skargl p[1] = (u >> 16) & 0xff; 108251292Sdas p[2] = (u >> 8) & 0xff; 109260067Skargl p[3] = u & 0xff; 110260067Skargl} 111143222Sdas 112121497Sdesstatic __inline void 113129864Sstefanfbe64enc(void *pp, uint64_t u) 114251121Sdas{ 115251121Sdas unsigned char *p = (unsigned char *)pp; 116226458Sdas 117219359Sdas be32enc(p, u >> 32); 118226458Sdas be32enc(p + 4, u & 0xffffffff); 119226458Sdas} 120129864Sstefanf 1212116Sjkhstatic __inline void 1222116Sjkhle16enc(void *pp, uint16_t u) 1232116Sjkh{ 124117912Speter unsigned char *p = (unsigned char *)pp; 125117912Speter 1266953Sbde p[0] = u & 0xff; 127117912Speter p[1] = (u >> 8) & 0xff; 1286953Sbde} 129117912Speter 1306953Sbdestatic __inline void 131117912Speterle32enc(void *pp, uint32_t u) 132117912Speter{ 133230192Sdas unsigned char *p = (unsigned char *)pp; 13496462Sru 135226458Sdas p[0] = u & 0xff; 136251121Sdas p[1] = (u >> 8) & 0xff; 137174617Sdas p[2] = (u >> 16) & 0xff; 138143709Sdas p[3] = (u >> 24) & 0xff; 139143709Sdas} 140140890Sdas 141175462Sdasstatic __inline void 142175462Sdasle64enc(void *pp, uint64_t u) 143218877Smurray{ 144218877Smurray unsigned char *p = (unsigned char *)pp; 14511682Sbde 146181074Sdas le32enc(p, u & 0xffffffff); 147251599Sdas le32enc(p + 4, u >> 32); 148181074Sdas} 149251599Sdas 150181074Sdas#endif /* _COMPAT_ENDIAN_ENC_H_ */ 151251599Sdas