Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:13

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  *  @file
0005  *
0006  *  @brief Includes endian information about the target
0007  */
0008 
0009 /*
0010  * COPYRIGHT (C) 1989-1999 On-Line Applications Research Corporation (OAR).
0011  *
0012  * Redistribution and use in source and binary forms, with or without
0013  * modification, are permitted provided that the following conditions
0014  * are met:
0015  * 1. Redistributions of source code must retain the above copyright
0016  *    notice, this list of conditions and the following disclaimer.
0017  * 2. Redistributions in binary form must reproduce the above copyright
0018  *    notice, this list of conditions and the following disclaimer in the
0019  *    documentation and/or other materials provided with the distribution.
0020  *
0021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0022  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0024  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0025  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0026  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0027  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0030  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0031  * POSSIBILITY OF SUCH DAMAGE.
0032  */
0033 
0034 #ifndef _RTEMS_ENDIAN_H
0035 #define _RTEMS_ENDIAN_H
0036 
0037 #include <stdint.h>
0038 
0039 #include <machine/endian.h>
0040 
0041 #ifndef _BYTEORDER_FUNC_DEFINED
0042 #define _BYTEORDER_FUNC_DEFINED
0043 #define htonl(x)    __htonl(x)
0044 #define htons(x)    __htons(x)
0045 #define ntohl(x)    __ntohl(x)
0046 #define ntohs(x)    __ntohs(x)
0047 #endif
0048 
0049 #define NTOHS(x) (x) = ntohs(x)
0050 #define HTONS(x) (x) = htons(x)
0051 #define NTOHL(x) (x) = ntohl(x)
0052 #define HTONL(x) (x) = htonl(x)
0053 
0054 static inline uint16_t rtems_uint16_from_little_endian( const uint8_t *data)
0055 {
0056   uint16_t value = 0;
0057   int i;
0058 
0059   for (i = 1; i >= 0; --i) {
0060     value = (uint16_t) ((value << 8) + data [i]);
0061   }
0062 
0063   return value;
0064 }
0065 
0066 static inline uint32_t rtems_uint32_from_little_endian( const uint8_t *data)
0067 {
0068   uint32_t value = 0;
0069   int i;
0070 
0071   for (i = 3; i >= 0; --i) {
0072     value = (value << 8) + data [i];
0073   }
0074 
0075   return value;
0076 }
0077 
0078 static inline uint64_t rtems_uint64_from_little_endian( const uint8_t *data)
0079 {
0080   uint64_t value = 0;
0081   int i;
0082 
0083   for (i = 7; i >= 0; --i) {
0084     value = (value << 8) + (uint64_t) data [i];
0085   }
0086 
0087   return value;
0088 }
0089 
0090 static inline uint16_t rtems_uint16_from_big_endian( const uint8_t *data)
0091 {
0092   uint16_t value = 0;
0093   int i;
0094 
0095   for (i = 0; i < 2; ++i) {
0096     value = (uint16_t) ((value << 8) + data [i]);
0097   }
0098 
0099   return value;
0100 }
0101 
0102 static inline uint32_t rtems_uint32_from_big_endian( const uint8_t *data)
0103 {
0104   uint32_t value = 0;
0105   int i;
0106 
0107   for (i = 0; i < 4; ++i) {
0108     value = (value << 8) + (uint32_t) data [i];
0109   }
0110 
0111   return value;
0112 }
0113 
0114 static inline uint64_t rtems_uint64_from_big_endian( const uint8_t *data)
0115 {
0116   uint64_t value = 0;
0117   int i;
0118 
0119   for (i = 0; i < 8; ++i) {
0120     value = (value << 8) + (uint64_t) data [i];
0121   }
0122 
0123   return value;
0124 }
0125 
0126 static inline void rtems_uint16_to_little_endian( uint16_t value, uint8_t *data)
0127 {
0128   int i;
0129 
0130   for (i = 0; i < 2; ++i) {
0131     data [i] = (uint8_t) value;
0132     value >>= 8;
0133   }
0134 }
0135 
0136 static inline void rtems_uint32_to_little_endian( uint32_t value, uint8_t *data)
0137 {
0138   int i;
0139 
0140   for (i = 0; i < 4; ++i) {
0141     data [i] = (uint8_t) value;
0142     value >>= 8;
0143   }
0144 }
0145 
0146 static inline void rtems_uint64_to_little_endian( uint64_t value, uint8_t *data)
0147 {
0148   int i;
0149 
0150   for (i = 0; i < 8; ++i) {
0151     data [i] = (uint8_t) value;
0152     value >>= 8;
0153   }
0154 }
0155 
0156 static inline void rtems_uint16_to_big_endian( uint16_t value, uint8_t *data)
0157 {
0158   int i;
0159 
0160   for (i = 1; i >= 0; --i) {
0161     data [i] = (uint8_t) value;
0162     value >>= 8;
0163   }
0164 }
0165 
0166 static inline void rtems_uint32_to_big_endian( uint32_t value, uint8_t *data)
0167 {
0168   int i;
0169 
0170   for (i = 3; i >= 0; --i) {
0171     data [i] = (uint8_t) value;
0172     value >>= 8;
0173   }
0174 }
0175 
0176 static inline void rtems_uint64_to_big_endian( uint64_t value, uint8_t *data)
0177 {
0178   int i;
0179 
0180   for (i = 7; i >= 0; --i) {
0181     data [i] = (uint8_t) value;
0182     value >>= 8;
0183   }
0184 }
0185 
0186 #endif /* _RTEMS_ENDIAN_H */