2 * Registry processing routines. Routines, common for registry
3 * processing frontends.
5 * Copyright 1999 Sylvain St-Germain
6 * Copyright 2002 Andriy Palamarchuk
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #define REG_VAL_BUF_SIZE 4096
33 /* maximal number of characters in hexadecimal data line,
34 not including '\' character */
35 #define REG_FILE_HEX_LINE_LEN 76
37 /* Globals used by the api setValue */
38 static LPSTR currentKeyName = NULL;
39 static HKEY currentKeyClass = 0;
40 static HKEY currentKeyHandle = 0;
41 static BOOL bTheKeyIsOpen = FALSE;
43 static const CHAR *reg_class_names[] = {
44 "HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_CLASSES_ROOT",
45 "HKEY_CURRENT_CONFIG", "HKEY_CURRENT_USER", "HKEY_DYN_DATA"
48 #define REG_CLASS_NUMBER (sizeof(reg_class_names) / sizeof(reg_class_names[0]))
50 static HKEY reg_class_keys[REG_CLASS_NUMBER] = {
51 HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CLASSES_ROOT,
52 HKEY_CURRENT_CONFIG, HKEY_CURRENT_USER, HKEY_DYN_DATA
56 #define NOT_ENOUGH_MEMORY 1
59 /* processing macros */
61 /* common check of memory allocation results */
62 #define CHECK_ENOUGH_MEMORY(p) \
65 fprintf(stderr,"%s: file %s, line %d: Not enough memory", \
66 getAppName(), __FILE__, __LINE__); \
67 exit(NOT_ENOUGH_MEMORY); \
70 /******************************************************************************
71 * Converts a hex representation of a DWORD into a DWORD.
73 static BOOL convertHexToDWord(char* str, DWORD *dw)
76 if (strlen(str) > 8 || sscanf(str, "%x%c", dw, &dummy) != 1) {
77 fprintf(stderr,"%s: ERROR, invalid hex value\n", getAppName());
83 /******************************************************************************
84 * Converts a hex comma separated values list into a binary string.
86 static BYTE* convertHexCSVToHex(char *str, DWORD *size)
91 /* The worst case is 1 digit + 1 comma per byte */
92 *size=(strlen(str)+1)/2;
93 data=HeapAlloc(GetProcessHeap(), 0, *size);
94 CHECK_ENOUGH_MEMORY(data);
103 if (s[1] != ',' && s[1] != '\0' && s[2] != ',' && s[2] != '\0') {
104 fprintf(stderr,"%s: ERROR converting CSV hex stream. Invalid sequence at '%s'\n",
106 HeapFree(GetProcessHeap(), 0, data);
109 if (sscanf(s, "%x%c", &wc, &dummy) < 1 || dummy != ',') {
110 fprintf(stderr,"%s: ERROR converting CSV hex stream. Invalid value at '%s'\n",
112 HeapFree(GetProcessHeap(), 0, data);
118 s+=(wc < 0x10 ? 2 : 3);
124 /******************************************************************************
125 * This function returns the HKEY associated with the data type encoded in the
126 * value. It modifies the input parameter (key value) in order to skip this
127 * "now useless" data type information.
129 * Note: Updated based on the algorithm used in 'server/registry.c'
131 static DWORD getDataType(LPSTR *lpValue, DWORD* parse_type)
133 struct data_type { const char *tag; int len; int type; int parse_type; };
135 static const struct data_type data_types[] = { /* actual type */ /* type to assume for parsing */
136 { "\"", 1, REG_SZ, REG_SZ },
137 { "str:\"", 5, REG_SZ, REG_SZ },
138 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
139 { "hex:", 4, REG_BINARY, REG_BINARY },
140 { "dword:", 6, REG_DWORD, REG_DWORD },
141 { "hex(", 4, -1, REG_BINARY },
145 const struct data_type *ptr;
148 for (ptr = data_types; ptr->tag; ptr++) {
149 if (memcmp( ptr->tag, *lpValue, ptr->len ))
153 *parse_type = ptr->parse_type;
158 /* "hex(xx):" is special */
159 type = (int)strtoul( *lpValue , &end, 16 );
160 if (**lpValue=='\0' || *end!=')' || *(end+1)!=':') {
168 *parse_type=REG_NONE;
172 /******************************************************************************
173 * Replaces escape sequences with the characters.
175 static void REGPROC_unescape_string(LPSTR str)
177 int str_idx = 0; /* current character under analysis */
178 int val_idx = 0; /* the last character of the unescaped string */
179 int len = strlen(str);
180 for (str_idx = 0; str_idx < len; str_idx++, val_idx++) {
181 if (str[str_idx] == '\\') {
183 switch (str[str_idx]) {
189 str[val_idx] = str[str_idx];
192 fprintf(stderr,"Warning! Unrecognized escape sequence: \\%c'\n",
194 str[val_idx] = str[str_idx];
198 str[val_idx] = str[str_idx];
204 /******************************************************************************
205 * Sets the value with name val_name to the data in val_data for the currently
209 * val_name - name of the registry value
210 * val_data - registry value data
212 static HRESULT setValue(LPSTR val_name, LPSTR val_data)
215 DWORD dwDataType, dwParseType;
219 if ( (val_name == NULL) || (val_data == NULL) )
220 return ERROR_INVALID_PARAMETER;
222 if (val_data[0] == '-')
223 return RegDeleteValue(currentKeyHandle,val_name);
225 /* Get the data type stored into the value field */
226 dwDataType = getDataType(&val_data, &dwParseType);
228 if (dwParseType == REG_SZ) /* no conversion for string */
230 REGPROC_unescape_string(val_data);
231 /* Compute dwLen after REGPROC_unescape_string because it may
232 * have changed the string length and we don't want to store
233 * the extra garbage in the registry.
235 dwLen = strlen(val_data);
236 if (dwLen>0 && val_data[dwLen-1]=='"')
239 val_data[dwLen]='\0';
241 lpbData = (BYTE*) val_data;
243 else if (dwParseType == REG_DWORD) /* Convert the dword types */
245 if (!convertHexToDWord(val_data, &dwData))
246 return ERROR_INVALID_DATA;
247 lpbData = (BYTE*)&dwData;
248 dwLen = sizeof(dwData);
250 else if (dwParseType == REG_BINARY) /* Convert the binary data */
252 lpbData = convertHexCSVToHex(val_data, &dwLen);
254 return ERROR_INVALID_DATA;
256 else /* unknown format */
258 fprintf(stderr,"%s: ERROR, unknown data format\n", getAppName());
259 return ERROR_INVALID_DATA;
262 hRes = RegSetValueEx(
269 if (dwParseType == REG_BINARY)
270 HeapFree(GetProcessHeap(), 0, lpbData);
275 /******************************************************************************
276 * Extracts from [HKEY\some\key\path] or HKEY\some\key\path types of line
277 * the key class (what ends before the first '\')
279 static HKEY getRegClass(LPSTR lpClass)
285 char lpClassCopy[KEY_MAX_LEN];
288 return (HKEY)ERROR_INVALID_PARAMETER;
290 lstrcpynA(lpClassCopy, lpClass, KEY_MAX_LEN);
292 classNameEnd = strchr(lpClassCopy, '\\'); /* The class name ends by '\' */
293 if (!classNameEnd) /* or the whole string */
295 classNameEnd = lpClassCopy + strlen(lpClassCopy);
296 if (classNameEnd[-1] == ']')
301 *classNameEnd = '\0'; /* Isolate the class name */
302 if (lpClassCopy[0] == '[') {
303 classNameBeg = lpClassCopy + 1;
305 classNameBeg = lpClassCopy;
308 for (i = 0; i < REG_CLASS_NUMBER; i++) {
309 if (!strcmp(classNameBeg, reg_class_names[i])) {
310 return reg_class_keys[i];
313 return (HKEY)ERROR_INVALID_PARAMETER;
316 /******************************************************************************
317 * Extracts from [HKEY\some\key\path] or HKEY\some\key\path types of line
318 * the key name (what starts after the first '\')
320 static LPSTR getRegKeyName(LPSTR lpLine)
323 char lpLineCopy[KEY_MAX_LEN];
328 strcpy(lpLineCopy, lpLine);
330 keyNameBeg = strchr(lpLineCopy, '\\'); /* The key name start by '\' */
332 keyNameBeg++; /* is not part of the name */
334 if (lpLine[0] == '[') /* need to find matching ']' */
338 keyNameEnd = strrchr(lpLineCopy, ']');
340 *keyNameEnd = '\0'; /* remove ']' from the key name */
344 keyNameBeg = lpLineCopy + strlen(lpLineCopy); /* branch - empty string */
346 currentKeyName = HeapAlloc(GetProcessHeap(), 0, strlen(keyNameBeg) + 1);
347 CHECK_ENOUGH_MEMORY(currentKeyName);
348 strcpy(currentKeyName, keyNameBeg);
349 return currentKeyName;
352 /******************************************************************************
355 static HRESULT openKey( LPSTR stdInput)
361 if (stdInput == NULL)
362 return ERROR_INVALID_PARAMETER;
364 /* Get the registry class */
365 currentKeyClass = getRegClass(stdInput); /* Sets global variable */
366 if (currentKeyClass == (HKEY)ERROR_INVALID_PARAMETER)
367 return (HRESULT)ERROR_INVALID_PARAMETER;
369 /* Get the key name */
370 currentKeyName = getRegKeyName(stdInput); /* Sets global variable */
371 if (currentKeyName == NULL)
372 return ERROR_INVALID_PARAMETER;
374 hRes = RegCreateKeyEx(
375 currentKeyClass, /* Class */
376 currentKeyName, /* Sub Key */
378 NULL, /* object type */
379 REG_OPTION_NON_VOLATILE, /* option, REG_OPTION_NON_VOLATILE ... */
380 KEY_ALL_ACCESS, /* access mask, KEY_ALL_ACCESS */
381 NULL, /* security attribute */
382 ¤tKeyHandle, /* result */
383 &dwDisp); /* disposition, REG_CREATED_NEW_KEY or
384 REG_OPENED_EXISTING_KEY */
386 if (hRes == ERROR_SUCCESS)
387 bTheKeyIsOpen = TRUE;
393 /******************************************************************************
394 * Close the currently opened key.
396 static void closeKey(void)
398 RegCloseKey(currentKeyHandle);
400 HeapFree(GetProcessHeap(), 0, currentKeyName); /* Allocated by getKeyName */
402 bTheKeyIsOpen = FALSE;
404 currentKeyName = NULL;
406 currentKeyHandle = 0;
409 /******************************************************************************
410 * This function is a wrapper for the setValue function. It prepares the
411 * land and clean the area once completed.
412 * Note: this function modifies the line parameter.
414 * line - registry file unwrapped line. Should have the registry value name and
415 * complete registry value data.
417 static void processSetValue(LPSTR line)
419 LPSTR val_name; /* registry value name */
420 LPSTR val_data; /* registry value data */
422 int line_idx = 0; /* current character under analysis */
426 if (line[line_idx] == '@' && line[line_idx + 1] == '=') {
427 line[line_idx] = '\0';
430 } else if (line[line_idx] == '\"') {
432 val_name = line + line_idx;
434 if (line[line_idx] == '\\') /* skip escaped character */
438 if (line[line_idx] == '\"') {
439 line[line_idx] = '\0';
447 if (line[line_idx] != '=') {
448 line[line_idx] = '\"';
449 fprintf(stderr,"Warning! unrecognized line:\n%s\n", line);
454 fprintf(stderr,"Warning! unrecognized line:\n%s\n", line);
457 line_idx++; /* skip the '=' character */
458 val_data = line + line_idx;
460 REGPROC_unescape_string(val_name);
461 hRes = setValue(val_name, val_data);
462 if ( hRes != ERROR_SUCCESS )
463 fprintf(stderr,"%s: ERROR Key %s not created. Value: %s, Data: %s\n",
470 /******************************************************************************
471 * This function receives the currently read entry and performs the
472 * corresponding action.
474 static void processRegEntry(LPSTR stdInput)
477 * We encountered the end of the file, make sure we
478 * close the opened key and exit
480 if (stdInput == NULL) {
481 if (bTheKeyIsOpen != FALSE)
487 if ( stdInput[0] == '[') /* We are reading a new key */
489 if ( bTheKeyIsOpen != FALSE )
490 closeKey(); /* Close the previous key before */
492 /* delete the key if we encounter '-' at the start of reg key */
493 if ( stdInput[1] == '-')
495 int last_chr = strlen(stdInput) - 1;
497 /* skip leading "[-" and get rid of trailing "]" */
498 if (stdInput[last_chr] == ']')
499 stdInput[last_chr] = '\0';
500 delete_registry_key(stdInput+2);
504 if ( openKey(stdInput) != ERROR_SUCCESS )
505 fprintf(stderr,"%s: setValue failed to open key %s\n",
506 getAppName(), stdInput);
507 } else if( ( bTheKeyIsOpen ) &&
508 (( stdInput[0] == '@') || /* reading a default @=data pair */
509 ( stdInput[0] == '\"'))) /* reading a new value=data pair */
511 processSetValue(stdInput);
512 } else /* since we are assuming that the */
513 { /* file format is valid we must */
514 if ( bTheKeyIsOpen ) /* be reading a blank line which */
515 closeKey(); /* indicate end of this key processing */
519 /******************************************************************************
520 * Processes a registry file.
521 * Correctly processes comments (in # form), line continuation.
524 * in - input stream to read from
526 void processRegLines(FILE *in)
528 LPSTR line = NULL; /* line read from input stream */
529 ULONG lineSize = REG_VAL_BUF_SIZE;
531 line = HeapAlloc(GetProcessHeap(), 0, lineSize);
532 CHECK_ENOUGH_MEMORY(line);
535 LPSTR s; /* The pointer into line for where the current fgets should read */
538 size_t size_remaining;
540 char *s_eol; /* various local uses */
542 /* Do we need to expand the buffer ? */
543 assert (s >= line && s <= line + lineSize);
544 size_remaining = lineSize - (s-line);
545 if (size_remaining < 2) /* room for 1 character and the \0 */
548 size_t new_size = lineSize + REG_VAL_BUF_SIZE;
549 if (new_size > lineSize) /* no arithmetic overflow */
550 new_buffer = HeapReAlloc (GetProcessHeap(), 0, line, new_size);
553 CHECK_ENOUGH_MEMORY(new_buffer);
555 s = line + lineSize - size_remaining;
557 size_remaining = lineSize - (s-line);
560 /* Get as much as possible into the buffer, terminated either by
561 * eof, error, eol or getting the maximum amount. Abort on error.
563 size_to_get = (size_remaining > INT_MAX ? INT_MAX : size_remaining);
564 if (NULL == fgets (s, size_to_get, in)) {
566 perror ("While reading input");
571 /* It is not clear to me from the definition that the
572 * contents of the buffer are well defined on detecting
573 * an eof without managing to read anything.
578 /* If we didn't read the eol nor the eof go around for the rest */
579 s_eol = strchr (s, '\n');
580 if (!feof (in) && !s_eol) {
581 s = strchr (s, '\0');
582 /* It should be s + size_to_get - 1 but this is safer */
586 /* If it is a comment line then discard it and go around again */
587 if (line [0] == '#') {
592 /* Remove any line feed. Leave s_eol on the \0 */
595 if (s_eol > line && *(s_eol-1) == '\r')
598 s_eol = strchr (s, '\0');
600 /* If there is a concatenating \\ then go around again */
601 if (s_eol > line && *(s_eol-1) == '\\') {
604 /* The following error protection could be made more self-
605 * correcting but I thought it not worth trying.
607 if ((c = fgetc (in)) == EOF || c != ' ' ||
608 (c = fgetc (in)) == EOF || c != ' ')
609 fprintf(stderr,"%s: ERROR - invalid continuation.\n",
614 break; /* That is the full virtual line */
617 processRegEntry(line);
619 processRegEntry(NULL);
621 HeapFree(GetProcessHeap(), 0, line);
624 /****************************************************************************
625 * REGPROC_print_error
627 * Print the message for GetLastError
630 static void REGPROC_print_error(void)
636 error_code = GetLastError ();
637 status = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
638 NULL, error_code, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
640 fprintf(stderr,"%s: Cannot display message for error %d, status %d\n",
641 getAppName(), error_code, GetLastError());
645 LocalFree((HLOCAL)lpMsgBuf);
649 /******************************************************************************
650 * Checks whether the buffer has enough room for the string or required size.
651 * Resizes the buffer if necessary.
654 * buffer - pointer to a buffer for string
655 * len - current length of the buffer in characters.
656 * required_len - length of the string to place to the buffer in characters.
657 * The length does not include the terminating null character.
659 static void REGPROC_resize_char_buffer(CHAR **buffer, DWORD *len, DWORD required_len)
662 if (required_len > *len) {
665 *buffer = HeapAlloc(GetProcessHeap(), 0, *len * sizeof(**buffer));
667 *buffer = HeapReAlloc(GetProcessHeap(), 0, *buffer, *len * sizeof(**buffer));
668 CHECK_ENOUGH_MEMORY(*buffer);
672 /******************************************************************************
673 * Prints string str to file
675 static void REGPROC_export_string(FILE *file, CHAR *str)
677 size_t len = strlen(str);
680 /* escaping characters */
681 for (i = 0; i < len; i++) {
700 /******************************************************************************
701 * Writes contents of the registry key to the specified file stream.
704 * file - writable file stream to export registry branch to.
705 * key - registry branch to export.
706 * reg_key_name_buf - name of the key with registry class.
707 * Is resized if necessary.
708 * reg_key_name_len - length of the buffer for the registry class in characters.
709 * val_name_buf - buffer for storing value name.
710 * Is resized if necessary.
711 * val_name_len - length of the buffer for storing value names in characters.
712 * val_buf - buffer for storing values while extracting.
713 * Is resized if necessary.
714 * val_size - size of the buffer for storing values in bytes.
716 static void export_hkey(FILE *file, HKEY key,
717 CHAR **reg_key_name_buf, DWORD *reg_key_name_len,
718 CHAR **val_name_buf, DWORD *val_name_len,
719 BYTE **val_buf, DWORD *val_size)
721 DWORD max_sub_key_len;
722 DWORD max_val_name_len;
729 /* get size information and resize the buffers if necessary */
730 if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL,
731 &max_sub_key_len, NULL,
732 NULL, &max_val_name_len, &max_val_size, NULL, NULL
733 ) != ERROR_SUCCESS) {
734 REGPROC_print_error();
736 curr_len = strlen(*reg_key_name_buf);
737 REGPROC_resize_char_buffer(reg_key_name_buf, reg_key_name_len,
738 max_sub_key_len + curr_len + 1);
739 REGPROC_resize_char_buffer(val_name_buf, val_name_len,
741 if (max_val_size > *val_size) {
742 *val_size = max_val_size;
743 if (!*val_buf) *val_buf = HeapAlloc(GetProcessHeap(), 0, *val_size);
744 else *val_buf = HeapReAlloc(GetProcessHeap(), 0, *val_buf, *val_size);
745 CHECK_ENOUGH_MEMORY(val_buf);
748 /* output data for the current key */
750 fputs(*reg_key_name_buf, file);
752 /* print all the values */
757 DWORD val_name_len1 = *val_name_len;
758 DWORD val_size1 = *val_size;
759 ret = RegEnumValue(key, i, *val_name_buf, &val_name_len1, NULL,
760 &value_type, *val_buf, &val_size1);
761 if (ret != ERROR_SUCCESS) {
763 if (ret != ERROR_NO_MORE_ITEMS) {
764 REGPROC_print_error();
769 if ((*val_name_buf)[0]) {
771 REGPROC_export_string(file, *val_name_buf);
777 switch (value_type) {
781 REGPROC_export_string(file, (char*) *val_buf);
786 fprintf(file, "dword:%08x\n", *((DWORD *)*val_buf));
790 fprintf(stderr,"%s: warning - unsupported registry format '%d', "
792 getAppName(), value_type);
793 fprintf(stderr,"key name: \"%s\"\n", *reg_key_name_buf);
794 fprintf(stderr,"value name:\"%s\"\n\n", *val_name_buf);
800 const CHAR *hex_prefix;
804 if (value_type == REG_BINARY) {
808 sprintf(buf, "hex(%d):", value_type);
811 /* position of where the next character will be printed */
812 /* NOTE: yes, strlen("hex:") is used even for hex(x): */
813 cur_pos = strlen("\"\"=") + strlen("hex:") +
814 strlen(*val_name_buf);
816 fputs(hex_prefix, file);
817 for (i1 = 0; i1 < val_size1; i1++) {
818 fprintf(file, "%02x", (unsigned int)(*val_buf)[i1]);
819 if (i1 + 1 < val_size1) {
825 if (cur_pos > REG_FILE_HEX_LINE_LEN) {
826 fputs("\\\n ", file);
839 (*reg_key_name_buf)[curr_len] = '\\';
841 DWORD buf_len = *reg_key_name_len - curr_len;
843 ret = RegEnumKeyEx(key, i, *reg_key_name_buf + curr_len + 1, &buf_len,
844 NULL, NULL, NULL, NULL);
845 if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA) {
847 if (ret != ERROR_NO_MORE_ITEMS) {
848 REGPROC_print_error();
854 if (RegOpenKey(key, *reg_key_name_buf + curr_len + 1,
855 &subkey) == ERROR_SUCCESS) {
856 export_hkey(file, subkey, reg_key_name_buf, reg_key_name_len,
857 val_name_buf, val_name_len, val_buf, val_size);
860 REGPROC_print_error();
864 (*reg_key_name_buf)[curr_len] = '\0';
867 /******************************************************************************
868 * Open file for export.
870 static FILE *REGPROC_open_export_file(CHAR *file_name)
872 FILE *file = fopen(file_name, "w");
875 fprintf(stderr,"%s: Can't open file \"%s\"\n", getAppName(), file_name);
878 fputs("REGEDIT4\n", file);
882 /******************************************************************************
883 * Writes contents of the registry key to the specified file stream.
886 * file_name - name of a file to export registry branch to.
887 * reg_key_name - registry branch to export. The whole registry is exported if
888 * reg_key_name is NULL or contains an empty string.
890 BOOL export_registry_key(CHAR *file_name, CHAR *reg_key_name)
894 CHAR *reg_key_name_buf;
897 DWORD reg_key_name_len = KEY_MAX_LEN;
898 DWORD val_name_len = KEY_MAX_LEN;
899 DWORD val_size = REG_VAL_BUF_SIZE;
902 reg_key_name_buf = HeapAlloc(GetProcessHeap(), 0,
903 reg_key_name_len * sizeof(*reg_key_name_buf));
904 val_name_buf = HeapAlloc(GetProcessHeap(), 0,
905 val_name_len * sizeof(*val_name_buf));
906 val_buf = HeapAlloc(GetProcessHeap(), 0, val_size);
907 CHECK_ENOUGH_MEMORY(reg_key_name_buf && val_name_buf && val_buf);
909 if (reg_key_name && reg_key_name[0]) {
913 REGPROC_resize_char_buffer(®_key_name_buf, ®_key_name_len,
914 strlen(reg_key_name));
915 strcpy(reg_key_name_buf, reg_key_name);
917 /* open the specified key */
918 reg_key_class = getRegClass(reg_key_name);
919 if (reg_key_class == (HKEY)ERROR_INVALID_PARAMETER) {
920 fprintf(stderr,"%s: Incorrect registry class specification in '%s'\n",
921 getAppName(), reg_key_name);
924 branch_name = getRegKeyName(reg_key_name);
925 CHECK_ENOUGH_MEMORY(branch_name);
926 if (!branch_name[0]) {
927 /* no branch - registry class is specified */
928 file = REGPROC_open_export_file(file_name);
929 export_hkey(file, reg_key_class,
930 ®_key_name_buf, ®_key_name_len,
931 &val_name_buf, &val_name_len,
932 &val_buf, &val_size);
933 } else if (RegOpenKey(reg_key_class, branch_name, &key) == ERROR_SUCCESS) {
934 file = REGPROC_open_export_file(file_name);
935 export_hkey(file, key,
936 ®_key_name_buf, ®_key_name_len,
937 &val_name_buf, &val_name_len,
938 &val_buf, &val_size);
941 fprintf(stderr,"%s: Can't export. Registry key '%s' does not exist!\n",
942 getAppName(), reg_key_name);
943 REGPROC_print_error();
945 HeapFree(GetProcessHeap(), 0, branch_name);
949 /* export all registry classes */
950 file = REGPROC_open_export_file(file_name);
951 for (i = 0; i < REG_CLASS_NUMBER; i++) {
952 /* do not export HKEY_CLASSES_ROOT */
953 if (reg_class_keys[i] != HKEY_CLASSES_ROOT &&
954 reg_class_keys[i] != HKEY_CURRENT_USER &&
955 reg_class_keys[i] != HKEY_CURRENT_CONFIG &&
956 reg_class_keys[i] != HKEY_DYN_DATA) {
957 strcpy(reg_key_name_buf, reg_class_names[i]);
958 export_hkey(file, reg_class_keys[i],
959 ®_key_name_buf, ®_key_name_len,
960 &val_name_buf, &val_name_len,
961 &val_buf, &val_size);
969 HeapFree(GetProcessHeap(), 0, reg_key_name);
970 HeapFree(GetProcessHeap(), 0, val_buf);
974 /******************************************************************************
975 * Reads contents of the specified file into the registry.
977 BOOL import_registry_file(LPTSTR filename)
979 FILE* reg_file = fopen(filename, "r");
982 processRegLines(reg_file);
988 /******************************************************************************
989 * Recursive function which removes the registry key with all subkeys.
991 static void delete_branch(HKEY key,
992 CHAR **reg_key_name_buf, DWORD *reg_key_name_len)
995 DWORD max_sub_key_len;
1001 if (RegOpenKey(key, *reg_key_name_buf, &branch_key) != ERROR_SUCCESS) {
1002 REGPROC_print_error();
1005 /* get size information and resize the buffers if necessary */
1006 if (RegQueryInfoKey(branch_key, NULL, NULL, NULL,
1007 &subkeys, &max_sub_key_len,
1008 NULL, NULL, NULL, NULL, NULL, NULL
1009 ) != ERROR_SUCCESS) {
1010 REGPROC_print_error();
1012 curr_len = strlen(*reg_key_name_buf);
1013 REGPROC_resize_char_buffer(reg_key_name_buf, reg_key_name_len,
1014 max_sub_key_len + curr_len + 1);
1016 (*reg_key_name_buf)[curr_len] = '\\';
1017 for (i = subkeys - 1; i >= 0; i--) {
1018 DWORD buf_len = *reg_key_name_len - curr_len;
1020 ret = RegEnumKeyEx(branch_key, i, *reg_key_name_buf + curr_len + 1,
1021 &buf_len, NULL, NULL, NULL, NULL);
1022 if (ret != ERROR_SUCCESS &&
1023 ret != ERROR_MORE_DATA &&
1024 ret != ERROR_NO_MORE_ITEMS) {
1025 REGPROC_print_error();
1027 delete_branch(key, reg_key_name_buf, reg_key_name_len);
1030 (*reg_key_name_buf)[curr_len] = '\0';
1031 RegCloseKey(branch_key);
1032 RegDeleteKey(key, *reg_key_name_buf);
1035 /******************************************************************************
1036 * Removes the registry key with all subkeys. Parses full key name.
1039 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1040 * empty, points to register key class, does not exist.
1042 void delete_registry_key(CHAR *reg_key_name)
1045 DWORD branch_name_len;
1049 if (!reg_key_name || !reg_key_name[0])
1051 /* open the specified key */
1052 reg_key_class = getRegClass(reg_key_name);
1053 if (reg_key_class == (HKEY)ERROR_INVALID_PARAMETER) {
1054 fprintf(stderr,"%s: Incorrect registry class specification in '%s'\n",
1055 getAppName(), reg_key_name);
1058 branch_name = getRegKeyName(reg_key_name);
1059 CHECK_ENOUGH_MEMORY(branch_name);
1060 branch_name_len = strlen(branch_name);
1061 if (!branch_name[0]) {
1062 fprintf(stderr,"%s: Can't delete registry class '%s'\n",
1063 getAppName(), reg_key_name);
1066 if (RegOpenKey(reg_key_class, branch_name, &branch_key) == ERROR_SUCCESS) {
1067 /* check whether the key exists */
1068 RegCloseKey(branch_key);
1069 delete_branch(reg_key_class, &branch_name, &branch_name_len);
1071 HeapFree(GetProcessHeap(), 0, branch_name);