Struct Parcel

Source
pub struct Parcel { /* private fields */ }
Expand description

Represents a binder serializable parcel

Implementations§

Source§

impl Parcel

Source

pub fn empty() -> Self

Create a new empty parcel.

Examples found in repository?
examples/simple.rs (line 9)
4fn main() {
5    let mut service_manager = &mut ServiceManager::new();
6
7    let mut package_manager = service_manager.get_service("myservice", "com.example.IMyService");
8
9    let mut parcel = Parcel::empty();
10    parcel.write_str16("Hello World");
11    let mut res = package_manager.call(1, &mut parcel);
12    println!("response: {:?}", res.read_str16());
13
14    let mut parcel = Parcel::empty();
15    parcel.write_str16("/data/local/tmp/testfile");
16    let mut res = package_manager.call(2, &mut parcel);
17    let fd = res.read_file_descriptor();
18    unsafe {
19        nix::libc::write(fd, "Hello world".as_ptr() as *const std::ffi::c_void, 11);
20    }
21}
More examples
Hide additional examples
examples/myservice.rs (line 30)
23    fn process_request(&self, code: u32, data: &mut Parcel) -> Parcel {
24        println!("Got command: {} -> {:?}", code, MyServiceCommands::from_u32(code));
25        match MyServiceCommands::from_u32(code).unwrap() {
26            MyServiceCommands::GetFile => {
27                let filename = &std::ffi::CString::new(data.read_str16()).unwrap();
28                let fd = unsafe { open(filename.as_ptr(), O_RDWR | O_CREAT) };
29                println!("filename: {:?}, fd: {}", filename, fd);
30                let mut parcel = Parcel::empty();
31                parcel.write_u32(0);
32                parcel.write_file_descriptor(fd, false);
33                parcel
34            },
35            MyServiceCommands::Echo => {
36                let mut parcel = Parcel::empty();
37                parcel.write_u32(0); //status
38                parcel.write_str16(&data.read_str16());
39                parcel
40            }
41        }
42    }
Source

pub fn from_slice(data: &[u8]) -> Self

Create a new empty parcel, with a reserved size

Source

pub fn from_data_and_offsets( data: *mut u8, data_size: usize, offsets: *mut usize, offsets_size: usize, ) -> Self

Source

pub fn reset(&mut self)

Source

pub fn position(&self) -> u64

Source

pub fn set_position(&mut self, pos: u64)

Source

pub fn append_parcel(&mut self, other: &mut Parcel)

Append the contents of another parcel to this parcel

Source

pub fn as_ptr(&self) -> *const u8

Retrieve the data of the parcel as a pointer

Source

pub fn as_mut_ptr(&mut self) -> *mut u8

Retrieve the data of the parcel as a mutable pointer

Source

pub fn to_slice(&self) -> &[u8]

Retrieve the data of the parce as a slice

Source

pub fn len(&self) -> usize

Retrieve the size of the parcel’s data

Source

pub fn offsets_len(&self) -> usize

Retrieve the number of object offsets

Source

pub fn offsets(&mut self) -> &mut Vec<usize>

Retrieve the object offsets

Source

pub fn has_unread_data(&self) -> bool

Check if the parcel has unread data

Source

pub fn write_i32(&mut self, data: i32)

Write an i32 to the parcel

Source

pub fn write_u32(&mut self, data: u32)

Write an u32 to the parcel

Examples found in repository?
examples/myservice.rs (line 31)
23    fn process_request(&self, code: u32, data: &mut Parcel) -> Parcel {
24        println!("Got command: {} -> {:?}", code, MyServiceCommands::from_u32(code));
25        match MyServiceCommands::from_u32(code).unwrap() {
26            MyServiceCommands::GetFile => {
27                let filename = &std::ffi::CString::new(data.read_str16()).unwrap();
28                let fd = unsafe { open(filename.as_ptr(), O_RDWR | O_CREAT) };
29                println!("filename: {:?}, fd: {}", filename, fd);
30                let mut parcel = Parcel::empty();
31                parcel.write_u32(0);
32                parcel.write_file_descriptor(fd, false);
33                parcel
34            },
35            MyServiceCommands::Echo => {
36                let mut parcel = Parcel::empty();
37                parcel.write_u32(0); //status
38                parcel.write_str16(&data.read_str16());
39                parcel
40            }
41        }
42    }
Source

pub fn write_u16(&mut self, data: u16)

Write an u16 to the parcel

Source

pub fn write_bool(&mut self, data: bool)

Write a bool to the parcel

Source

pub fn read_i32(&mut self) -> i32

Read an i32 from the parcel

Source

pub fn write(&mut self, data: &[u8])

Write a slice of data to the parcel

Source

pub fn write_transaction_data(&mut self, data: &BinderTransactionData)

Write a BinderTransactionData struct into the parcel

Source

pub fn read_u32(&mut self) -> u32

Read an u32 from the parcel

Source

pub fn read_u64(&mut self) -> u64

Read an u64 from the parcel

Source

pub fn read_usize(&mut self) -> usize

Read an usize from the parcel

Source

pub fn read_pointer(&mut self) -> *const c_void

Read a void pointer from the parcel

Source

pub fn read(&mut self, size: usize) -> Vec<u8>

Read a slice of size bytes from the parcel

Source

pub fn read_without_alignment(&mut self, size: usize) -> Vec<u8>

Read a slice of size bytes from the parcel

Source

pub fn read_transaction_data(&mut self) -> BinderTransactionData

Read a BinderTransactionData from the parcel

Source

pub fn read_object<T>(&mut self) -> T

Read an object of type T from the parcel

Source

pub fn write_object<T>(&mut self, object: T)

Source

pub fn write_str16(&mut self, string: &str)

Write a string to the parcel

Examples found in repository?
examples/simple.rs (line 10)
4fn main() {
5    let mut service_manager = &mut ServiceManager::new();
6
7    let mut package_manager = service_manager.get_service("myservice", "com.example.IMyService");
8
9    let mut parcel = Parcel::empty();
10    parcel.write_str16("Hello World");
11    let mut res = package_manager.call(1, &mut parcel);
12    println!("response: {:?}", res.read_str16());
13
14    let mut parcel = Parcel::empty();
15    parcel.write_str16("/data/local/tmp/testfile");
16    let mut res = package_manager.call(2, &mut parcel);
17    let fd = res.read_file_descriptor();
18    unsafe {
19        nix::libc::write(fd, "Hello world".as_ptr() as *const std::ffi::c_void, 11);
20    }
21}
More examples
Hide additional examples
examples/myservice.rs (line 38)
23    fn process_request(&self, code: u32, data: &mut Parcel) -> Parcel {
24        println!("Got command: {} -> {:?}", code, MyServiceCommands::from_u32(code));
25        match MyServiceCommands::from_u32(code).unwrap() {
26            MyServiceCommands::GetFile => {
27                let filename = &std::ffi::CString::new(data.read_str16()).unwrap();
28                let fd = unsafe { open(filename.as_ptr(), O_RDWR | O_CREAT) };
29                println!("filename: {:?}, fd: {}", filename, fd);
30                let mut parcel = Parcel::empty();
31                parcel.write_u32(0);
32                parcel.write_file_descriptor(fd, false);
33                parcel
34            },
35            MyServiceCommands::Echo => {
36                let mut parcel = Parcel::empty();
37                parcel.write_u32(0); //status
38                parcel.write_str16(&data.read_str16());
39                parcel
40            }
41        }
42    }
Source

pub fn write_binder(&mut self, object: *const c_void)

Write a Binder object into the parcel

Source

pub fn write_file_descriptor(&mut self, fd: RawFd, take_ownership: bool)

Write a file descriptor into the parcel

Examples found in repository?
examples/myservice.rs (line 32)
23    fn process_request(&self, code: u32, data: &mut Parcel) -> Parcel {
24        println!("Got command: {} -> {:?}", code, MyServiceCommands::from_u32(code));
25        match MyServiceCommands::from_u32(code).unwrap() {
26            MyServiceCommands::GetFile => {
27                let filename = &std::ffi::CString::new(data.read_str16()).unwrap();
28                let fd = unsafe { open(filename.as_ptr(), O_RDWR | O_CREAT) };
29                println!("filename: {:?}, fd: {}", filename, fd);
30                let mut parcel = Parcel::empty();
31                parcel.write_u32(0);
32                parcel.write_file_descriptor(fd, false);
33                parcel
34            },
35            MyServiceCommands::Echo => {
36                let mut parcel = Parcel::empty();
37                parcel.write_u32(0); //status
38                parcel.write_str16(&data.read_str16());
39                parcel
40            }
41        }
42    }
Source

pub fn read_file_descriptor(&mut self) -> RawFd

REad a file descriptor from the parcel

Examples found in repository?
examples/simple.rs (line 17)
4fn main() {
5    let mut service_manager = &mut ServiceManager::new();
6
7    let mut package_manager = service_manager.get_service("myservice", "com.example.IMyService");
8
9    let mut parcel = Parcel::empty();
10    parcel.write_str16("Hello World");
11    let mut res = package_manager.call(1, &mut parcel);
12    println!("response: {:?}", res.read_str16());
13
14    let mut parcel = Parcel::empty();
15    parcel.write_str16("/data/local/tmp/testfile");
16    let mut res = package_manager.call(2, &mut parcel);
17    let fd = res.read_file_descriptor();
18    unsafe {
19        nix::libc::write(fd, "Hello world".as_ptr() as *const std::ffi::c_void, 11);
20    }
21}
Source

pub fn read_str16(&mut self) -> String

Read a string from the parcel

Examples found in repository?
examples/simple.rs (line 12)
4fn main() {
5    let mut service_manager = &mut ServiceManager::new();
6
7    let mut package_manager = service_manager.get_service("myservice", "com.example.IMyService");
8
9    let mut parcel = Parcel::empty();
10    parcel.write_str16("Hello World");
11    let mut res = package_manager.call(1, &mut parcel);
12    println!("response: {:?}", res.read_str16());
13
14    let mut parcel = Parcel::empty();
15    parcel.write_str16("/data/local/tmp/testfile");
16    let mut res = package_manager.call(2, &mut parcel);
17    let fd = res.read_file_descriptor();
18    unsafe {
19        nix::libc::write(fd, "Hello world".as_ptr() as *const std::ffi::c_void, 11);
20    }
21}
More examples
Hide additional examples
examples/myservice.rs (line 27)
23    fn process_request(&self, code: u32, data: &mut Parcel) -> Parcel {
24        println!("Got command: {} -> {:?}", code, MyServiceCommands::from_u32(code));
25        match MyServiceCommands::from_u32(code).unwrap() {
26            MyServiceCommands::GetFile => {
27                let filename = &std::ffi::CString::new(data.read_str16()).unwrap();
28                let fd = unsafe { open(filename.as_ptr(), O_RDWR | O_CREAT) };
29                println!("filename: {:?}, fd: {}", filename, fd);
30                let mut parcel = Parcel::empty();
31                parcel.write_u32(0);
32                parcel.write_file_descriptor(fd, false);
33                parcel
34            },
35            MyServiceCommands::Echo => {
36                let mut parcel = Parcel::empty();
37                parcel.write_u32(0); //status
38                parcel.write_str16(&data.read_str16());
39                parcel
40            }
41        }
42    }
Source

pub fn read_str(&mut self) -> String

Read a string from the parcel

Source

pub fn read_interface_token(&mut self) -> String

Read an interface token from the parcel

Source

pub fn write_interface_token(&mut self, name: &str)

Write an interface token to the parcel

Trait Implementations§

Source§

impl Debug for Parcel

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Parcel

§

impl RefUnwindSafe for Parcel

§

impl Send for Parcel

§

impl Sync for Parcel

§

impl Unpin for Parcel

§

impl UnwindSafe for Parcel

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.