2 * Registry processing routines. Routines, common for registry
3 * processing frontends.
5 * Copyright 1999 Sylvain St-Germain
6 * Copyright 2002 Andriy Palamarchuk
7 * Copyright 2008 Alexander N. Sørnes <alex@thehandofagony.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include <wine/unicode.h>
33 #define REG_VAL_BUF_SIZE 4096
35 /* maximal number of characters in hexadecimal data line,
36 * including the indentation, but not including the '\' character
38 #define REG_FILE_HEX_LINE_LEN (2 + 25 * 3)
40 static const CHAR
*reg_class_names
[] = {
41 "HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_CLASSES_ROOT",
42 "HKEY_CURRENT_CONFIG", "HKEY_CURRENT_USER", "HKEY_DYN_DATA"
45 #define REG_CLASS_NUMBER (sizeof(reg_class_names) / sizeof(reg_class_names[0]))
47 extern const WCHAR
* reg_class_namesW
[];
49 static HKEY reg_class_keys
[REG_CLASS_NUMBER
] = {
50 HKEY_LOCAL_MACHINE
, HKEY_USERS
, HKEY_CLASSES_ROOT
,
51 HKEY_CURRENT_CONFIG
, HKEY_CURRENT_USER
, HKEY_DYN_DATA
55 #define NOT_ENOUGH_MEMORY 1
58 /* processing macros */
60 /* common check of memory allocation results */
61 #define CHECK_ENOUGH_MEMORY(p) \
64 fprintf(stderr,"%s: file %s, line %d: Not enough memory\n", \
65 getAppName(), __FILE__, __LINE__); \
66 exit(NOT_ENOUGH_MEMORY); \
69 /******************************************************************************
70 * Allocates memory and converts input from multibyte to wide chars
71 * Returned string must be freed by the caller
73 WCHAR
* GetWideString(const char* strA
)
78 int len
= MultiByteToWideChar(CP_ACP
, 0, strA
, -1, NULL
, 0);
80 strW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
81 CHECK_ENOUGH_MEMORY(strW
);
82 MultiByteToWideChar(CP_ACP
, 0, strA
, -1, strW
, len
);
88 /******************************************************************************
89 * Allocates memory and converts input from multibyte to wide chars
90 * Returned string must be freed by the caller
92 static WCHAR
* GetWideStringN(const char* strA
, int chars
, DWORD
*len
)
97 *len
= MultiByteToWideChar(CP_ACP
, 0, strA
, chars
, NULL
, 0);
99 strW
= HeapAlloc(GetProcessHeap(), 0, *len
* sizeof(WCHAR
));
100 CHECK_ENOUGH_MEMORY(strW
);
101 MultiByteToWideChar(CP_ACP
, 0, strA
, chars
, strW
, *len
);
108 /******************************************************************************
109 * Allocates memory and converts input from wide chars to multibyte
110 * Returned string must be freed by the caller
112 char* GetMultiByteString(const WCHAR
* strW
)
117 int len
= WideCharToMultiByte(CP_ACP
, 0, strW
, -1, NULL
, 0, NULL
, NULL
);
119 strA
= HeapAlloc(GetProcessHeap(), 0, len
);
120 CHECK_ENOUGH_MEMORY(strA
);
121 WideCharToMultiByte(CP_ACP
, 0, strW
, -1, strA
, len
, NULL
, NULL
);
127 /******************************************************************************
128 * Allocates memory and converts input from wide chars to multibyte
129 * Returned string must be freed by the caller
131 static char* GetMultiByteStringN(const WCHAR
* strW
, int chars
, DWORD
* len
)
136 *len
= WideCharToMultiByte(CP_ACP
, 0, strW
, chars
, NULL
, 0, NULL
, NULL
);
138 strA
= HeapAlloc(GetProcessHeap(), 0, *len
);
139 CHECK_ENOUGH_MEMORY(strA
);
140 WideCharToMultiByte(CP_ACP
, 0, strW
, chars
, strA
, *len
, NULL
, NULL
);
147 /******************************************************************************
148 * Converts a hex representation of a DWORD into a DWORD.
150 static BOOL
convertHexToDWord(WCHAR
* str
, DWORD
*dw
)
155 WideCharToMultiByte(CP_ACP
, 0, str
, -1, buf
, 9, NULL
, NULL
);
156 if (lstrlenW(str
) > 8 || sscanf(buf
, "%x%c", dw
, &dummy
) != 1) {
157 fprintf(stderr
,"%s: ERROR, invalid hex value\n", getAppName());
163 /******************************************************************************
164 * Converts a hex comma separated values list into a binary string.
166 static BYTE
* convertHexCSVToHex(WCHAR
*str
, DWORD
*size
)
171 /* The worst case is 1 digit + 1 comma per byte */
172 *size
=(lstrlenW(str
)+1)/2;
173 data
=HeapAlloc(GetProcessHeap(), 0, *size
);
174 CHECK_ENOUGH_MEMORY(data
);
183 wc
= strtoulW(s
,&end
,16);
184 if (end
== s
|| wc
> 0xff || (*end
&& *end
!= ',')) {
185 char* strA
= GetMultiByteString(s
);
186 fprintf(stderr
,"%s: ERROR converting CSV hex stream. Invalid value at '%s'\n",
188 HeapFree(GetProcessHeap(), 0, data
);
189 HeapFree(GetProcessHeap(), 0, strA
);
201 /******************************************************************************
202 * This function returns the HKEY associated with the data type encoded in the
203 * value. It modifies the input parameter (key value) in order to skip this
204 * "now useless" data type information.
206 * Note: Updated based on the algorithm used in 'server/registry.c'
208 static DWORD
getDataType(LPWSTR
*lpValue
, DWORD
* parse_type
)
210 struct data_type
{ const WCHAR
*tag
; int len
; int type
; int parse_type
; };
212 static const WCHAR quote
[] = {'"'};
213 static const WCHAR str
[] = {'s','t','r',':','"'};
214 static const WCHAR str2
[] = {'s','t','r','(','2',')',':','"'};
215 static const WCHAR hex
[] = {'h','e','x',':'};
216 static const WCHAR dword
[] = {'d','w','o','r','d',':'};
217 static const WCHAR hexp
[] = {'h','e','x','('};
219 static const struct data_type data_types
[] = { /* actual type */ /* type to assume for parsing */
220 { quote
, 1, REG_SZ
, REG_SZ
},
221 { str
, 5, REG_SZ
, REG_SZ
},
222 { str2
, 8, REG_EXPAND_SZ
, REG_SZ
},
223 { hex
, 4, REG_BINARY
, REG_BINARY
},
224 { dword
, 6, REG_DWORD
, REG_DWORD
},
225 { hexp
, 4, -1, REG_BINARY
},
229 const struct data_type
*ptr
;
232 for (ptr
= data_types
; ptr
->tag
; ptr
++) {
233 if (strncmpW( ptr
->tag
, *lpValue
, ptr
->len
))
237 *parse_type
= ptr
->parse_type
;
243 /* "hex(xx):" is special */
244 type
= (int)strtoulW( *lpValue
, &end
, 16 );
245 if (**lpValue
=='\0' || *end
!=')' || *(end
+1)!=':') {
253 *parse_type
=REG_NONE
;
257 /******************************************************************************
258 * Replaces escape sequences with the characters.
260 static void REGPROC_unescape_string(WCHAR
* str
)
262 int str_idx
= 0; /* current character under analysis */
263 int val_idx
= 0; /* the last character of the unescaped string */
264 int len
= lstrlenW(str
);
265 for (str_idx
= 0; str_idx
< len
; str_idx
++, val_idx
++) {
266 if (str
[str_idx
] == '\\') {
268 switch (str
[str_idx
]) {
274 str
[val_idx
] = str
[str_idx
];
277 fprintf(stderr
,"Warning! Unrecognized escape sequence: \\%c'\n",
279 str
[val_idx
] = str
[str_idx
];
283 str
[val_idx
] = str
[str_idx
];
289 static BOOL
parseKeyName(LPWSTR lpKeyName
, HKEY
*hKey
, LPWSTR
*lpKeyPath
)
291 WCHAR
* lpSlash
= NULL
;
294 if (lpKeyName
== NULL
)
297 for(i
= 0; *(lpKeyName
+i
) != 0; i
++)
299 if(*(lpKeyName
+i
) == '\\')
301 lpSlash
= lpKeyName
+i
;
308 len
= lpSlash
-lpKeyName
;
312 len
= lstrlenW(lpKeyName
);
313 lpSlash
= lpKeyName
+len
;
317 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++) {
318 if (CompareStringW(LOCALE_USER_DEFAULT
, 0, lpKeyName
, len
, reg_class_namesW
[i
], len
) == CSTR_EQUAL
&&
319 len
== lstrlenW(reg_class_namesW
[i
])) {
320 *hKey
= reg_class_keys
[i
];
329 if (*lpSlash
!= '\0')
331 *lpKeyPath
= lpSlash
;
335 /* Globals used by the setValue() & co */
336 static LPSTR currentKeyName
;
337 static HKEY currentKeyHandle
= NULL
;
339 /******************************************************************************
340 * Sets the value with name val_name to the data in val_data for the currently
344 * val_name - name of the registry value
345 * val_data - registry value data
347 static LONG
setValue(WCHAR
* val_name
, WCHAR
* val_data
, BOOL is_unicode
)
350 DWORD dwDataType
, dwParseType
;
353 WCHAR del
[] = {'-',0};
355 if ( (val_name
== NULL
) || (val_data
== NULL
) )
356 return ERROR_INVALID_PARAMETER
;
358 if (lstrcmpW(val_data
, del
) == 0)
360 res
=RegDeleteValueW(currentKeyHandle
,val_name
);
361 return (res
== ERROR_FILE_NOT_FOUND
? ERROR_SUCCESS
: res
);
364 /* Get the data type stored into the value field */
365 dwDataType
= getDataType(&val_data
, &dwParseType
);
367 if (dwParseType
== REG_SZ
) /* no conversion for string */
369 REGPROC_unescape_string(val_data
);
370 /* Compute dwLen after REGPROC_unescape_string because it may
371 * have changed the string length and we don't want to store
372 * the extra garbage in the registry.
374 dwLen
= lstrlenW(val_data
);
375 if (dwLen
>0 && val_data
[dwLen
-1]=='"')
378 val_data
[dwLen
]='\0';
380 lpbData
= (BYTE
*) val_data
;
381 dwLen
++; /* include terminating null */
382 dwLen
= dwLen
* sizeof(WCHAR
); /* size is in bytes */
384 else if (dwParseType
== REG_DWORD
) /* Convert the dword types */
386 if (!convertHexToDWord(val_data
, &dwData
))
387 return ERROR_INVALID_DATA
;
388 lpbData
= (BYTE
*)&dwData
;
389 dwLen
= sizeof(dwData
);
391 else if (dwParseType
== REG_BINARY
) /* Convert the binary data */
393 lpbData
= convertHexCSVToHex(val_data
, &dwLen
);
395 return ERROR_INVALID_DATA
;
397 if((dwDataType
== REG_MULTI_SZ
|| dwDataType
== REG_EXPAND_SZ
) && !is_unicode
)
399 LPBYTE tmp
= lpbData
;
400 lpbData
= (LPBYTE
)GetWideStringN((char*)lpbData
, dwLen
, &dwLen
);
401 dwLen
*= sizeof(WCHAR
);
402 HeapFree(GetProcessHeap(), 0, tmp
);
405 else /* unknown format */
407 fprintf(stderr
,"%s: ERROR, unknown data format\n", getAppName());
408 return ERROR_INVALID_DATA
;
411 res
= RegSetValueExW(
418 if (dwParseType
== REG_BINARY
)
419 HeapFree(GetProcessHeap(), 0, lpbData
);
423 /******************************************************************************
424 * A helper function for processRegEntry() that opens the current key.
425 * That key must be closed by calling closeKey().
427 static LONG
openKeyW(WCHAR
* stdInput
)
435 if (stdInput
== NULL
)
436 return ERROR_INVALID_PARAMETER
;
438 /* Get the registry class */
439 if (!parseKeyName(stdInput
, &keyClass
, &keyPath
))
440 return ERROR_INVALID_PARAMETER
;
442 res
= RegCreateKeyExW(
443 keyClass
, /* Class */
444 keyPath
, /* Sub Key */
446 NULL
, /* object type */
447 REG_OPTION_NON_VOLATILE
, /* option, REG_OPTION_NON_VOLATILE ... */
448 KEY_ALL_ACCESS
, /* access mask, KEY_ALL_ACCESS */
449 NULL
, /* security attribute */
450 ¤tKeyHandle
, /* result */
451 &dwDisp
); /* disposition, REG_CREATED_NEW_KEY or
452 REG_OPENED_EXISTING_KEY */
454 if (res
== ERROR_SUCCESS
)
455 currentKeyName
= GetMultiByteString(stdInput
);
457 currentKeyHandle
= NULL
;
463 /******************************************************************************
464 * Close the currently opened key.
466 static void closeKey(void)
468 if (currentKeyHandle
)
470 HeapFree(GetProcessHeap(), 0, currentKeyName
);
471 RegCloseKey(currentKeyHandle
);
472 currentKeyHandle
= NULL
;
476 /******************************************************************************
477 * This function is a wrapper for the setValue function. It prepares the
478 * land and cleans the area once completed.
479 * Note: this function modifies the line parameter.
481 * line - registry file unwrapped line. Should have the registry value name and
482 * complete registry value data.
484 static void processSetValue(WCHAR
* line
, BOOL is_unicode
)
486 WCHAR
* val_name
; /* registry value name */
487 WCHAR
* val_data
; /* registry value data */
488 int line_idx
= 0; /* current character under analysis */
492 while ( isspaceW(line
[line_idx
]) ) line_idx
++;
493 if (line
[line_idx
] == '@' && line
[line_idx
+ 1] == '=') {
494 line
[line_idx
] = '\0';
497 } else if (line
[line_idx
] == '\"') {
499 val_name
= line
+ line_idx
;
501 if (line
[line_idx
] == '\\') /* skip escaped character */
505 if (line
[line_idx
] == '\"') {
506 line
[line_idx
] = '\0';
514 while ( isspaceW(line
[line_idx
]) ) line_idx
++;
515 if (line
[line_idx
] != '=') {
517 line
[line_idx
] = '\"';
518 lineA
= GetMultiByteString(line
);
519 fprintf(stderr
,"Warning! unrecognized line:\n%s\n", lineA
);
520 HeapFree(GetProcessHeap(), 0, lineA
);
525 char* lineA
= GetMultiByteString(line
);
526 fprintf(stderr
,"Warning! unrecognized line:\n%s\n", lineA
);
527 HeapFree(GetProcessHeap(), 0, lineA
);
530 line_idx
++; /* skip the '=' character */
532 while ( isspaceW(line
[line_idx
]) ) line_idx
++;
533 val_data
= line
+ line_idx
;
534 /* trim trailing blanks */
535 line_idx
= strlenW(val_data
);
536 while (line_idx
> 0 && isspaceW(val_data
[line_idx
-1])) line_idx
--;
537 val_data
[line_idx
] = '\0';
539 REGPROC_unescape_string(val_name
);
540 res
= setValue(val_name
, val_data
, is_unicode
);
541 if ( res
!= ERROR_SUCCESS
)
543 char* val_nameA
= GetMultiByteString(val_name
);
544 char* val_dataA
= GetMultiByteString(val_data
);
545 fprintf(stderr
,"%s: ERROR Key %s not created. Value: %s, Data: %s\n",
550 HeapFree(GetProcessHeap(), 0, val_nameA
);
551 HeapFree(GetProcessHeap(), 0, val_dataA
);
555 /******************************************************************************
556 * This function receives the currently read entry and performs the
557 * corresponding action.
558 * isUnicode affects parsing of REG_MULTI_SZ values
560 static void processRegEntry(WCHAR
* stdInput
, BOOL isUnicode
)
563 * We encountered the end of the file, make sure we
564 * close the opened key and exit
566 if (stdInput
== NULL
) {
571 if ( stdInput
[0] == '[') /* We are reading a new key */
574 closeKey(); /* Close the previous key */
576 /* Get rid of the square brackets */
578 keyEnd
= strrchrW(stdInput
, ']');
582 /* delete the key if we encounter '-' at the start of reg key */
583 if ( stdInput
[0] == '-')
585 delete_registry_key(stdInput
+ 1);
586 } else if ( openKeyW(stdInput
) != ERROR_SUCCESS
)
588 char* stdInputA
= GetMultiByteString(stdInput
);
589 fprintf(stderr
,"%s: setValue failed to open key %s\n",
590 getAppName(), stdInputA
);
591 HeapFree(GetProcessHeap(), 0, stdInputA
);
593 } else if( currentKeyHandle
&&
594 (( stdInput
[0] == '@') || /* reading a default @=data pair */
595 ( stdInput
[0] == '\"'))) /* reading a new value=data pair */
597 processSetValue(stdInput
, isUnicode
);
600 /* Since we are assuming that the file format is valid we must be
601 * reading a blank line which indicates the end of this key processing
607 /******************************************************************************
608 * Processes a registry file.
609 * Correctly processes comments (in # form), line continuation.
612 * in - input stream to read from
614 static void processRegLinesA(FILE *in
)
616 LPSTR line
= NULL
; /* line read from input stream */
617 ULONG lineSize
= REG_VAL_BUF_SIZE
;
619 line
= HeapAlloc(GetProcessHeap(), 0, lineSize
);
620 CHECK_ENOUGH_MEMORY(line
);
623 LPSTR s
; /* The pointer into line for where the current fgets should read */
628 size_t size_remaining
;
630 char *s_eol
; /* various local uses */
632 /* Do we need to expand the buffer ? */
633 assert (s
>= line
&& s
<= line
+ lineSize
);
634 size_remaining
= lineSize
- (s
-line
);
635 if (size_remaining
< 2) /* room for 1 character and the \0 */
638 size_t new_size
= lineSize
+ REG_VAL_BUF_SIZE
;
639 if (new_size
> lineSize
) /* no arithmetic overflow */
640 new_buffer
= HeapReAlloc (GetProcessHeap(), 0, line
, new_size
);
643 CHECK_ENOUGH_MEMORY(new_buffer
);
645 s
= line
+ lineSize
- size_remaining
;
647 size_remaining
= lineSize
- (s
-line
);
650 /* Get as much as possible into the buffer, terminated either by
651 * eof, error, eol or getting the maximum amount. Abort on error.
653 size_to_get
= (size_remaining
> INT_MAX
? INT_MAX
: size_remaining
);
655 check
= fgets (s
, size_to_get
, in
);
659 perror ("While reading input");
664 /* It is not clear to me from the definition that the
665 * contents of the buffer are well defined on detecting
666 * an eof without managing to read anything.
671 /* If we didn't read the eol nor the eof go around for the rest */
672 s_eol
= strchr (s
, '\n');
673 if (!feof (in
) && !s_eol
) {
674 s
= strchr (s
, '\0');
675 /* It should be s + size_to_get - 1 but this is safer */
679 /* If it is a comment line then discard it and go around again */
680 if (line
[0] == '#') {
685 /* Remove any line feed. Leave s_eol on the \0 */
688 if (s_eol
> line
&& *(s_eol
-1) == '\r')
691 s_eol
= strchr (s
, '\0');
693 /* If there is a concatenating \\ then go around again */
694 if (s_eol
> line
&& *(s_eol
-1) == '\\') {
701 } while(c
== ' ' || c
== '\t');
705 fprintf(stderr
,"%s: ERROR - invalid continuation.\n",
716 lineW
= GetWideString(line
);
718 break; /* That is the full virtual line */
721 processRegEntry(lineW
, FALSE
);
722 HeapFree(GetProcessHeap(), 0, lineW
);
724 processRegEntry(NULL
, FALSE
);
726 HeapFree(GetProcessHeap(), 0, line
);
729 static void processRegLinesW(FILE *in
)
731 WCHAR
* buf
= NULL
; /* line read from input stream */
732 ULONG lineSize
= REG_VAL_BUF_SIZE
;
733 size_t CharsInBuf
= -1;
735 WCHAR
* s
; /* The pointer into buf for where the current fgets should read */
736 WCHAR
* line
; /* The start of the current line */
738 buf
= HeapAlloc(GetProcessHeap(), 0, lineSize
* sizeof(WCHAR
));
739 CHECK_ENOUGH_MEMORY(buf
);
745 size_t size_remaining
;
747 WCHAR
*s_eol
= NULL
; /* various local uses */
749 /* Do we need to expand the buffer ? */
750 assert (s
>= buf
&& s
<= buf
+ lineSize
);
751 size_remaining
= lineSize
- (s
-buf
);
752 if (size_remaining
< 2) /* room for 1 character and the \0 */
755 size_t new_size
= lineSize
+ (REG_VAL_BUF_SIZE
/ sizeof(WCHAR
));
756 if (new_size
> lineSize
) /* no arithmetic overflow */
757 new_buffer
= HeapReAlloc (GetProcessHeap(), 0, buf
, new_size
* sizeof(WCHAR
));
760 CHECK_ENOUGH_MEMORY(new_buffer
);
763 s
= buf
+ lineSize
- size_remaining
;
765 size_remaining
= lineSize
- (s
-buf
);
768 /* Get as much as possible into the buffer, terminated either by
769 * eof, error or getting the maximum amount. Abort on error.
771 size_to_get
= (size_remaining
> INT_MAX
? INT_MAX
: size_remaining
);
773 CharsInBuf
= fread(s
, sizeof(WCHAR
), size_to_get
- 1, in
);
776 if (CharsInBuf
== 0) {
778 perror ("While reading input");
783 /* It is not clear to me from the definition that the
784 * contents of the buffer are well defined on detecting
785 * an eof without managing to read anything.
790 /* If we didn't read the eol nor the eof go around for the rest */
793 s_eol
= strchrW(line
, '\n');
796 /* Move the stub of the line to the start of the buffer so
797 * we get the maximum space to read into, and so we don't
798 * have to recalculate 'line' if the buffer expands */
799 MoveMemory(buf
, line
, (strlenW(line
)+1) * sizeof(WCHAR
));
801 s
= strchrW(line
, '\0');
805 /* If it is a comment line then discard it and go around again */
811 /* If there is a concatenating \\ then go around again */
812 if ((*(s_eol
-1) == '\\') ||
813 (*(s_eol
-1) == '\r' && *(s_eol
-2) == '\\')) {
814 WCHAR
* NextLine
= s_eol
;
816 while(*(NextLine
+1) == ' ' || *(NextLine
+1) == '\t')
821 if(*(s_eol
-1) == '\r')
824 MoveMemory(s_eol
- 1, NextLine
, (CharsInBuf
- (NextLine
- s
) + 1)*sizeof(WCHAR
));
825 CharsInBuf
-= NextLine
- s_eol
+ 1;
830 /* Remove any line feed. Leave s_eol on the \0 */
833 if (s_eol
> buf
&& *(s_eol
-1) == '\r')
840 processRegEntry(line
, TRUE
);
843 continue; /* That is the full virtual line */
847 processRegEntry(NULL
, TRUE
);
849 HeapFree(GetProcessHeap(), 0, buf
);
852 /****************************************************************************
853 * REGPROC_print_error
855 * Print the message for GetLastError
858 static void REGPROC_print_error(void)
864 error_code
= GetLastError ();
865 status
= FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
866 NULL
, error_code
, 0, (LPTSTR
) &lpMsgBuf
, 0, NULL
);
868 fprintf(stderr
,"%s: Cannot display message for error %d, status %d\n",
869 getAppName(), error_code
, GetLastError());
877 /******************************************************************************
878 * Checks whether the buffer has enough room for the string or required size.
879 * Resizes the buffer if necessary.
882 * buffer - pointer to a buffer for string
883 * len - current length of the buffer in characters.
884 * required_len - length of the string to place to the buffer in characters.
885 * The length does not include the terminating null character.
887 static void REGPROC_resize_char_buffer(WCHAR
**buffer
, DWORD
*len
, DWORD required_len
)
890 if (required_len
> *len
) {
893 *buffer
= HeapAlloc(GetProcessHeap(), 0, *len
* sizeof(**buffer
));
895 *buffer
= HeapReAlloc(GetProcessHeap(), 0, *buffer
, *len
* sizeof(**buffer
));
896 CHECK_ENOUGH_MEMORY(*buffer
);
900 /******************************************************************************
901 * Same as REGPROC_resize_char_buffer() but on a regular buffer.
904 * buffer - pointer to a buffer
905 * len - current size of the buffer in bytes
906 * required_size - size of the data to place in the buffer in bytes
908 static void REGPROC_resize_binary_buffer(BYTE
**buffer
, DWORD
*size
, DWORD required_size
)
910 if (required_size
> *size
) {
911 *size
= required_size
;
913 *buffer
= HeapAlloc(GetProcessHeap(), 0, *size
);
915 *buffer
= HeapReAlloc(GetProcessHeap(), 0, *buffer
, *size
);
916 CHECK_ENOUGH_MEMORY(*buffer
);
920 /******************************************************************************
921 * Prints string str to file
923 static void REGPROC_export_string(WCHAR
**line_buf
, DWORD
*line_buf_size
, DWORD
*line_len
, WCHAR
*str
, DWORD str_len
)
928 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, *line_len
+ str_len
+ 10);
930 /* escaping characters */
932 for (i
= 0; i
< str_len
; i
++) {
937 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, *line_len
+ str_len
+ extra
);
938 (*line_buf
)[pos
++] = '\\';
939 (*line_buf
)[pos
++] = 'n';
945 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, *line_len
+ str_len
+ extra
);
946 (*line_buf
)[pos
++] = '\\';
950 (*line_buf
)[pos
++] = c
;
954 (*line_buf
)[pos
] = '\0';
958 static void REGPROC_export_binary(WCHAR
**line_buf
, DWORD
*line_buf_size
, DWORD
*line_len
, DWORD type
, BYTE
*value
, DWORD value_size
, BOOL unicode
)
960 DWORD hex_pos
, data_pos
;
961 const WCHAR
*hex_prefix
;
962 const WCHAR hex
[] = {'h','e','x',':',0};
964 const WCHAR concat
[] = {'\\','\n',' ',' ',0};
965 DWORD concat_prefix
, concat_len
;
966 const WCHAR newline
[] = {'\n',0};
967 CHAR
* value_multibyte
= NULL
;
969 if (type
== REG_BINARY
) {
972 const WCHAR hex_format
[] = {'h','e','x','(','%','u',')',':',0};
973 hex_prefix
= hex_buf
;
974 sprintfW(hex_buf
, hex_format
, type
);
975 if ((type
== REG_SZ
|| type
== REG_EXPAND_SZ
|| type
== REG_MULTI_SZ
) && !unicode
)
977 value_multibyte
= GetMultiByteStringN((WCHAR
*)value
, value_size
/ sizeof(WCHAR
), &value_size
);
978 value
= (BYTE
*)value_multibyte
;
982 concat_len
= lstrlenW(concat
);
986 *line_len
+= lstrlenW(hex_prefix
);
987 data_pos
= *line_len
;
988 *line_len
+= value_size
* 3;
989 /* - The 2 spaces that concat places at the start of the
990 * line effectively reduce the space available for data.
991 * - If the value name and hex prefix are very long
992 * ( > REG_FILE_HEX_LINE_LEN) then we may overestimate
993 * the needed number of lines by one. But that's ok.
994 * - The trailing linefeed takes the place of a comma so
995 * it's accounted for already.
997 *line_len
+= *line_len
/ (REG_FILE_HEX_LINE_LEN
- concat_prefix
) * concat_len
;
998 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, *line_len
);
999 lstrcpyW(*line_buf
+ hex_pos
, hex_prefix
);
1002 const WCHAR format
[] = {'%','0','2','x',0};
1005 column
= data_pos
; /* no line wrap yet */
1009 sprintfW(*line_buf
+ data_pos
, format
, (unsigned int)value
[i
]);
1011 if (++i
== value_size
)
1014 (*line_buf
)[data_pos
++] = ',';
1018 if (column
>= REG_FILE_HEX_LINE_LEN
) {
1019 lstrcpyW(*line_buf
+ data_pos
, concat
);
1020 data_pos
+= concat_len
;
1021 column
= concat_prefix
;
1025 lstrcpyW(*line_buf
+ data_pos
, newline
);
1026 HeapFree(GetProcessHeap(), 0, value_multibyte
);
1029 /******************************************************************************
1030 * Writes the given line to a file, in multi-byte or wide characters
1032 static void REGPROC_write_line(FILE *file
, const WCHAR
* str
, BOOL unicode
)
1036 fwrite(str
, sizeof(WCHAR
), lstrlenW(str
), file
);
1039 char* strA
= GetMultiByteString(str
);
1041 HeapFree(GetProcessHeap(), 0, strA
);
1045 /******************************************************************************
1046 * Writes contents of the registry key to the specified file stream.
1049 * file - writable file stream to export registry branch to.
1050 * key - registry branch to export.
1051 * reg_key_name_buf - name of the key with registry class.
1052 * Is resized if necessary.
1053 * reg_key_name_size - length of the buffer for the registry class in characters.
1054 * val_name_buf - buffer for storing value name.
1055 * Is resized if necessary.
1056 * val_name_size - length of the buffer for storing value names in characters.
1057 * val_buf - buffer for storing values while extracting.
1058 * Is resized if necessary.
1059 * val_size - size of the buffer for storing values in bytes.
1061 static void export_hkey(FILE *file
, HKEY key
,
1062 WCHAR
**reg_key_name_buf
, DWORD
*reg_key_name_size
,
1063 WCHAR
**val_name_buf
, DWORD
*val_name_size
,
1064 BYTE
**val_buf
, DWORD
*val_size
,
1065 WCHAR
**line_buf
, DWORD
*line_buf_size
,
1068 DWORD max_sub_key_len
;
1069 DWORD max_val_name_len
;
1075 WCHAR key_format
[] = {'\n','[','%','s',']','\n',0};
1077 /* get size information and resize the buffers if necessary */
1078 if (RegQueryInfoKeyW(key
, NULL
, NULL
, NULL
, NULL
,
1079 &max_sub_key_len
, NULL
,
1080 NULL
, &max_val_name_len
, &max_val_size
, NULL
, NULL
1081 ) != ERROR_SUCCESS
) {
1082 REGPROC_print_error();
1084 curr_len
= strlenW(*reg_key_name_buf
);
1085 REGPROC_resize_char_buffer(reg_key_name_buf
, reg_key_name_size
,
1086 max_sub_key_len
+ curr_len
+ 1);
1087 REGPROC_resize_char_buffer(val_name_buf
, val_name_size
,
1089 REGPROC_resize_binary_buffer(val_buf
, val_size
, max_val_size
);
1090 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, lstrlenW(*reg_key_name_buf
) + 4);
1091 /* output data for the current key */
1092 sprintfW(*line_buf
, key_format
, *reg_key_name_buf
);
1093 REGPROC_write_line(file
, *line_buf
, unicode
);
1095 /* print all the values */
1100 DWORD val_name_size1
= *val_name_size
;
1101 DWORD val_size1
= *val_size
;
1102 ret
= RegEnumValueW(key
, i
, *val_name_buf
, &val_name_size1
, NULL
,
1103 &value_type
, *val_buf
, &val_size1
);
1104 if (ret
== ERROR_MORE_DATA
) {
1105 /* Increase the size of the buffers and retry */
1106 REGPROC_resize_char_buffer(val_name_buf
, val_name_size
, val_name_size1
);
1107 REGPROC_resize_binary_buffer(val_buf
, val_size
, val_size1
);
1108 } else if (ret
!= ERROR_SUCCESS
) {
1110 if (ret
!= ERROR_NO_MORE_ITEMS
) {
1111 REGPROC_print_error();
1117 if ((*val_name_buf
)[0]) {
1118 const WCHAR val_start
[] = {'"','%','s','"','=',0};
1120 line_len
= 3 + lstrlenW(*val_name_buf
);
1121 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
);
1122 sprintfW(*line_buf
, val_start
, *val_name_buf
);
1124 const WCHAR std_val
[] = {'@','=',0};
1126 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
);
1127 lstrcpyW(*line_buf
, std_val
);
1130 switch (value_type
) {
1133 WCHAR
* wstr
= (WCHAR
*)*val_buf
;
1135 if (val_size1
< sizeof(WCHAR
) || val_size1
% sizeof(WCHAR
) ||
1136 wstr
[val_size1
/ sizeof(WCHAR
) - 1]) {
1137 REGPROC_export_binary(line_buf
, line_buf_size
, &line_len
, value_type
, *val_buf
, val_size1
, unicode
);
1139 const WCHAR start
[] = {'"',0};
1140 const WCHAR end
[] = {'"','\n',0};
1143 len
= lstrlenW(start
);
1144 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
+ len
);
1145 lstrcpyW(*line_buf
+ line_len
, start
);
1148 /* At this point we know wstr is '\0'-terminated
1149 * so we can substract 1 from the size
1151 REGPROC_export_string(line_buf
, line_buf_size
, &line_len
, wstr
, val_size1
/ sizeof(WCHAR
) - 1);
1153 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
+ lstrlenW(end
));
1154 lstrcpyW(*line_buf
+ line_len
, end
);
1161 WCHAR format
[] = {'d','w','o','r','d',':','%','0','8','x','\n',0};
1163 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
+ 15);
1164 sprintfW(*line_buf
+ line_len
, format
, *((DWORD
*)*val_buf
));
1170 char* key_nameA
= GetMultiByteString(*reg_key_name_buf
);
1171 char* value_nameA
= GetMultiByteString(*val_name_buf
);
1172 fprintf(stderr
,"%s: warning - unsupported registry format '%d', "
1173 "treat as binary\n",
1174 getAppName(), value_type
);
1175 fprintf(stderr
,"key name: \"%s\"\n", key_nameA
);
1176 fprintf(stderr
,"value name:\"%s\"\n\n", value_nameA
);
1177 HeapFree(GetProcessHeap(), 0, key_nameA
);
1178 HeapFree(GetProcessHeap(), 0, value_nameA
);
1185 REGPROC_export_binary(line_buf
, line_buf_size
, &line_len
, value_type
, *val_buf
, val_size1
, unicode
);
1187 REGPROC_write_line(file
, *line_buf
, unicode
);
1193 (*reg_key_name_buf
)[curr_len
] = '\\';
1195 DWORD buf_size
= *reg_key_name_size
- curr_len
- 1;
1197 ret
= RegEnumKeyExW(key
, i
, *reg_key_name_buf
+ curr_len
+ 1, &buf_size
,
1198 NULL
, NULL
, NULL
, NULL
);
1199 if (ret
== ERROR_MORE_DATA
) {
1200 /* Increase the size of the buffer and retry */
1201 REGPROC_resize_char_buffer(reg_key_name_buf
, reg_key_name_size
, curr_len
+ 1 + buf_size
);
1202 } else if (ret
!= ERROR_SUCCESS
) {
1204 if (ret
!= ERROR_NO_MORE_ITEMS
) {
1205 REGPROC_print_error();
1211 if (RegOpenKeyW(key
, *reg_key_name_buf
+ curr_len
+ 1,
1212 &subkey
) == ERROR_SUCCESS
) {
1213 export_hkey(file
, subkey
, reg_key_name_buf
, reg_key_name_size
,
1214 val_name_buf
, val_name_size
, val_buf
, val_size
,
1215 line_buf
, line_buf_size
, unicode
);
1216 RegCloseKey(subkey
);
1218 REGPROC_print_error();
1222 (*reg_key_name_buf
)[curr_len
] = '\0';
1225 /******************************************************************************
1226 * Open file for export.
1228 static FILE *REGPROC_open_export_file(WCHAR
*file_name
, BOOL unicode
)
1233 if (strncmpW(file_name
,&dash
,1)==0)
1237 CHAR
* file_nameA
= GetMultiByteString(file_name
);
1238 file
= fopen(file_nameA
, "w");
1241 fprintf(stderr
,"%s: Can't open file \"%s\"\n", getAppName(), file_nameA
);
1242 HeapFree(GetProcessHeap(), 0, file_nameA
);
1245 HeapFree(GetProcessHeap(), 0, file_nameA
);
1249 const BYTE unicode_seq
[] = {0xff,0xfe};
1250 const WCHAR header
[] = {'W','i','n','d','o','w','s',' ','R','e','g','i','s','t','r','y',' ','E','d','i','t','o','r',' ','V','e','r','s','i','o','n',' ','5','.','0','0','\n'};
1251 fwrite(unicode_seq
, sizeof(BYTE
), sizeof(unicode_seq
)/sizeof(unicode_seq
[0]), file
);
1252 fwrite(header
, sizeof(WCHAR
), sizeof(header
)/sizeof(header
[0]), file
);
1255 fputs("REGEDIT4\n", file
);
1261 /******************************************************************************
1262 * Writes contents of the registry key to the specified file stream.
1265 * file_name - name of a file to export registry branch to.
1266 * reg_key_name - registry branch to export. The whole registry is exported if
1267 * reg_key_name is NULL or contains an empty string.
1269 BOOL
export_registry_key(WCHAR
*file_name
, WCHAR
*reg_key_name
, DWORD format
)
1271 WCHAR
*reg_key_name_buf
;
1272 WCHAR
*val_name_buf
;
1275 DWORD reg_key_name_size
= KEY_MAX_LEN
;
1276 DWORD val_name_size
= KEY_MAX_LEN
;
1277 DWORD val_size
= REG_VAL_BUF_SIZE
;
1278 DWORD line_buf_size
= KEY_MAX_LEN
+ REG_VAL_BUF_SIZE
;
1280 BOOL unicode
= (format
== REG_FORMAT_5
);
1282 reg_key_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1283 reg_key_name_size
* sizeof(*reg_key_name_buf
));
1284 val_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1285 val_name_size
* sizeof(*val_name_buf
));
1286 val_buf
= HeapAlloc(GetProcessHeap(), 0, val_size
);
1287 line_buf
= HeapAlloc(GetProcessHeap(), 0, line_buf_size
* sizeof(*line_buf
));
1288 CHECK_ENOUGH_MEMORY(reg_key_name_buf
&& val_name_buf
&& val_buf
&& line_buf
);
1290 if (reg_key_name
&& reg_key_name
[0]) {
1292 WCHAR
*branch_name
= NULL
;
1295 REGPROC_resize_char_buffer(®_key_name_buf
, ®_key_name_size
,
1296 lstrlenW(reg_key_name
));
1297 lstrcpyW(reg_key_name_buf
, reg_key_name
);
1299 /* open the specified key */
1300 if (!parseKeyName(reg_key_name
, ®_key_class
, &branch_name
)) {
1301 CHAR
* key_nameA
= GetMultiByteString(reg_key_name
);
1302 fprintf(stderr
,"%s: Incorrect registry class specification in '%s'\n",
1303 getAppName(), key_nameA
);
1304 HeapFree(GetProcessHeap(), 0, key_nameA
);
1307 if (!branch_name
[0]) {
1308 /* no branch - registry class is specified */
1309 file
= REGPROC_open_export_file(file_name
, unicode
);
1310 export_hkey(file
, reg_key_class
,
1311 ®_key_name_buf
, ®_key_name_size
,
1312 &val_name_buf
, &val_name_size
,
1313 &val_buf
, &val_size
, &line_buf
,
1314 &line_buf_size
, unicode
);
1315 } else if (RegOpenKeyW(reg_key_class
, branch_name
, &key
) == ERROR_SUCCESS
) {
1316 file
= REGPROC_open_export_file(file_name
, unicode
);
1317 export_hkey(file
, key
,
1318 ®_key_name_buf
, ®_key_name_size
,
1319 &val_name_buf
, &val_name_size
,
1320 &val_buf
, &val_size
, &line_buf
,
1321 &line_buf_size
, unicode
);
1324 CHAR
* key_nameA
= GetMultiByteString(reg_key_name
);
1325 fprintf(stderr
,"%s: Can't export. Registry key '%s' does not exist!\n",
1326 getAppName(), key_nameA
);
1327 HeapFree(GetProcessHeap(), 0, key_nameA
);
1328 REGPROC_print_error();
1333 /* export all registry classes */
1334 file
= REGPROC_open_export_file(file_name
, unicode
);
1335 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++) {
1336 /* do not export HKEY_CLASSES_ROOT */
1337 if (reg_class_keys
[i
] != HKEY_CLASSES_ROOT
&&
1338 reg_class_keys
[i
] != HKEY_CURRENT_USER
&&
1339 reg_class_keys
[i
] != HKEY_CURRENT_CONFIG
&&
1340 reg_class_keys
[i
] != HKEY_DYN_DATA
) {
1341 lstrcpyW(reg_key_name_buf
, reg_class_namesW
[i
]);
1342 export_hkey(file
, reg_class_keys
[i
],
1343 ®_key_name_buf
, ®_key_name_size
,
1344 &val_name_buf
, &val_name_size
,
1345 &val_buf
, &val_size
, &line_buf
,
1346 &line_buf_size
, unicode
);
1354 HeapFree(GetProcessHeap(), 0, reg_key_name
);
1355 HeapFree(GetProcessHeap(), 0, val_name_buf
);
1356 HeapFree(GetProcessHeap(), 0, val_buf
);
1357 HeapFree(GetProcessHeap(), 0, line_buf
);
1361 /******************************************************************************
1362 * Reads contents of the specified file into the registry.
1364 BOOL
import_registry_file(FILE* reg_file
)
1369 if (fread( s
, 2, 1, reg_file
) == 1)
1371 if (s
[0] == 0xff && s
[1] == 0xfe)
1373 processRegLinesW(reg_file
);
1377 processRegLinesA(reg_file
);
1385 /******************************************************************************
1386 * Removes the registry key with all subkeys. Parses full key name.
1389 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1390 * empty, points to register key class, does not exist.
1392 void delete_registry_key(WCHAR
*reg_key_name
)
1394 WCHAR
*key_name
= NULL
;
1397 if (!reg_key_name
|| !reg_key_name
[0])
1400 if (!parseKeyName(reg_key_name
, &key_class
, &key_name
)) {
1401 char* reg_key_nameA
= GetMultiByteString(reg_key_name
);
1402 fprintf(stderr
,"%s: Incorrect registry class specification in '%s'\n",
1403 getAppName(), reg_key_nameA
);
1404 HeapFree(GetProcessHeap(), 0, reg_key_nameA
);
1408 char* reg_key_nameA
= GetMultiByteString(reg_key_name
);
1409 fprintf(stderr
,"%s: Can't delete registry class '%s'\n",
1410 getAppName(), reg_key_nameA
);
1411 HeapFree(GetProcessHeap(), 0, reg_key_nameA
);
1415 RegDeleteTreeW(key_class
, key_name
);