File size: 14,785 Bytes
f0f4f2b |
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 |
#![allow(dead_code)]
use crate::assets::LANGUAGES;
use crate::consts::{
ENCODING_MARKS, IANA_SUPPORTED, IANA_SUPPORTED_SIMILAR, RE_POSSIBLE_ENCODING_INDICATION,
UNICODE_RANGES_COMBINED, UNICODE_SECONDARY_RANGE_KEYWORD,
};
use crate::entity::Language;
use ahash::{HashSet, HashSetExt};
use encoding::label::encoding_from_whatwg_label;
use encoding::{CodecError, DecoderTrap, EncoderTrap, Encoding, EncodingRef, StringWriter};
use icu_normalizer::DecomposingNormalizer;
use unicode_names2::name;
use std::borrow::Cow;
use std::fs;
use std::path::{Path, PathBuf};
// Utils module
#[inline]
pub(crate) fn in_range(range: Option<&str>, ranges_partial: &[&str]) -> bool {
// unicode range part
if !ranges_partial.is_empty() {
if let Some(range) = range {
return ranges_partial.iter().any(|&r| range.contains(r));
}
}
false
}
#[inline]
pub(crate) fn in_description(character: char, patterns: &[&str]) -> bool {
name(character)
.is_some_and(|ucd_name| patterns.iter().any(|&s| ucd_name.to_string().contains(s)))
}
pub(crate) fn is_accentuated(character: char) -> bool {
let patterns = [
"WITH GRAVE",
"WITH ACUTE",
"WITH CEDILLA",
"WITH DIAERESIS",
"WITH CIRCUMFLEX",
"WITH TILDE",
];
in_description(character, &patterns)
}
pub(crate) fn is_unicode_range_secondary(range_name: &str) -> bool {
UNICODE_SECONDARY_RANGE_KEYWORD
.iter()
.any(|&s| range_name.contains(s))
}
// Retrieve the Unicode range official name from a single character
pub(crate) fn unicode_range(character: char) -> Option<&'static str> {
let char_code = character as u32;
UNICODE_RANGES_COMBINED
.iter()
.find(|&(_, range)| range.contains(&char_code))
.map(|(name, _)| *name)
}
pub(crate) fn range_scan(decoded_sequence: &str) -> HashSet<String> {
let (lower, upper) = decoded_sequence.chars().size_hint();
let mut result: HashSet<String> = HashSet::with_capacity(upper.unwrap_or(lower));
result.extend(
decoded_sequence
.chars()
.filter_map(|ch| unicode_range(ch).map(|r| r.to_string())),
);
result // decoded_sequence.chars().filter_map(|ch| unicode_range(&ch).map(|r| r.to_string())).collect()
}
pub(crate) fn remove_accent(ch: char) -> char {
DecomposingNormalizer::new_nfd() //initialize decomposer
.normalize(ch.to_string().as_str()) //normalize into String
.chars()
.next() // retrieve first component(unaccented char)
.unwrap_or(ch) //if fail, return the original char
}
// Verify is a specific encoding is a multi byte one based on it IANA name
pub fn is_multi_byte_encoding(name: &str) -> bool {
[
"utf-8",
"utf-16le",
"utf-16be",
"euc-jp",
"euc-kr",
"iso-2022-jp",
"gbk",
"gb18030",
"hz",
"big5",
"shift_jis",
]
.contains(&name)
}
// Try to detect multibyte encoding by signature
pub(crate) fn identify_sig_or_bom(sequence: &[u8]) -> (Option<String>, Option<&[u8]>) {
ENCODING_MARKS
.iter()
.find(|&(_, enc_sig)| sequence.starts_with(enc_sig))
.map_or((None, None), |(enc_name, enc_sig)| {
(Some((*enc_name).to_string()), Some(*enc_sig))
})
}
// Try to get standard name by alternative labels
pub fn iana_name(cp_name: &str) -> Option<&str> {
IANA_SUPPORTED
.contains(&cp_name) // first just try to search it in our list
.then_some(cp_name)
.or_else(|| {
// if not found, try to use alternative way
encoding_from_whatwg_label(cp_name).map(|enc| enc.whatwg_name().unwrap_or(enc.name()))
})
}
pub(crate) fn is_cp_similar(iana_name_a: &str, iana_name_b: &str) -> bool {
IANA_SUPPORTED_SIMILAR.contains_key(iana_name_a)
&& IANA_SUPPORTED_SIMILAR[iana_name_a].contains(&iana_name_b)
}
// Extract using ASCII-only decoder any specified encoding in the first n-bytes.
pub(crate) fn any_specified_encoding(sequence: &[u8], search_zone: usize) -> Option<String> {
encoding::all::ASCII
.decode(
&sequence[0..search_zone.min(sequence.len())],
DecoderTrap::Ignore,
)
.ok()
.and_then(|test_string| {
RE_POSSIBLE_ENCODING_INDICATION
.captures_iter(&test_string)
.map(|c| c.extract())
.find_map(|(_, [specified_encoding])| iana_name(specified_encoding))
.map(|found_iana| found_iana.to_string())
})
}
// Calculate similarity of two single byte encodings
pub(crate) fn cp_similarity(iana_name_a: &str, iana_name_b: &str) -> f32 {
// we don't want to compare multi-byte encodings
if is_multi_byte_encoding(iana_name_a) || is_multi_byte_encoding(iana_name_b) {
return 0.0;
}
if let (Some(encoder_a), Some(encoder_b)) = (
encoding_from_whatwg_label(iana_name_a),
encoding_from_whatwg_label(iana_name_b),
) {
let character_match_count = (1..255u8)
.filter(|&ch| {
let res_a = encoder_a.decode(&[ch], DecoderTrap::Ignore).ok();
let res_b = encoder_b.decode(&[ch], DecoderTrap::Ignore).ok();
res_a.is_some() && res_a == res_b //check that they aren't none and equal
})
.count();
return character_match_count as f32 / 254.0;
}
0.0 // Return 0.0 if encoders could not be retrieved.
}
// Test Decoding bytes to string with specified encoding without writing result to memory
// returns true if everything is correctly decoded, otherwise false
struct DecodeTestResult {
only_test: bool,
data: String,
}
impl StringWriter for DecodeTestResult {
fn writer_hint(&mut self, expectedlen: usize) {
if self.only_test {
return;
}
let newlen = self.data.len() + expectedlen;
self.data.reserve(newlen);
}
fn write_char(&mut self, c: char) {
if self.only_test {
return;
}
self.data.push(c);
}
fn write_str(&mut self, s: &str) {
if self.only_test {
return;
}
self.data.push_str(s);
}
}
impl DecodeTestResult {
pub fn get_buffer(&self) -> &str {
&self.data
}
}
// Decode bytes to string with specified encoding
// if is_chunk = true it will try to fix first and end bytes for multibyte encodings
pub fn decode(
input: &[u8],
from_encoding: &str,
how_process_errors: DecoderTrap,
only_test: bool,
is_chunk: bool,
) -> Result<String, String> {
let encoder = encoding_from_whatwg_label(from_encoding)
.ok_or(format!("Encoding '{}' not found", from_encoding))?;
let mut buf = DecodeTestResult {
only_test,
data: String::new(),
};
let mut err = CodecError {
upto: 0,
cause: Cow::from(String::new()),
};
let chunk_len = input.len();
let mut begin_offset: usize = 0;
let mut end_offset: usize = chunk_len;
let mut error_occured: bool;
loop {
let res = decode_to(
encoder,
&input[begin_offset..end_offset],
how_process_errors,
&mut buf,
);
error_occured = res.is_err();
if let DecoderTrap::Strict = how_process_errors {
if !is_chunk || res.is_ok() || !is_multi_byte_encoding(from_encoding) {
break;
}
err = res.unwrap_err();
if err.cause.contains("invalid sequence") {
begin_offset += 1;
} else if err.cause.contains("incomplete sequence") {
end_offset -= 1;
}
if end_offset - begin_offset < 1 || begin_offset > 3 || (chunk_len - end_offset) > 3 {
break;
}
} else {
break;
}
}
if error_occured {
return Err(format!("{} at index {}", err.cause, err.upto));
}
Ok(String::from(buf.get_buffer()))
}
// Copied implementation of decode_to from encoder lib
// (we need index of problematic chars & hacks for chunks)
fn decode_to(
encoder: EncodingRef,
input: &[u8],
trap: DecoderTrap,
ret: &mut dyn StringWriter,
) -> Result<(), CodecError> {
let mut decoder = encoder.raw_decoder();
let mut remaining = 0;
loop {
let (offset, err) = decoder.raw_feed(&input[remaining..], ret);
let unprocessed = remaining + offset;
match err {
Some(err) => {
remaining = remaining.wrapping_add_signed(err.upto);
if !trap.trap(&mut *decoder, &input[unprocessed..remaining], ret) {
return Err(err);
}
}
None => {
remaining = input.len();
if let Some(err) = decoder.raw_finish(ret) {
remaining = remaining.wrapping_add_signed(err.upto);
if !trap.trap(&mut *decoder, &input[unprocessed..remaining], ret) {
return Err(err);
}
}
if remaining >= input.len() {
return Ok(());
}
}
}
}
}
// Encode string to vec of bytes with specified encoding
pub fn encode(
input: &str,
to_encoding: &str,
how_process_errors: EncoderTrap,
) -> Result<Vec<u8>, String> {
if let Some(encoder) = encoding_from_whatwg_label(to_encoding) {
return Ok(encoder.encode(input, how_process_errors)?);
}
Err(format!("Encoding '{}' not found", to_encoding))
}
// Determine if two Unicode range seen next to each other can be considered as suspicious.
pub(crate) fn is_suspiciously_successive_range(
range_a: Option<&'static str>,
range_b: Option<&'static str>,
) -> bool {
if let (Some(range_a), Some(range_b)) = (range_a, range_b) {
if range_a == range_b
|| [range_a, range_b].iter().all(|x| x.contains("Latin"))
|| [range_a, range_b].iter().any(|x| x.contains("Emoticons"))
{
return false;
}
// Latin characters can be accompanied with a combining diacritical mark
// eg. Vietnamese.
if [range_a, range_b].iter().any(|x| x.contains("Latin"))
&& [range_a, range_b].iter().any(|x| x.contains("Combining"))
{
return false;
}
// keywords intersection
let set_a: HashSet<_> = range_a.split_whitespace().collect();
let set_b: HashSet<_> = range_b.split_whitespace().collect();
if set_a
.intersection(&set_b)
.any(|elem| !UNICODE_SECONDARY_RANGE_KEYWORD.contains(elem))
{
return false;
}
// Japanese exception
let jp_ranges = ["Hiragana", "Katakana"];
match (
jp_ranges.contains(&range_a), // has_jp_a
jp_ranges.contains(&range_b), // has_jp_b
[range_a, range_b].iter().any(|x| x.contains("CJK")), // has_cjk
[range_a, range_b].iter().any(|x| x.contains("Hangul")), // has_hangul
[range_a, range_b]
.iter()
.any(|x| x.contains("Punctuation") || x.contains("Forms")), // has_punct_or_forms
[range_a, range_b].iter().any(|&x| x == "Basic Latin"), // is_any_basic_latin
) {
(true, true, _, _, _, _) // both are japanese
| (true, _, true, _, _, _) | (_, true, true, _, _, _) //either is japanese and either contains CJK
| (_, _, true, true, _, _) // either has both CJK and Hanguls
| (_, _, true, _, true, _) // either has chinese and dedicated punctuation and separators
| (_, _, _, true, _, true) // either has hangul and basic latin
=> return false,
_ => {} // All other combinations
}
}
true // if either range is none or edge cases never triggers, return true
}
// Get data for specified language
pub(crate) fn get_language_data(language: &Language) -> Result<(&'static str, bool, bool), String> {
for (iterated_language, characters, has_accents, pure_latin) in LANGUAGES.iter() {
if iterated_language == language {
return Ok((characters, *has_accents, *pure_latin));
}
}
Err(String::from("Language wasn't found"))
}
// ascii in encodings means windows-1252 codepage with supports diacritis
// because of this we will check additionally it with is_ascii method
pub(super) fn is_invalid_chunk(
decoded_chunk_result: &Result<String, String>,
encoding_iana: &str,
) -> bool {
decoded_chunk_result.is_err()
|| (encoding_iana == "ascii" && !decoded_chunk_result.as_ref().is_ok_and(|s| s.is_ascii()))
}
// Get large datasets
fn collect_large_sets(dir: &Path) -> Vec<PathBuf> {
let mut files = Vec::new();
if dir.is_dir() {
for entry in fs::read_dir(dir).unwrap() {
let path = entry.unwrap().path();
if path.is_dir() {
// Recursively collect files in subdirectories
let subdirectory_files = collect_large_sets(&path);
files.extend(subdirectory_files);
} else {
// Add the file to the list if it's a regular file
files.push(path);
}
}
}
files
}
// Get large datasets
pub fn get_large_test_datasets() -> Result<Vec<(String, Vec<String>)>, String> {
let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("src/tests/data/largesets/");
match fs::metadata(&path) {
Ok(metadata) if metadata.is_dir() => Ok(collect_large_sets(&path)
.iter()
.filter_map(|set| {
let path = set.to_str()?;
let encoding: Vec<&str> = path.split('/').collect();
let encoding: Vec<String> = encoding
.get(encoding.len().checked_sub(2)?)?
.split(',')
.map(|s| s.to_string())
.collect();
if encoding.as_slice() == ["largesets"] {
return None; // None is ignored by filter_map
}
Some((path.to_string(), encoding)) // Return the tuple for the 'result'. unpacked by filter_map
})
.collect::<Vec<(String, Vec<String>)>>()),
Ok(metadata) => Err(format!(
"Path exists but not a directory: {:?} metadata: {:?}",
path, metadata
)),
Err(err) => Err(format!(
"Cannot find large datasets at {:?} error: {}",
path, err
)),
}
}
|