Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:01

0001 /*
0002  * Copyright © 2011 Guillem Jover <guillem@hadrons.org>
0003  *
0004  * Copyright © 2002 Thomas Moestl <tmm@FreeBSD.org>
0005  * All rights reserved.
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
0017  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
0020  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0021  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0022  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0023  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0024  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0025  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0026  * SUCH DAMAGE.
0027  */
0028 
0029 #ifdef LIBBSD_OVERLAY
0030 #include <sys/cdefs.h>
0031 #if __has_include_next(<endian.h>)
0032 #include_next <endian.h>
0033 #endif
0034 #else
0035 #include <bsd/sys/cdefs.h>
0036 #if __has_include(<endian.h>)
0037 #include <endian.h>
0038 #endif
0039 #endif
0040 
0041 #ifndef LIBBSD_SYS_ENDIAN_H
0042 #define LIBBSD_SYS_ENDIAN_H
0043 
0044 #ifndef _BYTE_ORDER
0045 #define _BYTE_ORDER __BYTE_ORDER
0046 #endif
0047 
0048 #ifndef _LITTLE_ENDIAN
0049 #define _LITTLE_ENDIAN __LITTLE_ENDIAN
0050 #endif
0051 
0052 #ifndef _BIG_ENDIAN
0053 #define _BIG_ENDIAN __BIG_ENDIAN
0054 #endif
0055 
0056 #ifndef _PDP_ENDIAN
0057 #define _PDP_ENDIAN __PDP_ENDIAN
0058 #endif
0059 
0060 #include <stdint.h>
0061 
0062 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
0063 
0064 static __inline uint16_t
0065 be16dec(const void *pp)
0066 {
0067     uint8_t const *p = (uint8_t const *)pp;
0068 
0069     return ((p[0] << 8) | p[1]);
0070 }
0071 
0072 static __inline uint32_t
0073 be32dec(const void *pp)
0074 {
0075     uint8_t const *p = (uint8_t const *)pp;
0076 
0077     return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
0078 }
0079 
0080 static __inline uint64_t
0081 be64dec(const void *pp)
0082 {
0083     uint8_t const *p = (uint8_t const *)pp;
0084 
0085     return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
0086 }
0087 
0088 static __inline uint16_t
0089 le16dec(const void *pp)
0090 {
0091     uint8_t const *p = (uint8_t const *)pp;
0092 
0093     return ((p[1] << 8) | p[0]);
0094 }
0095 
0096 static __inline uint32_t
0097 le32dec(const void *pp)
0098 {
0099     uint8_t const *p = (uint8_t const *)pp;
0100 
0101     return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
0102 }
0103 
0104 static __inline uint64_t
0105 le64dec(const void *pp)
0106 {
0107     uint8_t const *p = (uint8_t const *)pp;
0108 
0109     return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
0110 }
0111 
0112 static __inline void
0113 be16enc(void *pp, uint16_t u)
0114 {
0115     uint8_t *p = (uint8_t *)pp;
0116 
0117     p[0] = (u >> 8) & 0xff;
0118     p[1] = u & 0xff;
0119 }
0120 
0121 static __inline void
0122 be32enc(void *pp, uint32_t u)
0123 {
0124     uint8_t *p = (uint8_t *)pp;
0125 
0126     p[0] = (u >> 24) & 0xff;
0127     p[1] = (u >> 16) & 0xff;
0128     p[2] = (u >> 8) & 0xff;
0129     p[3] = u & 0xff;
0130 }
0131 
0132 static __inline void
0133 be64enc(void *pp, uint64_t u)
0134 {
0135     uint8_t *p = (uint8_t *)pp;
0136 
0137     be32enc(p, (uint32_t)(u >> 32));
0138     be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
0139 }
0140 
0141 static __inline void
0142 le16enc(void *pp, uint16_t u)
0143 {
0144     uint8_t *p = (uint8_t *)pp;
0145 
0146     p[0] = u & 0xff;
0147     p[1] = (u >> 8) & 0xff;
0148 }
0149 
0150 static __inline void
0151 le32enc(void *pp, uint32_t u)
0152 {
0153     uint8_t *p = (uint8_t *)pp;
0154 
0155     p[0] = u & 0xff;
0156     p[1] = (u >> 8) & 0xff;
0157     p[2] = (u >> 16) & 0xff;
0158     p[3] = (u >> 24) & 0xff;
0159 }
0160 
0161 static __inline void
0162 le64enc(void *pp, uint64_t u)
0163 {
0164     uint8_t *p = (uint8_t *)pp;
0165 
0166     le32enc(p, (uint32_t)(u & 0xffffffffU));
0167     le32enc(p + 4, (uint32_t)(u >> 32));
0168 }
0169 
0170 #endif