File indexing completed on 2025-01-31 10:12:19
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef GOOGLE_PROTOBUF_ENDIAN_H__
0009 #define GOOGLE_PROTOBUF_ENDIAN_H__
0010
0011 #if defined(_MSC_VER)
0012 #include <stdlib.h>
0013 #endif
0014
0015 #include <cstdint>
0016
0017 #include "absl/base/config.h"
0018
0019
0020 #include "google/protobuf/port_def.inc"
0021
0022 namespace google {
0023 namespace protobuf {
0024 namespace internal {
0025
0026 inline uint64_t BSwap64(uint64_t host_int) {
0027 #if defined(__GNUC__) || ABSL_HAVE_BUILTIN(__builtin_bswap64)
0028 return __builtin_bswap64(host_int);
0029 #elif defined(_MSC_VER)
0030 return _byteswap_uint64(host_int);
0031 #else
0032 return (((host_int & uint64_t{0xFF}) << 56) |
0033 ((host_int & uint64_t{0xFF00}) << 40) |
0034 ((host_int & uint64_t{0xFF0000}) << 24) |
0035 ((host_int & uint64_t{0xFF000000}) << 8) |
0036 ((host_int & uint64_t{0xFF00000000}) >> 8) |
0037 ((host_int & uint64_t{0xFF0000000000}) >> 24) |
0038 ((host_int & uint64_t{0xFF000000000000}) >> 40) |
0039 ((host_int & uint64_t{0xFF00000000000000}) >> 56));
0040 #endif
0041 }
0042
0043 inline uint32_t BSwap32(uint32_t host_int) {
0044 #if defined(__GNUC__) || ABSL_HAVE_BUILTIN(__builtin_bswap32)
0045 return __builtin_bswap32(host_int);
0046 #elif defined(_MSC_VER)
0047 return _byteswap_ulong(host_int);
0048 #else
0049 return (((host_int & uint32_t{0xFF}) << 24) |
0050 ((host_int & uint32_t{0xFF00}) << 8) |
0051 ((host_int & uint32_t{0xFF0000}) >> 8) |
0052 ((host_int & uint32_t{0xFF000000}) >> 24));
0053 #endif
0054 }
0055
0056 inline uint16_t BSwap16(uint16_t host_int) {
0057 #if defined(__GNUC__) || ABSL_HAVE_BUILTIN(__builtin_bswap16)
0058 return __builtin_bswap16(host_int);
0059 #elif defined(_MSC_VER)
0060 return _byteswap_ushort(host_int);
0061 #else
0062 return (((host_int & uint16_t{0xFF}) << 8) |
0063 ((host_int & uint16_t{0xFF00}) >> 8));
0064 #endif
0065 }
0066
0067 namespace little_endian {
0068
0069 inline uint16_t FromHost(uint16_t value) {
0070 #if defined(ABSL_IS_BIG_ENDIAN)
0071 return BSwap16(value);
0072 #else
0073 return value;
0074 #endif
0075 }
0076
0077 inline uint32_t FromHost(uint32_t value) {
0078 #if defined(ABSL_IS_BIG_ENDIAN)
0079 return BSwap32(value);
0080 #else
0081 return value;
0082 #endif
0083 }
0084
0085 inline uint64_t FromHost(uint64_t value) {
0086 #if defined(ABSL_IS_BIG_ENDIAN)
0087 return BSwap64(value);
0088 #else
0089 return value;
0090 #endif
0091 }
0092
0093 inline uint16_t ToHost(uint16_t value) {
0094 #if defined(ABSL_IS_BIG_ENDIAN)
0095 return BSwap16(value);
0096 #else
0097 return value;
0098 #endif
0099 }
0100
0101 inline uint32_t ToHost(uint32_t value) {
0102 #if defined(ABSL_IS_BIG_ENDIAN)
0103 return BSwap32(value);
0104 #else
0105 return value;
0106 #endif
0107 }
0108
0109 inline uint64_t ToHost(uint64_t value) {
0110 #if defined(ABSL_IS_BIG_ENDIAN)
0111 return BSwap64(value);
0112 #else
0113 return value;
0114 #endif
0115 }
0116
0117 }
0118
0119 namespace big_endian {
0120
0121 inline uint16_t FromHost(uint16_t value) {
0122 #if defined(ABSL_IS_BIG_ENDIAN)
0123 return value;
0124 #else
0125 return BSwap16(value);
0126 #endif
0127 }
0128
0129 inline uint32_t FromHost(uint32_t value) {
0130 #if defined(ABSL_IS_BIG_ENDIAN)
0131 return value;
0132 #else
0133 return BSwap32(value);
0134 #endif
0135 }
0136
0137 inline uint64_t FromHost(uint64_t value) {
0138 #if defined(ABSL_IS_BIG_ENDIAN)
0139 return value;
0140 #else
0141 return BSwap64(value);
0142 #endif
0143 }
0144
0145 inline uint16_t ToHost(uint16_t value) {
0146 #if defined(ABSL_IS_BIG_ENDIAN)
0147 return value;
0148 #else
0149 return BSwap16(value);
0150 #endif
0151 }
0152
0153 inline uint32_t ToHost(uint32_t value) {
0154 #if defined(ABSL_IS_BIG_ENDIAN)
0155 return value;
0156 #else
0157 return BSwap32(value);
0158 #endif
0159 }
0160
0161 inline uint64_t ToHost(uint64_t value) {
0162 #if defined(ABSL_IS_BIG_ENDIAN)
0163 return value;
0164 #else
0165 return BSwap64(value);
0166 #endif
0167 }
0168
0169 }
0170
0171 }
0172 }
0173 }
0174
0175 #include "google/protobuf/port_undef.inc"
0176
0177 #endif