-
Notifications
You must be signed in to change notification settings - Fork 42
/
sg.rs
310 lines (269 loc) · 7.75 KB
/
sg.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
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
//! Scattered-gathered buffers
//!
use DIGEST_SIZE;
use DataType;
use crypto::digest::Digest;
use flate2;
use owning_ref::ArcRef;
use rollsum;
use sha2;
use sodiumoxide::crypto::box_;
use std::{io, mem};
use std::io::Write;
use std::ops::{Deref, DerefMut};
use std::result::Result;
use std::sync::Arc;
pub trait EdgeFinder {
fn find_edges(&mut self, buf: &[u8]) -> Vec<usize>;
}
/// Finds edges using rolling sum
pub struct BupEdgeFinder {
chunk_bits: u32,
roll: rollsum::Bup,
}
impl BupEdgeFinder {
pub fn new(chunk_bits: u32) -> Self {
BupEdgeFinder {
chunk_bits: chunk_bits,
roll: rollsum::Bup::new_with_chunk_bits(chunk_bits),
}
}
}
impl EdgeFinder for BupEdgeFinder {
fn find_edges(&mut self, buf: &[u8]) -> Vec<usize> {
let mut ofs: usize = 0;
let len = buf.len();
let mut edges = vec![];
while ofs < len {
if let Some(count) = self.roll.find_chunk_edge(&buf[ofs..len]) {
ofs += count;
self.roll = rollsum::Bup::new_with_chunk_bits(self.chunk_bits);
edges.push(ofs);
} else {
break;
}
}
edges
}
}
pub struct ReaderVecIter<R: io::Read> {
reader: R,
buf_size: usize,
}
impl<R> ReaderVecIter<R>
where R: io::Read
{
pub fn new(reader: R, buf_size: usize) -> Self {
ReaderVecIter {
reader: reader,
buf_size: buf_size,
}
}
}
impl<R> Iterator for ReaderVecIter<R>
where R: io::Read
{
type Item = io::Result<Vec<u8>>;
fn next(&mut self) -> Option<Self::Item> {
let mut buf: Vec<u8> = vec![0u8; self.buf_size];
match self.reader.read(&mut buf) {
Ok(len) => {
if len == 0 {
return None;
}
buf.truncate(len);
Some(Ok(buf))
}
Err(e) => Some(Err(e)),
}
}
}
pub struct WhileOk<I, E> {
e: Option<E>,
i: I,
}
impl<I, E> WhileOk<I, E> {
pub fn new<O>(into_iter: I) -> WhileOk<I, E>
where I: Iterator<Item = Result<O, E>>
{
WhileOk {
e: None,
i: into_iter.into_iter(),
}
}
}
impl<I, O, E> Iterator for WhileOk<I, E>
where I: Iterator<Item = Result<O, E>>
{
type Item = O;
fn next(&mut self) -> Option<Self::Item> {
if self.e.is_some() {
return None;
}
match self.i.next() {
Some(Ok(o)) => Some(o),
Some(Err(e)) => {
self.e = Some(e);
None
}
None => None,
}
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct Chunk {
sg: SGBuf,
chunk_type: DataType,
data_type: DataType,
}
/// Scattered-gathered buffer
///
/// A piece of data potentially scattered between
/// multiple buffers.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct SGBuf(Vec<ArcRef<Vec<u8>, [u8]>>);
impl SGBuf {
fn new() -> Self {
SGBuf(vec![])
}
pub fn from_single(v: Vec<u8>) -> Self {
SGBuf::from_vec(vec![v])
}
fn from_vec(mut v: Vec<Vec<u8>>) -> Self {
SGBuf(v.drain(..)
.map(|v| ArcRef::new(Arc::new(v)).map(|v| &v[..]))
.collect())
}
pub fn total_len(&self) -> usize {
let mut size = 0;
for sg_part in &self.0 {
size += sg_part.len();
}
size
}
/// Calculate digest
pub fn calculate_digest(&self) -> Vec<u8> {
let mut sha = sha2::Sha256::new();
for sg_part in &self.0 {
sha.input(sg_part);
}
let mut sha256 = vec![0u8; DIGEST_SIZE];
sha.result(&mut sha256);
sha256
}
pub fn compress(&self) -> SGBuf {
let mut compressor =
flate2::write::DeflateEncoder::new(
Vec::with_capacity(self.total_len()),
flate2::Compression::Default);
for sg_part in &self.0 {
compressor.write_all(sg_part).unwrap();
}
SGBuf::from_single(compressor.finish().unwrap())
}
fn to_linear(&self) -> ArcRef<Vec<u8>, [u8]> {
match self.0.len() {
0 => ArcRef::new(Arc::new(vec![])).map(|v| &v[..]),
1 => self.0[0].clone(),
_ => {
let mut v = Vec::with_capacity(self.total_len());
for sg_part in &self.0 {
v.write_all(sg_part).unwrap();
}
ArcRef::new(Arc::new(v)).map(|v| &v[..])
}
}
}
pub fn encrypt(&self, pub_key: &box_::PublicKey, digest: &[u8]) -> SGBuf {
let nonce =
box_::Nonce::from_slice(digest).expect("Nonce::from_slice failed");
let (ephemeral_pub, ephemeral_sec) = box_::gen_keypair();
let cipher =
box_::seal(&self.to_linear(), &nonce, pub_key, &ephemeral_sec);
SGBuf::from_vec(vec![ephemeral_pub.0.to_vec(), cipher])
}
}
impl Deref for SGBuf {
type Target = Vec<ArcRef<Vec<u8>, [u8]>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for SGBuf {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
pub struct Chunker<I, EF> {
iter: I,
cur_buf_edges: Option<(Arc<Vec<u8>>, Vec<usize>)>,
cur_buf_i: usize,
cur_edge_i: usize,
cur_sgbuf: SGBuf,
chunks_returned: usize,
edge_finder: EF,
}
impl<I, EF> Chunker<I, EF> {
pub fn new(iter: I, edge_finder: EF) -> Self {
Chunker {
iter: iter,
cur_buf_edges: None,
cur_buf_i: 0,
cur_edge_i: 0,
cur_sgbuf: SGBuf::new(),
chunks_returned: 0,
edge_finder: edge_finder,
}
}
}
impl<I: Iterator<Item = Vec<u8>>, EF> Iterator for Chunker<I, EF>
where EF: EdgeFinder
{
type Item = SGBuf;
fn next(&mut self) -> Option<Self::Item> {
loop {
self.cur_buf_edges = if let Some((buf, edges)) =
self.cur_buf_edges.clone() {
if self.cur_edge_i < edges.len() {
let edge = edges[self.cur_edge_i];
let aref =
ArcRef::new(buf.clone()).map(|a| {
&a[self.cur_buf_i..
edge]
});
self.cur_sgbuf.push(aref);
self.cur_edge_i += 1;
self.cur_buf_i = edge;
self.chunks_returned += 1;
return Some(mem::replace(&mut self.cur_sgbuf,
SGBuf::new()));
} else {
if self.cur_buf_i != buf.len() {
let aref = ArcRef::new(buf.clone())
.map(|a| &a[self.cur_buf_i..]);
self.cur_sgbuf.push(aref);
}
self.cur_buf_i = 0;
None
}
} else if let Some(buf) = self.iter.next() {
self.cur_edge_i = 0;
let edges = self.edge_finder.find_edges(&buf[..]);
Some((Arc::new(buf), edges))
} else if self.cur_sgbuf.is_empty() {
if self.chunks_returned == 0 {
// at least one, zero sized chunk
self.chunks_returned += 1;
return Some(SGBuf::new());
} else {
return None;
}
} else {
self.chunks_returned += 1;
return Some(mem::replace(&mut self.cur_sgbuf, SGBuf::new()));
}
}
}
}
#[cfg(test)]
include!("sg_tests.rs");