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
// Copyright 2019 Parity Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! `BitVec` specific serialization.

use bitvec::{
	vec::BitVec, store::BitStore, order::BitOrder, slice::BitSlice, boxed::BitBox, view::BitView,
};
use crate::{
	EncodeLike, Encode, Decode, Input, Output, Error, Compact, codec::decode_vec_with_len,
};

impl<O: BitOrder, T: BitStore + Encode> Encode for BitSlice<T, O> {
	fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
		let bits = self.len();
		assert!(
			bits <= ARCH32BIT_BITSLICE_MAX_BITS,
			"Attempted to encode a BitSlice with too many bits.",
		);
		Compact(bits as u32).encode_to(dest);

		// Iterate over chunks
		for chunk in self.chunks(core::mem::size_of::<T>() * 8) {
			let mut element = T::ZERO;
			element.view_bits_mut::<O>()[..chunk.len()].copy_from_bitslice(chunk);
			element.encode_to(dest);
		}
	}
}

impl<O: BitOrder, T: BitStore + Encode> Encode for BitVec<T, O> {
	fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
		self.as_bitslice().encode_to(dest)
	}
}

impl<O: BitOrder, T: BitStore + Encode> EncodeLike for BitVec<T, O> {}

/// Equivalent of `BitStore::MAX_BITS` on 32bit machine.
const ARCH32BIT_BITSLICE_MAX_BITS: usize = 0x1fff_ffff;

impl<O: BitOrder, T: BitStore + Decode> Decode for BitVec<T, O> {
	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
		<Compact<u32>>::decode(input).and_then(move |Compact(bits)| {
			// Otherwise it is impossible to store it on 32bit machine.
			if bits as usize > ARCH32BIT_BITSLICE_MAX_BITS {
				return Err("Attempt to decode a BitVec with too many bits".into());
			}
			let vec = decode_vec_with_len(input, bitvec::mem::elts::<T>(bits as usize))?;

			let mut result = Self::try_from_vec(vec)
				.map_err(|_| {
					Error::from("UNEXPECTED ERROR: `bits` is less or equal to
					`ARCH32BIT_BITSLICE_MAX_BITS`; So BitVec must be able to handle the number of
					segment needed for `bits` to be represented; qed")
				})?;

			assert!(bits as usize <= result.len());
			result.truncate(bits as usize);
			Ok(result)
		})
	}
}

impl<O: BitOrder, T: BitStore + Encode> Encode for BitBox<T, O> {
	fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
		self.as_bitslice().encode_to(dest)
	}
}

impl<O: BitOrder, T: BitStore + Encode> EncodeLike for BitBox<T, O> {}

impl<O: BitOrder, T: BitStore + Decode> Decode for BitBox<T, O> {
	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
		Ok(BitVec::<T, O>::decode(input)?.into())
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use bitvec::{bitvec, order::{Msb0, Lsb0}};
	use crate::{codec::MAX_PREALLOCATION, CompactLen};

	macro_rules! test_data {
		($inner_type:ident) => (
			[
				BitVec::<$inner_type, Msb0>::new(),
				bitvec![$inner_type, Msb0; 0],
				bitvec![$inner_type, Msb0; 1],
				bitvec![$inner_type, Msb0; 0, 0],
				bitvec![$inner_type, Msb0; 1, 0],
				bitvec![$inner_type, Msb0; 0, 1],
				bitvec![$inner_type, Msb0; 1, 1],
				bitvec![$inner_type, Msb0; 1, 0, 1],
				bitvec![$inner_type, Msb0; 0, 1, 0, 1, 0, 1, 1],
				bitvec![$inner_type, Msb0; 0, 1, 0, 1, 0, 1, 1, 0],
				bitvec![$inner_type, Msb0; 1, 1, 0, 1, 0, 1, 1, 0, 1],
				bitvec![$inner_type, Msb0; 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0],
				bitvec![$inner_type, Msb0; 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0],
				bitvec![$inner_type, Msb0; 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0],
				bitvec![$inner_type, Msb0; 0; 15],
				bitvec![$inner_type, Msb0; 1; 16],
				bitvec![$inner_type, Msb0; 0; 17],
				bitvec![$inner_type, Msb0; 1; 31],
				bitvec![$inner_type, Msb0; 0; 32],
				bitvec![$inner_type, Msb0; 1; 33],
				bitvec![$inner_type, Msb0; 0; 63],
				bitvec![$inner_type, Msb0; 1; 64],
				bitvec![$inner_type, Msb0; 0; 65],
				bitvec![$inner_type, Msb0; 1; MAX_PREALLOCATION * 8 + 1],
				bitvec![$inner_type, Msb0; 0; MAX_PREALLOCATION * 9],
				bitvec![$inner_type, Msb0; 1; MAX_PREALLOCATION * 32 + 1],
				bitvec![$inner_type, Msb0; 0; MAX_PREALLOCATION * 33],
			]
		)
	}

	#[test]
	fn bitvec_u8() {
		for v in &test_data!(u8) {
			let encoded = v.encode();
			assert_eq!(*v, BitVec::<u8, Msb0>::decode(&mut &encoded[..]).unwrap());

			let elements = bitvec::mem::elts::<u8>(v.len());
			let compact_len = Compact::compact_len(&(v.len() as u32));
			assert_eq!(compact_len + elements, encoded.len(), "{}", v);
		}
	}

	#[test]
	fn bitvec_u16() {
		for v in &test_data!(u16) {
			let encoded = v.encode();
			assert_eq!(*v, BitVec::<u16, Msb0>::decode(&mut &encoded[..]).unwrap());

			let elements = bitvec::mem::elts::<u16>(v.len());
			let compact_len = Compact::compact_len(&(v.len() as u32));
			assert_eq!(compact_len + elements * 2, encoded.len(), "{}", v);
		}
	}

	#[test]
	fn bitvec_u32() {
		for v in &test_data!(u32) {
			let encoded = v.encode();
			assert_eq!(*v, BitVec::<u32, Msb0>::decode(&mut &encoded[..]).unwrap());

			let elements = bitvec::mem::elts::<u32>(v.len());
			let compact_len = Compact::compact_len(&(v.len() as u32));
			assert_eq!(compact_len + elements * 4, encoded.len(), "{}", v);
		}
	}

	#[test]
	fn bitvec_u64() {
		for v in &test_data!(u64) {
			let encoded = v.encode();
			assert_eq!(*v, BitVec::<u64, Msb0>::decode(&mut &encoded[..]).unwrap());

			let elements = bitvec::mem::elts::<u64>(v.len());
			let compact_len = Compact::compact_len(&(v.len() as u32));
			assert_eq!(compact_len + elements * 8, encoded.len(), "{}", v);
		}
	}

	#[test]
	fn bitslice() {
		let data: &[u8] = &[0x69];
		let slice = BitSlice::<u8, Msb0>::from_slice(data);
		let encoded = slice.encode();
		let decoded = BitVec::<u8, Msb0>::decode(&mut &encoded[..]).unwrap();
		assert_eq!(slice, decoded.as_bitslice());
	}

	#[test]
	fn bitbox() {
		let data: &[u8] = &[5, 10];
		let slice = BitSlice::<u8, Msb0>::from_slice(data);
		let bb = BitBox::<u8, Msb0>::from_bitslice(slice);
		let encoded = bb.encode();
		let decoded = BitBox::<u8, Msb0>::decode(&mut &encoded[..]).unwrap();
		assert_eq!(bb, decoded);
	}

	#[test]
	fn bitvec_u8_encodes_as_expected() {
		let cases = vec![
			(bitvec![u8, Lsb0; 0, 0, 1, 1].encode(), (Compact(4u32), 0b00001100u8).encode()),
			(bitvec![u8, Lsb0; 0, 1, 1, 1].encode(), (Compact(4u32), 0b00001110u8).encode()),
			(bitvec![u8, Lsb0; 1, 1, 1, 1].encode(), (Compact(4u32), 0b00001111u8).encode()),
			(bitvec![u8, Lsb0; 1, 1, 1, 1, 1].encode(), (Compact(5u32), 0b00011111u8).encode()),
			(bitvec![u8, Lsb0; 1, 1, 1, 1, 1, 0].encode(), (Compact(6u32), 0b00011111u8).encode()),
			(bitvec![u8, Lsb0; 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1].encode(), (Compact(12u32), 0b00011111u8, 0b00001011u8).encode()),
		];

		for (idx, (actual, expected)) in cases.into_iter().enumerate() {
			assert_eq!(actual, expected, "case at index {} failed; encodings differ", idx);
		}
	}
}