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
)
76 int len
= MultiByteToWideChar(CP_ACP
, 0, strA
, -1, NULL
, 0);
78 strW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
79 CHECK_ENOUGH_MEMORY(strW
);
80 MultiByteToWideChar(CP_ACP
, 0, strA
, -1, strW
, len
);
86 /******************************************************************************
87 * Allocates memory and convers input from multibyte to wide chars
88 * Returned string must be freed by the caller
90 WCHAR
* GetWideStringN(const char* strA
, int chars
, DWORD
*len
)
95 *len
= MultiByteToWideChar(CP_ACP
, 0, strA
, chars
, NULL
, 0);
97 strW
= HeapAlloc(GetProcessHeap(), 0, *len
* sizeof(WCHAR
));
98 CHECK_ENOUGH_MEMORY(strW
);
99 MultiByteToWideChar(CP_ACP
, 0, strA
, chars
, strW
, *len
);
106 /******************************************************************************
107 * Allocates memory and convers input from wide chars to multibyte
108 * Returned string must be freed by the caller
110 char* GetMultiByteString(const WCHAR
* strW
)
115 int len
= WideCharToMultiByte(CP_ACP
, 0, strW
, -1, NULL
, 0, NULL
, NULL
);
117 strA
= HeapAlloc(GetProcessHeap(), 0, len
);
118 CHECK_ENOUGH_MEMORY(strA
);
119 WideCharToMultiByte(CP_ACP
, 0, strW
, -1, strA
, len
, NULL
, NULL
);
125 /******************************************************************************
126 * Allocates memory and convers input from wide chars to multibyte
127 * Returned string must be freed by the caller
129 char* GetMultiByteStringN(const WCHAR
* strW
, int chars
, DWORD
* len
)
134 *len
= WideCharToMultiByte(CP_ACP
, 0, strW
, chars
, NULL
, 0, NULL
, NULL
);
136 strA
= HeapAlloc(GetProcessHeap(), 0, *len
);
137 CHECK_ENOUGH_MEMORY(strA
);
138 WideCharToMultiByte(CP_ACP
, 0, strW
, chars
, strA
, *len
, NULL
, NULL
);
145 /******************************************************************************
146 * Converts a hex representation of a DWORD into a DWORD.
148 static BOOL
convertHexToDWord(WCHAR
* str
, DWORD
*dw
)
153 WideCharToMultiByte(CP_ACP
, 0, str
, -1, buf
, 9, NULL
, NULL
);
154 if (lstrlenW(str
) > 8 || sscanf(buf
, "%x%c", dw
, &dummy
) != 1) {
155 fprintf(stderr
,"%s: ERROR, invalid hex value\n", getAppName());
161 /******************************************************************************
162 * Converts a hex comma separated values list into a binary string.
164 static BYTE
* convertHexCSVToHex(WCHAR
*str
, DWORD
*size
)
169 /* The worst case is 1 digit + 1 comma per byte */
170 *size
=(lstrlenW(str
)+1)/2;
171 data
=HeapAlloc(GetProcessHeap(), 0, *size
);
172 CHECK_ENOUGH_MEMORY(data
);
181 wc
= strtoulW(s
,&end
,16);
182 if (end
== s
|| wc
> 0xff || (*end
&& *end
!= ',')) {
183 char* strA
= GetMultiByteString(s
);
184 fprintf(stderr
,"%s: ERROR converting CSV hex stream. Invalid value at '%s'\n",
186 HeapFree(GetProcessHeap(), 0, data
);
187 HeapFree(GetProcessHeap(), 0, strA
);
199 /******************************************************************************
200 * This function returns the HKEY associated with the data type encoded in the
201 * value. It modifies the input parameter (key value) in order to skip this
202 * "now useless" data type information.
204 * Note: Updated based on the algorithm used in 'server/registry.c'
206 static DWORD
getDataType(LPWSTR
*lpValue
, DWORD
* parse_type
)
208 struct data_type
{ const WCHAR
*tag
; int len
; int type
; int parse_type
; };
210 static const WCHAR quote
[] = {'"'};
211 static const WCHAR str
[] = {'s','t','r',':','"'};
212 static const WCHAR str2
[] = {'s','t','r','(','2',')',':','"'};
213 static const WCHAR hex
[] = {'h','e','x',':'};
214 static const WCHAR dword
[] = {'d','w','o','r','d',':'};
215 static const WCHAR hexp
[] = {'h','e','x','('};
217 static const struct data_type data_types
[] = { /* actual type */ /* type to assume for parsing */
218 { quote
, 1, REG_SZ
, REG_SZ
},
219 { str
, 5, REG_SZ
, REG_SZ
},
220 { str2
, 8, REG_EXPAND_SZ
, REG_SZ
},
221 { hex
, 4, REG_BINARY
, REG_BINARY
},
222 { dword
, 6, REG_DWORD
, REG_DWORD
},
223 { hexp
, 4, -1, REG_BINARY
},
227 const struct data_type
*ptr
;
230 for (ptr
= data_types
; ptr
->tag
; ptr
++) {
231 if (strncmpW( ptr
->tag
, *lpValue
, ptr
->len
))
235 *parse_type
= ptr
->parse_type
;
241 /* "hex(xx):" is special */
242 type
= (int)strtoulW( *lpValue
, &end
, 16 );
243 if (**lpValue
=='\0' || *end
!=')' || *(end
+1)!=':') {
251 *parse_type
=REG_NONE
;
255 /******************************************************************************
256 * Replaces escape sequences with the characters.
258 static void REGPROC_unescape_string(WCHAR
* str
)
260 int str_idx
= 0; /* current character under analysis */
261 int val_idx
= 0; /* the last character of the unescaped string */
262 int len
= lstrlenW(str
);
263 for (str_idx
= 0; str_idx
< len
; str_idx
++, val_idx
++) {
264 if (str
[str_idx
] == '\\') {
266 switch (str
[str_idx
]) {
272 str
[val_idx
] = str
[str_idx
];
275 fprintf(stderr
,"Warning! Unrecognized escape sequence: \\%c'\n",
277 str
[val_idx
] = str
[str_idx
];
281 str
[val_idx
] = str
[str_idx
];
287 static BOOL
parseKeyName(LPWSTR lpKeyName
, HKEY
*hKey
, LPWSTR
*lpKeyPath
)
289 WCHAR
* lpSlash
= NULL
;
292 if (lpKeyName
== NULL
)
295 for(i
= 0; *(lpKeyName
+i
) != 0; i
++)
297 if(*(lpKeyName
+i
) == '\\')
299 lpSlash
= lpKeyName
+i
;
306 len
= lpSlash
-lpKeyName
;
310 len
= lstrlenW(lpKeyName
);
311 lpSlash
= lpKeyName
+len
;
315 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++) {
316 if (CompareStringW(LOCALE_USER_DEFAULT
, 0, lpKeyName
, len
, reg_class_namesW
[i
], len
) == CSTR_EQUAL
&&
317 len
== lstrlenW(reg_class_namesW
[i
])) {
318 *hKey
= reg_class_keys
[i
];
327 if (*lpSlash
!= '\0')
329 *lpKeyPath
= lpSlash
;
333 /* Globals used by the setValue() & co */
334 static LPSTR currentKeyName
;
335 static HKEY currentKeyHandle
= NULL
;
337 /******************************************************************************
338 * Sets the value with name val_name to the data in val_data for the currently
342 * val_name - name of the registry value
343 * val_data - registry value data
345 static LONG
setValue(WCHAR
* val_name
, WCHAR
* val_data
, BOOL is_unicode
)
348 DWORD dwDataType
, dwParseType
;
351 WCHAR del
[] = {'-',0};
353 if ( (val_name
== NULL
) || (val_data
== NULL
) )
354 return ERROR_INVALID_PARAMETER
;
356 if (lstrcmpW(val_data
, del
) == 0)
358 res
=RegDeleteValueW(currentKeyHandle
,val_name
);
359 return (res
== ERROR_FILE_NOT_FOUND
? ERROR_SUCCESS
: res
);
362 /* Get the data type stored into the value field */
363 dwDataType
= getDataType(&val_data
, &dwParseType
);
365 if (dwParseType
== REG_SZ
) /* no conversion for string */
367 REGPROC_unescape_string(val_data
);
368 /* Compute dwLen after REGPROC_unescape_string because it may
369 * have changed the string length and we don't want to store
370 * the extra garbage in the registry.
372 dwLen
= lstrlenW(val_data
);
373 if (dwLen
>0 && val_data
[dwLen
-1]=='"')
376 val_data
[dwLen
]='\0';
378 lpbData
= (BYTE
*) val_data
;
379 dwLen
++; /* include terminating null */
380 dwLen
= dwLen
* sizeof(WCHAR
); /* size is in bytes */
382 else if (dwParseType
== REG_DWORD
) /* Convert the dword types */
384 if (!convertHexToDWord(val_data
, &dwData
))
385 return ERROR_INVALID_DATA
;
386 lpbData
= (BYTE
*)&dwData
;
387 dwLen
= sizeof(dwData
);
389 else if (dwParseType
== REG_BINARY
) /* Convert the binary data */
391 lpbData
= convertHexCSVToHex(val_data
, &dwLen
);
393 return ERROR_INVALID_DATA
;
395 if(dwDataType
== REG_MULTI_SZ
&& !is_unicode
)
397 LPBYTE tmp
= lpbData
;
398 lpbData
= (LPBYTE
)GetWideStringN((char*)lpbData
, dwLen
, &dwLen
);
399 dwLen
*= sizeof(WCHAR
);
400 HeapFree(GetProcessHeap(), 0, tmp
);
403 else /* unknown format */
405 fprintf(stderr
,"%s: ERROR, unknown data format\n", getAppName());
406 return ERROR_INVALID_DATA
;
409 res
= RegSetValueExW(
416 if (dwParseType
== REG_BINARY
)
417 HeapFree(GetProcessHeap(), 0, lpbData
);
421 /******************************************************************************
422 * A helper function for processRegEntry() that opens the current key.
423 * That key must be closed by calling closeKey().
425 static LONG
openKeyW(WCHAR
* stdInput
)
433 if (stdInput
== NULL
)
434 return ERROR_INVALID_PARAMETER
;
436 /* Get the registry class */
437 if (!parseKeyName(stdInput
, &keyClass
, &keyPath
))
438 return ERROR_INVALID_PARAMETER
;
440 res
= RegCreateKeyExW(
441 keyClass
, /* Class */
442 keyPath
, /* Sub Key */
444 NULL
, /* object type */
445 REG_OPTION_NON_VOLATILE
, /* option, REG_OPTION_NON_VOLATILE ... */
446 KEY_ALL_ACCESS
, /* access mask, KEY_ALL_ACCESS */
447 NULL
, /* security attribute */
448 ¤tKeyHandle
, /* result */
449 &dwDisp
); /* disposition, REG_CREATED_NEW_KEY or
450 REG_OPENED_EXISTING_KEY */
452 if (res
== ERROR_SUCCESS
)
453 currentKeyName
= GetMultiByteString(stdInput
);
455 currentKeyHandle
= NULL
;
461 /******************************************************************************
462 * Close the currently opened key.
464 static void closeKey(void)
466 if (currentKeyHandle
)
468 HeapFree(GetProcessHeap(), 0, currentKeyName
);
469 RegCloseKey(currentKeyHandle
);
470 currentKeyHandle
= NULL
;
474 /******************************************************************************
475 * This function is a wrapper for the setValue function. It prepares the
476 * land and cleans the area once completed.
477 * Note: this function modifies the line parameter.
479 * line - registry file unwrapped line. Should have the registry value name and
480 * complete registry value data.
482 static void processSetValue(WCHAR
* line
, BOOL is_unicode
)
484 WCHAR
* val_name
; /* registry value name */
485 WCHAR
* val_data
; /* registry value data */
486 int line_idx
= 0; /* current character under analysis */
490 if (line
[line_idx
] == '@' && line
[line_idx
+ 1] == '=') {
491 line
[line_idx
] = '\0';
494 } else if (line
[line_idx
] == '\"') {
496 val_name
= line
+ line_idx
;
498 if (line
[line_idx
] == '\\') /* skip escaped character */
502 if (line
[line_idx
] == '\"') {
503 line
[line_idx
] = '\0';
511 if (line
[line_idx
] != '=') {
513 line
[line_idx
] = '\"';
514 lineA
= GetMultiByteString(line
);
515 fprintf(stderr
,"Warning! unrecognized line:\n%s\n", lineA
);
516 HeapFree(GetProcessHeap(), 0, lineA
);
521 char* lineA
= GetMultiByteString(line
);
522 fprintf(stderr
,"Warning! unrecognized line:\n%s\n", lineA
);
523 HeapFree(GetProcessHeap(), 0, lineA
);
526 line_idx
++; /* skip the '=' character */
527 val_data
= line
+ line_idx
;
529 REGPROC_unescape_string(val_name
);
530 res
= setValue(val_name
, val_data
, is_unicode
);
531 if ( res
!= ERROR_SUCCESS
)
533 char* val_nameA
= GetMultiByteString(val_name
);
534 char* val_dataA
= GetMultiByteString(val_data
);
535 fprintf(stderr
,"%s: ERROR Key %s not created. Value: %s, Data: %s\n",
540 HeapFree(GetProcessHeap(), 0, val_nameA
);
541 HeapFree(GetProcessHeap(), 0, val_dataA
);
545 /******************************************************************************
546 * This function receives the currently read entry and performs the
547 * corresponding action.
548 * isUnicode affects parsing of REG_MULTI_SZ values
550 static void processRegEntry(WCHAR
* stdInput
, BOOL isUnicode
)
553 * We encountered the end of the file, make sure we
554 * close the opened key and exit
556 if (stdInput
== NULL
) {
561 if ( stdInput
[0] == '[') /* We are reading a new key */
564 closeKey(); /* Close the previous key */
566 /* Get rid of the square brackets */
568 keyEnd
= strrchrW(stdInput
, ']');
572 /* delete the key if we encounter '-' at the start of reg key */
573 if ( stdInput
[0] == '-')
575 delete_registry_key(stdInput
+ 1);
576 } else if ( openKeyW(stdInput
) != ERROR_SUCCESS
)
578 fprintf(stderr
,"%s: setValue failed to open key %s\n",
579 getAppName(), stdInput
);
581 } else if( currentKeyHandle
&&
582 (( stdInput
[0] == '@') || /* reading a default @=data pair */
583 ( stdInput
[0] == '\"'))) /* reading a new value=data pair */
585 processSetValue(stdInput
, isUnicode
);
588 /* Since we are assuming that the file format is valid we must be
589 * reading a blank line which indicates the end of this key processing
595 /******************************************************************************
596 * Processes a registry file.
597 * Correctly processes comments (in # form), line continuation.
600 * in - input stream to read from
602 void processRegLinesA(FILE *in
)
604 LPSTR line
= NULL
; /* line read from input stream */
605 ULONG lineSize
= REG_VAL_BUF_SIZE
;
607 line
= HeapAlloc(GetProcessHeap(), 0, lineSize
);
608 CHECK_ENOUGH_MEMORY(line
);
611 LPSTR s
; /* The pointer into line for where the current fgets should read */
616 size_t size_remaining
;
618 char *s_eol
; /* various local uses */
620 /* Do we need to expand the buffer ? */
621 assert (s
>= line
&& s
<= line
+ lineSize
);
622 size_remaining
= lineSize
- (s
-line
);
623 if (size_remaining
< 2) /* room for 1 character and the \0 */
626 size_t new_size
= lineSize
+ REG_VAL_BUF_SIZE
;
627 if (new_size
> lineSize
) /* no arithmetic overflow */
628 new_buffer
= HeapReAlloc (GetProcessHeap(), 0, line
, new_size
);
631 CHECK_ENOUGH_MEMORY(new_buffer
);
633 s
= line
+ lineSize
- size_remaining
;
635 size_remaining
= lineSize
- (s
-line
);
638 /* Get as much as possible into the buffer, terminated either by
639 * eof, error, eol or getting the maximum amount. Abort on error.
641 size_to_get
= (size_remaining
> INT_MAX
? INT_MAX
: size_remaining
);
643 check
= fgets (s
, size_to_get
, in
);
647 perror ("While reading input");
652 /* It is not clear to me from the definition that the
653 * contents of the buffer are well defined on detecting
654 * an eof without managing to read anything.
659 /* If we didn't read the eol nor the eof go around for the rest */
660 s_eol
= strchr (s
, '\n');
661 if (!feof (in
) && !s_eol
) {
662 s
= strchr (s
, '\0');
663 /* It should be s + size_to_get - 1 but this is safer */
667 /* If it is a comment line then discard it and go around again */
668 if (line
[0] == '#') {
673 /* Remove any line feed. Leave s_eol on the \0 */
676 if (s_eol
> line
&& *(s_eol
-1) == '\r')
679 s_eol
= strchr (s
, '\0');
681 /* If there is a concatenating \\ then go around again */
682 if (s_eol
> line
&& *(s_eol
-1) == '\\') {
689 } while(c
== ' ' || c
== '\t');
693 fprintf(stderr
,"%s: ERROR - invalid continuation.\n",
704 lineW
= GetWideString(line
);
706 break; /* That is the full virtual line */
709 processRegEntry(lineW
, FALSE
);
710 HeapFree(GetProcessHeap(), 0, lineW
);
712 processRegEntry(NULL
, FALSE
);
714 HeapFree(GetProcessHeap(), 0, line
);
717 void processRegLinesW(FILE *in
)
719 WCHAR
* buf
= NULL
; /* line read from input stream */
720 ULONG lineSize
= REG_VAL_BUF_SIZE
;
721 size_t CharsInBuf
= -1;
723 WCHAR
* s
; /* The pointer into line for where the current fgets should read */
725 buf
= HeapAlloc(GetProcessHeap(), 0, lineSize
* sizeof(WCHAR
));
726 CHECK_ENOUGH_MEMORY(buf
);
731 size_t size_remaining
;
733 WCHAR
*s_eol
= NULL
; /* various local uses */
735 /* Do we need to expand the buffer ? */
736 assert (s
>= buf
&& s
<= buf
+ lineSize
);
737 size_remaining
= lineSize
- (s
-buf
);
738 if (size_remaining
< 2) /* room for 1 character and the \0 */
741 size_t new_size
= lineSize
+ (REG_VAL_BUF_SIZE
/ sizeof(WCHAR
));
742 if (new_size
> lineSize
) /* no arithmetic overflow */
743 new_buffer
= HeapReAlloc (GetProcessHeap(), 0, buf
, new_size
* sizeof(WCHAR
));
746 CHECK_ENOUGH_MEMORY(new_buffer
);
748 s
= buf
+ lineSize
- size_remaining
;
750 size_remaining
= lineSize
- (s
-buf
);
753 /* Get as much as possible into the buffer, terminated either by
754 * eof, error or getting the maximum amount. Abort on error.
756 size_to_get
= (size_remaining
> INT_MAX
? INT_MAX
: size_remaining
);
758 CharsInBuf
= fread(s
, sizeof(WCHAR
), size_to_get
- 1, in
);
761 if (CharsInBuf
== 0) {
763 perror ("While reading input");
768 /* It is not clear to me from the definition that the
769 * contents of the buffer are well defined on detecting
770 * an eof without managing to read anything.
775 /* If we didn't read the eol nor the eof go around for the rest */
778 s_eol
= strchrW(s
, '\n');
783 /* If it is a comment line then discard it and go around again */
789 /* If there is a concatenating \\ then go around again */
790 if ((*(s_eol
-1) == '\\') ||
791 (*(s_eol
-1) == '\r' && *(s_eol
-2) == '\\')) {
792 WCHAR
* NextLine
= s_eol
;
794 while(*(NextLine
+1) == ' ' || *(NextLine
+1) == '\t')
799 if(*(s_eol
-1) == '\r')
802 MoveMemory(s_eol
- 1, NextLine
, (CharsInBuf
- (NextLine
- buf
) + 1)*sizeof(WCHAR
));
803 CharsInBuf
-= NextLine
- s_eol
+ 1;
808 /* Remove any line feed. Leave s_eol on the \0 */
811 if (s_eol
> buf
&& *(s_eol
-1) == '\r')
818 processRegEntry(s
, TRUE
);
821 continue; /* That is the full virtual line */
825 processRegEntry(NULL
, TRUE
);
827 HeapFree(GetProcessHeap(), 0, buf
);
830 /****************************************************************************
831 * REGPROC_print_error
833 * Print the message for GetLastError
836 static void REGPROC_print_error(void)
842 error_code
= GetLastError ();
843 status
= FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
844 NULL
, error_code
, 0, (LPTSTR
) &lpMsgBuf
, 0, NULL
);
846 fprintf(stderr
,"%s: Cannot display message for error %d, status %d\n",
847 getAppName(), error_code
, GetLastError());
851 LocalFree((HLOCAL
)lpMsgBuf
);
855 /******************************************************************************
856 * Checks whether the buffer has enough room for the string or required size.
857 * Resizes the buffer if necessary.
860 * buffer - pointer to a buffer for string
861 * len - current length of the buffer in characters.
862 * required_len - length of the string to place to the buffer in characters.
863 * The length does not include the terminating null character.
865 static void REGPROC_resize_char_buffer(WCHAR
**buffer
, DWORD
*len
, DWORD required_len
)
868 if (required_len
> *len
) {
871 *buffer
= HeapAlloc(GetProcessHeap(), 0, *len
* sizeof(**buffer
));
873 *buffer
= HeapReAlloc(GetProcessHeap(), 0, *buffer
, *len
* sizeof(**buffer
));
874 CHECK_ENOUGH_MEMORY(*buffer
);
878 /******************************************************************************
879 * Prints string str to file
881 static void REGPROC_export_string(FILE *file
, WCHAR
*str
)
883 CHAR
* strA
= GetMultiByteString(str
);
884 size_t len
= strlen(strA
);
887 /* escaping characters */
888 for (i
= 0; i
< len
; i
++) {
905 HeapFree(GetProcessHeap(), 0, strA
);
908 /******************************************************************************
909 * Writes contents of the registry key to the specified file stream.
912 * file - writable file stream to export registry branch to.
913 * key - registry branch to export.
914 * reg_key_name_buf - name of the key with registry class.
915 * Is resized if necessary.
916 * reg_key_name_len - length of the buffer for the registry class in characters.
917 * val_name_buf - buffer for storing value name.
918 * Is resized if necessary.
919 * val_name_len - length of the buffer for storing value names in characters.
920 * val_buf - buffer for storing values while extracting.
921 * Is resized if necessary.
922 * val_size - size of the buffer for storing values in bytes.
924 static void export_hkey(FILE *file
, HKEY key
,
925 WCHAR
**reg_key_name_buf
, DWORD
*reg_key_name_len
,
926 WCHAR
**val_name_buf
, DWORD
*val_name_len
,
927 BYTE
**val_buf
, DWORD
*val_size
)
929 DWORD max_sub_key_len
;
930 DWORD max_val_name_len
;
938 /* get size information and resize the buffers if necessary */
939 if (RegQueryInfoKeyW(key
, NULL
, NULL
, NULL
, NULL
,
940 &max_sub_key_len
, NULL
,
941 NULL
, &max_val_name_len
, &max_val_size
, NULL
, NULL
942 ) != ERROR_SUCCESS
) {
943 REGPROC_print_error();
945 curr_len
= strlenW(*reg_key_name_buf
);
946 REGPROC_resize_char_buffer(reg_key_name_buf
, reg_key_name_len
,
947 max_sub_key_len
+ curr_len
+ 1);
948 REGPROC_resize_char_buffer(val_name_buf
, val_name_len
,
950 if (max_val_size
> *val_size
) {
951 *val_size
= max_val_size
;
952 if (!*val_buf
) *val_buf
= HeapAlloc(GetProcessHeap(), 0, *val_size
);
953 else *val_buf
= HeapReAlloc(GetProcessHeap(), 0, *val_buf
, *val_size
);
954 CHECK_ENOUGH_MEMORY(val_buf
);
957 /* output data for the current key */
959 bufA
= GetMultiByteString(*reg_key_name_buf
);
961 HeapFree(GetProcessHeap(), 0, bufA
);
963 /* print all the values */
968 DWORD val_name_len1
= *val_name_len
;
969 DWORD val_size1
= *val_size
;
970 ret
= RegEnumValueW(key
, i
, *val_name_buf
, &val_name_len1
, NULL
,
971 &value_type
, *val_buf
, &val_size1
);
972 if (ret
!= ERROR_SUCCESS
) {
974 if (ret
!= ERROR_NO_MORE_ITEMS
) {
975 REGPROC_print_error();
980 if ((*val_name_buf
)[0]) {
982 REGPROC_export_string(file
, *val_name_buf
);
988 switch (value_type
) {
992 if (val_size1
) REGPROC_export_string(file
, (WCHAR
*) *val_buf
);
997 fprintf(file
, "dword:%08x\n", *((DWORD
*)*val_buf
));
1001 fprintf(stderr
,"%s: warning - unsupported registry format '%d', "
1002 "treat as binary\n",
1003 getAppName(), value_type
);
1004 fprintf(stderr
,"key name: \"%s\"\n", *reg_key_name_buf
);
1005 fprintf(stderr
,"value name:\"%s\"\n\n", *val_name_buf
);
1011 const WCHAR
*hex_prefix
;
1014 const WCHAR hex
[] = {'h','e','x',':',0};
1015 const WCHAR delim
[] = {'"','"','=',0};
1017 BYTE
* val_buf1
= *val_buf
;
1018 DWORD val_buf1_size
= val_size1
;
1020 if (value_type
== REG_BINARY
) {
1023 const WCHAR hex_format
[] = {'h','e','x','(','%','d',')',':',0};
1025 wsprintfW(buf
, hex_format
, value_type
);
1026 if(value_type
== REG_MULTI_SZ
)
1027 val_buf1
= (BYTE
*)GetMultiByteStringN((WCHAR
*)*val_buf
, val_size1
/ sizeof(WCHAR
), &val_buf1_size
);
1030 /* position of where the next character will be printed */
1031 /* NOTE: yes, strlen("hex:") is used even for hex(x): */
1032 cur_pos
= lstrlenW(delim
) + lstrlenW(hex
) +
1033 lstrlenW(*val_name_buf
);
1035 hex_prefixA
= GetMultiByteString(hex_prefix
);
1036 fputs(hex_prefixA
, file
);
1037 HeapFree(GetProcessHeap(), 0, hex_prefixA
);
1038 for (i1
= 0; i1
< val_buf1_size
; i1
++) {
1039 fprintf(file
, "%02x", (unsigned int)(val_buf1
)[i1
]);
1040 if (i1
+ 1 < val_buf1_size
) {
1046 if (cur_pos
> REG_FILE_HEX_LINE_LEN
) {
1047 fputs("\\\n ", file
);
1051 if(value_type
== REG_MULTI_SZ
)
1052 HeapFree(GetProcessHeap(), 0, val_buf1
);
1062 (*reg_key_name_buf
)[curr_len
] = '\\';
1064 DWORD buf_len
= *reg_key_name_len
- curr_len
;
1066 ret
= RegEnumKeyExW(key
, i
, *reg_key_name_buf
+ curr_len
+ 1, &buf_len
,
1067 NULL
, NULL
, NULL
, NULL
);
1068 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_MORE_DATA
) {
1070 if (ret
!= ERROR_NO_MORE_ITEMS
) {
1071 REGPROC_print_error();
1077 if (RegOpenKeyW(key
, *reg_key_name_buf
+ curr_len
+ 1,
1078 &subkey
) == ERROR_SUCCESS
) {
1079 export_hkey(file
, subkey
, reg_key_name_buf
, reg_key_name_len
,
1080 val_name_buf
, val_name_len
, val_buf
, val_size
);
1081 RegCloseKey(subkey
);
1083 REGPROC_print_error();
1087 (*reg_key_name_buf
)[curr_len
] = '\0';
1090 /******************************************************************************
1091 * Open file for export.
1093 static FILE *REGPROC_open_export_file(WCHAR
*file_name
)
1098 if (strncmpW(file_name
,&dash
,1)==0)
1102 CHAR
* file_nameA
= GetMultiByteString(file_name
);
1103 file
= fopen(file_nameA
, "w");
1106 fprintf(stderr
,"%s: Can't open file \"%s\"\n", getAppName(), file_nameA
);
1107 HeapFree(GetProcessHeap(), 0, file_nameA
);
1110 HeapFree(GetProcessHeap(), 0, file_nameA
);
1112 fputs("REGEDIT4\n", file
);
1116 /******************************************************************************
1117 * Writes contents of the registry key to the specified file stream.
1120 * file_name - name of a file to export registry branch to.
1121 * reg_key_name - registry branch to export. The whole registry is exported if
1122 * reg_key_name is NULL or contains an empty string.
1124 BOOL
export_registry_key(WCHAR
*file_name
, WCHAR
*reg_key_name
)
1126 WCHAR
*reg_key_name_buf
;
1127 WCHAR
*val_name_buf
;
1129 DWORD reg_key_name_len
= KEY_MAX_LEN
;
1130 DWORD val_name_len
= KEY_MAX_LEN
;
1131 DWORD val_size
= REG_VAL_BUF_SIZE
;
1134 reg_key_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1135 reg_key_name_len
* sizeof(*reg_key_name_buf
));
1136 val_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1137 val_name_len
* sizeof(*val_name_buf
));
1138 val_buf
= HeapAlloc(GetProcessHeap(), 0, val_size
);
1139 CHECK_ENOUGH_MEMORY(reg_key_name_buf
&& val_name_buf
&& val_buf
);
1141 if (reg_key_name
&& reg_key_name
[0]) {
1143 WCHAR
*branch_name
= NULL
;
1146 REGPROC_resize_char_buffer(®_key_name_buf
, ®_key_name_len
,
1147 lstrlenW(reg_key_name
));
1148 lstrcpyW(reg_key_name_buf
, reg_key_name
);
1150 /* open the specified key */
1151 if (!parseKeyName(reg_key_name
, ®_key_class
, &branch_name
)) {
1152 CHAR
* key_nameA
= GetMultiByteString(reg_key_name
);
1153 fprintf(stderr
,"%s: Incorrect registry class specification in '%s'\n",
1154 getAppName(), key_nameA
);
1155 HeapFree(GetProcessHeap(), 0, key_nameA
);
1158 if (!branch_name
[0]) {
1159 /* no branch - registry class is specified */
1160 file
= REGPROC_open_export_file(file_name
);
1161 export_hkey(file
, reg_key_class
,
1162 ®_key_name_buf
, ®_key_name_len
,
1163 &val_name_buf
, &val_name_len
,
1164 &val_buf
, &val_size
);
1165 } else if (RegOpenKeyW(reg_key_class
, branch_name
, &key
) == ERROR_SUCCESS
) {
1166 file
= REGPROC_open_export_file(file_name
);
1167 export_hkey(file
, key
,
1168 ®_key_name_buf
, ®_key_name_len
,
1169 &val_name_buf
, &val_name_len
,
1170 &val_buf
, &val_size
);
1173 CHAR
* key_nameA
= GetMultiByteString(reg_key_name
);
1174 fprintf(stderr
,"%s: Can't export. Registry key '%s' does not exist!\n",
1175 getAppName(), key_nameA
);
1176 HeapFree(GetProcessHeap(), 0, key_nameA
);
1177 REGPROC_print_error();
1182 /* export all registry classes */
1183 file
= REGPROC_open_export_file(file_name
);
1184 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++) {
1185 /* do not export HKEY_CLASSES_ROOT */
1186 if (reg_class_keys
[i
] != HKEY_CLASSES_ROOT
&&
1187 reg_class_keys
[i
] != HKEY_CURRENT_USER
&&
1188 reg_class_keys
[i
] != HKEY_CURRENT_CONFIG
&&
1189 reg_class_keys
[i
] != HKEY_DYN_DATA
) {
1190 lstrcpyW(reg_key_name_buf
, reg_class_namesW
[i
]);
1191 export_hkey(file
, reg_class_keys
[i
],
1192 ®_key_name_buf
, ®_key_name_len
,
1193 &val_name_buf
, &val_name_len
,
1194 &val_buf
, &val_size
);
1202 HeapFree(GetProcessHeap(), 0, reg_key_name
);
1203 HeapFree(GetProcessHeap(), 0, val_name_buf
);
1204 HeapFree(GetProcessHeap(), 0, val_buf
);
1208 /******************************************************************************
1209 * Reads contents of the specified file into the registry.
1211 BOOL
import_registry_file(FILE* reg_file
)
1216 if (fread( s
, 2, 1, reg_file
) == 1)
1218 if (s
[0] == 0xff && s
[1] == 0xfe)
1220 processRegLinesW(reg_file
);
1224 processRegLinesA(reg_file
);
1232 /******************************************************************************
1233 * Removes the registry key with all subkeys. Parses full key name.
1236 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1237 * empty, points to register key class, does not exist.
1239 void delete_registry_key(WCHAR
*reg_key_name
)
1241 WCHAR
*key_name
= NULL
;
1244 if (!reg_key_name
|| !reg_key_name
[0])
1247 if (!parseKeyName(reg_key_name
, &key_class
, &key_name
)) {
1248 char* reg_key_nameA
= GetMultiByteString(reg_key_name
);
1249 fprintf(stderr
,"%s: Incorrect registry class specification in '%s'\n",
1250 getAppName(), reg_key_nameA
);
1251 HeapFree(GetProcessHeap(), 0, reg_key_nameA
);
1255 char* reg_key_nameA
= GetMultiByteString(reg_key_name
);
1256 fprintf(stderr
,"%s: Can't delete registry class '%s'\n",
1257 getAppName(), reg_key_nameA
);
1258 HeapFree(GetProcessHeap(), 0, reg_key_nameA
);
1262 RegDeleteTreeW(key_class
, key_name
);