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 <stdint.h>
     15 
     16 #include "build/build_config.h"
     17 
     18 namespace base {
     19 
     20 // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness.
     21 inline uint16_t ByteSwap(uint16_t x) {
     22   return ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
     23 }
     24 
     25 inline uint32_t ByteSwap(uint32_t x) {
     26   return ((x & 0x000000fful) << 24) | ((x & 0x0000ff00ul) << 8) |
     27       ((x & 0x00ff0000ul) >> 8) | ((x & 0xff000000ul) >> 24);
     28 }
     29 
     30 inline uint64_t ByteSwap(uint64_t x) {
     31   return ((x & 0x00000000000000ffull) << 56) |
     32       ((x & 0x000000000000ff00ull) << 40) |
     33       ((x & 0x0000000000ff0000ull) << 24) |
     34       ((x & 0x00000000ff000000ull) << 8) |
     35       ((x & 0x000000ff00000000ull) >> 8) |
     36       ((x & 0x0000ff0000000000ull) >> 24) |
     37       ((x & 0x00ff000000000000ull) >> 40) |
     38       ((x & 0xff00000000000000ull) >> 56);
     39 }
     40 
     41 // Converts the bytes in |x| from host order (endianness) to little endian, and
     42 // returns the result.
     43 inline uint16_t ByteSwapToLE16(uint16_t x) {
     44 #if defined(ARCH_CPU_LITTLE_ENDIAN)
     45   return x;
     46 #else
     47   return ByteSwap(x);
     48 #endif
     49 }
     50 inline uint32_t ByteSwapToLE32(uint32_t x) {
     51 #if defined(ARCH_CPU_LITTLE_ENDIAN)
     52   return x;
     53 #else
     54   return ByteSwap(x);
     55 #endif
     56 }
     57 inline uint64_t ByteSwapToLE64(uint64_t x) {
     58 #if defined(ARCH_CPU_LITTLE_ENDIAN)
     59   return x;
     60 #else
     61   return ByteSwap(x);
     62 #endif
     63 }
     64 
     65 // Converts the bytes in |x| from network to host order (endianness), and
     66 // returns the result.
     67 inline uint16_t NetToHost16(uint16_t x) {
     68 #if defined(ARCH_CPU_LITTLE_ENDIAN)
     69   return ByteSwap(x);
     70 #else
     71   return x;
     72 #endif
     73 }
     74 inline uint32_t NetToHost32(uint32_t x) {
     75 #if defined(ARCH_CPU_LITTLE_ENDIAN)
     76   return ByteSwap(x);
     77 #else
     78   return x;
     79 #endif
     80 }
     81 inline uint64_t NetToHost64(uint64_t x) {
     82 #if defined(ARCH_CPU_LITTLE_ENDIAN)
     83   return ByteSwap(x);
     84 #else
     85   return x;
     86 #endif
     87 }
     88 
     89 // Converts the bytes in |x| from host to network order (endianness), and
     90 // returns the result.
     91 inline uint16_t HostToNet16(uint16_t x) {
     92 #if defined(ARCH_CPU_LITTLE_ENDIAN)
     93   return ByteSwap(x);
     94 #else
     95   return x;
     96 #endif
     97 }
     98 inline uint32_t HostToNet32(uint32_t x) {
     99 #if defined(ARCH_CPU_LITTLE_ENDIAN)
    100   return ByteSwap(x);
    101 #else
    102   return x;
    103 #endif
    104 }
    105 inline uint64_t HostToNet64(uint64_t x) {
    106 #if defined(ARCH_CPU_LITTLE_ENDIAN)
    107   return ByteSwap(x);
    108 #else
    109   return x;
    110 #endif
    111 }
    112 
    113 }  // namespace base
    114 
    115 #endif  // BASE_SYS_BYTEORDER_H_
    116