From 43e659e3cc9ce53dd78cac3ce3c180ae0557610d Mon Sep 17 00:00:00 2001 From: Ralph Giles Date: Mon, 11 Jan 2016 11:01:00 +1300 Subject: [PATCH] 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. --- .../binding/byteorder-mod.patch | 12 +- media/libstagefright/binding/byteorder/mod.rs | 156 ++++++++++++------ media/libstagefright/binding/byteorder/new.rs | 30 ++++ media/libstagefright/binding/update-rust.sh | 2 +- 4 files changed, 144 insertions(+), 56 deletions(-) diff --git a/media/libstagefright/binding/byteorder-mod.patch b/media/libstagefright/binding/byteorder-mod.patch index cce9208bbb7c..b00a2e3e0ff8 100644 --- a/media/libstagefright/binding/byteorder-mod.patch +++ b/media/libstagefright/binding/byteorder-mod.patch @@ -1,8 +1,8 @@ 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]); ``` */ @@ -10,12 +10,14 @@ index 59ba692..9d2d1d5 100644 #![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; @@ -23,7 +25,7 @@ index 59ba692..9d2d1d5 100644 #[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; diff --git a/media/libstagefright/binding/byteorder/mod.rs b/media/libstagefright/binding/byteorder/mod.rs index 9d2d1d532b13..664717ef2788 100644 --- a/media/libstagefright/binding/byteorder/mod.rs +++ b/media/libstagefright/binding/byteorder/mod.rs @@ -41,18 +41,48 @@ assert_eq!(wtr, vec![5, 2, 0, 3]); #![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! @@ -120,6 +150,12 @@ pub trait ByteOrder { /// 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`. @@ -193,6 +229,15 @@ pub trait ByteOrder { 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`. @@ -238,41 +283,16 @@ 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()); @@ -299,7 +319,14 @@ impl ByteOrder for BigEndian { #[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] @@ -316,6 +343,19 @@ impl ByteOrder for BigEndian { 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 { @@ -336,7 +376,13 @@ impl ByteOrder for LittleEndian { #[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] @@ -353,6 +399,16 @@ impl ByteOrder for LittleEndian { 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)] @@ -386,8 +442,8 @@ mod test { 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); } @@ -397,7 +453,7 @@ mod test { 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); @@ -408,7 +464,7 @@ mod test { 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); @@ -467,23 +523,23 @@ mod test { 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, diff --git a/media/libstagefright/binding/byteorder/new.rs b/media/libstagefright/binding/byteorder/new.rs index a2e5393565b7..4efcbc36e146 100644 --- a/media/libstagefright/binding/byteorder/new.rs +++ b/media/libstagefright/binding/byteorder/new.rs @@ -295,6 +295,36 @@ pub trait WriteBytesExt: io::Write { 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( + &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( + &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] diff --git a/media/libstagefright/binding/update-rust.sh b/media/libstagefright/binding/update-rust.sh index ddcb83a04252..da2f6cdc25e4 100755 --- a/media/libstagefright/binding/update-rust.sh +++ b/media/libstagefright/binding/update-rust.sh @@ -23,7 +23,7 @@ cp _upstream/mp4parse/include/mp4parse.h include/ 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