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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
use crate::Data;

pub mod class;
pub mod instruction;
pub use instruction::Instruction;

pub mod writer;
pub use writer::{BufferFull, Writer};

mod datasource;
pub use datasource::{DataSource, DataStream};

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Command<const S: usize> {
    class: class::Class,
    instruction: Instruction,

    pub p1: u8,
    pub p2: u8,

    /// The main reason this is modeled as Bytes and not
    /// a fixed array is for serde purposes.
    data: Data<S>,

    le: usize,
    pub extended: bool,
}

impl<const S: usize> Command<S> {
    pub fn try_from(apdu: &[u8]) -> Result<Self, FromSliceError> {
        apdu.try_into()
    }

    pub fn class(&self) -> class::Class {
        self.class
    }

    pub fn instruction(&self) -> Instruction {
        self.instruction
    }

    pub fn data(&self) -> &Data<S> {
        &self.data
    }

    pub fn data_mut(&mut self) -> &mut Data<S> {
        &mut self.data
    }

    pub fn expected(&self) -> usize {
        self.le
    }

    pub fn as_view(&self) -> CommandView {
        CommandView {
            class: self.class,
            instruction: self.instruction,
            p1: self.p1,
            p2: self.p2,
            data: self.data(),
            le: self.le,
            extended: self.extended,
        }
    }

    /// This can be use for APDU chaining to convert
    /// multiple APDU's into one.
    /// * Global Platform GPC_SPE_055 3.10
    #[allow(clippy::result_unit_err)]
    pub fn extend_from_command<const T: usize>(
        &mut self,
        command: &Command<T>,
    ) -> core::result::Result<(), ()> {
        self.extend_from_command_view(command.as_view())
    }

    /// This can be use for APDU chaining to convert
    /// multiple APDU's into one.
    /// * Global Platform GPC_SPE_055 3.10
    #[allow(clippy::result_unit_err)]
    pub fn extend_from_command_view(
        &mut self,
        command: CommandView,
    ) -> core::result::Result<(), ()> {
        // Always take the header from the last command;
        self.class = command.class();
        self.instruction = command.instruction();
        self.p1 = command.p1;
        self.p2 = command.p2;
        self.le = command.le;
        self.extended = true;

        // add the data to the end.
        self.data.extend_from_slice(command.data())
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// Memory-efficient unowned version of [`Command`]
pub struct CommandView<'a> {
    class: class::Class,
    instruction: Instruction,

    pub p1: u8,
    pub p2: u8,

    data: &'a [u8],

    le: usize,
    pub extended: bool,
}

impl<'a> CommandView<'a> {
    pub fn class(&self) -> class::Class {
        self.class
    }

    pub fn instruction(&self) -> Instruction {
        self.instruction
    }

    pub fn data(&self) -> &[u8] {
        self.data
    }

    pub fn expected(&self) -> usize {
        self.le
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum ExtendedLen {
    Unsupported,
    Supported,
    Forced,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct CommandBuilder<D> {
    class: class::Class,
    instruction: Instruction,

    pub p1: u8,
    pub p2: u8,

    data: D,

    le: ExpectedLen,
    extended_length: ExtendedLen,
}

#[derive(Debug)]
pub struct ChainedCommandIterator<'a> {
    command: Option<CommandBuilder<&'a [u8]>>,
    available_len: usize,
}

impl<'a> Iterator for ChainedCommandIterator<'a> {
    type Item = CommandBuilder<&'a [u8]>;

    fn next(&mut self) -> Option<CommandBuilder<&'a [u8]>> {
        let Some(next) = self.command.take() else {
            return None;
        };

        if let Some((cur, next)) = next.should_split(self.available_len) {
            self.command = Some(next);
            Some(cur)
        } else {
            Some(next)
        }
    }
}

const HEADER_LEN: usize = 4;

#[derive(Debug, PartialEq, Eq, Clone, PartialOrd, Ord, Copy)]
pub enum ExpectedLen {
    Ne(u16),
    Max,
}

impl From<u16> for ExpectedLen {
    fn from(value: u16) -> Self {
        Self::Ne(value)
    }
}

impl From<ExpectedLen> for usize {
    fn from(value: ExpectedLen) -> Self {
        (match value {
            ExpectedLen::Ne(l) => l,
            ExpectedLen::Max => u16::MAX,
        }) as _
    }
}

impl<D: DataSource> CommandBuilder<D> {
    /// Panics if data.len() > u16::MAX
    ///
    /// Assumes that extended length is supported
    ///
    pub fn new(
        class: class::Class,
        instruction: instruction::Instruction,
        p1: u8,
        p2: u8,
        data: D,
        le: impl Into<ExpectedLen>,
    ) -> Self {
        assert!(data.len() <= u16::MAX as usize);
        Self {
            class,
            instruction,
            p1,
            p2,
            data,
            le: le.into(),
            extended_length: ExtendedLen::Supported,
        }
    }

    /// Force the encoding of the APDU to be extended,
    /// even when the data and expected length are not neccessarily extended.
    pub fn force_extended(mut self) -> Self {
        assert!(!matches!(self.extended_length, ExtendedLen::Unsupported));
        self.extended_length = ExtendedLen::Forced;
        self
    }

    pub fn data(&self) -> D
    where
        D: Copy,
    {
        self.data
    }

    fn header_data(&self) -> BuildingHeaderData {
        /// Returns (data, len of data, and is_extended)
        fn serialize_data_len(
            len: u16,
            expected_len: ExpectedLen,
            extended: ExtendedLen,
        ) -> (heapless::Vec<u8, 3>, bool) {
            let expected_is_extended =
                matches!(expected_len, ExpectedLen::Ne(257..) | ExpectedLen::Max);
            match (len, expected_is_extended, extended) {
                (0, _, _) => (Default::default(), false),
                (1..=255, false, ExtendedLen::Unsupported | ExtendedLen::Supported) => {
                    ([len as u8].as_slice().try_into().unwrap(), false)
                }
                _ => {
                    let l = len.to_be_bytes();
                    ([0, l[0], l[1]].as_slice().try_into().unwrap(), true)
                }
            }
        }

        fn serialize_expected_len(
            len: ExpectedLen,
            lc_extended: bool,
            data_is_empty: bool,
            extended: ExtendedLen,
        ) -> heapless::Vec<u8, 3> {
            match (len, lc_extended, data_is_empty, extended) {
                (ExpectedLen::Ne(0), _, _, _) => Default::default(),
                (
                    ExpectedLen::Ne(len @ 1..=255),
                    false,
                    _,
                    ExtendedLen::Unsupported | ExtendedLen::Supported,
                ) => [len as u8].as_slice().try_into().unwrap(),
                (
                    ExpectedLen::Ne(256),
                    false,
                    _,
                    ExtendedLen::Unsupported | ExtendedLen::Supported,
                ) => [0].as_slice().try_into().unwrap(),
                (ExpectedLen::Ne(len), true, false, _) => {
                    let l = len.to_be_bytes();
                    [l[0], l[1]].as_slice().try_into().unwrap()
                }
                (ExpectedLen::Max, true, false, _) => [0, 0].as_slice().try_into().unwrap(),
                (ExpectedLen::Ne(len), false, true, _) => {
                    let l = len.to_be_bytes();
                    [0, l[0], l[1]].as_slice().try_into().unwrap()
                }
                (ExpectedLen::Max, false, true, _) => [0, 0, 0].as_slice().try_into().unwrap(),
                (ExpectedLen::Ne(257..) | ExpectedLen::Max, false, false, _)
                | (_, false, false, ExtendedLen::Forced) => {
                    unreachable!("Can't have non extended Lc and extended Le")
                }
                (_, true, true, _) => {
                    unreachable!("Can't have both no data and data extended length")
                }
            }
        }

        let le = if self.extended_length == ExtendedLen::Unsupported {
            self.le.min(256.into())
        } else {
            self.le
        };

        // Safe to unwrap because of check in `new`
        let (data_len, lc_extended) = serialize_data_len(
            self.data.len().try_into().unwrap(),
            le,
            self.extended_length,
        );

        let expected_data_len =
            serialize_expected_len(le, lc_extended, self.data.is_empty(), self.extended_length);
        BuildingHeaderData {
            le,
            data_len,
            expected_data_len,
        }
    }

    /// Required length for serialization in only one command.
    /// Assumes extended length support
    ///
    /// This can be useful to get the necessary dimension for the buffer to provide to [serialize_into](Self::serialize_into)
    pub fn required_len(&self) -> usize {
        let header_data = self.header_data();
        let header_len = 4;
        let length_len = header_data.data_len.len() + header_data.expected_data_len.len();
        header_len + length_len + self.data.len()
    }

    /// Serialize into one vector with assuming support for extended length information
    #[cfg(any(feature = "std", test))]
    pub fn serialize_to_vec(self) -> Vec<u8>
    where
        D: DataStream<Vec<u8>>,
    {
        let required_len = self.required_len();
        let mut buffer = Vec::with_capacity(required_len);
        self.serialize_into(&mut buffer).unwrap();
        debug_assert_eq!(required_len, buffer.len());
        buffer
    }

    /// This assumes that the writer has enough space to encode the APDU.
    /// If that might not be the case, first use [`should_split`](Self::should_split)
    pub fn serialize_into<W: Writer>(&self, writer: &mut W) -> Result<(), W::Error>
    where
        D: DataStream<W>,
    {
        let BuildingHeaderData {
            data_len,
            expected_data_len,
            ..
        } = self.header_data();

        writer.write_all(&[
            self.class.into_inner(),
            self.instruction.into(),
            self.p1,
            self.p2,
        ])?;

        writer.write_all(&data_len)?;
        self.data.to_writer(writer)?;
        writer.write_all(&expected_data_len)?;
        Ok(())
    }
}

struct BuildingHeaderData {
    le: ExpectedLen,
    data_len: heapless::Vec<u8, 3>,
    expected_data_len: heapless::Vec<u8, 3>,
}

impl<'a, D: PartialEq<&'a [u8]>> PartialEq<CommandView<'a>> for CommandBuilder<D> {
    fn eq(&self, other: &CommandView<'a>) -> bool {
        let Self {
            class,
            instruction,
            p1,
            p2,
            data,
            le,
            extended_length: _,
        } = self;
        let le: usize = (*le).into();
        class == &other.class
            && instruction == &other.instruction
            && p1 == &other.p1
            && p2 == &other.p2
            && data == &other.data
            && le == other.le
    }
}

impl<'a> CommandBuilder<&'a [u8]> {
    /// Panics if data.len() > u16::MAX
    ///
    /// Assumes that extended length is supported
    pub fn new_non_extended(
        class: class::Class,
        instruction: instruction::Instruction,
        p1: u8,
        p2: u8,
        data: &'a [u8],
        le: u16,
        buffer_len: Option<usize>,
    ) -> ChainedCommandIterator<'a> {
        assert!(data.len() <= u16::MAX as usize);
        ChainedCommandIterator {
            command: Some(Self {
                class,
                instruction,
                p1,
                p2,
                data,
                le: le.into(),
                extended_length: ExtendedLen::Unsupported,
            }),
            // default to u8::max for data, 5 bytes for the header, 1 for the trailer
            available_len: buffer_len.unwrap_or(255 + 5 + 1),
        }
    }

    /// Given the available length and the extended length support, split the command in 2 commands that use command chaining to be sent
    ///
    /// `None` means that the command can we serialized withinn `available_len` without needing Chaining
    /// `Some(command, rem)` means that `command` can be sent within `available_len` and that `rem` must then be sent (for command chaining). Note that `should_split` should also be called on `rem` as more than 2 commands might be required.
    ///
    /// In certain conditions can panic if `available_len <= 9` since 9 is the minimum length required to encode the header and trailer of a command.
    pub fn should_split(&self, available_len: usize) -> Option<(Self, Self)> {
        if available_len < HEADER_LEN {
            panic!("Commands cannot be encoded to fit in buffers smaller than 9 bytes");
        }

        let BuildingHeaderData {
            le,
            data_len,
            expected_data_len,
        } = self.header_data();

        let mut max_data_len = u16::MAX as usize;
        if self.extended_length == ExtendedLen::Unsupported {
            max_data_len = 255;
        }

        let available_data_len = (available_len - HEADER_LEN)
            .saturating_sub(data_len.len() + expected_data_len.len())
            .min(max_data_len);
        if available_data_len >= self.data.len() {
            // slitting not necessary
            return None;
        }

        if available_data_len == 0 {
            // Let's not support this case
            panic!("Commands cannot be encoded to fit in buffers smaller than 9 bytes");
        }

        let (send_now, send_later) = self.data.split_at(available_data_len);

        let send_now = Self {
            class: self.class.as_chained(),
            instruction: self.instruction,
            p1: self.p1,
            p2: self.p2,
            data: send_now,
            le: 0.into(),
            extended_length: self.extended_length,
        };
        let send_later = Self {
            class: self.class,
            instruction: self.instruction,
            p1: self.p1,
            p2: self.p2,
            data: send_later,
            le,
            extended_length: self.extended_length,
        };
        Some((send_now, send_later))
    }
}

impl<D: DataSource> DataSource for CommandBuilder<D> {
    fn len(&self) -> usize {
        self.required_len()
    }

    fn is_empty(&self) -> bool {
        false
    }
}

impl<W: Writer, D: DataStream<W>> DataStream<W> for CommandBuilder<D> {
    fn to_writer(&self, writer: &mut W) -> Result<(), <W as Writer>::Error> {
        self.serialize_into(writer)
    }
}

impl<'a, D: PartialEq<&'a [u8]>> PartialEq<CommandBuilder<D>> for CommandView<'a> {
    fn eq(&self, other: &CommandBuilder<D>) -> bool {
        other == self
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum FromSliceError {
    TooShort,
    TooLong,
    InvalidClass,
    InvalidFirstBodyByteForExtended,
    InvalidSliceLength,
}

impl From<class::InvalidClass> for FromSliceError {
    fn from(_: class::InvalidClass) -> Self {
        Self::InvalidClass
    }
}

impl<'a> TryFrom<&'a [u8]> for CommandView<'a> {
    type Error = FromSliceError;
    fn try_from(apdu: &'a [u8]) -> core::result::Result<Self, Self::Error> {
        if apdu.len() < 4 {
            return Err(FromSliceError::TooShort);
        }
        #[cfg(test)]
        println!("{}", apdu.len());
        let (header, body) = apdu.split_at(4);
        let class = class::Class::try_from(header[0])?;
        let instruction = Instruction::from(header[1]);
        let p1 = header[2];
        let p2 = header[3];
        let parsed = parse_lengths(body)?;
        let data = &body[parsed.offset..][..parsed.lc];

        Ok(Self {
            // header
            class,
            instruction,
            p1,
            p2,
            // maximum expected response length
            le: parsed.le,
            // payload
            data,
            extended: parsed.extended,
        })
    }
}

impl<'a> CommandView<'a> {
    pub fn to_owned<const S: usize>(&self) -> Result<Command<S>, FromSliceError> {
        let &CommandView {
            class,
            instruction,
            p1,
            p2,
            le,
            data: data_slice,
            extended,
        } = self;
        // We use this way to construct the command instead of Data::from_slice as that would
        // triple stack usage on the lpc55.
        let mut command = Command {
            // header
            class,
            instruction,
            p1,
            p2,
            // maximum expected response length
            le,
            // payload
            data: Data::new(),
            extended,
        };
        command
            .data
            .extend_from_slice(data_slice)
            .map_err(|_| FromSliceError::TooLong)?;
        Ok(command)
    }
}

impl<const S: usize> TryFrom<&[u8]> for Command<S> {
    type Error = FromSliceError;
    fn try_from(apdu: &[u8]) -> core::result::Result<Self, Self::Error> {
        let view: CommandView = apdu.try_into()?;
        view.to_owned()
    }
}

// cf. ISO 7816-3, 12.1.3: Decoding conventions for command APDUs
// freely available version:
// http://www.ttfn.net/techno/smartcards/iso7816_4.html#table5

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
struct ParsedLengths {
    lc: usize,
    le: usize,
    offset: usize,
    extended: bool,
}

#[inline(always)]
fn replace_zero(value: usize, replacement: usize) -> usize {
    if value == 0 {
        replacement
    } else {
        value
    }
}
#[inline]
fn parse_lengths(body: &[u8]) -> Result<ParsedLengths, FromSliceError> {
    // Encoding rules:
    // - Lc or Le = 0 => leave out
    // - short + extended length fields shall not be combined
    // - for extended, if Lc > 0, then Le has no leading zero byte

    let l = body.len();

    let mut parsed: ParsedLengths = Default::default();

    // Case 1
    if l == 0 {
        return Ok(parsed);
    }

    // the reference starts indexing at 1
    let b1 = body[0] as usize;

    #[cfg(test)]
    println!("l = {}, b1 = {}", l, b1);

    // Case 2S
    if l == 1 {
        parsed.lc = 0;
        parsed.le = replace_zero(b1, 256);
        return Ok(parsed);
    }

    // Case 3S
    if l == 1 + b1 && b1 != 0 {
        // B1 encodes Lc valued from 1 to 255
        parsed.lc = b1;
        parsed.le = 0;
        parsed.offset = 1;
        return Ok(parsed);
    }

    // Case 4S
    if l == 2 + b1 && b1 != 0 {
        // B1 encodes Lc valued from 1 to 255
        // Bl encodes Le from 1 to 256
        parsed.lc = b1;
        parsed.le = replace_zero(body[l - 1] as usize, 256);
        parsed.offset = 1;
        return Ok(parsed);
    }

    parsed.extended = true;

    // only extended cases left now
    if b1 != 0 {
        return Err(FromSliceError::InvalidFirstBodyByteForExtended);
    } else if l < 3 {
        return Err(FromSliceError::InvalidSliceLength);
    }

    // Case 2E (no data)
    if l == 3 && b1 == 0 {
        parsed.lc = 0;
        parsed.le = replace_zero(u16::from_be_bytes([body[1], body[2]]) as usize, 65_536);
        return Ok(parsed);
    }

    parsed.lc = u16::from_be_bytes([body[1], body[2]]) as usize;

    // Case 3E
    if l == 3 + parsed.lc {
        parsed.le = 0;
        parsed.offset = 3;
        return Ok(parsed);
    }

    // Case 4E
    if l == 5 + parsed.lc {
        parsed.le = replace_zero(
            u16::from_be_bytes([body[l - 2], body[l - 1]]) as usize,
            65_536,
        );
        parsed.offset = 3;
        return Ok(parsed);
    }

    // If we haven’t returned yet, the slice has an invalid length:  Either the encoded lc value is
    // wrong, or the lc and le lengths are not encoded properly (one byte per value for simple
    // APDU, two bytes per value for extended APDU).

    Err(FromSliceError::InvalidSliceLength)
}

#[cfg(test)]
mod test {
    use super::*;
    use hex_literal::hex;
    use quickcheck_macros::quickcheck;

    #[quickcheck]
    fn parse_no_panic(data: Vec<u8>) {
        let _ = parse_lengths(&data);
    }

    #[quickcheck]
    fn lengths(lc: u16, le: Option<u16>) {
        let extended =
            lc > u16::from(u8::MAX) || le.map(|val| val > u16::from(u8::MAX)).unwrap_or_default();
        let nc = usize::from(lc);
        let ne = le
            .map(usize::from)
            .map(|val| {
                if val == 0 {
                    (if extended {
                        usize::from(u16::MAX)
                    } else {
                        usize::from(u8::MAX)
                    }) + 1
                } else {
                    val
                }
            })
            .unwrap_or_default();

        let mut data = Vec::new();
        let mut offset = 0;

        if lc > 0 {
            if extended {
                data.push(0);
                data.extend_from_slice(&lc.to_be_bytes());
                offset = 3;
            } else {
                data.push(lc as u8);
                offset = 1;
            }
        }

        data.resize(data.len() + nc, 0);

        if let Some(le) = le {
            if extended {
                if lc == 0 {
                    data.push(0);
                }
                data.extend_from_slice(&le.to_be_bytes());
            } else {
                data.push(le as u8);
            }
        }

        let lengths = parse_lengths(&data).expect("failed to parse lengths");
        assert_eq!(extended, lengths.extended);
        assert_eq!(offset, lengths.offset);
        assert_eq!(nc, lengths.lc);
        assert_eq!(ne, lengths.le);
    }

    #[test]
    fn builder_forced_extended() {
        let cla = 0.try_into().unwrap();
        let ins = 1.into();
        let command = CommandBuilder::new(cla, ins, 2, 3, &[], 0x04).force_extended();
        assert_eq!(command.serialize_to_vec(), &hex!("00 01 02 03 00 00 04"));

        let command = CommandBuilder::new(cla, ins, 2, 3, &[0x05, 0x06], 0x04).force_extended();
        assert_eq!(
            command.serialize_to_vec(),
            &hex!("00 01 02 03 00 00 02 05 06 00 04")
        );

        let command = CommandBuilder::new(cla, ins, 2, 3, &[0x01; 0x2AE], 0x100).force_extended();
        assert_eq!(
            command.serialize_to_vec(),
            [
                hex!("00 01 02 03 00 02AE").as_slice(),
                &[0x01; 0x2AE],
                &hex!("01 00"),
            ]
            .into_iter()
            .flatten()
            .copied()
            .collect::<Vec<u8>>()
        );
    }

    #[test]
    fn builder() {
        let cla = 0.try_into().unwrap();
        let ins = 1.into();
        let command = CommandBuilder::new(cla, ins, 2, 3, &[], 0x04);
        assert_eq!(command.serialize_to_vec(), &hex!("00 01 02 03 04"));

        let command = CommandBuilder::new(cla, ins, 2, 3, &[], 0x00);
        assert_eq!(command.serialize_to_vec(), &hex!("00 01 02 03"));

        let command = CommandBuilder::new(cla, ins, 2, 3, &[], 256);
        assert_eq!(command.serialize_to_vec(), &hex!("00 01 02 03 00"));
        let command = CommandBuilder::new(cla, ins, 2, 3, &[], 257);
        assert_eq!(command.serialize_to_vec(), &hex!("00 01 02 03 00 0101"));
        let command = CommandBuilder::new(cla, ins, 2, 3, &[], 0xFFFF);
        assert_eq!(command.serialize_to_vec(), &hex!("00 01 02 03 00 FFFF"));

        let command = CommandBuilder::new(cla, ins, 2, 3, &[0x05, 0x06], 0x04);
        assert_eq!(command.serialize_to_vec(), &hex!("00 01 02 03 02 05 06 04"));

        let command = CommandBuilder::new(cla, ins, 2, 3, &[0x05, 0x06], 0x00);
        assert_eq!(command.serialize_to_vec(), &hex!("00 01 02 03 02 05 06"));

        let command = CommandBuilder::new(cla, ins, 2, 3, &[0x05, 0x06], 0x100);
        assert_eq!(
            command.serialize_to_vec(),
            // Large LE also forces the data length to be extended (can't mix extended/non-extended)
            &hex!("00 01 02 03 02 05 06 00")
        );

        let command = CommandBuilder::new(cla, ins, 2, 3, &[0x01; 0x2AE], 0x100);
        assert_eq!(
            command.serialize_to_vec(),
            [
                hex!("00 01 02 03 00 02AE").as_slice(),
                &[0x01; 0x2AE],
                &hex!("01 00"),
            ]
            .into_iter()
            .flatten()
            .copied()
            .collect::<Vec<u8>>()
        );

        let command = CommandBuilder::new(cla, ins, 2, 3, &[0x01; 0x2AE], 0x01);
        assert_eq!(
            command.serialize_to_vec(),
            [
                hex!("00 01 02 03 00 02AE").as_slice(),
                &[0x01; 0x2AE],
                &hex!("00 01"),
            ]
            .into_iter()
            .flatten()
            .copied()
            .collect::<Vec<u8>>()
        );

        let command = CommandBuilder::new(cla, ins, 2, 3, &[0x01; 0x2AE], 0x00);
        assert_eq!(
            command.serialize_to_vec(),
            [hex!("00 01 02 03 00 02AE").as_slice(), &[0x01; 0x2AE],]
                .into_iter()
                .flatten()
                .copied()
                .collect::<Vec<u8>>()
        );

        let command = CommandBuilder::new(cla, ins, 2, 3, &[0x01; 0x2AE], 0xFF);
        assert_eq!(
            command.serialize_to_vec(),
            [
                hex!("00 01 02 03 00 02AE").as_slice(),
                &[0x01; 0x2AE],
                &[0x00, 0xFF]
            ]
            .into_iter()
            .flatten()
            .copied()
            .collect::<Vec<u8>>()
        );

        let command = CommandBuilder::new(cla, ins, 2, 3, &[0x01; 0xFFFF], 0xFFFF);
        assert_eq!(
            command.serialize_to_vec(),
            [
                hex!("00 01 02 03 00 FFFF").as_slice(),
                &[0x01; 0xFFFF],
                &[0xFF, 0xFF]
            ]
            .into_iter()
            .flatten()
            .copied()
            .collect::<Vec<u8>>()
        );

        let command = CommandBuilder::new(cla, ins, 2, 3, &[1, 2, 3, 4], 294);
        assert_eq!(
            command.serialize_to_vec(),
            &hex!("00 01 02 03 00 00 04 01020304 0126")
        );
    }

    #[test]
    fn building_chained() {
        let cla = 0x00.try_into().unwrap();
        let ins = 0x01.into();
        let mut buffer = heapless::Vec::<u8, 4096>::new();
        let command = CommandBuilder::new(cla, ins, 2, 3, &[], 0xFFFF);
        command.clone().serialize_into(&mut buffer).unwrap();
        assert_eq!(&*buffer, &command.clone().serialize_to_vec());

        buffer.clear();
        //  without extended length
        let command =
            CommandBuilder::new_non_extended(cla, ins, 2, 3, &[], 0xFFFF, Some(buffer.capacity()))
                .next()
                .unwrap();
        command.clone().serialize_into(&mut buffer).unwrap();
        assert_eq!(
            &*buffer,
            &CommandBuilder::new(cla, ins, 2, 3, &[], 0x0100).serialize_to_vec()
        );

        buffer.clear();
        //  without extended length
        let command =
            CommandBuilder::new_non_extended(cla, ins, 2, 3, &[], 0, Some(buffer.capacity()))
                .next()
                .unwrap();
        command.serialize_into(&mut buffer).unwrap();
        assert_eq!(
            &*buffer,
            &CommandBuilder::new(cla, ins, 2, 3, &[], 0).serialize_to_vec()
        );
        buffer.clear();

        let mut buffer = heapless::Vec::<u8, 105>::new();

        let mut command_iter =
            CommandBuilder::new_non_extended(cla, ins, 2, 3, &[5; 200], 0, Some(buffer.capacity()));
        let command = command_iter.next().unwrap();
        let mut rem = command_iter.next().unwrap();
        assert!(command_iter.next().is_none());
        command.serialize_into(&mut buffer).unwrap();
        assert_eq!(buffer.len(), 105);
        rem.extended_length = ExtendedLen::Supported;
        assert_eq!(
            rem,
            CommandBuilder::new(cla, ins, 2, 3, [5; 100].as_slice(), 0)
        );
        assert_eq!(
            &*buffer,
            &CommandBuilder::new(cla.as_chained(), ins, 2, 3, &[5; 100], 0).serialize_to_vec()
        );
    }

    #[test]
    fn nested_commands() {
        let cla = 0x00.try_into().unwrap();
        let ins = 0x01.into();
        let mut buffer = heapless::Vec::<u8, 4096>::new();
        let inner = CommandBuilder::new(cla, ins, 1, 2, &hex!("FFFEFDFCFBFA"), 0x10);
        let outer = CommandBuilder::new(cla, 0xAA.into(), 3, 4, &inner, 0x20);
        outer.serialize_into(&mut buffer).unwrap();
        #[rustfmt::skip]
        assert_eq!(
            &*buffer,
            hex!("
                00 AA 0304 0C
                   00 01 01 02 06  FFFEFDFCFBFA 10 
                20"
            )
        );
    }

    #[test]
    fn lengths_4s() {
        let data = &[0x02, 0xB6, 0x00, 0x00];
        let lengths = parse_lengths(data).expect("failed to parse lengths");
        assert_eq!(lengths.lc, 2);
        assert_eq!(lengths.le, 256);
        assert_eq!(lengths.offset, 1);
    }

    #[test]
    fn command_chaining() {
        let apdu = &[
            0x10, 0xdb, 0x3f, 0xff, 0xff, 0x5c, 0x03, 0x5f, 0xc1, 0x05, 0x53, 0x82, 0x01, 0x5b,
            0x70, 0x82, 0x01, 0x52, 0x30, 0x82, 0x01, 0x4e, 0x30, 0x81, 0xf5, 0xa0, 0x03, 0x02,
            0x01, 0x02, 0x02, 0x11, 0x00, 0x8b, 0xab, 0x31, 0xcf, 0x3e, 0xb9, 0xf5, 0x6a, 0x6f,
            0x38, 0xf0, 0x5a, 0x4d, 0x7f, 0x55, 0x62, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48,
            0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, 0x2a, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55,
            0x04, 0x0a, 0x13, 0x0d, 0x79, 0x75, 0x62, 0x69, 0x6b, 0x65, 0x79, 0x2d, 0x61, 0x67,
            0x65, 0x6e, 0x74, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x07,
            0x28, 0x64, 0x65, 0x76, 0x65, 0x6c, 0x29, 0x30, 0x20, 0x17, 0x0d, 0x32, 0x30, 0x30,
            0x35, 0x31, 0x36, 0x30, 0x31, 0x31, 0x37, 0x32, 0x36, 0x5a, 0x18, 0x0f, 0x32, 0x30,
            0x36, 0x32, 0x30, 0x35, 0x31, 0x36, 0x30, 0x32, 0x31, 0x37, 0x32, 0x36, 0x5a, 0x30,
            0x12, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x07, 0x53, 0x53,
            0x48, 0x20, 0x6b, 0x65, 0x79, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48,
            0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
            0x03, 0x42, 0x00, 0x04, 0x4f, 0x98, 0x63, 0x2f, 0x53, 0xbd, 0xab, 0xee, 0xbf, 0x69,
            0x73, 0x3a, 0x84, 0x0f, 0xfd, 0x9f, 0x9d, 0xb3, 0xce, 0x5c, 0x1e, 0x1b, 0x84, 0x06,
            0x63, 0x32, 0xff, 0x9c, 0x44, 0x0b, 0xce, 0x56, 0x13, 0x94, 0x00, 0x98, 0xe3, 0x46,
            0xc2, 0xbc, 0x3d, 0xe6, 0x5e, 0xf2, 0x81, 0x4b, 0xbc, 0xea, 0x2b, 0x9d, 0x47, 0xcc,
            0x9b, 0x5e, 0xbe, 0x1e, 0x2c, 0x69, 0x1d, 0xc3, 0x53, 0x4c, 0x89, 0x14, 0xa3, 0x12,
            0x30, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x1d,
        ];

        let _command = Command::<256>::try_from(apdu).unwrap();
    }

    #[test]
    fn lc_oob() {
        let apdu = &hex!("00C00000 00FF");
        let _ = Command::<256>::try_from(apdu);
        let apdu = &hex!("00C00000 0000");
        let _ = Command::<256>::try_from(apdu);
    }
}