iscc-lib 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +2027 -0
- data/Cargo.toml +17 -6
- data/README.md +6 -3
- data/core/CLAUDE.md +177 -0
- data/core/Cargo.lock +959 -0
- data/core/Cargo.toml +131 -0
- data/core/README.md +84 -0
- data/core/benches/benchmarks.rs +294 -0
- data/core/benches/iai_benches.rs +171 -0
- data/core/src/cdc.rs +488 -0
- data/core/src/codec.rs +2074 -0
- data/core/src/conformance.rs +754 -0
- data/core/src/dct.rs +148 -0
- data/core/src/lib.rs +2693 -0
- data/core/src/minhash.rs +286 -0
- data/core/src/simhash.rs +356 -0
- data/core/src/streaming.rs +688 -0
- data/core/src/types.rs +116 -0
- data/core/src/utils.rs +361 -0
- data/core/src/wtahash.rs +389 -0
- data/core/src/xxh32.rs +134 -0
- data/core/tests/codec_clean.rs +247 -0
- data/core/tests/data.json +645 -0
- data/core/tests/test_algorithm_primitives.rs +303 -0
- data/core/tests/test_seq.rs +565 -0
- data/core/tests/test_strict_base32.rs +158 -0
- data/core/tests/test_text_utils.rs +157 -0
- data/lib/iscc_lib/version.rb +1 -1
- data/src/lib.rs +108 -25
- metadata +43 -4
data/core/src/cdc.rs
ADDED
|
@@ -0,0 +1,488 @@
|
|
|
1
|
+
//! Content-Defined Chunking (CDC) for similarity-preserving data splitting.
|
|
2
|
+
//!
|
|
3
|
+
//! Implements the FastCDC-inspired gear rolling hash algorithm from iscc-core.
|
|
4
|
+
//! Splits byte data into variable-size chunks with content-dependent boundaries,
|
|
5
|
+
//! enabling similarity detection across different versions of binary data.
|
|
6
|
+
|
|
7
|
+
use crate::{IsccError, IsccResult};
|
|
8
|
+
|
|
9
|
+
/// Gear rolling hash lookup table (256 entries).
|
|
10
|
+
///
|
|
11
|
+
/// Fixed constant from iscc-core `cdc_gear` option. Each byte value maps to
|
|
12
|
+
/// a pseudo-random u32 used in the rolling hash for chunk boundary detection.
|
|
13
|
+
const CDC_GEAR: [u32; 256] = [
|
|
14
|
+
1553318008, 574654857, 759734804, 310648967, 1393527547, 1195718329, 694400241, 1154184075,
|
|
15
|
+
1319583805, 1298164590, 122602963, 989043992, 1918895050, 933636724, 1369634190, 1963341198,
|
|
16
|
+
1565176104, 1296753019, 1105746212, 1191982839, 1195494369, 29065008, 1635524067, 722221599,
|
|
17
|
+
1355059059, 564669751, 1620421856, 1100048288, 1018120624, 1087284781, 1723604070, 1415454125,
|
|
18
|
+
737834957, 1854265892, 1605418437, 1697446953, 973791659, 674750707, 1669838606, 320299026,
|
|
19
|
+
1130545851, 1725494449, 939321396, 748475270, 554975894, 1651665064, 1695413559, 671470969,
|
|
20
|
+
992078781, 1935142196, 1062778243, 1901125066, 1935811166, 1644847216, 744420649, 2068980838,
|
|
21
|
+
1988851904, 1263854878, 1979320293, 111370182, 817303588, 478553825, 694867320, 685227566,
|
|
22
|
+
345022554, 2095989693, 1770739427, 165413158, 1322704750, 46251975, 710520147, 700507188,
|
|
23
|
+
2104251000, 1350123687, 1593227923, 1756802846, 1179873910, 1629210470, 358373501, 807118919,
|
|
24
|
+
751426983, 172199468, 174707988, 1951167187, 1328704411, 2129871494, 1242495143, 1793093310,
|
|
25
|
+
1721521010, 306195915, 1609230749, 1992815783, 1790818204, 234528824, 551692332, 1930351755,
|
|
26
|
+
110996527, 378457918, 638641695, 743517326, 368806918, 1583529078, 1767199029, 182158924,
|
|
27
|
+
1114175764, 882553770, 552467890, 1366456705, 934589400, 1574008098, 1798094820, 1548210079,
|
|
28
|
+
821697741, 601807702, 332526858, 1693310695, 136360183, 1189114632, 506273277, 397438002,
|
|
29
|
+
620771032, 676183860, 1747529440, 909035644, 142389739, 1991534368, 272707803, 1905681287,
|
|
30
|
+
1210958911, 596176677, 1380009185, 1153270606, 1150188963, 1067903737, 1020928348, 978324723,
|
|
31
|
+
962376754, 1368724127, 1133797255, 1367747748, 1458212849, 537933020, 1295159285, 2104731913,
|
|
32
|
+
1647629177, 1691336604, 922114202, 170715530, 1608833393, 62657989, 1140989235, 381784875,
|
|
33
|
+
928003604, 449509021, 1057208185, 1239816707, 525522922, 476962140, 102897870, 132620570,
|
|
34
|
+
419788154, 2095057491, 1240747817, 1271689397, 973007445, 1380110056, 1021668229, 12064370,
|
|
35
|
+
1186917580, 1017163094, 597085928, 2018803520, 1795688603, 1722115921, 2015264326, 506263638,
|
|
36
|
+
1002517905, 1229603330, 1376031959, 763839898, 1970623926, 1109937345, 524780807, 1976131071,
|
|
37
|
+
905940439, 1313298413, 772929676, 1578848328, 1108240025, 577439381, 1293318580, 1512203375,
|
|
38
|
+
371003697, 308046041, 320070446, 1252546340, 568098497, 1341794814, 1922466690, 480833267,
|
|
39
|
+
1060838440, 969079660, 1836468543, 2049091118, 2023431210, 383830867, 2112679659, 231203270,
|
|
40
|
+
1551220541, 1377927987, 275637462, 2110145570, 1700335604, 738389040, 1688841319, 1506456297,
|
|
41
|
+
1243730675, 258043479, 599084776, 41093802, 792486733, 1897397356, 28077829, 1520357900,
|
|
42
|
+
361516586, 1119263216, 209458355, 45979201, 363681532, 477245280, 2107748241, 601938891,
|
|
43
|
+
244572459, 1689418013, 1141711990, 1485744349, 1181066840, 1950794776, 410494836, 1445347454,
|
|
44
|
+
2137242950, 852679640, 1014566730, 1999335993, 1871390758, 1736439305, 231222289, 603972436,
|
|
45
|
+
783045542, 370384393, 184356284, 709706295, 1453549767, 591603172, 768512391, 854125182,
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
/// Default average chunk size in bytes for Data-Code generation.
|
|
49
|
+
pub(crate) const DATA_AVG_CHUNK_SIZE: u32 = 1024;
|
|
50
|
+
|
|
51
|
+
/// Calculate CDC parameters from target average chunk size.
|
|
52
|
+
///
|
|
53
|
+
/// Returns `(min_size, max_size, center_size, mask_s, mask_l)` where:
|
|
54
|
+
/// - `min_size`: minimum chunk size (avg/4)
|
|
55
|
+
/// - `max_size`: maximum chunk size (avg*8)
|
|
56
|
+
/// - `center_size`: threshold between strict and relaxed mask phases
|
|
57
|
+
/// - `mask_s`: strict mask for early boundary detection (harder to match)
|
|
58
|
+
/// - `mask_l`: relaxed mask for late boundary detection (easier to match)
|
|
59
|
+
///
|
|
60
|
+
/// Matches the reference for every `avg_size >= 2`: `max_size` is computed in
|
|
61
|
+
/// `u64` and saturates at `usize::MAX`, and a mask of 32 or more bits saturates
|
|
62
|
+
/// at `u32::MAX` (see [`low_bits_mask`]). Callers must pass `avg_size >= 2`.
|
|
63
|
+
pub(crate) fn alg_cdc_params(avg_size: u32) -> (usize, usize, usize, u32, u32) {
|
|
64
|
+
let min_size = (avg_size / 4) as usize;
|
|
65
|
+
let max_size = usize::try_from(u64::from(avg_size) * 8).unwrap_or(usize::MAX);
|
|
66
|
+
let offset = min_size + min_size.div_ceil(2);
|
|
67
|
+
let center_size = avg_size as usize - offset;
|
|
68
|
+
let bits = (avg_size as f64).log2().round() as u32;
|
|
69
|
+
let mask_s = low_bits_mask(bits + 1);
|
|
70
|
+
let mask_l = low_bits_mask(bits - 1);
|
|
71
|
+
(min_size, max_size, center_size, mask_s, mask_l)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/// Return a `u32` with the lowest `n` bits set, saturating at `u32::MAX` for `n >= 32`.
|
|
75
|
+
///
|
|
76
|
+
/// The gear pattern is `(pattern >> 1) + gear[b]` with every gear value below
|
|
77
|
+
/// 2^31, so it never reaches 2^32 — a reference mask of 32 or more bits tests
|
|
78
|
+
/// the same bits as `u32::MAX`.
|
|
79
|
+
fn low_bits_mask(n: u32) -> u32 {
|
|
80
|
+
1u32.checked_shl(n).map_or(u32::MAX, |v| v - 1)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/// Scan buffer for a gear-hash cut point, returning the offset if found.
|
|
84
|
+
///
|
|
85
|
+
/// Uses unsafe unchecked indexing and 4x loop unrolling for performance.
|
|
86
|
+
///
|
|
87
|
+
/// # Safety
|
|
88
|
+
/// Caller must ensure `i <= barrier <= buffer.len()`.
|
|
89
|
+
#[inline(always)]
|
|
90
|
+
fn gear_scan(
|
|
91
|
+
buffer: &[u8],
|
|
92
|
+
pattern: &mut u32,
|
|
93
|
+
mut i: usize,
|
|
94
|
+
barrier: usize,
|
|
95
|
+
mask: u32,
|
|
96
|
+
) -> (usize, Option<usize>) {
|
|
97
|
+
// SAFETY: `i < barrier <= buffer.len()`, so `buffer[i]` is in bounds.
|
|
98
|
+
// `buffer[i]` is u8 (0..=255) and CDC_GEAR has 256 entries, always in bounds.
|
|
99
|
+
|
|
100
|
+
// Unrolled 4x to reduce loop overhead and branch frequency
|
|
101
|
+
while i + 3 < barrier {
|
|
102
|
+
unsafe {
|
|
103
|
+
*pattern = (*pattern >> 1)
|
|
104
|
+
.wrapping_add(*CDC_GEAR.get_unchecked(*buffer.get_unchecked(i) as usize));
|
|
105
|
+
}
|
|
106
|
+
if *pattern & mask == 0 {
|
|
107
|
+
return (i + 1, Some(i + 1));
|
|
108
|
+
}
|
|
109
|
+
unsafe {
|
|
110
|
+
*pattern = (*pattern >> 1)
|
|
111
|
+
.wrapping_add(*CDC_GEAR.get_unchecked(*buffer.get_unchecked(i + 1) as usize));
|
|
112
|
+
}
|
|
113
|
+
if *pattern & mask == 0 {
|
|
114
|
+
return (i + 2, Some(i + 2));
|
|
115
|
+
}
|
|
116
|
+
unsafe {
|
|
117
|
+
*pattern = (*pattern >> 1)
|
|
118
|
+
.wrapping_add(*CDC_GEAR.get_unchecked(*buffer.get_unchecked(i + 2) as usize));
|
|
119
|
+
}
|
|
120
|
+
if *pattern & mask == 0 {
|
|
121
|
+
return (i + 3, Some(i + 3));
|
|
122
|
+
}
|
|
123
|
+
unsafe {
|
|
124
|
+
*pattern = (*pattern >> 1)
|
|
125
|
+
.wrapping_add(*CDC_GEAR.get_unchecked(*buffer.get_unchecked(i + 3) as usize));
|
|
126
|
+
}
|
|
127
|
+
if *pattern & mask == 0 {
|
|
128
|
+
return (i + 4, Some(i + 4));
|
|
129
|
+
}
|
|
130
|
+
i += 4;
|
|
131
|
+
}
|
|
132
|
+
// Handle remaining bytes
|
|
133
|
+
while i < barrier {
|
|
134
|
+
unsafe {
|
|
135
|
+
*pattern = (*pattern >> 1)
|
|
136
|
+
.wrapping_add(*CDC_GEAR.get_unchecked(*buffer.get_unchecked(i) as usize));
|
|
137
|
+
}
|
|
138
|
+
if *pattern & mask == 0 {
|
|
139
|
+
return (i + 1, Some(i + 1));
|
|
140
|
+
}
|
|
141
|
+
i += 1;
|
|
142
|
+
}
|
|
143
|
+
(i, None)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/// Find the CDC cut point offset within a buffer.
|
|
147
|
+
///
|
|
148
|
+
/// Uses a gear rolling hash to scan the buffer in two phases:
|
|
149
|
+
/// 1. From `mi` to `cs`: strict phase using `mask_s` (harder to match)
|
|
150
|
+
/// 2. From `cs` to `ma`: relaxed phase using `mask_l` (easier to match)
|
|
151
|
+
///
|
|
152
|
+
/// Returns the byte offset of the cut point. For buffers smaller than
|
|
153
|
+
/// `mi`, returns the buffer length (entire buffer is one chunk).
|
|
154
|
+
pub(crate) fn alg_cdc_offset(
|
|
155
|
+
buffer: &[u8],
|
|
156
|
+
mi: usize,
|
|
157
|
+
ma: usize,
|
|
158
|
+
cs: usize,
|
|
159
|
+
mask_s: u32,
|
|
160
|
+
mask_l: u32,
|
|
161
|
+
) -> usize {
|
|
162
|
+
let mut pattern: u32 = 0;
|
|
163
|
+
let size = buffer.len();
|
|
164
|
+
let i = mi.min(size);
|
|
165
|
+
|
|
166
|
+
// Phase 1: strict mask (harder to match, produces larger chunks)
|
|
167
|
+
let (i, found) = gear_scan(buffer, &mut pattern, i, cs.min(size), mask_s);
|
|
168
|
+
if let Some(offset) = found {
|
|
169
|
+
return offset;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Phase 2: relaxed mask (easier to match, prevents overly large chunks)
|
|
173
|
+
let (i, found) = gear_scan(buffer, &mut pattern, i, ma.min(size), mask_l);
|
|
174
|
+
if let Some(offset) = found {
|
|
175
|
+
return offset;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
i
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/// Split data into content-defined chunks.
|
|
182
|
+
///
|
|
183
|
+
/// Uses the gear rolling hash CDC algorithm to find content-dependent
|
|
184
|
+
/// boundaries. Returns at least one chunk (empty slice for empty input).
|
|
185
|
+
/// When `utf32` is true, aligns cut points to 4-byte boundaries.
|
|
186
|
+
///
|
|
187
|
+
/// # Errors
|
|
188
|
+
///
|
|
189
|
+
/// Returns `IsccError::InvalidInput` if `avg_chunk_size < 2`. The mask
|
|
190
|
+
/// calculation requires `log2(avg_chunk_size) >= 1`.
|
|
191
|
+
pub fn alg_cdc_chunks(data: &[u8], utf32: bool, avg_chunk_size: u32) -> IsccResult<Vec<&[u8]>> {
|
|
192
|
+
if avg_chunk_size < 2 {
|
|
193
|
+
return Err(IsccError::InvalidInput(format!(
|
|
194
|
+
"avg_chunk_size must be >= 2, got {avg_chunk_size}"
|
|
195
|
+
)));
|
|
196
|
+
}
|
|
197
|
+
Ok(alg_cdc_chunks_unchecked(data, utf32, avg_chunk_size))
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/// Split data into content-defined chunks without validating `avg_chunk_size`.
|
|
201
|
+
///
|
|
202
|
+
/// Internal helper used by `DataHasher` and `gen_data_code_v0` where the
|
|
203
|
+
/// chunk size is always the validated constant `DATA_AVG_CHUNK_SIZE`.
|
|
204
|
+
pub(crate) fn alg_cdc_chunks_unchecked(
|
|
205
|
+
data: &[u8],
|
|
206
|
+
utf32: bool,
|
|
207
|
+
avg_chunk_size: u32,
|
|
208
|
+
) -> Vec<&[u8]> {
|
|
209
|
+
if data.is_empty() {
|
|
210
|
+
return vec![&data[0..0]];
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
let (mi, ma, cs, mask_s, mask_l) = alg_cdc_params(avg_chunk_size);
|
|
214
|
+
let mut chunks = Vec::new();
|
|
215
|
+
let mut pos = 0;
|
|
216
|
+
|
|
217
|
+
while pos < data.len() {
|
|
218
|
+
let remaining = &data[pos..];
|
|
219
|
+
let mut cut_point = alg_cdc_offset(remaining, mi, ma, cs, mask_s, mask_l);
|
|
220
|
+
|
|
221
|
+
// Align cut points to 4-byte boundaries for UTF-32 encoded text
|
|
222
|
+
if utf32 {
|
|
223
|
+
cut_point -= cut_point % 4;
|
|
224
|
+
if cut_point == 0 {
|
|
225
|
+
cut_point = remaining.len().min(4);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
chunks.push(&data[pos..pos + cut_point]);
|
|
230
|
+
pos += cut_point;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
chunks
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
#[cfg(test)]
|
|
237
|
+
mod tests {
|
|
238
|
+
use super::*;
|
|
239
|
+
|
|
240
|
+
#[test]
|
|
241
|
+
fn test_gear_table_length() {
|
|
242
|
+
assert_eq!(CDC_GEAR.len(), 256);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
#[test]
|
|
246
|
+
fn test_gear_table_first_last() {
|
|
247
|
+
assert_eq!(CDC_GEAR[0], 1553318008);
|
|
248
|
+
assert_eq!(CDC_GEAR[255], 854125182);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
#[test]
|
|
252
|
+
fn test_alg_cdc_params_default() {
|
|
253
|
+
let (mi, ma, cs, mask_s, mask_l) = alg_cdc_params(1024);
|
|
254
|
+
assert_eq!(mi, 256, "min_size");
|
|
255
|
+
assert_eq!(ma, 8192, "max_size");
|
|
256
|
+
assert_eq!(cs, 640, "center_size");
|
|
257
|
+
assert_eq!(mask_s, 2047, "mask_s = (1 << 11) - 1");
|
|
258
|
+
assert_eq!(mask_l, 511, "mask_l = (1 << 9) - 1");
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/// Expected `(min, max, center, mask_s, mask_l)`; `max` as `u64` to hold values above `u32`.
|
|
262
|
+
type ExpectedParams = (usize, u64, usize, u32, u32);
|
|
263
|
+
|
|
264
|
+
/// Reference parameters for large `avg_chunk_size` values.
|
|
265
|
+
const LARGE_AVG_PARAMS: [(u32, ExpectedParams); 4] = [
|
|
266
|
+
(
|
|
267
|
+
536_870_912,
|
|
268
|
+
(
|
|
269
|
+
134_217_728,
|
|
270
|
+
4_294_967_296,
|
|
271
|
+
335_544_320,
|
|
272
|
+
1_073_741_823,
|
|
273
|
+
268_435_455,
|
|
274
|
+
),
|
|
275
|
+
),
|
|
276
|
+
(
|
|
277
|
+
2_147_483_647,
|
|
278
|
+
(
|
|
279
|
+
536_870_911,
|
|
280
|
+
17_179_869_176,
|
|
281
|
+
1_342_177_280,
|
|
282
|
+
u32::MAX,
|
|
283
|
+
1_073_741_823,
|
|
284
|
+
),
|
|
285
|
+
),
|
|
286
|
+
(
|
|
287
|
+
2_147_483_648,
|
|
288
|
+
(
|
|
289
|
+
536_870_912,
|
|
290
|
+
17_179_869_184,
|
|
291
|
+
1_342_177_280,
|
|
292
|
+
u32::MAX,
|
|
293
|
+
1_073_741_823,
|
|
294
|
+
),
|
|
295
|
+
),
|
|
296
|
+
(
|
|
297
|
+
u32::MAX,
|
|
298
|
+
(
|
|
299
|
+
1_073_741_823,
|
|
300
|
+
34_359_738_360,
|
|
301
|
+
2_684_354_560,
|
|
302
|
+
u32::MAX,
|
|
303
|
+
2_147_483_647,
|
|
304
|
+
),
|
|
305
|
+
),
|
|
306
|
+
];
|
|
307
|
+
|
|
308
|
+
#[test]
|
|
309
|
+
fn test_alg_cdc_params_large_avg_matches_reference() {
|
|
310
|
+
for (avg, (mi, ma, cs, mask_s, mask_l)) in LARGE_AVG_PARAMS {
|
|
311
|
+
let got = alg_cdc_params(avg);
|
|
312
|
+
assert_eq!(got.0, mi, "min_size for {avg}");
|
|
313
|
+
assert_eq!(got.2, cs, "center_size for {avg}");
|
|
314
|
+
assert_eq!(got.3, mask_s, "mask_s for {avg}");
|
|
315
|
+
assert_eq!(got.4, mask_l, "mask_l for {avg}");
|
|
316
|
+
#[cfg(target_pointer_width = "64")]
|
|
317
|
+
assert_eq!(got.1 as u64, ma, "max_size for {avg}");
|
|
318
|
+
#[cfg(not(target_pointer_width = "64"))]
|
|
319
|
+
assert_eq!(got.1, usize::try_from(ma).unwrap_or(usize::MAX));
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
#[test]
|
|
324
|
+
fn test_alg_cdc_chunks_large_avg_single_chunk() {
|
|
325
|
+
// Debug builds panicked on `avg * 8` overflow before the u64 maths.
|
|
326
|
+
let data: Vec<u8> = (0..64).collect();
|
|
327
|
+
for (avg, _) in LARGE_AVG_PARAMS {
|
|
328
|
+
let chunks = alg_cdc_chunks(&data, false, avg).unwrap();
|
|
329
|
+
assert_eq!(chunks, vec![&data[..]], "avg_chunk_size {avg}");
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
#[test]
|
|
334
|
+
fn test_low_bits_mask_saturates() {
|
|
335
|
+
assert_eq!(low_bits_mask(0), 0);
|
|
336
|
+
assert_eq!(low_bits_mask(11), 2047);
|
|
337
|
+
assert_eq!(low_bits_mask(31), 0x7FFF_FFFF);
|
|
338
|
+
assert_eq!(low_bits_mask(32), u32::MAX);
|
|
339
|
+
assert_eq!(low_bits_mask(33), u32::MAX);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
#[test]
|
|
343
|
+
fn test_alg_cdc_offset_small_buffer() {
|
|
344
|
+
// Buffer smaller than min_size → returns buffer length
|
|
345
|
+
let buf = vec![0u8; 100];
|
|
346
|
+
let (mi, ma, cs, mask_s, mask_l) = alg_cdc_params(1024);
|
|
347
|
+
let offset = alg_cdc_offset(&buf, mi, ma, cs, mask_s, mask_l);
|
|
348
|
+
assert_eq!(offset, 100);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
#[test]
|
|
352
|
+
fn test_alg_cdc_offset_returns_at_most_max() {
|
|
353
|
+
// Buffer larger than max_size → returns at most max_size
|
|
354
|
+
let buf = vec![0xAA; 10000];
|
|
355
|
+
let (mi, ma, cs, mask_s, mask_l) = alg_cdc_params(1024);
|
|
356
|
+
let offset = alg_cdc_offset(&buf, mi, ma, cs, mask_s, mask_l);
|
|
357
|
+
assert!(offset <= ma, "offset {offset} exceeds max_size {ma}");
|
|
358
|
+
assert!(offset >= mi, "offset {offset} below min_size {mi}");
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
#[test]
|
|
362
|
+
fn test_alg_cdc_chunks_empty() {
|
|
363
|
+
let chunks = alg_cdc_chunks(b"", false, 1024).unwrap();
|
|
364
|
+
assert_eq!(chunks.len(), 1);
|
|
365
|
+
assert_eq!(chunks[0].len(), 0);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
#[test]
|
|
369
|
+
fn test_alg_cdc_chunks_small_data() {
|
|
370
|
+
// Data smaller than min_size → one chunk containing all data
|
|
371
|
+
let data = vec![42u8; 100];
|
|
372
|
+
let chunks = alg_cdc_chunks(&data, false, 1024).unwrap();
|
|
373
|
+
assert_eq!(chunks.len(), 1);
|
|
374
|
+
assert_eq!(chunks[0].len(), 100);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
#[test]
|
|
378
|
+
fn test_alg_cdc_chunks_reassembly() {
|
|
379
|
+
// Chunks must reassemble to original data
|
|
380
|
+
let data: Vec<u8> = (0..=255).cycle().take(4096).collect();
|
|
381
|
+
let chunks = alg_cdc_chunks(&data, false, 1024).unwrap();
|
|
382
|
+
let reassembled: Vec<u8> = chunks.iter().flat_map(|c| c.iter().copied()).collect();
|
|
383
|
+
assert_eq!(reassembled, data);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
#[test]
|
|
387
|
+
fn test_alg_cdc_chunks_deterministic() {
|
|
388
|
+
let data: Vec<u8> = (0..=255).cycle().take(4096).collect();
|
|
389
|
+
let chunks1 = alg_cdc_chunks(&data, false, 1024).unwrap();
|
|
390
|
+
let chunks2 = alg_cdc_chunks(&data, false, 1024).unwrap();
|
|
391
|
+
assert_eq!(chunks1.len(), chunks2.len());
|
|
392
|
+
for (a, b) in chunks1.iter().zip(chunks2.iter()) {
|
|
393
|
+
assert_eq!(a, b);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
#[test]
|
|
398
|
+
fn test_alg_cdc_chunks_multiple_chunks() {
|
|
399
|
+
// Large data produces multiple chunks
|
|
400
|
+
let data: Vec<u8> = (0..=255).cycle().take(8192).collect();
|
|
401
|
+
let chunks = alg_cdc_chunks(&data, false, 1024).unwrap();
|
|
402
|
+
assert!(
|
|
403
|
+
chunks.len() > 1,
|
|
404
|
+
"expected multiple chunks, got {}",
|
|
405
|
+
chunks.len()
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
#[test]
|
|
410
|
+
fn test_alg_cdc_chunks_utf32_small_buffer() {
|
|
411
|
+
// 3 bytes with utf32=true must terminate and reassemble to original.
|
|
412
|
+
// Primary regression test for the infinite loop bug where
|
|
413
|
+
// cut_point % 4 == cut_point reduced cut_point to 0.
|
|
414
|
+
let data = [0xAA, 0xBB, 0xCC];
|
|
415
|
+
let chunks = alg_cdc_chunks(&data, true, 1024).unwrap();
|
|
416
|
+
assert!(!chunks.is_empty(), "must return at least one chunk");
|
|
417
|
+
let reassembled: Vec<u8> = chunks.iter().flat_map(|c| c.iter().copied()).collect();
|
|
418
|
+
assert_eq!(reassembled, data);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
#[test]
|
|
422
|
+
fn test_alg_cdc_chunks_utf32_exact_4_bytes() {
|
|
423
|
+
// Exactly 4 bytes with utf32=true must return one 4-byte chunk.
|
|
424
|
+
let data = [0x01, 0x02, 0x03, 0x04];
|
|
425
|
+
let chunks = alg_cdc_chunks(&data, true, 1024).unwrap();
|
|
426
|
+
assert_eq!(chunks.len(), 1);
|
|
427
|
+
assert_eq!(chunks[0], &data[..]);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
#[test]
|
|
431
|
+
fn test_alg_cdc_chunks_utf32_7_bytes() {
|
|
432
|
+
// 7 bytes (4+3) with utf32=true verifies non-aligned tail handling.
|
|
433
|
+
let data = [0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70];
|
|
434
|
+
let chunks = alg_cdc_chunks(&data, true, 1024).unwrap();
|
|
435
|
+
assert!(!chunks.is_empty(), "must return at least one chunk");
|
|
436
|
+
let reassembled: Vec<u8> = chunks.iter().flat_map(|c| c.iter().copied()).collect();
|
|
437
|
+
assert_eq!(reassembled, data);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
#[test]
|
|
441
|
+
fn test_alg_cdc_chunks_utf32_reassembly() {
|
|
442
|
+
// Larger 4-byte-aligned input with utf32=true must reassemble correctly,
|
|
443
|
+
// and all chunks except possibly the last must be 4-byte aligned.
|
|
444
|
+
let data: Vec<u8> = (0..=255).cycle().take(4096).collect();
|
|
445
|
+
assert_eq!(data.len() % 4, 0, "test data must be 4-byte aligned");
|
|
446
|
+
let chunks = alg_cdc_chunks(&data, true, 1024).unwrap();
|
|
447
|
+
let reassembled: Vec<u8> = chunks.iter().flat_map(|c| c.iter().copied()).collect();
|
|
448
|
+
assert_eq!(reassembled, data);
|
|
449
|
+
// All chunks except the last must be 4-byte aligned
|
|
450
|
+
if chunks.len() > 1 {
|
|
451
|
+
for (i, chunk) in chunks[..chunks.len() - 1].iter().enumerate() {
|
|
452
|
+
assert_eq!(
|
|
453
|
+
chunk.len() % 4,
|
|
454
|
+
0,
|
|
455
|
+
"chunk {i} has length {} which is not 4-byte aligned",
|
|
456
|
+
chunk.len()
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
#[test]
|
|
463
|
+
fn test_alg_cdc_chunks_utf32_empty() {
|
|
464
|
+
// Empty input with utf32=true must not loop and must return one empty chunk.
|
|
465
|
+
let chunks = alg_cdc_chunks(b"", true, 1024).unwrap();
|
|
466
|
+
assert_eq!(chunks.len(), 1);
|
|
467
|
+
assert_eq!(chunks[0].len(), 0);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
#[test]
|
|
471
|
+
fn test_alg_cdc_chunks_zero_avg_chunk_size() {
|
|
472
|
+
let err = alg_cdc_chunks(b"hello", false, 0).unwrap_err();
|
|
473
|
+
assert!(err.to_string().contains("avg_chunk_size must be >= 2"));
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
#[test]
|
|
477
|
+
fn test_alg_cdc_chunks_one_avg_chunk_size() {
|
|
478
|
+
let err = alg_cdc_chunks(b"hello", false, 1).unwrap_err();
|
|
479
|
+
assert!(err.to_string().contains("avg_chunk_size must be >= 2"));
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
#[test]
|
|
483
|
+
fn test_alg_cdc_chunks_min_valid_avg_chunk_size() {
|
|
484
|
+
let chunks = alg_cdc_chunks(b"hello world", false, 2).unwrap();
|
|
485
|
+
let reassembled: Vec<u8> = chunks.iter().flat_map(|c| c.iter().copied()).collect();
|
|
486
|
+
assert_eq!(reassembled, b"hello world");
|
|
487
|
+
}
|
|
488
|
+
}
|