Bug 857957 - Use Endian.h for byte swap. f=nfroyd, r=jfkthame, r/sr=bsmedberg
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Wed, 24 Apr 2013 16:40:33 +0900
changeset 129718 1541b06b7a6ba69c4a9d52809f6fcd049f647b5d
parent 129717 1c5977e8d52f485243a8d409138c2037f3572293
child 129719 cf7a4852d99b48a1747f82288c74520bf2d3e234
push id26968
push userm_kato@ga2.so-net.ne.jp
push dateWed, 24 Apr 2013 07:41:35 +0000
treeherdermozilla-inbound@1541b06b7a6b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjfkthame, r
bugs857957
milestone23.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 857957 - Use Endian.h for byte swap. f=nfroyd, r=jfkthame, r/sr=bsmedberg
dom/indexedDB/Key.cpp
gfx/gl/GLContextProviderGLX.cpp
gfx/thebes/gfxColor.h
gfx/thebes/gfxDWriteFontList.cpp
gfx/thebes/gfxDWriteFonts.cpp
gfx/thebes/gfxFontUtils.cpp
gfx/thebes/gfxFontUtils.h
gfx/thebes/gfxFontconfigUtils.h
gfx/thebes/gfxGDIFontList.cpp
gfx/thebes/gfxSVGGlyphs.cpp
xpcom/io/nsBinaryStream.cpp
xpcom/io/nsIStreamBufferAccess.idl
--- a/dom/indexedDB/Key.cpp
+++ b/dom/indexedDB/Key.cpp
@@ -2,21 +2,21 @@
 /* vim: set ts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/FloatingPoint.h"
 
 #include "Key.h"
-#include "nsIStreamBufferAccess.h"
 #include "jsfriendapi.h"
 #include "nsAlgorithm.h"
 #include "nsJSUtils.h"
 #include "xpcpublic.h"
+#include "mozilla/Endian.h"
 #include <algorithm>
 
 USING_INDEXEDDB_NAMESPACE
 
 /*
  Here's how we encode keys:
 
  Basic strategy is the following
@@ -393,32 +393,31 @@ Key::EncodeNumber(double aFloat, uint8_t
   Float64Union pun;
   pun.d = aFloat;
   // Note: The subtraction from 0 below is necessary to fix
   // MSVC build warning C4146 (negating an unsigned value).
   uint64_t number = pun.u & PR_UINT64(0x8000000000000000) ?
                     (0 - pun.u) :
                     (pun.u | PR_UINT64(0x8000000000000000));
 
-  number = NS_SWAP64(number);
-  memcpy(buffer, &number, sizeof(number));
+  mozilla::BigEndian::writeUint64(buffer, number);
 }
 
 // static
 double
 Key::DecodeNumber(const unsigned char*& aPos, const unsigned char* aEnd)
 {
   NS_ASSERTION(*aPos % eMaxType == eFloat ||
                *aPos % eMaxType == eDate, "Don't call me!");
 
   ++aPos;
 
   uint64_t number = 0;
   memcpy(&number, aPos, std::min<size_t>(sizeof(number), aEnd - aPos));
-  number = NS_SWAP64(number);
+  number = mozilla::NativeEndian::swapFromBigEndian(number);
 
   aPos += sizeof(number);
 
   Float64Union pun;
   // Note: The subtraction from 0 below is necessary to fix
   // MSVC build warning C4146 (negating an unsigned value).
   pun.u = number & PR_UINT64(0x8000000000000000) ?
           (number & ~PR_UINT64(0x8000000000000000)) :
--- a/gfx/gl/GLContextProviderGLX.cpp
+++ b/gfx/gl/GLContextProviderGLX.cpp
@@ -13,16 +13,17 @@
 #endif
 
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
 
 #include "mozilla/X11Util.h"
 
 #include "prenv.h"
+#include "prbit.h" // for PR_FLOOR_LOG2
 #include "GLContextProvider.h"
 #include "GLLibraryLoader.h"
 #include "nsDebug.h"
 #include "nsIWidget.h"
 #include "GLXLibrary.h"
 #include "gfxXlibSurface.h"
 #include "gfxContext.h"
 #include "gfxImageSurface.h"
--- a/gfx/thebes/gfxColor.h
+++ b/gfx/thebes/gfxColor.h
@@ -3,61 +3,33 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef GFX_COLOR_H
 #define GFX_COLOR_H
 
 #include "gfxTypes.h"
 
-#include "prbit.h" // for PR_ROTATE_(LEFT,RIGHT)32
-#include "prio.h"  // for ntohl
-
 #include "mozilla/Attributes.h" // for MOZ_ALWAYS_INLINE
+#include "mozilla/Endian.h" // for mozilla::NativeEndian::swapToBigEndian
 
 #define GFX_UINT32_FROM_BPTR(pbptr,i) (((uint32_t*)(pbptr))[i])
 
-#if defined(IS_BIG_ENDIAN)
-  #define GFX_NTOHL(x) (x)
-  #define GFX_HAVE_CHEAP_NTOHL
-#elif defined(_WIN32)
-  #if (_MSC_VER >= 1300) // also excludes MinGW
-    #include <stdlib.h>
-    #pragma intrinsic(_byteswap_ulong)
-    #define GFX_NTOHL(x) _byteswap_ulong(x)
-    #define GFX_HAVE_CHEAP_NTOHL
-  #else
-    // A reasonably fast generic little-endian implementation.
-    #define GFX_NTOHL(x) \
-         ( (PR_ROTATE_RIGHT32((x),8) & 0xFF00FF00) | \
-           (PR_ROTATE_LEFT32((x),8)  & 0x00FF00FF) )
-  #endif
-#else
-  #define GFX_NTOHL(x) ntohl(x)
-  #define GFX_HAVE_CHEAP_NTOHL
-#endif
-
 /**
  * GFX_0XFF_PPIXEL_FROM_BPTR(x)
  *
  * Avoid tortured construction of 32-bit ARGB pixel from 3 individual bytes
  *   of memory plus constant 0xFF.  RGB bytes are already contiguous!
  * Equivalent to: GFX_PACKED_PIXEL(0xff,r,g,b)
  *
  * Attempt to use fast byte-swapping instruction(s), e.g. bswap on x86, in
  *   preference to a sequence of shift/or operations.
  */
-#if defined(GFX_HAVE_CHEAP_NTOHL)
-  #define GFX_0XFF_PPIXEL_FROM_UINT32(x) \
-       ( (GFX_NTOHL(x) >> 8) | (0xFF << 24) )
-#else
-  // A reasonably fast generic little-endian implementation.
-  #define GFX_0XFF_PPIXEL_FROM_UINT32(x) \
-       ( (PR_ROTATE_LEFT32((x),16) | 0xFF00FF00) & ((x) | 0xFFFF00FF) )
-#endif
+#define GFX_0XFF_PPIXEL_FROM_UINT32(x) \
+  ( (mozilla::NativeEndian::swapToBigEndian(uint32_t(x)) >> 8) | (0xFFU << 24) )
 
 #define GFX_0XFF_PPIXEL_FROM_BPTR(x) \
      ( GFX_0XFF_PPIXEL_FROM_UINT32(GFX_UINT32_FROM_BPTR((x),0)) )
 
 /**
  * GFX_BLOCK_RGB_TO_FRGB(from,to)
  *   sizeof(*from) == sizeof(char)
  *   sizeof(*to)   == sizeof(uint32_t)
@@ -65,19 +37,19 @@
  * Copy 4 pixels at a time, reading blocks of 12 bytes (RGB x4)
  *   and writing blocks of 16 bytes (FRGB x4)
  */
 #define GFX_BLOCK_RGB_TO_FRGB(from,to) \
   PR_BEGIN_MACRO \
     uint32_t m0 = GFX_UINT32_FROM_BPTR(from,0), \
              m1 = GFX_UINT32_FROM_BPTR(from,1), \
              m2 = GFX_UINT32_FROM_BPTR(from,2), \
-             rgbr = GFX_NTOHL(m0), \
-             gbrg = GFX_NTOHL(m1), \
-             brgb = GFX_NTOHL(m2), \
+             rgbr = mozilla::NativeEndian::swapToBigEndian(m0), \
+             gbrg = mozilla::NativeEndian::swapToBigEndian(m1), \
+             brgb = mozilla::NativeEndian::swapToBigEndian(m2), \
              p0, p1, p2, p3; \
     p0 = 0xFF000000 | ((rgbr) >>  8); \
     p1 = 0xFF000000 | ((rgbr) << 16) | ((gbrg) >> 16); \
     p2 = 0xFF000000 | ((gbrg) <<  8) | ((brgb) >> 24); \
     p3 = 0xFF000000 | (brgb); \
     to[0] = p0; to[1] = p1; to[2] = p2; to[3] = p3; \
   PR_END_MACRO
 
--- a/gfx/thebes/gfxDWriteFontList.cpp
+++ b/gfx/thebes/gfxDWriteFontList.cpp
@@ -285,20 +285,23 @@ gfxDWriteFontEntry::GetFontTable(uint32_
         LOGFONTW logfont = { 0 };
         if (!InitLogFont(mFont, &logfont))
             return NS_ERROR_FAILURE;
 
         AutoDC dc;
         AutoSelectFont font(dc.GetDC(), &logfont);
         if (font.IsValid()) {
             uint32_t tableSize =
-                ::GetFontData(dc.GetDC(), NS_SWAP32(aTableTag), 0, NULL, 0);
+                ::GetFontData(dc.GetDC(),
+                              NativeEndian::swapToBigEndian(aTableTag), 0,
+                              NULL, 0);
             if (tableSize != GDI_ERROR) {
                 if (aBuffer.SetLength(tableSize)) {
-                    ::GetFontData(dc.GetDC(), NS_SWAP32(aTableTag), 0,
+                    ::GetFontData(dc.GetDC(),
+                                  NativeEndian::swapToBigEndian(aTableTag), 0,
                                   aBuffer.Elements(), aBuffer.Length());
                     return NS_OK;
                 }
                 return NS_ERROR_OUT_OF_MEMORY;
             }
         }
         return NS_ERROR_FAILURE;
     }
@@ -312,17 +315,17 @@ gfxDWriteFontEntry::GetFontTable(uint32_
     if (NS_FAILED(rv)) {
         return rv;
     }
 
     uint8_t *tableData;
     uint32_t len;
     void *tableContext = NULL;
     BOOL exists;
-    hr = fontFace->TryGetFontTable(NS_SWAP32(aTableTag),
+    hr = fontFace->TryGetFontTable(NativeEndian::swapToBigEndian(aTableTag),
                                    (const void**)&tableData,
                                    &len,
                                    &tableContext,
                                    &exists);
 
     if (FAILED(hr) || !exists) {
         return NS_ERROR_FAILURE;
     }
--- a/gfx/thebes/gfxDWriteFonts.cpp
+++ b/gfx/thebes/gfxDWriteFonts.cpp
@@ -629,17 +629,17 @@ gfxDWriteFont::DestroyBlobFunc(void* aUs
 
 hb_blob_t *
 gfxDWriteFont::GetFontTable(uint32_t aTag)
 {
     const void *data;
     UINT32      size;
     void       *context;
     BOOL        exists;
-    HRESULT hr = mFontFace->TryGetFontTable(NS_SWAP32(aTag),
+    HRESULT hr = mFontFace->TryGetFontTable(mozilla::NativeEndian::swapToBigEndian(aTag),
                                             &data, &size, &context, &exists);
     if (SUCCEEDED(hr) && exists) {
         FontTableRec *ftr = new FontTableRec(mFontFace, context);
         return hb_blob_create(static_cast<const char*>(data), size,
                               HB_MEMORY_MODE_READONLY,
                               ftr, DestroyBlobFunc);
     }
 
--- a/gfx/thebes/gfxFontUtils.cpp
+++ b/gfx/thebes/gfxFontUtils.cpp
@@ -11,17 +11,16 @@
 #include "mozilla/Util.h"
 
 #include "gfxFontUtils.h"
 
 #include "nsServiceManagerUtils.h"
 
 #include "mozilla/Preferences.h"
 
-#include "nsIStreamBufferAccess.h"
 #include "nsIUUIDGenerator.h"
 #include "nsMemory.h"
 #include "nsICharsetConverterManager.h"
 
 #include "plbase64.h"
 #include "prlog.h"
 
 #ifdef XP_MACOSX
@@ -1108,23 +1107,20 @@ gfxFontUtils::RenameFont(const nsAString
         nameRecord->nameID = neededNameIDs[i];
         nameRecord->offset = 0;
         nameRecord->length = nameStrLength;
     }
     
     // -- string data, located after the name records, stored in big-endian form
     PRUnichar *strData = reinterpret_cast<PRUnichar*>(nameRecord);
 
-    const PRUnichar *nameStr = aName.BeginReading();
-    const PRUnichar *nameStrEnd = aName.EndReading();
-    while (nameStr < nameStrEnd) {
-        PRUnichar ch = *nameStr++;
-        *strData++ = NS_SWAP16(ch);
-    }
-    *strData = 0; // add null termination
+    mozilla::NativeEndian::copyAndSwapToBigEndian(strData,
+                                                  aName.BeginReading(),
+                                                  aName.Length());
+    strData[aName.Length()] = 0; // add null termination
     
     // adjust name table header to point to the new name table
     SFNTHeader *sfntHeader = reinterpret_cast<SFNTHeader*>(newFontData);
 
     // table directory entries begin immediately following SFNT header
     TableDirEntry *dirEntry = 
         reinterpret_cast<TableDirEntry*>(newFontData + sizeof(SFNTHeader));
 
--- a/gfx/thebes/gfxFontUtils.h
+++ b/gfx/thebes/gfxFontUtils.h
@@ -16,18 +16,18 @@
 
 #include "nsITimer.h"
 #include "nsCOMPtr.h"
 #include "nsIRunnable.h"
 #include "nsThreadUtils.h"
 #include "nsComponentManagerUtils.h"
 #include "nsTArray.h"
 #include "nsAutoPtr.h"
-#include "nsIStreamBufferAccess.h"
 #include "mozilla/Likely.h"
+#include "mozilla/Endian.h"
 
 #include "zlib.h"
 #include <algorithm>
 
 /* Bug 341128 - w32api defines min/max which causes problems with <bitset> */
 #ifdef __MINGW32__
 #undef min
 #undef max
@@ -334,68 +334,135 @@ namespace mozilla {
 
 // Byte-swapping types and name table structure definitions moved from
 // gfxFontUtils.cpp to .h file so that gfxFont.cpp can also refer to them
 #pragma pack(1)
 
 struct AutoSwap_PRUint16 {
 #ifdef __SUNPRO_CC
     AutoSwap_PRUint16& operator = (const uint16_t aValue)
-      { this->value = NS_SWAP16(aValue); return *this; }
+    {
+        this->value = mozilla::NativeEndian::swapToBigEndian(aValue);
+        return *this;
+    }
 #else
-    AutoSwap_PRUint16(uint16_t aValue) { value = NS_SWAP16(aValue); }
+    AutoSwap_PRUint16(uint16_t aValue)
+    {
+        value = mozilla::NativeEndian::swapToBigEndian(aValue);
+    }
 #endif
-    operator uint16_t() const { return NS_SWAP16(value); }
-    operator uint32_t() const { return NS_SWAP16(value); }
-    operator uint64_t() const { return NS_SWAP16(value); }
+    operator uint16_t() const
+    {
+        return mozilla::NativeEndian::swapFromBigEndian(value);
+    }
+
+    operator uint32_t() const
+    {
+        return mozilla::NativeEndian::swapFromBigEndian(value);
+    }
+
+    operator uint64_t() const
+    {
+        return mozilla::NativeEndian::swapFromBigEndian(value);
+    }
+
+private:
     uint16_t value;
 };
 
 struct AutoSwap_PRInt16 {
 #ifdef __SUNPRO_CC
     AutoSwap_PRInt16& operator = (const int16_t aValue)
-      { this->value = NS_SWAP16(aValue); return *this; }
+    {
+        this->value = mozilla::NativeEndian::swapToBigEndian(aValue);
+        return *this;
+    }
 #else
-    AutoSwap_PRInt16(int16_t aValue) { value = NS_SWAP16(aValue); }
+    AutoSwap_PRInt16(int16_t aValue)
+    {
+        value = mozilla::NativeEndian::swapToBigEndian(aValue);
+    }
 #endif
-    operator int16_t() const { return NS_SWAP16(value); }
-    operator uint32_t() const { return NS_SWAP16(value); }
+    operator int16_t() const
+    {
+        return mozilla::NativeEndian::swapFromBigEndian(value);
+    }
+
+    operator uint32_t() const
+    {
+        return mozilla::NativeEndian::swapFromBigEndian(value);
+    }
+
+private:
     int16_t  value;
 };
 
 struct AutoSwap_PRUint32 {
 #ifdef __SUNPRO_CC
     AutoSwap_PRUint32& operator = (const uint32_t aValue)
-      { this->value = NS_SWAP32(aValue); return *this; }
+    {
+        this->value = mozilla::NativeEndian::swapToBigEndian(aValue);
+        return *this;
+    }
 #else
-    AutoSwap_PRUint32(uint32_t aValue) { value = NS_SWAP32(aValue); }
+    AutoSwap_PRUint32(uint32_t aValue)
+    {
+        value = mozilla::NativeEndian::swapToBigEndian(aValue);
+    }
 #endif
-    operator uint32_t() const { return NS_SWAP32(value); }
+    operator uint32_t() const
+    {
+        return mozilla::NativeEndian::swapFromBigEndian(value);
+    }
+
+private:
     uint32_t  value;
 };
 
 struct AutoSwap_PRInt32 {
 #ifdef __SUNPRO_CC
     AutoSwap_PRInt32& operator = (const int32_t aValue)
-      { this->value = NS_SWAP32(aValue); return *this; }
+    {
+        this->value = mozilla::NativeEndian::swapToBigEndian(aValue);
+        return *this;
+    }
 #else
-    AutoSwap_PRInt32(int32_t aValue) { value = NS_SWAP32(aValue); }
+    AutoSwap_PRInt32(int32_t aValue)
+    {
+        value = mozilla::NativeEndian::swapToBigEndian(aValue);
+    }
 #endif
-    operator int32_t() const { return NS_SWAP32(value); }
+    operator int32_t() const
+    {
+        return mozilla::NativeEndian::swapFromBigEndian(value);
+    }
+
+private:
     int32_t  value;
 };
 
 struct AutoSwap_PRUint64 {
 #ifdef __SUNPRO_CC
     AutoSwap_PRUint64& operator = (const uint64_t aValue)
-      { this->value = NS_SWAP64(aValue); return *this; }
+    {
+        this->value = mozilla::NativeEndian::swapToBigEndian(aValue);
+        return *this;
+    }
 #else
-    AutoSwap_PRUint64(uint64_t aValue) { value = NS_SWAP64(aValue); }
+    AutoSwap_PRUint64(uint64_t aValue)
+    {
+        value = mozilla::NativeEndian::swapToBigEndian(aValue);
+    }
 #endif
-    operator uint64_t() const { return NS_SWAP64(value); }
+    operator uint64_t() const
+    {
+        return mozilla::NativeEndian::swapFromBigEndian(value);
+    }
+
+private:
     uint64_t  value;
 };
 
 struct AutoSwap_PRUint24 {
     operator uint32_t() const { return value[0] << 16 | value[1] << 8 | value[2]; }
 private:
     AutoSwap_PRUint24() { }
     uint8_t  value[3];
--- a/gfx/thebes/gfxFontconfigUtils.h
+++ b/gfx/thebes/gfxFontconfigUtils.h
@@ -7,16 +7,17 @@
 #define GFX_FONTCONFIG_UTILS_H
 
 #include "gfxPlatform.h"
 
 #include "nsAutoRef.h"
 #include "nsTArray.h"
 #include "nsTHashtable.h"
 #include "nsISupportsImpl.h"
+#include "prbit.h" // for PR_ROTATE_LEFT32
 
 #include <fontconfig/fontconfig.h>
 
 
 template <>
 class nsAutoRefTraits<FcPattern> : public nsPointerRefTraits<FcPattern>
 {
 public:
--- a/gfx/thebes/gfxGDIFontList.cpp
+++ b/gfx/thebes/gfxGDIFontList.cpp
@@ -276,20 +276,22 @@ GDIFontEntry::GetFontTable(uint32_t aTab
     if (!IsTrueType()) {
         return NS_ERROR_FAILURE;
     }
 
     AutoDC dc;
     AutoSelectFont font(dc.GetDC(), &mLogFont);
     if (font.IsValid()) {
         uint32_t tableSize =
-            ::GetFontData(dc.GetDC(), NS_SWAP32(aTableTag), 0, NULL, 0);
+            ::GetFontData(dc.GetDC(),
+                          NativeEndian::swapToBigEndian(aTableTag), 0, NULL, 0);
         if (tableSize != GDI_ERROR) {
             if (aBuffer.SetLength(tableSize)) {
-                ::GetFontData(dc.GetDC(), NS_SWAP32(aTableTag), 0,
+                ::GetFontData(dc.GetDC(),
+                              NativeEndian::swapToBigEndian(aTableTag), 0,
                               aBuffer.Elements(), tableSize);
                 return NS_OK;
             }
             return NS_ERROR_OUT_OF_MEMORY;
         }
     }
     return NS_ERROR_FAILURE;
 }
--- a/gfx/thebes/gfxSVGGlyphs.cpp
+++ b/gfx/thebes/gfxSVGGlyphs.cpp
@@ -83,25 +83,25 @@ gfxSVGGlyphs::gfxSVGGlyphs(FallibleTArra
     mGlyphDocs.Init();
     mGlyphIdMap.Init();
     mCmapData = aCmapTable;
 }
 
 void
 gfxSVGGlyphs::UnmangleHeaders()
 {
-    mHeader->mIndexLength = NS_SWAP16(mHeader->mIndexLength);
+    mHeader->mIndexLength = mozilla::NativeEndian::swapFromBigEndian(mHeader->mIndexLength);
 
     mIndex = reinterpret_cast<IndexEntry*>(mSVGData.Elements() + sizeof(Header));
 
     for (uint16_t i = 0; i < mHeader->mIndexLength; i++) {
-        mIndex[i].mStartGlyph = NS_SWAP16(mIndex[i].mStartGlyph);
-        mIndex[i].mEndGlyph = NS_SWAP16(mIndex[i].mEndGlyph);
-        mIndex[i].mDocOffset = NS_SWAP32(mIndex[i].mDocOffset);
-        mIndex[i].mDocLength = NS_SWAP32(mIndex[i].mDocLength);
+        mIndex[i].mStartGlyph = mozilla::NativeEndian::swapFromBigEndian(mIndex[i].mStartGlyph);
+        mIndex[i].mEndGlyph = mozilla::NativeEndian::swapFromBigEndian(mIndex[i].mEndGlyph);
+        mIndex[i].mDocOffset = mozilla::NativeEndian::swapFromBigEndian(mIndex[i].mDocOffset);
+        mIndex[i].mDocLength = mozilla::NativeEndian::swapFromBigEndian(mIndex[i].mDocLength);
     }
 }
 
 /*
  * Comparison operator for finding a range containing a given glyph ID. Simply
  *   checks whether |key| is less (greater) than every element of |range|, in
  *   which case return |key| < |range| (|key| > |range|). Otherwise |key| is in
  *   |range|, in which case return equality.
--- a/xpcom/io/nsBinaryStream.cpp
+++ b/xpcom/io/nsBinaryStream.cpp
@@ -15,23 +15,23 @@
  * network order.
  *
  * @See nsIBinaryInputStream
  * @See nsIBinaryOutputStream
  */
 #include <string.h>
 #include "nsBinaryStream.h"
 #include "nsCRT.h"
-#include "nsIStreamBufferAccess.h"
 #include "prlong.h"
 #include "nsString.h"
 #include "nsISerializable.h"
 #include "nsIClassInfo.h"
 #include "nsComponentManagerUtils.h"
 #include "nsIURI.h" // for NS_IURI_IID
+#include "mozilla/Endian.h"
 
 #include "jsapi.h"
 #include "jsfriendapi.h"
 
 NS_IMPL_ISUPPORTS3(nsBinaryOutputStream, nsIObjectOutputStream, nsIBinaryOutputStream, nsIOutputStream)
 
 NS_IMETHODIMP
 nsBinaryOutputStream::Flush() 
@@ -109,34 +109,34 @@ NS_IMETHODIMP
 nsBinaryOutputStream::Write8(uint8_t aByte)
 {
     return WriteFully((const char*)&aByte, sizeof aByte);
 }
 
 NS_IMETHODIMP
 nsBinaryOutputStream::Write16(uint16_t a16)
 {
-    a16 = NS_SWAP16(a16);
+    a16 = mozilla::NativeEndian::swapToBigEndian(a16);
     return WriteFully((const char*)&a16, sizeof a16);
 }
 
 NS_IMETHODIMP
 nsBinaryOutputStream::Write32(uint32_t a32)
 {
-    a32 = NS_SWAP32(a32);
+    a32 = mozilla::NativeEndian::swapToBigEndian(a32);
     return WriteFully((const char*)&a32, sizeof a32);
 }
 
 NS_IMETHODIMP
 nsBinaryOutputStream::Write64(uint64_t a64)
 {
     nsresult rv;
     uint32_t bytesWritten;
 
-    a64 = NS_SWAP64(a64);
+    a64 = mozilla::NativeEndian::swapToBigEndian(a64);
     rv = Write(reinterpret_cast<char*>(&a64), sizeof a64, &bytesWritten);
     if (NS_FAILED(rv)) return rv;
     if (bytesWritten != sizeof a64)
         return NS_ERROR_FAILURE;
     return rv;
 }
 
 NS_IMETHODIMP
@@ -189,18 +189,17 @@ nsBinaryOutputStream::WriteWStringZ(cons
     if (length <= 64) {
         copy = temp;
     } else {
         copy = reinterpret_cast<PRUnichar*>(moz_malloc(byteCount));
         if (!copy)
             return NS_ERROR_OUT_OF_MEMORY;
     }
     NS_ASSERTION((uintptr_t(aString) & 0x1) == 0, "aString not properly aligned");
-    for (uint32_t i = 0; i < length; i++)
-        copy[i] = NS_SWAP16(aString[i]);
+    mozilla::NativeEndian::copyAndSwapToBigEndian(copy, aString, length);
     rv = WriteBytes(reinterpret_cast<const char*>(copy), byteCount);
     if (copy != temp)
         moz_free(copy);
 #endif
 
     return rv;
 }
 
@@ -467,45 +466,45 @@ nsBinaryInputStream::Read16(uint16_t* a1
 {
     nsresult rv;
     uint32_t bytesRead;
 
     rv = Read(reinterpret_cast<char*>(a16), sizeof *a16, &bytesRead);
     if (NS_FAILED(rv)) return rv;
     if (bytesRead != sizeof *a16)
         return NS_ERROR_FAILURE;
-    *a16 = NS_SWAP16(*a16);
+    *a16 = mozilla::NativeEndian::swapFromBigEndian(*a16);
     return rv;
 }
 
 NS_IMETHODIMP
 nsBinaryInputStream::Read32(uint32_t* a32)
 {
     nsresult rv;
     uint32_t bytesRead;
 
     rv = Read(reinterpret_cast<char*>(a32), sizeof *a32, &bytesRead);
     if (NS_FAILED(rv)) return rv;
     if (bytesRead != sizeof *a32)
         return NS_ERROR_FAILURE;
-    *a32 = NS_SWAP32(*a32);
+    *a32 = mozilla::NativeEndian::swapFromBigEndian(*a32);
     return rv;
 }
 
 NS_IMETHODIMP
 nsBinaryInputStream::Read64(uint64_t* a64)
 {
     nsresult rv;
     uint32_t bytesRead;
 
     rv = Read(reinterpret_cast<char*>(a64), sizeof *a64, &bytesRead);
     if (NS_FAILED(rv)) return rv;
     if (bytesRead != sizeof *a64)
         return NS_ERROR_FAILURE;
-    *a64 = NS_SWAP64(*a64);
+    *a64 = mozilla::NativeEndian::swapFromBigEndian(*a64);
     return rv;
 }
 
 NS_IMETHODIMP
 nsBinaryInputStream::ReadFloat(float* aFloat)
 {
     NS_ASSERTION(sizeof(float) == sizeof (uint32_t),
                  "False assumption about sizeof(float)");
@@ -601,19 +600,17 @@ WriteSegmentToString(nsIInputStream* aSt
     *aWriteCount = aCount;
 
     // if the last Write had an odd-number of bytes read, then 
     if (closure->mHasCarryoverByte) {
         // re-create the two-byte sequence we want to work with
         char bytes[2] = { closure->mCarryoverByte, *aFromSegment };
         *cursor = *(PRUnichar*)bytes;
         // Now the little endianness dance
-#ifdef IS_LITTLE_ENDIAN
-        *cursor = (PRUnichar) NS_SWAP16(*cursor);
-#endif
+        mozilla::NativeEndian::swapToBigEndianInPlace(cursor, 1);
         ++cursor;
         
         // now skip past the first byte of the buffer.. code from here
         // can assume normal operations, but should not assume aCount
         // is relative to the ORIGINAL buffer
         ++aFromSegment;
         --aCount;
 
@@ -623,22 +620,20 @@ WriteSegmentToString(nsIInputStream* aSt
     // this array is possibly unaligned... be careful how we access it!
     const PRUnichar *unicodeSegment =
         reinterpret_cast<const PRUnichar*>(aFromSegment);
 
     // calculate number of full characters in segment (aCount could be odd!)
     uint32_t segmentLength = aCount / sizeof(PRUnichar);
 
     // copy all data into our aligned buffer.  byte swap if necessary.
+    // cursor may be unaligned, so we cannot use copyAndSwapToBigEndian directly
     memcpy(cursor, unicodeSegment, segmentLength * sizeof(PRUnichar));
     PRUnichar *end = cursor + segmentLength;
-#ifdef IS_LITTLE_ENDIAN
-    for (; cursor < end; ++cursor)
-        *cursor = (PRUnichar) NS_SWAP16(*cursor);
-#endif
+    mozilla::NativeEndian::swapToBigEndianInPlace(cursor, segmentLength);
     closure->mWriteCursor = end;
 
     // remember this is the modifed aCount and aFromSegment,
     // so that will take into account the fact that we might have
     // skipped the first byte in the buffer
     if (aCount % sizeof(PRUnichar) != 0) {
         // we must have had a carryover byte, that we'll need the next
         // time around
--- a/xpcom/io/nsIStreamBufferAccess.idl
+++ b/xpcom/io/nsIStreamBufferAccess.idl
@@ -73,82 +73,17 @@ interface nsIStreamBufferAccess : nsISup
     /**
      * The underlying, unbuffered input or output stream.
      */
     readonly attribute nsISupports unbufferedStream;
 };
 
 %{C++
 
-// Swap macros, used to convert to/from canonical (big-endian) format
-#if defined IS_LITTLE_ENDIAN
-
-# define NS_SWAP16(x) ((((x) & 0xff) << 8) | (((x) >> 8) & 0xff))
-# define NS_SWAP32(x) ((NS_SWAP16((x) & 0xffff) << 16) | (NS_SWAP16((x) >> 16)))
-
-// We want to avoid casting to 32-bit types if possible, since that violates
-// aliasing rules (a standard compiler may assume that pointers of two types
-// do not address overlapping storage).
-//
-// XXX What if we have a compiler that follows aliasing rules strictly but
-// doesn't have a 64-bit int type?
-//
-// XXXbe shouldn't NSPR's LL_INIT work for non-constant arguments in all cases?
-
-# if defined HAVE_LONG_LONG
-#  if PR_BYTES_PER_LONG == 8
-#   define ULL_(x)     x ## UL
-#  elif defined WIN32 && defined _MSC_VER
-#   define ULL_(x)     x ## ui64
-#  else
-#   define ULL_(x)     x ## ULL
-#  endif
-
-#  define NS_SWAP64(x) ((((x)    & ULL_(0xff00000000000000))    >> 56) |      \
-                        (((x)    & ULL_(0x00ff000000000000))    >> 40) |      \
-                        (((x)    & ULL_(0x0000ff0000000000))    >> 24) |      \
-                        (((x)    & ULL_(0x000000ff00000000))    >> 8)  |      \
-                        (((x)    & ULL_(0x00000000ff000000))    << 8)  |      \
-                        (((x)    & ULL_(0x0000000000ff0000))    << 24) |      \
-                        (((x)    & ULL_(0x000000000000ff00))    << 40) |      \
-                        (((x) /* & ULL_(0x00000000000000ff) */) << 56))
-# else
-#  define NS_SWAP64(x) LL_INIT((((x).lo /* & 0xff000000ul */) >> 24) |        \
-                               (((x).lo    & 0x00ff0000ul)    >> 8)  |        \
-                               (((x).lo    & 0x0000ff00ul)    << 8)  |        \
-                               (((x).lo /* & 0x000000fful */) << 24),         \
-                               (((x).hi /* & 0xff000000ul */) >> 24) |        \
-                               (((x).hi    & 0x00ff0000ul)    >> 8)  |        \
-                               (((x).hi    & 0x0000ff00ul)    << 8)  |        \
-                               (((x).hi /* & 0x000000fful */) << 24))
-# endif
-
-#elif defined IS_BIG_ENDIAN
-
-# define NS_SWAP16(x) (x)
-# define NS_SWAP32(x) (x)
-# define NS_SWAP64(x) (x)
-
-#else
-
-# error "Unknown byte order"
-
-#endif
-
 /**
  * These macros get and put a buffer given either an sba parameter that may
  * point to an object implementing nsIStreamBufferAccess, nsIObjectInputStream,
  * or nsIObjectOutputStream.
  */
 #define NS_GET_BUFFER(sba,n,a)  ((sba)->GetBuffer(n, a))
 #define NS_PUT_BUFFER(sba,p,n)  ((sba)->PutBuffer(p, n))
 
-#define NS_GET8(p)              (*(uint8_t*)(p))
-#define NS_GET16(p)             NS_SWAP16(*(uint16_t*)(p))
-#define NS_GET32(p)             NS_SWAP32(*(uint32_t*)(p))
-#define NS_GET64(p)             NS_SWAP64(*(uint64_t*)(p))
-
-#define NS_PUT8(p,x)            (*(uint8_t*)(p) = (x))
-#define NS_PUT16(p,x)           (*(uint16_t*)(p) = NS_SWAP16(x))
-#define NS_PUT32(p,x)           (*(uint32_t*)(p) = NS_SWAP32(x))
-#define NS_PUT64(p,x)           (*(uint64_t*)(p) = NS_SWAP64(x))
-
 %}