1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//! Synta: ASN.1 parser, decoder, and encoder library
//!
//! This library provides ASN.1 (Abstract Syntax Notation One) parsing, decoding,
//! and encoding capabilities with support for DER (Distinguished Encoding Rules)
//! and BER (Basic Encoding Rules).
//!
//! # Features
//! - Safe and ergonomic API leveraging Rust's type system
//! - Zero-copy parsing where possible
//! - DER, BER, and CER encoding support
//! - Derive macros for automatic trait implementations (with `derive` feature)
//! - C FFI compatibility (with `ffi` feature)
//! - `no_std` support (with `alloc`)
//! - Optional serde `Serialize`/`Deserialize` support (with `serde` feature)
//!
//! # Cargo features
//!
//! | Feature | Description |
//! |-----------|-------------|
//! | `std` | Enable standard library support (enabled by default). Without this, `alloc` is used. |
//! | `derive` | Enable derive macros (`Asn1Sequence`, `Asn1Set`, `Asn1Choice`). |
//! | `serde` | Enable `serde::Serialize`/`Deserialize` for all ASN.1 types. |
//! | `unchecked` | **Unsafe optimization**: skip overflow and bounds checks in tag and length decoding. Only enable for fully trusted, pre-validated input. Malformed data may cause incorrect behaviour. |
//!
//! # ASN.1 types
//!
//! All standard ASN.1 types are re-exported at the crate root for convenient
//! import. Two families of string and binary types are available:
//!
//! ## Owned types (heap-allocating)
//!
//! Suitable for constructing ASN.1 structures programmatically or when the
//! decoded value must outlive the input buffer.
//!
//! | Rust type | ASN.1 type |
//! |-------------------|-------------------|
//! | [`OctetString`] | `OCTET STRING` |
//! | [`BitString`] | `BIT STRING` |
//! | [`Utf8String`] | `UTF8String` |
//! | [`PrintableString`] | `PrintableString` |
//! | [`IA5String`] | `IA5String` |
//! | [`Integer`] | `INTEGER` |
//! | [`Boolean`] | `BOOLEAN` |
//! | [`ObjectIdentifier`] | `OBJECT IDENTIFIER` |
//!
//! ## Zero-copy borrowed types
//!
//! These types borrow directly from the decoder's input buffer, avoiding
//! allocation. Use them for parse-only workloads where the values do not
//! need to outlive the encoded bytes.
//!
//! | Rust type | ASN.1 type |
//! |----------------------|-------------------|
//! | [`OctetStringRef`] | `OCTET STRING` |
//! | [`BitStringRef`] | `BIT STRING` |
//! | [`Utf8StringRef`] | `UTF8String` |
//! | [`PrintableStringRef`] | `PrintableString` |
//! | [`IA5StringRef`] | `IA5String` |
//!
//! # Example
//! ```
//! use synta::{Decoder, Encoding, Integer};
//!
//! // Decode an integer
//! let data = &[0x02, 0x01, 0x2A]; // INTEGER 42
//! let mut decoder = Decoder::new(data, Encoding::Der);
//! let value: Integer = decoder.decode().unwrap();
//! assert_eq!(value.as_i64().unwrap(), 42);
//! ```
//!
//! # Convenience traits: `ToDer` and `FromDer`
//!
//! [`ToDer`] and [`FromDer`] are blanket-implemented on all types that
//! implement [`Encode`] / [`Decode`]. They let you encode or decode a
//! single value without constructing an [`Encoder`] or [`Decoder`] manually:
//!
//! ```
//! use synta::{Integer, ToDer, FromDer};
//!
//! let n = Integer::from_i64(42);
//! let der = n.to_der().unwrap();
//! let n2 = Integer::from_der(&der).unwrap();
//! assert_eq!(n, n2);
//! ```
//!
//! Zero-copy borrowed types (e.g. `OctetStringRef<'a>`) cannot be decoded
//! via [`FromDer`] because they carry a lifetime tied to the input buffer;
//! use [`Decoder`] directly for those cases.
//!
//! # Serde support
//!
//! Enable the `serde` feature to serialize and deserialize all ASN.1 types
//! with any serde-compatible format (JSON, CBOR, etc.):
//!
//! ```toml
//! [dependencies]
//! synta = { version = "0.1", features = ["serde"] }
//! ```
//!
//! ```ignore
//! use synta::{Integer, ObjectIdentifier};
//!
//! let i = Integer::from_i64(255);
//! let json = serde_json::to_string(&i).unwrap(); // "\"ff\""
//!
//! let oid = ObjectIdentifier::new(&[1, 2, 840, 113549]).unwrap();
//! let json = serde_json::to_string(&oid).unwrap(); // "\"1.2.840.113549\""
//! ```
extern crate alloc;
// Internal serde helpers (only compiled with the `serde` feature)
pub
// Module declarations
// Re-export commonly used types
pub use DecoderConfig;
pub use ;
pub use ;
pub use ;
pub use Length;
pub use ;
/// ASN.1 encoding rules
///
/// Specifies which encoding rules to use when encoding or decoding ASN.1 data.
/// Different encoding rules have different levels of strictness and features.
// Re-export core decoder/encoder from DER module (they work for all encodings)
pub use Decoder;
pub use Encoder;
// Re-export primitive types
pub use ObjectIdentifier;
pub use ;
// Owned heap-allocating string and binary types
pub use ;
// Zero-copy borrowed string and binary types (borrow from the decoder input buffer)
pub use ;
pub use ;
// Re-export constructed types
pub use ;
// Explicit Vec-backed aliases (same as SequenceOf/SetOf but clearer names)
pub use ;
// Re-export tagged types
pub use ;
// Re-export zero-copy raw TLV capture
pub use RawDer;
// Re-export derive macros (when derive feature is enabled)
pub use ;
// Re-export traits
pub use ;
pub use Decode;
pub use DecodeImplicit;
pub use Encode;
pub use TagForOptional;
pub use Tagged;
// ---- serde support for the Encoding enum ----