-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.c
432 lines (402 loc) · 12.1 KB
/
registry.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
* Novantotto
* Copyright (c) 2024 Andrea Bonomi
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "novantotto.h"
#include "handle.h"
#include "winreg.h"
#include "winerror.h"
#include "debug.h"
static LPCSTR ROOT_KEYS[] = {
"HKEY_CLASSES_ROOT",
"HKEY_CURRENT_USER",
"HKEY_LOCAL_MACHINE",
"HKEY_USERS",
"HKEY_PERFORMANCE_DATA",
"HKEY_CURRENT_CONFIG",
"HKEY_DYN_DATA",
};
static inline BOOL IsEmpty(LPCSTR s)
{
return s == NULL || strlen(s) == 0;
}
static inline BOOL IsValidPathname(LPCSTR s)
{
return IsEmpty(s) || s[0] != '\\';
}
static LPSTR ConcatenateKeys(LPCSTR k1, LPCSTR k2)
{
if (IsEmpty(k1) && IsEmpty(k2))
{
return NULL;
}
else if (IsEmpty(k2))
{
return strdup(k1);
}
else if (IsEmpty(k1))
{
return strdup(k2);
}
else
{
LPSTR result;
asprintf(&result, "%s\\%s", k1, k2);
return result;
}
}
LPSTR LocalStorageKey(HKEY hKey, LPCSTR lpSubKey, LSTATUS *pStatus)
{
if (hKey >= HKEY_CLASSES_ROOT && hKey <= HKEY_DYN_DATA)
{
*pStatus = ERROR_SUCCESS;
return ConcatenateKeys(ROOT_KEYS[hKey - HKEY_CLASSES_ROOT], lpSubKey);
}
else
{
RESOURCE *res = GetHandle(hKey, HANDLE_REGKEY);
if (res == NULL)
{
*pStatus = ERROR_INVALID_HANDLE;
return NULL;
}
*pStatus = ERROR_SUCCESS;
return ConcatenateKeys(res->regKey.lpSubKey, lpSubKey);
}
}
//*******************************************************************
// Open or create a registry key
//*******************************************************************
LSTATUS RegCreateKeyEx(HKEY hKey, LPCSTR lpSubKey, DWORD Reserved, LPSTR lpClass,
DWORD dwOptions, REGSAM samDesired, const LPSECURITY_ATTRIBUTES lpSecurityAttributes,
PHKEY phkResult, LPDWORD lpdwDisposition)
{
LSTATUS status;
if (!IsValidPathname(lpSubKey))
{
return ERROR_BAD_PATHNAME;
}
LPSTR localStorageKey = LocalStorageKey(hKey, lpSubKey, &status);
if (status != ERROR_SUCCESS)
{
return status;
}
// clang-format off
EM_ASM({
let item = localStorage.getItem(UTF8ToString($0));
if (item == null) {
localStorage.setItem(UTF8ToString($0), "{}");
}
}, localStorageKey);
// clang-format on
RESOURCE *res = AllocateHandle(HANDLE_REGKEY, NULL);
res->regKey.lpSubKey = localStorageKey;
*phkResult = (HKEY)res->handle;
return status;
}
LSTATUS RegCreateKey(HKEY hKey, LPCSTR lpSubKey, PHKEY phkResult)
{
LSTATUS status = RegCreateKeyEx(hKey, lpSubKey, 0, NULL, 0, 0, NULL, phkResult, NULL);
if (status == ERROR_INVALID_HANDLE)
{
status = ERROR_BADKEY;
}
return status;
}
//*******************************************************************
// Open a registry key
//*******************************************************************
LSTATUS RegOpenKeyEx(HKEY hKey, LPCSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
{
LSTATUS status;
if (!IsValidPathname(lpSubKey))
{
return ERROR_BAD_PATHNAME;
}
LPSTR localStorageKey = LocalStorageKey(hKey, lpSubKey, &status);
if (status != ERROR_SUCCESS)
{
return status;
}
// clang-format off
status = EM_ASM_INT({
let item = localStorage.getItem(UTF8ToString($0));
if (item == null) {
return 2; // ERROR_FILE_NOT_FOUND
} else {
return 0; // ERROR_SUCCESS
}
}, localStorageKey);
// clang-format on
if (status != ERROR_SUCCESS)
{
return status;
}
RESOURCE *res = AllocateHandle(HANDLE_REGKEY, NULL);
res->regKey.lpSubKey = localStorageKey;
*phkResult = (HKEY)res->handle;
return status;
}
LSTATUS RegOpenKey(HKEY hKey, LPCSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
{
return RegOpenKey(hKey, lpSubKey, 0, 0, phkResult);
}
//*******************************************************************
// Close a registry key handle
//*******************************************************************
LSTATUS RegCloseKey(HKEY hKey)
{
if (hKey >= HKEY_CLASSES_ROOT)
{
return ERROR_SUCCESS;
}
RESOURCE *res = GetHandle(hKey, HANDLE_REGKEY);
if (res == NULL)
{
return ERROR_INVALID_HANDLE;
}
free(res->regKey.lpSubKey);
ReleaseHandle(res);
return ERROR_SUCCESS;
}
//*******************************************************************
// Set the data for a registry key
//*******************************************************************
LSTATUS RegSetValue(HKEY hKey, LPCSTR lpSubKey, DWORD dwType, LPCSTR lpData, DWORD cbData)
{
LSTATUS status;
if (dwType != REG_SZ)
{
return ERROR_INVALID_PARAMETER;
}
LPSTR localStorageKey = LocalStorageKey(hKey, lpSubKey, &status);
if (localStorageKey == NULL)
{
return status;
}
// clang-format off
EM_ASM({
let item = localStorage.getItem(UTF8ToString($0));
let dict = JSON.parse(item || "{}");
dict[""] = UTF8ToString($1);
item = JSON.stringify(dict);
localStorage.setItem(UTF8ToString($0), item);
}, localStorageKey, lpData);
// clang-format on
free(localStorageKey);
return ERROR_SUCCESS;
}
//*******************************************************************
// Set data of a specified value under a registry key
//*******************************************************************
LSTATUS RegSetValueEx(HKEY hKey, LPCSTR lpValueName, DWORD Reserved, DWORD dwType, CONST BYTE *lpData, DWORD cbData)
{
LSTATUS status;
LPSTR localStorageKey = LocalStorageKey(hKey, "", &status);
if (localStorageKey == NULL)
{
return status;
}
// clang-format off
EM_ASM({
let item = localStorage.getItem(UTF8ToString($0));
let dict = JSON.parse(item || "{}");
const key = UTF8ToString($1).toLowerCase();
dict[key] = {};
dict[key].type = $3;
switch(dict[key].type) {
case 4: //REG_DWORD
case 5: //REG_DWORD_BIG_ENDIAN
dict[key].value = getValue($2, "i32");
break;
case 11: //REG_QWORD_LITTLE_ENDIAN
dict[key].value = getValue($2, "i64");
break;
default:
dict[key].value = UTF8ToString($2);
break;
}
item = JSON.stringify(dict);
localStorage.setItem(UTF8ToString($0), item);
}, localStorageKey, lpValueName, lpData, dwType);
// clang-format on
free(localStorageKey);
return ERROR_SUCCESS;
}
//*******************************************************************
// Remove a key (and the subvalues) from the registry
//*******************************************************************
LSTATUS RegDeleteKey(HKEY hKey, LPCSTR lpSubKey)
{
LSTATUS status;
LPSTR localStorageKey = LocalStorageKey(hKey, lpSubKey, &status);
if (localStorageKey == NULL)
{
return status;
}
EM_ASM(localStorage.removeItem(UTF8ToString($0)), localStorageKey);
free(localStorageKey);
return ERROR_SUCCESS;
}
//*******************************************************************
// Remove a value from the registry
//*******************************************************************
LSTATUS RegDeleteValue(HKEY hKey, LPCSTR lpValueName)
{
LSTATUS status;
LPSTR localStorageKey = LocalStorageKey(hKey, "", &status);
if (localStorageKey == NULL)
{
return status;
}
// clang-format off
status = EM_ASM_INT({
let item = localStorage.getItem(UTF8ToString($0));
if (item == null) {
return 2; // ERROR_FILE_NOT_FOUND
}
let dict = JSON.parse(item);
let value = dict[UTF8ToString($1).toLowerCase()];
if (value == null) {
return 2; // ERROR_FILE_NOT_FOUND
}
delete dict[UTF8ToString($1).toLowerCase()];
item = JSON.stringify(dict);
localStorage.setItem(UTF8ToString($0), item);
return 0; // ERROR_SUCCESS
}, localStorageKey, lpValueName);
// clang-format on
free(localStorageKey);
return status;
}
//*******************************************************************
// Retrieves the registry key value
//*******************************************************************
LSTATUS RegQueryValue(HKEY hKey, LPCSTR lpSubKey, LPSTR lpData, PLONG lpcbData)
{
LSTATUS status;
if (!IsValidPathname(lpSubKey))
{
return ERROR_BAD_PATHNAME;
}
LPSTR localStorageKey = LocalStorageKey(hKey, lpSubKey, &status);
if (localStorageKey == NULL)
{
return status;
}
// clang-format off
status = EM_ASM_INT({
let item = localStorage.getItem(UTF8ToString($0));
if (item == null) {
return 2; // ERROR_FILE_NOT_FOUND
}
let dict = JSON.parse(item);
let value = dict[""];
if (value == null) {
return 2; // ERROR_FILE_NOT_FOUND
}
if ($2 != 0) {
stringToUTF8(value, $1, $2);
}
return 0; // ERROR_SUCCESS
}, localStorageKey, lpData, *lpcbData);
// clang-format on
if (status == ERROR_SUCCESS && lpData != NULL && lpcbData != NULL)
{
*lpcbData = strlen(lpData);
}
free(localStorageKey);
return status;
}
//*******************************************************************
// Retrieves the data for a registry key value
//*******************************************************************
LSTATUS RegQueryValueEx(HKEY hKey, LPCSTR lpValueName, LPDWORD lpReserved, LPDWORD lpType, LPBYTE lpData, LPDWORD lpcbData)
{
LSTATUS status;
LPSTR localStorageKey = LocalStorageKey(hKey, "", &status);
if (localStorageKey == NULL)
{
return status;
}
// clang-format off
status = EM_ASM_INT({
let item = localStorage.getItem(UTF8ToString($0));
if (item == null) {
return 2; // ERROR_FILE_NOT_FOUND
}
let dict = JSON.parse(item);
const key = UTF8ToString($1).toLowerCase();
if (dict[key] == null) {
return 2; // ERROR_FILE_NOT_FOUND
}
if ($2 != 0) { // $2=lpData
switch(dict[key].type) {
case 4: //REG_DWORD
case 5: //REG_DWORD_BIG_ENDIAN
setValue($2, dict[key].value, "i32");
break;
case 11: //REG_QWORD_LITTLE_ENDIAN
setValue($2, dict[key].value, "i64");
break;
default:
stringToUTF8(dict[key].value, $2, $3);
break;
}
}
if ($4 != 0) { // $4=lpType
setValue($4, dict[key].type, "i16");
}
return 0; // ERROR_SUCCESS
}, localStorageKey, lpValueName, lpData, *lpcbData, lpType);
if (status == ERROR_SUCCESS) {
// clang-format on
if (lpData != NULL && lpcbData != NULL)
{
switch (*lpType)
{
case REG_DWORD:
case REG_DWORD_BIG_ENDIAN:
*lpcbData = 4;
break;
case REG_QWORD_LITTLE_ENDIAN:
*lpcbData = 8;
break;
default:
*lpcbData = strlen((char *)lpData) + 1;
break;
}
}
}
free(localStorageKey);
return status;
}
//*******************************************************************
// No nothing
//*******************************************************************
LSTATUS RegConnectRegistry(LPCSTR lpMachineName, HKEY hKey, PHKEY phkResult)
{
if (strlen(lpMachineName) == 0)
{
return ERROR_SUCCESS;
}
else
{
return ERROR_BAD_NETPATH;
}
}
//*******************************************************************
// No nothing
//*******************************************************************
LSTATUS RegFlushKey(HKEY hKey)
{
return ERROR_SUCCESS;
}