Store all object names as Unicode in the server.
[wine/multimedia.git] / server / registry.c
blob854e647097fb6b9ed3bb43fff30bd9afac32cced
1 /*
2 * Server-side registry management
4 * Copyright (C) 1999 Alexandre Julliard
5 */
7 /* To do:
8 * - behavior with deleted keys
9 * - values larger than request buffer
10 * - symbolic links
13 #include <assert.h>
14 #include <limits.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include "object.h"
19 #include "handle.h"
20 #include "request.h"
21 #include "unicode.h"
23 #include "winbase.h"
24 #include "winerror.h"
25 #include "winreg.h"
28 /* a registry key */
29 struct key
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 */
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 */
51 /* a key value */
52 struct key_value
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 */
60 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
61 #define MIN_VALUES 8 /* min. number of allocated values per key */
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];
71 static const char * const root_key_names[NB_ROOT_KEYS] =
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"
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;
89 static int saving_version = 1; /* file format version */
92 /* information about a file being loaded */
93 struct file_load_info
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 */
104 static void key_dump( struct object *obj, int verbose );
105 static void key_destroy( struct object *obj );
107 static const struct object_ops key_ops =
109 sizeof(struct key),
110 key_dump,
111 no_add_queue,
112 NULL, /* should never get called */
113 NULL, /* should never get called */
114 NULL, /* should never get called */
115 no_read_fd,
116 no_write_fd,
117 no_flush,
118 no_get_file_info,
119 key_destroy
124 * The registry text file format v2 used by this code is similar to the one
125 * used by REGEDIT import/export functionality, with the following differences:
126 * - strings and key names can contain \x escapes for Unicode
127 * - key names use escapes too in order to support Unicode
128 * - the modification time optionally follows the key name
129 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
132 static inline char to_hex( char ch )
134 if (isdigit(ch)) return ch - '0';
135 return tolower(ch) - 'a' + 10;
138 /* dump the full path of a key */
139 static void dump_path( struct key *key, FILE *f )
141 if (key->parent) dump_path( key->parent, f );
142 else if (key->name) fprintf( f, "?????" );
144 if (key->name)
146 fprintf( f, "\\\\" );
147 dump_strW( key->name, strlenW(key->name), f, "[]" );
149 else /* root key */
151 int i;
152 for (i = 0; i < NB_ROOT_KEYS; i++)
153 if (root_keys[i] == key) fprintf( f, "%s", root_key_names[i] );
157 /* dump a value to a text file */
158 static void dump_value( struct key_value *value, FILE *f )
160 int i, count;
162 if (value->name[0])
164 fputc( '\"', f );
165 count = 1 + dump_strW( value->name, strlenW(value->name), f, "\"\"" );
166 count += fprintf( f, "\"=" );
168 else count = fprintf( f, "@=" );
170 switch(value->type)
172 case REG_SZ:
173 case REG_EXPAND_SZ:
174 case REG_MULTI_SZ:
175 if (value->type != REG_SZ) fprintf( f, "str(%d):", value->type );
176 fputc( '\"', f );
177 if (value->data) dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
178 fputc( '\"', f );
179 break;
180 case REG_DWORD:
181 if (value->len == sizeof(DWORD))
183 DWORD dw;
184 memcpy( &dw, value->data, sizeof(DWORD) );
185 fprintf( f, "dword:%08lx", dw );
186 break;
188 /* else fall through */
189 default:
190 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
191 else count += fprintf( f, "hex(%x):", value->type );
192 for (i = 0; i < value->len; i++)
194 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
195 if (i < value->len-1)
197 fputc( ',', f );
198 if (++count > 76)
200 fprintf( f, "\\\n " );
201 count = 2;
205 break;
207 fputc( '\n', f );
210 /* save a registry and all its subkeys to a text file */
211 static void save_subkeys( struct key *key, FILE *f )
213 int i;
215 if (key->flags & KEY_VOLATILE) return;
216 /* save key if it has the proper level, and has either some values or no subkeys */
217 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
218 if ((key->level >= saving_level) && ((key->last_value >= 0) || (key->last_subkey == -1)))
220 fprintf( f, "\n[" );
221 dump_path( key, f );
222 fprintf( f, "] %ld\n", key->modif );
223 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
225 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], f );
228 static void dump_operation( struct key *key, struct key_value *value, const char *op )
230 fprintf( stderr, "%s key ", op );
231 if (key) dump_path( key, stderr );
232 else fprintf( stderr, "ERROR" );
233 if (value)
235 fprintf( stderr, " value ");
236 dump_value( value, stderr );
238 else fprintf( stderr, "\n" );
241 static void key_dump( struct object *obj, int verbose )
243 struct key *key = (struct key *)obj;
244 assert( obj->ops == &key_ops );
245 fprintf( stderr, "Key flags=%x ", key->flags );
246 dump_path( key, stderr );
247 fprintf( stderr, "\n" );
250 static void key_destroy( struct object *obj )
252 int i;
253 struct key *key = (struct key *)obj;
254 assert( obj->ops == &key_ops );
256 free( key->name );
257 if (key->class) free( key->class );
258 for (i = 0; i <= key->last_value; i++)
260 free( key->values[i].name );
261 if (key->values[i].data) free( key->values[i].data );
263 for (i = 0; i <= key->last_subkey; i++)
265 key->subkeys[i]->parent = NULL;
266 release_object( key->subkeys[i] );
270 /* duplicate a key path from the request buffer */
271 /* returns a pointer to a static buffer, so only useable once per request */
272 static WCHAR *copy_path( const path_t path )
274 static WCHAR buffer[MAX_PATH+1];
275 WCHAR *p = buffer;
277 while (p < buffer + sizeof(buffer) - 1) if (!(*p++ = *path++)) break;
278 *p = 0;
279 return buffer;
282 /* return the next token in a given path */
283 /* returns a pointer to a static buffer, so only useable once per request */
284 static WCHAR *get_path_token( const WCHAR *initpath, size_t maxlen )
286 static const WCHAR *path;
287 static const WCHAR *end;
288 static WCHAR buffer[MAX_PATH+1];
289 WCHAR *p = buffer;
291 if (initpath)
293 path = initpath;
294 end = path + maxlen / sizeof(WCHAR);
296 while ((path < end) && (*path == '\\')) path++;
297 while ((path < end) && (p < buffer + sizeof(buffer) - 1))
299 WCHAR ch = *path;
300 if (!ch || (ch == '\\')) break;
301 *p++ = ch;
302 path++;
304 *p = 0;
305 return buffer;
308 /* duplicate a Unicode string from the request buffer */
309 static WCHAR *req_strdupW( const WCHAR *str )
311 WCHAR *name;
312 size_t len = get_req_strlenW( str );
313 if ((name = mem_alloc( (len + 1) * sizeof(WCHAR) )) != NULL)
315 memcpy( name, str, len * sizeof(WCHAR) );
316 name[len] = 0;
318 return name;
321 /* allocate a key object */
322 static struct key *alloc_key( const WCHAR *name, time_t modif )
324 struct key *key;
325 if ((key = (struct key *)alloc_object( &key_ops )))
327 key->name = NULL;
328 key->class = NULL;
329 key->flags = 0;
330 key->last_subkey = -1;
331 key->nb_subkeys = 0;
332 key->subkeys = NULL;
333 key->nb_values = 0;
334 key->last_value = -1;
335 key->values = NULL;
336 key->level = current_level;
337 key->modif = modif;
338 key->parent = NULL;
339 if (name && !(key->name = strdupW( name )))
341 release_object( key );
342 key = NULL;
345 return key;
348 /* update key modification time */
349 static void touch_key( struct key *key )
351 key->modif = time(NULL);
352 key->level = MAX( key->level, current_level );
355 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
356 static int grow_subkeys( struct key *key )
358 struct key **new_subkeys;
359 int nb_subkeys;
361 if (key->nb_subkeys)
363 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
364 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
366 set_error( ERROR_OUTOFMEMORY );
367 return 0;
370 else
372 nb_subkeys = MIN_VALUES;
373 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
375 key->subkeys = new_subkeys;
376 key->nb_subkeys = nb_subkeys;
377 return 1;
380 /* allocate a subkey for a given key, and return its index */
381 static struct key *alloc_subkey( struct key *parent, const WCHAR *name, int index, time_t modif )
383 struct key *key;
384 int i;
386 if (parent->last_subkey + 1 == parent->nb_subkeys)
388 /* need to grow the array */
389 if (!grow_subkeys( parent )) return NULL;
391 if ((key = alloc_key( name, modif )) != NULL)
393 key->parent = parent;
394 for (i = ++parent->last_subkey; i > index; i--)
395 parent->subkeys[i] = parent->subkeys[i-1];
396 parent->subkeys[index] = key;
398 return key;
401 /* free a subkey of a given key */
402 static void free_subkey( struct key *parent, int index )
404 struct key *key;
405 int i, nb_subkeys;
407 assert( index >= 0 );
408 assert( index <= parent->last_subkey );
410 key = parent->subkeys[index];
411 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
412 parent->last_subkey--;
413 key->flags |= KEY_DELETED;
414 key->parent = NULL;
415 release_object( key );
417 /* try to shrink the array */
418 nb_subkeys = key->nb_subkeys;
419 if (nb_subkeys > MIN_SUBKEYS && key->last_subkey < nb_subkeys / 2)
421 struct key **new_subkeys;
422 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
423 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
424 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
425 key->subkeys = new_subkeys;
426 key->nb_subkeys = nb_subkeys;
430 /* find the named child of a given key and return its index */
431 static struct key *find_subkey( struct key *key, const WCHAR *name, int *index )
433 int i, min, max, res;
435 min = 0;
436 max = key->last_subkey;
437 while (min <= max)
439 i = (min + max) / 2;
440 if (!(res = strcmpiW( key->subkeys[i]->name, name )))
442 *index = i;
443 return key->subkeys[i];
445 if (res > 0) max = i - 1;
446 else min = i + 1;
448 *index = min; /* this is where we should insert it */
449 return NULL;
452 /* open a subkey */
453 static struct key *open_key( struct key *key, const WCHAR *name, size_t maxlen )
455 int index;
456 WCHAR *path;
458 path = get_path_token( name, maxlen );
459 while (*path)
461 if (!(key = find_subkey( key, path, &index )))
463 set_error( ERROR_FILE_NOT_FOUND );
464 break;
466 path = get_path_token( NULL, 0 );
469 if (debug_level > 1) dump_operation( key, NULL, "Open" );
470 if (key) grab_object( key );
471 return key;
474 /* create a subkey */
475 static struct key *create_key( struct key *key, const WCHAR *name, size_t maxlen, WCHAR *class,
476 unsigned int options, time_t modif, int *created )
478 struct key *base;
479 int base_idx, index, flags = 0;
480 WCHAR *path;
482 if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
484 set_error( ERROR_KEY_DELETED );
485 return NULL;
487 if (options & REG_OPTION_VOLATILE) flags |= KEY_VOLATILE;
488 else if (key->flags & KEY_VOLATILE)
490 set_error( ERROR_CHILD_MUST_BE_VOLATILE );
491 return NULL;
494 path = get_path_token( name, maxlen );
495 *created = 0;
496 while (*path)
498 struct key *subkey;
499 if (!(subkey = find_subkey( key, path, &index ))) break;
500 key = subkey;
501 path = get_path_token( NULL, 0 );
504 /* create the remaining part */
506 if (!*path) goto done;
507 *created = 1;
508 base = key;
509 base_idx = index;
510 key = alloc_subkey( key, path, index, modif );
511 while (key)
513 key->flags |= flags;
514 path = get_path_token( NULL, 0 );
515 if (!*path) goto done;
516 /* we know the index is always 0 in a new key */
517 key = alloc_subkey( key, path, 0, modif );
519 if (base_idx != -1) free_subkey( base, base_idx );
520 return NULL;
522 done:
523 if (debug_level > 1) dump_operation( key, NULL, "Create" );
524 if (class) key->class = strdupW(class);
525 grab_object( key );
526 return key;
529 /* find a subkey of a given key by its index */
530 static void enum_key( struct key *parent, int index, WCHAR *name, WCHAR *class, time_t *modif )
532 struct key *key;
534 if ((index < 0) || (index > parent->last_subkey)) set_error( ERROR_NO_MORE_ITEMS );
535 else
537 key = parent->subkeys[index];
538 *modif = key->modif;
539 strcpyW( name, key->name );
540 if (key->class) strcpyW( class, key->class ); /* FIXME: length */
541 else *class = 0;
542 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
546 /* query information about a key */
547 static void query_key( struct key *key, struct query_key_info_request *req )
549 int i, len;
550 int max_subkey = 0, max_class = 0;
551 int max_value = 0, max_data = 0;
553 for (i = 0; i < key->last_subkey; i++)
555 struct key *subkey = key->subkeys[i];
556 len = strlenW( subkey->name );
557 if (len > max_subkey) max_subkey = len;
558 if (!subkey->class) continue;
559 len = strlenW( subkey->class );
560 if (len > max_class) max_class = len;
562 for (i = 0; i < key->last_value; i++)
564 len = strlenW( key->values[i].name );
565 if (len > max_value) max_value = len;
566 len = key->values[i].len;
567 if (len > max_data) max_data = len;
569 req->subkeys = key->last_subkey + 1;
570 req->max_subkey = max_subkey;
571 req->max_class = max_class;
572 req->values = key->last_value + 1;
573 req->max_value = max_value;
574 req->max_data = max_data;
575 req->modif = key->modif;
576 if (key->class) strcpyW( req->class, key->class ); /* FIXME: length */
577 else req->class[0] = 0;
578 if (debug_level > 1) dump_operation( key, NULL, "Query" );
581 /* delete a key and its values */
582 static void delete_key( struct key *key, const WCHAR *name, size_t maxlen )
584 int index;
585 struct key *parent;
586 WCHAR *path;
588 path = get_path_token( name, maxlen );
589 if (!*path)
591 /* deleting this key, must find parent and index */
592 if (key->flags & KEY_ROOT)
594 set_error( ERROR_ACCESS_DENIED );
595 return;
597 if (!(parent = key->parent) || (key->flags & KEY_DELETED))
599 set_error( ERROR_KEY_DELETED );
600 return;
602 for (index = 0; index <= parent->last_subkey; index++)
603 if (parent->subkeys[index] == key) break;
604 assert( index <= parent->last_subkey );
606 else while (*path)
608 parent = key;
609 if (!(key = find_subkey( parent, path, &index )))
611 set_error( ERROR_FILE_NOT_FOUND );
612 return;
614 path = get_path_token( NULL, 0 );
617 /* we can only delete a key that has no subkeys (FIXME) */
618 if ((key->flags & KEY_ROOT) || (key->last_subkey >= 0))
620 set_error( ERROR_ACCESS_DENIED );
621 return;
623 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
624 free_subkey( parent, index );
625 touch_key( parent );
628 /* try to grow the array of values; return 1 if OK, 0 on error */
629 static int grow_values( struct key *key )
631 struct key_value *new_val;
632 int nb_values;
634 if (key->nb_values)
636 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
637 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
639 set_error( ERROR_OUTOFMEMORY );
640 return 0;
643 else
645 nb_values = MIN_VALUES;
646 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
648 key->values = new_val;
649 key->nb_values = nb_values;
650 return 1;
653 /* find the named value of a given key and return its index in the array */
654 static struct key_value *find_value( const struct key *key, const WCHAR *name, int *index )
656 int i, min, max, res;
658 min = 0;
659 max = key->last_value;
660 while (min <= max)
662 i = (min + max) / 2;
663 if (!(res = strcmpiW( key->values[i].name, name )))
665 *index = i;
666 return &key->values[i];
668 if (res > 0) max = i - 1;
669 else min = i + 1;
671 *index = min; /* this is where we should insert it */
672 return NULL;
675 /* insert a new value or return a pointer to an existing one */
676 static struct key_value *insert_value( struct key *key, const WCHAR *name )
678 struct key_value *value;
679 WCHAR *new_name;
680 int i, index;
682 if (!(value = find_value( key, name, &index )))
684 /* not found, add it */
685 if (key->last_value + 1 == key->nb_values)
687 if (!grow_values( key )) return NULL;
689 if (!(new_name = strdupW(name))) return NULL;
690 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
691 value = &key->values[index];
692 value->name = new_name;
693 value->len = 0;
694 value->data = NULL;
696 return value;
699 /* set a key value */
700 static void set_value( struct key *key, WCHAR *name, int type, int datalen, void *data )
702 struct key_value *value;
703 void *ptr = NULL;
705 /* first copy the data */
706 if (datalen)
708 if (!(ptr = mem_alloc( datalen ))) return;
709 memcpy( ptr, data, datalen );
712 if (!(value = insert_value( key, name )))
714 if (ptr) free( ptr );
715 return;
717 if (value->data) free( value->data ); /* already existing, free previous data */
718 value->type = type;
719 value->len = datalen;
720 value->data = ptr;
721 touch_key( key );
722 if (debug_level > 1) dump_operation( key, value, "Set" );
725 /* get a key value */
726 static void get_value( struct key *key, WCHAR *name, int *type, int *len, void *data )
728 struct key_value *value;
729 int index;
731 if ((value = find_value( key, name, &index )))
733 *type = value->type;
734 *len = value->len;
735 if (value->data) memcpy( data, value->data, value->len );
736 if (debug_level > 1) dump_operation( key, value, "Get" );
738 else
740 *type = -1;
741 *len = 0;
742 set_error( ERROR_FILE_NOT_FOUND );
746 /* enumerate a key value */
747 static void enum_value( struct key *key, int i, WCHAR *name, int *type, int *len, void *data )
749 struct key_value *value;
751 if (i < 0 || i > key->last_value)
753 name[0] = 0;
754 *len = 0;
755 set_error( ERROR_NO_MORE_ITEMS );
757 else
759 value = &key->values[i];
760 strcpyW( name, value->name );
761 *type = value->type;
762 *len = value->len;
763 if (value->data) memcpy( data, value->data, value->len );
764 if (debug_level > 1) dump_operation( key, value, "Enum" );
768 /* delete a value */
769 static void delete_value( struct key *key, const WCHAR *name )
771 struct key_value *value;
772 int i, index, nb_values;
774 if (!(value = find_value( key, name, &index )))
776 set_error( ERROR_FILE_NOT_FOUND );
777 return;
779 if (debug_level > 1) dump_operation( key, value, "Delete" );
780 free( value->name );
781 if (value->data) free( value->data );
782 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
783 key->last_value--;
784 touch_key( key );
786 /* try to shrink the array */
787 nb_values = key->nb_values;
788 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
790 struct key_value *new_val;
791 nb_values -= nb_values / 3; /* shrink by 33% */
792 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
793 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
794 key->values = new_val;
795 key->nb_values = nb_values;
799 static struct key *get_hkey_obj( int hkey, unsigned int access );
801 static struct key *create_root_key( int hkey )
803 int dummy;
804 struct key *key;
806 switch(hkey)
808 case HKEY_CLASSES_ROOT:
810 static const WCHAR name[] =
811 { 'S','O','F','T','W','A','R','E','\\','C','l','a','s','s','e','s',0 };
813 struct key *root = get_hkey_obj( HKEY_LOCAL_MACHINE, 0 );
814 if (!root) return NULL;
815 key = create_key( root, name, sizeof(name), NULL, 0, time(NULL), &dummy );
816 release_object( root );
818 break;
819 case HKEY_CURRENT_USER: /* FIXME: should be HKEY_USERS\\the_current_user_SID */
820 case HKEY_LOCAL_MACHINE:
821 case HKEY_USERS:
822 case HKEY_PERFORMANCE_DATA:
823 case HKEY_DYN_DATA:
824 case HKEY_CURRENT_CONFIG:
825 key = alloc_key( NULL, time(NULL) );
826 break;
827 default:
828 key = NULL;
829 assert(0);
831 if (key)
833 root_keys[hkey - HKEY_ROOT_FIRST] = key;
834 key->flags |= KEY_ROOT;
836 return key;
839 /* close the top-level keys; used on server exit */
840 void close_registry(void)
842 int i;
843 for (i = 0; i < NB_ROOT_KEYS; i++)
845 if (root_keys[i]) release_object( root_keys[i] );
849 /* get the registry key corresponding to an hkey handle */
850 static struct key *get_hkey_obj( int hkey, unsigned int access )
852 struct key *key;
854 if (IS_ROOT_HKEY(hkey))
856 if (!(key = root_keys[hkey - HKEY_ROOT_FIRST])) key = create_root_key( hkey );
857 grab_object( key );
859 else
860 key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
861 return key;
864 /* read a line from the input file */
865 static int read_next_line( struct file_load_info *info )
867 char *newbuf;
868 int newlen, pos = 0;
870 info->line++;
871 for (;;)
873 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
874 return (pos != 0); /* EOF */
875 pos = strlen(info->buffer);
876 if (info->buffer[pos-1] == '\n')
878 /* got a full line */
879 info->buffer[--pos] = 0;
880 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
881 return 1;
883 if (pos < info->len - 1) return 1; /* EOF but something was read */
885 /* need to enlarge the buffer */
886 newlen = info->len + info->len / 2;
887 if (!(newbuf = realloc( info->buffer, newlen )))
889 set_error( ERROR_OUTOFMEMORY );
890 return -1;
892 info->buffer = newbuf;
893 info->len = newlen;
897 /* make sure the temp buffer holds enough space */
898 static int get_file_tmp_space( struct file_load_info *info, int size )
900 char *tmp;
901 if (info->tmplen >= size) return 1;
902 if (!(tmp = realloc( info->tmp, size )))
904 set_error( ERROR_OUTOFMEMORY );
905 return 0;
907 info->tmp = tmp;
908 info->tmplen = size;
909 return 1;
912 /* report an error while loading an input file */
913 static void file_read_error( const char *err, struct file_load_info *info )
915 fprintf( stderr, "Line %d: %s '%s'\n", info->line, err, info->buffer );
918 /* parse an escaped string back into Unicode */
919 /* return the number of chars read from the input, or -1 on output overflow */
920 static int parse_strW( WCHAR *dest, int *len, const char *src, char endchar )
922 int count = sizeof(WCHAR); /* for terminating null */
923 const char *p = src;
924 while (*p && *p != endchar)
926 if (*p != '\\') *dest = (WCHAR)*p++;
927 else
929 p++;
930 switch(*p)
932 case 'a': *dest = '\a'; p++; break;
933 case 'b': *dest = '\b'; p++; break;
934 case 'e': *dest = '\e'; p++; break;
935 case 'f': *dest = '\f'; p++; break;
936 case 'n': *dest = '\n'; p++; break;
937 case 'r': *dest = '\r'; p++; break;
938 case 't': *dest = '\t'; p++; break;
939 case 'v': *dest = '\v'; p++; break;
940 case 'x': /* hex escape */
941 p++;
942 if (!isxdigit(*p)) *dest = 'x';
943 else
945 *dest = to_hex(*p++);
946 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
947 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
948 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
950 break;
951 case '0':
952 case '1':
953 case '2':
954 case '3':
955 case '4':
956 case '5':
957 case '6':
958 case '7': /* octal escape */
959 *dest = *p++ - '0';
960 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
961 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
962 break;
963 default:
964 *dest = (WCHAR)*p++;
965 break;
968 if ((count += sizeof(WCHAR)) > *len) return -1; /* dest buffer overflow */
969 dest++;
971 *dest = 0;
972 if (!*p) return -1; /* delimiter not found */
973 *len = count;
974 return p + 1 - src;
977 /* convert a data type tag to a value type */
978 static int get_data_type( const char *buffer, int *type, int *parse_type )
980 struct data_type { const char *tag; int len; int type; int parse_type; };
982 static const struct data_type data_types[] =
983 { /* actual type */ /* type to assume for parsing */
984 { "\"", 1, REG_SZ, REG_SZ },
985 { "str:\"", 5, REG_SZ, REG_SZ },
986 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
987 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
988 { "hex:", 4, REG_BINARY, REG_BINARY },
989 { "dword:", 6, REG_DWORD, REG_DWORD },
990 { "hex(", 4, -1, REG_BINARY },
991 { NULL, }
994 const struct data_type *ptr;
995 char *end;
997 for (ptr = data_types; ptr->tag; ptr++)
999 if (memcmp( ptr->tag, buffer, ptr->len )) continue;
1000 *parse_type = ptr->parse_type;
1001 if ((*type = ptr->type) != -1) return ptr->len;
1002 /* "hex(xx):" is special */
1003 *type = (int)strtoul( buffer + 4, &end, 16 );
1004 if ((end <= buffer) || memcmp( end, "):", 2 )) return 0;
1005 return end + 2 - buffer;
1007 return 0;
1010 /* load and create a key from the input file */
1011 static struct key *load_key( struct key *base, const char *buffer, struct file_load_info *info )
1013 WCHAR *p;
1014 int res, len, modif;
1016 len = strlen(buffer) * sizeof(WCHAR);
1017 if (!get_file_tmp_space( info, len )) return NULL;
1019 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, ']' )) == -1)
1021 file_read_error( "Malformed key", info );
1022 return NULL;
1024 if (!sscanf( buffer + res, " %d", &modif )) modif = time(NULL);
1026 for (p = (WCHAR *)info->tmp; *p; p++) if (*p == '\\') { p++; break; }
1027 return create_key( base, p, len - ((char *)p - info->tmp), NULL, 0, modif, &res );
1030 /* parse a comma-separated list of hex digits */
1031 static int parse_hex( unsigned char *dest, int *len, const char *buffer )
1033 const char *p = buffer;
1034 int count = 0;
1035 while (isxdigit(*p))
1037 int val;
1038 char buf[3];
1039 memcpy( buf, p, 2 );
1040 buf[2] = 0;
1041 sscanf( buf, "%x", &val );
1042 if (count++ >= *len) return -1; /* dest buffer overflow */
1043 *dest++ = (unsigned char )val;
1044 p += 2;
1045 if (*p == ',') p++;
1047 *len = count;
1048 return p - buffer;
1051 /* parse a value name and create the corresponding value */
1052 static struct key_value *parse_value_name( struct key *key, const char *buffer, int *len,
1053 struct file_load_info *info )
1055 int maxlen = strlen(buffer) * sizeof(WCHAR);
1056 if (!get_file_tmp_space( info, maxlen )) return NULL;
1057 if (buffer[0] == '@')
1059 info->tmp[0] = info->tmp[1] = 0;
1060 *len = 1;
1062 else
1064 if ((*len = parse_strW( (WCHAR *)info->tmp, &maxlen, buffer + 1, '\"' )) == -1) goto error;
1065 (*len)++; /* for initial quote */
1067 if (buffer[*len] != '=') goto error;
1068 (*len)++;
1069 return insert_value( key, (WCHAR *)info->tmp );
1071 error:
1072 file_read_error( "Malformed value name", info );
1073 return NULL;
1076 /* load a value from the input file */
1077 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1079 DWORD dw;
1080 void *ptr, *newptr;
1081 int maxlen, len, res;
1082 int type, parse_type;
1083 struct key_value *value;
1085 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1086 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1087 buffer += len + res;
1089 switch(parse_type)
1091 case REG_SZ:
1092 len = strlen(buffer) * sizeof(WCHAR);
1093 if (!get_file_tmp_space( info, len )) return 0;
1094 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, '\"' )) == -1) goto error;
1095 ptr = info->tmp;
1096 break;
1097 case REG_DWORD:
1098 dw = strtoul( buffer, NULL, 16 );
1099 ptr = &dw;
1100 len = sizeof(dw);
1101 break;
1102 case REG_BINARY: /* hex digits */
1103 len = 0;
1104 for (;;)
1106 maxlen = 1 + strlen(buffer)/3; /* 3 chars for one hex byte */
1107 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1108 if ((res = parse_hex( info->tmp + len, &maxlen, buffer )) == -1) goto error;
1109 len += maxlen;
1110 buffer += res;
1111 while (isspace(*buffer)) buffer++;
1112 if (!*buffer) break;
1113 if (*buffer != '\\') goto error;
1114 if (read_next_line( info) != 1) goto error;
1115 buffer = info->buffer;
1116 while (isspace(*buffer)) buffer++;
1118 ptr = info->tmp;
1119 break;
1120 default:
1121 assert(0);
1122 ptr = NULL; /* keep compiler quiet */
1123 break;
1126 if (!len) newptr = NULL;
1127 else if (!(newptr = memdup( ptr, len ))) return 0;
1129 if (value->data) free( value->data );
1130 value->data = newptr;
1131 value->len = len;
1132 value->type = type;
1133 /* update the key level but not the modification time */
1134 key->level = MAX( key->level, current_level );
1135 return 1;
1137 error:
1138 file_read_error( "Malformed value", info );
1139 return 0;
1142 /* load all the keys from the input file */
1143 static void load_keys( struct key *key, FILE *f )
1145 struct key *subkey = NULL;
1146 struct file_load_info info;
1147 char *p;
1149 info.file = f;
1150 info.len = 4;
1151 info.tmplen = 4;
1152 info.line = 0;
1153 if (!(info.buffer = mem_alloc( info.len ))) return;
1154 if (!(info.tmp = mem_alloc( info.tmplen )))
1156 free( info.buffer );
1157 return;
1160 if ((read_next_line( &info ) != 1) ||
1161 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1163 set_error( ERROR_NOT_REGISTRY_FILE );
1164 goto done;
1167 while (read_next_line( &info ) == 1)
1169 for (p = info.buffer; *p && isspace(*p); p++);
1170 switch(*p)
1172 case '[': /* new key */
1173 if (subkey) release_object( subkey );
1174 subkey = load_key( key, p + 1, &info );
1175 break;
1176 case '@': /* default value */
1177 case '\"': /* value */
1178 if (subkey) load_value( subkey, p, &info );
1179 else file_read_error( "Value without key", &info );
1180 break;
1181 case '#': /* comment */
1182 case ';': /* comment */
1183 case 0: /* empty line */
1184 break;
1185 default:
1186 file_read_error( "Unrecognized input", &info );
1187 break;
1191 done:
1192 if (subkey) release_object( subkey );
1193 free( info.buffer );
1194 free( info.tmp );
1197 /* load a part of the registry from a file */
1198 static void load_registry( struct key *key, int handle )
1200 struct object *obj;
1201 int fd;
1203 if (!(obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL ))) return;
1204 fd = obj->ops->get_read_fd( obj );
1205 release_object( obj );
1206 if (fd != -1)
1208 FILE *f = fdopen( fd, "r" );
1209 if (f)
1211 load_keys( key, f );
1212 fclose( f );
1214 else file_set_error();
1218 /* update the level of the parents of a key (only needed for the old format) */
1219 static int update_level( struct key *key )
1221 int i;
1222 int max = key->level;
1223 for (i = 0; i <= key->last_subkey; i++)
1225 int sub = update_level( key->subkeys[i] );
1226 if (sub > max) max = sub;
1228 key->level = max;
1229 return max;
1232 /* dump a string to a registry save file in the old v1 format */
1233 static void save_string_v1( LPCWSTR str, FILE *f )
1235 if (!str) return;
1236 while (*str)
1238 if ((*str > 0x7f) || (*str == '\n') || (*str == '='))
1239 fprintf( f, "\\u%04x", *str );
1240 else
1242 if (*str == '\\') fputc( '\\', f );
1243 fputc( (char)*str, f );
1245 str++;
1249 /* save a registry and all its subkeys to a text file in the old v1 format */
1250 static void save_subkeys_v1( struct key *key, int nesting, FILE *f )
1252 int i, j;
1254 if (key->flags & KEY_VOLATILE) return;
1255 if (key->level < saving_level) return;
1256 for (i = 0; i <= key->last_value; i++)
1258 struct key_value *value = &key->values[i];
1259 for (j = nesting; j > 0; j --) fputc( '\t', f );
1260 save_string_v1( value->name, f );
1261 fprintf( f, "=%d,%d,", value->type, 0 );
1262 if (value->type == REG_SZ || value->type == REG_EXPAND_SZ)
1263 save_string_v1( (LPWSTR)value->data, f );
1264 else
1265 for (j = 0; j < value->len; j++)
1266 fprintf( f, "%02x", *((unsigned char *)value->data + j) );
1267 fputc( '\n', f );
1269 for (i = 0; i <= key->last_subkey; i++)
1271 for (j = nesting; j > 0; j --) fputc( '\t', f );
1272 save_string_v1( key->subkeys[i]->name, f );
1273 fputc( '\n', f );
1274 save_subkeys_v1( key->subkeys[i], nesting + 1, f );
1278 /* save a registry branch to a file handle */
1279 static void save_registry( struct key *key, int handle )
1281 struct object *obj;
1282 int fd;
1284 if (key->flags & KEY_DELETED)
1286 set_error( ERROR_KEY_DELETED );
1287 return;
1289 if (!(obj = get_handle_obj( current->process, handle, GENERIC_WRITE, NULL ))) return;
1290 fd = obj->ops->get_write_fd( obj );
1291 release_object( obj );
1292 if (fd != -1)
1294 FILE *f = fdopen( fd, "w" );
1295 if (f)
1297 fprintf( f, "WINE REGISTRY Version %d\n", saving_version );
1298 if (saving_version == 2) save_subkeys( key, f );
1299 else
1301 update_level( key );
1302 save_subkeys_v1( key, 0, f );
1304 if (fclose( f )) file_set_error();
1306 else
1308 file_set_error();
1309 close( fd );
1314 /* create a registry key */
1315 DECL_HANDLER(create_key)
1317 struct key *key, *parent;
1318 WCHAR *class;
1319 unsigned int access = req->access;
1321 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1322 req->hkey = -1;
1323 if ((parent = get_hkey_obj( req->parent, KEY_CREATE_SUB_KEY )))
1325 if ((class = req_strdupW( req->class )))
1327 if ((key = create_key( parent, req->name, sizeof(req->name), class, req->options,
1328 req->modif, &req->created )))
1330 req->hkey = alloc_handle( current->process, key, access, 0 );
1331 release_object( key );
1333 free( class );
1335 release_object( parent );
1339 /* open a registry key */
1340 DECL_HANDLER(open_key)
1342 struct key *key, *parent;
1343 unsigned int access = req->access;
1345 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1346 req->hkey = -1;
1347 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1349 if ((key = open_key( parent, req->name, sizeof(req->name) )))
1351 req->hkey = alloc_handle( current->process, key, access, 0 );
1352 release_object( key );
1354 release_object( parent );
1358 /* delete a registry key */
1359 DECL_HANDLER(delete_key)
1361 struct key *key;
1363 if ((key = get_hkey_obj( req->hkey, KEY_CREATE_SUB_KEY /*FIXME*/ )))
1365 delete_key( key, req->name, sizeof(req->name) );
1366 release_object( key );
1370 /* close a registry key */
1371 DECL_HANDLER(close_key)
1373 int hkey = req->hkey;
1374 /* ignore attempts to close a root key */
1375 if (!IS_ROOT_HKEY(hkey)) close_handle( current->process, hkey );
1378 /* enumerate registry subkeys */
1379 DECL_HANDLER(enum_key)
1381 struct key *key;
1383 if ((key = get_hkey_obj( req->hkey, KEY_ENUMERATE_SUB_KEYS )))
1385 enum_key( key, req->index, req->name, req->class, &req->modif );
1386 release_object( key );
1390 /* query information about a registry key */
1391 DECL_HANDLER(query_key_info)
1393 struct key *key;
1395 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1397 query_key( key, req );
1398 release_object( key );
1402 /* set a value of a registry key */
1403 DECL_HANDLER(set_key_value)
1405 struct key *key;
1406 int max = get_req_size( req->data, sizeof(req->data[0]) );
1407 int datalen = req->len;
1408 if (datalen > max)
1410 set_error( ERROR_OUTOFMEMORY ); /* FIXME */
1411 return;
1413 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1415 set_value( key, copy_path( req->name ), req->type, datalen, req->data );
1416 release_object( key );
1420 /* retrieve the value of a registry key */
1421 DECL_HANDLER(get_key_value)
1423 struct key *key;
1425 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1427 get_value( key, copy_path( req->name ), &req->type, &req->len, req->data );
1428 release_object( key );
1432 /* enumerate the value of a registry key */
1433 DECL_HANDLER(enum_key_value)
1435 struct key *key;
1437 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1439 enum_value( key, req->index, req->name, &req->type, &req->len, req->data );
1440 release_object( key );
1444 /* delete a value of a registry key */
1445 DECL_HANDLER(delete_key_value)
1447 WCHAR *name;
1448 struct key *key;
1450 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1452 if ((name = req_strdupW( req->name )))
1454 delete_value( key, name );
1455 free( name );
1457 release_object( key );
1461 /* load a registry branch from a file */
1462 DECL_HANDLER(load_registry)
1464 struct key *key;
1466 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE | KEY_CREATE_SUB_KEY )))
1468 /* FIXME: use subkey name */
1469 load_registry( key, req->file );
1470 release_object( key );
1474 /* save a registry branch to a file */
1475 DECL_HANDLER(save_registry)
1477 struct key *key;
1479 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1481 save_registry( key, req->file );
1482 release_object( key );
1486 /* set the current and saving level for the registry */
1487 DECL_HANDLER(set_registry_levels)
1489 current_level = req->current;
1490 saving_level = req->saving;
1491 saving_version = req->version;