| 1 | //! Contains UTF-8 validation implementations. |
| 2 | |
| 3 | type Utf8ErrorCompat = crate::compat::Utf8Error; |
| 4 | type Utf8ErrorBasic = crate::basic::Utf8Error; |
| 5 | |
| 6 | #[allow(unused_macros)] |
| 7 | #[macro_use] |
| 8 | mod algorithm; |
| 9 | |
| 10 | pub(crate) mod helpers; |
| 11 | |
| 12 | // UTF-8 validation function types |
| 13 | |
| 14 | #[allow(dead_code)] |
| 15 | type ValidateUtf8Fn = unsafe fn(input: &[u8]) -> Result<(), Utf8ErrorBasic>; |
| 16 | |
| 17 | #[allow(dead_code)] |
| 18 | type ValidateUtf8CompatFn = unsafe fn(input: &[u8]) -> Result<(), Utf8ErrorCompat>; |
| 19 | |
| 20 | // x86 implementation |
| 21 | |
| 22 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 23 | pub(crate) mod x86; |
| 24 | |
| 25 | /// Fn needed instead of re-import, otherwise not inlined in non-std case |
| 26 | #[allow(clippy::inline_always)] |
| 27 | #[inline(always)] |
| 28 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 29 | pub(super) unsafe fn validate_utf8_basic(input: &[u8]) -> Result<(), Utf8ErrorBasic> { |
| 30 | x86::validate_utf8_basic(input) |
| 31 | } |
| 32 | |
| 33 | /// Fn needed instead of re-import, otherwise not inlined in non-std case |
| 34 | #[allow(clippy::inline_always)] |
| 35 | #[inline(always)] |
| 36 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 37 | pub(super) unsafe fn validate_utf8_compat(input: &[u8]) -> Result<(), Utf8ErrorCompat> { |
| 38 | x86::validate_utf8_compat(input) |
| 39 | } |
| 40 | |
| 41 | // aarch64 implementation |
| 42 | |
| 43 | #[cfg(target_arch = "aarch64")] |
| 44 | pub(crate) mod aarch64; |
| 45 | |
| 46 | #[cfg(target_arch = "aarch64")] |
| 47 | pub(super) use aarch64::validate_utf8_basic; |
| 48 | |
| 49 | #[cfg(target_arch = "aarch64")] |
| 50 | pub(super) use aarch64::validate_utf8_compat; |
| 51 | |
| 52 | // wasm32 implementation |
| 53 | |
| 54 | #[cfg(target_arch = "wasm32")] |
| 55 | pub(crate) mod wasm32; |
| 56 | |
| 57 | #[cfg(target_arch = "wasm32")] |
| 58 | pub(super) use wasm32::validate_utf8_basic; |
| 59 | |
| 60 | #[cfg(target_arch = "wasm32")] |
| 61 | pub(super) use wasm32::validate_utf8_compat; |
| 62 | |
| 63 | // fallback for unsupported architectures |
| 64 | |
| 65 | #[cfg(not(any( |
| 66 | target_arch = "x86", |
| 67 | target_arch = "x86_64", |
| 68 | target_arch = "aarch64", |
| 69 | target_arch = "wasm32" |
| 70 | )))] |
| 71 | pub(super) use validate_utf8_basic_fallback as validate_utf8_basic; |
| 72 | |
| 73 | #[cfg(not(any( |
| 74 | target_arch = "x86", |
| 75 | target_arch = "x86_64", |
| 76 | target_arch = "aarch64", |
| 77 | target_arch = "wasm32" |
| 78 | )))] |
| 79 | pub(super) use validate_utf8_compat_fallback as validate_utf8_compat; |
| 80 | |
| 81 | // fallback method implementations |
| 82 | |
| 83 | #[inline] |
| 84 | #[allow(dead_code)] |
| 85 | pub(crate) fn validate_utf8_basic_fallback(input: &[u8]) -> Result<(), Utf8ErrorBasic> { |
| 86 | match core::str::from_utf8(input) { |
| 87 | Ok(_) => Ok(()), |
| 88 | Err(_) => Err(Utf8ErrorBasic {}), |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | #[inline] |
| 93 | #[allow(dead_code)] |
| 94 | pub(crate) fn validate_utf8_compat_fallback(input: &[u8]) -> Result<(), Utf8ErrorCompat> { |
| 95 | helpers::validate_utf8_at_offset(input, 0) |
| 96 | } |