seqc 0.1.1

Pattern-based encoding library
Documentation
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
use crate::{error::DialectError, pattern::VariablePattern};

/// Represents a dialect with a fixed number of patterns.
///
/// # Parameters
///
/// * `N` - Number of patterns
#[cfg_attr(feature = "std", derive(Debug))]
pub struct Dialect<const N: usize> {
    pub patterns: [VariablePattern; N],
}

impl<const N: usize> Dialect<N> {
    pub const fn new(patterns: [VariablePattern; N]) -> Self {
        Self { patterns }
    }

    pub const fn measure_encode_capacity(&self, payload: &[u8]) -> usize {
        let (mut pattern_idx, mut i) = (0usize, 0usize);
        let mut capacity: usize = 0;

        while i < payload.len() {
            let remaining: usize = payload.len() - i;
            let chunk_size: usize = if remaining >= 3 { 3 } else { remaining };

            let groups: &[u8] = match chunk_size {
                3 => &[
                    // Bits 23-18
                    (((((payload[i] as u32) << 16)
                        | ((payload[i + 1] as u32) << 8)
                        | (payload[i + 2] as u32))
                        >> 18)
                        & 0x3f) as u8,
                    // Bits 17-12
                    (((((payload[i] as u32) << 16)
                        | ((payload[i + 1] as u32) << 8)
                        | (payload[i + 2] as u32))
                        >> 12)
                        & 0x3f) as u8,
                    // Bits 11-6
                    (((((payload[i] as u32) << 16)
                        | ((payload[i + 1] as u32) << 8)
                        | (payload[i + 2] as u32))
                        >> 6)
                        & 0x3f) as u8,
                    // Bits 5-0
                    ((((payload[i] as u32) << 16)
                        | ((payload[i + 1] as u32) << 8)
                        | (payload[i + 2] as u32))
                        & 0x3f) as u8,
                ],
                2 => &[
                    // Bits 15-10
                    (((((payload[i] as u16) << 8) | (payload[i + 1] as u16)) >> 10) & 0x3f) as u8,
                    // Bits 9-4
                    (((((payload[i] as u16) << 8) | (payload[i + 1] as u16)) >> 4) & 0x3f) as u8,
                    // Bits 3-0
                    ((((payload[i] as u16) << 8) | (payload[i + 1] as u16)) & 0xf) as u8,
                ],
                1 => &[(payload[i] >> 4) & 0x0f, payload[i] & 0x0f],
                _ => &[],
            };

            let mut group_idx: usize = 0;

            while group_idx < groups.len() {
                let bits: u8 = groups[group_idx];
                let pattern: &VariablePattern = &self.patterns[pattern_idx % self.patterns.len()];

                capacity += pattern.measure_encode_capacity(bits) + 1;
                pattern_idx += 1;
                group_idx += 1;
            }

            i += chunk_size;
        }

        capacity
    }

    /// Encodes a byte slice into a pre-allocated buffer using the dialect's patterns.
    ///
    /// This is the core, `no_std`-compatible encoding function. It processes the
    /// input `payload` in chunks, distributing the bits from each chunk across a
    /// series of words according to the dialect's patterns. These words are then
    /// written sequentially into the output `buffer`.
    ///
    /// # Parameters
    ///
    /// * `payload`: The raw byte slice to be encoded.
    /// * `buffer`: A mutable byte slice that will receive the encoded output. Its
    ///   length must be sufficient to hold the entire encoded message. Use
    ///   [`Dialect::measure_encode_capacity`] to determine the required size.
    ///
    /// # Returns
    ///
    /// A `Result` which is:
    /// * `Ok(usize)`: The number of bytes written to the `buffer`.
    /// * `Err(DialectError)`: If an error occurs during encoding.
    ///
    /// # Errors
    /// * [`DialectError::BufferExhausted`]: If the provided `buffer` is too small
    ///   to hold the encoded data.
    ///
    /// # Examples
    ///
    /// ```
    /// use seqc::{Dialect, Pattern, VariablePattern};
    ///
    /// const DIALECT: Dialect<1> = Dialect::new([
    ///     VariablePattern::Binary(Pattern::from([b'0', b'1'])),
    /// ]);
    ///
    /// const DATA: &[u8] = &[0xB4];
    ///
    /// let mut buffer = [0u8; DIALECT.measure_encode_capacity(DATA)];
    /// let bytes_written = DIALECT.encode_slice(DATA, &mut buffer).unwrap();
    ///
    /// // The output reflects the bit distribution for the single byte.
    /// assert_eq!(bytes_written, 14);
    /// assert_eq!(&buffer[..bytes_written], b"001111 011111 ");
    /// ```
    pub fn encode_slice(&self, payload: &[u8], buffer: &mut [u8]) -> Result<usize, DialectError> {
        let (mut pattern_idx, mut buffer_idx) = (0usize, 0usize);

        macro_rules! write_word {
            ($bits:expr) => {
                let pattern = &self.patterns[pattern_idx % N];
                pattern_idx += 1;

                let word = pattern.encode($bits);

                if buffer_idx + word.length > buffer.len() {
                    return Err(DialectError::BufferExhausted);
                }

                buffer[buffer_idx..buffer_idx + word.length]
                    .copy_from_slice(&word.buffer[..word.length]);
                buffer_idx += word.length;

                buffer[buffer_idx] = b' ';
                buffer_idx += 1;
            };
        }

        for bytes in payload.chunks(3) {
            match bytes.len() {
                // Process 3 bytes (24 bits), producing 4 words
                3 => {
                    let packed_value: u32 = (u32::from(bytes[0]) << 16)
                        | (u32::from(bytes[1]) << 8)
                        | u32::from(bytes[2]);

                    let group0: u8 = ((packed_value >> 18) & 0x3f) as u8; // Bits 23-18
                    let group1: u8 = ((packed_value >> 12) & 0x3f) as u8; // Bits 17-12
                    let group2: u8 = ((packed_value >> 6) & 0x3f) as u8; // Bits 11-6
                    let group3: u8 = (packed_value & 0x3f) as u8; // Bits 5-0

                    write_word!(group0);
                    write_word!(group1);
                    write_word!(group2);
                    write_word!(group3);
                }
                // Process 2 bytes (16 bits), producing 3 words
                2 => {
                    let packed_value: u16 = (u16::from(bytes[0]) << 8) | u16::from(bytes[1]);

                    // Split into groups: 6 bits, 6 bits, 4 bits
                    let group0: u8 = ((packed_value >> 10) & 0x3f) as u8; // Bits 15-10
                    let group1: u8 = ((packed_value >> 4) & 0x3f) as u8; // Bits 9-4
                    let group2: u8 = (packed_value & 0xf) as u8; // Bits 3-0

                    write_word!(group0);
                    write_word!(group1);
                    write_word!(group2);
                }
                // Process 1 byte (8 bits), producing 2 words
                1 => {
                    let hi_bits: u8 = (bytes[0] >> 4) & 0x0f;
                    let lo_bits: u8 = bytes[0] & 0x0f;

                    write_word!(hi_bits);
                    write_word!(lo_bits);
                }
                _ => unreachable!(),
            }
        }

        Ok(buffer_idx)
    }

    /// Decodes a slice of space-separated words into a pre-allocated buffer.
    ///
    /// This is the core, `no_std`-compatible decoding function. It interprets the
    /// `payload` as a UTF-8 string of words separated by spaces. It processes the
    /// words in groups, reconstructing the original byte chunks by collecting bits
    /// from each word according to the dialect's patterns. The resulting raw bytes
    /// are written to the output `buffer`.
    ///
    /// # Parameters
    ///
    /// * `payload`: The encoded byte slice. Must contain valid UTF-8 characters.
    /// * `buffer`: A mutable byte slice that will receive the decoded raw bytes.
    ///   Its length should be sufficient to hold the output.
    ///
    /// # Returns
    ///
    /// A `Result` which is:
    /// * `Ok(usize)`: The number of decoded bytes written to the `buffer`.
    /// * `Err(DialectError)`: If an error occurs during decoding.
    ///
    /// # Errors
    ///
    /// * [`DialectError::NonUtf8Payload`]: If the `payload` is not a valid UTF-8 string.
    /// * [`DialectError::UnexpectedRemainder`]: If the `payload` contains a number of
    ///   words that cannot be fully decoded back.
    ///
    /// # Examples
    ///
    /// ```
    /// use seqc::{Dialect, Pattern, VariablePattern};
    ///
    /// let dialect: Dialect<2> = Dialect::new([
    ///     VariablePattern::Ternary(Pattern::from([b'a', b'b', b'c'])),
    ///     VariablePattern::Quaternary(Pattern::from([b'x', b'y', b'z', b'w'])),
    /// ]);
    ///
    /// const ENCODED: &str = "aaabbbccc xxxyyyzzww aaabbbbcccc xyyyyzw abbbbcc xyyyyzww ";
    ///
    /// let mut buffer = [0u8; ENCODED.len()];
    /// let bytes_written = dialect.decode_slice(ENCODED.as_bytes(), &mut buffer).unwrap();
    ///
    /// assert_eq!(bytes_written, 4);
    /// assert_eq!(&buffer[..bytes_written], &[0xAA, 0xBB, 0xCC, 0xDD]);
    /// ```
    pub fn decode_slice(&self, payload: &[u8], buffer: &mut [u8]) -> Result<usize, DialectError> {
        let (mut pattern_idx, mut buffer_idx) = (0usize, 0usize);

        let payload_str: &str =
            str::from_utf8(payload).map_err(|_| DialectError::NonUtf8Payload)?;

        let mut chunks_iter = payload_str.split_ascii_whitespace().array_chunks::<4>();

        macro_rules! decode_word {
            ($word:expr) => {
                (|pattern_idx: &mut usize| {
                    let pattern = &self.patterns[*pattern_idx % N];
                    *pattern_idx += 1;

                    pattern.decode($word)
                })(&mut pattern_idx)
            };
        }

        macro_rules! write_bytes {
            ($bytes:expr) => {
                buffer[buffer_idx..buffer_idx + $bytes.len()].copy_from_slice($bytes);
                buffer_idx += $bytes.len();
            };
        }

        // Process 4 words, producing 3 bytes (24 bits)
        while let Some(bytes) = chunks_iter.next() {
            let group0: u8 = decode_word!(bytes[0]);
            let group1: u8 = decode_word!(bytes[1]);
            let group2: u8 = decode_word!(bytes[2]);
            let group3: u8 = decode_word!(bytes[3]);

            let bits0: u8 = group0 & 0x3f;
            let bits1: u8 = group1 & 0x3f;
            let bits2: u8 = group2 & 0x3f;
            let bits3: u8 = group3 & 0x3f;

            // Assemble full 24-bit value
            let packed_value: u32 = (u32::from(bits0) << 18)
                | (u32::from(bits1) << 12)
                | (u32::from(bits2) << 6)
                | u32::from(bits3);

            // Extract 3 bytes
            let byte0: u8 = ((packed_value >> 16) & 0xff) as u8;
            let byte1: u8 = ((packed_value >> 8) & 0xff) as u8;
            let byte2: u8 = (packed_value & 0xff) as u8;

            write_bytes!(&[byte0, byte1, byte2]);
        }

        let remainder = chunks_iter.into_remainder();
        let remainder_slice: &[&str] = remainder.as_slice();

        match remainder.len() {
            // Process 3 words, producing 2 bytes (16 bits)
            3 => {
                let group0: u8 = decode_word!(remainder_slice[0]);
                let group1: u8 = decode_word!(remainder_slice[1]);
                let group2: u8 = decode_word!(remainder_slice[2]);

                let bits0: u8 = group0 & 0x3f; // 6 bits
                let bits1: u8 = group1 & 0x3f; // 6 bits
                let bits2: u8 = group2 & 0xf; // 4 bits

                // Assemble 16-bit value
                let packed_value: u32 =
                    (u32::from(bits0) << 10) | (u32::from(bits1) << 4) | u32::from(bits2);

                // Extract 2 bytes
                let byte0: u8 = ((packed_value >> 8) & 0xff) as u8;
                let byte1: u8 = (packed_value & 0xff) as u8;

                write_bytes!(&[byte0, byte1]);
            }
            // Process 2 words, producing 1 byte (8 bits)
            2 => {
                let hi_group: u8 = decode_word!(remainder_slice[0]);
                let lo_group: u8 = decode_word!(remainder_slice[1]);

                // Assemble 8-bit value
                let hi_bits: u8 = hi_group & 0xf; // 4 bits
                let lo_bits: u8 = lo_group & 0xf; // 4 bits

                // Extract byte
                let byte: u8 = (hi_bits << 4) | lo_bits;

                write_bytes!(&[byte]);
            }
            0 => (),
            _ => return Err(DialectError::UnexpectedRemainder),
        }

        Ok(buffer_idx)
    }

    /// Encodes a byte slice into a new `Vec<u8>` using the dialect's pattern.
    ///
    /// This method provides a convenient, high-level interface for encoding. It first
    /// calculates the exact required buffer size, allocates a `Vec<u8>`, and then
    /// processes the input payload by distributing bits across chunks (e.g., even or
    /// uneven distribution). This function is only available when the `std` feature
    /// is enabled.
    ///
    /// # Parameters
    ///
    /// * `payload`: A type that can be referenced as a byte slice
    ///   containing the raw data to be encoded.
    ///
    /// # Returns
    ///
    /// A `Vec<u8>` containing the encoded, human-readable data.
    ///
    /// # Examples
    ///
    /// ```
    /// use seqc::{Dialect, Pattern, VariablePattern};
    ///
    /// let dialect: Dialect<2> = Dialect::new([
    ///     VariablePattern::Ternary(Pattern::from([b'a', b'b', b'c'])),
    ///     VariablePattern::Quaternary(Pattern::from([b'x', b'y', b'z', b'w'])),
    /// ]);
    ///
    /// let data = vec![0xAA, 0xBB, 0xCC, 0xDD];
    /// let encoded = dialect.encode(&data);
    ///
    /// assert_eq!(
    ///     &*String::from_utf8_lossy(&encoded),
    ///     "aaabbbccc xxxyyyzzww aaabbbbcccc xyyyyzw abbbbcc xyyyyzww "
    /// );
    ///
    /// ```
    #[cfg(feature = "std")]
    pub fn encode(&self, payload: impl AsRef<[u8]>) -> Vec<u8> {
        let payload: &[u8] = payload.as_ref();
        let mut buffer: Vec<u8> = vec![0u8; self.measure_encode_capacity(payload)];

        // SAFETY: Since the buffer is pre-allocated with the measured encoded size,
        // we can guarantee that [`Dialect::encode_slice`] will not throw.
        self.encode_slice(payload, &mut buffer)
            .map(|x| {
                unsafe { buffer.set_len(x) };
                buffer.shrink_to_fit();

                buffer
            })
            .unwrap()
    }

    /// Decodes a slice of space-separated words back into a new `Vec<u8>`.
    ///
    /// This high-level method provides a convenient interface for decoding. It
    /// allocates a `Vec<u8>` and processes the input payload, which is expected to
    /// be a UTF-8 string of words separated by spaces. It reconstructs the original
    /// raw bytes by interpreting the words according to the dialect's patterns. This
    /// function is only available when the `std` feature is enabled.
    ///
    /// # Parameters
    ///
    /// * `payload`: A type that can be referenced as a byte slice
    ///   containing the encoded, space-separated words.
    ///
    /// # Returns
    ///
    /// A `Result` which is:
    /// * `Ok(Vec<u8>)`: A new vector containing the decoded raw data.
    /// * `Err(DialectError)`: If an error occurs during decoding.
    ///
    /// # Errors
    ///
    /// This function can fail if the input payload is not valid UTF-8 or if it
    /// contains a number of words that cannot be decoded back into bytes using
    /// dialect's patterns.
    ///
    /// # Examples
    ///
    /// ```
    /// use seqc::{Dialect, Pattern, VariablePattern};
    ///
    /// let dialect: Dialect<2> = Dialect::new([
    ///     VariablePattern::Ternary(Pattern::from([b'a', b'b', b'c'])),
    ///     VariablePattern::Quaternary(Pattern::from([b'x', b'y', b'z', b'w'])),
    /// ]);
    ///
    /// // Corresponds to the encoded form of `[0xAA, 0xBB, 0xCC, 0xDD]`
    /// let encoded_data = b"aaabbbccc xxxyyyzzww aaabbbbcccc xyyyyzw abbbbcc xyyyyzww ";
    /// let decoded = dialect.decode(encoded_data).unwrap();
    ///
    /// assert_eq!(decoded, vec![0xAA, 0xBB, 0xCC, 0xDD]);
    /// ```
    #[cfg(feature = "std")]
    pub fn decode(&self, payload: impl AsRef<[u8]>) -> Result<Vec<u8>, DialectError> {
        let payload: &[u8] = payload.as_ref();
        let mut buffer: Vec<u8> = vec![0u8; payload.len()];

        self.decode_slice(payload, &mut buffer).map(|x| {
            unsafe { buffer.set_len(x) };
            buffer.shrink_to_fit();

            buffer
        })
    }
}

macro_rules! impl_variable_dialect {
    (
        $(#[$outer:meta])*
        $vis:vis enum $name:ident {
            $(
                $(#[$inner:meta])*
                $variant:ident($inner_ty:ty)
            ),*
            $(,)?
        }
    ) => {
        $(#[$outer])*
        $vis enum $name {
            $(
                $(#[$inner])*
                $variant($inner_ty),
            )*
        }

        impl $name {
            pub const fn measure_encode_capacity(&self, payload: &[u8]) -> usize {
                match self {
                    $( Self::$variant(p) => p.measure_encode_capacity(payload), )*
                }
            }

            pub fn encode_slice(&self, payload: &[u8], buffer: &mut [u8]) -> Result<usize, DialectError> {
                match self {
                    $( Self::$variant(p) => p.encode_slice(payload, buffer), )*
                }
            }

            pub fn decode_slice(&self, payload: &[u8], buffer: &mut [u8]) -> Result<usize, DialectError> {
                match self {
                    $( Self::$variant(p) => p.decode_slice(payload, buffer), )*
                }
            }

            #[cfg(feature = "std")]
            pub fn encode(&self, payload: impl AsRef<[u8]>) -> Vec<u8> {
                match self {
                    $( Self::$variant(p) => p.encode(payload), )*
                }
            }

            #[cfg(feature = "std")]
            pub fn decode(&self, payload: impl AsRef<[u8]>) -> Result<Vec<u8>, DialectError> {
                match self {
                    $( Self::$variant(p) => p.decode(payload), )*
                }
            }
        }
    };
}

// todo! think about ambiguous size fix due `lookup_table`
impl_variable_dialect! {
    /// Represents a dialect with a variable number of patterns.
    #[cfg_attr(feature = "std", derive(Debug))]
    #[allow(clippy::large_enum_variant)]
    pub enum VariableDialect {
        /// Dialect with 1 pattern.
        Unary(Dialect<1>),
        /// Dialect with 2 patterns.
        Binary(Dialect<2>),
        /// Dialect with 3 patterns.
        Ternary(Dialect<3>),
        /// Dialect with 4 patterns.
        Quaternary(Dialect<4>),
        /// Dialect with 5 patterns.
        Quinary(Dialect<5>),
        /// Dialect with 6 patterns.
        Senary(Dialect<6>),
    }
}