Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unsafe constructs #3

Merged
merged 8 commits into from
Nov 26, 2019
1 change: 1 addition & 0 deletions cmd/qbsdiff.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![forbid(unsafe_code)]
use qbsdiff::Bsdiff;
use std::fs::File;
use std::io;
Expand Down
1 change: 1 addition & 0 deletions cmd/qbspatch.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![forbid(unsafe_code)]
use qbsdiff::Bspatch;
use std::fs::File;
use std::io;
Expand Down
20 changes: 11 additions & 9 deletions src/bsdiff.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![forbid(unsafe_code)]
use super::utils::*;
use bzip2::write::BzEncoder;
use std::io::{Cursor, Result, Write};
Expand Down Expand Up @@ -156,10 +157,9 @@ where
let mut spos = 0;
let mut tpos = 0;
let mut cbuf = [0; 24];
let mut dbuf = Vec::with_capacity(bsize);
unsafe {
dbuf.set_len(bsize);
}

let mut dat = Vec::with_capacity(bsize);

for ctl in diff {
// Write control data.
encode_int(ctl.add as i64, &mut cbuf[0..8]);
Expand All @@ -173,11 +173,13 @@ where
while n > 0 {
let k = Ord::min(n, bsize as u64) as usize;

let dat = Iterator::zip(s[spos as usize..].iter(), t[tpos as usize..].iter());
for (d, (&x, &y)) in Iterator::zip(dbuf[..k].iter_mut(), dat) {
*d = y.wrapping_sub(x)
}
delta.write_all(&dbuf[..k])?;
dat.extend(Iterator::zip(
s[spos as usize..].iter(),
t[tpos as usize..].iter()
).map(|(x, y)| y.wrapping_sub(*x)).take(k));

delta.write_all(&dat[..])?;
dat.clear();

spos += k as u64;
tpos += k as u64;
Expand Down
19 changes: 5 additions & 14 deletions src/bspatch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![forbid(unsafe_code)]
use super::utils::*;
use bzip2::read::BzDecoder;
use std::io::{Cursor, Error, ErrorKind, Read, Result, Seek, SeekFrom, Write};
use std::iter;

/// Default buffer size.
pub const BUFFER_SIZE: usize = 16384;
Expand Down Expand Up @@ -169,22 +171,15 @@ where
bsize: usize,
dsize: usize,
) -> Self {
let mut buf = Vec::with_capacity(bsize);
let mut dlt = Vec::with_capacity(dsize);
unsafe {
buf.set_len(bsize);
dlt.set_len(dsize);
}

Context {
source: Cursor::new(source),
target,
ctrls,
delta,
extra,
n: 0,
buf,
dlt,
buf: vec![0; bsize],
dlt: vec![0; dsize],
ctl: [0; 24],
total: 0,
}
Expand Down Expand Up @@ -272,11 +267,7 @@ where
/// Extend the delta cache if not large enough.
fn reserve_delta(&mut self, size: usize) {
if size > self.dlt.len() {
let n = size - self.dlt.len();
self.dlt.reserve(n);
unsafe {
self.dlt.set_len(size);
}
self.dlt.resize(size, 0);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Note that `qbsdiff` would not generate exactly the same patch file as `bsdiff`.
Only the patch file format is promised to be compatible.
*/

#![forbid(unsafe_code)]
pub mod bsdiff;
pub mod bspatch;
mod utils;
Expand Down
1 change: 1 addition & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![forbid(unsafe_code)]
use byteorder::{ByteOrder, LE};

/// Single bsdiff control instruction.
Expand Down
Empty file modified tests/bin/bsdiff
100644 → 100755
Empty file.
Empty file modified tests/bin/bspatch
100644 → 100755
Empty file.