MAV'RIC
|
00001 /******************************************************************************* 00002 * Copyright (c) 2009-2016, MAV'RIC Development Team 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * 00011 * 2. Redistributions in binary form must reproduce the above copyright notice, 00012 * this list of conditions and the following disclaimer in the documentation 00013 * and/or other materials provided with the distribution. 00014 * 00015 * 3. Neither the name of the copyright holder nor the names of its contributors 00016 * may be used to endorse or promote products derived from this software without 00017 * specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00023 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00024 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00025 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00026 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00027 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00029 * POSSIBILITY OF SUCH DAMAGE. 00030 ******************************************************************************/ 00031 00032 /******************************************************************************* 00033 * \file endian.h 00034 * 00035 * \author MAV'RIC Team 00036 * \author Julien Lecoeur 00037 * 00038 * \brief Implementation of endianness-dependant functions 00039 * 00040 ******************************************************************************/ 00041 00042 00043 #ifndef MAVRIC_ENDIAN_H_ 00044 #define MAVRIC_ENDIAN_H_ 00045 00046 #ifdef __cplusplus 00047 extern "C" { 00048 #endif 00049 00050 #ifndef __MAVRIC_ENDIAN_BIG__ 00051 #ifndef __MAVRIC_ENDIAN_LITTLE__ 00052 #warning Unknown Endian, using default __MAVRIC_ENDIAN_LITTLE__ 00053 #define __MAVRIC_ENDIAN_LITTLE__ 00054 #endif 00055 #endif 00056 00057 #include <stdint.h> 00058 00065 static inline uint16_t endian_to_little16(uint16_t data); 00066 00067 00074 static inline uint16_t endian_to_big16(uint16_t data); 00075 00076 00083 static inline uint32_t endian_to_little32(uint32_t data); 00084 00085 00092 static inline uint32_t endian_to_big32(uint32_t data); 00093 00094 00101 static inline uint16_t endian_htons(uint16_t data); 00102 00103 00110 static inline uint16_t endian_ntohs(uint16_t data); 00111 00112 00119 static inline uint32_t endian_htonl(uint32_t data); 00120 00121 00128 static inline uint32_t endian_ntohl(uint32_t data); 00129 00130 00137 static inline uint16_t endian_rev16(uint16_t data) 00138 { 00139 return ((data & 0x00FF) << 8) | 00140 ((data & 0xFF00) >> 8); 00141 }; 00142 00143 00150 static inline int16_t endian_rev16s(int16_t data) 00151 { 00152 return ((data >> 8) & 0x00ff) | ((data & 0x00ff) << 8); 00153 }; 00154 00155 00162 static inline uint32_t endian_rev32(uint32_t data) 00163 { 00164 return ((data & 0x000000FF) << 24) | 00165 ((data & 0x0000FF00) << 8) | 00166 ((data & 0x00FF0000) >> 8) | 00167 ((data & 0xFF000000) >> 24); 00168 }; 00169 00170 00171 00172 #ifdef __MAVRIC_ENDIAN_BIG__ 00173 00174 static inline uint16_t endian_to_little16(uint16_t data) 00175 { 00176 return endian_rev16(data); 00177 } 00178 00179 static inline uint16_t endian_to_big16(uint16_t data) 00180 { 00181 return data; 00182 } 00183 00184 static inline uint32_t endian_to_little32(uint32_t data) 00185 { 00186 return endian_rev32(data); 00187 } 00188 00189 static inline uint32_t endian_to_big32(uint32_t data) 00190 { 00191 return data; 00192 } 00193 00194 static inline uint16_t endian_htons(uint16_t data) 00195 { 00196 return data; 00197 } 00198 00199 static inline uint16_t endian_ntohs(uint16_t data) 00200 { 00201 return data; 00202 } 00203 00204 static inline uint32_t endian_htonl(uint32_t data) 00205 { 00206 return data; 00207 } 00208 00209 static inline uint32_t endian_ntohl(uint32_t data) 00210 { 00211 return data; 00212 } 00213 00214 #endif 00215 00216 00217 #ifdef __MAVRIC_ENDIAN_LITTLE__ 00218 00219 static inline uint16_t endian_to_little16(uint16_t data) 00220 { 00221 return data; 00222 } 00223 00224 static inline uint16_t endian_to_big16(uint16_t data) 00225 { 00226 return endian_rev16(data); 00227 } 00228 00229 static inline uint32_t endian_to_little32(uint32_t data) 00230 { 00231 return data; 00232 } 00233 00234 static inline uint32_t endian_to_big32(uint32_t data) 00235 { 00236 return endian_rev32(data); 00237 } 00238 00239 static inline uint16_t endian_htons(uint16_t data) 00240 { 00241 return endian_rev16(data); 00242 } 00243 00244 static inline uint16_t endian_ntohs(uint16_t data) 00245 { 00246 return endian_rev16(data); 00247 } 00248 00249 static inline uint32_t endian_htonl(uint32_t data) 00250 { 00251 return endian_rev32(data); 00252 } 00253 00254 static inline uint32_t endian_ntohl(uint32_t data) 00255 { 00256 return endian_rev32(data); 00257 } 00258 00259 #endif 00260 00261 #ifdef __cplusplus 00262 } 00263 #endif 00264 00265 #endif /* MAVRIC_ENDIAN_H_ */