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
#![allow(dead_code)]
//! ASN.1 PER Encoding

pub mod error;

pub mod aper;

pub mod uper;

mod common;

pub use error::Error as PerCodecError;

use bitvec::prelude::*;

/// Structure representing an APER Codec.
///
/// While En(De)coding ASN.1 Types using the APER encoding scheme, the encoded data is stored in a
/// `BitVec`.
#[derive(Default, Debug)]
pub struct PerCodecData {
    bits: BitVec<u8, Msb0>,
    decode_offset: usize,
    key: Option<i128>,
    aligned: bool,
}

impl PerCodecData {
    /// Default `PerCodecData` for AperCodec
    pub fn new_aper() -> Self {
        Self {
            aligned: true,
            ..Self::default()
        }
    }

    /// Default `PerCodecData` for UperCodec
    pub fn new_uper() -> Self {
        Self::default()
    }

    /// Create Our `PerCodecData` Structure from a slice of u8 for AperCodec
    pub fn from_slice_aper(bytes: &[u8]) -> Self {
        Self::from_slice_internal(bytes, true)
    }

    /// Create Our `PerCodecData` Structure from a slice of u8 for UperCodec
    pub fn from_slice_uper(bytes: &[u8]) -> Self {
        Self::from_slice_internal(bytes, false)
    }

    fn from_slice_internal(bytes: &[u8], aligned: bool) -> Self {
        Self {
            bits: BitSlice::<_, _>::from_slice(bytes).to_bitvec(),
            decode_offset: 0,
            key: None,
            aligned,
        }
    }

    /// Get's the inner buffer as a `Vec<u8>` consuming the struct.
    pub fn into_bytes(self) -> Vec<u8> {
        self.bits.into()
    }

    /// Align to 8 bit boundry during decode.
    pub fn decode_align(&mut self) -> Result<(), PerCodecError> {
        if self.decode_offset % 8 == 0 {
            return Ok(());
        }

        let remaining = 8 - (self.decode_offset & 0x7_usize);
        log::debug!("Aligning Codec Buffer with {} bits", remaining);

        if !self.bits[self.decode_offset..self.decode_offset + remaining]
            .iter()
            .all(|b| b == false)
        {
            Err(PerCodecError::new(
                format!(
                    "{} Padding bits at Offset {} not all '0'.",
                    remaining, self.decode_offset,
                )
                .as_str(),
            ))
        } else {
            self.decode_offset += remaining;
            Ok(())
        }
    }

    fn decode_bool(&mut self) -> Result<bool, PerCodecError> {
        if self.bits.len() == self.decode_offset {
            return Err(PerCodecError::new(
                "perCodec:DecodeError:End of Bitstream reached while trying to decode bool.",
            ));
        }
        let bit = *self.bits.get(self.decode_offset).as_deref().unwrap();
        self.advance_maybe_err(1, true)?;

        Ok(bit)
    }

    fn decode_bits_as_integer(&mut self, bits: usize, signed: bool) -> Result<i128, PerCodecError> {
        let remaining = self.bits.len() - self.decode_offset;
        if remaining < bits {
            Err(PerCodecError::new(
                format!(
                    "PerCodec:DecodeError:Requested Bits to decode {}, Remaining bits {}",
                    bits, remaining
                )
                .as_str(),
            ))
        } else {
            log::trace!(
                "Decoding Bits as Integer. offset: {}, bits: {}",
                self.decode_offset,
                bits
            );
            let value = if !signed {
                if bits == 0 {
                    0_i128
                } else {
                    self.bits[self.decode_offset..self.decode_offset + bits].load_be::<u128>()
                        as i128
                }
            } else {
                match bits {
                    8 => {
                        let inner = self.bits[self.decode_offset..self.decode_offset + bits]
                            .load_be::<u128>() as i8;
                        inner as i128
                    }
                    16 => {
                        let inner = self.bits[self.decode_offset..self.decode_offset + bits]
                            .load_be::<u128>() as i16;
                        inner as i128
                    }
                    24 => {
                        let inner = self.bits[self.decode_offset..self.decode_offset + bits]
                            .load_be::<u32>() as u32;
                        let inner = if self.bits[self.decode_offset] {
                            // Bit is 1 negative no.
                            inner | 0xFF000000
                        } else {
                            inner & 0x00FFFFFF
                        };
                        let inner = inner as i32;
                        inner as i128
                    }
                    32 => {
                        let inner = self.bits[self.decode_offset..self.decode_offset + bits]
                            .load_be::<u128>() as i32;
                        inner as i128
                    }
                    40 => {
                        let inner = self.bits[self.decode_offset..self.decode_offset + bits]
                            .load_be::<u64>() as u64;
                        let inner = if self.bits[self.decode_offset] {
                            // Bit is 1 negative no.
                            inner | 0xFFFFFF0000000000
                        } else {
                            inner & 0x000000FFFFFFFFFF
                        };
                        let inner = inner as i64;
                        inner as i128
                    }
                    48 => {
                        let inner = self.bits[self.decode_offset..self.decode_offset + bits]
                            .load_be::<u64>() as u64;
                        let inner = if self.bits[self.decode_offset] {
                            // Bit is 1 negative no.
                            inner | 0xFFFF000000000000
                        } else {
                            inner & 0x0000FFFFFFFFFFFF
                        };
                        let inner = inner as i64;
                        inner as i128
                    }
                    56 => {
                        eprintln!("{}", self.decode_offset);
                        let inner = self.bits[self.decode_offset..self.decode_offset + bits]
                            .load_be::<u64>() as u64;
                        let inner = if self.bits[self.decode_offset] {
                            // Bit is 1 negative no.
                            inner | 0xFF00000000000000
                        } else {
                            inner & 0x00FFFFFFFFFFFFFF
                        };
                        let inner = inner as i64;
                        inner as i128
                    }
                    64 => {
                        let inner = self.bits[self.decode_offset..self.decode_offset + bits]
                            .load_be::<u128>() as i64;
                        inner as i128
                    }
                    128 => {
                        let inner = self.bits[self.decode_offset..self.decode_offset + bits]
                            .load_be::<u128>() as i128;
                        inner as i128
                    }
                    _ => {
                        return Err(
                            PerCodecError::new(
                                format!(
                                    "For a signed number in 2's compliment form, requested bits {} not supported!",
                                    bits)));
                    }
                }
            };
            log::trace!("Decoded Value: {:?}", value);
            self.advance_maybe_err(bits, false)?;
            Ok(value)
        }
    }

    fn advance_maybe_err(&mut self, bits: usize, ignore: bool) -> Result<(), PerCodecError> {
        let offset = self.decode_offset + bits;
        if offset > self.bits.len() {
            if ignore {
                self.decode_offset = self.bits.len()
            } else {
                let remaining = self.bits.len() - self.decode_offset;
                return Err(PerCodecError::new(
                    format!(
                        "PerCodec:DecodeError:Requested Bits to advance {}, Remaining bits {}",
                        bits, remaining
                    )
                    .as_str(),
                ));
            }
        } else {
            self.decode_offset = offset
        }
        Ok(())
    }

    fn get_bit(&self) -> Result<bool, PerCodecError> {
        if self.decode_offset >= self.bits.len() {
            return Err(PerCodecError::new(
                format!(
                    "PerCodec:GetBitError:Requested Bit {}, Remaining bits {}",
                    self.decode_offset,
                    self.bits.len() - self.decode_offset
                )
                .as_str(),
            ));
        }
        let bit = *self.bits.get(self.decode_offset).as_deref().unwrap();
        Ok(bit)
    }

    fn get_bitvec(&mut self, length: usize) -> Result<BitVec<u8, Msb0>, PerCodecError> {
        if length + self.decode_offset > self.bits.len() {
            return Err(PerCodecError::new(
                format!(
                    "PerCodec:GetBitError:Requested Bit {}, Remaining bits {}",
                    length,
                    self.bits.len() - self.decode_offset
                )
                .as_str(),
            ));
        }
        let bv = BitVec::from_bitslice(&self.bits[self.decode_offset..self.decode_offset + length]);
        self.advance_maybe_err(length, true)?;

        Ok(bv)
    }

    fn get_bytes(&mut self, length: usize) -> Result<Vec<u8>, PerCodecError> {
        let length = length * 8;
        if length + self.decode_offset > self.bits.len() {
            return Err(PerCodecError::new(
                format!(
                    "PerCodec:GetBitError:Requested Bits {}, Remaining bits {}",
                    length,
                    self.bits.len() - self.decode_offset
                )
                .as_str(),
            ));
        }
        let mut bv = self.bits[self.decode_offset..self.decode_offset + length].to_bitvec();
        bv.force_align();
        self.advance_maybe_err(length, true)?;
        Ok(BitVec::into_vec(bv))
    }

    pub fn get_inner(&self) -> Result<Vec<u8>, PerCodecError> {
        Ok(BitVec::into_vec(self.bits.to_bitvec()))
    }

    /// Get's the current `key` value.
    ///
    /// This value will be used by a decoder to determine which 'decode' function is to be called
    /// (for example in an `enum`, it will be used to determine which `variant` of the `enum` will
    /// be decoded.
    pub fn get_key(&self) -> Option<i128> {
        self.key
    }

    /// Sets the current `key` value.
    ///
    /// During decoding 'open' types, the 'key' used to decode the type further is determined by a
    /// `key_value` field. ie. a field with attribute `key_value` set in a struct (derived from a
    /// SEQUENCE ASN.1 type.) This value is passed to the 'decoder' logic further through `set_key`
    /// function, which updates the internal state of the decoder data.
    pub fn set_key(&mut self, key: i128) {
        let _ = self.key.replace(key);
    }

    /// Dump current 'offset'.
    #[inline]
    pub fn dump(&self) {
        log::debug!("PerCodecData: offset: {}", self.decode_offset);
    }

    #[inline]
    pub fn dump_encode(&self) {
        log::debug!("PerCodecData: current_len : {}", self.bits.len());
    }

    /// Reserve certain bits at the current `offset`.
    #[inline]
    pub fn reserve(&mut self, count: usize) {
        self.bits.reserve(count);
        self.decode_offset = count;
    }

    /// `seek` pointer to the offset in the internal buffer
    #[inline]
    pub fn seek(&mut self, offset: usize) {
        self.decode_offset = offset;
    }

    pub fn swap_bits(&mut self, other: &mut BitSlice<u8, Msb0>, offset: usize) {
        self.bits[offset..other.len() + offset].swap_with_bitslice(other);
    }

    pub fn set_bit(&mut self, index: usize, value: bool) {
        self.bits.set(index, value);
    }
    // Encoding functions.

    /// Encode a bool.
    fn encode_bool(&mut self, value: bool) {
        self.bits.push(value);
    }

    /// Add bits to the encoding buffer.
    fn append_bits(&mut self, bits: &BitSlice<u8, Msb0>) {
        self.bits.extend_from_bitslice(bits);
    }

    /// Byte align the encoding buffer by padding with zero bits.
    fn align(&mut self) {
        let remaining = 8 - (self.bits.len() & 0x7_usize);
        if remaining < 8 {
            self.bits.resize(self.bits.len() + remaining, false);
        }
    }

    /// Get the length of the data in bytes
    /// This is useful when encoding an open type.
    pub fn length_in_bytes(&self) -> usize {
        ((self.bits.len() - 1) / 8) + 1
    }

    /// Append one encoding to another preserving byte alignment.
    /// This is useful when encoding an open type.
    pub fn append_aligned(&mut self, other: &mut Self) {
        self.align();
        other.align();
        self.append_bits(&other.bits)
    }
}