All Projects → Samuel-Tyler → Fast_ber

Samuel-Tyler / Fast_ber

Licence: bsl-1.0
A C++11 ASN.1 BER Encoding and Decoding Library

Programming Languages

c
50402 projects - #5 most used programming language
cpp
1120 projects

Projects that are alternatives of or similar to Fast ber

Ffmpeg Video Player
An FFmpeg and SDL Tutorial.
Stars: ✭ 149 (+175.93%)
Mutual labels:  cmake, encoding, decoding
Go Geom
Package geom implements efficient geometry types for geospatial applications.
Stars: ✭ 456 (+744.44%)
Mutual labels:  encoding, decoding
Libmorton
C++ header-only library with methods to efficiently encode/decode Morton codes in/from 2D/3D coordinates
Stars: ✭ 373 (+590.74%)
Mutual labels:  encoding, decoding
Pbf
A low-level, lightweight protocol buffers implementation in JavaScript.
Stars: ✭ 618 (+1044.44%)
Mutual labels:  encoding, decoding
Libspng
Simple, modern libpng alternative
Stars: ✭ 265 (+390.74%)
Mutual labels:  cmake, decoding
Xmorse
🌞 ~1.5Kb morse code library for all. 一个支持 Unicode 中文摩斯密码编码的 Javascript 库。
Stars: ✭ 266 (+392.59%)
Mutual labels:  encoding, decoding
Flac
Free Lossless Audio Codec
Stars: ✭ 593 (+998.15%)
Mutual labels:  encoding, decoding
avro ex
An Avro Library that emphasizes testability and ease of use.
Stars: ✭ 47 (-12.96%)
Mutual labels:  encoding, decoding
Encoding
Go package containing implementations of efficient encoding, decoding, and validation APIs.
Stars: ✭ 705 (+1205.56%)
Mutual labels:  encoding, decoding
Scodec
Scala combinator library for working with binary data
Stars: ✭ 709 (+1212.96%)
Mutual labels:  encoding, decoding
Anycodable
Type-erased wrappers for Encodable, Decodable, and Codable values
Stars: ✭ 811 (+1401.85%)
Mutual labels:  encoding, decoding
AnimatedGif
📼 A high performance .NET library for reading and creating animated GIFs
Stars: ✭ 106 (+96.3%)
Mutual labels:  encoding, decoding
scure-base
Secure, audited & 0-deps implementation of bech32, base64, base32, base16 & base58
Stars: ✭ 27 (-50%)
Mutual labels:  encoding, decoding
Serpent
A protocol to serialize Swift structs and classes for encoding and decoding.
Stars: ✭ 281 (+420.37%)
Mutual labels:  encoding, decoding
velvet-video
Java library for encoding / decoding / muxing / demuxing video and audio in various formats
Stars: ✭ 32 (-40.74%)
Mutual labels:  encoding, decoding
Hashids.net
A small .NET package to generate YouTube-like hashes from one or many numbers. Use hashids when you do not want to expose your database ids to the user.
Stars: ✭ 470 (+770.37%)
Mutual labels:  encoding, decoding
X509
A PHP library for X.509 public key certificates, attribute certificates, certification requests and certification path validation.
Stars: ✭ 27 (-50%)
Mutual labels:  encoding, decoding
go-fixedwidth
Encoding and decoding for fixed-width formatted data
Stars: ✭ 64 (+18.52%)
Mutual labels:  encoding, decoding
sirdez
Glorious Binary Serialization and Deserialization for TypeScript.
Stars: ✭ 20 (-62.96%)
Mutual labels:  encoding, decoding
Decodify
Detect and decode encoded strings, recursively.
Stars: ✭ 670 (+1140.74%)
Mutual labels:  encoding, decoding

fast_ber version Appveyor status Travis status C++11 C++14 C++17 C++20 clang-format

A performant ASN.1 BER encoding and decoding library written in C++11

Introduction

fast_ber is a small, lightweight library for BER encoding and decoding. Fast ber forgoes some tight ASN.1 specification conformance to provide fast encoding and decoding performance in common use cases

Design Decisions

  • Simple, modern C++ interface
  • ASN.1 sequences are represented as POD structs - no private members or complex getters and setters
  • No exceptions, no RTTI and limited memory allocations (everything is small buffer optimised)
  • View classes are provided for zero copy decoding
  • Interfaces mimic STL types such as std::string, std::vector and std::optional

Read more about the decisions made writing this library

Limitations

  • No circular data structures
  • Size and value constraints are not implemented

Tools

fast_ber_view can be used to dump the contents of a BER PDU, without requiring a schema.

./build/src/fast_ber_view ./build_gcc/sample/pokemon.ber | jq
{
  "length": 125,
  "identifier": {
    "class": "Universal",
    "tag": "Sequence / Sequence Of"
  },
  "content": ...
}

Call for Test Data

Test data is wanted to improve this project! If you have any test ASN.1 specs or BER files please share them. More test data will improve parsing and help find any issues with the library.

Example Project

fast_ber is designed to be easy to consume via CMake. This is demonstrated in fast_ber_ldap3, an example project using fast_ber to create and dissect LDAP3 certificates.

Compiler Usage

  1. Build the compiler:
git submodule update --init
mkdir build_cmake
cd build_cmake
cmake ..
make
ctest
  1. Define an ASN.1 file - Example: pokemon.asn
Pokemon DEFINITIONS EXPLICIT TAGS ::= BEGIN

Team ::= SEQUENCE {
    team-name OCTET STRING,
    members SEQUENCE OF Pokemon
}

Pokemon ::= SEQUENCE {
    name OCTET STRING,
    category OCTET STRING,
    type Type,
    ability OCTET STRING,
    weakness OCTET STRING,
    weight INTEGER
}

Type ::= ENUMERATED {
    normal,
    fire,
    fighting,
    water,
    flying,
    grass
}

END
  1. Compile an asn file into a header file defining the ber structure
cd build_cmake
./src/fast_ber_compiler pokemon.asn pokemon

output:

#pragma once

#include "fast_ber/ber_types/All.hpp"
#include "pokemon.fwd.hpp"


namespace fast_ber {
using namespace abbreviations;

namespace Pokemon {

enum class TypeValues {
    normal,
    fire,
    fighting,
    water,
    flying,
    grass,
};

template <typename Identifier>
FAST_BER_ALIAS(Type, Enumerated<TypeValues>);

template <typename Identifier>
struct Pokemon {
    OctetString<> name;
    OctetString<> category;
    Type<> type;
    OctetString<> ability;
    OctetString<> weakness;
    Integer<> weight;
};

template <typename Identifier>
struct Team {
    OctetString<> team_name;
    SequenceOf<Pokemon<>> members;
};

} // End namespace Pokemon
} // End namespace fast_ber
  1. Include the header file into your application. Fields in the generated structure can be assigned to standard types. Encode and decode functions are used to serialize and deserialize the data
#include "pokemon.hpp"

int main()
{
    fast_ber::Pokemon::Team<>    team    = {"Sam's Team"};
    fast_ber::Pokemon::Pokemon<> muchlax = { "Munchlax",
                                             "Big Eater",
                                             fast_ber::Pokemon::Type<>::Values::normal,
                                             "Thick Fat, Pickup",
                                             "Fighting",
                                             105 };
    fast_ber::Pokemon::Pokemon<> piplup  = { "Piplup",
                                             "Penguin",
                                             fast_ber::Pokemon::Type<>::Values::water,
                                             Torrent",
                                             "Electric, Grass",
                                             12 };
    team.members.push_back(muchlax);
    team.members.push_back(piplup);

    std::array<uint8_t, 2000> buffer{};
    const auto                encode_result = fast_ber::encode(absl::Span<uint8_t>(buffer), team);
    if (!encode_result.success)
    {
        return -1;
    }

    return 0;
}

Take a look at fast_ber_ldap3 for an example of these steps in action.

Features

fast_ber is in development. The following table documents what has been implemented and what features are coming soon

Type Implemented
Integer Yes
Boolean Yes
Strings Yes
Sequence Yes
Sequence Of Yes
Choice Yes
Set No
Set Of Yes
Enumerated Yes
Object Identifier Yes
Dates / Times No
Null Yes
Any No
Feature Implemented
Explicit tags Yes
Implicit tags Yes
Indefinitive length No
Arbitrary top level structure Yes
Arbritary definition order Yes
Embedded Types Yes
Imports Yes

Possible Future Extensions

The following features may be added in the future

Partial decoding
Partial encode at compile time (templated / constexpr)
High / low conformance modes
DER mode

Benchmarks

fast_ber includes benchmarks against asn1c, an ASN library written in C. Here is an example of the output:

-------------------------------------------------------------------------------
Benchmark: Decode Performance
-------------------------------------------------------------------------------
...............................................................................

benchmark name                                  iters   elapsed ns      average
-------------------------------------------------------------------------------
fast_ber        - 1,000,000 x decode 2107B pdu      1    516381442   516.381 ms
asn1c           - 1,000,000 x decode 2107B pdu      1   4996255249    4.99626 s
fast_ber        - 1,000,000 x decode 64B pdu        1    192230063    192.23 ms
asn1c           - 1,000,000 x decode 64B pdu        1   2581069031    2.58107 s

-------------------------------------------------------------------------------
Benchmark: Encode Performance
-------------------------------------------------------------------------------
...............................................................................

benchmark name                                  iters   elapsed ns      average
-------------------------------------------------------------------------------
fast_ber        - 1,000,000 x encode 2107B pdu      1    191266512   191.267 ms
asn1c           - 1,000,000 x encode 2107B pdu      1   7349946740    7.34995 s

-------------------------------------------------------------------------------
Benchmark: Object Construction Performance
-------------------------------------------------------------------------------
...............................................................................

benchmark name                                  iters   elapsed ns      average
-------------------------------------------------------------------------------
fast_ber        - 1,000,000 x construct data        1   1005938231    1.00594 s
asn1c           - 1,000,000 x construct data        1    511881940   511.882 ms

-------------------------------------------------------------------------------
Benchmark: Calculate Encoded Length Performance
-------------------------------------------------------------------------------
...............................................................................

benchmark name                                  iters   elapsed ns      average
-------------------------------------------------------------------------------
fast_ber        - 1,000,000 x encoded length        1     17084558   17.0846 ms

===============================================================================
All tests passed (31 assertions in 8 test cases)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].