diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-03-04 21:26:04 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-03-04 21:35:09 +0800 |
| commit | 22926ce29e3f8e040ec349401aeb6a77f32eae72 (patch) | |
| tree | 678753ec49a61fb9d3e2d8e869393dec90ea7ef4 /policy/butck | |
Initialize Butchunker project structure and policy system
Diffstat (limited to 'policy/butck')
| -rw-r--r-- | policy/butck/butck_fixed_size/Cargo.lock | 7 | ||||
| -rw-r--r-- | policy/butck/butck_fixed_size/Cargo.toml | 7 | ||||
| -rw-r--r-- | policy/butck/butck_fixed_size/src/lib.rs | 48 |
3 files changed, 62 insertions, 0 deletions
diff --git a/policy/butck/butck_fixed_size/Cargo.lock b/policy/butck/butck_fixed_size/Cargo.lock new file mode 100644 index 0000000..c1e1873 --- /dev/null +++ b/policy/butck/butck_fixed_size/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "butck_fixed_size" +version = "0.1.0" diff --git a/policy/butck/butck_fixed_size/Cargo.toml b/policy/butck/butck_fixed_size/Cargo.toml new file mode 100644 index 0000000..1550cb9 --- /dev/null +++ b/policy/butck/butck_fixed_size/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "butck_fixed_size" +authors = ["Butchunker"] +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/policy/butck/butck_fixed_size/src/lib.rs b/policy/butck/butck_fixed_size/src/lib.rs new file mode 100644 index 0000000..28cabff --- /dev/null +++ b/policy/butck/butck_fixed_size/src/lib.rs @@ -0,0 +1,48 @@ +use std::collections::HashMap; + +const DEFAULT_CHUNK_SIZE: usize = 1024 * 1024; // 1MB + +fn get_chunk_size(params: &HashMap<&str, &str>) -> usize { + params + .get("size") + .and_then(|s| s.parse().ok()) + .unwrap_or(DEFAULT_CHUNK_SIZE) +} + +pub async fn chunk_fixed_size(raw_data: &[u8], params: &HashMap<&str, &str>) -> Vec<u32> { + let chunk_size = get_chunk_size(params); + (chunk_size..raw_data.len()) + .step_by(chunk_size) + .map(|pos| pos as u32) + .collect() +} + +#[derive(Default)] +pub struct FixedSizeStream { + processed_bytes: usize, +} + +pub async fn chunk_fixed_size_stream( + _current_data: &[u8], + len: u32, + stream: &mut FixedSizeStream, + params: &HashMap<&str, &str>, +) -> Option<u32> { + let chunk_size = get_chunk_size(params); + let valid_len = len as usize; + + let prev_chunk = stream.processed_bytes / chunk_size; + let new_processed = stream.processed_bytes + valid_len; + let new_chunk = new_processed / chunk_size; + + if prev_chunk != new_chunk { + // Find chunk boundary in current data, update processed bytes, return position + let boundary_in_chunk = chunk_size - (stream.processed_bytes % chunk_size); + stream.processed_bytes += boundary_in_chunk; + Some(boundary_in_chunk.min(valid_len) as u32) + } else { + // Update bytes processed + stream.processed_bytes = new_processed; + None + } +} |
