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 not including '\' character */
37 #define REG_FILE_HEX_LINE_LEN 76
39 static const CHAR
*reg_class_names
[] = {
40 "HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_CLASSES_ROOT",
41 "HKEY_CURRENT_CONFIG", "HKEY_CURRENT_USER", "HKEY_DYN_DATA"
44 #define REG_CLASS_NUMBER (sizeof(reg_class_names) / sizeof(reg_class_names[0]))
46 extern const WCHAR
* reg_class_namesW
[];
48 static HKEY reg_class_keys
[REG_CLASS_NUMBER
] = {
49 HKEY_LOCAL_MACHINE
, HKEY_USERS
, HKEY_CLASSES_ROOT
,
50 HKEY_CURRENT_CONFIG
, HKEY_CURRENT_USER
, HKEY_DYN_DATA
54 #define NOT_ENOUGH_MEMORY 1
57 /* processing macros */
59 /* common check of memory allocation results */
60 #define CHECK_ENOUGH_MEMORY(p) \
63 fprintf(stderr,"%s: file %s, line %d: Not enough memory\n", \
64 getAppName(), __FILE__, __LINE__); \
65 exit(NOT_ENOUGH_MEMORY); \
68 /******************************************************************************
69 * Allocates memory and convers input from multibyte to wide chars
70 * Returned string must be freed by the caller
72 WCHAR
* GetWideString(const char* strA
)
77 int len
= MultiByteToWideChar(CP_ACP
, 0, strA
, -1, NULL
, 0);
79 strW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
80 CHECK_ENOUGH_MEMORY(strW
);
81 MultiByteToWideChar(CP_ACP
, 0, strA
, -1, strW
, len
);
87 /******************************************************************************
88 * Allocates memory and convers input from multibyte to wide chars
89 * Returned string must be freed by the caller
91 WCHAR
* GetWideStringN(const char* strA
, int chars
, DWORD
*len
)
96 *len
= MultiByteToWideChar(CP_ACP
, 0, strA
, chars
, NULL
, 0);
98 strW
= HeapAlloc(GetProcessHeap(), 0, *len
* sizeof(WCHAR
));
99 CHECK_ENOUGH_MEMORY(strW
);
100 MultiByteToWideChar(CP_ACP
, 0, strA
, chars
, strW
, *len
);
107 /******************************************************************************
108 * Allocates memory and convers input from wide chars to multibyte
109 * Returned string must be freed by the caller
111 char* GetMultiByteString(const WCHAR
* strW
)
116 int len
= WideCharToMultiByte(CP_ACP
, 0, strW
, -1, NULL
, 0, NULL
, NULL
);
118 strA
= HeapAlloc(GetProcessHeap(), 0, len
);
119 CHECK_ENOUGH_MEMORY(strA
);
120 WideCharToMultiByte(CP_ACP
, 0, strW
, -1, strA
, len
, NULL
, NULL
);
126 /******************************************************************************
127 * Allocates memory and convers input from wide chars to multibyte
128 * Returned string must be freed by the caller
130 char* GetMultiByteStringN(const WCHAR
* strW
, int chars
, DWORD
* len
)
135 *len
= WideCharToMultiByte(CP_ACP
, 0, strW
, chars
, NULL
, 0, NULL
, NULL
);
137 strA
= HeapAlloc(GetProcessHeap(), 0, *len
);
138 CHECK_ENOUGH_MEMORY(strA
);
139 WideCharToMultiByte(CP_ACP
, 0, strW
, chars
, strA
, *len
, NULL
, NULL
);
146 /******************************************************************************
147 * Converts a hex representation of a DWORD into a DWORD.
149 static BOOL
convertHexToDWord(WCHAR
* str
, DWORD
*dw
)
154 WideCharToMultiByte(CP_ACP
, 0, str
, -1, buf
, 9, NULL
, NULL
);
155 if (lstrlenW(str
) > 8 || sscanf(buf
, "%x%c", dw
, &dummy
) != 1) {
156 fprintf(stderr
,"%s: ERROR, invalid hex value\n", getAppName());
162 /******************************************************************************
163 * Converts a hex comma separated values list into a binary string.
165 static BYTE
* convertHexCSVToHex(WCHAR
*str
, DWORD
*size
)
170 /* The worst case is 1 digit + 1 comma per byte */
171 *size
=(lstrlenW(str
)+1)/2;
172 data
=HeapAlloc(GetProcessHeap(), 0, *size
);
173 CHECK_ENOUGH_MEMORY(data
);
182 wc
= strtoulW(s
,&end
,16);
183 if (end
== s
|| wc
> 0xff || (*end
&& *end
!= ',')) {
184 char* strA
= GetMultiByteString(s
);
185 fprintf(stderr
,"%s: ERROR converting CSV hex stream. Invalid value at '%s'\n",
187 HeapFree(GetProcessHeap(), 0, data
);
188 HeapFree(GetProcessHeap(), 0, strA
);
200 /******************************************************************************
201 * This function returns the HKEY associated with the data type encoded in the
202 * value. It modifies the input parameter (key value) in order to skip this
203 * "now useless" data type information.
205 * Note: Updated based on the algorithm used in 'server/registry.c'
207 static DWORD
getDataType(LPWSTR
*lpValue
, DWORD
* parse_type
)
209 struct data_type
{ const WCHAR
*tag
; int len
; int type
; int parse_type
; };
211 static const WCHAR quote
[] = {'"'};
212 static const WCHAR str
[] = {'s','t','r',':','"'};
213 static const WCHAR str2
[] = {'s','t','r','(','2',')',':','"'};
214 static const WCHAR hex
[] = {'h','e','x',':'};
215 static const WCHAR dword
[] = {'d','w','o','r','d',':'};
216 static const WCHAR hexp
[] = {'h','e','x','('};
218 static const struct data_type data_types
[] = { /* actual type */ /* type to assume for parsing */
219 { quote
, 1, REG_SZ
, REG_SZ
},
220 { str
, 5, REG_SZ
, REG_SZ
},
221 { str2
, 8, REG_EXPAND_SZ
, REG_SZ
},
222 { hex
, 4, REG_BINARY
, REG_BINARY
},
223 { dword
, 6, REG_DWORD
, REG_DWORD
},
224 { hexp
, 4, -1, REG_BINARY
},
228 const struct data_type
*ptr
;
231 for (ptr
= data_types
; ptr
->tag
; ptr
++) {
232 if (strncmpW( ptr
->tag
, *lpValue
, ptr
->len
))
236 *parse_type
= ptr
->parse_type
;
242 /* "hex(xx):" is special */
243 type
= (int)strtoulW( *lpValue
, &end
, 16 );
244 if (**lpValue
=='\0' || *end
!=')' || *(end
+1)!=':') {
252 *parse_type
=REG_NONE
;
256 /******************************************************************************
257 * Replaces escape sequences with the characters.
259 static void REGPROC_unescape_string(WCHAR
* str
)
261 int str_idx
= 0; /* current character under analysis */
262 int val_idx
= 0; /* the last character of the unescaped string */
263 int len
= lstrlenW(str
);
264 for (str_idx
= 0; str_idx
< len
; str_idx
++, val_idx
++) {
265 if (str
[str_idx
] == '\\') {
267 switch (str
[str_idx
]) {
273 str
[val_idx
] = str
[str_idx
];
276 fprintf(stderr
,"Warning! Unrecognized escape sequence: \\%c'\n",
278 str
[val_idx
] = str
[str_idx
];
282 str
[val_idx
] = str
[str_idx
];
288 static BOOL
parseKeyName(LPWSTR lpKeyName
, HKEY
*hKey
, LPWSTR
*lpKeyPath
)
290 WCHAR
* lpSlash
= NULL
;
293 if (lpKeyName
== NULL
)
296 for(i
= 0; *(lpKeyName
+i
) != 0; i
++)
298 if(*(lpKeyName
+i
) == '\\')
300 lpSlash
= lpKeyName
+i
;
307 len
= lpSlash
-lpKeyName
;
311 len
= lstrlenW(lpKeyName
);
312 lpSlash
= lpKeyName
+len
;
316 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++) {
317 if (CompareStringW(LOCALE_USER_DEFAULT
, 0, lpKeyName
, len
, reg_class_namesW
[i
], len
) == CSTR_EQUAL
&&
318 len
== lstrlenW(reg_class_namesW
[i
])) {
319 *hKey
= reg_class_keys
[i
];
328 if (*lpSlash
!= '\0')
330 *lpKeyPath
= lpSlash
;
334 /* Globals used by the setValue() & co */
335 static LPSTR currentKeyName
;
336 static HKEY currentKeyHandle
= NULL
;
338 /******************************************************************************
339 * Sets the value with name val_name to the data in val_data for the currently
343 * val_name - name of the registry value
344 * val_data - registry value data
346 static LONG
setValue(WCHAR
* val_name
, WCHAR
* val_data
, BOOL is_unicode
)
349 DWORD dwDataType
, dwParseType
;
352 WCHAR del
[] = {'-',0};
354 if ( (val_name
== NULL
) || (val_data
== NULL
) )
355 return ERROR_INVALID_PARAMETER
;
357 if (lstrcmpW(val_data
, del
) == 0)
359 res
=RegDeleteValueW(currentKeyHandle
,val_name
);
360 return (res
== ERROR_FILE_NOT_FOUND
? ERROR_SUCCESS
: res
);
363 /* Get the data type stored into the value field */
364 dwDataType
= getDataType(&val_data
, &dwParseType
);
366 if (dwParseType
== REG_SZ
) /* no conversion for string */
368 REGPROC_unescape_string(val_data
);
369 /* Compute dwLen after REGPROC_unescape_string because it may
370 * have changed the string length and we don't want to store
371 * the extra garbage in the registry.
373 dwLen
= lstrlenW(val_data
);
374 if (dwLen
>0 && val_data
[dwLen
-1]=='"')
377 val_data
[dwLen
]='\0';
379 lpbData
= (BYTE
*) val_data
;
380 dwLen
++; /* include terminating null */
381 dwLen
= dwLen
* sizeof(WCHAR
); /* size is in bytes */
383 else if (dwParseType
== REG_DWORD
) /* Convert the dword types */
385 if (!convertHexToDWord(val_data
, &dwData
))
386 return ERROR_INVALID_DATA
;
387 lpbData
= (BYTE
*)&dwData
;
388 dwLen
= sizeof(dwData
);
390 else if (dwParseType
== REG_BINARY
) /* Convert the binary data */
392 lpbData
= convertHexCSVToHex(val_data
, &dwLen
);
394 return ERROR_INVALID_DATA
;
396 if(dwDataType
== REG_MULTI_SZ
&& !is_unicode
)
398 LPBYTE tmp
= lpbData
;
399 lpbData
= (LPBYTE
)GetWideStringN((char*)lpbData
, dwLen
, &dwLen
);
400 dwLen
*= sizeof(WCHAR
);
401 HeapFree(GetProcessHeap(), 0, tmp
);
404 else /* unknown format */
406 fprintf(stderr
,"%s: ERROR, unknown data format\n", getAppName());
407 return ERROR_INVALID_DATA
;
410 res
= RegSetValueExW(
417 if (dwParseType
== REG_BINARY
)
418 HeapFree(GetProcessHeap(), 0, lpbData
);
422 /******************************************************************************
423 * A helper function for processRegEntry() that opens the current key.
424 * That key must be closed by calling closeKey().
426 static LONG
openKeyW(WCHAR
* stdInput
)
434 if (stdInput
== NULL
)
435 return ERROR_INVALID_PARAMETER
;
437 /* Get the registry class */
438 if (!parseKeyName(stdInput
, &keyClass
, &keyPath
))
439 return ERROR_INVALID_PARAMETER
;
441 res
= RegCreateKeyExW(
442 keyClass
, /* Class */
443 keyPath
, /* Sub Key */
445 NULL
, /* object type */
446 REG_OPTION_NON_VOLATILE
, /* option, REG_OPTION_NON_VOLATILE ... */
447 KEY_ALL_ACCESS
, /* access mask, KEY_ALL_ACCESS */
448 NULL
, /* security attribute */
449 ¤tKeyHandle
, /* result */
450 &dwDisp
); /* disposition, REG_CREATED_NEW_KEY or
451 REG_OPENED_EXISTING_KEY */
453 if (res
== ERROR_SUCCESS
)
454 currentKeyName
= GetMultiByteString(stdInput
);
456 currentKeyHandle
= NULL
;
462 /******************************************************************************
463 * Close the currently opened key.
465 static void closeKey(void)
467 if (currentKeyHandle
)
469 HeapFree(GetProcessHeap(), 0, currentKeyName
);
470 RegCloseKey(currentKeyHandle
);
471 currentKeyHandle
= NULL
;
475 /******************************************************************************
476 * This function is a wrapper for the setValue function. It prepares the
477 * land and cleans the area once completed.
478 * Note: this function modifies the line parameter.
480 * line - registry file unwrapped line. Should have the registry value name and
481 * complete registry value data.
483 static void processSetValue(WCHAR
* line
, BOOL is_unicode
)
485 WCHAR
* val_name
; /* registry value name */
486 WCHAR
* val_data
; /* registry value data */
487 int line_idx
= 0; /* current character under analysis */
491 if (line
[line_idx
] == '@' && line
[line_idx
+ 1] == '=') {
492 line
[line_idx
] = '\0';
495 } else if (line
[line_idx
] == '\"') {
497 val_name
= line
+ line_idx
;
499 if (line
[line_idx
] == '\\') /* skip escaped character */
503 if (line
[line_idx
] == '\"') {
504 line
[line_idx
] = '\0';
512 if (line
[line_idx
] != '=') {
514 line
[line_idx
] = '\"';
515 lineA
= GetMultiByteString(line
);
516 fprintf(stderr
,"Warning! unrecognized line:\n%s\n", lineA
);
517 HeapFree(GetProcessHeap(), 0, lineA
);
522 char* lineA
= GetMultiByteString(line
);
523 fprintf(stderr
,"Warning! unrecognized line:\n%s\n", lineA
);
524 HeapFree(GetProcessHeap(), 0, lineA
);
527 line_idx
++; /* skip the '=' character */
528 val_data
= line
+ line_idx
;
530 REGPROC_unescape_string(val_name
);
531 res
= setValue(val_name
, val_data
, is_unicode
);
532 if ( res
!= ERROR_SUCCESS
)
534 char* val_nameA
= GetMultiByteString(val_name
);
535 char* val_dataA
= GetMultiByteString(val_data
);
536 fprintf(stderr
,"%s: ERROR Key %s not created. Value: %s, Data: %s\n",
541 HeapFree(GetProcessHeap(), 0, val_nameA
);
542 HeapFree(GetProcessHeap(), 0, val_dataA
);
546 /******************************************************************************
547 * This function receives the currently read entry and performs the
548 * corresponding action.
549 * isUnicode affects parsing of REG_MULTI_SZ values
551 static void processRegEntry(WCHAR
* stdInput
, BOOL isUnicode
)
554 * We encountered the end of the file, make sure we
555 * close the opened key and exit
557 if (stdInput
== NULL
) {
562 if ( stdInput
[0] == '[') /* We are reading a new key */
565 closeKey(); /* Close the previous key */
567 /* Get rid of the square brackets */
569 keyEnd
= strrchrW(stdInput
, ']');
573 /* delete the key if we encounter '-' at the start of reg key */
574 if ( stdInput
[0] == '-')
576 delete_registry_key(stdInput
+ 1);
577 } else if ( openKeyW(stdInput
) != ERROR_SUCCESS
)
579 char* stdInputA
= GetMultiByteString(stdInput
);
580 fprintf(stderr
,"%s: setValue failed to open key %s\n",
581 getAppName(), stdInputA
);
582 HeapFree(GetProcessHeap(), 0, stdInputA
);
584 } else if( currentKeyHandle
&&
585 (( stdInput
[0] == '@') || /* reading a default @=data pair */
586 ( stdInput
[0] == '\"'))) /* reading a new value=data pair */
588 processSetValue(stdInput
, isUnicode
);
591 /* Since we are assuming that the file format is valid we must be
592 * reading a blank line which indicates the end of this key processing
598 /******************************************************************************
599 * Processes a registry file.
600 * Correctly processes comments (in # form), line continuation.
603 * in - input stream to read from
605 void processRegLinesA(FILE *in
)
607 LPSTR line
= NULL
; /* line read from input stream */
608 ULONG lineSize
= REG_VAL_BUF_SIZE
;
610 line
= HeapAlloc(GetProcessHeap(), 0, lineSize
);
611 CHECK_ENOUGH_MEMORY(line
);
614 LPSTR s
; /* The pointer into line for where the current fgets should read */
619 size_t size_remaining
;
621 char *s_eol
; /* various local uses */
623 /* Do we need to expand the buffer ? */
624 assert (s
>= line
&& s
<= line
+ lineSize
);
625 size_remaining
= lineSize
- (s
-line
);
626 if (size_remaining
< 2) /* room for 1 character and the \0 */
629 size_t new_size
= lineSize
+ REG_VAL_BUF_SIZE
;
630 if (new_size
> lineSize
) /* no arithmetic overflow */
631 new_buffer
= HeapReAlloc (GetProcessHeap(), 0, line
, new_size
);
634 CHECK_ENOUGH_MEMORY(new_buffer
);
636 s
= line
+ lineSize
- size_remaining
;
638 size_remaining
= lineSize
- (s
-line
);
641 /* Get as much as possible into the buffer, terminated either by
642 * eof, error, eol or getting the maximum amount. Abort on error.
644 size_to_get
= (size_remaining
> INT_MAX
? INT_MAX
: size_remaining
);
646 check
= fgets (s
, size_to_get
, in
);
650 perror ("While reading input");
655 /* It is not clear to me from the definition that the
656 * contents of the buffer are well defined on detecting
657 * an eof without managing to read anything.
662 /* If we didn't read the eol nor the eof go around for the rest */
663 s_eol
= strchr (s
, '\n');
664 if (!feof (in
) && !s_eol
) {
665 s
= strchr (s
, '\0');
666 /* It should be s + size_to_get - 1 but this is safer */
670 /* If it is a comment line then discard it and go around again */
671 if (line
[0] == '#') {
676 /* Remove any line feed. Leave s_eol on the \0 */
679 if (s_eol
> line
&& *(s_eol
-1) == '\r')
682 s_eol
= strchr (s
, '\0');
684 /* If there is a concatenating \\ then go around again */
685 if (s_eol
> line
&& *(s_eol
-1) == '\\') {
692 } while(c
== ' ' || c
== '\t');
696 fprintf(stderr
,"%s: ERROR - invalid continuation.\n",
707 lineW
= GetWideString(line
);
709 break; /* That is the full virtual line */
712 processRegEntry(lineW
, FALSE
);
713 HeapFree(GetProcessHeap(), 0, lineW
);
715 processRegEntry(NULL
, FALSE
);
717 HeapFree(GetProcessHeap(), 0, line
);
720 void processRegLinesW(FILE *in
)
722 WCHAR
* buf
= NULL
; /* line read from input stream */
723 ULONG lineSize
= REG_VAL_BUF_SIZE
;
724 size_t CharsInBuf
= -1;
726 WCHAR
* s
; /* The pointer into line for where the current fgets should read */
728 buf
= HeapAlloc(GetProcessHeap(), 0, lineSize
* sizeof(WCHAR
));
729 CHECK_ENOUGH_MEMORY(buf
);
734 size_t size_remaining
;
736 WCHAR
*s_eol
= NULL
; /* various local uses */
738 /* Do we need to expand the buffer ? */
739 assert (s
>= buf
&& s
<= buf
+ lineSize
);
740 size_remaining
= lineSize
- (s
-buf
);
741 if (size_remaining
< 2) /* room for 1 character and the \0 */
744 size_t new_size
= lineSize
+ (REG_VAL_BUF_SIZE
/ sizeof(WCHAR
));
745 if (new_size
> lineSize
) /* no arithmetic overflow */
746 new_buffer
= HeapReAlloc (GetProcessHeap(), 0, buf
, new_size
* sizeof(WCHAR
));
749 CHECK_ENOUGH_MEMORY(new_buffer
);
751 s
= buf
+ lineSize
- size_remaining
;
753 size_remaining
= lineSize
- (s
-buf
);
756 /* Get as much as possible into the buffer, terminated either by
757 * eof, error or getting the maximum amount. Abort on error.
759 size_to_get
= (size_remaining
> INT_MAX
? INT_MAX
: size_remaining
);
761 CharsInBuf
= fread(s
, sizeof(WCHAR
), size_to_get
- 1, in
);
764 if (CharsInBuf
== 0) {
766 perror ("While reading input");
771 /* It is not clear to me from the definition that the
772 * contents of the buffer are well defined on detecting
773 * an eof without managing to read anything.
778 /* If we didn't read the eol nor the eof go around for the rest */
781 s_eol
= strchrW(s
, '\n');
786 /* If it is a comment line then discard it and go around again */
792 /* If there is a concatenating \\ then go around again */
793 if ((*(s_eol
-1) == '\\') ||
794 (*(s_eol
-1) == '\r' && *(s_eol
-2) == '\\')) {
795 WCHAR
* NextLine
= s_eol
;
797 while(*(NextLine
+1) == ' ' || *(NextLine
+1) == '\t')
802 if(*(s_eol
-1) == '\r')
805 MoveMemory(s_eol
- 1, NextLine
, (CharsInBuf
- (NextLine
- buf
) + 1)*sizeof(WCHAR
));
806 CharsInBuf
-= NextLine
- s_eol
+ 1;
811 /* Remove any line feed. Leave s_eol on the \0 */
814 if (s_eol
> buf
&& *(s_eol
-1) == '\r')
821 processRegEntry(s
, TRUE
);
824 continue; /* That is the full virtual line */
828 processRegEntry(NULL
, TRUE
);
830 HeapFree(GetProcessHeap(), 0, buf
);
833 /****************************************************************************
834 * REGPROC_print_error
836 * Print the message for GetLastError
839 static void REGPROC_print_error(void)
845 error_code
= GetLastError ();
846 status
= FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
847 NULL
, error_code
, 0, (LPTSTR
) &lpMsgBuf
, 0, NULL
);
849 fprintf(stderr
,"%s: Cannot display message for error %d, status %d\n",
850 getAppName(), error_code
, GetLastError());
854 LocalFree((HLOCAL
)lpMsgBuf
);
858 /******************************************************************************
859 * Checks whether the buffer has enough room for the string or required size.
860 * Resizes the buffer if necessary.
863 * buffer - pointer to a buffer for string
864 * len - current length of the buffer in characters.
865 * required_len - length of the string to place to the buffer in characters.
866 * The length does not include the terminating null character.
868 static void REGPROC_resize_char_buffer(WCHAR
**buffer
, DWORD
*len
, DWORD required_len
)
871 if (required_len
> *len
) {
874 *buffer
= HeapAlloc(GetProcessHeap(), 0, *len
* sizeof(**buffer
));
876 *buffer
= HeapReAlloc(GetProcessHeap(), 0, *buffer
, *len
* sizeof(**buffer
));
877 CHECK_ENOUGH_MEMORY(*buffer
);
881 /******************************************************************************
882 * Prints string str to file
884 static void REGPROC_export_string(WCHAR
**line_buf
, DWORD
*line_buf_size
, DWORD
*line_size
, WCHAR
*str
)
886 DWORD len
= lstrlenW(str
);
890 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, len
+ *line_size
+ 10);
892 /* escaping characters */
893 for (i
= 0; i
< len
; i
++) {
898 const WCHAR escape
[] = {'\\','\\'};
900 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, len
+ *line_size
+ extra
+ 1);
901 memcpy(*line_buf
+ *line_size
+ i
+ extra
- 1, escape
, 2 * sizeof(WCHAR
));
907 const WCHAR escape
[] = {'\\','"'};
909 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, len
+ *line_size
+ extra
+ 1);
910 memcpy(*line_buf
+ *line_size
+ i
+ extra
- 1, escape
, 2 * sizeof(WCHAR
));
916 const WCHAR escape
[] = {'\\','n'};
918 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, len
+ *line_size
+ extra
+ 1);
919 memcpy(*line_buf
+ *line_size
+ i
+ extra
- 1, escape
, 2 * sizeof(WCHAR
));
924 memcpy(*line_buf
+ *line_size
+ i
+ extra
- 1, &c
, sizeof(WCHAR
));
928 *line_size
+= len
+ extra
;
929 *(*line_buf
+ *line_size
- 1) = 0;
932 /******************************************************************************
933 * Writes the given line to a file, in multi-byte or wide characters
935 static void REGPROC_write_line(FILE *file
, const WCHAR
* str
, BOOL unicode
)
939 fwrite(str
, sizeof(WCHAR
), lstrlenW(str
), file
);
942 char* strA
= GetMultiByteString(str
);
944 HeapFree(GetProcessHeap(), 0, strA
);
948 /******************************************************************************
949 * Writes contents of the registry key to the specified file stream.
952 * file - writable file stream to export registry branch to.
953 * key - registry branch to export.
954 * reg_key_name_buf - name of the key with registry class.
955 * Is resized if necessary.
956 * reg_key_name_len - length of the buffer for the registry class in characters.
957 * val_name_buf - buffer for storing value name.
958 * Is resized if necessary.
959 * val_name_len - length of the buffer for storing value names in characters.
960 * val_buf - buffer for storing values while extracting.
961 * Is resized if necessary.
962 * val_size - size of the buffer for storing values in bytes.
964 static void export_hkey(FILE *file
, HKEY key
,
965 WCHAR
**reg_key_name_buf
, DWORD
*reg_key_name_len
,
966 WCHAR
**val_name_buf
, DWORD
*val_name_len
,
967 BYTE
**val_buf
, DWORD
*val_size
,
968 WCHAR
**line_buf
, DWORD
*line_buf_size
,
971 DWORD max_sub_key_len
;
972 DWORD max_val_name_len
;
978 WCHAR key_format
[] = {'\n','[','%','s',']','\n',0};
981 /* get size information and resize the buffers if necessary */
982 if (RegQueryInfoKeyW(key
, NULL
, NULL
, NULL
, NULL
,
983 &max_sub_key_len
, NULL
,
984 NULL
, &max_val_name_len
, &max_val_size
, NULL
, NULL
985 ) != ERROR_SUCCESS
) {
986 REGPROC_print_error();
988 curr_len
= strlenW(*reg_key_name_buf
);
989 REGPROC_resize_char_buffer(reg_key_name_buf
, reg_key_name_len
,
990 max_sub_key_len
+ curr_len
+ 1);
991 REGPROC_resize_char_buffer(val_name_buf
, val_name_len
,
993 if (max_val_size
> *val_size
) {
994 *val_size
= max_val_size
;
995 if (!*val_buf
) *val_buf
= HeapAlloc(GetProcessHeap(), 0, *val_size
);
996 else *val_buf
= HeapReAlloc(GetProcessHeap(), 0, *val_buf
, *val_size
);
997 CHECK_ENOUGH_MEMORY(val_buf
);
1000 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, lstrlenW(*reg_key_name_buf
) + 4);
1001 /* output data for the current key */
1002 wsprintfW(*line_buf
, key_format
, *reg_key_name_buf
);
1003 REGPROC_write_line(file
, *line_buf
, unicode
);
1005 /* print all the values */
1010 DWORD val_name_len1
= *val_name_len
;
1011 DWORD val_size1
= *val_size
;
1012 DWORD line_size
= 0;
1013 ret
= RegEnumValueW(key
, i
, *val_name_buf
, &val_name_len1
, NULL
,
1014 &value_type
, *val_buf
, &val_size1
);
1015 if (ret
!= ERROR_SUCCESS
) {
1017 if (ret
!= ERROR_NO_MORE_ITEMS
) {
1018 REGPROC_print_error();
1023 if ((*val_name_buf
)[0]) {
1024 const WCHAR val_start
[] = {'"','%','s','"','=',0};
1026 line_size
= 4 + lstrlenW(*val_name_buf
);
1027 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_size
);
1028 wsprintfW(*line_buf
, val_start
, *val_name_buf
);
1029 line_pos
= lstrlenW(*line_buf
);
1031 const WCHAR std_val
[] = {'@','=',0};
1033 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_size
);
1034 lstrcpyW(*line_buf
, std_val
);
1035 line_pos
= lstrlenW(*line_buf
);
1038 switch (value_type
) {
1042 const WCHAR start
[] = {'"',0};
1043 const WCHAR end
[] = {'"','\n',0};
1045 line_size
+= lstrlenW(start
);
1046 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_size
);
1047 lstrcatW(*line_buf
, start
);
1049 if (val_size1
) REGPROC_export_string(line_buf
, line_buf_size
, &line_size
, (WCHAR
*) *val_buf
);
1051 line_size
+= lstrlenW(end
);
1052 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_size
);
1053 lstrcatW(*line_buf
, end
);
1059 WCHAR format
[] = {'d','w','o','r','d',':','%','0','8','x','\n',0};
1062 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_size
);
1063 wsprintfW(*line_buf
+ line_pos
, format
, *((DWORD
*)*val_buf
));
1069 char* key_nameA
= GetMultiByteString(*reg_key_name_buf
);
1070 char* value_nameA
= GetMultiByteString(*val_name_buf
);
1071 fprintf(stderr
,"%s: warning - unsupported registry format '%d', "
1072 "treat as binary\n",
1073 getAppName(), value_type
);
1074 fprintf(stderr
,"key name: \"%s\"\n", key_nameA
);
1075 fprintf(stderr
,"value name:\"%s\"\n\n", value_nameA
);
1076 HeapFree(GetProcessHeap(), 0, key_nameA
);
1077 HeapFree(GetProcessHeap(), 0, value_nameA
);
1084 const WCHAR
*hex_prefix
;
1087 const WCHAR hex
[] = {'h','e','x',':',0};
1088 const WCHAR delim
[] = {'"','"','=',0};
1089 const WCHAR format
[] = {'%','0','2','x',0};
1090 const WCHAR comma
[] = {',',0};
1091 const WCHAR concat
[] = {'\\','\n',' ',' ',0};
1092 const WCHAR newline
[] = {'\n',0};
1093 BYTE
* val_buf1
= *val_buf
;
1094 DWORD val_buf1_size
= val_size1
;
1096 if (value_type
== REG_BINARY
) {
1099 const WCHAR hex_format
[] = {'h','e','x','(','%','d',')',':',0};
1101 wsprintfW(buf
, hex_format
, value_type
);
1102 if(value_type
== REG_MULTI_SZ
&& !unicode
)
1103 val_buf1
= (BYTE
*)GetMultiByteStringN((WCHAR
*)*val_buf
, val_size1
/ sizeof(WCHAR
), &val_buf1_size
);
1106 /* position of where the next character will be printed */
1107 /* NOTE: yes, strlen("hex:") is used even for hex(x): */
1108 cur_pos
= lstrlenW(delim
) + lstrlenW(hex
) +
1109 lstrlenW(*val_name_buf
);
1111 line_size
+= lstrlenW(hex_prefix
);
1112 line_size
+= val_buf1_size
* 3 + lstrlenW(concat
) * ((int)((float)val_buf1_size
* 3.0 / (float)REG_FILE_HEX_LINE_LEN
) + 1 ) + 1;
1113 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_size
);
1114 lstrcatW(*line_buf
, hex_prefix
);
1115 line_pos
+= lstrlenW(hex_prefix
);
1116 for (i1
= 0; i1
< val_buf1_size
; i1
++) {
1117 wsprintfW(*line_buf
+ line_pos
, format
, (unsigned int)(val_buf1
)[i1
]);
1119 if (i1
+ 1 < val_buf1_size
) {
1120 lstrcpyW(*line_buf
+ line_pos
, comma
);
1126 if (cur_pos
> REG_FILE_HEX_LINE_LEN
) {
1127 lstrcpyW(*line_buf
+ line_pos
, concat
);
1128 line_pos
+= lstrlenW(concat
);
1132 if(value_type
== REG_MULTI_SZ
&& !unicode
)
1133 HeapFree(GetProcessHeap(), 0, val_buf1
);
1134 lstrcpyW(*line_buf
+ line_pos
, newline
);
1138 REGPROC_write_line(file
, *line_buf
, unicode
);
1144 (*reg_key_name_buf
)[curr_len
] = '\\';
1146 DWORD buf_len
= *reg_key_name_len
- curr_len
;
1148 ret
= RegEnumKeyExW(key
, i
, *reg_key_name_buf
+ curr_len
+ 1, &buf_len
,
1149 NULL
, NULL
, NULL
, NULL
);
1150 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_MORE_DATA
) {
1152 if (ret
!= ERROR_NO_MORE_ITEMS
) {
1153 REGPROC_print_error();
1159 if (RegOpenKeyW(key
, *reg_key_name_buf
+ curr_len
+ 1,
1160 &subkey
) == ERROR_SUCCESS
) {
1161 export_hkey(file
, subkey
, reg_key_name_buf
, reg_key_name_len
,
1162 val_name_buf
, val_name_len
, val_buf
, val_size
,
1163 line_buf
, line_buf_size
, unicode
);
1164 RegCloseKey(subkey
);
1166 REGPROC_print_error();
1170 (*reg_key_name_buf
)[curr_len
] = '\0';
1173 /******************************************************************************
1174 * Open file for export.
1176 static FILE *REGPROC_open_export_file(WCHAR
*file_name
, BOOL unicode
)
1181 if (strncmpW(file_name
,&dash
,1)==0)
1185 CHAR
* file_nameA
= GetMultiByteString(file_name
);
1186 file
= fopen(file_nameA
, "w");
1189 fprintf(stderr
,"%s: Can't open file \"%s\"\n", getAppName(), file_nameA
);
1190 HeapFree(GetProcessHeap(), 0, file_nameA
);
1193 HeapFree(GetProcessHeap(), 0, file_nameA
);
1197 const BYTE unicode_seq
[] = {0xff,0xfe};
1198 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'};
1199 fwrite(unicode_seq
, sizeof(BYTE
), sizeof(unicode_seq
)/sizeof(unicode_seq
[0]), file
);
1200 fwrite(header
, sizeof(WCHAR
), sizeof(header
)/sizeof(header
[0]), file
);
1203 fputs("REGEDIT4\n", file
);
1209 /******************************************************************************
1210 * Writes contents of the registry key to the specified file stream.
1213 * file_name - name of a file to export registry branch to.
1214 * reg_key_name - registry branch to export. The whole registry is exported if
1215 * reg_key_name is NULL or contains an empty string.
1217 BOOL
export_registry_key(WCHAR
*file_name
, WCHAR
*reg_key_name
, DWORD format
)
1219 WCHAR
*reg_key_name_buf
;
1220 WCHAR
*val_name_buf
;
1223 DWORD reg_key_name_len
= KEY_MAX_LEN
;
1224 DWORD val_name_len
= KEY_MAX_LEN
;
1225 DWORD val_size
= REG_VAL_BUF_SIZE
;
1226 DWORD line_buf_size
= KEY_MAX_LEN
+ REG_VAL_BUF_SIZE
;
1228 BOOL unicode
= (format
== REG_FORMAT_5
);
1230 reg_key_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1231 reg_key_name_len
* sizeof(*reg_key_name_buf
));
1232 val_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1233 val_name_len
* sizeof(*val_name_buf
));
1234 val_buf
= HeapAlloc(GetProcessHeap(), 0, val_size
);
1235 line_buf
= HeapAlloc(GetProcessHeap(), 0, line_buf_size
);
1236 CHECK_ENOUGH_MEMORY(reg_key_name_buf
&& val_name_buf
&& val_buf
);
1238 if (reg_key_name
&& reg_key_name
[0]) {
1240 WCHAR
*branch_name
= NULL
;
1243 REGPROC_resize_char_buffer(®_key_name_buf
, ®_key_name_len
,
1244 lstrlenW(reg_key_name
));
1245 lstrcpyW(reg_key_name_buf
, reg_key_name
);
1247 /* open the specified key */
1248 if (!parseKeyName(reg_key_name
, ®_key_class
, &branch_name
)) {
1249 CHAR
* key_nameA
= GetMultiByteString(reg_key_name
);
1250 fprintf(stderr
,"%s: Incorrect registry class specification in '%s'\n",
1251 getAppName(), key_nameA
);
1252 HeapFree(GetProcessHeap(), 0, key_nameA
);
1255 if (!branch_name
[0]) {
1256 /* no branch - registry class is specified */
1257 file
= REGPROC_open_export_file(file_name
, unicode
);
1258 export_hkey(file
, reg_key_class
,
1259 ®_key_name_buf
, ®_key_name_len
,
1260 &val_name_buf
, &val_name_len
,
1261 &val_buf
, &val_size
, &line_buf
,
1262 &line_buf_size
, unicode
);
1263 } else if (RegOpenKeyW(reg_key_class
, branch_name
, &key
) == ERROR_SUCCESS
) {
1264 file
= REGPROC_open_export_file(file_name
, unicode
);
1265 export_hkey(file
, key
,
1266 ®_key_name_buf
, ®_key_name_len
,
1267 &val_name_buf
, &val_name_len
,
1268 &val_buf
, &val_size
, &line_buf
,
1269 &line_buf_size
, unicode
);
1272 CHAR
* key_nameA
= GetMultiByteString(reg_key_name
);
1273 fprintf(stderr
,"%s: Can't export. Registry key '%s' does not exist!\n",
1274 getAppName(), key_nameA
);
1275 HeapFree(GetProcessHeap(), 0, key_nameA
);
1276 REGPROC_print_error();
1281 /* export all registry classes */
1282 file
= REGPROC_open_export_file(file_name
, unicode
);
1283 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++) {
1284 /* do not export HKEY_CLASSES_ROOT */
1285 if (reg_class_keys
[i
] != HKEY_CLASSES_ROOT
&&
1286 reg_class_keys
[i
] != HKEY_CURRENT_USER
&&
1287 reg_class_keys
[i
] != HKEY_CURRENT_CONFIG
&&
1288 reg_class_keys
[i
] != HKEY_DYN_DATA
) {
1289 lstrcpyW(reg_key_name_buf
, reg_class_namesW
[i
]);
1290 export_hkey(file
, reg_class_keys
[i
],
1291 ®_key_name_buf
, ®_key_name_len
,
1292 &val_name_buf
, &val_name_len
,
1293 &val_buf
, &val_size
, &line_buf
,
1294 &line_buf_size
, unicode
);
1302 HeapFree(GetProcessHeap(), 0, reg_key_name
);
1303 HeapFree(GetProcessHeap(), 0, val_name_buf
);
1304 HeapFree(GetProcessHeap(), 0, val_buf
);
1305 HeapFree(GetProcessHeap(), 0, line_buf
);
1309 /******************************************************************************
1310 * Reads contents of the specified file into the registry.
1312 BOOL
import_registry_file(FILE* reg_file
)
1317 if (fread( s
, 2, 1, reg_file
) == 1)
1319 if (s
[0] == 0xff && s
[1] == 0xfe)
1321 processRegLinesW(reg_file
);
1325 processRegLinesA(reg_file
);
1333 /******************************************************************************
1334 * Removes the registry key with all subkeys. Parses full key name.
1337 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1338 * empty, points to register key class, does not exist.
1340 void delete_registry_key(WCHAR
*reg_key_name
)
1342 WCHAR
*key_name
= NULL
;
1345 if (!reg_key_name
|| !reg_key_name
[0])
1348 if (!parseKeyName(reg_key_name
, &key_class
, &key_name
)) {
1349 char* reg_key_nameA
= GetMultiByteString(reg_key_name
);
1350 fprintf(stderr
,"%s: Incorrect registry class specification in '%s'\n",
1351 getAppName(), reg_key_nameA
);
1352 HeapFree(GetProcessHeap(), 0, reg_key_nameA
);
1356 char* reg_key_nameA
= GetMultiByteString(reg_key_name
);
1357 fprintf(stderr
,"%s: Can't delete registry class '%s'\n",
1358 getAppName(), reg_key_nameA
);
1359 HeapFree(GetProcessHeap(), 0, reg_key_nameA
);
1363 RegDeleteTreeW(key_class
, key_name
);