qcap/tests: Add media tests for the SmartTee filter.
[wine/multimedia.git] / programs / regedit / regproc.c
blob643b559f4f1f29c2b27bdd883444c3e6ab6e176d
1 /*
2 * Registry processing routines. Routines, common for registry
3 * processing frontends.
5 * Copyright 1999 Sylvain St-Germain
6 * Copyright 2002 Andriy Palamarchuk
7 * Copyright 2008 Alexander N. Sørnes <alex@thehandofagony.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <io.h>
29 #include <windows.h>
30 #include <winnt.h>
31 #include <winreg.h>
32 #include <assert.h>
33 #include <wine/unicode.h>
34 #include "regproc.h"
36 #define REG_VAL_BUF_SIZE 4096
38 /* maximal number of characters in hexadecimal data line,
39 * including the indentation, but not including the '\' character
41 #define REG_FILE_HEX_LINE_LEN (2 + 25 * 3)
43 extern const WCHAR* reg_class_namesW[];
45 static HKEY reg_class_keys[] = {
46 HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CLASSES_ROOT,
47 HKEY_CURRENT_CONFIG, HKEY_CURRENT_USER, HKEY_DYN_DATA
50 #define REG_CLASS_NUMBER (sizeof(reg_class_keys) / sizeof(reg_class_keys[0]))
52 /* return values */
53 #define NOT_ENOUGH_MEMORY 1
54 #define IO_ERROR 2
56 /* processing macros */
58 /* common check of memory allocation results */
59 #define CHECK_ENOUGH_MEMORY(p) \
60 if (!(p)) \
61 { \
62 fprintf(stderr,"%s: file %s, line %d: Not enough memory\n", \
63 getAppName(), __FILE__, __LINE__); \
64 exit(NOT_ENOUGH_MEMORY); \
67 /******************************************************************************
68 * Allocates memory and converts input from multibyte to wide chars
69 * Returned string must be freed by the caller
71 WCHAR* GetWideString(const char* strA)
73 if(strA)
75 WCHAR* strW;
76 int len = MultiByteToWideChar(CP_ACP, 0, strA, -1, NULL, 0);
78 strW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
79 CHECK_ENOUGH_MEMORY(strW);
80 MultiByteToWideChar(CP_ACP, 0, strA, -1, strW, len);
81 return strW;
83 return NULL;
86 /******************************************************************************
87 * Allocates memory and converts input from multibyte to wide chars
88 * Returned string must be freed by the caller
90 static WCHAR* GetWideStringN(const char* strA, int chars, DWORD *len)
92 if(strA)
94 WCHAR* strW;
95 *len = MultiByteToWideChar(CP_ACP, 0, strA, chars, NULL, 0);
97 strW = HeapAlloc(GetProcessHeap(), 0, *len * sizeof(WCHAR));
98 CHECK_ENOUGH_MEMORY(strW);
99 MultiByteToWideChar(CP_ACP, 0, strA, chars, strW, *len);
100 return strW;
102 *len = 0;
103 return NULL;
106 /******************************************************************************
107 * Allocates memory and converts input from wide chars to multibyte
108 * Returned string must be freed by the caller
110 char* GetMultiByteString(const WCHAR* strW)
112 if(strW)
114 char* strA;
115 int len = WideCharToMultiByte(CP_ACP, 0, strW, -1, NULL, 0, NULL, NULL);
117 strA = HeapAlloc(GetProcessHeap(), 0, len);
118 CHECK_ENOUGH_MEMORY(strA);
119 WideCharToMultiByte(CP_ACP, 0, strW, -1, strA, len, NULL, NULL);
120 return strA;
122 return NULL;
125 /******************************************************************************
126 * Allocates memory and converts input from wide chars to multibyte
127 * Returned string must be freed by the caller
129 static char* GetMultiByteStringN(const WCHAR* strW, int chars, DWORD* len)
131 if(strW)
133 char* strA;
134 *len = WideCharToMultiByte(CP_ACP, 0, strW, chars, NULL, 0, NULL, NULL);
136 strA = HeapAlloc(GetProcessHeap(), 0, *len);
137 CHECK_ENOUGH_MEMORY(strA);
138 WideCharToMultiByte(CP_ACP, 0, strW, chars, strA, *len, NULL, NULL);
139 return strA;
141 *len = 0;
142 return NULL;
145 /******************************************************************************
146 * Converts a hex representation of a DWORD into a DWORD.
148 static BOOL convertHexToDWord(WCHAR* str, DWORD *dw)
150 char buf[9];
151 char dummy;
153 WideCharToMultiByte(CP_ACP, 0, str, -1, buf, 9, NULL, NULL);
154 if (lstrlenW(str) > 8 || sscanf(buf, "%x%c", dw, &dummy) != 1) {
155 fprintf(stderr,"%s: ERROR, invalid hex value\n", getAppName());
156 return FALSE;
158 return TRUE;
161 /******************************************************************************
162 * Converts a hex comma separated values list into a binary string.
164 static BYTE* convertHexCSVToHex(WCHAR *str, DWORD *size)
166 WCHAR *s;
167 BYTE *d, *data;
169 /* The worst case is 1 digit + 1 comma per byte */
170 *size=(lstrlenW(str)+1)/2;
171 data=HeapAlloc(GetProcessHeap(), 0, *size);
172 CHECK_ENOUGH_MEMORY(data);
174 s = str;
175 d = data;
176 *size=0;
177 while (*s != '\0') {
178 UINT wc;
179 WCHAR *end;
181 wc = strtoulW(s,&end,16);
182 if (end == s || wc > 0xff || (*end && *end != ',')) {
183 char* strA = GetMultiByteString(s);
184 fprintf(stderr,"%s: ERROR converting CSV hex stream. Invalid value at '%s'\n",
185 getAppName(), strA);
186 HeapFree(GetProcessHeap(), 0, data);
187 HeapFree(GetProcessHeap(), 0, strA);
188 return NULL;
190 *d++ =(BYTE)wc;
191 (*size)++;
192 if (*end) end++;
193 s = end;
196 return data;
199 /******************************************************************************
200 * This function returns the HKEY associated with the data type encoded in the
201 * value. It modifies the input parameter (key value) in order to skip this
202 * "now useless" data type information.
204 * Note: Updated based on the algorithm used in 'server/registry.c'
206 static DWORD getDataType(LPWSTR *lpValue, DWORD* parse_type)
208 struct data_type { const WCHAR *tag; int len; int type; int parse_type; };
210 static const WCHAR quote[] = {'"'};
211 static const WCHAR str[] = {'s','t','r',':','"'};
212 static const WCHAR str2[] = {'s','t','r','(','2',')',':','"'};
213 static const WCHAR hex[] = {'h','e','x',':'};
214 static const WCHAR dword[] = {'d','w','o','r','d',':'};
215 static const WCHAR hexp[] = {'h','e','x','('};
217 static const struct data_type data_types[] = { /* actual type */ /* type to assume for parsing */
218 { quote, 1, REG_SZ, REG_SZ },
219 { str, 5, REG_SZ, REG_SZ },
220 { str2, 8, REG_EXPAND_SZ, REG_SZ },
221 { hex, 4, REG_BINARY, REG_BINARY },
222 { dword, 6, REG_DWORD, REG_DWORD },
223 { hexp, 4, -1, REG_BINARY },
224 { NULL, 0, 0, 0 }
227 const struct data_type *ptr;
228 int type;
230 for (ptr = data_types; ptr->tag; ptr++) {
231 if (strncmpW( ptr->tag, *lpValue, ptr->len ))
232 continue;
234 /* Found! */
235 *parse_type = ptr->parse_type;
236 type=ptr->type;
237 *lpValue+=ptr->len;
238 if (type == -1) {
239 WCHAR* end;
241 /* "hex(xx):" is special */
242 type = (int)strtoulW( *lpValue , &end, 16 );
243 if (**lpValue=='\0' || *end!=')' || *(end+1)!=':') {
244 type=REG_NONE;
245 } else {
246 *lpValue = end + 2;
249 return type;
251 *parse_type=REG_NONE;
252 return REG_NONE;
255 /******************************************************************************
256 * Replaces escape sequences with the characters.
258 static int REGPROC_unescape_string(WCHAR* str)
260 int str_idx = 0; /* current character under analysis */
261 int val_idx = 0; /* the last character of the unescaped string */
262 int len = lstrlenW(str);
263 for (str_idx = 0; str_idx < len; str_idx++, val_idx++) {
264 if (str[str_idx] == '\\') {
265 str_idx++;
266 switch (str[str_idx]) {
267 case 'n':
268 str[val_idx] = '\n';
269 break;
270 case 'r':
271 str[val_idx] = '\r';
272 break;
273 case '0':
274 str[val_idx] = '\0';
275 break;
276 case '\\':
277 case '"':
278 str[val_idx] = str[str_idx];
279 break;
280 default:
281 fprintf(stderr,"Warning! Unrecognized escape sequence: \\%c'\n",
282 str[str_idx]);
283 str[val_idx] = str[str_idx];
284 break;
286 } else {
287 str[val_idx] = str[str_idx];
290 str[val_idx] = '\0';
291 return val_idx;
294 static BOOL parseKeyName(LPWSTR lpKeyName, HKEY *hKey, LPWSTR *lpKeyPath)
296 WCHAR* lpSlash = NULL;
297 unsigned int i, len;
299 if (lpKeyName == NULL)
300 return FALSE;
302 for(i = 0; *(lpKeyName+i) != 0; i++)
304 if(*(lpKeyName+i) == '\\')
306 lpSlash = lpKeyName+i;
307 break;
311 if (lpSlash)
313 len = lpSlash-lpKeyName;
315 else
317 len = lstrlenW(lpKeyName);
318 lpSlash = lpKeyName+len;
320 *hKey = NULL;
322 for (i = 0; i < REG_CLASS_NUMBER; i++) {
323 if (CompareStringW(LOCALE_USER_DEFAULT, 0, lpKeyName, len, reg_class_namesW[i], -1) == CSTR_EQUAL &&
324 len == lstrlenW(reg_class_namesW[i])) {
325 *hKey = reg_class_keys[i];
326 break;
330 if (*hKey == NULL)
331 return FALSE;
334 if (*lpSlash != '\0')
335 lpSlash++;
336 *lpKeyPath = lpSlash;
337 return TRUE;
340 /* Globals used by the setValue() & co */
341 static LPSTR currentKeyName;
342 static HKEY currentKeyHandle = NULL;
344 /******************************************************************************
345 * Sets the value with name val_name to the data in val_data for the currently
346 * opened key.
348 * Parameters:
349 * val_name - name of the registry value
350 * val_data - registry value data
352 static LONG setValue(WCHAR* val_name, WCHAR* val_data, BOOL is_unicode)
354 LONG res;
355 DWORD dwDataType, dwParseType;
356 LPBYTE lpbData;
357 DWORD dwData, dwLen;
358 WCHAR del[] = {'-',0};
360 if ( (val_name == NULL) || (val_data == NULL) )
361 return ERROR_INVALID_PARAMETER;
363 if (lstrcmpW(val_data, del) == 0)
365 res=RegDeleteValueW(currentKeyHandle,val_name);
366 return (res == ERROR_FILE_NOT_FOUND ? ERROR_SUCCESS : res);
369 /* Get the data type stored into the value field */
370 dwDataType = getDataType(&val_data, &dwParseType);
372 if (dwParseType == REG_SZ) /* no conversion for string */
374 dwLen = REGPROC_unescape_string(val_data);
375 if(!dwLen || val_data[dwLen-1] != '"')
376 return ERROR_INVALID_DATA;
377 val_data[dwLen-1] = '\0'; /* remove last quotes */
378 lpbData = (BYTE*) val_data;
379 dwLen = dwLen * sizeof(WCHAR); /* size is in bytes */
381 else if (dwParseType == REG_DWORD) /* Convert the dword types */
383 if (!convertHexToDWord(val_data, &dwData))
384 return ERROR_INVALID_DATA;
385 lpbData = (BYTE*)&dwData;
386 dwLen = sizeof(dwData);
388 else if (dwParseType == REG_BINARY) /* Convert the binary data */
390 lpbData = convertHexCSVToHex(val_data, &dwLen);
391 if (!lpbData)
392 return ERROR_INVALID_DATA;
394 if((dwDataType == REG_MULTI_SZ || dwDataType == REG_EXPAND_SZ) && !is_unicode)
396 LPBYTE tmp = lpbData;
397 lpbData = (LPBYTE)GetWideStringN((char*)lpbData, dwLen, &dwLen);
398 dwLen *= sizeof(WCHAR);
399 HeapFree(GetProcessHeap(), 0, tmp);
402 else /* unknown format */
404 fprintf(stderr,"%s: ERROR, unknown data format\n", getAppName());
405 return ERROR_INVALID_DATA;
408 res = RegSetValueExW(
409 currentKeyHandle,
410 val_name,
411 0, /* Reserved */
412 dwDataType,
413 lpbData,
414 dwLen);
415 if (dwParseType == REG_BINARY)
416 HeapFree(GetProcessHeap(), 0, lpbData);
417 return res;
420 /******************************************************************************
421 * A helper function for processRegEntry() that opens the current key.
422 * That key must be closed by calling closeKey().
424 static LONG openKeyW(WCHAR* stdInput)
426 HKEY keyClass;
427 WCHAR* keyPath;
428 DWORD dwDisp;
429 LONG res;
431 /* Sanity checks */
432 if (stdInput == NULL)
433 return ERROR_INVALID_PARAMETER;
435 /* Get the registry class */
436 if (!parseKeyName(stdInput, &keyClass, &keyPath))
437 return ERROR_INVALID_PARAMETER;
439 res = RegCreateKeyExW(
440 keyClass, /* Class */
441 keyPath, /* Sub Key */
442 0, /* MUST BE 0 */
443 NULL, /* object type */
444 REG_OPTION_NON_VOLATILE, /* option, REG_OPTION_NON_VOLATILE ... */
445 KEY_ALL_ACCESS, /* access mask, KEY_ALL_ACCESS */
446 NULL, /* security attribute */
447 &currentKeyHandle, /* result */
448 &dwDisp); /* disposition, REG_CREATED_NEW_KEY or
449 REG_OPENED_EXISTING_KEY */
451 if (res == ERROR_SUCCESS)
452 currentKeyName = GetMultiByteString(stdInput);
453 else
454 currentKeyHandle = NULL;
456 return res;
460 /******************************************************************************
461 * Close the currently opened key.
463 static void closeKey(void)
465 if (currentKeyHandle)
467 HeapFree(GetProcessHeap(), 0, currentKeyName);
468 RegCloseKey(currentKeyHandle);
469 currentKeyHandle = NULL;
473 /******************************************************************************
474 * This function is a wrapper for the setValue function. It prepares the
475 * land and cleans the area once completed.
476 * Note: this function modifies the line parameter.
478 * line - registry file unwrapped line. Should have the registry value name and
479 * complete registry value data.
481 static void processSetValue(WCHAR* line, BOOL is_unicode)
483 WCHAR* val_name; /* registry value name */
484 WCHAR* val_data; /* registry value data */
485 int line_idx = 0; /* current character under analysis */
486 LONG res;
488 /* get value name */
489 while ( isspaceW(line[line_idx]) ) line_idx++;
490 if (line[line_idx] == '@' && line[line_idx + 1] == '=') {
491 line[line_idx] = '\0';
492 val_name = line;
493 line_idx++;
494 } else if (line[line_idx] == '\"') {
495 line_idx++;
496 val_name = line + line_idx;
497 while (line[line_idx]) {
498 if (line[line_idx] == '\\') /* skip escaped character */
500 line_idx += 2;
501 } else {
502 if (line[line_idx] == '\"') {
503 line[line_idx] = '\0';
504 line_idx++;
505 break;
506 } else {
507 line_idx++;
511 while ( isspaceW(line[line_idx]) ) line_idx++;
512 if (!line[line_idx]) {
513 fprintf(stderr, "%s: warning: unexpected EOL\n", getAppName());
514 return;
516 if (line[line_idx] != '=') {
517 char* lineA;
518 line[line_idx] = '\"';
519 lineA = GetMultiByteString(line);
520 fprintf(stderr,"%s: warning: unrecognized line: '%s'\n", getAppName(), lineA);
521 HeapFree(GetProcessHeap(), 0, lineA);
522 return;
525 } else {
526 char* lineA = GetMultiByteString(line);
527 fprintf(stderr,"%s: warning: unrecognized line: '%s'\n", getAppName(), lineA);
528 HeapFree(GetProcessHeap(), 0, lineA);
529 return;
531 line_idx++; /* skip the '=' character */
533 while ( isspaceW(line[line_idx]) ) line_idx++;
534 val_data = line + line_idx;
535 /* trim trailing blanks */
536 line_idx = strlenW(val_data);
537 while (line_idx > 0 && isspaceW(val_data[line_idx-1])) line_idx--;
538 val_data[line_idx] = '\0';
540 REGPROC_unescape_string(val_name);
541 res = setValue(val_name, val_data, is_unicode);
542 if ( res != ERROR_SUCCESS )
544 char* val_nameA = GetMultiByteString(val_name);
545 char* val_dataA = GetMultiByteString(val_data);
546 fprintf(stderr,"%s: ERROR Key %s not created. Value: %s, Data: %s\n",
547 getAppName(),
548 currentKeyName,
549 val_nameA,
550 val_dataA);
551 HeapFree(GetProcessHeap(), 0, val_nameA);
552 HeapFree(GetProcessHeap(), 0, val_dataA);
556 /******************************************************************************
557 * This function receives the currently read entry and performs the
558 * corresponding action.
559 * isUnicode affects parsing of REG_MULTI_SZ values
561 static void processRegEntry(WCHAR* stdInput, BOOL isUnicode)
564 * We encountered the end of the file, make sure we
565 * close the opened key and exit
567 if (stdInput == NULL) {
568 closeKey();
569 return;
572 if ( stdInput[0] == '[') /* We are reading a new key */
574 WCHAR* keyEnd;
575 closeKey(); /* Close the previous key */
577 /* Get rid of the square brackets */
578 stdInput++;
579 keyEnd = strrchrW(stdInput, ']');
580 if (keyEnd)
581 *keyEnd='\0';
583 /* delete the key if we encounter '-' at the start of reg key */
584 if ( stdInput[0] == '-')
586 delete_registry_key(stdInput + 1);
587 } else if ( openKeyW(stdInput) != ERROR_SUCCESS )
589 char* stdInputA = GetMultiByteString(stdInput);
590 fprintf(stderr,"%s: setValue failed to open key %s\n",
591 getAppName(), stdInputA);
592 HeapFree(GetProcessHeap(), 0, stdInputA);
594 } else if( currentKeyHandle &&
595 (( stdInput[0] == '@') || /* reading a default @=data pair */
596 ( stdInput[0] == '\"'))) /* reading a new value=data pair */
598 processSetValue(stdInput, isUnicode);
599 } else
601 /* Since we are assuming that the file format is valid we must be
602 * reading a blank line which indicates the end of this key processing
604 closeKey();
608 /******************************************************************************
609 * Processes a registry file.
610 * Correctly processes comments (in # and ; form), line continuation.
612 * Parameters:
613 * in - input stream to read from
614 * first_chars - beginning of stream, read due to Unicode check
616 static void processRegLinesA(FILE *in, char* first_chars)
618 LPSTR line = NULL; /* line read from input stream */
619 ULONG lineSize = REG_VAL_BUF_SIZE;
621 line = HeapAlloc(GetProcessHeap(), 0, lineSize);
622 CHECK_ENOUGH_MEMORY(line);
623 memcpy(line, first_chars, 2);
625 while (!feof(in)) {
626 LPSTR s; /* The pointer into line for where the current fgets should read */
627 WCHAR* lineW;
628 s = line;
630 if(first_chars)
632 s += 2;
633 first_chars = NULL;
636 for (;;) {
637 size_t size_remaining;
638 int size_to_get, i;
639 char *s_eol; /* various local uses */
641 /* Do we need to expand the buffer ? */
642 assert (s >= line && s <= line + lineSize);
643 size_remaining = lineSize - (s-line);
644 if (size_remaining < 2) /* room for 1 character and the \0 */
646 char *new_buffer;
647 size_t new_size = lineSize + REG_VAL_BUF_SIZE;
648 if (new_size > lineSize) /* no arithmetic overflow */
649 new_buffer = HeapReAlloc (GetProcessHeap(), 0, line, new_size);
650 else
651 new_buffer = NULL;
652 CHECK_ENOUGH_MEMORY(new_buffer);
653 line = new_buffer;
654 s = line + lineSize - size_remaining;
655 lineSize = new_size;
656 size_remaining = lineSize - (s-line);
659 /* Get as much as possible into the buffer, terminated either by
660 * eof, error, eol or getting the maximum amount. Abort on error.
662 size_to_get = (size_remaining > INT_MAX ? INT_MAX : size_remaining);
664 /* get a single line. note that `i' must be one past the last
665 * meaningful character in `s' when this loop exits */
666 for(i = 0; i < size_to_get-1; ++i){
667 int xchar;
669 xchar = fgetc(in);
670 s[i] = xchar;
671 if(xchar == EOF){
672 if(ferror(in)){
673 perror("While reading input");
674 exit(IO_ERROR);
675 }else
676 assert(feof(in));
677 break;
679 if(s[i] == '\r'){
680 /* read the next character iff it's \n */
681 if(i+2 >= size_to_get){
682 /* buffer too short, so put back the EOL char to
683 * read next cycle */
684 ungetc('\r', in);
685 break;
687 s[i+1] = fgetc(in);
688 if(s[i+1] != '\n'){
689 ungetc(s[i+1], in);
690 i = i+1;
691 }else
692 i = i+2;
693 break;
695 if(s[i] == '\n'){
696 i = i+1;
697 break;
700 s[i] = '\0';
702 /* If we didn't read the eol nor the eof go around for the rest */
703 s_eol = strpbrk (s, "\r\n");
704 if (!feof (in) && !s_eol) {
705 s = strchr (s, '\0');
706 continue;
709 /* If it is a comment line then discard it and go around again */
710 if (line [0] == '#' || line [0] == ';') {
711 s = line;
712 continue;
715 /* Remove any line feed. Leave s_eol on the first \0 */
716 if (s_eol) {
717 if (*s_eol == '\r' && *(s_eol+1) == '\n')
718 *(s_eol+1) = '\0';
719 *s_eol = '\0';
720 } else
721 s_eol = strchr (s, '\0');
723 /* If there is a concatenating \\ then go around again */
724 if (s_eol > line && *(s_eol-1) == '\\') {
725 int c;
726 s = s_eol-1;
730 c = fgetc(in);
731 } while(c == ' ' || c == '\t');
733 if(c == EOF)
735 fprintf(stderr,"%s: ERROR - invalid continuation.\n",
736 getAppName());
738 else
740 *s = c;
741 s++;
743 continue;
746 lineW = GetWideString(line);
748 break; /* That is the full virtual line */
751 processRegEntry(lineW, FALSE);
752 HeapFree(GetProcessHeap(), 0, lineW);
754 processRegEntry(NULL, FALSE);
756 HeapFree(GetProcessHeap(), 0, line);
759 static void processRegLinesW(FILE *in)
761 WCHAR* buf = NULL; /* line read from input stream */
762 ULONG lineSize = REG_VAL_BUF_SIZE;
763 size_t CharsInBuf = -1;
765 WCHAR* s; /* The pointer into buf for where the current fgets should read */
766 WCHAR* line; /* The start of the current line */
768 buf = HeapAlloc(GetProcessHeap(), 0, lineSize * sizeof(WCHAR));
769 CHECK_ENOUGH_MEMORY(buf);
771 s = buf;
772 line = buf;
774 while(!feof(in)) {
775 size_t size_remaining;
776 int size_to_get;
777 WCHAR *s_eol = NULL; /* various local uses */
779 /* Do we need to expand the buffer ? */
780 assert (s >= buf && s <= buf + lineSize);
781 size_remaining = lineSize - (s-buf);
782 if (size_remaining < 2) /* room for 1 character and the \0 */
784 WCHAR *new_buffer;
785 size_t new_size = lineSize + (REG_VAL_BUF_SIZE / sizeof(WCHAR));
786 if (new_size > lineSize) /* no arithmetic overflow */
787 new_buffer = HeapReAlloc (GetProcessHeap(), 0, buf, new_size * sizeof(WCHAR));
788 else
789 new_buffer = NULL;
790 CHECK_ENOUGH_MEMORY(new_buffer);
791 buf = new_buffer;
792 line = buf;
793 s = buf + lineSize - size_remaining;
794 lineSize = new_size;
795 size_remaining = lineSize - (s-buf);
798 /* Get as much as possible into the buffer, terminated either by
799 * eof, error or getting the maximum amount. Abort on error.
801 size_to_get = (size_remaining > INT_MAX ? INT_MAX : size_remaining);
803 CharsInBuf = fread(s, sizeof(WCHAR), size_to_get - 1, in);
804 s[CharsInBuf] = 0;
806 if (CharsInBuf == 0) {
807 if (ferror(in)) {
808 perror ("While reading input");
809 exit (IO_ERROR);
810 } else {
811 assert (feof(in));
812 *s = '\0';
813 /* It is not clear to me from the definition that the
814 * contents of the buffer are well defined on detecting
815 * an eof without managing to read anything.
820 /* If we didn't read the eol nor the eof go around for the rest */
821 while(1)
823 const WCHAR line_endings[] = {'\r','\n',0};
824 s_eol = strpbrkW(line, line_endings);
826 if(!s_eol) {
827 /* Move the stub of the line to the start of the buffer so
828 * we get the maximum space to read into, and so we don't
829 * have to recalculate 'line' if the buffer expands */
830 MoveMemory(buf, line, (strlenW(line)+1) * sizeof(WCHAR));
831 line = buf;
832 s = strchrW(line, '\0');
833 break;
836 /* If it is a comment line then discard it and go around again */
837 if (*line == '#' || *line == ';') {
838 if (*s_eol == '\r' && *(s_eol+1) == '\n')
839 line = s_eol + 2;
840 else
841 line = s_eol + 1;
842 continue;
845 /* If there is a concatenating \\ then go around again */
846 if (*(s_eol-1) == '\\') {
847 WCHAR* NextLine = s_eol + 1;
849 if(*s_eol == '\r' && *(s_eol+1) == '\n')
850 NextLine++;
852 while(*(NextLine+1) == ' ' || *(NextLine+1) == '\t')
853 NextLine++;
855 MoveMemory(s_eol - 1, NextLine, (CharsInBuf - (NextLine - s) + 1)*sizeof(WCHAR));
856 CharsInBuf -= NextLine - s_eol + 1;
857 s_eol = 0;
858 continue;
861 /* Remove any line feed. Leave s_eol on the last \0 */
862 if (*s_eol == '\r' && *(s_eol + 1) == '\n')
863 *s_eol++ = '\0';
864 *s_eol = '\0';
866 processRegEntry(line, TRUE);
867 line = s_eol + 1;
868 s_eol = 0;
869 continue; /* That is the full virtual line */
873 processRegEntry(NULL, TRUE);
875 HeapFree(GetProcessHeap(), 0, buf);
878 /****************************************************************************
879 * REGPROC_print_error
881 * Print the message for GetLastError
884 static void REGPROC_print_error(void)
886 LPVOID lpMsgBuf;
887 DWORD error_code;
888 int status;
890 error_code = GetLastError ();
891 status = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
892 NULL, error_code, 0, (LPSTR) &lpMsgBuf, 0, NULL);
893 if (!status) {
894 fprintf(stderr,"%s: Cannot display message for error %d, status %d\n",
895 getAppName(), error_code, GetLastError());
896 exit(1);
898 puts(lpMsgBuf);
899 LocalFree(lpMsgBuf);
900 exit(1);
903 /******************************************************************************
904 * Checks whether the buffer has enough room for the string or required size.
905 * Resizes the buffer if necessary.
907 * Parameters:
908 * buffer - pointer to a buffer for string
909 * len - current length of the buffer in characters.
910 * required_len - length of the string to place to the buffer in characters.
911 * The length does not include the terminating null character.
913 static void REGPROC_resize_char_buffer(WCHAR **buffer, DWORD *len, DWORD required_len)
915 required_len++;
916 if (required_len > *len) {
917 *len = required_len;
918 if (!*buffer)
919 *buffer = HeapAlloc(GetProcessHeap(), 0, *len * sizeof(**buffer));
920 else
921 *buffer = HeapReAlloc(GetProcessHeap(), 0, *buffer, *len * sizeof(**buffer));
922 CHECK_ENOUGH_MEMORY(*buffer);
926 /******************************************************************************
927 * Same as REGPROC_resize_char_buffer() but on a regular buffer.
929 * Parameters:
930 * buffer - pointer to a buffer
931 * len - current size of the buffer in bytes
932 * required_size - size of the data to place in the buffer in bytes
934 static void REGPROC_resize_binary_buffer(BYTE **buffer, DWORD *size, DWORD required_size)
936 if (required_size > *size) {
937 *size = required_size;
938 if (!*buffer)
939 *buffer = HeapAlloc(GetProcessHeap(), 0, *size);
940 else
941 *buffer = HeapReAlloc(GetProcessHeap(), 0, *buffer, *size);
942 CHECK_ENOUGH_MEMORY(*buffer);
946 /******************************************************************************
947 * Prints string str to file
949 static void REGPROC_export_string(WCHAR **line_buf, DWORD *line_buf_size, DWORD *line_len, WCHAR *str, DWORD str_len)
951 DWORD i, pos;
952 DWORD extra = 0;
954 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + 10);
956 /* escaping characters */
957 pos = *line_len;
958 for (i = 0; i < str_len; i++) {
959 WCHAR c = str[i];
960 switch (c) {
961 case '\n':
962 extra++;
963 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + extra);
964 (*line_buf)[pos++] = '\\';
965 (*line_buf)[pos++] = 'n';
966 break;
968 case '\r':
969 extra++;
970 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + extra);
971 (*line_buf)[pos++] = '\\';
972 (*line_buf)[pos++] = 'r';
973 break;
975 case '\\':
976 case '"':
977 extra++;
978 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + extra);
979 (*line_buf)[pos++] = '\\';
980 /* Fall through */
982 default:
983 (*line_buf)[pos++] = c;
984 break;
987 (*line_buf)[pos] = '\0';
988 *line_len = pos;
991 static void REGPROC_export_binary(WCHAR **line_buf, DWORD *line_buf_size, DWORD *line_len, DWORD type, BYTE *value, DWORD value_size, BOOL unicode)
993 DWORD hex_pos, data_pos;
994 const WCHAR *hex_prefix;
995 const WCHAR hex[] = {'h','e','x',':',0};
996 WCHAR hex_buf[17];
997 const WCHAR concat[] = {'\\','\r','\n',' ',' ',0};
998 DWORD concat_prefix, concat_len;
999 const WCHAR newline[] = {'\r','\n',0};
1000 CHAR* value_multibyte = NULL;
1002 if (type == REG_BINARY) {
1003 hex_prefix = hex;
1004 } else {
1005 const WCHAR hex_format[] = {'h','e','x','(','%','x',')',':',0};
1006 hex_prefix = hex_buf;
1007 sprintfW(hex_buf, hex_format, type);
1008 if ((type == REG_SZ || type == REG_EXPAND_SZ || type == REG_MULTI_SZ) && !unicode)
1010 value_multibyte = GetMultiByteStringN((WCHAR*)value, value_size / sizeof(WCHAR), &value_size);
1011 value = (BYTE*)value_multibyte;
1015 concat_len = lstrlenW(concat);
1016 concat_prefix = 2;
1018 hex_pos = *line_len;
1019 *line_len += lstrlenW(hex_prefix);
1020 data_pos = *line_len;
1021 *line_len += value_size * 3;
1022 /* - The 2 spaces that concat places at the start of the
1023 * line effectively reduce the space available for data.
1024 * - If the value name and hex prefix are very long
1025 * ( > REG_FILE_HEX_LINE_LEN) or *line_len divides
1026 * without a remainder then we may overestimate
1027 * the needed number of lines by one. But that's ok.
1028 * - The trailing '\r' takes the place of a comma so
1029 * we only need to add 1 for the trailing '\n'
1031 *line_len += *line_len / (REG_FILE_HEX_LINE_LEN - concat_prefix) * concat_len + 1;
1032 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len);
1033 lstrcpyW(*line_buf + hex_pos, hex_prefix);
1034 if (value_size)
1036 const WCHAR format[] = {'%','0','2','x',0};
1037 DWORD i, column;
1039 column = data_pos; /* no line wrap yet */
1040 i = 0;
1041 while (1)
1043 sprintfW(*line_buf + data_pos, format, (unsigned int)value[i]);
1044 data_pos += 2;
1045 if (++i == value_size)
1046 break;
1048 (*line_buf)[data_pos++] = ',';
1049 column += 3;
1051 /* wrap the line */
1052 if (column >= REG_FILE_HEX_LINE_LEN) {
1053 lstrcpyW(*line_buf + data_pos, concat);
1054 data_pos += concat_len;
1055 column = concat_prefix;
1059 lstrcpyW(*line_buf + data_pos, newline);
1060 HeapFree(GetProcessHeap(), 0, value_multibyte);
1063 /******************************************************************************
1064 * Writes the given line to a file, in multi-byte or wide characters
1066 static void REGPROC_write_line(FILE *file, const WCHAR* str, BOOL unicode)
1068 if(unicode)
1070 fwrite(str, sizeof(WCHAR), lstrlenW(str), file);
1071 } else
1073 char* strA = GetMultiByteString(str);
1074 fputs(strA, file);
1075 HeapFree(GetProcessHeap(), 0, strA);
1079 /******************************************************************************
1080 * Writes contents of the registry key to the specified file stream.
1082 * Parameters:
1083 * file - writable file stream to export registry branch to.
1084 * key - registry branch to export.
1085 * reg_key_name_buf - name of the key with registry class.
1086 * Is resized if necessary.
1087 * reg_key_name_size - length of the buffer for the registry class in characters.
1088 * val_name_buf - buffer for storing value name.
1089 * Is resized if necessary.
1090 * val_name_size - length of the buffer for storing value names in characters.
1091 * val_buf - buffer for storing values while extracting.
1092 * Is resized if necessary.
1093 * val_size - size of the buffer for storing values in bytes.
1095 static void export_hkey(FILE *file, HKEY key,
1096 WCHAR **reg_key_name_buf, DWORD *reg_key_name_size,
1097 WCHAR **val_name_buf, DWORD *val_name_size,
1098 BYTE **val_buf, DWORD *val_size,
1099 WCHAR **line_buf, DWORD *line_buf_size,
1100 BOOL unicode)
1102 DWORD max_sub_key_len;
1103 DWORD max_val_name_len;
1104 DWORD max_val_size;
1105 DWORD curr_len;
1106 DWORD i;
1107 BOOL more_data;
1108 LONG ret;
1109 WCHAR key_format[] = {'\r','\n','[','%','s',']','\r','\n',0};
1111 /* get size information and resize the buffers if necessary */
1112 if (RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL,
1113 &max_sub_key_len, NULL,
1114 NULL, &max_val_name_len, &max_val_size, NULL, NULL
1115 ) != ERROR_SUCCESS) {
1116 REGPROC_print_error();
1118 curr_len = strlenW(*reg_key_name_buf);
1119 REGPROC_resize_char_buffer(reg_key_name_buf, reg_key_name_size,
1120 max_sub_key_len + curr_len + 1);
1121 REGPROC_resize_char_buffer(val_name_buf, val_name_size,
1122 max_val_name_len);
1123 REGPROC_resize_binary_buffer(val_buf, val_size, max_val_size);
1124 REGPROC_resize_char_buffer(line_buf, line_buf_size, lstrlenW(*reg_key_name_buf) + 4);
1125 /* output data for the current key */
1126 sprintfW(*line_buf, key_format, *reg_key_name_buf);
1127 REGPROC_write_line(file, *line_buf, unicode);
1129 /* print all the values */
1130 i = 0;
1131 more_data = TRUE;
1132 while(more_data) {
1133 DWORD value_type;
1134 DWORD val_name_size1 = *val_name_size;
1135 DWORD val_size1 = *val_size;
1136 ret = RegEnumValueW(key, i, *val_name_buf, &val_name_size1, NULL,
1137 &value_type, *val_buf, &val_size1);
1138 if (ret == ERROR_MORE_DATA) {
1139 /* Increase the size of the buffers and retry */
1140 REGPROC_resize_char_buffer(val_name_buf, val_name_size, val_name_size1);
1141 REGPROC_resize_binary_buffer(val_buf, val_size, val_size1);
1142 } else if (ret != ERROR_SUCCESS) {
1143 more_data = FALSE;
1144 if (ret != ERROR_NO_MORE_ITEMS) {
1145 REGPROC_print_error();
1147 } else {
1148 DWORD line_len;
1149 i++;
1151 if ((*val_name_buf)[0]) {
1152 const WCHAR val_start[] = {'"','%','s','"','=',0};
1154 line_len = 0;
1155 REGPROC_export_string(line_buf, line_buf_size, &line_len, *val_name_buf, lstrlenW(*val_name_buf));
1156 REGPROC_resize_char_buffer(val_name_buf, val_name_size, lstrlenW(*line_buf) + 1);
1157 lstrcpyW(*val_name_buf, *line_buf);
1159 line_len = 3 + lstrlenW(*val_name_buf);
1160 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len);
1161 sprintfW(*line_buf, val_start, *val_name_buf);
1162 } else {
1163 const WCHAR std_val[] = {'@','=',0};
1164 line_len = 2;
1165 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len);
1166 lstrcpyW(*line_buf, std_val);
1169 switch (value_type) {
1170 case REG_SZ:
1172 WCHAR* wstr = (WCHAR*)*val_buf;
1174 if (val_size1 < sizeof(WCHAR) || val_size1 % sizeof(WCHAR) ||
1175 wstr[val_size1 / sizeof(WCHAR) - 1]) {
1176 REGPROC_export_binary(line_buf, line_buf_size, &line_len, value_type, *val_buf, val_size1, unicode);
1177 } else {
1178 const WCHAR start[] = {'"',0};
1179 const WCHAR end[] = {'"','\r','\n',0};
1180 DWORD len;
1182 len = lstrlenW(start);
1183 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len + len);
1184 lstrcpyW(*line_buf + line_len, start);
1185 line_len += len;
1187 REGPROC_export_string(line_buf, line_buf_size, &line_len, wstr, lstrlenW(wstr));
1189 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len + lstrlenW(end));
1190 lstrcpyW(*line_buf + line_len, end);
1192 break;
1195 case REG_DWORD:
1197 WCHAR format[] = {'d','w','o','r','d',':','%','0','8','x','\r','\n',0};
1199 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len + 15);
1200 sprintfW(*line_buf + line_len, format, *((DWORD *)*val_buf));
1201 break;
1204 default:
1206 char* key_nameA = GetMultiByteString(*reg_key_name_buf);
1207 char* value_nameA = GetMultiByteString(*val_name_buf);
1208 fprintf(stderr,"%s: warning - unsupported registry format '%d', "
1209 "treat as binary\n",
1210 getAppName(), value_type);
1211 fprintf(stderr,"key name: \"%s\"\n", key_nameA);
1212 fprintf(stderr,"value name:\"%s\"\n\n", value_nameA);
1213 HeapFree(GetProcessHeap(), 0, key_nameA);
1214 HeapFree(GetProcessHeap(), 0, value_nameA);
1216 /* falls through */
1217 case REG_EXPAND_SZ:
1218 case REG_MULTI_SZ:
1219 /* falls through */
1220 case REG_BINARY:
1221 REGPROC_export_binary(line_buf, line_buf_size, &line_len, value_type, *val_buf, val_size1, unicode);
1223 REGPROC_write_line(file, *line_buf, unicode);
1227 i = 0;
1228 more_data = TRUE;
1229 (*reg_key_name_buf)[curr_len] = '\\';
1230 while(more_data) {
1231 DWORD buf_size = *reg_key_name_size - curr_len - 1;
1233 ret = RegEnumKeyExW(key, i, *reg_key_name_buf + curr_len + 1, &buf_size,
1234 NULL, NULL, NULL, NULL);
1235 if (ret == ERROR_MORE_DATA) {
1236 /* Increase the size of the buffer and retry */
1237 REGPROC_resize_char_buffer(reg_key_name_buf, reg_key_name_size, curr_len + 1 + buf_size);
1238 } else if (ret != ERROR_SUCCESS) {
1239 more_data = FALSE;
1240 if (ret != ERROR_NO_MORE_ITEMS) {
1241 REGPROC_print_error();
1243 } else {
1244 HKEY subkey;
1246 i++;
1247 if (RegOpenKeyW(key, *reg_key_name_buf + curr_len + 1,
1248 &subkey) == ERROR_SUCCESS) {
1249 export_hkey(file, subkey, reg_key_name_buf, reg_key_name_size,
1250 val_name_buf, val_name_size, val_buf, val_size,
1251 line_buf, line_buf_size, unicode);
1252 RegCloseKey(subkey);
1253 } else {
1254 REGPROC_print_error();
1258 (*reg_key_name_buf)[curr_len] = '\0';
1261 /******************************************************************************
1262 * Open file in binary mode for export.
1264 static FILE *REGPROC_open_export_file(WCHAR *file_name, BOOL unicode)
1266 FILE *file;
1267 WCHAR dash = '-';
1269 if (strncmpW(file_name,&dash,1)==0) {
1270 file=stdout;
1271 _setmode(_fileno(file), _O_BINARY);
1272 } else
1274 CHAR* file_nameA = GetMultiByteString(file_name);
1275 file = fopen(file_nameA, "wb");
1276 if (!file) {
1277 perror("");
1278 fprintf(stderr,"%s: Can't open file \"%s\"\n", getAppName(), file_nameA);
1279 HeapFree(GetProcessHeap(), 0, file_nameA);
1280 exit(1);
1282 HeapFree(GetProcessHeap(), 0, file_nameA);
1284 if(unicode)
1286 const BYTE unicode_seq[] = {0xff,0xfe};
1287 const WCHAR header[] = {'W','i','n','d','o','w','s',' ','R','e','g','i','s','t','r','y',' ','E','d','i','t','o','r',' ','V','e','r','s','i','o','n',' ','5','.','0','0','\r','\n'};
1288 fwrite(unicode_seq, sizeof(BYTE), sizeof(unicode_seq)/sizeof(unicode_seq[0]), file);
1289 fwrite(header, sizeof(WCHAR), sizeof(header)/sizeof(header[0]), file);
1290 } else
1292 fputs("REGEDIT4\r\n", file);
1295 return file;
1298 /******************************************************************************
1299 * Writes contents of the registry key to the specified file stream.
1301 * Parameters:
1302 * file_name - name of a file to export registry branch to.
1303 * reg_key_name - registry branch to export. The whole registry is exported if
1304 * reg_key_name is NULL or contains an empty string.
1306 BOOL export_registry_key(WCHAR *file_name, WCHAR *reg_key_name, DWORD format)
1308 WCHAR *reg_key_name_buf;
1309 WCHAR *val_name_buf;
1310 BYTE *val_buf;
1311 WCHAR *line_buf;
1312 DWORD reg_key_name_size = KEY_MAX_LEN;
1313 DWORD val_name_size = KEY_MAX_LEN;
1314 DWORD val_size = REG_VAL_BUF_SIZE;
1315 DWORD line_buf_size = KEY_MAX_LEN + REG_VAL_BUF_SIZE;
1316 FILE *file = NULL;
1317 BOOL unicode = (format == REG_FORMAT_5);
1319 reg_key_name_buf = HeapAlloc(GetProcessHeap(), 0,
1320 reg_key_name_size * sizeof(*reg_key_name_buf));
1321 val_name_buf = HeapAlloc(GetProcessHeap(), 0,
1322 val_name_size * sizeof(*val_name_buf));
1323 val_buf = HeapAlloc(GetProcessHeap(), 0, val_size);
1324 line_buf = HeapAlloc(GetProcessHeap(), 0, line_buf_size * sizeof(*line_buf));
1325 CHECK_ENOUGH_MEMORY(reg_key_name_buf && val_name_buf && val_buf && line_buf);
1327 if (reg_key_name && reg_key_name[0]) {
1328 HKEY reg_key_class;
1329 WCHAR *branch_name = NULL;
1330 HKEY key;
1332 REGPROC_resize_char_buffer(&reg_key_name_buf, &reg_key_name_size,
1333 lstrlenW(reg_key_name));
1334 lstrcpyW(reg_key_name_buf, reg_key_name);
1336 /* open the specified key */
1337 if (!parseKeyName(reg_key_name, &reg_key_class, &branch_name)) {
1338 CHAR* key_nameA = GetMultiByteString(reg_key_name);
1339 fprintf(stderr,"%s: Incorrect registry class specification in '%s'\n",
1340 getAppName(), key_nameA);
1341 HeapFree(GetProcessHeap(), 0, key_nameA);
1342 exit(1);
1344 if (!branch_name[0]) {
1345 /* no branch - registry class is specified */
1346 file = REGPROC_open_export_file(file_name, unicode);
1347 export_hkey(file, reg_key_class,
1348 &reg_key_name_buf, &reg_key_name_size,
1349 &val_name_buf, &val_name_size,
1350 &val_buf, &val_size, &line_buf,
1351 &line_buf_size, unicode);
1352 } else if (RegOpenKeyW(reg_key_class, branch_name, &key) == ERROR_SUCCESS) {
1353 file = REGPROC_open_export_file(file_name, unicode);
1354 export_hkey(file, key,
1355 &reg_key_name_buf, &reg_key_name_size,
1356 &val_name_buf, &val_name_size,
1357 &val_buf, &val_size, &line_buf,
1358 &line_buf_size, unicode);
1359 RegCloseKey(key);
1360 } else {
1361 CHAR* key_nameA = GetMultiByteString(reg_key_name);
1362 fprintf(stderr,"%s: Can't export. Registry key '%s' does not exist!\n",
1363 getAppName(), key_nameA);
1364 HeapFree(GetProcessHeap(), 0, key_nameA);
1365 REGPROC_print_error();
1367 } else {
1368 unsigned int i;
1370 /* export all registry classes */
1371 file = REGPROC_open_export_file(file_name, unicode);
1372 for (i = 0; i < REG_CLASS_NUMBER; i++) {
1373 /* do not export HKEY_CLASSES_ROOT */
1374 if (reg_class_keys[i] != HKEY_CLASSES_ROOT &&
1375 reg_class_keys[i] != HKEY_CURRENT_USER &&
1376 reg_class_keys[i] != HKEY_CURRENT_CONFIG &&
1377 reg_class_keys[i] != HKEY_DYN_DATA) {
1378 lstrcpyW(reg_key_name_buf, reg_class_namesW[i]);
1379 export_hkey(file, reg_class_keys[i],
1380 &reg_key_name_buf, &reg_key_name_size,
1381 &val_name_buf, &val_name_size,
1382 &val_buf, &val_size, &line_buf,
1383 &line_buf_size, unicode);
1388 if (file) {
1389 fclose(file);
1391 HeapFree(GetProcessHeap(), 0, reg_key_name);
1392 HeapFree(GetProcessHeap(), 0, val_name_buf);
1393 HeapFree(GetProcessHeap(), 0, val_buf);
1394 HeapFree(GetProcessHeap(), 0, line_buf);
1395 return TRUE;
1398 /******************************************************************************
1399 * Reads contents of the specified file into the registry.
1401 BOOL import_registry_file(FILE* reg_file)
1403 if (reg_file)
1405 BYTE s[2];
1406 if (fread( s, 2, 1, reg_file) == 1)
1408 if (s[0] == 0xff && s[1] == 0xfe)
1410 processRegLinesW(reg_file);
1411 } else
1413 processRegLinesA(reg_file, (char*)s);
1416 return TRUE;
1418 return FALSE;
1421 /******************************************************************************
1422 * Removes the registry key with all subkeys. Parses full key name.
1424 * Parameters:
1425 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1426 * empty, points to register key class, does not exist.
1428 void delete_registry_key(WCHAR *reg_key_name)
1430 WCHAR *key_name = NULL;
1431 HKEY key_class;
1433 if (!reg_key_name || !reg_key_name[0])
1434 return;
1436 if (!parseKeyName(reg_key_name, &key_class, &key_name)) {
1437 char* reg_key_nameA = GetMultiByteString(reg_key_name);
1438 fprintf(stderr,"%s: Incorrect registry class specification in '%s'\n",
1439 getAppName(), reg_key_nameA);
1440 HeapFree(GetProcessHeap(), 0, reg_key_nameA);
1441 exit(1);
1443 if (!*key_name) {
1444 char* reg_key_nameA = GetMultiByteString(reg_key_name);
1445 fprintf(stderr,"%s: Can't delete registry class '%s'\n",
1446 getAppName(), reg_key_nameA);
1447 HeapFree(GetProcessHeap(), 0, reg_key_nameA);
1448 exit(1);
1451 RegDeleteTreeW(key_class, key_name);