Assume I am passing a variable bytes (memory) to a function. This variable corresponds to A concatenated with B where A is of size 32 and B is 96 bytes, how can I return A (bytes32) and B (bytes).
function readData(bytes memory data) public returns (bytes32, bytes memory){
bytes32 A;
bytes memory B;
assembly{
A:= mload(add(data,0x20))
calldatacopy(B,0x45,0xA0) // not sure about this
}
return (A,B);
}
Edit
knowing the length of B make things easy but the things is that B size can change, therefore hardcoding a sequence of mload
(one per word of data) won't do. this could be solved with an assembly loop but I wonder if there is a more efficient way to do it.
public
functions, you should not assume call data to contain function arguments, because call data is basically the same asmsg.data
, i.e. it refers to parameters of the whole smart contract call rather than particular function call.public
function (in contrast toexternal
functions) could be called from another function of the same contract with arguments that are completely different from what is inmsg.data
.