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
// Copyright 2020 Parity Technologies
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use bytes::{BufMut, BytesMut};
use core::borrow::Borrow;

use crate::traits::Encodable;

#[derive(Debug, Copy, Clone)]
struct ListInfo {
	position: usize,
	current: usize,
	max: Option<usize>,
}

impl ListInfo {
	fn new(position: usize, max: Option<usize>) -> ListInfo {
		ListInfo { position, current: 0, max }
	}
}

/// Appendable rlp encoder.
pub struct RlpStream {
	unfinished_lists: Vec<ListInfo>,
	start_pos: usize,
	buffer: BytesMut,
	finished_list: bool,
}

impl Default for RlpStream {
	fn default() -> Self {
		RlpStream::new()
	}
}

impl RlpStream {
	/// Initializes instance of empty `Stream`.
	pub fn new() -> Self {
		Self::new_with_buffer(BytesMut::with_capacity(1024))
	}

	/// Initializes the `Stream` as a list.
	pub fn new_list(len: usize) -> Self {
		Self::new_list_with_buffer(BytesMut::with_capacity(1024), len)
	}

	/// Initializes instance of empty `Stream`.
	pub fn new_with_buffer(buffer: BytesMut) -> Self {
		RlpStream { unfinished_lists: Vec::with_capacity(16), start_pos: buffer.len(), buffer, finished_list: false }
	}

	/// Initializes the `Stream` as a list.
	pub fn new_list_with_buffer(buffer: BytesMut, len: usize) -> Self {
		let mut stream = RlpStream::new_with_buffer(buffer);
		stream.begin_list(len);
		stream
	}

	fn total_written(&self) -> usize {
		self.buffer.len() - self.start_pos
	}

	/// Apends null to the end of stream, chainable.
	///
	/// ```
	/// use tetsy_rlp::RlpStream;
	/// let mut stream = RlpStream::new_list(2);
	/// stream.append_empty_data().append_empty_data();
	/// let out = stream.out();
	/// assert_eq!(out, vec![0xc2, 0x80, 0x80]);
	/// ```
	pub fn append_empty_data(&mut self) -> &mut Self {
		// self push raw item
		self.buffer.put_u8(0x80);

		// try to finish and prepend the length
		self.note_appended(1);

		// return chainable self
		self
	}

	/// Appends raw (pre-serialised) RLP data. Use with caution. Chainable.
	pub fn append_raw(&mut self, bytes: &[u8], item_count: usize) -> &mut Self {
		// push raw items
		self.buffer.extend_from_slice(bytes);

		// try to finish and prepend the length
		self.note_appended(item_count);

		// return chainable self
		self
	}

	/// Appends value to the end of stream, chainable.
	///
	/// ```
	/// use tetsy_rlp::RlpStream;
	/// let mut stream = RlpStream::new_list(2);
	/// stream.append(&"cat").append(&"dog");
	/// let out = stream.out();
	/// assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
	/// ```
	pub fn append<E>(&mut self, value: &E) -> &mut Self
	where
		E: Encodable,
	{
		self.finished_list = false;
		value.rlp_append(self);
		if !self.finished_list {
			self.note_appended(1);
		}
		self
	}

	/// Appends iterator to the end of stream, chainable.
	///
	/// ```
	/// use tetsy_rlp::RlpStream;
	/// let mut stream = RlpStream::new_list(2);
	/// stream.append(&"cat").append_iter("dog".as_bytes().iter().cloned());
	/// let out = stream.out();
	/// assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
	/// ```
	pub fn append_iter<I>(&mut self, value: I) -> &mut Self
	where
		I: IntoIterator<Item = u8>,
	{
		self.finished_list = false;
		self.encoder().encode_iter(value);
		if !self.finished_list {
			self.note_appended(1);
		}
		self
	}

	/// Appends list of values to the end of stream, chainable.
	pub fn append_list<E, K>(&mut self, values: &[K]) -> &mut Self
	where
		E: Encodable,
		K: Borrow<E>,
	{
		self.begin_list(values.len());
		for value in values {
			self.append(value.borrow());
		}
		self
	}

	/// Appends value to the end of stream, but do not count it as an appended item.
	/// It's useful for wrapper types
	pub fn append_internal<E>(&mut self, value: &E) -> &mut Self
	where
		E: Encodable,
	{
		value.rlp_append(self);
		self
	}

	/// Declare appending the list of given size, chainable.
	///
	/// ```
	/// use tetsy_rlp::RlpStream;
	/// let mut stream = RlpStream::new_list(2);
	/// stream.begin_list(2).append(&"cat").append(&"dog");
	/// stream.append(&"");
	/// let out = stream.out();
	/// assert_eq!(out, vec![0xca, 0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g', 0x80]);
	/// ```
	pub fn begin_list(&mut self, len: usize) -> &mut RlpStream {
		self.finished_list = false;
		match len {
			0 => {
				// we may finish, if the appended list len is equal 0
				self.buffer.put_u8(0xc0u8);
				self.note_appended(1);
				self.finished_list = true;
			}
			_ => {
				// payload is longer than 1 byte only for lists > 55 bytes
				// by pushing always this 1 byte we may avoid unnecessary shift of data
				self.buffer.put_u8(0);

				let position = self.total_written();
				self.unfinished_lists.push(ListInfo::new(position, Some(len)));
			}
		}

		// return chainable self
		self
	}

	/// Declare appending the list of unknown size, chainable.
	pub fn begin_unbounded_list(&mut self) -> &mut RlpStream {
		self.finished_list = false;
		// payload is longer than 1 byte only for lists > 55 bytes
		// by pushing always this 1 byte we may avoid unnecessary shift of data
		self.buffer.put_u8(0);
		let position = self.total_written();
		self.unfinished_lists.push(ListInfo::new(position, None));
		// return chainable self
		self
	}

	/// Appends raw (pre-serialised) RLP data. Checks for size overflow.
	pub fn append_raw_checked(&mut self, bytes: &[u8], item_count: usize, max_size: usize) -> bool {
		if self.estimate_size(bytes.len()) > max_size {
			return false;
		}
		self.append_raw(bytes, item_count);
		true
	}

	/// Calculate total RLP size for appended payload.
	pub fn estimate_size(&self, add: usize) -> usize {
		let total_size = self.total_written() + add;
		let mut base_size = total_size;
		for list in &self.unfinished_lists[..] {
			let len = total_size - list.position;
			if len > 55 {
				let leading_empty_bytes = (len as u64).leading_zeros() as usize / 8;
				let size_bytes = 8 - leading_empty_bytes;
				base_size += size_bytes;
			}
		}
		base_size
	}

	/// Returns current RLP size in bytes for the data pushed into the list.
	pub fn len(&self) -> usize {
		self.estimate_size(0)
	}

	pub fn is_empty(&self) -> bool {
		self.len() == 0
	}

	/// Clear the output stream so far.
	///
	/// ```
	/// use tetsy_rlp::RlpStream;
	/// let mut stream = RlpStream::new_list(3);
	/// stream.append(&"cat");
	/// stream.clear();
	/// stream.append(&"dog");
	/// let out = stream.out();
	/// assert_eq!(out, vec![0x83, b'd', b'o', b'g']);
	/// ```
	pub fn clear(&mut self) {
		// clear bytes
		self.buffer.truncate(self.start_pos);

		// clear lists
		self.unfinished_lists.clear();
	}

	/// Returns true if stream doesnt expect any more items.
	///
	/// ```
	/// use tetsy_rlp::RlpStream;
	/// let mut stream = RlpStream::new_list(2);
	/// stream.append(&"cat");
	/// assert_eq!(stream.is_finished(), false);
	/// stream.append(&"dog");
	/// assert_eq!(stream.is_finished(), true);
	/// let out = stream.out();
	/// assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
	/// ```
	pub fn is_finished(&self) -> bool {
		self.unfinished_lists.is_empty()
	}

	/// Get raw encoded bytes
	pub fn as_raw(&self) -> &[u8] {
		//&self.encoder.bytes
		&self.buffer
	}

	/// Streams out encoded bytes.
	///
	/// panic! if stream is not finished.
	pub fn out(self) -> BytesMut {
		if self.is_finished() {
			self.buffer
		} else {
			panic!()
		}
	}

	/// Try to finish lists
	fn note_appended(&mut self, inserted_items: usize) {
		if self.unfinished_lists.is_empty() {
			return;
		}

		let back = self.unfinished_lists.len() - 1;
		let should_finish = match self.unfinished_lists.get_mut(back) {
			None => false,
			Some(ref mut x) => {
				x.current += inserted_items;
				match x.max {
					Some(ref max) if x.current > *max => panic!("You cannot append more items than you expect!"),
					Some(ref max) => x.current == *max,
					_ => false,
				}
			}
		};
		if should_finish {
			let x = self.unfinished_lists.pop().unwrap();
			let len = self.total_written() - x.position;
			self.encoder().insert_list_payload(len, x.position);
			self.note_appended(1);
		}
		self.finished_list = should_finish;
	}

	pub fn encoder(&mut self) -> BasicEncoder {
		BasicEncoder::new(self, self.start_pos)
	}

	/// Finalize current unbounded list. Panics if no unbounded list has been opened.
	pub fn finalize_unbounded_list(&mut self) {
		let list = self.unfinished_lists.pop().expect("No open list.");
		if list.max.is_some() {
			panic!("List type mismatch.");
		}
		let len = self.total_written() - list.position;
		self.encoder().insert_list_payload(len, list.position);
		self.note_appended(1);
		self.finished_list = true;
	}
}

pub struct BasicEncoder<'a> {
	buffer: &'a mut BytesMut,
	start_pos: usize,
}

impl<'a> BasicEncoder<'a> {
	fn new(stream: &'a mut RlpStream, start_pos: usize) -> Self {
		BasicEncoder { buffer: &mut stream.buffer, start_pos }
	}

	fn total_written(&self) -> usize {
		self.buffer.len() - self.start_pos
	}

	fn insert_size(&mut self, size: usize, position: usize) -> u8 {
		let size = size as u32;
		let leading_empty_bytes = size.leading_zeros() as usize / 8;
		let size_bytes = 4 - leading_empty_bytes as u8;
		let buffer: [u8; 4] = size.to_be_bytes();
		assert!(position <= self.total_written());

		self.buffer.extend_from_slice(&buffer[leading_empty_bytes..]);
		self.buffer[self.start_pos + position..].rotate_right(size_bytes as usize);
		size_bytes as u8
	}

	/// Inserts list prefix at given position
	fn insert_list_payload(&mut self, len: usize, pos: usize) {
		// 1 byte was already reserved for payload earlier
		match len {
			0..=55 => {
				self.buffer[self.start_pos + pos - 1] = 0xc0u8 + len as u8;
			}
			_ => {
				let inserted_bytes = self.insert_size(len, pos);
				self.buffer[self.start_pos + pos - 1] = 0xf7u8 + inserted_bytes;
			}
		};
	}

	pub fn encode_value(&mut self, value: &[u8]) {
		self.encode_iter(value.iter().cloned());
	}

	/// Pushes encoded value to the end of buffer
	pub fn encode_iter<I>(&mut self, value: I)
	where
		I: IntoIterator<Item = u8>,
	{
		let mut value = value.into_iter();
		let len = match value.size_hint() {
			(lower, Some(upper)) if lower == upper => lower,
			_ => {
				let value = value.collect::<Vec<_>>();
				return self.encode_iter(value);
			}
		};
		match len {
			// just 0
			0 => self.buffer.put_u8(0x80u8),
			len @ 1..=55 => {
				let first = value.next().expect("iterator length is higher than 1");
				if len == 1 && first < 0x80 {
					// byte is its own encoding if < 0x80
					self.buffer.put_u8(first);
				} else {
					// (prefix + length), followed by the string
					self.buffer.put_u8(0x80u8 + len as u8);
					self.buffer.put_u8(first);
					self.buffer.extend(value);
				}
			}
			// (prefix + length of length), followed by the length, followd by the string
			len => {
				self.buffer.put_u8(0);
				let position = self.total_written();
				let inserted_bytes = self.insert_size(len, position);
				self.buffer[self.start_pos + position - 1] = 0xb7 + inserted_bytes;
				self.buffer.extend(value);
			}
		}
	}
}