]>
Commit | Line | Data |
---|---|---|
2aa62f2b | 1 | /** @File\r |
2 | Routines for translating between host and network byte-order.\r | |
3 | \r | |
4 | Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r | |
5 | This program and the accompanying materials are licensed and made available\r | |
6 | under the terms and conditions of the BSD License that accompanies this\r | |
7 | distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | #include <Library/BaseLib.h>\r | |
15 | #include <LibConfig.h>\r | |
16 | #include <sys/endian.h>\r | |
17 | \r | |
18 | // Undefine macro versions of the functions to be defined below.\r | |
19 | #undef htonl\r | |
20 | #undef htons\r | |
21 | #undef ntohl\r | |
22 | #undef ntohs\r | |
23 | \r | |
24 | /** 32-bit Host to Network byte order conversion.\r | |
25 | \r | |
26 | @param[in] Datum The 32-bit value to be converted.\r | |
27 | @return Datum, converted to network byte order.\r | |
28 | **/\r | |
29 | uint32_t\r | |
30 | htonl(\r | |
31 | IN uint32_t Datum\r | |
32 | )\r | |
33 | {\r | |
34 | #if BYTE_ORDER == LITTLE_ENDIAN\r | |
35 | return SwapBytes32(Datum);\r | |
36 | #else\r | |
37 | return Datum;\r | |
38 | #endif\r | |
39 | }\r | |
40 | \r | |
41 | /** 16-bit Host to Network byte order conversion.\r | |
42 | \r | |
43 | @param[in] Datum The 16-bit value to be converted.\r | |
44 | @return Datum, converted to network byte order.\r | |
45 | **/\r | |
46 | uint16_t\r | |
47 | htons(\r | |
48 | IN uint16_t Datum\r | |
49 | )\r | |
50 | {\r | |
51 | #if BYTE_ORDER == LITTLE_ENDIAN\r | |
52 | return SwapBytes16(Datum);\r | |
53 | #else\r | |
54 | return Datum;\r | |
55 | #endif\r | |
56 | }\r | |
57 | \r | |
58 | /** 32-bit Network to Host byte order conversion.\r | |
59 | \r | |
60 | @param[in] Datum The 16-bit value to be converted.\r | |
61 | @return Datum, converted to host byte order.\r | |
62 | **/\r | |
63 | uint32_t\r | |
64 | ntohl(\r | |
65 | IN uint32_t Datum\r | |
66 | )\r | |
67 | {\r | |
68 | #if BYTE_ORDER == LITTLE_ENDIAN\r | |
69 | return SwapBytes32(Datum);\r | |
70 | #else\r | |
71 | return Datum;\r | |
72 | #endif\r | |
73 | }\r | |
74 | \r | |
75 | /** 16-bit Network to Host byte order conversion.\r | |
76 | \r | |
77 | @param[in] Datum The 16-bit value to be converted.\r | |
78 | @return Datum, converted to host byte order.\r | |
79 | **/\r | |
80 | uint16_t\r | |
81 | ntohs(\r | |
82 | IN uint16_t Datum\r | |
83 | )\r | |
84 | {\r | |
85 | #if BYTE_ORDER == LITTLE_ENDIAN\r | |
86 | return SwapBytes16(Datum);\r | |
87 | #else\r | |
88 | return Datum;\r | |
89 | #endif\r | |
90 | }\r |