| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 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 | |
| Shaoyu Zhang | 55bc35f | 2025-11-25 13:59:16 -0800 | [diff] [blame] | 25 | #ifdef _WIN32 |
| Jeremiah | 50819ce | 2022-02-08 12:46:45 +0100 | [diff] [blame] | 26 | #include <winsock2.h> |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 27 | #pragma comment(lib, "ws2_32.lib") |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 28 | #define inline __inline |
| Shaoyu Zhang | 55bc35f | 2025-11-25 13:59:16 -0800 | [diff] [blame] | 29 | #else |
| 30 | #include <netinet/in.h> |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 31 | |
| Gregg Donovan | 62ec929 | 2026-01-29 16:51:37 -0500 | [diff] [blame^] | 32 | #ifndef ntohll |
| George Koehler | 1e17c93 | 2023-02-06 18:08:05 -0500 | [diff] [blame] | 33 | static inline unsigned long long ntohll(unsigned long long n) { |
| 34 | union { |
| 35 | unsigned long long f; |
| 36 | unsigned char t[8]; |
| 37 | } u; |
| 38 | u.f = n; |
| 39 | return static_cast<unsigned long long>(u.t[0]) << 56 |
| 40 | | static_cast<unsigned long long>(u.t[1]) << 48 |
| 41 | | static_cast<unsigned long long>(u.t[2]) << 40 |
| 42 | | static_cast<unsigned long long>(u.t[3]) << 32 |
| 43 | | static_cast<unsigned long long>(u.t[4]) << 24 |
| 44 | | static_cast<unsigned long long>(u.t[5]) << 16 |
| 45 | | static_cast<unsigned long long>(u.t[6]) << 8 | static_cast<unsigned long long>(u.t[7]); |
| 46 | } |
| Gregg Donovan | 62ec929 | 2026-01-29 16:51:37 -0500 | [diff] [blame^] | 47 | #endif |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 48 | |
| Gregg Donovan | 62ec929 | 2026-01-29 16:51:37 -0500 | [diff] [blame^] | 49 | #ifndef htonll |
| George Koehler | 1e17c93 | 2023-02-06 18:08:05 -0500 | [diff] [blame] | 50 | #define htonll(n) ntohll(n) |
| Gregg Donovan | 62ec929 | 2026-01-29 16:51:37 -0500 | [diff] [blame^] | 51 | #endif |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 52 | |
| Shaoyu Zhang | 55bc35f | 2025-11-25 13:59:16 -0800 | [diff] [blame] | 53 | #endif // !_WIN32 |
| 54 | |
| George Koehler | 1e17c93 | 2023-02-06 18:08:05 -0500 | [diff] [blame] | 55 | static inline unsigned long long letohll(unsigned long long n) { |
| 56 | union { |
| 57 | unsigned long long f; |
| 58 | unsigned char t[8]; |
| 59 | } u; |
| 60 | u.f = n; |
| 61 | return static_cast<unsigned long long>(u.t[0]) | static_cast<unsigned long long>(u.t[1]) << 8 |
| 62 | | static_cast<unsigned long long>(u.t[2]) << 16 |
| 63 | | static_cast<unsigned long long>(u.t[3]) << 24 |
| 64 | | static_cast<unsigned long long>(u.t[4]) << 32 |
| 65 | | static_cast<unsigned long long>(u.t[5]) << 40 |
| 66 | | static_cast<unsigned long long>(u.t[6]) << 48 |
| 67 | | static_cast<unsigned long long>(u.t[7]) << 56; |
| 68 | } |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 69 | |
| George Koehler | 1e17c93 | 2023-02-06 18:08:05 -0500 | [diff] [blame] | 70 | #define htolell(n) letohll(n) |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 71 | |
| 72 | #endif // THRIFT_PY_ENDIAN_H |