/*
* rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
* This toolkit aims to be reliable, simple, performant, and standards compliant.
* Copyright (C) 2026 Luis M. Santos, M.D. <lsantos@medicalmasses.com>
* Copyright (C) 2026 MedicalMasses L.L.C. <contact@medicalmasses.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
* rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
* This toolkit aims to be reliable, simple, performant, and standards compliant.
* Copyright (C) 2026 Luis M. Santos, M.D. <lsantos@medicalmasses.com>
* Copyright (C) 2026 MedicalMasses L.L.C. <contact@medicalmasses.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
///
/// This module is specifically for RegEx based sanitization of arbitrary inputs.
///
use std::borrow::Cow;
use crate::base::RUMResult;
use crate::regex::*;
use crate::rumtk_cache_fetch;
////////////////////// Expressions /////////////////////////////////
pub const REGEX_ANSI_ESCAPE_SEQUENCES: &str = r"([\x1b\x9b](?:\][^\x07\x1b]*(?:\x07|\x1b\\)|[PX^_][^\x1b]*\x1b\\|[\[(][0-?]*[ -/]*[@-~]|[@-Z\\-_])|\x1b((\[\??\d+[hl])|([=<>a-kzNM78])|([\(\)][a-b0-2])|(\[\d{0,2}[ma-dgkjqi])|(\[\d+;\d+[hfy]?)|(\[;?[hf])|(#[3-68])|([01356]n)|(O[mlnp-z]?)|(/Z)|(\d+)|(\[\?\d;\d0c)|(\d;\dR)|([A-Z]*)|)|\\x1b((\[\??\d+[hl])|([=<>a-kzNM78])|([\(\)][a-b0-2])|(\[\d{0,2}[ma-dgkjqi])|(\[\d+;\d+[hfy]?)|(\[;?[hf])|(#[3-68])|([01356]n)|(O[mlnp-z]?)|(/Z)|(\d+)|(\[\?\d;\d0c)|(\d;\dR)|([A-Z]*)|))";
///////////////////// Expression Hashes //////////////////////////////
//////////////////// Filter Functions ////////////////////////////////
///
/// Search for pattern and replace all matches.
///
#[inline]
pub fn string_replace_all_matches<'a, 'b, 'c>(input: &'a str, expr: &'b str, replacement: &'c str) -> RUMResult<Cow<'a, str>> {
let key = hash_regex_expression(expr);
let re = rumtk_cache_fetch!(get_regex_cache(), &key, || {compile_regex(expr)})?;
Ok(re.replace_all(input, replacement))
}
///
/// Removes all ANSI escape sequences from a string.
/// ```text
/// ANSI escape sequences can be used as a vector
/// to execute arbitrary commands. The attack vector is having an application log data containing
/// a specially crafted ANSI escape sequence payload such that an administrator with the correct
/// shell terminal ends up executing the payload when inspecting the log data on the terminal.
///
/// Limitations:
/// - Highly featured shell is required with full ANSI implementation.
/// ```
///
/// See this DefCon recording for more info!
/// [DEF CON 31 - Weaponizing Plain Text ANSI Escape Sequences as a Forensic Nightmare - STÖK](https://www.youtube.com/watch?v=3T2Al3jdY38)
///
/// ## Example
/// ```
/// use rumtk_core::regex::filter_ansi_escape_sequences;
///
/// const ANSI_EXAMPLE: &str = "I want \x1b[31mRED\x1b[0m color";
///
/// assert_eq!(filter_ansi_escape_sequences(ANSI_EXAMPLE).unwrap(), "I want RED color");
/// ```
///
#[inline]
pub fn filter_ansi_escape_sequences(input: &str) -> RUMResult<Cow<str>> {
string_replace_all_matches(input, REGEX_ANSI_ESCAPE_SEQUENCES, "")
}