      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
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144

    // Copyright 2015-2017 Parity Technologies (UK) Ltd.
    // This file is part of Parity.

    // Parity is free software: you can redistribute it and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.

    // Parity is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details.

    // You should have received a copy of the GNU General Public License
    // along with Parity.  If not, see <http://www.gnu.org/licenses/>.

    use util::{U256, Uint};

    pub trait Memory {
    	/// Retrieve current size of the memory
    	fn size(&self) -> usize;
    	/// Resize (shrink or expand) the memory to specified size (fills 0)
    	fn resize(&mut self, new_size: usize);
    	/// Resize the memory only if its smaller
    	fn expand(&mut self, new_size: usize);
    	/// Write single byte to memory
    	fn write_byte(&mut self, offset: U256, value: U256);
    	/// Write a word to memory. Does not resize memory!
    	fn write(&mut self, offset: U256, value: U256);
    	/// Read a word from memory
    	fn read(&self, offset: U256) -> U256;
    	/// Write slice of bytes to memory. Does not resize memory!
    	fn write_slice(&mut self, offset: U256, &[u8]);
    	/// Retrieve part of the memory between offset and offset + size
    	fn read_slice(&self, offset: U256, size: U256) -> &[u8];
    	/// Retrieve writeable part of memory
    	fn writeable_slice(&mut self, offset: U256, size: U256) -> &mut[u8];
    	fn dump(&self);
    }

    /// Checks whether offset and size is valid memory range
    fn is_valid_range(off: usize, size: usize)  -> bool {
    	// When size is zero we haven't actually expanded the memory
    	let overflow = off.overflowing_add(size).1;
    	size > 0 && !overflow
    }

    impl Memory for Vec<u8> {
    	fn dump(&self) {
    		println!("MemoryDump:");
    		for i in self.iter() {
    			println!("{:02x} ", i);
    		}
    		println!("");
    	}

    	fn size(&self) -> usize {
    		self.len()
    	}

    	fn read_slice(&self, init_off_u: U256, init_size_u: U256) -> &[u8] {
    		let off = init_off_u.low_u64() as usize;
    		let size = init_size_u.low_u64() as usize;
    		if !is_valid_range(off, size) {
    			&self[0..0]
    		} else {
    			&self[off..off+size]
    		}
    	}

    	fn read(&self, offset: U256) -> U256 {
    		let off = offset.low_u64() as usize;
    		U256::from(&self[off..off+32])
    	}

    	fn writeable_slice(&mut self, offset: U256, size: U256) -> &mut [u8] {
    		let off = offset.low_u64() as usize;
    		let s = size.low_u64() as usize;
    		if !is_valid_range(off, s) {
    			&mut self[0..0]
    		} else {
    			&mut self[off..off+s]
    		}
    	}

    	fn write_slice(&mut self, offset: U256, slice: &[u8]) {
    		let off = offset.low_u64() as usize;

    		// TODO [todr] Optimize?
    		for pos in off..off+slice.len() {
    			self[pos] = slice[pos - off];
    		}
    	}

    	fn write(&mut self, offset: U256, value: U256) {
    		let off = offset.low_u64() as usize;
    		value.to_big_endian(&mut self[off..off+32]);
    	}

    	fn write_byte(&mut self, offset: U256, value: U256) {
    		let off = offset.low_u64() as usize;
    		let val = value.low_u64() as u64;
    		self[off] = val as u8;
    	}

    	fn resize(&mut self, new_size: usize) {
    		self.resize(new_size, 0);
    	}

    	fn expand(&mut self, size: usize) {
    		if size > self.len() {
    			Memory::resize(self, size)
    		}
    	}
    }

    #[test]
    fn test_memory_read_and_write() {
    	// given
    	let mem: &mut Memory = &mut vec![];
    	mem.resize(0x80 + 32);

    	// when
    	mem.write(U256::from(0x80), U256::from(0xabcdef));

    	// then
    	assert_eq!(mem.read(U256::from(0x80)), U256::from(0xabcdef));
    }

    #[test]
    fn test_memory_read_and_write_byte() {
    	// given
    	let mem: &mut Memory = &mut vec![];
    	mem.resize(32);

    	// when
    	mem.write_byte(U256::from(0x1d), U256::from(0xab));
    	mem.write_byte(U256::from(0x1e), U256::from(0xcd));
    	mem.write_byte(U256::from(0x1f), U256::from(0xef));

    	// then
    	assert_eq!(mem.read(U256::from(0x00)), U256::from(0xabcdef));
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)