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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #define REG_VAL_BUF_SIZE 4096
33 /* Delimiters used to parse the "value" to query queryValue*/
34 #define QUERY_VALUE_MAX_ARGS 1
36 /* maximal number of characters in hexadecimal data line,
37 not including '\' character */
38 #define REG_FILE_HEX_LINE_LEN 76
40 /* Globals used by the api setValue, queryValue */
41 static LPSTR currentKeyName
= NULL
;
42 static HKEY currentKeyClass
= 0;
43 static HKEY currentKeyHandle
= 0;
44 static BOOL bTheKeyIsOpen
= FALSE
;
46 static CHAR
*app_name
= "UNKNOWN";
48 static CHAR
*reg_class_names
[] = {
49 "HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_CLASSES_ROOT",
50 "HKEY_CURRENT_CONFIG", "HKEY_CURRENT_USER"
53 #define REG_CLASS_NUMBER (sizeof(reg_class_names) / sizeof(reg_class_names[0]))
55 static HKEY reg_class_keys
[REG_CLASS_NUMBER
] = {
56 HKEY_LOCAL_MACHINE
, HKEY_USERS
, HKEY_CLASSES_ROOT
,
57 HKEY_CURRENT_CONFIG
, HKEY_CURRENT_USER
61 #define NOT_ENOUGH_MEMORY 1
64 /* processing macros */
66 /* common check of memory allocation results */
67 #define CHECK_ENOUGH_MEMORY(p) \
70 printf("%s: file %s, line %d: Not enough memory", \
71 getAppName(), __FILE__, __LINE__); \
72 exit(NOT_ENOUGH_MEMORY); \
75 /******************************************************************************
76 * This is a replacement for strsep which is not portable (missing on Solaris).
80 char* getToken(char** str
, const char* delims
)
91 if (strchr(delims
,**str
)!=NULL
) {
98 /* There is no other token */
104 /******************************************************************************
105 * Copies file name from command line string to the buffer.
106 * Rewinds the command line string pointer to the next non-space character
107 * after the file name.
108 * Buffer contains an empty string if no filename was found;
111 * command_line - command line current position pointer
112 * where *s[0] is the first symbol of the file name.
113 * file_name - buffer to write the file name to.
115 void get_file_name(CHAR
**command_line
, CHAR
*file_name
)
117 CHAR
*s
= *command_line
;
118 int pos
= 0; /* position of pointer "s" in *command_line */
134 printf("%s: Unexpected end of file name!\n", getAppName());
141 while(s
[0] && !isspace(s
[0]))
147 memcpy(file_name
, *command_line
, pos
* sizeof((*command_line
)[0]));
148 /* remove the last backslash */
149 if (file_name
[pos
- 1] == '\\')
151 file_name
[pos
- 1] = '\0';
153 file_name
[pos
] = '\0';
161 while(s
[0] && isspace(s
[0]))
166 (*command_line
) += pos
;
170 /******************************************************************************
171 * Converts a hex representation of a DWORD into a DWORD.
173 DWORD
convertHexToDWord(char *str
, BYTE
*buf
)
180 sscanf(xbuf
,"%08lx",&dw
);
181 memcpy(buf
,&dw
,sizeof(DWORD
));
182 return sizeof(DWORD
);
185 /******************************************************************************
186 * Converts a hex buffer into a hex comma separated values
188 char* convertHexToHexCSV(BYTE
*buf
, ULONG bufLen
)
196 str
= HeapAlloc(GetProcessHeap(), 0, (bufLen
+1)*2);
197 memset(str
, 0, (bufLen
+1)*2);
198 ptrStr
= str
; /* Pointer to result */
199 ptrBuf
= buf
; /* Pointer to current */
201 while (current
< bufLen
)
203 BYTE bCur
= ptrBuf
[current
++];
206 sprintf(res
, "%02x", (unsigned int)*&bCur
);
211 /* Get rid of the last comma */
212 str
[strlen(str
)-1] = '\0';
216 /******************************************************************************
217 * Converts a hex buffer into a DWORD string
219 char* convertHexToDWORDStr(BYTE
*buf
, ULONG bufLen
)
224 if ( bufLen
!= sizeof(DWORD
) ) return NULL
;
226 str
= HeapAlloc(GetProcessHeap(), 0, (bufLen
*2)+1);
228 memcpy(&dw
,buf
,sizeof(DWORD
));
229 sprintf(str
, "%08lx", dw
);
231 /* Get rid of the last comma */
235 /******************************************************************************
236 * Converts a hex comma separated values list into a hex list.
237 * The Hex input string must be in exactly the correct form.
239 DWORD
convertHexCSVToHex(char *str
, BYTE
*buf
, ULONG bufLen
)
241 char *s
= str
; /* Pointer to current */
242 char *b
= buf
; /* Pointer to result */
244 ULONG strLen
= strlen(str
);
248 memset(buf
, 0, bufLen
);
251 * warn the user if we are here with a string longer than 2 bytes that does
252 * not contains ",". It is more likely because the data is invalid.
254 if ( ( strLen
> 2) && ( strchr(str
, ',') == NULL
) )
255 printf("%s: WARNING converting CSV hex stream with no comma, "
256 "input data seems invalid.\n", getAppName());
257 if (strLen
> 3*bufLen
)
258 printf ("%s: ERROR converting CSV hex stream. Too long\n", getAppName());
260 while (strPos
< strLen
)
265 memcpy(xbuf
,s
,2); xbuf
[2]='\0';
266 sscanf(xbuf
,"%02x",&wc
);
267 if (byteCount
< bufLen
)
268 *b
++ =(unsigned char)wc
;
278 /******************************************************************************
279 * This function returns the HKEY associated with the data type encoded in the
280 * value. It modifies the input parameter (key value) in order to skip this
281 * "now useless" data type information.
283 * Note: Updated based on the algorithm used in 'server/registry.c'
285 DWORD
getDataType(LPSTR
*lpValue
, DWORD
* parse_type
)
287 struct data_type
{ const char *tag
; int len
; int type
; int parse_type
; };
289 static const struct data_type data_types
[] =
290 { /* actual type */ /* type to assume for parsing */
291 { "\"", 1, REG_SZ
, REG_SZ
},
292 { "str:\"", 5, REG_SZ
, REG_SZ
},
293 { "str(2):\"", 8, REG_EXPAND_SZ
, REG_SZ
},
294 { "hex:", 4, REG_BINARY
, REG_BINARY
},
295 { "dword:", 6, REG_DWORD
, REG_DWORD
},
296 { "hex(", 4, -1, REG_BINARY
},
300 const struct data_type
*ptr
;
303 for (ptr
= data_types
; ptr
->tag
; ptr
++)
305 if (memcmp( ptr
->tag
, *lpValue
, ptr
->len
))
309 *parse_type
= ptr
->parse_type
;
314 /* "hex(xx):" is special */
315 type
= (int)strtoul( *lpValue
, &end
, 16 );
316 if (**lpValue
=='\0' || *end
!=')' || *(end
+1)!=':') {
324 return (**lpValue
=='\0'?REG_SZ
:REG_NONE
);
327 /******************************************************************************
328 * Returns an allocated buffer with a cleaned copy (removed the surrounding
329 * dbl quotes) of the passed value.
331 LPSTR
getArg( LPSTR arg
)
340 * Get rid of surrounding quotes
344 if( arg
[len
-1] == '\"' ) arg
[len
-1] = '\0';
345 if( arg
[0] == '\"' ) arg
++;
347 tmp
= HeapAlloc(GetProcessHeap(), 0, strlen(arg
)+1);
353 /******************************************************************************
354 * Replaces escape sequences with the characters.
356 void REGPROC_unescape_string(LPSTR str
)
358 int str_idx
= 0; /* current character under analysis */
359 int val_idx
= 0; /* the last character of the unescaped string */
360 int len
= strlen(str
);
361 for (str_idx
= 0; str_idx
< len
; str_idx
++, val_idx
++)
363 if (str
[str_idx
] == '\\')
366 switch (str
[str_idx
])
373 str
[val_idx
] = str
[str_idx
];
376 printf("Warning! Unrecognized escape sequence: \\%c'\n",
378 str
[val_idx
] = str
[str_idx
];
382 str
[val_idx
] = str
[str_idx
];
388 /******************************************************************************
389 * Sets the value with name val_name to the data in val_data for the currently
393 * val_name - name of the registry value
394 * val_data - registry value data
396 HRESULT
setValue(LPSTR val_name
, LPSTR val_data
)
399 DWORD dwDataType
, dwParseType
;
401 BYTE convert
[KEY_MAX_LEN
];
402 BYTE
*bBigBuffer
= 0;
405 if ( (val_name
== NULL
) || (val_data
== NULL
) )
406 return ERROR_INVALID_PARAMETER
;
408 /* Get the data type stored into the value field */
409 dwDataType
= getDataType(&val_data
, &dwParseType
);
411 if ( dwParseType
== REG_SZ
) /* no conversion for string */
413 dwLen
= strlen(val_data
);
414 if (dwLen
>0 && val_data
[dwLen
-1]=='"')
417 val_data
[dwLen
]='\0';
420 REGPROC_unescape_string(val_data
);
423 else if (dwParseType
== REG_DWORD
) /* Convert the dword types */
425 dwLen
= convertHexToDWord(val_data
, convert
);
428 else /* Convert the hexadecimal types */
430 int b_len
= strlen (val_data
)+2/3;
431 if (b_len
> KEY_MAX_LEN
)
433 bBigBuffer
= HeapAlloc (GetProcessHeap(), 0, b_len
);
434 CHECK_ENOUGH_MEMORY(bBigBuffer
);
435 dwLen
= convertHexCSVToHex(val_data
, bBigBuffer
, b_len
);
436 lpbData
= bBigBuffer
;
440 dwLen
= convertHexCSVToHex(val_data
, convert
, KEY_MAX_LEN
);
445 hRes
= RegSetValueEx(
454 HeapFree (GetProcessHeap(), 0, bBigBuffer
);
459 /******************************************************************************
462 HRESULT
openKey( LPSTR stdInput
)
468 if (stdInput
== NULL
)
469 return ERROR_INVALID_PARAMETER
;
471 /* Get the registry class */
472 currentKeyClass
= getRegClass(stdInput
); /* Sets global variable */
473 if (currentKeyClass
== (HKEY
)ERROR_INVALID_PARAMETER
)
474 return (HRESULT
)ERROR_INVALID_PARAMETER
;
476 /* Get the key name */
477 currentKeyName
= getRegKeyName(stdInput
); /* Sets global variable */
478 if (currentKeyName
== NULL
)
479 return ERROR_INVALID_PARAMETER
;
481 hRes
= RegCreateKeyEx(
482 currentKeyClass
, /* Class */
483 currentKeyName
, /* Sub Key */
485 NULL
, /* object type */
486 REG_OPTION_NON_VOLATILE
, /* option, REG_OPTION_NON_VOLATILE ... */
487 KEY_ALL_ACCESS
, /* access mask, KEY_ALL_ACCESS */
488 NULL
, /* security attribute */
489 ¤tKeyHandle
, /* result */
490 &dwDisp
); /* disposition, REG_CREATED_NEW_KEY or
491 REG_OPENED_EXISTING_KEY */
493 if (hRes
== ERROR_SUCCESS
)
494 bTheKeyIsOpen
= TRUE
;
500 /******************************************************************************
501 * Extracts from [HKEY\some\key\path] or HKEY\some\key\path types of line
502 * the key name (what starts after the first '\')
504 LPSTR
getRegKeyName(LPSTR lpLine
)
507 char lpLineCopy
[KEY_MAX_LEN
];
512 strcpy(lpLineCopy
, lpLine
);
514 keyNameBeg
= strchr(lpLineCopy
, '\\'); /* The key name start by '\' */
519 keyNameBeg
++; /* is not part of the name */
520 keyNameEnd
= strchr(lpLineCopy
, ']');
523 *keyNameEnd
= '\0'; /* remove ']' from the key name */
526 keyNameBeg
= lpLineCopy
+ strlen(lpLineCopy
); /* branch - empty string */
528 currentKeyName
= HeapAlloc(GetProcessHeap(), 0, strlen(keyNameBeg
) + 1);
529 CHECK_ENOUGH_MEMORY(currentKeyName
);
530 strcpy(currentKeyName
, keyNameBeg
);
531 return currentKeyName
;
534 /******************************************************************************
535 * Extracts from [HKEY\some\key\path] or HKEY\some\key\path types of line
536 * the key class (what ends before the first '\')
538 HKEY
getRegClass(LPSTR lpClass
)
544 char lpClassCopy
[KEY_MAX_LEN
];
547 return (HKEY
)ERROR_INVALID_PARAMETER
;
549 strncpy(lpClassCopy
, lpClass
, KEY_MAX_LEN
);
551 classNameEnd
= strchr(lpClassCopy
, '\\'); /* The class name ends by '\' */
552 if (!classNameEnd
) /* or the whole string */
554 classNameEnd
= lpClassCopy
+ strlen(lpClassCopy
);
555 if (classNameEnd
[-1] == ']')
560 *classNameEnd
= '\0'; /* Isolate the class name */
561 if (lpClassCopy
[0] == '[')
563 classNameBeg
= lpClassCopy
+ 1;
565 classNameBeg
= lpClassCopy
;
568 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++)
570 if (!strcmp(classNameBeg
, reg_class_names
[i
]))
572 return reg_class_keys
[i
];
575 return (HKEY
)ERROR_INVALID_PARAMETER
;
578 /******************************************************************************
579 * Close the currently opened key.
583 RegCloseKey(currentKeyHandle
);
585 HeapFree(GetProcessHeap(), 0, currentKeyName
); /* Allocated by getKeyName */
587 bTheKeyIsOpen
= FALSE
;
589 currentKeyName
= NULL
;
591 currentKeyHandle
= 0;
594 /******************************************************************************
595 * This function is the main entry point to the setValue type of action. It
596 * receives the currently read line and dispatch the work depending on the
599 void doSetValue(LPSTR stdInput
)
602 * We encoutered the end of the file, make sure we
603 * close the opened key and exit
605 if (stdInput
== NULL
)
607 if (bTheKeyIsOpen
!= FALSE
)
613 if ( stdInput
[0] == '[') /* We are reading a new key */
615 if ( bTheKeyIsOpen
!= FALSE
)
616 closeKey(); /* Close the previous key before */
618 if ( openKey(stdInput
) != ERROR_SUCCESS
)
619 printf("%s: setValue failed to open key %s\n", getAppName(), stdInput
);
621 else if( ( bTheKeyIsOpen
) &&
622 (( stdInput
[0] == '@') || /* reading a default @=data pair */
623 ( stdInput
[0] == '\"'))) /* reading a new value=data pair */
625 processSetValue(stdInput
);
627 else /* since we are assuming that the */
628 { /* file format is valid we must */
629 if ( bTheKeyIsOpen
) /* be reading a blank line which */
630 closeKey(); /* indicate end of this key processing */
634 /******************************************************************************
635 * This funtion is the main entry point to the queryValue type of action. It
636 * receives the currently read line and dispatch the work depending on the
639 void doQueryValue(LPSTR stdInput
) {
641 * We encoutered the end of the file, make sure we
642 * close the opened key and exit
644 if (stdInput
== NULL
)
646 if (bTheKeyIsOpen
!= FALSE
)
652 if ( stdInput
[0] == '[') /* We are reading a new key */
654 if ( bTheKeyIsOpen
!= FALSE
)
655 closeKey(); /* Close the previous key before */
657 if ( openKey(stdInput
) != ERROR_SUCCESS
)
658 printf("%s: queryValue failed to open key %s\n", getAppName(), stdInput
);
660 else if( ( bTheKeyIsOpen
) &&
661 (( stdInput
[0] == '@') || /* reading a default @=data pair */
662 ( stdInput
[0] == '\"'))) /* reading a new value=data pair */
664 processQueryValue(stdInput
);
666 else /* since we are assuming that the */
667 { /* file format is valid we must */
668 if ( bTheKeyIsOpen
) /* be reading a blank line which */
669 closeKey(); /* indicate end of this key processing */
673 /******************************************************************************
674 * This funtion is the main entry point to the deletetValue type of action. It
675 * receives the currently read line and dispatch the work depending on the
678 void doDeleteValue(LPSTR line
) {
679 printf ("%s: deleteValue not yet implemented\n", getAppName());
682 /******************************************************************************
683 * This funtion is the main entry point to the deleteKey type of action. It
684 * receives the currently read line and dispatch the work depending on the
687 void doDeleteKey(LPSTR line
) {
688 printf ("%s: deleteKey not yet implemented\n", getAppName());
691 /******************************************************************************
692 * This funtion is the main entry point to the createKey type of action. It
693 * receives the currently read line and dispatch the work depending on the
696 void doCreateKey(LPSTR line
) {
697 printf ("%s: createKey not yet implemented\n", getAppName());
700 /******************************************************************************
701 * This function is a wrapper for the setValue function. It prepares the
702 * land and clean the area once completed.
703 * Note: this function modifies the line parameter.
705 * line - registry file unwrapped line. Should have the registry value name and
706 * complete registry value data.
708 void processSetValue(LPSTR line
)
710 LPSTR val_name
; /* registry value name */
711 LPSTR val_data
; /* registry value data */
713 int line_idx
= 0; /* current character under analysis */
717 if (line
[line_idx
] == '@' && line
[line_idx
+ 1] == '=')
719 line
[line_idx
] = '\0';
723 else if (line
[line_idx
] == '\"')
726 val_name
= line
+ line_idx
;
729 if (line
[line_idx
] == '\\') /* skip escaped character */
733 if (line
[line_idx
] == '\"')
735 line
[line_idx
] = '\0';
743 if (line
[line_idx
] != '=')
745 line
[line_idx
] = '\"';
746 printf("Warning! unrecognized line:\n%s\n", line
);
753 printf("Warning! unrecognized line:\n%s\n", line
);
756 line_idx
++; /* skip the '=' character */
757 val_data
= line
+ line_idx
;
759 REGPROC_unescape_string(val_name
);
760 hRes
= setValue(val_name
, val_data
);
761 if ( hRes
!= ERROR_SUCCESS
)
762 printf("%s: ERROR Key %s not created. Value: %s, Data: %s\n",
769 /******************************************************************************
770 * This function is a wrapper for the queryValue function. It prepares the
771 * land and clean the area once completed.
773 void processQueryValue(LPSTR cmdline
)
775 printf("ERROR!!! - temporary disabled");
778 LPSTR argv
[QUERY_VALUE_MAX_ARGS
];/* args storage */
779 LPSTR token
= NULL
; /* current token analyzed */
780 ULONG argCounter
= 0; /* counter of args */
783 LPSTR keyValue
= NULL
;
787 * Init storage and parse the line
789 for (counter
=0; counter
<QUERY_VALUE_MAX_ARGS
; counter
++)
792 while( (token
= getToken(&cmdline
, queryValueDelim
[argCounter
])) != NULL
)
794 argv
[argCounter
++] = getArg(token
);
796 if (argCounter
== QUERY_VALUE_MAX_ARGS
)
797 break; /* Stop processing args no matter what */
800 /* The value we look for is the first token on the line */
801 if ( argv
[0] == NULL
)
802 return; /* SHOULD NOT HAPPEN */
806 if( (keyValue
[0] == '@') && (strlen(keyValue
) == 1) )
808 LONG lLen
= KEY_MAX_LEN
;
809 CHAR
* lpsData
=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,KEY_MAX_LEN
);
811 * We need to query the key default value
813 hRes
= RegQueryValue(
819 if (hRes
==ERROR_MORE_DATA
) {
820 lpsData
=HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,lpsData
,lLen
);
821 hRes
= RegQueryValue(currentKeyHandle
,currentKeyName
,(LPBYTE
)lpsData
,&lLen
);
824 if (hRes
== ERROR_SUCCESS
)
826 lpsRes
= HeapAlloc( GetProcessHeap(), 0, lLen
);
827 strncpy(lpsRes
, lpsData
, lLen
);
833 DWORD dwLen
= KEY_MAX_LEN
;
834 BYTE
* lpbData
=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,KEY_MAX_LEN
);
837 * We need to query a specific value for the key
839 hRes
= RegQueryValueEx(
847 if (hRes
==ERROR_MORE_DATA
) {
848 lpbData
=HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,lpbData
,dwLen
);
849 hRes
= RegQueryValueEx(currentKeyHandle
,keyValue
,NULL
,&dwType
,(LPBYTE
)lpbData
,&dwLen
);
852 if (hRes
== ERROR_SUCCESS
)
855 * Convert the returned data to a displayable format
862 lpsRes
= HeapAlloc( GetProcessHeap(), 0, dwLen
);
863 strncpy(lpsRes
, lpbData
, dwLen
);
864 lpsRes
[dwLen
-1]='\0';
869 lpsRes
= convertHexToDWORDStr(lpbData
, dwLen
);
874 lpsRes
= convertHexToHexCSV(lpbData
, dwLen
);
880 HeapFree(GetProcessHeap(), 0, lpbData
);
884 if ( hRes
== ERROR_SUCCESS
)
886 "%s: Value \"%s\" = \"%s\" in key [%s]\n",
893 printf("%s: ERROR Value \"%s\" not found for key \"%s\".\n",
901 for (counter
=0; counter
<argCounter
; counter
++)
902 if (argv
[counter
] != NULL
)
903 HeapFree(GetProcessHeap(), 0, argv
[counter
]);
906 HeapFree(GetProcessHeap(), 0, lpsRes
);
910 /******************************************************************************
911 * Calls command for each line of a registry file.
912 * Correctly processes comments (in # form), line continuation.
915 * in - input stream to read from
916 * command - command to be called for each line
918 void processRegLines(FILE *in
, CommandAPI command
)
920 LPSTR line
= NULL
; /* line read from input stream */
921 ULONG lineSize
= REG_VAL_BUF_SIZE
;
923 line
= HeapAlloc(GetProcessHeap(), 0, lineSize
);
924 CHECK_ENOUGH_MEMORY(line
);
928 LPSTR s
; /* The pointer into line for where the current fgets should read */
932 size_t size_remaining
;
934 char *s_eol
; /* various local uses */
936 /* Do we need to expand the buffer ? */
937 assert (s
>= line
&& s
<= line
+ lineSize
);
938 size_remaining
= lineSize
- (s
-line
);
939 if (size_remaining
< 2) /* room for 1 character and the \0 */
942 size_t new_size
= lineSize
+ REG_VAL_BUF_SIZE
;
943 if (new_size
> lineSize
) /* no arithmetic overflow */
944 new_buffer
= HeapReAlloc (GetProcessHeap(), 0, line
, new_size
);
947 CHECK_ENOUGH_MEMORY(new_buffer
);
949 s
= line
+ lineSize
- size_remaining
;
951 size_remaining
= lineSize
- (s
-line
);
954 /* Get as much as possible into the buffer, terminated either by
955 * eof, error, eol or getting the maximum amount. Abort on error.
957 size_to_get
= (size_remaining
> INT_MAX
? INT_MAX
: size_remaining
);
958 if (NULL
== fgets (s
, size_to_get
, in
))
962 perror ("While reading input");
969 /* It is not clear to me from the definition that the
970 * contents of the buffer are well defined on detecting
971 * an eof without managing to read anything.
976 /* If we didn't read the eol nor the eof go around for the rest */
977 s_eol
= strchr (s
, '\n');
978 if (!feof (in
) && !s_eol
)
980 s
= strchr (s
, '\0');
981 /* It should be s + size_to_get - 1 but this is safer */
985 /* If it is a comment line then discard it and go around again */
992 /* Remove any line feed. Leave s_eol on the \0 */
996 if (s_eol
> line
&& *(s_eol
-1) == '\r')
1000 s_eol
= strchr (s
, '\0');
1002 /* If there is a concatenating \\ then go around again */
1003 if (s_eol
> line
&& *(s_eol
-1) == '\\')
1007 /* The following error protection could be made more self-
1008 * correcting but I thought it not worth trying.
1010 if ((c
= fgetc (in
)) == EOF
|| c
!= ' ' ||
1011 (c
= fgetc (in
)) == EOF
|| c
!= ' ')
1012 printf ("%s: ERROR - invalid continuation.\n", getAppName());
1016 break; /* That is the full virtual line */
1023 HeapFree(GetProcessHeap(), 0, line
);
1026 /******************************************************************************
1027 * This funtion is the main entry point to the registerDLL action. It
1028 * receives the currently read line, then loads and registers the requested DLLs
1030 void doRegisterDLL(LPSTR stdInput
) {
1034 /* Check for valid input */
1035 if (stdInput
== NULL
)
1038 /* Load and register the library, then free it */
1039 theLib
= LoadLibrary(stdInput
);
1042 FARPROC lpfnDLLRegProc
= GetProcAddress(theLib
, "DllRegisterServer");
1044 retVal
= (*lpfnDLLRegProc
)();
1046 printf("%s: Couldn't find DllRegisterServer proc in '%s'.\n",
1047 getAppName(), stdInput
);
1050 printf("%s: DLLRegisterServer error 0x%x in '%s'.\n",
1051 getAppName(), retVal
, stdInput
);
1053 FreeLibrary(theLib
);
1057 printf("%s: Could not load DLL '%s'.\n", getAppName(), stdInput
);
1061 /******************************************************************************
1062 * This funtion is the main entry point to the unregisterDLL action. It
1063 * receives the currently read line, then loads and unregisters the requested DLLs
1065 void doUnregisterDLL(LPSTR stdInput
) {
1069 /* Check for valid input */
1070 if (stdInput
== NULL
)
1073 /* Load and unregister the library, then free it */
1074 theLib
= LoadLibrary(stdInput
);
1077 FARPROC lpfnDLLRegProc
= GetProcAddress(theLib
, "DllUnregisterServer");
1079 retVal
= (*lpfnDLLRegProc
)();
1081 printf("%s: Couldn't find DllUnregisterServer proc in '%s'.\n",
1082 getAppName(), stdInput
);
1085 printf("%s: DLLUnregisterServer error 0x%x in '%s'.\n",
1086 getAppName(), retVal
, stdInput
);
1088 FreeLibrary(theLib
);
1092 printf("%s: Could not load DLL '%s'.\n", getAppName(), stdInput
);
1096 /****************************************************************************
1097 * REGPROC_print_error
1099 * Print the message for GetLastError
1102 void REGPROC_print_error() {
1107 error_code
= GetLastError ();
1108 status
= FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
1109 NULL
, error_code
, 0, (LPTSTR
) &lpMsgBuf
, 0, NULL
);
1111 printf("%s: Cannot display message for error %ld, status %ld\n",
1112 getAppName(), error_code
, GetLastError());
1116 LocalFree((HLOCAL
)lpMsgBuf
);
1120 /******************************************************************************
1121 * Checks whether the buffer has enough room for the string or required size.
1122 * Resizes the buffer if necessary.
1125 * buffer - pointer to a buffer for string
1126 * len - current length of the buffer in characters.
1127 * required_len - length of the string to place to the buffer in characters.
1128 * The length does not include the terminating null character.
1130 void REGPROC_resize_char_buffer(CHAR
**buffer
, DWORD
*len
, DWORD required_len
)
1133 if (required_len
> *len
)
1135 *len
= required_len
;
1136 *buffer
= HeapReAlloc(GetProcessHeap(), 0, *buffer
,
1137 *len
* sizeof(**buffer
));
1138 CHECK_ENOUGH_MEMORY(*buffer
);
1142 /******************************************************************************
1143 * Prints string str to file
1145 void REGPROC_export_string(FILE *file
, CHAR
*str
)
1147 size_t len
= strlen(str
);
1150 /* escaping characters */
1151 for (i
= 0; i
< len
; i
++)
1157 fputs("\\\\", file
);
1160 fputs("\\\"", file
);
1163 fputs("\\\n", file
);
1172 /******************************************************************************
1173 * Writes contents of the registry key to the specified file stream.
1176 * file - writable file stream to export registry branch to.
1177 * key - registry branch to export.
1178 * reg_key_name_buf - name of the key with registry class.
1179 * Is resized if necessary.
1180 * reg_key_name_len - length of the buffer for the registry class in characters.
1181 * val_name_buf - buffer for storing value name.
1182 * Is resized if necessary.
1183 * val_name_len - length of the buffer for storing value names in characters.
1184 * val_buf - buffer for storing values while extracting.
1185 * Is resized if necessary.
1186 * val_size - size of the buffer for storing values in bytes.
1188 void export_hkey(FILE *file
, HKEY key
,
1189 CHAR
**reg_key_name_buf
, DWORD
*reg_key_name_len
,
1190 CHAR
**val_name_buf
, DWORD
*val_name_len
,
1191 BYTE
**val_buf
, DWORD
*val_size
)
1193 DWORD max_sub_key_len
;
1194 DWORD max_val_name_len
;
1201 /* get size information and resize the buffers if necessary */
1202 if (RegQueryInfoKey(key
, NULL
, NULL
, NULL
, NULL
,
1203 &max_sub_key_len
, NULL
,
1204 NULL
, &max_val_name_len
, &max_val_size
, NULL
, NULL
1207 REGPROC_print_error();
1209 curr_len
= strlen(*reg_key_name_buf
);
1210 REGPROC_resize_char_buffer(reg_key_name_buf
, reg_key_name_len
,
1211 max_sub_key_len
+ curr_len
+ 1);
1212 REGPROC_resize_char_buffer(val_name_buf
, val_name_len
,
1214 if (max_val_size
> *val_size
)
1216 *val_size
= max_val_size
;
1217 *val_buf
= HeapReAlloc(GetProcessHeap(), 0, *val_buf
, *val_size
);
1218 CHECK_ENOUGH_MEMORY(val_buf
);
1221 /* output data for the current key */
1223 fputs(*reg_key_name_buf
, file
);
1225 /* print all the values */
1231 DWORD val_name_len1
= *val_name_len
;
1232 DWORD val_size1
= *val_size
;
1233 ret
= RegEnumValue(key
, i
, *val_name_buf
, &val_name_len1
, NULL
,
1234 &value_type
, *val_buf
, &val_size1
);
1235 if (ret
!= ERROR_SUCCESS
)
1238 if (ret
!= ERROR_NO_MORE_ITEMS
)
1240 REGPROC_print_error();
1245 if ((*val_name_buf
)[0])
1248 REGPROC_export_string(file
, *val_name_buf
);
1259 REGPROC_export_string(file
, *val_buf
);
1260 fputs("\"\n", file
);
1264 fprintf(file
, "dword:%08lx\n", *((DWORD
*)*val_buf
));
1268 printf("%s: warning - unsupported registry format '%ld', "
1269 "treat as binary\n",
1270 getAppName(), value_type
);
1271 printf("key name: \"%s\"\n", *reg_key_name_buf
);
1272 printf("value name:\"%s\"\n\n", *val_name_buf
);
1283 if (value_type
== REG_BINARY
)
1285 hex_prefix
= "hex:";
1288 sprintf(buf
, "hex(%ld):", value_type
);
1291 /* position of where the next character will be printed */
1292 /* NOTE: yes, strlen("hex:") is used even for hex(x): */
1293 cur_pos
= strlen("\"\"=") + strlen("hex:") +
1294 strlen(*val_name_buf
);
1296 fputs(hex_prefix
, file
);
1297 for (i1
= 0; i1
< val_size1
; i1
++)
1299 fprintf(file
, "%02x", (unsigned int)(*val_buf
)[i1
]);
1300 if (i1
+ 1 < val_size1
)
1307 if (cur_pos
> REG_FILE_HEX_LINE_LEN
)
1309 fputs("\\\n ", file
);
1322 (*reg_key_name_buf
)[curr_len
] = '\\';
1325 DWORD buf_len
= *reg_key_name_len
- curr_len
;
1327 ret
= RegEnumKeyEx(key
, i
, *reg_key_name_buf
+ curr_len
+ 1, &buf_len
,
1328 NULL
, NULL
, NULL
, NULL
);
1329 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_MORE_DATA
)
1332 if (ret
!= ERROR_NO_MORE_ITEMS
)
1334 REGPROC_print_error();
1340 if (RegOpenKey(key
, *reg_key_name_buf
+ curr_len
+ 1,
1341 &subkey
) == ERROR_SUCCESS
)
1343 export_hkey(file
, subkey
, reg_key_name_buf
, reg_key_name_len
,
1344 val_name_buf
, val_name_len
, val_buf
, val_size
);
1345 RegCloseKey(subkey
);
1347 REGPROC_print_error();
1351 (*reg_key_name_buf
)[curr_len
] = '\0';
1354 /******************************************************************************
1355 * Open file for export.
1357 FILE *REGPROC_open_export_file(CHAR
*file_name
)
1359 FILE *file
= fopen(file_name
, "w");
1363 printf("%s: Can't open file \"%s\"\n", getAppName(), file_name
);
1366 fputs("REGEDIT4\n", file
);
1370 /******************************************************************************
1371 * Writes contents of the registry key to the specified file stream.
1374 * file_name - name of a file to export registry branch to.
1375 * reg_key_name - registry branch to export. The whole registry is exported if
1376 * reg_key_name is NULL or contains an empty string.
1378 BOOL
export_registry_key(CHAR
*file_name
, CHAR
*reg_key_name
)
1382 CHAR
*reg_key_name_buf
;
1385 DWORD reg_key_name_len
= KEY_MAX_LEN
;
1386 DWORD val_name_len
= KEY_MAX_LEN
;
1387 DWORD val_size
= REG_VAL_BUF_SIZE
;
1390 reg_key_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1391 reg_key_name_len
* sizeof(*reg_key_name_buf
));
1392 val_name_buf
= HeapAlloc(GetProcessHeap(), 0,
1393 val_name_len
* sizeof(*val_name_buf
));
1394 val_buf
= HeapAlloc(GetProcessHeap(), 0, val_size
);
1395 CHECK_ENOUGH_MEMORY(reg_key_name_buf
&& val_name_buf
&& val_buf
);
1397 if (reg_key_name
&& reg_key_name
[0])
1402 REGPROC_resize_char_buffer(®_key_name_buf
, ®_key_name_len
,
1403 strlen(reg_key_name
));
1404 strcpy(reg_key_name_buf
, reg_key_name
);
1406 /* open the specified key */
1407 reg_key_class
= getRegClass(reg_key_name
);
1408 if (reg_key_class
== (HKEY
)ERROR_INVALID_PARAMETER
)
1410 printf("%s: Incorrect registry class specification in '%s'\n",
1411 getAppName(), reg_key_name
);
1414 branch_name
= getRegKeyName(reg_key_name
);
1415 CHECK_ENOUGH_MEMORY(branch_name
);
1416 if (!branch_name
[0])
1418 /* no branch - registry class is specified */
1419 file
= REGPROC_open_export_file(file_name
);
1420 export_hkey(file
, reg_key_class
,
1421 ®_key_name_buf
, ®_key_name_len
,
1422 &val_name_buf
, &val_name_len
,
1423 &val_buf
, &val_size
);
1425 else if (RegOpenKey(reg_key_class
, branch_name
, &key
) == ERROR_SUCCESS
)
1427 file
= REGPROC_open_export_file(file_name
);
1428 export_hkey(file
, key
,
1429 ®_key_name_buf
, ®_key_name_len
,
1430 &val_name_buf
, &val_name_len
,
1431 &val_buf
, &val_size
);
1434 printf("%s: Can't export. Registry key '%s' does not exist!\n",
1435 getAppName(), reg_key_name
);
1436 REGPROC_print_error();
1438 HeapFree(GetProcessHeap(), 0, branch_name
);
1442 /* export all registry classes */
1443 file
= REGPROC_open_export_file(file_name
);
1444 for (i
= 0; i
< REG_CLASS_NUMBER
; i
++)
1446 /* do not export HKEY_CLASSES_ROOT */
1447 if (reg_class_keys
[i
] != HKEY_CLASSES_ROOT
&&
1448 reg_class_keys
[i
] != HKEY_CURRENT_USER
&&
1449 reg_class_keys
[i
] != HKEY_CURRENT_CONFIG
)
1451 strcpy(reg_key_name_buf
, reg_class_names
[i
]);
1452 export_hkey(file
, reg_class_keys
[i
],
1453 ®_key_name_buf
, ®_key_name_len
,
1454 &val_name_buf
, &val_name_len
,
1455 &val_buf
, &val_size
);
1464 HeapFree(GetProcessHeap(), 0, reg_key_name
);
1465 HeapFree(GetProcessHeap(), 0, val_buf
);
1469 /******************************************************************************
1470 * Reads contents of the specified file into the registry.
1472 BOOL
import_registry_file(LPTSTR filename
)
1474 FILE* reg_file
= fopen(filename
, "r");
1477 processRegLines(reg_file
, doSetValue
);
1483 /******************************************************************************
1484 * Recursive function which removes the registry key with all subkeys.
1486 void delete_branch(HKEY key
,
1487 CHAR
**reg_key_name_buf
, DWORD
*reg_key_name_len
)
1490 DWORD max_sub_key_len
;
1496 if (RegOpenKey(key
, *reg_key_name_buf
, &branch_key
) != ERROR_SUCCESS
)
1498 REGPROC_print_error();
1501 /* get size information and resize the buffers if necessary */
1502 if (RegQueryInfoKey(branch_key
, NULL
, NULL
, NULL
,
1503 &subkeys
, &max_sub_key_len
,
1504 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
1507 REGPROC_print_error();
1509 curr_len
= strlen(*reg_key_name_buf
);
1510 REGPROC_resize_char_buffer(reg_key_name_buf
, reg_key_name_len
,
1511 max_sub_key_len
+ curr_len
+ 1);
1513 (*reg_key_name_buf
)[curr_len
] = '\\';
1514 for (i
= subkeys
- 1; i
>= 0; i
--)
1516 DWORD buf_len
= *reg_key_name_len
- curr_len
;
1518 ret
= RegEnumKeyEx(branch_key
, i
, *reg_key_name_buf
+ curr_len
+ 1,
1519 &buf_len
, NULL
, NULL
, NULL
, NULL
);
1520 if (ret
!= ERROR_SUCCESS
&&
1521 ret
!= ERROR_MORE_DATA
&&
1522 ret
!= ERROR_NO_MORE_ITEMS
)
1524 REGPROC_print_error();
1526 delete_branch(key
, reg_key_name_buf
, reg_key_name_len
);
1529 (*reg_key_name_buf
)[curr_len
] = '\0';
1530 RegCloseKey(branch_key
);
1531 RegDeleteKey(key
, *reg_key_name_buf
);
1534 /******************************************************************************
1535 * Removes the registry key with all subkeys. Parses full key name.
1538 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1539 * empty, points to register key class, does not exist.
1541 void delete_registry_key(CHAR
*reg_key_name
)
1544 DWORD branch_name_len
;
1548 if (!reg_key_name
|| !reg_key_name
[0])
1550 /* open the specified key */
1551 reg_key_class
= getRegClass(reg_key_name
);
1552 if (reg_key_class
== (HKEY
)ERROR_INVALID_PARAMETER
)
1554 printf("%s: Incorrect registry class specification in '%s'\n",
1555 getAppName(), reg_key_name
);
1558 branch_name
= getRegKeyName(reg_key_name
);
1559 CHECK_ENOUGH_MEMORY(branch_name
);
1560 branch_name_len
= strlen(branch_name
);
1561 if (!branch_name
[0])
1563 printf("%s: Can't delete registry class '%s'\n",
1564 getAppName(), reg_key_name
);
1567 if (RegOpenKey(reg_key_class
, branch_name
, &branch_key
) == ERROR_SUCCESS
)
1569 /* check whether the key exists */
1570 RegCloseKey(branch_key
);
1571 delete_branch(reg_key_class
, &branch_name
, &branch_name_len
);
1573 HeapFree(GetProcessHeap(), 0, branch_name
);
1576 /******************************************************************************
1577 * Sets the application name. Then application name is used in the error
1580 void setAppName(CHAR
*name
)