-
Notifications
You must be signed in to change notification settings - Fork 2
/
casc.c
339 lines (285 loc) · 9.31 KB
/
casc.c
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <stdio.h>
#include <windows.h>
#include "casc.h"
#include "CascLib.h"
// Why, CascLib, why
#undef bool
#undef sprintf
HANDLE casc = NULL;
// internal buffer for parsing files in casc archive, to prevent excessive malloc/free use
u8* readbuf = NULL;
u32 bufMaxSize = 0;
// protobuf data
// https://developers.google.com/protocol-buffers/docs/encoding
#define WIRE_VARINT 0 // int32, int64, uint32, uint64, sint32, sint64, bool, enum
#define WIRE_64BIT 1 // fixed64, sfixed64, double
#define WIRE_LENGTH_DELIMITED 2 // string, bytes, embedded messages, packed repeated fields
#define WIRE_START_GROUP 3 // groups (deprecated)
#define WIRE_END_GROUP 4 // groups (deprecated)
#define WIRE_32BIT 5 // fixed32, sfixed32, float
// product.db format: https://gist.github.com/neivv/d4c822619c8c845d91ca35a52668d48e
// Arbitrary indexes for use in recursive function
#define MESSAGE_ROOT 0
#define MESSAGE_PRODUCT 1
#define MESSAGE_INSTALLATION 2
// used field ID's within the message types
#define ROOT_INSTALLEDPRODUCT 1
#define PRODUCT_VARIANT_UID 2
#define PRODUCT_INSTALL 3
#define INSTALLATION_PATH 1
const u8 sc_uid[] = "s1";
u8* getInstallPath();
bool getInstallPath_Proto(FILE* f, u32 start, u32 len, u32 message, u8** path);
u32 varint(FILE* f, u32* decodeH);
bool loadCascArchive(){
// from SI https://discord.com/channels/258178661369249802/583406212272619571/739635572787773541
/* // Try the uninstall key (SC:R only?)
if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Starcraft\\"), 0, KEY_READ | KEY_WOW64_32KEY, &hBlizzSettings) == ERROR_SUCCESS ||
::RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Starcraft\\"), 0, KEY_READ | KEY_WOW64_32KEY, &hBlizzSettings) == ERROR_SUCCESS ||
::RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Starcraft\\"), 0, KEY_READ | KEY_WOW64_64KEY, &hBlizzSettings) == ERROR_SUCCESS ||
::RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Starcraft\\"), 0, KEY_READ | KEY_WOW64_64KEY, &hBlizzSettings) == ERROR_SUCCESS )
{
DWORD keyType;
DWORD length = MAX_PATH;
LSTATUS res = ::RegQueryValueExW(hBlizzSettings, L"InstallLocation", NULL, &keyType, reinterpret_cast<BYTE*>( tempStarcraftPath ), &length);
::RegCloseKey(hBlizzSettings);*/
// /from SI
u8* path = getInstallPath();
if(path == NULL){
puts("Error: Could not locate CASC archive");
return false;
}
if(CascOpenStorage(path, 0, &casc) == false){
free(path);
return false;
}
free(path);
return true;
}
void closeCascArchive(){
CascCloseStorage(casc);
if(readbuf != NULL){
free(readbuf);
readbuf = NULL;
bufMaxSize = 0;
}
}
// Allocates a buffer and loads the entirety of the file contents
u8* loadCascFile(const u8* path, u32* size_out){
HANDLE cascfile = NULL;
u8* data = NULL;
u32 size = 0;
u32 sizeread = 0;
if(casc == NULL){
puts("Error: No CASC archive loaded");
return NULL;
}
if(CascOpenFile(casc, path, 0, CASC_OPEN_BY_NAME, &cascfile) == false){
puts("Error: Could not open CASC file");
return NULL;
}
size = CascGetFileSize(cascfile, NULL);
if(size == CASC_INVALID_SIZE){
puts("Error: Could not get file size");
}else{
data = malloc(size);
if(data == NULL){
puts("Error: Memory");
}else{
CascReadFile(cascfile, data, size, (DWORD*)&sizeread);
if(size != sizeread){
printf("Error: %d of %d read :(\n", sizeread, size);
free(data);
data = NULL;
}
}
}
CascCloseFile(cascfile);
if(size_out != NULL){
*size_out = size;
}
return data;
}
// Opens a casc file and returns the handle
HANDLE openCascFile(const u8* path){
HANDLE cascfile = NULL;
if(casc == NULL){
puts("Error: No CASC archive loaded");
return NULL;
}
if(CascOpenFile(casc, path, 0, CASC_OPEN_BY_NAME, &cascfile) == false){
puts("Error: Could not open CASC file");
return NULL;
}
return cascfile;
}
// Reads some of a file in to an existing buffer
// - if buf is NULL then an internal buffer is used
// - if offset is -1 then the file is read from the current position
// - if size is -1 then the entire file is read
u8* readCascData(HANDLE hfile, void* buf, u32 offset, u32 size, u32* read){
if(size == -1){
size = CascGetFileSize(hfile, NULL);
}
if(buf == NULL){
if(size > bufMaxSize){
readbuf = realloc(readbuf, size);
if(readbuf == NULL) puts("Error allocating memory in readCascData");
bufMaxSize = size;
}
buf = readbuf;
}
if(offset != -1){
CascSetFilePointer(hfile, offset, NULL, FILE_BEGIN);
}
CascReadFile(hfile, buf, size, (DWORD*)read);
if(size != *read) printf("CascReadFile size mismatch? %d != %d\n", size, *read);
return buf;
}
// Closes external handle
void closeCascFile(HANDLE hfile){
CascCloseFile(hfile);
}
// Attempts to find the starcraft install path from product.db
u8* getInstallPath(){
u8* path = NULL;
u8* programdata = getenv("PROGRAMDATA");
if(programdata == NULL) return;
u8 agentpath[0x400];
sprintf(agentpath, "%s\\Battle.net\\Agent\\product.db", programdata);
FILE* f = fopen(agentpath, "rb");
if(f == NULL){
puts("Error: Could not locate Battle.net Agent data.");
return;
}
getInstallPath_Proto(f, 0, 0, MESSAGE_ROOT, &path);
fclose(f);
return path;
}
// Decode protobuf structures
bool getInstallPath_Proto(FILE* f, u32 start, u32 len, u32 message, u8** path){
u8 b;
u8 wire;
u8 field;
u32 i;
u32 decode;
u32 decodeH;
bool exit = false;
u32 instoffs = 0;
u32 instlen = 0;
u8 uid[4] = {0,0,0,0};
while(!exit && !feof(f) && (message == MESSAGE_ROOT || ftell(f)-start < len)){
b = fgetc(f);
wire = b&7;
field = b>>3;
switch(wire){
case WIRE_VARINT:
decode = varint(f, &decodeH);
//printf("varint %08x%08x\n", decodeH, decode);
break;
case WIRE_64BIT:
fread(&decode, 1, 4, f);
fread(&decodeH, 1, 4, f);
//printf("64 bit %08x%08x\n", decodeH, decode);
break;
case WIRE_LENGTH_DELIMITED:
{
// get length
decode = varint(f, &decodeH);
if(decodeH != 0) puts("Nonzero high value in message length !?");
//printf("len %d\n", decode);
// Different behavior depending on message type
switch(message){
case MESSAGE_ROOT:
{
if(field == ROOT_INSTALLEDPRODUCT){
exit = getInstallPath_Proto(f, ftell(f), decode, MESSAGE_PRODUCT, path);
decode = 0; // already processed, but not necessarily exiting
}
break;
}
case MESSAGE_PRODUCT:
{
switch(field){
case PRODUCT_INSTALL:
instoffs = ftell(f);
instlen = decode;
if(strcmp(uid, sc_uid) == 0){
// don't need to continue processing this message since correct sc_uid was already found
exit = true;
}
break;
case PRODUCT_VARIANT_UID:
if(decode == strlen(sc_uid) && decodeH == 0){
for(i=0; i<decode; i++){
uid[i] = fgetc(f);
}
if(instlen != 0 && strcmp(uid, sc_uid) == 0){
// don't need to continue processing since correct uid and install field already found
exit = true;
}
decode = 0; // already processed, but not necessarily exiting
break;
}
break;
default:
break;
}
break;
}
case MESSAGE_INSTALLATION:
{
if(field == INSTALLATION_PATH){
*path = malloc(decode+1);
if((*path) == NULL){
puts("Error: Memory");
return true;
}
fread(*path, 1, decode, f);
(*path)[decode] = 0;
puts(*path);
return true;
}
break;
}
}
// Eat bytes for any unread string/whatever
if(!exit && decode != 0){
fseek(f, decode, SEEK_CUR);
}
break;
}
case WIRE_START_GROUP:
//puts("Start group (deprecated)");
break;
case WIRE_END_GROUP:
//puts("End group (deprecated)");
break;
case WIRE_32BIT:
fread(&decode, 1, 4, f);
//printf("32 bit %08x%08x\n", decodeH, decode);
break;
default:
printf("Unknown wire type %d\n", wire);
exit = true;
}
}
// down here since fields 2 and 3 must both be found
if(message == MESSAGE_PRODUCT && instlen != 0 && strcmp(uid, sc_uid) == 0){
fseek(f, instoffs, SEEK_SET);
exit = getInstallPath_Proto(f, instoffs, instlen, MESSAGE_INSTALLATION, path);
}
return exit;
}
u32 varint(FILE* f, u32* decodeH){
u8 b;
u8 s = 0;
u64 out = 0;
do{
b = fgetc(f);
out |= (b&0x7F) << s;
s += 7;
} while(b&0x80);
*decodeH = (u32)(out >> 32);
return (u32)(out);
}