summaryrefslogtreecommitdiffstats
path: root/rust/vendor/asn1-rs-impl
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 17:39:49 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 17:39:49 +0000
commita0aa2307322cd47bbf416810ac0292925e03be87 (patch)
tree37076262a026c4b48c8a0e84f44ff9187556ca35 /rust/vendor/asn1-rs-impl
parentInitial commit. (diff)
downloadsuricata-a0aa2307322cd47bbf416810ac0292925e03be87.tar.xz
suricata-a0aa2307322cd47bbf416810ac0292925e03be87.zip
Adding upstream version 1:7.0.3.upstream/1%7.0.3
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'rust/vendor/asn1-rs-impl')
-rw-r--r--rust/vendor/asn1-rs-impl/.cargo-checksum.json1
-rw-r--r--rust/vendor/asn1-rs-impl/Cargo.toml31
-rw-r--r--rust/vendor/asn1-rs-impl/src/lib.rs126
-rw-r--r--rust/vendor/asn1-rs-impl/tests/test_oid.rs15
4 files changed, 173 insertions, 0 deletions
diff --git a/rust/vendor/asn1-rs-impl/.cargo-checksum.json b/rust/vendor/asn1-rs-impl/.cargo-checksum.json
new file mode 100644
index 0000000..4a255f4
--- /dev/null
+++ b/rust/vendor/asn1-rs-impl/.cargo-checksum.json
@@ -0,0 +1 @@
+{"files":{"Cargo.toml":"1fda40b79fae27b66071324aeffa2deba87fd7bea40f36afb013bf90af50790d","src/lib.rs":"c052170f4c8f719ef07a722a1961c0e52423e28951ddbdf3700d7329bb79a277","tests/test_oid.rs":"0a4ed684b2c503315955b28d0587cf27ee7ed07e860f598960a99ee0415684d8"},"package":"2777730b2039ac0f95f093556e61b6d26cebed5393ca6f152717777cec3a42ed"} \ No newline at end of file
diff --git a/rust/vendor/asn1-rs-impl/Cargo.toml b/rust/vendor/asn1-rs-impl/Cargo.toml
new file mode 100644
index 0000000..261b72f
--- /dev/null
+++ b/rust/vendor/asn1-rs-impl/Cargo.toml
@@ -0,0 +1,31 @@
+# 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-impl"
+version = "0.1.0"
+authors = ["Pierre Chifflier <chifflier@wzdftpd.net>"]
+description = "Implementation details for the `asn1-rs` crate"
+homepage = "https://github.com/rusticata/asn1-rs"
+license = "MIT/Apache-2.0"
+repository = "https://github.com/rusticata/asn1-rs.git"
+
+[lib]
+proc-macro = true
+[dependencies.proc-macro2]
+version = "1"
+
+[dependencies.quote]
+version = "1"
+
+[dependencies.syn]
+version = "1"
diff --git a/rust/vendor/asn1-rs-impl/src/lib.rs b/rust/vendor/asn1-rs-impl/src/lib.rs
new file mode 100644
index 0000000..ea635cc
--- /dev/null
+++ b/rust/vendor/asn1-rs-impl/src/lib.rs
@@ -0,0 +1,126 @@
+extern crate proc_macro;
+use proc_macro::{Span, TokenStream};
+use syn::{parse_macro_input, Error, LitInt};
+
+#[proc_macro]
+pub fn encode_oid(input: TokenStream) -> TokenStream {
+ let token_stream = input.to_string();
+ let (s, relative) = if token_stream.starts_with("rel ") {
+ (&token_stream[4..], true)
+ } else {
+ (token_stream.as_ref(), false)
+ };
+ let items: Result<Vec<_>, _> = s.split('.').map(|x| x.trim().parse::<u128>()).collect();
+ let mut items: &[_] = match items.as_ref() {
+ Ok(v) => v.as_ref(),
+ Err(_) => return create_error("Could not parse OID"),
+ };
+ let mut v = Vec::new();
+ if !relative {
+ if items.len() < 2 {
+ if items.len() == 1 && items[0] == 0 {
+ return "[0]".parse().unwrap();
+ }
+ return create_error("Need at least two components for non-relative oid");
+ }
+ if items[0] > 2 || items[1] > 39 {
+ return create_error("First components are too big");
+ }
+ let first_byte = (items[0] * 40 + items[1]) as u8;
+ v.push(first_byte);
+ items = &items[2..];
+ }
+ for &int in items {
+ let enc = encode_base128(int);
+ v.extend_from_slice(&enc);
+ }
+ // "fn answer() -> u32 { 42 }".parse().unwrap()
+ let mut s = String::with_capacity(2 + 6 * v.len());
+ s.push('[');
+ for byte in v.iter() {
+ s.insert_str(s.len(), &format!("0x{:02x}, ", byte));
+ }
+ s.push(']');
+ s.parse().unwrap()
+}
+
+#[inline]
+fn create_error(msg: &str) -> TokenStream {
+ let s = format!("Invalid OID({})", msg);
+ Error::new(Span::call_site().into(), &s)
+ .to_compile_error()
+ .into()
+}
+
+// encode int as base128
+fn encode_base128(int: u128) -> Vec<u8> {
+ let mut val = int;
+ let mut base128 = Vec::new();
+ let lo = val & 0x7f;
+ base128.push(lo as u8);
+ val >>= 7;
+ loop {
+ if val == 0 {
+ base128.reverse();
+ return base128;
+ }
+ let lo = val & 0x7f;
+ base128.push(lo as u8 | 0x80);
+ val >>= 7;
+ }
+}
+
+#[proc_macro]
+pub fn encode_int(input: TokenStream) -> TokenStream {
+ let lit = parse_macro_input!(input as LitInt);
+
+ match impl_encode_int(lit) {
+ Ok(ts) => ts,
+ Err(e) => e.to_compile_error().into(),
+ }
+
+ // let token_stream = input.to_string();
+ // let items: Result<Vec<_>, _> = token_stream
+ // .split('.')
+ // .map(|x| x.trim().parse::<u64>())
+ // .collect();
+ // let err = Error::new(Span::call_site().into(), "invalid OID");
+ // if let Ok(items) = items {
+ // let mut v = Vec::new();
+ // if items.len() < 2 || items[0] > 2 || items[1] > 39 {
+ // return err.to_compile_error().into();
+ // }
+ // let first_byte = (items[0] * 40 + items[1]) as u8;
+ // v.push(first_byte);
+ // for &int in &items[2..] {
+ // let enc = encode_base128(int);
+ // v.extend_from_slice(&enc);
+ // }
+ // // "fn answer() -> u32 { 42 }".parse().unwrap()
+ // let mut s = String::with_capacity(2 + 6 * v.len());
+ // s.push('[');
+ // for byte in v.iter() {
+ // s.insert_str(s.len(), &format!("0x{:02x}, ", byte));
+ // }
+ // s.push(']');
+ // s.parse().unwrap()
+ // } else {
+ // eprintln!("could not parse OID '{}'", token_stream);
+ // err.to_compile_error().into()
+ // }
+}
+
+fn impl_encode_int(lit: LitInt) -> Result<TokenStream, Error> {
+ let value = lit.base10_parse::<u64>()?;
+
+ let bytes = value.to_be_bytes();
+ let v: Vec<_> = bytes.iter().skip_while(|&c| *c == 0).collect();
+
+ let mut s = String::with_capacity(2 + 6 * v.len());
+ s.push('[');
+ for byte in v.iter() {
+ s.insert_str(s.len(), &format!("0x{:02x}, ", byte));
+ }
+ s.push(']');
+ Ok(s.parse().unwrap())
+}
diff --git a/rust/vendor/asn1-rs-impl/tests/test_oid.rs b/rust/vendor/asn1-rs-impl/tests/test_oid.rs
new file mode 100644
index 0000000..b85d622
--- /dev/null
+++ b/rust/vendor/asn1-rs-impl/tests/test_oid.rs
@@ -0,0 +1,15 @@
+use asn1_rs_impl::{encode_int, encode_oid};
+
+#[test]
+fn test_encode_oid() {
+ // example from http://luca.ntop.org/Teaching/Appunti/asn1.html
+ let oid = encode_oid! {1.2.840.113549};
+ assert_eq!(oid, [0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d]);
+}
+
+#[test]
+fn test_encode_int() {
+ //
+ let int = encode_int!(1234);
+ assert_eq!(int, [0x04, 0xd2]);
+}