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
use tag;

/// Provides ASN.1 information about a Rust type, including the BER tag and ASN.1 type.
pub trait Asn1Info {
  /// Get the ASN.1 tag (if defined) for this Rust type. Some types don't have a tag, eg. CHOICE.
  fn asn1_tag() -> Option<tag::Tag>;

  /// Get the ASN.1 type for this Rust type.
  fn asn1_type() -> tag::Type;
}

#[macro_export]
/// This macro defines the Asn1Info trait for a rust type.
///
/// This information is used to match tag information during deserialization,
/// so it should match the expected values in the ASN.1 stream.
///
/// ```
/// #[macro_use] extern crate asn1_cereal; fn main() {
/// // For A ::= [APPLICATION 3] u64
/// struct A(u64);
/// asn1_info!(A, [APPLICATION 3], "A");
///
/// // For B ::= [PRIVATE 3] u64
/// struct B(u64);
/// asn1_info!(B, [PRIVATE 3], "B");
///
/// // For a primitive type, with application tag 3.
/// struct C(u64);
/// // (The false here sets the constructed flag).
/// asn1_info!(C, asn1_cereal::tag::Class::Application, 3, false, "C");
/// }
///
/// // For a type that won't have a tag.
/// struct D(i32);
/// asn1_info!(D, "D");
///
/// // For a type with a generic.
/// struct E<T>(T);
/// asn1_info!(E<T> => T, [PRIVATE 4], "E");
///
/// ```
macro_rules! asn1_info {
  ($rs_type:ty => $gen:ident, $($args:tt)*) => (
    impl<$gen> $crate::Asn1Info for $rs_type {
      asn1_info!{__impl $($args)*}
    }
  );
  (__impl [$($args:tt)*], $asn1_ty:expr) => (
    fn asn1_tag() -> Option<$crate::tag::Tag> {
      Some(asn1_spec_tag!([$($args)*]))
    }
    asn1_info!(__type $asn1_ty);
  );
  (__impl $class:expr, $tagnum:expr, $constructed:expr, $asn1_ty:expr) => (
    fn asn1_tag() -> Option<$crate::tag::Tag> {
      Some($crate::tag::Tag {
        class: ($class as u8).into(),
        tagnum: $tagnum as $crate::tag::TagNum,
        constructed: $constructed,
      })
    }
    asn1_info!(__type $asn1_ty);
  );
  (__impl $asn1_ty:expr) => (
    fn asn1_tag() -> Option<$crate::tag::Tag> {
      None
    }
    asn1_info!(__type $asn1_ty);
  );
  (__type $asn1_ty:expr) => (
    fn asn1_type() -> $crate::tag::Type {
      $crate::tag::Type::from($asn1_ty)
    }
  );
  ($rs_type:ty, $($args:tt)*) => (
    impl $crate::Asn1Info for $rs_type {
      asn1_info!{__impl $($args)*}
    }
  );
}

#[macro_export]
/// This macro parses an ASN.1 tag specification, and returns the appropriate Tag.
macro_rules! asn1_spec_tag {
  ({ $count:ident }) => (
    asn1_spec_tag!([])
  );
  ({ $count:ident } []) => ({
    let _count = $count;
    $count += 1;
    asn1_spec_tag!([CONTEXT _count])
  });
  ({ $count:ident } [$($args:tt)*]) => (
    asn1_spec_tag!([$($args)*])
  );
  ([$tagnum:expr]) => (
    asn1_spec_tag!([CONTEXT $tagnum]);
  );
  ([UNIVERSAL $tagnum:expr]) => (
    $crate::tag::Tag {
      class: $crate::tag::Class::Universal,
      tagnum: $tagnum,
      constructed: true,
    }
  );
  ([CONTEXT $tagnum:expr]) => (
    $crate::tag::Tag {
      class: $crate::tag::Class::ContextSpecific,
      tagnum: $tagnum,
      constructed: true,
    }
  );
  ([APPLICATION $tagnum:expr]) => (
    $crate::tag::Tag {
      class: $crate::tag::Class::Application,
      tagnum: $tagnum,
      constructed: true,
    }
  );
  ([PRIVATE $tagnum:expr]) => (
    $crate::tag::Tag {
      class: $crate::tag::Class::Private,
      tagnum: $tagnum,
      constructed: true,
    }
  );
}