简体   繁体   中英

Is there a good C++ library to read, create and modify BER encoded files?

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM