server: Split the fallback to desktop async keystate.
[wine/zf.git] / programs / reg / add.c
blobf259f0a41003c309b71f1f7e9e0ce419e3af43f6
1 /*
2 * Copyright 2016-2017, 2021 Hugh McMaster
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <errno.h>
20 #include "reg.h"
22 static DWORD wchar_get_type(const WCHAR *type_name)
24 DWORD i;
26 if (!type_name)
27 return REG_SZ;
29 for (i = 0; i < ARRAY_SIZE(type_rels); i++)
31 if (!wcsicmp(type_rels[i].name, type_name))
32 return type_rels[i].type;
35 return ~0u;
38 /* hexchar_to_byte from programs/regedit/hexedit.c */
39 static inline BYTE hexchar_to_byte(WCHAR ch)
41 if (ch >= '0' && ch <= '9')
42 return ch - '0';
43 else if (ch >= 'a' && ch <= 'f')
44 return ch - 'a' + 10;
45 else if (ch >= 'A' && ch <= 'F')
46 return ch - 'A' + 10;
47 else
48 return -1;
51 static LPBYTE get_regdata(const WCHAR *data, DWORD reg_type, WCHAR separator, DWORD *reg_count)
53 static const WCHAR empty;
54 LPBYTE out_data = NULL;
55 *reg_count = 0;
57 if (!data) data = &empty;
59 switch (reg_type)
61 case REG_NONE:
62 case REG_SZ:
63 case REG_EXPAND_SZ:
65 *reg_count = (lstrlenW(data) + 1) * sizeof(WCHAR);
66 out_data = malloc(*reg_count);
67 lstrcpyW((LPWSTR)out_data,data);
68 break;
70 case REG_DWORD:
71 /* case REG_DWORD_LITTLE_ENDIAN: */
72 case REG_DWORD_BIG_ENDIAN: /* Yes, this is correct! */
74 LPWSTR rest;
75 unsigned long val;
76 val = wcstoul(data, &rest, (towlower(data[1]) == 'x') ? 16 : 10);
77 if (*rest || data[0] == '-' || (val == ~0u && errno == ERANGE)) {
78 output_message(STRING_MISSING_INTEGER);
79 break;
81 *reg_count = sizeof(DWORD);
82 out_data = malloc(*reg_count);
83 ((LPDWORD)out_data)[0] = val;
84 break;
86 case REG_BINARY:
88 BYTE hex0, hex1;
89 int i = 0, destByteIndex = 0, datalen = lstrlenW(data);
90 *reg_count = ((datalen + datalen % 2) / 2) * sizeof(BYTE);
91 out_data = malloc(*reg_count);
92 if(datalen % 2)
94 hex1 = hexchar_to_byte(data[i++]);
95 if(hex1 == 0xFF)
96 goto no_hex_data;
97 out_data[destByteIndex++] = hex1;
99 for(;i + 1 < datalen;i += 2)
101 hex0 = hexchar_to_byte(data[i]);
102 hex1 = hexchar_to_byte(data[i + 1]);
103 if(hex0 == 0xFF || hex1 == 0xFF)
104 goto no_hex_data;
105 out_data[destByteIndex++] = (hex0 << 4) | hex1;
107 break;
108 no_hex_data:
109 /* cleanup, print error */
110 free(out_data);
111 output_message(STRING_MISSING_HEXDATA);
112 out_data = NULL;
113 break;
115 case REG_MULTI_SZ:
117 int i, destindex, len = lstrlenW(data);
118 WCHAR *buffer = malloc((len + 2) * sizeof(WCHAR));
120 for (i = 0, destindex = 0; i < len; i++, destindex++)
122 if (!separator && data[i] == '\\' && data[i + 1] == '0')
124 buffer[destindex] = 0;
125 i++;
127 else if (data[i] == separator)
128 buffer[destindex] = 0;
129 else
130 buffer[destindex] = data[i];
132 if (destindex && !buffer[destindex - 1] && (!buffer[destindex] || destindex == 1))
134 free(buffer);
135 output_message(STRING_INVALID_STRING);
136 return NULL;
139 buffer[destindex] = 0;
140 if (destindex && buffer[destindex - 1])
141 buffer[++destindex] = 0;
142 *reg_count = (destindex + 1) * sizeof(WCHAR);
143 return (BYTE *)buffer;
145 default:
146 output_message(STRING_UNHANDLED_TYPE, reg_type, data);
149 return out_data;
152 static int run_add(HKEY root, WCHAR *path, WCHAR *value_name, BOOL value_empty,
153 WCHAR *type, WCHAR separator, WCHAR *data, BOOL force)
155 HKEY key;
157 if (RegCreateKeyExW(root, path, 0, NULL, REG_OPTION_NON_VOLATILE,
158 KEY_READ|KEY_WRITE, NULL, &key, NULL))
160 output_message(STRING_INVALID_KEY);
161 return 1;
164 if (value_name || value_empty || data)
166 DWORD reg_type;
167 DWORD reg_count = 0;
168 BYTE* reg_data = NULL;
170 if (!force)
172 if (RegQueryValueExW(key, value_name, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
174 if (!ask_confirm(STRING_OVERWRITE_VALUE, value_name))
176 RegCloseKey(key);
177 output_message(STRING_CANCELLED);
178 return 0;
183 reg_type = wchar_get_type(type);
184 if (reg_type == ~0u)
186 RegCloseKey(key);
187 output_message(STRING_UNSUPPORTED_TYPE, type);
188 return 1;
190 if ((reg_type == REG_DWORD || reg_type == REG_DWORD_BIG_ENDIAN) && !data)
192 RegCloseKey(key);
193 output_message(STRING_INVALID_CMDLINE);
194 return 1;
197 if (!(reg_data = get_regdata(data, reg_type, separator, &reg_count)))
199 RegCloseKey(key);
200 return 1;
203 RegSetValueExW(key, value_name, 0, reg_type, reg_data, reg_count);
204 free(reg_data);
207 RegCloseKey(key);
208 output_message(STRING_SUCCESS);
210 return 0;
213 int reg_add(int argc, WCHAR *argvW[])
215 HKEY root;
216 WCHAR *path, *key_name, *value_name = NULL, *type = NULL, *data = NULL, separator = '\0';
217 BOOL value_empty = FALSE, force = FALSE;
218 int i;
220 if (!parse_registry_key(argvW[2], &root, &path, &key_name))
221 return 1;
223 for (i = 3; i < argc; i++)
225 WCHAR *str;
227 if (argvW[i][0] != '/' && argvW[i][0] != '-')
228 goto invalid;
230 str = &argvW[i][1];
232 if (!lstrcmpiW(str, L"ve"))
234 if (value_empty) goto invalid;
235 value_empty = TRUE;
236 continue;
238 else if (!str[0] || str[1])
239 goto invalid;
241 switch (towlower(*str))
243 case 'v':
244 if (value_name || !(value_name = argvW[++i]))
245 goto invalid;
246 break;
247 case 't':
248 if (type || !(type = argvW[++i]))
249 goto invalid;
250 break;
251 case 'd':
252 if (data || !(data = argvW[++i]))
253 goto invalid;
254 break;
255 case 's':
256 str = argvW[++i];
257 if (separator || !str || lstrlenW(str) != 1)
258 goto invalid;
259 separator = str[0];
260 break;
261 case 'f':
262 if (force) goto invalid;
263 force = TRUE;
264 break;
265 default:
266 goto invalid;
270 if (value_name && value_empty)
271 goto invalid;
273 return run_add(root, path, value_name, value_empty, type, separator, data, force);
275 invalid:
276 output_message(STRING_INVALID_CMDLINE);
277 return 1;