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