roxify 1.13.7 → 1.13.9

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.
Files changed (56) hide show
  1. package/dist/cli.js +1 -8
  2. package/dist/stub-progress.d.ts +4 -4
  3. package/dist/stub-progress.js +4 -4
  4. package/dist/utils/decoder.d.ts +46 -2
  5. package/dist/utils/decoder.js +248 -38
  6. package/dist/utils/ecc.js +0 -1
  7. package/dist/utils/encoder.d.ts +30 -1
  8. package/dist/utils/encoder.js +34 -18
  9. package/dist/utils/inspection.d.ts +1 -1
  10. package/dist/utils/inspection.js +2 -2
  11. package/dist/utils/robust-audio.js +0 -13
  12. package/dist/utils/robust-image.js +0 -26
  13. package/package.json +12 -29
  14. package/roxify_native-aarch64-apple-darwin.node +0 -0
  15. package/roxify_native-aarch64-pc-windows-msvc.node +0 -0
  16. package/roxify_native-aarch64-unknown-linux-gnu.node +0 -0
  17. package/roxify_native-i686-pc-windows-msvc.node +0 -0
  18. package/roxify_native-i686-unknown-linux-gnu.node +0 -0
  19. package/{dist/rox-macos-universal → roxify_native-universal-apple-darwin.node} +0 -0
  20. package/roxify_native-x86_64-apple-darwin.node +0 -0
  21. package/roxify_native-x86_64-pc-windows-msvc.node +0 -0
  22. package/roxify_native-x86_64-unknown-linux-gnu.node +0 -0
  23. package/scripts/postinstall.cjs +23 -2
  24. package/Cargo.toml +0 -91
  25. package/dist/roxify_native +0 -0
  26. package/dist/roxify_native-macos-arm64 +0 -0
  27. package/dist/roxify_native-macos-x64 +0 -0
  28. package/dist/roxify_native.exe +0 -0
  29. package/native/archive.rs +0 -220
  30. package/native/audio.rs +0 -151
  31. package/native/bench_hybrid.rs +0 -145
  32. package/native/bwt.rs +0 -56
  33. package/native/context_mixing.rs +0 -117
  34. package/native/core.rs +0 -378
  35. package/native/crypto.rs +0 -209
  36. package/native/encoder.rs +0 -405
  37. package/native/hybrid.rs +0 -297
  38. package/native/image_utils.rs +0 -82
  39. package/native/io_advice.rs +0 -43
  40. package/native/io_ntfs_optimized.rs +0 -99
  41. package/native/lib.rs +0 -480
  42. package/native/main.rs +0 -842
  43. package/native/mtf.rs +0 -106
  44. package/native/packer.rs +0 -604
  45. package/native/png_chunk_writer.rs +0 -146
  46. package/native/png_utils.rs +0 -554
  47. package/native/pool.rs +0 -101
  48. package/native/progress.rs +0 -142
  49. package/native/rans.rs +0 -149
  50. package/native/rans_byte.rs +0 -286
  51. package/native/reconstitution.rs +0 -623
  52. package/native/streaming.rs +0 -189
  53. package/native/streaming_decode.rs +0 -625
  54. package/native/streaming_encode.rs +0 -684
  55. package/native/test_small_bwt.rs +0 -31
  56. package/native/test_stages.rs +0 -70
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "roxify",
3
- "version": "1.13.7",
3
+ "version": "1.13.9",
4
4
  "type": "module",
5
5
  "description": "Ultra-lightweight PNG steganography with native Rust acceleration. Encode binary data into PNG images with zstd compression.",
6
6
  "main": "dist/index.js",
@@ -10,34 +10,17 @@
10
10
  "roxify": "dist/cli.js"
11
11
  },
12
12
  "files": [
13
- "dist",
14
- "roxify_native-x86_64-unknown-linux-gnu.node",
15
- "roxify_native-i686-unknown-linux-gnu.node",
16
- "roxify_native-aarch64-unknown-linux-gnu.node",
17
- "roxify_native-x86_64-apple-darwin.node",
18
- "roxify_native-aarch64-apple-darwin.node",
19
- "roxify_native-x86_64-pc-windows-msvc.node",
20
- "roxify_native-i686-pc-windows-msvc.node",
21
- "roxify_native-aarch64-pc-windows-msvc.node",
22
- "roxify_native-x86_64-pc-windows-gnu.node",
23
- "libroxify_native-x86_64-unknown-linux-gnu.node",
24
- "libroxify_native-i686-unknown-linux-gnu.node",
25
- "libroxify_native-aarch64-unknown-linux-gnu.node",
26
- "libroxify_native-x86_64-apple-darwin.node",
27
- "libroxify_native-aarch64-apple-darwin.node",
28
- "dist/roxify_native.exe",
29
- "dist/roxify_native",
30
- "dist/roxify_native-macos-arm64",
31
- "dist/roxify_native-macos-x64",
32
- "dist/rox-macos-universal",
13
+ "dist/**/*.js",
14
+ "dist/**/*.d.ts",
33
15
  "scripts/postinstall.cjs",
34
- "native",
35
- "Cargo.toml",
16
+ "roxify_native-*.node",
17
+ "libroxify_native-*.node",
36
18
  "README.md",
37
19
  "LICENSE"
38
20
  ],
39
21
  "scripts": {
40
22
  "build": "tsc",
23
+ "bundle": "esbuild src/index.ts --bundle --platform=node --target=node18 --outfile=dist/index.js --format=esm --minify && esbuild src/cli.ts --bundle --platform=node --target=node18 --outfile=dist/cli.js --format=esm --minify && tsc --emitDeclarationOnly --declaration --declarationDir dist",
41
24
  "prebuild:pkg": "node scripts/copy-cli-binary.js",
42
25
  "build:native": "cargo build --release --lib",
43
26
  "build:native:linux": "cargo build --release --lib --target x86_64-unknown-linux-gnu",
@@ -59,7 +42,7 @@
59
42
  "clean:targets": "node scripts/clean-artifacts.js",
60
43
  "release:prepare": "npm run build && npm run build:native:targets && node scripts/prepare-release.cjs",
61
44
  "release:github": "npm run release:prepare && node scripts/create-gh-release.cjs",
62
- "package:prepare": "npm run build && npm run build:native:targets && node scripts/pack-npm.cjs",
45
+ "package:prepare": "npm run bundle && npm run build:native:targets && node scripts/pack-npm.cjs",
63
46
  "publish:npm": "npm run package:prepare && echo 'Run npm publish --access public'",
64
47
  "release:flow": "node scripts/release-flow.cjs",
65
48
  "release:flow:auto": "AUTO_PUBLISH=1 node scripts/release-flow.cjs",
@@ -103,12 +86,12 @@
103
86
  "devDependencies": {
104
87
  "@types/node": "^22.0.0",
105
88
  "pkg": "^5.8.1",
106
- "typescript": "^5.9.3"
89
+ "typescript": "^5.9.3",
90
+ "esbuild": "^0.18.0",
91
+ "pngjs": "^7.0.0"
107
92
  },
108
93
  "engines": {
109
94
  "node": ">=18.0.0"
110
95
  },
111
- "dependencies": {
112
- "pngjs": "^7.0.0"
113
- }
114
- }
96
+ "dependencies": {}
97
+ }
@@ -1,5 +1,5 @@
1
1
  const { execSync } = require('child_process');
2
- const { existsSync, copyFileSync, mkdirSync } = require('fs');
2
+ const { existsSync, copyFileSync, mkdirSync, readdirSync, unlinkSync } = require('fs');
3
3
  const { join } = require('path');
4
4
  const { platform, arch } = require('os');
5
5
 
@@ -157,5 +157,26 @@ function ensureNativeLib() {
157
157
  }
158
158
  }
159
159
 
160
- ensureCliBinary();
160
+ if (process.env.ROXIFY_ENABLE_RUST_CLI === '1') {
161
+ ensureCliBinary();
162
+ }
161
163
  ensureNativeLib();
164
+
165
+ // Cleanup: remove other platform .node artifacts to reduce disk usage after install
166
+ try {
167
+ const triples = getTriples();
168
+ if (triples && triples.length) {
169
+ const files = readdirSync(root);
170
+ for (const f of files) {
171
+ // match roxify_native-<triple>.node or libroxify_native-<triple>.node
172
+ const m = f.match(/^(lib)?roxify_native-(.+)\.node$/);
173
+ if (m) {
174
+ const fileTriple = m[2];
175
+ // keep if fileTriple is one of the allowed triples
176
+ if (!triples.includes(fileTriple)) {
177
+ try { unlinkSync(join(root, f)); console.log(`roxify: removed ${f}`); } catch (e) { }
178
+ }
179
+ }
180
+ }
181
+ }
182
+ } catch (e) { }
package/Cargo.toml DELETED
@@ -1,91 +0,0 @@
1
- [package]
2
- name = "roxify_native"
3
- version = "1.13.6"
4
- edition = "2021"
5
- publish = false
6
-
7
- [lib]
8
- name = "roxify_native"
9
- crate-type = ["cdylib"]
10
- path = "native/lib.rs"
11
-
12
- [[bin]]
13
- name = "roxify_native"
14
- path = "native/main.rs"
15
-
16
- [[bin]]
17
- name = "bench_hybrid"
18
- path = "native/bench_hybrid.rs"
19
-
20
- [[bin]]
21
- name = "test_stages"
22
- path = "native/test_stages.rs"
23
-
24
- [[bin]]
25
- name = "test_small_bwt"
26
- path = "native/test_small_bwt.rs"
27
-
28
- [dev-dependencies]
29
-
30
- [dependencies]
31
- napi = "2"
32
- napi-derive = "2"
33
- rayon = "1.7"
34
- zstd = { version = "0.11", features = ["zstdmt"] }
35
- crc32fast = "1.3"
36
- num_cpus = "1.16"
37
- clap = { version = "4", features = ["derive"] }
38
- serde = { version = "1.0", features = ["derive"] }
39
- serde_json = "1.0"
40
- anyhow = "1.0"
41
- png = "0.18.0"
42
- image = { version = "0.25", default-features = false, features = ["png"] }
43
- # indicatif removed from default deps to reduce heavy build graph
44
- walkdir = "2.5.0"
45
- tar = "0.4"
46
- aes-gcm = "0.10"
47
- aes = "0.8"
48
- ctr = "0.9"
49
- cipher = { version = "0.4", features = ["std"] }
50
- hmac = "0.12"
51
- pbkdf2 = "0.12"
52
- rand = "0.8"
53
- sha2 = "0.10"
54
- mimalloc = "0.1"
55
- simd-adler32 = "0.3"
56
-
57
- memmap2 = "0.9"
58
- bytemuck = { version = "1.14", features = ["derive"] }
59
- tokio = { version = "1", features = ["sync", "rt"], optional = true }
60
- parking_lot = "0.12"
61
- libsais = { version = "0.2.0", default-features = false }
62
- libc = "0.2"
63
-
64
- [features]
65
- default = []
66
- async = ["tokio"]
67
-
68
- [profile.release]
69
- opt-level = 3
70
- lto = "fat"
71
- codegen-units = 1
72
- strip = true
73
- panic = "abort"
74
-
75
- [profile.release-size]
76
- inherits = "release"
77
- opt-level = "z"
78
- lto = true
79
- strip = true
80
-
81
- [profile.fastdev]
82
- # Fast development profile for minimal user CPU and fast compilation.
83
- # Lower optimization and high codegen units to parallelize compilation work,
84
- # enable incremental to speed up subsequent incremental builds.
85
- inherits = "release"
86
- opt-level = 1
87
- lto = false
88
- codegen-units = 16
89
- debug = false
90
- incremental = true
91
- panic = "abort"
Binary file
Binary file
Binary file
Binary file
package/native/archive.rs DELETED
@@ -1,220 +0,0 @@
1
- use std::io::Cursor;
2
- use std::path::Path;
3
- use rayon::prelude::*;
4
- use tar::{Archive, Builder, Header};
5
- use walkdir::WalkDir;
6
-
7
- pub struct TarPackResult {
8
- pub data: Vec<u8>,
9
- pub file_list: Vec<(String, u64)>,
10
- }
11
-
12
- pub fn tar_pack_directory_with_list(dir_path: &Path) -> Result<TarPackResult, String> {
13
- let base = dir_path;
14
-
15
- let entries: Vec<_> = WalkDir::new(dir_path)
16
- .follow_links(false)
17
- .into_iter()
18
- .filter_map(|e| e.ok())
19
- .filter(|e| e.file_type().is_file())
20
- .collect();
21
-
22
- let file_data: Vec<(String, Vec<u8>)> = entries
23
- .par_iter()
24
- .filter_map(|entry| {
25
- let full = entry.path();
26
- let rel = full.strip_prefix(base).unwrap_or(full);
27
- let rel_str = rel.to_string_lossy().replace('\\', "/");
28
- match std::fs::read(full) {
29
- Ok(data) => Some((rel_str, data)),
30
- Err(_) => None,
31
- }
32
- })
33
- .collect();
34
-
35
- let file_list: Vec<(String, u64)> = file_data.iter()
36
- .map(|(name, data)| (name.clone(), data.len() as u64))
37
- .collect();
38
-
39
- let total_estimate: usize = file_data.iter().map(|(n, d)| 512 + d.len() + 512 + n.len()).sum();
40
- let mut buf = Vec::with_capacity(total_estimate + 1024);
41
- {
42
- let mut builder = Builder::new(&mut buf);
43
- for (rel_str, data) in &file_data {
44
- let mut header = Header::new_gnu();
45
- header.set_size(data.len() as u64);
46
- header.set_mode(0o644);
47
- header.set_cksum();
48
- builder
49
- .append_data(&mut header, rel_str, &data[..])
50
- .map_err(|e| format!("tar append {}: {}", rel_str, e))?;
51
- }
52
- builder.finish().map_err(|e| format!("tar finish: {}", e))?;
53
- }
54
- Ok(TarPackResult { data: buf, file_list })
55
- }
56
-
57
- pub fn tar_pack_directory(dir_path: &Path) -> Result<Vec<u8>, String> {
58
- tar_pack_directory_with_list(dir_path).map(|r| r.data)
59
- }
60
-
61
- pub fn tar_file_list_fast(tar_data: &[u8]) -> Vec<(String, u64)> {
62
- let mut list = Vec::new();
63
- let mut pos = 0;
64
- while pos + 512 <= tar_data.len() {
65
- let header = &tar_data[pos..pos + 512];
66
- if header.iter().all(|&b| b == 0) {
67
- break;
68
- }
69
- let name_end = header[..100].iter().position(|&b| b == 0).unwrap_or(100);
70
- let name = String::from_utf8_lossy(&header[..name_end]).to_string();
71
- let size_str = String::from_utf8_lossy(&header[124..136]);
72
- let size = u64::from_str_radix(size_str.trim().trim_matches('\0'), 8).unwrap_or(0);
73
- if !name.is_empty() {
74
- list.push((name, size));
75
- }
76
- let data_blocks = (size as usize + 511) / 512;
77
- pos += 512 + data_blocks * 512;
78
- }
79
- list
80
- }
81
-
82
- pub fn tar_unpack(tar_data: &[u8], output_dir: &Path) -> Result<Vec<String>, String> {
83
- let mut archive = Archive::new(Cursor::new(tar_data));
84
- let mut entries_data: Vec<(std::path::PathBuf, Vec<u8>)> = Vec::new();
85
-
86
- let entries = archive.entries().map_err(|e| format!("tar entries: {}", e))?;
87
- for entry in entries {
88
- let mut entry = entry.map_err(|e| format!("tar entry: {}", e))?;
89
- let path = entry.path().map_err(|e| format!("tar entry path: {}", e))?.to_path_buf();
90
-
91
- let mut safe = std::path::PathBuf::new();
92
- for comp in path.components() {
93
- if let std::path::Component::Normal(osstr) = comp {
94
- safe.push(osstr);
95
- }
96
- }
97
- if safe.as_os_str().is_empty() {
98
- continue;
99
- }
100
-
101
- let mut data = Vec::with_capacity(entry.size() as usize);
102
- std::io::Read::read_to_end(&mut entry, &mut data)
103
- .map_err(|e| format!("tar read {:?}: {}", safe, e))?;
104
- entries_data.push((safe, data));
105
- }
106
-
107
- let dirs: std::collections::HashSet<_> = entries_data.iter()
108
- .filter_map(|(p, _)| {
109
- let dest = output_dir.join(p);
110
- dest.parent().map(|d| d.to_path_buf())
111
- })
112
- .collect();
113
- for dir in &dirs {
114
- std::fs::create_dir_all(dir).map_err(|e| format!("mkdir {:?}: {}", dir, e))?;
115
- }
116
-
117
- let written: Vec<String> = entries_data.par_iter()
118
- .filter_map(|(safe, data)| {
119
- let dest = output_dir.join(safe);
120
- match std::fs::write(&dest, data) {
121
- Ok(_) => Some(safe.to_string_lossy().to_string()),
122
- Err(_) => None,
123
- }
124
- })
125
- .collect();
126
-
127
- Ok(written)
128
- }
129
-
130
- pub fn is_tar(data: &[u8]) -> bool {
131
- if data.len() < 263 {
132
- return false;
133
- }
134
- &data[257..262] == b"ustar"
135
- }
136
-
137
- pub fn tar_file_list(tar_data: &[u8]) -> Result<Vec<(String, u64)>, String> {
138
- let mut archive = Archive::new(Cursor::new(tar_data));
139
- let mut list = Vec::new();
140
- let entries = archive.entries().map_err(|e| format!("tar entries: {}", e))?;
141
- for entry in entries {
142
- let entry = entry.map_err(|e| format!("tar entry: {}", e))?;
143
- let path = entry
144
- .path()
145
- .map_err(|e| format!("tar path: {}", e))?
146
- .to_string_lossy()
147
- .to_string();
148
- let size = entry.size();
149
- list.push((path, size));
150
- }
151
- Ok(list)
152
- }
153
-
154
- #[cfg(test)]
155
- mod tests {
156
- use super::*;
157
- use std::fs;
158
-
159
- #[test]
160
- fn test_tar_roundtrip() {
161
- let tmp = std::env::temp_dir().join("rox_tar_test");
162
- let _ = fs::remove_dir_all(&tmp);
163
- fs::create_dir_all(tmp.join("sub")).unwrap();
164
- fs::write(tmp.join("hello.txt"), b"Hello TAR").unwrap();
165
- fs::write(tmp.join("sub/nested.txt"), b"Nested!").unwrap();
166
-
167
- let tar_data = tar_pack_directory(&tmp).unwrap();
168
- assert!(is_tar(&tar_data));
169
-
170
- let list = tar_file_list(&tar_data).unwrap();
171
- assert_eq!(list.len(), 2);
172
-
173
- let out = std::env::temp_dir().join("rox_tar_test_out");
174
- let _ = fs::remove_dir_all(&out);
175
- fs::create_dir_all(&out).unwrap();
176
-
177
- let written = tar_unpack(&tar_data, &out).unwrap();
178
- assert_eq!(written.len(), 2);
179
- assert_eq!(fs::read_to_string(out.join("hello.txt")).unwrap(), "Hello TAR");
180
- assert_eq!(fs::read_to_string(out.join("sub/nested.txt")).unwrap(), "Nested!");
181
-
182
- let _ = fs::remove_dir_all(&tmp);
183
- let _ = fs::remove_dir_all(&out);
184
- }
185
-
186
- #[test]
187
- fn test_tar_zstd_roundtrip() {
188
- use std::io::Write;
189
-
190
- let tmp = std::env::temp_dir().join("rox_tar_zstd_test");
191
- let _ = fs::remove_dir_all(&tmp);
192
- fs::create_dir_all(tmp.join("a/b")).unwrap();
193
- fs::write(tmp.join("root.txt"), b"root file content").unwrap();
194
- fs::write(tmp.join("a/mid.txt"), b"mid level").unwrap();
195
- fs::write(tmp.join("a/b/deep.txt"), b"deep nested file").unwrap();
196
-
197
- let tar_data = tar_pack_directory(&tmp).unwrap();
198
- assert!(is_tar(&tar_data));
199
-
200
- let mut encoder = zstd::stream::Encoder::new(Vec::new(), 3).unwrap();
201
- encoder.write_all(&tar_data).unwrap();
202
- let compressed = encoder.finish().unwrap();
203
-
204
- let decompressed = crate::core::zstd_decompress_bytes(&compressed, None).unwrap();
205
- assert!(is_tar(&decompressed));
206
-
207
- let out = std::env::temp_dir().join("rox_tar_zstd_test_out");
208
- let _ = fs::remove_dir_all(&out);
209
- fs::create_dir_all(&out).unwrap();
210
-
211
- let written = tar_unpack(&decompressed, &out).unwrap();
212
- assert_eq!(written.len(), 3);
213
- assert_eq!(fs::read_to_string(out.join("root.txt")).unwrap(), "root file content");
214
- assert_eq!(fs::read_to_string(out.join("a/mid.txt")).unwrap(), "mid level");
215
- assert_eq!(fs::read_to_string(out.join("a/b/deep.txt")).unwrap(), "deep nested file");
216
-
217
- let _ = fs::remove_dir_all(&tmp);
218
- let _ = fs::remove_dir_all(&out);
219
- }
220
- }
package/native/audio.rs DELETED
@@ -1,151 +0,0 @@
1
- /// WAV container for binary data.
2
- ///
3
- /// Encodes raw bytes as 8-bit unsigned PCM mono samples (44100 Hz).
4
- /// Header is exactly 44 bytes. Total overhead: 44 bytes.
5
- ///
6
- /// Compared to PNG (stored deflate): PNG overhead grows with data size
7
- /// (zlib framing, filter bytes, chunk CRCs). WAV overhead is constant.
8
-
9
- const WAV_HEADER_SIZE: usize = 44;
10
- const SAMPLE_RATE: u32 = 44100;
11
- const BITS_PER_SAMPLE: u16 = 8;
12
- const NUM_CHANNELS: u16 = 1;
13
-
14
- /// Pack raw bytes into a WAV file (8-bit PCM, mono, 44100 Hz).
15
- /// The bytes are stored directly as unsigned PCM samples.
16
- /// Returns the complete WAV file as a Vec<u8>.
17
- pub fn bytes_to_wav(data: &[u8]) -> Vec<u8> {
18
- let data_size = data.len() as u32;
19
- let file_size = WAV_HEADER_SIZE as u32 - 8 + data_size; // RIFF chunk size
20
-
21
- let byte_rate = SAMPLE_RATE * NUM_CHANNELS as u32 * (BITS_PER_SAMPLE as u32 / 8);
22
- let block_align = NUM_CHANNELS * (BITS_PER_SAMPLE / 8);
23
-
24
- let mut wav = Vec::with_capacity(WAV_HEADER_SIZE + data.len());
25
-
26
- // RIFF header
27
- wav.extend_from_slice(b"RIFF");
28
- wav.extend_from_slice(&file_size.to_le_bytes());
29
- wav.extend_from_slice(b"WAVE");
30
-
31
- // fmt sub-chunk
32
- wav.extend_from_slice(b"fmt ");
33
- wav.extend_from_slice(&16u32.to_le_bytes()); // sub-chunk size (PCM = 16)
34
- wav.extend_from_slice(&1u16.to_le_bytes()); // audio format (1 = PCM)
35
- wav.extend_from_slice(&NUM_CHANNELS.to_le_bytes());
36
- wav.extend_from_slice(&SAMPLE_RATE.to_le_bytes());
37
- wav.extend_from_slice(&byte_rate.to_le_bytes());
38
- wav.extend_from_slice(&block_align.to_le_bytes());
39
- wav.extend_from_slice(&BITS_PER_SAMPLE.to_le_bytes());
40
-
41
- // data sub-chunk
42
- wav.extend_from_slice(b"data");
43
- wav.extend_from_slice(&data_size.to_le_bytes());
44
- wav.extend_from_slice(data);
45
-
46
- wav
47
- }
48
-
49
- /// Extract raw bytes from a WAV file.
50
- /// Returns the PCM data (the original bytes) or an error.
51
- pub fn wav_to_bytes(wav: &[u8]) -> Result<Vec<u8>, String> {
52
- if wav.len() < WAV_HEADER_SIZE {
53
- return Err("WAV data too short".to_string());
54
- }
55
-
56
- // Validate RIFF header
57
- if &wav[0..4] != b"RIFF" {
58
- return Err("Not a RIFF file".to_string());
59
- }
60
- if &wav[8..12] != b"WAVE" {
61
- return Err("Not a WAVE file".to_string());
62
- }
63
-
64
- // Find the "data" sub-chunk (skip fmt and any other chunks)
65
- let mut offset = 12; // past "RIFF" + size + "WAVE"
66
- loop {
67
- if offset + 8 > wav.len() {
68
- return Err("data chunk not found".to_string());
69
- }
70
- let chunk_id = &wav[offset..offset + 4];
71
- let chunk_size = u32::from_le_bytes([
72
- wav[offset + 4],
73
- wav[offset + 5],
74
- wav[offset + 6],
75
- wav[offset + 7],
76
- ]) as usize;
77
-
78
- if chunk_id == b"data" {
79
- let data_start = offset + 8;
80
- let data_end = data_start + chunk_size;
81
- if data_end > wav.len() {
82
- // Allow truncation: return what we have
83
- return Ok(wav[data_start..].to_vec());
84
- }
85
- return Ok(wav[data_start..data_end].to_vec());
86
- }
87
-
88
- // Skip this chunk (+ padding byte if odd size)
89
- offset += 8 + chunk_size;
90
- if chunk_size % 2 != 0 {
91
- offset += 1; // RIFF chunks are word-aligned
92
- }
93
- }
94
- }
95
-
96
- /// Check if a buffer starts with a RIFF/WAVE header.
97
- pub fn is_wav(buf: &[u8]) -> bool {
98
- buf.len() >= 12 && &buf[0..4] == b"RIFF" && &buf[8..12] == b"WAVE"
99
- }
100
-
101
- #[cfg(test)]
102
- mod tests {
103
- use super::*;
104
-
105
- #[test]
106
- fn test_wav_roundtrip() {
107
- let data = b"Hello, World! This is roxify audio container test data.";
108
- let wav = bytes_to_wav(data);
109
-
110
- // Check header
111
- assert_eq!(&wav[0..4], b"RIFF");
112
- assert_eq!(&wav[8..12], b"WAVE");
113
- assert_eq!(wav.len(), 44 + data.len());
114
-
115
- // Roundtrip
116
- let recovered = wav_to_bytes(&wav).expect("decode should succeed");
117
- assert_eq!(recovered, data);
118
- }
119
-
120
- #[test]
121
- fn test_wav_empty() {
122
- let data: &[u8] = b"";
123
- let wav = bytes_to_wav(data);
124
- assert_eq!(wav.len(), 44);
125
- let recovered = wav_to_bytes(&wav).expect("decode empty");
126
- assert!(recovered.is_empty());
127
- }
128
-
129
- #[test]
130
- fn test_wav_large() {
131
- let data = vec![0xAB_u8; 1024 * 1024]; // 1 MB
132
- let wav = bytes_to_wav(&data);
133
- assert_eq!(wav.len(), 44 + 1024 * 1024);
134
- let recovered = wav_to_bytes(&wav).expect("decode large");
135
- assert_eq!(recovered, data);
136
- }
137
-
138
- #[test]
139
- fn test_is_wav() {
140
- let wav = bytes_to_wav(b"test");
141
- assert!(is_wav(&wav));
142
- assert!(!is_wav(b"not a wav"));
143
- assert!(!is_wav(b"RIFF1234XXXX")); // RIFF but not WAVE
144
- }
145
-
146
- #[test]
147
- fn test_invalid_wav() {
148
- assert!(wav_to_bytes(b"short").is_err());
149
- assert!(wav_to_bytes(b"NOT a RIFF file!").is_err());
150
- }
151
- }