Home | History | Annotate | Download | only in base
      1 /*
      2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #ifndef WEBRTC_BASE_BYTEORDER_H_
     12 #define WEBRTC_BASE_BYTEORDER_H_
     13 
     14 #if defined(WEBRTC_POSIX) && !defined(__native_client__)
     15 #include <arpa/inet.h>
     16 #endif
     17 
     18 #if defined(WEBRTC_WIN)
     19 #include <stdlib.h>
     20 #endif
     21 
     22 #include "webrtc/base/basictypes.h"
     23 
     24 namespace rtc {
     25 
     26 // Reading and writing of little and big-endian numbers from memory
     27 // TODO: Optimized versions, with direct read/writes of
     28 // integers in host-endian format, when the platform supports it.
     29 
     30 inline void Set8(void* memory, size_t offset, uint8 v) {
     31   static_cast<uint8*>(memory)[offset] = v;
     32 }
     33 
     34 inline uint8 Get8(const void* memory, size_t offset) {
     35   return static_cast<const uint8*>(memory)[offset];
     36 }
     37 
     38 inline void SetBE16(void* memory, uint16 v) {
     39   Set8(memory, 0, static_cast<uint8>(v >> 8));
     40   Set8(memory, 1, static_cast<uint8>(v >> 0));
     41 }
     42 
     43 inline void SetBE32(void* memory, uint32 v) {
     44   Set8(memory, 0, static_cast<uint8>(v >> 24));
     45   Set8(memory, 1, static_cast<uint8>(v >> 16));
     46   Set8(memory, 2, static_cast<uint8>(v >> 8));
     47   Set8(memory, 3, static_cast<uint8>(v >> 0));
     48 }
     49 
     50 inline void SetBE64(void* memory, uint64 v) {
     51   Set8(memory, 0, static_cast<uint8>(v >> 56));
     52   Set8(memory, 1, static_cast<uint8>(v >> 48));
     53   Set8(memory, 2, static_cast<uint8>(v >> 40));
     54   Set8(memory, 3, static_cast<uint8>(v >> 32));
     55   Set8(memory, 4, static_cast<uint8>(v >> 24));
     56   Set8(memory, 5, static_cast<uint8>(v >> 16));
     57   Set8(memory, 6, static_cast<uint8>(v >> 8));
     58   Set8(memory, 7, static_cast<uint8>(v >> 0));
     59 }
     60 
     61 inline uint16 GetBE16(const void* memory) {
     62   return static_cast<uint16>((Get8(memory, 0) << 8) |
     63                              (Get8(memory, 1) << 0));
     64 }
     65 
     66 inline uint32 GetBE32(const void* memory) {
     67   return (static_cast<uint32>(Get8(memory, 0)) << 24) |
     68       (static_cast<uint32>(Get8(memory, 1)) << 16) |
     69       (static_cast<uint32>(Get8(memory, 2)) << 8) |
     70       (static_cast<uint32>(Get8(memory, 3)) << 0);
     71 }
     72 
     73 inline uint64 GetBE64(const void* memory) {
     74   return (static_cast<uint64>(Get8(memory, 0)) << 56) |
     75       (static_cast<uint64>(Get8(memory, 1)) << 48) |
     76       (static_cast<uint64>(Get8(memory, 2)) << 40) |
     77       (static_cast<uint64>(Get8(memory, 3)) << 32) |
     78       (static_cast<uint64>(Get8(memory, 4)) << 24) |
     79       (static_cast<uint64>(Get8(memory, 5)) << 16) |
     80       (static_cast<uint64>(Get8(memory, 6)) << 8) |
     81       (static_cast<uint64>(Get8(memory, 7)) << 0);
     82 }
     83 
     84 inline void SetLE16(void* memory, uint16 v) {
     85   Set8(memory, 0, static_cast<uint8>(v >> 0));
     86   Set8(memory, 1, static_cast<uint8>(v >> 8));
     87 }
     88 
     89 inline void SetLE32(void* memory, uint32 v) {
     90   Set8(memory, 0, static_cast<uint8>(v >> 0));
     91   Set8(memory, 1, static_cast<uint8>(v >> 8));
     92   Set8(memory, 2, static_cast<uint8>(v >> 16));
     93   Set8(memory, 3, static_cast<uint8>(v >> 24));
     94 }
     95 
     96 inline void SetLE64(void* memory, uint64 v) {
     97   Set8(memory, 0, static_cast<uint8>(v >> 0));
     98   Set8(memory, 1, static_cast<uint8>(v >> 8));
     99   Set8(memory, 2, static_cast<uint8>(v >> 16));
    100   Set8(memory, 3, static_cast<uint8>(v >> 24));
    101   Set8(memory, 4, static_cast<uint8>(v >> 32));
    102   Set8(memory, 5, static_cast<uint8>(v >> 40));
    103   Set8(memory, 6, static_cast<uint8>(v >> 48));
    104   Set8(memory, 7, static_cast<uint8>(v >> 56));
    105 }
    106 
    107 inline uint16 GetLE16(const void* memory) {
    108   return static_cast<uint16>((Get8(memory, 0) << 0) |
    109                              (Get8(memory, 1) << 8));
    110 }
    111 
    112 inline uint32 GetLE32(const void* memory) {
    113   return (static_cast<uint32>(Get8(memory, 0)) << 0) |
    114       (static_cast<uint32>(Get8(memory, 1)) << 8) |
    115       (static_cast<uint32>(Get8(memory, 2)) << 16) |
    116       (static_cast<uint32>(Get8(memory, 3)) << 24);
    117 }
    118 
    119 inline uint64 GetLE64(const void* memory) {
    120   return (static_cast<uint64>(Get8(memory, 0)) << 0) |
    121       (static_cast<uint64>(Get8(memory, 1)) << 8) |
    122       (static_cast<uint64>(Get8(memory, 2)) << 16) |
    123       (static_cast<uint64>(Get8(memory, 3)) << 24) |
    124       (static_cast<uint64>(Get8(memory, 4)) << 32) |
    125       (static_cast<uint64>(Get8(memory, 5)) << 40) |
    126       (static_cast<uint64>(Get8(memory, 6)) << 48) |
    127       (static_cast<uint64>(Get8(memory, 7)) << 56);
    128 }
    129 
    130 // Check if the current host is big endian.
    131 inline bool IsHostBigEndian() {
    132   static const int number = 1;
    133   return 0 == *reinterpret_cast<const char*>(&number);
    134 }
    135 
    136 inline uint16 HostToNetwork16(uint16 n) {
    137   uint16 result;
    138   SetBE16(&result, n);
    139   return result;
    140 }
    141 
    142 inline uint32 HostToNetwork32(uint32 n) {
    143   uint32 result;
    144   SetBE32(&result, n);
    145   return result;
    146 }
    147 
    148 inline uint64 HostToNetwork64(uint64 n) {
    149   uint64 result;
    150   SetBE64(&result, n);
    151   return result;
    152 }
    153 
    154 inline uint16 NetworkToHost16(uint16 n) {
    155   return GetBE16(&n);
    156 }
    157 
    158 inline uint32 NetworkToHost32(uint32 n) {
    159   return GetBE32(&n);
    160 }
    161 
    162 inline uint64 NetworkToHost64(uint64 n) {
    163   return GetBE64(&n);
    164 }
    165 
    166 }  // namespace rtc
    167 
    168 #endif  // WEBRTC_BASE_BYTEORDER_H_
    169