sqlite_compressions/
bsdiff4.rs1use std::io::Cursor;
2
3use qbsdiff::bsdiff::Bsdiff;
4use qbsdiff::bspatch::Bspatch;
5use rusqlite::Error::UserFunctionError;
6
7use crate::common_diff::{register_differ, Differ};
8use crate::rusqlite::{Connection, Result};
9
10pub fn register_bsdiff4_functions(conn: &Connection) -> Result<()> {
30 register_differ::<Bsdiff4Differ>(conn)
31}
32
33pub struct Bsdiff4Differ;
34
35impl Differ for Bsdiff4Differ {
36 fn diff_name() -> &'static str {
37 "bsdiff4"
38 }
39
40 fn patch_name() -> &'static str {
41 "bspatch4"
42 }
43
44 fn diff(source: &[u8], target: &[u8]) -> Result<Vec<u8>> {
45 let mut patch = Vec::new();
46 Bsdiff::new(source, target)
47 .compare(Cursor::new(&mut patch))
48 .map_err(|e| UserFunctionError(e.into()))?;
49 Ok(patch)
50 }
51
52 fn patch(source: &[u8], patch: &[u8]) -> Result<Vec<u8>> {
53 let mut target = Vec::new();
54 Bspatch::new(patch)
55 .and_then(|patch| patch.apply(source, Cursor::new(&mut target)))
56 .map_err(|e| UserFunctionError(e.into()))?;
57 Ok(target)
58 }
59}