-
Notifications
You must be signed in to change notification settings - Fork 7
/
TMD.hpp
93 lines (91 loc) · 2.55 KB
/
TMD.hpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#ifndef __TMD_HPP__
#define __TMD_HPP__
#include <stdexcept>
#include "Endian.hpp"
#include "types.h"
namespace NintendoData {
class TMD {
public:
enum class SignatureType : u32 {
RSA_4096_SHA1 = 0x10000,
RSA_2048_SHA1,
ECDSA_SHA1,
RSA_4096_SHA256,
RSA_2048_SHA256,
ECDSA_SHA256
};
#pragma pack(push,1)
struct Header {
char Issuer[64];
u8 Version;
u8 CaCrlVersion;
u8 SignerCrlVersion;
u8 Reserved1;
u64 SystemVersion;
u64 TitleID;
u32 TitleType;
u16 GroupID;
u32 SaveDataSize;
u32 SRLPrivateSaveDataSize;
u32 Reserved2;
u8 SRLFlag;
u8 Reserved3[0x31];
u32 AccessRights;
u16 TitleVersion;
u16 ContentCount;
u16 BootContent;
u16 Padding;
u8 SHA256ContentInfoDigest[0x20];
};
struct ContentInfoRecords {
u16 IndexOffset;
u16 CommandCount;
u8 SHA256ContentRecords[0x20];
};
struct ContentChunkRecords {
u32 ContentId;
u16 ContentIndex;
u16 ContentType;
u64 ContentSize;
u8 SHA256[0x20];
u32 GetContentId() const {return Endian::Be(ContentId);}
u16 GetContentIndex() const {return Endian::Be(ContentIndex);}
u16 GetContentType() const {return Endian::Be(ContentType);}
u64 GetContentSize() const {return Endian::Be(ContentSize);}
bool IsOptional() const {return (GetContentType() & 0x4000) != 0;}
};
#pragma pack(pop)
private:
u8* rawtmd;
struct Header *header;
struct ContentInfoRecords *inforecords;
struct ContentChunkRecords *chunkrecords;
public:
TMD& operator=(const TMD& other);
TMD& operator=(TMD&& other);
static size_t MaxTMDSize() {
return 0x240u + sizeof(struct Header) + 64u * sizeof(struct ContentInfoRecords) + 0x10000u * sizeof(struct ContentChunkRecords);
}
u16 GetContentCount() const {
return Endian::Be(header->ContentCount);
}
const struct ContentInfoRecords &InfoRecord(const int index) const {
if(index < 0 || index > 64) std::out_of_range("TMDs only have 64 info records. Excepted range from 0 to 63.");
return inforecords[index];
}
const struct ContentChunkRecords &ChunkRecord(const int index) const {
if(index < 0 || index > GetContentCount()) std::out_of_range("TMD doesn't have that many content records.");
return chunkrecords[index];
}
bool IsStubbed() const;
size_t FullSize() const {
return (uptr)chunkrecords - (uptr)rawtmd + sizeof(struct ContentChunkRecords) * Endian::Be(header->ContentCount);
}
TMD();
TMD(const void* ptr, size_t ptrlen);
TMD(const TMD& other) : rawtmd(NULL) {*this = other;}
TMD(TMD&& other) : rawtmd(NULL) {*this = std::move(other);}
~TMD();
};
}
#endif