1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 #ifndef THRIFT_PY_ENDIAN_H 21 #define THRIFT_PY_ENDIAN_H 22 23 #include <Python.h> 24 25 #ifndef _WIN32 26 #include <netinet/in.h> 27 #else 28 #include <winsock2.h> 29 #pragma comment(lib, "ws2_32.lib") 30 #define BIG_ENDIAN (4321) 31 #define LITTLE_ENDIAN (1234) 32 #define BYTE_ORDER LITTLE_ENDIAN 33 #define inline __inline 34 #endif 35 36 /* Fix endianness issues on Solaris */ 37 #if defined(__SVR4) && defined(__sun) 38 #if defined(__i386) && !defined(__i386__) 39 #define __i386__ 40 #endif 41 42 #ifndef BIG_ENDIAN 43 #define BIG_ENDIAN (4321) 44 #endif 45 #ifndef LITTLE_ENDIAN 46 #define LITTLE_ENDIAN (1234) 47 #endif 48 49 /* I386 is LE, even on Solaris */ 50 #if !defined(BYTE_ORDER) && defined(__i386__) 51 #define BYTE_ORDER LITTLE_ENDIAN 52 #endif 53 #endif 54 55 #ifndef __BYTE_ORDER 56 #if defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) 57 #define __BYTE_ORDER BYTE_ORDER 58 #define __LITTLE_ENDIAN LITTLE_ENDIAN 59 #define __BIG_ENDIAN BIG_ENDIAN 60 #else 61 #error "Cannot determine endianness" 62 #endif 63 #endif 64 65 // Same comment as the enum. Sorry. 66 #if __BYTE_ORDER == __BIG_ENDIAN 67 #define ntohll(n) (n) 68 #define htonll(n) (n) 69 #if defined(__GNUC__) && defined(__GLIBC__) 70 #include <byteswap.h> 71 #define letohll(n) bswap_64(n) 72 #define htolell(n) bswap_64(n) 73 #else /* GNUC & GLIBC */ 74 #define letohll(n) ((((unsigned long long)ntohl(n)) << 32) + ntohl(n >> 32)) 75 #define htolell(n) ((((unsigned long long)htonl(n)) << 32) + htonl(n >> 32)) 76 #endif 77 #elif __BYTE_ORDER == __LITTLE_ENDIAN 78 #if defined(__GNUC__) && defined(__GLIBC__) 79 #include <byteswap.h> 80 #define ntohll(n) bswap_64(n) 81 #define htonll(n) bswap_64(n) 82 #elif defined(_MSC_VER) 83 #include <stdlib.h> 84 #define ntohll(n) _byteswap_uint64(n) 85 #define htonll(n) _byteswap_uint64(n) 86 #else /* GNUC & GLIBC */ 87 #define ntohll(n) ((((unsigned long long)ntohl(n)) << 32) + ntohl(n >> 32)) 88 #define htonll(n) ((((unsigned long long)htonl(n)) << 32) + htonl(n >> 32)) 89 #endif /* GNUC & GLIBC */ 90 #define letohll(n) (n) 91 #define htolell(n) (n) 92 #else /* __BYTE_ORDER */ 93 #error "Can't define htonll or ntohll!" 94 #endif 95 96 #endif // THRIFT_PY_ENDIAN_H 97