push cc8bc80451cc24f4d7cf75168b569f0ebfe19547
[wine/hacks.git] / programs / regedit / regproc.c
blobc46edc6b369bac4b7a77761d41ff22e4e4c3cf0d
1 /*
2 * Registry processing routines. Routines, common for registry
3 * processing frontends.
5 * Copyright 1999 Sylvain St-Germain
6 * Copyright 2002 Andriy Palamarchuk
7 * Copyright 2008 Alexander N. Sørnes <alex@thehandofagony.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <limits.h>
25 #include <stdio.h>
26 #include <windows.h>
27 #include <winnt.h>
28 #include <winreg.h>
29 #include <assert.h>
30 #include <wine/unicode.h>
31 #include "regproc.h"
33 #define REG_VAL_BUF_SIZE 4096
35 /* maximal number of characters in hexadecimal data line,
36 * including the indentation, but not including the '\' character
38 #define REG_FILE_HEX_LINE_LEN (2 + 25 * 3)
40 static const CHAR *reg_class_names[] = {
41 "HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_CLASSES_ROOT",
42 "HKEY_CURRENT_CONFIG", "HKEY_CURRENT_USER", "HKEY_DYN_DATA"
45 #define REG_CLASS_NUMBER (sizeof(reg_class_names) / sizeof(reg_class_names[0]))
47 extern const WCHAR* reg_class_namesW[];
49 static HKEY reg_class_keys[REG_CLASS_NUMBER] = {
50 HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CLASSES_ROOT,
51 HKEY_CURRENT_CONFIG, HKEY_CURRENT_USER, HKEY_DYN_DATA
54 /* return values */
55 #define NOT_ENOUGH_MEMORY 1
56 #define IO_ERROR 2
58 /* processing macros */
60 /* common check of memory allocation results */
61 #define CHECK_ENOUGH_MEMORY(p) \
62 if (!(p)) \
63 { \
64 fprintf(stderr,"%s: file %s, line %d: Not enough memory\n", \
65 getAppName(), __FILE__, __LINE__); \
66 exit(NOT_ENOUGH_MEMORY); \
69 /******************************************************************************
70 * Allocates memory and converts input from multibyte to wide chars
71 * Returned string must be freed by the caller
73 WCHAR* GetWideString(const char* strA)
75 if(strA)
77 WCHAR* strW;
78 int len = MultiByteToWideChar(CP_ACP, 0, strA, -1, NULL, 0);
80 strW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
81 CHECK_ENOUGH_MEMORY(strW);
82 MultiByteToWideChar(CP_ACP, 0, strA, -1, strW, len);
83 return strW;
85 return NULL;
88 /******************************************************************************
89 * Allocates memory and converts input from multibyte to wide chars
90 * Returned string must be freed by the caller
92 static WCHAR* GetWideStringN(const char* strA, int chars, DWORD *len)
94 if(strA)
96 WCHAR* strW;
97 *len = MultiByteToWideChar(CP_ACP, 0, strA, chars, NULL, 0);
99 strW = HeapAlloc(GetProcessHeap(), 0, *len * sizeof(WCHAR));
100 CHECK_ENOUGH_MEMORY(strW);
101 MultiByteToWideChar(CP_ACP, 0, strA, chars, strW, *len);
102 return strW;
104 *len = 0;
105 return NULL;
108 /******************************************************************************
109 * Allocates memory and converts input from wide chars to multibyte
110 * Returned string must be freed by the caller
112 char* GetMultiByteString(const WCHAR* strW)
114 if(strW)
116 char* strA;
117 int len = WideCharToMultiByte(CP_ACP, 0, strW, -1, NULL, 0, NULL, NULL);
119 strA = HeapAlloc(GetProcessHeap(), 0, len);
120 CHECK_ENOUGH_MEMORY(strA);
121 WideCharToMultiByte(CP_ACP, 0, strW, -1, strA, len, NULL, NULL);
122 return strA;
124 return NULL;
127 /******************************************************************************
128 * Allocates memory and converts input from wide chars to multibyte
129 * Returned string must be freed by the caller
131 static char* GetMultiByteStringN(const WCHAR* strW, int chars, DWORD* len)
133 if(strW)
135 char* strA;
136 *len = WideCharToMultiByte(CP_ACP, 0, strW, chars, NULL, 0, NULL, NULL);
138 strA = HeapAlloc(GetProcessHeap(), 0, *len);
139 CHECK_ENOUGH_MEMORY(strA);
140 WideCharToMultiByte(CP_ACP, 0, strW, chars, strA, *len, NULL, NULL);
141 return strA;
143 *len = 0;
144 return NULL;
147 /******************************************************************************
148 * Converts a hex representation of a DWORD into a DWORD.
150 static BOOL convertHexToDWord(WCHAR* str, DWORD *dw)
152 char buf[9];
153 char dummy;
155 WideCharToMultiByte(CP_ACP, 0, str, -1, buf, 9, NULL, NULL);
156 if (lstrlenW(str) > 8 || sscanf(buf, "%x%c", dw, &dummy) != 1) {
157 fprintf(stderr,"%s: ERROR, invalid hex value\n", getAppName());
158 return FALSE;
160 return TRUE;
163 /******************************************************************************
164 * Converts a hex comma separated values list into a binary string.
166 static BYTE* convertHexCSVToHex(WCHAR *str, DWORD *size)
168 WCHAR *s;
169 BYTE *d, *data;
171 /* The worst case is 1 digit + 1 comma per byte */
172 *size=(lstrlenW(str)+1)/2;
173 data=HeapAlloc(GetProcessHeap(), 0, *size);
174 CHECK_ENOUGH_MEMORY(data);
176 s = str;
177 d = data;
178 *size=0;
179 while (*s != '\0') {
180 UINT wc;
181 WCHAR *end;
183 wc = strtoulW(s,&end,16);
184 if (end == s || wc > 0xff || (*end && *end != ',')) {
185 char* strA = GetMultiByteString(s);
186 fprintf(stderr,"%s: ERROR converting CSV hex stream. Invalid value at '%s'\n",
187 getAppName(), strA);
188 HeapFree(GetProcessHeap(), 0, data);
189 HeapFree(GetProcessHeap(), 0, strA);
190 return NULL;
192 *d++ =(BYTE)wc;
193 (*size)++;
194 if (*end) end++;
195 s = end;
198 return data;
201 /******************************************************************************
202 * This function returns the HKEY associated with the data type encoded in the
203 * value. It modifies the input parameter (key value) in order to skip this
204 * "now useless" data type information.
206 * Note: Updated based on the algorithm used in 'server/registry.c'
208 static DWORD getDataType(LPWSTR *lpValue, DWORD* parse_type)
210 struct data_type { const WCHAR *tag; int len; int type; int parse_type; };
212 static const WCHAR quote[] = {'"'};
213 static const WCHAR str[] = {'s','t','r',':','"'};
214 static const WCHAR str2[] = {'s','t','r','(','2',')',':','"'};
215 static const WCHAR hex[] = {'h','e','x',':'};
216 static const WCHAR dword[] = {'d','w','o','r','d',':'};
217 static const WCHAR hexp[] = {'h','e','x','('};
219 static const struct data_type data_types[] = { /* actual type */ /* type to assume for parsing */
220 { quote, 1, REG_SZ, REG_SZ },
221 { str, 5, REG_SZ, REG_SZ },
222 { str2, 8, REG_EXPAND_SZ, REG_SZ },
223 { hex, 4, REG_BINARY, REG_BINARY },
224 { dword, 6, REG_DWORD, REG_DWORD },
225 { hexp, 4, -1, REG_BINARY },
226 { NULL, 0, 0, 0 }
229 const struct data_type *ptr;
230 int type;
232 for (ptr = data_types; ptr->tag; ptr++) {
233 if (strncmpW( ptr->tag, *lpValue, ptr->len ))
234 continue;
236 /* Found! */
237 *parse_type = ptr->parse_type;
238 type=ptr->type;
239 *lpValue+=ptr->len;
240 if (type == -1) {
241 WCHAR* end;
243 /* "hex(xx):" is special */
244 type = (int)strtoulW( *lpValue , &end, 16 );
245 if (**lpValue=='\0' || *end!=')' || *(end+1)!=':') {
246 type=REG_NONE;
247 } else {
248 *lpValue = end + 2;
251 return type;
253 *parse_type=REG_NONE;
254 return REG_NONE;
257 /******************************************************************************
258 * Replaces escape sequences with the characters.
260 static void REGPROC_unescape_string(WCHAR* str)
262 int str_idx = 0; /* current character under analysis */
263 int val_idx = 0; /* the last character of the unescaped string */
264 int len = lstrlenW(str);
265 for (str_idx = 0; str_idx < len; str_idx++, val_idx++) {
266 if (str[str_idx] == '\\') {
267 str_idx++;
268 switch (str[str_idx]) {
269 case 'n':
270 str[val_idx] = '\n';
271 break;
272 case '\\':
273 case '"':
274 str[val_idx] = str[str_idx];
275 break;
276 default:
277 fprintf(stderr,"Warning! Unrecognized escape sequence: \\%c'\n",
278 str[str_idx]);
279 str[val_idx] = str[str_idx];
280 break;
282 } else {
283 str[val_idx] = str[str_idx];
286 str[val_idx] = '\0';
289 static BOOL parseKeyName(LPWSTR lpKeyName, HKEY *hKey, LPWSTR *lpKeyPath)
291 WCHAR* lpSlash = NULL;
292 unsigned int i, len;
294 if (lpKeyName == NULL)
295 return FALSE;
297 for(i = 0; *(lpKeyName+i) != 0; i++)
299 if(*(lpKeyName+i) == '\\')
301 lpSlash = lpKeyName+i;
302 break;
306 if (lpSlash)
308 len = lpSlash-lpKeyName;
310 else
312 len = lstrlenW(lpKeyName);
313 lpSlash = lpKeyName+len;
315 *hKey = NULL;
317 for (i = 0; i < REG_CLASS_NUMBER; i++) {
318 if (CompareStringW(LOCALE_USER_DEFAULT, 0, lpKeyName, len, reg_class_namesW[i], len) == CSTR_EQUAL &&
319 len == lstrlenW(reg_class_namesW[i])) {
320 *hKey = reg_class_keys[i];
321 break;
325 if (*hKey == NULL)
326 return FALSE;
329 if (*lpSlash != '\0')
330 lpSlash++;
331 *lpKeyPath = lpSlash;
332 return TRUE;
335 /* Globals used by the setValue() & co */
336 static LPSTR currentKeyName;
337 static HKEY currentKeyHandle = NULL;
339 /******************************************************************************
340 * Sets the value with name val_name to the data in val_data for the currently
341 * opened key.
343 * Parameters:
344 * val_name - name of the registry value
345 * val_data - registry value data
347 static LONG setValue(WCHAR* val_name, WCHAR* val_data, BOOL is_unicode)
349 LONG res;
350 DWORD dwDataType, dwParseType;
351 LPBYTE lpbData;
352 DWORD dwData, dwLen;
353 WCHAR del[] = {'-',0};
355 if ( (val_name == NULL) || (val_data == NULL) )
356 return ERROR_INVALID_PARAMETER;
358 if (lstrcmpW(val_data, del) == 0)
360 res=RegDeleteValueW(currentKeyHandle,val_name);
361 return (res == ERROR_FILE_NOT_FOUND ? ERROR_SUCCESS : res);
364 /* Get the data type stored into the value field */
365 dwDataType = getDataType(&val_data, &dwParseType);
367 if (dwParseType == REG_SZ) /* no conversion for string */
369 REGPROC_unescape_string(val_data);
370 /* Compute dwLen after REGPROC_unescape_string because it may
371 * have changed the string length and we don't want to store
372 * the extra garbage in the registry.
374 dwLen = lstrlenW(val_data);
375 if (dwLen>0 && val_data[dwLen-1]=='"')
377 dwLen--;
378 val_data[dwLen]='\0';
380 lpbData = (BYTE*) val_data;
381 dwLen++; /* include terminating null */
382 dwLen = dwLen * sizeof(WCHAR); /* size is in bytes */
384 else if (dwParseType == REG_DWORD) /* Convert the dword types */
386 if (!convertHexToDWord(val_data, &dwData))
387 return ERROR_INVALID_DATA;
388 lpbData = (BYTE*)&dwData;
389 dwLen = sizeof(dwData);
391 else if (dwParseType == REG_BINARY) /* Convert the binary data */
393 lpbData = convertHexCSVToHex(val_data, &dwLen);
394 if (!lpbData)
395 return ERROR_INVALID_DATA;
397 if((dwDataType == REG_MULTI_SZ || dwDataType == REG_EXPAND_SZ) && !is_unicode)
399 LPBYTE tmp = lpbData;
400 lpbData = (LPBYTE)GetWideStringN((char*)lpbData, dwLen, &dwLen);
401 dwLen *= sizeof(WCHAR);
402 HeapFree(GetProcessHeap(), 0, tmp);
405 else /* unknown format */
407 fprintf(stderr,"%s: ERROR, unknown data format\n", getAppName());
408 return ERROR_INVALID_DATA;
411 res = RegSetValueExW(
412 currentKeyHandle,
413 val_name,
414 0, /* Reserved */
415 dwDataType,
416 lpbData,
417 dwLen);
418 if (dwParseType == REG_BINARY)
419 HeapFree(GetProcessHeap(), 0, lpbData);
420 return res;
423 /******************************************************************************
424 * A helper function for processRegEntry() that opens the current key.
425 * That key must be closed by calling closeKey().
427 static LONG openKeyW(WCHAR* stdInput)
429 HKEY keyClass;
430 WCHAR* keyPath;
431 DWORD dwDisp;
432 LONG res;
434 /* Sanity checks */
435 if (stdInput == NULL)
436 return ERROR_INVALID_PARAMETER;
438 /* Get the registry class */
439 if (!parseKeyName(stdInput, &keyClass, &keyPath))
440 return ERROR_INVALID_PARAMETER;
442 res = RegCreateKeyExW(
443 keyClass, /* Class */
444 keyPath, /* Sub Key */
445 0, /* MUST BE 0 */
446 NULL, /* object type */
447 REG_OPTION_NON_VOLATILE, /* option, REG_OPTION_NON_VOLATILE ... */
448 KEY_ALL_ACCESS, /* access mask, KEY_ALL_ACCESS */
449 NULL, /* security attribute */
450 &currentKeyHandle, /* result */
451 &dwDisp); /* disposition, REG_CREATED_NEW_KEY or
452 REG_OPENED_EXISTING_KEY */
454 if (res == ERROR_SUCCESS)
455 currentKeyName = GetMultiByteString(stdInput);
456 else
457 currentKeyHandle = NULL;
459 return res;
463 /******************************************************************************
464 * Close the currently opened key.
466 static void closeKey(void)
468 if (currentKeyHandle)
470 HeapFree(GetProcessHeap(), 0, currentKeyName);
471 RegCloseKey(currentKeyHandle);
472 currentKeyHandle = NULL;
476 /******************************************************************************
477 * This function is a wrapper for the setValue function. It prepares the
478 * land and cleans the area once completed.
479 * Note: this function modifies the line parameter.
481 * line - registry file unwrapped line. Should have the registry value name and
482 * complete registry value data.
484 static void processSetValue(WCHAR* line, BOOL is_unicode)
486 WCHAR* val_name; /* registry value name */
487 WCHAR* val_data; /* registry value data */
488 int line_idx = 0; /* current character under analysis */
489 LONG res;
491 /* get value name */
492 while ( isspaceW(line[line_idx]) ) line_idx++;
493 if (line[line_idx] == '@' && line[line_idx + 1] == '=') {
494 line[line_idx] = '\0';
495 val_name = line;
496 line_idx++;
497 } else if (line[line_idx] == '\"') {
498 line_idx++;
499 val_name = line + line_idx;
500 while (TRUE) {
501 if (line[line_idx] == '\\') /* skip escaped character */
503 line_idx += 2;
504 } else {
505 if (line[line_idx] == '\"') {
506 line[line_idx] = '\0';
507 line_idx++;
508 break;
509 } else {
510 line_idx++;
514 while ( isspaceW(line[line_idx]) ) line_idx++;
515 if (line[line_idx] != '=') {
516 char* lineA;
517 line[line_idx] = '\"';
518 lineA = GetMultiByteString(line);
519 fprintf(stderr,"Warning! unrecognized line:\n%s\n", lineA);
520 HeapFree(GetProcessHeap(), 0, lineA);
521 return;
524 } else {
525 char* lineA = GetMultiByteString(line);
526 fprintf(stderr,"Warning! unrecognized line:\n%s\n", lineA);
527 HeapFree(GetProcessHeap(), 0, lineA);
528 return;
530 line_idx++; /* skip the '=' character */
532 while ( isspaceW(line[line_idx]) ) line_idx++;
533 val_data = line + line_idx;
534 /* trim trailing blanks */
535 line_idx = strlenW(val_data);
536 while (line_idx > 0 && isspaceW(val_data[line_idx-1])) line_idx--;
537 val_data[line_idx] = '\0';
539 REGPROC_unescape_string(val_name);
540 res = setValue(val_name, val_data, is_unicode);
541 if ( res != ERROR_SUCCESS )
543 char* val_nameA = GetMultiByteString(val_name);
544 char* val_dataA = GetMultiByteString(val_data);
545 fprintf(stderr,"%s: ERROR Key %s not created. Value: %s, Data: %s\n",
546 getAppName(),
547 currentKeyName,
548 val_nameA,
549 val_dataA);
550 HeapFree(GetProcessHeap(), 0, val_nameA);
551 HeapFree(GetProcessHeap(), 0, val_dataA);
555 /******************************************************************************
556 * This function receives the currently read entry and performs the
557 * corresponding action.
558 * isUnicode affects parsing of REG_MULTI_SZ values
560 static void processRegEntry(WCHAR* stdInput, BOOL isUnicode)
563 * We encountered the end of the file, make sure we
564 * close the opened key and exit
566 if (stdInput == NULL) {
567 closeKey();
568 return;
571 if ( stdInput[0] == '[') /* We are reading a new key */
573 WCHAR* keyEnd;
574 closeKey(); /* Close the previous key */
576 /* Get rid of the square brackets */
577 stdInput++;
578 keyEnd = strrchrW(stdInput, ']');
579 if (keyEnd)
580 *keyEnd='\0';
582 /* delete the key if we encounter '-' at the start of reg key */
583 if ( stdInput[0] == '-')
585 delete_registry_key(stdInput + 1);
586 } else if ( openKeyW(stdInput) != ERROR_SUCCESS )
588 char* stdInputA = GetMultiByteString(stdInput);
589 fprintf(stderr,"%s: setValue failed to open key %s\n",
590 getAppName(), stdInputA);
591 HeapFree(GetProcessHeap(), 0, stdInputA);
593 } else if( currentKeyHandle &&
594 (( stdInput[0] == '@') || /* reading a default @=data pair */
595 ( stdInput[0] == '\"'))) /* reading a new value=data pair */
597 processSetValue(stdInput, isUnicode);
598 } else
600 /* Since we are assuming that the file format is valid we must be
601 * reading a blank line which indicates the end of this key processing
603 closeKey();
607 /******************************************************************************
608 * Processes a registry file.
609 * Correctly processes comments (in # form), line continuation.
611 * Parameters:
612 * in - input stream to read from
613 * first_chars - beginning of stream, read due to Unicode check
615 static void processRegLinesA(FILE *in, char* first_chars)
617 LPSTR line = NULL; /* line read from input stream */
618 ULONG lineSize = REG_VAL_BUF_SIZE;
620 line = HeapAlloc(GetProcessHeap(), 0, lineSize);
621 CHECK_ENOUGH_MEMORY(line);
622 memcpy(line, first_chars, 2);
624 while (!feof(in)) {
625 LPSTR s; /* The pointer into line for where the current fgets should read */
626 LPSTR check;
627 WCHAR* lineW;
628 s = line;
630 if(first_chars)
632 s += 2;
633 first_chars = NULL;
636 for (;;) {
637 size_t size_remaining;
638 int size_to_get;
639 char *s_eol; /* various local uses */
641 /* Do we need to expand the buffer ? */
642 assert (s >= line && s <= line + lineSize);
643 size_remaining = lineSize - (s-line);
644 if (size_remaining < 2) /* room for 1 character and the \0 */
646 char *new_buffer;
647 size_t new_size = lineSize + REG_VAL_BUF_SIZE;
648 if (new_size > lineSize) /* no arithmetic overflow */
649 new_buffer = HeapReAlloc (GetProcessHeap(), 0, line, new_size);
650 else
651 new_buffer = NULL;
652 CHECK_ENOUGH_MEMORY(new_buffer);
653 line = new_buffer;
654 s = line + lineSize - size_remaining;
655 lineSize = new_size;
656 size_remaining = lineSize - (s-line);
659 /* Get as much as possible into the buffer, terminated either by
660 * eof, error, eol or getting the maximum amount. Abort on error.
662 size_to_get = (size_remaining > INT_MAX ? INT_MAX : size_remaining);
664 check = fgets (s, size_to_get, in);
666 if (check == NULL) {
667 if (ferror(in)) {
668 perror ("While reading input");
669 exit (IO_ERROR);
670 } else {
671 assert (feof(in));
672 *s = '\0';
673 /* It is not clear to me from the definition that the
674 * contents of the buffer are well defined on detecting
675 * an eof without managing to read anything.
680 /* If we didn't read the eol nor the eof go around for the rest */
681 s_eol = strchr (s, '\n');
682 if (!feof (in) && !s_eol) {
683 s = strchr (s, '\0');
684 /* It should be s + size_to_get - 1 but this is safer */
685 continue;
688 /* If it is a comment line then discard it and go around again */
689 if (line [0] == '#') {
690 s = line;
691 continue;
694 /* Remove any line feed. Leave s_eol on the \0 */
695 if (s_eol) {
696 *s_eol = '\0';
697 if (s_eol > line && *(s_eol-1) == '\r')
698 *--s_eol = '\0';
699 } else
700 s_eol = strchr (s, '\0');
702 /* If there is a concatenating \\ then go around again */
703 if (s_eol > line && *(s_eol-1) == '\\') {
704 int c;
705 s = s_eol-1;
709 c = fgetc(in);
710 } while(c == ' ' || c == '\t');
712 if(c == EOF)
714 fprintf(stderr,"%s: ERROR - invalid continuation.\n",
715 getAppName());
717 else
719 *s = c;
720 s++;
722 continue;
725 lineW = GetWideString(line);
727 break; /* That is the full virtual line */
730 processRegEntry(lineW, FALSE);
731 HeapFree(GetProcessHeap(), 0, lineW);
733 processRegEntry(NULL, FALSE);
735 HeapFree(GetProcessHeap(), 0, line);
738 static void processRegLinesW(FILE *in)
740 WCHAR* buf = NULL; /* line read from input stream */
741 ULONG lineSize = REG_VAL_BUF_SIZE;
742 size_t CharsInBuf = -1;
744 WCHAR* s; /* The pointer into buf for where the current fgets should read */
745 WCHAR* line; /* The start of the current line */
747 buf = HeapAlloc(GetProcessHeap(), 0, lineSize * sizeof(WCHAR));
748 CHECK_ENOUGH_MEMORY(buf);
750 s = buf;
751 line = buf;
753 while(!feof(in)) {
754 size_t size_remaining;
755 int size_to_get;
756 WCHAR *s_eol = NULL; /* various local uses */
758 /* Do we need to expand the buffer ? */
759 assert (s >= buf && s <= buf + lineSize);
760 size_remaining = lineSize - (s-buf);
761 if (size_remaining < 2) /* room for 1 character and the \0 */
763 WCHAR *new_buffer;
764 size_t new_size = lineSize + (REG_VAL_BUF_SIZE / sizeof(WCHAR));
765 if (new_size > lineSize) /* no arithmetic overflow */
766 new_buffer = HeapReAlloc (GetProcessHeap(), 0, buf, new_size * sizeof(WCHAR));
767 else
768 new_buffer = NULL;
769 CHECK_ENOUGH_MEMORY(new_buffer);
770 buf = new_buffer;
771 line = buf;
772 s = buf + lineSize - size_remaining;
773 lineSize = new_size;
774 size_remaining = lineSize - (s-buf);
777 /* Get as much as possible into the buffer, terminated either by
778 * eof, error or getting the maximum amount. Abort on error.
780 size_to_get = (size_remaining > INT_MAX ? INT_MAX : size_remaining);
782 CharsInBuf = fread(s, sizeof(WCHAR), size_to_get - 1, in);
783 s[CharsInBuf] = 0;
785 if (CharsInBuf == 0) {
786 if (ferror(in)) {
787 perror ("While reading input");
788 exit (IO_ERROR);
789 } else {
790 assert (feof(in));
791 *s = '\0';
792 /* It is not clear to me from the definition that the
793 * contents of the buffer are well defined on detecting
794 * an eof without managing to read anything.
799 /* If we didn't read the eol nor the eof go around for the rest */
800 while(1)
802 s_eol = strchrW(line, '\n');
804 if(!s_eol) {
805 /* Move the stub of the line to the start of the buffer so
806 * we get the maximum space to read into, and so we don't
807 * have to recalculate 'line' if the buffer expands */
808 MoveMemory(buf, line, (strlenW(line)+1) * sizeof(WCHAR));
809 line = buf;
810 s = strchrW(line, '\0');
811 break;
814 /* If it is a comment line then discard it and go around again */
815 if (*line == '#') {
816 line = s_eol + 1;
817 continue;
820 /* If there is a concatenating \\ then go around again */
821 if ((*(s_eol-1) == '\\') ||
822 (*(s_eol-1) == '\r' && *(s_eol-2) == '\\')) {
823 WCHAR* NextLine = s_eol;
825 while(*(NextLine+1) == ' ' || *(NextLine+1) == '\t')
826 NextLine++;
828 NextLine++;
830 if(*(s_eol-1) == '\r')
831 s_eol--;
833 MoveMemory(s_eol - 1, NextLine, (CharsInBuf - (NextLine - s) + 1)*sizeof(WCHAR));
834 CharsInBuf -= NextLine - s_eol + 1;
835 s_eol = 0;
836 continue;
839 /* Remove any line feed. Leave s_eol on the \0 */
840 if (s_eol) {
841 *s_eol = '\0';
842 if (s_eol > buf && *(s_eol-1) == '\r')
843 *(s_eol-1) = '\0';
846 if(!s_eol)
847 break;
849 processRegEntry(line, TRUE);
850 line = s_eol + 1;
851 s_eol = 0;
852 continue; /* That is the full virtual line */
856 processRegEntry(NULL, TRUE);
858 HeapFree(GetProcessHeap(), 0, buf);
861 /****************************************************************************
862 * REGPROC_print_error
864 * Print the message for GetLastError
867 static void REGPROC_print_error(void)
869 LPVOID lpMsgBuf;
870 DWORD error_code;
871 int status;
873 error_code = GetLastError ();
874 status = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
875 NULL, error_code, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
876 if (!status) {
877 fprintf(stderr,"%s: Cannot display message for error %d, status %d\n",
878 getAppName(), error_code, GetLastError());
879 exit(1);
881 puts(lpMsgBuf);
882 LocalFree(lpMsgBuf);
883 exit(1);
886 /******************************************************************************
887 * Checks whether the buffer has enough room for the string or required size.
888 * Resizes the buffer if necessary.
890 * Parameters:
891 * buffer - pointer to a buffer for string
892 * len - current length of the buffer in characters.
893 * required_len - length of the string to place to the buffer in characters.
894 * The length does not include the terminating null character.
896 static void REGPROC_resize_char_buffer(WCHAR **buffer, DWORD *len, DWORD required_len)
898 required_len++;
899 if (required_len > *len) {
900 *len = required_len;
901 if (!*buffer)
902 *buffer = HeapAlloc(GetProcessHeap(), 0, *len * sizeof(**buffer));
903 else
904 *buffer = HeapReAlloc(GetProcessHeap(), 0, *buffer, *len * sizeof(**buffer));
905 CHECK_ENOUGH_MEMORY(*buffer);
909 /******************************************************************************
910 * Same as REGPROC_resize_char_buffer() but on a regular buffer.
912 * Parameters:
913 * buffer - pointer to a buffer
914 * len - current size of the buffer in bytes
915 * required_size - size of the data to place in the buffer in bytes
917 static void REGPROC_resize_binary_buffer(BYTE **buffer, DWORD *size, DWORD required_size)
919 if (required_size > *size) {
920 *size = required_size;
921 if (!*buffer)
922 *buffer = HeapAlloc(GetProcessHeap(), 0, *size);
923 else
924 *buffer = HeapReAlloc(GetProcessHeap(), 0, *buffer, *size);
925 CHECK_ENOUGH_MEMORY(*buffer);
929 /******************************************************************************
930 * Prints string str to file
932 static void REGPROC_export_string(WCHAR **line_buf, DWORD *line_buf_size, DWORD *line_len, WCHAR *str, DWORD str_len)
934 DWORD i, pos;
935 DWORD extra = 0;
937 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + 10);
939 /* escaping characters */
940 pos = *line_len;
941 for (i = 0; i < str_len; i++) {
942 WCHAR c = str[i];
943 switch (c) {
944 case '\n':
945 extra++;
946 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + extra);
947 (*line_buf)[pos++] = '\\';
948 (*line_buf)[pos++] = 'n';
949 break;
951 case '\\':
952 case '"':
953 extra++;
954 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len + str_len + extra);
955 (*line_buf)[pos++] = '\\';
956 /* Fall through */
958 default:
959 (*line_buf)[pos++] = c;
960 break;
963 (*line_buf)[pos] = '\0';
964 *line_len = pos;
967 static void REGPROC_export_binary(WCHAR **line_buf, DWORD *line_buf_size, DWORD *line_len, DWORD type, BYTE *value, DWORD value_size, BOOL unicode)
969 DWORD hex_pos, data_pos;
970 const WCHAR *hex_prefix;
971 const WCHAR hex[] = {'h','e','x',':',0};
972 WCHAR hex_buf[17];
973 const WCHAR concat[] = {'\\','\n',' ',' ',0};
974 DWORD concat_prefix, concat_len;
975 const WCHAR newline[] = {'\n',0};
976 CHAR* value_multibyte = NULL;
978 if (type == REG_BINARY) {
979 hex_prefix = hex;
980 } else {
981 const WCHAR hex_format[] = {'h','e','x','(','%','u',')',':',0};
982 hex_prefix = hex_buf;
983 sprintfW(hex_buf, hex_format, type);
984 if ((type == REG_SZ || type == REG_EXPAND_SZ || type == REG_MULTI_SZ) && !unicode)
986 value_multibyte = GetMultiByteStringN((WCHAR*)value, value_size / sizeof(WCHAR), &value_size);
987 value = (BYTE*)value_multibyte;
991 concat_len = lstrlenW(concat);
992 concat_prefix = 2;
994 hex_pos = *line_len;
995 *line_len += lstrlenW(hex_prefix);
996 data_pos = *line_len;
997 *line_len += value_size * 3;
998 /* - The 2 spaces that concat places at the start of the
999 * line effectively reduce the space available for data.
1000 * - If the value name and hex prefix are very long
1001 * ( > REG_FILE_HEX_LINE_LEN) then we may overestimate
1002 * the needed number of lines by one. But that's ok.
1003 * - The trailing linefeed takes the place of a comma so
1004 * it's accounted for already.
1006 *line_len += *line_len / (REG_FILE_HEX_LINE_LEN - concat_prefix) * concat_len;
1007 REGPROC_resize_char_buffer(line_buf, line_buf_size, *line_len);
1008 lstrcpyW(*line_buf + hex_pos, hex_prefix);
1009 if (value_size)
1011 const WCHAR format[] = {'%','0','2','x',0};
1012 DWORD i, column;
1014 column = data_pos; /* no line wrap yet */
1015 i = 0;
1016 while (1)
1018 sprintfW(*line_buf + data_pos, format, (unsigned int)value[i]);
1019 data_pos += 2;
1020 if (++i == value_size)
1021 break;
1023 (*line_buf)[data_pos++] = ',';
1024 column += 3;
1026 /* wrap the line */
1027 if (column >= REG_FILE_HEX_LINE_LEN) {
1028 lstrcpyW(*line_buf + data_pos, concat);
1029 data_pos += concat_len;
1030 column = concat_prefix;
1034 lstrcpyW(*line_buf + data_pos, newline);
1035 HeapFree(GetProcessHeap(), 0, value_multibyte);
1038 /******************************************************************************
1039 * Writes the given line to a file, in multi-byte or wide characters
1041 static void REGPROC_write_line(FILE *file, const WCHAR* str, BOOL unicode)
1043 if(unicode)
1045 fwrite(str, sizeof(WCHAR), lstrlenW(str), file);
1046 } else
1048 char* strA = GetMultiByteString(str);
1049 fputs(strA, file);
1050 HeapFree(GetProcessHeap(), 0, strA);
1054 /******************************************************************************
1055 * Writes contents of the registry key to the specified file stream.
1057 * Parameters:
1058 * file - writable file stream to export registry branch to.
1059 * key - registry branch to export.
1060 * reg_key_name_buf - name of the key with registry class.
1061 * Is resized if necessary.
1062 * reg_key_name_size - length of the buffer for the registry class in characters.
1063 * val_name_buf - buffer for storing value name.
1064 * Is resized if necessary.
1065 * val_name_size - length of the buffer for storing value names in characters.
1066 * val_buf - buffer for storing values while extracting.
1067 * Is resized if necessary.
1068 * val_size - size of the buffer for storing values in bytes.
1070 static void export_hkey(FILE *file, HKEY key,
1071 WCHAR **reg_key_name_buf, DWORD *reg_key_name_size,
1072 WCHAR **val_name_buf, DWORD *val_name_size,
1073 BYTE **val_buf, DWORD *val_size,
1074 WCHAR **line_buf, DWORD *line_buf_size,
1075 BOOL unicode)
1077 DWORD max_sub_key_len;
1078 DWORD max_val_name_len;
1079 DWORD max_val_size;
1080 DWORD curr_len;
1081 DWORD i;
1082 BOOL more_data;
1083 LONG ret;
1084 WCHAR key_format[] = {'\n','[','%','s',']','\n',0};
1086 /* get size information and resize the buffers if necessary */
1087 if (RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL,
1088 &max_sub_key_len, NULL,
1089 NULL, &max_val_name_len, &max_val_size, NULL, NULL
1090 ) != ERROR_SUCCESS) {
1091 REGPROC_print_error();
1093 curr_len = strlenW(*reg_key_name_buf);
1094 REGPROC_resize_char_buffer(reg_key_name_buf, reg_key_name_size,
1095 max_sub_key_len + curr_len + 1);
1096 REGPROC_resize_char_buffer(val_name_buf, val_name_size,
1097 max_val_name_len);
1098 REGPROC_resize_binary_buffer(val_buf, val_size, max_val_size);
1099 REGPROC_resize_char_buffer(line_buf, line_buf_size, lstrlenW(*reg_key_name_buf) + 4);
1100 /* output data for the current key */
1101 sprintfW(*line_buf, key_format, *reg_key_name_buf);
1102 REGPROC_write_line(file, *line_buf, unicode);
1104 /* print all the values */
1105 i = 0;
1106 more_data = TRUE;
1107 while(more_data) {
1108 DWORD value_type;
1109 DWORD val_name_size1 = *val_name_size;
1110 DWORD val_size1 = *val_size;
1111 ret = RegEnumValueW(key, i, *val_name_buf, &val_name_size1, NULL,
1112 &value_type, *val_buf, &val_size1);
1113 if (ret == ERROR_MORE_DATA) {
1114 /* Increase the size of the buffers and retry */
1115 REGPROC_resize_char_buffer(val_name_buf, val_name_size, val_name_size1);
1116 REGPROC_resize_binary_buffer(val_buf, val_size, val_size1);
1117 } else if (ret != ERROR_SUCCESS) {
1118 more_data = FALSE;
1119 if (ret != ERROR_NO_MORE_ITEMS) {
1120 REGPROC_print_error();
1122 } else {
1123 DWORD line_len;
1124 i++;
1126 if ((*val_name_buf)[0]) {
1127 const WCHAR val_start[] = {'"','%','s','"','=',0};
1129 line_len = 3 + lstrlenW(*val_name_buf);
1130 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len);
1131 sprintfW(*line_buf, val_start, *val_name_buf);
1132 } else {
1133 const WCHAR std_val[] = {'@','=',0};
1134 line_len = 2;
1135 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len);
1136 lstrcpyW(*line_buf, std_val);
1139 switch (value_type) {
1140 case REG_SZ:
1142 WCHAR* wstr = (WCHAR*)*val_buf;
1144 if (val_size1 < sizeof(WCHAR) || val_size1 % sizeof(WCHAR) ||
1145 wstr[val_size1 / sizeof(WCHAR) - 1]) {
1146 REGPROC_export_binary(line_buf, line_buf_size, &line_len, value_type, *val_buf, val_size1, unicode);
1147 } else {
1148 const WCHAR start[] = {'"',0};
1149 const WCHAR end[] = {'"','\n',0};
1150 DWORD len;
1152 len = lstrlenW(start);
1153 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len + len);
1154 lstrcpyW(*line_buf + line_len, start);
1155 line_len += len;
1157 /* At this point we know wstr is '\0'-terminated
1158 * so we can substract 1 from the size
1160 REGPROC_export_string(line_buf, line_buf_size, &line_len, wstr, val_size1 / sizeof(WCHAR) - 1);
1162 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len + lstrlenW(end));
1163 lstrcpyW(*line_buf + line_len, end);
1165 break;
1168 case REG_DWORD:
1170 WCHAR format[] = {'d','w','o','r','d',':','%','0','8','x','\n',0};
1172 REGPROC_resize_char_buffer(line_buf, line_buf_size, line_len + 15);
1173 sprintfW(*line_buf + line_len, format, *((DWORD *)*val_buf));
1174 break;
1177 default:
1179 char* key_nameA = GetMultiByteString(*reg_key_name_buf);
1180 char* value_nameA = GetMultiByteString(*val_name_buf);
1181 fprintf(stderr,"%s: warning - unsupported registry format '%d', "
1182 "treat as binary\n",
1183 getAppName(), value_type);
1184 fprintf(stderr,"key name: \"%s\"\n", key_nameA);
1185 fprintf(stderr,"value name:\"%s\"\n\n", value_nameA);
1186 HeapFree(GetProcessHeap(), 0, key_nameA);
1187 HeapFree(GetProcessHeap(), 0, value_nameA);
1189 /* falls through */
1190 case REG_EXPAND_SZ:
1191 case REG_MULTI_SZ:
1192 /* falls through */
1193 case REG_BINARY:
1194 REGPROC_export_binary(line_buf, line_buf_size, &line_len, value_type, *val_buf, val_size1, unicode);
1196 REGPROC_write_line(file, *line_buf, unicode);
1200 i = 0;
1201 more_data = TRUE;
1202 (*reg_key_name_buf)[curr_len] = '\\';
1203 while(more_data) {
1204 DWORD buf_size = *reg_key_name_size - curr_len - 1;
1206 ret = RegEnumKeyExW(key, i, *reg_key_name_buf + curr_len + 1, &buf_size,
1207 NULL, NULL, NULL, NULL);
1208 if (ret == ERROR_MORE_DATA) {
1209 /* Increase the size of the buffer and retry */
1210 REGPROC_resize_char_buffer(reg_key_name_buf, reg_key_name_size, curr_len + 1 + buf_size);
1211 } else if (ret != ERROR_SUCCESS) {
1212 more_data = FALSE;
1213 if (ret != ERROR_NO_MORE_ITEMS) {
1214 REGPROC_print_error();
1216 } else {
1217 HKEY subkey;
1219 i++;
1220 if (RegOpenKeyW(key, *reg_key_name_buf + curr_len + 1,
1221 &subkey) == ERROR_SUCCESS) {
1222 export_hkey(file, subkey, reg_key_name_buf, reg_key_name_size,
1223 val_name_buf, val_name_size, val_buf, val_size,
1224 line_buf, line_buf_size, unicode);
1225 RegCloseKey(subkey);
1226 } else {
1227 REGPROC_print_error();
1231 (*reg_key_name_buf)[curr_len] = '\0';
1234 /******************************************************************************
1235 * Open file for export.
1237 static FILE *REGPROC_open_export_file(WCHAR *file_name, BOOL unicode)
1239 FILE *file;
1240 WCHAR dash = '-';
1242 if (strncmpW(file_name,&dash,1)==0)
1243 file=stdout;
1244 else
1246 CHAR* file_nameA = GetMultiByteString(file_name);
1247 file = fopen(file_nameA, "w");
1248 if (!file) {
1249 perror("");
1250 fprintf(stderr,"%s: Can't open file \"%s\"\n", getAppName(), file_nameA);
1251 HeapFree(GetProcessHeap(), 0, file_nameA);
1252 exit(1);
1254 HeapFree(GetProcessHeap(), 0, file_nameA);
1256 if(unicode)
1258 const BYTE unicode_seq[] = {0xff,0xfe};
1259 const WCHAR header[] = {'W','i','n','d','o','w','s',' ','R','e','g','i','s','t','r','y',' ','E','d','i','t','o','r',' ','V','e','r','s','i','o','n',' ','5','.','0','0','\n'};
1260 fwrite(unicode_seq, sizeof(BYTE), sizeof(unicode_seq)/sizeof(unicode_seq[0]), file);
1261 fwrite(header, sizeof(WCHAR), sizeof(header)/sizeof(header[0]), file);
1262 } else
1264 fputs("REGEDIT4\n", file);
1267 return file;
1270 /******************************************************************************
1271 * Writes contents of the registry key to the specified file stream.
1273 * Parameters:
1274 * file_name - name of a file to export registry branch to.
1275 * reg_key_name - registry branch to export. The whole registry is exported if
1276 * reg_key_name is NULL or contains an empty string.
1278 BOOL export_registry_key(WCHAR *file_name, WCHAR *reg_key_name, DWORD format)
1280 WCHAR *reg_key_name_buf;
1281 WCHAR *val_name_buf;
1282 BYTE *val_buf;
1283 WCHAR *line_buf;
1284 DWORD reg_key_name_size = KEY_MAX_LEN;
1285 DWORD val_name_size = KEY_MAX_LEN;
1286 DWORD val_size = REG_VAL_BUF_SIZE;
1287 DWORD line_buf_size = KEY_MAX_LEN + REG_VAL_BUF_SIZE;
1288 FILE *file = NULL;
1289 BOOL unicode = (format == REG_FORMAT_5);
1291 reg_key_name_buf = HeapAlloc(GetProcessHeap(), 0,
1292 reg_key_name_size * sizeof(*reg_key_name_buf));
1293 val_name_buf = HeapAlloc(GetProcessHeap(), 0,
1294 val_name_size * sizeof(*val_name_buf));
1295 val_buf = HeapAlloc(GetProcessHeap(), 0, val_size);
1296 line_buf = HeapAlloc(GetProcessHeap(), 0, line_buf_size * sizeof(*line_buf));
1297 CHECK_ENOUGH_MEMORY(reg_key_name_buf && val_name_buf && val_buf && line_buf);
1299 if (reg_key_name && reg_key_name[0]) {
1300 HKEY reg_key_class;
1301 WCHAR *branch_name = NULL;
1302 HKEY key;
1304 REGPROC_resize_char_buffer(&reg_key_name_buf, &reg_key_name_size,
1305 lstrlenW(reg_key_name));
1306 lstrcpyW(reg_key_name_buf, reg_key_name);
1308 /* open the specified key */
1309 if (!parseKeyName(reg_key_name, &reg_key_class, &branch_name)) {
1310 CHAR* key_nameA = GetMultiByteString(reg_key_name);
1311 fprintf(stderr,"%s: Incorrect registry class specification in '%s'\n",
1312 getAppName(), key_nameA);
1313 HeapFree(GetProcessHeap(), 0, key_nameA);
1314 exit(1);
1316 if (!branch_name[0]) {
1317 /* no branch - registry class is specified */
1318 file = REGPROC_open_export_file(file_name, unicode);
1319 export_hkey(file, reg_key_class,
1320 &reg_key_name_buf, &reg_key_name_size,
1321 &val_name_buf, &val_name_size,
1322 &val_buf, &val_size, &line_buf,
1323 &line_buf_size, unicode);
1324 } else if (RegOpenKeyW(reg_key_class, branch_name, &key) == ERROR_SUCCESS) {
1325 file = REGPROC_open_export_file(file_name, unicode);
1326 export_hkey(file, key,
1327 &reg_key_name_buf, &reg_key_name_size,
1328 &val_name_buf, &val_name_size,
1329 &val_buf, &val_size, &line_buf,
1330 &line_buf_size, unicode);
1331 RegCloseKey(key);
1332 } else {
1333 CHAR* key_nameA = GetMultiByteString(reg_key_name);
1334 fprintf(stderr,"%s: Can't export. Registry key '%s' does not exist!\n",
1335 getAppName(), key_nameA);
1336 HeapFree(GetProcessHeap(), 0, key_nameA);
1337 REGPROC_print_error();
1339 } else {
1340 unsigned int i;
1342 /* export all registry classes */
1343 file = REGPROC_open_export_file(file_name, unicode);
1344 for (i = 0; i < REG_CLASS_NUMBER; i++) {
1345 /* do not export HKEY_CLASSES_ROOT */
1346 if (reg_class_keys[i] != HKEY_CLASSES_ROOT &&
1347 reg_class_keys[i] != HKEY_CURRENT_USER &&
1348 reg_class_keys[i] != HKEY_CURRENT_CONFIG &&
1349 reg_class_keys[i] != HKEY_DYN_DATA) {
1350 lstrcpyW(reg_key_name_buf, reg_class_namesW[i]);
1351 export_hkey(file, reg_class_keys[i],
1352 &reg_key_name_buf, &reg_key_name_size,
1353 &val_name_buf, &val_name_size,
1354 &val_buf, &val_size, &line_buf,
1355 &line_buf_size, unicode);
1360 if (file) {
1361 fclose(file);
1363 HeapFree(GetProcessHeap(), 0, reg_key_name);
1364 HeapFree(GetProcessHeap(), 0, val_name_buf);
1365 HeapFree(GetProcessHeap(), 0, val_buf);
1366 HeapFree(GetProcessHeap(), 0, line_buf);
1367 return TRUE;
1370 /******************************************************************************
1371 * Reads contents of the specified file into the registry.
1373 BOOL import_registry_file(FILE* reg_file)
1375 if (reg_file)
1377 BYTE s[2];
1378 if (fread( s, 2, 1, reg_file) == 1)
1380 if (s[0] == 0xff && s[1] == 0xfe)
1382 processRegLinesW(reg_file);
1383 } else
1385 processRegLinesA(reg_file, (char*)s);
1388 return TRUE;
1390 return FALSE;
1393 /******************************************************************************
1394 * Removes the registry key with all subkeys. Parses full key name.
1396 * Parameters:
1397 * reg_key_name - full name of registry branch to delete. Ignored if is NULL,
1398 * empty, points to register key class, does not exist.
1400 void delete_registry_key(WCHAR *reg_key_name)
1402 WCHAR *key_name = NULL;
1403 HKEY key_class;
1405 if (!reg_key_name || !reg_key_name[0])
1406 return;
1408 if (!parseKeyName(reg_key_name, &key_class, &key_name)) {
1409 char* reg_key_nameA = GetMultiByteString(reg_key_name);
1410 fprintf(stderr,"%s: Incorrect registry class specification in '%s'\n",
1411 getAppName(), reg_key_nameA);
1412 HeapFree(GetProcessHeap(), 0, reg_key_nameA);
1413 exit(1);
1415 if (!*key_name) {
1416 char* reg_key_nameA = GetMultiByteString(reg_key_name);
1417 fprintf(stderr,"%s: Can't delete registry class '%s'\n",
1418 getAppName(), reg_key_nameA);
1419 HeapFree(GetProcessHeap(), 0, reg_key_nameA);
1420 exit(1);
1423 RegDeleteTreeW(key_class, key_name);