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
use std::fmt;
use wasmer_runtime::{Array, Memory, WasmPtr};
pub trait AsmScriptString {
fn get_as_string(self, memory: &Memory) -> Result<String, Error>;
fn size(offset: u32, memory: &Memory) -> Result<u32, Error> {
if offset < 4 {
return Err(Error::Mem("Wrong offset: les than 2"));
}
if let Some(cell) = memory.view::<u32>().get(offset as usize / 4 - 1) {
Ok(cell.get())
} else {
Err(Error::Mem("Wrong offset: can't read size"))
}
}
}
impl AsmScriptString for WasmPtr<u16, Array> {
fn get_as_string(self, memory: &Memory) -> Result<String, Error> {
let offset = self.offset();
let size = Self::size(offset, memory)?;
if let Some(buf) = self.deref(memory, 0, size / 2) {
let input: Vec<u16> = buf.iter().map(|b| b.get()).collect();
let mut output: Vec<u8> = vec![0; size as usize * 3];
let len = ucs2::decode(&input, &mut output)?;
Ok(String::from_utf8_lossy(output[..len].into()).into_owned())
} else {
Err(Error::Mem("Wrong offset: can't read buf"))
}
}
}
#[derive(Debug)]
pub enum Error {
Ucs2(ucs2::Error),
Mem(&'static str),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Ucs2(err) => write!(f, "{:?}", err),
Error::Mem(err) => write!(f, "{}", err),
}
}
}
impl std::error::Error for Error {}
impl From<ucs2::Error> for Error {
fn from(err: ucs2::Error) -> Self {
Self::Ucs2(err)
}
}