1#[macro_use]
17extern crate static_assertions_next;
18
19mod decode;
20mod encode;
21pub mod hash;
22mod index;
23pub mod match_bytes;
24pub mod varint;
25
26type DefaultHash = hash::CyclicPoly32;
27
28const COPY_CMD_FLAG: u8 = 0x80;
29const ZERO_COPY_LEN: u32 = 0x10000;
30
31pub use decode::DiffDecodeError;
32pub use encode::get_delta_len;
33pub use encode::write_delta_to;
34pub use hash::RollingHash;
35pub use index::DeltaIndex;
36pub use index::create_delta_index_custom;
37
38pub fn write_delta_to_vec<H: RollingHash>(
39 base: &[u8],
40 index: &DeltaIndex<H>,
41 target: &[u8],
42 max_output_len: usize,
43) -> Option<Vec<u8>> {
44 const MAX_INITIAL_CAP: usize = 8192; let mut initial_cap = MAX_INITIAL_CAP.min(target.len() / 2);
46 let max_op_size = 5 + 5 + 1 + H::WINDOW + 7;
47 if initial_cap >= max_output_len {
48 initial_cap = max_output_len + max_op_size + 1;
49 }
50
51 let mut output = Vec::with_capacity(max_output_len.min(initial_cap));
52 write_delta_to(base, index, target, max_output_len, &mut output)?;
53 debug_assert!(output.len() <= max_output_len);
54 #[cfg(debug_assertions)]
55 {
56 let len = get_delta_len(base, index, target, max_output_len).unwrap();
57 assert_eq!(output.len(), len);
58 }
59 Some(output)
60}
61
62pub fn create_delta_index(base: &[u8]) -> Option<DeltaIndex<DefaultHash>> {
63 create_delta_index_custom::<DefaultHash>(base)
64}
65
66pub fn create_delta(base: &[u8], target: &[u8]) -> Option<Vec<u8>> {
74 if target.len() < 18 {
75 return None;
76 }
77 let index = create_delta_index(base)?;
78 let _max_size = target.len() - 1;
79 write_delta_to_vec(base, &index, target, usize::MAX)
80}
81
82pub fn patch_delta(base: &[u8], delta: &[u8]) -> Result<Vec<u8>, DiffDecodeError> {
91 let mut data = vec![];
92 decode::decode(base, delta, &mut data)?;
93 Ok(data)
94}
95
96#[cfg(test)]
97mod tests {
98 use super::*;
99
100 const SAMPLE_DATA: &str = include_str!("../tests/data/diff-delta.c");
101
102 #[test]
103 fn test_round_trip_rabin() {
104 test_round_trip::<hash::GitRabin>(130);
105 }
106
107 #[test]
108 fn test_round_trip_cyclic_poly32() {
109 test_round_trip::<hash::CyclicPoly32>(130);
110 }
111
112 #[test]
113 fn test_round_trip_cyclic_poly64() {
114 test_round_trip::<hash::CyclicPoly64>(130);
115 }
116
117 #[test]
118 fn example1() {
119 let base = "This is a pure rust implementation of Git's delta compression algorithm.";
120 let target = base.replace("Git", "git");
121 let delta = create_delta(base.as_bytes(), target.as_bytes()).unwrap();
122 assert_eq!(delta.len(), 9);
123 let target2 = patch_delta(base.as_bytes(), &delta).unwrap();
124 assert_eq!(target.as_bytes(), target2);
125 }
126
127 fn test_round_trip<R: RollingHash>(exp: usize) {
128 let base = SAMPLE_DATA.as_bytes();
129 let target = SAMPLE_DATA.to_owned().replace("RABIN", "ROBIN");
130 let target = target.as_bytes();
131
132 let index = create_delta_index_custom::<R>(base).unwrap();
133 let delta = write_delta_to_vec(base, &index, target, usize::MAX).unwrap();
134 let decoded = patch_delta(base, &delta).unwrap();
135 assert_eq!(decoded, target);
136 assert_eq!(delta.len(), exp);
137 }
138}