Skip to content

Commit

Permalink
Replace NtfsString by U16StrLe from my nt-string crate.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinFinck committed Jun 13, 2023
1 parent 8537120 commit ea4b054
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 236 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ derive_more = "0.99.17"
displaydoc = { version = "0.2.3", default-features = false }
enumn = "0.1.3"
memoffset = "0.8.0"
nt-string = { version = "0.1.1", features = ["alloc"], default-features = false }
strum_macros = "0.24.0"
time = { version = "0.3.9", features = ["large-dates", "macros"], default-features = false, optional = true }

Expand All @@ -31,7 +32,7 @@ time = { version = "0.3.9", features = ["formatting", "large-dates", "macros"],

[features]
default = ["std"]
std = ["arrayvec/std", "binread/std", "byteorder/std", "time?/std"]
std = ["arrayvec/std", "binread/std", "byteorder/std", "nt-string/std", "time?/std"]

[[example]]
name = "ntfs-shell"
Expand Down
10 changes: 5 additions & 5 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use bitflags::bitflags;
use byteorder::{ByteOrder, LittleEndian};
use enumn::N;
use memoffset::offset_of;
use nt_string::u16strle::U16StrLe;
use strum_macros::Display;

use crate::attribute_value::{
Expand All @@ -18,7 +19,6 @@ use crate::attribute_value::{
};
use crate::error::{NtfsError, Result};
use crate::file::NtfsFile;
use crate::string::NtfsString;
use crate::structured_values::{
NtfsAttributeList, NtfsAttributeListEntries, NtfsStructuredValue,
NtfsStructuredValueFromResidentAttributeValue,
Expand Down Expand Up @@ -239,20 +239,20 @@ impl<'n, 'f> NtfsAttribute<'n, 'f> {
is_non_resident == 0
}

/// Gets the name of this NTFS Attribute (if any) and returns it wrapped in an [`NtfsString`].
/// Gets the name of this NTFS Attribute (if any) and returns it wrapped in a [`U16StrLe`].
///
/// Note that most NTFS attributes have no name and are distinguished by their types.
/// Use [`NtfsAttribute::ty`] to get the attribute type.
pub fn name(&self) -> Result<NtfsString<'f>> {
pub fn name(&self) -> Result<U16StrLe<'f>> {
if self.name_offset() == 0 || self.name_length() == 0 {
return Ok(NtfsString(&[]));
return Ok(U16StrLe(&[]));
}

self.validate_name_sizes()?;

let start = self.offset + self.name_offset() as usize;
let end = start + self.name_length();
let string = NtfsString(&self.file.record_data()[start..end]);
let string = U16StrLe(&self.file.record_data()[start..end]);

Ok(string)
}
Expand Down
7 changes: 4 additions & 3 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use binread::io::{Read, Seek, SeekFrom};
use bitflags::bitflags;
use byteorder::{ByteOrder, LittleEndian};
use memoffset::offset_of;
use nt_string::u16strle::U16StrLe;

use crate::attribute::{
NtfsAttribute, NtfsAttributeItem, NtfsAttributeType, NtfsAttributes, NtfsAttributesRaw,
Expand All @@ -19,12 +20,12 @@ use crate::index::NtfsIndex;
use crate::indexes::NtfsFileNameIndex;
use crate::ntfs::Ntfs;
use crate::record::{Record, RecordHeader};
use crate::string::{NtfsString, UpcaseOrd};
use crate::structured_values::{
NtfsFileName, NtfsFileNamespace, NtfsIndexRoot, NtfsStandardInformation,
NtfsStructuredValueFromResidentAttributeValue,
};
use crate::types::NtfsPosition;
use crate::upcase_table::UpcaseOrd;

/// A list of standardized NTFS File Record Numbers.
///
Expand Down Expand Up @@ -208,9 +209,9 @@ impl<'n> NtfsFile<'n> {

let equal = if data_stream_name.is_empty() {
// Use a simpler "comparison" that doesn't require the $UpCase table.
|_ntfs: &Ntfs, name: &NtfsString, _data_stream_name: &str| name.is_empty()
|_ntfs: &Ntfs, name: &U16StrLe, _data_stream_name: &str| name.is_empty()
} else {
|ntfs: &Ntfs, name: &NtfsString, data_stream_name: &str| {
|ntfs: &Ntfs, name: &U16StrLe, data_stream_name: &str| {
name.upcase_cmp(ntfs, &data_stream_name) == Ordering::Equal
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/indexes/file_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::index::NtfsIndexFinder;
use crate::index_entry::NtfsIndexEntry;
use crate::indexes::{NtfsIndexEntryHasFileReference, NtfsIndexEntryType};
use crate::ntfs::Ntfs;
use crate::string::UpcaseOrd;
use crate::structured_values::NtfsFileName;
use crate::upcase_table::UpcaseOrd;
use binread::io::{Read, Seek};

/// Defines the [`NtfsIndexEntryType`] for filename indexes (commonly known as "directories").
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ mod index_record;
pub mod indexes;
mod ntfs;
mod record;
mod string;
pub mod structured_values;
mod time;
mod traits;
Expand All @@ -71,6 +70,6 @@ pub use crate::index::*;
pub use crate::index_entry::*;
pub use crate::index_record::*;
pub use crate::ntfs::*;
pub use crate::string::*;
pub use crate::time::*;
pub use crate::traits::*;
pub use crate::upcase_table::*;
211 changes: 0 additions & 211 deletions src/string.rs

This file was deleted.

8 changes: 4 additions & 4 deletions src/structured_values/attribute_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use core::mem;
use arrayvec::ArrayVec;
use binread::io::{Cursor, Read, Seek, SeekFrom};
use binread::{BinRead, BinReaderExt};
use nt_string::u16strle::U16StrLe;

use crate::attribute::{NtfsAttribute, NtfsAttributeType};
use crate::attribute_value::{NtfsAttributeValue, NtfsNonResidentAttributeValue};
use crate::error::{NtfsError, Result};
use crate::file::NtfsFile;
use crate::file_reference::NtfsFileReference;
use crate::ntfs::Ntfs;
use crate::string::NtfsString;
use crate::structured_values::NtfsStructuredValue;
use crate::traits::NtfsReadSeek;
use crate::types::{NtfsPosition, Vcn};
Expand Down Expand Up @@ -228,9 +228,9 @@ impl NtfsAttributeListEntry {
self.header.lowest_vcn
}

/// Gets the attribute name and returns it wrapped in an [`NtfsString`].
pub fn name(&self) -> NtfsString {
NtfsString(&self.name)
/// Gets the attribute name and returns it wrapped in a [`U16StrLe`].
pub fn name(&self) -> U16StrLe {
U16StrLe(&self.name)
}

/// Returns the file name length, in bytes.
Expand Down
10 changes: 5 additions & 5 deletions src/structured_values/file_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use arrayvec::ArrayVec;
use binread::io::{Cursor, Read, Seek};
use binread::{BinRead, BinReaderExt};
use enumn::N;
use nt_string::u16strle::U16StrLe;

use crate::attribute::NtfsAttributeType;
use crate::attribute_value::NtfsAttributeValue;
use crate::error::{NtfsError, Result};
use crate::file_reference::NtfsFileReference;
use crate::indexes::NtfsIndexEntryKey;
use crate::string::NtfsString;
use crate::structured_values::{NtfsFileAttributeFlags, NtfsStructuredValue};
use crate::time::NtfsTime;
use crate::types::NtfsPosition;
Expand Down Expand Up @@ -206,9 +206,9 @@ impl NtfsFileName {
self.header.modification_time
}

/// Gets the file name and returns it wrapped in an [`NtfsString`].
pub fn name(&self) -> NtfsString {
NtfsString(&self.name)
/// Gets the file name and returns it wrapped in a [`U16StrLe`].
pub fn name(&self) -> U16StrLe {
U16StrLe(&self.name)
}

/// Returns the file name length, in bytes.
Expand Down Expand Up @@ -339,7 +339,7 @@ mod tests {
assert_eq!(file_name.name().to_string_lossy(), String::from("$MFT"));
assert_eq!(
file_name.name(),
NtfsString(&[b'$', 0, b'M', 0, b'F', 0, b'T', 0])
U16StrLe(&[b'$', 0, b'M', 0, b'F', 0, b'T', 0])
);
}
}
Loading

0 comments on commit ea4b054

Please sign in to comment.