Skip to content
API Documentation
BastionGuard™
PartitionParser Class Reference

#include <partition_parser.hpp>

Static Public Member Functions

static std::vector< ScanResult > parse_mbr (DiskReader &reader, std::vector< std::string > &logs)
static std::vector< ScanResult > parse_gpt (DiskReader &reader, std::vector< std::string > &logs)

Detailed Description

Definition at line 7 of file partition_parser.hpp.

Member Function Documentation

◆ parse_gpt()

std::vector< ScanResult > PartitionParser::parse_gpt ( DiskReader & reader,
std::vector< std::string > & logs )
static

Definition at line 153 of file partition_parser.cpp.

153 {
154 std::vector<ScanResult> out;
155 // Bug #8 fix: dynamic sector size (4Kn drives need 4096, not 512)
156 const uint32_t sector_size = reader.logical_sector_size() != 0
157 ? reader.logical_sector_size() : 512u;
158
159 // Primary GPT header is at LBA 1
160 const bool primary_ok = parse_gpt_table(reader, 1, sector_size, out, logs, false);
161 if (primary_ok && !out.empty()) {
162 logs.push_back(trs("GPT: valid header found."));
163 return out;
164 }
165
166 // Bug #9 fix: fall back to backup GPT header at the last sector of the disk.
167 // The backup header itself sits at the very last LBA; the entry table sits
168 // BEFORE it. We just read the header and trust its table_lba field.
169 const uint64_t disk_size = reader.size();
170 if (disk_size < static_cast<uint64_t>(sector_size) * 2) {
171 return out;
172 }
173 const uint64_t backup_header_lba = (disk_size / sector_size) - 1;
174 logs.push_back(trs("GPT: trying backup header at LBA ") + std::to_string(backup_header_lba));
175
176 std::vector<ScanResult> backup_out;
177 const bool backup_ok = parse_gpt_table(reader, backup_header_lba, sector_size,
178 backup_out, logs, true);
179 if (backup_ok && !backup_out.empty()) {
180 logs.push_back(trs("GPT: recovered partitions from backup header."));
181 return backup_out;
182 }
183
184 return out;
185}
uint32_t logical_sector_size() const
uint64_t size() const
std::string trs(const char *msgid)
Definition i18n.cpp:22
bool parse_gpt_table(DiskReader &reader, uint64_t header_lba, uint32_t sector_size, std::vector< ScanResult > &out, std::vector< std::string > &logs, bool is_backup)

References DiskReader::logical_sector_size(), DiskReader::size(), and trs().

Referenced by ScannerEngine::run_scan().

◆ parse_mbr()

std::vector< ScanResult > PartitionParser::parse_mbr ( DiskReader & reader,
std::vector< std::string > & logs )
static

Definition at line 112 of file partition_parser.cpp.

112 {
113 std::vector<ScanResult> out;
114 // Bug #8 fix: use the disk's logical sector size for partition LBA scaling
115 const uint32_t sector_size = reader.logical_sector_size() != 0
116 ? reader.logical_sector_size() : 512u;
117
118 std::vector<unsigned char> sector(sector_size);
119 if (!reader.read_at(0, sector.data(), sector.size())) {
120 logs.push_back(trs("MBR: read failed."));
121 return out;
122 }
123
124 if (sector.size() < 512 || !(sector[510] == 0x55 && sector[511] == 0xAA)) {
125 logs.push_back(trs("MBR: 0x55AA signature not found."));
126 return out;
127 }
128
129 logs.push_back(trs("MBR: valid signature found."));
130
131 for (int i = 0; i < 4; ++i) {
132 const size_t off = 446 + static_cast<size_t>(i) * 16;
133 const auto* entry = sector.data() + off;
134 const unsigned char ptype = entry[4];
135 const uint32_t start_lba = le32(entry + 8);
136 const uint32_t num_sectors = le32(entry + 12);
137 if (ptype == 0 || num_sectors == 0) {
138 continue;
139 }
140 ScanResult r;
142 r.offset_bytes = static_cast<uint64_t>(start_lba) * sector_size;
143 r.size_bytes = static_cast<uint64_t>(num_sectors) * sector_size;
144 r.type = trs("MBR ") + hex2(ptype);
145 r.source = trs("MBR");
146 r.detail = trs("MBR partition table entry");
147 r.confidence = 0.95;
148 out.push_back(r);
149 }
150 return out;
151}
bool read_at(uint64_t offset, void *buffer, size_t len)
@ Partition
Definition models.hpp:8
uint32_t le32(const unsigned char *p)

References DiskReader::logical_sector_size(), Partition, DiskReader::read_at(), and trs().

Referenced by ScannerEngine::run_scan().


The documentation for this class was generated from the following files: