Skip to content

Commit

Permalink
htp.rs: restrict rust visibiluty and remove some dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
catenacyber committed Feb 3, 2025
1 parent 11abb4b commit e512c24
Show file tree
Hide file tree
Showing 22 changed files with 229 additions and 294 deletions.
50 changes: 17 additions & 33 deletions rust/htp/src/bstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,21 @@ impl Default for Bstr {

impl Bstr {
/// Make a new owned Bstr
pub fn new() -> Self {
pub(crate) fn new() -> Self {
Bstr {
s: BString::from(Vec::new()),
}
}

/// Make a new owned Bstr with given capacity
pub fn with_capacity(len: usize) -> Self {
pub(crate) fn with_capacity(len: usize) -> Self {
Bstr {
s: BString::from(Vec::with_capacity(len)),
}
}

/// Split the Bstr into a a collection of substrings, seperated by the given byte string.
/// Each element yielded is guaranteed not to include the splitter substring.
/// Returns a Vector of the substrings.
pub fn split_str_collect<'b, B: ?Sized + AsRef<[u8]>>(
&'b self, splitter: &'b B,
) -> Vec<&'b [u8]> {
self.s.as_bstr().split_str(splitter.as_ref()).collect()
}

/// Compare this bstr with the given slice
pub fn cmp_slice<B: AsRef<[u8]>>(&self, other: B) -> Ordering {
pub(crate) fn cmp_slice<B: AsRef<[u8]>>(&self, other: B) -> Ordering {
self.as_slice().cmp(other.as_ref())
}

Expand All @@ -54,7 +45,7 @@ impl Bstr {
}

/// Compare bstr with the given slice, ingnoring ascii case.
pub fn cmp_nocase<B: AsRef<[u8]>>(&self, other: B) -> Ordering {
pub(crate) fn cmp_nocase<B: AsRef<[u8]>>(&self, other: B) -> Ordering {
let lefts = &self.as_slice();
let rights = &other.as_ref();
let left = LowercaseIterator::new(lefts);
Expand All @@ -63,21 +54,16 @@ impl Bstr {
}

/// Compare trimmed bstr with the given slice, ingnoring ascii case.
pub fn cmp_nocase_trimmed<B: AsRef<[u8]>>(&self, other: B) -> Ordering {
pub(crate) fn cmp_nocase_trimmed<B: AsRef<[u8]>>(&self, other: B) -> Ordering {
let lefts = &self.trim_with(|c| c.is_ascii_whitespace());
let rights = &other.as_ref();
let left = LowercaseIterator::new(lefts);
let right = LowercaseIterator::new(rights);
left.cmp(right)
}

/// Return true if self is equal to other ignoring ascii case
pub fn eq_nocase<B: AsRef<[u8]>>(&self, other: B) -> bool {
self.cmp_nocase(other) == Ordering::Equal
}

/// Case insensitive comparison between self and other, ignoring any zeros in self
pub fn cmp_nocase_nozero<B: AsRef<[u8]>>(&self, other: B) -> Ordering {
pub(crate) fn cmp_nocase_nozero<B: AsRef<[u8]>>(&self, other: B) -> Ordering {
let lefts = &self.as_slice();
let rights = &other.as_ref();
let left = LowercaseNoZeroIterator::new(lefts);
Expand All @@ -86,37 +72,34 @@ impl Bstr {
}

/// Case insensitive comparison between trimmed self and other, ignoring any zeros in self
pub fn cmp_nocase_nozero_trimmed<B: AsRef<[u8]>>(&self, other: B) -> Ordering {
pub(crate) fn cmp_nocase_nozero_trimmed<B: AsRef<[u8]>>(&self, other: B) -> Ordering {
let lefts = &self.trim();
let rights = &other.as_ref();
let left = LowercaseNoZeroIterator::new(lefts);
let right = LowercaseIterator::new(rights);
left.cmp(right)
}

/// Return true if self is equal to other, ignoring ascii case and zeros in self
pub fn eq_nocase_nozero<B: AsRef<[u8]>>(&self, other: B) -> bool {
self.cmp_nocase_nozero(other) == Ordering::Equal
}

/// Extend this bstr with the given slice
pub fn add<B: AsRef<[u8]>>(&mut self, other: B) {
pub(crate) fn add<B: AsRef<[u8]>>(&mut self, other: B) {
self.extend_from_slice(other.as_ref())
}

/// Extend the bstr as much as possible without growing
pub fn add_noex<B: AsRef<[u8]>>(&mut self, other: B) {
#[cfg(test)]
pub(crate) fn add_noex<B: AsRef<[u8]>>(&mut self, other: B) {
let len = std::cmp::min(self.capacity() - self.len(), other.as_ref().len());
self.add(&other.as_ref()[..len]);
}

/// Return true if this bstr starts with other
pub fn starts_with<B: AsRef<[u8]>>(&self, other: B) -> bool {
#[cfg(test)]
pub(crate) fn starts_with<B: AsRef<[u8]>>(&self, other: B) -> bool {
self.as_slice().starts_with(other.as_ref())
}

/// Return true if this bstr starts with other, ignoring ascii case
pub fn starts_with_nocase<B: AsRef<[u8]>>(&self, other: B) -> bool {
pub(crate) fn starts_with_nocase<B: AsRef<[u8]>>(&self, other: B) -> bool {
if self.len() < other.as_ref().len() {
return false;
}
Expand All @@ -125,20 +108,21 @@ impl Bstr {
}

/// Find the index of the given slice
pub fn index_of<B: AsRef<[u8]>>(&self, other: B) -> Option<usize> {
#[cfg(test)]
pub(crate) fn index_of<B: AsRef<[u8]>>(&self, other: B) -> Option<usize> {
self.find(other.as_ref())
}

/// Find the index of the given slice ignoring ascii case
pub fn index_of_nocase<B: AsRef<[u8]>>(&self, other: B) -> Option<usize> {
pub(crate) fn index_of_nocase<B: AsRef<[u8]>>(&self, other: B) -> Option<usize> {
let src = self.as_slice();
let mut haystack = LowercaseIterator::new(&src);
let needle = other.as_ref().to_ascii_lowercase();
haystack.index_of(&needle)
}

/// Find the index of the given slice ignoring ascii case and any zeros in self
pub fn index_of_nocase_nozero<B: AsRef<[u8]>>(&self, other: B) -> Option<usize> {
pub(crate) fn index_of_nocase_nozero<B: AsRef<[u8]>>(&self, other: B) -> Option<usize> {
let src = self.as_slice();
let mut haystack = LowercaseNoZeroIterator::new(&src);
let needle = other.as_ref().to_ascii_lowercase();
Expand Down
12 changes: 6 additions & 6 deletions rust/htp/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Default for Connection {
impl Connection {
/// Opens a connection. This function will essentially only store the provided data
/// for future reference.
pub fn open(
pub(crate) fn open(
&mut self, client_addr: Option<IpAddr>, client_port: Option<u16>,
server_addr: Option<IpAddr>, server_port: Option<u16>, timestamp: Option<OffsetDateTime>,
) {
Expand All @@ -83,25 +83,25 @@ impl Connection {
}

/// Closes the connection.
pub fn close(&mut self, timestamp: Option<OffsetDateTime>) {
pub(crate) fn close(&mut self, timestamp: Option<OffsetDateTime>) {
// Update timestamp.
if let Some(timestamp) = timestamp {
self.close_timestamp = timestamp;
}
}

/// Keeps track of inbound packets and data.
pub fn track_inbound_data(&mut self, len: usize) {
pub(crate) fn track_inbound_data(&mut self, len: usize) {
self.request_data_counter = (self.request_data_counter).wrapping_add(len as u64);
}

/// Keeps track of outbound packets and data.
pub fn track_outbound_data(&mut self, len: usize) {
pub(crate) fn track_outbound_data(&mut self, len: usize) {
self.response_data_counter = (self.response_data_counter).wrapping_add(len as u64);
}

/// Return the log channel sender
pub fn get_sender(&self) -> &Sender<Message> {
pub(crate) fn get_sender(&self) -> &Sender<Message> {
&self.log_channel.0
}

Expand All @@ -115,7 +115,7 @@ impl Connection {
}

/// Returns the next logged message received by the log channel
pub fn get_next_log(&self) -> Option<Log> {
pub(crate) fn get_next_log(&self) -> Option<Log> {
self.log_channel
.1
.try_recv()
Expand Down
50 changes: 17 additions & 33 deletions rust/htp/src/decompressors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,6 @@ pub struct Options {
}

impl Options {
/// Get the lzma memlimit.
///
/// A value of 0 indicates that lzma is disabled.
pub fn get_lzma_memlimit(&self) -> usize {
if let Some(options) = self.lzma {
options.memlimit.unwrap_or(0)
} else {
0
}
}

/// Set the lzma memlimit.
///
/// A value of 0 will disable lzma.
Expand All @@ -67,17 +56,17 @@ impl Options {
}

/// Configures the maximum layers passed to lzma-rs.
pub fn set_lzma_layers(&mut self, layers: Option<u32>) {
pub(crate) fn set_lzma_layers(&mut self, layers: Option<u32>) {
self.lzma_layers = layers;
}

/// Gets the maximum layers passed to lzma-rs.
pub fn get_lzma_layers(&self) -> Option<u32> {
pub(crate) fn get_lzma_layers(&self) -> Option<u32> {
self.lzma_layers
}

/// Get the compression bomb limit.
pub fn get_bomb_limit(&self) -> u64 {
pub(crate) fn get_bomb_limit(&self) -> u64 {
self.bomb_limit
}

Expand All @@ -87,7 +76,7 @@ impl Options {
}

/// Get the bomb ratio.
pub fn get_bomb_ratio(&self) -> u64 {
pub(crate) fn get_bomb_ratio(&self) -> u64 {
self.bomb_ratio
}

Expand All @@ -97,7 +86,7 @@ impl Options {
}

/// Get the compression time limit in microseconds.
pub fn get_time_limit(&self) -> u32 {
pub(crate) fn get_time_limit(&self) -> u32 {
self.time_limit
}

Expand All @@ -107,22 +96,17 @@ impl Options {
}

/// Get the time test frequency.
pub fn get_time_test_freq(&self) -> u32 {
pub(crate) fn get_time_test_freq(&self) -> u32 {
self.time_test_freq
}

/// Set the time test frequency.
pub fn set_time_test_freq(&mut self, time_test_freq: u32) {
self.time_test_freq = time_test_freq;
}

/// Get the decompression layer limit.
pub fn get_layer_limit(&self) -> Option<u32> {
pub(crate) fn get_layer_limit(&self) -> Option<u32> {
self.layer_limit
}

/// Set the decompression layer limit.
pub fn set_layer_limit(&mut self, layer_limit: Option<u32>) {
pub(crate) fn set_layer_limit(&mut self, layer_limit: Option<u32>) {
self.layer_limit = layer_limit;
}
}
Expand All @@ -146,7 +130,7 @@ impl Default for Options {

/// Describes a decompressor that is able to restart and passthrough data.
/// Actual decompression is done using the `Write` trait.
pub trait Decompress: Write {
pub(crate) trait Decompress: Write {
/// Restarts the decompressor to try the same one again or a different one.
fn restart(&mut self) -> std::io::Result<()>;

Expand All @@ -160,14 +144,14 @@ pub trait Decompress: Write {
}

/// Type alias for callback function.
pub type CallbackFn = Box<dyn FnMut(Option<&[u8]>) -> Result<usize, std::io::Error>>;
pub(crate) type CallbackFn = Box<dyn FnMut(Option<&[u8]>) -> Result<usize, std::io::Error>>;

/// Simple wrapper around a closure to chain it to the other decompressors
pub struct CallbackWriter(CallbackFn);

impl CallbackWriter {
/// Create a new CallbackWriter.
pub fn new(cbk: CallbackFn) -> Self {
pub(crate) fn new(cbk: CallbackFn) -> Self {
CallbackWriter(cbk)
}
}
Expand Down Expand Up @@ -303,7 +287,7 @@ impl Decompressor {

/// Stops the decompression timer, updates and returns the time spent
/// decompressing in microseconds (usec).
pub fn timer_reset(&mut self) -> Option<u64> {
pub(crate) fn timer_reset(&mut self) -> Option<u64> {
let now = Instant::now();
if let Some(time_before) = self.time_before.replace(now) {
// it is unlikely that more than 2^64 will be spent on a single stream
Expand All @@ -317,13 +301,13 @@ impl Decompressor {
}

/// Increments the number of times the callback was called.
pub fn callback_inc(&mut self) -> u32 {
pub(crate) fn callback_inc(&mut self) -> u32 {
self.nb_callbacks = self.nb_callbacks.wrapping_add(1);
self.nb_callbacks
}

/// Returns the time spent decompressing in microseconds (usec).
pub fn time_spent(&self) -> u64 {
pub(crate) fn time_spent(&self) -> u64 {
self.time_spent
}

Expand All @@ -344,12 +328,12 @@ impl Decompressor {

/// Notify decompressors that the end of stream as reached. This is equivalent
/// to sending a NULL data pointer.
pub fn finish(&mut self) -> std::io::Result<()> {
pub(crate) fn finish(&mut self) -> std::io::Result<()> {
self.inner.finish()
}

/// Set this decompressor to passthrough
pub fn set_passthrough(&mut self, passthrough: bool) {
pub(crate) fn set_passthrough(&mut self, passthrough: bool) {
self.inner.set_passthrough(passthrough)
}
}
Expand All @@ -365,7 +349,7 @@ impl std::fmt::Debug for Decompressor {

/// Trait that represents the decompression writers (gzip, deflate, etc.) and
/// methods needed to write to a temporary buffer.
pub trait BufWriter: Write {
pub(crate) trait BufWriter: Write {
/// Get a mutable reference to the buffer.
fn get_mut(&mut self) -> Option<&mut Cursor<Box<[u8]>>>;
/// Notify end of data.
Expand Down
Loading

0 comments on commit e512c24

Please sign in to comment.