From d507c1fa99f881ff7f21a289f4efa2b64f4f9c4b Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Fri, 8 Jun 2018 05:29:23 -0400 Subject: Allow passing a writer function callback to DER_Encoder --- src/lib/asn1/der_enc.cpp | 18 +++++++++--------- src/lib/asn1/der_enc.h | 10 +++++++++- 2 files changed, 18 insertions(+), 10 deletions(-) (limited to 'src/lib') diff --git a/src/lib/asn1/der_enc.cpp b/src/lib/asn1/der_enc.cpp index c1ec010a0..99b833d27 100644 --- a/src/lib/asn1/der_enc.cpp +++ b/src/lib/asn1/der_enc.cpp @@ -68,7 +68,7 @@ void encode_length(std::vector& encoded_length, size_t length) DER_Encoder::DER_Encoder(secure_vector& vec) { - m_append_output_fn = [&vec](const uint8_t b[], size_t l) + m_append_output = [&vec](const uint8_t b[], size_t l) { vec.insert(vec.end(), b, b + l); }; @@ -76,7 +76,7 @@ DER_Encoder::DER_Encoder(secure_vector& vec) DER_Encoder::DER_Encoder(std::vector& vec) { - m_append_output_fn = [&vec](const uint8_t b[], size_t l) + m_append_output = [&vec](const uint8_t b[], size_t l) { vec.insert(vec.end(), b, b + l); }; @@ -154,7 +154,7 @@ secure_vector DER_Encoder::get_contents() if(m_subsequences.size() != 0) throw Invalid_State("DER_Encoder: Sequence hasn't been marked done"); - if(m_append_output_fn) + if(m_append_output) throw Invalid_State("DER_Encoder Cannot get contents when using output vector"); secure_vector output; @@ -167,7 +167,7 @@ std::vector DER_Encoder::get_contents_unlocked() if(m_subsequences.size() != 0) throw Invalid_State("DER_Encoder: Sequence hasn't been marked done"); - if(m_append_output_fn) + if(m_append_output) throw Invalid_State("DER_Encoder Cannot get contents when using output vector"); std::vector output(m_default_outbuf.begin(), m_default_outbuf.end()); @@ -231,9 +231,9 @@ DER_Encoder& DER_Encoder::raw_bytes(const uint8_t bytes[], size_t length) { m_subsequences[m_subsequences.size()-1].add_bytes(bytes, length); } - else if(m_append_output_fn) + else if(m_append_output) { - m_append_output_fn(bytes, length); + m_append_output(bytes, length); } else { @@ -257,10 +257,10 @@ DER_Encoder& DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, { m_subsequences[m_subsequences.size()-1].add_bytes(hdr.data(), hdr.size(), rep, length); } - else if(m_append_output_fn) + else if(m_append_output) { - m_append_output_fn(hdr.data(), hdr.size()); - m_append_output_fn(rep, length); + m_append_output(hdr.data(), hdr.size()); + m_append_output(rep, length); } else { diff --git a/src/lib/asn1/der_enc.h b/src/lib/asn1/der_enc.h index f351817f9..135a70d07 100644 --- a/src/lib/asn1/der_enc.h +++ b/src/lib/asn1/der_enc.h @@ -23,6 +23,8 @@ class ASN1_Object; class BOTAN_PUBLIC_API(2,0) DER_Encoder final { public: + typedef std::function append_fn; + /** * DER encode, writing to an internal buffer * Use get_contents or get_contents_unlocked to read the results @@ -42,6 +44,12 @@ class BOTAN_PUBLIC_API(2,0) DER_Encoder final */ DER_Encoder(std::vector& vec); + /** + * DER encode, calling append to write output + * If this constructor is used, get_contents* may not be called. + */ + DER_Encoder(append_fn append) : m_append_output(append) {} + secure_vector get_contents(); std::vector get_contents_unlocked(); @@ -203,7 +211,7 @@ class BOTAN_PUBLIC_API(2,0) DER_Encoder final std::vector< secure_vector > m_set_contents; }; - std::function m_append_output_fn; + append_fn m_append_output; secure_vector m_default_outbuf; std::vector m_subsequences; }; -- cgit v1.2.3