2 * Server-side registry management
4 * Copyright (C) 1999 Alexandre Julliard
8 * - behavior with deleted keys
9 * - values larger than request buffer
31 #include "winnt.h" /* registry definitions */
37 struct object obj; /* object header */
38 WCHAR *name; /* key name */
39 WCHAR *class; /* key class */
40 struct key *parent; /* parent key */
41 int last_subkey; /* last in use subkey */
42 int nb_subkeys; /* count of allocated subkeys */
43 struct key **subkeys; /* subkeys array */
44 int last_value; /* last in use value */
45 int nb_values; /* count of allocated values in array */
46 struct key_value *values; /* values array */
47 short flags; /* flags */
48 short level; /* saving level */
49 time_t modif; /* last modification time */
53 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
54 #define KEY_DELETED 0x0002 /* key has been deleted */
55 #define KEY_ROOT 0x0004 /* key is a root key */
60 WCHAR *name; /* value name */
61 int type; /* value type */
62 size_t len; /* value data length in bytes */
63 void *data; /* pointer to value data */
66 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
67 #define MIN_VALUES 8 /* min. number of allocated values per key */
70 /* the special root keys */
71 #define HKEY_SPECIAL_ROOT_FIRST HKEY_CLASSES_ROOT
72 #define HKEY_SPECIAL_ROOT_LAST HKEY_DYN_DATA
73 #define NB_SPECIAL_ROOT_KEYS (HKEY_SPECIAL_ROOT_LAST - HKEY_SPECIAL_ROOT_FIRST + 1)
74 #define IS_SPECIAL_ROOT_HKEY(h) (((h) >= HKEY_SPECIAL_ROOT_FIRST) && ((h) <= HKEY_SPECIAL_ROOT_LAST))
75 static struct key *special_root_keys[NB_SPECIAL_ROOT_KEYS];
77 /* the real root key */
78 static struct key *root_key;
80 /* the special root key names */
81 static const char * const special_root_names[NB_SPECIAL_ROOT_KEYS] =
83 "Machine\\Software\\Classes", /* HKEY_CLASSES_ROOT */
84 "User\\", /* we append the user name dynamically */ /* HKEY_CURRENT_USER */
85 "Machine", /* HKEY_LOCAL_MACHINE */
86 "User", /* HKEY_USERS */
87 "PerfData", /* HKEY_PERFORMANCE_DATA */
88 "Machine\\System\\CurrentControlSet\\HardwareProfiles\\Current", /* HKEY_CURRENT_CONFIG */
89 "DynData" /* HKEY_DYN_DATA */
93 /* keys saving level */
94 /* current_level is the level that is put into all newly created or modified keys */
95 /* saving_level is the minimum level that a key needs in order to get saved */
96 static int current_level;
97 static int saving_level;
99 static struct timeval next_save_time; /* absolute time of next periodic save */
100 static int save_period; /* delay between periodic saves (ms) */
101 static struct timeout_user *save_timeout_user; /* saving timer */
103 /* information about where to save a registry branch */
104 struct save_branch_info
110 #define MAX_SAVE_BRANCH_INFO 8
111 static int save_branch_count;
112 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
115 /* information about a file being loaded */
116 struct file_load_info
118 FILE *file; /* input file */
119 char *buffer; /* line buffer */
120 int len; /* buffer length */
121 int line; /* current input line */
122 char *tmp; /* temp buffer to use while parsing input */
123 int tmplen; /* length of temp buffer */
127 static void key_dump( struct object *obj, int verbose );
128 static void key_destroy( struct object *obj );
130 static const struct object_ops key_ops =
132 sizeof(struct key), /* size */
134 no_add_queue, /* add_queue */
135 NULL, /* remove_queue */
137 NULL, /* satisfied */
138 NULL, /* get_poll_events */
139 NULL, /* poll_event */
140 no_read_fd, /* get_read_fd */
141 no_write_fd, /* get_write_fd */
142 no_flush, /* flush */
143 no_get_file_info, /* get_file_info */
144 key_destroy /* destroy */
149 * The registry text file format v2 used by this code is similar to the one
150 * used by REGEDIT import/export functionality, with the following differences:
151 * - strings and key names can contain \x escapes for Unicode
152 * - key names use escapes too in order to support Unicode
153 * - the modification time optionally follows the key name
154 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
157 static inline char to_hex( char ch )
159 if (isdigit(ch)) return ch - '0';
160 return tolower(ch) - 'a' + 10;
163 /* dump the full path of a key */
164 static void dump_path( struct key *key, struct key *base, FILE *f )
166 if (key->parent && key->parent != base)
168 dump_path( key->parent, base, f );
169 fprintf( f, "\\\\" );
171 dump_strW( key->name, strlenW(key->name), f, "[]" );
174 /* dump a value to a text file */
175 static void dump_value( struct key_value *value, FILE *f )
182 count = 1 + dump_strW( value->name, strlenW(value->name), f, "\"\"" );
183 count += fprintf( f, "\"=" );
185 else count = fprintf( f, "@=" );
192 if (value->type != REG_SZ) fprintf( f, "str(%d):", value->type );
194 if (value->data) dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
198 if (value->len == sizeof(DWORD))
201 memcpy( &dw, value->data, sizeof(DWORD) );
202 fprintf( f, "dword:%08lx", dw );
205 /* else fall through */
207 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
208 else count += fprintf( f, "hex(%x):", value->type );
209 for (i = 0; i < value->len; i++)
211 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
212 if (i < value->len-1)
217 fprintf( f, "\\\n " );
227 /* save a registry and all its subkeys to a text file */
228 static void save_subkeys( struct key *key, struct key *base, FILE *f )
232 if (key->flags & KEY_VOLATILE) return;
233 /* save key if it has the proper level, and has either some values or no subkeys */
234 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
235 if ((key->level >= saving_level) && ((key->last_value >= 0) || (key->last_subkey == -1)))
238 if (key != base) dump_path( key, base, f );
239 fprintf( f, "] %ld\n", key->modif );
240 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
242 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
245 static void dump_operation( struct key *key, struct key_value *value, const char *op )
247 fprintf( stderr, "%s key ", op );
248 if (key) dump_path( key, NULL, stderr );
249 else fprintf( stderr, "ERROR" );
252 fprintf( stderr, " value ");
253 dump_value( value, stderr );
255 else fprintf( stderr, "\n" );
258 static void key_dump( struct object *obj, int verbose )
260 struct key *key = (struct key *)obj;
261 assert( obj->ops == &key_ops );
262 fprintf( stderr, "Key flags=%x ", key->flags );
263 dump_path( key, NULL, stderr );
264 fprintf( stderr, "\n" );
267 static void key_destroy( struct object *obj )
270 struct key *key = (struct key *)obj;
271 assert( obj->ops == &key_ops );
273 if (key->name) free( key->name );
274 if (key->class) free( key->class );
275 for (i = 0; i <= key->last_value; i++)
277 free( key->values[i].name );
278 if (key->values[i].data) free( key->values[i].data );
280 for (i = 0; i <= key->last_subkey; i++)
282 key->subkeys[i]->parent = NULL;
283 release_object( key->subkeys[i] );
287 /* duplicate a key path from the request buffer */
288 /* returns a pointer to a static buffer, so only useable once per request */
289 static WCHAR *copy_path( const WCHAR *path, size_t len )
291 static WCHAR buffer[MAX_PATH+1];
293 if (len > sizeof(buffer)-sizeof(buffer[0]))
295 set_error( STATUS_BUFFER_OVERFLOW );
298 memcpy( buffer, path, len );
299 buffer[len / sizeof(WCHAR)] = 0;
303 /* copy a path from the request buffer, in cases where the length is stored in front of the path */
304 static WCHAR *copy_req_path( void *req, size_t *len )
306 const WCHAR *name_ptr = get_req_data(req);
307 if ((*len = sizeof(WCHAR) + *name_ptr++) > get_req_data_size(req))
309 fatal_protocol_error( current, "copy_req_path: invalid length %d/%d\n",
310 *len, get_req_data_size(req) );
313 return copy_path( name_ptr, *len - sizeof(WCHAR) );
316 /* return the next token in a given path */
317 /* returns a pointer to a static buffer, so only useable once per request */
318 static WCHAR *get_path_token( WCHAR *initpath )
325 /* path cannot start with a backslash */
326 if (*initpath == '\\')
328 set_error( STATUS_OBJECT_PATH_INVALID );
333 else while (*path == '\\') path++;
336 while (*path && *path != '\\') path++;
337 if (*path) *path++ = 0;
341 /* duplicate a Unicode string from the request buffer */
342 static WCHAR *req_strdupW( const void *req, const WCHAR *str, size_t len )
345 if ((name = mem_alloc( len + sizeof(WCHAR) )) != NULL)
347 memcpy( name, str, len );
348 name[len / sizeof(WCHAR)] = 0;
353 /* allocate a key object */
354 static struct key *alloc_key( const WCHAR *name, time_t modif )
357 if ((key = (struct key *)alloc_object( &key_ops, -1 )))
361 key->last_subkey = -1;
365 key->last_value = -1;
367 key->level = current_level;
370 if (!(key->name = strdupW( name )))
372 release_object( key );
379 /* update key modification time */
380 static void touch_key( struct key *key )
382 key->modif = time(NULL);
383 key->level = max( key->level, current_level );
386 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
387 static int grow_subkeys( struct key *key )
389 struct key **new_subkeys;
394 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
395 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
397 set_error( STATUS_NO_MEMORY );
403 nb_subkeys = MIN_VALUES;
404 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
406 key->subkeys = new_subkeys;
407 key->nb_subkeys = nb_subkeys;
411 /* allocate a subkey for a given key, and return its index */
412 static struct key *alloc_subkey( struct key *parent, const WCHAR *name, int index, time_t modif )
417 if (parent->last_subkey + 1 == parent->nb_subkeys)
419 /* need to grow the array */
420 if (!grow_subkeys( parent )) return NULL;
422 if ((key = alloc_key( name, modif )) != NULL)
424 key->parent = parent;
425 for (i = ++parent->last_subkey; i > index; i--)
426 parent->subkeys[i] = parent->subkeys[i-1];
427 parent->subkeys[index] = key;
432 /* free a subkey of a given key */
433 static void free_subkey( struct key *parent, int index )
438 assert( index >= 0 );
439 assert( index <= parent->last_subkey );
441 key = parent->subkeys[index];
442 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
443 parent->last_subkey--;
444 key->flags |= KEY_DELETED;
446 release_object( key );
448 /* try to shrink the array */
449 nb_subkeys = key->nb_subkeys;
450 if (nb_subkeys > MIN_SUBKEYS && key->last_subkey < nb_subkeys / 2)
452 struct key **new_subkeys;
453 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
454 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
455 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
456 key->subkeys = new_subkeys;
457 key->nb_subkeys = nb_subkeys;
461 /* find the named child of a given key and return its index */
462 static struct key *find_subkey( struct key *key, const WCHAR *name, int *index )
464 int i, min, max, res;
467 max = key->last_subkey;
471 if (!(res = strcmpiW( key->subkeys[i]->name, name )))
474 return key->subkeys[i];
476 if (res > 0) max = i - 1;
479 *index = min; /* this is where we should insert it */
484 /* warning: the key name must be writeable (use copy_path) */
485 static struct key *open_key( struct key *key, WCHAR *name )
490 if (!(path = get_path_token( name ))) return NULL;
493 if (!(key = find_subkey( key, path, &index )))
495 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
498 path = get_path_token( NULL );
501 if (debug_level > 1) dump_operation( key, NULL, "Open" );
502 if (key) grab_object( key );
506 /* create a subkey */
507 /* warning: the key name must be writeable (use copy_path) */
508 static struct key *create_key( struct key *key, WCHAR *name, WCHAR *class,
509 unsigned int options, time_t modif, int *created )
512 int base_idx, index, flags = 0;
515 if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
517 set_error( STATUS_KEY_DELETED );
520 if (options & REG_OPTION_VOLATILE) flags |= KEY_VOLATILE;
521 else if (key->flags & KEY_VOLATILE)
523 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
526 if (!modif) modif = time(NULL);
528 if (!(path = get_path_token( name ))) return NULL;
533 if (!(subkey = find_subkey( key, path, &index ))) break;
535 path = get_path_token( NULL );
538 /* create the remaining part */
540 if (!*path) goto done;
544 key = alloc_subkey( key, path, index, modif );
548 path = get_path_token( NULL );
549 if (!*path) goto done;
550 /* we know the index is always 0 in a new key */
551 key = alloc_subkey( key, path, 0, modif );
553 if (base_idx != -1) free_subkey( base, base_idx );
557 if (debug_level > 1) dump_operation( key, NULL, "Create" );
558 if (class) key->class = strdupW(class);
563 /* query information about a key or a subkey */
564 static size_t enum_key( struct key *key, int index, struct enum_key_request *req )
567 size_t len, namelen, classlen;
568 int max_subkey = 0, max_class = 0;
569 int max_value = 0, max_data = 0;
570 WCHAR *data = get_req_data(req);
572 if (index != -1) /* -1 means use the specified key directly */
574 if ((index < 0) || (index > key->last_subkey))
576 set_error( STATUS_NO_MORE_ENTRIES );
579 key = key->subkeys[index];
584 for (i = 0; i <= key->last_subkey; i++)
586 struct key *subkey = key->subkeys[i];
587 len = strlenW( subkey->name );
588 if (len > max_subkey) max_subkey = len;
589 if (!subkey->class) continue;
590 len = strlenW( subkey->class );
591 if (len > max_class) max_class = len;
593 for (i = 0; i <= key->last_value; i++)
595 len = strlenW( key->values[i].name );
596 if (len > max_value) max_value = len;
597 len = key->values[i].len;
598 if (len > max_data) max_data = len;
600 req->max_subkey = max_subkey;
601 req->max_class = max_class;
602 req->max_value = max_value;
603 req->max_data = max_data;
612 req->subkeys = key->last_subkey + 1;
613 req->values = key->last_value + 1;
614 req->modif = key->modif;
616 namelen = strlenW(key->name) * sizeof(WCHAR);
617 classlen = key->class ? strlenW(key->class) * sizeof(WCHAR) : 0;
619 len = namelen + classlen + sizeof(WCHAR);
620 if (len > get_req_data_size(req))
622 len = get_req_data_size(req);
623 if (len < sizeof(WCHAR)) return 0;
627 len -= sizeof(WCHAR);
630 memcpy( data, key->name, namelen );
631 memcpy( (char *)data + namelen, key->class, min(classlen,len-namelen) );
633 else memcpy( data, key->name, len );
635 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
636 return len + sizeof(WCHAR);
639 /* delete a key and its values */
640 static void delete_key( struct key *key )
645 /* must find parent and index */
646 if (key->flags & KEY_ROOT)
648 set_error( STATUS_ACCESS_DENIED );
651 if (!(parent = key->parent) || (key->flags & KEY_DELETED))
653 set_error( STATUS_KEY_DELETED );
656 for (index = 0; index <= parent->last_subkey; index++)
657 if (parent->subkeys[index] == key) break;
658 assert( index <= parent->last_subkey );
660 /* we can only delete a key that has no subkeys (FIXME) */
661 if ((key->flags & KEY_ROOT) || (key->last_subkey >= 0))
663 set_error( STATUS_ACCESS_DENIED );
666 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
667 free_subkey( parent, index );
671 /* try to grow the array of values; return 1 if OK, 0 on error */
672 static int grow_values( struct key *key )
674 struct key_value *new_val;
679 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
680 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
682 set_error( STATUS_NO_MEMORY );
688 nb_values = MIN_VALUES;
689 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
691 key->values = new_val;
692 key->nb_values = nb_values;
696 /* find the named value of a given key and return its index in the array */
697 static struct key_value *find_value( const struct key *key, const WCHAR *name, int *index )
699 int i, min, max, res;
702 max = key->last_value;
706 if (!(res = strcmpiW( key->values[i].name, name )))
709 return &key->values[i];
711 if (res > 0) max = i - 1;
714 *index = min; /* this is where we should insert it */
718 /* insert a new value or return a pointer to an existing one */
719 static struct key_value *insert_value( struct key *key, const WCHAR *name )
721 struct key_value *value;
725 if (!(value = find_value( key, name, &index )))
727 /* not found, add it */
728 if (key->last_value + 1 == key->nb_values)
730 if (!grow_values( key )) return NULL;
732 if (!(new_name = strdupW(name))) return NULL;
733 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
734 value = &key->values[index];
735 value->name = new_name;
742 /* set a key value */
743 static void set_value( struct key *key, WCHAR *name, int type, unsigned int total_len,
744 unsigned int offset, unsigned int data_len, const void *data )
746 struct key_value *value;
749 if (data_len + offset > total_len)
751 set_error( STATUS_INVALID_PARAMETER );
755 if (offset) /* adding data to an existing value */
758 if (!(value = find_value( key, name, &index )))
760 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
763 if (value->len != total_len)
765 set_error( STATUS_INVALID_PARAMETER );
768 memcpy( (char *)value->data + offset, data, data_len );
769 if (debug_level > 1) dump_operation( key, value, "Set" );
773 /* first copy the data */
776 if (!(ptr = mem_alloc( total_len ))) return;
777 memcpy( ptr, data, data_len );
778 if (data_len < total_len) memset( (char *)ptr + data_len, 0, total_len - data_len );
781 if (!(value = insert_value( key, name )))
783 if (ptr) free( ptr );
786 if (value->data) free( value->data ); /* already existing, free previous data */
788 value->len = total_len;
791 if (debug_level > 1) dump_operation( key, value, "Set" );
794 /* get a key value */
795 static size_t get_value( struct key *key, WCHAR *name, unsigned int offset,
796 unsigned int maxlen, int *type, int *len, void *data )
798 struct key_value *value;
802 if ((value = find_value( key, name, &index )))
806 if (value->data && offset < value->len)
808 if (maxlen > value->len - offset) maxlen = value->len - offset;
809 memcpy( data, (char *)value->data + offset, maxlen );
812 if (debug_level > 1) dump_operation( key, value, "Get" );
817 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
822 /* enumerate a key value */
823 static void enum_value( struct key *key, int i, WCHAR *name, unsigned int offset,
824 unsigned int maxlen, int *type, int *len, void *data )
826 struct key_value *value;
828 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
831 value = &key->values[i];
832 strcpyW( name, value->name );
835 if (value->data && offset < value->len)
837 if (maxlen > value->len - offset) maxlen = value->len - offset;
838 memcpy( data, (char *)value->data + offset, maxlen );
840 if (debug_level > 1) dump_operation( key, value, "Enum" );
845 static void delete_value( struct key *key, const WCHAR *name )
847 struct key_value *value;
848 int i, index, nb_values;
850 if (!(value = find_value( key, name, &index )))
852 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
855 if (debug_level > 1) dump_operation( key, value, "Delete" );
857 if (value->data) free( value->data );
858 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
862 /* try to shrink the array */
863 nb_values = key->nb_values;
864 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
866 struct key_value *new_val;
867 nb_values -= nb_values / 3; /* shrink by 33% */
868 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
869 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
870 key->values = new_val;
871 key->nb_values = nb_values;
875 static struct key *create_root_key( int hkey )
882 p = special_root_names[hkey - HKEY_SPECIAL_ROOT_FIRST];
884 while (*p) keyname[i++] = *p++;
886 if (hkey == HKEY_CURRENT_USER) /* this one is special */
888 /* get the current user name */
890 struct passwd *pwd = getpwuid( getuid() );
892 if (pwd) p = pwd->pw_name;
895 sprintf( buffer, "%ld", (long) getuid() );
898 while (*p && i < sizeof(keyname)/sizeof(WCHAR)-1) keyname[i++] = *p++;
902 if ((key = create_key( root_key, keyname, NULL, 0, time(NULL), &dummy )))
904 special_root_keys[hkey - HKEY_SPECIAL_ROOT_FIRST] = key;
905 key->flags |= KEY_ROOT;
910 /* get the registry key corresponding to an hkey handle */
911 static struct key *get_hkey_obj( int hkey, unsigned int access )
915 if (!hkey) return (struct key *)grab_object( root_key );
916 if (IS_SPECIAL_ROOT_HKEY(hkey))
918 if (!(key = special_root_keys[hkey - HKEY_SPECIAL_ROOT_FIRST]))
919 key = create_root_key( hkey );
924 key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
928 /* read a line from the input file */
929 static int read_next_line( struct file_load_info *info )
937 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
938 return (pos != 0); /* EOF */
939 pos = strlen(info->buffer);
940 if (info->buffer[pos-1] == '\n')
942 /* got a full line */
943 info->buffer[--pos] = 0;
944 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
947 if (pos < info->len - 1) return 1; /* EOF but something was read */
949 /* need to enlarge the buffer */
950 newlen = info->len + info->len / 2;
951 if (!(newbuf = realloc( info->buffer, newlen )))
953 set_error( STATUS_NO_MEMORY );
956 info->buffer = newbuf;
961 /* make sure the temp buffer holds enough space */
962 static int get_file_tmp_space( struct file_load_info *info, int size )
965 if (info->tmplen >= size) return 1;
966 if (!(tmp = realloc( info->tmp, size )))
968 set_error( STATUS_NO_MEMORY );
976 /* report an error while loading an input file */
977 static void file_read_error( const char *err, struct file_load_info *info )
979 fprintf( stderr, "Line %d: %s '%s'\n", info->line, err, info->buffer );
982 /* parse an escaped string back into Unicode */
983 /* return the number of chars read from the input, or -1 on output overflow */
984 static int parse_strW( WCHAR *dest, int *len, const char *src, char endchar )
986 int count = sizeof(WCHAR); /* for terminating null */
988 while (*p && *p != endchar)
990 if (*p != '\\') *dest = (WCHAR)*p++;
996 case 'a': *dest = '\a'; p++; break;
997 case 'b': *dest = '\b'; p++; break;
998 case 'e': *dest = '\e'; p++; break;
999 case 'f': *dest = '\f'; p++; break;
1000 case 'n': *dest = '\n'; p++; break;
1001 case 'r': *dest = '\r'; p++; break;
1002 case 't': *dest = '\t'; p++; break;
1003 case 'v': *dest = '\v'; p++; break;
1004 case 'x': /* hex escape */
1006 if (!isxdigit(*p)) *dest = 'x';
1009 *dest = to_hex(*p++);
1010 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1011 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1012 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1022 case '7': /* octal escape */
1024 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1025 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1028 *dest = (WCHAR)*p++;
1032 if ((count += sizeof(WCHAR)) > *len) return -1; /* dest buffer overflow */
1036 if (!*p) return -1; /* delimiter not found */
1041 /* convert a data type tag to a value type */
1042 static int get_data_type( const char *buffer, int *type, int *parse_type )
1044 struct data_type { const char *tag; int len; int type; int parse_type; };
1046 static const struct data_type data_types[] =
1047 { /* actual type */ /* type to assume for parsing */
1048 { "\"", 1, REG_SZ, REG_SZ },
1049 { "str:\"", 5, REG_SZ, REG_SZ },
1050 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1051 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1052 { "hex:", 4, REG_BINARY, REG_BINARY },
1053 { "dword:", 6, REG_DWORD, REG_DWORD },
1054 { "hex(", 4, -1, REG_BINARY },
1058 const struct data_type *ptr;
1061 for (ptr = data_types; ptr->tag; ptr++)
1063 if (memcmp( ptr->tag, buffer, ptr->len )) continue;
1064 *parse_type = ptr->parse_type;
1065 if ((*type = ptr->type) != -1) return ptr->len;
1066 /* "hex(xx):" is special */
1067 *type = (int)strtoul( buffer + 4, &end, 16 );
1068 if ((end <= buffer) || memcmp( end, "):", 2 )) return 0;
1069 return end + 2 - buffer;
1074 /* load and create a key from the input file */
1075 static struct key *load_key( struct key *base, const char *buffer, unsigned int options,
1076 int prefix_len, struct file_load_info *info )
1079 int res, len, modif;
1081 len = strlen(buffer) * sizeof(WCHAR);
1082 if (!get_file_tmp_space( info, len )) return NULL;
1084 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, ']' )) == -1)
1086 file_read_error( "Malformed key", info );
1089 if (sscanf( buffer + res, " %d", &modif ) != 1) modif = time(NULL);
1091 p = (WCHAR *)info->tmp;
1092 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1098 file_read_error( "Malformed key", info );
1101 /* empty key name, return base key */
1102 return (struct key *)grab_object( base );
1104 if (!(name = copy_path( p, len - ((char *)p - info->tmp) )))
1106 file_read_error( "Key is too long", info );
1109 return create_key( base, name, NULL, options, modif, &res );
1112 /* parse a comma-separated list of hex digits */
1113 static int parse_hex( unsigned char *dest, int *len, const char *buffer )
1115 const char *p = buffer;
1117 while (isxdigit(*p))
1121 memcpy( buf, p, 2 );
1123 sscanf( buf, "%x", &val );
1124 if (count++ >= *len) return -1; /* dest buffer overflow */
1125 *dest++ = (unsigned char )val;
1133 /* parse a value name and create the corresponding value */
1134 static struct key_value *parse_value_name( struct key *key, const char *buffer, int *len,
1135 struct file_load_info *info )
1137 int maxlen = strlen(buffer) * sizeof(WCHAR);
1138 if (!get_file_tmp_space( info, maxlen )) return NULL;
1139 if (buffer[0] == '@')
1141 info->tmp[0] = info->tmp[1] = 0;
1146 if ((*len = parse_strW( (WCHAR *)info->tmp, &maxlen, buffer + 1, '\"' )) == -1) goto error;
1147 (*len)++; /* for initial quote */
1149 while (isspace(buffer[*len])) (*len)++;
1150 if (buffer[*len] != '=') goto error;
1152 while (isspace(buffer[*len])) (*len)++;
1153 return insert_value( key, (WCHAR *)info->tmp );
1156 file_read_error( "Malformed value name", info );
1160 /* load a value from the input file */
1161 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1165 int maxlen, len, res;
1166 int type, parse_type;
1167 struct key_value *value;
1169 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1170 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1171 buffer += len + res;
1176 len = strlen(buffer) * sizeof(WCHAR);
1177 if (!get_file_tmp_space( info, len )) return 0;
1178 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, '\"' )) == -1) goto error;
1182 dw = strtoul( buffer, NULL, 16 );
1186 case REG_BINARY: /* hex digits */
1190 maxlen = 1 + strlen(buffer)/3; /* 3 chars for one hex byte */
1191 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1192 if ((res = parse_hex( info->tmp + len, &maxlen, buffer )) == -1) goto error;
1195 while (isspace(*buffer)) buffer++;
1196 if (!*buffer) break;
1197 if (*buffer != '\\') goto error;
1198 if (read_next_line( info) != 1) goto error;
1199 buffer = info->buffer;
1200 while (isspace(*buffer)) buffer++;
1206 ptr = NULL; /* keep compiler quiet */
1210 if (!len) newptr = NULL;
1211 else if (!(newptr = memdup( ptr, len ))) return 0;
1213 if (value->data) free( value->data );
1214 value->data = newptr;
1217 /* update the key level but not the modification time */
1218 key->level = max( key->level, current_level );
1222 file_read_error( "Malformed value", info );
1226 /* return the length (in path elements) of name that is part of the key name */
1227 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1228 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1232 int len = strlen(name) * sizeof(WCHAR);
1233 if (!get_file_tmp_space( info, len )) return 0;
1235 if ((res = parse_strW( (WCHAR *)info->tmp, &len, name, ']' )) == -1)
1237 file_read_error( "Malformed key", info );
1240 for (p = (WCHAR *)info->tmp; *p; p++) if (*p == '\\') break;
1242 for (res = 1; key != root_key; res++)
1244 if (!strcmpiW( (WCHAR *)info->tmp, key->name )) break;
1247 if (key == root_key) res = 0; /* no matching name */
1251 /* load all the keys from the input file */
1252 static void load_keys( struct key *key, FILE *f )
1254 struct key *subkey = NULL;
1255 struct file_load_info info;
1257 unsigned int options = 0;
1258 int prefix_len = -1; /* number of key name prefixes to skip */
1260 if (key->flags & KEY_VOLATILE) options |= REG_OPTION_VOLATILE;
1266 if (!(info.buffer = mem_alloc( info.len ))) return;
1267 if (!(info.tmp = mem_alloc( info.tmplen )))
1269 free( info.buffer );
1273 if ((read_next_line( &info ) != 1) ||
1274 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1276 set_error( STATUS_NOT_REGISTRY_FILE );
1280 while (read_next_line( &info ) == 1)
1282 for (p = info.buffer; *p && isspace(*p); p++);
1285 case '[': /* new key */
1286 if (subkey) release_object( subkey );
1287 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1288 if (!(subkey = load_key( key, p + 1, options, prefix_len, &info )))
1289 file_read_error( "Error creating key", &info );
1291 case '@': /* default value */
1292 case '\"': /* value */
1293 if (subkey) load_value( subkey, p, &info );
1294 else file_read_error( "Value without key", &info );
1296 case '#': /* comment */
1297 case ';': /* comment */
1298 case 0: /* empty line */
1301 file_read_error( "Unrecognized input", &info );
1307 if (subkey) release_object( subkey );
1308 free( info.buffer );
1312 /* load a part of the registry from a file */
1313 static void load_registry( struct key *key, int handle )
1318 if (!(obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL ))) return;
1319 fd = obj->ops->get_read_fd( obj );
1320 release_object( obj );
1323 FILE *f = fdopen( fd, "r" );
1326 load_keys( key, f );
1329 else file_set_error();
1333 /* registry initialisation */
1334 void init_registry(void)
1336 static const WCHAR root_name[] = { 0 };
1337 static const WCHAR config_name[] =
1338 { 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
1339 'W','i','n','e','\\','W','i','n','e','\\','C','o','n','f','i','g',0 };
1345 /* create the root key */
1346 root_key = alloc_key( root_name, time(NULL) );
1348 root_key->flags |= KEY_ROOT;
1350 /* load the config file */
1351 config = get_config_dir();
1352 if (!(filename = malloc( strlen(config) + 8 ))) fatal_error( "out of memory\n" );
1353 strcpy( filename, config );
1354 strcat( filename, "/config" );
1355 if ((f = fopen( filename, "r" )))
1360 /* create the config key */
1361 if (!(key = create_key( root_key, copy_path( config_name, sizeof(config_name) ),
1362 NULL, 0, time(NULL), &dummy )))
1363 fatal_error( "could not create config key\n" );
1364 key->flags |= KEY_VOLATILE;
1366 load_keys( key, f );
1368 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1369 fatal_error( "%s is not a valid registry file\n", filename );
1371 fatal_error( "loading %s failed with error %x\n", filename, get_error() );
1373 release_object( key );
1378 /* update the level of the parents of a key (only needed for the old format) */
1379 static int update_level( struct key *key )
1382 int max = key->level;
1383 for (i = 0; i <= key->last_subkey; i++)
1385 int sub = update_level( key->subkeys[i] );
1386 if (sub > max) max = sub;
1392 /* save a registry branch to a file */
1393 static void save_all_subkeys( struct key *key, FILE *f )
1395 fprintf( f, "WINE REGISTRY Version 2\n" );
1396 fprintf( f, ";; All keys relative to " );
1397 dump_path( key, NULL, f );
1399 save_subkeys( key, key, f );
1402 /* save a registry branch to a file handle */
1403 static void save_registry( struct key *key, int handle )
1408 if (key->flags & KEY_DELETED)
1410 set_error( STATUS_KEY_DELETED );
1413 if (!(obj = get_handle_obj( current->process, handle, GENERIC_WRITE, NULL ))) return;
1414 fd = obj->ops->get_write_fd( obj );
1415 release_object( obj );
1418 FILE *f = fdopen( fd, "w" );
1421 save_all_subkeys( key, f );
1422 if (fclose( f )) file_set_error();
1432 /* register a key branch for being saved on exit */
1433 static void register_branch_for_saving( struct key *key, const char *path, size_t len )
1435 if (save_branch_count >= MAX_SAVE_BRANCH_INFO)
1437 set_error( STATUS_NO_MORE_ENTRIES );
1440 if (!len || !(save_branch_info[save_branch_count].path = memdup( path, len ))) return;
1441 save_branch_info[save_branch_count].path[len - 1] = 0;
1442 save_branch_info[save_branch_count].key = (struct key *)grab_object( key );
1443 save_branch_count++;
1446 /* save a registry branch to a file */
1447 static int save_branch( struct key *key, const char *path )
1449 char *p, *real, *tmp = NULL;
1450 int fd, count = 0, ret = 0;
1453 /* get the real path */
1455 if (!(real = malloc( PATH_MAX ))) return 0;
1456 if (!realpath( path, real ))
1463 /* test the file type */
1465 if ((fd = open( path, O_WRONLY )) != -1)
1468 /* if file is not a regular file or has multiple links,
1469 write directly into it; otherwise use a temp file */
1470 if (!fstat( fd, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1478 /* create a temp file in the same directory */
1480 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1481 strcpy( tmp, path );
1482 if ((p = strrchr( tmp, '/' ))) p++;
1486 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1487 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1488 if (errno != EEXIST) goto done;
1492 /* now save to it */
1495 if (!(f = fdopen( fd, "w" )))
1497 if (tmp) unlink( tmp );
1502 if (debug_level > 1)
1504 fprintf( stderr, "%s: ", path );
1505 dump_operation( key, NULL, "saving" );
1508 save_all_subkeys( key, f );
1513 /* if successfully written, rename to final name */
1514 if (ret) ret = !rename( tmp, path );
1515 if (!ret) unlink( tmp );
1520 if (real) free( real );
1524 /* periodic saving of the registry */
1525 static void periodic_save( void *arg )
1528 for (i = 0; i < save_branch_count; i++)
1529 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1530 add_timeout( &next_save_time, save_period );
1531 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1534 /* save the registry and close the top-level keys; used on server exit */
1535 void close_registry(void)
1539 for (i = 0; i < save_branch_count; i++)
1541 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1543 fprintf( stderr, "wineserver: could not save registry branch to %s",
1544 save_branch_info[i].path );
1547 release_object( save_branch_info[i].key );
1549 release_object( root_key );
1553 /* create a registry key */
1554 DECL_HANDLER(create_key)
1556 struct key *key = NULL, *parent;
1557 unsigned int access = req->access;
1558 WCHAR *name, *class;
1561 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1563 if (!(name = copy_req_path( req, &len ))) return;
1564 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1566 if (len == get_req_data_size(req)) /* no class specified */
1568 key = create_key( parent, name, NULL, req->options, req->modif, &req->created );
1572 const WCHAR *class_ptr = (WCHAR *)((char *)get_req_data(req) + len);
1574 if ((class = req_strdupW( req, class_ptr, get_req_data_size(req) - len )))
1576 key = create_key( parent, name, class, req->options,
1577 req->modif, &req->created );
1583 req->hkey = alloc_handle( current->process, key, access, 0 );
1584 release_object( key );
1586 release_object( parent );
1590 /* open a registry key */
1591 DECL_HANDLER(open_key)
1593 struct key *key, *parent;
1594 unsigned int access = req->access;
1596 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1598 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1600 WCHAR *name = copy_path( get_req_data(req), get_req_data_size(req) );
1601 if (name && (key = open_key( parent, name )))
1603 req->hkey = alloc_handle( current->process, key, access, 0 );
1604 release_object( key );
1606 release_object( parent );
1610 /* delete a registry key */
1611 DECL_HANDLER(delete_key)
1615 if ((key = get_hkey_obj( req->hkey, 0 /*FIXME*/ )))
1618 release_object( key );
1622 /* enumerate registry subkeys */
1623 DECL_HANDLER(enum_key)
1628 if ((key = get_hkey_obj( req->hkey,
1629 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
1631 len = enum_key( key, req->index, req );
1632 release_object( key );
1634 set_req_data_size( req, len );
1637 /* set a value of a registry key */
1638 DECL_HANDLER(set_key_value)
1644 if (!(name = copy_req_path( req, &len ))) return;
1645 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1647 size_t datalen = get_req_data_size(req) - len;
1648 const char *data = (char *)get_req_data(req) + len;
1650 set_value( key, name, req->type, req->total, req->offset, datalen, data );
1651 release_object( key );
1655 /* retrieve the value of a registry key */
1656 DECL_HANDLER(get_key_value)
1660 size_t len = 0, tmp;
1663 if (!(name = copy_req_path( req, &tmp ))) return;
1664 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1666 len = get_value( key, name, req->offset, get_req_data_size(req),
1667 &req->type, &req->len, get_req_data(req) );
1668 release_object( key );
1670 set_req_data_size( req, len );
1673 /* enumerate the value of a registry key */
1674 DECL_HANDLER(enum_key_value)
1677 unsigned int max = get_req_size( req, req->data, sizeof(req->data[0]) );
1681 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1683 enum_value( key, req->index, req->name, req->offset, max,
1684 &req->type, &req->len, req->data );
1685 release_object( key );
1689 /* delete a value of a registry key */
1690 DECL_HANDLER(delete_key_value)
1695 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1697 if ((name = req_strdupW( req, get_req_data(req), get_req_data_size(req) )))
1699 delete_value( key, name );
1702 release_object( key );
1706 /* load a registry branch from a file */
1707 DECL_HANDLER(load_registry)
1711 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE | KEY_CREATE_SUB_KEY )))
1713 /* FIXME: use subkey name */
1714 load_registry( key, req->file );
1715 release_object( key );
1719 /* save a registry branch to a file */
1720 DECL_HANDLER(save_registry)
1724 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1726 save_registry( key, req->file );
1727 release_object( key );
1731 /* set the current and saving level for the registry */
1732 DECL_HANDLER(set_registry_levels)
1734 current_level = req->current;
1735 saving_level = req->saving;
1737 /* set periodic save timer */
1739 if (save_timeout_user)
1741 remove_timeout_user( save_timeout_user );
1742 save_timeout_user = NULL;
1744 if ((save_period = req->period))
1746 if (save_period < 10000) save_period = 10000; /* limit rate */
1747 gettimeofday( &next_save_time, 0 );
1748 add_timeout( &next_save_time, save_period );
1749 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1753 /* save a registry branch at server exit */
1754 DECL_HANDLER(save_registry_atexit)
1758 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1760 register_branch_for_saving( key, get_req_data(req), get_req_data_size(req) );
1761 release_object( key );