attr_cat/
attr_cat.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5extern crate nss_certdata_parser;
6
7use std::fs::File;
8use std::env::args;
9use std::io::BufReader;
10
11use nss_certdata_parser::reader::AttrIter;
12use nss_certdata_parser::syntax::Value;
13
14// cf. perl -C0 -ne 's/^([^"]*)#.*/$1/;s/\\x(..)/chr(hex($1))/eg;print if /\S/'
15// (Note that the certdata.txt has both hex-escaped and unescaped non-ASCII chars.)
16
17fn main() {
18    for path in args().skip(1) {
19        println!("BEGINDATA");
20        for res_attr in AttrIter::new(BufReader::new(File::open(path).unwrap())) {
21            // This might be useful as part of the actual library....
22            match res_attr.unwrap() {
23                (k, Value::Token(t, v)) => println!("{} {} {}", k, t, v),
24                // This isn't quite right -- it `\u{...}` escapes non-ASCII.
25                // (Hint: perl -C7 -pe 's/\\u\{(.+?)\}/chr(hex($1))/eg')
26                (k, Value::String(v)) => println!("{} UTF8 {:?}", k, v),
27                (k, Value::Binary(v)) => {
28                    print!("{} MULTILINE_OCTAL", k);
29                    for (i, b) in v.into_iter().enumerate() {
30                        if i % 16 == 0 {
31                            println!("");
32                        }
33                        print!("\\{:03o}", b);
34                    }
35                    println!("");
36                    println!("END");
37                }
38            }
39        }
40    }
41}