fsuipc/lib.rs
1
2//
3// FSUIPC library
4// Copyright (c) 2015 Alvaro Polo
5//
6// This Source Code Form is subject to the terms of the Mozilla Public
7// License, v. 2.0. If a copy of the MPL was not distributed with this
8// file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10extern crate byteorder;
11extern crate kernel32;
12extern crate user32;
13extern crate winapi;
14
15mod ipc;
16mod raw;
17
18#[cfg(all(windows))]
19pub mod local;
20
21#[cfg(all(windows))]
22pub mod user;
23
24use std::io;
25use std::mem::size_of;
26
27/// A handle to FSUIPC
28/// This type represents a handle to FSUIPC. It cannot be used directly to read of write from or
29/// to FSUIPC offsets. A `Session` object is created from the handle instead.
30pub trait Handle<'a> {
31 /// The type of the session objects created by this handle.
32 type Sess: Session;
33
34 /// Create a new session from this handle
35 fn session(&'a mut self) -> Self::Sess;
36}
37
38/// A session of read & write operations from/to FSUIPC
39/// Objects of this trait represents a session comprised of a sequence of read and write
40/// operations. The operations are requested by using `read()` and `write()` methods.
41/// They are not executed immediately but after calling `process()` method, which consumes
42/// the session.
43pub trait Session {
44 fn read_bytes(&mut self, offset: u16, dest: *mut u8, len: usize) -> io::Result<usize>;
45 fn write_bytes(&mut self, offset: u16, src: *const u8, len: usize) -> io::Result<usize>;
46 fn process(self) -> io::Result<usize>;
47
48 fn read<'a, T>(&'a mut self, offset: u16, result: &'a mut T) -> io::Result<usize> {
49 self.read_bytes(offset, result as *mut T as *mut u8, size_of::<T>())
50 }
51
52 fn write<T>(&mut self, offset: u16, value: &T) -> io::Result<usize> {
53 self.write_bytes(offset, value as *const T as *const u8, size_of::<T>())
54 }
55}