Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side registry management |
| 3 | * |
| 4 | * Copyright (C) 1999 Alexandre Julliard |
| 5 | */ |
| 6 | |
| 7 | /* To do: |
| 8 | * - behavior with deleted keys |
| 9 | * - values larger than request buffer |
| 10 | * - symbolic links |
| 11 | */ |
| 12 | |
| 13 | #include <assert.h> |
| 14 | #include <limits.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <unistd.h> |
Juergen Schmied | 788d8d5 | 2000-01-27 05:37:54 +0000 | [diff] [blame] | 18 | #include <pwd.h> |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 19 | #include "object.h" |
| 20 | #include "handle.h" |
| 21 | #include "request.h" |
| 22 | #include "unicode.h" |
| 23 | |
| 24 | #include "winbase.h" |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 25 | #include "winreg.h" |
| 26 | |
| 27 | |
| 28 | /* a registry key */ |
| 29 | struct key |
| 30 | { |
| 31 | struct object obj; /* object header */ |
| 32 | WCHAR *name; /* key name */ |
| 33 | WCHAR *class; /* key class */ |
| 34 | struct key *parent; /* parent key */ |
| 35 | int last_subkey; /* last in use subkey */ |
| 36 | int nb_subkeys; /* count of allocated subkeys */ |
| 37 | struct key **subkeys; /* subkeys array */ |
| 38 | int last_value; /* last in use value */ |
| 39 | int nb_values; /* count of allocated values in array */ |
| 40 | struct key_value *values; /* values array */ |
| 41 | short flags; /* flags */ |
| 42 | short level; /* saving level */ |
| 43 | time_t modif; /* last modification time */ |
| 44 | }; |
| 45 | |
| 46 | /* key flags */ |
| 47 | #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */ |
| 48 | #define KEY_DELETED 0x0002 /* key has been deleted */ |
| 49 | #define KEY_ROOT 0x0004 /* key is a root key */ |
| 50 | |
| 51 | /* a key value */ |
| 52 | struct key_value |
| 53 | { |
| 54 | WCHAR *name; /* value name */ |
| 55 | int type; /* value type */ |
| 56 | size_t len; /* value data length in bytes */ |
| 57 | void *data; /* pointer to value data */ |
| 58 | }; |
| 59 | |
| 60 | #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */ |
| 61 | #define MIN_VALUES 8 /* min. number of allocated values per key */ |
| 62 | |
| 63 | |
| 64 | /* the root keys */ |
| 65 | #define HKEY_ROOT_FIRST HKEY_CLASSES_ROOT |
| 66 | #define HKEY_ROOT_LAST HKEY_DYN_DATA |
| 67 | #define NB_ROOT_KEYS (HKEY_ROOT_LAST - HKEY_ROOT_FIRST + 1) |
| 68 | #define IS_ROOT_HKEY(h) (((h) >= HKEY_ROOT_FIRST) && ((h) <= HKEY_ROOT_LAST)) |
| 69 | static struct key *root_keys[NB_ROOT_KEYS]; |
| 70 | |
| 71 | static const char * const root_key_names[NB_ROOT_KEYS] = |
| 72 | { |
| 73 | "HKEY_CLASSES_ROOT", |
| 74 | "HKEY_CURRENT_USER", |
| 75 | "HKEY_LOCAL_MACHINE", |
| 76 | "HKEY_USERS", |
| 77 | "HKEY_PERFORMANCE_DATA", |
| 78 | "HKEY_CURRENT_CONFIG", |
| 79 | "HKEY_DYN_DATA" |
| 80 | }; |
| 81 | |
| 82 | |
| 83 | /* keys saving level */ |
| 84 | /* current_level is the level that is put into all newly created or modified keys */ |
| 85 | /* saving_level is the minimum level that a key needs in order to get saved */ |
| 86 | static int current_level; |
| 87 | static int saving_level; |
| 88 | |
| 89 | static int saving_version = 1; /* file format version */ |
| 90 | |
| 91 | |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 92 | /* information about a file being loaded */ |
| 93 | struct file_load_info |
| 94 | { |
| 95 | FILE *file; /* input file */ |
| 96 | char *buffer; /* line buffer */ |
| 97 | int len; /* buffer length */ |
| 98 | int line; /* current input line */ |
| 99 | char *tmp; /* temp buffer to use while parsing input */ |
| 100 | int tmplen; /* length of temp buffer */ |
| 101 | }; |
| 102 | |
| 103 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 104 | static void key_dump( struct object *obj, int verbose ); |
| 105 | static void key_destroy( struct object *obj ); |
| 106 | |
| 107 | static const struct object_ops key_ops = |
| 108 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 109 | sizeof(struct key), /* size */ |
| 110 | key_dump, /* dump */ |
| 111 | no_add_queue, /* add_queue */ |
| 112 | NULL, /* remove_queue */ |
| 113 | NULL, /* signaled */ |
| 114 | NULL, /* satisfied */ |
| 115 | NULL, /* get_poll_events */ |
| 116 | NULL, /* poll_event */ |
| 117 | no_read_fd, /* get_read_fd */ |
| 118 | no_write_fd, /* get_write_fd */ |
| 119 | no_flush, /* flush */ |
| 120 | no_get_file_info, /* get_file_info */ |
| 121 | key_destroy /* destroy */ |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | |
| 125 | /* |
| 126 | * The registry text file format v2 used by this code is similar to the one |
| 127 | * used by REGEDIT import/export functionality, with the following differences: |
| 128 | * - strings and key names can contain \x escapes for Unicode |
| 129 | * - key names use escapes too in order to support Unicode |
| 130 | * - the modification time optionally follows the key name |
| 131 | * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex |
| 132 | */ |
| 133 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 134 | static inline char to_hex( char ch ) |
| 135 | { |
| 136 | if (isdigit(ch)) return ch - '0'; |
| 137 | return tolower(ch) - 'a' + 10; |
| 138 | } |
| 139 | |
| 140 | /* dump the full path of a key */ |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 141 | static void dump_path( struct key *key, struct key *base, FILE *f ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 142 | { |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 143 | if (key->parent && key != base) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 144 | { |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 145 | dump_path( key->parent, base, f ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 146 | fprintf( f, "\\\\" ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 147 | } |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 148 | |
| 149 | if (key->name) dump_strW( key->name, strlenW(key->name), f, "[]" ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 150 | else /* root key */ |
| 151 | { |
| 152 | int i; |
| 153 | for (i = 0; i < NB_ROOT_KEYS; i++) |
| 154 | if (root_keys[i] == key) fprintf( f, "%s", root_key_names[i] ); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /* dump a value to a text file */ |
| 159 | static void dump_value( struct key_value *value, FILE *f ) |
| 160 | { |
| 161 | int i, count; |
| 162 | |
| 163 | if (value->name[0]) |
| 164 | { |
| 165 | fputc( '\"', f ); |
| 166 | count = 1 + dump_strW( value->name, strlenW(value->name), f, "\"\"" ); |
| 167 | count += fprintf( f, "\"=" ); |
| 168 | } |
| 169 | else count = fprintf( f, "@=" ); |
| 170 | |
| 171 | switch(value->type) |
| 172 | { |
| 173 | case REG_SZ: |
| 174 | case REG_EXPAND_SZ: |
| 175 | case REG_MULTI_SZ: |
| 176 | if (value->type != REG_SZ) fprintf( f, "str(%d):", value->type ); |
| 177 | fputc( '\"', f ); |
| 178 | if (value->data) dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" ); |
| 179 | fputc( '\"', f ); |
| 180 | break; |
| 181 | case REG_DWORD: |
| 182 | if (value->len == sizeof(DWORD)) |
| 183 | { |
| 184 | DWORD dw; |
| 185 | memcpy( &dw, value->data, sizeof(DWORD) ); |
| 186 | fprintf( f, "dword:%08lx", dw ); |
| 187 | break; |
| 188 | } |
| 189 | /* else fall through */ |
| 190 | default: |
| 191 | if (value->type == REG_BINARY) count += fprintf( f, "hex:" ); |
| 192 | else count += fprintf( f, "hex(%x):", value->type ); |
| 193 | for (i = 0; i < value->len; i++) |
| 194 | { |
| 195 | count += fprintf( f, "%02x", *((unsigned char *)value->data + i) ); |
| 196 | if (i < value->len-1) |
| 197 | { |
| 198 | fputc( ',', f ); |
| 199 | if (++count > 76) |
| 200 | { |
| 201 | fprintf( f, "\\\n " ); |
| 202 | count = 2; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | break; |
| 207 | } |
| 208 | fputc( '\n', f ); |
| 209 | } |
| 210 | |
| 211 | /* save a registry and all its subkeys to a text file */ |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 212 | static void save_subkeys( struct key *key, struct key *base, FILE *f ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 213 | { |
| 214 | int i; |
| 215 | |
| 216 | if (key->flags & KEY_VOLATILE) return; |
| 217 | /* save key if it has the proper level, and has either some values or no subkeys */ |
| 218 | /* keys with no values but subkeys are saved implicitly by saving the subkeys */ |
| 219 | if ((key->level >= saving_level) && ((key->last_value >= 0) || (key->last_subkey == -1))) |
| 220 | { |
| 221 | fprintf( f, "\n[" ); |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 222 | dump_path( key, base, f ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 223 | fprintf( f, "] %ld\n", key->modif ); |
| 224 | for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f ); |
| 225 | } |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 226 | for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static void dump_operation( struct key *key, struct key_value *value, const char *op ) |
| 230 | { |
| 231 | fprintf( stderr, "%s key ", op ); |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 232 | if (key) dump_path( key, NULL, stderr ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 233 | else fprintf( stderr, "ERROR" ); |
| 234 | if (value) |
| 235 | { |
| 236 | fprintf( stderr, " value "); |
| 237 | dump_value( value, stderr ); |
| 238 | } |
| 239 | else fprintf( stderr, "\n" ); |
| 240 | } |
| 241 | |
| 242 | static void key_dump( struct object *obj, int verbose ) |
| 243 | { |
| 244 | struct key *key = (struct key *)obj; |
| 245 | assert( obj->ops == &key_ops ); |
| 246 | fprintf( stderr, "Key flags=%x ", key->flags ); |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 247 | dump_path( key, NULL, stderr ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 248 | fprintf( stderr, "\n" ); |
| 249 | } |
| 250 | |
| 251 | static void key_destroy( struct object *obj ) |
| 252 | { |
| 253 | int i; |
| 254 | struct key *key = (struct key *)obj; |
| 255 | assert( obj->ops == &key_ops ); |
| 256 | |
| 257 | free( key->name ); |
| 258 | if (key->class) free( key->class ); |
| 259 | for (i = 0; i <= key->last_value; i++) |
| 260 | { |
| 261 | free( key->values[i].name ); |
| 262 | if (key->values[i].data) free( key->values[i].data ); |
| 263 | } |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 264 | for (i = 0; i <= key->last_subkey; i++) |
| 265 | { |
| 266 | key->subkeys[i]->parent = NULL; |
| 267 | release_object( key->subkeys[i] ); |
| 268 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* duplicate a key path from the request buffer */ |
| 272 | /* returns a pointer to a static buffer, so only useable once per request */ |
| 273 | static WCHAR *copy_path( const path_t path ) |
| 274 | { |
| 275 | static WCHAR buffer[MAX_PATH+1]; |
| 276 | WCHAR *p = buffer; |
| 277 | |
| 278 | while (p < buffer + sizeof(buffer) - 1) if (!(*p++ = *path++)) break; |
| 279 | *p = 0; |
| 280 | return buffer; |
| 281 | } |
| 282 | |
| 283 | /* return the next token in a given path */ |
| 284 | /* returns a pointer to a static buffer, so only useable once per request */ |
| 285 | static WCHAR *get_path_token( const WCHAR *initpath, size_t maxlen ) |
| 286 | { |
| 287 | static const WCHAR *path; |
| 288 | static const WCHAR *end; |
| 289 | static WCHAR buffer[MAX_PATH+1]; |
| 290 | WCHAR *p = buffer; |
| 291 | |
| 292 | if (initpath) |
| 293 | { |
| 294 | path = initpath; |
| 295 | end = path + maxlen / sizeof(WCHAR); |
| 296 | } |
| 297 | while ((path < end) && (*path == '\\')) path++; |
| 298 | while ((path < end) && (p < buffer + sizeof(buffer) - 1)) |
| 299 | { |
| 300 | WCHAR ch = *path; |
| 301 | if (!ch || (ch == '\\')) break; |
| 302 | *p++ = ch; |
| 303 | path++; |
| 304 | } |
| 305 | *p = 0; |
| 306 | return buffer; |
| 307 | } |
| 308 | |
| 309 | /* duplicate a Unicode string from the request buffer */ |
| 310 | static WCHAR *req_strdupW( const WCHAR *str ) |
| 311 | { |
| 312 | WCHAR *name; |
| 313 | size_t len = get_req_strlenW( str ); |
| 314 | if ((name = mem_alloc( (len + 1) * sizeof(WCHAR) )) != NULL) |
| 315 | { |
| 316 | memcpy( name, str, len * sizeof(WCHAR) ); |
| 317 | name[len] = 0; |
| 318 | } |
| 319 | return name; |
| 320 | } |
| 321 | |
| 322 | /* allocate a key object */ |
| 323 | static struct key *alloc_key( const WCHAR *name, time_t modif ) |
| 324 | { |
| 325 | struct key *key; |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 326 | if ((key = (struct key *)alloc_object( &key_ops, -1 ))) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 327 | { |
| 328 | key->name = NULL; |
| 329 | key->class = NULL; |
| 330 | key->flags = 0; |
| 331 | key->last_subkey = -1; |
| 332 | key->nb_subkeys = 0; |
| 333 | key->subkeys = NULL; |
| 334 | key->nb_values = 0; |
| 335 | key->last_value = -1; |
| 336 | key->values = NULL; |
| 337 | key->level = current_level; |
| 338 | key->modif = modif; |
| 339 | key->parent = NULL; |
| 340 | if (name && !(key->name = strdupW( name ))) |
| 341 | { |
| 342 | release_object( key ); |
| 343 | key = NULL; |
| 344 | } |
| 345 | } |
| 346 | return key; |
| 347 | } |
| 348 | |
| 349 | /* update key modification time */ |
| 350 | static void touch_key( struct key *key ) |
| 351 | { |
| 352 | key->modif = time(NULL); |
| 353 | key->level = MAX( key->level, current_level ); |
| 354 | } |
| 355 | |
| 356 | /* try to grow the array of subkeys; return 1 if OK, 0 on error */ |
| 357 | static int grow_subkeys( struct key *key ) |
| 358 | { |
| 359 | struct key **new_subkeys; |
| 360 | int nb_subkeys; |
| 361 | |
| 362 | if (key->nb_subkeys) |
| 363 | { |
| 364 | nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */ |
| 365 | if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) |
| 366 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 367 | set_error( STATUS_NO_MEMORY ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 368 | return 0; |
| 369 | } |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | nb_subkeys = MIN_VALUES; |
| 374 | if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0; |
| 375 | } |
| 376 | key->subkeys = new_subkeys; |
| 377 | key->nb_subkeys = nb_subkeys; |
| 378 | return 1; |
| 379 | } |
| 380 | |
| 381 | /* allocate a subkey for a given key, and return its index */ |
| 382 | static struct key *alloc_subkey( struct key *parent, const WCHAR *name, int index, time_t modif ) |
| 383 | { |
| 384 | struct key *key; |
| 385 | int i; |
| 386 | |
| 387 | if (parent->last_subkey + 1 == parent->nb_subkeys) |
| 388 | { |
| 389 | /* need to grow the array */ |
| 390 | if (!grow_subkeys( parent )) return NULL; |
| 391 | } |
| 392 | if ((key = alloc_key( name, modif )) != NULL) |
| 393 | { |
| 394 | key->parent = parent; |
| 395 | for (i = ++parent->last_subkey; i > index; i--) |
| 396 | parent->subkeys[i] = parent->subkeys[i-1]; |
| 397 | parent->subkeys[index] = key; |
| 398 | } |
| 399 | return key; |
| 400 | } |
| 401 | |
| 402 | /* free a subkey of a given key */ |
| 403 | static void free_subkey( struct key *parent, int index ) |
| 404 | { |
| 405 | struct key *key; |
| 406 | int i, nb_subkeys; |
| 407 | |
| 408 | assert( index >= 0 ); |
| 409 | assert( index <= parent->last_subkey ); |
| 410 | |
| 411 | key = parent->subkeys[index]; |
| 412 | for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1]; |
| 413 | parent->last_subkey--; |
| 414 | key->flags |= KEY_DELETED; |
| 415 | key->parent = NULL; |
| 416 | release_object( key ); |
| 417 | |
| 418 | /* try to shrink the array */ |
| 419 | nb_subkeys = key->nb_subkeys; |
| 420 | if (nb_subkeys > MIN_SUBKEYS && key->last_subkey < nb_subkeys / 2) |
| 421 | { |
| 422 | struct key **new_subkeys; |
| 423 | nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */ |
| 424 | if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS; |
| 425 | if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return; |
| 426 | key->subkeys = new_subkeys; |
| 427 | key->nb_subkeys = nb_subkeys; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /* find the named child of a given key and return its index */ |
| 432 | static struct key *find_subkey( struct key *key, const WCHAR *name, int *index ) |
| 433 | { |
| 434 | int i, min, max, res; |
| 435 | |
| 436 | min = 0; |
| 437 | max = key->last_subkey; |
| 438 | while (min <= max) |
| 439 | { |
| 440 | i = (min + max) / 2; |
| 441 | if (!(res = strcmpiW( key->subkeys[i]->name, name ))) |
| 442 | { |
| 443 | *index = i; |
| 444 | return key->subkeys[i]; |
| 445 | } |
| 446 | if (res > 0) max = i - 1; |
| 447 | else min = i + 1; |
| 448 | } |
| 449 | *index = min; /* this is where we should insert it */ |
| 450 | return NULL; |
| 451 | } |
| 452 | |
| 453 | /* open a subkey */ |
| 454 | static struct key *open_key( struct key *key, const WCHAR *name, size_t maxlen ) |
| 455 | { |
| 456 | int index; |
| 457 | WCHAR *path; |
| 458 | |
| 459 | path = get_path_token( name, maxlen ); |
| 460 | while (*path) |
| 461 | { |
| 462 | if (!(key = find_subkey( key, path, &index ))) |
| 463 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 464 | set_error( STATUS_OBJECT_NAME_NOT_FOUND ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 465 | break; |
| 466 | } |
| 467 | path = get_path_token( NULL, 0 ); |
| 468 | } |
| 469 | |
| 470 | if (debug_level > 1) dump_operation( key, NULL, "Open" ); |
| 471 | if (key) grab_object( key ); |
| 472 | return key; |
| 473 | } |
| 474 | |
| 475 | /* create a subkey */ |
| 476 | static struct key *create_key( struct key *key, const WCHAR *name, size_t maxlen, WCHAR *class, |
| 477 | unsigned int options, time_t modif, int *created ) |
| 478 | { |
| 479 | struct key *base; |
| 480 | int base_idx, index, flags = 0; |
| 481 | WCHAR *path; |
| 482 | |
| 483 | if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */ |
| 484 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 485 | set_error( STATUS_KEY_DELETED ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 486 | return NULL; |
| 487 | } |
| 488 | if (options & REG_OPTION_VOLATILE) flags |= KEY_VOLATILE; |
| 489 | else if (key->flags & KEY_VOLATILE) |
| 490 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 491 | set_error( STATUS_CHILD_MUST_BE_VOLATILE ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 492 | return NULL; |
| 493 | } |
| 494 | |
| 495 | path = get_path_token( name, maxlen ); |
| 496 | *created = 0; |
| 497 | while (*path) |
| 498 | { |
| 499 | struct key *subkey; |
| 500 | if (!(subkey = find_subkey( key, path, &index ))) break; |
| 501 | key = subkey; |
| 502 | path = get_path_token( NULL, 0 ); |
| 503 | } |
| 504 | |
| 505 | /* create the remaining part */ |
| 506 | |
| 507 | if (!*path) goto done; |
| 508 | *created = 1; |
| 509 | base = key; |
| 510 | base_idx = index; |
| 511 | key = alloc_subkey( key, path, index, modif ); |
| 512 | while (key) |
| 513 | { |
| 514 | key->flags |= flags; |
| 515 | path = get_path_token( NULL, 0 ); |
| 516 | if (!*path) goto done; |
| 517 | /* we know the index is always 0 in a new key */ |
| 518 | key = alloc_subkey( key, path, 0, modif ); |
| 519 | } |
| 520 | if (base_idx != -1) free_subkey( base, base_idx ); |
| 521 | return NULL; |
| 522 | |
| 523 | done: |
| 524 | if (debug_level > 1) dump_operation( key, NULL, "Create" ); |
| 525 | if (class) key->class = strdupW(class); |
| 526 | grab_object( key ); |
| 527 | return key; |
| 528 | } |
| 529 | |
| 530 | /* find a subkey of a given key by its index */ |
| 531 | static void enum_key( struct key *parent, int index, WCHAR *name, WCHAR *class, time_t *modif ) |
| 532 | { |
| 533 | struct key *key; |
| 534 | |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 535 | if ((index < 0) || (index > parent->last_subkey)) set_error( STATUS_NO_MORE_ENTRIES ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 536 | else |
| 537 | { |
| 538 | key = parent->subkeys[index]; |
| 539 | *modif = key->modif; |
| 540 | strcpyW( name, key->name ); |
| 541 | if (key->class) strcpyW( class, key->class ); /* FIXME: length */ |
| 542 | else *class = 0; |
| 543 | if (debug_level > 1) dump_operation( key, NULL, "Enum" ); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /* query information about a key */ |
| 548 | static void query_key( struct key *key, struct query_key_info_request *req ) |
| 549 | { |
| 550 | int i, len; |
| 551 | int max_subkey = 0, max_class = 0; |
| 552 | int max_value = 0, max_data = 0; |
| 553 | |
Ulrich Czekalla | 6f676cd | 1999-12-10 03:43:07 +0000 | [diff] [blame] | 554 | for (i = 0; i <= key->last_subkey; i++) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 555 | { |
| 556 | struct key *subkey = key->subkeys[i]; |
| 557 | len = strlenW( subkey->name ); |
| 558 | if (len > max_subkey) max_subkey = len; |
| 559 | if (!subkey->class) continue; |
| 560 | len = strlenW( subkey->class ); |
| 561 | if (len > max_class) max_class = len; |
| 562 | } |
Ulrich Czekalla | 6f676cd | 1999-12-10 03:43:07 +0000 | [diff] [blame] | 563 | for (i = 0; i <= key->last_value; i++) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 564 | { |
| 565 | len = strlenW( key->values[i].name ); |
| 566 | if (len > max_value) max_value = len; |
| 567 | len = key->values[i].len; |
| 568 | if (len > max_data) max_data = len; |
| 569 | } |
| 570 | req->subkeys = key->last_subkey + 1; |
| 571 | req->max_subkey = max_subkey; |
| 572 | req->max_class = max_class; |
| 573 | req->values = key->last_value + 1; |
| 574 | req->max_value = max_value; |
| 575 | req->max_data = max_data; |
| 576 | req->modif = key->modif; |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 577 | strcpyW( req->name, key->name); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 578 | if (key->class) strcpyW( req->class, key->class ); /* FIXME: length */ |
| 579 | else req->class[0] = 0; |
| 580 | if (debug_level > 1) dump_operation( key, NULL, "Query" ); |
| 581 | } |
| 582 | |
| 583 | /* delete a key and its values */ |
| 584 | static void delete_key( struct key *key, const WCHAR *name, size_t maxlen ) |
| 585 | { |
| 586 | int index; |
| 587 | struct key *parent; |
| 588 | WCHAR *path; |
| 589 | |
| 590 | path = get_path_token( name, maxlen ); |
| 591 | if (!*path) |
| 592 | { |
| 593 | /* deleting this key, must find parent and index */ |
| 594 | if (key->flags & KEY_ROOT) |
| 595 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 596 | set_error( STATUS_ACCESS_DENIED ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 597 | return; |
| 598 | } |
| 599 | if (!(parent = key->parent) || (key->flags & KEY_DELETED)) |
| 600 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 601 | set_error( STATUS_KEY_DELETED ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 602 | return; |
| 603 | } |
| 604 | for (index = 0; index <= parent->last_subkey; index++) |
| 605 | if (parent->subkeys[index] == key) break; |
| 606 | assert( index <= parent->last_subkey ); |
| 607 | } |
| 608 | else while (*path) |
| 609 | { |
| 610 | parent = key; |
| 611 | if (!(key = find_subkey( parent, path, &index ))) |
| 612 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 613 | set_error( STATUS_OBJECT_NAME_NOT_FOUND ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 614 | return; |
| 615 | } |
| 616 | path = get_path_token( NULL, 0 ); |
| 617 | } |
| 618 | |
| 619 | /* we can only delete a key that has no subkeys (FIXME) */ |
| 620 | if ((key->flags & KEY_ROOT) || (key->last_subkey >= 0)) |
| 621 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 622 | set_error( STATUS_ACCESS_DENIED ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 623 | return; |
| 624 | } |
| 625 | if (debug_level > 1) dump_operation( key, NULL, "Delete" ); |
| 626 | free_subkey( parent, index ); |
| 627 | touch_key( parent ); |
| 628 | } |
| 629 | |
| 630 | /* try to grow the array of values; return 1 if OK, 0 on error */ |
| 631 | static int grow_values( struct key *key ) |
| 632 | { |
| 633 | struct key_value *new_val; |
| 634 | int nb_values; |
| 635 | |
| 636 | if (key->nb_values) |
| 637 | { |
| 638 | nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */ |
| 639 | if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) |
| 640 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 641 | set_error( STATUS_NO_MEMORY ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 642 | return 0; |
| 643 | } |
| 644 | } |
| 645 | else |
| 646 | { |
| 647 | nb_values = MIN_VALUES; |
| 648 | if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0; |
| 649 | } |
| 650 | key->values = new_val; |
| 651 | key->nb_values = nb_values; |
| 652 | return 1; |
| 653 | } |
| 654 | |
| 655 | /* find the named value of a given key and return its index in the array */ |
| 656 | static struct key_value *find_value( const struct key *key, const WCHAR *name, int *index ) |
| 657 | { |
| 658 | int i, min, max, res; |
| 659 | |
| 660 | min = 0; |
| 661 | max = key->last_value; |
| 662 | while (min <= max) |
| 663 | { |
| 664 | i = (min + max) / 2; |
| 665 | if (!(res = strcmpiW( key->values[i].name, name ))) |
| 666 | { |
| 667 | *index = i; |
| 668 | return &key->values[i]; |
| 669 | } |
| 670 | if (res > 0) max = i - 1; |
| 671 | else min = i + 1; |
| 672 | } |
| 673 | *index = min; /* this is where we should insert it */ |
| 674 | return NULL; |
| 675 | } |
| 676 | |
| 677 | /* insert a new value or return a pointer to an existing one */ |
| 678 | static struct key_value *insert_value( struct key *key, const WCHAR *name ) |
| 679 | { |
| 680 | struct key_value *value; |
| 681 | WCHAR *new_name; |
| 682 | int i, index; |
| 683 | |
| 684 | if (!(value = find_value( key, name, &index ))) |
| 685 | { |
| 686 | /* not found, add it */ |
| 687 | if (key->last_value + 1 == key->nb_values) |
| 688 | { |
| 689 | if (!grow_values( key )) return NULL; |
| 690 | } |
| 691 | if (!(new_name = strdupW(name))) return NULL; |
| 692 | for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1]; |
| 693 | value = &key->values[index]; |
| 694 | value->name = new_name; |
| 695 | value->len = 0; |
| 696 | value->data = NULL; |
| 697 | } |
| 698 | return value; |
| 699 | } |
| 700 | |
| 701 | /* set a key value */ |
| 702 | static void set_value( struct key *key, WCHAR *name, int type, int datalen, void *data ) |
| 703 | { |
| 704 | struct key_value *value; |
| 705 | void *ptr = NULL; |
| 706 | |
| 707 | /* first copy the data */ |
| 708 | if (datalen) |
| 709 | { |
| 710 | if (!(ptr = mem_alloc( datalen ))) return; |
| 711 | memcpy( ptr, data, datalen ); |
| 712 | } |
| 713 | |
| 714 | if (!(value = insert_value( key, name ))) |
| 715 | { |
| 716 | if (ptr) free( ptr ); |
| 717 | return; |
| 718 | } |
| 719 | if (value->data) free( value->data ); /* already existing, free previous data */ |
| 720 | value->type = type; |
| 721 | value->len = datalen; |
| 722 | value->data = ptr; |
| 723 | touch_key( key ); |
| 724 | if (debug_level > 1) dump_operation( key, value, "Set" ); |
| 725 | } |
| 726 | |
| 727 | /* get a key value */ |
| 728 | static void get_value( struct key *key, WCHAR *name, int *type, int *len, void *data ) |
| 729 | { |
| 730 | struct key_value *value; |
| 731 | int index; |
| 732 | |
| 733 | if ((value = find_value( key, name, &index ))) |
| 734 | { |
| 735 | *type = value->type; |
| 736 | *len = value->len; |
| 737 | if (value->data) memcpy( data, value->data, value->len ); |
| 738 | if (debug_level > 1) dump_operation( key, value, "Get" ); |
| 739 | } |
| 740 | else |
| 741 | { |
| 742 | *type = -1; |
| 743 | *len = 0; |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 744 | set_error( STATUS_OBJECT_NAME_NOT_FOUND ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
| 748 | /* enumerate a key value */ |
| 749 | static void enum_value( struct key *key, int i, WCHAR *name, int *type, int *len, void *data ) |
| 750 | { |
| 751 | struct key_value *value; |
| 752 | |
| 753 | if (i < 0 || i > key->last_value) |
| 754 | { |
| 755 | name[0] = 0; |
| 756 | *len = 0; |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 757 | set_error( STATUS_NO_MORE_ENTRIES ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 758 | } |
| 759 | else |
| 760 | { |
| 761 | value = &key->values[i]; |
| 762 | strcpyW( name, value->name ); |
| 763 | *type = value->type; |
| 764 | *len = value->len; |
| 765 | if (value->data) memcpy( data, value->data, value->len ); |
| 766 | if (debug_level > 1) dump_operation( key, value, "Enum" ); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | /* delete a value */ |
| 771 | static void delete_value( struct key *key, const WCHAR *name ) |
| 772 | { |
| 773 | struct key_value *value; |
| 774 | int i, index, nb_values; |
| 775 | |
| 776 | if (!(value = find_value( key, name, &index ))) |
| 777 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 778 | set_error( STATUS_OBJECT_NAME_NOT_FOUND ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 779 | return; |
| 780 | } |
| 781 | if (debug_level > 1) dump_operation( key, value, "Delete" ); |
| 782 | free( value->name ); |
| 783 | if (value->data) free( value->data ); |
| 784 | for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1]; |
| 785 | key->last_value--; |
| 786 | touch_key( key ); |
| 787 | |
| 788 | /* try to shrink the array */ |
| 789 | nb_values = key->nb_values; |
| 790 | if (nb_values > MIN_VALUES && key->last_value < nb_values / 2) |
| 791 | { |
| 792 | struct key_value *new_val; |
| 793 | nb_values -= nb_values / 3; /* shrink by 33% */ |
| 794 | if (nb_values < MIN_VALUES) nb_values = MIN_VALUES; |
| 795 | if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return; |
| 796 | key->values = new_val; |
| 797 | key->nb_values = nb_values; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | static struct key *get_hkey_obj( int hkey, unsigned int access ); |
| 802 | |
| 803 | static struct key *create_root_key( int hkey ) |
| 804 | { |
| 805 | int dummy; |
| 806 | struct key *key; |
| 807 | |
| 808 | switch(hkey) |
| 809 | { |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 810 | /* the two real root-keys */ |
| 811 | case HKEY_LOCAL_MACHINE: |
| 812 | { |
| 813 | static const WCHAR name[] = { 'M','A','C','H','I','N','E',0 }; |
| 814 | key = alloc_key( name, time(NULL) ); |
| 815 | } |
| 816 | break; |
| 817 | case HKEY_USERS: |
| 818 | { |
| 819 | static const WCHAR name[] = { 'U','S','E','R',0 }; |
| 820 | key = alloc_key( name, time(NULL) ); |
| 821 | } |
| 822 | break; |
| 823 | /* special subkeys */ |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 824 | case HKEY_CLASSES_ROOT: |
| 825 | { |
| 826 | static const WCHAR name[] = |
| 827 | { 'S','O','F','T','W','A','R','E','\\','C','l','a','s','s','e','s',0 }; |
| 828 | |
| 829 | struct key *root = get_hkey_obj( HKEY_LOCAL_MACHINE, 0 ); |
Juergen Schmied | 788d8d5 | 2000-01-27 05:37:54 +0000 | [diff] [blame] | 830 | assert( root ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 831 | key = create_key( root, name, sizeof(name), NULL, 0, time(NULL), &dummy ); |
| 832 | release_object( root ); |
| 833 | } |
| 834 | break; |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 835 | case HKEY_CURRENT_CONFIG: |
| 836 | { |
| 837 | static const WCHAR name[] = { |
| 838 | 'S','Y','S','T','E','M','\\', |
| 839 | 'C','U','R','R','E','N','T','C','O','N','T','R','O','L','S','E','T','\\', |
| 840 | 'H','A','R','D','W','A','R','E','P','R','O','F','I','L','E','S','\\', |
| 841 | 'C','U','R','R','E','N','T',0}; |
| 842 | struct key *root = get_hkey_obj( HKEY_LOCAL_MACHINE, 0 ); |
Juergen Schmied | 788d8d5 | 2000-01-27 05:37:54 +0000 | [diff] [blame] | 843 | assert( root ); |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 844 | key = create_key( root, name, sizeof(name), NULL, 0, time(NULL), &dummy ); |
| 845 | release_object( root ); |
| 846 | } |
| 847 | break; |
| 848 | case HKEY_CURRENT_USER: |
| 849 | { |
Juergen Schmied | 788d8d5 | 2000-01-27 05:37:54 +0000 | [diff] [blame] | 850 | /* get the current user name */ |
| 851 | int i, len; |
| 852 | WCHAR *name; |
| 853 | char buffer[10]; |
| 854 | const char *p; |
| 855 | struct passwd *pwd = getpwuid( getuid() ); |
| 856 | |
| 857 | if (pwd) p = pwd->pw_name; |
| 858 | else |
| 859 | { |
Patrik Stridvall | 7e9913f | 2000-03-08 18:25:22 +0000 | [diff] [blame] | 860 | sprintf( buffer, "%ld", (long) getuid() ); |
Juergen Schmied | 788d8d5 | 2000-01-27 05:37:54 +0000 | [diff] [blame] | 861 | p = buffer; |
| 862 | } |
| 863 | len = strlen(p); |
| 864 | if ((name = mem_alloc( (len+1) * sizeof(WCHAR) ))) |
| 865 | { |
| 866 | struct key *root = get_hkey_obj( HKEY_USERS, 0 ); |
| 867 | assert( root ); |
| 868 | for (i = 0; i <= len; i++) name[i] = p[i]; |
| 869 | key = create_key( root, name, (len+1) * sizeof(WCHAR), |
| 870 | NULL, 0, time(NULL), &dummy ); |
| 871 | release_object( root ); |
| 872 | free( name ); |
| 873 | } |
| 874 | else key = NULL; |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 875 | } |
| 876 | break; |
| 877 | /* dynamically generated keys */ |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 878 | case HKEY_PERFORMANCE_DATA: |
| 879 | case HKEY_DYN_DATA: |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 880 | key = alloc_key( NULL, time(NULL) ); |
| 881 | break; |
| 882 | default: |
| 883 | key = NULL; |
| 884 | assert(0); |
| 885 | } |
| 886 | if (key) |
| 887 | { |
| 888 | root_keys[hkey - HKEY_ROOT_FIRST] = key; |
| 889 | key->flags |= KEY_ROOT; |
| 890 | } |
| 891 | return key; |
| 892 | } |
| 893 | |
| 894 | /* close the top-level keys; used on server exit */ |
| 895 | void close_registry(void) |
| 896 | { |
| 897 | int i; |
| 898 | for (i = 0; i < NB_ROOT_KEYS; i++) |
| 899 | { |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 900 | if (root_keys[i]) release_object( root_keys[i] ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | |
| 904 | /* get the registry key corresponding to an hkey handle */ |
| 905 | static struct key *get_hkey_obj( int hkey, unsigned int access ) |
| 906 | { |
| 907 | struct key *key; |
| 908 | |
| 909 | if (IS_ROOT_HKEY(hkey)) |
| 910 | { |
| 911 | if (!(key = root_keys[hkey - HKEY_ROOT_FIRST])) key = create_root_key( hkey ); |
| 912 | grab_object( key ); |
| 913 | } |
| 914 | else |
| 915 | key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops ); |
| 916 | return key; |
| 917 | } |
| 918 | |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 919 | /* read a line from the input file */ |
| 920 | static int read_next_line( struct file_load_info *info ) |
| 921 | { |
| 922 | char *newbuf; |
| 923 | int newlen, pos = 0; |
| 924 | |
| 925 | info->line++; |
| 926 | for (;;) |
| 927 | { |
| 928 | if (!fgets( info->buffer + pos, info->len - pos, info->file )) |
| 929 | return (pos != 0); /* EOF */ |
| 930 | pos = strlen(info->buffer); |
| 931 | if (info->buffer[pos-1] == '\n') |
| 932 | { |
| 933 | /* got a full line */ |
| 934 | info->buffer[--pos] = 0; |
| 935 | if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0; |
| 936 | return 1; |
| 937 | } |
| 938 | if (pos < info->len - 1) return 1; /* EOF but something was read */ |
| 939 | |
| 940 | /* need to enlarge the buffer */ |
| 941 | newlen = info->len + info->len / 2; |
| 942 | if (!(newbuf = realloc( info->buffer, newlen ))) |
| 943 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 944 | set_error( STATUS_NO_MEMORY ); |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 945 | return -1; |
| 946 | } |
| 947 | info->buffer = newbuf; |
| 948 | info->len = newlen; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | /* make sure the temp buffer holds enough space */ |
| 953 | static int get_file_tmp_space( struct file_load_info *info, int size ) |
| 954 | { |
| 955 | char *tmp; |
| 956 | if (info->tmplen >= size) return 1; |
| 957 | if (!(tmp = realloc( info->tmp, size ))) |
| 958 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 959 | set_error( STATUS_NO_MEMORY ); |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 960 | return 0; |
| 961 | } |
| 962 | info->tmp = tmp; |
| 963 | info->tmplen = size; |
| 964 | return 1; |
| 965 | } |
| 966 | |
| 967 | /* report an error while loading an input file */ |
| 968 | static void file_read_error( const char *err, struct file_load_info *info ) |
| 969 | { |
| 970 | fprintf( stderr, "Line %d: %s '%s'\n", info->line, err, info->buffer ); |
| 971 | } |
| 972 | |
| 973 | /* parse an escaped string back into Unicode */ |
| 974 | /* return the number of chars read from the input, or -1 on output overflow */ |
| 975 | static int parse_strW( WCHAR *dest, int *len, const char *src, char endchar ) |
| 976 | { |
| 977 | int count = sizeof(WCHAR); /* for terminating null */ |
| 978 | const char *p = src; |
| 979 | while (*p && *p != endchar) |
| 980 | { |
| 981 | if (*p != '\\') *dest = (WCHAR)*p++; |
| 982 | else |
| 983 | { |
| 984 | p++; |
| 985 | switch(*p) |
| 986 | { |
| 987 | case 'a': *dest = '\a'; p++; break; |
| 988 | case 'b': *dest = '\b'; p++; break; |
| 989 | case 'e': *dest = '\e'; p++; break; |
| 990 | case 'f': *dest = '\f'; p++; break; |
| 991 | case 'n': *dest = '\n'; p++; break; |
| 992 | case 'r': *dest = '\r'; p++; break; |
| 993 | case 't': *dest = '\t'; p++; break; |
| 994 | case 'v': *dest = '\v'; p++; break; |
| 995 | case 'x': /* hex escape */ |
| 996 | p++; |
| 997 | if (!isxdigit(*p)) *dest = 'x'; |
| 998 | else |
| 999 | { |
| 1000 | *dest = to_hex(*p++); |
| 1001 | if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++); |
| 1002 | if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++); |
| 1003 | if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++); |
| 1004 | } |
| 1005 | break; |
| 1006 | case '0': |
| 1007 | case '1': |
| 1008 | case '2': |
| 1009 | case '3': |
| 1010 | case '4': |
| 1011 | case '5': |
| 1012 | case '6': |
| 1013 | case '7': /* octal escape */ |
| 1014 | *dest = *p++ - '0'; |
| 1015 | if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0'); |
| 1016 | if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0'); |
| 1017 | break; |
| 1018 | default: |
| 1019 | *dest = (WCHAR)*p++; |
| 1020 | break; |
| 1021 | } |
| 1022 | } |
| 1023 | if ((count += sizeof(WCHAR)) > *len) return -1; /* dest buffer overflow */ |
| 1024 | dest++; |
| 1025 | } |
| 1026 | *dest = 0; |
| 1027 | if (!*p) return -1; /* delimiter not found */ |
| 1028 | *len = count; |
| 1029 | return p + 1 - src; |
| 1030 | } |
| 1031 | |
| 1032 | /* convert a data type tag to a value type */ |
| 1033 | static int get_data_type( const char *buffer, int *type, int *parse_type ) |
| 1034 | { |
| 1035 | struct data_type { const char *tag; int len; int type; int parse_type; }; |
| 1036 | |
| 1037 | static const struct data_type data_types[] = |
| 1038 | { /* actual type */ /* type to assume for parsing */ |
| 1039 | { "\"", 1, REG_SZ, REG_SZ }, |
| 1040 | { "str:\"", 5, REG_SZ, REG_SZ }, |
| 1041 | { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ }, |
| 1042 | { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ }, |
| 1043 | { "hex:", 4, REG_BINARY, REG_BINARY }, |
| 1044 | { "dword:", 6, REG_DWORD, REG_DWORD }, |
| 1045 | { "hex(", 4, -1, REG_BINARY }, |
| 1046 | { NULL, } |
| 1047 | }; |
| 1048 | |
| 1049 | const struct data_type *ptr; |
| 1050 | char *end; |
| 1051 | |
| 1052 | for (ptr = data_types; ptr->tag; ptr++) |
| 1053 | { |
| 1054 | if (memcmp( ptr->tag, buffer, ptr->len )) continue; |
| 1055 | *parse_type = ptr->parse_type; |
| 1056 | if ((*type = ptr->type) != -1) return ptr->len; |
| 1057 | /* "hex(xx):" is special */ |
| 1058 | *type = (int)strtoul( buffer + 4, &end, 16 ); |
| 1059 | if ((end <= buffer) || memcmp( end, "):", 2 )) return 0; |
| 1060 | return end + 2 - buffer; |
| 1061 | } |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | /* load and create a key from the input file */ |
| 1066 | static struct key *load_key( struct key *base, const char *buffer, struct file_load_info *info ) |
| 1067 | { |
| 1068 | WCHAR *p; |
| 1069 | int res, len, modif; |
| 1070 | |
| 1071 | len = strlen(buffer) * sizeof(WCHAR); |
| 1072 | if (!get_file_tmp_space( info, len )) return NULL; |
| 1073 | |
| 1074 | if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, ']' )) == -1) |
| 1075 | { |
| 1076 | file_read_error( "Malformed key", info ); |
| 1077 | return NULL; |
| 1078 | } |
| 1079 | if (!sscanf( buffer + res, " %d", &modif )) modif = time(NULL); |
| 1080 | |
| 1081 | for (p = (WCHAR *)info->tmp; *p; p++) if (*p == '\\') { p++; break; } |
| 1082 | return create_key( base, p, len - ((char *)p - info->tmp), NULL, 0, modif, &res ); |
| 1083 | } |
| 1084 | |
| 1085 | /* parse a comma-separated list of hex digits */ |
| 1086 | static int parse_hex( unsigned char *dest, int *len, const char *buffer ) |
| 1087 | { |
| 1088 | const char *p = buffer; |
| 1089 | int count = 0; |
| 1090 | while (isxdigit(*p)) |
| 1091 | { |
| 1092 | int val; |
| 1093 | char buf[3]; |
| 1094 | memcpy( buf, p, 2 ); |
| 1095 | buf[2] = 0; |
| 1096 | sscanf( buf, "%x", &val ); |
| 1097 | if (count++ >= *len) return -1; /* dest buffer overflow */ |
| 1098 | *dest++ = (unsigned char )val; |
| 1099 | p += 2; |
| 1100 | if (*p == ',') p++; |
| 1101 | } |
| 1102 | *len = count; |
| 1103 | return p - buffer; |
| 1104 | } |
| 1105 | |
| 1106 | /* parse a value name and create the corresponding value */ |
| 1107 | static struct key_value *parse_value_name( struct key *key, const char *buffer, int *len, |
| 1108 | struct file_load_info *info ) |
| 1109 | { |
| 1110 | int maxlen = strlen(buffer) * sizeof(WCHAR); |
| 1111 | if (!get_file_tmp_space( info, maxlen )) return NULL; |
| 1112 | if (buffer[0] == '@') |
| 1113 | { |
| 1114 | info->tmp[0] = info->tmp[1] = 0; |
| 1115 | *len = 1; |
| 1116 | } |
| 1117 | else |
| 1118 | { |
| 1119 | if ((*len = parse_strW( (WCHAR *)info->tmp, &maxlen, buffer + 1, '\"' )) == -1) goto error; |
| 1120 | (*len)++; /* for initial quote */ |
| 1121 | } |
| 1122 | if (buffer[*len] != '=') goto error; |
| 1123 | (*len)++; |
| 1124 | return insert_value( key, (WCHAR *)info->tmp ); |
| 1125 | |
| 1126 | error: |
| 1127 | file_read_error( "Malformed value name", info ); |
| 1128 | return NULL; |
| 1129 | } |
| 1130 | |
| 1131 | /* load a value from the input file */ |
| 1132 | static int load_value( struct key *key, const char *buffer, struct file_load_info *info ) |
| 1133 | { |
| 1134 | DWORD dw; |
| 1135 | void *ptr, *newptr; |
| 1136 | int maxlen, len, res; |
| 1137 | int type, parse_type; |
| 1138 | struct key_value *value; |
| 1139 | |
| 1140 | if (!(value = parse_value_name( key, buffer, &len, info ))) return 0; |
| 1141 | if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error; |
| 1142 | buffer += len + res; |
| 1143 | |
| 1144 | switch(parse_type) |
| 1145 | { |
| 1146 | case REG_SZ: |
| 1147 | len = strlen(buffer) * sizeof(WCHAR); |
| 1148 | if (!get_file_tmp_space( info, len )) return 0; |
| 1149 | if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, '\"' )) == -1) goto error; |
| 1150 | ptr = info->tmp; |
| 1151 | break; |
| 1152 | case REG_DWORD: |
| 1153 | dw = strtoul( buffer, NULL, 16 ); |
| 1154 | ptr = &dw; |
| 1155 | len = sizeof(dw); |
| 1156 | break; |
| 1157 | case REG_BINARY: /* hex digits */ |
| 1158 | len = 0; |
| 1159 | for (;;) |
| 1160 | { |
| 1161 | maxlen = 1 + strlen(buffer)/3; /* 3 chars for one hex byte */ |
| 1162 | if (!get_file_tmp_space( info, len + maxlen )) return 0; |
| 1163 | if ((res = parse_hex( info->tmp + len, &maxlen, buffer )) == -1) goto error; |
| 1164 | len += maxlen; |
| 1165 | buffer += res; |
| 1166 | while (isspace(*buffer)) buffer++; |
| 1167 | if (!*buffer) break; |
| 1168 | if (*buffer != '\\') goto error; |
| 1169 | if (read_next_line( info) != 1) goto error; |
| 1170 | buffer = info->buffer; |
| 1171 | while (isspace(*buffer)) buffer++; |
| 1172 | } |
| 1173 | ptr = info->tmp; |
| 1174 | break; |
| 1175 | default: |
| 1176 | assert(0); |
| 1177 | ptr = NULL; /* keep compiler quiet */ |
| 1178 | break; |
| 1179 | } |
| 1180 | |
| 1181 | if (!len) newptr = NULL; |
| 1182 | else if (!(newptr = memdup( ptr, len ))) return 0; |
| 1183 | |
| 1184 | if (value->data) free( value->data ); |
| 1185 | value->data = newptr; |
| 1186 | value->len = len; |
| 1187 | value->type = type; |
| 1188 | /* update the key level but not the modification time */ |
| 1189 | key->level = MAX( key->level, current_level ); |
| 1190 | return 1; |
| 1191 | |
| 1192 | error: |
| 1193 | file_read_error( "Malformed value", info ); |
| 1194 | return 0; |
| 1195 | } |
| 1196 | |
| 1197 | /* load all the keys from the input file */ |
| 1198 | static void load_keys( struct key *key, FILE *f ) |
| 1199 | { |
| 1200 | struct key *subkey = NULL; |
| 1201 | struct file_load_info info; |
| 1202 | char *p; |
| 1203 | |
| 1204 | info.file = f; |
| 1205 | info.len = 4; |
| 1206 | info.tmplen = 4; |
| 1207 | info.line = 0; |
| 1208 | if (!(info.buffer = mem_alloc( info.len ))) return; |
| 1209 | if (!(info.tmp = mem_alloc( info.tmplen ))) |
| 1210 | { |
| 1211 | free( info.buffer ); |
| 1212 | return; |
| 1213 | } |
| 1214 | |
| 1215 | if ((read_next_line( &info ) != 1) || |
| 1216 | strcmp( info.buffer, "WINE REGISTRY Version 2" )) |
| 1217 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 1218 | set_error( STATUS_NOT_REGISTRY_FILE ); |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1219 | goto done; |
| 1220 | } |
| 1221 | |
| 1222 | while (read_next_line( &info ) == 1) |
| 1223 | { |
| 1224 | for (p = info.buffer; *p && isspace(*p); p++); |
| 1225 | switch(*p) |
| 1226 | { |
| 1227 | case '[': /* new key */ |
| 1228 | if (subkey) release_object( subkey ); |
| 1229 | subkey = load_key( key, p + 1, &info ); |
| 1230 | break; |
| 1231 | case '@': /* default value */ |
| 1232 | case '\"': /* value */ |
| 1233 | if (subkey) load_value( subkey, p, &info ); |
| 1234 | else file_read_error( "Value without key", &info ); |
| 1235 | break; |
| 1236 | case '#': /* comment */ |
| 1237 | case ';': /* comment */ |
| 1238 | case 0: /* empty line */ |
| 1239 | break; |
| 1240 | default: |
| 1241 | file_read_error( "Unrecognized input", &info ); |
| 1242 | break; |
| 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | done: |
| 1247 | if (subkey) release_object( subkey ); |
| 1248 | free( info.buffer ); |
| 1249 | free( info.tmp ); |
| 1250 | } |
| 1251 | |
| 1252 | /* load a part of the registry from a file */ |
| 1253 | static void load_registry( struct key *key, int handle ) |
| 1254 | { |
| 1255 | struct object *obj; |
| 1256 | int fd; |
| 1257 | |
| 1258 | if (!(obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL ))) return; |
| 1259 | fd = obj->ops->get_read_fd( obj ); |
| 1260 | release_object( obj ); |
| 1261 | if (fd != -1) |
| 1262 | { |
| 1263 | FILE *f = fdopen( fd, "r" ); |
| 1264 | if (f) |
| 1265 | { |
| 1266 | load_keys( key, f ); |
| 1267 | fclose( f ); |
| 1268 | } |
| 1269 | else file_set_error(); |
| 1270 | } |
| 1271 | } |
| 1272 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1273 | /* update the level of the parents of a key (only needed for the old format) */ |
| 1274 | static int update_level( struct key *key ) |
| 1275 | { |
| 1276 | int i; |
| 1277 | int max = key->level; |
| 1278 | for (i = 0; i <= key->last_subkey; i++) |
| 1279 | { |
| 1280 | int sub = update_level( key->subkeys[i] ); |
| 1281 | if (sub > max) max = sub; |
| 1282 | } |
| 1283 | key->level = max; |
| 1284 | return max; |
| 1285 | } |
| 1286 | |
| 1287 | /* dump a string to a registry save file in the old v1 format */ |
Huw D M Davies | 361adc8 | 2000-03-24 19:46:30 +0000 | [diff] [blame^] | 1288 | static void save_string_v1( LPCWSTR str, FILE *f, int len ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1289 | { |
| 1290 | if (!str) return; |
Huw D M Davies | 361adc8 | 2000-03-24 19:46:30 +0000 | [diff] [blame^] | 1291 | while ((len == -1) ? *str : (*str && len--)) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1292 | { |
| 1293 | if ((*str > 0x7f) || (*str == '\n') || (*str == '=')) |
| 1294 | fprintf( f, "\\u%04x", *str ); |
| 1295 | else |
| 1296 | { |
| 1297 | if (*str == '\\') fputc( '\\', f ); |
| 1298 | fputc( (char)*str, f ); |
| 1299 | } |
| 1300 | str++; |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | /* save a registry and all its subkeys to a text file in the old v1 format */ |
| 1305 | static void save_subkeys_v1( struct key *key, int nesting, FILE *f ) |
| 1306 | { |
| 1307 | int i, j; |
| 1308 | |
| 1309 | if (key->flags & KEY_VOLATILE) return; |
| 1310 | if (key->level < saving_level) return; |
| 1311 | for (i = 0; i <= key->last_value; i++) |
| 1312 | { |
| 1313 | struct key_value *value = &key->values[i]; |
| 1314 | for (j = nesting; j > 0; j --) fputc( '\t', f ); |
Huw D M Davies | 361adc8 | 2000-03-24 19:46:30 +0000 | [diff] [blame^] | 1315 | save_string_v1( value->name, f, -1 ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1316 | fprintf( f, "=%d,%d,", value->type, 0 ); |
| 1317 | if (value->type == REG_SZ || value->type == REG_EXPAND_SZ) |
Huw D M Davies | 361adc8 | 2000-03-24 19:46:30 +0000 | [diff] [blame^] | 1318 | save_string_v1( (LPWSTR)value->data, f, value->len / 2 ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1319 | else |
| 1320 | for (j = 0; j < value->len; j++) |
| 1321 | fprintf( f, "%02x", *((unsigned char *)value->data + j) ); |
| 1322 | fputc( '\n', f ); |
| 1323 | } |
| 1324 | for (i = 0; i <= key->last_subkey; i++) |
| 1325 | { |
| 1326 | for (j = nesting; j > 0; j --) fputc( '\t', f ); |
Huw D M Davies | 361adc8 | 2000-03-24 19:46:30 +0000 | [diff] [blame^] | 1327 | save_string_v1( key->subkeys[i]->name, f, -1 ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1328 | fputc( '\n', f ); |
| 1329 | save_subkeys_v1( key->subkeys[i], nesting + 1, f ); |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | /* save a registry branch to a file handle */ |
| 1334 | static void save_registry( struct key *key, int handle ) |
| 1335 | { |
| 1336 | struct object *obj; |
| 1337 | int fd; |
| 1338 | |
| 1339 | if (key->flags & KEY_DELETED) |
| 1340 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 1341 | set_error( STATUS_KEY_DELETED ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1342 | return; |
| 1343 | } |
| 1344 | if (!(obj = get_handle_obj( current->process, handle, GENERIC_WRITE, NULL ))) return; |
| 1345 | fd = obj->ops->get_write_fd( obj ); |
| 1346 | release_object( obj ); |
| 1347 | if (fd != -1) |
| 1348 | { |
| 1349 | FILE *f = fdopen( fd, "w" ); |
| 1350 | if (f) |
| 1351 | { |
| 1352 | fprintf( f, "WINE REGISTRY Version %d\n", saving_version ); |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 1353 | if (saving_version == 2) save_subkeys( key, key, f ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1354 | else |
| 1355 | { |
| 1356 | update_level( key ); |
| 1357 | save_subkeys_v1( key, 0, f ); |
| 1358 | } |
| 1359 | if (fclose( f )) file_set_error(); |
| 1360 | } |
| 1361 | else |
| 1362 | { |
| 1363 | file_set_error(); |
| 1364 | close( fd ); |
| 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | /* create a registry key */ |
| 1370 | DECL_HANDLER(create_key) |
| 1371 | { |
| 1372 | struct key *key, *parent; |
| 1373 | WCHAR *class; |
| 1374 | unsigned int access = req->access; |
| 1375 | |
| 1376 | if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */ |
| 1377 | req->hkey = -1; |
| 1378 | if ((parent = get_hkey_obj( req->parent, KEY_CREATE_SUB_KEY ))) |
| 1379 | { |
| 1380 | if ((class = req_strdupW( req->class ))) |
| 1381 | { |
| 1382 | if ((key = create_key( parent, req->name, sizeof(req->name), class, req->options, |
| 1383 | req->modif, &req->created ))) |
| 1384 | { |
| 1385 | req->hkey = alloc_handle( current->process, key, access, 0 ); |
| 1386 | release_object( key ); |
| 1387 | } |
| 1388 | free( class ); |
| 1389 | } |
| 1390 | release_object( parent ); |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | /* open a registry key */ |
| 1395 | DECL_HANDLER(open_key) |
| 1396 | { |
| 1397 | struct key *key, *parent; |
| 1398 | unsigned int access = req->access; |
| 1399 | |
| 1400 | if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */ |
| 1401 | req->hkey = -1; |
| 1402 | if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ ))) |
| 1403 | { |
| 1404 | if ((key = open_key( parent, req->name, sizeof(req->name) ))) |
| 1405 | { |
| 1406 | req->hkey = alloc_handle( current->process, key, access, 0 ); |
| 1407 | release_object( key ); |
| 1408 | } |
| 1409 | release_object( parent ); |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | /* delete a registry key */ |
| 1414 | DECL_HANDLER(delete_key) |
| 1415 | { |
| 1416 | struct key *key; |
| 1417 | |
| 1418 | if ((key = get_hkey_obj( req->hkey, KEY_CREATE_SUB_KEY /*FIXME*/ ))) |
| 1419 | { |
| 1420 | delete_key( key, req->name, sizeof(req->name) ); |
| 1421 | release_object( key ); |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | /* close a registry key */ |
| 1426 | DECL_HANDLER(close_key) |
| 1427 | { |
| 1428 | int hkey = req->hkey; |
| 1429 | /* ignore attempts to close a root key */ |
| 1430 | if (!IS_ROOT_HKEY(hkey)) close_handle( current->process, hkey ); |
| 1431 | } |
| 1432 | |
| 1433 | /* enumerate registry subkeys */ |
| 1434 | DECL_HANDLER(enum_key) |
| 1435 | { |
| 1436 | struct key *key; |
| 1437 | |
| 1438 | if ((key = get_hkey_obj( req->hkey, KEY_ENUMERATE_SUB_KEYS ))) |
| 1439 | { |
| 1440 | enum_key( key, req->index, req->name, req->class, &req->modif ); |
| 1441 | release_object( key ); |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | /* query information about a registry key */ |
| 1446 | DECL_HANDLER(query_key_info) |
| 1447 | { |
| 1448 | struct key *key; |
| 1449 | |
| 1450 | if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE ))) |
| 1451 | { |
| 1452 | query_key( key, req ); |
| 1453 | release_object( key ); |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | /* set a value of a registry key */ |
| 1458 | DECL_HANDLER(set_key_value) |
| 1459 | { |
| 1460 | struct key *key; |
| 1461 | int max = get_req_size( req->data, sizeof(req->data[0]) ); |
| 1462 | int datalen = req->len; |
| 1463 | if (datalen > max) |
| 1464 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 1465 | set_error( STATUS_NO_MEMORY ); /* FIXME */ |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1466 | return; |
| 1467 | } |
| 1468 | if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE ))) |
| 1469 | { |
| 1470 | set_value( key, copy_path( req->name ), req->type, datalen, req->data ); |
| 1471 | release_object( key ); |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | /* retrieve the value of a registry key */ |
| 1476 | DECL_HANDLER(get_key_value) |
| 1477 | { |
| 1478 | struct key *key; |
| 1479 | |
| 1480 | if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE ))) |
| 1481 | { |
| 1482 | get_value( key, copy_path( req->name ), &req->type, &req->len, req->data ); |
| 1483 | release_object( key ); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | /* enumerate the value of a registry key */ |
| 1488 | DECL_HANDLER(enum_key_value) |
| 1489 | { |
| 1490 | struct key *key; |
| 1491 | |
| 1492 | if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE ))) |
| 1493 | { |
| 1494 | enum_value( key, req->index, req->name, &req->type, &req->len, req->data ); |
| 1495 | release_object( key ); |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | /* delete a value of a registry key */ |
| 1500 | DECL_HANDLER(delete_key_value) |
| 1501 | { |
| 1502 | WCHAR *name; |
| 1503 | struct key *key; |
| 1504 | |
| 1505 | if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE ))) |
| 1506 | { |
| 1507 | if ((name = req_strdupW( req->name ))) |
| 1508 | { |
| 1509 | delete_value( key, name ); |
| 1510 | free( name ); |
| 1511 | } |
| 1512 | release_object( key ); |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | /* load a registry branch from a file */ |
| 1517 | DECL_HANDLER(load_registry) |
| 1518 | { |
| 1519 | struct key *key; |
| 1520 | |
| 1521 | if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE | KEY_CREATE_SUB_KEY ))) |
| 1522 | { |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1523 | /* FIXME: use subkey name */ |
| 1524 | load_registry( key, req->file ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1525 | release_object( key ); |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | /* save a registry branch to a file */ |
| 1530 | DECL_HANDLER(save_registry) |
| 1531 | { |
| 1532 | struct key *key; |
| 1533 | |
| 1534 | if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS ))) |
| 1535 | { |
| 1536 | save_registry( key, req->file ); |
| 1537 | release_object( key ); |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | /* set the current and saving level for the registry */ |
| 1542 | DECL_HANDLER(set_registry_levels) |
| 1543 | { |
| 1544 | current_level = req->current; |
| 1545 | saving_level = req->saving; |
Alexandre Julliard | 705686e | 1999-11-24 19:34:32 +0000 | [diff] [blame] | 1546 | saving_version = req->version; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |