cum-rs 0.1.1

๐Ÿงน A multilanguage crate to remove AI-provider watermarks from text, images, and documents.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// Copyright 2026 Mahmoud Harmouch.
//
// Licensed under the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! # CLI layer
//!
//! Defines the top-level [`Cli`] struct and every subcommand that the `cum`
//! binary exposes.  All heavy lifting is delegated to the library crate so
//! this module stays thin.

use crate::cleaner::{clean, inspect};
use crate::stochastic::StochasticEnhancer;
use crate::types::MediaHint;
use crate::unicode::{CleanOpts, InspectOpts, clean_text, inspect_text};
use anyhow::{Context, Result, bail};
use clap::builder::styling::{AnsiColor, Effects, Styles};
use clap::{Parser, Subcommand, ValueEnum};
use std::path::PathBuf;

fn styles() -> Styles {
    Styles::styled()
        .header(AnsiColor::Magenta.on_default() | Effects::BOLD)
        .usage(AnsiColor::Magenta.on_default() | Effects::BOLD)
        .literal(AnsiColor::Blue.on_default() | Effects::BOLD)
        .error(AnsiColor::Red.on_default() | Effects::BOLD)
        .placeholder(AnsiColor::Green.on_default())
}

/// ๐Ÿงน cum-rs: Claude Unmarking Machine
///
/// Remove AI-provider watermarks from text, images, and documents.
/// All processing is 100% local: no data is sent anywhere.
///
/// Repository: <https://github.com/wiseaidotdev/cum>
#[derive(Debug, Parser)]
#[command(
    name = "cum",
    author = "Mahmoud Harmouch <oss@wiseai.dev>",
    version = env!("CARGO_PKG_VERSION"),
    propagate_version = true,
    arg_required_else_help = true,
    styles = styles(),
    help_template = r#"{before-help}{about}

{usage-heading} {usage}

{all-args}{after-help}

AUTHORS:
    {author}
"#,
    about = r#"
 โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ•—   โ–ˆโ–ˆโ•—โ–ˆโ–ˆโ–ˆโ•—   โ–ˆโ–ˆโ–ˆโ•—
โ–ˆโ–ˆโ•”โ•โ•โ•โ•โ• โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ•‘
โ–ˆโ–ˆโ•‘      โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•”โ–ˆโ–ˆโ–ˆโ–ˆโ•”โ–ˆโ–ˆโ•‘
โ–ˆโ–ˆโ•‘      โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘โ•šโ–ˆโ–ˆโ•”โ•โ–ˆโ–ˆโ•‘
โ•šโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ•šโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•”โ•โ–ˆโ–ˆโ•‘ โ•šโ•โ• โ–ˆโ–ˆโ•‘
 โ•šโ•โ•โ•โ•โ•โ•  โ•šโ•โ•โ•โ•โ•โ• โ•šโ•โ•     โ•šโ•โ•
 Claude Unmarking Machine 
=========================

Remove AI-provider watermarks from text, images, and documents safely and completely offline.

FEATURES:
  - Text format checking: Strip Layer A Unicode carriers (ZWSP, Tags, Variations).
  - Confusable patching: --aggressive swaps Cyrillic / fullwidth homoglyphs back to Latin.
  - Image sweeping: Remove hidden C2PA or invisible EXIF tracking.
  - Document sweeping: Scrub hidden markers in PDF, DOCX, ODT.
  - Stochastic enhancement: Best-effort Layer B synonym substitution to defeat token-sampling watermarks.
  - Media auto-detection: Format guessed from magic bytes automatically.
  - Output formatting: --json output for programmatic scraping.

USAGE:
  cum [OPTIONS] <COMMAND>

EXAMPLES:
  - Clean inline text using --text (-t):
    cum clean --text "Hello world"

  - Strip watermarks from an image to an output file:
    cum clean profile.jpeg --output profile_clean.jpeg

  - Inspect metadata without modifying:
    cum inspect suspected.pdf

  - Aggressively fix spacing/confusables over stdin:
    cat prompt.txt | cum clean --stdin -a

  - Apply stochastic synonym replacement at 70% probability:
    cum enhance --text "The chaos governs the universe" --probability 0.7

  - Enhance a text file and save the result:
    cum enhance report.txt --probability 0.5 --output report_enhanced.txt

For more detail, check CLI.md or https://github.com/wiseaidotdev/cum
"#
)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Command,

    /// Output results as JSON (machine-readable).
    #[arg(long, short = 'j', global = true)]
    pub json: bool,

    /// Suppress all output except errors.
    #[arg(long, short = 'q', global = true)]
    pub quiet: bool,
}

#[derive(Debug, Subcommand)]
pub enum Command {
    /// Clean a file or text, removing all watermark carriers.
    #[command(visible_alias = "c")]
    Clean {
        /// Path to the file to clean.  Omit to read from `--text` or stdin.
        #[arg(value_name = "FILE")]
        file: Option<PathBuf>,

        /// Inline text to clean (alternative to supplying a file).
        #[arg(long, short = 't', value_name = "TEXT", conflicts_with = "file")]
        text: Option<String>,

        /// Read input from stdin.
        #[arg(long, conflicts_with_all = &["file", "text"])]
        stdin: bool,

        /// Write cleaned bytes to this path instead of stdout.
        #[arg(long, short = 'o', value_name = "OUT")]
        output: Option<PathBuf>,

        /// Also replace Cyrillic / fullwidth-Latin confusable letters.
        #[arg(long, short = 'a', default_value_t = true)]
        aggressive: bool,

        /// Force treat input as this media type (skipping auto-detection).
        #[arg(long, short = 'm', value_name = "MEDIA", value_enum)]
        media: Option<MediaArg>,
    },

    /// Inspect a file or text for watermark carriers without modifying it.
    #[command(visible_alias = "i")]
    Inspect {
        /// Path to the file to inspect.  Omit to pipe from `--text` or stdin.
        #[arg(value_name = "FILE")]
        file: Option<PathBuf>,

        /// Inline text to inspect.
        #[arg(long, short = 't', value_name = "TEXT", conflicts_with = "file")]
        text: Option<String>,

        /// Read input from stdin.
        #[arg(long, conflicts_with_all = &["file", "text"])]
        stdin: bool,

        /// Also check Cyrillic / fullwidth-Latin confusable letters.
        #[arg(long, short = 'a', default_value_t = true)]
        aggressive: bool,

        /// Force treat input as this media type.
        #[arg(long, short = 'm', value_name = "MEDIA", value_enum)]
        media: Option<MediaArg>,
    },

    /// Apply stochastic synonym substitution to defeat Layer B statistical watermarks.
    ///
    /// Each non-stop word is replaced with a synonym with the given probability,
    /// using a curated PHF table and the Linux system dictionary as fallback.
    #[command(visible_alias = "e")]
    Enhance {
        /// Path to the plain-text file to enhance.  Omit to use `--text` or stdin.
        #[arg(value_name = "FILE")]
        file: Option<PathBuf>,

        /// Inline text to enhance (alternative to supplying a file).
        #[arg(long, short = 't', value_name = "TEXT", conflicts_with = "file")]
        text: Option<String>,

        /// Read plain-text input from stdin.
        #[arg(long, conflicts_with_all = &["file", "text"])]
        stdin: bool,

        /// Write the enhanced output to this path instead of stdout.
        #[arg(long, short = 'o', value_name = "OUT")]
        output: Option<PathBuf>,

        /// Per-word synonym-substitution probability in the range `[0.0, 1.0]`.
        ///
        /// 0.0 disables all substitution; 1.0 replaces every eligible word.
        #[arg(long, short = 'p', value_name = "PROB", default_value_t = 0.5)]
        probability: f64,
    },

    /// Print the crate version.
    #[command(hide = true)]
    Version,
}

/// Explicit media type override (skips magic-byte auto-detection).
#[derive(Debug, Clone, ValueEnum)]
pub enum MediaArg {
    Text,
    Png,
    Jpeg,
    Webp,
    Svg,
    Pdf,
    Docx,
    Odt,
    Html,
    Markdown,
}

impl From<MediaArg> for MediaHint {
    fn from(a: MediaArg) -> Self {
        match a {
            MediaArg::Text => MediaHint::Text,
            MediaArg::Png => MediaHint::Png,
            MediaArg::Jpeg => MediaHint::Jpeg,
            MediaArg::Webp => MediaHint::Webp,
            MediaArg::Svg => MediaHint::Svg,
            MediaArg::Pdf => MediaHint::Pdf,
            MediaArg::Docx => MediaHint::Docx,
            MediaArg::Odt => MediaHint::Odt,
            MediaArg::Html => MediaHint::Html,
            MediaArg::Markdown => MediaHint::Markdown,
        }
    }
}

/// Entry-point called from `src/bin/main.rs`.
pub fn run(cli: Cli) -> Result<()> {
    match cli.command {
        Command::Version => {
            println!("{}", env!("CARGO_PKG_VERSION"));
        }

        Command::Clean {
            file,
            text,
            stdin,
            output,
            aggressive,
            media,
        } => {
            run_clean(
                file, text, stdin, output, aggressive, media, cli.json, cli.quiet,
            )?;
        }

        Command::Inspect {
            file,
            text,
            stdin,
            aggressive,
            media,
        } => {
            run_inspect(file, text, stdin, aggressive, media, cli.json)?;
        }

        Command::Enhance {
            file,
            text,
            stdin,
            output,
            probability,
        } => {
            run_enhance(file, text, stdin, output, probability, cli.json, cli.quiet)?;
        }
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn run_clean(
    file: Option<PathBuf>,
    text: Option<String>,
    stdin: bool,
    output: Option<PathBuf>,
    aggressive: bool,
    media: Option<MediaArg>,
    json: bool,
    quiet: bool,
) -> Result<()> {
    let (bytes, is_text_mode) = resolve_input(file.as_deref(), text.as_deref(), stdin)?;

    let (cleaned_bytes, removed, replaced) = if is_text_mode {
        let s = std::str::from_utf8(&bytes).context("input is not valid UTF-8")?;
        let opts = CleanOpts {
            aggressive_confusables: aggressive,
            ..CleanOpts::safe()
        };
        let (clean, stats) = clean_text(s, &opts)?;
        (
            clean.into_bytes(),
            stats.removed_count,
            stats.replaced_count,
        )
    } else {
        let hint = media.map(MediaHint::from);
        let out = clean(&bytes, hint)?;
        let r = out.stats.removed_count;
        let rp = out.stats.replaced_count;
        (out.bytes, r, rp)
    };

    if let Some(path) = output {
        std::fs::write(&path, &cleaned_bytes)
            .with_context(|| format!("writing to {}", path.display()))?;
        if !quiet {
            eprintln!(
                "โœ…  Wrote cleaned output โ†’ {} ({} stripped, {} replaced)",
                path.display(),
                removed,
                replaced
            );
        }
    } else {
        if is_text_mode {
            print!("{}", String::from_utf8_lossy(&cleaned_bytes));
        } else {
            use std::io::Write;
            std::io::stdout().write_all(&cleaned_bytes)?;
        }
        if !quiet && json {
            eprintln!(
                "{}",
                serde_json::json!({
                    "removed_count": removed,
                    "replaced_count": replaced,
                })
            );
        } else if !quiet {
            eprintln!("๐Ÿงน  {} stripped, {} replaced", removed, replaced);
        }
    }
    Ok(())
}

fn run_inspect(
    file: Option<PathBuf>,
    text: Option<String>,
    stdin: bool,
    aggressive: bool,
    media: Option<MediaArg>,
    json: bool,
) -> Result<()> {
    let (bytes, is_text_mode) = resolve_input(file.as_deref(), text.as_deref(), stdin)?;

    if is_text_mode {
        let s = std::str::from_utf8(&bytes).context("input is not valid UTF-8")?;
        let opts = InspectOpts {
            aggressive_confusables: aggressive,
            ..InspectOpts::default()
        };
        let report = inspect_text(s, &opts)?;
        if json {
            println!("{}", serde_json::to_string_pretty(&report)?);
        } else {
            let total = report.hits.iter().map(|h| h.count).sum::<usize>();
            if total == 0 {
                println!("โœ…  No watermarks detected.");
            } else {
                println!("โš ๏ธ   {} suspicious codepoint instance(s) found:", total);
                for hit in &report.hits {
                    println!(
                        "  U+{:04X}  {:?}  ร—{}  ({:?})",
                        hit.codepoint, hit.kind, hit.count, hit.confidence
                    );
                }
            }
        }
    } else {
        let hint = media.map(MediaHint::from);
        let out = inspect(&bytes, hint)?;
        if json {
            println!(
                "{}",
                serde_json::to_string_pretty(&serde_json::json!({
                    "format": format!("{:?}", out.format),
                    "meta_findings": out.meta_findings,
                }))?
            );
        } else {
            let findings = &out.meta_findings;
            if findings.is_empty() {
                println!("โœ…  No metadata watermarks found.");
            } else {
                println!("โš ๏ธ   {} finding(s):", findings.len());
                for f in findings {
                    println!("  [{:?}] {}", f.confidence, f.description);
                }
            }
        }
    }
    Ok(())
}

/// Applies stochastic synonym substitution to plain-text input.
///
/// # Complexity
/// - Time: O(t) where t is the number of whitespace-separated tokens.
/// - Space: O(t).
#[allow(clippy::too_many_arguments)]
fn run_enhance(
    file: Option<PathBuf>,
    text: Option<String>,
    stdin: bool,
    output: Option<PathBuf>,
    probability: f64,
    json: bool,
    quiet: bool,
) -> Result<()> {
    let (bytes, _) = resolve_input(file.as_deref(), text.as_deref(), stdin)?;
    let input = std::str::from_utf8(&bytes).context("input is not valid UTF-8")?;
    let enhancer = StochasticEnhancer::new(probability);
    let out = enhancer.enhance(input);

    if let Some(path) = output {
        std::fs::write(&path, out.text.as_bytes())
            .with_context(|| format!("writing to {}", path.display()))?;
        if !quiet {
            eprintln!(
                "โœจ  Wrote enhanced output โ†’ {} ({} words substituted, p={:.2})",
                path.display(),
                out.words_substituted,
                out.probability,
            );
        }
    } else {
        print!("{}", out.text);
        if !quiet && json {
            eprintln!(
                "{}",
                serde_json::json!({
                    "words_substituted": out.words_substituted,
                    "probability": out.probability,
                })
            );
        } else if !quiet {
            eprintln!(
                "โœจ  {} word(s) substituted (p={:.2})",
                out.words_substituted, out.probability,
            );
        }
    }
    Ok(())
}

/// Returns `(bytes, is_text_mode)`.
/// `is_text_mode` is true when the source is a raw `--text` string or stdin
/// without an explicit `--media` binary type.
fn resolve_input(
    file: Option<&std::path::Path>,
    text: Option<&str>,
    stdin: bool,
) -> Result<(Vec<u8>, bool)> {
    if let Some(t) = text {
        return Ok((t.as_bytes().to_vec(), true));
    }
    if stdin {
        use std::io::Read;
        let mut buf = Vec::new();
        std::io::stdin().read_to_end(&mut buf)?;
        return Ok((buf, false));
    }
    if let Some(path) = file {
        let bytes = std::fs::read(path).with_context(|| format!("reading {}", path.display()))?;
        let is_text = matches!(
            path.extension().and_then(|e| e.to_str()),
            Some("txt" | "md" | "markdown" | "html" | "htm")
        );
        return Ok((bytes, is_text));
    }
    bail!("Provide a FILE, --text TEXT, or --stdin")
}