e911d6af8c00d751957fe5c5804f395f562635cc
[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 /***********************************************************************
58  Utility function for splitting the base path of a registry path off
59  by setting base and new_path to the appropriate offsets withing the
60  path.
61  
62  WARNING!!  Does modify the original string!
63  ***********************************************************************/
64
65 BOOL reg_split_key( char *path, char **base, char **key )
66 {
67         char *p;
68         
69         *key = *base = NULL;
70         
71         if ( !path)
72                 return False;
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 /**********************************************************************
88  The full path to the registry key is used as database after the 
89  \'s are converted to /'s.  Key string is also normalized to UPPER
90  case. 
91 **********************************************************************/
92
93 void normalize_reg_path( pstring keyname )
94 {
95         pstring_sub( keyname, "\\", "/" );
96         strupper_m( keyname  );
97 }
98
99 /**********************************************************************
100  move to next non-delimter character
101 *********************************************************************/
102
103 char* reg_remaining_path( const char *key )
104 {
105         pstring new_path;
106         char *p;
107         
108         if ( !key || !*key )
109                 return NULL;
110
111         pstrcpy( new_path, key );
112         /* normalize_reg_path( new_path ); */
113         
114         if ( !(p = strchr( new_path, '\\' )) ) 
115         {
116                 if ( !(p = strchr( new_path, '/' )) )
117                         p = new_path;
118                 else 
119                         p++;
120         }
121         else
122                 p++;
123                 
124         return talloc_strdup(talloc_tos(), p);
125 }
126
127 /**********************************************************************
128 *********************************************************************/
129
130 int regval_convert_multi_sz( uint16 *multi_string, size_t byte_len, char ***values )
131 {
132         char **sz;
133         int i;
134         int num_strings = 0;
135         fstring buffer;
136         uint16 *wp;
137         size_t multi_len = byte_len / 2;
138         
139         if ( !multi_string || !values )
140                 return 0;
141
142         *values = NULL;
143
144         /* just count the NULLs */
145         
146         for ( i=0; (i<multi_len-1) && !(multi_string[i]==0x0 && multi_string[i+1]==0x0); i++ ) {
147                 /* peek ahead */
148                 if ( multi_string[i+1] == 0x0 )
149                         num_strings++;
150         }
151
152         if ( num_strings == 0 )
153                 return 0;
154         
155         if ( !(sz = TALLOC_ARRAY( NULL, char*, num_strings+1 )) ) {
156                 DEBUG(0,("reg_convert_multi_sz: talloc() failed!\n"));
157                 return -1;
158         }
159
160         wp = multi_string;
161         
162         for ( i=0; i<num_strings; i++ ) {
163                 rpcstr_pull( buffer, wp, sizeof(buffer), -1, STR_TERMINATE );
164                 sz[i] = talloc_strdup( sz, buffer );
165                 
166                 /* skip to the next string NULL and then one more */
167                 while ( *wp )
168                         wp++;
169                 wp++;
170         }
171         
172         /* tag the array off with an empty string */
173         sz[i] = '\0';
174         
175         *values = sz;
176         
177         return num_strings;
178 }
179
180 /**********************************************************************
181  Returns number of bytes, not number of unicode characters
182 *********************************************************************/
183
184 size_t regval_build_multi_sz( char **values, uint16 **buffer )
185 {
186         int i;
187         size_t buf_size = 0;
188         uint16 *buf, *b;
189         UNISTR2 sz;
190
191         if ( !values || !buffer )
192                 return 0;
193         
194         /* go ahead and alloc some space */
195         
196         if ( !(buf = TALLOC_ARRAY( NULL, uint16, 2 )) ) {
197                 DEBUG(0,("regval_build_multi_sz: talloc() failed!\n"));
198                 return 0;
199         }
200         
201         for ( i=0; values[i]; i++ ) {
202                 ZERO_STRUCT( sz );
203                 /* DEBUG(0,("regval_build_multi_sz: building [%s]\n",values[i])); */
204                 init_unistr2( &sz, values[i], UNI_STR_TERMINATE );
205                 
206                 /* Alloc some more memory.  Always add one one to account for the 
207                    double NULL termination */
208                    
209                 b = TALLOC_REALLOC_ARRAY( NULL, buf, uint16, buf_size+sz.uni_str_len+1 );
210                 if ( !b ) {
211                         DEBUG(0,("regval_build_multi_sz: talloc() reallocation error!\n"));
212                         TALLOC_FREE( buffer );
213                         return 0;
214                 }
215                 buf = b;
216
217                 /* copy the unistring2 buffer and increment the size */ 
218                 /* dump_data(1,sz.buffer,sz.uni_str_len*2); */
219                 memcpy( buf+buf_size, sz.buffer, sz.uni_str_len*2 );
220                 buf_size += sz.uni_str_len;
221                 
222                 /* cleanup rather than leaving memory hanging around */
223                 TALLOC_FREE( sz.buffer );
224         }
225         
226         buf[buf_size++] = 0x0;
227
228         *buffer = buf;
229
230         /* return number of bytes */
231         return buf_size*2;
232 }
233
234