1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/////////////
// HELPERS //
/////////////

use interface::*;
use errors::*;

use std::cmp;

/// Copy all of the `src` memory block to `dst`.
/// Returns `Ok(writtenbytes)` to signify `writtenbytes` bytes have been copied,
/// or `Err(error)` if some error happened during copying.
pub fn copy<T: MemoryBlock>(src: &T, dst: &mut T) -> Result<Addr, Error> {
    let src_sz = src.get_size();
    let dst_sz = dst.get_size();
    if src_sz > dst_sz {
        bail!(ErrorKind::TooBig(src_sz, dst_sz));
    }
    for i in 0..src_sz {
        let status = match src.get(i) {
            Ok(byte) => dst.set(i, byte),
            Err(e) => Err(e),
        };
        status.chain_err(|| "in copy helper")?;
    }
    Ok(src_sz)
}


/// Copy the contents of `src` at `from` to `to` to `dst`, starting at `pos`.
/// Returns `Ok(writtenbytes)` to signify `writtenbytes` bytes have been copied,
/// or `Err(error)` if some error happened during copying.
pub fn copy_at<T: MemoryBlock>(src: &T, dst: &mut T, from: Addr, to: Addr, pos: Addr) -> Result<Addr, Error> {
    let src_sz = src.get_size();
    let dst_sz = dst.get_size();

    let lowest = cmp::min(to, from);
    let numbytes = cmp::max(to, from) - lowest;

    // Handle invalid cases.
    if (lowest + numbytes) > src_sz {
        bail!(ErrorKind::TooBig(lowest + numbytes, src_sz));
    };
    if (pos + numbytes) > dst_sz {
        bail!(ErrorKind::TooBig(pos + numbytes, dst_sz));
    };

    // Actual copying
    for i in from..(to+1) {
        let status = match src.get(i) {
            Ok(byte) => {
                let dstpos = pos + (i - lowest);
                dst.set(dstpos, byte)
            },
            Err(e) => Err(e),
        };
        status.chain_err(|| "in copy_at helper")?;
    }
    Ok(numbytes)
}