r128: Another registry update. Changes:
[bbaumbach/samba-autobuild/.git] / source4 / lib / registry / common / reg_util.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Transparent registry backend handling
4    Copyright (C) Jelmer Vernooij                        2003-2004.
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_REGISTRY
25
26 /* Return string description of registry value type */
27 const char *str_regtype(int type)
28 {
29         switch(type) {
30         case REG_SZ: return "STRING";
31         case REG_DWORD: return "DWORD";
32         case REG_BINARY: return "BINARY";
33         }
34         return "Unknown";
35 }
36
37 char *reg_val_data_string(REG_VAL *v)
38
39   char *asciip;
40   char *ret = NULL;
41   int i;
42
43   if(reg_val_size(v) == 0) return strdup("");
44
45   switch (reg_val_type(v)) {
46   case REG_SZ:
47           /* FIXME: Convert to ascii */
48           return strdup(reg_val_data_blk(v));
49
50   case REG_EXPAND_SZ:
51           return strdup(reg_val_data_blk(v));
52
53   case REG_BINARY:
54           ret = malloc(reg_val_size(v) * 3 + 2);
55           asciip = ret;
56           for (i=0; i<reg_val_size(v); i++) { 
57                   int str_rem = reg_val_size(v) * 3 - (asciip - ret);
58                   asciip += snprintf(asciip, str_rem, "%02x", *(unsigned char *)(reg_val_data_blk(v)+i));
59                   if (i < reg_val_size(v) && str_rem > 0)
60                           *asciip = ' '; asciip++;      
61           }
62           *asciip = '\0';
63           return ret;
64           break;
65
66   case REG_DWORD:
67           if (*(int *)reg_val_data_blk(v) == 0)
68                   ret = strdup("0");
69           else
70                   asprintf(&ret, "0x%x", *(int *)reg_val_data_blk(v));
71           break;
72
73   case REG_MULTI_SZ:
74         /* FIXME */
75     break;
76
77   default:
78     return 0;
79     break;
80   } 
81
82   return ret;
83 }
84
85 char *reg_val_description(REG_VAL *val) 
86 {
87         char *ret, *ds = reg_val_data_string(val);
88         asprintf(&ret, "%s = %s : %s", reg_val_name(val)?reg_val_name(val):"<No Name>", str_regtype(reg_val_type(val)), ds);
89         free(ds);
90         return ret;
91 }
92
93 BOOL reg_val_set_string(REG_VAL *val, char *str)
94 {
95         /* FIXME */
96         return False;
97 }
98
99 WERROR reg_key_get_subkey_val(REG_KEY *key, const char *subname, const char *valname, REG_VAL **val)
100 {
101         REG_KEY *k;
102         WERROR error = reg_key_get_subkey_by_name(key, subname, &k);
103         if(!W_ERROR_IS_OK(error)) return error;
104         
105         return reg_key_get_value_by_name(k, valname, val);
106 }
107
108 WERROR reg_key_set_subkey_val(REG_KEY *key, const char *subname, const char *valname, uint32 type, uint8 *data, int real_len)
109 {
110         REG_KEY *k;
111         REG_VAL *v;
112         WERROR error;
113
114         error = reg_key_get_subkey_by_name(key, subname, &k);
115         if(!W_ERROR_IS_OK(error)) return error;
116
117         error = reg_key_get_value_by_name(k, valname, &v);
118         if(!W_ERROR_IS_OK(error)) return error;
119         
120         return reg_val_update(v, type, data, real_len);
121 }
122
123 /***********************************************************************
124  Utility function for splitting the base path of a registry path off
125  by setting base and new_path to the apprapriate offsets withing the
126  path.
127  
128  WARNING!!  Does modify the original string!
129  ***********************************************************************/
130
131 BOOL reg_split_path( char *path, char **base, char **new_path )
132 {
133         char *p;
134         
135         *new_path = *base = NULL;
136         
137         if ( !path)
138                 return False;
139         
140         *base = path;
141         
142         p = strchr( path, '\\' );
143         
144         if ( p ) {
145                 *p = '\0';
146                 *new_path = p+1;
147         }
148         
149         return True;
150 }
151
152 /**
153  * Replace all \'s with /'s
154  */
155 char *reg_path_win2unix(char *path) 
156 {
157         int i;
158         for(i = 0; path[i]; i++) {
159                 if(path[i] == '\\') path[i] = '/';
160         }
161         return path;
162 }
163 /**
164  * Replace all /'s with \'s
165  */
166 char *reg_path_unix2win(char *path) 
167 {
168         int i;
169         for(i = 0; path[i]; i++) {
170                 if(path[i] == '/') path[i] = '\\';
171         }
172         return path;
173 }