"Fossies" - the Fresh Open Source Software Archive

Member "n2n-3.1.1/include/portable_endian.h" (31 Mar 2022, 8575 Bytes) of package /linux/misc/n2n-3.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "portable_endian.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.0_vs_3.1.1.

A hint: This file contains one or more very long lines, so maybe it is better readable using the pure text view mode that shows the contents as wrapped lines within the browser window.


    1 /**
    2  * (C) 2007-22 - ntop.org and contributors
    3  *
    4  * This program is free software; you can redistribute it and/or modify
    5  * it under the terms of the GNU General Public License as published by
    6  * the Free Software Foundation; either version 3 of the License, or
    7  * (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not see see <http://www.gnu.org/licenses/>
   16  *
   17  */
   18 
   19 
   20 // taken from
   21 // https://raw.githubusercontent.com/pyca/bcrypt/master/src/_csrc/portable_endian.h
   22 // as of June 11, 2020
   23 
   24 // "License": Public Domain
   25 // I, Mathias Panzenböck, place this file hereby into the public domain. Use it at your own risk for whatever you like.
   26 // In case there are jurisdictions that don't support putting things in the public domain you can also consider it to
   27 // be "dual licensed" under the BSD, MIT and Apache licenses, if you want to. This code is trivial anyway. Consider it
   28 // an example on how to get the endian conversion functions on different platforms.
   29 
   30 #ifndef PORTABLE_ENDIAN_H__
   31 #define PORTABLE_ENDIAN_H__
   32 
   33 #if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
   34 
   35 #   define __WINDOWS__
   36 
   37 #endif
   38 
   39 #if defined(__linux__) || defined(__CYGWIN__)
   40 /* Define necessary macros for the header to expose all fields. */
   41 #   if !defined(_BSD_SOURCE)
   42 #       define _BSD_SOURCE
   43 #   endif
   44 #   if !defined(__USE_BSD)
   45 #       define __USE_BSD
   46 #   endif
   47 #   if !defined(_DEFAULT_SOURCE)
   48 #       define _DEFAULT_SOURCE
   49 #   endif
   50 #   include <endian.h>
   51 #   include <features.h>
   52 /* See http://linux.die.net/man/3/endian */
   53 #   if defined(htobe16) && defined(htole16) && defined(be16toh) && defined(le16toh) && defined(htobe32) && defined(htole32) && defined(be32toh) && defined(htole32) && defined(htobe64) && defined(htole64) && defined(htobe64) && defined(be64toh) && defined(htole64) && defined(le64toh)
   54 /* Do nothing. The macros we need already exist. */
   55 #   elif !defined(__GLIBC__) || !defined(__GLIBC_MINOR__) || ((__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 9)))
   56 #       include <arpa/inet.h>
   57 #       if defined(__BYTE_ORDER) && (__BYTE_ORDER == __LITTLE_ENDIAN)
   58 #           define htobe16(x) htons(x)
   59 #           define htole16(x) (x)
   60 #           define be16toh(x) ntohs(x)
   61 #           define le16toh(x) (x)
   62 
   63 #           define htobe32(x) htonl(x)
   64 #           define htole32(x) (x)
   65 #           define be32toh(x) ntohl(x)
   66 #           define le32toh(x) (x)
   67 
   68 #           define htobe64(x) (((uint64_t)htonl(((uint32_t)(((uint64_t)(x)) >> 32)))) | (((uint64_t)htonl(((uint32_t)(x)))) << 32))
   69 #           define htole64(x) (x)
   70 #           define be64toh(x) (((uint64_t)ntohl(((uint32_t)(((uint64_t)(x)) >> 32)))) | (((uint64_t)ntohl(((uint32_t)(x)))) << 32))
   71 #           define le64toh(x) (x)
   72 #       elif defined(__BYTE_ORDER) && (__BYTE_ORDER == __BIG_ENDIAN)
   73 #           define htobe16(x) (x)
   74 #           define htole16(x) (((((uint16_t)(x)) >> 8))|((((uint16_t)(x)) << 8)))
   75 #           define be16toh(x) (x)
   76 #           define le16toh(x) (((((uint16_t)(x)) >> 8))|((((uint16_t)(x)) << 8)))
   77 
   78 #           define htobe32(x) (x)
   79 #           define htole32(x) (((uint32_t)htole16(((uint16_t)(((uint32_t)(x)) >> 16)))) | (((uint32_t)htole16(((uint16_t)(x)))) << 16))
   80 #           define be32toh(x) (x)
   81 #           define le32toh(x) (((uint32_t)le16toh(((uint16_t)(((uint32_t)(x)) >> 16)))) | (((uint32_t)le16toh(((uint16_t)(x)))) << 16))
   82 
   83 #           define htobe64(x) (x)
   84 #           define htole64(x) (((uint64_t)htole32(((uint32_t)(((uint64_t)(x)) >> 32)))) | (((uint64_t)htole32(((uint32_t)(x)))) << 32))
   85 #           define be64toh(x) (x)
   86 #           define le64toh(x) (((uint64_t)le32toh(((uint32_t)(((uint64_t)(x)) >> 32)))) | (((uint64_t)le32toh(((uint32_t)(x)))) << 32))
   87 #       else
   88 #           error Byte Order not supported or not defined.
   89 #       endif
   90 #   endif
   91 
   92 #elif defined(__APPLE__)
   93 
   94 #   include <libkern/OSByteOrder.h>
   95 
   96 #   define htobe16(x) OSSwapHostToBigInt16(x)
   97 #   define htole16(x) OSSwapHostToLittleInt16(x)
   98 #   define be16toh(x) OSSwapBigToHostInt16(x)
   99 #   define le16toh(x) OSSwapLittleToHostInt16(x)
  100 
  101 #   define htobe32(x) OSSwapHostToBigInt32(x)
  102 #   define htole32(x) OSSwapHostToLittleInt32(x)
  103 #   define be32toh(x) OSSwapBigToHostInt32(x)
  104 #   define le32toh(x) OSSwapLittleToHostInt32(x)
  105 
  106 #   define htobe64(x) OSSwapHostToBigInt64(x)
  107 #   define htole64(x) OSSwapHostToLittleInt64(x)
  108 #   define be64toh(x) OSSwapBigToHostInt64(x)
  109 #   define le64toh(x) OSSwapLittleToHostInt64(x)
  110 
  111 #   define __BYTE_ORDER    BYTE_ORDER
  112 #   define __BIG_ENDIAN    BIG_ENDIAN
  113 #   define __LITTLE_ENDIAN LITTLE_ENDIAN
  114 #   define __PDP_ENDIAN    PDP_ENDIAN
  115 
  116 #elif defined(__OpenBSD__)
  117 
  118 #   include <sys/endian.h>
  119 
  120 #elif defined(__HAIKU__)
  121 
  122 #   include <endian.h>
  123 
  124 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
  125 
  126 #   include <sys/endian.h>
  127 
  128 #   if !defined(be16toh)
  129     #   define be16toh(x) betoh16(x)
  130     #   define le16toh(x) letoh16(x)
  131 #   endif
  132 
  133 #   if !defined(be32toh)
  134     #   define be32toh(x) betoh32(x)
  135     #   define le32toh(x) letoh32(x)
  136 #   endif
  137 
  138 #   if !defined(be64toh)
  139     #   define be64toh(x) betoh64(x)
  140     #   define le64toh(x) letoh64(x)
  141 #   endif
  142 
  143 #elif defined(__WINDOWS__)
  144 
  145 #   if BYTE_ORDER == LITTLE_ENDIAN
  146 
  147 #       define htobe16(x) _byteswap_ushort(x)
  148 #       define htole16(x) (x)
  149 #       define be16toh(x) _byteswap_ushort(x)
  150 #       define le16toh(x) (x)
  151 
  152 #       define htobe32(x) _byteswap_ulong(x)
  153 #       define htole32(x) (x)
  154 #       define be32toh(x) _byteswap_ulong(x)
  155 #       define le32toh(x) (x)
  156 
  157 #       define htobe64(x) (((uint64_t)htobe32(((uint32_t)(((uint64_t)(x)) >> 32))) & 0x00000000FFFFFFFFULL) | (((uint64_t)htobe32(((uint32_t)(x)))) << 32))
  158 #       define be64toh(x) (((uint64_t)be32toh(((uint32_t)(((uint64_t)(x)) >> 32))) & 0x00000000FFFFFFFFULL) | (((uint64_t)be32toh(((uint32_t)(x)))) << 32))
  159 #       define htole64(x) (x)
  160 #       define le64toh(x) (x)
  161 
  162 #   elif BYTE_ORDER == BIG_ENDIAN
  163 
  164         /* that would be xbox 360 */
  165 #       define htobe16(x) (x)
  166 #       define htole16(x) __builtin_bswap16(x)
  167 #       define be16toh(x) (x)
  168 #       define le16toh(x) __builtin_bswap16(x)
  169 
  170 #       define htobe32(x) (x)
  171 #       define htole32(x) __builtin_bswap32(x)
  172 #       define be32toh(x) (x)
  173 #       define le32toh(x) __builtin_bswap32(x)
  174 
  175 #       define htobe64(x) (x)
  176 #       define htole64(x) __builtin_bswap64(x)
  177 #       define be64toh(x) (x)
  178 #       define le64toh(x) __builtin_bswap64(x)
  179 
  180 #   else
  181 
  182 #       error byte order not supported
  183 
  184 #   endif
  185 
  186 #   define __BYTE_ORDER    BYTE_ORDER
  187 #   define __BIG_ENDIAN    BIG_ENDIAN
  188 #   define __LITTLE_ENDIAN LITTLE_ENDIAN
  189 #   define __PDP_ENDIAN    PDP_ENDIAN
  190 
  191 #elif defined(__sun)
  192 
  193 #   include <sys/byteorder.h>
  194 
  195 #   define htobe16(x) BE_16(x)
  196 #   define htole16(x) LE_16(x)
  197 #   define be16toh(x) BE_16(x)
  198 #   define le16toh(x) LE_16(x)
  199 
  200 #   define htobe32(x) BE_32(x)
  201 #   define htole32(x) LE_32(x)
  202 #   define be32toh(x) BE_32(x)
  203 #   define le32toh(x) LE_32(x)
  204 
  205 #   define htobe64(x) BE_64(x)
  206 #   define htole64(x) LE_64(x)
  207 #   define be64toh(x) BE_64(x)
  208 #   define le64toh(x) LE_64(x)
  209 
  210 #elif defined _AIX      /* AIX is always big endian */
  211 #       define be64toh(x) (x)
  212 #       define be32toh(x) (x)
  213 #       define be16toh(x) (x)
  214 #       define le32toh(x)                              \
  215          ((((x) & 0xff) << 24) |                 \
  216            (((x) & 0xff00) << 8) |                \
  217            (((x) & 0xff0000) >> 8) |              \
  218            (((x) & 0xff000000) >> 24))
  219 #       define   le64toh(x)                               \
  220          ((((x) & 0x00000000000000ffL) << 56) |   \
  221           (((x) & 0x000000000000ff00L) << 40) |   \
  222           (((x) & 0x0000000000ff0000L) << 24) |   \
  223           (((x) & 0x00000000ff000000L) << 8)  |   \
  224           (((x) & 0x000000ff00000000L) >> 8)  |   \
  225           (((x) & 0x0000ff0000000000L) >> 24) |   \
  226           (((x) & 0x00ff000000000000L) >> 40) |   \
  227           (((x) & 0xff00000000000000L) >> 56))
  228 #       ifndef htobe64
  229 #               define htobe64(x) be64toh(x)
  230 #       endif
  231 #       ifndef htobe32
  232 #               define htobe32(x) be32toh(x)
  233 #       endif
  234 #       ifndef htobe16
  235 #               define htobe16(x) be16toh(x)
  236 #       endif
  237 
  238 
  239 #else
  240 
  241 #   error platform not supported
  242 
  243 #endif
  244 
  245 #endif