summaryrefslogtreecommitdiff
path: root/StdLib/LibC/Main/HtoNtoH.c
blob: c501874b845b6da1473a3a927f4aeb6d23ac41f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/** @File
    Routines for translating between host and network byte-order.

    Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
    This program and the accompanying materials are licensed and made available
    under the terms and conditions of the BSD License that accompanies this
    distribution.  The full text of the license may be found at
    http://opensource.org/licenses/bsd-license.

    THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
    WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/
#include  <Library/BaseLib.h>
#include  <LibConfig.h>
#include  <sys/endian.h>

// Undefine macro versions of the functions to be defined below.
#undef  htonl
#undef  htons
#undef  ntohl
#undef  ntohs

/** 32-bit Host to Network byte order conversion.

  @param[in]  Datum   The 32-bit value to be converted.
  @return     Datum, converted to network byte order.
**/
uint32_t
htonl(
  IN  uint32_t Datum
  )
{
#if BYTE_ORDER == LITTLE_ENDIAN
  return SwapBytes32(Datum);
#else
  return Datum;
#endif
}

/** 16-bit Host to Network byte order conversion.

  @param[in]  Datum   The 16-bit value to be converted.
  @return     Datum, converted to network byte order.
**/
uint16_t
htons(
  IN  uint16_t Datum
  )
{
#if BYTE_ORDER == LITTLE_ENDIAN
  return SwapBytes16(Datum);
#else
  return Datum;
#endif
}

/** 32-bit Network to Host byte order conversion.

  @param[in]  Datum   The 16-bit value to be converted.
  @return     Datum, converted to host byte order.
**/
uint32_t
ntohl(
  IN  uint32_t Datum
  )
{
#if BYTE_ORDER == LITTLE_ENDIAN
  return SwapBytes32(Datum);
#else
  return Datum;
#endif
}

/** 16-bit Network to Host byte order conversion.

  @param[in]  Datum   The 16-bit value to be converted.
  @return     Datum, converted to host byte order.
**/
uint16_t
ntohs(
  IN uint16_t Datum
  )
{
#if BYTE_ORDER == LITTLE_ENDIAN
  return SwapBytes16(Datum);
#else
  return Datum;
#endif
}