-
Notifications
You must be signed in to change notification settings - Fork 3
/
regdelta.cpp
805 lines (624 loc) · 17.7 KB
/
regdelta.cpp
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
// Copyright (c) 2005-2015 Ross Smith II. See Mit LICENSE in /LICENSE
// #define TINY_TEST 1
/*
@todo rename regtool
@todo don't write HKEY_USERS tree for HKEY_CURRENT_USER branch
@todo Profile the code to see where the biggest speed improvements can be made
@todo Don't use .readall() to read in the entire file
@todo For speed, write output to memory, then write to disk when done
@todo add:
-r | --redo
-u | --undo
@todo Rename options:
mv -x => -e | --export
mv -e => -E | --execute
mv -r => -R | --regedit
mv -u => -U | --unsorted
@todo Deal with write errors!
@todo Add -s | --sort file.reg support
@todo Add -e | --execute cmd args support
@todo Add -x | --exclude key
NOTES:
http://winhlp.com/WxRegistryRemoving.htm
A minus sign inserted directly after the opening bracket in an otherwise normal .reg file removes that key. It works only for keys, not for values, but you can remove, then recreate a key.
Example:
[-HKEY_LOCAL_MACHINE\Software\Adobe]
This entry in a .reg file will remove that entire key and all of its subkeys and values.
You can remove a value by entering a minus sign (without quotes) as the new value.
Example:
[HKEY_LOCAL_MACHINE\Software\Test]
"Test"=-
This removes the value named "Test", but leaves the key "Test" in place.
*/
#pragma warning(disable:4786)
#pragma comment(lib, "advapi32")
#ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN 1
#endif
//#include <assert.h>
//#include <tchar.h>
//#include <tstl.h>
#include <windows.h>
//#include <conio.h>
//#include <algorithm>
#include <map>
#include <sstream>
#include <string>
#include "SystemMessage.h"
#include "tgetopt.h"
#include "RegFileReader.h"
#include "RegFileWriter.h"
#include "version.h"
#define APPNAME _T(VER_INTERNAL_NAME)
#define APPVERSION _T(VER_STRING2)
#define APPCOPYRIGHT _T(VER_LEGAL_COPYRIGHT)
using namespace std;
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/registry_element_size_limits.asp
//HKEY_String_Map hkey_name;
struct tchar_less_nocase {
bool operator() (const TCHAR *p, const TCHAR *q) {
return _tcsicmp(p, q) < 0;
}
};
typedef map<TCHAR *, HKEY, tchar_less_nocase> TCHAR_HKEY_Map;
static TCHAR_HKEY_Map name_hkey;
static HKEY root_key[] = {
HKEY_CURRENT_CONFIG,
HKEY_CURRENT_USER,
HKEY_CLASSES_ROOT,
HKEY_LOCAL_MACHINE,
HKEY_USERS
};
static const int root_keys = sizeof(root_key) / sizeof(root_key[0]);
const TCHAR *dash = _T("-");
static TCHAR *progname(const TCHAR *argv0);
struct _opt {
bool all;
TCHAR *execute;
bool o_export;
TCHAR *output;
bool comments;
bool quirks;
bool regedit;
bool sort;
bool unsorted;
bool verbose;
bool wordwrap;
};
typedef struct _opt Opt;
Opt opt = {
false, // all
NULL, // execute
false, // export
false, // comments
false, // quirks
false, // regedit
false, // sort
false, // unsorted
false, // verbose
false // wordwrap
};
static const TCHAR *short_options = _T("aco:ruvVwx?"); // _T("q")
static const struct toption long_options[] = {
{_T("all"), no_argument, 0, 'a'},
{_T("a"), no_argument, 0, 'a'},
{_T("export"), no_argument, 0, 'x'},
{_T("x"), no_argument, 0, 'x'},
// {_T("execute"), required_argument, 0, 'e'},
// {_T("e"), required_argument, 0, 'e'},
{_T("output"), required_argument, 0, 'o'},
{_T("o"), required_argument, 0, 'o'},
{_T("comments"), no_argument, 0, 'c'},
{_T("c"), no_argument, 0, 'c'},
// {_T("quirks"), no_argument, 0, 'q'},
{_T("regedit"), no_argument, 0, 'r'},
{_T("r"), no_argument, 0, 'r'},
// {_T("sort"), no_argument, 0, 's'},
// {_T("s"), no_argument, 0, 's'},
{_T("wordwrap"), no_argument, 0, 'w'},
{_T("w"), no_argument, 0, 'w'},
{_T("unsorted"), no_argument, 0, 'u'},
{_T("u"), no_argument, 0, 'u'},
{_T("verbose"), no_argument, 0, 'v'},
{_T("v"), no_argument, 0, 'v'},
{_T("version"), no_argument, 0, 'V'},
{_T("V"), no_argument, 0, 'V'},
{_T("help"), no_argument, 0, '?'},
{_T("h"), no_argument, 0, '?'},
{_T("?"), no_argument, 0, '?'},
{NULL, 0, 0, 0}
};
static void usage(int exitval) {
TCHAR *me = progname(NULL);
_tprintf(
_T("Usage: %s [options] [key | file.reg] [key | file.reg]\n")
_T("Options:\n"),
me
);
/* 12345678901234567890123456789012345678901234567890123456789012345678901234567890*/
_tprintf(
_T("-a | --all Compare/export all keys (HKCC HKCU HKCR HKLM HKU)\n")
_T("-x | --export key(s) ex.reg Export key(s) to ex.reg\n")
//_T("-e | --execute 'cmd' ex.reg Execute cmd & save differences to ex.reg (TODO)\n")
_T("-o | --output output.reg Set output filename to 'output.reg'\n")
_T("-c | --comments Include original values as comments in diff\n")
// _T("-q | --quirks Implement REGEDIT quirks\n")
_T("-r | --regedit Export in RegEdit format (implies -u -w)\n")
//_T("-s | --sort in.reg out.reg Sort in.reg to out.reg (TODO)\n")
_T("-u | --unsorted Do not sort the keys or values, as RegEdit\n")
_T("-w | --wordwrap Wordwrap at 78 chars, as RegEdit\n")
_T("-v | --verbose Show warnings that are normally hidden\n")
_T("-V | --version Show version & copyright information & quit\n")
_T("-? | --help Show this help message and quit\n")
_T("Examples:\n")
_T(" %s HKCU hkcu.reg ; compare HKCU key with hkcu.reg\n")
_T(" %s HKCU\\1 HKCU\\2 ; compare HKCU\\1 key with HKCU\\2 key\n")
_T(" %s -o diff.reg HKCU hkcu.reg ; compare HKCU key with hkcu.reg\n")
_T(" %s -x HKCU hkcu.reg ; export HKCU key to hkcu.reg\n")
_T(" %s -xr HKCU\\Software ex.reg ; export HKCU to ex.reg in REGEDIT format\n")
_T(" %s -ax allkeys.reg ; export all keys to allkeys.reg\n")
//_T(" %s -s input.reg output.reg ; sort input.reg to output.reg\n")
//_T(" %s -e 'setup.exe /S' ex.reg ; run setup & save differences in ex.reg\n")
,me, me, me, me, me, me //, me, me
);
exit(exitval);
}
/* per http://www.scit.wlv.ac.uk/cgi-bin/mansec?3C+basename */
static TCHAR* basename(const TCHAR* s) {
TCHAR* rv;
if (!s || !*s)
return _T(".");
unsigned int ui = _tcslen(s);
rv = const_cast<TCHAR *>(s);
rv += (_tcslen(s) - 1) * sizeof(TCHAR);
do {
if (*rv == '\\' || *rv == '/')
return rv + 1;
--rv;
} while (rv >= s);
return const_cast<TCHAR *>(s);
}
static TCHAR *progname(const TCHAR *argv0) {
static TCHAR *progname = NULL;
if (progname == NULL && argv0 != NULL) {
progname = basename(argv0);
int len = _tcslen(progname);
TCHAR *p = progname + (len - 4) * sizeof(TCHAR);
if (len > 4 && _tcsicmp(p, _T(".exe")) == 0)
progname[len - 4] = '\0';
}
return progname;
}
static DWORD export_key(RegFileWriter *f, TCHAR *start_key) {
DWORD rv = 0;
TCHAR *subkey = NULL;
TCHAR *p = _tcschr(start_key, '\\');
if (p != NULL) {
*p = '\0';
++p;
subkey = p;
}
HKEY hkey = (HKEY) name_hkey[start_key];
#ifdef TINY_TEST
hkey = HKEY_CURRENT_USER;
subkey = _T("regdelta");
#endif
if (hkey == 0) {
_ftprintf(stderr, _T("Invalid key: '%s'. Must be one of:"), start_key);
for (TCHAR_HKEY_Map::const_iterator it = name_hkey.begin(); it != name_hkey.end(); ++it) {
TCHAR *abbr = (TCHAR *) it->first;
if (_tcslen(abbr) < 5) {
_ftprintf(stderr, _T(" %s"), abbr);
}
}
_ftprintf(stderr, EOL);
return 1;
}
rv = f->writeValues(hkey, subkey);
return rv;
}
int export2(const int argc, TCHAR *const *argv, int toptind, const TCHAR *output_file) {
DWORD rv = 0;
if (!opt.all && toptind >= argc)
usage(1);
RegFileWriter f;
f.setQuirks(opt.quirks);
f.setUnsorted(opt.unsorted);
f.setVerbose(opt.verbose);
f.setWordwrap(opt.wordwrap);
f.create(output_file);
f.writeHeader(true);
TCHAR *start_key = NULL;
if (opt.all) {
for (int i = 0; i < root_keys; ++i) {
rv = f.writeValues(root_key[i], NULL);
if (rv)
break;
}
} else {
while (toptind < argc) {
start_key = argv[toptind++];
rv = export_key(&f, start_key);
if (rv)
break;
}
}
return rv;
}
int do_export(int argc, TCHAR *const *argv, int toptind) {
DWORD rv = 0;
TCHAR *output_file;
if (opt.output) {
output_file = opt.output;
} else {
if (toptind >= argc)
usage(1);
output_file = argv[--argc];
}
rv = export2(argc, argv, toptind, output_file);
return rv;
}
static TCHAR* get_temp_name(const TCHAR *prefix) {
TCHAR lpPathBuffer[MAX_PATH];
DWORD rv = GetTempPath(
MAX_PATH, // length of the buffer
lpPathBuffer // buffer for path
);
if (rv == 0) {
rv = GetLastError();
_ftprintf(stderr, _T("Cannot get temporary path: Error %d: %s"), rv, SystemMessage(rv));
return NULL;
}
static TCHAR szTempName[MAX_PATH];
if (prefix == NULL)
prefix = _T("~rd");
rv = GetTempFileName(
lpPathBuffer, // directory for tmp files
prefix, // temp file name prefix
0, // create unique name
szTempName // buffer for name
);
if (rv == 0) {
rv = GetLastError();
_ftprintf(stderr, _T("Cannot get temporary path: Error %d: %s"), rv, SystemMessage(rv));
return NULL;
}
return _tcsdup(szTempName);
}
static DWORD sort(const int argc, TCHAR *const *argv, int toptind) {
DWORD rv = 0;
if (toptind >= argc)
usage(1);
RegFileReader input;
input.open(argv[toptind++]);
if (!opt.output) {
opt.output = const_cast<TCHAR*>(dash);
} else {
if (toptind >= argc)
usage(1);
opt.output = argv[toptind++];
}
RegFileWriter output;
output.create(opt.output);
/****************************************************************************/
// @todo Add -s | --sort file.reg support
_ftprintf(stderr, _T("Option not yet implemented"));
exit(1);
/****************************************************************************/
input.close();
output.close();
return rv;
}
static DWORD export_file(const TCHAR *filename, TCHAR *start_key) {
DWORD rv = 0;
RegFileWriter f;
f.create(filename);
f.writeHeader(true);
rv = export_key(&f, start_key);
f.close();
return rv;
}
static DWORD diff_files(const TCHAR *file1, const TCHAR *file2) {
DWORD rv = 0;
RegFileReader r1;
RegFileReader r2;
rv = r1.open(file1);
if (rv)
return rv;
rv = r2.open(file2);
if (rv)
return rv;
RegFileWriter w;
if (!opt.output)
opt.output = const_cast<TCHAR*>(dash);
rv = w.create(opt.output);
if (rv)
return rv;
r1.readall();
r2.readall();
r1.skipHeader();
r2.skipHeader();
w.writeHeader(false);
while (!r1.eof() || !r2.eof()) {
if (r1.eof() && !r2.eof()) {
w.writeSection(r2.getSection());
if (!r2.isSection()) {
w.writeLine(r2.getLine());
}
r2.readNextLine();
continue;
}
if (!r1.eof() && r2.eof()) {
if (r1.isSection()) {
w.writeDelSection(r1.getSection());
r1.skipDelSection();
continue;
}
w.writeSection(r1.getSection());
w.writeDelLine(r1.getLine());
r1.readNextLine();
continue;
}
if (r1.isSection() && r2.isSection()) {
if (r2.sectionCompare(&r1) > 0) {
w.writeDelSection(r1.getSection());
r1.skipDelSection();
continue;
}
if (r1.sectionCompare(&r2) > 0) {
w.writeSection(r2.getSection());
if (!r2.isSection()) {
w.writeLine(r2.getLine());
}
while (!r2.eof()) {
r2.readNextLine();
if (r2.eof())
break;
if (r2.isSection())
break;
w.writeLine(r2.getLine());
}
continue;
}
// sections are the same
if (r1.isSection())
r1.readNextLine();
if (r2.isSection())
r2.readNextLine();
continue;
}
if (r2.isSection() || (!r1.isSection() && (r1.keyCompare(&r2) < 0))) {
w.writeSection(r1.getSection());
w.writeDelLine(r1.getLine());
r1.readNextLine();
continue;
}
if (r1.isSection() || (!r2.isSection() && (r1.keyCompare(&r2) > 0))) {
w.writeSection(r2.getSection());
w.writeLine(r2.getLine());
r2.readNextLine();
continue;
}
if (!r1.areValuesEqual(&r2)) {
w.writeSection(r2.getSection());
if (opt.comments)
w.writeComment(r1.getLine());
w.writeLine(r2.getLine());
}
// same key, same or different values
r1.readNextLine();
r2.readNextLine();
}
r1.close();
r2.close();
w.close();
return rv;
}
static DWORD diff(int argc, TCHAR *const *argv, int toptind) {
DWORD rv;
TCHAR *file1 = NULL;
TCHAR *file2 = NULL;
if (toptind >= argc)
usage(1);
TCHAR *file1_key = argv[toptind++];
if (toptind >= argc)
usage(1);
TCHAR *file2_key = argv[toptind++];
HKEY file1_hkey = NULL;
TCHAR *p;
TCHAR *file1_subkey = NULL;
if (_tcsnicmp(file1_key, _T("HK"), 2) == 0) {
p = _tcschr(file1_key, '\\');
if (p != NULL) {
*p = '\0';
++p;
file1_subkey = p;
file1_hkey = (HKEY) name_hkey[file1_key];
}
}
TCHAR *file2_subkey = NULL;
HKEY file2_hkey = NULL;
if (_tcsnicmp(file1_key, _T("HK"), 2) == 0) {
p = _tcschr(file2_key, '\\');
if (p != NULL) {
*p = '\0';
++p;
file2_subkey = p;
file2_hkey = (HKEY) name_hkey[file2_key];
}
}
if (file1_hkey) {
file1 = get_temp_name(_T("rd1"));
opt.quirks = 0;
opt.regedit = 0;
opt.unsorted = 0;
rv = export_file(file1, file1_key);
if (rv)
return rv;
} else {
file1 = file1_key;
}
if (file2_hkey) {
file2 = get_temp_name(_T("rd2"));
opt.quirks = 0;
opt.regedit = 0;
opt.unsorted = 0;
rv = export_file(file2, file2_key);
if (rv)
return rv;
} else {
file2 = file2_key;
}
rv = diff_files(file1, file2);
return rv;
}
static DWORD execute(const int argc, TCHAR *const *argv, int toptind) {
DWORD rv = 0;
TCHAR *before_file = get_temp_name(_T("re1"));
rv = export2(argc, argv, toptind, before_file);
if (rv)
return rv;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
BOOL b = CreateProcess(
NULL, // LPCTSTR lpApplicationName,
opt.execute, // LPTSTR lpCommandLine,
NULL, // LPSECURITY_ATTRIBUTES lpProcessAttributes,
NULL, // LPSECURITY_ATTRIBUTES lpThreadAttributes,
true, // BOOL bInheritHandles,
0, // DWORD dwCreationFlags,
NULL, // LPVOID lpEnvironment,
NULL, // LPCTSTR lpCurrentDirectory,
&si, // LPSTARTUPINFO lpStartupInfo,
&pi // LPPROCESS_INFORMATION lpProcessInformation
);
if (!b) {
rv = GetLastError();
_ftprintf(stderr, _T("Failed to execute '%s': Error %d: %s"), opt.execute, rv, SystemMessage(rv));
return rv;
}
// Wait until child process exits.
DWORD dw = WaitForSingleObject(pi.hProcess, INFINITE);
if (dw == WAIT_FAILED) {
_ftprintf(stderr, _T("Failed to wait on '%s': Error %d: %s"), opt.execute, rv, SystemMessage(rv));
return dw;
}
b = GetExitCodeProcess(pi.hProcess, &dw);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
TCHAR *after_file = get_temp_name(_T("re2"));
rv = export2(argc, argv, toptind, after_file);
if (rv)
return rv;
rv = diff_files(before_file, after_file);
return rv;
}
int _tmain(int argc, TCHAR *const *argv) {
DWORD rv = 0;
#ifdef _DEBUG_ALLOC
InitAllocCheck();
#endif
progname(argv[0]);
name_hkey[_T("HKCR")] = HKEY_CLASSES_ROOT;
name_hkey[_T("HKCC")] = HKEY_CURRENT_CONFIG;
name_hkey[_T("HKCU")] = HKEY_CURRENT_USER;
name_hkey[_T("HKLM")] = HKEY_LOCAL_MACHINE;
name_hkey[_T("HKU")] = HKEY_USERS;
name_hkey[_T("HKEY_CLASSES_ROOT")] = HKEY_CLASSES_ROOT;
name_hkey[_T("HKEY_CURRENT_CONFIG")] = HKEY_CURRENT_CONFIG;
name_hkey[_T("HKEY_CURRENT_USER")] = HKEY_CURRENT_USER;
name_hkey[_T("HKEY_LOCAL_MACHINE")] = HKEY_LOCAL_MACHINE;
name_hkey[_T("HKEY_USERS")] = HKEY_USERS;
topterr = 0;
toptind = 1;
while (1) {
int c;
int option_index = 0;
if (toptind < argc && argv[toptind] && argv[toptind][0] == '/')
argv[toptind][0] = '-';
c = tgetopt_long(argc, argv, short_options, long_options, &option_index);
if (topterr) {
usage(1);
}
if (c == -1)
break;
switch (c) {
case 'a': // all
opt.all = true;
opt.o_export = true;
break;
/*
case 'e': // execute
opt.execute = toptarg;
break;
*/
case 'o': // output
opt.output = toptarg;
break;
case 'c': // comments
opt.comments = true;
break;
/*
case 'q': // quirks
opt.quirks = true;
break;
*/
case 'r': // regedit
opt.regedit = true;
opt.quirks = true;
opt.unsorted = true;
opt.wordwrap = true;
break;
/*
case 's': // sort
opt.sort = true;
break;
*/
case 'u': // unsorted
opt.unsorted = true;
break;
case 'v': // verbose
opt.verbose = true;
break;
case 'w': // wordwrap
opt.wordwrap = true;
break;
case 'x': // export
opt.o_export = true;
break;
case 'V': // version
_tprintf(_T("%s - Version %s - %s\n"), APPNAME, APPVERSION, _T(__DATE__)
#ifdef _DEBUG
_T(" ") _T(__TIME__) _T(" (Debug build)")
#endif
);
_tprintf(APPCOPYRIGHT);
exit(0);
break;
case '?': // help
usage(0);
break;
case ':':
_ftprintf(stderr, _T("Option -%c requires an operand\n"), toptopt);
// fallthrough
default:
usage(1);
}
}
if (opt.execute) {
return execute(argc, argv, toptind);
}
if (opt.o_export) {
return do_export(argc, argv, toptind);
}
if (opt.sort) {
return sort(argc, argv, toptind);
}
return diff(argc, argv, toptind);
// _getch();
return rv;
}