2 * Registry processing routines. Routines, common for registry
3 * processing frontends.
5 * Copyright (C) 1999 Sylvain St-Germain
6 * Copyright (C) 2002 Andriy Palamarchuk
7 * Copyright (C) 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
28 #define REG_VAL_BUF_SIZE 4096
30 /* maximal number of characters in hexadecimal data line,
31 * including the indentation, but not including the '\' character
33 #define REG_FILE_HEX_LINE_LEN (2 + 25 * 3)
35 static const CHAR
*reg_class_names
[] =
37 "HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_CLASSES_ROOT",
38 "HKEY_CURRENT_CONFIG", "HKEY_CURRENT_USER", "HKEY_DYN_DATA"
41 #define REG_CLASS_NUMBER (COUNT_OF(reg_class_names))
43 const WCHAR
* reg_class_namesW
[REG_CLASS_NUMBER
] =
45 L
"HKEY_LOCAL_MACHINE", L
"HKEY_USERS", L
"HKEY_CLASSES_ROOT",
46 L
"HKEY_CURRENT_CONFIG", L
"HKEY_CURRENT_USER", L
"HKEY_DYN_DATA"
49 static HKEY reg_class_keys
[REG_CLASS_NUMBER
] =
51 HKEY_LOCAL_MACHINE
, HKEY_USERS
, HKEY_CLASSES_ROOT
,
52 HKEY_CURRENT_CONFIG
, HKEY_CURRENT_USER
, HKEY_DYN_DATA
56 #define NOT_ENOUGH_MEMORY 1
59 /* processing macros */
61 /* common check of memory allocation results */
62 #define CHECK_ENOUGH_MEMORY(p) \
65 fprintf(stderr,"%S: file %s, line %d: Not enough memory\n", \
66 getAppName(), __FILE__, __LINE__); \
67 exit(NOT_ENOUGH_MEMORY); \
70 /******************************************************************************
71 * Allocates memory and converts input from multibyte to wide chars
72 * Returned string must be freed by the caller
74 WCHAR
* GetWideString(const char* strA
)
79 int len
= MultiByteToWideChar(CP_ACP
, 0, strA
, -1, NULL
, 0);
81 strW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
82 CHECK_ENOUGH_MEMORY(strW
);
83 MultiByteToWideChar(CP_ACP
, 0, strA
, -1, strW
, len
);
89 /******************************************************************************
90 * Allocates memory and converts input from multibyte to wide chars
91 * Returned string must be freed by the caller
93 static WCHAR
* GetWideStringN(const char* strA
, int chars
, DWORD
*len
)
98 *len
= MultiByteToWideChar(CP_ACP
, 0, strA
, chars
, NULL
, 0);
100 strW
= HeapAlloc(GetProcessHeap(), 0, *len
* sizeof(WCHAR
));
101 CHECK_ENOUGH_MEMORY(strW
);
102 MultiByteToWideChar(CP_ACP
, 0, strA
, chars
, strW
, *len
);
109 /******************************************************************************
110 * Allocates memory and converts input from wide chars to multibyte
111 * Returned string must be freed by the caller
113 char* GetMultiByteString(const WCHAR
* strW
)
118 int len
= WideCharToMultiByte(CP_ACP
, 0, strW
, -1, NULL
, 0, NULL
, NULL
);
120 strA
= HeapAlloc(GetProcessHeap(), 0, len
);
121 CHECK_ENOUGH_MEMORY(strA
);
122 WideCharToMultiByte(CP_ACP
, 0, strW
, -1, strA
, len
, NULL
, NULL
);
128 /******************************************************************************
129 * Allocates memory and converts input from wide chars to multibyte
130 * Returned string must be freed by the caller
132 static char* GetMultiByteStringN(const WCHAR
* strW
, int chars
, DWORD
* len
)
137 *len
= WideCharToMultiByte(CP_ACP
, 0, strW
, chars
, NULL
, 0, NULL
, NULL
);
139 strA
= HeapAlloc(GetProcessHeap(), 0, *len
);
140 CHECK_ENOUGH_MEMORY(strA
);
141 WideCharToMultiByte(CP_ACP
, 0, strW
, chars
, strA
, *len
, NULL
, NULL
);
148 /******************************************************************************
149 * Converts a hex representation of a DWORD into a DWORD.
151 static BOOL
convertHexToDWord(WCHAR
* str
, DWORD
*dw
)
156 WideCharToMultiByte(CP_ACP
, 0, str
, -1, buf
, 9, NULL
, NULL
);
157 if (lstrlenW(str
) > 8 || sscanf(buf
, "%lx%c", dw
, &dummy
) != 1)
159 fprintf(stderr
,"%S: ERROR, invalid hex value\n", getAppName());
165 /******************************************************************************
166 * Converts a hex comma separated values list into a binary string.
168 static BYTE
* convertHexCSVToHex(WCHAR
*str
, DWORD
*size
)
173 /* The worst case is 1 digit + 1 comma per byte */
174 *size
=(lstrlenW(str
)+1)/2;
175 data
=HeapAlloc(GetProcessHeap(), 0, *size
);
176 CHECK_ENOUGH_MEMORY(data
);
186 wc
= wcstoul(s
,&end
, 16);
187 if (end
== s
|| wc
> 0xff || (*end
&& *end
!= L
','))
189 char* strA
= GetMultiByteString(s
);
190 fprintf(stderr
,"%S: ERROR converting CSV hex stream. Invalid value at '%s'\n",
192 HeapFree(GetProcessHeap(), 0, data
);
193 HeapFree(GetProcessHeap(), 0, strA
);
205 /******************************************************************************
206 * This function returns the HKEY associated with the data type encoded in the
207 * value. It modifies the input parameter (key value) in order to skip this
208 * "now useless" data type information.
210 * Note: Updated based on the algorithm used in 'server/registry.c'
212 static DWORD
getDataType(LPWSTR
*lpValue
, DWORD
* parse_type
)
222 static const WCHAR quote
[] = {'"'};
223 static const WCHAR str
[] = {'s','t','r',':','"'};
224 static const WCHAR str2
[] = {'s','t','r','(','2',')',':','"'};
225 static const WCHAR hex
[] = {'h','e','x',':'};
226 static const WCHAR dword
[] = {'d','w','o','r','d',':'};
227 static const WCHAR hexp
[] = {'h','e','x','('};
229 static const struct data_type data_types
[] = { /* actual type */ /* type to assume for parsing */
230 { quote
, 1, REG_SZ
, REG_SZ
},
231 { str
, 5, REG_SZ
, REG_SZ
},
232 { str2
, 8, REG_EXPAND_SZ
, REG_SZ
},
233 { hex
, 4, REG_BINARY
, REG_BINARY
},
234 { dword
, 6, REG_DWORD
, REG_DWORD
},
235 { hexp
, 4, -1, REG_BINARY
},
239 const struct data_type
*ptr
;
242 for (ptr
= data_types
; ptr
->tag
; ptr
++)
244 if (wcsncmp(ptr
->tag
, *lpValue
, ptr
->len
))
248 *parse_type
= ptr
->parse_type
;
255 /* "hex(xx):" is special */
256 type
= (int)wcstoul( *lpValue
, &end
, 16 );
257 if (**lpValue
=='\0' || *end
!=')' || *(end
+1)!=':')
268 *parse_type
=REG_NONE
;
272 /******************************************************************************
273 * Replaces escape sequences with the characters.
275 static void REGPROC_unescape_string(WCHAR
* str
)
277 int str_idx
= 0; /* current character under analysis */
278 int val_idx
= 0; /* the last character of the unescaped string */
279 int len
= lstrlenW(str
);
280 for (str_idx
= 0; str_idx
< len
; str_idx
++, val_idx
++)
282 if (str
[str_idx
] == '\\')
285 switch (str
[str_idx
])
292 str
[val_idx
] = str
[str_idx
];
295 fprintf(stderr
,"Warning! Unrecognized escape sequence: \\%C'\n",
297 str
[val_idx
] = str
[str_idx
];
303 str
[val_idx
] = str
[str_idx
];
309 static BOOL
parseKeyName(LPWSTR lpKeyName
, HKEY
*hKey
, LPWSTR
*lpKeyPath
)
311 WCHAR
* lpSlash
= NULL
;
314 if (lpKeyName
== NULL
)
317 for(i
= 0; *(lpKeyName
+ i
) != 0; i
++)
319 if(*(lpKeyName
+i
) == '\\')
321 lpSlash
= lpKeyName
+ i
;
328 len
= lpSlash
-lpKeyName
;
332 len
= lstrlenW(lpKeyName
);
333 lpSlash
= lpKeyName
+len
;
337 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++)
339 if (CompareStringW(LOCALE_USER_DEFAULT
, 0, lpKeyName
, len
, reg_class_namesW
[i
], len
) == CSTR_EQUAL
&&
340 len
== lstrlenW(reg_class_namesW
[i
]))
342 *hKey
= reg_class_keys
[i
];
350 if (*lpSlash
!= '\0')
352 *lpKeyPath
= lpSlash
;
356 /* Globals used by the setValue() & co */
357 static LPSTR currentKeyName
;
358 static HKEY currentKeyHandle
= NULL
;
360 /******************************************************************************
361 * Sets the value with name val_name to the data in val_data for the currently
365 * val_name - name of the registry value
366 * val_data - registry value data
368 static LONG
setValue(WCHAR
* val_name
, WCHAR
* val_data
, BOOL is_unicode
)
371 DWORD dwDataType
, dwParseType
;
374 WCHAR del
[] = {'-',0};
376 if ( (val_name
== NULL
) || (val_data
== NULL
) )
377 return ERROR_INVALID_PARAMETER
;
379 if (lstrcmpW(val_data
, del
) == 0)
381 res
=RegDeleteValueW(currentKeyHandle
,val_name
);
382 return (res
== ERROR_FILE_NOT_FOUND
? ERROR_SUCCESS
: res
);
385 /* Get the data type stored into the value field */
386 dwDataType
= getDataType(&val_data
, &dwParseType
);
388 if (dwParseType
== REG_SZ
) /* no conversion for string */
390 REGPROC_unescape_string(val_data
);
391 /* Compute dwLen after REGPROC_unescape_string because it may
392 * have changed the string length and we don't want to store
393 * the extra garbage in the registry.
395 dwLen
= lstrlenW(val_data
);
396 if (dwLen
>0 && val_data
[dwLen
-1]=='"')
399 val_data
[dwLen
]='\0';
401 lpbData
= (BYTE
*) val_data
;
402 dwLen
++; /* include terminating null */
403 dwLen
= dwLen
* sizeof(WCHAR
); /* size is in bytes */
405 else if (dwParseType
== REG_DWORD
) /* Convert the dword types */
407 if (!convertHexToDWord(val_data
, &dwData
))
408 return ERROR_INVALID_DATA
;
409 lpbData
= (BYTE
*)&dwData
;
410 dwLen
= sizeof(dwData
);
412 else if (dwParseType
== REG_BINARY
) /* Convert the binary data */
414 lpbData
= convertHexCSVToHex(val_data
, &dwLen
);
416 return ERROR_INVALID_DATA
;
418 if((dwDataType
== REG_MULTI_SZ
|| dwDataType
== REG_EXPAND_SZ
) && !is_unicode
)
420 LPBYTE tmp
= lpbData
;
421 lpbData
= (LPBYTE
)GetWideStringN((char*)lpbData
, dwLen
, &dwLen
);
422 dwLen
*= sizeof(WCHAR
);
423 HeapFree(GetProcessHeap(), 0, tmp
);
426 else /* unknown format */
428 fprintf(stderr
,"%S: ERROR, unknown data format\n", getAppName());
429 return ERROR_INVALID_DATA
;
432 res
= RegSetValueExW(
439 if (dwParseType
== REG_BINARY
)
440 HeapFree(GetProcessHeap(), 0, lpbData
);
444 /******************************************************************************
445 * A helper function for processRegEntry() that opens the current key.
446 * That key must be closed by calling closeKey().
448 static LONG
openKeyW(WCHAR
* stdInput
)
456 if (stdInput
== NULL
)
457 return ERROR_INVALID_PARAMETER
;
459 /* Get the registry class */
460 if (!parseKeyName(stdInput
, &keyClass
, &keyPath
))
461 return ERROR_INVALID_PARAMETER
;
463 res
= RegCreateKeyExW(keyClass
, /* Class */
464 keyPath
, /* Sub Key */
466 NULL
, /* object type */
467 REG_OPTION_NON_VOLATILE
, /* option, REG_OPTION_NON_VOLATILE ... */
468 KEY_ALL_ACCESS
, /* access mask, KEY_ALL_ACCESS */
469 NULL
, /* security attribute */
470 ¤tKeyHandle
, /* result */
471 &dwDisp
); /* disposition, REG_CREATED_NEW_KEY or
472 REG_OPENED_EXISTING_KEY */
474 if (res
== ERROR_SUCCESS
)
475 currentKeyName
= GetMultiByteString(stdInput
);
477 currentKeyHandle
= NULL
;
483 /******************************************************************************
484 * Close the currently opened key.
486 static void closeKey(void)
488 if (currentKeyHandle
)
490 HeapFree(GetProcessHeap(), 0, currentKeyName
);
491 RegCloseKey(currentKeyHandle
);
492 currentKeyHandle
= NULL
;
496 /******************************************************************************
497 * This function is a wrapper for the setValue function. It prepares the
498 * land and cleans the area once completed.
499 * Note: this function modifies the line parameter.
501 * line - registry file unwrapped line. Should have the registry value name and
502 * complete registry value data.
504 static void processSetValue(WCHAR
* line
, BOOL is_unicode
)
506 WCHAR
* val_name
; /* registry value name */
507 WCHAR
* val_data
; /* registry value data */
508 int line_idx
= 0; /* current character under analysis */
512 while ( iswspace(line
[line_idx
]) ) line_idx
++;
513 if (line
[line_idx
] == '@' && line
[line_idx
+ 1] == '=')
515 line
[line_idx
] = '\0';
519 else if (line
[line_idx
] == '\"')
522 val_name
= line
+ line_idx
;
525 if (line
[line_idx
] == '\\') /* skip escaped character */
531 if (line
[line_idx
] == '\"')
533 line
[line_idx
] = '\0';
543 while ( iswspace(line
[line_idx
]) ) line_idx
++;
544 if (line
[line_idx
] != '=')
547 line
[line_idx
] = '\"';
548 lineA
= GetMultiByteString(line
);
549 fprintf(stderr
,"Warning! unrecognized line:\n%s\n", lineA
);
550 HeapFree(GetProcessHeap(), 0, lineA
);
557 char* lineA
= GetMultiByteString(line
);
558 fprintf(stderr
,"Warning! unrecognized line:\n%s\n", lineA
);
559 HeapFree(GetProcessHeap(), 0, lineA
);
562 line_idx
++; /* skip the '=' character */
564 while ( iswspace(line
[line_idx
]) ) line_idx
++;
565 val_data
= line
+ line_idx
;
566 /* trim trailing blanks */
567 line_idx
= lstrlenW(val_data
);
568 while (line_idx
> 0 && iswspace(val_data
[line_idx
-1])) line_idx
--;
569 val_data
[line_idx
] = '\0';
571 REGPROC_unescape_string(val_name
);
572 res
= setValue(val_name
, val_data
, is_unicode
);
573 if ( res
!= ERROR_SUCCESS
)
575 char* val_nameA
= GetMultiByteString(val_name
);
576 char* val_dataA
= GetMultiByteString(val_data
);
577 fprintf(stderr
,"%S: ERROR Key %s not created. Value: %s, Data: %s\n",
582 HeapFree(GetProcessHeap(), 0, val_nameA
);
583 HeapFree(GetProcessHeap(), 0, val_dataA
);
587 /******************************************************************************
588 * This function receives the currently read entry and performs the
589 * corresponding action.
590 * isUnicode affects parsing of REG_MULTI_SZ values
592 static void processRegEntry(WCHAR
* stdInput
, BOOL isUnicode
)
595 * We encountered the end of the file, make sure we
596 * close the opened key and exit
598 if (stdInput
== NULL
)
604 if ( stdInput
[0] == L
'[') /* We are reading a new key */
607 closeKey(); /* Close the previous key */
609 /* Get rid of the square brackets */
611 keyEnd
= wcsrchr(stdInput
, L
']');
615 /* delete the key if we encounter '-' at the start of reg key */
616 if ( stdInput
[0] == '-')
618 delete_registry_key(stdInput
+ 1);
620 else if ( openKeyW(stdInput
) != ERROR_SUCCESS
)
622 char* stdInputA
= GetMultiByteString(stdInput
);
623 fprintf(stderr
,"%S: setValue failed to open key %s\n",
624 getAppName(), stdInputA
);
625 HeapFree(GetProcessHeap(), 0, stdInputA
);
628 else if( currentKeyHandle
&&
629 (( stdInput
[0] == '@') || /* reading a default @=data pair */
630 ( stdInput
[0] == '\"'))) /* reading a new value=data pair */
632 processSetValue(stdInput
, isUnicode
);
636 /* Since we are assuming that the file format is valid we must be
637 * reading a blank line which indicates the end of this key processing
643 /******************************************************************************
644 * Processes a registry file.
645 * Correctly processes comments (in # form), line continuation.
648 * in - input stream to read from
650 static void processRegLinesA(FILE *in
)
652 LPSTR line
= NULL
; /* line read from input stream */
653 ULONG lineSize
= REG_VAL_BUF_SIZE
;
655 line
= HeapAlloc(GetProcessHeap(), 0, lineSize
);
656 CHECK_ENOUGH_MEMORY(line
);
660 LPSTR s
; /* The pointer into line for where the current fgets should read */
667 size_t size_remaining
;
669 char *s_eol
; /* various local uses */
671 /* Do we need to expand the buffer ? */
672 assert (s
>= line
&& s
<= line
+ lineSize
);
673 size_remaining
= lineSize
- (s
-line
);
674 if (size_remaining
< 2) /* room for 1 character and the \0 */
677 size_t new_size
= lineSize
+ REG_VAL_BUF_SIZE
;
678 if (new_size
> lineSize
) /* no arithmetic overflow */
679 new_buffer
= HeapReAlloc (GetProcessHeap(), 0, line
, new_size
);
682 CHECK_ENOUGH_MEMORY(new_buffer
);
684 s
= line
+ lineSize
- size_remaining
;
686 size_remaining
= lineSize
- (s
-line
);
689 /* Get as much as possible into the buffer, terminated either by
690 * eof, error, eol or getting the maximum amount. Abort on error.
692 size_to_get
= (size_remaining
> INT_MAX
? INT_MAX
: size_remaining
);
694 check
= fgets (s
, size_to_get
, in
);
700 perror ("While reading input");
707 /* It is not clear to me from the definition that the
708 * contents of the buffer are well defined on detecting
709 * an eof without managing to read anything.
714 /* If we didn't read the eol nor the eof go around for the rest */
715 s_eol
= strchr (s
, '\n');
716 if (!feof (in
) && !s_eol
)
718 s
= strchr (s
, '\0');
719 /* It should be s + size_to_get - 1 but this is safer */
723 /* If it is a comment line then discard it and go around again */
730 /* Remove any line feed. Leave s_eol on the \0 */
734 if (s_eol
> line
&& *(s_eol
-1) == '\r')
738 s_eol
= strchr (s
, '\0');
740 /* If there is a concatenating \\ then go around again */
741 if (s_eol
> line
&& *(s_eol
-1) == '\\')
750 while(c
== ' ' || c
== '\t');
754 fprintf(stderr
,"%S: ERROR - invalid continuation.\n",
765 lineW
= GetWideString(line
);
767 break; /* That is the full virtual line */
770 processRegEntry(lineW
, FALSE
);
771 HeapFree(GetProcessHeap(), 0, lineW
);
773 processRegEntry(NULL
, FALSE
);
775 HeapFree(GetProcessHeap(), 0, line
);
778 static void processRegLinesW(FILE *in
)
780 WCHAR
* buf
= NULL
; /* line read from input stream */
781 ULONG lineSize
= REG_VAL_BUF_SIZE
;
782 size_t CharsInBuf
= -1;
784 WCHAR
* s
; /* The pointer into buf for where the current fgets should read */
785 WCHAR
* line
; /* The start of the current line */
787 buf
= HeapAlloc(GetProcessHeap(), 0, lineSize
* sizeof(WCHAR
));
788 CHECK_ENOUGH_MEMORY(buf
);
795 size_t size_remaining
;
797 WCHAR
*s_eol
= NULL
; /* various local uses */
799 /* Do we need to expand the buffer ? */
800 assert (s
>= buf
&& s
<= buf
+ lineSize
);
801 size_remaining
= lineSize
- (s
-buf
);
802 if (size_remaining
< 2) /* room for 1 character and the \0 */
805 size_t new_size
= lineSize
+ (REG_VAL_BUF_SIZE
/ sizeof(WCHAR
));
806 if (new_size
> lineSize
) /* no arithmetic overflow */
807 new_buffer
= HeapReAlloc (GetProcessHeap(), 0, buf
, new_size
* sizeof(WCHAR
));
810 CHECK_ENOUGH_MEMORY(new_buffer
);
813 s
= buf
+ lineSize
- size_remaining
;
815 size_remaining
= lineSize
- (s
-buf
);
818 /* Get as much as possible into the buffer, terminated either by
819 * eof, error or getting the maximum amount. Abort on error.
821 size_to_get
= (size_remaining
> INT_MAX
? INT_MAX
: size_remaining
);
823 CharsInBuf
= fread(s
, sizeof(WCHAR
), size_to_get
- 1, in
);
830 perror ("While reading input");
837 /* It is not clear to me from the definition that the
838 * contents of the buffer are well defined on detecting
839 * an eof without managing to read anything.
844 /* If we didn't read the eol nor the eof go around for the rest */
847 s_eol
= wcschr(line
, '\n');
851 /* Move the stub of the line to the start of the buffer so
852 * we get the maximum space to read into, and so we don't
853 * have to recalculate 'line' if the buffer expands */
854 MoveMemory(buf
, line
, (lstrlenW(line
) + 1) * sizeof(WCHAR
));
856 s
= wcschr(line
, '\0');
860 /* If it is a comment line then discard it and go around again */
867 /* If there is a concatenating \\ then go around again */
868 if ((*(s_eol
-1) == '\\') ||
869 (*(s_eol
-1) == '\r' && *(s_eol
-2) == '\\'))
871 WCHAR
* NextLine
= s_eol
;
873 while(*(NextLine
+1) == ' ' || *(NextLine
+1) == '\t')
878 if(*(s_eol
-1) == '\r')
881 MoveMemory(s_eol
- 1, NextLine
, (CharsInBuf
- (NextLine
- s
) + 1)*sizeof(WCHAR
));
882 CharsInBuf
-= NextLine
- s_eol
+ 1;
887 /* Remove any line feed. Leave s_eol on the \0 */
891 if (s_eol
> buf
&& *(s_eol
-1) == '\r')
898 processRegEntry(line
, TRUE
);
901 continue; /* That is the full virtual line */
905 processRegEntry(NULL
, TRUE
);
907 HeapFree(GetProcessHeap(), 0, buf
);
910 /****************************************************************************
911 * REGPROC_print_error
913 * Print the message for GetLastError
916 static void REGPROC_print_error(void)
922 error_code
= GetLastError ();
923 status
= FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
924 NULL
, error_code
, 0, (LPTSTR
) &lpMsgBuf
, 0, NULL
);
927 fprintf(stderr
,"%S: Cannot display message for error %ld, status %ld\n",
928 getAppName(), error_code
, GetLastError());
936 /******************************************************************************
937 * Checks whether the buffer has enough room for the string or required size.
938 * Resizes the buffer if necessary.
941 * buffer - pointer to a buffer for string
942 * len - current length of the buffer in characters.
943 * required_len - length of the string to place to the buffer in characters.
944 * The length does not include the terminating null character.
946 static void REGPROC_resize_char_buffer(WCHAR
**buffer
, DWORD
*len
, DWORD required_len
)
949 if (required_len
> *len
)
953 *buffer
= HeapAlloc(GetProcessHeap(), 0, *len
* sizeof(**buffer
));
955 *buffer
= HeapReAlloc(GetProcessHeap(), 0, *buffer
, *len
* sizeof(**buffer
));
956 CHECK_ENOUGH_MEMORY(*buffer
);
960 /******************************************************************************
961 * Same as REGPROC_resize_char_buffer() but on a regular buffer.
964 * buffer - pointer to a buffer
965 * len - current size of the buffer in bytes
966 * required_size - size of the data to place in the buffer in bytes
968 static void REGPROC_resize_binary_buffer(BYTE
**buffer
, DWORD
*size
, DWORD required_size
)
970 if (required_size
> *size
)
972 *size
= required_size
;
974 *buffer
= HeapAlloc(GetProcessHeap(), 0, *size
);
976 *buffer
= HeapReAlloc(GetProcessHeap(), 0, *buffer
, *size
);
977 CHECK_ENOUGH_MEMORY(*buffer
);
981 /******************************************************************************
982 * Prints string str to file
984 static void REGPROC_export_string(WCHAR
**line_buf
, DWORD
*line_buf_size
, DWORD
*line_len
, WCHAR
*str
, DWORD str_len
)
989 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, *line_len
+ str_len
+ 10);
991 /* escaping characters */
993 for (i
= 0; i
< str_len
; i
++)
1000 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, *line_len
+ str_len
+ extra
);
1001 (*line_buf
)[pos
++] = '\\';
1002 (*line_buf
)[pos
++] = 'n';
1008 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, *line_len
+ str_len
+ extra
);
1009 (*line_buf
)[pos
++] = '\\';
1013 (*line_buf
)[pos
++] = c
;
1017 (*line_buf
)[pos
] = '\0';
1021 static void REGPROC_export_binary(WCHAR
**line_buf
, DWORD
*line_buf_size
, DWORD
*line_len
, DWORD type
, BYTE
*value
, DWORD value_size
, BOOL unicode
)
1023 DWORD hex_pos
, data_pos
;
1024 const WCHAR
*hex_prefix
;
1025 const WCHAR hex
[] = {'h','e','x',':',0};
1027 const WCHAR concat
[] = {'\\','\n',' ',' ',0};
1028 DWORD concat_prefix
, concat_len
;
1029 const WCHAR newline
[] = {'\n',0};
1030 CHAR
* value_multibyte
= NULL
;
1032 if (type
== REG_BINARY
)
1038 const WCHAR hex_format
[] = {'h','e','x','(','%','u',')',':',0};
1039 hex_prefix
= hex_buf
;
1040 wsprintfW(hex_buf
, hex_format
, type
);
1041 if ((type
== REG_SZ
|| type
== REG_EXPAND_SZ
|| type
== REG_MULTI_SZ
) && !unicode
)
1043 value_multibyte
= GetMultiByteStringN((WCHAR
*)value
, value_size
/ sizeof(WCHAR
), &value_size
);
1044 value
= (BYTE
*)value_multibyte
;
1048 concat_len
= lstrlenW(concat
);
1051 hex_pos
= *line_len
;
1052 *line_len
+= lstrlenW(hex_prefix
);
1053 data_pos
= *line_len
;
1054 *line_len
+= value_size
* 3;
1055 /* - The 2 spaces that concat places at the start of the
1056 * line effectively reduce the space available for data.
1057 * - If the value name and hex prefix are very long
1058 * ( > REG_FILE_HEX_LINE_LEN) then we may overestimate
1059 * the needed number of lines by one. But that's ok.
1060 * - The trailing linefeed takes the place of a comma so
1061 * it's accounted for already.
1063 *line_len
+= *line_len
/ (REG_FILE_HEX_LINE_LEN
- concat_prefix
) * concat_len
;
1064 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, *line_len
);
1065 lstrcpyW(*line_buf
+ hex_pos
, hex_prefix
);
1068 const WCHAR format
[] = {'%','0','2','x',0};
1071 column
= data_pos
; /* no line wrap yet */
1075 wsprintfW(*line_buf
+ data_pos
, format
, (unsigned int)value
[i
]);
1077 if (++i
== value_size
)
1080 (*line_buf
)[data_pos
++] = ',';
1084 if (column
>= REG_FILE_HEX_LINE_LEN
)
1086 lstrcpyW(*line_buf
+ data_pos
, concat
);
1087 data_pos
+= concat_len
;
1088 column
= concat_prefix
;
1092 lstrcpyW(*line_buf
+ data_pos
, newline
);
1093 HeapFree(GetProcessHeap(), 0, value_multibyte
);
1096 /******************************************************************************
1097 * Writes the given line to a file, in multi-byte or wide characters
1099 static void REGPROC_write_line(FILE *file
, const WCHAR
* str
, BOOL unicode
)
1104 for(i
= 0; str
[i
]; i
++)
1106 if (str
[i
] == L
'\n')
1107 fputwc(L
'\r', file
);
1108 fputwc(str
[i
], file
);
1113 char* strA
= GetMultiByteString(str
);
1115 HeapFree(GetProcessHeap(), 0, strA
);
1119 /******************************************************************************
1120 * Writes contents of the registry key to the specified file stream.
1123 * file - writable file stream to export registry branch to.
1124 * key - registry branch to export.
1125 * reg_key_name_buf - name of the key with registry class.
1126 * Is resized if necessary.
1127 * reg_key_name_size - length of the buffer for the registry class in characters.
1128 * val_name_buf - buffer for storing value name.
1129 * Is resized if necessary.
1130 * val_name_size - length of the buffer for storing value names in characters.
1131 * val_buf - buffer for storing values while extracting.
1132 * Is resized if necessary.
1133 * val_size - size of the buffer for storing values in bytes.
1135 static void export_hkey(FILE *file
, HKEY key
,
1136 WCHAR
**reg_key_name_buf
, DWORD
*reg_key_name_size
,
1137 WCHAR
**val_name_buf
, DWORD
*val_name_size
,
1138 BYTE
**val_buf
, DWORD
*val_size
,
1139 WCHAR
**line_buf
, DWORD
*line_buf_size
,
1142 DWORD max_sub_key_len
;
1143 DWORD max_val_name_len
;
1149 WCHAR key_format
[] = {'\n','[','%','s',']','\n',0};
1151 /* get size information and resize the buffers if necessary */
1152 if (RegQueryInfoKeyW(key
, NULL
, NULL
, NULL
, NULL
,
1153 &max_sub_key_len
, NULL
,
1154 NULL
, &max_val_name_len
, &max_val_size
, NULL
, NULL
1157 REGPROC_print_error();
1159 curr_len
= lstrlenW(*reg_key_name_buf
);
1160 REGPROC_resize_char_buffer(reg_key_name_buf
, reg_key_name_size
,
1161 max_sub_key_len
+ curr_len
+ 1);
1162 REGPROC_resize_char_buffer(val_name_buf
, val_name_size
,
1164 REGPROC_resize_binary_buffer(val_buf
, val_size
, max_val_size
);
1165 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, lstrlenW(*reg_key_name_buf
) + 4);
1166 /* output data for the current key */
1167 wsprintfW(*line_buf
, key_format
, *reg_key_name_buf
);
1168 REGPROC_write_line(file
, *line_buf
, unicode
);
1170 /* print all the values */
1176 DWORD val_name_size1
= *val_name_size
;
1177 DWORD val_size1
= *val_size
;
1178 ret
= RegEnumValueW(key
, i
, *val_name_buf
, &val_name_size1
, NULL
,
1179 &value_type
, *val_buf
, &val_size1
);
1180 if (ret
== ERROR_MORE_DATA
)
1182 /* Increase the size of the buffers and retry */
1183 REGPROC_resize_char_buffer(val_name_buf
, val_name_size
, val_name_size1
);
1184 REGPROC_resize_binary_buffer(val_buf
, val_size
, val_size1
);
1186 else if (ret
!= ERROR_SUCCESS
)
1189 if (ret
!= ERROR_NO_MORE_ITEMS
)
1191 REGPROC_print_error();
1199 if ((*val_name_buf
)[0])
1201 const WCHAR val_start
[] = {'"','%','s','"','=',0};
1204 REGPROC_export_string(line_buf
, line_buf_size
, &line_len
, *val_name_buf
, lstrlenW(*val_name_buf
));
1205 REGPROC_resize_char_buffer(val_name_buf
, val_name_size
, lstrlenW(*line_buf
) + 1);
1206 lstrcpyW(*val_name_buf
, *line_buf
);
1208 line_len
= 3 + lstrlenW(*val_name_buf
);
1209 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
);
1210 wsprintfW(*line_buf
, val_start
, *val_name_buf
);
1214 const WCHAR std_val
[] = {'@','=',0};
1216 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
);
1217 lstrcpyW(*line_buf
, std_val
);
1224 WCHAR
* wstr
= (WCHAR
*)*val_buf
;
1226 if (val_size1
< sizeof(WCHAR
) || val_size1
% sizeof(WCHAR
) ||
1227 wstr
[val_size1
/ sizeof(WCHAR
) - 1])
1229 REGPROC_export_binary(line_buf
, line_buf_size
, &line_len
, value_type
, *val_buf
, val_size1
, unicode
);
1233 const WCHAR start
[] = {'"',0};
1234 const WCHAR end
[] = {'"','\n',0};
1237 len
= lstrlenW(start
);
1238 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
+ len
);
1239 lstrcpyW(*line_buf
+ line_len
, start
);
1242 /* At this point we know wstr is '\0'-terminated
1243 * so we can substract 1 from the size
1245 REGPROC_export_string(line_buf
, line_buf_size
, &line_len
, wstr
, val_size1
/ sizeof(WCHAR
) - 1);
1247 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
+ lstrlenW(end
));
1248 lstrcpyW(*line_buf
+ line_len
, end
);
1255 WCHAR format
[] = {'d','w','o','r','d',':','%','0','8','x','\n',0};
1257 REGPROC_resize_char_buffer(line_buf
, line_buf_size
, line_len
+ 15);
1258 wsprintfW(*line_buf
+ line_len
, format
, *((DWORD
*)*val_buf
));
1264 char* key_nameA
= GetMultiByteString(*reg_key_name_buf
);
1265 char* value_nameA
= GetMultiByteString(*val_name_buf
);
1266 fprintf(stderr
,"%S: warning - unsupported registry format '%ld', "
1267 "treat as binary\n",
1268 getAppName(), value_type
);
1269 fprintf(stderr
,"key name: \"%s\"\n", key_nameA
);
1270 fprintf(stderr
,"value name:\"%s\"\n\n", value_nameA
);
1271 HeapFree(GetProcessHeap(), 0, key_nameA
);
1272 HeapFree(GetProcessHeap(), 0, value_nameA
);
1279 REGPROC_export_binary(line_buf
, line_buf_size
, &line_len
, value_type
, *val_buf
, val_size1
, unicode
);
1281 REGPROC_write_line(file
, *line_buf
, unicode
);
1287 (*reg_key_name_buf
)[curr_len
] = '\\';
1290 DWORD buf_size
= *reg_key_name_size
- curr_len
- 1;
1292 ret
= RegEnumKeyExW(key
, i
, *reg_key_name_buf
+ curr_len
+ 1, &buf_size
,
1293 NULL
, NULL
, NULL
, NULL
);
1294 if (ret
== ERROR_MORE_DATA
)
1296 /* Increase the size of the buffer and retry */
1297 REGPROC_resize_char_buffer(reg_key_name_buf
, reg_key_name_size
, curr_len
+ 1 + buf_size
);
1299 else if (ret
!= ERROR_SUCCESS
)
1302 if (ret
!= ERROR_NO_MORE_ITEMS
)
1304 REGPROC_print_error();
1312 if (RegOpenKeyW(key
, *reg_key_name_buf
+ curr_len
+ 1,
1313 &subkey
) == ERROR_SUCCESS
)
1315 export_hkey(file
, subkey
, reg_key_name_buf
, reg_key_name_size
,
1316 val_name_buf
, val_name_size
, val_buf
, val_size
,
1317 line_buf
, line_buf_size
, unicode
);
1318 RegCloseKey(subkey
);
1322 REGPROC_print_error();
1326 (*reg_key_name_buf
)[curr_len
] = '\0';
1329 /******************************************************************************
1330 * Open file for export.
1332 static FILE *REGPROC_open_export_file(WCHAR
*file_name
, BOOL unicode
)
1337 if (wcsncmp(file_name
, &dash
, 1) == 0)
1342 file
= _wfopen(file_name
, L
"wb");
1344 file
= _wfopen(file_name
, L
"w");
1347 CHAR
* file_nameA
= GetMultiByteString(file_name
);
1349 fprintf(stderr
,"%S: Can't open file \"%s\"\n", getAppName(), file_nameA
);
1350 HeapFree(GetProcessHeap(), 0, file_nameA
);
1356 const BYTE unicode_seq
[] = {0xff,0xfe};
1357 const WCHAR header
[] = L
"Windows Registry Editor Version 5.00\r\n";
1358 fwrite(unicode_seq
, sizeof(BYTE
), COUNT_OF(unicode_seq
), file
);
1359 fwrite(header
, sizeof(WCHAR
), lstrlenW(header
), file
);
1363 fputs("REGEDIT4\n", file
);
1369 /******************************************************************************
1370 * Writes contents of the registry key to the specified file stream.
1373 * file_name - name of a file to export registry branch to.
1374 * reg_key_name - registry branch to export. The whole registry is exported if
1375 * reg_key_name is NULL or contains an empty string.
1377 BOOL
export_registry_key(WCHAR
*file_name
, WCHAR
*reg_key_name
, DWORD format
)
1379 WCHAR
*reg_key_name_buf
;
1380 WCHAR
*val_name_buf
;
1383 DWORD reg_key_name_size
= KEY_MAX_LEN
;
1384 DWORD val_name_size
= KEY_MAX_LEN
;
1385 DWORD val_size
= REG_VAL_BUF_SIZE
;
1386 DWORD line_buf_size
= KEY_MAX_LEN
+ REG_VAL_BUF_SIZE
;
1388 BOOL unicode
= (format
== REG_FORMAT_5
);
1390 reg_key_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1391 reg_key_name_size
* sizeof(*reg_key_name_buf
));
1392 val_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1393 val_name_size
* sizeof(*val_name_buf
));
1394 val_buf
= HeapAlloc(GetProcessHeap(), 0, val_size
);
1395 line_buf
= HeapAlloc(GetProcessHeap(), 0, line_buf_size
* sizeof(*line_buf
));
1396 CHECK_ENOUGH_MEMORY(reg_key_name_buf
&& val_name_buf
&& val_buf
&& line_buf
);
1398 if (reg_key_name
&& reg_key_name
[0])
1401 WCHAR
*branch_name
= NULL
;
1404 REGPROC_resize_char_buffer(®_key_name_buf
, ®_key_name_size
,
1405 lstrlenW(reg_key_name
));
1406 lstrcpyW(reg_key_name_buf
, reg_key_name
);
1408 /* open the specified key */
1409 if (!parseKeyName(reg_key_name
, ®_key_class
, &branch_name
))
1411 CHAR
* key_nameA
= GetMultiByteString(reg_key_name
);
1412 fprintf(stderr
,"%S: Incorrect registry class specification in '%s'\n",
1413 getAppName(), key_nameA
);
1414 HeapFree(GetProcessHeap(), 0, key_nameA
);
1417 if (!branch_name
[0])
1419 /* no branch - registry class is specified */
1420 file
= REGPROC_open_export_file(file_name
, unicode
);
1421 export_hkey(file
, reg_key_class
,
1422 ®_key_name_buf
, ®_key_name_size
,
1423 &val_name_buf
, &val_name_size
,
1424 &val_buf
, &val_size
, &line_buf
,
1425 &line_buf_size
, unicode
);
1427 else if (RegOpenKeyW(reg_key_class
, branch_name
, &key
) == ERROR_SUCCESS
)
1429 file
= REGPROC_open_export_file(file_name
, unicode
);
1430 export_hkey(file
, key
,
1431 ®_key_name_buf
, ®_key_name_size
,
1432 &val_name_buf
, &val_name_size
,
1433 &val_buf
, &val_size
, &line_buf
,
1434 &line_buf_size
, unicode
);
1439 CHAR
* key_nameA
= GetMultiByteString(reg_key_name
);
1440 fprintf(stderr
,"%S: Can't export. Registry key '%s' does not exist!\n",
1441 getAppName(), key_nameA
);
1442 HeapFree(GetProcessHeap(), 0, key_nameA
);
1443 REGPROC_print_error();
1450 /* export all registry classes */
1451 file
= REGPROC_open_export_file(file_name
, unicode
);
1452 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++)
1454 /* do not export HKEY_CLASSES_ROOT */
1455 if (reg_class_keys
[i
] != HKEY_CLASSES_ROOT
&&
1456 reg_class_keys
[i
] != HKEY_CURRENT_USER
&&
1457 reg_class_keys
[i
] != HKEY_CURRENT_CONFIG
&&
1458 reg_class_keys
[i
] != HKEY_DYN_DATA
)
1460 lstrcpyW(reg_key_name_buf
, reg_class_namesW
[i
]);
1461 export_hkey(file
, reg_class_keys
[i
],
1462 ®_key_name_buf
, ®_key_name_size
,
1463 &val_name_buf
, &val_name_size
,
1464 &val_buf
, &val_size
, &line_buf
,
1465 &line_buf_size
, unicode
);
1474 HeapFree(GetProcessHeap(), 0, reg_key_name
);
1475 HeapFree(GetProcessHeap(), 0, val_name_buf
);
1476 HeapFree(GetProcessHeap(), 0, val_buf
);
1477 HeapFree(GetProcessHeap(), 0, line_buf
);
1481 /******************************************************************************
1482 * Reads contents of the specified file into the registry.
1484 BOOL
import_registry_file(FILE* reg_file
)
1489 if (fread( s
, 2, 1, reg_file
) == 1)
1491 if (s
[0] == 0xff && s
[1] == 0xfe)
1493 processRegLinesW(reg_file
);
1497 fseek(reg_file
, 0, SEEK_SET
);
1498 processRegLinesA(reg_file
);
1506 /******************************************************************************
1507 * Removes the registry key with all subkeys. Parses full key name.
1510 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1511 * empty, points to register key class, does not exist.
1513 void delete_registry_key(WCHAR
*reg_key_name
)
1515 WCHAR
*key_name
= NULL
;
1518 if (!reg_key_name
|| !reg_key_name
[0])
1521 if (!parseKeyName(reg_key_name
, &key_class
, &key_name
))
1523 char* reg_key_nameA
= GetMultiByteString(reg_key_name
);
1524 fprintf(stderr
,"%S: Incorrect registry class specification in '%s'\n",
1525 getAppName(), reg_key_nameA
);
1526 HeapFree(GetProcessHeap(), 0, reg_key_nameA
);
1531 char* reg_key_nameA
= GetMultiByteString(reg_key_name
);
1532 fprintf(stderr
,"%S: Can't delete registry class '%s'\n",
1533 getAppName(), reg_key_nameA
);
1534 HeapFree(GetProcessHeap(), 0, reg_key_nameA
);
1538 SHDeleteKey(key_class
, key_name
);