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