-
Notifications
You must be signed in to change notification settings - Fork 2
/
regproc.cpp
1535 lines (1289 loc) · 39.8 KB
/
regproc.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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Registry processing routines. Routines, common for registry
* processing frontends.
*
* Copyright 1999 Sylvain St-Germain
* Copyright 2002 Andriy Palamarchuk
* Copyright 2008 Alexander N. Sørnes <alex@thehandofagony.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdio.h>
#include <windows.h>
#include <tchar.h>
#define REG_VAL_BUF_SIZE 4096
static HKEY reg_class_keys[] = {
HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS,
#ifndef _WIN32_WCE
HKEY_CURRENT_CONFIG, HKEY_DYN_DATA,
#endif
};
static const TCHAR *reg_class_names[] = {
_T("HKEY_CLASSES_ROOT"), _T("HKEY_CURRENT_USER"), _T("HKEY_LOCAL_MACHINE"), _T("HKEY_USERS"),
#ifndef _WIN32_WCE
_T("HKEY_CURRENT_CONFIG"), _T("HKEY_DYN_DATA"),
#endif
};
/******************************************************************************
* Allocates memory and converts input from multibyte to wide chars
* Returned string must be freed by the caller
*/
static WCHAR* GetWideString(const char* strA)
{
if(strA)
{
WCHAR* strW;
int len = MultiByteToWideChar(CP_ACP, 0, strA, -1, NULL, 0);
strW = (WCHAR*)malloc(len * sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, strA, -1, strW, len);
return strW;
}
return NULL;
}
/******************************************************************************
* Allocates memory and converts input from multibyte to wide chars
* Returned string must be freed by the caller
*/
static WCHAR* GetWideStringN(const char* strA, int chars, DWORD *len)
{
if(strA)
{
WCHAR* strW;
*len = MultiByteToWideChar(CP_ACP, 0, strA, chars, NULL, 0);
strW = (WCHAR*)malloc(*len * sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, strA, chars, strW, *len);
return strW;
}
*len = 0;
return NULL;
}
/******************************************************************************
* Allocates memory and converts input from wide chars to multibyte
* Returned string must be freed by the caller
*/
char* GetMultiByteString(const WCHAR* strW)
{
if(strW)
{
char* strA;
int len = WideCharToMultiByte(CP_ACP, 0, strW, -1, NULL, 0, NULL, NULL);
strA = (char*)malloc(len);
WideCharToMultiByte(CP_ACP, 0, strW, -1, strA, len, NULL, NULL);
return strA;
}
return NULL;
}
/******************************************************************************
* Allocates memory and converts input from wide chars to multibyte
* Returned string must be freed by the caller
*/
static char* GetMultiByteStringN(const WCHAR* strW, int chars, DWORD* len)
{
if(strW)
{
char* strA;
*len = WideCharToMultiByte(CP_ACP, 0, strW, chars, NULL, 0, NULL, NULL);
strA = (char*)malloc(*len);
WideCharToMultiByte(CP_ACP, 0, strW, chars, strA, *len, NULL, NULL);
return strA;
}
*len = 0;
return NULL;
}
static TCHAR *(*get_line)(FILE *);
/* parser definitions */
enum parser_state
{
HEADER, /* parsing the registry file version header */
PARSE_WIN31_LINE, /* parsing a Windows 3.1 registry line */
LINE_START, /* at the beginning of a registry line */
KEY_NAME, /* parsing a key name */
DELETE_KEY, /* deleting a registry key */
DEFAULT_VALUE_NAME, /* parsing a default value name */
QUOTED_VALUE_NAME, /* parsing a double-quoted value name */
DATA_START, /* preparing for data parsing operations */
DELETE_VALUE, /* deleting a registry value */
DATA_TYPE, /* parsing the registry data type */
STRING_DATA, /* parsing REG_SZ data */
DWORD_DATA, /* parsing DWORD data */
HEX_DATA, /* parsing REG_BINARY, REG_NONE, REG_EXPAND_SZ or REG_MULTI_SZ data */
EOL_BACKSLASH, /* preparing to parse multiple lines of hex data */
HEX_MULTILINE, /* parsing multiple lines of hex data */
UNKNOWN_DATA, /* parsing an unhandled or invalid data type */
SET_VALUE, /* adding a value to the registry */
NB_PARSER_STATES
};
struct parser
{
FILE *file; /* pointer to a registry file */
TCHAR two_wchars[2]; /* first two characters from the encoding check */
BOOL is_unicode; /* parsing Unicode or ASCII data */
short int reg_version; /* registry file version */
HKEY hkey; /* current registry key */
TCHAR *key_name; /* current key name */
TCHAR *value_name; /* value name */
DWORD parse_type; /* generic data type for parsing */
DWORD data_type; /* data type */
void *data; /* value data */
DWORD data_size; /* size of the data (in bytes) */
BOOL backslash; /* TRUE if the current line contains a backslash */
enum parser_state state; /* current parser state */
};
typedef TCHAR *(*parser_state_func)(struct parser *parser, TCHAR *pos);
/* parser state machine functions */
static TCHAR *header_state(struct parser *parser, TCHAR *pos);
static TCHAR *parse_win31_line_state(struct parser *parser, TCHAR *pos);
static TCHAR *line_start_state(struct parser *parser, TCHAR *pos);
static TCHAR *key_name_state(struct parser *parser, TCHAR *pos);
static TCHAR *delete_key_state(struct parser *parser, TCHAR *pos);
static TCHAR *default_value_name_state(struct parser *parser, TCHAR *pos);
static TCHAR *quoted_value_name_state(struct parser *parser, TCHAR *pos);
static TCHAR *data_start_state(struct parser *parser, TCHAR *pos);
static TCHAR *delete_value_state(struct parser *parser, TCHAR *pos);
static TCHAR *data_type_state(struct parser *parser, TCHAR *pos);
static TCHAR *string_data_state(struct parser *parser, TCHAR *pos);
static TCHAR *dword_data_state(struct parser *parser, TCHAR *pos);
static TCHAR *hex_data_state(struct parser *parser, TCHAR *pos);
static TCHAR *eol_backslash_state(struct parser *parser, TCHAR *pos);
static TCHAR *hex_multiline_state(struct parser *parser, TCHAR *pos);
static TCHAR *unknown_data_state(struct parser *parser, TCHAR *pos);
static TCHAR *set_value_state(struct parser *parser, TCHAR *pos);
static const parser_state_func parser_funcs[NB_PARSER_STATES] =
{
header_state, /* HEADER */
parse_win31_line_state, /* PARSE_WIN31_LINE */
line_start_state, /* LINE_START */
key_name_state, /* KEY_NAME */
delete_key_state, /* DELETE_KEY */
default_value_name_state, /* DEFAULT_VALUE_NAME */
quoted_value_name_state, /* QUOTED_VALUE_NAME */
data_start_state, /* DATA_START */
delete_value_state, /* DELETE_VALUE */
data_type_state, /* DATA_TYPE */
string_data_state, /* STRING_DATA */
dword_data_state, /* DWORD_DATA */
hex_data_state, /* HEX_DATA */
eol_backslash_state, /* EOL_BACKSLASH */
hex_multiline_state, /* HEX_MULTILINE */
unknown_data_state, /* UNKNOWN_DATA */
set_value_state, /* SET_VALUE */
};
/* set the new parser state and return the previous one */
static inline enum parser_state set_state(struct parser *parser, enum parser_state state)
{
enum parser_state ret = parser->state;
parser->state = state;
return ret;
}
/******************************************************************************
* Converts a hex representation of a DWORD into a DWORD.
*/
static BOOL convert_hex_to_dword(TCHAR *str, DWORD *dw)
{
TCHAR *p, *end;
int count = 0;
while (*str == ' ' || *str == '\t') str++;
if (!*str) goto error;
p = str;
while (_istxdigit(*p))
{
count++;
p++;
}
if (count > 8) goto error;
end = p;
while (*p == ' ' || *p == '\t') p++;
if (*p && *p != ';') goto error;
*end = 0;
*dw = _tcstoul(str, &end, 16);
return TRUE;
error:
return FALSE;
}
/******************************************************************************
* Converts comma-separated hex data into a binary string and modifies
* the input parameter to skip the concatenating backslash, if found.
*
* Returns TRUE or FALSE to indicate whether parsing was successful.
*/
static BOOL convert_hex_csv_to_hex(struct parser *parser, TCHAR **str)
{
size_t size;
BYTE *d;
TCHAR *s;
parser->backslash = FALSE;
/* The worst case is 1 digit + 1 comma per byte */
size = ((_tcslen(*str) + 1) / 2) + parser->data_size;
parser->data = realloc(parser->data, size);
s = *str;
d = (BYTE *)parser->data + parser->data_size;
while (*s)
{
TCHAR *end;
unsigned long wc;
wc = _tcstoul(s, &end, 16);
if (wc > 0xff) return FALSE;
if (s == end && wc == 0)
{
while (*end == ' ' || *end == '\t') end++;
if (*end == '\\')
{
parser->backslash = TRUE;
*str = end + 1;
return TRUE;
}
else if (*end == ';')
return TRUE;
return FALSE;
}
*d++ = (BYTE)wc;
parser->data_size++;
if (*end && *end != ',')
{
while (*end == ' ' || *end == '\t') end++;
if (*end && *end != ';') return FALSE;
return TRUE;
}
if (*end) end++;
s = end;
}
return TRUE;
}
/******************************************************************************
* Parses the data type of the registry value being imported and modifies
* the input parameter to skip the string representation of the data type.
*
* Returns TRUE or FALSE to indicate whether a data type was found.
*/
static BOOL parse_data_type(struct parser *parser, TCHAR **line)
{
struct data_type { const TCHAR *tag; int len; int type; int parse_type; };
static const struct data_type data_types[] = {
/* tag len type parse type */
{ _T("\""), 1, REG_SZ, REG_SZ },
{ _T("hex:"), 4, REG_BINARY, REG_BINARY },
{ _T("dword:"), 6, REG_DWORD, REG_DWORD },
{ _T("hex("), 4, -1, REG_BINARY }, /* REG_NONE, REG_EXPAND_SZ, REG_MULTI_SZ */
{ NULL, 0, 0, 0 }
};
const struct data_type *ptr;
for (ptr = data_types; ptr->tag; ptr++)
{
if (_tcsncmp(ptr->tag, *line, ptr->len))
continue;
parser->parse_type = ptr->parse_type;
parser->data_type = ptr->parse_type;
*line += ptr->len;
if (ptr->type == -1)
{
TCHAR *end;
DWORD val;
if (!**line || _totlower((*line)[1]) == 'x')
return FALSE;
/* "hex(xx):" is special */
val = _tcstoul(*line, &end, 16);
if (*end != ')' || *(end + 1) != ':' || (val == ~0u))
return FALSE;
parser->data_type = val;
*line = end + 2;
}
return TRUE;
}
return FALSE;
}
/******************************************************************************
* Replaces escape sequences with their character equivalents and
* null-terminates the string on the first non-escaped double quote.
*
* Assigns a pointer to the remaining unparsed data in the line.
* Returns TRUE or FALSE to indicate whether a closing double quote was found.
*/
static BOOL REGPROC_unescape_string(TCHAR *str, TCHAR **unparsed)
{
int str_idx = 0; /* current character under analysis */
int val_idx = 0; /* the last character of the unescaped string */
int len = _tcslen(str);
BOOL ret;
for (str_idx = 0; str_idx < len; str_idx++, val_idx++) {
if (str[str_idx] == '\\') {
str_idx++;
switch (str[str_idx]) {
case 'n':
str[val_idx] = '\n';
break;
case 'r':
str[val_idx] = '\r';
break;
case '0':
return FALSE;
case '\\':
case '"':
str[val_idx] = str[str_idx];
break;
default:
if (!str[str_idx]) return FALSE;
//output_message(STRING_ESCAPE_SEQUENCE, str[str_idx]);
str[val_idx] = str[str_idx];
break;
}
} else if (str[str_idx] == '"') {
break;
} else {
str[val_idx] = str[str_idx];
}
}
ret = (str[str_idx] == '"');
*unparsed = str + str_idx + 1;
str[val_idx] = '\0';
return ret;
}
static HKEY parse_key_name(TCHAR *key_name, TCHAR **key_path)
{
unsigned int i;
if (!key_name) return 0;
*key_path = _tcschr(key_name, '\\');
if (*key_path) (*key_path)++;
for (i = 0; i < _countof(reg_class_keys); i++)
{
int len = _tcslen(reg_class_names[i]);
if (!_tcsnicmp(key_name, reg_class_names[i], len) &&
(key_name[len] == 0 || key_name[len] == '\\'))
{
return reg_class_keys[i];
}
}
return 0;
}
static void close_key(struct parser *parser)
{
if (parser->hkey)
{
free(parser->key_name);
parser->key_name = NULL;
RegCloseKey(parser->hkey);
parser->hkey = NULL;
}
}
/******************************************************************************
* Opens the registry key given by the input path.
* This key must be closed by calling close_key().
*/
static LONG open_key(struct parser *parser, TCHAR *path)
{
HKEY key_class;
TCHAR *key_path;
LONG res;
close_key(parser);
/* Get the registry class */
if (!path || !(key_class = parse_key_name(path, &key_path)))
return ERROR_INVALID_PARAMETER;
res = RegCreateKeyEx(key_class, key_path, 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, NULL, &parser->hkey, NULL);
if (res == ERROR_SUCCESS)
{
parser->key_name = (TCHAR *)malloc((_tcslen(path) + 1) * sizeof(TCHAR));
_tcscpy(parser->key_name, path);
}
else
parser->hkey = NULL;
return res;
}
static void free_parser_data(struct parser *parser)
{
if (parser->parse_type == REG_DWORD || parser->parse_type == REG_BINARY)
free(parser->data);
parser->data = NULL;
parser->data_size = 0;
}
static void prepare_hex_string_data(struct parser *parser)
{
if (parser->data_type == REG_EXPAND_SZ || parser->data_type == REG_MULTI_SZ ||
parser->data_type == REG_SZ)
{
if (parser->is_unicode)
{
TCHAR *data = (TCHAR *)parser->data;
DWORD len = parser->data_size / sizeof(TCHAR);
if (data[len - 1] != 0)
{
data[len] = 0;
parser->data_size += sizeof(TCHAR);
}
}
else
{
BYTE *data = (BYTE *)parser->data;
if (data[parser->data_size - 1] != 0)
{
data[parser->data_size] = 0;
parser->data_size++;
}
#ifdef _UNICODE
parser->data = GetWideStringN((char*)parser->data, parser->data_size, &parser->data_size);
parser->data_size *= sizeof(TCHAR);
free(data);
#endif
}
}
}
enum reg_versions {
REG_VERSION_31,
REG_VERSION_40,
REG_VERSION_50,
REG_VERSION_FUZZY,
REG_VERSION_INVALID
};
static enum reg_versions parse_file_header(const TCHAR *s)
{
static const TCHAR header_31[] = _T("REGEDIT");
while (*s == ' ' || *s == '\t') s++;
if (!_tcscmp(s, header_31))
return REG_VERSION_31;
if (!_tcscmp(s, _T("REGEDIT4")))
return REG_VERSION_40;
if (!_tcscmp(s, _T("Windows Registry Editor Version 5.00")))
return REG_VERSION_50;
/* The Windows version accepts registry file headers beginning with "REGEDIT" and ending
* with other characters, as long as "REGEDIT" appears at the start of the line. For example,
* "REGEDIT 4", "REGEDIT9" and "REGEDIT4FOO" are all treated as valid file headers.
* In all such cases, however, the contents of the registry file are not imported.
*/
if (!_tcsncmp(s, header_31, 7)) /* "REGEDIT" without NUL */
return REG_VERSION_FUZZY;
return REG_VERSION_INVALID;
}
/* handler for parser HEADER state */
static TCHAR *header_state(struct parser *parser, TCHAR *pos)
{
TCHAR *line, *header;
if (!(line = get_line(parser->file)))
return NULL;
if (!parser->is_unicode)
{
header = (TCHAR *)malloc((_tcslen(line) + 3) * sizeof(TCHAR));
header[0] = parser->two_wchars[0];
header[1] = parser->two_wchars[1];
_tcscpy(header + 2, line);
parser->reg_version = parse_file_header(header);
free(header);
}
else parser->reg_version = parse_file_header(line);
switch (parser->reg_version)
{
case REG_VERSION_31:
set_state(parser, PARSE_WIN31_LINE);
break;
case REG_VERSION_40:
case REG_VERSION_50:
set_state(parser, LINE_START);
break;
default:
get_line(NULL); /* Reset static variables */
return NULL;
}
return line;
}
/* handler for parser PARSE_WIN31_LINE state */
static TCHAR *parse_win31_line_state(struct parser *parser, TCHAR *pos)
{
TCHAR *line, *value;
static TCHAR hkcr[] = _T("HKEY_CLASSES_ROOT");
unsigned int key_end = 0;
if (!(line = get_line(parser->file)))
return NULL;
if (_tcsncmp(line, hkcr, _tcslen(hkcr)))
return line;
/* get key name */
while (line[key_end] && !_istspace(line[key_end])) key_end++;
value = line + key_end;
while (*value == ' ' || *value == '\t') value++;
if (*value == '=') value++;
if (*value == ' ') value++; /* at most one space is skipped */
line[key_end] = 0;
if (open_key(parser, line) != ERROR_SUCCESS)
{
//output_message(STRING_OPEN_KEY_FAILED, line);
return line;
}
parser->value_name = NULL;
parser->data_type = REG_SZ;
parser->data = value;
parser->data_size = (_tcslen(value) + 1) * sizeof(TCHAR);
set_state(parser, SET_VALUE);
return value;
}
/* handler for parser LINE_START state */
static TCHAR *line_start_state(struct parser *parser, TCHAR *pos)
{
TCHAR *line, *p;
if (!(line = get_line(parser->file)))
return NULL;
for (p = line; *p; p++)
{
switch (*p)
{
case '[':
set_state(parser, KEY_NAME);
return p + 1;
case '@':
set_state(parser, DEFAULT_VALUE_NAME);
return p;
case '"':
set_state(parser, QUOTED_VALUE_NAME);
return p + 1;
case ' ':
case '\t':
break;
default:
return p;
}
}
return p;
}
/* handler for parser KEY_NAME state */
static TCHAR *key_name_state(struct parser *parser, TCHAR *pos)
{
TCHAR *p = pos, *key_end;
if (*p == ' ' || *p == '\t' || !(key_end = _tcsrchr(p, ']')))
goto done;
*key_end = 0;
if (*p == '-')
{
set_state(parser, DELETE_KEY);
return p + 1;
}
else if (open_key(parser, p) != ERROR_SUCCESS)
{
//output_message(STRING_OPEN_KEY_FAILED, p);
}
done:
set_state(parser, LINE_START);
return p;
}
/******************************************************************************
* Removes the registry key with all subkeys. Parses full key name.
*
* Parameters:
* reg_key_name - full name of registry branch to delete. Ignored if is NULL,
* empty, points to register key class, does not exist.
*/
void delete_registry_key(TCHAR *reg_key_name)
{
TCHAR *key_name = NULL;
HKEY key_class;
if (!reg_key_name || !reg_key_name[0])
return;
if (!(key_class = parse_key_name(reg_key_name, &key_name)))
{
if (key_name) *(key_name - 1) = 0;
return;
}
if (!key_name || !*key_name)
return;
#ifdef _WIN32_WCE
RegDeleteKey(key_class, key_name);
#else
RegDeleteTree(key_class, key_name);
#endif
}
/* handler for parser DELETE_KEY state */
static TCHAR *delete_key_state(struct parser *parser, TCHAR *pos)
{
TCHAR *p = pos;
close_key(parser);
if (*p == 'H' || *p == 'h')
delete_registry_key(p);
set_state(parser, LINE_START);
return p;
}
/* handler for parser DEFAULT_VALUE_NAME state */
static TCHAR *default_value_name_state(struct parser *parser, TCHAR *pos)
{
free(parser->value_name);
parser->value_name = NULL;
set_state(parser, DATA_START);
return pos + 1;
}
/* handler for parser QUOTED_VALUE_NAME state */
static TCHAR *quoted_value_name_state(struct parser *parser, TCHAR *pos)
{
TCHAR *val_name = pos, *p;
free(parser->value_name);
parser->value_name = NULL;
if (!REGPROC_unescape_string(val_name, &p))
goto invalid;
/* copy the value name in case we need to parse multiple lines and the buffer is overwritten */
parser->value_name = (TCHAR *)malloc((_tcslen(val_name) + 1) * sizeof(TCHAR));
_tcscpy(parser->value_name, val_name);
set_state(parser, DATA_START);
return p;
invalid:
set_state(parser, LINE_START);
return val_name;
}
/* handler for parser DATA_START state */
static TCHAR *data_start_state(struct parser *parser, TCHAR *pos)
{
TCHAR *p = pos;
unsigned int len;
while (*p == ' ' || *p == '\t') p++;
if (*p != '=') goto done;
p++;
while (*p == ' ' || *p == '\t') p++;
/* trim trailing whitespace */
len = _tcslen(p);
while (len > 0 && (p[len - 1] == ' ' || p[len - 1] == '\t')) len--;
p[len] = 0;
if (*p == '-')
set_state(parser, DELETE_VALUE);
else
set_state(parser, DATA_TYPE);
return p;
done:
set_state(parser, LINE_START);
return p;
}
/* handler for parser DELETE_VALUE state */
static TCHAR *delete_value_state(struct parser *parser, TCHAR *pos)
{
TCHAR *p = pos + 1;
while (*p == ' ' || *p == '\t') p++;
if (*p && *p != ';') goto done;
RegDeleteValue(parser->hkey, parser->value_name);
done:
set_state(parser, LINE_START);
return p;
}
/* handler for parser DATA_TYPE state */
static TCHAR *data_type_state(struct parser *parser, TCHAR *pos)
{
TCHAR *line = pos;
if (!parse_data_type(parser, &line))
{
set_state(parser, LINE_START);
return line;
}
switch (parser->parse_type)
{
case REG_SZ:
set_state(parser, STRING_DATA);
break;
case REG_DWORD:
set_state(parser, DWORD_DATA);
break;
case REG_BINARY: /* all hex data types, including undefined */
set_state(parser, HEX_DATA);
break;
default:
set_state(parser, UNKNOWN_DATA);
}
return line;
}
/* handler for parser STRING_DATA state */
static TCHAR *string_data_state(struct parser *parser, TCHAR *pos)
{
TCHAR *line;
parser->data = pos;
if (!REGPROC_unescape_string((TCHAR *)parser->data, &line))
goto invalid;
while (*line == ' ' || *line == '\t') line++;
if (*line && *line != ';') goto invalid;
parser->data_size = (_tcslen((TCHAR *)parser->data) + 1) * sizeof(TCHAR);
set_state(parser, SET_VALUE);
return line;
invalid:
free_parser_data(parser);
set_state(parser, LINE_START);
return line;
}
/* handler for parser DWORD_DATA state */
static TCHAR *dword_data_state(struct parser *parser, TCHAR *pos)
{
TCHAR *line = pos;
parser->data = malloc(sizeof(DWORD));
if (!convert_hex_to_dword(line, (DWORD *)parser->data))
goto invalid;
parser->data_size = sizeof(DWORD);
set_state(parser, SET_VALUE);
return line;
invalid:
free_parser_data(parser);
set_state(parser, LINE_START);
return line;
}
/* handler for parser HEX_DATA state */
static TCHAR *hex_data_state(struct parser *parser, TCHAR *pos)
{
TCHAR *line = pos;
if (!*line)
goto set_value;
if (!convert_hex_csv_to_hex(parser, &line))
goto invalid;
if (parser->backslash)
{
set_state(parser, EOL_BACKSLASH);
return line;
}
prepare_hex_string_data(parser);
set_value:
set_state(parser, SET_VALUE);
return line;
invalid:
free_parser_data(parser);
set_state(parser, LINE_START);
return line;
}
/* handler for parser EOL_BACKSLASH state */
static TCHAR *eol_backslash_state(struct parser *parser, TCHAR *pos)
{
TCHAR *p = pos;
while (*p == ' ' || *p == '\t') p++;
if (*p && *p != ';') goto invalid;
set_state(parser, HEX_MULTILINE);
return pos;
invalid:
free_parser_data(parser);
set_state(parser, LINE_START);
return p;
}
/* handler for parser HEX_MULTILINE state */
static TCHAR *hex_multiline_state(struct parser *parser, TCHAR *pos)
{
TCHAR *line;
if (!(line = get_line(parser->file)))
{
prepare_hex_string_data(parser);
set_state(parser, SET_VALUE);
return pos;
}
while (*line == ' ' || *line == '\t') line++;
if (!*line || *line == ';') return line;
if (!iswxdigit(*line)) goto invalid;
set_state(parser, HEX_DATA);
return line;
invalid:
free_parser_data(parser);
set_state(parser, LINE_START);
return line;
}
/* handler for parser UNKNOWN_DATA state */
static TCHAR *unknown_data_state(struct parser *parser, TCHAR *pos)
{
//output_message(STRING_UNKNOWN_DATA_FORMAT, parser->data_type);
set_state(parser, LINE_START);
return pos;
}
/* handler for parser SET_VALUE state */
static TCHAR *set_value_state(struct parser *parser, TCHAR *pos)
{
RegSetValueEx(parser->hkey, parser->value_name, 0, parser->data_type,
(BYTE *)parser->data, parser->data_size);
free_parser_data(parser);
if (parser->reg_version == REG_VERSION_31)
set_state(parser, PARSE_WIN31_LINE);
else
set_state(parser, LINE_START);
return pos;
}
static TCHAR *get_lineA(FILE *fp)
{
static TCHAR *lineW;
static size_t size;
static char *buf, *next;
char *line;
free(lineW);
if (!fp) goto cleanup;
if (!size)
{
size = REG_VAL_BUF_SIZE;
buf = (char *)malloc(size);
*buf = 0;
next = buf;
}
line = next;
while (next)
{
char *p = strpbrk(line, "\r\n");
if (!p)
{
size_t len, count;
len = strlen(next);
memmove(buf, next, len + 1);
if (size - len < 3)
{
size *= 2;
buf = (char *)realloc(buf, size);
}
if (!(count = fread(buf + len, 1, size - len - 1, fp)))
{
next = NULL;
#ifdef _UNICODE
lineW = GetWideString(buf);