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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
OxiRush
Copyright 2025-2026 Valentin D'Emmanuele
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.
*/
//! Aligned PER (ITU-T X.691, the ALIGNED variant of BASIC-PER) for generated bindings
//! of 3GPP-style ASN.1: a reader, a writer, and the types of the values.
//!
//! ```rust
//! use oxirush_aper::{Reader, Writer};
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let mut octets = Vec::new();
//! let mut writer = Writer::new(&mut octets);
//! // INTEGER (0..7): three bits
//! writer.unsigned(5, 0, 7)?;
//! // INTEGER (0..255, ...): the extension bit, then one octet, aligned
//! writer.unsigned_extensible(200, 0, 255)?;
//! // ENUMERATED with 6 root values and an extension marker: the fourth
//! writer.index(3, 6, true);
//! // SIZE (1..256) of a SEQUENCE OF: its count, then its items
//! writer.size(2, 1, 256)?;
//! writer.unsigned(1, 0, 1)?;
//! writer.unsigned(0, 0, 1)?;
//! println!("{octets:02x?}");
//!
//! let mut reader = Reader::new(&octets);
//! println!("{}", reader.unsigned(0, 7)?);
//! println!("{}", reader.unsigned_extensible(0, 255)?);
//! println!("{}", reader.enumerated(6, 0, true)?);
//! println!("{} items: {} {}", reader.size(1, 256)?, reader.bit()?, reader.bit()?);
//!
//! println!("{}", Writer::new(&mut Vec::new()).unsigned(8, 0, 7).unwrap_err());
//! Ok(())
//! }
//! ```
//!
//! ```text
//! [a0, c8, 30, 01, 80]
//! 5
//! 200
//! 3
//! 2 items: true false
//! the value does not encode: 8 is outside its constraint (0..7)
//! ```
//!
//! # Tasks
//!
//! The [README](https://github.com/linouxis9/oxirush-aper#readme) has an example of
//! each, with what it prints.
//!
//! | Task | Items |
//! |------|-------|
//! | Write the codec of a type | [`AsnType`], [`Decode`], [`Encode`], [`decode`], [`encode`], [`encode_into`] |
//! | Read and write the fields | [`Reader`], [`Writer`], [`UNBOUNDED`] |
//! | Bit strings | [`Bits`], [`BitString`] |
//! | An open type | [`Reader::open_type`], [`Writer::begin_open_type`], [`Writer::end_open_type`], [`Octets`] |
//! | Extension additions | [`Reader::additions`], [`Additions`], [`Writer::additions`], [`Reader::skip_additions`] |
//! | What the reader accepts | [`decode_with_faults`], [`decode_strict`], [`Fault`], [`Reader::noting`] |
//! | Check a PDU against the object set of its message | [`check`] |
//! | The macros and the readers of a protocol crate | [`protocol`] |
//!
//! A [`Reader`] and a [`Writer`] have a method for each field of X.691 that the two
//! protocols of the family use. They work on octets where the encoding is aligned and
//! on bits where it is not, the bounds of a constraint are their arguments, and a
//! reader allocates nothing but the lists and the strings in fragments of what it
//! reads.
//!
//! The values are native numbers, arrays for an OCTET STRING of one size, [`Octets`]
//! for the others and for an open type, [`Bits`] for a BIT STRING of 64 bits at most,
//! [`BitString`] for a longer one, `Cow<str>` for a character string and
//! [`ObjectIdentifier`].
//!
//! # The OxiRush family
//!
//! - [oxirush-utils](https://docs.rs/oxirush-utils): the 3GPP identities, and the JSON tree with its paths that the others share.
//! - [oxirush-aper](https://docs.rs/oxirush-aper): aligned PER, the runtime of the NGAP and S1AP codecs.
//! - [oxirush-nas](https://docs.rs/oxirush-nas): the NAS messages of 5GS and EPS.
//! - [oxirush-security](https://docs.rs/oxirush-security): NAS security, AKA and the derivation of keys.
//! - [oxirush-ngap](https://docs.rs/oxirush-ngap): NGAP, between a gNB and an AMF.
//! - [oxirush-s1ap](https://docs.rs/oxirush-s1ap): S1AP, between an eNB and an MME.
//! - [oxirush-gtp-u](https://docs.rs/oxirush-gtp-u): GTP-U tunnels, with a test UPF.
//! - [oxirush-sctp](https://docs.rs/oxirush-sctp): SCTP sockets, which carry NGAP and S1AP.
extern crate alloc;
// The plumbing of the generated crates, hidden from the documentation: it changes with
// them, and oxirush-ngap and oxirush-s1ap depend on the one version of this crate that is
// released with theirs (`=0.1.0`), so that no other version of it reaches their builds.
use Vec;
/// The examples of the README, which are compiled and run with the tests of the
/// documentation.
;
pub use ;
pub use ;
pub use ;
pub use Octets;
pub use ObjectIdentifier;
pub use ;
pub use Writer;
/// The upper bound of a size constraint that has none, as `SIZE (1..MAX)`.
///
/// ```
/// use oxirush_aper::{Reader, UNBOUNDED, Writer};
///
/// // OCTET STRING (SIZE (1..MAX)): the length of the octets, then the octets
/// let mut octets = Vec::new();
/// Writer::new(&mut octets).octet_string(&[0xca, 0xfe], 1, UNBOUNDED)?;
/// assert_eq!(octets, [0x02, 0xca, 0xfe]);
/// assert_eq!(Reader::new(&octets).octet_string(1, UNBOUNDED)?, [0xca, 0xfe]);
/// # Ok::<(), oxirush_aper::Error>(())
/// ```
pub const UNBOUNDED: usize = usizeMAX;
/// A type of an ASN.1 module. The example of [`Decode`] implements it.
/// A type whose values are read in aligned PER. `'a` is how long the octets live that a
/// value borrows from.
///
/// A type of a module implements [`AsnType`], `Decode` and [`Encode`]:
///
/// ```
/// use oxirush_aper::{AsnType, Decode, Encode, Error, Reader, Writer};
///
/// // AMF-UE-NGAP-ID ::= INTEGER (0..1099511627775)
/// #[derive(Debug, PartialEq)]
/// struct AmfUeNgapId(u64);
///
/// impl AsnType for AmfUeNgapId {
/// const NAME: &'static str = "AMF-UE-NGAP-ID";
/// }
///
/// impl Decode<'_> for AmfUeNgapId {
/// fn decode(reader: &mut Reader<'_>) -> Result<Self, Error> {
/// Ok(Self(reader.unsigned(0, 1_099_511_627_775)?))
/// }
/// }
///
/// impl Encode for AmfUeNgapId {
/// fn encode(&self, writer: &mut Writer<'_>) -> Result<(), Error> {
/// writer.unsigned(self.0, 0, 1_099_511_627_775)
/// }
/// }
///
/// // The length of the number in three bits, then its octet, aligned
/// let octets = oxirush_aper::encode(&AmfUeNgapId(42))?;
/// assert_eq!(octets, [0x00, 0x2a]);
/// assert_eq!(oxirush_aper::decode::<AmfUeNgapId>(&octets)?, AmfUeNgapId(42));
/// # Ok::<(), Error>(())
/// ```
/// A type whose values are written in aligned PER. The example of [`Decode`]
/// implements it.
/// NULL: no bit.
/// BOOLEAN: one bit.
/// The value of the type `T` that `octets` are the complete encoding of (X.691 §11.1):
/// one value, whole, and no octet after it. What the value holds of octets and of
/// strings is borrowed from `octets`.
///
/// ```
/// use oxirush_aper::Octets;
///
/// // An OCTET STRING without a constraint, whose octets stay in the input
/// let input = [0x02, 0xca, 0xfe];
/// let octets: Octets = oxirush_aper::decode(&input)?;
/// assert_eq!(octets.as_borrowed(), Some(&input[1..]));
///
/// // Octets that end early, and an octet after the value
/// assert!(oxirush_aper::decode::<Octets>(&input[..2]).is_err());
/// assert!(oxirush_aper::decode::<Octets>(&[0x01, 0xca, 0xfe]).is_err());
/// # Ok::<(), oxirush_aper::Error>(())
/// ```
///
/// # Errors
///
/// An error of the kind `Decode`, which names the type, says why, and gives the field
/// where the decoding stopped.
/// The value of the type `T` that `octets` are the complete encoding of, as [`decode`]
/// reads it, and what of the encoding is in another form than the one that X.691 gives
/// it: the [`Faults`] of the reader, with the padding of the end among them. They are
/// none for octets that are written by the clauses.
///
/// A value in such a form is whole, and the encoder of a peer may write it: a receiver
/// reads it, a tool that tests the peer says that its encoder is not canonical, and
/// [`decode_strict`] refuses it. What a value holds as the octets of an open type is not
/// read here, and has its faults where it is decoded.
///
/// ```
/// use oxirush_aper::{Fault, Octets, decode_with_faults};
///
/// // An OCTET STRING of one octet, with its length in two octets
/// let (octets, faults) = decode_with_faults::<Octets>(&[0x80, 0x01, 0xaa])?;
/// assert_eq!(octets, [0xaa]);
/// assert_eq!(faults[0].fault, Fault::LengthInTwoOctets);
/// assert_eq!(faults[0].to_string(), "at bit 0, a length of 127 or less is in two octets (X.691 §11.9.3.6)");
/// # Ok::<(), oxirush_aper::Error>(())
/// ```
///
/// # Errors
///
/// Those of [`decode`].
/// The value of the type `T` that `octets` are the complete encoding of, when they are
/// the one encoding that X.691 gives the value: what [`decode`] reads in another form,
/// a [`Fault`], is an error here. The counterpart of [`encode_strict`].
///
/// ```
/// use oxirush_aper::{Octets, decode, decode_strict};
///
/// // An OCTET STRING of one octet, whose length is in two octets
/// let input = [0x80, 0x01, 0xaa];
/// assert!(decode::<Octets>(&input).is_ok());
/// assert!(decode_strict::<Octets>(&input).is_err());
/// ```
///
/// # Errors
///
/// Those of [`decode`], and an error of the kind `Decode` that says the first fault,
/// its clause and the bit at which it is.
/// The complete encoding of `value` (X.691 §11.1): its fields, zero bits up to the end
/// of the last octet, and one zero octet for a value that has no field.
///
/// # Errors
///
/// An error of the kind `Encode`, which names the type and says why, as for a number
/// outside its constraint.
///
/// ```
/// use oxirush_aper::Octets;
///
/// // An OCTET STRING without a constraint: its length, then its octets
/// let octets = Octets::from(&[0xca, 0xfe][..]);
/// assert_eq!(oxirush_aper::encode(&octets)?, [0x02, 0xca, 0xfe]);
/// # Ok::<(), oxirush_aper::Error>(())
/// ```
/// The complete encoding of `value`, as [`encode`] gives it, for a value that is one of
/// this version of its type: a number or a size outside the root of an extensible
/// constraint, which `encode` writes as an extension, is an error.
///
/// An extensible constraint, as `INTEGER (0..255, ...)` or `SIZE (1..150, ...)`, has a
/// receiver read what a later version of the type allows, so `encode` writes any value.
/// The sender of this version sends none of them: a name of no character is no AMF
/// name, though it encodes.
///
/// # Errors
///
/// An error of the kind `Encode`, which names the type, says why, and gives the field
/// that does not encode.
/// Append the complete encoding of `value` to `octets`. After an error, `octets` are
/// as they were.
///
/// ```
/// // A BOOLEAN, one bit in an octet, then a NULL, no bit, which is one zero octet
/// let mut octets = vec![0xff];
/// oxirush_aper::encode_into(&true, &mut octets)?;
/// oxirush_aper::encode_into(&(), &mut octets)?;
/// assert_eq!(octets, [0xff, 0x80, 0x00]);
/// # Ok::<(), oxirush_aper::Error>(())
/// ```