Repository: google/OpenSK
File: src/ctap/apdu.rs (Rust)

Code before:
                le: 0,
            });
        }
        if payload.len() == 2 + (byte_0 as usize) && byte_0 != 0 {
            // Lc is one-byte long and since the size specified by Lc covers the rest of the
            // payload with ONE additional byte that byte must be Le
            let last_byte: u32 = (*payload.last().unwrap()).into();
            return Ok(APDU {
                header: array_ref!(header, 0, APDU_HEADER_LEN).into(),
                lc: byte_0.into(),
                data: payload[1..(payload.len() - 1)].to_vec(),
                le: if last_byte == 0x00 { 0x100 } else { last_byte },
                case_type: ApduType::Short(Case::Lc1DataLe1),
            });
        }
        if payload.len() > 2 {
            // Lc is possibly three-bytes long
            let extended_apdu_lc = BigEndian::read_u16(&payload[1..3]) as usize;
            if payload.len() < extended_apdu_lc + 3 {
                return Err(ApduStatusCode::SW_WRONG_LENGTH);
            }

            let extended_apdu_le_len: usize = payload
                .len()
                .checked_sub(extended_apdu_lc + 3)
                .unwrap_or(0xff);
            if extended_apdu_le_len > 3 {
                return Err(ApduStatusCode::SW_WRONG_LENGTH);
            }

            if byte_0 == 0 && extended_apdu_le_len <= 3 {
                // If first byte is zero AND the next two bytes can be parsed as a big-endian
                // length that covers the rest of the block (plus few additional bytes for Le), we
                // have an extended-length APDU
                let last_byte: u32 = (*payload.last().unwrap()).into();
                return Ok(APDU {
                    header: array_ref!(header, 0, APDU_HEADER_LEN).into(),
                    lc: extended_apdu_lc as u16,
                    data: payload[3..(payload.len() - extended_apdu_le_len)].to_vec(),
                    le: match extended_apdu_le_len {
                        0 => 0,
                        1 => {
                            if last_byte == 0x00 {
                                0x100
                            } else {
                                last_byte
                            }
                        }
                        2 => {
                            let le_parsed = BigEndian::read_u16(&payload[payload.len() - 2..]);
                            if le_parsed == 0x00 {

Review comment (suggestion):
You are implicitly trying to reuse the error code handling in the `if` below? Should work, but explicit is usually better than implicit. That's why I proposed `ok_or(ERROR_CODE)?`.

Code after:
                case_type: ApduType::Short(Case::Lc1Data),
                le: 0,
            });
        }
        if payload.len() == 2 + (byte_0 as usize) && byte_0 != 0 {
            // Lc is one-byte long and since the size specified by Lc covers the rest of the
            // payload with ONE additional byte that byte must be Le
            let last_byte: u32 = (*payload.last().unwrap()).into();
            return Ok(APDU {
                header: array_ref!(header, 0, APDU_HEADER_LEN).into(),
                lc: byte_0.into(),
                data: payload[1..(payload.len() - 1)].to_vec(),
                le: if last_byte == 0x00 { 0x100 } else { last_byte },
                case_type: ApduType::Short(Case::Lc1DataLe1),
            });
        }
        if payload.len() > 2 {
            // Lc is possibly three-bytes long
            let extended_apdu_lc = BigEndian::read_u16(&payload[1..3]) as usize;
            if payload.len() < extended_apdu_lc + 3 {
                return Err(ApduStatusCode::SW_WRONG_LENGTH);
            }

            let extended_apdu_le_len: usize = payload
                .len()
                .checked_sub(extended_apdu_lc + 3)
                .ok_or(ApduStatusCode::SW_WRONG_LENGTH)?;
            if extended_apdu_le_len > 3 {
                return Err(ApduStatusCode::SW_WRONG_LENGTH);
            }

            if byte_0 == 0 && extended_apdu_le_len <= 3 {
                // If first byte is zero AND the next two bytes can be parsed as a big-endian
                // length that covers the rest of the block (plus few additional bytes for Le), we
                // have an extended-length APDU
                let last_byte: u32 = (*payload.last().unwrap()).into();
                return Ok(APDU {
                    header: array_ref!(header, 0, APDU_HEADER_LEN).into(),
                    lc: extended_apdu_lc as u16,
                    data: payload[3..(payload.len() - extended_apdu_le_len)].to_vec(),
                    le: match extended_apdu_le_len {
                        0 => 0,
                        1 => {
                            if last_byte == 0x00 {
                                0x100
                            } else {
                                last_byte
                            }
                        }
                        2 => {
                            let le_parsed = BigEndian::read_u16(&payload[payload.len() - 2..]);