registry: move normalize_dbkey() from lib/util_reg.c to registry/reg_util.c
[kai/samba.git] / source3 / registry / reg_util.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Virtual Windows Registry Layer (utility functions)
4  *  Copyright (C) Gerald Carter                     2002-2005
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 /* Implementation of registry frontend view functions. */
21
22 #include "includes.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_REGISTRY
26
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.
31
32  WARNING!!  Does modify the original string!
33  ***********************************************************************/
34
35 bool reg_split_path(char *path, char **base, char **new_path)
36 {
37         char *p;
38
39         *new_path = *base = NULL;
40
41         if (!path) {
42                 return false;
43         }
44         *base = path;
45
46         p = strchr(path, '\\');
47
48         if ( p ) {
49                 *p = '\0';
50                 *new_path = p+1;
51         }
52
53         return true;
54 }
55
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.
60
61  WARNING!!  Does modify the original string!
62  ***********************************************************************/
63
64 bool reg_split_key(char *path, char **base, char **key)
65 {
66         char *p;
67
68         *key = *base = NULL;
69
70         if (!path) {
71                 return false;
72         }
73
74         *base = path;
75
76         p = strrchr(path, '\\');
77
78         if (p) {
79                 *p = '\0';
80                 *key = p+1;
81         }
82
83         return true;
84 }
85
86 /**********************************************************************
87  The full path to the registry key is used as database after the
88  \'s are converted to /'s.  Key string is also normalized to UPPER
89  case.
90 **********************************************************************/
91
92 char *normalize_reg_path(TALLOC_CTX *ctx, const char *keyname )
93 {
94         char *nkeyname = talloc_string_sub(ctx, keyname, "\\", "/");
95         if (!nkeyname) {
96                 return NULL;
97         }
98         strupper_m(nkeyname);
99         return nkeyname;
100 }
101
102 /**
103  * normalize ther registry path in place.
104  */
105 void normalize_dbkey(char *key)
106 {
107         size_t len = strlen(key);
108         string_sub(key, "\\", "/", len+1);
109         strupper_m(key);
110 }
111
112 /**********************************************************************
113  move to next non-delimter character
114 *********************************************************************/
115
116 char *reg_remaining_path(TALLOC_CTX *ctx, const char *key)
117 {
118         char *new_path = NULL;
119         char *p = NULL;
120
121         if (!key || !*key) {
122                 return NULL;
123         }
124
125         new_path = talloc_strdup(ctx, key);
126         if (!new_path) {
127                 return NULL;
128         }
129         /* normalize_reg_path( new_path ); */
130         if (!(p = strchr(new_path, '\\')) ) {
131                 if (!(p = strchr( new_path, '/'))) {
132                         p = new_path;
133                 } else {
134                         p++;
135                 }
136         } else {
137                 p++;
138         }
139
140         return p;
141 }
142
143 /**********************************************************************
144 *********************************************************************/
145
146 int regval_convert_multi_sz( uint16 *multi_string, size_t byte_len, char ***values )
147 {
148         char **sz;
149         int i;
150         int num_strings = 0;
151         fstring buffer;
152         uint16 *wp;
153         size_t multi_len = byte_len / 2;
154
155         if ( !multi_string || !values )
156                 return 0;
157
158         *values = NULL;
159
160         /* just count the NULLs */
161
162         for ( i=0; (i<multi_len-1) && !(multi_string[i]==0x0 && multi_string[i+1]==0x0); i++ ) {
163                 /* peek ahead */
164                 if ( multi_string[i+1] == 0x0 )
165                         num_strings++;
166         }
167
168         if ( num_strings == 0 )
169                 return 0;
170
171         if ( !(sz = TALLOC_ARRAY( NULL, char*, num_strings+1 )) ) {
172                 DEBUG(0,("reg_convert_multi_sz: talloc() failed!\n"));
173                 return -1;
174         }
175
176         wp = multi_string;
177
178         for ( i=0; i<num_strings; i++ ) {
179                 rpcstr_pull( buffer, wp, sizeof(buffer), -1, STR_TERMINATE );
180                 sz[i] = talloc_strdup( sz, buffer );
181
182                 /* skip to the next string NULL and then one more */
183                 while ( *wp )
184                         wp++;
185                 wp++;
186         }
187
188         /* tag the array off with an empty string */
189         sz[i] = '\0';
190
191         *values = sz;
192
193         return num_strings;
194 }
195
196 /**********************************************************************
197  Returns number of bytes, not number of unicode characters
198 *********************************************************************/
199
200 size_t regval_build_multi_sz( char **values, uint16 **buffer )
201 {
202         int i;
203         size_t buf_size = 0;
204         uint16 *buf, *b;
205         UNISTR2 sz;
206
207         if ( !values || !buffer )
208                 return 0;
209
210         /* go ahead and alloc some space */
211
212         if ( !(buf = TALLOC_ARRAY( NULL, uint16, 2 )) ) {
213                 DEBUG(0,("regval_build_multi_sz: talloc() failed!\n"));
214                 return 0;
215         }
216
217         for ( i=0; values[i]; i++ ) {
218                 ZERO_STRUCT( sz );
219                 /* DEBUG(0,("regval_build_multi_sz: building [%s]\n",values[i])); */
220                 init_unistr2( &sz, values[i], UNI_STR_TERMINATE );
221
222                 /* Alloc some more memory.  Always add one one to account for the 
223                    double NULL termination */
224
225                 b = TALLOC_REALLOC_ARRAY( NULL, buf, uint16, buf_size+sz.uni_str_len+1 );
226                 if ( !b ) {
227                         DEBUG(0,("regval_build_multi_sz: talloc() reallocation error!\n"));
228                         TALLOC_FREE( buffer );
229                         return 0;
230                 }
231                 buf = b;
232
233                 /* copy the unistring2 buffer and increment the size */
234                 /* dump_data(1,sz.buffer,sz.uni_str_len*2); */
235                 memcpy( buf+buf_size, sz.buffer, sz.uni_str_len*2 );
236                 buf_size += sz.uni_str_len;
237
238                 /* cleanup rather than leaving memory hanging around */
239                 TALLOC_FREE( sz.buffer );
240         }
241
242         buf[buf_size++] = 0x0;
243
244         *buffer = buf;
245
246         /* return number of bytes */
247         return buf_size*2;
248 }