Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // This header defines cross-platform ByteSwap() implementations for 16, 32 and
      6 // 64-bit values, and NetToHostXX() / HostToNextXX() functions equivalent to
      7 // the traditional ntohX() and htonX() functions.
      8 // Use the functions defined here rather than using the platform-specific
      9 // functions directly.
     10 
     11 #ifndef BASE_SYS_BYTEORDER_H_
     12 #define BASE_SYS_BYTEORDER_H_
     13 
     14 #include "base/basictypes.h"
     15 #include "build/build_config.h"
     16 
     17 #if defined(OS_WIN)
     18 #include <winsock2.h>
     19 #else
     20 #include <arpa/inet.h>
     21 #endif
     22 
     23 // Include headers to provide byteswap for all platforms.
     24 #if defined(COMPILER_MSVC)
     25 #include <stdlib.h>
     26 #elif defined(OS_MACOSX)
     27 #include <libkern/OSByteOrder.h>
     28 #elif defined(OS_BSD)
     29 #include <sys/endian.h>
     30 #else
     31 #include <byteswap.h>
     32 #endif
     33 
     34 
     35 namespace base {
     36 
     37 // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness.
     38 inline uint16 ByteSwap(uint16 x) {
     39 #if defined(COMPILER_MSVC)
     40   return _byteswap_ushort(x);
     41 #elif defined(OS_MACOSX)
     42   return OSSwapInt16(x);
     43 #elif defined(OS_OPENBSD)
     44   return swap16(x);
     45 #elif defined(OS_FREEBSD)
     46   return bswap16(x);
     47 #else
     48   return bswap_16(x);
     49 #endif
     50 }
     51 inline uint32 ByteSwap(uint32 x) {
     52 #if defined(COMPILER_MSVC)
     53   return _byteswap_ulong(x);
     54 #elif defined(OS_MACOSX)
     55   return OSSwapInt32(x);
     56 #elif defined(OS_OPENBSD)
     57   return swap32(x);
     58 #elif defined(OS_FREEBSD)
     59   return bswap32(x);
     60 #else
     61   return bswap_32(x);
     62 #endif
     63 }
     64 inline uint64 ByteSwap(uint64 x) {
     65 #if defined(COMPILER_MSVC)
     66   return _byteswap_uint64(x);
     67 #elif defined(OS_MACOSX)
     68   return OSSwapInt64(x);
     69 #elif defined(OS_OPENBSD)
     70   return swap64(x);
     71 #elif defined(OS_FREEBSD)
     72   return bswap64(x);
     73 #else
     74   return bswap_64(x);
     75 #endif
     76 }
     77 
     78 // Converts the bytes in |x| from host order (endianness) to little endian, and
     79 // returns the result.
     80 inline uint16 ByteSwapToLE16(uint16 x) {
     81 #if defined(ARCH_CPU_LITTLE_ENDIAN)
     82   return x;
     83 #else
     84   return ByteSwap(x);
     85 #endif
     86 }
     87 inline uint32 ByteSwapToLE32(uint32 x) {
     88 #if defined(ARCH_CPU_LITTLE_ENDIAN)
     89   return x;
     90 #else
     91   return ByteSwap(x);
     92 #endif
     93 }
     94 inline uint64 ByteSwapToLE64(uint64 x) {
     95 #if defined(ARCH_CPU_LITTLE_ENDIAN)
     96   return x;
     97 #else
     98   return ByteSwap(x);
     99 #endif
    100 }
    101 
    102 // Converts the bytes in |x| from network to host order (endianness), and
    103 // returns the result.
    104 inline uint16 NetToHost16(uint16 x) {
    105 #if defined(ARCH_CPU_LITTLE_ENDIAN)
    106   return ByteSwap(x);
    107 #else
    108   return x;
    109 #endif
    110 }
    111 inline uint32 NetToHost32(uint32 x) {
    112 #if defined(ARCH_CPU_LITTLE_ENDIAN)
    113   return ByteSwap(x);
    114 #else
    115   return x;
    116 #endif
    117 }
    118 inline uint64 NetToHost64(uint64 x) {
    119 #if defined(ARCH_CPU_LITTLE_ENDIAN)
    120   return ByteSwap(x);
    121 #else
    122   return x;
    123 #endif
    124 }
    125 
    126 // Converts the bytes in |x| from host to network order (endianness), and
    127 // returns the result.
    128 inline uint16 HostToNet16(uint16 x) {
    129 #if defined(ARCH_CPU_LITTLE_ENDIAN)
    130   return ByteSwap(x);
    131 #else
    132   return x;
    133 #endif
    134 }
    135 inline uint32 HostToNet32(uint32 x) {
    136 #if defined(ARCH_CPU_LITTLE_ENDIAN)
    137   return ByteSwap(x);
    138 #else
    139   return x;
    140 #endif
    141 }
    142 inline uint64 HostToNet64(uint64 x) {
    143 #if defined(ARCH_CPU_LITTLE_ENDIAN)
    144   return ByteSwap(x);
    145 #else
    146   return x;
    147 #endif
    148 }
    149 
    150 }  // namespace base
    151 
    152 
    153 #endif  // BASE_SYS_BYTEORDER_H_
    154