2 * Registry processing routines. Routines, common for registry
3 * processing frontends.
5 * Copyright 1999 Sylvain St-Germain
6 * Copyright 2002 Andriy Palamarchuk
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #define REG_VAL_BUF_SIZE 4096
33 /* maximal number of characters in hexadecimal data line,
34 not including '\' character */
35 #define REG_FILE_HEX_LINE_LEN 76
37 /* Globals used by the api setValue */
38 static LPSTR currentKeyName
= NULL
;
39 static HKEY currentKeyClass
= 0;
40 static HKEY currentKeyHandle
= 0;
41 static BOOL bTheKeyIsOpen
= FALSE
;
43 static const CHAR
*app_name
= "UNKNOWN";
45 static const CHAR
*reg_class_names
[] = {
46 "HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_CLASSES_ROOT",
47 "HKEY_CURRENT_CONFIG", "HKEY_CURRENT_USER", "HKEY_DYN_DATA"
50 #define REG_CLASS_NUMBER (sizeof(reg_class_names) / sizeof(reg_class_names[0]))
52 static HKEY reg_class_keys
[REG_CLASS_NUMBER
] = {
53 HKEY_LOCAL_MACHINE
, HKEY_USERS
, HKEY_CLASSES_ROOT
,
54 HKEY_CURRENT_CONFIG
, HKEY_CURRENT_USER
, HKEY_DYN_DATA
58 #define NOT_ENOUGH_MEMORY 1
61 /* processing macros */
63 /* common check of memory allocation results */
64 #define CHECK_ENOUGH_MEMORY(p) \
67 fprintf(stderr,"%s: file %s, line %d: Not enough memory", \
68 getAppName(), __FILE__, __LINE__); \
69 exit(NOT_ENOUGH_MEMORY); \
72 /******************************************************************************
73 * This is a replacement for strsep which is not portable (missing on Solaris).
77 char* getToken(char** str
, const char* delims
)
88 if (strchr(delims
,**str
)!=NULL
) {
95 /* There is no other token */
101 /******************************************************************************
102 * Copies file name from command line string to the buffer.
103 * Rewinds the command line string pointer to the next non-space character
104 * after the file name.
105 * Buffer contains an empty string if no filename was found;
108 * command_line - command line current position pointer
109 * where *s[0] is the first symbol of the file name.
110 * file_name - buffer to write the file name to.
112 void get_file_name(CHAR
**command_line
, CHAR
*file_name
)
114 CHAR
*s
= *command_line
;
115 int pos
= 0; /* position of pointer "s" in *command_line */
127 fprintf(stderr
,"%s: Unexpected end of file name!\n",
135 while(s
[0] && !isspace(s
[0])) {
140 memcpy(file_name
, *command_line
, pos
* sizeof((*command_line
)[0]));
141 /* remove the last backslash */
142 if (file_name
[pos
- 1] == '\\') {
143 file_name
[pos
- 1] = '\0';
145 file_name
[pos
] = '\0';
152 while(s
[0] && isspace(s
[0])) {
156 (*command_line
) += pos
;
160 /******************************************************************************
161 * Converts a hex representation of a DWORD into a DWORD.
163 DWORD
convertHexToDWord(char *str
, BYTE
*buf
)
170 sscanf(xbuf
,"%08x",&dw
);
171 memcpy(buf
,&dw
,sizeof(DWORD
));
172 return sizeof(DWORD
);
175 /******************************************************************************
176 * Converts a hex comma separated values list into a hex list.
177 * The Hex input string must be in exactly the correct form.
179 DWORD
convertHexCSVToHex(char *str
, BYTE
*buf
, ULONG bufLen
)
181 char *s
= str
; /* Pointer to current */
182 char *b
= (char*) buf
; /* Pointer to result */
184 ULONG strLen
= strlen(str
);
188 memset(buf
, 0, bufLen
);
191 * warn the user if we are here with a string longer than 2 bytes that does
192 * not contains ",". It is more likely because the data is invalid.
194 if ( ( strLen
> 2) && ( strchr(str
, ',') == NULL
) )
195 fprintf(stderr
,"%s: WARNING converting CSV hex stream with no comma, "
196 "input data seems invalid.\n", getAppName());
197 if (strLen
> 3*bufLen
)
198 fprintf(stderr
,"%s: ERROR converting CSV hex stream. Too long\n",
201 while (strPos
< strLen
) {
205 memcpy(xbuf
,s
,2); xbuf
[2]='\0';
206 sscanf(xbuf
,"%02x",&wc
);
207 if (byteCount
< bufLen
)
208 *b
++ =(unsigned char)wc
;
218 /******************************************************************************
219 * This function returns the HKEY associated with the data type encoded in the
220 * value. It modifies the input parameter (key value) in order to skip this
221 * "now useless" data type information.
223 * Note: Updated based on the algorithm used in 'server/registry.c'
225 DWORD
getDataType(LPSTR
*lpValue
, DWORD
* parse_type
)
227 struct data_type
{ const char *tag
; int len
; int type
; int parse_type
; };
229 static const struct data_type data_types
[] = { /* actual type */ /* type to assume for parsing */
230 { "\"", 1, REG_SZ
, REG_SZ
},
231 { "str:\"", 5, REG_SZ
, REG_SZ
},
232 { "str(2):\"", 8, REG_EXPAND_SZ
, REG_SZ
},
233 { "hex:", 4, REG_BINARY
, REG_BINARY
},
234 { "dword:", 6, REG_DWORD
, REG_DWORD
},
235 { "hex(", 4, -1, REG_BINARY
},
239 const struct data_type
*ptr
;
242 for (ptr
= data_types
; ptr
->tag
; ptr
++) {
243 if (memcmp( ptr
->tag
, *lpValue
, ptr
->len
))
247 *parse_type
= ptr
->parse_type
;
252 /* "hex(xx):" is special */
253 type
= (int)strtoul( *lpValue
, &end
, 16 );
254 if (**lpValue
=='\0' || *end
!=')' || *(end
+1)!=':') {
262 return (**lpValue
=='\0'?REG_SZ
:REG_NONE
);
265 /******************************************************************************
266 * Returns an allocated buffer with a cleaned copy (removed the surrounding
267 * dbl quotes) of the passed value.
269 LPSTR
getArg( LPSTR arg
)
278 * Get rid of surrounding quotes
282 if( arg
[len
-1] == '\"' ) arg
[len
-1] = '\0';
283 if( arg
[0] == '\"' ) arg
++;
285 tmp
= HeapAlloc(GetProcessHeap(), 0, strlen(arg
)+1);
286 CHECK_ENOUGH_MEMORY(tmp
);
292 /******************************************************************************
293 * Replaces escape sequences with the characters.
295 static void REGPROC_unescape_string(LPSTR str
)
297 int str_idx
= 0; /* current character under analysis */
298 int val_idx
= 0; /* the last character of the unescaped string */
299 int len
= strlen(str
);
300 for (str_idx
= 0; str_idx
< len
; str_idx
++, val_idx
++) {
301 if (str
[str_idx
] == '\\') {
303 switch (str
[str_idx
]) {
309 str
[val_idx
] = str
[str_idx
];
312 fprintf(stderr
,"Warning! Unrecognized escape sequence: \\%c'\n",
314 str
[val_idx
] = str
[str_idx
];
318 str
[val_idx
] = str
[str_idx
];
324 /******************************************************************************
325 * Sets the value with name val_name to the data in val_data for the currently
329 * val_name - name of the registry value
330 * val_data - registry value data
332 HRESULT
setValue(LPSTR val_name
, LPSTR val_data
)
335 DWORD dwDataType
, dwParseType
;
337 BYTE convert
[KEY_MAX_LEN
];
338 BYTE
*bBigBuffer
= 0;
341 if ( (val_name
== NULL
) || (val_data
== NULL
) )
342 return ERROR_INVALID_PARAMETER
;
344 if (val_data
[0] == '-')
345 return RegDeleteValue(currentKeyHandle
,val_name
);
347 /* Get the data type stored into the value field */
348 dwDataType
= getDataType(&val_data
, &dwParseType
);
350 if ( dwParseType
== REG_SZ
) /* no conversion for string */
352 REGPROC_unescape_string(val_data
);
353 /* Compute dwLen after REGPROC_unescape_string because it may
354 * have changed the string length and we don't want to store
355 * the extra garbage in the registry.
357 dwLen
= strlen(val_data
);
358 if (dwLen
>0 && val_data
[dwLen
-1]=='"')
361 val_data
[dwLen
]='\0';
363 lpbData
= (BYTE
*) val_data
;
364 } else if (dwParseType
== REG_DWORD
) /* Convert the dword types */
366 dwLen
= convertHexToDWord(val_data
, convert
);
368 } else /* Convert the hexadecimal types */
370 int b_len
= strlen (val_data
)+2/3;
371 if (b_len
> KEY_MAX_LEN
) {
372 bBigBuffer
= HeapAlloc (GetProcessHeap(), 0, b_len
);
373 CHECK_ENOUGH_MEMORY(bBigBuffer
);
374 dwLen
= convertHexCSVToHex(val_data
, bBigBuffer
, b_len
);
375 lpbData
= bBigBuffer
;
377 dwLen
= convertHexCSVToHex(val_data
, convert
, KEY_MAX_LEN
);
382 hRes
= RegSetValueEx(
390 HeapFree (GetProcessHeap(), 0, bBigBuffer
);
395 /******************************************************************************
398 HRESULT
openKey( LPSTR stdInput
)
404 if (stdInput
== NULL
)
405 return ERROR_INVALID_PARAMETER
;
407 /* Get the registry class */
408 currentKeyClass
= getRegClass(stdInput
); /* Sets global variable */
409 if (currentKeyClass
== (HKEY
)ERROR_INVALID_PARAMETER
)
410 return (HRESULT
)ERROR_INVALID_PARAMETER
;
412 /* Get the key name */
413 currentKeyName
= getRegKeyName(stdInput
); /* Sets global variable */
414 if (currentKeyName
== NULL
)
415 return ERROR_INVALID_PARAMETER
;
417 hRes
= RegCreateKeyEx(
418 currentKeyClass
, /* Class */
419 currentKeyName
, /* Sub Key */
421 NULL
, /* object type */
422 REG_OPTION_NON_VOLATILE
, /* option, REG_OPTION_NON_VOLATILE ... */
423 KEY_ALL_ACCESS
, /* access mask, KEY_ALL_ACCESS */
424 NULL
, /* security attribute */
425 ¤tKeyHandle
, /* result */
426 &dwDisp
); /* disposition, REG_CREATED_NEW_KEY or
427 REG_OPENED_EXISTING_KEY */
429 if (hRes
== ERROR_SUCCESS
)
430 bTheKeyIsOpen
= TRUE
;
436 /******************************************************************************
437 * Extracts from [HKEY\some\key\path] or HKEY\some\key\path types of line
438 * the key name (what starts after the first '\')
440 LPSTR
getRegKeyName(LPSTR lpLine
)
443 char lpLineCopy
[KEY_MAX_LEN
];
448 strcpy(lpLineCopy
, lpLine
);
450 keyNameBeg
= strchr(lpLineCopy
, '\\'); /* The key name start by '\' */
452 keyNameBeg
++; /* is not part of the name */
454 if (lpLine
[0] == '[') /* need to find matching ']' */
458 keyNameEnd
= strrchr(lpLineCopy
, ']');
460 *keyNameEnd
= '\0'; /* remove ']' from the key name */
464 keyNameBeg
= lpLineCopy
+ strlen(lpLineCopy
); /* branch - empty string */
466 currentKeyName
= HeapAlloc(GetProcessHeap(), 0, strlen(keyNameBeg
) + 1);
467 CHECK_ENOUGH_MEMORY(currentKeyName
);
468 strcpy(currentKeyName
, keyNameBeg
);
469 return currentKeyName
;
472 /******************************************************************************
473 * Extracts from [HKEY\some\key\path] or HKEY\some\key\path types of line
474 * the key class (what ends before the first '\')
476 HKEY
getRegClass(LPSTR lpClass
)
482 char lpClassCopy
[KEY_MAX_LEN
];
485 return (HKEY
)ERROR_INVALID_PARAMETER
;
487 lstrcpynA(lpClassCopy
, lpClass
, KEY_MAX_LEN
);
489 classNameEnd
= strchr(lpClassCopy
, '\\'); /* The class name ends by '\' */
490 if (!classNameEnd
) /* or the whole string */
492 classNameEnd
= lpClassCopy
+ strlen(lpClassCopy
);
493 if (classNameEnd
[-1] == ']')
498 *classNameEnd
= '\0'; /* Isolate the class name */
499 if (lpClassCopy
[0] == '[') {
500 classNameBeg
= lpClassCopy
+ 1;
502 classNameBeg
= lpClassCopy
;
505 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++) {
506 if (!strcmp(classNameBeg
, reg_class_names
[i
])) {
507 return reg_class_keys
[i
];
510 return (HKEY
)ERROR_INVALID_PARAMETER
;
513 /******************************************************************************
514 * Close the currently opened key.
518 RegCloseKey(currentKeyHandle
);
520 HeapFree(GetProcessHeap(), 0, currentKeyName
); /* Allocated by getKeyName */
522 bTheKeyIsOpen
= FALSE
;
524 currentKeyName
= NULL
;
526 currentKeyHandle
= 0;
529 /******************************************************************************
530 * This function is the main entry point to the setValue type of action. It
531 * receives the currently read line and dispatch the work depending on the
534 void doSetValue(LPSTR stdInput
)
537 * We encountered the end of the file, make sure we
538 * close the opened key and exit
540 if (stdInput
== NULL
) {
541 if (bTheKeyIsOpen
!= FALSE
)
547 if ( stdInput
[0] == '[') /* We are reading a new key */
549 if ( bTheKeyIsOpen
!= FALSE
)
550 closeKey(); /* Close the previous key before */
552 /* delete the key if we encounter '-' at the start of reg key */
553 if ( stdInput
[1] == '-')
555 int last_chr
= strlen(stdInput
) - 1;
557 /* skip leading "[-" and get rid of trailing "]" */
558 if (stdInput
[last_chr
] == ']')
559 stdInput
[last_chr
] = '\0';
560 delete_registry_key(stdInput
+2);
564 if ( openKey(stdInput
) != ERROR_SUCCESS
)
565 fprintf(stderr
,"%s: setValue failed to open key %s\n",
566 getAppName(), stdInput
);
567 } else if( ( bTheKeyIsOpen
) &&
568 (( stdInput
[0] == '@') || /* reading a default @=data pair */
569 ( stdInput
[0] == '\"'))) /* reading a new value=data pair */
571 processSetValue(stdInput
);
572 } else /* since we are assuming that the */
573 { /* file format is valid we must */
574 if ( bTheKeyIsOpen
) /* be reading a blank line which */
575 closeKey(); /* indicate end of this key processing */
579 /******************************************************************************
580 * This function is the main entry point to the deleteValue type of action. It
581 * receives the currently read line and dispatch the work depending on the
584 void doDeleteValue(LPSTR line
)
586 fprintf(stderr
,"%s: deleteValue not yet implemented\n", getAppName());
589 /******************************************************************************
590 * This function is the main entry point to the deleteKey type of action. It
591 * receives the currently read line and dispatch the work depending on the
594 void doDeleteKey(LPSTR line
)
596 fprintf(stderr
,"%s: deleteKey not yet implemented\n", getAppName());
599 /******************************************************************************
600 * This function is the main entry point to the createKey type of action. It
601 * receives the currently read line and dispatch the work depending on the
604 void doCreateKey(LPSTR line
)
606 fprintf(stderr
,"%s: createKey not yet implemented\n", getAppName());
609 /******************************************************************************
610 * This function is a wrapper for the setValue function. It prepares the
611 * land and clean the area once completed.
612 * Note: this function modifies the line parameter.
614 * line - registry file unwrapped line. Should have the registry value name and
615 * complete registry value data.
617 void processSetValue(LPSTR line
)
619 LPSTR val_name
; /* registry value name */
620 LPSTR val_data
; /* registry value data */
622 int line_idx
= 0; /* current character under analysis */
626 if (line
[line_idx
] == '@' && line
[line_idx
+ 1] == '=') {
627 line
[line_idx
] = '\0';
630 } else if (line
[line_idx
] == '\"') {
632 val_name
= line
+ line_idx
;
634 if (line
[line_idx
] == '\\') /* skip escaped character */
638 if (line
[line_idx
] == '\"') {
639 line
[line_idx
] = '\0';
647 if (line
[line_idx
] != '=') {
648 line
[line_idx
] = '\"';
649 fprintf(stderr
,"Warning! unrecognized line:\n%s\n", line
);
654 fprintf(stderr
,"Warning! unrecognized line:\n%s\n", line
);
657 line_idx
++; /* skip the '=' character */
658 val_data
= line
+ line_idx
;
660 REGPROC_unescape_string(val_name
);
661 hRes
= setValue(val_name
, val_data
);
662 if ( hRes
!= ERROR_SUCCESS
)
663 fprintf(stderr
,"%s: ERROR Key %s not created. Value: %s, Data: %s\n",
670 /******************************************************************************
671 * Calls command for each line of a registry file.
672 * Correctly processes comments (in # form), line continuation.
675 * in - input stream to read from
676 * command - command to be called for each line
678 void processRegLines(FILE *in
, CommandAPI command
)
680 LPSTR line
= NULL
; /* line read from input stream */
681 ULONG lineSize
= REG_VAL_BUF_SIZE
;
683 line
= HeapAlloc(GetProcessHeap(), 0, lineSize
);
684 CHECK_ENOUGH_MEMORY(line
);
687 LPSTR s
; /* The pointer into line for where the current fgets should read */
690 size_t size_remaining
;
692 char *s_eol
; /* various local uses */
694 /* Do we need to expand the buffer ? */
695 assert (s
>= line
&& s
<= line
+ lineSize
);
696 size_remaining
= lineSize
- (s
-line
);
697 if (size_remaining
< 2) /* room for 1 character and the \0 */
700 size_t new_size
= lineSize
+ REG_VAL_BUF_SIZE
;
701 if (new_size
> lineSize
) /* no arithmetic overflow */
702 new_buffer
= HeapReAlloc (GetProcessHeap(), 0, line
, new_size
);
705 CHECK_ENOUGH_MEMORY(new_buffer
);
707 s
= line
+ lineSize
- size_remaining
;
709 size_remaining
= lineSize
- (s
-line
);
712 /* Get as much as possible into the buffer, terminated either by
713 * eof, error, eol or getting the maximum amount. Abort on error.
715 size_to_get
= (size_remaining
> INT_MAX
? INT_MAX
: size_remaining
);
716 if (NULL
== fgets (s
, size_to_get
, in
)) {
718 perror ("While reading input");
723 /* It is not clear to me from the definition that the
724 * contents of the buffer are well defined on detecting
725 * an eof without managing to read anything.
730 /* If we didn't read the eol nor the eof go around for the rest */
731 s_eol
= strchr (s
, '\n');
732 if (!feof (in
) && !s_eol
) {
733 s
= strchr (s
, '\0');
734 /* It should be s + size_to_get - 1 but this is safer */
738 /* If it is a comment line then discard it and go around again */
739 if (line
[0] == '#') {
744 /* Remove any line feed. Leave s_eol on the \0 */
747 if (s_eol
> line
&& *(s_eol
-1) == '\r')
750 s_eol
= strchr (s
, '\0');
752 /* If there is a concatenating \\ then go around again */
753 if (s_eol
> line
&& *(s_eol
-1) == '\\') {
756 /* The following error protection could be made more self-
757 * correcting but I thought it not worth trying.
759 if ((c
= fgetc (in
)) == EOF
|| c
!= ' ' ||
760 (c
= fgetc (in
)) == EOF
|| c
!= ' ')
761 fprintf(stderr
,"%s: ERROR - invalid continuation.\n",
766 break; /* That is the full virtual line */
773 HeapFree(GetProcessHeap(), 0, line
);
776 /******************************************************************************
777 * This function is the main entry point to the registerDLL action. It
778 * receives the currently read line, then loads and registers the requested DLLs
780 void doRegisterDLL(LPSTR stdInput
)
785 /* Check for valid input */
786 if (stdInput
== NULL
)
789 /* Load and register the library, then free it */
790 theLib
= LoadLibrary(stdInput
);
792 FARPROC lpfnDLLRegProc
= GetProcAddress(theLib
, "DllRegisterServer");
794 retVal
= (*lpfnDLLRegProc
)();
796 fprintf(stderr
,"%s: Couldn't find DllRegisterServer proc in '%s'.\n",
797 getAppName(), stdInput
);
800 fprintf(stderr
,"%s: DLLRegisterServer error 0x%x in '%s'.\n",
801 getAppName(), retVal
, stdInput
);
805 fprintf(stderr
,"%s: Could not load DLL '%s'.\n", getAppName(), stdInput
);
809 /******************************************************************************
810 * This function is the main entry point to the unregisterDLL action. It
811 * receives the currently read line, then loads and unregisters the requested DLLs
813 void doUnregisterDLL(LPSTR stdInput
)
818 /* Check for valid input */
819 if (stdInput
== NULL
)
822 /* Load and unregister the library, then free it */
823 theLib
= LoadLibrary(stdInput
);
825 FARPROC lpfnDLLRegProc
= GetProcAddress(theLib
, "DllUnregisterServer");
827 retVal
= (*lpfnDLLRegProc
)();
829 fprintf(stderr
,"%s: Couldn't find DllUnregisterServer proc in '%s'.\n",
830 getAppName(), stdInput
);
833 fprintf(stderr
,"%s: DLLUnregisterServer error 0x%x in '%s'.\n",
834 getAppName(), retVal
, stdInput
);
838 fprintf(stderr
,"%s: Could not load DLL '%s'.\n", getAppName(), stdInput
);
842 /****************************************************************************
843 * REGPROC_print_error
845 * Print the message for GetLastError
848 static void REGPROC_print_error(void)
854 error_code
= GetLastError ();
855 status
= FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
856 NULL
, error_code
, 0, (LPTSTR
) &lpMsgBuf
, 0, NULL
);
858 fprintf(stderr
,"%s: Cannot display message for error %d, status %d\n",
859 getAppName(), error_code
, GetLastError());
863 LocalFree((HLOCAL
)lpMsgBuf
);
867 /******************************************************************************
868 * Checks whether the buffer has enough room for the string or required size.
869 * Resizes the buffer if necessary.
872 * buffer - pointer to a buffer for string
873 * len - current length of the buffer in characters.
874 * required_len - length of the string to place to the buffer in characters.
875 * The length does not include the terminating null character.
877 static void REGPROC_resize_char_buffer(CHAR
**buffer
, DWORD
*len
, DWORD required_len
)
880 if (required_len
> *len
) {
883 *buffer
= HeapAlloc(GetProcessHeap(), 0, *len
* sizeof(**buffer
));
885 *buffer
= HeapReAlloc(GetProcessHeap(), 0, *buffer
, *len
* sizeof(**buffer
));
886 CHECK_ENOUGH_MEMORY(*buffer
);
890 /******************************************************************************
891 * Prints string str to file
893 static void REGPROC_export_string(FILE *file
, CHAR
*str
)
895 size_t len
= strlen(str
);
898 /* escaping characters */
899 for (i
= 0; i
< len
; i
++) {
918 /******************************************************************************
919 * Writes contents of the registry key to the specified file stream.
922 * file - writable file stream to export registry branch to.
923 * key - registry branch to export.
924 * reg_key_name_buf - name of the key with registry class.
925 * Is resized if necessary.
926 * reg_key_name_len - length of the buffer for the registry class in characters.
927 * val_name_buf - buffer for storing value name.
928 * Is resized if necessary.
929 * val_name_len - length of the buffer for storing value names in characters.
930 * val_buf - buffer for storing values while extracting.
931 * Is resized if necessary.
932 * val_size - size of the buffer for storing values in bytes.
934 static void export_hkey(FILE *file
, HKEY key
,
935 CHAR
**reg_key_name_buf
, DWORD
*reg_key_name_len
,
936 CHAR
**val_name_buf
, DWORD
*val_name_len
,
937 BYTE
**val_buf
, DWORD
*val_size
)
939 DWORD max_sub_key_len
;
940 DWORD max_val_name_len
;
947 /* get size information and resize the buffers if necessary */
948 if (RegQueryInfoKey(key
, NULL
, NULL
, NULL
, NULL
,
949 &max_sub_key_len
, NULL
,
950 NULL
, &max_val_name_len
, &max_val_size
, NULL
, NULL
951 ) != ERROR_SUCCESS
) {
952 REGPROC_print_error();
954 curr_len
= strlen(*reg_key_name_buf
);
955 REGPROC_resize_char_buffer(reg_key_name_buf
, reg_key_name_len
,
956 max_sub_key_len
+ curr_len
+ 1);
957 REGPROC_resize_char_buffer(val_name_buf
, val_name_len
,
959 if (max_val_size
> *val_size
) {
960 *val_size
= max_val_size
;
961 if (!*val_buf
) *val_buf
= HeapAlloc(GetProcessHeap(), 0, *val_size
);
962 else *val_buf
= HeapReAlloc(GetProcessHeap(), 0, *val_buf
, *val_size
);
963 CHECK_ENOUGH_MEMORY(val_buf
);
966 /* output data for the current key */
968 fputs(*reg_key_name_buf
, file
);
970 /* print all the values */
975 DWORD val_name_len1
= *val_name_len
;
976 DWORD val_size1
= *val_size
;
977 ret
= RegEnumValue(key
, i
, *val_name_buf
, &val_name_len1
, NULL
,
978 &value_type
, *val_buf
, &val_size1
);
979 if (ret
!= ERROR_SUCCESS
) {
981 if (ret
!= ERROR_NO_MORE_ITEMS
) {
982 REGPROC_print_error();
987 if ((*val_name_buf
)[0]) {
989 REGPROC_export_string(file
, *val_name_buf
);
995 switch (value_type
) {
999 REGPROC_export_string(file
, (char*) *val_buf
);
1000 fputs("\"\n", file
);
1004 fprintf(file
, "dword:%08x\n", *((DWORD
*)*val_buf
));
1008 fprintf(stderr
,"%s: warning - unsupported registry format '%d', "
1009 "treat as binary\n",
1010 getAppName(), value_type
);
1011 fprintf(stderr
,"key name: \"%s\"\n", *reg_key_name_buf
);
1012 fprintf(stderr
,"value name:\"%s\"\n\n", *val_name_buf
);
1018 const CHAR
*hex_prefix
;
1022 if (value_type
== REG_BINARY
) {
1023 hex_prefix
= "hex:";
1026 sprintf(buf
, "hex(%d):", value_type
);
1029 /* position of where the next character will be printed */
1030 /* NOTE: yes, strlen("hex:") is used even for hex(x): */
1031 cur_pos
= strlen("\"\"=") + strlen("hex:") +
1032 strlen(*val_name_buf
);
1034 fputs(hex_prefix
, file
);
1035 for (i1
= 0; i1
< val_size1
; i1
++) {
1036 fprintf(file
, "%02x", (unsigned int)(*val_buf
)[i1
]);
1037 if (i1
+ 1 < val_size1
) {
1043 if (cur_pos
> REG_FILE_HEX_LINE_LEN
) {
1044 fputs("\\\n ", file
);
1057 (*reg_key_name_buf
)[curr_len
] = '\\';
1059 DWORD buf_len
= *reg_key_name_len
- curr_len
;
1061 ret
= RegEnumKeyEx(key
, i
, *reg_key_name_buf
+ curr_len
+ 1, &buf_len
,
1062 NULL
, NULL
, NULL
, NULL
);
1063 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_MORE_DATA
) {
1065 if (ret
!= ERROR_NO_MORE_ITEMS
) {
1066 REGPROC_print_error();
1072 if (RegOpenKey(key
, *reg_key_name_buf
+ curr_len
+ 1,
1073 &subkey
) == ERROR_SUCCESS
) {
1074 export_hkey(file
, subkey
, reg_key_name_buf
, reg_key_name_len
,
1075 val_name_buf
, val_name_len
, val_buf
, val_size
);
1076 RegCloseKey(subkey
);
1078 REGPROC_print_error();
1082 (*reg_key_name_buf
)[curr_len
] = '\0';
1085 /******************************************************************************
1086 * Open file for export.
1088 static FILE *REGPROC_open_export_file(CHAR
*file_name
)
1090 FILE *file
= fopen(file_name
, "w");
1093 fprintf(stderr
,"%s: Can't open file \"%s\"\n", getAppName(), file_name
);
1096 fputs("REGEDIT4\n", file
);
1100 /******************************************************************************
1101 * Writes contents of the registry key to the specified file stream.
1104 * file_name - name of a file to export registry branch to.
1105 * reg_key_name - registry branch to export. The whole registry is exported if
1106 * reg_key_name is NULL or contains an empty string.
1108 BOOL
export_registry_key(CHAR
*file_name
, CHAR
*reg_key_name
)
1112 CHAR
*reg_key_name_buf
;
1115 DWORD reg_key_name_len
= KEY_MAX_LEN
;
1116 DWORD val_name_len
= KEY_MAX_LEN
;
1117 DWORD val_size
= REG_VAL_BUF_SIZE
;
1120 reg_key_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1121 reg_key_name_len
* sizeof(*reg_key_name_buf
));
1122 val_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1123 val_name_len
* sizeof(*val_name_buf
));
1124 val_buf
= HeapAlloc(GetProcessHeap(), 0, val_size
);
1125 CHECK_ENOUGH_MEMORY(reg_key_name_buf
&& val_name_buf
&& val_buf
);
1127 if (reg_key_name
&& reg_key_name
[0]) {
1131 REGPROC_resize_char_buffer(®_key_name_buf
, ®_key_name_len
,
1132 strlen(reg_key_name
));
1133 strcpy(reg_key_name_buf
, reg_key_name
);
1135 /* open the specified key */
1136 reg_key_class
= getRegClass(reg_key_name
);
1137 if (reg_key_class
== (HKEY
)ERROR_INVALID_PARAMETER
) {
1138 fprintf(stderr
,"%s: Incorrect registry class specification in '%s'\n",
1139 getAppName(), reg_key_name
);
1142 branch_name
= getRegKeyName(reg_key_name
);
1143 CHECK_ENOUGH_MEMORY(branch_name
);
1144 if (!branch_name
[0]) {
1145 /* no branch - registry class is specified */
1146 file
= REGPROC_open_export_file(file_name
);
1147 export_hkey(file
, reg_key_class
,
1148 ®_key_name_buf
, ®_key_name_len
,
1149 &val_name_buf
, &val_name_len
,
1150 &val_buf
, &val_size
);
1151 } else if (RegOpenKey(reg_key_class
, branch_name
, &key
) == ERROR_SUCCESS
) {
1152 file
= REGPROC_open_export_file(file_name
);
1153 export_hkey(file
, key
,
1154 ®_key_name_buf
, ®_key_name_len
,
1155 &val_name_buf
, &val_name_len
,
1156 &val_buf
, &val_size
);
1159 fprintf(stderr
,"%s: Can't export. Registry key '%s' does not exist!\n",
1160 getAppName(), reg_key_name
);
1161 REGPROC_print_error();
1163 HeapFree(GetProcessHeap(), 0, branch_name
);
1167 /* export all registry classes */
1168 file
= REGPROC_open_export_file(file_name
);
1169 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++) {
1170 /* do not export HKEY_CLASSES_ROOT */
1171 if (reg_class_keys
[i
] != HKEY_CLASSES_ROOT
&&
1172 reg_class_keys
[i
] != HKEY_CURRENT_USER
&&
1173 reg_class_keys
[i
] != HKEY_CURRENT_CONFIG
&&
1174 reg_class_keys
[i
] != HKEY_DYN_DATA
) {
1175 strcpy(reg_key_name_buf
, reg_class_names
[i
]);
1176 export_hkey(file
, reg_class_keys
[i
],
1177 ®_key_name_buf
, ®_key_name_len
,
1178 &val_name_buf
, &val_name_len
,
1179 &val_buf
, &val_size
);
1187 HeapFree(GetProcessHeap(), 0, reg_key_name
);
1188 HeapFree(GetProcessHeap(), 0, val_buf
);
1192 /******************************************************************************
1193 * Reads contents of the specified file into the registry.
1195 BOOL
import_registry_file(LPTSTR filename
)
1197 FILE* reg_file
= fopen(filename
, "r");
1200 processRegLines(reg_file
, doSetValue
);
1206 /******************************************************************************
1207 * Recursive function which removes the registry key with all subkeys.
1209 static void delete_branch(HKEY key
,
1210 CHAR
**reg_key_name_buf
, DWORD
*reg_key_name_len
)
1213 DWORD max_sub_key_len
;
1219 if (RegOpenKey(key
, *reg_key_name_buf
, &branch_key
) != ERROR_SUCCESS
) {
1220 REGPROC_print_error();
1223 /* get size information and resize the buffers if necessary */
1224 if (RegQueryInfoKey(branch_key
, NULL
, NULL
, NULL
,
1225 &subkeys
, &max_sub_key_len
,
1226 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
1227 ) != ERROR_SUCCESS
) {
1228 REGPROC_print_error();
1230 curr_len
= strlen(*reg_key_name_buf
);
1231 REGPROC_resize_char_buffer(reg_key_name_buf
, reg_key_name_len
,
1232 max_sub_key_len
+ curr_len
+ 1);
1234 (*reg_key_name_buf
)[curr_len
] = '\\';
1235 for (i
= subkeys
- 1; i
>= 0; i
--) {
1236 DWORD buf_len
= *reg_key_name_len
- curr_len
;
1238 ret
= RegEnumKeyEx(branch_key
, i
, *reg_key_name_buf
+ curr_len
+ 1,
1239 &buf_len
, NULL
, NULL
, NULL
, NULL
);
1240 if (ret
!= ERROR_SUCCESS
&&
1241 ret
!= ERROR_MORE_DATA
&&
1242 ret
!= ERROR_NO_MORE_ITEMS
) {
1243 REGPROC_print_error();
1245 delete_branch(key
, reg_key_name_buf
, reg_key_name_len
);
1248 (*reg_key_name_buf
)[curr_len
] = '\0';
1249 RegCloseKey(branch_key
);
1250 RegDeleteKey(key
, *reg_key_name_buf
);
1253 /******************************************************************************
1254 * Removes the registry key with all subkeys. Parses full key name.
1257 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1258 * empty, points to register key class, does not exist.
1260 void delete_registry_key(CHAR
*reg_key_name
)
1263 DWORD branch_name_len
;
1267 if (!reg_key_name
|| !reg_key_name
[0])
1269 /* open the specified key */
1270 reg_key_class
= getRegClass(reg_key_name
);
1271 if (reg_key_class
== (HKEY
)ERROR_INVALID_PARAMETER
) {
1272 fprintf(stderr
,"%s: Incorrect registry class specification in '%s'\n",
1273 getAppName(), reg_key_name
);
1276 branch_name
= getRegKeyName(reg_key_name
);
1277 CHECK_ENOUGH_MEMORY(branch_name
);
1278 branch_name_len
= strlen(branch_name
);
1279 if (!branch_name
[0]) {
1280 fprintf(stderr
,"%s: Can't delete registry class '%s'\n",
1281 getAppName(), reg_key_name
);
1284 if (RegOpenKey(reg_key_class
, branch_name
, &branch_key
) == ERROR_SUCCESS
) {
1285 /* check whether the key exists */
1286 RegCloseKey(branch_key
);
1287 delete_branch(reg_key_class
, &branch_name
, &branch_name_len
);
1289 HeapFree(GetProcessHeap(), 0, branch_name
);
1292 /******************************************************************************
1293 * Sets the application name. Then application name is used in the error
1296 void setAppName(const CHAR
*name
)
1301 const CHAR
*getAppName(void)