s3-net-vampire: add support for partial replication (individual deltas).
[Samba.git] / source / registry / reg_util.c
blob714a39f307a2adee3534598d5046c8d4f9021027
1 /*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer (utility functions)
4 * Copyright (C) Gerald Carter 2002-2005
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 /* Implementation of registry frontend view functions. */
22 #include "includes.h"
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_REGISTRY
27 /***********************************************************************
28 Utility function for splitting the base path of a registry path off
29 by setting base and new_path to the apprapriate offsets withing the
30 path.
32 WARNING!! Does modify the original string!
33 ***********************************************************************/
35 bool reg_split_path(char *path, char **base, char **new_path)
37 char *p;
39 *new_path = *base = NULL;
41 if (!path) {
42 return false;
44 *base = path;
46 p = strchr(path, '\\');
48 if ( p ) {
49 *p = '\0';
50 *new_path = p+1;
53 return true;
56 /***********************************************************************
57 Utility function for splitting the base path of a registry path off
58 by setting base and new_path to the appropriate offsets withing the
59 path.
61 WARNING!! Does modify the original string!
62 ***********************************************************************/
64 bool reg_split_key(char *path, char **base, char **key)
66 char *p;
68 *key = *base = NULL;
70 if (!path) {
71 return false;
74 *base = path;
76 p = strrchr(path, '\\');
78 if (p) {
79 *p = '\0';
80 *key = p+1;
83 return true;
86 /**
87 * The full path to the registry key is used as database key
88 * after the \'s are converted to /'s.
89 * Leading and trailing '/' and '\' characters are stripped.
90 * Key string is also normalized to UPPER case.
93 char *normalize_reg_path(TALLOC_CTX *ctx, const char *keyname )
95 char *p;
96 char *nkeyname;
98 /* skip leading '/' and '\' chars */
99 p = (char *)keyname;
100 while ((*p == '/') || (*p == '\\')) {
101 p++;
104 nkeyname = talloc_string_sub(ctx, p, "\\", "/");
105 if (nkeyname == NULL) {
106 return NULL;
109 /* strip trailing '/' chars */
110 p = strrchr(nkeyname, '/');
111 while ((p != NULL) && (p[1] == '\0')) {
112 *p = '\0';
113 p = strrchr(nkeyname, '/');
116 strupper_m(nkeyname);
118 return nkeyname;
122 * normalize ther registry path in place.
124 void normalize_dbkey(char *key)
126 size_t len = strlen(key);
127 string_sub(key, "\\", "/", len+1);
128 strupper_m(key);
131 /**********************************************************************
132 move to next non-delimter character
133 *********************************************************************/
135 char *reg_remaining_path(TALLOC_CTX *ctx, const char *key)
137 char *new_path = NULL;
138 char *p = NULL;
140 if (!key || !*key) {
141 return NULL;
144 new_path = talloc_strdup(ctx, key);
145 if (!new_path) {
146 return NULL;
148 /* normalize_reg_path( new_path ); */
149 if (!(p = strchr(new_path, '\\')) ) {
150 if (!(p = strchr( new_path, '/'))) {
151 p = new_path;
152 } else {
153 p++;
155 } else {
156 p++;
159 return p;
162 /**********************************************************************
163 *********************************************************************/
165 int regval_convert_multi_sz( uint16 *multi_string, size_t byte_len, char ***values )
167 char **sz;
168 int i;
169 int num_strings = 0;
170 fstring buffer;
171 uint16 *wp;
172 size_t multi_len = byte_len / 2;
174 if ( !multi_string || !values )
175 return 0;
177 *values = NULL;
179 /* just count the NULLs */
181 for ( i=0; (i<multi_len-1) && !(multi_string[i]==0x0 && multi_string[i+1]==0x0); i++ ) {
182 /* peek ahead */
183 if ( multi_string[i+1] == 0x0 )
184 num_strings++;
187 if ( num_strings == 0 )
188 return 0;
190 if ( !(sz = TALLOC_ARRAY( NULL, char*, num_strings+1 )) ) {
191 DEBUG(0,("reg_convert_multi_sz: talloc() failed!\n"));
192 return -1;
195 wp = multi_string;
197 for ( i=0; i<num_strings; i++ ) {
198 rpcstr_pull( buffer, wp, sizeof(buffer), -1, STR_TERMINATE );
199 sz[i] = talloc_strdup( sz, buffer );
201 /* skip to the next string NULL and then one more */
202 while ( *wp )
203 wp++;
204 wp++;
207 /* tag the array off with an empty string */
208 sz[i] = '\0';
210 *values = sz;
212 return num_strings;
215 /**********************************************************************
216 Returns number of bytes, not number of unicode characters
217 *********************************************************************/
219 size_t regval_build_multi_sz( char **values, uint16 **buffer )
221 int i;
222 size_t buf_size = 0;
223 uint16 *buf, *b;
224 UNISTR2 sz;
226 if ( !values || !buffer )
227 return 0;
229 /* go ahead and alloc some space */
231 if ( !(buf = TALLOC_ARRAY( NULL, uint16, 2 )) ) {
232 DEBUG(0,("regval_build_multi_sz: talloc() failed!\n"));
233 return 0;
236 for ( i=0; values[i]; i++ ) {
237 ZERO_STRUCT( sz );
238 /* DEBUG(0,("regval_build_multi_sz: building [%s]\n",values[i])); */
239 init_unistr2( &sz, values[i], UNI_STR_TERMINATE );
241 /* Alloc some more memory. Always add one one to account for the
242 double NULL termination */
244 b = TALLOC_REALLOC_ARRAY( NULL, buf, uint16, buf_size+sz.uni_str_len+1 );
245 if ( !b ) {
246 DEBUG(0,("regval_build_multi_sz: talloc() reallocation error!\n"));
247 TALLOC_FREE( buffer );
248 return 0;
250 buf = b;
252 /* copy the unistring2 buffer and increment the size */
253 /* dump_data(1,sz.buffer,sz.uni_str_len*2); */
254 memcpy( buf+buf_size, sz.buffer, sz.uni_str_len*2 );
255 buf_size += sz.uni_str_len;
257 /* cleanup rather than leaving memory hanging around */
258 TALLOC_FREE( sz.buffer );
261 buf[buf_size++] = 0x0;
263 *buffer = buf;
265 /* return number of bytes */
266 return buf_size*2;