Struct iso7816_tlv::ber::Tag

source ·
pub struct Tag { /* private fields */ }
Expand description

Tag for BER-TLV data as defined in [ISO7816-4].

Tags can be generated using the TryFrom trait from integer types (see Trait Implementations for valid input types) or hex str.

Tags are now imported in a more permissivie way. Invalid tags in ISO7816 meaning can be checked, see Self::iso7816_compliant

§Example

use std::convert::TryFrom;
use iso7816_tlv::ber::Tag;

assert!(Tag::try_from("80").is_ok());
assert!(Tag::try_from(8u8).is_ok());
assert!(Tag::try_from(8u16).is_ok());
assert!(Tag::try_from(8u32).is_ok());
assert!(Tag::try_from(8i32).is_ok());
assert!(Tag::try_from("7f22").is_ok());

assert!(Tag::try_from("error ").is_err());
assert!(Tag::try_from("7fffff01").is_err());
assert!(Tag::try_from("7f80ff").is_err());
// now more permissive interface :
assert!(Tag::try_from("7f00").is_ok());
assert!(Tag::try_from("7f1e").is_ok());

Implementations§

source§

impl Tag

source

pub fn to_bytes(&self) -> &[u8]

serializes the tag as byte array

source

pub fn len_as_bytes(&self) -> usize

length of the tag as byte array

§Example
use std::convert::TryFrom;
use iso7816_tlv::ber::Tag;

let tag = Tag::try_from("80")?;
assert_eq!(1, tag.len_as_bytes());

let tag = Tag::try_from("7f22")?;
assert_eq!(2, tag.len_as_bytes());  

let tag = Tag::try_from("7f8122")?;
assert_eq!(3, tag.len_as_bytes());

let tag = Tag::try_from("5fff22")?;
assert_eq!(3, tag.len_as_bytes());

let tag = Tag::try_from("5f2d")?;
assert_eq!(2, tag.len_as_bytes());
source

pub fn is_constructed(&self) -> bool

Wether the tag is constructed or not

Bit 6 of the first byte of the tag field indicates an encoding.

  • The value 0 indicates a primitive encoding of the data object, i.e., the value field is not encoded in BER - TLV .
  • The value 1 indicates a constructed encoding of the data object, i.e., the value field is encoded in BER - TLV
§Example
use std::convert::TryFrom;
use iso7816_tlv::ber::Tag;

let valid = Tag::try_from(0b0010_0000)?;
assert!(valid.is_constructed());

let invalid = Tag::try_from(0b0000_0001)?;
assert!(!invalid.is_constructed());
source

pub fn iso7816_compliant(&self) -> bool

Valid tags are defined as follow:

ISO/IEC 7816 supports tag fields of one, two and three bytes; longer tag fields are reserved for future use

In tag fields of two or more bytes, the values ‘00’ to ‘1E’ and ‘80’ are invalid for the second byte.

  • In two-byte tag fields, the second byte consists of bit 8 set to 0 and bits 7 to 1 encoding a number greater than thirty. The second byte is valued from ‘1F’ to ’7F; the tag number is from 31 to 127.
  • In three-byte tag fields, the second byte consists of bit 8 set to 1 and bits 7 to 1 not all set to 0; the third byte consists of bit 8 set to 0 and bits 7 to 1 with any value.
§Example
use std::convert::TryFrom;
use iso7816_tlv::ber::Tag;

let t = Tag::try_from("7f00")?;
assert!(!t.iso7816_compliant());
// 9f1e is a valid EMV tag, but not ISO7816-4 compliant
let t = Tag::try_from("9f1e")?;
assert!(!t.iso7816_compliant());
source

pub fn class(&self) -> Class

Get the tag class.

§Example
use std::convert::TryFrom;
use iso7816_tlv::ber::{Tag, Class};

let tag = Tag::try_from(0b0010_0000)?;
assert_eq!(Class::Universal, tag.class());
let tag = Tag::try_from(0b1100_0000)?;
assert_eq!(Class::Private, tag.class());

Trait Implementations§

source§

impl Clone for Tag

source§

fn clone(&self) -> Tag

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Tag

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for Tag

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Into<u64> for Tag

source§

fn into(self) -> u64

Converts this type into the (usually inferred) input type.
source§

impl PartialEq for Tag

source§

fn eq(&self, other: &Tag) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl TryFrom<&str> for Tag

§

type Error = TlvError

The type returned in the event of a conversion error.
source§

fn try_from(v: &str) -> Result<Self, TlvError>

Performs the conversion.
source§

impl TryFrom<i16> for Tag

§

type Error = TlvError

The type returned in the event of a conversion error.
source§

fn try_from(v: i16) -> Result<Self, TlvError>

Performs the conversion.
source§

impl TryFrom<i32> for Tag

§

type Error = TlvError

The type returned in the event of a conversion error.
source§

fn try_from(v: i32) -> Result<Self, TlvError>

Performs the conversion.
source§

impl TryFrom<i8> for Tag

§

type Error = TlvError

The type returned in the event of a conversion error.
source§

fn try_from(v: i8) -> Result<Self, TlvError>

Performs the conversion.
source§

impl TryFrom<u16> for Tag

§

type Error = TlvError

The type returned in the event of a conversion error.
source§

fn try_from(v: u16) -> Result<Self, TlvError>

Performs the conversion.
source§

impl TryFrom<u32> for Tag

§

type Error = TlvError

The type returned in the event of a conversion error.
source§

fn try_from(v: u32) -> Result<Self, TlvError>

Performs the conversion.
source§

impl TryFrom<u64> for Tag

§

type Error = TlvError

The type returned in the event of a conversion error.
source§

fn try_from(v: u64) -> Result<Self, TlvError>

Performs the conversion.
source§

impl TryFrom<u8> for Tag

§

type Error = TlvError

The type returned in the event of a conversion error.
source§

fn try_from(v: u8) -> Result<Self, TlvError>

Performs the conversion.
source§

impl TryFrom<usize> for Tag

§

type Error = TlvError

The type returned in the event of a conversion error.
source§

fn try_from(v: usize) -> Result<Self, TlvError>

Performs the conversion.
source§

impl StructuralPartialEq for Tag

Auto Trait Implementations§

§

impl RefUnwindSafe for Tag

§

impl Send for Tag

§

impl Sync for Tag

§

impl Unpin for Tag

§

impl UnwindSafe for Tag

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.