librpc: rerun make idl
[Samba/gbeck.git] / source3 / lib / util_reg_api.c
blob8f28e9c282f2cb52e680bc5a8aa01aa5564c0bd5
1 /*
2 * Unix SMB/CIFS implementation.
3 * Registry helper routines
4 * Copyright (C) Volker Lendecke 2006
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 3 of the License, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_REGISTRY
25 WERROR registry_pull_value(TALLOC_CTX *mem_ctx,
26 struct registry_value **pvalue,
27 enum winreg_Type type, uint8 *data,
28 uint32 size, uint32 length)
30 struct registry_value *value;
31 WERROR err;
33 if (!(value = TALLOC_ZERO_P(mem_ctx, struct registry_value))) {
34 return WERR_NOMEM;
37 value->type = type;
39 switch (type) {
40 case REG_DWORD:
41 if ((size != 4) || (length != 4)) {
42 err = WERR_INVALID_PARAM;
43 goto error;
45 value->v.dword = IVAL(data, 0);
46 break;
47 case REG_SZ:
48 case REG_EXPAND_SZ:
51 * Make sure we get a NULL terminated string for
52 * convert_string_talloc().
55 smb_ucs2_t *tmp;
57 if (length == 1) {
58 /* win2k regedit gives us a string of 1 byte when
59 * creating a new value of type REG_SZ. this workaround
60 * replaces the input by using the same string as
61 * winxp delivers. */
62 length = 2;
63 if (!(tmp = SMB_MALLOC_ARRAY(smb_ucs2_t, 2))) {
64 err = WERR_NOMEM;
65 goto error;
67 tmp[0] = 0;
68 tmp[1] = 0;
69 DEBUG(10, ("got REG_SZ value of length 1 - workaround "
70 "activated.\n"));
72 else if ((length % 2) != 0) {
73 err = WERR_INVALID_PARAM;
74 goto error;
76 else {
77 uint32 num_ucs2 = length / 2;
78 if (!(tmp = SMB_MALLOC_ARRAY(smb_ucs2_t, num_ucs2+1))) {
79 err = WERR_NOMEM;
80 goto error;
83 memcpy((void *)tmp, (const void *)data, length);
84 tmp[num_ucs2] = 0;
87 if (length + 2 < length) {
88 /* Integer wrap. */
89 SAFE_FREE(tmp);
90 err = WERR_INVALID_PARAM;
91 goto error;
94 if (!convert_string_talloc(value, CH_UTF16LE, CH_UNIX, tmp,
95 length+2, &value->v.sz.str,
96 &value->v.sz.len, False)) {
97 SAFE_FREE(tmp);
98 err = WERR_INVALID_PARAM;
99 goto error;
102 SAFE_FREE(tmp);
103 break;
105 case REG_MULTI_SZ:
106 err = reg_pull_multi_sz(value, (void *)data, length,
107 &value->v.multi_sz.num_strings,
108 &value->v.multi_sz.strings);
109 if (!(W_ERROR_IS_OK(err))) {
110 goto error;
112 break;
113 case REG_BINARY:
114 value->v.binary = data_blob_talloc(mem_ctx, data, length);
115 break;
116 default:
117 err = WERR_INVALID_PARAM;
118 goto error;
121 *pvalue = value;
122 return WERR_OK;
124 error:
125 TALLOC_FREE(value);
126 return err;
129 WERROR registry_push_value(TALLOC_CTX *mem_ctx,
130 const struct registry_value *value,
131 DATA_BLOB *presult)
133 switch (value->type) {
134 case REG_DWORD: {
135 char buf[4];
136 SIVAL(buf, 0, value->v.dword);
137 *presult = data_blob_talloc(mem_ctx, (void *)buf, 4);
138 if (presult->data == NULL) {
139 return WERR_NOMEM;
141 break;
143 case REG_SZ:
144 case REG_EXPAND_SZ: {
145 if (!convert_string_talloc(mem_ctx, CH_UNIX, CH_UTF16LE,
146 value->v.sz.str,
147 MIN(value->v.sz.len,
148 strlen(value->v.sz.str)+1),
149 (void *)&(presult->data),
150 &presult->length, False))
152 return WERR_NOMEM;
154 break;
156 case REG_MULTI_SZ: {
157 uint32_t count;
158 size_t len = 0;
159 char **strings;
160 size_t *string_lengths;
161 uint32_t ofs;
162 TALLOC_CTX *tmp_ctx = talloc_stackframe();
164 strings = TALLOC_ARRAY(tmp_ctx, char *,
165 value->v.multi_sz.num_strings);
166 if (strings == NULL) {
167 return WERR_NOMEM;
170 string_lengths = TALLOC_ARRAY(tmp_ctx, size_t,
171 value->v.multi_sz.num_strings);
172 if (string_lengths == NULL) {
173 TALLOC_FREE(tmp_ctx);
174 return WERR_NOMEM;
177 /* convert the single strings */
178 for (count = 0; count < value->v.multi_sz.num_strings; count++)
180 if (!convert_string_talloc(strings, CH_UNIX,
181 CH_UTF16LE, value->v.multi_sz.strings[count],
182 strlen(value->v.multi_sz.strings[count])+1,
183 (void *)&strings[count],
184 &string_lengths[count], false))
187 TALLOC_FREE(tmp_ctx);
188 return WERR_NOMEM;
190 len += string_lengths[count];
193 /* now concatenate all into the data blob */
194 presult->data = TALLOC_ARRAY(mem_ctx, uint8_t, len);
195 if (presult->data == NULL) {
196 TALLOC_FREE(tmp_ctx);
197 return WERR_NOMEM;
199 for (count = 0, ofs = 0;
200 count < value->v.multi_sz.num_strings;
201 count++)
203 memcpy(presult->data + ofs, strings[count],
204 string_lengths[count]);
205 ofs += string_lengths[count];
207 presult->length = len;
209 TALLOC_FREE(tmp_ctx);
211 break;
213 case REG_BINARY:
214 *presult = data_blob_talloc(mem_ctx,
215 value->v.binary.data,
216 value->v.binary.length);
217 break;
218 default:
219 return WERR_INVALID_PARAM;
222 return WERR_OK;