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

Functions

uint32_t le32 (const unsigned char *p)
uint64_t le64 (const unsigned char *p)
std::string hex2 (unsigned v)
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)

Function Documentation

◆ hex2()

std::string anonymous_namespace{partition_parser.cpp}::hex2 ( unsigned v)

Definition at line 30 of file partition_parser.cpp.

30 {
31 std::ostringstream oss;
32 oss << "0x" << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << v;
33 return oss.str();
34}

References hex2().

Referenced by hex2().

◆ le32()

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

Definition at line 12 of file partition_parser.cpp.

12 {
13 return static_cast<uint32_t>(p[0]) |
14 (static_cast<uint32_t>(p[1]) << 8) |
15 (static_cast<uint32_t>(p[2]) << 16) |
16 (static_cast<uint32_t>(p[3]) << 24);
17}

References le32().

Referenced by le32(), and parse_gpt_table().

◆ le64()

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

Definition at line 19 of file partition_parser.cpp.

19 {
20 return static_cast<uint64_t>(p[0]) |
21 (static_cast<uint64_t>(p[1]) << 8) |
22 (static_cast<uint64_t>(p[2]) << 16) |
23 (static_cast<uint64_t>(p[3]) << 24) |
24 (static_cast<uint64_t>(p[4]) << 32) |
25 (static_cast<uint64_t>(p[5]) << 40) |
26 (static_cast<uint64_t>(p[6]) << 48) |
27 (static_cast<uint64_t>(p[7]) << 56);
28}

References le64().

Referenced by le64(), and parse_gpt_table().

◆ parse_gpt_table()

bool anonymous_namespace{partition_parser.cpp}::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 )

Definition at line 38 of file partition_parser.cpp.

43 {
44 std::array<unsigned char, 92> hdr{};
45 const uint64_t header_offset = header_lba * sector_size;
46
47 if (!reader.read_at(header_offset, hdr.data(), hdr.size())) {
48 logs.push_back(std::string("GPT: header read failed at offset ") + std::to_string(header_offset));
49 return false;
50 }
51
52 if (std::string(reinterpret_cast<const char*>(hdr.data()), 8) != "EFI PART") {
53 if (is_backup) {
54 logs.push_back(trs("GPT: backup EFI PART signature not found."));
55 } else {
56 logs.push_back(trs("GPT: EFI PART header not found."));
57 }
58 return false;
59 }
60
61 const uint64_t table_lba = le64(hdr.data() + 72);
62 const uint32_t num_entries = le32(hdr.data() + 80);
63 const uint32_t entry_size = le32(hdr.data() + 84);
64
65 if (num_entries == 0 || entry_size < 128 || entry_size > 4096) {
66 logs.push_back(trs("GPT: invalid partition table."));
67 return false;
68 }
69
70 const uint64_t table_offset = table_lba * sector_size;
71
72 for (uint32_t i = 0; i < num_entries; ++i) {
73 std::vector<unsigned char> entry(entry_size);
74 if (!reader.read_at(table_offset + static_cast<uint64_t>(i) * entry_size,
75 entry.data(), entry.size())) {
76 break;
77 }
78
79 // Bug #21 fix: check both type GUID AND first/last LBA for empty entry
80 bool type_guid_zero = true;
81 for (size_t k = 0; k < 16; ++k) {
82 if (entry[k] != 0) { type_guid_zero = false; break; }
83 }
84 const uint64_t first_lba = le64(entry.data() + 32);
85 const uint64_t last_lba = le64(entry.data() + 40);
86
87 if (type_guid_zero && first_lba == 0 && last_lba == 0) {
88 continue;
89 }
90 if (last_lba < first_lba || first_lba == 0) {
91 continue;
92 }
93
94 ScanResult r;
96 r.offset_bytes = first_lba * sector_size;
97 r.size_bytes = (last_lba - first_lba + 1) * sector_size;
98 r.type = is_backup ? trs("GPT partition (backup)") : trs("GPT partition");
99 r.source = is_backup ? trs("GPT backup") : trs("GPT");
100 r.detail = is_backup
101 ? trs("GPT partition table entry (recovered from backup header)")
102 : trs("GPT partition table entry");
103 r.confidence = is_backup ? 0.92 : 0.98;
104 out.push_back(r);
105 }
106
107 return true;
108}
bool read_at(uint64_t offset, void *buffer, size_t len)
std::string trs(const char *msgid)
Definition i18n.cpp:22
@ Partition
Definition models.hpp:8
uint64_t le64(const unsigned char *p)
uint32_t le32(const unsigned char *p)

References le32(), le64(), parse_gpt_table(), Partition, DiskReader::read_at(), and trs().

Referenced by parse_gpt_table().