forked from blastrock/pkgj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsfo.cpp
More file actions
51 lines (40 loc) · 1.35 KB
/
Copy pathsfo.cpp
File metadata and controls
51 lines (40 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "sfo.hpp"
#include <fmt/format.h>
#include <stdexcept>
static constexpr uint32_t SFO_MAGIC = 0x46535000;
struct SfoHeader
{
uint32_t magic;
uint32_t version;
uint32_t keyofs;
uint32_t valofs;
uint32_t count;
} __attribute__((packed));
struct SfoEntry
{
uint16_t nameofs;
uint8_t alignment;
uint8_t type;
uint32_t valsize;
uint32_t totalsize;
uint32_t dataofs;
} __attribute__((packed));
std::string pkgi_sfo_get_string(
const uint8_t* buffer, size_t size, const std::string& name)
{
if (size < sizeof(SfoHeader))
throw std::runtime_error("truncated param.sfo");
const SfoHeader* header = reinterpret_cast<const SfoHeader*>(buffer);
const SfoEntry* entries =
reinterpret_cast<const SfoEntry*>(buffer + sizeof(SfoHeader));
if (header->magic != SFO_MAGIC)
throw std::runtime_error("can't parse SFO, invalid magic");
if (size < sizeof(SfoHeader) + header->count * sizeof(SfoEntry))
throw std::runtime_error("truncated param.sfo");
for (uint32_t i = 0; i < header->count; i++)
if (std::string(reinterpret_cast<const char*>(
buffer + header->keyofs + entries[i].nameofs)) == name)
return std::string(reinterpret_cast<const char*>(
buffer + header->valofs + entries[i].dataofs));
return {};
}