Bug 1238420 - Update byteorder to v0.4.2. r=kinetik
authorRalph Giles <giles@mozilla.com>
Mon, 11 Jan 2016 11:01:00 +1300
changeset 280337 b0192610a0e7aee0286e4b45c40fd4b3c357f0cd
parent 280336 075d8bd8b9a50a712e2ab0400eea762238164a6f
child 280338 1ca581e5420d1cffe8a8e68cb302647fa131553c
push id70402
push usermgregan@mozilla.com
push dateSun, 17 Jan 2016 22:49:41 +0000
treeherdermozilla-inbound@33b4a7fd0c53 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskinetik
bugs1238420
milestone46.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 1238420 - Update byteorder to v0.4.2. r=kinetik Rebase patches against the new upstream release. No changes on the client side are necessary.
media/libstagefright/binding/byteorder-mod.patch
media/libstagefright/binding/byteorder/mod.rs
media/libstagefright/binding/byteorder/new.rs
media/libstagefright/binding/update-rust.sh
--- a/media/libstagefright/binding/byteorder-mod.patch
+++ b/media/libstagefright/binding/byteorder-mod.patch
@@ -1,34 +1,36 @@
 diff --git a/media/libstagefright/binding/byteorder/mod.rs b/media/libstagefright/binding/byteorder/mod.rs
-index 59ba692..9d2d1d5 100644
+index 7eea1e3..8a108cf 100644
 --- a/media/libstagefright/binding/byteorder/mod.rs
 +++ b/media/libstagefright/binding/byteorder/mod.rs
-@@ -36,16 +36,16 @@ assert_eq!(wtr, vec![5, 2, 0, 3]);
+@@ -36,7 +36,6 @@ assert_eq!(wtr, vec![5, 2, 0, 3]);
  ```
  */
  
 -#![crate_name = "byteorder"]
  #![doc(html_root_url = "http://burntsushi.net/rustdoc/byteorder")]
  
  #![deny(missing_docs)]
+@@ -45,10 +44,11 @@ use std::mem::transmute;
+ use std::ptr::copy_nonoverlapping;
  
- use std::mem::transmute;
- 
+ #[cfg(not(feature = "no-std"))]
 -pub use new::{ReadBytesExt, WriteBytesExt, Error, Result};
 +pub use byteorder::new::{ReadBytesExt, WriteBytesExt, Error, Result};
  
+ #[cfg(not(feature = "no-std"))]
 -mod new;
 +// Re-export new so gecko can build us as a mod intead of a crate.
 +pub mod new;
  
  #[inline]
  fn extend_sign(val: u64, nbytes: usize) -> i64 {
 diff --git a/media/libstagefright/binding/byteorder/new.rs b/media/libstagefright/binding/byteorder/new.rs
-index bbef0cd..a2e5393 100644
+index 54ee6a7..4efcbc3 100644
 --- a/media/libstagefright/binding/byteorder/new.rs
 +++ b/media/libstagefright/binding/byteorder/new.rs
 @@ -3,7 +3,7 @@ use std::fmt;
  use std::io;
  use std::result;
  
 -use ByteOrder;
 +use byteorder::ByteOrder;
--- a/media/libstagefright/binding/byteorder/mod.rs
+++ b/media/libstagefright/binding/byteorder/mod.rs
@@ -36,28 +36,58 @@ assert_eq!(wtr, vec![5, 2, 0, 3]);
 ```
 */
 
 #![doc(html_root_url = "http://burntsushi.net/rustdoc/byteorder")]
 
 #![deny(missing_docs)]
 
 use std::mem::transmute;
+use std::ptr::copy_nonoverlapping;
 
+#[cfg(not(feature = "no-std"))]
 pub use byteorder::new::{ReadBytesExt, WriteBytesExt, Error, Result};
 
+#[cfg(not(feature = "no-std"))]
 // Re-export new so gecko can build us as a mod intead of a crate.
 pub mod new;
 
 #[inline]
 fn extend_sign(val: u64, nbytes: usize) -> i64 {
-    let shift  = (8 - nbytes) * 8;
+    let shift = (8 - nbytes) * 8;
     (val << shift) as i64 >> shift
 }
 
+#[inline]
+fn unextend_sign(val: i64, nbytes: usize) -> u64 {
+    let shift = (8 - nbytes) * 8;
+    (val << shift) as u64 >> shift
+}
+
+#[inline]
+fn pack_size(n: u64) -> usize {
+    if n < 1 << 8 {
+        1
+    } else if n < 1 << 16 {
+        2
+    } else if n < 1 << 24 {
+        3
+    } else if n < 1 << 32 {
+        4
+    } else if n < 1 << 40 {
+        5
+    } else if n < 1 << 48 {
+        6
+    } else if n < 1 << 56 {
+        7
+    } else {
+        8
+    }
+}
+
 /// ByteOrder describes types that can serialize integers as bytes.
 ///
 /// Note that `Self` does not appear anywhere in this trait's definition!
 /// Therefore, in order to use it, you'll need to use syntax like
 /// `T::read_u16(&[0, 1])` where `T` implements `ByteOrder`.
 ///
 /// This crate provides two types that implement `ByteOrder`: `BigEndian`
 /// and `LittleEndian`.
@@ -115,16 +145,22 @@ pub trait ByteOrder {
     /// Panics when `buf.len() < 4`.
     fn write_u32(buf: &mut [u8], n: u32);
 
     /// Writes an unsigned 64 bit integer `n` to `buf`.
     ///
     /// Panics when `buf.len() < 8`.
     fn write_u64(buf: &mut [u8], n: u64);
 
+    /// Writes an unsigned integer `n` to `buf` using only `nbytes`.
+    ///
+    /// If `n` is not representable in `nbytes`, or if `nbytes` is `> 8`, then
+    /// this method panics.
+    fn write_uint(buf: &mut [u8], n: u64, nbytes: usize);
+
     /// Reads a signed 16 bit integer from `buf`.
     ///
     /// Panics when `buf.len() < 2`.
     #[inline]
     fn read_i16(buf: &[u8]) -> i16 {
         Self::read_u16(buf) as i16
     }
 
@@ -188,16 +224,25 @@ pub trait ByteOrder {
     /// Writes a signed 64 bit integer `n` to `buf`.
     ///
     /// Panics when `buf.len() < 8`.
     #[inline]
     fn write_i64(buf: &mut [u8], n: i64) {
         Self::write_u64(buf, n as u64)
     }
 
+    /// Writes a signed integer `n` to `buf` using only `nbytes`.
+    ///
+    /// If `n` is not representable in `nbytes`, or if `nbytes` is `> 8`, then
+    /// this method panics.
+    #[inline]
+    fn write_int(buf: &mut [u8], n: i64, nbytes: usize) {
+        Self::write_uint(buf, unextend_sign(n, nbytes), nbytes)
+    }
+
     /// Writes a IEEE754 single-precision (4 bytes) floating point number.
     ///
     /// Panics when `buf.len() < 4`.
     #[inline]
     fn write_f32(buf: &mut [u8], n: f32) {
         Self::write_u32(buf, unsafe { transmute(n) })
     }
 
@@ -233,51 +278,26 @@ pub type NativeEndian = LittleEndian;
 ///
 /// Note that this type has no value constructor. It is used purely at the
 /// type level.
 #[cfg(target_endian = "big")]
 pub type NativeEndian = BigEndian;
 
 macro_rules! read_num_bytes {
     ($ty:ty, $size:expr, $src:expr, $which:ident) => ({
-        assert!($src.len() >= $size); // critical for memory safety!
+        assert!($size <= $src.len());
         unsafe {
             (*($src.as_ptr() as *const $ty)).$which()
         }
     });
-    ($ty:ty, $size:expr, le $bytes:expr, $src:expr, $which:ident) => ({
-        use std::ptr::copy_nonoverlapping;
-
-        assert!($bytes > 0 && $bytes < 9 && $bytes <= $src.len());
-        let mut out = [0u8; $size];
-        let ptr_out = out.as_mut_ptr();
-        unsafe {
-            copy_nonoverlapping($src.as_ptr(), ptr_out, $bytes);
-            (*(ptr_out as *const $ty)).$which()
-        }
-    });
-    ($ty:ty, $size:expr, be $bytes:expr, $src:expr, $which:ident) => ({
-        use std::ptr::copy_nonoverlapping;
-
-        assert!($bytes > 0 && $bytes < 9 && $bytes <= $src.len());
-        let mut out = [0u8; $size];
-        let ptr_out = out.as_mut_ptr();
-        unsafe {
-            copy_nonoverlapping($src.as_ptr(),
-                                ptr_out.offset((8 - $bytes) as isize), $bytes);
-            (*(ptr_out as *const $ty)).$which()
-        }
-    });
 }
 
 macro_rules! write_num_bytes {
     ($ty:ty, $size:expr, $n:expr, $dst:expr, $which:ident) => ({
-        use std::ptr::copy_nonoverlapping;
-
-        assert!($dst.len() >= $size); // critical for memory safety!
+        assert!($size <= $dst.len());
         unsafe {
             // N.B. https://github.com/rust-lang/rust/issues/22776
             let bytes = transmute::<_, [u8; $size]>($n.$which());
             copy_nonoverlapping((&bytes).as_ptr(), $dst.as_mut_ptr(), $size);
         }
     });
 }
 
@@ -294,33 +314,53 @@ impl ByteOrder for BigEndian {
 
     #[inline]
     fn read_u64(buf: &[u8]) -> u64 {
         read_num_bytes!(u64, 8, buf, to_be)
     }
 
     #[inline]
     fn read_uint(buf: &[u8], nbytes: usize) -> u64 {
-        read_num_bytes!(u64, 8, be nbytes, buf, to_be)
+        assert!(1 <= nbytes && nbytes <= 8 && nbytes <= buf.len());
+        let mut out = [0u8; 8];
+        let ptr_out = out.as_mut_ptr();
+        unsafe {
+            copy_nonoverlapping(
+                buf.as_ptr(), ptr_out.offset((8 - nbytes) as isize), nbytes);
+            (*(ptr_out as *const u64)).to_be()
+        }
     }
 
     #[inline]
     fn write_u16(buf: &mut [u8], n: u16) {
         write_num_bytes!(u16, 2, n, buf, to_be);
     }
 
     #[inline]
     fn write_u32(buf: &mut [u8], n: u32) {
         write_num_bytes!(u32, 4, n, buf, to_be);
     }
 
     #[inline]
     fn write_u64(buf: &mut [u8], n: u64) {
         write_num_bytes!(u64, 8, n, buf, to_be);
     }
+
+    #[inline]
+    fn write_uint(buf: &mut [u8], n: u64, nbytes: usize) {
+        assert!(pack_size(n) <= nbytes && nbytes <= 8);
+        assert!(nbytes <= buf.len());
+        unsafe {
+            let bytes: [u8; 8] = transmute(n.to_be());
+            copy_nonoverlapping(
+                bytes.as_ptr().offset((8 - nbytes) as isize),
+                buf.as_mut_ptr(),
+                nbytes);
+        }
+    }
 }
 
 impl ByteOrder for LittleEndian {
     #[inline]
     fn read_u16(buf: &[u8]) -> u16 {
         read_num_bytes!(u16, 2, buf, to_le)
     }
 
@@ -331,33 +371,49 @@ impl ByteOrder for LittleEndian {
 
     #[inline]
     fn read_u64(buf: &[u8]) -> u64 {
         read_num_bytes!(u64, 8, buf, to_le)
     }
 
     #[inline]
     fn read_uint(buf: &[u8], nbytes: usize) -> u64 {
-        read_num_bytes!(u64, 8, le nbytes, buf, to_le)
+        assert!(1 <= nbytes && nbytes <= 8 && nbytes <= buf.len());
+        let mut out = [0u8; 8];
+        let ptr_out = out.as_mut_ptr();
+        unsafe {
+            copy_nonoverlapping(buf.as_ptr(), ptr_out, nbytes);
+            (*(ptr_out as *const u64)).to_le()
+        }
     }
 
     #[inline]
     fn write_u16(buf: &mut [u8], n: u16) {
         write_num_bytes!(u16, 2, n, buf, to_le);
     }
 
     #[inline]
     fn write_u32(buf: &mut [u8], n: u32) {
         write_num_bytes!(u32, 4, n, buf, to_le);
     }
 
     #[inline]
     fn write_u64(buf: &mut [u8], n: u64) {
         write_num_bytes!(u64, 8, n, buf, to_le);
     }
+
+    #[inline]
+    fn write_uint(buf: &mut [u8], n: u64, nbytes: usize) {
+        assert!(pack_size(n as u64) <= nbytes && nbytes <= 8);
+        assert!(nbytes <= buf.len());
+        unsafe {
+            let bytes: [u8; 8] = transmute(n.to_le());
+            copy_nonoverlapping(bytes.as_ptr(), buf.as_mut_ptr(), nbytes);
+        }
+    }
 }
 
 #[cfg(test)]
 mod test {
     extern crate quickcheck;
     extern crate rand;
 
     use test::rand::thread_rng;
@@ -381,39 +437,39 @@ mod test {
                 use {BigEndian, ByteOrder, NativeEndian, LittleEndian};
                 use super::qc_sized;
 
                 #[test]
                 fn big_endian() {
                     let max = ($max - 1) >> (8 * (8 - $bytes));
                     fn prop(n: $ty_int) -> bool {
                         let mut buf = [0; 8];
-                        BigEndian::$write(&mut buf, n);
-                        n == BigEndian::$read(&mut buf[8 - $bytes..], $bytes)
+                        BigEndian::$write(&mut buf, n, $bytes);
+                        n == BigEndian::$read(&mut buf[..$bytes], $bytes)
                     }
                     qc_sized(prop as fn($ty_int) -> bool, max);
                 }
 
                 #[test]
                 fn little_endian() {
                     let max = ($max - 1) >> (8 * (8 - $bytes));
                     fn prop(n: $ty_int) -> bool {
                         let mut buf = [0; 8];
-                        LittleEndian::$write(&mut buf, n);
+                        LittleEndian::$write(&mut buf, n, $bytes);
                         n == LittleEndian::$read(&mut buf[..$bytes], $bytes)
                     }
                     qc_sized(prop as fn($ty_int) -> bool, max);
                 }
 
                 #[test]
                 fn native_endian() {
                     let max = ($max - 1) >> (8 * (8 - $bytes));
                     fn prop(n: $ty_int) -> bool {
                         let mut buf = [0; 8];
-                        NativeEndian::$write(&mut buf, n);
+                        NativeEndian::$write(&mut buf, n, $bytes);
                         n == NativeEndian::$read(&mut buf[..$bytes], $bytes)
                     }
                     qc_sized(prop as fn($ty_int) -> bool, max);
                 }
             }
         );
         ($name:ident, $ty_int:ident, $max:expr,
          $read:ident, $write:ident) => (
@@ -462,33 +518,33 @@ mod test {
     qc_byte_order!(prop_i16, i16, ::std::i16::MAX as u64, read_i16, write_i16);
     qc_byte_order!(prop_u32, u32, ::std::u32::MAX as u64, read_u32, write_u32);
     qc_byte_order!(prop_i32, i32, ::std::i32::MAX as u64, read_i32, write_i32);
     qc_byte_order!(prop_u64, u64, ::std::u64::MAX as u64, read_u64, write_u64);
     qc_byte_order!(prop_i64, i64, ::std::i64::MAX as u64, read_i64, write_i64);
     qc_byte_order!(prop_f32, f32, ::std::u64::MAX as u64, read_f32, write_f32);
     qc_byte_order!(prop_f64, f64, ::std::i64::MAX as u64, read_f64, write_f64);
 
-    qc_byte_order!(prop_uint_1, u64, super::U64_MAX, 1, read_uint, write_u64);
-    qc_byte_order!(prop_uint_2, u64, super::U64_MAX, 2, read_uint, write_u64);
-    qc_byte_order!(prop_uint_3, u64, super::U64_MAX, 3, read_uint, write_u64);
-    qc_byte_order!(prop_uint_4, u64, super::U64_MAX, 4, read_uint, write_u64);
-    qc_byte_order!(prop_uint_5, u64, super::U64_MAX, 5, read_uint, write_u64);
-    qc_byte_order!(prop_uint_6, u64, super::U64_MAX, 6, read_uint, write_u64);
-    qc_byte_order!(prop_uint_7, u64, super::U64_MAX, 7, read_uint, write_u64);
-    qc_byte_order!(prop_uint_8, u64, super::U64_MAX, 8, read_uint, write_u64);
+    qc_byte_order!(prop_uint_1, u64, super::U64_MAX, 1, read_uint, write_uint);
+    qc_byte_order!(prop_uint_2, u64, super::U64_MAX, 2, read_uint, write_uint);
+    qc_byte_order!(prop_uint_3, u64, super::U64_MAX, 3, read_uint, write_uint);
+    qc_byte_order!(prop_uint_4, u64, super::U64_MAX, 4, read_uint, write_uint);
+    qc_byte_order!(prop_uint_5, u64, super::U64_MAX, 5, read_uint, write_uint);
+    qc_byte_order!(prop_uint_6, u64, super::U64_MAX, 6, read_uint, write_uint);
+    qc_byte_order!(prop_uint_7, u64, super::U64_MAX, 7, read_uint, write_uint);
+    qc_byte_order!(prop_uint_8, u64, super::U64_MAX, 8, read_uint, write_uint);
 
-    qc_byte_order!(prop_int_1, i64, super::I64_MAX, 1, read_int, write_i64);
-    qc_byte_order!(prop_int_2, i64, super::I64_MAX, 2, read_int, write_i64);
-    qc_byte_order!(prop_int_3, i64, super::I64_MAX, 3, read_int, write_i64);
-    qc_byte_order!(prop_int_4, i64, super::I64_MAX, 4, read_int, write_i64);
-    qc_byte_order!(prop_int_5, i64, super::I64_MAX, 5, read_int, write_i64);
-    qc_byte_order!(prop_int_6, i64, super::I64_MAX, 6, read_int, write_i64);
-    qc_byte_order!(prop_int_7, i64, super::I64_MAX, 7, read_int, write_i64);
-    qc_byte_order!(prop_int_8, i64, super::I64_MAX, 8, read_int, write_i64);
+    qc_byte_order!(prop_int_1, i64, super::I64_MAX, 1, read_int, write_int);
+    qc_byte_order!(prop_int_2, i64, super::I64_MAX, 2, read_int, write_int);
+    qc_byte_order!(prop_int_3, i64, super::I64_MAX, 3, read_int, write_int);
+    qc_byte_order!(prop_int_4, i64, super::I64_MAX, 4, read_int, write_int);
+    qc_byte_order!(prop_int_5, i64, super::I64_MAX, 5, read_int, write_int);
+    qc_byte_order!(prop_int_6, i64, super::I64_MAX, 6, read_int, write_int);
+    qc_byte_order!(prop_int_7, i64, super::I64_MAX, 7, read_int, write_int);
+    qc_byte_order!(prop_int_8, i64, super::I64_MAX, 8, read_int, write_int);
 
     macro_rules! qc_bytes_ext {
         ($name:ident, $ty_int:ident, $max:expr,
          $bytes:expr, $read:ident, $write:ident) => (
             mod $name {
                 use std::io::Cursor;
                 use {
                     ReadBytesExt, WriteBytesExt,
--- a/media/libstagefright/binding/byteorder/new.rs
+++ b/media/libstagefright/binding/byteorder/new.rs
@@ -290,16 +290,46 @@ pub trait WriteBytesExt: io::Write {
     /// Writes a signed 64 bit integer to the underlying writer.
     #[inline]
     fn write_i64<T: ByteOrder>(&mut self, n: i64) -> Result<()> {
         let mut buf = [0; 8];
         T::write_i64(&mut buf, n);
         write_all(self, &buf)
     }
 
+    /// Writes an unsigned n-bytes integer to the underlying writer.
+    ///
+    /// If the given integer is not representable in the given number of bytes,
+    /// this method panics. If `nbytes > 8`, this method panics.
+    #[inline]
+    fn write_uint<T: ByteOrder>(
+        &mut self,
+        n: u64,
+        nbytes: usize,
+    ) -> Result<()> {
+        let mut buf = [0; 8];
+        T::write_uint(&mut buf, n, nbytes);
+        write_all(self, &buf[0..nbytes])
+    }
+
+    /// Writes a signed n-bytes integer to the underlying writer.
+    ///
+    /// If the given integer is not representable in the given number of bytes,
+    /// this method panics. If `nbytes > 8`, this method panics.
+    #[inline]
+    fn write_int<T: ByteOrder>(
+        &mut self,
+        n: i64,
+        nbytes: usize,
+    ) -> Result<()> {
+        let mut buf = [0; 8];
+        T::write_int(&mut buf, n, nbytes);
+        write_all(self, &buf[0..nbytes])
+    }
+
     /// Writes a IEEE754 single-precision (4 bytes) floating point number to
     /// the underlying writer.
     #[inline]
     fn write_f32<T: ByteOrder>(&mut self, n: f32) -> Result<()> {
         let mut buf = [0; 4];
         T::write_f32(&mut buf, n);
         write_all(self, &buf)
     }
--- a/media/libstagefright/binding/update-rust.sh
+++ b/media/libstagefright/binding/update-rust.sh
@@ -18,17 +18,17 @@ popd
 cp _upstream/mp4parse/src/lib.rs MP4Metadata.rs
 cp _upstream/mp4parse/src/capi.rs .
 cp _upstream/mp4parse/include/mp4parse.h include/
 
 # TODO: download deps from crates.io.
 
 git clone https://github.com/BurntSushi/byteorder _upstream/byteorder
 pushd _upstream/byteorder
-git checkout 0.3.13
+git checkout 0.4.2
 popd
 cp _upstream/byteorder/src/lib.rs byteorder/mod.rs
 cp _upstream/byteorder/src/new.rs byteorder/new.rs
 
 echo "Applying patches..."
 patch -p4 < byteorder-mod.patch
 patch -p4 < mp4parse-mod.patch