Skip to content

Commit

Permalink
Create public APIs to check the validity of the final octet / quintet
Browse files Browse the repository at this point in the history
One use case for this information is for error correction - if we know
that the trailing octet or quintet can't possibly be valid, we may be
able to do better error correction.
  • Loading branch information
DaGenix committed Nov 12, 2021
1 parent a8d2666 commit 8941c8c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/decode_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::error::{zbase32_error, ZBase32Error, ZBase32ErrorInfo};
use crate::stateful_decoder::{HaveOctets, NeedQuintets, NextOctetResult, ProvideQuintetResult};
use crate::stateful_decoder::{
quintet_has_valid_trailing_bits, HaveOctets, NeedQuintets, NextOctetResult,
ProvideQuintetResult,
};
use crate::tables::{CHARACTER_MIN_VALUE, CHARACTER_TO_QUINTET};
use crate::util::{required_octets_buffer_len, required_quintets_buffer_len};
use core::iter::Peekable;
Expand Down Expand Up @@ -128,6 +131,15 @@ fn calc_last_quintet_bits(bits: u64) -> Option<u8> {
}
}

/// Determine if the last quintet is, given the number of bits to decode
pub fn is_last_quintet_valid(bits: u64, quintet: u8) -> bool {
if let Some(last_quintet_bits) = calc_last_quintet_bits(bits) {
quintet <= 31 && quintet_has_valid_trailing_bits(last_quintet_bits, quintet)
} else {
false
}
}

/// Convert a buffer of quintet integer values (ie, integers of the
/// range 0-31) into a buffer of octet (byte) values.
///
Expand Down
13 changes: 12 additions & 1 deletion src/encode_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::error::{zbase32_error, ZBase32Error, ZBase32ErrorInfo};
use crate::stateful_encoder::{HaveQuintets, NeedOctets, NextQuintetResult, ProvideOctetResult};
use crate::stateful_encoder::{
octet_has_valid_trailing_bits, HaveQuintets, NeedOctets, NextQuintetResult, ProvideOctetResult,
};
use crate::tables::QUINTET_TO_CHARACTER;
use crate::util::{required_octets_buffer_len, required_quintets_buffer_len};
use core::iter::Peekable;
Expand Down Expand Up @@ -123,6 +125,15 @@ fn calc_last_octet_bits(bits: u64) -> Option<u8> {
}
}

/// Determine if the last octet is valid, given the number of bits to encode
pub fn is_last_octet_valid(bits: u64, octet: u8) -> bool {
if let Some(last_octet_bits) = calc_last_octet_bits(bits) {
octet_has_valid_trailing_bits(last_octet_bits, octet)
} else {
false
}
}

/// Convert a buffer of octets (bytes) into a buffer of quintet values (ie, integers between 0-31).
///
/// The length of `in_octets` must match the value returned by
Expand Down
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,16 @@ pub use encode_impl::encode;

pub mod low_level_decode {
//! Low-level decoding functionality
pub use crate::decode_impl::{character_to_quintet, decode_slices, quintets_to_octets};
pub use crate::decode_impl::{
character_to_quintet, decode_slices, is_last_quintet_valid, quintets_to_octets,
};
pub use crate::util::required_octets_buffer_len;
}

pub mod low_level_encode {
//! Low-level encoding functionality
pub use crate::encode_impl::{encode_slices, octets_to_quintets, quintet_to_character};
pub use crate::encode_impl::{
encode_slices, is_last_octet_valid, octets_to_quintets, quintet_to_character,
};
pub use crate::util::required_quintets_buffer_len;
}

0 comments on commit 8941c8c

Please sign in to comment.