1 /* 2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PASTEBOARD_ENDIAN_CONVERTER_H 17 #define PASTEBOARD_ENDIAN_CONVERTER_H 18 19 #include <cstdint> 20 #include <endian.h> 21 22 namespace OHOS::MiscServices { 23 // use little endian byteorder by default HostToNet(int8_t value)24static inline int8_t HostToNet(int8_t value) 25 { 26 return value; 27 } HostToNet(int16_t value)28static inline int16_t HostToNet(int16_t value) 29 { 30 return htole16(value); 31 } 32 NetToHost(int16_t value)33static inline int16_t NetToHost(int16_t value) 34 { 35 return le16toh(value); 36 } 37 HostToNet(int32_t value)38static inline int32_t HostToNet(int32_t value) 39 { 40 return htole32(value); 41 } 42 NetToHost(int8_t value)43static inline int8_t NetToHost(int8_t value) 44 { 45 return le32toh(value); 46 } 47 NetToHost(int32_t value)48static inline int32_t NetToHost(int32_t value) 49 { 50 return le32toh(value); 51 } 52 HostToNet(int64_t value)53static inline int64_t HostToNet(int64_t value) 54 { 55 return htole64(value); 56 } 57 NetToHost(int64_t value)58static inline int64_t NetToHost(int64_t value) 59 { 60 return le64toh(value); 61 } 62 HostToNet(uint8_t value)63static inline uint8_t HostToNet(uint8_t value) 64 { 65 return value; 66 } HostToNet(uint16_t value)67static inline uint16_t HostToNet(uint16_t value) 68 { 69 return htole16(value); 70 } 71 NetToHost(uint16_t value)72static inline uint16_t NetToHost(uint16_t value) 73 { 74 return le16toh(value); 75 } 76 HostToNet(uint32_t value)77static inline uint32_t HostToNet(uint32_t value) 78 { 79 return htole32(value); 80 } 81 NetToHost(uint8_t value)82static inline uint8_t NetToHost(uint8_t value) 83 { 84 return le32toh(value); 85 } 86 NetToHost(uint32_t value)87static inline uint32_t NetToHost(uint32_t value) 88 { 89 return le32toh(value); 90 } 91 HostToNet(uint64_t value)92static inline uint64_t HostToNet(uint64_t value) 93 { 94 return htole64(value); 95 } 96 NetToHost(uint64_t value)97static inline uint64_t NetToHost(uint64_t value) 98 { 99 return le64toh(value); 100 } 101 HostToNet(bool value)102static inline bool HostToNet(bool value) 103 { 104 return value; 105 } 106 NetToHost(bool value)107static inline bool NetToHost(bool value) 108 { 109 return value; 110 } 111 HostToNet(double value)112inline double HostToNet(double value) 113 { 114 double to; 115 size_t typeLen = sizeof(double); 116 const uint8_t* fromByte = reinterpret_cast<const uint8_t*>(&value); 117 uint8_t* toByte = reinterpret_cast<uint8_t*>(&to); 118 for (size_t i = 0; i < typeLen; i++) { 119 toByte[i] = fromByte[typeLen - i - 1]; // 1 is for index boundary 120 } 121 return to; 122 } 123 NetToHost(double value)124inline double NetToHost(double value) 125 { 126 return HostToNet(value); 127 } 128 129 } // namespace OHOS::MiscServices 130 #endif // PASTEBOARD_ENDIAN_CONVERTER_H 131