summaryrefslogtreecommitdiffstats
path: root/tools/qrcheck/src/main.rs
blob: d294485e126e6b5d094212f76accf69686b0cd83 (plain) (blame)
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
//! Host-side cross-check tool for the kernel QR encoder.
//!
//! Usage: `qrcheck TEXT [VERSION] [MASK]`
//!   - with VERSION+MASK: force both and dump the matrix (compared against
//!     segno by `tools/qrcheck.py`);
//!   - with neither: auto-pick version and best mask, print `v<ver> m<mask>`.
//!
//! Output: first line `v<N> m<N|999>`, then one line per matrix row of
//! `#` (dark) / `.` (light).
#![allow(static_mut_refs)]

#[path = "../../../rsnix/src/qr.rs"]
mod qr;

fn main() {
    let args: Vec<String> = std::env::args().collect();
    if args.get(1).map(String::as_str) == Some("--raw") {
        let text = args.get(2).map(String::as_str).unwrap_or("hello");
        let version: usize = args.get(3).and_then(|s| s.parse().ok()).unwrap_or(0);
        let v = if version > 0 {
            qr::encode_at(text.as_bytes(), version, Some(0))
        } else {
            qr::encode(text.as_bytes())
        };
        if v == 0 {
            eprintln!("FAIL");
            std::process::exit(1);
        }
        for b in qr::codeword_stream() {
            print!("{:02x}", b);
        }
        println!();
        return;
    }
    let text = args.get(1).map(String::as_str).unwrap_or("hello world");
    let version: usize = args.get(2).and_then(|s| s.parse().ok()).unwrap_or(0);
    let mask: Option<usize> = args.get(3).and_then(|s| s.parse().ok());

    let v = if version > 0 {
        qr::encode_at(text.as_bytes(), version, mask)
    } else {
        qr::encode(text.as_bytes())
    };
    if v == 0 {
        eprintln!("FAIL: cannot encode {} bytes at v{}", text.len(), version);
        std::process::exit(1);
    }
    let size = qr::size();
    let m = if let Some(m) = mask { m } else { qr::last_mask() };
    println!("v{} m{}", v, m);
    for y in 0..size {
        let mut row = String::with_capacity(size);
        for x in 0..size {
            row.push(if qr::module(x, y) { '#' } else { '.' });
        }
        println!("{}", row);
    }
}