Skip to content
API Documentation
BastionGuard™
anonymous_namespace{ntfs_recovery.cpp} Namespace Reference

Classes

struct  DiskSpan
struct  NtfsDataRun
struct  NtfsVolumeInfo
struct  NtfsAttributeListEntry
struct  NtfsDataStream
struct  NtfsFileRecordInfo

Functions

uint16_t le16 (const unsigned char *p)
uint32_t le32 (const unsigned char *p)
uint64_t le64 (const unsigned char *p)
std::string sanitize_component (const std::string &in)
std::string decode_utf16le_ascii (const unsigned char *data, size_t byte_count)
bool lznt1_decompress (const std::vector< unsigned char > &src, size_t expected_size, std::vector< unsigned char > &dst)

Function Documentation

◆ decode_utf16le_ascii()

std::string anonymous_namespace{ntfs_recovery.cpp}::decode_utf16le_ascii ( const unsigned char * data,
size_t byte_count )
inline

Definition at line 52 of file ntfs_recovery.cpp.

52 {
53 // Function name kept for backwards compatibility, but it now produces
54 // proper UTF-8 output (the previous version replaced every non-ASCII
55 // character with '_').
56 return text_utils::utf16le_to_utf8(data, byte_count);
57}
std::string utf16le_to_utf8(const unsigned char *data, std::size_t byte_count)

References decode_utf16le_ascii(), and text_utils::utf16le_to_utf8().

Referenced by decode_utf16le_ascii(), NtfsRecoveryImpl::export_reparse_sidecar(), NtfsRecoveryImpl::parse_attribute_list_entries(), NtfsRecoveryImpl::parse_index_entries(), and NtfsRecoveryImpl::process_attributes().

◆ le16()

uint16_t anonymous_namespace{ntfs_recovery.cpp}::le16 ( const unsigned char * p)

◆ le32()

uint32_t anonymous_namespace{ntfs_recovery.cpp}::le32 ( const unsigned char * p)

Definition at line 28 of file ntfs_recovery.cpp.

28 {
29 return static_cast<uint32_t>(p[0]) |
30 (static_cast<uint32_t>(p[1]) << 8) |
31 (static_cast<uint32_t>(p[2]) << 16) |
32 (static_cast<uint32_t>(p[3]) << 24);
33}

References le32().

Referenced by NtfsRecoveryImpl::export_reparse_sidecar(), le32(), NtfsRecoveryImpl::parse_attribute_list_entries(), NtfsRecoveryImpl::parse_index_entries(), NtfsRecoveryImpl::process_attributes(), NtfsRecoveryImpl::scan_logfile(), and NtfsRecoveryImpl::walk_i30().

◆ le64()

uint64_t anonymous_namespace{ntfs_recovery.cpp}::le64 ( const unsigned char * p)

Definition at line 35 of file ntfs_recovery.cpp.

35 {
36 return static_cast<uint64_t>(p[0]) |
37 (static_cast<uint64_t>(p[1]) << 8) |
38 (static_cast<uint64_t>(p[2]) << 16) |
39 (static_cast<uint64_t>(p[3]) << 24) |
40 (static_cast<uint64_t>(p[4]) << 32) |
41 (static_cast<uint64_t>(p[5]) << 40) |
42 (static_cast<uint64_t>(p[6]) << 48) |
43 (static_cast<uint64_t>(p[7]) << 56);
44}

References le64().

Referenced by le64(), NtfsRecoveryImpl::parse_attribute_list_entries(), NtfsRecoveryImpl::parse_index_entries(), NtfsRecoveryImpl::process_attributes(), and NtfsRecoveryImpl::read_boot_sector().

◆ lznt1_decompress()

bool anonymous_namespace{ntfs_recovery.cpp}::lznt1_decompress ( const std::vector< unsigned char > & src,
size_t expected_size,
std::vector< unsigned char > & dst )

Definition at line 144 of file ntfs_recovery.cpp.

146 {
147 dst.clear();
148 size_t pos = 0;
149 while (pos + 2 <= src.size() && dst.size() < expected_size) {
150 const uint16_t header = le16(src.data() + pos);
151 pos += 2;
152 const size_t chunk_size = (header & 0x0FFF) + 1;
153 const bool is_compressed = (header & 0x8000) != 0;
154 if (pos + chunk_size > src.size()) return false;
155
156 if (!is_compressed) {
157 const size_t take = std::min(chunk_size, expected_size - dst.size());
158 dst.insert(dst.end(), src.begin() + static_cast<long>(pos), src.begin() + static_cast<long>(pos + take));
159 pos += chunk_size;
160 continue;
161 }
162
163 const size_t chunk_end = pos + chunk_size;
164 std::vector<unsigned char> chunk_out;
165 chunk_out.reserve(4096);
166 while (pos < chunk_end && chunk_out.size() < 4096) {
167 const uint8_t flags = src[pos++];
168 for (int bit = 0; bit < 8 && pos < chunk_end; ++bit) {
169 if ((flags & (1u << bit)) == 0) {
170 chunk_out.push_back(src[pos++]);
171 } else {
172 if (pos + 1 >= chunk_end) return false;
173 const uint16_t token = le16(src.data() + pos);
174 pos += 2;
175
176 int length_bits = 4;
177 while ((1u << (length_bits + 4)) < chunk_out.size() && length_bits < 12) ++length_bits;
178 const size_t length_mask = (1u << length_bits) - 1u;
179 const size_t offset = (token >> length_bits) + 1u;
180 const size_t length = (token & length_mask) + 3u;
181 if (offset == 0 || offset > chunk_out.size()) return false;
182 for (size_t i = 0; i < length && chunk_out.size() < 4096; ++i) {
183 chunk_out.push_back(chunk_out[chunk_out.size() - offset]);
184 }
185 }
186 if (chunk_out.size() >= 4096) break;
187 }
188 }
189 const size_t take = std::min(chunk_out.size(), expected_size - dst.size());
190 dst.insert(dst.end(), chunk_out.begin(), chunk_out.begin() + static_cast<long>(take));
191 if (chunk_out.size() < 4096 && dst.size() < expected_size) {
192 dst.resize(std::min(expected_size, dst.size() + (4096 - chunk_out.size())), 0);
193 }
194 }
195 if (dst.size() < expected_size) dst.resize(expected_size, 0);
196 return true;
197}
uint16_t le16(const unsigned char *p)

References le16(), and lznt1_decompress().

Referenced by NtfsRecoveryImpl::decode_compressed_stream(), and lznt1_decompress().

◆ sanitize_component()

std::string anonymous_namespace{ntfs_recovery.cpp}::sanitize_component ( const std::string & in)
inline