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
pub mod tables;

use crate::core::util;
use crate::error::{err, Error, Result};

#[derive(Debug, Clone, PartialEq)]
pub struct Counter {
    pub(crate) code: String,
    pub(crate) count: u32,
}

impl Counter {
    pub fn new(
        count: Option<u32>,
        count_b64: Option<&str>,
        code: Option<&str>,
        qb64b: Option<&[u8]>,
        qb64: Option<&str>,
        qb2: Option<&[u8]>,
    ) -> Result<Self> {
        if let Some(code) = code {
            let count = if let Some(count) = count {
                count
            } else if let Some(count_b64) = count_b64 {
                util::b64_to_u32(count_b64)?
            } else {
                1
            };

            Self::new_with_code_and_count(code, count)
        } else if let Some(qb64b) = qb64b {
            Self::new_with_qb64b(qb64b)
        } else if let Some(qb64) = qb64 {
            Self::new_with_qb64(qb64)
        } else if let Some(qb2) = qb2 {
            Self::new_with_qb2(qb2)
        } else {
            err!(Error::Validation("need either code and count, qb64b, qb64 or qb2".to_string()))
        }
    }

    pub fn code(&self) -> String {
        self.code.clone()
    }

    pub fn count(&self) -> u32 {
        self.count
    }

    pub fn count_as_b64(&self, length: usize) -> Result<String> {
        let length = if length == 0 { tables::sizage(&self.code())?.ss as usize } else { length };
        util::u32_to_b64(self.count(), length)
    }

    pub fn qb64(&self) -> Result<String> {
        self.infil()
    }

    pub fn qb64b(&self) -> Result<Vec<u8>> {
        Ok(self.qb64()?.as_bytes().to_vec())
    }

    pub fn qb2(&self) -> Result<Vec<u8>> {
        self.binfil()
    }

    pub fn sem_ver_str_to_b64(version: &str) -> Result<String> {
        let strings = version.split('.').collect::<Vec<_>>();
        let mut parts = vec![0u8; 3];

        if strings.len() > 3 {
            return err!(Error::Conversion(format!(
                "invalid semantic version: version = '{version}'"
            )));
        }

        for i in 0..strings.len() {
            let n = match strings[i].parse::<i8>() {
                Ok(n) => {
                    if n < 0 {
                        return err!(Error::Conversion(format!(
                            "invalid semantic version: version = '{version}'"
                        )));
                    } else {
                        n as u8
                    }
                }
                Err(_) => {
                    if strings[i].is_empty() {
                        0
                    } else {
                        return err!(Error::Conversion(format!(
                            "invalid semantic version: version = '{version}'"
                        )));
                    }
                }
            };
            parts[i] = n;
        }

        Counter::sem_ver_parts_to_b64(&parts)
    }

    pub fn sem_ver_to_b64(major: u8, minor: u8, patch: u8) -> Result<String> {
        let parts = &vec![major, minor, patch];
        Counter::sem_ver_parts_to_b64(parts)
    }

    pub fn new_with_code_and_count(code: &str, count: u32) -> Result<Self> {
        if code.is_empty() {
            return err!(Error::EmptyMaterial("empty code".to_string()));
        }

        let szg = tables::sizage(code)?;
        let cs = szg.hs + szg.ss;
        if szg.fs != cs || cs % 4 != 0 {
            // unreachable
            // code validated and unless sizages are broken this cannot be reached
            return err!(Error::InvalidCodeSize(format!(
                "whole code size not a multiple of 4: cs = {cs}, fs = {}",
                szg.fs
            )));
        }

        if count > 64_u32.pow(szg.ss) - 1 {
            return err!(Error::InvalidVarIndex(format!(
                "invalid count for code: count = {count}, code = '{code}'"
            )));
        }

        Ok(Counter { code: code.to_string(), count })
    }

    pub fn new_with_qb64(qb64: &str) -> Result<Self> {
        let mut counter: Counter = Default::default();
        counter.exfil(qb64)?;
        Ok(counter)
    }

    pub fn new_with_qb64b(qb64b: &[u8]) -> Result<Self> {
        let qb64 = String::from_utf8(qb64b.to_vec())?;

        let mut counter: Counter = Default::default();
        counter.exfil(&qb64)?;
        Ok(counter)
    }

    pub fn new_with_qb2(qb2: &[u8]) -> Result<Self> {
        let mut counter: Counter = Default::default();
        counter.bexfil(qb2)?;
        Ok(counter)
    }

    fn sem_ver_parts_to_b64(parts: &[u8]) -> Result<String> {
        for p in parts.iter().copied() {
            if p > 63 {
                return err!(Error::Parsing(format!(
                    "semantic version out of bounds: parts = {parts:?}"
                )));
            }
        }

        Ok(parts
            .iter()
            .map(|p| {
                match util::u32_to_b64(*p as u32, 1) {
                    Ok(s) => s,
                    Err(_) => unreachable!(), // this is programmer error, since *p < 64
                }
            })
            .collect::<Vec<String>>()
            .join(""))
    }

    fn infil(&self) -> Result<String> {
        let code = &self.code();
        let count = self.count();

        let szg = tables::sizage(code)?;
        let cs = szg.hs + szg.ss;

        if szg.fs != cs || cs % 4 != 0 {
            // unreachable
            // unless sizages are broken this cannot happen
            return err!(Error::InvalidCodeSize(format!(
                "whole code size not complete or not a multiple of 4: cs = {cs}, fs = {}",
                szg.fs
            )));
        }

        if count > 64_u32.pow(szg.ss) - 1 {
            return err!(Error::InvalidVarIndex(format!(
                "invalid count for code: count = {count}, code = '{code}'"
            )));
        }

        let both = format!("{code}{}", util::u32_to_b64(count, szg.ss as usize)?);
        if both.len() != cs as usize {
            // unreachable
            // unless sizages are broken, we constructed both to be of length cs
            return err!(Error::InvalidCodeSize(format!(
                "mismatched code size: size = {}, code = '{both}'",
                both.len()
            )));
        }

        Ok(both)
    }

    fn binfil(&self) -> Result<Vec<u8>> {
        let both = self.infil()?;
        util::code_b64_to_b2(&both)
    }

    fn exfil(&mut self, qb64: &str) -> Result<()> {
        if qb64.is_empty() {
            return err!(Error::EmptyMaterial("empty qb64".to_string()));
        }

        // we validated there will be a char here, above.
        let first = &qb64[..2];

        let hs = tables::hardage(first)? as usize;
        if qb64.len() < hs {
            return err!(Error::Shortage(format!(
                "insufficient material for hard part of code: qb64 size = {}, hs = {hs}",
                qb64.len()
            )));
        }

        // bounds already checked
        let hard = &qb64[..hs];
        let szg = tables::sizage(hard)?;
        let cs = szg.hs + szg.ss;

        if qb64.len() < cs as usize {
            return err!(Error::Shortage(format!(
                "insufficient material for code: qb64 size = {}, cs = {cs}",
                qb64.len()
            )));
        }

        let count_b64 = &qb64[szg.hs as usize..cs as usize];
        let count = util::b64_to_u64(count_b64)? as u32;

        self.code = hard.to_string();
        self.count = count;

        Ok(())
    }

    fn bexfil(&mut self, qb2: &[u8]) -> Result<()> {
        if qb2.is_empty() {
            return err!(Error::EmptyMaterial("empty qualified base2".to_string()));
        }

        let first = util::nab_sextets(qb2, 2)?;
        if first[0] > 0x3e {
            if first[0] == 0x3f {
                return err!(Error::UnexpectedOpCode(
                    "unexpected start during extraction".to_string(),
                ));
            } else {
                // unreachable
                // programmer error - nab_sextets ensures values fall below 0x40. the only possible
                // value is 0x3f, and we handle it
                return err!(Error::UnexpectedCode(format!(
                    "unexpected code start: sextets = {first:?}"
                )));
            }
        }

        let hs = tables::bardage(&first)?;
        let bhs = ((hs + 1) * 3) / 4;
        if qb2.len() < bhs as usize {
            return err!(Error::Shortage(format!(
                "need more bytes: qb2 size = {}, bhs = {bhs}",
                qb2.len()
            )));
        }

        let hard = util::code_b2_to_b64(qb2, hs as usize)?;
        let szg = tables::sizage(&hard)?;
        let cs = szg.hs + szg.ss;
        let bcs = ((cs + 1) * 3) / 4;
        if qb2.len() < bcs as usize {
            return err!(Error::Shortage(format!(
                "need more bytes: qb2 size = {}, bcs = {bcs}",
                qb2.len()
            )));
        }

        let both = util::code_b2_to_b64(qb2, cs as usize)?;
        let mut count = 0;
        for c in both[hs as usize..cs as usize].chars() {
            count <<= 6;
            count += util::b64_char_to_index(c)? as u32;
        }

        self.code = hard;
        self.count = count;

        Ok(())
    }

    pub fn full_size(&self) -> Result<u32> {
        Ok(tables::sizage(&self.code())?.fs)
    }
}

impl Default for Counter {
    fn default() -> Self {
        Counter { code: "".to_string(), count: 0 }
    }
}

#[cfg(test)]
mod test {
    use crate::core::counter::{tables as counter, Counter};
    use base64::{engine::general_purpose as b64_engine, Engine};
    use rstest::rstest;

    #[rstest]
    #[case("-AAB", 1, "B", counter::Codex::ControllerIdxSigs)]
    #[case("-AAF", 5, "F", counter::Codex::ControllerIdxSigs)]
    #[case("-0VAAAQA", 1024, "QA", counter::Codex::BigAttachedMaterialQuadlets)]
    fn new(#[case] qsc: &str, #[case] count: u32, #[case] count_b64: &str, #[case] code: &str) {
        assert!(Counter::new(None, None, None, None, None, None).is_err());
        let counter = Counter::new(None, None, Some(code), None, None, None).unwrap();
        assert_eq!(counter.count(), 1);

        let counter1 = Counter::new(Some(count), None, Some(code), None, None, None).unwrap();
        let counter2 = Counter::new(None, Some(count_b64), Some(code), None, None, None).unwrap();
        let counter3 = Counter::new(None, None, None, None, Some(qsc), None).unwrap();

        assert_eq!(counter1.code(), code);
        assert_eq!(counter2.code(), code);
        assert_eq!(counter3.code(), code);
        assert_eq!(counter1.count(), count);
        assert_eq!(counter2.count(), count);
        assert_eq!(counter3.count(), count);

        let qb64b = counter1.qb64b().unwrap();
        let qb2 = counter1.qb2().unwrap();

        assert!(Counter::new(None, None, None, Some(&qb64b), None, None).is_ok());
        assert!(Counter::new(None, None, None, None, None, Some(&qb2)).is_ok());
    }

    #[rstest]
    #[case("-AAB", 1, "B", counter::Codex::ControllerIdxSigs)]
    #[case("-AAF", 5, "F", counter::Codex::ControllerIdxSigs)]
    #[case("-0VAAAQA", 1024, "QA", counter::Codex::BigAttachedMaterialQuadlets)]
    fn creation(
        #[case] qsc: &str,
        #[case] count: u32,
        #[case] count_b64: &str,
        #[case] code: &str,
    ) {
        let qscb = qsc.as_bytes();
        let qscb2 = b64_engine::URL_SAFE.decode(qsc).unwrap();

        let counter1 = Counter::new(Some(count), None, Some(code), None, None, None).unwrap();
        let counter2 = Counter::new(None, Some(count_b64), Some(code), None, None, None).unwrap();
        let counter3 = Counter::new(None, None, None, None, Some(qsc), None).unwrap();
        let counter4 = Counter::new(None, None, None, Some(qscb), None, None).unwrap();
        let counter5 = Counter::new(None, None, None, None, None, Some(&qscb2)).unwrap();

        assert_eq!(counter1.code(), counter2.code());
        assert_eq!(counter1.count(), counter2.count());
        assert_eq!(counter1.code(), counter3.code());
        assert_eq!(counter1.count(), counter3.count());
        assert_eq!(counter1.code(), counter4.code());
        assert_eq!(counter1.count(), counter4.count());
        assert_eq!(counter1.code(), counter5.code());
        assert_eq!(counter1.count(), counter5.count());
    }

    #[rstest]
    #[case(0, "AAA", 0, "AAA", counter::Codex::KERIProtocolStack)]
    fn versioned_creation(
        #[case] verint: u32,
        #[case] version: &str,
        #[case] count: u32,
        #[case] count_b64: &str,
        #[case] code: &str,
    ) {
        let qsc = &format!("{code}{version}");
        let qscb = qsc.as_bytes();
        let qscb2 = b64_engine::URL_SAFE.decode(qsc).unwrap();

        let counter1 = Counter::new(Some(count), None, Some(code), None, None, None).unwrap();
        let counter2 = Counter::new(None, Some(count_b64), Some(code), None, None, None).unwrap();
        let counter3 = Counter::new(None, None, None, None, Some(qsc), None).unwrap();
        let counter4 = Counter::new(None, None, None, Some(qscb), None, None).unwrap();
        let counter5 = Counter::new(None, None, None, None, None, Some(&qscb2)).unwrap();

        assert_eq!(counter1.code(), code);
        assert_eq!(counter1.count(), verint);
        assert_eq!(counter1.code(), counter2.code());
        assert_eq!(counter1.count(), counter2.count());
        assert_eq!(counter1.code(), counter3.code());
        assert_eq!(counter1.count(), counter3.count());
        assert_eq!(counter1.code(), counter4.code());
        assert_eq!(counter1.count(), counter4.count());
        assert_eq!(counter1.code(), counter5.code());
        assert_eq!(counter1.count(), counter5.count());

        assert_eq!(counter1.count_as_b64(3).unwrap(), version);

        // when 0 is an argument, we use a default
        assert_eq!(counter1.count_as_b64(0).unwrap(), version);
    }

    #[rstest]
    fn b64_overflow_and_underflow(#[values("-AAB")] qsc: &str) {
        // add some chars
        let longqsc64 = &format!("{qsc}ABCD");
        let counter = Counter::new(None, None, None, None, Some(&longqsc64), None).unwrap();
        assert_eq!(
            counter.qb64().unwrap().len() as u32,
            counter::sizage(&counter.code()).unwrap().fs
        );

        // remove a char
        let shortqsc64 = &qsc[..qsc.len() - 1];
        assert!(Counter::new_with_qb64(shortqsc64).is_err());
    }

    #[rstest]
    fn binary_overflow_and_underflow(#[values(vec![248, 0, 1])] qscb2: Vec<u8>) {
        // add some bytes
        let mut longqscb2 = qscb2.clone();
        longqscb2.resize(longqscb2.len() + 5, 1);
        let counter = Counter::new(None, None, None, None, None, Some(&longqscb2)).unwrap();
        assert_eq!(counter.qb2().unwrap(), *qscb2);
        assert_eq!(
            counter.qb64().unwrap().len() as u32,
            counter::sizage(&counter.code()).unwrap().fs
        );

        // remove a bytes
        let shortqscb2 = &qscb2[..qscb2.len() - 1];
        assert!(Counter::new(None, None, None, None, None, Some(shortqscb2)).is_err());
    }

    #[rstest]
    fn exfil_infil_bexfil_binfil(#[values("-0VAAAQA")] qsc: &str) {
        let counter1 = Counter::new(None, None, None, None, Some(qsc), None).unwrap();
        let qb2 = counter1.qb2().unwrap();
        let counter2 = Counter::new(None, None, None, None, None, Some(&qb2)).unwrap();
        assert_eq!(counter1.code(), counter2.code());
        assert_eq!(counter1.count(), counter2.count());
        assert_eq!(counter1.qb2().unwrap(), counter2.qb2().unwrap());
        assert_eq!(qsc, counter2.qb64().unwrap());
    }

    #[rstest]
    #[case("1.2.3", "BCD")]
    #[case("1.1", "BBA")]
    #[case("1.", "BAA")]
    #[case("1", "BAA")]
    #[case("1.2.", "BCA")]
    #[case("..", "AAA")]
    #[case("1..3", "BAD")]
    fn semantic_versioning_strings(#[case] version: &str, #[case] b64: &str) {
        assert_eq!(Counter::sem_ver_str_to_b64(version).unwrap(), b64);
    }

    #[rstest]
    #[case(1, 0, 0, "BAA")]
    #[case(0, 1, 0, "ABA")]
    #[case(0, 0, 1, "AAB")]
    #[case(3, 4, 5, "DEF")]
    fn semantic_versioning_u8s(
        #[case] major: u8,
        #[case] minor: u8,
        #[case] patch: u8,
        #[case] b64: &str,
    ) {
        assert_eq!(Counter::sem_ver_to_b64(major, minor, patch).unwrap(), b64);
    }

    #[rstest]
    fn semantic_versioning_unhappy_strings(#[values("64.0.1", "-1.0.1", "0.0.64")] version: &str) {
        assert!(Counter::sem_ver_str_to_b64(version).is_err());
    }

    #[rstest]
    #[case(64, 0, 0)]
    fn semantic_versioning_unhappy_u32s(#[case] major: u8, #[case] minor: u8, #[case] patch: u8) {
        assert!(Counter::sem_ver_to_b64(major, minor, patch).is_err());
    }

    #[test]
    fn unhappy_paths() {
        assert!(Counter::new_with_code_and_count("", 1).is_err());
        assert!(
            Counter::new_with_code_and_count(counter::Codex::ControllerIdxSigs, 64 * 64).is_err()
        );
        assert!(Counter::sem_ver_str_to_b64("1.2.3.4").is_err());
        assert!(Counter::sem_ver_str_to_b64("bad.semantic.version").is_err());
        assert!((Counter { code: counter::Codex::ControllerIdxSigs.to_string(), count: 64 * 64 })
            .qb64()
            .is_err());

        assert!(Counter::new(None, None, None, None, Some(""), None).is_err());
        assert!(Counter::new(None, None, None, None, Some("--"), None).is_err());
        assert!(Counter::new(None, None, None, None, Some("__"), None).is_err());
        assert!(Counter::new(
            None,
            None,
            None,
            None,
            Some(counter::Codex::ControllerIdxSigs),
            None
        )
        .is_err());

        assert!(Counter::new(None, None, None, Some(&[]), None, None).is_err());

        assert!(Counter::new(None, None, None, None, None, Some(&[])).is_err());
        assert!(Counter::new(None, None, None, None, None, Some(&[0xf8, 0])).is_err());
        assert!(Counter::new(None, None, None, None, None, Some(&[0xfc, 0])).is_err());
        assert!(Counter::new(None, None, None, None, None, Some(&[0xfb, 0xe0])).is_err());
    }

    #[rstest]
    #[case(counter::Codex::ControllerIdxSigs, 1)]
    fn qb64b(#[case] code: &str, #[case] count: u32) {
        let c = Counter { code: code.to_string(), count };
        let qb64b = c.qb64b().unwrap();
        assert!(Counter::new(None, None, None, Some(&qb64b), None, None).is_ok());
    }

    #[rstest]
    #[case("-AAB", counter::Codex::ControllerIdxSigs, 4)]
    #[case("-0VAAAQA", counter::Codex::BigAttachedMaterialQuadlets, 8)]
    #[case("--AAAAAA", counter::Codex::KERIProtocolStack, 8)]
    fn qb_size(#[case] qsc: &str, #[case] code: &str, #[case] full_size: u32) {
        let counter = Counter::new(None, None, None, None, Some(qsc), None).unwrap();
        assert_eq!(counter.code(), code); // Just a self-check of the input data
        assert_eq!(counter.full_size().unwrap(), full_size);
    }
}