Repository navigation
Expand file tree
/
Copy pathPJ64KeyGen.cpp
More file actions
231 lines (210 loc) · 7 KB
/
Copy pathPJ64KeyGen.cpp
File metadata and controls
231 lines (210 loc) · 7 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// PJ64KeyGen - console frontend for the Project64 supporter key generator.
#include "PJ64KeyCore.h"
#include <stdio.h>
#include <string>
#include <vector>
using namespace pj64;
static void PrintUsage(void)
{
printf("PJ64KeyGen - Project64 supporter key generator\n\n");
printf("Usage: PJ64KeyGen [options]\n\n");
printf(" (no options) validate the existing key, create one if missing/invalid\n");
printf(" -i show machine id and current key status\n");
printf(" -t self test (builds and verifies a key, does not touch the registry)\n");
printf(" -f force generate a new key, overwriting any existing one\n");
printf(" -d delete the stored key\n");
printf(" -b <file> back up the stored key blob to <file>\n");
printf(" -r <file> restore the key blob from <file>\n");
printf(" -n <name> supporter name (default: windows user name)\n");
printf(" -e <email> supporter email\n");
printf(" -c <code> supporter code\n");
printf(" -h this help\n");
}
int main(int argc, char *argv[])
{
bool Force = false;
bool ShowInfo = false;
bool SelfTest = false;
bool DoDelete = false;
bool HaveName = false, HaveEmail = false, HaveCode = false;
const char *BackupPath = nullptr;
const char *RestorePath = nullptr;
std::string Name, Email, Code;
for (int i = 1; i < argc; i++)
{
std::string Arg = argv[i];
if (Arg == "-f" || Arg == "--force")
{
Force = true;
}
else if (Arg == "-i" || Arg == "--info")
{
ShowInfo = true;
}
else if (Arg == "-t" || Arg == "--test")
{
SelfTest = true;
}
else if (Arg == "-d" || Arg == "--delete")
{
DoDelete = true;
}
else if ((Arg == "-b" || Arg == "--backup") && i + 1 < argc)
{
BackupPath = argv[++i];
}
else if ((Arg == "-r" || Arg == "--restore") && i + 1 < argc)
{
RestorePath = argv[++i];
}
else if ((Arg == "-n" || Arg == "--name") && i + 1 < argc)
{
Name = argv[++i];
HaveName = true;
}
else if ((Arg == "-e" || Arg == "--email") && i + 1 < argc)
{
Email = argv[++i];
HaveEmail = true;
}
else if ((Arg == "-c" || Arg == "--code") && i + 1 < argc)
{
Code = argv[++i];
HaveCode = true;
}
else if (Arg == "-h" || Arg == "--help")
{
PrintUsage();
return 0;
}
else
{
fprintf(stderr, "unknown or incomplete option: %s\n", Arg.c_str());
PrintUsage();
return 1;
}
}
std::string MachineID = GenerateMachineID();
if (SelfTest)
{
SupportInfo Info;
BuildSupportInfo(Info, MachineID, HaveName ? Name : CurrentUserName(), Email, Code);
std::vector<uint8_t> Blob;
if (!EncodeSupportInfo(Info, Blob))
{
fprintf(stderr, "self test failed: could not encode key\n");
return 1;
}
SupportInfo Decoded;
if (!DecodeSupportInfo(Blob, MachineID, Decoded) || memcmp(&Info, &Decoded, sizeof(SupportInfo)) != 0 || !Decoded.Validated)
{
fprintf(stderr, "self test failed: could not decode key\n");
return 1;
}
if (!RunSelfTest())
{
fprintf(stderr, "self test failed\n");
return 1;
}
printf("self test passed\n");
printf("sizeof(SupportInfo) = %u, payload = %u, blob = %u bytes\n",
(unsigned int)sizeof(SupportInfo),
(unsigned int)(sizeof(SupportInfo) + MD5_HEX_LENGTH),
(unsigned int)Blob.size());
printf("machine id = %s\n", MachineID.c_str());
return 0;
}
if (DoDelete)
{
if (DeleteKeyBlob())
{
printf("deleted stored key\n");
}
else
{
printf("no stored key to delete\n");
}
}
if (RestorePath != nullptr)
{
std::wstring Path = Utf8ToWide(RestorePath);
std::vector<uint8_t> Blob;
if (!ReadFileBlob(Path.c_str(), Blob) || Blob.empty())
{
fprintf(stderr, "could not read backup file: %s\n", RestorePath);
return 1;
}
SupportInfo Info;
if (!DecodeSupportInfo(Blob, MachineID, Info))
{
fprintf(stderr, "backup file is not a valid key for this machine\n");
return 1;
}
if (!WriteKeyBlob(Blob))
{
fprintf(stderr, "could not write the registry key\n");
return 1;
}
printf("restored key from %s (name: %s)\n", RestorePath, Utf8Field(Info.Name, sizeof(Info.Name)).c_str());
}
if (BackupPath != nullptr)
{
std::vector<uint8_t> Blob;
if (!ReadKeyBlob(Blob) || Blob.empty())
{
fprintf(stderr, "no stored key to back up\n");
return 1;
}
std::wstring Path = Utf8ToWide(BackupPath);
if (!WriteFileBlob(Path.c_str(), Blob))
{
fprintf(stderr, "could not write backup file: %s\n", BackupPath);
return 1;
}
printf("backed up stored key to %s (%u bytes)\n", BackupPath, (unsigned int)Blob.size());
}
if ((RestorePath != nullptr || DoDelete || BackupPath != nullptr) && !Force && !ShowInfo)
{
return 0;
}
SupportInfo Current;
KeyState State = ReadStoredKey(MachineID, Current);
if (ShowInfo)
{
printf("machine id = %s\n", MachineID.c_str());
switch (State)
{
case KEY_VALID:
printf("stored key = valid\n");
printf(" name = %s\n", Utf8Field(Current.Name, sizeof(Current.Name)).c_str());
printf(" email = %s\n", Utf8Field(Current.Email, sizeof(Current.Email)).c_str());
printf(" code = %s\n", Utf8Field(Current.Code, sizeof(Current.Code)).c_str());
printf(" run count = %u\n", (unsigned int)Current.RunCount);
break;
case KEY_NONE:
printf("stored key = none\n");
break;
default:
printf("stored key = present but invalid for this machine\n");
break;
}
if (!Force)
{
return 0;
}
printf("regenerating forced...\n");
}
if (State == KEY_VALID && !Force)
{
printf("validated existing key (name: %s)\n", Utf8Field(Current.Name, sizeof(Current.Name)).c_str());
return 0;
}
SupportInfo Written;
if (!GenerateStoredKey(MachineID, HaveName ? Name : CurrentUserName(), Email, Code, Written))
{
fprintf(stderr, "could not generate key\n");
return 1;
}
printf("generated new key (name: %s, machine: %s)\n", Utf8Field(Written.Name, sizeof(Written.Name)).c_str(), MachineID.c_str());
return 0;
}