Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
Serialization Namespace Reference

Classes

struct  ArrayOutputStream
 
struct  FatalSerializationException
 
struct  InputStream
 
struct  MemoryInputStream
 
struct  OutputStream
 

Functions

FORCEINLINE void serializeBytes (OutputStream &stream, const U8 *bytes, Uptr numBytes)
 
FORCEINLINE void serializeBytes (InputStream &stream, U8 *bytes, Uptr numBytes)
 
template<typename Stream , typename Value >
FORCEINLINE void serializeNativeValue (Stream &stream, Value &value)
 
template<typename Stream >
void serialize (Stream &stream, U8 &i)
 
template<typename Stream >
void serialize (Stream &stream, U32 &i)
 
template<typename Stream >
void serialize (Stream &stream, U64 &i)
 
template<typename Stream >
void serialize (Stream &stream, I8 &i)
 
template<typename Stream >
void serialize (Stream &stream, I32 &i)
 
template<typename Stream >
void serialize (Stream &stream, I64 &i)
 
template<typename Stream >
void serialize (Stream &stream, F32 &f)
 
template<typename Stream >
void serialize (Stream &stream, F64 &f)
 
template<typename Value , Uptr maxBits>
FORCEINLINE void serializeVarInt (OutputStream &stream, Value &inValue, Value minValue, Value maxValue)
 
template<typename Value , Uptr maxBits>
FORCEINLINE void serializeVarInt (InputStream &stream, Value &value, Value minValue, Value maxValue)
 
template<typename Stream , typename Value >
void serializeVarUInt1 (Stream &stream, Value &value)
 
template<typename Stream , typename Value >
void serializeVarUInt7 (Stream &stream, Value &value)
 
template<typename Stream , typename Value >
void serializeVarUInt32 (Stream &stream, Value &value)
 
template<typename Stream , typename Value >
void serializeVarUInt64 (Stream &stream, Value &value)
 
template<typename Stream , typename Value >
void serializeVarInt7 (Stream &stream, Value &value)
 
template<typename Stream , typename Value >
void serializeVarInt32 (Stream &stream, Value &value)
 
template<typename Stream , typename Value >
void serializeVarInt64 (Stream &stream, Value &value)
 
template<typename Constant >
void serializeConstant (InputStream &stream, const char *constantMismatchMessage, Constant constant)
 
template<typename Constant >
void serializeConstant (OutputStream &stream, const char *constantMismatchMessage, Constant constant)
 
template<typename Stream >
void serialize (Stream &stream, std::string &string)
 
template<typename Stream , typename Element , typename Allocator , typename SerializeElement >
void serializeArray (Stream &stream, std::vector< Element, Allocator > &vector, SerializeElement serializeElement)
 
template<typename Stream , typename Element , typename Allocator >
void serialize (Stream &stream, std::vector< Element, Allocator > &vector)
 

Function Documentation

◆ serialize() [1/10]

template<typename Stream >
void Serialization::serialize ( Stream & stream,
F32 & f )

Definition at line 153 of file Serialization.h.

153{ serializeNativeValue(stream,f); }
FORCEINLINE void serializeNativeValue(Stream &stream, Value &value)
Here is the call graph for this function:

◆ serialize() [2/10]

template<typename Stream >
void Serialization::serialize ( Stream & stream,
F64 & f )

Definition at line 154 of file Serialization.h.

154{ serializeNativeValue(stream,f); }
Here is the call graph for this function:

◆ serialize() [3/10]

template<typename Stream >
void Serialization::serialize ( Stream & stream,
I32 & i )

Definition at line 151 of file Serialization.h.

151{ serializeNativeValue(stream,i); }
Here is the call graph for this function:

◆ serialize() [4/10]

template<typename Stream >
void Serialization::serialize ( Stream & stream,
I64 & i )

Definition at line 152 of file Serialization.h.

152{ serializeNativeValue(stream,i); }
Here is the call graph for this function:

◆ serialize() [5/10]

template<typename Stream >
void Serialization::serialize ( Stream & stream,
I8 & i )

Definition at line 150 of file Serialization.h.

150{ serializeNativeValue(stream,i); }
Here is the call graph for this function:

◆ serialize() [6/10]

template<typename Stream >
void Serialization::serialize ( Stream & stream,
std::string & string )

Definition at line 264 of file Serialization.h.

265 {
267 Uptr size = string.size();
268 serializeVarUInt32(stream,size);
269 if(Stream::isInput)
270 {
271 // Advance the stream before resizing the string:
272 // try to get a serialization exception before making a huge allocation for malformed input.
273 const U8* inputBytes = stream.advance(size);
274 if (size >= max_size && WASM::check_limits)
275 throw FatalSerializationException(std::string("Trying to deserialize string of size : " + std::to_string((uint64_t)size) + ", which is over by "+std::to_string(size - max_size )+" bytes"));
276 string.resize(size);
277 memcpy(const_cast<char*>(string.data()),inputBytes,size);
278 string.shrink_to_fit();
279 }
280 else { serializeBytes(stream,(U8*)string.c_str(),size); }
281 }
uint8_t U8
Definition BasicTypes.h:5
PointerIntHelper< sizeof(size_t)>::UnsignedIntType Uptr
Definition BasicTypes.h:22
void serializeVarUInt32(Stream &stream, Value &value)
bool check_limits
Definition WASM.h:14
unsigned __int64 uint64_t
Definition stdint.h:136
memcpy((char *) pInfo->slotDescription, s, l)
Here is the call graph for this function:

◆ serialize() [7/10]

template<typename Stream , typename Element , typename Allocator >
void Serialization::serialize ( Stream & stream,
std::vector< Element, Allocator > & vector )

Definition at line 310 of file Serialization.h.

311 {
312 serializeArray(stream,vector,[](Stream& stream,Element& element){serialize(stream,element);});
313 }
Concept for reading and writing characters.
void serialize(Stream &stream, U8 &i)
void serializeArray(Stream &stream, std::vector< Element, Allocator > &vector, SerializeElement serializeElement)
Here is the call graph for this function:

◆ serialize() [8/10]

template<typename Stream >
void Serialization::serialize ( Stream & stream,
U32 & i )

Definition at line 148 of file Serialization.h.

148{ serializeNativeValue(stream,i); }
Here is the call graph for this function:

◆ serialize() [9/10]

template<typename Stream >
void Serialization::serialize ( Stream & stream,
U64 & i )

Definition at line 149 of file Serialization.h.

149{ serializeNativeValue(stream,i); }
Here is the call graph for this function:

◆ serialize() [10/10]

template<typename Stream >
void Serialization::serialize ( Stream & stream,
U8 & i )

Definition at line 147 of file Serialization.h.

147{ serializeNativeValue(stream,i); }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeArray()

template<typename Stream , typename Element , typename Allocator , typename SerializeElement >
void Serialization::serializeArray ( Stream & stream,
std::vector< Element, Allocator > & vector,
SerializeElement serializeElement )

Definition at line 284 of file Serialization.h.

285 {
287 Uptr size = vector.size();
288 serializeVarUInt32(stream,size);
289 if(Stream::isInput)
290 {
291 // Grow the vector one element at a time:
292 // try to get a serialization exception before making a huge allocation for malformed input.
293 vector.clear();
294 if (size >= max_size && WASM::check_limits)
295 throw FatalSerializationException(std::string("Trying to deserialize array of size : " + std::to_string((uint64_t)size) + ", which is over by "+std::to_string(size - max_size )+" bytes"));
296 for(Uptr index = 0;index < size;++index)
297 {
298 vector.push_back(Element());
299 serializeElement(stream,vector.back());
300 }
301 vector.shrink_to_fit();
302 }
303 else
304 {
305 for(Uptr index = 0;index < vector.size();++index) { serializeElement(stream,vector[index]); }
306 }
307 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeBytes() [1/2]

FORCEINLINE void Serialization::serializeBytes ( InputStream & stream,
U8 * bytes,
Uptr numBytes )

Definition at line 135 of file Serialization.h.

136 {
137 if ( numBytes < sysio::chain::wasm_constraints::wasm_page_size || !WASM::check_limits)
138 memcpy(bytes,stream.advance(numBytes),numBytes);
139 else
140 throw FatalSerializationException(std::string("Trying to deserialize bytes of size : " + std::to_string((uint64_t)numBytes)));
141 }
Here is the call graph for this function:

◆ serializeBytes() [2/2]

FORCEINLINE void Serialization::serializeBytes ( OutputStream & stream,
const U8 * bytes,
Uptr numBytes )

Definition at line 133 of file Serialization.h.

134 { memcpy(stream.advance(numBytes),bytes,numBytes); }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeConstant() [1/2]

template<typename Constant >
void Serialization::serializeConstant ( InputStream & stream,
const char * constantMismatchMessage,
Constant constant )

Definition at line 247 of file Serialization.h.

248 {
249 Constant savedConstant;
250 serialize(stream,savedConstant);
251 if(savedConstant != constant)
252 {
253 throw FatalSerializationException(std::string(constantMismatchMessage) + ": loaded " + std::to_string(savedConstant) + " but was expecting " + std::to_string(constant));
254 }
255 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeConstant() [2/2]

template<typename Constant >
void Serialization::serializeConstant ( OutputStream & stream,
const char * constantMismatchMessage,
Constant constant )

Definition at line 257 of file Serialization.h.

258 {
259 serialize(stream,constant);
260 }
Here is the call graph for this function:

◆ serializeNativeValue()

template<typename Stream , typename Value >
FORCEINLINE void Serialization::serializeNativeValue ( Stream & stream,
Value & value )

Definition at line 145 of file Serialization.h.

145{ serializeBytes(stream,(U8*)&value,sizeof(Value)); }
FORCEINLINE void serializeBytes(OutputStream &stream, const U8 *bytes, Uptr numBytes)
#define value
Definition pkcs11.h:157
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeVarInt() [1/2]

template<typename Value , Uptr maxBits>
FORCEINLINE void Serialization::serializeVarInt ( InputStream & stream,
Value & value,
Value minValue,
Value maxValue )

Definition at line 181 of file Serialization.h.

182 {
183 // First, read the variable number of input bytes into a fixed size buffer.
184 enum { maxBytes = (maxBits + 6) / 7 };
185 U8 bytes[maxBytes] = {0};
186 Uptr numBytes = 0;
187 I8 signExtendShift = (I8)sizeof(Value) * 8;
188 while(numBytes < maxBytes)
189 {
190 U8 byte = *stream.advance(1);
191 bytes[numBytes] = byte;
192 ++numBytes;
193 signExtendShift -= 7;
194 if(!(byte & 0x80)) { break; }
195 };
196
197 // Ensure that the input does not encode more than maxBits of data.
198 enum { numUsedBitsInLastByte = maxBits - (maxBytes-1) * 7 };
199 enum { numUnusedBitsInLast = 8 - numUsedBitsInLastByte };
200 enum { lastBitUsedMask = U8(1<<(numUsedBitsInLastByte-1)) };
201 enum { lastByteUsedMask = U8(1<<numUsedBitsInLastByte)-U8(1) };
202 enum { lastByteSignedMask = U8(~U8(lastByteUsedMask) & ~U8(0x80)) };
203 const U8 lastByte = bytes[maxBytes-1];
204 if(!std::is_signed<Value>::value)
205 {
206 if((lastByte & ~lastByteUsedMask) != 0)
207 {
208 throw FatalSerializationException("Invalid unsigned LEB encoding: unused bits in final byte must be 0");
209 }
210 }
211 else
212 {
213 const I8 signBit = I8((lastByte & lastBitUsedMask) << numUnusedBitsInLast);
214 const I8 signExtendedLastBit = signBit >> numUnusedBitsInLast;
215 if((lastByte & ~lastByteUsedMask) != (signExtendedLastBit & lastByteSignedMask))
216 {
217 throw FatalSerializationException(
218 "Invalid signed LEB encoding: unused bits in final byte must match the most-significant used bit");
219 }
220 }
221
222 // Decode the buffer's bytes into the output integer.
223 value = 0;
224 for(Uptr byteIndex = 0;byteIndex < maxBytes;++byteIndex)
225 { value |= Value(bytes[byteIndex] & ~0x80) << (byteIndex * 7); }
226
227 // Sign extend the output integer to the full size of Value.
228 if(std::is_signed<Value>::value && signExtendShift > 0)
229 { value = Value(value << signExtendShift) >> signExtendShift; }
230
231 // Check that the output integer is in the expected range.
232 if(value < minValue || value > maxValue)
233 { throw FatalSerializationException(std::string("out-of-range value: ") + std::to_string(minValue) + "<=" + std::to_string(value) + "<=" + std::to_string(maxValue)); }
234 }
int8_t I8
Definition BasicTypes.h:6
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition document.h:2110
#define signBit
Definition fp128.h:17
static const AddressFrame byte(8)
vector< char > bytes
Definition types.hpp:243

◆ serializeVarInt() [2/2]

template<typename Value , Uptr maxBits>
FORCEINLINE void Serialization::serializeVarInt ( OutputStream & stream,
Value & inValue,
Value minValue,
Value maxValue )

Definition at line 158 of file Serialization.h.

159 {
160 Value value = inValue;
161
162 if(value < minValue || value > maxValue)
163 {
164 throw FatalSerializationException(std::string("out-of-range value: ") + std::to_string(minValue) + "<=" + std::to_string(value) + "<=" + std::to_string(maxValue));
165 }
166
167 bool more = true;
168 while(more)
169 {
170 U8 outputByte = (U8)(value&127);
171 value >>= 7;
172 more = std::is_signed<Value>::value
173 ? (value != 0 && value != Value(-1)) || (value >= 0 && (outputByte & 0x40)) || (value < 0 && !(outputByte & 0x40))
174 : (value != 0);
175 if(more) { outputByte |= 0x80; }
176 *stream.advance(1) = outputByte;
177 };
178 }
Here is the caller graph for this function:

◆ serializeVarInt32()

template<typename Stream , typename Value >
void Serialization::serializeVarInt32 ( Stream & stream,
Value & value )

Definition at line 242 of file Serialization.h.

242{ serializeVarInt<Value,32>(stream,value,INT32_MIN,INT32_MAX); }
#define INT32_MAX
Definition stdint.h:183
#define INT32_MIN
Definition stdint.h:182
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeVarInt64()

template<typename Stream , typename Value >
void Serialization::serializeVarInt64 ( Stream & stream,
Value & value )

Definition at line 243 of file Serialization.h.

243{ serializeVarInt<Value,64>(stream,value,INT64_MIN,INT64_MAX); }
#define INT64_MIN
Definition stdint.h:184
#define INT64_MAX
Definition stdint.h:185
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeVarInt7()

template<typename Stream , typename Value >
void Serialization::serializeVarInt7 ( Stream & stream,
Value & value )

Definition at line 241 of file Serialization.h.

241{ serializeVarInt<Value,7>(stream,value,-64,63); }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeVarUInt1()

template<typename Stream , typename Value >
void Serialization::serializeVarUInt1 ( Stream & stream,
Value & value )

Definition at line 237 of file Serialization.h.

237{ serializeVarInt<Value,1>(stream,value,0,1); }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeVarUInt32()

template<typename Stream , typename Value >
void Serialization::serializeVarUInt32 ( Stream & stream,
Value & value )

Definition at line 239 of file Serialization.h.

239{ serializeVarInt<Value,32>(stream,value,0,UINT32_MAX); }
#define UINT32_MAX
Definition stdint.h:188
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeVarUInt64()

template<typename Stream , typename Value >
void Serialization::serializeVarUInt64 ( Stream & stream,
Value & value )

Definition at line 240 of file Serialization.h.

240{ serializeVarInt<Value,64>(stream,value,0,UINT64_MAX); }
#define UINT64_MAX
Definition stdint.h:189
Here is the call graph for this function:

◆ serializeVarUInt7()

template<typename Stream , typename Value >
void Serialization::serializeVarUInt7 ( Stream & stream,
Value & value )

Definition at line 238 of file Serialization.h.

238{ serializeVarInt<Value,7>(stream,value,0,127); }
Here is the call graph for this function:
Here is the caller graph for this function: