chore: checkpoint before Python removal

This commit is contained in:
2026-03-26 22:33:59 +00:00
parent 683cec9307
commit e568ddf82a
29972 changed files with 11269302 additions and 2 deletions

View File

@@ -0,0 +1 @@
{"files":{".cargo_vcs_info.json":"25161fe6c96766e8e12867863d667f90ad4cab46baaa13cdf7dda664ce16a7b4","Cargo.lock":"a012731eb4cbf565de708b85dd95b1b4c6c3cb55b8072ccafd6e1742bb30bcae","Cargo.toml":"8fd0e2bea8ed9b98d689c764aab519f7c411078f571e0cf113054f6ddae695f5","Cargo.toml.orig":"c87f9704a05e8cc11baa6ce853a2f3df7850baaef8f7ffd4744f0b21ac758d8b","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"a5c61b93b6ee1d104af9920cf020ff3c7efe818e31fe562c72261847a728f513","src/alias.rs":"dfd9578f8631038a60ead96c6fad24d5a07ede8301d094621d647c7e6c06e2d0","src/container.rs":"a3d02d0bf7d47a9e81f094b999cec2c9336fe781d893538f037aa7e1310a1d44","src/lib.rs":"4e9adbe0f76dfcc3d407005f71e1d56e7ed095f8761ec89fcd9b18323deec753","src/sequence.rs":"27c481f83db11619f32092953ad0d302d01f114d5f771f3dda280f14b819d1f0","src/set.rs":"92ab517a7cdaefbd69bb427ff3c5fc27b38b063ea9ab1131c71600c64bac40c3","src/tostatic.rs":"32ddcac7714e08462e551d341016b1b067d9f052ef1bf5b7f65bdb1e3d679124"},"package":"3109e49b1e4909e9db6515a30c633684d68cdeaa252f215214cb4fa1a5bfee2c"}

View File

@@ -0,0 +1,6 @@
{
"git": {
"sha1": "f0bf28d2ce0697604110db64d89ca78bbf53ef58"
},
"path_in_vcs": "derive"
}

59
vendor/asn1-rs-derive/Cargo.lock generated vendored Normal file
View File

@@ -0,0 +1,59 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "asn1-rs-derive"
version = "0.6.0"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]]
name = "proc-macro2"
version = "1.0.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "2.0.96"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5d0adab1ae378d7f53bdebc67a39f1f151407ef230f0ce2883572f5d8985c80"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "synstructure"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"

45
vendor/asn1-rs-derive/Cargo.toml vendored Normal file
View File

@@ -0,0 +1,45 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2018"
name = "asn1-rs-derive"
version = "0.6.0"
authors = ["Pierre Chifflier <chifflier@wzdftpd.net>"]
build = false
autolib = false
autobins = false
autoexamples = false
autotests = false
autobenches = false
description = "Derive macros for the `asn1-rs` crate"
homepage = "https://github.com/rusticata/asn1-rs"
readme = false
license = "MIT OR Apache-2.0"
repository = "https://github.com/rusticata/asn1-rs.git"
[lib]
name = "asn1_rs_derive"
path = "src/lib.rs"
proc-macro = true
[dependencies.proc-macro2]
version = "1.0"
[dependencies.quote]
version = "1.0"
[dependencies.syn]
version = "2.0"
features = ["full"]
[dependencies.synstructure]
version = "0.13"

201
vendor/asn1-rs-derive/LICENSE-APACHE vendored Normal file
View File

@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

25
vendor/asn1-rs-derive/LICENSE-MIT vendored Normal file
View File

@@ -0,0 +1,25 @@
Copyright (c) 2017 Pierre Chifflier
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

63
vendor/asn1-rs-derive/src/alias.rs vendored Normal file
View File

@@ -0,0 +1,63 @@
use crate::container::*;
use proc_macro2::Span;
use quote::quote;
use syn::{Data, Ident};
pub fn derive_ber_alias(s: synstructure::Structure) -> proc_macro2::TokenStream {
let ast = s.ast();
let container = match &ast.data {
Data::Struct(ds) => Container::from_datastruct(ds, ast, ContainerType::Alias),
_ => panic!("Unsupported type, cannot derive"),
};
let debug_derive = ast.attrs.iter().any(|attr| {
attr.meta
.path()
.is_ident(&Ident::new("debug_derive", Span::call_site()))
});
let impl_tryfrom = container.gen_tryfrom();
let impl_tagged = container.gen_tagged();
let ts = s.gen_impl(quote! {
extern crate asn1_rs;
#impl_tryfrom
#impl_tagged
});
if debug_derive {
eprintln!("{}", ts);
}
ts
}
pub fn derive_der_alias(s: synstructure::Structure) -> proc_macro2::TokenStream {
let ast = s.ast();
let container = match &ast.data {
Data::Struct(ds) => Container::from_datastruct(ds, ast, ContainerType::Alias),
_ => panic!("Unsupported type, cannot derive"),
};
let debug_derive = ast.attrs.iter().any(|attr| {
attr.meta
.path()
.is_ident(&Ident::new("debug_derive", Span::call_site()))
});
let impl_tryfrom = container.gen_tryfrom();
let impl_tagged = container.gen_tagged();
let impl_checkconstraints = container.gen_checkconstraints();
let impl_fromder = container.gen_fromder();
let ts = s.gen_impl(quote! {
extern crate asn1_rs;
#impl_tryfrom
#impl_tagged
#impl_checkconstraints
#impl_fromder
});
if debug_derive {
eprintln!("{}", ts);
}
ts
}

550
vendor/asn1-rs-derive/src/container.rs vendored Normal file
View File

@@ -0,0 +1,550 @@
use proc_macro2::{Literal, Span, TokenStream};
use quote::{quote, ToTokens};
use syn::{
parse::ParseStream, parse_quote, spanned::Spanned, Attribute, DataStruct, DeriveInput, Field,
Fields, Ident, Lifetime, LitInt, Meta, Type, WherePredicate,
};
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ContainerType {
Alias,
Sequence,
Set,
}
impl ToTokens for ContainerType {
fn to_tokens(&self, tokens: &mut TokenStream) {
let s = match self {
ContainerType::Alias => quote! {},
ContainerType::Sequence => quote! { asn1_rs::Tag::Sequence },
ContainerType::Set => quote! { asn1_rs::Tag::Set },
};
s.to_tokens(tokens)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
enum Asn1Type {
Ber,
Der,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Asn1TagKind {
Explicit,
Implicit,
}
impl ToTokens for Asn1TagKind {
fn to_tokens(&self, tokens: &mut TokenStream) {
let s = match self {
Asn1TagKind::Explicit => quote! { asn1_rs::Explicit },
Asn1TagKind::Implicit => quote! { asn1_rs::Implicit },
};
s.to_tokens(tokens)
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Asn1TagClass {
Universal,
Application,
ContextSpecific,
Private,
}
impl ToTokens for Asn1TagClass {
fn to_tokens(&self, tokens: &mut TokenStream) {
let s = match self {
Asn1TagClass::Application => quote! { asn1_rs::Class::APPLICATION },
Asn1TagClass::ContextSpecific => quote! { asn1_rs::Class::CONTEXT_SPECIFIC },
Asn1TagClass::Private => quote! { asn1_rs::Class::PRIVATE },
Asn1TagClass::Universal => quote! { asn1_rs::Class::UNIVERSAL },
};
s.to_tokens(tokens)
}
}
pub struct Container {
pub container_type: ContainerType,
pub fields: Vec<FieldInfo>,
pub where_predicates: Vec<WherePredicate>,
pub error: Option<Attribute>,
is_any: bool,
}
impl Container {
pub fn from_datastruct(
ds: &DataStruct,
ast: &DeriveInput,
container_type: ContainerType,
) -> Self {
let mut is_any = false;
match (container_type, &ds.fields) {
(ContainerType::Alias, Fields::Unnamed(f)) => {
if f.unnamed.len() != 1 {
panic!("Alias: only tuple fields with one element are supported");
}
match &f.unnamed[0].ty {
Type::Path(type_path)
if type_path
.clone()
.into_token_stream()
.to_string()
.starts_with("Any") =>
{
is_any = true;
}
_ => (),
}
}
(ContainerType::Alias, _) => panic!("BER/DER alias must be used with tuple strucs"),
(_, Fields::Unnamed(_)) => panic!("BER/DER sequence cannot be used on tuple structs"),
_ => (),
}
let fields = ds.fields.iter().map(FieldInfo::from).collect();
// get lifetimes from generics
let lfts: Vec<_> = ast.generics.lifetimes().collect();
let mut where_predicates = Vec::new();
if !lfts.is_empty() {
// input slice must outlive all lifetimes from Self
let lft = Lifetime::new("'ber", Span::call_site());
let wh: WherePredicate = parse_quote! { #lft: #(#lfts)+* };
where_predicates.push(wh);
};
// get custom attributes on container
let error = ast
.attrs
.iter()
.find(|attr| {
attr.meta
.path()
.is_ident(&Ident::new("error", Span::call_site()))
})
.cloned();
Container {
container_type,
fields,
where_predicates,
error,
is_any,
}
}
pub fn gen_tryfrom(&self) -> TokenStream {
let field_names = &self.fields.iter().map(|f| &f.name).collect::<Vec<_>>();
let parse_content =
derive_ber_sequence_content(&self.fields, Asn1Type::Ber, self.error.is_some());
let lifetime = Lifetime::new("'ber", Span::call_site());
let wh = &self.where_predicates;
let error = if let Some(attr) = &self.error {
get_attribute_meta(attr).expect("Invalid error attribute format")
} else {
quote! { asn1_rs::Error }
};
let fn_content = if self.container_type == ContainerType::Alias {
// special case: is this an alias for Any
if self.is_any {
quote! { Ok(Self(any)) }
} else {
quote! {
let res = TryFrom::try_from(any)?;
Ok(Self(res))
}
}
} else {
quote! {
use asn1_rs::nom::*;
any.tag().assert_eq(Self::TAG)?;
// no need to parse sequence, we already have content
let i = any.data;
//
#parse_content
//
let _ = i; // XXX check if empty?
Ok(Self{#(#field_names),*})
}
};
// note: `gen impl` in synstructure takes care of appending extra where clauses if any, and removing
// the `where` statement if there are none.
quote! {
use asn1_rs::{Any, FromBer};
use core::convert::TryFrom;
gen impl<#lifetime> TryFrom<Any<#lifetime>> for @Self where #(#wh)+* {
type Error = #error;
fn try_from(any: Any<#lifetime>) -> asn1_rs::Result<Self, #error> {
#fn_content
}
}
}
}
pub fn gen_tagged(&self) -> TokenStream {
let tag = if self.container_type == ContainerType::Alias {
// special case: is this an alias for Any
if self.is_any {
return quote! {};
}
// find type of sub-item
let ty = &self.fields[0].type_;
quote! { <#ty as asn1_rs::Tagged>::TAG }
} else {
let container_type = self.container_type;
quote! { #container_type }
};
quote! {
gen impl<'ber> asn1_rs::Tagged for @Self {
const TAG: asn1_rs::Tag = #tag;
}
}
}
pub fn gen_checkconstraints(&self) -> TokenStream {
let lifetime = Lifetime::new("'ber", Span::call_site());
let wh = &self.where_predicates;
// let parse_content = derive_ber_sequence_content(&field_names, Asn1Type::Der);
let fn_content = if self.container_type == ContainerType::Alias {
// special case: is this an alias for Any
if self.is_any {
return quote! {};
}
let ty = &self.fields[0].type_;
quote! {
any.tag().assert_eq(Self::TAG)?;
<#ty>::check_constraints(any)
}
} else {
let check_fields: Vec<_> = self
.fields
.iter()
.map(|field| {
let ty = &field.type_;
quote! {
let (rem, any) = Any::from_der(rem)?;
<#ty as CheckDerConstraints>::check_constraints(&any)?;
}
})
.collect();
quote! {
any.tag().assert_eq(Self::TAG)?;
let rem = &any.data;
#(#check_fields)*
Ok(())
}
};
// note: `gen impl` in synstructure takes care of appending extra where clauses if any, and removing
// the `where` statement if there are none.
quote! {
use asn1_rs::{CheckDerConstraints, Tagged};
gen impl<#lifetime> CheckDerConstraints for @Self where #(#wh)+* {
fn check_constraints(any: &Any) -> asn1_rs::Result<()> {
#fn_content
}
}
}
}
pub fn gen_fromder(&self) -> TokenStream {
let lifetime = Lifetime::new("'ber", Span::call_site());
let wh = &self.where_predicates;
let field_names = &self.fields.iter().map(|f| &f.name).collect::<Vec<_>>();
let parse_content =
derive_ber_sequence_content(&self.fields, Asn1Type::Der, self.error.is_some());
let error = if let Some(attr) = &self.error {
get_attribute_meta(attr).expect("Invalid error attribute format")
} else {
quote! { asn1_rs::Error }
};
let fn_content = if self.container_type == ContainerType::Alias {
// special case: is this an alias for Any
if self.is_any {
quote! {
let (rem, any) = asn1_rs::Any::from_der(bytes).map_err(asn1_rs::nom::Err::convert)?;
Ok((rem,Self(any)))
}
} else {
quote! {
let (rem, any) = asn1_rs::Any::from_der(bytes).map_err(asn1_rs::nom::Err::convert)?;
any.header.assert_tag(Self::TAG).map_err(|e| asn1_rs::nom::Err::Error(e.into()))?;
let res = TryFrom::try_from(any)?;
Ok((rem,Self(res)))
}
}
} else {
quote! {
let (rem, any) = asn1_rs::Any::from_der(bytes).map_err(asn1_rs::nom::Err::convert)?;
any.header.assert_tag(Self::TAG).map_err(|e| asn1_rs::nom::Err::Error(e.into()))?;
let i = any.data;
//
#parse_content
//
// let _ = i; // XXX check if empty?
Ok((rem,Self{#(#field_names),*}))
}
};
// note: `gen impl` in synstructure takes care of appending extra where clauses if any, and removing
// the `where` statement if there are none.
quote! {
use asn1_rs::FromDer;
gen impl<#lifetime> asn1_rs::FromDer<#lifetime, #error> for @Self where #(#wh)+* {
fn from_der(bytes: &#lifetime [u8]) -> asn1_rs::ParseResult<#lifetime, Self, #error> {
#fn_content
}
}
}
}
pub fn gen_to_der_len(&self) -> TokenStream {
let field_names = &self.fields.iter().map(|f| &f.name).collect::<Vec<_>>();
let add_len_instructions = field_names.iter().fold(Vec::new(), |mut instrs, field| {
instrs.push(quote! {total_len += self.#field.to_der_len()?;});
instrs
});
quote! {
fn to_der_len(&self) -> asn1_rs::Result<usize> {
let mut total_len = 0;
#(#add_len_instructions)*
// now add header length computation
if total_len < 127 {
// 1 (class+tag) + 1 (length) + len
Ok(2 + total_len)
} else {
// 1 (class+tag) + n (length) + len
let n = asn1_rs::Length::Definite(total_len).to_der_len()?;
Ok(1 + n + total_len)
}
}
}
}
pub fn gen_write_der_header(&self) -> TokenStream {
quote! {
fn write_der_header(&self, writer: &mut dyn std::io::Write) -> asn1_rs::SerializeResult<usize> {
let mut empty = std::io::empty();
let num_bytes = self.write_der_content(&mut empty)?;
let header = asn1_rs::Header::new(
asn1_rs::Class::Universal,
true,
asn1_rs::Sequence::TAG,
asn1_rs::Length::Definite(num_bytes),
);
header.write_der_header(writer).map_err(Into::into)
}
}
}
pub fn gen_write_der_content(&self) -> TokenStream {
let field_names = &self.fields.iter().map(|f| &f.name).collect::<Vec<_>>();
let write_instructions = field_names.iter().fold(Vec::new(), |mut instrs, field| {
instrs.push(quote! {num_bytes += self.#field.write_der_header(writer)?;});
instrs.push(quote! {num_bytes += self.#field.write_der_content(writer)?;});
instrs
});
quote! {
fn write_der_content(&self, writer: &mut dyn std::io::Write) -> asn1_rs::SerializeResult<usize> {
let mut num_bytes = 0;
#(#write_instructions)*
Ok(num_bytes)
}
}
}
}
#[derive(Debug)]
pub struct FieldInfo {
pub name: Ident,
pub type_: Type,
pub default: Option<TokenStream>,
pub optional: bool,
pub tag: Option<(Asn1TagKind, Asn1TagClass, u16)>,
pub map_err: Option<TokenStream>,
}
impl From<&Field> for FieldInfo {
fn from(field: &Field) -> Self {
// parse attributes and keep supported ones
let mut optional = false;
let mut tag = None;
let mut map_err = None;
let mut default = None;
let name = field
.ident
.as_ref()
.map_or_else(|| Ident::new("_", Span::call_site()), |s| s.clone());
for attr in &field.attrs {
let ident = match attr.meta.path().get_ident() {
Some(ident) => ident.to_string(),
None => continue,
};
match ident.as_str() {
"map_err" => {
let expr: syn::Expr = attr.parse_args().expect("could not parse map_err");
map_err = Some(quote! { #expr });
}
"default" => {
let expr: syn::Expr = attr.parse_args().expect("could not parse default");
default = Some(quote! { #expr });
optional = true;
}
"optional" => optional = true,
"tag_explicit" => {
if tag.is_some() {
panic!("tag cannot be set twice!");
}
let (class, value) = attr.parse_args_with(parse_tag_args).unwrap();
tag = Some((Asn1TagKind::Explicit, class, value));
}
"tag_implicit" => {
if tag.is_some() {
panic!("tag cannot be set twice!");
}
let (class, value) = attr.parse_args_with(parse_tag_args).unwrap();
tag = Some((Asn1TagKind::Implicit, class, value));
}
// ignore unknown attributes
_ => (),
}
}
FieldInfo {
name,
type_: field.ty.clone(),
default,
optional,
tag,
map_err,
}
}
}
fn parse_tag_args(stream: ParseStream) -> Result<(Asn1TagClass, u16), syn::Error> {
let tag_class: Option<Ident> = stream.parse()?;
let tag_class = if let Some(ident) = tag_class {
let s = ident.to_string().to_uppercase();
match s.as_str() {
"UNIVERSAL" => Asn1TagClass::Universal,
"CONTEXT-SPECIFIC" => Asn1TagClass::ContextSpecific,
"APPLICATION" => Asn1TagClass::Application,
"PRIVATE" => Asn1TagClass::Private,
_ => {
return Err(syn::Error::new(stream.span(), "Invalid tag class"));
}
}
} else {
Asn1TagClass::ContextSpecific
};
let lit: LitInt = stream.parse()?;
let value = lit.base10_parse::<u16>()?;
Ok((tag_class, value))
}
fn derive_ber_sequence_content(
fields: &[FieldInfo],
asn1_type: Asn1Type,
custom_errors: bool,
) -> TokenStream {
let field_parsers: Vec<_> = fields
.iter()
.map(|f| get_field_parser(f, asn1_type, custom_errors))
.collect();
quote! {
#(#field_parsers)*
}
}
fn get_field_parser(f: &FieldInfo, asn1_type: Asn1Type, custom_errors: bool) -> TokenStream {
let from = match asn1_type {
Asn1Type::Ber => quote! {FromBer::from_ber},
Asn1Type::Der => quote! {FromDer::from_der},
};
let name = &f.name;
let default = f
.default
.as_ref()
// use a type hint, otherwise compiler will not know what type provides .unwrap_or
.map(|x| quote! {let #name: Option<_> = #name; let #name = #name.unwrap_or(#x);});
let map_err = if let Some(tt) = f.map_err.as_ref() {
if asn1_type == Asn1Type::Ber {
Some(quote! {
.map_err(|err| err.map(#tt))
.map_err(asn1_rs::from_nom_error::<_, Self::Error>)
})
} else {
// Some(quote! { .map_err(|err| nom::Err::convert(#tt)) })
Some(quote! { .map_err(|err| err.map(#tt)) })
}
} else {
// add mapping functions only if custom errors are used
if custom_errors {
if asn1_type == Asn1Type::Ber {
Some(quote! { .map_err(asn1_rs::from_nom_error::<_, Self::Error>) })
} else {
Some(quote! { .map_err(nom::Err::convert) })
}
} else {
None
}
};
if let Some((tag_kind, class, n)) = f.tag {
let tag = Literal::u16_unsuffixed(n);
// test if tagged + optional
if f.optional {
return quote! {
let (i, #name) = {
if i.is_empty() {
(i, None)
} else {
let (_, header): (_, asn1_rs::Header) = #from(i)#map_err?;
if header.tag().0 == #tag {
let (i, t): (_, asn1_rs::TaggedValue::<_, _, #tag_kind, {#class}, #tag>) = #from(i)#map_err?;
(i, Some(t.into_inner()))
} else {
(i, None)
}
}
};
#default
};
} else {
// tagged, but not OPTIONAL
return quote! {
let (i, #name) = {
let (i, t): (_, asn1_rs::TaggedValue::<_, _, #tag_kind, {#class}, #tag>) = #from(i)#map_err?;
(i, t.into_inner())
};
#default
};
}
} else {
// neither tagged nor optional
quote! {
let (i, #name) = #from(i)#map_err?;
#default
}
}
}
fn get_attribute_meta(attr: &Attribute) -> Result<TokenStream, syn::Error> {
if let Meta::List(meta) = &attr.meta {
let content = &meta.tokens;
Ok(quote! { #content })
} else {
Err(syn::Error::new(
attr.span(),
"Invalid error attribute format",
))
}
}

74
vendor/asn1-rs-derive/src/lib.rs vendored Normal file
View File

@@ -0,0 +1,74 @@
mod alias;
mod container;
mod sequence;
mod set;
mod tostatic;
use alias::*;
use sequence::*;
use set::*;
use tostatic::derive_tostatic;
synstructure::decl_derive!([BerAlias, attributes(
debug_derive,
default,
optional,
tag_explicit,
tag_implicit,
error,
map_err
)] => derive_ber_alias);
synstructure::decl_derive!([DerAlias, attributes(
debug_derive,
default,
optional,
tag_explicit,
tag_implicit,
error,
map_err
)] => derive_der_alias);
synstructure::decl_derive!([BerSequence, attributes(
debug_derive,
default,
optional,
tag_explicit,
tag_implicit,
error,
map_err
)] => derive_ber_sequence);
synstructure::decl_derive!([DerSequence, attributes(
debug_derive,
default,
optional,
tag_explicit,
tag_implicit,
error,
map_err
)] => derive_der_sequence);
synstructure::decl_derive!([BerSet, attributes(
debug_derive,
default,
optional,
tag_explicit,
tag_implicit,
error,
map_err
)] => derive_ber_set);
synstructure::decl_derive!([DerSet, attributes(
debug_derive,
default,
optional,
tag_explicit,
tag_implicit,
error,
map_err
)] => derive_der_set);
synstructure::decl_derive!([ToStatic, attributes(
debug_derive
)] => derive_tostatic);
synstructure::decl_derive!([ToDerSequence, attributes(
debug_derive,
)] => derive_toder_sequence);

107
vendor/asn1-rs-derive/src/sequence.rs vendored Normal file
View File

@@ -0,0 +1,107 @@
use crate::container::*;
use proc_macro2::Span;
use quote::quote;
use syn::{Data, Ident, WherePredicate};
pub fn derive_ber_sequence(s: synstructure::Structure) -> proc_macro2::TokenStream {
let ast = s.ast();
let container = match &ast.data {
Data::Struct(ds) => Container::from_datastruct(ds, ast, ContainerType::Sequence),
_ => panic!("Unsupported type, cannot derive"),
};
let debug_derive = ast.attrs.iter().any(|attr| {
attr.meta
.path()
.is_ident(&Ident::new("debug_derive", Span::call_site()))
});
let impl_tryfrom = container.gen_tryfrom();
let impl_tagged = container.gen_tagged();
let ts = s.gen_impl(quote! {
extern crate asn1_rs;
#impl_tryfrom
#impl_tagged
});
if debug_derive {
eprintln!("{}", ts);
}
ts
}
pub fn derive_der_sequence(s: synstructure::Structure) -> proc_macro2::TokenStream {
let ast = s.ast();
let container = match &ast.data {
Data::Struct(ds) => Container::from_datastruct(ds, ast, ContainerType::Sequence),
_ => panic!("Unsupported type, cannot derive"),
};
let debug_derive = ast.attrs.iter().any(|attr| {
attr.meta
.path()
.is_ident(&Ident::new("debug_derive", Span::call_site()))
});
let impl_tryfrom = container.gen_tryfrom();
let impl_tagged = container.gen_tagged();
let impl_checkconstraints = container.gen_checkconstraints();
let impl_fromder = container.gen_fromder();
let ts = s.gen_impl(quote! {
extern crate asn1_rs;
#impl_tryfrom
#impl_tagged
#impl_checkconstraints
#impl_fromder
});
if debug_derive {
eprintln!("{}", ts);
}
ts
}
pub fn derive_toder_sequence(s: synstructure::Structure) -> proc_macro2::TokenStream {
let ast = s.ast();
let container = match &ast.data {
Data::Struct(ds) => Container::from_datastruct(ds, ast, ContainerType::Sequence),
_ => panic!("Unsupported type, cannot derive"),
};
let debug_derive = ast.attrs.iter().any(|attr| {
attr.meta
.path()
.is_ident(&Ident::new("debug_derive", Span::call_site()))
});
//let lifetime = Lifetime::new("'ber", Span::call_site());
let wh = &container.where_predicates;
// we must filter out the 'ber lifetime (added for parsers, but not used here)
let wh = wh.iter().filter(|predicate| match predicate {
WherePredicate::Lifetime(lft) => lft.lifetime.ident != "ber",
_ => true,
});
let impl_to_der_len = container.gen_to_der_len();
let impl_write_der_header = container.gen_write_der_header();
let impl_write_der_content = container.gen_write_der_content();
// note: `gen impl` in synstructure takes care of appending extra where clauses if any, and removing
// the `where` statement if there are none.
let ts = s.gen_impl(quote! {
extern crate asn1_rs;
#[cfg(feature = "std")]
gen impl asn1_rs::ToDer for @Self where #(#wh)+* {
#impl_to_der_len
#impl_write_der_header
#impl_write_der_content
}
});
if debug_derive {
eprintln!("{}", ts);
}
ts
}

63
vendor/asn1-rs-derive/src/set.rs vendored Normal file
View File

@@ -0,0 +1,63 @@
use crate::container::*;
use proc_macro2::Span;
use quote::quote;
use syn::{Data, Ident};
pub fn derive_ber_set(s: synstructure::Structure) -> proc_macro2::TokenStream {
let ast = s.ast();
let container = match &ast.data {
Data::Struct(ds) => Container::from_datastruct(ds, ast, ContainerType::Set),
_ => panic!("Unsupported type, cannot derive"),
};
let debug_derive = ast.attrs.iter().any(|attr| {
attr.meta
.path()
.is_ident(&Ident::new("debug_derive", Span::call_site()))
});
let impl_tryfrom = container.gen_tryfrom();
let impl_tagged = container.gen_tagged();
let ts = s.gen_impl(quote! {
extern crate asn1_rs;
#impl_tryfrom
#impl_tagged
});
if debug_derive {
eprintln!("{}", ts);
}
ts
}
pub fn derive_der_set(s: synstructure::Structure) -> proc_macro2::TokenStream {
let ast = s.ast();
let container = match &ast.data {
Data::Struct(ds) => Container::from_datastruct(ds, ast, ContainerType::Set),
_ => panic!("Unsupported type, cannot derive"),
};
let debug_derive = ast.attrs.iter().any(|attr| {
attr.meta
.path()
.is_ident(&Ident::new("debug_derive", Span::call_site()))
});
let impl_tryfrom = container.gen_tryfrom();
let impl_tagged = container.gen_tagged();
let impl_checkconstraints = container.gen_checkconstraints();
let impl_fromder = container.gen_fromder();
let ts = s.gen_impl(quote! {
extern crate asn1_rs;
#impl_tryfrom
#impl_tagged
#impl_checkconstraints
#impl_fromder
});
if debug_derive {
eprintln!("{}", ts);
}
ts
}

65
vendor/asn1-rs-derive/src/tostatic.rs vendored Normal file
View File

@@ -0,0 +1,65 @@
use proc_macro2::Span;
use quote::quote;
use syn::{Ident, Lifetime};
pub fn derive_tostatic(s: synstructure::Structure) -> proc_macro2::TokenStream {
let ast = s.ast();
let debug_derive = ast.attrs.iter().any(|attr| {
attr.meta
.path()
.is_ident(&Ident::new("debug_derive", Span::call_site()))
});
// if deriving a struct, there will be only one variant
// for enums, this will iterate on each variant
let body = s.each_variant(|vi| {
// bindings can be empty for unit variants
let instrs = vi
.bindings()
.iter()
.enumerate()
.fold(quote! {}, |acc, (idx, bi)| {
let ident = Ident::new(&format!("_{idx}"), Span::call_site());
quote! { #acc let #ident = #bi.to_static(); }
});
// use construct() to handle possible cases (unit/named/unnamed)
let c = vi.construct(|_f, i| {
let ident = Ident::new(&format!("_{i}"), Span::call_site());
quote! { #ident }
});
quote! { #instrs #c }
});
let struct_ident = &ast.ident;
// check if struct has lifetimes
let static_token = match ast.generics.lifetimes().count() {
0 => None,
1 => {
let lt = Lifetime::new("'static", Span::call_site());
Some(quote! {<#lt>})
}
_ => {
let lt_static = Lifetime::new("'static", Span::call_site());
let lts = ast.generics.lifetimes().map(|_| lt_static.clone());
Some(quote! {<#(#lts),*>})
}
};
let ts = s.gen_impl(quote! {
gen impl asn1_rs::ToStatic for @Self {
type Owned = #struct_ident #static_token;
fn to_static(&self) -> Self::Owned {
match *self {
#body
}
}
}
});
if debug_derive {
eprintln!("TS: {ts}");
}
ts
}