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
use crate::abi::TypeAbi;
use alloc::alloc::{alloc, alloc_zeroed, realloc, Layout};
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
use dharitri_codec::*;

/// Simple wrapper around a boxed byte slice,
/// but with a lot of optimized methods for manipulating it.
/// The focus is on readucing code size rather improving speed.
#[derive(Clone, PartialEq, Debug)]
pub struct BoxedBytes(Box<[u8]>);

impl BoxedBytes {
	pub fn empty() -> Self {
		BoxedBytes(Box::from([0u8; 0]))
	}

	pub fn zeros(len: usize) -> Self {
		unsafe {
			let layout = Layout::from_size_align(len, core::mem::align_of::<u8>()).unwrap();
			let bytes_ptr = alloc_zeroed(layout);
			let bytes_box = Box::from_raw(core::slice::from_raw_parts_mut(bytes_ptr, len));
			BoxedBytes(bytes_box)
		}
	}

	/// Allocates an uninitialized BoxedBytes to heap.
	///
	/// # Safety
	///
	/// Should only be called if the contents are initialized immediately afterwards, e.g. via a FFI call.
	pub unsafe fn allocate(len: usize) -> Self {
		let layout = Layout::from_size_align(len, core::mem::align_of::<u8>()).unwrap();
		let bytes_ptr = alloc(layout);
		let bytes_box = Box::from_raw(core::slice::from_raw_parts_mut(bytes_ptr, len));
		BoxedBytes(bytes_box)
	}

	#[inline]
	pub fn as_ptr(&self) -> *const u8 {
		self.0.as_ptr()
	}

	#[inline]
	pub fn as_mut_ptr(&mut self) -> *mut u8 {
		self.0.as_mut_ptr()
	}

	#[inline]
	pub fn len(&self) -> usize {
		self.0.len()
	}

	#[inline]
	pub fn is_empty(&self) -> bool {
		self.0.is_empty()
	}

	#[inline]
	pub fn into_box(self) -> Box<[u8]> {
		self.0
	}

	#[inline]
	pub fn as_slice(&self) -> &[u8] {
		&*self.0
	}

	/// Create new instance by concatenating several byte slices.
	pub fn from_concat(slices: &[&[u8]]) -> Self {
		let mut result_len = 0usize;
		let mut index = slices.len();
		while index > 0 {
			index -= 1;
			result_len += slices[index].len();
		}
		unsafe {
			let layout = Layout::from_size_align(result_len, core::mem::align_of::<u8>()).unwrap();
			let result_ptr = alloc(layout);
			let mut current_index = 0usize;
			for slice in slices.iter() {
				core::ptr::copy_nonoverlapping(
					slice.as_ptr(),
					result_ptr.add(current_index),
					slice.len(),
				);
				current_index += slice.len();
			}
			let bytes_box = Box::from_raw(core::slice::from_raw_parts_mut(result_ptr, result_len));
			BoxedBytes(bytes_box)
		}
	}

	/// Splits BoxedBytes into 2 others at designated position.
	/// Returns the original and an empty BoxedBytes if position arugment out of range.
	pub fn split(self, at: usize) -> (BoxedBytes, BoxedBytes) {
		if at >= self.len() {
			(self, BoxedBytes::empty())
		} else {
			let other_len = self.len() - at;
			unsafe {
				// breaking down the input into its components
				let self_layout =
					Layout::from_size_align(self.len(), core::mem::align_of::<u8>()).unwrap();
				let self_ptr = Box::into_raw(self.0) as *mut u8;

				// the data for the second result needs to be copied somewhere else
				let other_layout =
					Layout::from_size_align(other_len, core::mem::align_of::<u8>()).unwrap();
				let other_ptr = alloc(other_layout);
				core::ptr::copy_nonoverlapping(self_ptr.add(at), other_ptr, other_len);

				// truncating the memory for the first using a realloc
				// got inspiration for this from the RawVec implementation
				let realloc_ptr = realloc(self_ptr, self_layout, at);

				// packaging the resulting parts nicely
				let bytes_box_1 = Box::from_raw(core::slice::from_raw_parts_mut(realloc_ptr, at));
				let bytes_box_2 =
					Box::from_raw(core::slice::from_raw_parts_mut(other_ptr, other_len));
				(BoxedBytes(bytes_box_1), BoxedBytes(bytes_box_2))
			}
		}
	}
}

impl AsRef<[u8]> for BoxedBytes {
	#[inline]
	fn as_ref(&self) -> &[u8] {
		&*self.0
	}
}

impl<'a> From<&'a [u8]> for BoxedBytes {
	#[inline]
	fn from(byte_slice: &'a [u8]) -> Self {
		BoxedBytes(Box::from(byte_slice))
	}
}

impl From<Box<[u8]>> for BoxedBytes {
	#[inline]
	fn from(b: Box<[u8]>) -> Self {
		BoxedBytes(b)
	}
}

impl From<Vec<u8>> for BoxedBytes {
	#[inline]
	fn from(v: Vec<u8>) -> Self {
		BoxedBytes(v.into_boxed_slice())
	}
}

impl NestedEncode for BoxedBytes {
	#[inline]
	fn dep_encode<O: NestedEncodeOutput>(&self, dest: &mut O) -> Result<(), EncodeError> {
		self.len().dep_encode(dest)?;
		dest.write(self.as_ref());
		Ok(())
	}

	#[inline]
	fn dep_encode_or_exit<O: NestedEncodeOutput, ExitCtx: Clone>(
		&self,
		dest: &mut O,
		c: ExitCtx,
		exit: fn(ExitCtx, EncodeError) -> !,
	) {
		self.len().dep_encode_or_exit(dest, c, exit);
		dest.write(self.as_ref());
	}
}

impl TopEncode for BoxedBytes {
	#[inline]
	fn top_encode<O: TopEncodeOutput>(&self, output: O) -> Result<(), EncodeError> {
		output.set_slice_u8(self.as_ref());
		Ok(())
	}

	#[inline]
	fn top_encode_or_exit<O: TopEncodeOutput, ExitCtx: Clone>(
		&self,
		output: O,
		_: ExitCtx,
		_: fn(ExitCtx, EncodeError) -> !,
	) {
		output.set_slice_u8(self.as_ref());
	}
}

impl NestedDecode for BoxedBytes {
	fn dep_decode<I: NestedDecodeInput>(input: &mut I) -> Result<Self, DecodeError> {
		let size = usize::dep_decode(input)?;
		let byte_slice = input.read_slice(size)?;
		Ok(byte_slice.into())
	}

	fn dep_decode_or_exit<I: NestedDecodeInput, ExitCtx: Clone>(
		input: &mut I,
		c: ExitCtx,
		exit: fn(ExitCtx, DecodeError) -> !,
	) -> Self {
		let size = usize::dep_decode_or_exit(input, c.clone(), exit);
		let byte_slice = input.read_slice_or_exit(size, c, exit);
		byte_slice.into()
	}
}

impl TopDecode for BoxedBytes {
	fn top_decode<I: TopDecodeInput>(input: I) -> Result<Self, DecodeError> {
		Ok(BoxedBytes(input.into_boxed_slice_u8()))
	}

	fn top_decode_or_exit<I: TopDecodeInput, ExitCtx: Clone>(
		input: I,
		_: ExitCtx,
		_: fn(ExitCtx, DecodeError) -> !,
	) -> Self {
		BoxedBytes(input.into_boxed_slice_u8())
	}
}

impl TypeAbi for BoxedBytes {
	fn type_name() -> String {
		"bytes".into()
	}
}

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

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn test_concat_1() {
		let bb = BoxedBytes::from_concat(&[&b"abc"[..], &b"def"[..]]);
		assert_eq!(bb, BoxedBytes::from(&b"abcdef"[..]));
	}

	#[test]
	fn test_concat_2() {
		let bb = BoxedBytes::from_concat(&[&b"abc"[..], &b""[..], &b"def"[..]]);
		assert_eq!(bb, BoxedBytes::from(&b"abcdef"[..]));
	}

	#[test]
	fn test_concat_empty_1() {
		let bb = BoxedBytes::from_concat(&[&b""[..], &b""[..], &b""[..]]);
		assert_eq!(bb, BoxedBytes::from(&b""[..]));
	}

	#[test]
	fn test_concat_empty_2() {
		let bb = BoxedBytes::from_concat(&[]);
		assert_eq!(bb, BoxedBytes::from(&b""[..]));
	}

	#[test]
	fn test_is_empty() {
		assert!(BoxedBytes::empty().is_empty());
	}

	#[test]
	fn test_size_of() {
		use core::mem::size_of;
		assert_eq!(size_of::<BoxedBytes>(), 2 * size_of::<usize>());
		assert_eq!(size_of::<Option<BoxedBytes>>(), 2 * size_of::<usize>());
	}

	#[test]
	fn test_split_1() {
		let (bb1, bb2) = BoxedBytes::from(&b"abcdef"[..]).split(3);
		assert_eq!(bb1, BoxedBytes::from(&b"abc"[..]));
		assert_eq!(bb2, BoxedBytes::from(&b"def"[..]));
	}

	#[test]
	fn test_split_2() {
		let (bb1, bb2) = BoxedBytes::from(&b"abcdef"[..]).split(0);
		assert_eq!(bb1, BoxedBytes::from(&b""[..]));
		assert_eq!(bb2, BoxedBytes::from(&b"abcdef"[..]));
	}

	#[test]
	fn test_split_over() {
		let (bb1, bb2) = BoxedBytes::from(&b"abcdef"[..]).split(6);
		assert_eq!(bb1, BoxedBytes::from(&b"abcdef"[..]));
		assert_eq!(bb2, BoxedBytes::from(&b""[..]));

		let (bb1, bb2) = BoxedBytes::from(&b"abcdef"[..]).split(7);
		assert_eq!(bb1, BoxedBytes::from(&b"abcdef"[..]));
		assert_eq!(bb2, BoxedBytes::from(&b""[..]));
	}
}