blob: 13c9b6b91b86999b83de7665a59cdbaddefcb837 [file] [log] [blame]
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +09001/*
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
Jeremiah50819ce2022-02-08 12:46:45 +010028#include <winsock2.h>
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090029#pragma comment(lib, "ws2_32.lib")
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090030#define inline __inline
31#endif
32
George Koehler1e17c932023-02-06 18:08:05 -050033static 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}
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090047
George Koehler1e17c932023-02-06 18:08:05 -050048#define htonll(n) ntohll(n)
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090049
George Koehler1e17c932023-02-06 18:08:05 -050050static inline unsigned long long letohll(unsigned long long n) {
51 union {
52 unsigned long long f;
53 unsigned char t[8];
54 } u;
55 u.f = n;
56 return static_cast<unsigned long long>(u.t[0]) | static_cast<unsigned long long>(u.t[1]) << 8
57 | static_cast<unsigned long long>(u.t[2]) << 16
58 | static_cast<unsigned long long>(u.t[3]) << 24
59 | static_cast<unsigned long long>(u.t[4]) << 32
60 | static_cast<unsigned long long>(u.t[5]) << 40
61 | static_cast<unsigned long long>(u.t[6]) << 48
62 | static_cast<unsigned long long>(u.t[7]) << 56;
63}
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090064
George Koehler1e17c932023-02-06 18:08:05 -050065#define htolell(n) letohll(n)
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090066
67#endif // THRIFT_PY_ENDIAN_H