# HG changeset patch
# User t_mrc-ct@users.sourceforge.jp
# Date 1403420730 -32400
# Branch GECKO3111_2014090813_RELBRANCH
# Node ID 74ea7b0714306a5ba3d57a3ec08da3f59b66fe08
# Parent  20d25fd4c387d70d3731d3a1644a353e29cb3ca4
PropertyListUtils.jsm big-endian support

diff --git a/toolkit/modules/PropertyListUtils.jsm b/toolkit/modules/PropertyListUtils.jsm
--- a/toolkit/modules/PropertyListUtils.jsm
+++ b/toolkit/modules/PropertyListUtils.jsm
@@ -277,48 +277,34 @@
   },
 
   _readObjectsOffsets: function BPLR__readObjectsOffsets() {
     this._offsetTable = this._readUnsignedInts(this._offsetTableOffset,
                                                this._offsetTableIntegerSize,
                                                this._numberOfObjects);
   },
 
-  // TODO: This should be removed once DataView is implemented (Bug 575688).
-  _swapForBigEndian:
-  function BPLR__swapForBigEndian(aByteOffset, aIntSize, aNumberOfInts) {
-    let bytesCount = aIntSize * aNumberOfInts;
-    let bytes = new Uint8Array(this._buffer, aByteOffset, bytesCount);
-    let swapped = new Uint8Array(bytesCount);
-    for (let i = 0; i < aNumberOfInts; i++) {
-      for (let j = 0; j < aIntSize; j++) {
-        swapped[(i * aIntSize) + j] = bytes[(i * aIntSize) + (aIntSize - 1 - j)];
-      }
-    }
-    return swapped;
-  },
-
   _readSignedInt64: function BPLR__readSignedInt64(aByteOffset) {
-    let swapped = this._swapForBigEndian(aByteOffset, 8, 1);
-    let lo = new Uint32Array(swapped.buffer, 0, 1)[0];
-    let hi = new Int32Array(swapped.buffer, 4, 1)[0];
+    let dview = new DataView(this._buffer, aByteOffset, 8);
+    let hi = dview.getInt32(0, false);
+    let lo = dview.getUint32(4, false);
     let int64 = ctypes.Int64.join(hi, lo);
     if (ctypes.Int64.compare(int64, this._JS_MAX_INT_SIGNED) == 1 ||
         ctypes.Int64.compare(int64, this._JS_MIN_INT) == -1)
       return PropertyListUtils.wrapInt64(int64.toString());
 
     return parseInt(int64.toString(), 10);
   },
 
   _readReal: function BPLR__readReal(aByteOffset, aRealSize) {
-    let swapped = this._swapForBigEndian(aByteOffset, aRealSize, 1);
+    let dview = new DataView(this._buffer, aByteOffset, aRealSize);
     if (aRealSize == 4)
-      return new Float32Array(swapped.buffer, 0, 1)[0];
+      return dview.getFloat32(0, false);
     if (aRealSize == 8)
-      return new Float64Array(swapped.buffer, 0, 1)[0];
+      return dview.getFloat64(0, false);
 
     throw new Error("Unsupported real size: " + aRealSize);
   },
 
   OBJECT_TYPE_BITS: {
     SIMPLE:                  parseInt("0000", 2),
     INTEGER:                 parseInt("0001", 2),
     REAL:                    parseInt("0010", 2),
@@ -412,31 +398,38 @@
    * @throws if aBigIntAllowed is false and one of the integers in the array
    * cannot be represented by a primitive js number.
    */
   _readUnsignedInts:
   function BPLR__readUnsignedInts(aByteOffset, aIntSize, aLength, aBigIntAllowed) {
     if (aIntSize == 1)
       return new Uint8Array(this._buffer, aByteOffset, aLength);
 
-    // There are two reasons for the complexity you see here:
-    // (1) 64-bit integers - For which we use ctypes. When possible, the
-    //     number is converted back to js's default float-64 type.
-    // (2) The DataView object for ArrayBuffer, which takes care of swaping
-    //     bytes, is not yet implemented (bug 575688).
-    let swapped = this._swapForBigEndian(aByteOffset, aIntSize, aLength);
-    if (aIntSize == 2)
-      return new Uint16Array(swapped.buffer);
-    if (aIntSize == 4)
-      return new Uint32Array(swapped.buffer);
+    let dview = new DataView(this._buffer, aByteOffset, aIntSize * aLength);
+    if (aIntSize == 2) {
+      let intsArray = new Uint16Array(aLength);
+      for(let i = 0; i < aLength; i ++) {
+        intsArray[i] = dview.getUint16(2 * i, false);
+      }
+      return intsArray;
+    }
+    if (aIntSize == 4) {
+      let intsArray = new Uint32Array(aLength);
+      for(let i = 0; i < aLength; i ++) {
+        intsArray[i] = dview.getUint32(4 * i, false);
+      }
+      return intsArray;
+    }
     if (aIntSize == 8) {
+      // 64-bit integers - For which we use ctypes. When possible, the
+      // number is converted back to js's default float-64 type.
       let intsArray = [];
-      let lo_hi_view = new Uint32Array(swapped.buffer);
-      for (let i = 0; i < lo_hi_view.length; i += 2) {
-        let [lo, hi] = [lo_hi_view[i], lo_hi_view[i+1]];
+      for (let i = 0; i < aLength; i ++) {
+        let hi = dview.getUint32(8 * i, false);
+        let lo = dview.getUint32(8 * i + 4, false);
         let uint64 = ctypes.UInt64.join(hi, lo);
         if (ctypes.UInt64.compare(uint64, this._JS_MAX_INT_UNSIGNED) == 1) {
           if (aBigIntAllowed === true)
             intsArray.push(PropertyListUtils.wrapInt64(uint64.toString()));
           else
             throw new Error("Integer too big to be read as float 64");
         }
         else {
