cde 0.3.7

A universal encoding scheme for all cryptographic data
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
use crate::{idx, CryptoData, Error, Result, VarUInt, CDE_ALPHABET, ENCODER};
use std::fmt::{self, Debug, Display, Formatter};

// include the generated hashmaps
include!(concat!(env!("OUT_DIR"), "/hashmaps.rs"));

static NUMBERS: &'static str = "0123456789";
static UNDEFINED: &'static str = "undefined";

#[derive(Clone, Copy, Default, PartialEq)]
pub struct Tag {
    b: [u8; 2],
    l: VarUInt,
}

impl Tag {
    pub(crate) fn new(b: &[u8]) -> Self {
        let mut t = Tag::default();
        t.b.copy_from_slice(&b[0..2]);
        t.l = VarUInt::from(&b[2..]);
        t
    }

    pub fn set_data_length(&mut self, len: usize) {
        self.l = VarUInt::from(len);
    }

    pub fn get_data_length(&self) -> usize {
        self.l.into()
    }

    pub fn is_exp_class(&self) -> bool {
        (self.b[0] & 0x80) != 0
    }

    pub fn is_exp_sub_class(&self) -> bool {
        (self.b[0] & 0x02) != 0
    }

    pub fn set_exp_class(&mut self, exp: bool) {
        if exp {
            self.b[0] |= 0x80;
        } else {
            self.b[0] &= 0xf7;
        }
    }

    pub fn set_exp_sub_class(&mut self, exp: bool) {
        if exp {
            self.b[0] |= 0x02;
        } else {
            self.b[0] &= 0xfd;
        }
    }

    pub fn name(&self) -> Result<(&str, &str, Option<&str>)> {
        let n: [u8; 3] = [self.class(), self.subclass(), self.subsubclass()];
        let i: [usize; 3] = [n[0] as usize, n[1] as usize, n[2] as usize];

        if let Some((c, sc_map)) = NAMES.get(&n[0]) {
            if let Some((sc, ssc_map)) = sc_map.get(&n[1]) {
                if let Some(ssc_map) = ssc_map {
                    //NOTE: due to a bug in the phf maps, we cannot use 0-indexing
                    //so we use 1-indexing instead as a work-around
                    if let Some(ssc) = ssc_map.get(&(n[2] + 1)) {
                        return Ok((c, sc, Some(*ssc)));
                    } else {
                        return Ok((c, sc, NUMBERS.get(i[2]..i[2] + 1)));
                    }
                } else {
                    return Ok((c, sc, NUMBERS.get(i[2]..i[2] + 1)));
                }
            } else if let Some(sc) = CDE_ALPHABET.get(i[1]..i[1] + 1) {
                return Ok((c, sc, None));
            } else {
                return Ok((c, UNDEFINED, None));
            }
        } else if let Some(c) = CDE_ALPHABET.get(i[0]..i[0] + 1) {
            if let Some(sc) = CDE_ALPHABET.get(i[1]..i[1] + 1) {
                return Ok((c, sc, None));
            } else {
                return Ok((c, UNDEFINED, None));
            }
        } else {
            return Ok((UNDEFINED, UNDEFINED, None));
        }
    }

    pub fn class(&self) -> u8 {
        ((self.b[0] & 0xfc) >> 2) & 0x3f
    }

    pub fn subclass(&self) -> u8 {
        (((self.b[0] & 0x03) << 4) | ((self.b[1] & 0xf0) >> 4)) & 0x3f
    }

    pub fn subsubclass(&self) -> u8 {
        self.b[1] & 0x0f
    }
}

impl CryptoData for Tag {
    fn len(&self) -> usize {
        2 + self.l.len()
    }

    fn bytes(&self, buf: &mut [u8]) -> usize {
        buf[0..2].copy_from_slice(&self.b);
        self.l.bytes(&mut buf[2..self.len()]);
        self.len()
    }

    fn encode_len(&self) -> usize {
        ENCODER.encode_len(self.len())
    }

    fn encode(&self, buf: &mut [u8]) -> usize {
        let mut b = [0u8; 9];
        let len = self.bytes(&mut b);
        ENCODER.encode_mut(&b[0..len], &mut buf[0..self.encode_len()]);
        self.encode_len()
    }
}

enum TagBuildFrom {
    Tag,
    Bytes,
    Encoded,
}

pub struct TagBuilder<'a> {
    how: TagBuildFrom,
    tag: Option<&'a str>,
    bytes: Option<&'a [u8]>,
}

// create a tag in the provided buffer copying from the bytes slice
// let tt = TagBuilder::from_bytes(&bytes).build().unwrap();
//
// create a tag in the provided buffer from the type string
// let tt = TagBuilder::from_tag("key.p256.secret").build().unwrap();

impl<'a> TagBuilder<'a> {
    pub fn from_tag(s: &'a str) -> Self {
        TagBuilder {
            how: TagBuildFrom::Tag,
            tag: Some(s),
            bytes: None,
        }
    }

    pub fn from_bytes(b: &'a [u8]) -> Self {
        TagBuilder {
            how: TagBuildFrom::Bytes,
            tag: None,
            bytes: Some(b),
        }
    }

    pub fn from_encoded(e: &'a [u8]) -> Self {
        TagBuilder {
            how: TagBuildFrom::Encoded,
            tag: None,
            bytes: Some(e),
        }
    }

    pub fn build(&self) -> Result<Tag> {
        let mut buf = [0u8; 9];
        let tag = match self.how {
            TagBuildFrom::Tag => {
                if let Some(tag) = self.tag {
                    TagBuilder::decode_str(tag, &mut buf)?;
                    Tag::new(&buf)
                } else {
                    return Err(Error::FromStr);
                }
            }
            TagBuildFrom::Bytes => {
                if let Some(bytes) = self.bytes {
                    if bytes.len() < 3 {
                        return Err(Error::InvalidLength);
                    } else {
                        if let Ok(_) = VarUInt::try_parse(&bytes[2..]) {
                            Tag::new(&bytes)
                        } else {
                            return Err(Error::InvalidLength);
                        }
                    }
                } else {
                    return Err(Error::InvalidLength);
                }
            }
            TagBuildFrom::Encoded => {
                if let Some(bytes) = self.bytes {
                    ENCODER
                        .decode_mut(&bytes[0..4], &mut buf[0..3])
                        .map_err(|_| Error::DecodeError)?;
                    if (buf[2] & 0x80) != 0 {
                        ENCODER
                            .decode_mut(&bytes[4..8], &mut buf[3..6])
                            .map_err(|_| Error::DecodeError)?;
                    }
                    if (buf[5] & 0x80) != 0 {
                        ENCODER
                            .decode_mut(&bytes[8..12], &mut buf[6..9])
                            .map_err(|_| Error::DecodeError)?;
                    }
                    Tag::new(&buf)
                } else {
                    return Err(Error::DecodeError);
                }
            }
        };

        Ok(tag)
    }

    /// This takes a tag string name like "key.ed25519.public" and parses it
    /// into a Tag containing the correct class, sub-class, and sub-sub-class
    /// values. The length is initiatlized to zero.
    fn decode_str(tag: &str, buf: &mut [u8]) -> Result<()> {
        // checks if the value is experimental
        fn experimental(v: u8) -> bool {
            (v > 31) && (v != 63)
        }

        /// If the str is a single character
        fn name_or_char(v: &str) -> Option<u8> {
            if v.len() >= 1 {
                if let Some(c) = v.chars().next() {
                    if let Some(c) = CDE_ALPHABET.find(c) {
                        return Some(c as u8);
                    }
                }
            }
            None
        }

        let parts = match tag.len() {
            0 => (None, None, None),
            _ => {
                let mut s = tag.split('.');
                (s.next(), s.next(), s.next())
            }
        };

        let (c, sc, ssc) = match parts {
            (Some(c_name), Some(sc_name), Some(ssc_name)) => {
                match VALUES.get(c_name) {
                    None => {
                        if let Some(c) = name_or_char(c_name) {
                            // if we get here they specified a non-standard...
                            if !experimental(c) {
                                // ...it must be experimental or it is an error...
                                return Err(Error::InvalidClass);
                            } else if let Some(sc) = name_or_char(sc_name) {
                                if !experimental(sc) {
                                    // ...the sub-class must be experimental or it is an error
                                    return Err(Error::InvalidSubClass);
                                } else if let Ok(ssc) = u8::from_str_radix(ssc_name, 10) {
                                    // ...both class and sub-class are experimental so
                                    // also return the sub-sub-class number
                                    (c, sc, ssc)
                                } else {
                                    // ...the sub-sub-class was not a base 10 number
                                    return Err(Error::InvalidSubSubClass);
                                }
                            } else {
                                // ...the sub-class value wasn't a string
                                return Err(Error::InvalidSubClass);
                            }
                        } else {
                            // .. the class value wasn't a string
                            return Err(Error::InvalidClass);
                        }
                    }
                    Some((c, sc_map)) => {
                        // the class name was a standard class name
                        match sc_map.get(sc_name) {
                            None => {
                                // the sub-class was not a standard sub-class name
                                if !experimental(*c) {
                                    // ...the class must be experimental or it is an error
                                    return Err(Error::InvalidClass);
                                } else if let Some(sc) = name_or_char(sc_name) {
                                    if !experimental(sc) {
                                        // ...the sub-class must be experimental or it is an error
                                        return Err(Error::InvalidSubClass);
                                    } else if let Ok(ssc) = u8::from_str_radix(ssc_name, 10) {
                                        // ...both class and sub-class are experimental so
                                        // also return the sub-sub-class number
                                        (*c, sc, ssc)
                                    } else {
                                        // ...the sub-sub-class was not a base 10 number
                                        return Err(Error::InvalidSubSubClass);
                                    }
                                } else {
                                    // ...the sub-class value wasn't a string
                                    return Err(Error::InvalidSubClass);
                                }
                            }
                            Some((sc, ssc_map)) => {
                                // the sub-class name was a standard class name
                                match ssc_map {
                                    None => {
                                        // there are no sub-sub-classes for this class and
                                        // sub-class combination
                                        if experimental(*c) {
                                            if experimental(*sc) {
                                                // both the class and sub-class are standard
                                                // and experimental so just return them with
                                                // the experimental sub-sub-class
                                                if let Ok(ssc) = u8::from_str_radix(ssc_name, 10) {
                                                    (*c, *sc, ssc)
                                                } else {
                                                    // the sub-sub-class was not a base 10 number
                                                    return Err(Error::InvalidSubSubClass);
                                                }
                                            } else {
                                                // an experimental class with a non-experimental
                                                // sub-class is an error
                                                return Err(Error::InvalidSubClass);
                                            }
                                        } else {
                                            // this is a standard class and standard sub-class
                                            // without any standard sub-sub-classes so the
                                            // sub-class must be experimental
                                            if !experimental(*sc) {
                                                // there is a special corner case to take into
                                                // account here... both "undefined" ('_') and list
                                                // ('-') are not considered experimental but we
                                                // allow list.list, undefined.list, and
                                                // undefined.undefined to have sub-sub-classes set
                                                // so that user can have different kinds of these
                                                // types
                                                if (*c == idx('_')
                                                    && (*sc == idx('_') || *sc == idx('-')))
                                                    || (*c == idx('-') && *sc == idx('-'))
                                                {
                                                    if let Ok(ssc) =
                                                        u8::from_str_radix(ssc_name, 10)
                                                    {
                                                        (*c, *sc, ssc)
                                                    } else {
                                                        return Err(Error::InvalidSubSubClass);
                                                    }
                                                } else {
                                                    return Err(Error::InvalidSubClass);
                                                }
                                            } else if let Ok(ssc) = u8::from_str_radix(ssc_name, 10)
                                            {
                                                // the sub-class is experimental so just get the
                                                // sub-sub-class number and return all three
                                                (*c, *sc, ssc)
                                            } else {
                                                // the sub-sub-class was not a base 10 number
                                                return Err(Error::InvalidSubSubClass);
                                            }
                                        }
                                    }
                                    Some(ssc_map) => {
                                        // there are sub-sub-classes for this class and
                                        // sub-class combination
                                        if experimental(*c) {
                                            if experimental(*sc) {
                                                // both the class and sub-class are standard
                                                // and experimental so just return them with
                                                // the experimental sub-sub-class
                                                match ssc_map.get(ssc_name) {
                                                    None => {
                                                        if let Ok(ssc) =
                                                            u8::from_str_radix(ssc_name, 10)
                                                        {
                                                            (*c, *sc, ssc)
                                                        } else {
                                                            return Err(Error::InvalidSubSubClass);
                                                        }
                                                    }
                                                    // subtract 1 from the sub-sub-class number
                                                    // because of a bug in the phf map we had to
                                                    // use 1-indexed maps instead of 0-indexed maps
                                                    Some(ssc) => (*c, *sc, *ssc - 1),
                                                }
                                            } else {
                                                // an experimental class with a non-experimental
                                                // sub-class is an error
                                                return Err(Error::InvalidSubClass);
                                            }
                                        } else {
                                            // the class is not experimental so it doesn't matter
                                            // if the sub-class is experimental or not...just get
                                            // the sub-sub-class number and return all three
                                            match ssc_map.get(ssc_name) {
                                                None => {
                                                    if let Ok(ssc) =
                                                        u8::from_str_radix(ssc_name, 10)
                                                    {
                                                        (*c, *sc, ssc)
                                                    } else {
                                                        return Err(Error::InvalidSubSubClass);
                                                    }
                                                }
                                                // subtract 1 from the sub-sub-class number
                                                // because of a bug in the phf map we had to
                                                // use 1-indexed maps instead of 0-indexed maps
                                                Some(ssc) => (*c, *sc, *ssc - 1),
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            (Some(c_name), Some(sc_name), None) => {
                match VALUES.get(c_name) {
                    None => {
                        // the class is non-standard...
                        if let Some(c) = name_or_char(c_name) {
                            if !experimental(c) {
                                // ...it must be experimental or it is an error
                                return Err(Error::InvalidClass);
                            } else if let Some(sc) = name_or_char(sc_name) {
                                if !experimental(sc) {
                                    // ...and therefore the sub-class must also be
                                    // experimental or it is an error
                                    return Err(Error::InvalidSubClass);
                                } else {
                                    // it is OK to specify an experimental non-standard class and
                                    // experimental non-standard sub-class without a sub-sub-class.
                                    // the sub-sub-class just defaults to 0
                                    (c, sc, 0)
                                }
                            } else {
                                // the sub-class name wasn't a string
                                return Err(Error::InvalidSubClass);
                            }
                        } else {
                            // the class name wasn't a string
                            return Err(Error::InvalidClass);
                        }
                    }
                    Some((c, sc_map)) => {
                        // the class is standard...
                        match sc_map.get(sc_name) {
                            None => {
                                // ...the sub-class name is non-standard...
                                if !experimental(*c) {
                                    // ...the class must be experimental or it is an error...
                                    return Err(Error::InvalidClass);
                                } else if let Some(sc) = name_or_char(sc_name) {
                                    if !experimental(sc) {
                                        // ...the sub-class must also be experimental
                                        // or it is an error
                                        return Err(Error::InvalidSubClass);
                                    } else {
                                        // it is OK to specify an experimental standard class and
                                        // an experimental non-standard sub-class without
                                        // specifying a sub-sub-class. the sub-sub-class just
                                        // defaults to 0
                                        (*c, sc, 0)
                                    }
                                } else {
                                    // ...the sub-class name wasn't a string
                                    return Err(Error::InvalidSubClass);
                                }
                            }
                            Some((sc, ssc_map)) => {
                                // ...the sub-class name is standard...
                                match ssc_map {
                                    None => {
                                        // standard class and standard sub-class without any
                                        // standard sub-sub-classes are allowed
                                        (*c, *sc, 0)
                                    }
                                    Some(_) => {
                                        // if we get here, there is a sub-sub-class
                                        // map and they didn't specify which sub-sub-class
                                        // this is an error
                                        return Err(Error::InvalidSubSubClass);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            (Some(_), None, None) => {
                // there is no valid case where just the class is specified
                return Err(Error::InvalidSubClass);
            }

            (None, None, None) => {
                // there is no valid case where nothing is specified...
                // this case is triggered if the string is the empty string
                return Err(Error::InvalidClass);
            }

            // everything other combination is invalid...
            _ => return Err(Error::FromStr),
        };

        buf[0] = (((c & 0x3f) << 2) & 0xfc) | (((sc & 0x30) >> 4) & 0x03);
        buf[1] = (((sc & 0x0f) << 4) & 0xf0) | (ssc & 0x07);
        buf[2] = 0;

        Ok(())
    }
}

impl Display for Tag {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        if let Ok((c, sc, ssc)) = self.name() {
            if let Some(ssc) = ssc {
                write!(f, "{}.{}.{}", c, sc, ssc)
            } else {
                write!(f, "{}.{}", c, sc)
            }
        } else {
            Err(core::fmt::Error)
        }
    }
}

impl Debug for Tag {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let mut b = [0u8; 12];
        self.encode(&mut b);
        let s = core::str::from_utf8(&mut b).unwrap();
        let mut i = s.chars();
        let (cn, scn, sscn) = self.name().unwrap();
        let sscn = match sscn {
            None => "None",
            Some(sscn) => sscn,
        };
        match self.len() {
            3 => {
                let c: [char; 4] = [
                    i.next().unwrap(),
                    i.next().unwrap(),
                    i.next().unwrap(),
                    i.next().unwrap(),
                ];

                writeln!(f, "       encoding unit 1")?;
                writeln!(f, " /--------------------------/")?;
                writeln!(
                    f,
                    "/--{}--//--{}--//--{}--//--{}--/",
                    c[0], c[1], c[2], c[3]
                )?;
                writeln!(
                    f,
                    "{:06b} {:06b} {:06b} {:06b}",
                    idx(c[0]),
                    idx(c[1]),
                    idx(c[2]),
                    idx(c[3])
                )?;
                writeln!(f, "||   | ||   | |  ||       |")?;
                writeln!(
                    f,
                    "||   | ||   | |  |+-------+.. len: {}",
                    self.get_data_length()
                )?;
                writeln!(f, "||   | ||   | +--+........... sub-sub-class: {}", sscn)?;
                writeln!(f, "||   | |+---+................ sub-class: {}", scn)?;
                writeln!(
                    f,
                    "||   | +..................... exp. sub-class: {}",
                    self.is_exp_sub_class()
                )?;
                writeln!(f, "|+---+....................... class: {}", cn)?;
                writeln!(
                    f,
                    "+............................ exp. class: {}",
                    self.is_exp_class()
                )?;
            }
            6 => {
                let c: [char; 8] = [
                    i.next().unwrap(),
                    i.next().unwrap(),
                    i.next().unwrap(),
                    i.next().unwrap(),
                    i.next().unwrap(),
                    i.next().unwrap(),
                    i.next().unwrap(),
                    i.next().unwrap(),
                ];

                writeln!(
                    f,
                    "       encoding unit 1          optional encoding unit 2"
                )?;
                writeln!(
                    f,
                    " /--------------------------/ /--------------------------/"
                )?;
                writeln!(
                    f,
                    "/--{}--//--{}--//--{}--//--{}--/ /--{}--//--{}--//--{}--//--{}--/",
                    c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7]
                )?;
                writeln!(
                    f,
                    "{:06b} {:06b} {:06b} {:06b}  {:06b} {:06b} {:06b} {:06b}",
                    idx(c[0]),
                    idx(c[1]),
                    idx(c[2]),
                    idx(c[3]),
                    idx(c[4]),
                    idx(c[5]),
                    idx(c[6]),
                    idx(c[7])
                )?;
                writeln!(
                    f,
                    "||   | ||   | |  ||                                    |"
                )?;
                writeln!(
                    f,
                    "||   | ||   | |  |+-------+--+-------++-------++-------+"
                )?;
                writeln!(
                    f,
                    "||   | ||   | |  |        |.. len: {}",
                    self.get_data_length()
                )?;
                writeln!(f, "||   | ||   | +--+........... sub-sub-class: {}", sscn)?;
                writeln!(f, "||   | |+---+................ sub-class: {}", scn)?;
                writeln!(
                    f,
                    "||   | +..................... exp. sub-class: {}",
                    self.is_exp_sub_class()
                )?;
                writeln!(f, "|+---+....................... class: {}", cn)?;
                writeln!(
                    f,
                    "+............................ exp. class: {}",
                    self.is_exp_class()
                )?;
            }
            9 => {
                let c: [char; 12] = [
                    i.next().unwrap(),
                    i.next().unwrap(),
                    i.next().unwrap(),
                    i.next().unwrap(),
                    i.next().unwrap(),
                    i.next().unwrap(),
                    i.next().unwrap(),
                    i.next().unwrap(),
                    i.next().unwrap(),
                    i.next().unwrap(),
                    i.next().unwrap(),
                    i.next().unwrap(),
                ];

                writeln!(f, "       encoding unit 1          optional encoding unit 2     optional encoding unit 3")?;
                writeln!(f, " /--------------------------/ /--------------------------/ /--------------------------/")?;
                writeln!(f, "/--{}--//--{}--//--{}--//--{}--/ /--{}--//--{}--//--{}--//--{}--/ /--{}--//--{}--//--{}--//--{}--/",
                            c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], c[10], c[11])?;
                writeln!(f, "{:06b} {:06b} {:06b} {:06b}  {:06b} {:06b} {:06b} {:06b}  {:06b} {:06b} {:06b} {:06b}",
                            idx(c[0]), idx(c[1]), idx(c[2]), idx(c[3]),
                            idx(c[4]), idx(c[5]), idx(c[6]), idx(c[7]),
                            idx(c[8]), idx(c[9]), idx(c[10]), idx(c[11]))?;
                writeln!(f, "||   | ||   | |  ||                                                                  |")?;
                writeln!(f, "||   | ||   | |  |+-------+--+-------++-------++-------++--------++--------++--------+")?;
                writeln!(
                    f,
                    "||   | ||   | |  |        |.. len: {}",
                    self.get_data_length()
                )?;
                writeln!(f, "||   | ||   | +--+........... sub-sub-class: {}", sscn)?;
                writeln!(f, "||   | |+---+................ sub-class: {}", scn)?;
                writeln!(
                    f,
                    "||   | +..................... exp. sub-class: {}",
                    self.is_exp_sub_class()
                )?;
                writeln!(f, "|+---+....................... class: {}", cn)?;
                writeln!(
                    f,
                    "+............................ exp. class: {}",
                    self.is_exp_class()
                )?;
            }
            _ => {
                return Err(fmt::Error);
            }
        }
        Ok(())
    }
}