diff options
author | Jack Lloyd <[email protected]> | 2018-06-08 05:29:23 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-06-08 05:29:23 -0400 |
commit | d507c1fa99f881ff7f21a289f4efa2b64f4f9c4b (patch) | |
tree | 8e20d78e4a6858f868289b2ae568c8b1bd4f0c83 /src/lib/asn1 | |
parent | e57a7a00a172cadce319714c1a5110c3360a8abc (diff) |
Allow passing a writer function callback to DER_Encoder
Diffstat (limited to 'src/lib/asn1')
-rw-r--r-- | src/lib/asn1/der_enc.cpp | 18 | ||||
-rw-r--r-- | src/lib/asn1/der_enc.h | 10 |
2 files changed, 18 insertions, 10 deletions
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<uint8_t>& encoded_length, size_t length) DER_Encoder::DER_Encoder(secure_vector<uint8_t>& 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<uint8_t>& vec) DER_Encoder::DER_Encoder(std::vector<uint8_t>& 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<uint8_t> 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<uint8_t> output; @@ -167,7 +167,7 @@ std::vector<uint8_t> 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<uint8_t> 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<void (const uint8_t[], size_t)> 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<uint8_t>& 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<uint8_t> get_contents(); std::vector<uint8_t> get_contents_unlocked(); @@ -203,7 +211,7 @@ class BOTAN_PUBLIC_API(2,0) DER_Encoder final std::vector< secure_vector<uint8_t> > m_set_contents; }; - std::function<void (const uint8_t[], size_t)> m_append_output_fn; + append_fn m_append_output; secure_vector<uint8_t> m_default_outbuf; std::vector<DER_Sequence> m_subsequences; }; |