Samuel-Tyler/fast_ber

Add support for dynamic encoding

melg8 opened this issue · 3 comments

melg8 commented

Introduction

In current version (master/devel) of library we have fast_ber::encode function, which ber encodes provided asn.1 structure inside provided absl::Span continuous storage. Usage example and tests use std::array or std::vector as storage provider for creation absl::Span. Size of storage varies from test to test in values from 100 to 5000 or even 10000 in some cases.

Problem 1: minimal memory allocation

User can't allocate minimum amount of memory needed to successfully encode provided asn.1 structure. If Pokemon team takes exactly 125 bytes to encode - there is no way to know that before fast_ber::encode call or after bad call, because, as i can see encode_result.length returns 125 only at succsess case (with storage size > 125) and 0 on faliure.

Possible solutions:

  • return minimal needed value at "too small buffer provided" type of failure (this approach takes GNU libtasn1 take a look at asn1_der_coding)
  • provide separate function which calculates needed size without attempting to encode structure

Problem 2: limited available memory (less than full encoded binary)

If user have limited memory or want to send encoded structure asap to some external device or to some stream, without accumulating full binary representation in memory on encoding side - there is no way to do that using current fast_ber::encode function.

Possible solutions:

  • provide callback function which is called on each PDU. (this approach takes asn1c look for der_encode wich have argument asn_app_consume_bytes_f *consume_bytes_cb with description)
  • more general approach to use pair of iterators or, better, single range to be used for encoding instead of hard wired absl::Span.

P.s. I'm really interested in your library to become viable alternative to asn1c in cpp world of ber/der encoding, but i have too little time/discipline/knowledge to code it myself, on the other hand, if you interested, i can sometimes provide issues which i think are important. Anyway great job and thanks for inspiration.

Thank you very much!

Really appreciate your feedback.

Problem one sounds relatively easy fix, with the solution being adding functionality to calculate the encoded length ahead of time.

Problem two sounds more complex, and will require some thought.

The thing that would be most helpful to me is more test data. Both ASN.1 schemas and encoded BER data will allow me to improve the parsing and encoding/decoding. If you have any available it would be of great use to me.

Thank you very much, I hope to hear more of your use case to understand good directions for the project.

Sam

Hi I've added functionality to determine encoded length, this is shown in sample code:
e9da424

I plan on expanding the DecodeResult and EncodeResult functions to include more information on fail soon.

I don't plan on supporting Problem 2 at the moment. Perhaps once the library is more mature.

melg8 commented

That's really great news! I'll take a look at your example and try to play with it. Solving at least size problem is very helpful. Thanks for your work. If you want you can close that issue than.