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

Classes

struct  Range

Functions

bool overlaps (const std::vector< Range > &ranges, uint64_t start, uint64_t end)
bool is_ntfs (const std::array< unsigned char, 512 > &b)
bool is_fat32 (const std::array< unsigned char, 512 > &b)
bool is_exfat (const std::array< unsigned char, 512 > &b)
bool looks_ext4 (DiskReader &reader, uint64_t offset)
bool looks_btrfs (DiskReader &reader, uint64_t offset)
bool looks_xfs (DiskReader &reader, uint64_t offset)
uint64_t xfs_size_bytes (DiskReader &reader, uint64_t offset)
bool looks_hfs (DiskReader &reader, uint64_t offset)
uint64_t hfs_size_bytes (DiskReader &reader, uint64_t offset)
uint64_t le64 (const unsigned char *p)
uint32_t le32 (const unsigned char *p)
uint64_t ntfs_size_bytes (const std::array< unsigned char, 512 > &b)
uint64_t fat32_size_bytes (const std::array< unsigned char, 512 > &b)
uint64_t exfat_size_bytes (const std::array< unsigned char, 512 > &b)
uint64_t ext4_size_bytes (DiskReader &reader, uint64_t offset)
uint64_t btrfs_size_bytes (DiskReader &reader, uint64_t offset)

Variables

constexpr uint64_t MiB = 1024ull * 1024ull
constexpr uint64_t GiB = 1024ull * 1024ull * 1024ull
constexpr uint64_t SECTOR_SIZE = 512ull
constexpr uint64_t SCAN_STRIDE = 1ull * MiB
constexpr uint64_t DEFAULT_MAX_SCAN = 4ull * 1024ull * GiB
constexpr uint64_t HARD_MAX_SCAN = 64ull * 1024ull * GiB

Function Documentation

◆ btrfs_size_bytes()

uint64_t anonymous_namespace{lost_partition_finder.cpp}::btrfs_size_bytes ( DiskReader & reader,
uint64_t offset )

Definition at line 151 of file lost_partition_finder.cpp.

151 {
152 std::array<unsigned char, 8> total{};
153 // btrfs superblock at +0x10000, total_bytes is at offset 0x70 within sb
154 if (!reader.read_at(offset + 0x10000 + 0x70, total.data(), total.size())) return 0;
155 return le64(total.data());
156}
bool read_at(uint64_t offset, void *buffer, size_t len)

References btrfs_size_bytes(), le64(), and DiskReader::read_at().

Referenced by btrfs_size_bytes().

◆ exfat_size_bytes()

uint64_t anonymous_namespace{lost_partition_finder.cpp}::exfat_size_bytes ( const std::array< unsigned char, 512 > & b)

Definition at line 135 of file lost_partition_finder.cpp.

135 {
136 return le64(b.data() + 72) * SECTOR_SIZE;
137}

References exfat_size_bytes(), le64(), and SECTOR_SIZE.

Referenced by exfat_size_bytes().

◆ ext4_size_bytes()

uint64_t anonymous_namespace{lost_partition_finder.cpp}::ext4_size_bytes ( DiskReader & reader,
uint64_t offset )

Definition at line 140 of file lost_partition_finder.cpp.

140 {
141 std::array<unsigned char, 1024> sb{};
142 if (!reader.read_at(offset + 1024, sb.data(), sb.size())) return 0;
143 const uint32_t blocks_lo = le32(sb.data() + 0x04);
144 const uint32_t log_block_size = le32(sb.data() + 0x18);
145 const uint64_t block_size = 1024ull << log_block_size;
146 if (block_size < 1024 || block_size > 65536) return 0;
147 return static_cast<uint64_t>(blocks_lo) * block_size;
148}

References ext4_size_bytes(), le32(), and DiskReader::read_at().

Referenced by ext4_size_bytes().

◆ fat32_size_bytes()

uint64_t anonymous_namespace{lost_partition_finder.cpp}::fat32_size_bytes ( const std::array< unsigned char, 512 > & b)

Definition at line 130 of file lost_partition_finder.cpp.

130 {
131 const uint32_t sectors32 = le32(b.data() + 32);
132 return static_cast<uint64_t>(sectors32) * SECTOR_SIZE;
133}

References fat32_size_bytes(), le32(), and SECTOR_SIZE.

Referenced by fat32_size_bytes().

◆ hfs_size_bytes()

uint64_t anonymous_namespace{lost_partition_finder.cpp}::hfs_size_bytes ( DiskReader & reader,
uint64_t offset )

Definition at line 97 of file lost_partition_finder.cpp.

97 {
98 std::array<unsigned char, 512> sector{};
99 if (!reader.read_at(offset + hfs::HFSPLUS_VOLUME_HEADER_OFFSET,
100 sector.data(), sector.size())) {
101 return 0;
102 }
103 hfs::VolumeHeader vh;
104 if (!hfs::parse_volume_header(sector.data(), vh)) return 0;
105 return vh.volume_size_bytes();
106}
bool parse_volume_header(const unsigned char *buf, VolumeHeader &out)
constexpr uint32_t HFSPLUS_VOLUME_HEADER_OFFSET
uint64_t volume_size_bytes() const

References hfs_size_bytes(), hfs::HFSPLUS_VOLUME_HEADER_OFFSET, hfs::parse_volume_header(), DiskReader::read_at(), and hfs::VolumeHeader::volume_size_bytes().

Referenced by hfs_size_bytes().

◆ is_exfat()

bool anonymous_namespace{lost_partition_finder.cpp}::is_exfat ( const std::array< unsigned char, 512 > & b)

Definition at line 52 of file lost_partition_finder.cpp.

52 {
53 return std::string(reinterpret_cast<const char*>(b.data() + 3), 8) == "EXFAT " &&
54 b[510] == 0x55 && b[511] == 0xAA;
55}

References is_exfat().

Referenced by is_exfat().

◆ is_fat32()

bool anonymous_namespace{lost_partition_finder.cpp}::is_fat32 ( const std::array< unsigned char, 512 > & b)

Definition at line 47 of file lost_partition_finder.cpp.

47 {
48 return std::string(reinterpret_cast<const char*>(b.data() + 82), 8) == "FAT32 " &&
49 b[510] == 0x55 && b[511] == 0xAA;
50}

References is_fat32().

Referenced by is_fat32().

◆ is_ntfs()

bool anonymous_namespace{lost_partition_finder.cpp}::is_ntfs ( const std::array< unsigned char, 512 > & b)

Definition at line 42 of file lost_partition_finder.cpp.

42 {
43 return std::string(reinterpret_cast<const char*>(b.data() + 3), 8) == "NTFS " &&
44 b[510] == 0x55 && b[511] == 0xAA;
45}

References is_ntfs().

Referenced by is_ntfs().

◆ le32()

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

Definition at line 119 of file lost_partition_finder.cpp.

119 {
120 return static_cast<uint32_t>(p[0]) |
121 (static_cast<uint32_t>(p[1]) << 8) |
122 (static_cast<uint32_t>(p[2]) << 16) |
123 (static_cast<uint32_t>(p[3]) << 24);
124}

References le32().

Referenced by ext4_size_bytes(), fat32_size_bytes(), and le32().

◆ le64()

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

Definition at line 108 of file lost_partition_finder.cpp.

108 {
109 return static_cast<uint64_t>(p[0]) |
110 (static_cast<uint64_t>(p[1]) << 8) |
111 (static_cast<uint64_t>(p[2]) << 16) |
112 (static_cast<uint64_t>(p[3]) << 24) |
113 (static_cast<uint64_t>(p[4]) << 32) |
114 (static_cast<uint64_t>(p[5]) << 40) |
115 (static_cast<uint64_t>(p[6]) << 48) |
116 (static_cast<uint64_t>(p[7]) << 56);
117}

References le64().

Referenced by btrfs_size_bytes(), exfat_size_bytes(), le64(), and ntfs_size_bytes().

◆ looks_btrfs()

bool anonymous_namespace{lost_partition_finder.cpp}::looks_btrfs ( DiskReader & reader,
uint64_t offset )

Definition at line 62 of file lost_partition_finder.cpp.

62 {
63 std::array<char, 8> sig{};
64 return reader.read_at(offset + 0x10040, sig.data(), sig.size()) && std::string(sig.data(), sig.size()) == "_BHRfS_M";
65}

References looks_btrfs(), and DiskReader::read_at().

Referenced by looks_btrfs().

◆ looks_ext4()

bool anonymous_namespace{lost_partition_finder.cpp}::looks_ext4 ( DiskReader & reader,
uint64_t offset )

Definition at line 57 of file lost_partition_finder.cpp.

57 {
58 std::array<unsigned char, 2> sig{};
59 return reader.read_at(offset + 1024 + 56, sig.data(), sig.size()) && sig[0] == 0x53 && sig[1] == 0xEF;
60}

References looks_ext4(), and DiskReader::read_at().

Referenced by looks_ext4().

◆ looks_hfs()

bool anonymous_namespace{lost_partition_finder.cpp}::looks_hfs ( DiskReader & reader,
uint64_t offset )

Definition at line 87 of file lost_partition_finder.cpp.

87 {
88 std::array<unsigned char, 4> sig{};
89 if (!reader.read_at(offset + hfs::HFSPLUS_VOLUME_HEADER_OFFSET,
90 sig.data(), sig.size())) {
91 return false;
92 }
93 const uint16_t s = static_cast<uint16_t>((sig[0] << 8) | sig[1]);
95}
constexpr uint16_t HFSX_SIGNATURE
constexpr uint16_t HFSPLUS_SIGNATURE

References hfs::HFSPLUS_SIGNATURE, hfs::HFSPLUS_VOLUME_HEADER_OFFSET, hfs::HFSX_SIGNATURE, looks_hfs(), and DiskReader::read_at().

Referenced by looks_hfs().

◆ looks_xfs()

bool anonymous_namespace{lost_partition_finder.cpp}::looks_xfs ( DiskReader & reader,
uint64_t offset )

Definition at line 69 of file lost_partition_finder.cpp.

69 {
70 std::array<unsigned char, 4> sig{};
71 if (!reader.read_at(offset, sig.data(), sig.size())) return false;
72 return sig[0] == 'X' && sig[1] == 'F' && sig[2] == 'S' && sig[3] == 'B';
73}

References looks_xfs(), and DiskReader::read_at().

Referenced by looks_xfs().

◆ ntfs_size_bytes()

uint64_t anonymous_namespace{lost_partition_finder.cpp}::ntfs_size_bytes ( const std::array< unsigned char, 512 > & b)

Definition at line 126 of file lost_partition_finder.cpp.

126 {
127 return le64(b.data() + 40) * SECTOR_SIZE;
128}

References le64(), ntfs_size_bytes(), and SECTOR_SIZE.

Referenced by ntfs_size_bytes().

◆ overlaps()

bool anonymous_namespace{lost_partition_finder.cpp}::overlaps ( const std::vector< Range > & ranges,
uint64_t start,
uint64_t end )

Definition at line 33 of file lost_partition_finder.cpp.

33 {
34 for (const auto& r : ranges) {
35 if (start < r.end && end > r.start) {
36 return true;
37 }
38 }
39 return false;
40}

References overlaps().

Referenced by overlaps().

◆ xfs_size_bytes()

uint64_t anonymous_namespace{lost_partition_finder.cpp}::xfs_size_bytes ( DiskReader & reader,
uint64_t offset )

Definition at line 77 of file lost_partition_finder.cpp.

77 {
78 std::array<unsigned char, 512> sector{};
79 if (!reader.read_at(offset, sector.data(), sector.size())) return 0;
81 if (!xfs::parse_superblock(sector.data(), sb)) return 0;
82 return sb.volume_size_bytes();
83}
bool parse_superblock(const unsigned char *buf, Superblock &out)
uint64_t volume_size_bytes() const

References xfs::parse_superblock(), DiskReader::read_at(), xfs::Superblock::volume_size_bytes(), and xfs_size_bytes().

Referenced by xfs_size_bytes().

Variable Documentation

◆ DEFAULT_MAX_SCAN

uint64_t anonymous_namespace{lost_partition_finder.cpp}::DEFAULT_MAX_SCAN = 4ull * 1024ull * GiB
constexpr

Definition at line 25 of file lost_partition_finder.cpp.

◆ GiB

uint64_t anonymous_namespace{lost_partition_finder.cpp}::GiB = 1024ull * 1024ull * 1024ull
constexpr

Definition at line 13 of file lost_partition_finder.cpp.

◆ HARD_MAX_SCAN

uint64_t anonymous_namespace{lost_partition_finder.cpp}::HARD_MAX_SCAN = 64ull * 1024ull * GiB
constexpr

Definition at line 26 of file lost_partition_finder.cpp.

◆ MiB

uint64_t anonymous_namespace{lost_partition_finder.cpp}::MiB = 1024ull * 1024ull
constexpr

Definition at line 12 of file lost_partition_finder.cpp.

◆ SCAN_STRIDE

uint64_t anonymous_namespace{lost_partition_finder.cpp}::SCAN_STRIDE = 1ull * MiB
constexpr

Definition at line 20 of file lost_partition_finder.cpp.

◆ SECTOR_SIZE

uint64_t anonymous_namespace{lost_partition_finder.cpp}::SECTOR_SIZE = 512ull
constexpr

Definition at line 14 of file lost_partition_finder.cpp.

Referenced by exfat_size_bytes(), fat32_size_bytes(), and ntfs_size_bytes().