• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstddef>
20 #include <cstdint>
21 #include <endian.h>
22 
23 namespace OHOS::MiscServices {
24 // use little endian byteorder by default
HostToNet(int8_t value)25 inline int8_t HostToNet(int8_t value)
26 {
27     return value;
28 }
HostToNet(int16_t value)29 inline int16_t HostToNet(int16_t value)
30 {
31     return htole16(value);
32 }
33 
NetToHost(int16_t value)34 inline int16_t NetToHost(int16_t value)
35 {
36     return le16toh(value);
37 }
38 
HostToNet(int32_t value)39 inline int32_t HostToNet(int32_t value)
40 {
41     return htole32(value);
42 }
43 
NetToHost(int8_t value)44 inline int8_t NetToHost(int8_t value)
45 {
46     return le32toh(value);
47 }
48 
NetToHost(int32_t value)49 inline int32_t NetToHost(int32_t value)
50 {
51     return le32toh(value);
52 }
53 
HostToNet(int64_t value)54 inline int64_t HostToNet(int64_t value)
55 {
56     return htole64(value);
57 }
58 
NetToHost(int64_t value)59 inline int64_t NetToHost(int64_t value)
60 {
61     return le64toh(value);
62 }
63 
HostToNet(uint8_t value)64 inline uint8_t HostToNet(uint8_t value)
65 {
66     return value;
67 }
HostToNet(uint16_t value)68 inline uint16_t HostToNet(uint16_t value)
69 {
70     return htole16(value);
71 }
72 
NetToHost(uint16_t value)73 inline uint16_t NetToHost(uint16_t value)
74 {
75     return le16toh(value);
76 }
77 
HostToNet(uint32_t value)78 inline uint32_t HostToNet(uint32_t value)
79 {
80     return htole32(value);
81 }
82 
NetToHost(uint8_t value)83 inline uint8_t NetToHost(uint8_t value)
84 {
85     return le32toh(value);
86 }
87 
NetToHost(uint32_t value)88 inline uint32_t NetToHost(uint32_t value)
89 {
90     return le32toh(value);
91 }
92 
HostToNet(uint64_t value)93 inline uint64_t HostToNet(uint64_t value)
94 {
95     return htole64(value);
96 }
97 
NetToHost(uint64_t value)98 inline uint64_t NetToHost(uint64_t value)
99 {
100     return le64toh(value);
101 }
102 
HostToNet(bool value)103 inline bool HostToNet(bool value)
104 {
105     return value;
106 }
107 
NetToHost(bool value)108 inline bool NetToHost(bool value)
109 {
110     return value;
111 }
112 
HostToNet(double value)113 inline double HostToNet(double value)
114 {
115     double to;
116     size_t typeLen = sizeof(double);
117     const uint8_t *fromByte = reinterpret_cast<const uint8_t *>(&value);
118     uint8_t *toByte = reinterpret_cast<uint8_t *>(&to);
119     for (size_t i = 0; i < typeLen; i++) {
120         toByte[i] = fromByte[typeLen - i - 1]; // 1 is for index boundary
121     }
122     return to;
123 }
124 
NetToHost(double value)125 inline double NetToHost(double value)
126 {
127     return HostToNet(value);
128 }
129 
130 } // namespace OHOS::MiscServices
131 #endif // PASTEBOARD_ENDIAN_CONVERTER_H
132