void GptPartitionTable::set_name(utils::ByteBuffer& bb, uint64_t _offset, const std::string& name) { std::size_t offset = std::size_t(_offset); auto chars_to_write = std::min(unsigned(name.size()), GPT_PARTITION_NAME_SIZE / 2); for (unsigned i = 0; i < chars_to_write; ++i) { bb.set_le16(offset + i * 2, uint16_t(name[i])); } // fill remaining bytes with zeros for (unsigned i = chars_to_write; i < GPT_PARTITION_NAME_SIZE / 2; ++i) { bb.set_le16(offset + i * 2, 0); } }
void GptPartitionTable::set_guid(utils::ByteBuffer& bb, uint64_t _offset, const std::string& guid) { std::size_t offset = std::size_t(_offset); static constexpr unsigned EXPECTED_MATCHES = 6; static constexpr int HEX_BASE = 16; std::regex expr("([0-9a-fA-F]{8})-([0-9a-fA-F]{4})-([0-9a-fA-F]{4})-([0-9a-fA-F]{4})-([0-9a-fA-F]{12})"); std::smatch match{}; if (!std::regex_match(guid, match, expr) || match.size() != EXPECTED_MATCHES) { throw std::runtime_error(std::string{"Invalid GUID format: "} + guid); } bb.set_le32(offset + 0, uint32_t(std::stoul(match[1].str(), nullptr, HEX_BASE))); bb.set_le16(offset + 4, uint16_t(std::stoul(match[2].str(), nullptr, HEX_BASE))); bb.set_le16(offset + 6, uint16_t(std::stoul(match[3].str(), nullptr, HEX_BASE))); bb.set_be16(offset + 8, uint16_t(std::stoul(match[4].str(), nullptr, HEX_BASE))); bb.set_be48(offset + 10, std::stoull(match[5].str(), nullptr, HEX_BASE)); }