2021-01-20 20:46:22 +00:00
|
|
|
//! String type used by SWF files.
|
2021-01-18 21:17:17 +00:00
|
|
|
|
2021-01-20 20:46:22 +00:00
|
|
|
pub use encoding_rs::{Encoding, SHIFT_JIS, UTF_8, WINDOWS_1252};
|
2021-01-18 21:17:17 +00:00
|
|
|
use std::{borrow::Cow, fmt};
|
|
|
|
|
2021-01-20 20:46:22 +00:00
|
|
|
/// `SwfStr` is the string type returned by SWF parsing functions.
|
|
|
|
/// `SwfStr` is a bstr-like type analogous to `str`:
|
|
|
|
/// * The encoding depends on the SWF version (UTF-8 for SWF6 and higher).
|
|
|
|
/// Use `Reader::encoding` or `SwfStr::encoding_for_version` to get the
|
|
|
|
/// proper encoding.
|
2021-01-18 21:17:17 +00:00
|
|
|
/// * Invalid data for any particular encoding is allowed;
|
|
|
|
/// any conversions to std::String will be lossy for invalid data.
|
|
|
|
/// To convert this to a standard Rust string, use `SwfStr::to_str_lossy`.
|
2021-01-20 20:46:22 +00:00
|
|
|
#[derive(Eq, PartialEq)]
|
|
|
|
#[repr(transparent)]
|
|
|
|
pub struct SwfStr {
|
2021-01-18 21:17:17 +00:00
|
|
|
/// The string bytes.
|
2021-01-20 20:46:22 +00:00
|
|
|
string: [u8],
|
2021-01-18 21:17:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 20:46:22 +00:00
|
|
|
impl SwfStr {
|
2021-01-18 21:17:17 +00:00
|
|
|
/// Create a new `SwfStr` from a byte slice with a given encoding.
|
|
|
|
/// The string will be truncated if a null byte is encountered.
|
|
|
|
/// The data is not required to be valid for the given encoding.
|
|
|
|
#[inline]
|
2021-01-20 20:46:22 +00:00
|
|
|
pub fn from_bytes(string: &[u8]) -> &Self {
|
|
|
|
unsafe { &*(string as *const [u8] as *const Self) }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn from_bytes_null_terminated(string: &[u8]) -> &Self {
|
2021-01-20 04:18:53 +00:00
|
|
|
let i = string.iter().position(|&c| c == 0).unwrap_or(string.len());
|
2021-01-20 20:46:22 +00:00
|
|
|
Self::from_bytes(&string[..i])
|
2021-01-18 21:17:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 20:46:22 +00:00
|
|
|
/// Create a new UTF-8 `SwfStr` from a Rust `str`.
|
2021-01-18 21:17:17 +00:00
|
|
|
#[inline]
|
2021-01-20 20:46:22 +00:00
|
|
|
pub fn from_utf8_str(string: &str) -> &Self {
|
|
|
|
Self::from_bytes(string.as_bytes())
|
2021-01-18 21:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new UTF-8 `SwfStr` from a Rust `str`.
|
|
|
|
/// The string will be truncated if a null byte is encountered.
|
|
|
|
#[inline]
|
2021-01-20 20:46:22 +00:00
|
|
|
pub fn from_utf8_str_null_terminated(string: &str) -> &Self {
|
|
|
|
Self::from_bytes_null_terminated(string.as_bytes())
|
2021-01-18 21:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new `SwfStr` with the given encoding from a Rust `str`.
|
|
|
|
/// The string will be re-encoded with the given encoding.
|
|
|
|
/// The string will be truncated if a null byte is encountered.
|
|
|
|
/// `None` is returned if the encoding is not lossless.
|
|
|
|
/// Intended for tests.
|
2021-01-20 20:46:22 +00:00
|
|
|
pub fn from_str_with_encoding<'a>(
|
|
|
|
string: &'a str,
|
|
|
|
encoding: &'static Encoding,
|
|
|
|
) -> Option<&'a Self> {
|
2021-01-18 21:17:17 +00:00
|
|
|
if let (Cow::Borrowed(s), _, false) = encoding.encode(&string) {
|
2021-01-20 20:46:22 +00:00
|
|
|
Some(Self::from_bytes(s))
|
2021-01-18 21:17:17 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 20:46:22 +00:00
|
|
|
/// Returns the suggested string encoding for the given SWF version.
|
|
|
|
/// For SWF version 6 and higher, this is always UTF-8.
|
|
|
|
/// For SWF version 5 and lower, this is locale-dependent,
|
|
|
|
/// and we default to WINDOWS-1252.
|
2021-01-18 21:17:17 +00:00
|
|
|
#[inline]
|
2021-01-20 20:46:22 +00:00
|
|
|
pub fn encoding_for_version(swf_version: u8) -> &'static Encoding {
|
|
|
|
if swf_version >= 6 {
|
|
|
|
UTF_8
|
|
|
|
} else {
|
|
|
|
WINDOWS_1252
|
|
|
|
}
|
2021-01-18 21:17:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 20:46:22 +00:00
|
|
|
/// Returns the byte slice of this string.
|
2021-01-18 21:17:17 +00:00
|
|
|
#[inline]
|
2021-01-20 20:46:22 +00:00
|
|
|
pub fn as_bytes(&self) -> &[u8] {
|
|
|
|
&self.string
|
2021-01-18 21:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the string has a length of zero, and `false` otherwise.
|
|
|
|
#[inline]
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.string.is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the `len` of the string in bytes.
|
|
|
|
#[inline]
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.string.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Decodes the string into a Rust UTF-8 `str`.
|
|
|
|
/// The UTF-8 replacement character will be uses for any invalid data.
|
|
|
|
#[inline]
|
2021-01-20 20:46:22 +00:00
|
|
|
pub fn to_str_lossy(&self, encoding: &'static Encoding) -> Cow<'_, str> {
|
|
|
|
encoding.decode_without_bom_handling(&self.string).0
|
2021-01-18 21:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Decodes the string into a Rust UTF-8 `String`.
|
|
|
|
/// The UTF-8 replacement character will be uses for any invalid data.
|
|
|
|
#[inline]
|
2021-01-20 20:46:22 +00:00
|
|
|
pub fn to_string_lossy(&self, encoding: &'static Encoding) -> String {
|
|
|
|
self.to_str_lossy(encoding).into_owned()
|
2021-01-18 21:17:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 20:46:22 +00:00
|
|
|
impl<'a> Default for &'a SwfStr {
|
|
|
|
fn default() -> &'a SwfStr {
|
|
|
|
SwfStr::from_bytes(&[])
|
2021-01-18 21:17:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 20:46:22 +00:00
|
|
|
impl<'a> From<&'a str> for &'a SwfStr {
|
|
|
|
fn from(s: &'a str) -> &'a SwfStr {
|
2021-01-20 04:18:53 +00:00
|
|
|
SwfStr::from_utf8_str(s)
|
2021-01-18 21:17:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 20:46:22 +00:00
|
|
|
impl<'a, T: ?Sized + AsRef<str>> PartialEq<T> for SwfStr {
|
2021-01-18 21:17:17 +00:00
|
|
|
fn eq(&self, other: &T) -> bool {
|
2021-01-20 20:46:22 +00:00
|
|
|
&self.string == other.as_ref().as_bytes()
|
2021-01-18 21:17:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 20:46:22 +00:00
|
|
|
impl fmt::Debug for SwfStr {
|
2021-01-18 21:17:17 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2021-01-20 20:46:22 +00:00
|
|
|
// Note that this assumes UTF-8 encoding;
|
|
|
|
// other encodings like Shift-JIS will output gibberish.
|
|
|
|
f.write_str(&self.to_str_lossy(UTF_8))
|
2021-01-18 21:17:17 +00:00
|
|
|
}
|
|
|
|
}
|