/ compat / endian_util.h
endian_util.h
  1  /*-
  2   * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
  3   * All rights reserved.
  4   *
  5   * Redistribution and use in source and binary forms, with or without
  6   * modification, are permitted provided that the following conditions
  7   * are met:
  8   * 1. Redistributions of source code must retain the above copyright
  9   *    notice, this list of conditions and the following disclaimer.
 10   * 2. Redistributions in binary form must reproduce the above copyright
 11   *    notice, this list of conditions and the following disclaimer in the
 12   *    documentation and/or other materials provided with the distribution.
 13   *
 14   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 15   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 16   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 17   * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 18   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 19   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 20   * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 21   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 22   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 23   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 24   * SUCH DAMAGE.
 25   *
 26   * $FreeBSD: releng/10.1/sys/sys/endian.h 208331 2010-05-20 06:16:13Z phk $
 27   */
 28  
 29  #ifndef _ENDIAN_UTIL_H_
 30  #define _ENDIAN_UTIL_H_
 31  
 32  #include "pkg_config.h"
 33  
 34  #include <sys/types.h>
 35  #include <inttypes.h>
 36  
 37  /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
 38  
 39  #if defined(HAVE_DECL_BE16DEC) && !HAVE_DECL_BE16DEC
 40  static __inline uint16_t
 41  be16dec(const void *pp)
 42  {
 43  	uint8_t const *p = (uint8_t const *)pp;
 44  
 45  	return ((p[0] << 8) | p[1]);
 46  }
 47  #endif
 48  
 49  #if defined(HAVE_DECL_BE32DEC) && !HAVE_DECL_BE32DEC
 50  static __inline uint32_t
 51  be32dec(const void *pp)
 52  {
 53  	uint8_t const *p = (uint8_t const *)pp;
 54  
 55  	return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
 56  }
 57  #endif
 58  
 59  #if defined(HAVE_DECL_BE64DEC) && !HAVE_DECL_BE64DEC
 60  static __inline uint64_t
 61  be64dec(const void *pp)
 62  {
 63  	uint8_t const *p = (uint8_t const *)pp;
 64  
 65  	return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
 66  }
 67  #endif
 68  
 69  #if defined(HAVE_DECL_LE16DEC) && !HAVE_DECL_LE16DEC
 70  static __inline uint16_t
 71  le16dec(const void *pp)
 72  {
 73  	uint8_t const *p = (uint8_t const *)pp;
 74  
 75  	return ((p[1] << 8) | p[0]);
 76  }
 77  #endif
 78  
 79  #if defined(HAVE_DECL_LE32DEC) && !HAVE_DECL_LE32DEC
 80  static __inline uint32_t
 81  le32dec(const void *pp)
 82  {
 83  	uint8_t const *p = (uint8_t const *)pp;
 84  
 85  	return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
 86  }
 87  #endif
 88  
 89  #if defined(HAVE_DECL_LE64DEC) && !HAVE_DECL_LE64DEC
 90  static __inline uint64_t
 91  le64dec(const void *pp)
 92  {
 93  	uint8_t const *p = (uint8_t const *)pp;
 94  
 95  	return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
 96  }
 97  #endif
 98  
 99  #if defined(HAVE_DECL_BE16ENC) && !HAVE_DECL_BE16ENC
100  static __inline void
101  be16enc(void *pp, uint16_t u)
102  {
103  	uint8_t *p = (uint8_t *)pp;
104  
105  	p[0] = (u >> 8) & 0xff;
106  	p[1] = u & 0xff;
107  }
108  #endif
109  
110  #if defined(HAVE_DECL_BE32ENC) && !HAVE_DECL_BE32ENC
111  static __inline void
112  be32enc(void *pp, uint32_t u)
113  {
114  	uint8_t *p = (uint8_t *)pp;
115  
116  	p[0] = (u >> 24) & 0xff;
117  	p[1] = (u >> 16) & 0xff;
118  	p[2] = (u >> 8) & 0xff;
119  	p[3] = u & 0xff;
120  }
121  #endif
122  
123  #if defined(HAVE_DECL_BE64ENC) && !HAVE_DECL_BE64ENC
124  static __inline void
125  be64enc(void *pp, uint64_t u)
126  {
127  	uint8_t *p = (uint8_t *)pp;
128  
129  	be32enc(p, (uint32_t)(u >> 32));
130  	be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
131  }
132  #endif
133  
134  #if defined(HAVE_DECL_LE16ENC) && !HAVE_DECL_LE16ENC
135  static __inline void
136  le16enc(void *pp, uint16_t u)
137  {
138  	uint8_t *p = (uint8_t *)pp;
139  
140  	p[0] = u & 0xff;
141  	p[1] = (u >> 8) & 0xff;
142  }
143  #endif
144  
145  #if defined(HAVE_DECL_LE32ENC) && !HAVE_DECL_LE32ENC
146  static __inline void
147  le32enc(void *pp, uint32_t u)
148  {
149  	uint8_t *p = (uint8_t *)pp;
150  
151  	p[0] = u & 0xff;
152  	p[1] = (u >> 8) & 0xff;
153  	p[2] = (u >> 16) & 0xff;
154  	p[3] = (u >> 24) & 0xff;
155  }
156  #endif
157  
158  #if defined(HAVE_DECL_LE64ENC) && !HAVE_DECL_LE64ENC
159  static __inline void
160  le64enc(void *pp, uint64_t u)
161  {
162  	uint8_t *p = (uint8_t *)pp;
163  
164  	le32enc(p, (uint32_t)(u & 0xffffffffU));
165  	le32enc(p + 4, (uint32_t)(u >> 32));
166  }
167  #endif
168  
169  #endif	/* _ENDIAN_UTIL_H_ */