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
//! Standard library for parity wasm programs

#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "strict", deny(warnings))]
#![feature(lang_items)]
#![feature(link_args)]
#![feature(alloc)]
#![feature(core_intrinsics)]
#![feature(panic_info_message)]

#![warn(missing_docs)]

#[cfg(feature = "std")]
extern crate core;

#[cfg(not(feature = "std"))]
extern crate pwasm_alloc;

#[cfg(not(feature = "std"))]
extern crate pwasm_libc;

#[allow(unused)]
#[macro_use]
extern crate alloc;
pub use alloc::{vec, format};

extern crate byteorder;

#[macro_use]
extern crate fixed_hash;

extern crate uint;

extern crate tiny_keccak;

use byteorder::{LittleEndian, ByteOrder};

pub use alloc::boxed::Box;
pub use alloc::string::String;
pub use alloc::str;
pub use alloc::vec::Vec;

pub mod types;

// Safe wrapper around debug logging
pub mod logger;

// Crypto functions
mod crypto;

mod panic;

#[no_mangle]
#[cfg(not(feature = "std"))]
pub use panic::panic_fmt;

#[no_mangle]
#[cfg(not(feature = "std"))]
pub use panic::oom;

pub use crypto::keccak;

/// Read u32 using native endianness
pub fn read_u32(slc: &[u8]) -> u32 {
	LittleEndian::read_u32(slc)
}

/// Write u32 using native endianness
pub fn write_u32(dst: &mut [u8], val: u32) {
	LittleEndian::write_u32(dst, val)
}

/// Write ptr using native endianness
pub fn write_ptr(dst: &mut [u8], ptr: *mut u8) {
	// todo: consider: add assert that arch is 32bit
	write_u32(dst, ptr as usize as u32);
}

/// Read u64 using native endianness
pub fn read_u64(slc: &[u8]) -> u64 {
	LittleEndian::read_u64(slc)
}

/// Write u64 using native endianness
pub fn write_u64(dst: &mut [u8], val: u64) {
	LittleEndian::write_u64(dst, val)
}