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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
use alloc::vec::Vec;
use arrayvec::ArrayVec;
use core::num::NonZeroUsize;

use crate::codec_err::DecodeError;
use crate::TypeInfo;

/// Trait that allows reading of data into a slice.
pub trait Input {
	/// Should return the remaining length of the input data. If no information about the input
	/// length is available, `None` should be returned.
	///
	/// The length is used to constrain the preallocation while decoding. Returning a garbage
	/// length can open the doors for a denial of service attack to your application.
	/// Otherwise, returning `None` can decrease the performance of your application.
    fn remaining_len(&mut self) -> usize;
    
    fn empty(&mut self)-> bool {
        self.remaining_len() == 0
    }

	/// Read the exact number of bytes required to fill the given buffer.
    fn read_into(&mut self, into: &mut [u8]) -> Result<(), DecodeError>;

	/// Read a single byte from the input.
	fn read_byte(&mut self) -> Result<u8, DecodeError> {
		let mut buf = [0u8];
		self.read_into(&mut buf[..])?;
		Ok(buf[0])
    }

    /// Read the exact number of bytes required to fill the given buffer.
	fn read_slice(&mut self, length: usize) -> Result<&[u8], DecodeError>;
    
    fn flush(&mut self) -> Result<&[u8], DecodeError>;

}

impl<'a> Input for &'a [u8] {
	fn remaining_len(&mut self) -> usize {
		self.len()
    }

	fn read_into(&mut self, into: &mut [u8]) -> Result<(), DecodeError> {
		if into.len() > self.len() {
			return Err(DecodeError::InputTooShort);
		}
		let len = into.len();
		into.copy_from_slice(&self[..len]);
		*self = &self[len..];
		Ok(())
    }

    fn read_slice(&mut self, length: usize) -> Result<&[u8], DecodeError> {
        if length > self.len() {
            return Err(DecodeError::InputTooShort);
        }

        let (result, rest) = self.split_at(length);
        *self = rest;
        Ok(result)
    }
    
    fn flush(&mut self) -> Result<&[u8], DecodeError> {
        let result = &self[..];
        *self = &[];
        Ok(result)
    }
}

/// Trait that allows zero-copy read of value-references from slices in LE format.
pub trait Decode: Sized {
	// !INTERNAL USE ONLY!
	// This const helps elrond-wasm to optimize the encoding/decoding by doing fake specialization.
	#[doc(hidden)]
	const TYPE_INFO: TypeInfo = TypeInfo::Unknown;

    /// Attempt to deserialise the value from input,
    /// using the format of an object nested inside another structure.
    /// In case of success returns the deserialized value and the number of bytes consumed during the operation.
    fn dep_decode<I: Input>(input: &mut I) -> Result<Self, DecodeError>;

    /// Attempt to deserialise the value from input.
	fn top_decode<I: Input>(input: &mut I) -> Result<Self, DecodeError> {
        let result = Self::dep_decode(input)?;
        if input.remaining_len() > 0 {
            return Err(DecodeError::InputTooLong);
        }
        Ok(result)
    }

    /// Used to optimize handling of arguments/storage.
    /// Optionally calls a closure that retrieves an i64 and if it returns Some(...), the result will be used.
    #[inline]
    fn top_decode_from_i64<I: FnOnce() -> i64>(_input: I) -> Option<Result<Self, DecodeError>> {
        None
    }
}

/// Convenience method, to avoid having to specify type when calling `top_decode`.
/// Especially useful in the macros.
#[inline]
pub fn decode_from_byte_slice<D: Decode>(input: &[u8]) -> Result<D, DecodeError> {
    // the input doesn't need to be mutable because we are not changing the underlying data 
    D::top_decode(&mut &*input)
}

impl Decode for () {
    const TYPE_INFO: TypeInfo = TypeInfo::Unit;

	fn dep_decode<I: Input>(_: &mut I) -> Result<(), DecodeError> {
		Ok(())
	}
}

impl Decode for u8 {
    const TYPE_INFO: TypeInfo = TypeInfo::U8;
    
	fn top_decode<I: Input>(input: &mut I) -> Result<Self, DecodeError> {
        let bytes = input.flush()?;
        match bytes.len() {
            0 => Ok(0u8),
            1 => Ok(bytes[0]),
            _ => Err(DecodeError::InputTooLong),
        }
    }
    
    fn dep_decode<I: Input>(input: &mut I) -> Result<Self, DecodeError> {
        input.read_byte()
    }
}

impl<T: Decode> Decode for Vec<T> {
	fn top_decode<I: Input>(input: &mut I) -> Result<Self, DecodeError> {
        match T::TYPE_INFO {
			TypeInfo::U8 => {
                let bytes = input.flush()?;
                let bytes_copy = bytes.to_vec(); // copy is needed because result might outlive input
                let cast_vec: Vec<T> = unsafe { core::mem::transmute(bytes_copy) };
                Ok(cast_vec)
			},
			_ => {
                let mut result: Vec<T> = Vec::new();
                while input.remaining_len() > 0 {
                    result.push(T::dep_decode(input)?);
                }
                Ok(result)
			}
        }
    }
    
    fn dep_decode<I: Input>(input: &mut I) -> Result<Self, DecodeError> {
        let size = usize::dep_decode(input)?;
        match T::TYPE_INFO {
			TypeInfo::U8 => {
                let bytes = input.read_slice(size)?;
                let bytes_copy = bytes.to_vec(); // copy is needed because result might outlive input
                let cast_vec: Vec<T> = unsafe { core::mem::transmute(bytes_copy) };
                Ok(cast_vec)
			},
			_ => {
                let mut result: Vec<T> = Vec::with_capacity(size);
				for _ in 0..size {
                    result.push(T::dep_decode(input)?);
                }
                Ok(result)
			}
        }
    }
}

/// Handles both signed and unsigned of any length.
/// No generics here, because we want the executable binary as small as possible.
pub fn bytes_to_number(bytes: &[u8], signed: bool) -> u64 {
    if bytes.is_empty() {
        return 0;
    }
    let negative = signed && bytes[0] >> 7 == 1;
    let mut result = 
        if negative {
            // start with all bits set to 1, 
            // to ensure that if there are fewer bytes than the result type width,
            // the leading bits will be 1 instead of 0
            0xffffffffffffffffu64 
        } else { 
            0u64 
        };
    for byte in bytes.iter() {
        result <<= 8;
        result |= *byte as u64;
    }
    result
}

macro_rules! decode_num_unsigned {
    ($ty:ty, $num_bytes:expr, $type_info:expr) => {
        impl Decode for $ty {
            const TYPE_INFO: TypeInfo = $type_info;
            
            fn top_decode<I: Input>(input: &mut I) -> Result<Self, DecodeError> {
                let bytes = input.flush()?;
                if bytes.len() > $num_bytes {
                    return Err(DecodeError::InputTooLong)
                }
                let num = bytes_to_number(bytes, false) as $ty;
                Ok(num)
            }
            
            fn dep_decode<I: Input>(input: &mut I) -> Result<Self, DecodeError> {
                let bytes = input.read_slice($num_bytes)?;
                let num = bytes_to_number(bytes, false) as $ty;
                Ok(num)
            }
        }
    }
}

decode_num_unsigned!(u16, 2, TypeInfo::U16);
decode_num_unsigned!(u32, 4, TypeInfo::U32);
decode_num_unsigned!(usize, 4, TypeInfo::USIZE);
decode_num_unsigned!(u64, 8, TypeInfo::U64);

macro_rules! decode_num_signed {
    ($ty:ty, $num_bytes:expr, $type_info:expr) => {
        impl Decode for $ty {
            const TYPE_INFO: TypeInfo = $type_info;
            
            fn top_decode<I: Input>(input: &mut I) -> Result<Self, DecodeError> {
                let bytes = input.flush()?;
                if bytes.len() > $num_bytes {
                    return Err(DecodeError::InputTooLong)
                }
                let num = bytes_to_number(bytes, true) as $ty;
                Ok(num)
            }
            
            fn dep_decode<I: Input>(input: &mut I) -> Result<Self, DecodeError> {
                let bytes = input.read_slice($num_bytes)?;
                let num = bytes_to_number(bytes, true) as $ty;
                Ok(num)
            }

            #[inline]
            fn top_decode_from_i64<I: FnOnce() -> i64>(input: I) -> Option<Result<Self, DecodeError>> {
                let arg_i64 = input();
                let min = <$ty>::MIN as i64;
                let max = <$ty>::MAX as i64;
                if arg_i64 < min || arg_i64 > max {
                    Some(Err(DecodeError::InputOutOfRange))
                } else {
                    Some(Ok(arg_i64 as $ty))
                }
            }
        }
    }
}

decode_num_signed!(i8 , 1, TypeInfo::I8);
decode_num_signed!(i16, 2, TypeInfo::I16);
decode_num_signed!(i32, 4, TypeInfo::I32);
decode_num_signed!(isize, 4, TypeInfo::ISIZE);
decode_num_signed!(i64, 8, TypeInfo::I64);

impl Decode for bool {
    const TYPE_INFO: TypeInfo = TypeInfo::Bool;
    
	fn top_decode<I: Input>(input: &mut I) -> Result<Self, DecodeError> {
        let bytes = input.flush()?;
        match bytes.len() {
            0 => Ok(false),
            1 => match bytes[0] {
                0 => Ok(false),
                1 => Ok(true),
                _ => Err(DecodeError::InvalidValue),
            }
            _ => Err(DecodeError::InputTooLong),
        }
    }
    
    fn dep_decode<I: Input>(input: &mut I) -> Result<Self, DecodeError> {
        match input.read_byte()? {
            0 => Ok(false),
            1 => Ok(true),
            _ => Err(DecodeError::InvalidValue),
        }
    }

    #[inline]
    fn top_decode_from_i64<I: FnOnce() -> i64>(input: I) -> Option<Result<Self, DecodeError>> {
        Some(match input() {
            0 => Ok(false),
            1 => Ok(true),
            _ => Err(DecodeError::InputOutOfRange),
        })
    }
}

impl<T: Decode> Decode for Option<T> {
	fn top_decode<I: Input>(input: &mut I) -> Result<Self, DecodeError> {
        if input.empty() {
            Ok(None)
        } else {
            let result = Self::dep_decode(input);
            if input.remaining_len() > 0 {
                return Err(DecodeError::InputTooLong);
            }
            result
        }
    }
    
    fn dep_decode<I: Input>(input: &mut I) -> Result<Self, DecodeError> {
        match input.read_byte()? {
			0 => Ok(None),
			1 => Ok(Some(T::dep_decode(input)?)),
			_ => Err(DecodeError::InvalidValue),
		}
    }
}

macro_rules! tuple_impls {
    ($($len:expr => ($($n:tt $name:ident)+))+) => {
        $(
            impl<$($name),+> Decode for ($($name,)+)
            where
                $($name: Decode,)+
            {
                fn dep_decode<I: Input>(input: &mut I) -> Result<Self, DecodeError> {
                    let tuple = (
                        $(
                            $name::dep_decode(input)?,
                        )+
                    );
                    Ok(tuple)
                }
            }
        )+
    }
}

tuple_impls! {
    1 => (0 T0)
    2 => (0 T0 1 T1)
    3 => (0 T0 1 T1 2 T2)
    4 => (0 T0 1 T1 2 T2 3 T3)
    5 => (0 T0 1 T1 2 T2 3 T3 4 T4)
    6 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5)
    7 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6)
    8 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7)
    9 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8)
    10 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9)
    11 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10)
    12 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11)
    13 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12)
    14 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13)
    15 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14)
    16 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15)
}

macro_rules! array_impls {
    ($($n: tt,)+) => {
        $(
            impl<T: Decode> Decode for [T; $n] {
				fn dep_decode<I: Input>(input: &mut I) -> Result<Self, DecodeError> {
					let mut r = ArrayVec::new();
					for _ in 0..$n {
						r.push(T::dep_decode(input)?);
					}
					let i = r.into_inner();

					match i {
						Ok(a) => Ok(a),
						Err(_) => Err(DecodeError::ArrayDecodeErr),
					}
				}
            }
        )+
    }
}

array_impls!(
	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, 88, 89, 90, 91,
	92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
	109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124,
	125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140,
	141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
	157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172,
	173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188,
	189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204,
	205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220,
	221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236,
	237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252,
	253, 254, 255, 256, 384, 512, 768, 1024, 2048, 4096, 8192, 16384, 32768,
);

fn decode_non_zero_usize(num: usize) -> Result<NonZeroUsize, DecodeError> {
    if let Some(nz) = NonZeroUsize::new(num) {
        Ok(nz)
    } else {
        Err(DecodeError::InvalidValue)
    }
}

impl Decode for NonZeroUsize {
    fn top_decode<I: Input>(input: &mut I) -> Result<Self, DecodeError> {
        decode_non_zero_usize(usize::top_decode(input)?)
    }
    
    fn dep_decode<I: Input>(input: &mut I) -> Result<Self, DecodeError> {
        decode_non_zero_usize(usize::dep_decode(input)?)
    }
}

////////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
mod tests {
    use super::*;
	use super::super::test_struct::*;
    use core::fmt::Debug;

    fn deser_ok<V>(element: V, bytes: &[u8])
    where
        V: Decode + PartialEq + Debug + 'static,
    {
        let input = bytes.to_vec();
        let deserialized: V = V::top_decode(&mut &input[..]).unwrap();
        assert_eq!(deserialized, element);
    }

    #[test]
    fn test_top_numbers_decompacted() {
        // unsigned positive
        deser_ok(5u8, &[5]);
        deser_ok(5u16, &[5]);
        deser_ok(5u32, &[5]);
        deser_ok(5u64, &[5]);
        deser_ok(5usize, &[5]);
        // signed positive
        deser_ok(5i8, &[5]);
        deser_ok(5i16, &[5]);
        deser_ok(5i32, &[5]);
        deser_ok(5i64, &[5]);
        deser_ok(5isize, &[5]);
        // signed negative
        deser_ok(-5i8, &[251]);
        deser_ok(-5i16, &[251]);
        deser_ok(-5i32, &[251]);
        deser_ok(-5i64, &[251]);
        deser_ok(-5isize, &[251]);
        // non zero usize
        deser_ok(NonZeroUsize::new(5).unwrap(), &[5]);
    }

    

    #[test]
    fn test_struct() {
        let test = Test {
            int: 1,
            seq: [5, 6].to_vec(),
            another_byte: 7,
        };
        deser_ok(test, &[0, 1, 0, 0, 0, 2, 5, 6, 7]);
    }

    #[test]
    fn test_enum() {
        let u = E::Unit;
        let expected: &[u8] = &[/*variant index*/ 0, 0, 0, 0];
        deser_ok(u, expected);

        let n = E::Newtype(1);
        let expected: &[u8] = &[/*variant index*/ 0, 0, 0, 1, /*data*/ 0, 0, 0, 1];
        deser_ok(n, expected);

        let t = E::Tuple(1, 2);
        let expected: &[u8] = &[/*variant index*/ 0, 0, 0, 2, /*(*/ 0, 0, 0, 1, /*,*/ 0, 0, 0, 2 /*)*/];
        deser_ok(t, expected);

        let s = E::Struct { a: 1 };
        let expected: &[u8] = &[/*variant index*/ 0, 0, 0, 3, /*data*/ 0, 0, 0, 1];
        deser_ok(s, expected);
    }
}