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
33 #include <wine/unicode.h>
34 #include <wine/debug.h>
37 #define REG_VAL_BUF_SIZE 4096
39 /* maximal number of characters in hexadecimal data line,
40 * including the indentation, but not including the '\' character
42 #define REG_FILE_HEX_LINE_LEN (2 + 25 * 3)
44 extern const WCHAR
* reg_class_namesW
[];
46 static HKEY reg_class_keys
[] = {
47 HKEY_LOCAL_MACHINE
, HKEY_USERS
, HKEY_CLASSES_ROOT
,
48 HKEY_CURRENT_CONFIG
, HKEY_CURRENT_USER
, HKEY_DYN_DATA
51 #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*A))
54 #define NOT_ENOUGH_MEMORY 1
57 /* processing macros */
59 /* common check of memory allocation results */
60 #define CHECK_ENOUGH_MEMORY(p) \
63 output_message(STRING_OUT_OF_MEMORY, __FILE__, __LINE__); \
64 exit(NOT_ENOUGH_MEMORY); \
67 /******************************************************************************
68 * Allocates memory and converts input from multibyte to wide chars
69 * Returned string must be freed by the caller
71 static 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 converts input from multibyte to wide chars
88 * Returned string must be freed by the caller
90 static 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 converts 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 converts input from wide chars to multibyte
127 * Returned string must be freed by the caller
129 static 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 output_message(STRING_INVALID_HEX
);
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 output_message(STRING_CSV_HEX_ERROR
, s
);
184 HeapFree(GetProcessHeap(), 0, data
);
196 /******************************************************************************
197 * This function returns the HKEY associated with the data type encoded in the
198 * value. It modifies the input parameter (key value) in order to skip this
199 * "now useless" data type information.
201 * Note: Updated based on the algorithm used in 'server/registry.c'
203 static DWORD
getDataType(LPWSTR
*lpValue
, DWORD
* parse_type
)
205 struct data_type
{ const WCHAR
*tag
; int len
; int type
; int parse_type
; };
207 static const WCHAR quote
[] = {'"'};
208 static const WCHAR str
[] = {'s','t','r',':','"'};
209 static const WCHAR str2
[] = {'s','t','r','(','2',')',':','"'};
210 static const WCHAR hex
[] = {'h','e','x',':'};
211 static const WCHAR dword
[] = {'d','w','o','r','d',':'};
212 static const WCHAR hexp
[] = {'h','e','x','('};
214 static const struct data_type data_types
[] = { /* actual type */ /* type to assume for parsing */
215 { quote
, 1, REG_SZ
, REG_SZ
},
216 { str
, 5, REG_SZ
, REG_SZ
},
217 { str2
, 8, REG_EXPAND_SZ
, REG_SZ
},
218 { hex
, 4, REG_BINARY
, REG_BINARY
},
219 { dword
, 6, REG_DWORD
, REG_DWORD
},
220 { hexp
, 4, -1, REG_BINARY
},
224 const struct data_type
*ptr
;
227 for (ptr
= data_types
; ptr
->tag
; ptr
++) {
228 if (strncmpW( ptr
->tag
, *lpValue
, ptr
->len
))
232 *parse_type
= ptr
->parse_type
;
238 /* "hex(xx):" is special */
239 type
= (int)strtoulW( *lpValue
, &end
, 16 );
240 if (**lpValue
=='\0' || *end
!=')' || *(end
+1)!=':') {
248 *parse_type
=REG_NONE
;
252 /******************************************************************************
253 * Replaces escape sequences with the characters.
255 static int REGPROC_unescape_string(WCHAR
* str
)
257 int str_idx
= 0; /* current character under analysis */
258 int val_idx
= 0; /* the last character of the unescaped string */
259 int len
= lstrlenW(str
);
260 for (str_idx
= 0; str_idx
< len
; str_idx
++, val_idx
++) {
261 if (str
[str_idx
] == '\\') {
263 switch (str
[str_idx
]) {
275 str
[val_idx
] = str
[str_idx
];
278 output_message(STRING_ESCAPE_SEQUENCE
, str
[str_idx
]);
279 str
[val_idx
] = str
[str_idx
];
283 str
[val_idx
] = str
[str_idx
];
290 static BOOL
parseKeyName(LPWSTR lpKeyName
, HKEY
*hKey
, LPWSTR
*lpKeyPath
)
292 WCHAR
* lpSlash
= NULL
;
295 if (lpKeyName
== NULL
)
298 for(i
= 0; *(lpKeyName
+i
) != 0; i
++)
300 if(*(lpKeyName
+i
) == '\\')
302 lpSlash
= lpKeyName
+i
;
309 len
= lpSlash
-lpKeyName
;
313 len
= lstrlenW(lpKeyName
);
314 lpSlash
= lpKeyName
+len
;
318 for (i
= 0; i
< ARRAY_SIZE(reg_class_keys
); i
++) {
319 if (CompareStringW(LOCALE_USER_DEFAULT
, 0, lpKeyName
, len
, reg_class_namesW
[i
], -1) == CSTR_EQUAL
&&
320 len
== lstrlenW(reg_class_namesW
[i
])) {
321 *hKey
= reg_class_keys
[i
];
330 if (*lpSlash
!= '\0')
332 *lpKeyPath
= lpSlash
;
336 /* Globals used by the setValue() & co */
337 static WCHAR
*currentKeyName
;
338 static HKEY currentKeyHandle
= NULL
;
340 /* Registry data types */
341 static const WCHAR type_none
[] = {'R','E','G','_','N','O','N','E',0};
342 static const WCHAR type_sz
[] = {'R','E','G','_','S','Z',0};
343 static const WCHAR type_expand_sz
[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
344 static const WCHAR type_binary
[] = {'R','E','G','_','B','I','N','A','R','Y',0};
345 static const WCHAR type_dword
[] = {'R','E','G','_','D','W','O','R','D',0};
346 static const WCHAR type_dword_le
[] = {'R','E','G','_','D','W','O','R','D','_','L','I','T','T','L','E','_','E','N','D','I','A','N',0};
347 static const WCHAR type_dword_be
[] = {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0};
348 static const WCHAR type_multi_sz
[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
357 {REG_NONE
, type_none
},
359 {REG_EXPAND_SZ
, type_expand_sz
},
360 {REG_BINARY
, type_binary
},
361 {REG_DWORD
, type_dword
},
362 {REG_DWORD_LITTLE_ENDIAN
, type_dword_le
},
363 {REG_DWORD_BIG_ENDIAN
, type_dword_be
},
364 {REG_MULTI_SZ
, type_multi_sz
},
367 static const WCHAR
*reg_type_to_wchar(DWORD type
)
369 int i
, array_size
= ARRAY_SIZE(type_rels
);
371 for (i
= 0; i
< array_size
; i
++)
373 if (type
== type_rels
[i
].type
)
374 return type_rels
[i
].name
;
379 /******************************************************************************
380 * Sets the value with name val_name to the data in val_data for the currently
384 * val_name - name of the registry value
385 * val_data - registry value data
387 static LONG
setValue(WCHAR
* val_name
, WCHAR
* val_data
, BOOL is_unicode
)
390 DWORD dwDataType
, dwParseType
;
393 WCHAR del
[] = {'-',0};
395 if ( (val_name
== NULL
) || (val_data
== NULL
) )
396 return ERROR_INVALID_PARAMETER
;
398 if (lstrcmpW(val_data
, del
) == 0)
400 res
=RegDeleteValueW(currentKeyHandle
,val_name
);
401 return (res
== ERROR_FILE_NOT_FOUND
? ERROR_SUCCESS
: res
);
404 /* Get the data type stored into the value field */
405 dwDataType
= getDataType(&val_data
, &dwParseType
);
407 if (dwParseType
== REG_SZ
) /* no conversion for string */
409 dwLen
= REGPROC_unescape_string(val_data
);
410 if(!dwLen
|| val_data
[dwLen
-1] != '"')
411 return ERROR_INVALID_DATA
;
412 val_data
[dwLen
-1] = '\0'; /* remove last quotes */
413 lpbData
= (BYTE
*) val_data
;
414 dwLen
= dwLen
* sizeof(WCHAR
); /* size is in bytes */
416 else if (dwParseType
== REG_DWORD
) /* Convert the dword types */
418 if (!convertHexToDWord(val_data
, &dwData
))
419 return ERROR_INVALID_DATA
;
420 lpbData
= (BYTE
*)&dwData
;
421 dwLen
= sizeof(dwData
);
423 else if (dwParseType
== REG_BINARY
) /* Convert the binary data */
425 lpbData
= convertHexCSVToHex(val_data
, &dwLen
);
427 return ERROR_INVALID_DATA
;
429 if((dwDataType
== REG_MULTI_SZ
|| dwDataType
== REG_EXPAND_SZ
) && !is_unicode
)
431 LPBYTE tmp
= lpbData
;
432 lpbData
= (LPBYTE
)GetWideStringN((char*)lpbData
, dwLen
, &dwLen
);
433 dwLen
*= sizeof(WCHAR
);
434 HeapFree(GetProcessHeap(), 0, tmp
);
437 else /* unknown format */
439 output_message(STRING_UNKNOWN_DATA_FORMAT
, reg_type_to_wchar(dwDataType
));
440 return ERROR_INVALID_DATA
;
443 res
= RegSetValueExW(
450 if (dwParseType
== REG_BINARY
)
451 HeapFree(GetProcessHeap(), 0, lpbData
);
455 /******************************************************************************
456 * A helper function for processRegEntry() that opens the current key.
457 * That key must be closed by calling closeKey().
459 static LONG
openKeyW(WCHAR
* stdInput
)
467 if (stdInput
== NULL
)
468 return ERROR_INVALID_PARAMETER
;
470 /* Get the registry class */
471 if (!parseKeyName(stdInput
, &keyClass
, &keyPath
))
472 return ERROR_INVALID_PARAMETER
;
474 res
= RegCreateKeyExW(
475 keyClass
, /* Class */
476 keyPath
, /* Sub Key */
478 NULL
, /* object type */
479 REG_OPTION_NON_VOLATILE
, /* option, REG_OPTION_NON_VOLATILE ... */
480 KEY_ALL_ACCESS
, /* access mask, KEY_ALL_ACCESS */
481 NULL
, /* security attribute */
482 ¤tKeyHandle
, /* result */
483 &dwDisp
); /* disposition, REG_CREATED_NEW_KEY or
484 REG_OPENED_EXISTING_KEY */
486 if (res
== ERROR_SUCCESS
)
488 currentKeyName
= HeapAlloc(GetProcessHeap(), 0, (strlenW(stdInput
) + 1) * sizeof(WCHAR
));
489 CHECK_ENOUGH_MEMORY(currentKeyName
);
490 strcpyW(currentKeyName
, stdInput
);
493 currentKeyHandle
= NULL
;
499 /******************************************************************************
500 * Close the currently opened key.
502 static void closeKey(void)
504 if (currentKeyHandle
)
506 HeapFree(GetProcessHeap(), 0, currentKeyName
);
507 RegCloseKey(currentKeyHandle
);
508 currentKeyHandle
= NULL
;
512 /******************************************************************************
513 * This function is a wrapper for the setValue function. It prepares the
514 * land and cleans the area once completed.
515 * Note: this function modifies the line parameter.
517 * line - registry file unwrapped line. Should have the registry value name and
518 * complete registry value data.
520 static void processSetValue(WCHAR
* line
, BOOL is_unicode
)
522 WCHAR
* val_name
; /* registry value name */
523 WCHAR
* val_data
; /* registry value data */
524 int line_idx
= 0; /* current character under analysis */
528 while ( isspaceW(line
[line_idx
]) ) line_idx
++;
529 if (line
[line_idx
] == '@' && line
[line_idx
+ 1] == '=') {
530 line
[line_idx
] = '\0';
533 } else if (line
[line_idx
] == '\"') {
535 val_name
= line
+ line_idx
;
536 while (line
[line_idx
]) {
537 if (line
[line_idx
] == '\\') /* skip escaped character */
541 if (line
[line_idx
] == '\"') {
542 line
[line_idx
] = '\0';
550 while ( isspaceW(line
[line_idx
]) ) line_idx
++;
551 if (!line
[line_idx
]) {
552 output_message(STRING_UNEXPECTED_EOL
, line
);
555 if (line
[line_idx
] != '=') {
556 line
[line_idx
] = '\"';
557 output_message(STRING_UNRECOGNIZED_LINE
, line
);
562 output_message(STRING_UNRECOGNIZED_LINE
, line
);
565 line_idx
++; /* skip the '=' character */
567 while ( isspaceW(line
[line_idx
]) ) line_idx
++;
568 val_data
= line
+ line_idx
;
569 /* trim trailing blanks */
570 line_idx
= strlenW(val_data
);
571 while (line_idx
> 0 && isspaceW(val_data
[line_idx
-1])) line_idx
--;
572 val_data
[line_idx
] = '\0';
574 REGPROC_unescape_string(val_name
);
575 res
= setValue(val_name
, val_data
, is_unicode
);
576 if ( res
!= ERROR_SUCCESS
)
577 output_message(STRING_SETVALUE_FAILED
, val_name
, currentKeyName
);
580 /******************************************************************************
581 * This function receives the currently read entry and performs the
582 * corresponding action.
583 * isUnicode affects parsing of REG_MULTI_SZ values
585 static void processRegEntry(WCHAR
* stdInput
, BOOL isUnicode
)
587 if ( stdInput
[0] == '[') /* We are reading a new key */
590 closeKey(); /* Close the previous key */
592 /* Get rid of the square brackets */
594 keyEnd
= strrchrW(stdInput
, ']');
598 /* delete the key if we encounter '-' at the start of reg key */
599 if (stdInput
[0] == '-')
600 delete_registry_key(stdInput
+ 1);
601 else if (openKeyW(stdInput
) != ERROR_SUCCESS
)
602 output_message(STRING_OPEN_KEY_FAILED
, stdInput
);
603 } else if( currentKeyHandle
&&
604 (( stdInput
[0] == '@') || /* reading a default @=data pair */
605 ( stdInput
[0] == '\"'))) /* reading a new value=data pair */
607 processSetValue(stdInput
, isUnicode
);
611 /* version for Windows 3.1 */
612 static void processRegEntry31(WCHAR
*line
)
618 static WCHAR empty
[] = {0};
619 static WCHAR hkcr
[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T'};
621 if (strncmpW(line
, hkcr
, sizeof(hkcr
) / sizeof(WCHAR
))) return;
624 while (line
[key_end
] && !isspaceW(line
[key_end
])) key_end
++;
626 value
= line
+ key_end
;
627 while (isspaceW(value
[0])) value
++;
629 if (value
[0] == '=') value
++;
630 if (value
[0] == ' ') value
++; /* at most one space is skipped */
632 line
[key_end
] = '\0';
633 if (openKeyW(line
) != ERROR_SUCCESS
)
634 output_message(STRING_OPEN_KEY_FAILED
, line
);
636 res
= RegSetValueExW(
642 (strlenW(value
) + 1) * sizeof(WCHAR
));
643 if (res
!= ERROR_SUCCESS
)
644 output_message(STRING_SETVALUE_FAILED
, empty
, currentKeyName
);
649 /* version constants */
651 #define REG_VERSION_31 3
652 #define REG_VERSION_40 4
653 #define REG_VERSION_50 5
655 /******************************************************************************
656 * Processes a registry file.
657 * Correctly processes comments (in # and ; form), line continuation.
660 * in - input stream to read from
661 * first_chars - beginning of stream, read due to Unicode check
663 static void processRegLinesA(FILE *in
, char* first_chars
)
665 char *buf
= NULL
; /* the line read from the input stream */
666 unsigned long line_size
= REG_VAL_BUF_SIZE
;
667 size_t chars_in_buf
= -1;
668 char *s
; /* A pointer to buf for fread */
669 char *line
; /* The start of the current line */
671 unsigned long version
= 0;
673 static const char header_31
[] = "REGEDIT";
674 static const char header_40
[] = "REGEDIT4";
675 static const char header_50
[] = "Windows Registry Editor Version 5.00";
677 buf
= HeapAlloc(GetProcessHeap(), 0, line_size
);
678 CHECK_ENOUGH_MEMORY(buf
);
682 memcpy(line
, first_chars
, 2);
688 size_t size_remaining
;
690 char *s_eol
= NULL
; /* various local uses */
692 /* Do we need to expand the buffer? */
693 assert(s
>= buf
&& s
<= buf
+ line_size
);
694 size_remaining
= line_size
- (s
- buf
);
695 if (size_remaining
< 3) /* we need at least 3 bytes of room for \r\n\0 */
698 size_t new_size
= line_size
+ REG_VAL_BUF_SIZE
;
699 if (new_size
> line_size
) /* no arithmetic overflow */
700 new_buffer
= HeapReAlloc(GetProcessHeap(), 0, buf
, new_size
);
703 CHECK_ENOUGH_MEMORY(new_buffer
);
706 s
= buf
+ line_size
- size_remaining
;
707 line_size
= new_size
;
708 size_remaining
= line_size
- (s
- buf
);
711 /* Get as much as possible into the buffer, terminating on EOF,
712 * error or once we have read the maximum amount. Abort on error.
714 size_to_get
= (size_remaining
> INT_MAX
? INT_MAX
: size_remaining
);
716 chars_in_buf
= fread(s
, 1, size_to_get
- 1, in
);
719 if (chars_in_buf
== 0) {
721 perror("While reading input");
729 /* If we didn't read the end-of-line sequence or EOF, go around again */
732 s_eol
= strpbrk(line
, "\r\n");
734 /* Move the stub of the line to the start of the buffer so
735 * we get the maximum space to read into, and so we don't
736 * have to recalculate 'line' if the buffer expands */
737 MoveMemory(buf
, line
, strlen(line
) + 1);
739 s
= strchr(line
, '\0');
743 /* If we find a comment line, discard it and go around again */
744 if (line
[0] == '#' || line
[0] == ';') {
745 if (*s_eol
== '\r' && *(s_eol
+ 1) == '\n')
752 /* If there is a concatenating '\\', go around again */
753 if (*(s_eol
- 1) == '\\') {
754 char *next_line
= s_eol
+ 1;
756 if (*s_eol
== '\r' && *(s_eol
+ 1) == '\n')
759 while (*(next_line
+ 1) == ' ' || *(next_line
+ 1) == '\t')
762 MoveMemory(s_eol
- 1, next_line
, chars_in_buf
- (next_line
- s
) + 1);
763 chars_in_buf
-= next_line
- s_eol
+ 1;
767 /* Remove any line feed. Leave s_eol on the last \0 */
768 if (*s_eol
== '\r' && *(s_eol
+ 1) == '\n')
772 /* Check if the line is a header string */
773 if (!strcmp(line
, header_31
)) {
774 version
= REG_VERSION_31
;
775 } else if (!strcmp(line
, header_40
)) {
776 version
= REG_VERSION_40
;
777 } else if (!strcmp(line
, header_50
)) {
778 version
= REG_VERSION_50
;
780 lineW
= GetWideString(line
);
781 if (version
== REG_VERSION_31
) {
782 processRegEntry31(lineW
);
783 } else if(version
== REG_VERSION_40
|| version
== REG_VERSION_50
) {
784 processRegEntry(lineW
, FALSE
);
786 HeapFree(GetProcessHeap(), 0, lineW
);
793 HeapFree(GetProcessHeap(), 0, buf
);
796 static void processRegLinesW(FILE *in
)
798 WCHAR
* buf
= NULL
; /* line read from input stream */
799 ULONG lineSize
= REG_VAL_BUF_SIZE
;
800 size_t CharsInBuf
= -1;
802 WCHAR
* s
; /* The pointer into buf for where the current fgets should read */
803 WCHAR
* line
; /* The start of the current line */
805 buf
= HeapAlloc(GetProcessHeap(), 0, lineSize
* sizeof(WCHAR
));
806 CHECK_ENOUGH_MEMORY(buf
);
812 size_t size_remaining
;
814 WCHAR
*s_eol
= NULL
; /* various local uses */
816 /* Do we need to expand the buffer ? */
817 assert (s
>= buf
&& s
<= buf
+ lineSize
);
818 size_remaining
= lineSize
- (s
-buf
);
819 if (size_remaining
< 2) /* room for 1 character and the \0 */
822 size_t new_size
= lineSize
+ (REG_VAL_BUF_SIZE
/ sizeof(WCHAR
));
823 if (new_size
> lineSize
) /* no arithmetic overflow */
824 new_buffer
= HeapReAlloc (GetProcessHeap(), 0, buf
, new_size
* sizeof(WCHAR
));
827 CHECK_ENOUGH_MEMORY(new_buffer
);
830 s
= buf
+ lineSize
- size_remaining
;
832 size_remaining
= lineSize
- (s
-buf
);
835 /* Get as much as possible into the buffer, terminated either by
836 * eof, error or getting the maximum amount. Abort on error.
838 size_to_get
= (size_remaining
> INT_MAX
? INT_MAX
: size_remaining
);
840 CharsInBuf
= fread(s
, sizeof(WCHAR
), size_to_get
- 1, in
);
843 if (CharsInBuf
== 0) {
845 perror ("While reading input");
850 /* It is not clear to me from the definition that the
851 * contents of the buffer are well defined on detecting
852 * an eof without managing to read anything.
857 /* If we didn't read the eol nor the eof go around for the rest */
860 const WCHAR line_endings
[] = {'\r','\n',0};
861 s_eol
= strpbrkW(line
, line_endings
);
864 /* Move the stub of the line to the start of the buffer so
865 * we get the maximum space to read into, and so we don't
866 * have to recalculate 'line' if the buffer expands */
867 MoveMemory(buf
, line
, (strlenW(line
)+1) * sizeof(WCHAR
));
869 s
= strchrW(line
, '\0');
873 /* If it is a comment line then discard it and go around again */
874 if (*line
== '#' || *line
== ';') {
875 if (*s_eol
== '\r' && *(s_eol
+1) == '\n')
882 /* If there is a concatenating \\ then go around again */
883 if (*(s_eol
-1) == '\\') {
884 WCHAR
* NextLine
= s_eol
+ 1;
886 if(*s_eol
== '\r' && *(s_eol
+1) == '\n')
889 while(*(NextLine
+1) == ' ' || *(NextLine
+1) == '\t')
892 MoveMemory(s_eol
- 1, NextLine
, (CharsInBuf
- (NextLine
- s
) + 1)*sizeof(WCHAR
));
893 CharsInBuf
-= NextLine
- s_eol
+ 1;
897 /* Remove any line feed. Leave s_eol on the last \0 */
898 if (*s_eol
== '\r' && *(s_eol
+ 1) == '\n')
902 processRegEntry(line
, TRUE
);
909 HeapFree(GetProcessHeap(), 0, buf
);
912 /******************************************************************************
913 * Checks whether the buffer has enough room for the string or required size.
914 * Resizes the buffer if necessary.
917 * buffer - pointer to a buffer for string
918 * len - current length of the buffer in characters.
919 * required_len - length of the string to place to the buffer in characters.
920 * The length does not include the terminating null character.
922 static void REGPROC_resize_char_buffer(WCHAR
**buffer
, DWORD
*len
, DWORD required_len
)
925 if (required_len
> *len
) {
928 *buffer
= HeapAlloc(GetProcessHeap(), 0, *len
* sizeof(**buffer
));
930 *buffer
= HeapReAlloc(GetProcessHeap(), 0, *buffer
, *len
* sizeof(**buffer
));
931 CHECK_ENOUGH_MEMORY(*buffer
);
935 /******************************************************************************
936 * Same as REGPROC_resize_char_buffer() but on a regular buffer.
939 * buffer - pointer to a buffer
940 * len - current size of the buffer in bytes
941 * required_size - size of the data to place in the buffer in bytes
943 static void REGPROC_resize_binary_buffer(BYTE
**buffer
, DWORD
*size
, DWORD required_size
)
945 if (required_size
> *size
) {
946 *size
= required_size
;
948 *buffer
= HeapAlloc(GetProcessHeap(), 0, *size
);
950 *buffer
= HeapReAlloc(GetProcessHeap(), 0, *buffer
, *size
);
951 CHECK_ENOUGH_MEMORY(*buffer
);
955 /******************************************************************************
956 * Prints string str to file
958 static void REGPROC_export_string(WCHAR
**line_buf
, DWORD
*line_buf_size
, DWORD
*line_len
, WCHAR
*str
, DWORD str_len
)
963 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, *line_len
+ str_len
+ 10);
965 /* escaping characters */
967 for (i
= 0; i
< str_len
; i
++) {
972 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, *line_len
+ str_len
+ extra
);
973 (*line_buf
)[pos
++] = '\\';
974 (*line_buf
)[pos
++] = 'n';
979 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, *line_len
+ str_len
+ extra
);
980 (*line_buf
)[pos
++] = '\\';
981 (*line_buf
)[pos
++] = 'r';
987 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, *line_len
+ str_len
+ extra
);
988 (*line_buf
)[pos
++] = '\\';
992 (*line_buf
)[pos
++] = c
;
996 (*line_buf
)[pos
] = '\0';
1000 static void REGPROC_export_binary(WCHAR
**line_buf
, DWORD
*line_buf_size
, DWORD
*line_len
, DWORD type
, BYTE
*value
, DWORD value_size
, BOOL unicode
)
1002 DWORD hex_pos
, data_pos
;
1003 const WCHAR
*hex_prefix
;
1004 const WCHAR hex
[] = {'h','e','x',':',0};
1006 const WCHAR concat
[] = {'\\','\r','\n',' ',' ',0};
1007 DWORD concat_prefix
, concat_len
;
1008 const WCHAR newline
[] = {'\r','\n',0};
1009 CHAR
* value_multibyte
= NULL
;
1011 if (type
== REG_BINARY
) {
1014 const WCHAR hex_format
[] = {'h','e','x','(','%','x',')',':',0};
1015 hex_prefix
= hex_buf
;
1016 sprintfW(hex_buf
, hex_format
, type
);
1017 if ((type
== REG_SZ
|| type
== REG_EXPAND_SZ
|| type
== REG_MULTI_SZ
) && !unicode
)
1019 value_multibyte
= GetMultiByteStringN((WCHAR
*)value
, value_size
/ sizeof(WCHAR
), &value_size
);
1020 value
= (BYTE
*)value_multibyte
;
1024 concat_len
= lstrlenW(concat
);
1027 hex_pos
= *line_len
;
1028 *line_len
+= lstrlenW(hex_prefix
);
1029 data_pos
= *line_len
;
1030 *line_len
+= value_size
* 3;
1031 /* - The 2 spaces that concat places at the start of the
1032 * line effectively reduce the space available for data.
1033 * - If the value name and hex prefix are very long
1034 * ( > REG_FILE_HEX_LINE_LEN) or *line_len divides
1035 * without a remainder then we may overestimate
1036 * the needed number of lines by one. But that's ok.
1037 * - The trailing '\r' takes the place of a comma so
1038 * we only need to add 1 for the trailing '\n'
1040 *line_len
+= *line_len
/ (REG_FILE_HEX_LINE_LEN
- concat_prefix
) * concat_len
+ 1;
1041 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, *line_len
);
1042 lstrcpyW(*line_buf
+ hex_pos
, hex_prefix
);
1045 const WCHAR format
[] = {'%','0','2','x',0};
1048 column
= data_pos
; /* no line wrap yet */
1052 sprintfW(*line_buf
+ data_pos
, format
, (unsigned int)value
[i
]);
1054 if (++i
== value_size
)
1057 (*line_buf
)[data_pos
++] = ',';
1061 if (column
>= REG_FILE_HEX_LINE_LEN
) {
1062 lstrcpyW(*line_buf
+ data_pos
, concat
);
1063 data_pos
+= concat_len
;
1064 column
= concat_prefix
;
1068 lstrcpyW(*line_buf
+ data_pos
, newline
);
1069 HeapFree(GetProcessHeap(), 0, value_multibyte
);
1072 /******************************************************************************
1073 * Writes the given line to a file, in multi-byte or wide characters
1075 static void REGPROC_write_line(FILE *file
, const WCHAR
* str
, BOOL unicode
)
1079 fwrite(str
, sizeof(WCHAR
), lstrlenW(str
), file
);
1082 char* strA
= GetMultiByteString(str
);
1084 HeapFree(GetProcessHeap(), 0, strA
);
1088 /******************************************************************************
1089 * Writes contents of the registry key to the specified file stream.
1092 * file - writable file stream to export registry branch to.
1093 * key - registry branch to export.
1094 * reg_key_name_buf - name of the key with registry class.
1095 * Is resized if necessary.
1096 * reg_key_name_size - length of the buffer for the registry class in characters.
1097 * val_name_buf - buffer for storing value name.
1098 * Is resized if necessary.
1099 * val_name_size - length of the buffer for storing value names in characters.
1100 * val_buf - buffer for storing values while extracting.
1101 * Is resized if necessary.
1102 * val_size - size of the buffer for storing values in bytes.
1104 static void export_hkey(FILE *file
, HKEY key
,
1105 WCHAR
**reg_key_name_buf
, DWORD
*reg_key_name_size
,
1106 WCHAR
**val_name_buf
, DWORD
*val_name_size
,
1107 BYTE
**val_buf
, DWORD
*val_size
,
1108 WCHAR
**line_buf
, DWORD
*line_buf_size
,
1111 DWORD max_sub_key_len
;
1112 DWORD max_val_name_len
;
1117 WCHAR key_format
[] = {'\r','\n','[','%','s',']','\r','\n',0};
1119 /* get size information and resize the buffers if necessary */
1120 if (RegQueryInfoKeyW(key
, NULL
, NULL
, NULL
, NULL
,
1121 &max_sub_key_len
, NULL
,
1122 NULL
, &max_val_name_len
, &max_val_size
, NULL
, NULL
1125 curr_len
= strlenW(*reg_key_name_buf
);
1126 REGPROC_resize_char_buffer(reg_key_name_buf
, reg_key_name_size
,
1127 max_sub_key_len
+ curr_len
+ 1);
1128 REGPROC_resize_char_buffer(val_name_buf
, val_name_size
,
1130 REGPROC_resize_binary_buffer(val_buf
, val_size
, max_val_size
);
1131 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, lstrlenW(*reg_key_name_buf
) + 4);
1132 /* output data for the current key */
1133 sprintfW(*line_buf
, key_format
, *reg_key_name_buf
);
1134 REGPROC_write_line(file
, *line_buf
, unicode
);
1136 /* print all the values */
1140 DWORD val_name_size1
= *val_name_size
;
1141 DWORD val_size1
= *val_size
;
1142 ret
= RegEnumValueW(key
, i
, *val_name_buf
, &val_name_size1
, NULL
,
1143 &value_type
, *val_buf
, &val_size1
);
1144 if (ret
== ERROR_MORE_DATA
) {
1145 /* Increase the size of the buffers and retry */
1146 REGPROC_resize_char_buffer(val_name_buf
, val_name_size
, val_name_size1
);
1147 REGPROC_resize_binary_buffer(val_buf
, val_size
, val_size1
);
1148 } else if (ret
== ERROR_SUCCESS
) {
1152 if ((*val_name_buf
)[0]) {
1153 const WCHAR val_start
[] = {'"','%','s','"','=',0};
1156 REGPROC_export_string(line_buf
, line_buf_size
, &line_len
, *val_name_buf
, lstrlenW(*val_name_buf
));
1157 REGPROC_resize_char_buffer(val_name_buf
, val_name_size
, lstrlenW(*line_buf
) + 1);
1158 lstrcpyW(*val_name_buf
, *line_buf
);
1160 line_len
= 3 + lstrlenW(*val_name_buf
);
1161 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
);
1162 sprintfW(*line_buf
, val_start
, *val_name_buf
);
1164 const WCHAR std_val
[] = {'@','=',0};
1166 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
);
1167 lstrcpyW(*line_buf
, std_val
);
1170 switch (value_type
) {
1173 WCHAR
* wstr
= (WCHAR
*)*val_buf
;
1175 if (val_size1
< sizeof(WCHAR
) || val_size1
% sizeof(WCHAR
) ||
1176 wstr
[val_size1
/ sizeof(WCHAR
) - 1]) {
1177 REGPROC_export_binary(line_buf
, line_buf_size
, &line_len
, value_type
, *val_buf
, val_size1
, unicode
);
1179 const WCHAR start
[] = {'"',0};
1180 const WCHAR end
[] = {'"','\r','\n',0};
1183 len
= lstrlenW(start
);
1184 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
+ len
);
1185 lstrcpyW(*line_buf
+ line_len
, start
);
1188 REGPROC_export_string(line_buf
, line_buf_size
, &line_len
, wstr
, lstrlenW(wstr
));
1190 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
+ lstrlenW(end
));
1191 lstrcpyW(*line_buf
+ line_len
, end
);
1198 WCHAR format
[] = {'d','w','o','r','d',':','%','0','8','x','\r','\n',0};
1200 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
+ 15);
1201 sprintfW(*line_buf
+ line_len
, format
, *((DWORD
*)*val_buf
));
1207 output_message(STRING_UNSUPPORTED_TYPE
, reg_type_to_wchar(value_type
), *reg_key_name_buf
);
1208 output_message(STRING_EXPORT_AS_BINARY
, *val_name_buf
);
1215 REGPROC_export_binary(line_buf
, line_buf_size
, &line_len
, value_type
, *val_buf
, val_size1
, unicode
);
1217 REGPROC_write_line(file
, *line_buf
, unicode
);
1223 (*reg_key_name_buf
)[curr_len
] = '\\';
1225 DWORD buf_size
= *reg_key_name_size
- curr_len
- 1;
1227 ret
= RegEnumKeyExW(key
, i
, *reg_key_name_buf
+ curr_len
+ 1, &buf_size
,
1228 NULL
, NULL
, NULL
, NULL
);
1229 if (ret
== ERROR_MORE_DATA
) {
1230 /* Increase the size of the buffer and retry */
1231 REGPROC_resize_char_buffer(reg_key_name_buf
, reg_key_name_size
, curr_len
+ 1 + buf_size
);
1232 } else if (ret
== ERROR_SUCCESS
) {
1236 if (RegOpenKeyW(key
, *reg_key_name_buf
+ curr_len
+ 1,
1237 &subkey
) == ERROR_SUCCESS
) {
1238 export_hkey(file
, subkey
, reg_key_name_buf
, reg_key_name_size
,
1239 val_name_buf
, val_name_size
, val_buf
, val_size
,
1240 line_buf
, line_buf_size
, unicode
);
1241 RegCloseKey(subkey
);
1247 (*reg_key_name_buf
)[curr_len
] = '\0';
1250 /******************************************************************************
1251 * Open file in binary mode for export.
1253 static FILE *REGPROC_open_export_file(WCHAR
*file_name
, BOOL unicode
)
1258 if (strncmpW(file_name
,&dash
,1)==0) {
1260 _setmode(_fileno(file
), _O_BINARY
);
1263 WCHAR wb_mode
[] = {'w','b',0};
1264 WCHAR regedit
[] = {'r','e','g','e','d','i','t',0};
1266 file
= _wfopen(file_name
, wb_mode
);
1269 output_message(STRING_CANNOT_OPEN_FILE
, file_name
);
1275 const BYTE unicode_seq
[] = {0xff,0xfe};
1276 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','\r','\n'};
1277 fwrite(unicode_seq
, sizeof(BYTE
), sizeof(unicode_seq
)/sizeof(unicode_seq
[0]), file
);
1278 fwrite(header
, sizeof(WCHAR
), sizeof(header
)/sizeof(header
[0]), file
);
1281 fputs("REGEDIT4\r\n", file
);
1287 /******************************************************************************
1288 * Writes contents of the registry key to the specified file stream.
1291 * file_name - name of a file to export registry branch to.
1292 * reg_key_name - registry branch to export. The whole registry is exported if
1293 * reg_key_name is NULL or contains an empty string.
1295 BOOL
export_registry_key(WCHAR
*file_name
, WCHAR
*reg_key_name
, DWORD format
)
1297 WCHAR
*reg_key_name_buf
;
1298 WCHAR
*val_name_buf
;
1301 DWORD reg_key_name_size
= KEY_MAX_LEN
;
1302 DWORD val_name_size
= KEY_MAX_LEN
;
1303 DWORD val_size
= REG_VAL_BUF_SIZE
;
1304 DWORD line_buf_size
= KEY_MAX_LEN
+ REG_VAL_BUF_SIZE
;
1306 BOOL unicode
= (format
== REG_FORMAT_5
);
1308 reg_key_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1309 reg_key_name_size
* sizeof(*reg_key_name_buf
));
1310 val_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1311 val_name_size
* sizeof(*val_name_buf
));
1312 val_buf
= HeapAlloc(GetProcessHeap(), 0, val_size
);
1313 line_buf
= HeapAlloc(GetProcessHeap(), 0, line_buf_size
* sizeof(*line_buf
));
1314 CHECK_ENOUGH_MEMORY(reg_key_name_buf
&& val_name_buf
&& val_buf
&& line_buf
);
1316 if (reg_key_name
&& reg_key_name
[0]) {
1318 WCHAR
*branch_name
= NULL
;
1321 REGPROC_resize_char_buffer(®_key_name_buf
, ®_key_name_size
,
1322 lstrlenW(reg_key_name
));
1323 lstrcpyW(reg_key_name_buf
, reg_key_name
);
1325 /* open the specified key */
1326 if (!parseKeyName(reg_key_name
, ®_key_class
, &branch_name
)) {
1327 output_message(STRING_INCORRECT_REG_CLASS
, reg_key_name
);
1330 if (!branch_name
[0]) {
1331 /* no branch - registry class is specified */
1332 file
= REGPROC_open_export_file(file_name
, unicode
);
1333 export_hkey(file
, reg_key_class
,
1334 ®_key_name_buf
, ®_key_name_size
,
1335 &val_name_buf
, &val_name_size
,
1336 &val_buf
, &val_size
, &line_buf
,
1337 &line_buf_size
, unicode
);
1338 } else if (RegOpenKeyW(reg_key_class
, branch_name
, &key
) == ERROR_SUCCESS
) {
1339 file
= REGPROC_open_export_file(file_name
, unicode
);
1340 export_hkey(file
, key
,
1341 ®_key_name_buf
, ®_key_name_size
,
1342 &val_name_buf
, &val_name_size
,
1343 &val_buf
, &val_size
, &line_buf
,
1344 &line_buf_size
, unicode
);
1347 output_message(STRING_REG_KEY_NOT_FOUND
, reg_key_name
);
1352 /* export all registry classes */
1353 file
= REGPROC_open_export_file(file_name
, unicode
);
1354 for (i
= 0; i
< ARRAY_SIZE(reg_class_keys
); i
++) {
1355 /* do not export HKEY_CLASSES_ROOT */
1356 if (reg_class_keys
[i
] != HKEY_CLASSES_ROOT
&&
1357 reg_class_keys
[i
] != HKEY_CURRENT_USER
&&
1358 reg_class_keys
[i
] != HKEY_CURRENT_CONFIG
&&
1359 reg_class_keys
[i
] != HKEY_DYN_DATA
) {
1360 lstrcpyW(reg_key_name_buf
, reg_class_namesW
[i
]);
1361 export_hkey(file
, reg_class_keys
[i
],
1362 ®_key_name_buf
, ®_key_name_size
,
1363 &val_name_buf
, &val_name_size
,
1364 &val_buf
, &val_size
, &line_buf
,
1365 &line_buf_size
, unicode
);
1373 HeapFree(GetProcessHeap(), 0, reg_key_name
);
1374 HeapFree(GetProcessHeap(), 0, val_name_buf
);
1375 HeapFree(GetProcessHeap(), 0, val_buf
);
1376 HeapFree(GetProcessHeap(), 0, line_buf
);
1380 /******************************************************************************
1381 * Reads contents of the specified file into the registry.
1383 BOOL
import_registry_file(FILE* reg_file
)
1388 if (fread( s
, 2, 1, reg_file
) == 1)
1390 if (s
[0] == 0xff && s
[1] == 0xfe)
1392 processRegLinesW(reg_file
);
1395 processRegLinesA(reg_file
, (char*)s
);
1403 /******************************************************************************
1404 * Removes the registry key with all subkeys. Parses full key name.
1407 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1408 * empty, points to register key class, does not exist.
1410 void delete_registry_key(WCHAR
*reg_key_name
)
1412 WCHAR
*key_name
= NULL
;
1415 if (!reg_key_name
|| !reg_key_name
[0])
1418 if (!parseKeyName(reg_key_name
, &key_class
, &key_name
)) {
1419 output_message(STRING_INCORRECT_REG_CLASS
, reg_key_name
);
1423 output_message(STRING_DELETE_REG_CLASS_FAILED
, reg_key_name
);
1427 RegDeleteTreeW(key_class
, key_name
);