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 static const CHAR
*reg_class_names
[] = {
38 "HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_CLASSES_ROOT",
39 "HKEY_CURRENT_CONFIG", "HKEY_CURRENT_USER", "HKEY_DYN_DATA"
42 #define REG_CLASS_NUMBER (sizeof(reg_class_names) / sizeof(reg_class_names[0]))
44 static HKEY reg_class_keys
[REG_CLASS_NUMBER
] = {
45 HKEY_LOCAL_MACHINE
, HKEY_USERS
, HKEY_CLASSES_ROOT
,
46 HKEY_CURRENT_CONFIG
, HKEY_CURRENT_USER
, HKEY_DYN_DATA
50 #define NOT_ENOUGH_MEMORY 1
53 /* processing macros */
55 /* common check of memory allocation results */
56 #define CHECK_ENOUGH_MEMORY(p) \
59 fprintf(stderr,"%s: file %s, line %d: Not enough memory\n", \
60 getAppName(), __FILE__, __LINE__); \
61 exit(NOT_ENOUGH_MEMORY); \
64 /******************************************************************************
65 * Converts a hex representation of a DWORD into a DWORD.
67 static BOOL
convertHexToDWord(char* str
, DWORD
*dw
)
70 if (strlen(str
) > 8 || sscanf(str
, "%x%c", dw
, &dummy
) != 1) {
71 fprintf(stderr
,"%s: ERROR, invalid hex value\n", getAppName());
77 /******************************************************************************
78 * Converts a hex comma separated values list into a binary string.
80 static BYTE
* convertHexCSVToHex(char *str
, DWORD
*size
)
85 /* The worst case is 1 digit + 1 comma per byte */
86 *size
=(strlen(str
)+1)/2;
87 data
=HeapAlloc(GetProcessHeap(), 0, *size
);
88 CHECK_ENOUGH_MEMORY(data
);
97 if (s
[1] != ',' && s
[1] != '\0' && s
[2] != ',' && s
[2] != '\0') {
98 fprintf(stderr
,"%s: ERROR converting CSV hex stream. Invalid sequence at '%s'\n",
100 HeapFree(GetProcessHeap(), 0, data
);
103 if (sscanf(s
, "%x%c", &wc
, &dummy
) < 1 || dummy
!= ',') {
104 fprintf(stderr
,"%s: ERROR converting CSV hex stream. Invalid value at '%s'\n",
106 HeapFree(GetProcessHeap(), 0, data
);
112 s
+=(wc
< 0x10 ? 2 : 3);
118 /******************************************************************************
119 * This function returns the HKEY associated with the data type encoded in the
120 * value. It modifies the input parameter (key value) in order to skip this
121 * "now useless" data type information.
123 * Note: Updated based on the algorithm used in 'server/registry.c'
125 static DWORD
getDataType(LPSTR
*lpValue
, DWORD
* parse_type
)
127 struct data_type
{ const char *tag
; int len
; int type
; int parse_type
; };
129 static const struct data_type data_types
[] = { /* actual type */ /* type to assume for parsing */
130 { "\"", 1, REG_SZ
, REG_SZ
},
131 { "str:\"", 5, REG_SZ
, REG_SZ
},
132 { "str(2):\"", 8, REG_EXPAND_SZ
, REG_SZ
},
133 { "hex:", 4, REG_BINARY
, REG_BINARY
},
134 { "dword:", 6, REG_DWORD
, REG_DWORD
},
135 { "hex(", 4, -1, REG_BINARY
},
139 const struct data_type
*ptr
;
142 for (ptr
= data_types
; ptr
->tag
; ptr
++) {
143 if (memcmp( ptr
->tag
, *lpValue
, ptr
->len
))
147 *parse_type
= ptr
->parse_type
;
152 /* "hex(xx):" is special */
153 type
= (int)strtoul( *lpValue
, &end
, 16 );
154 if (**lpValue
=='\0' || *end
!=')' || *(end
+1)!=':') {
162 *parse_type
=REG_NONE
;
166 /******************************************************************************
167 * Replaces escape sequences with the characters.
169 static void REGPROC_unescape_string(LPSTR str
)
171 int str_idx
= 0; /* current character under analysis */
172 int val_idx
= 0; /* the last character of the unescaped string */
173 int len
= strlen(str
);
174 for (str_idx
= 0; str_idx
< len
; str_idx
++, val_idx
++) {
175 if (str
[str_idx
] == '\\') {
177 switch (str
[str_idx
]) {
183 str
[val_idx
] = str
[str_idx
];
186 fprintf(stderr
,"Warning! Unrecognized escape sequence: \\%c'\n",
188 str
[val_idx
] = str
[str_idx
];
192 str
[val_idx
] = str
[str_idx
];
198 /******************************************************************************
199 * Parses HKEY_SOME_ROOT\some\key\path to get the root key handle and
200 * extract the key path (what comes after the first '\').
202 static BOOL
parseKeyName(LPSTR lpKeyName
, HKEY
*hKey
, LPSTR
*lpKeyPath
)
207 if (lpKeyName
== NULL
)
210 lpSlash
= strchr(lpKeyName
, '\\');
213 len
= lpSlash
-lpKeyName
;
217 len
= strlen(lpKeyName
);
218 lpSlash
= lpKeyName
+len
;
221 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++) {
222 if (strncmp(lpKeyName
, reg_class_names
[i
], len
) == 0 &&
223 len
== strlen(reg_class_names
[i
])) {
224 *hKey
= reg_class_keys
[i
];
231 if (*lpSlash
!= '\0')
233 *lpKeyPath
= lpSlash
;
237 /* Globals used by the setValue() & co */
238 static LPSTR currentKeyName
;
239 static HKEY currentKeyHandle
= NULL
;
241 /******************************************************************************
242 * Sets the value with name val_name to the data in val_data for the currently
246 * val_name - name of the registry value
247 * val_data - registry value data
249 static LONG
setValue(LPSTR val_name
, LPSTR val_data
)
252 DWORD dwDataType
, dwParseType
;
256 if ( (val_name
== NULL
) || (val_data
== NULL
) )
257 return ERROR_INVALID_PARAMETER
;
259 if (strcmp(val_data
, "-") == 0)
261 res
=RegDeleteValue(currentKeyHandle
,val_name
);
262 return (res
== ERROR_FILE_NOT_FOUND
? ERROR_SUCCESS
: res
);
265 /* Get the data type stored into the value field */
266 dwDataType
= getDataType(&val_data
, &dwParseType
);
268 if (dwParseType
== REG_SZ
) /* no conversion for string */
270 REGPROC_unescape_string(val_data
);
271 /* Compute dwLen after REGPROC_unescape_string because it may
272 * have changed the string length and we don't want to store
273 * the extra garbage in the registry.
275 dwLen
= strlen(val_data
);
276 if (dwLen
>0 && val_data
[dwLen
-1]=='"')
279 val_data
[dwLen
]='\0';
281 lpbData
= (BYTE
*) val_data
;
283 else if (dwParseType
== REG_DWORD
) /* Convert the dword types */
285 if (!convertHexToDWord(val_data
, &dwData
))
286 return ERROR_INVALID_DATA
;
287 lpbData
= (BYTE
*)&dwData
;
288 dwLen
= sizeof(dwData
);
290 else if (dwParseType
== REG_BINARY
) /* Convert the binary data */
292 lpbData
= convertHexCSVToHex(val_data
, &dwLen
);
294 return ERROR_INVALID_DATA
;
296 else /* unknown format */
298 fprintf(stderr
,"%s: ERROR, unknown data format\n", getAppName());
299 return ERROR_INVALID_DATA
;
309 if (dwParseType
== REG_BINARY
)
310 HeapFree(GetProcessHeap(), 0, lpbData
);
314 /******************************************************************************
315 * A helper function for processRegEntry() that opens the current key.
316 * That key must be closed by calling closeKey().
318 static LONG
openKey(LPSTR stdInput
)
326 if (stdInput
== NULL
)
327 return ERROR_INVALID_PARAMETER
;
329 /* Get the registry class */
330 if (!parseKeyName(stdInput
, &keyClass
, &keyPath
))
331 return ERROR_INVALID_PARAMETER
;
333 res
= RegCreateKeyEx(
334 keyClass
, /* Class */
335 keyPath
, /* Sub Key */
337 NULL
, /* object type */
338 REG_OPTION_NON_VOLATILE
, /* option, REG_OPTION_NON_VOLATILE ... */
339 KEY_ALL_ACCESS
, /* access mask, KEY_ALL_ACCESS */
340 NULL
, /* security attribute */
341 ¤tKeyHandle
, /* result */
342 &dwDisp
); /* disposition, REG_CREATED_NEW_KEY or
343 REG_OPENED_EXISTING_KEY */
345 if (res
== ERROR_SUCCESS
)
347 currentKeyName
= HeapAlloc(GetProcessHeap(), 0, strlen(stdInput
)+1);
348 CHECK_ENOUGH_MEMORY(currentKeyName
);
349 strcpy(currentKeyName
, stdInput
);
353 currentKeyHandle
= NULL
;
360 /******************************************************************************
361 * Close the currently opened key.
363 static void closeKey(void)
365 if (currentKeyHandle
)
367 HeapFree(GetProcessHeap(), 0, currentKeyName
);
368 RegCloseKey(currentKeyHandle
);
369 currentKeyHandle
= NULL
;
373 /******************************************************************************
374 * This function is a wrapper for the setValue function. It prepares the
375 * land and clean the area once completed.
376 * Note: this function modifies the line parameter.
378 * line - registry file unwrapped line. Should have the registry value name and
379 * complete registry value data.
381 static void processSetValue(LPSTR line
)
383 LPSTR val_name
; /* registry value name */
384 LPSTR val_data
; /* registry value data */
386 int line_idx
= 0; /* current character under analysis */
390 if (line
[line_idx
] == '@' && line
[line_idx
+ 1] == '=') {
391 line
[line_idx
] = '\0';
394 } else if (line
[line_idx
] == '\"') {
396 val_name
= line
+ line_idx
;
398 if (line
[line_idx
] == '\\') /* skip escaped character */
402 if (line
[line_idx
] == '\"') {
403 line
[line_idx
] = '\0';
411 if (line
[line_idx
] != '=') {
412 line
[line_idx
] = '\"';
413 fprintf(stderr
,"Warning! unrecognized line:\n%s\n", line
);
418 fprintf(stderr
,"Warning! unrecognized line:\n%s\n", line
);
421 line_idx
++; /* skip the '=' character */
422 val_data
= line
+ line_idx
;
424 REGPROC_unescape_string(val_name
);
425 res
= setValue(val_name
, val_data
);
426 if ( res
!= ERROR_SUCCESS
)
427 fprintf(stderr
,"%s: ERROR Key %s not created. Value: %s, Data: %s\n",
434 /******************************************************************************
435 * This function receives the currently read entry and performs the
436 * corresponding action.
438 static void processRegEntry(LPSTR stdInput
)
441 * We encountered the end of the file, make sure we
442 * close the opened key and exit
444 if (stdInput
== NULL
) {
449 if ( stdInput
[0] == '[') /* We are reading a new key */
452 closeKey(); /* Close the previous key */
454 /* Get rid of the square brackets */
456 keyEnd
= strrchr(stdInput
, ']');
460 /* delete the key if we encounter '-' at the start of reg key */
461 if ( stdInput
[0] == '-')
462 delete_registry_key(stdInput
+1);
463 else if ( openKey(stdInput
) != ERROR_SUCCESS
)
464 fprintf(stderr
,"%s: setValue failed to open key %s\n",
465 getAppName(), stdInput
);
466 } else if( currentKeyHandle
&&
467 (( stdInput
[0] == '@') || /* reading a default @=data pair */
468 ( stdInput
[0] == '\"'))) /* reading a new value=data pair */
470 processSetValue(stdInput
);
473 /* Since we are assuming that the file format is valid we must be
474 * reading a blank line which indicates the end of this key processing
480 /******************************************************************************
481 * Processes a registry file.
482 * Correctly processes comments (in # form), line continuation.
485 * in - input stream to read from
487 void processRegLines(FILE *in
)
489 LPSTR line
= NULL
; /* line read from input stream */
490 ULONG lineSize
= REG_VAL_BUF_SIZE
;
492 line
= HeapAlloc(GetProcessHeap(), 0, lineSize
);
493 CHECK_ENOUGH_MEMORY(line
);
496 LPSTR s
; /* The pointer into line for where the current fgets should read */
499 size_t size_remaining
;
501 char *s_eol
; /* various local uses */
503 /* Do we need to expand the buffer ? */
504 assert (s
>= line
&& s
<= line
+ lineSize
);
505 size_remaining
= lineSize
- (s
-line
);
506 if (size_remaining
< 2) /* room for 1 character and the \0 */
509 size_t new_size
= lineSize
+ REG_VAL_BUF_SIZE
;
510 if (new_size
> lineSize
) /* no arithmetic overflow */
511 new_buffer
= HeapReAlloc (GetProcessHeap(), 0, line
, new_size
);
514 CHECK_ENOUGH_MEMORY(new_buffer
);
516 s
= line
+ lineSize
- size_remaining
;
518 size_remaining
= lineSize
- (s
-line
);
521 /* Get as much as possible into the buffer, terminated either by
522 * eof, error, eol or getting the maximum amount. Abort on error.
524 size_to_get
= (size_remaining
> INT_MAX
? INT_MAX
: size_remaining
);
525 if (NULL
== fgets (s
, size_to_get
, in
)) {
527 perror ("While reading input");
532 /* It is not clear to me from the definition that the
533 * contents of the buffer are well defined on detecting
534 * an eof without managing to read anything.
539 /* If we didn't read the eol nor the eof go around for the rest */
540 s_eol
= strchr (s
, '\n');
541 if (!feof (in
) && !s_eol
) {
542 s
= strchr (s
, '\0');
543 /* It should be s + size_to_get - 1 but this is safer */
547 /* If it is a comment line then discard it and go around again */
548 if (line
[0] == '#') {
553 /* Remove any line feed. Leave s_eol on the \0 */
556 if (s_eol
> line
&& *(s_eol
-1) == '\r')
559 s_eol
= strchr (s
, '\0');
561 /* If there is a concatenating \\ then go around again */
562 if (s_eol
> line
&& *(s_eol
-1) == '\\') {
565 /* The following error protection could be made more self-
566 * correcting but I thought it not worth trying.
568 if ((c
= fgetc (in
)) == EOF
|| c
!= ' ' ||
569 (c
= fgetc (in
)) == EOF
|| c
!= ' ')
570 fprintf(stderr
,"%s: ERROR - invalid continuation.\n",
575 break; /* That is the full virtual line */
578 processRegEntry(line
);
580 processRegEntry(NULL
);
582 HeapFree(GetProcessHeap(), 0, line
);
585 /****************************************************************************
586 * REGPROC_print_error
588 * Print the message for GetLastError
591 static void REGPROC_print_error(void)
597 error_code
= GetLastError ();
598 status
= FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
599 NULL
, error_code
, 0, (LPTSTR
) &lpMsgBuf
, 0, NULL
);
601 fprintf(stderr
,"%s: Cannot display message for error %d, status %d\n",
602 getAppName(), error_code
, GetLastError());
606 LocalFree((HLOCAL
)lpMsgBuf
);
610 /******************************************************************************
611 * Checks whether the buffer has enough room for the string or required size.
612 * Resizes the buffer if necessary.
615 * buffer - pointer to a buffer for string
616 * len - current length of the buffer in characters.
617 * required_len - length of the string to place to the buffer in characters.
618 * The length does not include the terminating null character.
620 static void REGPROC_resize_char_buffer(CHAR
**buffer
, DWORD
*len
, DWORD required_len
)
623 if (required_len
> *len
) {
626 *buffer
= HeapAlloc(GetProcessHeap(), 0, *len
* sizeof(**buffer
));
628 *buffer
= HeapReAlloc(GetProcessHeap(), 0, *buffer
, *len
* sizeof(**buffer
));
629 CHECK_ENOUGH_MEMORY(*buffer
);
633 /******************************************************************************
634 * Prints string str to file
636 static void REGPROC_export_string(FILE *file
, CHAR
*str
)
638 size_t len
= strlen(str
);
641 /* escaping characters */
642 for (i
= 0; i
< len
; i
++) {
661 /******************************************************************************
662 * Writes contents of the registry key to the specified file stream.
665 * file - writable file stream to export registry branch to.
666 * key - registry branch to export.
667 * reg_key_name_buf - name of the key with registry class.
668 * Is resized if necessary.
669 * reg_key_name_len - length of the buffer for the registry class in characters.
670 * val_name_buf - buffer for storing value name.
671 * Is resized if necessary.
672 * val_name_len - length of the buffer for storing value names in characters.
673 * val_buf - buffer for storing values while extracting.
674 * Is resized if necessary.
675 * val_size - size of the buffer for storing values in bytes.
677 static void export_hkey(FILE *file
, HKEY key
,
678 CHAR
**reg_key_name_buf
, DWORD
*reg_key_name_len
,
679 CHAR
**val_name_buf
, DWORD
*val_name_len
,
680 BYTE
**val_buf
, DWORD
*val_size
)
682 DWORD max_sub_key_len
;
683 DWORD max_val_name_len
;
690 /* get size information and resize the buffers if necessary */
691 if (RegQueryInfoKey(key
, NULL
, NULL
, NULL
, NULL
,
692 &max_sub_key_len
, NULL
,
693 NULL
, &max_val_name_len
, &max_val_size
, NULL
, NULL
694 ) != ERROR_SUCCESS
) {
695 REGPROC_print_error();
697 curr_len
= strlen(*reg_key_name_buf
);
698 REGPROC_resize_char_buffer(reg_key_name_buf
, reg_key_name_len
,
699 max_sub_key_len
+ curr_len
+ 1);
700 REGPROC_resize_char_buffer(val_name_buf
, val_name_len
,
702 if (max_val_size
> *val_size
) {
703 *val_size
= max_val_size
;
704 if (!*val_buf
) *val_buf
= HeapAlloc(GetProcessHeap(), 0, *val_size
);
705 else *val_buf
= HeapReAlloc(GetProcessHeap(), 0, *val_buf
, *val_size
);
706 CHECK_ENOUGH_MEMORY(val_buf
);
709 /* output data for the current key */
711 fputs(*reg_key_name_buf
, file
);
713 /* print all the values */
718 DWORD val_name_len1
= *val_name_len
;
719 DWORD val_size1
= *val_size
;
720 ret
= RegEnumValue(key
, i
, *val_name_buf
, &val_name_len1
, NULL
,
721 &value_type
, *val_buf
, &val_size1
);
722 if (ret
!= ERROR_SUCCESS
) {
724 if (ret
!= ERROR_NO_MORE_ITEMS
) {
725 REGPROC_print_error();
730 if ((*val_name_buf
)[0]) {
732 REGPROC_export_string(file
, *val_name_buf
);
738 switch (value_type
) {
742 REGPROC_export_string(file
, (char*) *val_buf
);
747 fprintf(file
, "dword:%08x\n", *((DWORD
*)*val_buf
));
751 fprintf(stderr
,"%s: warning - unsupported registry format '%d', "
753 getAppName(), value_type
);
754 fprintf(stderr
,"key name: \"%s\"\n", *reg_key_name_buf
);
755 fprintf(stderr
,"value name:\"%s\"\n\n", *val_name_buf
);
761 const CHAR
*hex_prefix
;
765 if (value_type
== REG_BINARY
) {
769 sprintf(buf
, "hex(%d):", value_type
);
772 /* position of where the next character will be printed */
773 /* NOTE: yes, strlen("hex:") is used even for hex(x): */
774 cur_pos
= strlen("\"\"=") + strlen("hex:") +
775 strlen(*val_name_buf
);
777 fputs(hex_prefix
, file
);
778 for (i1
= 0; i1
< val_size1
; i1
++) {
779 fprintf(file
, "%02x", (unsigned int)(*val_buf
)[i1
]);
780 if (i1
+ 1 < val_size1
) {
786 if (cur_pos
> REG_FILE_HEX_LINE_LEN
) {
787 fputs("\\\n ", file
);
800 (*reg_key_name_buf
)[curr_len
] = '\\';
802 DWORD buf_len
= *reg_key_name_len
- curr_len
;
804 ret
= RegEnumKeyEx(key
, i
, *reg_key_name_buf
+ curr_len
+ 1, &buf_len
,
805 NULL
, NULL
, NULL
, NULL
);
806 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_MORE_DATA
) {
808 if (ret
!= ERROR_NO_MORE_ITEMS
) {
809 REGPROC_print_error();
815 if (RegOpenKey(key
, *reg_key_name_buf
+ curr_len
+ 1,
816 &subkey
) == ERROR_SUCCESS
) {
817 export_hkey(file
, subkey
, reg_key_name_buf
, reg_key_name_len
,
818 val_name_buf
, val_name_len
, val_buf
, val_size
);
821 REGPROC_print_error();
825 (*reg_key_name_buf
)[curr_len
] = '\0';
828 /******************************************************************************
829 * Open file for export.
831 static FILE *REGPROC_open_export_file(CHAR
*file_name
)
833 FILE *file
= fopen(file_name
, "w");
836 fprintf(stderr
,"%s: Can't open file \"%s\"\n", getAppName(), file_name
);
839 fputs("REGEDIT4\n", file
);
843 /******************************************************************************
844 * Writes contents of the registry key to the specified file stream.
847 * file_name - name of a file to export registry branch to.
848 * reg_key_name - registry branch to export. The whole registry is exported if
849 * reg_key_name is NULL or contains an empty string.
851 BOOL
export_registry_key(CHAR
*file_name
, CHAR
*reg_key_name
)
853 CHAR
*reg_key_name_buf
;
856 DWORD reg_key_name_len
= KEY_MAX_LEN
;
857 DWORD val_name_len
= KEY_MAX_LEN
;
858 DWORD val_size
= REG_VAL_BUF_SIZE
;
861 reg_key_name_buf
= HeapAlloc(GetProcessHeap(), 0,
862 reg_key_name_len
* sizeof(*reg_key_name_buf
));
863 val_name_buf
= HeapAlloc(GetProcessHeap(), 0,
864 val_name_len
* sizeof(*val_name_buf
));
865 val_buf
= HeapAlloc(GetProcessHeap(), 0, val_size
);
866 CHECK_ENOUGH_MEMORY(reg_key_name_buf
&& val_name_buf
&& val_buf
);
868 if (reg_key_name
&& reg_key_name
[0]) {
873 REGPROC_resize_char_buffer(®_key_name_buf
, ®_key_name_len
,
874 strlen(reg_key_name
));
875 strcpy(reg_key_name_buf
, reg_key_name
);
877 /* open the specified key */
878 if (!parseKeyName(reg_key_name
, ®_key_class
, &branch_name
)) {
879 fprintf(stderr
,"%s: Incorrect registry class specification in '%s'\n",
880 getAppName(), reg_key_name
);
883 if (!branch_name
[0]) {
884 /* no branch - registry class is specified */
885 file
= REGPROC_open_export_file(file_name
);
886 export_hkey(file
, reg_key_class
,
887 ®_key_name_buf
, ®_key_name_len
,
888 &val_name_buf
, &val_name_len
,
889 &val_buf
, &val_size
);
890 } else if (RegOpenKey(reg_key_class
, branch_name
, &key
) == ERROR_SUCCESS
) {
891 file
= REGPROC_open_export_file(file_name
);
892 export_hkey(file
, key
,
893 ®_key_name_buf
, ®_key_name_len
,
894 &val_name_buf
, &val_name_len
,
895 &val_buf
, &val_size
);
898 fprintf(stderr
,"%s: Can't export. Registry key '%s' does not exist!\n",
899 getAppName(), reg_key_name
);
900 REGPROC_print_error();
905 /* export all registry classes */
906 file
= REGPROC_open_export_file(file_name
);
907 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++) {
908 /* do not export HKEY_CLASSES_ROOT */
909 if (reg_class_keys
[i
] != HKEY_CLASSES_ROOT
&&
910 reg_class_keys
[i
] != HKEY_CURRENT_USER
&&
911 reg_class_keys
[i
] != HKEY_CURRENT_CONFIG
&&
912 reg_class_keys
[i
] != HKEY_DYN_DATA
) {
913 strcpy(reg_key_name_buf
, reg_class_names
[i
]);
914 export_hkey(file
, reg_class_keys
[i
],
915 ®_key_name_buf
, ®_key_name_len
,
916 &val_name_buf
, &val_name_len
,
917 &val_buf
, &val_size
);
925 HeapFree(GetProcessHeap(), 0, reg_key_name
);
926 HeapFree(GetProcessHeap(), 0, val_buf
);
930 /******************************************************************************
931 * Reads contents of the specified file into the registry.
933 BOOL
import_registry_file(LPTSTR filename
)
935 FILE* reg_file
= fopen(filename
, "r");
938 processRegLines(reg_file
);
944 /******************************************************************************
945 * Recursive function which removes the registry key with all subkeys.
947 static void delete_branch(HKEY key
,
948 CHAR
**reg_key_name_buf
, DWORD
*reg_key_name_len
)
951 DWORD max_sub_key_len
;
957 if (RegOpenKey(key
, *reg_key_name_buf
, &branch_key
) != ERROR_SUCCESS
) {
958 REGPROC_print_error();
961 /* get size information and resize the buffers if necessary */
962 if (RegQueryInfoKey(branch_key
, NULL
, NULL
, NULL
,
963 &subkeys
, &max_sub_key_len
,
964 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
965 ) != ERROR_SUCCESS
) {
966 REGPROC_print_error();
968 curr_len
= strlen(*reg_key_name_buf
);
969 REGPROC_resize_char_buffer(reg_key_name_buf
, reg_key_name_len
,
970 max_sub_key_len
+ curr_len
+ 1);
972 (*reg_key_name_buf
)[curr_len
] = '\\';
973 for (i
= subkeys
- 1; i
>= 0; i
--) {
974 DWORD buf_len
= *reg_key_name_len
- curr_len
;
976 ret
= RegEnumKeyEx(branch_key
, i
, *reg_key_name_buf
+ curr_len
+ 1,
977 &buf_len
, NULL
, NULL
, NULL
, NULL
);
978 if (ret
!= ERROR_SUCCESS
&&
979 ret
!= ERROR_MORE_DATA
&&
980 ret
!= ERROR_NO_MORE_ITEMS
) {
981 REGPROC_print_error();
983 delete_branch(key
, reg_key_name_buf
, reg_key_name_len
);
986 (*reg_key_name_buf
)[curr_len
] = '\0';
987 RegCloseKey(branch_key
);
988 RegDeleteKey(key
, *reg_key_name_buf
);
991 /******************************************************************************
992 * Removes the registry key with all subkeys. Parses full key name.
995 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
996 * empty, points to register key class, does not exist.
998 void delete_registry_key(CHAR
*reg_key_name
)
1004 if (!reg_key_name
|| !reg_key_name
[0])
1007 if (!parseKeyName(reg_key_name
, &key_class
, &key_name
)) {
1008 fprintf(stderr
,"%s: Incorrect registry class specification in '%s'\n",
1009 getAppName(), reg_key_name
);
1013 fprintf(stderr
,"%s: Can't delete registry class '%s'\n",
1014 getAppName(), reg_key_name
);
1018 /* open the specified key to make sure it exists */
1019 if (RegOpenKey(key_class
, key_name
, &branch_key
) == ERROR_SUCCESS
) {
1021 DWORD branch_name_len
;
1022 RegCloseKey(branch_key
);
1024 /* Copy the key name to a new buffer that delete_branch() can
1025 * reallocate as needed
1027 branch_name_len
= strlen(key_name
);
1028 branch_name
= HeapAlloc(GetProcessHeap(), 0, branch_name_len
+1);
1029 CHECK_ENOUGH_MEMORY(branch_name
);
1030 strcpy(branch_name
, key_name
);
1031 delete_branch(key_class
, &branch_name
, &branch_name_len
);
1032 HeapFree(GetProcessHeap(), 0, branch_name
);