052ccc347be46a576cff3470d1c8b1b07d2702e2
[samba.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 #include "registry.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_REGISTRY
26
27 /* Return string description of registry value type */
28 const char *str_regtype(int type)
29 {
30         switch(type) {
31         case REG_SZ: return "STRING";
32         case REG_DWORD: return "DWORD";
33         case REG_BINARY: return "BINARY";
34         }
35         return "Unknown";
36 }
37
38 char *reg_val_data_string(TALLOC_CTX *mem_ctx, struct registry_value *v)
39
40   char *asciip;
41   char *ret = NULL;
42   int i;
43
44   if(v->data_len == 0) return talloc_strdup(mem_ctx, "");
45
46   switch (v->data_type) {
47   case REG_SZ:
48           return talloc_strndup(mem_ctx, v->data_blk, v->data_len);
49
50   case REG_EXPAND_SZ:
51           return talloc_strndup(mem_ctx, v->data_blk, v->data_len);
52
53   case REG_BINARY:
54           ret = talloc(mem_ctx, v->data_len * 3 + 2);
55           asciip = ret;
56           for (i=0; i<v->data_len; i++) { 
57                   int str_rem = v->data_len * 3 - (asciip - ret);
58                   asciip += snprintf(asciip, str_rem, "%02x", *(uint8_t *)(((char *)v->data_blk)+i));
59                   if (i < v->data_len && str_rem > 0)
60                           *asciip = ' '; asciip++;      
61           }
62           *asciip = '\0';
63           return ret;
64
65   case REG_DWORD:
66           if (*(int *)v->data_blk == 0)
67                   return talloc_strdup(mem_ctx, "0");
68
69           return talloc_asprintf(mem_ctx, "0x%x", *(int *)v->data_blk);
70
71   case REG_MULTI_SZ:
72         /* FIXME */
73     break;
74
75   default:
76     break;
77   } 
78
79   return ret;
80 }
81
82 char *reg_val_description(TALLOC_CTX *mem_ctx, struct registry_value *val) 
83 {
84         return talloc_asprintf(mem_ctx, "%s = %s : %s", val->name?val->name:"<No Name>", str_regtype(val->data_type), reg_val_data_string(mem_ctx, val));
85 }
86
87 BOOL reg_val_set_string(struct registry_value *val, char *str)
88 {
89         /* FIXME */
90         return False;
91 }
92
93 WERROR reg_key_get_subkey_val(TALLOC_CTX *mem_ctx, struct registry_key *key, const char *subname, const char *valname, struct registry_value **val)
94 {
95         struct registry_key *k;
96         WERROR error = reg_key_get_subkey_by_name(mem_ctx, key, subname, &k);
97         if(!W_ERROR_IS_OK(error)) return error;
98         
99         return reg_key_get_value_by_name(mem_ctx, k, valname, val);
100 }
101
102 /***********************************************************************
103  Utility function for splitting the base path of a registry path off
104  by setting base and new_path to the apprapriate offsets withing the
105  path.
106  
107  WARNING!!  Does modify the original string!
108  ***********************************************************************/
109
110 BOOL reg_split_path( char *path, char **base, char **new_path )
111 {
112         char *p;
113         
114         *new_path = *base = NULL;
115         
116         if ( !path)
117                 return False;
118         
119         *base = path;
120         
121         p = strchr( path, '\\' );
122         
123         if ( p ) {
124                 *p = '\0';
125                 *new_path = p+1;
126         }
127         
128         return True;
129 }
130
131 /**
132  * Replace all \'s with /'s
133  */
134 char *reg_path_win2unix(char *path) 
135 {
136         int i;
137         for(i = 0; path[i]; i++) {
138                 if(path[i] == '\\') path[i] = '/';
139         }
140         return path;
141 }
142 /**
143  * Replace all /'s with \'s
144  */
145 char *reg_path_unix2win(char *path) 
146 {
147         int i;
148         for(i = 0; path[i]; i++) {
149                 if(path[i] == '/') path[i] = '\\';
150         }
151         return path;
152 }