r25055: Add file_id_string_tos
[Samba.git] / source / lib / util_reg_api.c
blob243499cb9915ddf835bdc2e641d2f9e7827c4f69
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 WERROR registry_pull_value(TALLOC_CTX *mem_ctx,
23 struct registry_value **pvalue,
24 enum winreg_Type type, uint8 *data,
25 uint32 size, uint32 length)
27 struct registry_value *value;
28 WERROR err;
30 if (!(value = TALLOC_ZERO_P(mem_ctx, struct registry_value))) {
31 return WERR_NOMEM;
34 value->type = type;
36 switch (type) {
37 case REG_DWORD:
38 if ((size != 4) || (length != 4)) {
39 err = WERR_INVALID_PARAM;
40 goto error;
42 value->v.dword = IVAL(data, 0);
43 break;
44 case REG_SZ:
45 case REG_EXPAND_SZ:
48 * Make sure we get a NULL terminated string for
49 * convert_string_talloc().
52 smb_ucs2_t *tmp;
54 if (length == 1) {
55 /* win2k regedit gives us a string of 1 byte when
56 * creating a new value of type REG_SZ. this workaround
57 * replaces the input by using the same string as
58 * winxp delivers. */
59 length = 2;
60 if (!(tmp = SMB_MALLOC_ARRAY(smb_ucs2_t, 2))) {
61 err = WERR_NOMEM;
62 goto error;
64 tmp[0] = 0;
65 tmp[1] = 0;
66 DEBUG(10, ("got REG_SZ value of length 1 - workaround "
67 "activated.\n"));
69 else if ((length % 2) != 0) {
70 err = WERR_INVALID_PARAM;
71 goto error;
73 else {
74 uint32 num_ucs2 = length / 2;
75 if (!(tmp = SMB_MALLOC_ARRAY(smb_ucs2_t, num_ucs2+1))) {
76 err = WERR_NOMEM;
77 goto error;
80 memcpy((void *)tmp, (const void *)data, length);
81 tmp[num_ucs2] = 0;
84 if (length + 2 < length) {
85 /* Integer wrap. */
86 SAFE_FREE(tmp);
87 err = WERR_INVALID_PARAM;
88 goto error;
91 value->v.sz.len = convert_string_talloc(
92 value, CH_UTF16LE, CH_UNIX, tmp, length+2,
93 &value->v.sz.str, False);
95 SAFE_FREE(tmp);
97 if (value->v.sz.len == (size_t)-1) {
98 err = WERR_INVALID_PARAM;
99 goto error;
101 break;
103 case REG_MULTI_SZ:
104 err = reg_pull_multi_sz(value, (void *)data, length,
105 &value->v.multi_sz.num_strings,
106 &value->v.multi_sz.strings);
107 if (!(W_ERROR_IS_OK(err))) {
108 goto error;
110 break;
111 case REG_BINARY:
112 value->v.binary = data_blob_talloc(mem_ctx, data, length);
113 break;
114 default:
115 err = WERR_INVALID_PARAM;
116 goto error;
119 *pvalue = value;
120 return WERR_OK;
122 error:
123 TALLOC_FREE(value);
124 return err;
127 WERROR registry_push_value(TALLOC_CTX *mem_ctx,
128 const struct registry_value *value,
129 DATA_BLOB *presult)
131 switch (value->type) {
132 case REG_DWORD: {
133 char buf[4];
134 SIVAL(buf, 0, value->v.dword);
135 *presult = data_blob_talloc(mem_ctx, (void *)buf, 4);
136 if (presult->data == NULL) {
137 return WERR_NOMEM;
139 break;
141 case REG_SZ:
142 case REG_EXPAND_SZ: {
143 presult->length = convert_string_talloc(
144 mem_ctx, CH_UNIX, CH_UTF16LE, value->v.sz.str,
145 MIN(value->v.sz.len, strlen(value->v.sz.str)+1),
146 (void *)&(presult->data), False);
147 if (presult->length == (size_t)-1) {
148 return WERR_NOMEM;
150 break;
152 case REG_BINARY:
153 *presult = data_blob_talloc(mem_ctx,
154 value->v.binary.data,
155 value->v.binary.length);
156 break;
157 default:
158 return WERR_INVALID_PARAM;
161 return WERR_OK;