Skip to main content

luaur_vm/functions/
buffer_copy.rs

1use crate::functions::lua_l_checkbuffer::lua_l_checkbuffer;
2use crate::functions::lua_l_checkinteger::lua_l_checkinteger;
3use crate::functions::lua_l_optinteger::lua_l_optinteger;
4use crate::macros::isoutofbounds::isoutofbounds;
5use crate::macros::lua_l_error::luaL_error;
6use crate::type_aliases::lua_state::lua_State;
7
8pub fn buffer_copy(L: *mut lua_State) -> core::ffi::c_int {
9    let mut tlen: usize = 0;
10    let tbuf = lua_l_checkbuffer(L, 1, &mut tlen);
11    let toffset = lua_l_checkinteger(L, 2);
12
13    let mut slen: usize = 0;
14    let sbuf = lua_l_checkbuffer(L, 3, &mut slen);
15    let soffset = lua_l_optinteger(L, 4, 0);
16
17    let size = lua_l_optinteger(L, 5, (slen as core::ffi::c_int) - soffset);
18
19    if size < 0 {
20        luaL_error!(L, "buffer access out of bounds");
21    }
22
23    if isoutofbounds(soffset, slen, size as usize) {
24        luaL_error!(L, "buffer access out of bounds");
25    }
26
27    if isoutofbounds(toffset, tlen, size as usize) {
28        luaL_error!(L, "buffer access out of bounds");
29    }
30
31    unsafe {
32        core::ptr::copy(
33            (sbuf as *const core::ffi::c_char).offset(soffset as isize),
34            (tbuf as *mut core::ffi::c_char).offset(toffset as isize),
35            size as usize,
36        );
37    }
38
39    0
40}