libmicrohttpd2

HTTP server C library (MHD 2.x, alpha)
Log | Files | Refs | README | LICENSE

mhd_byteorder.h (7263B)


      1 /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */
      2 /*
      3   This file is part of GNU libmicrohttpd.
      4   Copyright (C) 2015-2022 Karlson2k (Evgeny Grin)
      5 
      6   GNU libmicrohttpd is free software; you can redistribute it and/or
      7   modify it under the terms of the GNU Lesser General Public
      8   License as published by the Free Software Foundation; either
      9   version 2.1 of the License, or (at your option) any later version.
     10 
     11   GNU libmicrohttpd is distributed in the hope that it will be useful,
     12   but WITHOUT ANY WARRANTY; without even the implied warranty of
     13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14   Lesser General Public License for more details.
     15 
     16   Alternatively, you can redistribute GNU libmicrohttpd and/or
     17   modify it under the terms of the GNU General Public License as
     18   published by the Free Software Foundation; either version 2 of
     19   the License, or (at your option) any later version, together
     20   with the eCos exception, as follows:
     21 
     22     As a special exception, if other files instantiate templates or
     23     use macros or inline functions from this file, or you compile this
     24     file and link it with other works to produce a work based on this
     25     file, this file does not by itself cause the resulting work to be
     26     covered by the GNU General Public License. However the source code
     27     for this file must still be made available in accordance with
     28     section (3) of the GNU General Public License v2.
     29 
     30     This exception does not invalidate any other reasons why a work
     31     based on this file might be covered by the GNU General Public
     32     License.
     33 
     34   You should have received copies of the GNU Lesser General Public
     35   License and the GNU General Public License along with this library;
     36   if not, see <https://www.gnu.org/licenses/>.
     37 */
     38 
     39 /**
     40  * @file src/mhd2/mhd_byteorder.h
     41  * @brief  macro definitions for host byte order
     42  * @author Karlson2k (Evgeny Grin)
     43  */
     44 
     45 #ifndef MHD_BYTEORDER_H
     46 #define MHD_BYTEORDER_H
     47 
     48 #include "mhd_sys_options.h"
     49 
     50 #include "sys_base_types.h"
     51 
     52 #ifdef HAVE_ENDIAN_H
     53 #  include <endian.h>
     54 #endif
     55 
     56 #ifdef HAVE_SYS_PARAM_H
     57 #  include <sys/param.h>
     58 #endif
     59 
     60 #ifdef HAVE_MACHINE_ENDIAN_H
     61 #  include <machine/endian.h>
     62 #endif
     63 
     64 #ifdef HAVE_SYS_ENDIAN_H
     65 #  include <sys/endian.h>
     66 #endif
     67 
     68 #ifdef HAVE_SYS_TYPES_H
     69 #  include <sys/types.h>
     70 #endif
     71 
     72 #ifdef HAVE_SYS_BYTEORDER_H
     73 #  include <sys/byteorder.h>
     74 #endif
     75 
     76 #ifdef HAVE_SYS_MACHINE_H
     77 #  include <sys/machine.h>
     78 #endif
     79 
     80 #ifdef HAVE_MACHINE_PARAM_H
     81 #  include <machine/param.h>
     82 #endif
     83 
     84 #ifdef HAVE_SYS_ISA_DEFS_H
     85 #  include <sys/isa_defs.h>
     86 #endif
     87 
     88 #define mhd_BIG_ENDIAN 1234
     89 #define mhd_LITTLE_ENDIAN 4321
     90 #define mhd_PDP_ENDIAN 2143
     91 
     92 #if defined(__BYTE_ORDER__)
     93 #  if defined(__ORDER_BIG_ENDIAN__) && \
     94   __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
     95 #    define mhd_BYTE_ORDER mhd_BIG_ENDIAN
     96 #  elif defined(__ORDER_LITTLE_ENDIAN__) && \
     97   __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
     98 #    define mhd_BYTE_ORDER mhd_LITTLE_ENDIAN
     99 #  elif defined(__ORDER_PDP_ENDIAN__) && \
    100   __BYTE_ORDER__ == __ORDER_PDP_ENDIAN__
    101 #    define mhd_BYTE_ORDER mhd_PDP_ENDIAN
    102 #  endif /* __BYTE_ORDER__ == __ORDER_PDP_ENDIAN__ */
    103 #elif defined(__BYTE_ORDER)
    104 #  if defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
    105 #    define mhd_BYTE_ORDER mhd_BIG_ENDIAN
    106 #  elif defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN
    107 #    define mhd_BYTE_ORDER mhd_LITTLE_ENDIAN
    108 #  elif defined(__PDP_ENDIAN) && __BYTE_ORDER == __PDP_ENDIAN
    109 #    define mhd_BYTE_ORDER mhd_PDP_ENDIAN
    110 #  endif /* __BYTE_ORDER == __PDP_ENDIAN */
    111 #elif defined(BYTE_ORDER)
    112 #  if defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
    113 #    define mhd_BYTE_ORDER mhd_BIG_ENDIAN
    114 #  elif defined(LITTLE_ENDIAN) && BYTE_ORDER == LITTLE_ENDIAN
    115 #    define mhd_BYTE_ORDER mhd_LITTLE_ENDIAN
    116 #  elif defined(PDP_ENDIAN) && BYTE_ORDER == PDP_ENDIAN
    117 #    define mhd_BYTE_ORDER mhd_PDP_ENDIAN
    118 #  endif /* __BYTE_ORDER == _PDP_ENDIAN */
    119 #elif defined(_BYTE_ORDER)
    120 #  if defined(_BIG_ENDIAN) && _BYTE_ORDER == _BIG_ENDIAN
    121 #    define mhd_BYTE_ORDER mhd_BIG_ENDIAN
    122 #  elif defined(_LITTLE_ENDIAN) && _BYTE_ORDER == _LITTLE_ENDIAN
    123 #    define mhd_BYTE_ORDER mhd_LITTLE_ENDIAN
    124 #  elif defined(_PDP_ENDIAN) && _BYTE_ORDER == _PDP_ENDIAN
    125 #    define mhd_BYTE_ORDER mhd_PDP_ENDIAN
    126 #  endif /* _BYTE_ORDER == _PDP_ENDIAN */
    127 #endif /* _BYTE_ORDER */
    128 
    129 #ifndef mhd_BYTE_ORDER
    130 /* Byte order specification didn't detected in system headers */
    131 /* Try some guessing */
    132 
    133 #  if   (defined(__BIG_ENDIAN__) && ! defined(__LITTLE_ENDIAN__)) || \
    134   (defined(_BIG_ENDIAN) && ! defined(_LITTLE_ENDIAN))
    135 /* Seems that this is a big endian platform */
    136 #    define mhd_BYTE_ORDER mhd_BIG_ENDIAN
    137 #  elif (defined(__LITTLE_ENDIAN__) && ! defined(__BIG_ENDIAN__)) || \
    138   (defined(_LITTLE_ENDIAN) && ! defined(_BIG_ENDIAN))
    139 /* Seems that this is a little endian platform */
    140 #    define mhd_BYTE_ORDER mhd_LITTLE_ENDIAN
    141 #  elif defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || \
    142   defined(__x86_64) || \
    143   defined(_M_X64) || defined(_M_AMD64) || defined(i386) || defined(__i386) || \
    144   defined(__i386__) || defined(__i486__) || defined(__i586__) || \
    145   defined(__i686__) || \
    146   defined(_M_IX86) || defined(_X86_) || defined(__THW_INTEL__)
    147 /* x86 family is little endian */
    148 #    define mhd_BYTE_ORDER mhd_LITTLE_ENDIAN
    149 #  elif defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \
    150   defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__)
    151 /* Looks like this is ARM/MIPS in big endian mode */
    152 #    define mhd_BYTE_ORDER mhd_BIG_ENDIAN
    153 #  elif defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || \
    154   defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__)
    155 /* Looks like this is ARM/MIPS in little endian mode */
    156 #    define mhd_BYTE_ORDER mhd_LITTLE_ENDIAN
    157 #  elif defined(__m68k__) || defined(M68000) || defined(__hppa__) || \
    158   defined(__hppa) || \
    159   defined(__HPPA__) || defined(__370__) || defined(__THW_370__) || \
    160   defined(__s390__) || defined(__s390x__) || defined(__SYSC_ZARCH__)
    161 /* Looks like this is a big endian platform */
    162 #    define mhd_BYTE_ORDER mhd_BIG_ENDIAN
    163 #  elif defined(__ia64__) || defined(_IA64) || defined(__IA64__) || \
    164   defined(__ia64) || \
    165   defined(_M_IA64) || defined(__itanium__) || defined(__bfin__) || \
    166   defined(__BFIN__) || defined(bfin) || defined(BFIN)
    167 /* Looks like this is a little endian platform */
    168 #  define mhd_BYTE_ORDER mhd_LITTLE_ENDIAN
    169 #  elif defined(_WIN32)
    170 /* W32 is always little endian on all platforms, except XBOX 360 */
    171 #    if ! defined(_M_PPC) && ! defined(XBOX360)
    172 #      define mhd_BYTE_ORDER mhd_LITTLE_ENDIAN
    173 #    endif
    174 #  elif defined(WORDS_BIGENDIAN)
    175 /* Use byte order detected by configure */
    176 #    define mhd_BYTE_ORDER mhd_BIG_ENDIAN
    177 #  endif /* _WIN32 */
    178 #endif /* !mhd_BYTE_ORDER */
    179 
    180 #ifdef mhd_BYTE_ORDER
    181 /* Some sanity checks */
    182 #  if defined(WORDS_BIGENDIAN) && mhd_BYTE_ORDER != mhd_BIG_ENDIAN
    183 #error \
    184   Configure detected big endian byte order but headers specify different byte order
    185 #  elif ! defined(WORDS_BIGENDIAN) && mhd_BYTE_ORDER == mhd_BIG_ENDIAN
    186 #error \
    187   Configure did not detect big endian byte order but headers specify big endian byte order
    188 #  endif /* !WORDS_BIGENDIAN && mhd_BYTE_ORDER == mhd_BIG_ENDIAN */
    189 #endif /* mhd_BYTE_ORDER */
    190 
    191 #endif /* !MHD_BYTEORDER_H */