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 static const WCHAR hkey_local_machine
[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0};
44 static const WCHAR hkey_users
[] = {'H','K','E','Y','_','U','S','E','R','S',0};
45 static const WCHAR hkey_classes_root
[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0};
46 static const WCHAR hkey_current_config
[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0};
47 static const WCHAR hkey_current_user
[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0};
48 static const WCHAR hkey_dyn_data
[] = {'H','K','E','Y','_','D','Y','N','_','D','A','T','A',0};
50 static const WCHAR
*reg_class_namesW
[] = {hkey_local_machine
, hkey_users
,
51 hkey_classes_root
, hkey_current_config
,
52 hkey_current_user
, hkey_dyn_data
56 #define REG_CLASS_NUMBER (sizeof(reg_class_names) / sizeof(reg_class_names[0]))
58 static HKEY reg_class_keys
[REG_CLASS_NUMBER
] = {
59 HKEY_LOCAL_MACHINE
, HKEY_USERS
, HKEY_CLASSES_ROOT
,
60 HKEY_CURRENT_CONFIG
, HKEY_CURRENT_USER
, HKEY_DYN_DATA
64 #define NOT_ENOUGH_MEMORY 1
67 /* processing macros */
69 /* common check of memory allocation results */
70 #define CHECK_ENOUGH_MEMORY(p) \
73 fprintf(stderr,"%s: file %s, line %d: Not enough memory\n", \
74 getAppName(), __FILE__, __LINE__); \
75 exit(NOT_ENOUGH_MEMORY); \
78 /******************************************************************************
79 * Allocates memory and convers input from multibyte to wide chars
80 * Returned string must be freed by the caller
82 WCHAR
* GetWideString(const char* strA
)
85 int len
= MultiByteToWideChar(CP_ACP
, 0, strA
, -1, NULL
, 0);
87 strW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
88 CHECK_ENOUGH_MEMORY(strW
);
89 MultiByteToWideChar(CP_ACP
, 0, strA
, -1, strW
, len
);
93 /******************************************************************************
94 * Allocates memory and convers input from wide chars to multibyte
95 * Returned string must be freed by the caller
97 char* GetMultiByteString(const WCHAR
* strW
)
100 int len
= WideCharToMultiByte(CP_ACP
, 0, strW
, -1, NULL
, 0, NULL
, NULL
);
102 strA
= HeapAlloc(GetProcessHeap(), 0, len
);
103 CHECK_ENOUGH_MEMORY(strA
);
104 WideCharToMultiByte(CP_ACP
, 0, strW
, -1, strA
, len
, NULL
, NULL
);
108 /******************************************************************************
109 * Converts a hex representation of a DWORD into a DWORD.
111 static BOOL
convertHexToDWord(WCHAR
* str
, DWORD
*dw
)
116 WideCharToMultiByte(CP_ACP
, 0, str
, -1, buf
, 9, NULL
, NULL
);
117 if (lstrlenW(str
) > 8 || sscanf(buf
, "%x%c", dw
, &dummy
) != 1) {
118 fprintf(stderr
,"%s: ERROR, invalid hex value\n", getAppName());
124 /******************************************************************************
125 * Converts a hex comma separated values list into a binary string.
127 static BYTE
* convertHexCSVToHex(WCHAR
*strW
, DWORD
*size
)
131 char* strA
= GetMultiByteString(strW
);
133 /* The worst case is 1 digit + 1 comma per byte */
134 *size
=(strlen(strA
)+1)/2;
135 data
=HeapAlloc(GetProcessHeap(), 0, *size
);
136 CHECK_ENOUGH_MEMORY(data
);
145 wc
= strtoul(s
,&end
,16);
146 if (end
== s
|| wc
> 0xff || (*end
&& *end
!= ',')) {
147 fprintf(stderr
,"%s: ERROR converting CSV hex stream. Invalid value at '%s'\n",
149 HeapFree(GetProcessHeap(), 0, data
);
150 HeapFree(GetProcessHeap(), 0, strA
);
159 HeapFree(GetProcessHeap(), 0, strA
);
164 /******************************************************************************
165 * This function returns the HKEY associated with the data type encoded in the
166 * value. It modifies the input parameter (key value) in order to skip this
167 * "now useless" data type information.
169 * Note: Updated based on the algorithm used in 'server/registry.c'
171 static DWORD
getDataType(LPWSTR
*lpValue
, DWORD
* parse_type
)
173 struct data_type
{ const WCHAR
*tag
; int len
; int type
; int parse_type
; };
175 static const WCHAR quote
[] = {'"'};
176 static const WCHAR str
[] = {'s','t','r',':','"'};
177 static const WCHAR str2
[] = {'s','t','r','(','2',')',':','"'};
178 static const WCHAR hex
[] = {'h','e','x',':'};
179 static const WCHAR dword
[] = {'d','w','o','r','d',':'};
180 static const WCHAR hexp
[] = {'h','e','x','('};
182 static const struct data_type data_types
[] = { /* actual type */ /* type to assume for parsing */
183 { quote
, 1, REG_SZ
, REG_SZ
},
184 { str
, 5, REG_SZ
, REG_SZ
},
185 { str2
, 8, REG_EXPAND_SZ
, REG_SZ
},
186 { hex
, 4, REG_BINARY
, REG_BINARY
},
187 { dword
, 6, REG_DWORD
, REG_DWORD
},
188 { hexp
, 4, -1, REG_BINARY
},
192 const struct data_type
*ptr
;
195 for (ptr
= data_types
; ptr
->tag
; ptr
++) {
196 if (strncmpW( ptr
->tag
, *lpValue
, ptr
->len
))
200 *parse_type
= ptr
->parse_type
;
206 /* "hex(xx):" is special */
207 type
= (int)strtoulW( *lpValue
, &end
, 16 );
208 if (**lpValue
=='\0' || *end
!=')' || *(end
+1)!=':') {
216 *parse_type
=REG_NONE
;
220 /******************************************************************************
221 * Replaces escape sequences with the characters.
223 static void REGPROC_unescape_string(WCHAR
* str
)
225 int str_idx
= 0; /* current character under analysis */
226 int val_idx
= 0; /* the last character of the unescaped string */
227 int len
= lstrlenW(str
);
228 for (str_idx
= 0; str_idx
< len
; str_idx
++, val_idx
++) {
229 if (str
[str_idx
] == '\\') {
231 switch (str
[str_idx
]) {
237 str
[val_idx
] = str
[str_idx
];
240 fprintf(stderr
,"Warning! Unrecognized escape sequence: \\%c'\n",
242 str
[val_idx
] = str
[str_idx
];
246 str
[val_idx
] = str
[str_idx
];
252 /******************************************************************************
253 * Parses HKEY_SOME_ROOT\some\key\path to get the root key handle and
254 * extract the key path (what comes after the first '\').
256 static BOOL
parseKeyName(LPSTR lpKeyName
, HKEY
*hKey
, LPSTR
*lpKeyPath
)
261 if (lpKeyName
== NULL
)
264 lpSlash
= strchr(lpKeyName
, '\\');
267 len
= lpSlash
-lpKeyName
;
271 len
= strlen(lpKeyName
);
272 lpSlash
= lpKeyName
+len
;
275 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++) {
276 if (strncmp(lpKeyName
, reg_class_names
[i
], len
) == 0 &&
277 len
== strlen(reg_class_names
[i
])) {
278 *hKey
= reg_class_keys
[i
];
285 if (*lpSlash
!= '\0')
287 *lpKeyPath
= lpSlash
;
291 static BOOL
parseKeyNameW(LPWSTR lpKeyName
, HKEY
*hKey
, LPWSTR
*lpKeyPath
)
293 WCHAR
* lpSlash
= NULL
;
296 if (lpKeyName
== NULL
)
299 for(i
= 0; *(lpKeyName
+i
) != 0; i
++)
301 if(*(lpKeyName
+i
) == '\\')
303 lpSlash
= lpKeyName
+i
;
310 len
= lpSlash
-lpKeyName
;
314 len
= lstrlenW(lpKeyName
);
315 lpSlash
= lpKeyName
+len
;
319 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++) {
320 if (CompareStringW(LOCALE_USER_DEFAULT
, 0, lpKeyName
, len
, reg_class_namesW
[i
], len
) == CSTR_EQUAL
&&
321 len
== lstrlenW(reg_class_namesW
[i
])) {
322 *hKey
= reg_class_keys
[i
];
331 if (*lpSlash
!= '\0')
333 *lpKeyPath
= lpSlash
;
337 /* Globals used by the setValue() & co */
338 static LPSTR currentKeyName
;
339 static HKEY currentKeyHandle
= NULL
;
341 /******************************************************************************
342 * Sets the value with name val_name to the data in val_data for the currently
346 * val_name - name of the registry value
347 * val_data - registry value data
349 static LONG
setValue(WCHAR
* val_name
, WCHAR
* val_data
)
352 DWORD dwDataType
, dwParseType
;
355 WCHAR del
[] = {'-',0};
357 if ( (val_name
== NULL
) || (val_data
== NULL
) )
358 return ERROR_INVALID_PARAMETER
;
360 if (lstrcmpW(val_data
, del
) == 0)
362 res
=RegDeleteValueW(currentKeyHandle
,val_name
);
363 return (res
== ERROR_FILE_NOT_FOUND
? ERROR_SUCCESS
: res
);
366 /* Get the data type stored into the value field */
367 dwDataType
= getDataType(&val_data
, &dwParseType
);
369 if (dwParseType
== REG_SZ
) /* no conversion for string */
371 REGPROC_unescape_string(val_data
);
372 /* Compute dwLen after REGPROC_unescape_string because it may
373 * have changed the string length and we don't want to store
374 * the extra garbage in the registry.
376 dwLen
= lstrlenW(val_data
);
377 if (dwLen
>0 && val_data
[dwLen
-1]=='"')
380 val_data
[dwLen
]='\0';
382 lpbData
= (BYTE
*) val_data
;
383 dwLen
++; /* include terminating null */
384 dwLen
= dwLen
* sizeof(WCHAR
); /* size is in bytes */
386 else if (dwParseType
== REG_DWORD
) /* Convert the dword types */
388 if (!convertHexToDWord(val_data
, &dwData
))
389 return ERROR_INVALID_DATA
;
390 lpbData
= (BYTE
*)&dwData
;
391 dwLen
= sizeof(dwData
);
393 else if (dwParseType
== REG_BINARY
) /* Convert the binary data */
395 lpbData
= convertHexCSVToHex(val_data
, &dwLen
);
397 return ERROR_INVALID_DATA
;
399 else /* unknown format */
401 fprintf(stderr
,"%s: ERROR, unknown data format\n", getAppName());
402 return ERROR_INVALID_DATA
;
405 res
= RegSetValueExW(
412 if (dwParseType
== REG_BINARY
)
413 HeapFree(GetProcessHeap(), 0, lpbData
);
417 /******************************************************************************
418 * A helper function for processRegEntry() that opens the current key.
419 * That key must be closed by calling closeKey().
421 static LONG
openKeyW(WCHAR
* stdInput
)
429 if (stdInput
== NULL
)
430 return ERROR_INVALID_PARAMETER
;
432 /* Get the registry class */
433 if (!parseKeyNameW(stdInput
, &keyClass
, &keyPath
))
434 return ERROR_INVALID_PARAMETER
;
436 res
= RegCreateKeyExW(
437 keyClass
, /* Class */
438 keyPath
, /* Sub Key */
440 NULL
, /* object type */
441 REG_OPTION_NON_VOLATILE
, /* option, REG_OPTION_NON_VOLATILE ... */
442 KEY_ALL_ACCESS
, /* access mask, KEY_ALL_ACCESS */
443 NULL
, /* security attribute */
444 ¤tKeyHandle
, /* result */
445 &dwDisp
); /* disposition, REG_CREATED_NEW_KEY or
446 REG_OPENED_EXISTING_KEY */
448 if (res
== ERROR_SUCCESS
)
449 currentKeyName
= GetMultiByteString(stdInput
);
451 currentKeyHandle
= NULL
;
457 /******************************************************************************
458 * Close the currently opened key.
460 static void closeKey(void)
462 if (currentKeyHandle
)
464 HeapFree(GetProcessHeap(), 0, currentKeyName
);
465 RegCloseKey(currentKeyHandle
);
466 currentKeyHandle
= NULL
;
470 /******************************************************************************
471 * This function is a wrapper for the setValue function. It prepares the
472 * land and cleans the area once completed.
473 * Note: this function modifies the line parameter.
475 * line - registry file unwrapped line. Should have the registry value name and
476 * complete registry value data.
478 static void processSetValue(WCHAR
* line
)
480 WCHAR
* val_name
; /* registry value name */
481 WCHAR
* val_data
; /* registry value data */
482 int line_idx
= 0; /* current character under analysis */
486 if (line
[line_idx
] == '@' && line
[line_idx
+ 1] == '=') {
487 line
[line_idx
] = '\0';
490 } else if (line
[line_idx
] == '\"') {
492 val_name
= line
+ line_idx
;
494 if (line
[line_idx
] == '\\') /* skip escaped character */
498 if (line
[line_idx
] == '\"') {
499 line
[line_idx
] = '\0';
507 if (line
[line_idx
] != '=') {
509 line
[line_idx
] = '\"';
510 lineA
= GetMultiByteString(line
);
511 fprintf(stderr
,"Warning! unrecognized line:\n%s\n", lineA
);
512 HeapFree(GetProcessHeap(), 0, lineA
);
517 char* lineA
= GetMultiByteString(line
);
518 fprintf(stderr
,"Warning! unrecognized line:\n%s\n", lineA
);
519 HeapFree(GetProcessHeap(), 0, lineA
);
522 line_idx
++; /* skip the '=' character */
523 val_data
= line
+ line_idx
;
525 REGPROC_unescape_string(val_name
);
526 res
= setValue(val_name
, val_data
);
527 if ( res
!= ERROR_SUCCESS
)
529 char* val_nameA
= GetMultiByteString(val_name
);
530 char* val_dataA
= GetMultiByteString(val_data
);
531 fprintf(stderr
,"%s: ERROR Key %s not created. Value: %s, Data: %s\n",
536 HeapFree(GetProcessHeap(), 0, val_nameA
);
537 HeapFree(GetProcessHeap(), 0, val_dataA
);
541 /******************************************************************************
542 * This function receives the currently read entry and performs the
543 * corresponding action.
545 static void processRegEntry(WCHAR
* stdInput
)
548 * We encountered the end of the file, make sure we
549 * close the opened key and exit
551 if (stdInput
== NULL
) {
556 if ( stdInput
[0] == '[') /* We are reading a new key */
559 closeKey(); /* Close the previous key */
561 /* Get rid of the square brackets */
563 keyEnd
= strrchrW(stdInput
, ']');
567 /* delete the key if we encounter '-' at the start of reg key */
568 if ( stdInput
[0] == '-')
570 delete_registry_key(stdInput
+ 1);
571 } else if ( openKeyW(stdInput
) != ERROR_SUCCESS
)
573 fprintf(stderr
,"%s: setValue failed to open key %s\n",
574 getAppName(), stdInput
);
576 } else if( currentKeyHandle
&&
577 (( stdInput
[0] == '@') || /* reading a default @=data pair */
578 ( stdInput
[0] == '\"'))) /* reading a new value=data pair */
580 processSetValue(stdInput
);
583 /* Since we are assuming that the file format is valid we must be
584 * reading a blank line which indicates the end of this key processing
590 /******************************************************************************
591 * Processes a registry file.
592 * Correctly processes comments (in # form), line continuation.
595 * in - input stream to read from
597 void processRegLinesA(FILE *in
)
599 LPSTR line
= NULL
; /* line read from input stream */
600 ULONG lineSize
= REG_VAL_BUF_SIZE
;
602 line
= HeapAlloc(GetProcessHeap(), 0, lineSize
);
603 CHECK_ENOUGH_MEMORY(line
);
606 LPSTR s
; /* The pointer into line for where the current fgets should read */
611 size_t size_remaining
;
613 char *s_eol
; /* various local uses */
615 /* Do we need to expand the buffer ? */
616 assert (s
>= line
&& s
<= line
+ lineSize
);
617 size_remaining
= lineSize
- (s
-line
);
618 if (size_remaining
< 2) /* room for 1 character and the \0 */
621 size_t new_size
= lineSize
+ REG_VAL_BUF_SIZE
;
622 if (new_size
> lineSize
) /* no arithmetic overflow */
623 new_buffer
= HeapReAlloc (GetProcessHeap(), 0, line
, new_size
);
626 CHECK_ENOUGH_MEMORY(new_buffer
);
628 s
= line
+ lineSize
- size_remaining
;
630 size_remaining
= lineSize
- (s
-line
);
633 /* Get as much as possible into the buffer, terminated either by
634 * eof, error, eol or getting the maximum amount. Abort on error.
636 size_to_get
= (size_remaining
> INT_MAX
? INT_MAX
: size_remaining
);
638 check
= fgets (s
, size_to_get
, in
);
642 perror ("While reading input");
647 /* It is not clear to me from the definition that the
648 * contents of the buffer are well defined on detecting
649 * an eof without managing to read anything.
654 /* If we didn't read the eol nor the eof go around for the rest */
655 s_eol
= strchr (s
, '\n');
656 if (!feof (in
) && !s_eol
) {
657 s
= strchr (s
, '\0');
658 /* It should be s + size_to_get - 1 but this is safer */
662 /* If it is a comment line then discard it and go around again */
663 if (line
[0] == '#') {
668 /* Remove any line feed. Leave s_eol on the \0 */
671 if (s_eol
> line
&& *(s_eol
-1) == '\r')
674 s_eol
= strchr (s
, '\0');
676 /* If there is a concatenating \\ then go around again */
677 if (s_eol
> line
&& *(s_eol
-1) == '\\') {
680 /* The following error protection could be made more self-
681 * correcting but I thought it not worth trying.
683 if ((c
= fgetc (in
)) == EOF
|| c
!= ' ' ||
684 (c
= fgetc (in
)) == EOF
|| c
!= ' ')
685 fprintf(stderr
,"%s: ERROR - invalid continuation.\n",
690 lineW
= GetWideString(line
);
692 break; /* That is the full virtual line */
695 processRegEntry(lineW
);
696 HeapFree(GetProcessHeap(), 0, lineW
);
698 processRegEntry(NULL
);
700 HeapFree(GetProcessHeap(), 0, line
);
703 void processRegLinesW(FILE *in
)
705 WCHAR
* buf
= NULL
; /* line read from input stream */
706 ULONG lineSize
= REG_VAL_BUF_SIZE
;
707 size_t CharsInBuf
= -1;
709 WCHAR
* s
; /* The pointer into line for where the current fgets should read */
711 buf
= HeapAlloc(GetProcessHeap(), 0, lineSize
* sizeof(WCHAR
));
712 CHECK_ENOUGH_MEMORY(buf
);
717 size_t size_remaining
;
719 WCHAR
*s_eol
= NULL
; /* various local uses */
721 /* Do we need to expand the buffer ? */
722 assert (s
>= buf
&& s
<= buf
+ lineSize
);
723 size_remaining
= lineSize
- (s
-buf
);
724 if (size_remaining
< 2) /* room for 1 character and the \0 */
727 size_t new_size
= lineSize
+ (REG_VAL_BUF_SIZE
/ sizeof(WCHAR
));
728 if (new_size
> lineSize
) /* no arithmetic overflow */
729 new_buffer
= HeapReAlloc (GetProcessHeap(), 0, buf
, new_size
* sizeof(WCHAR
));
732 CHECK_ENOUGH_MEMORY(new_buffer
);
734 s
= buf
+ lineSize
- size_remaining
;
736 size_remaining
= lineSize
- (s
-buf
);
739 /* Get as much as possible into the buffer, terminated either by
740 * eof, error or getting the maximum amount. Abort on error.
742 size_to_get
= (size_remaining
> INT_MAX
? INT_MAX
: size_remaining
);
744 CharsInBuf
= fread(s
, sizeof(WCHAR
), size_to_get
- 1, in
);
747 if (CharsInBuf
== 0) {
749 perror ("While reading input");
754 /* It is not clear to me from the definition that the
755 * contents of the buffer are well defined on detecting
756 * an eof without managing to read anything.
761 /* If we didn't read the eol nor the eof go around for the rest */
764 s_eol
= strchrW(s
, '\n');
769 /* If it is a comment line then discard it and go around again */
775 /* If there is a concatenating \\ then go around again */
776 if ((*(s_eol
-1) == '\\') ||
777 (*(s_eol
-1) == '\r' && *(s_eol
-2) == '\\')) {
778 WCHAR
* NextLine
= s_eol
;
780 while(*(NextLine
+1) == ' ' || *(NextLine
+1) == '\t')
785 if(*(s_eol
-1) == '\r')
788 MoveMemory(s_eol
- 1, NextLine
, (CharsInBuf
- (NextLine
- buf
) + 1)*sizeof(WCHAR
));
789 CharsInBuf
-= NextLine
- s_eol
+ 1;
794 /* Remove any line feed. Leave s_eol on the \0 */
797 if (s_eol
> buf
&& *(s_eol
-1) == '\r')
807 continue; /* That is the full virtual line */
811 processRegEntry(NULL
);
813 HeapFree(GetProcessHeap(), 0, buf
);
816 /****************************************************************************
817 * REGPROC_print_error
819 * Print the message for GetLastError
822 static void REGPROC_print_error(void)
828 error_code
= GetLastError ();
829 status
= FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
830 NULL
, error_code
, 0, (LPTSTR
) &lpMsgBuf
, 0, NULL
);
832 fprintf(stderr
,"%s: Cannot display message for error %d, status %d\n",
833 getAppName(), error_code
, GetLastError());
837 LocalFree((HLOCAL
)lpMsgBuf
);
841 /******************************************************************************
842 * Checks whether the buffer has enough room for the string or required size.
843 * Resizes the buffer if necessary.
846 * buffer - pointer to a buffer for string
847 * len - current length of the buffer in characters.
848 * required_len - length of the string to place to the buffer in characters.
849 * The length does not include the terminating null character.
851 static void REGPROC_resize_char_buffer(CHAR
**buffer
, DWORD
*len
, DWORD required_len
)
854 if (required_len
> *len
) {
857 *buffer
= HeapAlloc(GetProcessHeap(), 0, *len
* sizeof(**buffer
));
859 *buffer
= HeapReAlloc(GetProcessHeap(), 0, *buffer
, *len
* sizeof(**buffer
));
860 CHECK_ENOUGH_MEMORY(*buffer
);
864 /******************************************************************************
865 * Prints string str to file
867 static void REGPROC_export_string(FILE *file
, CHAR
*str
)
869 size_t len
= strlen(str
);
872 /* escaping characters */
873 for (i
= 0; i
< len
; i
++) {
892 /******************************************************************************
893 * Writes contents of the registry key to the specified file stream.
896 * file - writable file stream to export registry branch to.
897 * key - registry branch to export.
898 * reg_key_name_buf - name of the key with registry class.
899 * Is resized if necessary.
900 * reg_key_name_len - length of the buffer for the registry class in characters.
901 * val_name_buf - buffer for storing value name.
902 * Is resized if necessary.
903 * val_name_len - length of the buffer for storing value names in characters.
904 * val_buf - buffer for storing values while extracting.
905 * Is resized if necessary.
906 * val_size - size of the buffer for storing values in bytes.
908 static void export_hkey(FILE *file
, HKEY key
,
909 CHAR
**reg_key_name_buf
, DWORD
*reg_key_name_len
,
910 CHAR
**val_name_buf
, DWORD
*val_name_len
,
911 BYTE
**val_buf
, DWORD
*val_size
)
913 DWORD max_sub_key_len
;
914 DWORD max_val_name_len
;
921 /* get size information and resize the buffers if necessary */
922 if (RegQueryInfoKey(key
, NULL
, NULL
, NULL
, NULL
,
923 &max_sub_key_len
, NULL
,
924 NULL
, &max_val_name_len
, &max_val_size
, NULL
, NULL
925 ) != ERROR_SUCCESS
) {
926 REGPROC_print_error();
928 curr_len
= strlen(*reg_key_name_buf
);
929 REGPROC_resize_char_buffer(reg_key_name_buf
, reg_key_name_len
,
930 max_sub_key_len
+ curr_len
+ 1);
931 REGPROC_resize_char_buffer(val_name_buf
, val_name_len
,
933 if (max_val_size
> *val_size
) {
934 *val_size
= max_val_size
;
935 if (!*val_buf
) *val_buf
= HeapAlloc(GetProcessHeap(), 0, *val_size
);
936 else *val_buf
= HeapReAlloc(GetProcessHeap(), 0, *val_buf
, *val_size
);
937 CHECK_ENOUGH_MEMORY(val_buf
);
940 /* output data for the current key */
942 fputs(*reg_key_name_buf
, file
);
944 /* print all the values */
949 DWORD val_name_len1
= *val_name_len
;
950 DWORD val_size1
= *val_size
;
951 ret
= RegEnumValue(key
, i
, *val_name_buf
, &val_name_len1
, NULL
,
952 &value_type
, *val_buf
, &val_size1
);
953 if (ret
!= ERROR_SUCCESS
) {
955 if (ret
!= ERROR_NO_MORE_ITEMS
) {
956 REGPROC_print_error();
961 if ((*val_name_buf
)[0]) {
963 REGPROC_export_string(file
, *val_name_buf
);
969 switch (value_type
) {
973 if (val_size1
) REGPROC_export_string(file
, (char*) *val_buf
);
978 fprintf(file
, "dword:%08x\n", *((DWORD
*)*val_buf
));
982 fprintf(stderr
,"%s: warning - unsupported registry format '%d', "
984 getAppName(), value_type
);
985 fprintf(stderr
,"key name: \"%s\"\n", *reg_key_name_buf
);
986 fprintf(stderr
,"value name:\"%s\"\n\n", *val_name_buf
);
992 const CHAR
*hex_prefix
;
996 if (value_type
== REG_BINARY
) {
1000 sprintf(buf
, "hex(%d):", value_type
);
1003 /* position of where the next character will be printed */
1004 /* NOTE: yes, strlen("hex:") is used even for hex(x): */
1005 cur_pos
= strlen("\"\"=") + strlen("hex:") +
1006 strlen(*val_name_buf
);
1008 fputs(hex_prefix
, file
);
1009 for (i1
= 0; i1
< val_size1
; i1
++) {
1010 fprintf(file
, "%02x", (unsigned int)(*val_buf
)[i1
]);
1011 if (i1
+ 1 < val_size1
) {
1017 if (cur_pos
> REG_FILE_HEX_LINE_LEN
) {
1018 fputs("\\\n ", file
);
1031 (*reg_key_name_buf
)[curr_len
] = '\\';
1033 DWORD buf_len
= *reg_key_name_len
- curr_len
;
1035 ret
= RegEnumKeyEx(key
, i
, *reg_key_name_buf
+ curr_len
+ 1, &buf_len
,
1036 NULL
, NULL
, NULL
, NULL
);
1037 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_MORE_DATA
) {
1039 if (ret
!= ERROR_NO_MORE_ITEMS
) {
1040 REGPROC_print_error();
1046 if (RegOpenKey(key
, *reg_key_name_buf
+ curr_len
+ 1,
1047 &subkey
) == ERROR_SUCCESS
) {
1048 export_hkey(file
, subkey
, reg_key_name_buf
, reg_key_name_len
,
1049 val_name_buf
, val_name_len
, val_buf
, val_size
);
1050 RegCloseKey(subkey
);
1052 REGPROC_print_error();
1056 (*reg_key_name_buf
)[curr_len
] = '\0';
1059 /******************************************************************************
1060 * Open file for export.
1062 static FILE *REGPROC_open_export_file(CHAR
*file_name
)
1066 if (strcmp(file_name
,"-")==0)
1070 file
= fopen(file_name
, "w");
1073 fprintf(stderr
,"%s: Can't open file \"%s\"\n", getAppName(), file_name
);
1077 fputs("REGEDIT4\n", file
);
1081 /******************************************************************************
1082 * Writes contents of the registry key to the specified file stream.
1085 * file_name - name of a file to export registry branch to.
1086 * reg_key_name - registry branch to export. The whole registry is exported if
1087 * reg_key_name is NULL or contains an empty string.
1089 BOOL
export_registry_key(CHAR
*file_name
, CHAR
*reg_key_name
)
1091 CHAR
*reg_key_name_buf
;
1094 DWORD reg_key_name_len
= KEY_MAX_LEN
;
1095 DWORD val_name_len
= KEY_MAX_LEN
;
1096 DWORD val_size
= REG_VAL_BUF_SIZE
;
1099 reg_key_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1100 reg_key_name_len
* sizeof(*reg_key_name_buf
));
1101 val_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1102 val_name_len
* sizeof(*val_name_buf
));
1103 val_buf
= HeapAlloc(GetProcessHeap(), 0, val_size
);
1104 CHECK_ENOUGH_MEMORY(reg_key_name_buf
&& val_name_buf
&& val_buf
);
1106 if (reg_key_name
&& reg_key_name
[0]) {
1108 CHAR
*branch_name
= NULL
;
1111 REGPROC_resize_char_buffer(®_key_name_buf
, ®_key_name_len
,
1112 strlen(reg_key_name
));
1113 strcpy(reg_key_name_buf
, reg_key_name
);
1115 /* open the specified key */
1116 if (!parseKeyName(reg_key_name
, ®_key_class
, &branch_name
)) {
1117 fprintf(stderr
,"%s: Incorrect registry class specification in '%s'\n",
1118 getAppName(), reg_key_name
);
1121 if (!branch_name
[0]) {
1122 /* no branch - registry class is specified */
1123 file
= REGPROC_open_export_file(file_name
);
1124 export_hkey(file
, reg_key_class
,
1125 ®_key_name_buf
, ®_key_name_len
,
1126 &val_name_buf
, &val_name_len
,
1127 &val_buf
, &val_size
);
1128 } else if (RegOpenKey(reg_key_class
, branch_name
, &key
) == ERROR_SUCCESS
) {
1129 file
= REGPROC_open_export_file(file_name
);
1130 export_hkey(file
, key
,
1131 ®_key_name_buf
, ®_key_name_len
,
1132 &val_name_buf
, &val_name_len
,
1133 &val_buf
, &val_size
);
1136 fprintf(stderr
,"%s: Can't export. Registry key '%s' does not exist!\n",
1137 getAppName(), reg_key_name
);
1138 REGPROC_print_error();
1143 /* export all registry classes */
1144 file
= REGPROC_open_export_file(file_name
);
1145 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++) {
1146 /* do not export HKEY_CLASSES_ROOT */
1147 if (reg_class_keys
[i
] != HKEY_CLASSES_ROOT
&&
1148 reg_class_keys
[i
] != HKEY_CURRENT_USER
&&
1149 reg_class_keys
[i
] != HKEY_CURRENT_CONFIG
&&
1150 reg_class_keys
[i
] != HKEY_DYN_DATA
) {
1151 strcpy(reg_key_name_buf
, reg_class_names
[i
]);
1152 export_hkey(file
, reg_class_keys
[i
],
1153 ®_key_name_buf
, ®_key_name_len
,
1154 &val_name_buf
, &val_name_len
,
1155 &val_buf
, &val_size
);
1163 HeapFree(GetProcessHeap(), 0, reg_key_name
);
1164 HeapFree(GetProcessHeap(), 0, val_buf
);
1168 /******************************************************************************
1169 * Reads contents of the specified file into the registry.
1171 BOOL
import_registry_file(FILE* reg_file
)
1176 if (fread( s
, 2, 1, reg_file
) == 1)
1178 if (s
[0] == 0xff && s
[1] == 0xfe)
1180 processRegLinesW(reg_file
);
1184 processRegLinesA(reg_file
);
1192 /******************************************************************************
1193 * Removes the registry key with all subkeys. Parses full key name.
1196 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1197 * empty, points to register key class, does not exist.
1199 void delete_registry_key(WCHAR
*reg_key_name
)
1201 WCHAR
*key_name
= NULL
;
1204 if (!reg_key_name
|| !reg_key_name
[0])
1207 if (!parseKeyNameW(reg_key_name
, &key_class
, &key_name
)) {
1208 char* reg_key_nameA
= GetMultiByteString(reg_key_name
);
1209 fprintf(stderr
,"%s: Incorrect registry class specification in '%s'\n",
1210 getAppName(), reg_key_nameA
);
1211 HeapFree(GetProcessHeap(), 0, reg_key_nameA
);
1215 char* reg_key_nameA
= GetMultiByteString(reg_key_name
);
1216 fprintf(stderr
,"%s: Can't delete registry class '%s'\n",
1217 getAppName(), reg_key_nameA
);
1218 HeapFree(GetProcessHeap(), 0, reg_key_nameA
);
1222 RegDeleteTreeW(key_class
, key_name
);