• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2022 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 <endian.h>
20 
21 #include <cstdint>
22 
23 namespace OHOS::MiscServices {
24 // use little endian byteorder by default
HostToNet(int8_t value)25 static inline int8_t HostToNet(int8_t value)
26 {
27     return value;
28 }
HostToNet(int16_t value)29 static inline int16_t HostToNet(int16_t value)
30 {
31     return htole16(value);
32 }
33 
NetToHost(int16_t value)34 static inline int16_t NetToHost(int16_t value)
35 {
36     return le16toh(value);
37 }
38 
HostToNet(int32_t value)39 static inline int32_t HostToNet(int32_t value)
40 {
41     return htole32(value);
42 }
43 
NetToHost(int8_t value)44 static inline int8_t NetToHost(int8_t value)
45 {
46     return le32toh(value);
47 }
48 
NetToHost(int32_t value)49 static inline int32_t NetToHost(int32_t value)
50 {
51     return le32toh(value);
52 }
53 
HostToNet(int64_t value)54 static inline int64_t HostToNet(int64_t value)
55 {
56     return htole64(value);
57 }
58 
NetToHost(int64_t value)59 static inline int64_t NetToHost(int64_t value)
60 {
61     return le64toh(value);
62 }
63 
HostToNet(uint8_t value)64 static inline uint8_t HostToNet(uint8_t value)
65 {
66     return value;
67 }
HostToNet(uint16_t value)68 static inline uint16_t HostToNet(uint16_t value)
69 {
70     return htole16(value);
71 }
72 
NetToHost(uint16_t value)73 static inline uint16_t NetToHost(uint16_t value)
74 {
75     return le16toh(value);
76 }
77 
HostToNet(uint32_t value)78 static inline uint32_t HostToNet(uint32_t value)
79 {
80     return htole32(value);
81 }
82 
NetToHost(uint8_t value)83 static inline uint8_t NetToHost(uint8_t value)
84 {
85     return le32toh(value);
86 }
87 
NetToHost(uint32_t value)88 static inline uint32_t NetToHost(uint32_t value)
89 {
90     return le32toh(value);
91 }
92 
HostToNet(uint64_t value)93 static inline uint64_t HostToNet(uint64_t value)
94 {
95     return htole64(value);
96 }
97 
NetToHost(uint64_t value)98 static inline uint64_t NetToHost(uint64_t value)
99 {
100     return le64toh(value);
101 }
102 } // namespace OHOS::MiscServices
103 #endif // PASTEBOARD_ENDIAN_CONVERTER_H
104