6ab2be0a1148de7cf8e0f226fd3459d4c073c70b
[bbaumbach/samba-autobuild/.git] / libcli / registry / util_reg.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Registry helper routines
4  * Copyright (C) Volker Lendecke 2006
5  * Copyright (C) Guenther Deschner 2009
6  * Copyright (C) Jelmer Vernooij 2003-2007
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the Free
10  * Software Foundation; either version 3 of the License, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "includes.h"
23 #include "../librpc/gen_ndr/ndr_misc.h"
24
25 /**
26  * @file
27  * @brief Registry utility functions
28  */
29
30 static const struct {
31         uint32_t id;
32         const char *name;
33 } reg_value_types[] = {
34         { REG_NONE, "REG_NONE" },
35         { REG_SZ, "REG_SZ" },
36         { REG_EXPAND_SZ, "REG_EXPAND_SZ" },
37         { REG_BINARY, "REG_BINARY" },
38         { REG_DWORD, "REG_DWORD" },
39         { REG_DWORD_BIG_ENDIAN, "REG_DWORD_BIG_ENDIAN" },
40         { REG_LINK, "REG_LINK" },
41         { REG_MULTI_SZ, "REG_MULTI_SZ" },
42         { REG_RESOURCE_LIST, "REG_RESOURCE_LIST" },
43         { REG_FULL_RESOURCE_DESCRIPTOR, "REG_FULL_RESOURCE_DESCRIPTOR" },
44         { REG_RESOURCE_REQUIREMENTS_LIST, "REG_RESOURCE_REQUIREMENTS_LIST" },
45         { REG_QWORD, "REG_QWORD" },
46
47         { 0, NULL }
48 };
49
50 /** Return string description of registry value type */
51 _PUBLIC_ const char *str_regtype(int type)
52 {
53         unsigned int i;
54         for (i = 0; reg_value_types[i].name; i++) {
55                 if (reg_value_types[i].id == type)
56                         return reg_value_types[i].name;
57         }
58
59         return "Unknown";
60 }
61
62 /** Return registry value type for string description */
63 _PUBLIC_ int regtype_by_string(const char *str)
64 {
65         unsigned int i;
66         for (i = 0; reg_value_types[i].name; i++) {
67                 if (strequal(reg_value_types[i].name, str))
68                         return reg_value_types[i].id;
69         }
70
71         return -1;
72 }
73
74 /*******************************************************************
75  push a string in unix charset into a REG_SZ UCS2 null terminated blob
76  ********************************************************************/
77
78 bool push_reg_sz(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *ic,
79                  DATA_BLOB *blob, const char *s)
80 {
81         union winreg_Data data;
82         enum ndr_err_code ndr_err;
83         data.string = s;
84         ndr_err = ndr_push_union_blob(blob, mem_ctx, ic, &data, REG_SZ,
85                         (ndr_push_flags_fn_t)ndr_push_winreg_Data);
86         return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
87 }
88
89 /*******************************************************************
90  push a string_array in unix charset into a REG_MULTI_SZ UCS2 double-null
91  terminated blob
92  ********************************************************************/
93
94 bool push_reg_multi_sz(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *ic,
95                        DATA_BLOB *blob, const char **a)
96 {
97         union winreg_Data data;
98         enum ndr_err_code ndr_err;
99         data.string_array = a;
100         ndr_err = ndr_push_union_blob(blob, mem_ctx, ic, &data, REG_MULTI_SZ,
101                         (ndr_push_flags_fn_t)ndr_push_winreg_Data);
102         return NDR_ERR_CODE_IS_SUCCESS(ndr_err);
103 }
104
105 /*******************************************************************
106  pull a string in unix charset out of a REG_SZ UCS2 null terminated blob
107  ********************************************************************/
108
109 bool pull_reg_sz(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *ic,
110                  const DATA_BLOB *blob, const char **s)
111 {
112         union winreg_Data data;
113         enum ndr_err_code ndr_err;
114         ndr_err = ndr_pull_union_blob(blob, mem_ctx, ic, &data, REG_SZ,
115                         (ndr_pull_flags_fn_t)ndr_pull_winreg_Data);
116         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
117                 return false;
118         }
119         *s = data.string;
120         return true;
121 }
122
123 /*******************************************************************
124  pull a string_array in unix charset out of a REG_MULTI_SZ UCS2 double-null
125  terminated blob
126  ********************************************************************/
127
128 bool pull_reg_multi_sz(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *ic,
129                        const DATA_BLOB *blob, const char ***a)
130 {
131         union winreg_Data data;
132         enum ndr_err_code ndr_err;
133         ndr_err = ndr_pull_union_blob(blob, mem_ctx, ic, &data, REG_MULTI_SZ,
134                         (ndr_pull_flags_fn_t)ndr_pull_winreg_Data);
135         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
136                 return false;
137         }
138         *a = data.string_array;
139         return true;
140 }