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
#[macro_export]
macro_rules! ber_sequence_of {
($($token:tt)*) => (
ber_sequence_of_serialize!($($token)*);
ber_sequence_of_deserialize!($($token)*);
);
}
#[macro_export]
macro_rules! ber_sequence_of_serialize {
(impl: $rs_type:ty) => (
fn serialize_value<E: $crate::BerEncRules, W: ::std::io::Write>
(&self, e: E, writer: &mut W) -> Result<(), $crate::err::EncodeError> {
for item in self {
try!($crate::BerSerialize::serialize_enc(item, e, writer));
}
Ok(())
}
);
($rs_type:ty) => (
impl $crate::BerSerialize for $rs_type {
ber_sequence_of_serialize!{impl: $rs_type}
}
);
($rs_type:ty => $gen:ident) => (
impl<$gen: $crate::BerSerialize> $crate::BerSerialize for $rs_type {
ber_sequence_of_serialize!{impl: $rs_type}
}
);
($rs_type:ty => $gen:ident, $($where_attr:tt)*) => (
impl<$gen: $crate::BerSerialize> $crate::BerSerialize for $rs_type where $($where_attr)* {
ber_sequence_of_serialize!{impl: $rs_type}
}
);
}
#[macro_export]
macro_rules! ber_sequence_of_deserialize {
(impl: $rs_type:ty) => (
fn deserialize_with_tag<E: $crate::BerEncRules, I: Iterator<Item=::std::io::Result<u8>>>
(e: E, reader: &mut I, tag: $crate::tag::Tag, len: $crate::tag::Len) ->
Result<Self, $crate::err::DecodeError> {
struct SeqOfDecoder<T, F, J: Iterator<Item=::std::io::Result<u8>>> {
len: $crate::tag::Len,
reader: $crate::byte::ByteReader<J>,
e: F,
_p: ::std::marker::PhantomData<T>,
}
impl<T, F, J> Iterator for SeqOfDecoder<T, F, J> where
F: $crate::BerEncRules,
J: Iterator<Item=::std::io::Result<u8>>,
T: $crate::BerDeserialize {
type Item = Result<T, $crate::err::DecodeError>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
match self.len.partial_cmp(&self.reader.count) {
Some(::std::cmp::Ordering::Less) => return Some(Err($crate::err::DecodeError::GreaterLen)),
Some(::std::cmp::Ordering::Equal) => return None,
Some(::std::cmp::Ordering::Greater) | None => {},
}
let (tag, len) = match $crate::tag::read_taglen(&mut self.reader) {
Ok(t) => t,
Err(e) => return Some(Err(e)),
};
if tag.tagnum == 0 && tag.class == $crate::tag::Class::Universal &&
len == $crate::tag::Len::Def(0) {
return None;
}
Some($crate::BerDeserialize::deserialize_value(self.e, &mut self.reader, len))
}
}
if Some(tag) != <Self as $crate::Asn1Info>::asn1_tag() {
return Err($crate::err::DecodeError::TagTypeMismatch);
}
if len == $crate::tag::Len::Indef &&
E::len_rules() == $crate::ber::enc::LenEnc::Definite {
return Err($crate::err::DecodeError::IndefiniteLen);
}
let mut decoder = SeqOfDecoder {
e: e,
len: len.into(),
reader: $crate::byte::ByteReader::new(reader),
_p: ::std::marker::PhantomData,
};
let v: Result<$rs_type, _> = ::std::iter::FromIterator::from_iter(decoder.by_ref());
Ok(try!(v))
}
);
($rs_type:ty) => (
impl $crate::BerDeserialize for $rs_type {
ber_sequence_of_deserialize!{impl: $rs_type}
}
);
($rs_type:ty => $gen:ident) => (
impl<$gen: $crate::BerDeserialize> $crate::BerDeserialize for $rs_type {
ber_sequence_of_deserialize!{impl: $rs_type}
}
);
($rs_type:ty => $gen:ident, $($where_attr:tt)*) => (
impl<$gen: $crate::BerDeserialize> $crate::BerDeserialize for $rs_type where $($where_attr)* {
ber_sequence_of_deserialize!{impl: $rs_type}
}
);
}
use std::collections::HashSet;
use std::hash::Hash;
asn1_info!(Vec<T> => T, ::tag::Class::Universal, 16, true, "SEQUENCE OF");
ber_sequence_of!(Vec<T> => T);
asn1_info!(HashSet<T> => T, ::tag::Class::Universal, 17, true, "SET OF");
ber_sequence_of!(HashSet<T> => T, T: Eq + Hash);