s3-util: use pull_reg_multi_sz in reg_pull_multi_sz.
[obnox/samba-ctdb.git] / source3 / lib / util_reg.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 3 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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "../librpc/gen_ndr/ndr_winreg.h"
22
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_REGISTRY
25
26 extern struct registry_ops smbconf_reg_ops;
27
28 const char *reg_type_lookup(enum winreg_Type type)
29 {
30         const char *result;
31
32         switch(type) {
33         case REG_NONE:
34                 result = "REG_NONE";
35                 break;
36         case REG_SZ:
37                 result = "REG_SZ";
38                 break;
39         case REG_EXPAND_SZ:
40                 result = "REG_EXPAND_SZ";
41                 break;
42         case REG_BINARY:
43                 result = "REG_BINARY";
44                 break;
45         case REG_DWORD:
46                 result = "REG_DWORD";
47                 break;
48         case REG_DWORD_BIG_ENDIAN:
49                 result = "REG_DWORD_BIG_ENDIAN";
50                 break;
51         case REG_LINK:
52                 result = "REG_LINK";
53                 break;
54         case REG_MULTI_SZ:
55                 result = "REG_MULTI_SZ";
56                 break;
57         case REG_RESOURCE_LIST:
58                 result = "REG_RESOURCE_LIST";
59                 break;
60         case REG_FULL_RESOURCE_DESCRIPTOR:
61                 result = "REG_FULL_RESOURCE_DESCRIPTOR";
62                 break;
63         case REG_RESOURCE_REQUIREMENTS_LIST:
64                 result = "REG_RESOURCE_REQUIREMENTS_LIST";
65                 break;
66         case REG_QWORD:
67                 result = "REG_QWORD";
68                 break;
69         default:
70                 result = "REG TYPE IS UNKNOWN";
71                 break;
72         }
73         return result;
74 }
75
76 WERROR reg_pull_multi_sz(TALLOC_CTX *mem_ctx, const void *buf, size_t len,
77                          uint32 *num_values, char ***values)
78 {
79         DATA_BLOB blob;
80         const char **vals;
81         int i;
82
83         blob = data_blob_const((uint8_t *)buf, len);
84
85         if (!pull_reg_multi_sz(mem_ctx, &blob, &vals)) {
86                 return WERR_NOMEM;
87         }
88
89         for (i=0; vals[i]; i++) {
90                 ;;
91         }
92
93         *num_values = i;
94         *values = (char **)vals;
95
96         return WERR_OK;
97 }
98
99 /*******************************************************************
100  push a string in unix charset into a REG_SZ UCS2 null terminated blob
101  ********************************************************************/
102
103 bool push_reg_sz(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, const char *s)
104 {
105         union winreg_Data data;
106         enum ndr_err_code ndr_err;
107         data.string = s;
108         ndr_err = ndr_push_union_blob(blob, mem_ctx, NULL, &data, REG_SZ,
109                         (ndr_push_flags_fn_t)ndr_push_winreg_Data);
110         return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
111 }
112
113 /*******************************************************************
114  push a string_array in unix charset into a REG_MULTI_SZ UCS2 double-null
115  terminated blob
116  ********************************************************************/
117
118 bool push_reg_multi_sz(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, const char **a)
119 {
120         union winreg_Data data;
121         enum ndr_err_code ndr_err;
122         data.string_array = a;
123         ndr_err = ndr_push_union_blob(blob, mem_ctx, NULL, &data, REG_MULTI_SZ,
124                         (ndr_push_flags_fn_t)ndr_push_winreg_Data);
125         return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
126 }
127
128 /*******************************************************************
129  pull a string in unix charset out of a REG_SZ UCS2 null terminated blob
130  ********************************************************************/
131
132 bool pull_reg_sz(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, const char **s)
133 {
134         union winreg_Data data;
135         enum ndr_err_code ndr_err;
136         ndr_err = ndr_pull_union_blob(blob, mem_ctx, NULL, &data, REG_SZ,
137                         (ndr_pull_flags_fn_t)ndr_pull_winreg_Data);
138         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
139                 return false;
140         }
141         *s = data.string;
142         return true;
143 }
144
145 /*******************************************************************
146  pull a string_array in unix charset out of a REG_MULTI_SZ UCS2 double-null
147  terminated blob
148  ********************************************************************/
149
150 bool pull_reg_multi_sz(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, const char ***a)
151 {
152         union winreg_Data data;
153         enum ndr_err_code ndr_err;
154         ndr_err = ndr_pull_union_blob(blob, mem_ctx, NULL, &data, REG_MULTI_SZ,
155                         (ndr_pull_flags_fn_t)ndr_pull_winreg_Data);
156         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
157                 return false;
158         }
159         *a = data.string_array;
160         return true;
161 }