Giter Club home page Giter Club logo

asn1_aper's Introduction

This crate provides tools for encoding and decoding ASN.1 messages.

Currently, only the Aligned Packed Encoding Rules (APER) are supported.

Documentation

See https://melvinw.github.io/rust-asn1/asn1.

Usage

Add the following to your Cargo.toml.

[dependencies]
asn1 = { git = "https://github.com/melvinw/rust-asn1" }

To encode/decode your own types, just implement the APerElement trait. Below is an example for a simple ASN.1 messsage, foo.

foo ::= SEQUENCE {
    bar BIT STRING(SIZE(4)
    baz INTEGER(0..4294967295)
}
#![feature(associated_consts)]
extern crate asn1;
use asn1::BitString;
use asn1::aper::{self, APerElement, Constraint, Constraints, UNCONSTRAINED};

struct foo {
    pub bar: BitString,
    pub baz: u32,
}

impl APerElement for Foo {
    const CONSTRAINTS: Constraints = UNCONSTRAINED;
    fn from_aper(decoder: &mut aper::Decoder, constraints: Constraints) -> Result<Self, aper::DecodeError> {
        let bar = BitString::from_aper(decoder , Constraints {
            value: None,
            size: Some(Constraint::new(Some(4), Some(4))),
        });

        let mut baz = u32::from_aper(decoder, UNCONSTRAINED);

        if bar.is_err() {
            return Err(bar.err().unwrap());
        }
        if baz.is_err() {
            return Err(baz.err().unwrap());
        }

        Ok(Foo{
            bar: bar.unwrap(),
            baz: baz.unwrap(),
        })
    }

    fn to_aper(&self, constraints: Constraints) -> Result<Encoding, aper::EncodeError> {
        let mut enc = self.bar.to_aper(Constraints {
            value: None,
            size: Some(Constraint::new(None, Some(4))),
        }).unwrap();

        enc.append(&self.baz.to_aper(UNCONSTRAINED).unwrap());

        Ok(enc)
    }
}

asn1_aper's People

Contributors

melvinw avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.