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
29 #include <wine/unicode.h>
32 #define REG_VAL_BUF_SIZE 4096
34 /* maximal number of characters in hexadecimal data line,
35 not including '\' character */
36 #define REG_FILE_HEX_LINE_LEN 76
38 static const CHAR
*reg_class_names
[] = {
39 "HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_CLASSES_ROOT",
40 "HKEY_CURRENT_CONFIG", "HKEY_CURRENT_USER", "HKEY_DYN_DATA"
43 #define REG_CLASS_NUMBER (sizeof(reg_class_names) / sizeof(reg_class_names[0]))
45 extern const WCHAR
* reg_class_namesW
[];
47 static HKEY reg_class_keys
[REG_CLASS_NUMBER
] = {
48 HKEY_LOCAL_MACHINE
, HKEY_USERS
, HKEY_CLASSES_ROOT
,
49 HKEY_CURRENT_CONFIG
, HKEY_CURRENT_USER
, HKEY_DYN_DATA
53 #define NOT_ENOUGH_MEMORY 1
56 /* processing macros */
58 /* common check of memory allocation results */
59 #define CHECK_ENOUGH_MEMORY(p) \
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 convers input from multibyte to wide chars
69 * Returned string must be freed by the caller
71 WCHAR
* GetWideString(const char* strA
)
74 int len
= MultiByteToWideChar(CP_ACP
, 0, strA
, -1, NULL
, 0);
76 strW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
77 CHECK_ENOUGH_MEMORY(strW
);
78 MultiByteToWideChar(CP_ACP
, 0, strA
, -1, strW
, len
);
82 /******************************************************************************
83 * Allocates memory and convers input from wide chars to multibyte
84 * Returned string must be freed by the caller
86 char* GetMultiByteString(const WCHAR
* strW
)
89 int len
= WideCharToMultiByte(CP_ACP
, 0, strW
, -1, NULL
, 0, NULL
, NULL
);
91 strA
= HeapAlloc(GetProcessHeap(), 0, len
);
92 CHECK_ENOUGH_MEMORY(strA
);
93 WideCharToMultiByte(CP_ACP
, 0, strW
, -1, strA
, len
, NULL
, NULL
);
97 /******************************************************************************
98 * Converts a hex representation of a DWORD into a DWORD.
100 static BOOL
convertHexToDWord(WCHAR
* str
, DWORD
*dw
)
105 WideCharToMultiByte(CP_ACP
, 0, str
, -1, buf
, 9, NULL
, NULL
);
106 if (lstrlenW(str
) > 8 || sscanf(buf
, "%x%c", dw
, &dummy
) != 1) {
107 fprintf(stderr
,"%s: ERROR, invalid hex value\n", getAppName());
113 /******************************************************************************
114 * Converts a hex comma separated values list into a binary string.
116 static BYTE
* convertHexCSVToHex(WCHAR
*strW
, DWORD
*size
)
120 char* strA
= GetMultiByteString(strW
);
122 /* The worst case is 1 digit + 1 comma per byte */
123 *size
=(strlen(strA
)+1)/2;
124 data
=HeapAlloc(GetProcessHeap(), 0, *size
);
125 CHECK_ENOUGH_MEMORY(data
);
134 wc
= strtoul(s
,&end
,16);
135 if (end
== s
|| wc
> 0xff || (*end
&& *end
!= ',')) {
136 fprintf(stderr
,"%s: ERROR converting CSV hex stream. Invalid value at '%s'\n",
138 HeapFree(GetProcessHeap(), 0, data
);
139 HeapFree(GetProcessHeap(), 0, strA
);
148 HeapFree(GetProcessHeap(), 0, strA
);
153 /******************************************************************************
154 * This function returns the HKEY associated with the data type encoded in the
155 * value. It modifies the input parameter (key value) in order to skip this
156 * "now useless" data type information.
158 * Note: Updated based on the algorithm used in 'server/registry.c'
160 static DWORD
getDataType(LPWSTR
*lpValue
, DWORD
* parse_type
)
162 struct data_type
{ const WCHAR
*tag
; int len
; int type
; int parse_type
; };
164 static const WCHAR quote
[] = {'"'};
165 static const WCHAR str
[] = {'s','t','r',':','"'};
166 static const WCHAR str2
[] = {'s','t','r','(','2',')',':','"'};
167 static const WCHAR hex
[] = {'h','e','x',':'};
168 static const WCHAR dword
[] = {'d','w','o','r','d',':'};
169 static const WCHAR hexp
[] = {'h','e','x','('};
171 static const struct data_type data_types
[] = { /* actual type */ /* type to assume for parsing */
172 { quote
, 1, REG_SZ
, REG_SZ
},
173 { str
, 5, REG_SZ
, REG_SZ
},
174 { str2
, 8, REG_EXPAND_SZ
, REG_SZ
},
175 { hex
, 4, REG_BINARY
, REG_BINARY
},
176 { dword
, 6, REG_DWORD
, REG_DWORD
},
177 { hexp
, 4, -1, REG_BINARY
},
181 const struct data_type
*ptr
;
184 for (ptr
= data_types
; ptr
->tag
; ptr
++) {
185 if (strncmpW( ptr
->tag
, *lpValue
, ptr
->len
))
189 *parse_type
= ptr
->parse_type
;
195 /* "hex(xx):" is special */
196 type
= (int)strtoulW( *lpValue
, &end
, 16 );
197 if (**lpValue
=='\0' || *end
!=')' || *(end
+1)!=':') {
205 *parse_type
=REG_NONE
;
209 /******************************************************************************
210 * Replaces escape sequences with the characters.
212 static void REGPROC_unescape_string(WCHAR
* str
)
214 int str_idx
= 0; /* current character under analysis */
215 int val_idx
= 0; /* the last character of the unescaped string */
216 int len
= lstrlenW(str
);
217 for (str_idx
= 0; str_idx
< len
; str_idx
++, val_idx
++) {
218 if (str
[str_idx
] == '\\') {
220 switch (str
[str_idx
]) {
226 str
[val_idx
] = str
[str_idx
];
229 fprintf(stderr
,"Warning! Unrecognized escape sequence: \\%c'\n",
231 str
[val_idx
] = str
[str_idx
];
235 str
[val_idx
] = str
[str_idx
];
241 /******************************************************************************
242 * Parses HKEY_SOME_ROOT\some\key\path to get the root key handle and
243 * extract the key path (what comes after the first '\').
245 static BOOL
parseKeyName(LPSTR lpKeyName
, HKEY
*hKey
, LPSTR
*lpKeyPath
)
250 if (lpKeyName
== NULL
)
253 lpSlash
= strchr(lpKeyName
, '\\');
256 len
= lpSlash
-lpKeyName
;
260 len
= strlen(lpKeyName
);
261 lpSlash
= lpKeyName
+len
;
264 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++) {
265 if (strncmp(lpKeyName
, reg_class_names
[i
], len
) == 0 &&
266 len
== strlen(reg_class_names
[i
])) {
267 *hKey
= reg_class_keys
[i
];
274 if (*lpSlash
!= '\0')
276 *lpKeyPath
= lpSlash
;
280 static BOOL
parseKeyNameW(LPWSTR lpKeyName
, HKEY
*hKey
, LPWSTR
*lpKeyPath
)
282 WCHAR
* lpSlash
= NULL
;
285 if (lpKeyName
== NULL
)
288 for(i
= 0; *(lpKeyName
+i
) != 0; i
++)
290 if(*(lpKeyName
+i
) == '\\')
292 lpSlash
= lpKeyName
+i
;
299 len
= lpSlash
-lpKeyName
;
303 len
= lstrlenW(lpKeyName
);
304 lpSlash
= lpKeyName
+len
;
308 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++) {
309 if (CompareStringW(LOCALE_USER_DEFAULT
, 0, lpKeyName
, len
, reg_class_namesW
[i
], len
) == CSTR_EQUAL
&&
310 len
== lstrlenW(reg_class_namesW
[i
])) {
311 *hKey
= reg_class_keys
[i
];
320 if (*lpSlash
!= '\0')
322 *lpKeyPath
= lpSlash
;
326 /* Globals used by the setValue() & co */
327 static LPSTR currentKeyName
;
328 static HKEY currentKeyHandle
= NULL
;
330 /******************************************************************************
331 * Sets the value with name val_name to the data in val_data for the currently
335 * val_name - name of the registry value
336 * val_data - registry value data
338 static LONG
setValue(WCHAR
* val_name
, WCHAR
* val_data
)
341 DWORD dwDataType
, dwParseType
;
344 WCHAR del
[] = {'-',0};
346 if ( (val_name
== NULL
) || (val_data
== NULL
) )
347 return ERROR_INVALID_PARAMETER
;
349 if (lstrcmpW(val_data
, del
) == 0)
351 res
=RegDeleteValueW(currentKeyHandle
,val_name
);
352 return (res
== ERROR_FILE_NOT_FOUND
? ERROR_SUCCESS
: res
);
355 /* Get the data type stored into the value field */
356 dwDataType
= getDataType(&val_data
, &dwParseType
);
358 if (dwParseType
== REG_SZ
) /* no conversion for string */
360 REGPROC_unescape_string(val_data
);
361 /* Compute dwLen after REGPROC_unescape_string because it may
362 * have changed the string length and we don't want to store
363 * the extra garbage in the registry.
365 dwLen
= lstrlenW(val_data
);
366 if (dwLen
>0 && val_data
[dwLen
-1]=='"')
369 val_data
[dwLen
]='\0';
371 lpbData
= (BYTE
*) val_data
;
372 dwLen
++; /* include terminating null */
373 dwLen
= dwLen
* sizeof(WCHAR
); /* size is in bytes */
375 else if (dwParseType
== REG_DWORD
) /* Convert the dword types */
377 if (!convertHexToDWord(val_data
, &dwData
))
378 return ERROR_INVALID_DATA
;
379 lpbData
= (BYTE
*)&dwData
;
380 dwLen
= sizeof(dwData
);
382 else if (dwParseType
== REG_BINARY
) /* Convert the binary data */
384 lpbData
= convertHexCSVToHex(val_data
, &dwLen
);
386 return ERROR_INVALID_DATA
;
388 else /* unknown format */
390 fprintf(stderr
,"%s: ERROR, unknown data format\n", getAppName());
391 return ERROR_INVALID_DATA
;
394 res
= RegSetValueExW(
401 if (dwParseType
== REG_BINARY
)
402 HeapFree(GetProcessHeap(), 0, lpbData
);
406 /******************************************************************************
407 * A helper function for processRegEntry() that opens the current key.
408 * That key must be closed by calling closeKey().
410 static LONG
openKeyW(WCHAR
* stdInput
)
418 if (stdInput
== NULL
)
419 return ERROR_INVALID_PARAMETER
;
421 /* Get the registry class */
422 if (!parseKeyNameW(stdInput
, &keyClass
, &keyPath
))
423 return ERROR_INVALID_PARAMETER
;
425 res
= RegCreateKeyExW(
426 keyClass
, /* Class */
427 keyPath
, /* Sub Key */
429 NULL
, /* object type */
430 REG_OPTION_NON_VOLATILE
, /* option, REG_OPTION_NON_VOLATILE ... */
431 KEY_ALL_ACCESS
, /* access mask, KEY_ALL_ACCESS */
432 NULL
, /* security attribute */
433 ¤tKeyHandle
, /* result */
434 &dwDisp
); /* disposition, REG_CREATED_NEW_KEY or
435 REG_OPENED_EXISTING_KEY */
437 if (res
== ERROR_SUCCESS
)
438 currentKeyName
= GetMultiByteString(stdInput
);
440 currentKeyHandle
= NULL
;
446 /******************************************************************************
447 * Close the currently opened key.
449 static void closeKey(void)
451 if (currentKeyHandle
)
453 HeapFree(GetProcessHeap(), 0, currentKeyName
);
454 RegCloseKey(currentKeyHandle
);
455 currentKeyHandle
= NULL
;
459 /******************************************************************************
460 * This function is a wrapper for the setValue function. It prepares the
461 * land and cleans the area once completed.
462 * Note: this function modifies the line parameter.
464 * line - registry file unwrapped line. Should have the registry value name and
465 * complete registry value data.
467 static void processSetValue(WCHAR
* line
)
469 WCHAR
* val_name
; /* registry value name */
470 WCHAR
* val_data
; /* registry value data */
471 int line_idx
= 0; /* current character under analysis */
475 if (line
[line_idx
] == '@' && line
[line_idx
+ 1] == '=') {
476 line
[line_idx
] = '\0';
479 } else if (line
[line_idx
] == '\"') {
481 val_name
= line
+ line_idx
;
483 if (line
[line_idx
] == '\\') /* skip escaped character */
487 if (line
[line_idx
] == '\"') {
488 line
[line_idx
] = '\0';
496 if (line
[line_idx
] != '=') {
498 line
[line_idx
] = '\"';
499 lineA
= GetMultiByteString(line
);
500 fprintf(stderr
,"Warning! unrecognized line:\n%s\n", lineA
);
501 HeapFree(GetProcessHeap(), 0, lineA
);
506 char* lineA
= GetMultiByteString(line
);
507 fprintf(stderr
,"Warning! unrecognized line:\n%s\n", lineA
);
508 HeapFree(GetProcessHeap(), 0, lineA
);
511 line_idx
++; /* skip the '=' character */
512 val_data
= line
+ line_idx
;
514 REGPROC_unescape_string(val_name
);
515 res
= setValue(val_name
, val_data
);
516 if ( res
!= ERROR_SUCCESS
)
518 char* val_nameA
= GetMultiByteString(val_name
);
519 char* val_dataA
= GetMultiByteString(val_data
);
520 fprintf(stderr
,"%s: ERROR Key %s not created. Value: %s, Data: %s\n",
525 HeapFree(GetProcessHeap(), 0, val_nameA
);
526 HeapFree(GetProcessHeap(), 0, val_dataA
);
530 /******************************************************************************
531 * This function receives the currently read entry and performs the
532 * corresponding action.
534 static void processRegEntry(WCHAR
* stdInput
)
537 * We encountered the end of the file, make sure we
538 * close the opened key and exit
540 if (stdInput
== NULL
) {
545 if ( stdInput
[0] == '[') /* We are reading a new key */
548 closeKey(); /* Close the previous key */
550 /* Get rid of the square brackets */
552 keyEnd
= strrchrW(stdInput
, ']');
556 /* delete the key if we encounter '-' at the start of reg key */
557 if ( stdInput
[0] == '-')
559 delete_registry_key(stdInput
+ 1);
560 } else if ( openKeyW(stdInput
) != ERROR_SUCCESS
)
562 fprintf(stderr
,"%s: setValue failed to open key %s\n",
563 getAppName(), stdInput
);
565 } else if( currentKeyHandle
&&
566 (( stdInput
[0] == '@') || /* reading a default @=data pair */
567 ( stdInput
[0] == '\"'))) /* reading a new value=data pair */
569 processSetValue(stdInput
);
572 /* Since we are assuming that the file format is valid we must be
573 * reading a blank line which indicates the end of this key processing
579 /******************************************************************************
580 * Processes a registry file.
581 * Correctly processes comments (in # form), line continuation.
584 * in - input stream to read from
586 void processRegLinesA(FILE *in
)
588 LPSTR line
= NULL
; /* line read from input stream */
589 ULONG lineSize
= REG_VAL_BUF_SIZE
;
591 line
= HeapAlloc(GetProcessHeap(), 0, lineSize
);
592 CHECK_ENOUGH_MEMORY(line
);
595 LPSTR s
; /* The pointer into line for where the current fgets should read */
600 size_t size_remaining
;
602 char *s_eol
; /* various local uses */
604 /* Do we need to expand the buffer ? */
605 assert (s
>= line
&& s
<= line
+ lineSize
);
606 size_remaining
= lineSize
- (s
-line
);
607 if (size_remaining
< 2) /* room for 1 character and the \0 */
610 size_t new_size
= lineSize
+ REG_VAL_BUF_SIZE
;
611 if (new_size
> lineSize
) /* no arithmetic overflow */
612 new_buffer
= HeapReAlloc (GetProcessHeap(), 0, line
, new_size
);
615 CHECK_ENOUGH_MEMORY(new_buffer
);
617 s
= line
+ lineSize
- size_remaining
;
619 size_remaining
= lineSize
- (s
-line
);
622 /* Get as much as possible into the buffer, terminated either by
623 * eof, error, eol or getting the maximum amount. Abort on error.
625 size_to_get
= (size_remaining
> INT_MAX
? INT_MAX
: size_remaining
);
627 check
= fgets (s
, size_to_get
, in
);
631 perror ("While reading input");
636 /* It is not clear to me from the definition that the
637 * contents of the buffer are well defined on detecting
638 * an eof without managing to read anything.
643 /* If we didn't read the eol nor the eof go around for the rest */
644 s_eol
= strchr (s
, '\n');
645 if (!feof (in
) && !s_eol
) {
646 s
= strchr (s
, '\0');
647 /* It should be s + size_to_get - 1 but this is safer */
651 /* If it is a comment line then discard it and go around again */
652 if (line
[0] == '#') {
657 /* Remove any line feed. Leave s_eol on the \0 */
660 if (s_eol
> line
&& *(s_eol
-1) == '\r')
663 s_eol
= strchr (s
, '\0');
665 /* If there is a concatenating \\ then go around again */
666 if (s_eol
> line
&& *(s_eol
-1) == '\\') {
669 /* The following error protection could be made more self-
670 * correcting but I thought it not worth trying.
672 if ((c
= fgetc (in
)) == EOF
|| c
!= ' ' ||
673 (c
= fgetc (in
)) == EOF
|| c
!= ' ')
674 fprintf(stderr
,"%s: ERROR - invalid continuation.\n",
679 lineW
= GetWideString(line
);
681 break; /* That is the full virtual line */
684 processRegEntry(lineW
);
685 HeapFree(GetProcessHeap(), 0, lineW
);
687 processRegEntry(NULL
);
689 HeapFree(GetProcessHeap(), 0, line
);
692 void processRegLinesW(FILE *in
)
694 WCHAR
* buf
= NULL
; /* line read from input stream */
695 ULONG lineSize
= REG_VAL_BUF_SIZE
;
696 size_t CharsInBuf
= -1;
698 WCHAR
* s
; /* The pointer into line for where the current fgets should read */
700 buf
= HeapAlloc(GetProcessHeap(), 0, lineSize
* sizeof(WCHAR
));
701 CHECK_ENOUGH_MEMORY(buf
);
706 size_t size_remaining
;
708 WCHAR
*s_eol
= NULL
; /* various local uses */
710 /* Do we need to expand the buffer ? */
711 assert (s
>= buf
&& s
<= buf
+ lineSize
);
712 size_remaining
= lineSize
- (s
-buf
);
713 if (size_remaining
< 2) /* room for 1 character and the \0 */
716 size_t new_size
= lineSize
+ (REG_VAL_BUF_SIZE
/ sizeof(WCHAR
));
717 if (new_size
> lineSize
) /* no arithmetic overflow */
718 new_buffer
= HeapReAlloc (GetProcessHeap(), 0, buf
, new_size
* sizeof(WCHAR
));
721 CHECK_ENOUGH_MEMORY(new_buffer
);
723 s
= buf
+ lineSize
- size_remaining
;
725 size_remaining
= lineSize
- (s
-buf
);
728 /* Get as much as possible into the buffer, terminated either by
729 * eof, error or getting the maximum amount. Abort on error.
731 size_to_get
= (size_remaining
> INT_MAX
? INT_MAX
: size_remaining
);
733 CharsInBuf
= fread(s
, sizeof(WCHAR
), size_to_get
- 1, in
);
736 if (CharsInBuf
== 0) {
738 perror ("While reading input");
743 /* It is not clear to me from the definition that the
744 * contents of the buffer are well defined on detecting
745 * an eof without managing to read anything.
750 /* If we didn't read the eol nor the eof go around for the rest */
753 s_eol
= strchrW(s
, '\n');
758 /* If it is a comment line then discard it and go around again */
764 /* If there is a concatenating \\ then go around again */
765 if ((*(s_eol
-1) == '\\') ||
766 (*(s_eol
-1) == '\r' && *(s_eol
-2) == '\\')) {
767 WCHAR
* NextLine
= s_eol
;
769 while(*(NextLine
+1) == ' ' || *(NextLine
+1) == '\t')
774 if(*(s_eol
-1) == '\r')
777 MoveMemory(s_eol
- 1, NextLine
, (CharsInBuf
- (NextLine
- buf
) + 1)*sizeof(WCHAR
));
778 CharsInBuf
-= NextLine
- s_eol
+ 1;
783 /* Remove any line feed. Leave s_eol on the \0 */
786 if (s_eol
> buf
&& *(s_eol
-1) == '\r')
796 continue; /* That is the full virtual line */
800 processRegEntry(NULL
);
802 HeapFree(GetProcessHeap(), 0, buf
);
805 /****************************************************************************
806 * REGPROC_print_error
808 * Print the message for GetLastError
811 static void REGPROC_print_error(void)
817 error_code
= GetLastError ();
818 status
= FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
819 NULL
, error_code
, 0, (LPTSTR
) &lpMsgBuf
, 0, NULL
);
821 fprintf(stderr
,"%s: Cannot display message for error %d, status %d\n",
822 getAppName(), error_code
, GetLastError());
826 LocalFree((HLOCAL
)lpMsgBuf
);
830 /******************************************************************************
831 * Checks whether the buffer has enough room for the string or required size.
832 * Resizes the buffer if necessary.
835 * buffer - pointer to a buffer for string
836 * len - current length of the buffer in characters.
837 * required_len - length of the string to place to the buffer in characters.
838 * The length does not include the terminating null character.
840 static void REGPROC_resize_char_buffer(CHAR
**buffer
, DWORD
*len
, DWORD required_len
)
843 if (required_len
> *len
) {
846 *buffer
= HeapAlloc(GetProcessHeap(), 0, *len
* sizeof(**buffer
));
848 *buffer
= HeapReAlloc(GetProcessHeap(), 0, *buffer
, *len
* sizeof(**buffer
));
849 CHECK_ENOUGH_MEMORY(*buffer
);
853 /******************************************************************************
854 * Prints string str to file
856 static void REGPROC_export_string(FILE *file
, CHAR
*str
)
858 size_t len
= strlen(str
);
861 /* escaping characters */
862 for (i
= 0; i
< len
; i
++) {
881 /******************************************************************************
882 * Writes contents of the registry key to the specified file stream.
885 * file - writable file stream to export registry branch to.
886 * key - registry branch to export.
887 * reg_key_name_buf - name of the key with registry class.
888 * Is resized if necessary.
889 * reg_key_name_len - length of the buffer for the registry class in characters.
890 * val_name_buf - buffer for storing value name.
891 * Is resized if necessary.
892 * val_name_len - length of the buffer for storing value names in characters.
893 * val_buf - buffer for storing values while extracting.
894 * Is resized if necessary.
895 * val_size - size of the buffer for storing values in bytes.
897 static void export_hkey(FILE *file
, HKEY key
,
898 CHAR
**reg_key_name_buf
, DWORD
*reg_key_name_len
,
899 CHAR
**val_name_buf
, DWORD
*val_name_len
,
900 BYTE
**val_buf
, DWORD
*val_size
)
902 DWORD max_sub_key_len
;
903 DWORD max_val_name_len
;
910 /* get size information and resize the buffers if necessary */
911 if (RegQueryInfoKey(key
, NULL
, NULL
, NULL
, NULL
,
912 &max_sub_key_len
, NULL
,
913 NULL
, &max_val_name_len
, &max_val_size
, NULL
, NULL
914 ) != ERROR_SUCCESS
) {
915 REGPROC_print_error();
917 curr_len
= strlen(*reg_key_name_buf
);
918 REGPROC_resize_char_buffer(reg_key_name_buf
, reg_key_name_len
,
919 max_sub_key_len
+ curr_len
+ 1);
920 REGPROC_resize_char_buffer(val_name_buf
, val_name_len
,
922 if (max_val_size
> *val_size
) {
923 *val_size
= max_val_size
;
924 if (!*val_buf
) *val_buf
= HeapAlloc(GetProcessHeap(), 0, *val_size
);
925 else *val_buf
= HeapReAlloc(GetProcessHeap(), 0, *val_buf
, *val_size
);
926 CHECK_ENOUGH_MEMORY(val_buf
);
929 /* output data for the current key */
931 fputs(*reg_key_name_buf
, file
);
933 /* print all the values */
938 DWORD val_name_len1
= *val_name_len
;
939 DWORD val_size1
= *val_size
;
940 ret
= RegEnumValue(key
, i
, *val_name_buf
, &val_name_len1
, NULL
,
941 &value_type
, *val_buf
, &val_size1
);
942 if (ret
!= ERROR_SUCCESS
) {
944 if (ret
!= ERROR_NO_MORE_ITEMS
) {
945 REGPROC_print_error();
950 if ((*val_name_buf
)[0]) {
952 REGPROC_export_string(file
, *val_name_buf
);
958 switch (value_type
) {
962 if (val_size1
) REGPROC_export_string(file
, (char*) *val_buf
);
967 fprintf(file
, "dword:%08x\n", *((DWORD
*)*val_buf
));
971 fprintf(stderr
,"%s: warning - unsupported registry format '%d', "
973 getAppName(), value_type
);
974 fprintf(stderr
,"key name: \"%s\"\n", *reg_key_name_buf
);
975 fprintf(stderr
,"value name:\"%s\"\n\n", *val_name_buf
);
981 const CHAR
*hex_prefix
;
985 if (value_type
== REG_BINARY
) {
989 sprintf(buf
, "hex(%d):", value_type
);
992 /* position of where the next character will be printed */
993 /* NOTE: yes, strlen("hex:") is used even for hex(x): */
994 cur_pos
= strlen("\"\"=") + strlen("hex:") +
995 strlen(*val_name_buf
);
997 fputs(hex_prefix
, file
);
998 for (i1
= 0; i1
< val_size1
; i1
++) {
999 fprintf(file
, "%02x", (unsigned int)(*val_buf
)[i1
]);
1000 if (i1
+ 1 < val_size1
) {
1006 if (cur_pos
> REG_FILE_HEX_LINE_LEN
) {
1007 fputs("\\\n ", file
);
1020 (*reg_key_name_buf
)[curr_len
] = '\\';
1022 DWORD buf_len
= *reg_key_name_len
- curr_len
;
1024 ret
= RegEnumKeyEx(key
, i
, *reg_key_name_buf
+ curr_len
+ 1, &buf_len
,
1025 NULL
, NULL
, NULL
, NULL
);
1026 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_MORE_DATA
) {
1028 if (ret
!= ERROR_NO_MORE_ITEMS
) {
1029 REGPROC_print_error();
1035 if (RegOpenKey(key
, *reg_key_name_buf
+ curr_len
+ 1,
1036 &subkey
) == ERROR_SUCCESS
) {
1037 export_hkey(file
, subkey
, reg_key_name_buf
, reg_key_name_len
,
1038 val_name_buf
, val_name_len
, val_buf
, val_size
);
1039 RegCloseKey(subkey
);
1041 REGPROC_print_error();
1045 (*reg_key_name_buf
)[curr_len
] = '\0';
1048 /******************************************************************************
1049 * Open file for export.
1051 static FILE *REGPROC_open_export_file(CHAR
*file_name
)
1055 if (strcmp(file_name
,"-")==0)
1059 file
= fopen(file_name
, "w");
1062 fprintf(stderr
,"%s: Can't open file \"%s\"\n", getAppName(), file_name
);
1066 fputs("REGEDIT4\n", file
);
1070 /******************************************************************************
1071 * Writes contents of the registry key to the specified file stream.
1074 * file_name - name of a file to export registry branch to.
1075 * reg_key_name - registry branch to export. The whole registry is exported if
1076 * reg_key_name is NULL or contains an empty string.
1078 BOOL
export_registry_key(CHAR
*file_name
, CHAR
*reg_key_name
)
1080 CHAR
*reg_key_name_buf
;
1083 DWORD reg_key_name_len
= KEY_MAX_LEN
;
1084 DWORD val_name_len
= KEY_MAX_LEN
;
1085 DWORD val_size
= REG_VAL_BUF_SIZE
;
1088 reg_key_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1089 reg_key_name_len
* sizeof(*reg_key_name_buf
));
1090 val_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1091 val_name_len
* sizeof(*val_name_buf
));
1092 val_buf
= HeapAlloc(GetProcessHeap(), 0, val_size
);
1093 CHECK_ENOUGH_MEMORY(reg_key_name_buf
&& val_name_buf
&& val_buf
);
1095 if (reg_key_name
&& reg_key_name
[0]) {
1097 CHAR
*branch_name
= NULL
;
1100 REGPROC_resize_char_buffer(®_key_name_buf
, ®_key_name_len
,
1101 strlen(reg_key_name
));
1102 strcpy(reg_key_name_buf
, reg_key_name
);
1104 /* open the specified key */
1105 if (!parseKeyName(reg_key_name
, ®_key_class
, &branch_name
)) {
1106 fprintf(stderr
,"%s: Incorrect registry class specification in '%s'\n",
1107 getAppName(), reg_key_name
);
1110 if (!branch_name
[0]) {
1111 /* no branch - registry class is specified */
1112 file
= REGPROC_open_export_file(file_name
);
1113 export_hkey(file
, reg_key_class
,
1114 ®_key_name_buf
, ®_key_name_len
,
1115 &val_name_buf
, &val_name_len
,
1116 &val_buf
, &val_size
);
1117 } else if (RegOpenKey(reg_key_class
, branch_name
, &key
) == ERROR_SUCCESS
) {
1118 file
= REGPROC_open_export_file(file_name
);
1119 export_hkey(file
, key
,
1120 ®_key_name_buf
, ®_key_name_len
,
1121 &val_name_buf
, &val_name_len
,
1122 &val_buf
, &val_size
);
1125 fprintf(stderr
,"%s: Can't export. Registry key '%s' does not exist!\n",
1126 getAppName(), reg_key_name
);
1127 REGPROC_print_error();
1132 /* export all registry classes */
1133 file
= REGPROC_open_export_file(file_name
);
1134 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++) {
1135 /* do not export HKEY_CLASSES_ROOT */
1136 if (reg_class_keys
[i
] != HKEY_CLASSES_ROOT
&&
1137 reg_class_keys
[i
] != HKEY_CURRENT_USER
&&
1138 reg_class_keys
[i
] != HKEY_CURRENT_CONFIG
&&
1139 reg_class_keys
[i
] != HKEY_DYN_DATA
) {
1140 strcpy(reg_key_name_buf
, reg_class_names
[i
]);
1141 export_hkey(file
, reg_class_keys
[i
],
1142 ®_key_name_buf
, ®_key_name_len
,
1143 &val_name_buf
, &val_name_len
,
1144 &val_buf
, &val_size
);
1152 HeapFree(GetProcessHeap(), 0, reg_key_name
);
1153 HeapFree(GetProcessHeap(), 0, val_buf
);
1157 /******************************************************************************
1158 * Reads contents of the specified file into the registry.
1160 BOOL
import_registry_file(FILE* reg_file
)
1165 if (fread( s
, 2, 1, reg_file
) == 1)
1167 if (s
[0] == 0xff && s
[1] == 0xfe)
1169 processRegLinesW(reg_file
);
1173 processRegLinesA(reg_file
);
1181 /******************************************************************************
1182 * Removes the registry key with all subkeys. Parses full key name.
1185 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1186 * empty, points to register key class, does not exist.
1188 void delete_registry_key(WCHAR
*reg_key_name
)
1190 WCHAR
*key_name
= NULL
;
1193 if (!reg_key_name
|| !reg_key_name
[0])
1196 if (!parseKeyNameW(reg_key_name
, &key_class
, &key_name
)) {
1197 char* reg_key_nameA
= GetMultiByteString(reg_key_name
);
1198 fprintf(stderr
,"%s: Incorrect registry class specification in '%s'\n",
1199 getAppName(), reg_key_nameA
);
1200 HeapFree(GetProcessHeap(), 0, reg_key_nameA
);
1204 char* reg_key_nameA
= GetMultiByteString(reg_key_name
);
1205 fprintf(stderr
,"%s: Can't delete registry class '%s'\n",
1206 getAppName(), reg_key_nameA
);
1207 HeapFree(GetProcessHeap(), 0, reg_key_nameA
);
1211 RegDeleteTreeW(key_class
, key_name
);