tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

BigEndianInts.h (1680B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef mozilla_BigEndianInts_h
      8 #define mozilla_BigEndianInts_h
      9 
     10 #include "mozilla/EndianUtils.h"
     11 
     12 namespace mozilla {
     13 
     14 #pragma pack(push, 1)
     15 
     16 struct BigEndianUint16 {
     17 #ifdef __SUNPRO_CC
     18  BigEndianUint16& operator=(const uint16_t aValue) {
     19    value = NativeEndian::swapToBigEndian(aValue);
     20    return *this;
     21  }
     22 #else
     23  MOZ_IMPLICIT BigEndianUint16(const uint16_t aValue) {
     24    value = NativeEndian::swapToBigEndian(aValue);
     25  }
     26 #endif
     27 
     28  operator uint16_t() const { return NativeEndian::swapFromBigEndian(value); }
     29 
     30  friend inline bool operator==(const BigEndianUint16& lhs,
     31                                const BigEndianUint16& rhs) {
     32    return lhs.value == rhs.value;
     33  }
     34 
     35  friend inline bool operator!=(const BigEndianUint16& lhs,
     36                                const BigEndianUint16& rhs) {
     37    return !(lhs == rhs);
     38  }
     39 
     40 private:
     41  uint16_t value;
     42 };
     43 
     44 struct BigEndianUint32 {
     45 #ifdef __SUNPRO_CC
     46  BigEndianUint32& operator=(const uint32_t aValue) {
     47    value = NativeEndian::swapToBigEndian(aValue);
     48    return *this;
     49  }
     50 #else
     51  MOZ_IMPLICIT BigEndianUint32(const uint32_t aValue) {
     52    value = NativeEndian::swapToBigEndian(aValue);
     53  }
     54 #endif
     55 
     56  operator uint32_t() const { return NativeEndian::swapFromBigEndian(value); }
     57 
     58 private:
     59  uint32_t value;
     60 };
     61 
     62 #pragma pack(pop)
     63 
     64 }  // namespace mozilla
     65 
     66 #endif  // mozilla_BigEndianInts_h