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
use crate::*;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::convert::TryFrom;
use core::fmt::Debug;
use self::debug::{macros::debug_eprintln, trace, trace_generic};
// // XXX this compiles but requires bound TryFrom :/
// impl<'a, 'b, T> TryFrom<&'b Any<'a>> for Vec<T>
// where
// T: TryFrom<&'b Any<'a>>,
// for<'e> <T as TryFrom<&'b Any<'a>>>::Error: From<Error>,
// T: FromBer<'a, <T as TryFrom<&'b Any<'a>>>::Error>,
// // T: FromBer<'a, E>,
// // E: From<Error>,
// {
// type Error = <T as TryFrom<&'b Any<'a>>>::Error;
// fn try_from(any: &'b Any<'a>) -> Result<Vec<T>, Self::Error> {
// any.tag().assert_eq(Self::TAG)?;
// any.header.assert_constructed()?;
// let v = SequenceIterator::<T, BerParser, Self::Error>::new(any.data)
// .collect::<Result<Vec<T>, Self::Error>>()?;
// Ok(v)
// }
// }
// // XXX this compiles but requires bound TryFrom :/
// impl<'a, 'b, T> TryFrom<&'b Any<'a>> for Vec<T>
// where
// T: TryFrom<&'b Any<'a>>,
// <T as TryFrom<&'b Any<'a>>>::Error: From<Error>,
// T: FromBer<'a, <T as TryFrom<&'b Any<'a>>>::Error>,
// // T: FromBer<'a, E>,
// // E: From<Error>,
// {
// type Error = <T as TryFrom<&'b Any<'a>>>::Error;
// fn try_from(any: &'b Any<'a>) -> Result<Vec<T>, Self::Error> {
// any.tag().assert_eq(Self::TAG)?;
// any.header.assert_constructed()?;
// let v = SequenceIterator::<T, BerParser, Self::Error>::new(any.data)
// .collect::<Result<Vec<T>, Self::Error>>()?;
// Ok(v)
// }
// }
impl<'a, T> TryFrom<Any<'a>> for Vec<T>
where
T: FromBer<'a>,
{
type Error = Error;
fn try_from(any: Any<'a>) -> Result<Self> {
trace_generic(
core::any::type_name::<Self>(),
"T::from(Any)",
|any| {
any.tag().assert_eq(Self::TAG)?;
any.header.assert_constructed()?;
let res_items: Result<Vec<T>> =
SetIterator::<T, BerParser>::new(any.data).collect();
if res_items.is_err() {
debug_eprintln!(
core::any::type_name::<T>(),
"≠ {}",
"Conversion from Any failed".red()
);
}
res_items
},
any,
)
}
}
impl<T> CheckDerConstraints for Vec<T>
where
T: CheckDerConstraints,
{
fn check_constraints(any: &Any) -> Result<()> {
any.tag().assert_eq(Self::TAG)?;
any.header.assert_constructed()?;
for item in SequenceIterator::<Any, DerParser>::new(any.data) {
let item = item?;
<T as CheckDerConstraints>::check_constraints(&item)?;
}
Ok(())
}
}
impl<T> Tagged for Vec<T> {
const TAG: Tag = Tag::Sequence;
}
// impl<'a, T> FromBer<'a> for Vec<T>
// where
// T: FromBer<'a>,
// {
// fn from_ber(bytes: &'a [u8]) -> ParseResult<Self> {
// let (rem, any) = Any::from_ber(bytes)?;
// any.header.assert_tag(Self::TAG)?;
// let v = SequenceIterator::<T, BerParser>::new(any.data).collect::<Result<Vec<T>>>()?;
// Ok((rem, v))
// }
// }
/// manual impl of FromDer, so we do not need to require `TryFrom<Any> + CheckDerConstraints`
impl<'a, T, E> FromDer<'a, E> for Vec<T>
where
T: FromDer<'a, E>,
E: From<Error> + Debug,
{
fn from_der(bytes: &'a [u8]) -> ParseResult<Self, E> {
trace_generic(
core::any::type_name::<Self>(),
"Sequence::from_der",
|bytes| {
let (rem, any) = trace(core::any::type_name::<Self>(), parse_der_any, bytes)
.map_err(Err::convert)?;
any.header
.assert_tag(Self::TAG)
.map_err(|e| Err::Error(e.into()))?;
let v = SequenceIterator::<T, DerParser, E>::new(any.data)
.collect::<Result<Vec<T>, E>>()
.map_err(Err::Error)?;
Ok((rem, v))
},
bytes,
)
}
}
#[cfg(feature = "std")]
impl<T> ToDer for Vec<T>
where
T: ToDer,
{
fn to_der_len(&self) -> Result<usize> {
let mut len = 0;
for t in self.iter() {
len += t.to_der_len()?;
}
let header = Header::new(Class::Universal, true, Self::TAG, Length::Definite(len));
Ok(header.to_der_len()? + len)
}
fn write_der_header(&self, writer: &mut dyn std::io::Write) -> SerializeResult<usize> {
let mut len = 0;
for t in self.iter() {
len += t.to_der_len().map_err(|_| SerializeError::InvalidLength)?;
}
let header = Header::new(Class::Universal, true, Self::TAG, Length::Definite(len));
header.write_der_header(writer).map_err(Into::into)
}
fn write_der_content(&self, writer: &mut dyn std::io::Write) -> SerializeResult<usize> {
let mut sz = 0;
for t in self.iter() {
sz += t.write_der(writer)?;
}
Ok(sz)
}
}