-
Notifications
You must be signed in to change notification settings - Fork 6
/
lib.rs
98 lines (81 loc) · 2.18 KB
/
lib.rs
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
#![cfg_attr(feature = "nightly", deny(missing_docs))]
#![cfg_attr(feature = "nightly", feature(external_doc))]
#![cfg_attr(feature = "nightly", doc(include = "../README.md"))]
#![cfg_attr(test, deny(warnings))]
#[macro_use]
extern crate failure;
extern crate varinteger as varint;
use failure::Error;
use std::convert::AsRef;
/// Result type.
pub type Result<T> = std::result::Result<T, Error>;
/// Returns how many bytes are needed to encode the bitfield.
pub fn encode_len(_buf: &[u8]) -> usize {
unimplemented!();
}
/// Returns how many bytes a decoded bitfield will use.
pub fn decode_len(_buf: &[u8]) -> usize {
unimplemented!();
}
/// Encode a bitfield.
pub fn encode(reader: impl AsRef<Vec<u8>>) -> Vec<u8> {
let offset = 0;
encode_with_offset(reader, offset)
}
/// Encode a bitfield at a specific offset
pub fn encode_with_offset(
reader: impl AsRef<Vec<u8>>,
offset: usize,
) -> Vec<u8> {
unimplemented!();
}
/// Decode an encoded bitfield.
pub fn decode(buf: impl AsRef<Vec<u8>>) -> Result<(Vec<u8>, usize)> {
let _reader = buf.as_ref();
let output = Vec::new();
let len = decode_with_offset(&output, 0)?;
Ok((output, len))
}
/// Decode an encoded bitfield at a specific offset.
pub fn decode_with_offset(
buf: impl AsRef<Vec<u8>>,
offset: usize,
) -> Result<usize> {
let buf = buf.as_ref();
let _len = decoding_len_with_offset(&buf, offset)?;
unimplemented!();
}
/// Returns how many bytes a decoded bitfield will use.
pub fn decoding_len(buf: impl AsRef<Vec<u8>>) -> Result<usize> {
decoding_len_with_offset(&buf, 0)
}
/// Returns how many bytes a decoded bitfield will use at a specific offset.
pub fn decoding_len_with_offset(
buf: impl AsRef<Vec<u8>>,
mut offset: usize,
) -> Result<usize> {
let buf = buf.as_ref();
let mut len = 0;
let mut val = 0u64;
while offset < buf.len() {
let next = varint::decode(buf, &mut val);
offset += next;
let repeat = next & 1;
let slice = if repeat > 0 {
next - (next & 3) / 4
} else {
next / 2
};
len += slice;
if repeat == 0 {
offset += slice;
}
}
ensure!(
!offset > buf.len(),
"Invalid RLE bitfield {} > {}",
offset,
buf.len()
);
Ok(len)
}