asn1_len
A crate that allow transformation between ASN.1 Length object and various unsigned int types.
Supported operation
Compile time ASN.1 Length declaration
This crate provides value_def helper function to parse/tranform LitInt when dealing with procedural macro. The function return tuple of (TokenStream, usize).
The TokenStream contains a comma separated value of LitInt which conform to ASN.1 format. Each LitInt is a byte. If there're more than one byte, the first byte is encoded length of the length itself. See this wiki for how it encode the length.
The usize contains a number of LitInt inside the TokenStream including the encoded length byte.
It is mandatory that the procedural macro that make use of this function must produce a complete valid syntax of Rust.
Example
Build fixed size array of ASN.1 Length
let = value_def!;
quote! // Build a fixed size array of ASN.1 Length
Run time ASN.1 Length declaration
This crate provides struct Asn1Len. The struct provides method with_header and without_header. with_header returns a slice contains a complete byte sequence including an encoded length of the length itself. See this wiki for how it encode the length. . without_header simply return length value encoded as big-endian byte sequence. The only different between the two is the first byte. without_header excludes the first byte of with_header method.
To construct Asn1Len, there are 3 methods.
Asn1Len::from_primitiveorAsn1Len::fromusingstd::convert::Fromtrait. It allow any of Rust unsigned integer primitive to be convert intoAsn1Len.Asn1Len::from_big_uintmethod to create an encoded ASN.1 Length out of givenUintobject.Asn1Len::from_boxed_big_uintmethod to create an encoded ASN.1 Length out of givenBoxedUintobject.
Only Asn1Len::from_primitive guarantee to return Asn1Len. The others return Result which if value is too big to be encoded into ASN.1 Length, it return OverflowError.
To work with Asn1Len, this required a convert back from Asn1Len object into some kind of workable type such as Rust primitive data type or those of Uint variants.
This crate provide 3 methods.
Asn1Len::to_primitive. It allow a conversion to one of unsigned integer type of Rust. If value cannot be fit into such type, it returnOverflowError.Asn1Len::to_big_uint. It allow a conversion tocrypto_bigint::Uinttype. If theLIMBspecified is not large enough to accommodate current ASN.1 Length, it returnOverflowError.Asn1Len::to_boxed_big_uint. It allow a conversion tocrypto_bigint::BoxedUinttype.
Only Asn1Len::to_boxed_big_uint guarantee to return BoxedUint. The others return Result which if value is too big to be accommodate to target type, it return OverflowError.
Example
use Asn1Len;
let len = from_primitive;
assert_eq!;
let new_len = from_primitive;
assert_eq!;
new_len..unwrap_err; // Value too large to fit into u8
let big_len = from_big_uint.unwrap;
big_len..unwrap_err; // Value too large to fit into this Uint with 1 limb
let variable_len = big_len.to_boxed_big_uint.widen; // Make 4096 bits with value from previous big_len
variable_len.checked_add.unwrap;
// Make largest possible length in ASN.1 Length specification
let mut max_len: = ;
max_len = 0;
max_len = 0;
let max_val = 128 / BYTES as usize}>from_be_bytes;
from_big_uint.unwrap; // it should succeed.
let overflow_val = max_val.checked_add.unwrap; // Add 1 to max_len
from_big_uint.unwrap_err; // It should overflow when turn it into ASN.1 Length
What's new
1.1.0
Asn1Len now implements PartialEq, Eq, PartialOrd, and Ord