| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
use std::{cmp::min, mem}; |
| 8 |
|
| 9 |
use crate::codec::Decoder; |
| 10 |
|
| 11 |
#[derive(Clone, Debug, Default)] |
| 12 |
pub struct IncrementalDecoderUint { |
| 13 |
v: u64, |
| 14 |
remaining: Option<usize>, |
| 15 |
} |
| 16 |
|
| 17 |
impl IncrementalDecoderUint { |
| 18 |
#[must_use] |
| 19 |
pub fn min_remaining(&self) -> usize { |
| 20 |
self.remaining.unwrap_or(1) |
| 21 |
} |
| 22 |
|
| 23 |
/// Consume some data. |
| 24 |
/// |
| 25 |
/// # Panics |
| 26 |
/// |
| 27 |
/// Never, but this is not something the compiler can tell. |
| 28 |
pub fn consume(&mut self, dv: &mut Decoder) -> Option<u64> { |
| 29 |
if let Some(r) = &mut self.remaining { |
| 30 |
let amount = min(*r, dv.remaining()); |
| 31 |
if amount < 8 { |
| 32 |
self.v <<= amount * 8; |
| 33 |
} |
| 34 |
self.v |= dv.decode_n(amount)?; |
| 35 |
*r -= amount; |
| 36 |
(*r == 0).then_some(self.v) |
| 37 |
} else { |
| 38 |
let (v, remaining) = dv.decode_uint::<u8>().map_or_else( |
| 39 |
|| unreachable!(), |
| 40 |
|b| { |
| 41 |
( |
| 42 |
u64::from(b & 0x3f), |
| 43 |
match b >> 6 { |
| 44 |
0 => 0, |
| 45 |
1 => 1, |
| 46 |
2 => 3, |
| 47 |
3 => 7, |
| 48 |
_ => unreachable!(), |
| 49 |
}, |
| 50 |
) |
| 51 |
}, |
| 52 |
); |
| 53 |
self.remaining = Some(remaining); |
| 54 |
self.v = v; |
| 55 |
(remaining == 0).then_some(v) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
#[must_use] |
| 60 |
pub const fn decoding_in_progress(&self) -> bool { |
| 61 |
self.remaining.is_some() |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
#[derive(Clone, Debug)] |
| 66 |
pub struct IncrementalDecoderBuffer { |
| 67 |
v: Vec<u8>, |
| 68 |
remaining: usize, |
| 69 |
} |
| 70 |
|
| 71 |
impl IncrementalDecoderBuffer { |
| 72 |
#[must_use] |
| 73 |
pub const fn new(n: usize) -> Self { |
| 74 |
Self { |
| 75 |
v: Vec::new(), |
| 76 |
remaining: n, |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
#[must_use] |
| 81 |
pub const fn min_remaining(&self) -> usize { |
| 82 |
self.remaining |
| 83 |
} |
| 84 |
|
| 85 |
/// Consume some bytes from the decoder. |
| 86 |
/// |
| 87 |
/// # Panics |
| 88 |
/// |
| 89 |
/// Never; but rust doesn't know that. |
| 90 |
pub fn consume(&mut self, dv: &mut Decoder) -> Option<Vec<u8>> { |
| 91 |
let amount = min(self.remaining, dv.remaining()); |
| 92 |
let b = dv.decode(amount)?; |
| 93 |
self.v.extend_from_slice(b); |
| 94 |
self.remaining -= amount; |
| 95 |
(self.remaining == 0).then(|| mem::take(&mut self.v)) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
#[derive(Clone, Debug)] |
| 100 |
pub struct IncrementalDecoderIgnore { |
| 101 |
remaining: usize, |
| 102 |
} |
| 103 |
|
| 104 |
impl IncrementalDecoderIgnore { |
| 105 |
/// Make a new ignoring decoder. |
| 106 |
/// |
| 107 |
/// # Panics |
| 108 |
/// |
| 109 |
/// If the amount to ignore is zero. |
| 110 |
#[must_use] |
| 111 |
pub fn new(n: usize) -> Self { |
| 112 |
assert_ne!(n, 0); |
| 113 |
Self { remaining: n } |
| 114 |
} |
| 115 |
|
| 116 |
#[must_use] |
| 117 |
pub const fn min_remaining(&self) -> usize { |
| 118 |
self.remaining |
| 119 |
} |
| 120 |
|
| 121 |
pub fn consume(&mut self, dv: &mut Decoder) -> bool { |
| 122 |
let amount = min(self.remaining, dv.remaining()); |
| 123 |
_ = dv.decode(amount); |
| 124 |
self.remaining -= amount; |
| 125 |
self.remaining == 0 |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
#[cfg(test)] |
| 130 |
mod tests { |
| 131 |
use super::{ |
| 132 |
Decoder, IncrementalDecoderBuffer, IncrementalDecoderIgnore, IncrementalDecoderUint, |
| 133 |
}; |
| 134 |
use crate::codec::Encoder; |
| 135 |
|
| 136 |
#[test] |
| 137 |
fn buffer_incremental() { |
| 138 |
let b = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 139 |
let mut dec = IncrementalDecoderBuffer::new(b.len()); |
| 140 |
let mut i = 0; |
| 141 |
while i < b.len() { |
| 142 |
// Feed in b in increasing-sized chunks. |
| 143 |
let incr = if i < b.len() / 2 { i + 1 } else { b.len() - i }; |
| 144 |
let mut dv = Decoder::from(&b[i..i + incr]); |
| 145 |
i += incr; |
| 146 |
match dec.consume(&mut dv) { |
| 147 |
None => { |
| 148 |
assert!(i < b.len()); |
| 149 |
} |
| 150 |
Some(res) => { |
| 151 |
assert_eq!(i, b.len()); |
| 152 |
assert_eq!(res, b); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
struct UintTestCase { |
| 159 |
b: String, |
| 160 |
v: u64, |
| 161 |
} |
| 162 |
|
| 163 |
impl UintTestCase { |
| 164 |
pub fn run(&self) { |
| 165 |
eprintln!( |
| 166 |
"IncrementalDecoderUint decoder with {:?} ; expect {:?}", |
| 167 |
self.b, self.v |
| 168 |
); |
| 169 |
|
| 170 |
let decoder = IncrementalDecoderUint::default(); |
| 171 |
let mut db = Encoder::from_hex(&self.b); |
| 172 |
// Add padding so that we can verify that the reader doesn't over-consume. |
| 173 |
db.encode_byte(0xff); |
| 174 |
|
| 175 |
for tail in 1..db.len() { |
| 176 |
let split = db.len() - tail; |
| 177 |
let mut dv = Decoder::from(&db.as_ref()[0..split]); |
| 178 |
eprintln!(" split at {split}: {dv:?}"); |
| 179 |
|
| 180 |
// Clone the basic decoder for each iteration of the loop. |
| 181 |
let mut dec = decoder.clone(); |
| 182 |
let mut res = None; |
| 183 |
while dv.remaining() > 0 { |
| 184 |
res = dec.consume(&mut dv); |
| 185 |
} |
| 186 |
assert!(dec.min_remaining() < tail); |
| 187 |
|
| 188 |
if tail > 1 { |
| 189 |
assert_eq!(res, None); |
| 190 |
assert!(dec.min_remaining() > 0); |
| 191 |
let mut dv = Decoder::from(&db.as_ref()[split..]); |
| 192 |
eprintln!(" split remainder {split}: {dv:?}"); |
| 193 |
res = dec.consume(&mut dv); |
| 194 |
assert_eq!(dv.remaining(), 1); |
| 195 |
} |
| 196 |
|
| 197 |
assert_eq!(dec.min_remaining(), 0); |
| 198 |
assert_eq!(res.unwrap(), self.v); |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
macro_rules! uint_tc { |
| 204 |
[$( $b:expr => $v:expr ),+ $(,)?] => { |
| 205 |
vec![ $( UintTestCase { b: String::from($b), v: $v, } ),+] |
| 206 |
}; |
| 207 |
} |
| 208 |
|
| 209 |
#[test] |
| 210 |
fn varint() { |
| 211 |
for c in uint_tc![ |
| 212 |
"00" => 0, |
| 213 |
"01" => 1, |
| 214 |
"3f" => 63, |
| 215 |
"4040" => 64, |
| 216 |
"7fff" => 16383, |
| 217 |
"80004000" => 16384, |
| 218 |
"bfffffff" => (1 << 30) - 1, |
| 219 |
"c000000040000000" => 1 << 30, |
| 220 |
"ffffffffffffffff" => (1 << 62) - 1, |
| 221 |
] { |
| 222 |
c.run(); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
#[test] |
| 227 |
fn zero_len() { |
| 228 |
let enc = Encoder::from_hex("ff"); |
| 229 |
let mut dec = Decoder::new(enc.as_ref()); |
| 230 |
let mut incr = IncrementalDecoderBuffer::new(0); |
| 231 |
assert_eq!(incr.consume(&mut dec), Some(Vec::new())); |
| 232 |
assert_eq!(dec.remaining(), enc.len()); |
| 233 |
} |
| 234 |
|
| 235 |
#[test] |
| 236 |
fn ignore() { |
| 237 |
let db = Encoder::from_hex("12345678ff"); |
| 238 |
|
| 239 |
let decoder = IncrementalDecoderIgnore::new(4); |
| 240 |
|
| 241 |
for tail in 1..db.len() { |
| 242 |
let split = db.len() - tail; |
| 243 |
let mut dv = Decoder::from(&db.as_ref()[0..split]); |
| 244 |
eprintln!(" split at {split}: {dv:?}"); |
| 245 |
|
| 246 |
// Clone the basic decoder for each iteration of the loop. |
| 247 |
let mut dec = decoder.clone(); |
| 248 |
let mut res = dec.consume(&mut dv); |
| 249 |
assert_eq!(dv.remaining(), 0); |
| 250 |
assert!(dec.min_remaining() < tail); |
| 251 |
|
| 252 |
if tail > 1 { |
| 253 |
assert!(!res); |
| 254 |
assert!(dec.min_remaining() > 0); |
| 255 |
let mut dv = Decoder::from(&db.as_ref()[split..]); |
| 256 |
eprintln!(" split remainder {split}: {dv:?}"); |
| 257 |
res = dec.consume(&mut dv); |
| 258 |
assert_eq!(dv.remaining(), 1); |
| 259 |
} |
| 260 |
|
| 261 |
assert_eq!(dec.min_remaining(), 0); |
| 262 |
assert!(res); |
| 263 |
} |
| 264 |
} |
| 265 |
} |