11

There are several tools that can automatically generate C++ (or other) code for reading and writing BER encoded files. In my C++ project, I need libraries to read and modify BER encoded files. I can not generate C++ classes based on a given data structure because there is no given data structure. The user should be able to add and delete integers, strings etc. I found an open source project that has an editor with this kind of functionality: http://www.codeproject.com/Articles/4910/ASN-1-Editor

However, this is in C# ....

Please let me know, if you know how I can get a good C++ library with this functionality which I can use for my C++ project.

Daan
  • 2,478
  • 3
  • 36
  • 76
  • 1
    possible duplicate of [ASN.1 encoded file](http://stackoverflow.com/questions/9902410/asn-1-encoded-file) – Some programmer dude Jan 17 '13 at 14:01
  • I am not really sure if this is the same problem as the problem of Joachim. I do not only need multiple records, I need the same flexibility as in editors. – Daan Jan 17 '13 at 14:49
  • I just concluded that for C++.NET, these libraries exists: http://msdn.microsoft.com/en-us/library/system.directoryservices.protocols.berconverter.encode.aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1 However, I am not using .NET in my project. – Daan Jan 17 '13 at 16:17
  • Note that in general you MUST have the ASN.1 specification that corresponds with the data you want to encode/decode. If you are looking for a tool that can read any arbitrary BER-encoded data and pick out the INTEGER fields &c. without taking the ASN.1 spec as input, you are searching in vain. – Kevin Jan 18 '13 at 14:22
  • 1
    In general, yes. In my situation the ASN.1 specification is flexibile. It needs to be defined by the user not by me as a developer. This is not a strange situation. The people who make generic ASN.1 editors also do not have an ASN.1 specification. What I am looking for exists: http://msdn.microsoft.com/en-us/library/system.directoryservices.protocols.berconverter.encode.aspx . But this is a C++.NET solution. This library has a function " static array^ Encode(String^ format, array^ value". The format is flexible. This is the kind of library I look for (but not .NET). – Daan Jan 18 '13 at 15:57

3 Answers3

3

Make sure that you have a correct ASN defintion file. Then go to link http://lionet.info/asn1c/asn1c.cgi

paste your ASN definition in the given window. Press the button "Proceed with ASN.1 compilation". If you get any compilation error rectify those. After the compilation is successful it will generate the code for your decoder. Give it a try its good.

paper.plane
  • 1,201
  • 10
  • 17
  • The problem is that the ASN definition is flexible.... This makes it hard to create an ASN definition file. This is also the case for editors like this one: http://www.codeproject.com/Articles/4910/ASN-1-Editor (which is in C#). I would like to have the same flexibility which is in this editor. – Daan Jan 17 '13 at 14:47
3

There aren't a plethora of generic BER reading libraries because it is NOT POSSIBLE to unambiguously parse arbitrary BER data without at least some knowledge of the schema.

The tool you point to at CodeProject edits ASN.1 schemas, which are USED to create BER encodings, but you CAN NOT go in the other direction without knowing things about the data, or by guessing. There is no way to tell, by looking at the id & length of a BER element, whether that element contains primitive data, or other BER elements.

Here's the smallest example I came up with to demonstrate the problem, a mere 5 bytes, with two radically different interpretations:

0xA0 0x03 0x02 0x01 0x00

Now, that's clearly a constructed, context-specific type. It's also of definite length. But what's inside it? You can't tell!

It could be an integer (0x02 is the primitive tag for integer, 0x01 is the len, value 0x00).

But it could also be a bitstring (you can put a bit string into a constructed type, and then the 2 is the # of bits unused at the end, meaning we have 14 bits, which comes out 0000 0001 0000 00xxb = 00 0000 0100 0000b = 0x0040.

You can write an editor that would read/modify/write BER files, but it has to know the schema, or it's going to be unable to read anything in properly.

Michael Kohne
  • 11,888
  • 3
  • 47
  • 79
  • You use a "Context-specific" tag without context, of course it's ambiguous! If you introduce the "Application" or "Private" classes it gets even worse. Still it's quite possible to generically parse and display the data as hex-strings which will make it possible for you to edit it even if the editor does not know the actual schema. As long as the one editing has some idea about it. – Samuel Åslund Mar 06 '15 at 16:01
  • @SamuelÅslund - absolutely. I interpreted the OP as wanting to edit BER WITHOUT knowing the schema. – Michael Kohne Mar 06 '15 at 16:38
3

Shameless self plug:

I have created an open source ASN1 ber encoder / decoder library which provides a modern C++ interface.

The library provides a compiler which generates .hpp include files from an ASN.1 specification. Functionality to encode and decode BER data according to these specs is provided in the include files.

https://github.com/Samuel-Tyler/fast_ber

Sam Tyler
  • 51
  • 3