pub struct GCR { /* private fields */ }Implementations§
Source§impl GCR
impl GCR
Sourcepub fn new() -> Self
pub fn new() -> Self
Constructs a new GCR (Group Code Recording) instance with precomputed
lookup tables for efficient encoding and decoding operations.
The GCR struct uses two lookup tables:
decode_mappings: A table that maps 5-bit encoded values (keys) to their decoded 4-bit values. This is used for decoding operations. Values that are considered invalid are initialized to0xFF.encode_mappings: A table that maps 4-bit decoded values into their respective 5-bit encoded counterparts, which is used for encoding operations.
The mapping pairs are predefined and represent the 4-bit to 5-bit encoding scheme:
(Encoded, Decoded)
(01010, 0), (01011, 1), (10010, 2), (10011, 3),
(01110, 4), (01111, 5), (10110, 6), (10111, 7),
(01001, 8), (11001, 9), (11010, 10), (11011, 11),
(01101, 12), (11101, 13), (11110, 14), (10101, 15)Each (encoded, decoded) mapping is used to populate the appropriate
indices in the lookup tables. For example:
decode_mappings[encoded] = decodedencode_mappings[decoded] = encoded
§Returns
Returns an instance of the GCR struct with initialized decode_mappings
and encode_mappings.
§Example
let gcr = GCR::new();
assert_eq!(gcr.decode_mappings[0b01010], 0); // Decodes "01010" to 0
assert_eq!(gcr.encode_mappings[0], 0b01010); // Encodes 0 to "01010"Sourcepub fn decode(&self, value: &[u8]) -> Option<Vec<u8>>
pub fn decode(&self, value: &[u8]) -> Option<Vec<u8>>
Decodes the provided input byte slice (value) into a Vec<u8>.
The decode method processes the input in chunks of 5 bytes, converting each chunk into a
64-bit integer by padding it with three leading zero bytes. It then calls the
decode_quintuple method to decode the chunk into a vector of bytes.
§Parameters
value: A reference to a slice of bytes (&[u8]) that represents the encoded input data. This slice must have a length that is a multiple of 5 for full decoding.
§Return
- Returns
Some(Vec<u8>)if decoding is successful for all chunks. - Returns
Noneif any chunk fails to decode.
§Example
let mut decoder = MyDecoder::new();
let input = vec![0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A];
if let Some(decoded) = decoder.decode(&input) {
println!("Decoded output: {:?}", decoded);
} else {
eprintln!("Failed to decode input.");
}§Notes
- This method uses
chunks_exact(5)to divide the input into fixed-size chunks of 5. - For each chunk, a
u64is constructed by appending three leading zero bytes to the 5-byte chunk to match the byte size of au64. - It relies on the
decode_quintuplemethod to handle the actual decoding logic for each chunk of reconstructed data. Ifdecode_quintuplereturnsNonefor any chunk, the entire decoding fails and the method returnsNone.
§Panics
This method does not panic under normal operation. However, improper implementation of
decode_quintuple or incorrect input may result in unexpected behavior.
Sourcepub fn encode(&self, value: &[u8]) -> Vec<u8> ⓘ
pub fn encode(&self, value: &[u8]) -> Vec<u8> ⓘ
Encodes a slice of bytes using a custom encoding scheme and returns the encoded data as a Vec<u8>.
This method processes the input data in chunks of 4 bytes at a time. For each chunk:
- Each byte is split into two 4-bit nibbles (high and low).
- These nibbles are then mapped to corresponding 5-bit encoded values using a predefined
encode_mappingstable. - The 8 encoded nibbles (now 5-bit codes) are packed into a 40-bit value in a big-endian format.
- Finally, the 40-bit value is split into 5 bytes and appended to the output vector.
§Parameters
value: A slice of bytes (&[u8]) to be encoded.
§Returns
Vec<u8>: A vector containing the encoded bytes.
§Panics
This function assumes that self.encode_mappings is properly defined (with valid mappings for all 4-bit values [0-15])
and does not perform boundary checks on its size. Providing an invalid or incorrectly sized mapping may result in undefined behavior.
§Example
struct Encoder {
encode_mappings: [u8; 16],
}
let encoder = Encoder {
encode_mappings: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
};
let input = vec![0x12, 0x34, 0x56, 0x78];
let encoded = encoder.encode(&input);
println!("{:?}", encoded);§Notes
- The function processes the input in chunks of exactly 4 bytes. If the length of the input slice is not a multiple of 4, the remaining bytes will be ignored. It is the caller’s responsibility to handle padding or provide properly-sized input.