aad53e8e7c4b96cac7652675966740bf6c2ac284
[ira/wip.git] / source3 / lib / util_reg_api.c
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 2 of the License, or (at your option)
9  * any later version.
10  * 
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.
15  * 
16  * You should have received a copy of the GNU General Public License along with
17  * this program; if not, write to the Free Software Foundation, Inc., 675
18  * Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include "includes.h"
22
23 WERROR registry_pull_value(TALLOC_CTX *mem_ctx,
24                            struct registry_value **pvalue,
25                            enum winreg_Type type, uint8 *data,
26                            uint32 size, uint32 length)
27 {
28         struct registry_value *value;
29         WERROR err;
30
31         if (!(value = TALLOC_ZERO_P(mem_ctx, struct registry_value))) {
32                 return WERR_NOMEM;
33         }
34
35         value->type = type;
36
37         switch (type) {
38         case REG_DWORD:
39                 if ((size != 4) || (length != 4)) {
40                         err = WERR_INVALID_PARAM;
41                         goto error;
42                 }
43                 value->v.dword = IVAL(data, 0);
44                 break;
45         case REG_SZ:
46         case REG_EXPAND_SZ:
47         {
48                 /*
49                  * Make sure we get a NULL terminated string for
50                  * convert_string_talloc().
51                  */
52
53                 smb_ucs2_t *tmp;
54                 uint32 num_ucs2 = length / 2;
55
56                 if ((length % 2) != 0) {
57                         err = WERR_INVALID_PARAM;
58                         goto error;
59                 }
60
61                 if (!(tmp = SMB_MALLOC_ARRAY(smb_ucs2_t, num_ucs2+1))) {
62                         err = WERR_NOMEM;
63                         goto error;
64                 }
65
66                 memcpy((void *)tmp, (const void *)data, length);
67                 tmp[num_ucs2] = 0;
68
69                 value->v.sz.len = convert_string_talloc(
70                         value, CH_UTF16LE, CH_UNIX, tmp, length+2,
71                         &value->v.sz.str, False);
72
73                 SAFE_FREE(tmp);
74
75                 if (value->v.sz.len == (size_t)-1) {
76                         err = WERR_INVALID_PARAM;
77                         goto error;
78                 }
79                 break;
80         }
81         case REG_MULTI_SZ:
82                 err = reg_pull_multi_sz(value, (void *)data, length,
83                                         &value->v.multi_sz.num_strings,
84                                         &value->v.multi_sz.strings);
85                 if (!(W_ERROR_IS_OK(err))) {
86                         goto error;
87                 }
88                 break;
89         case REG_BINARY:
90                 value->v.binary.data = talloc_move(value, &data);
91                 value->v.binary.length = length;
92                 break;
93         default:
94                 err = WERR_INVALID_PARAM;
95                 goto error;
96         }
97
98         *pvalue = value;
99         return WERR_OK;
100
101  error:
102         TALLOC_FREE(value);
103         return err;
104 }
105
106 WERROR registry_push_value(TALLOC_CTX *mem_ctx,
107                            const struct registry_value *value,
108                            DATA_BLOB *presult)
109 {
110         switch (value->type) {
111         case REG_DWORD: {
112                 char buf[4];
113                 SIVAL(buf, 0, value->v.dword);
114                 *presult = data_blob_talloc(mem_ctx, (void *)buf, 4);
115                 if (presult->data == NULL) {
116                         return WERR_NOMEM;
117                 }
118                 break;
119         }
120         case REG_SZ:
121         case REG_EXPAND_SZ: {
122                 presult->length = convert_string_talloc(
123                         mem_ctx, CH_UNIX, CH_UTF16LE, value->v.sz.str,
124                         MIN(value->v.sz.len, strlen(value->v.sz.str)+1),
125                         (void *)&(presult->data), False);
126                 if (presult->length == (size_t)-1) {
127                         return WERR_NOMEM;
128                 }
129                 break;
130         }
131         default:
132                 return WERR_INVALID_PARAM;
133         }
134
135         return WERR_OK;
136 }