2 * Server-side registry management
4 * Copyright (C) 1999 Alexandre Julliard
8 * - behavior with deleted keys
9 * - values larger than request buffer
26 #include "winnt.h" /* registry definitions */
32 struct object obj
; /* object header */
33 WCHAR
*name
; /* key name */
34 WCHAR
*class; /* key class */
35 struct key
*parent
; /* parent key */
36 int last_subkey
; /* last in use subkey */
37 int nb_subkeys
; /* count of allocated subkeys */
38 struct key
**subkeys
; /* subkeys array */
39 int last_value
; /* last in use value */
40 int nb_values
; /* count of allocated values in array */
41 struct key_value
*values
; /* values array */
42 short flags
; /* flags */
43 short level
; /* saving level */
44 time_t modif
; /* last modification time */
48 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
49 #define KEY_DELETED 0x0002 /* key has been deleted */
50 #define KEY_ROOT 0x0004 /* key is a root key */
55 WCHAR
*name
; /* value name */
56 int type
; /* value type */
57 size_t len
; /* value data length in bytes */
58 void *data
; /* pointer to value data */
61 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
62 #define MIN_VALUES 8 /* min. number of allocated values per key */
66 #define HKEY_ROOT_FIRST HKEY_CLASSES_ROOT
67 #define HKEY_ROOT_LAST HKEY_DYN_DATA
68 #define NB_ROOT_KEYS (HKEY_ROOT_LAST - HKEY_ROOT_FIRST + 1)
69 #define IS_ROOT_HKEY(h) (((h) >= HKEY_ROOT_FIRST) && ((h) <= HKEY_ROOT_LAST))
70 static struct key
*root_keys
[NB_ROOT_KEYS
];
72 static const char * const root_key_names
[NB_ROOT_KEYS
] =
78 "HKEY_PERFORMANCE_DATA",
79 "HKEY_CURRENT_CONFIG",
84 /* keys saving level */
85 /* current_level is the level that is put into all newly created or modified keys */
86 /* saving_level is the minimum level that a key needs in order to get saved */
87 static int current_level
;
88 static int saving_level
;
90 static int saving_version
= 1; /* file format version */
93 /* information about a file being loaded */
96 FILE *file
; /* input file */
97 char *buffer
; /* line buffer */
98 int len
; /* buffer length */
99 int line
; /* current input line */
100 char *tmp
; /* temp buffer to use while parsing input */
101 int tmplen
; /* length of temp buffer */
105 static void key_dump( struct object
*obj
, int verbose
);
106 static void key_destroy( struct object
*obj
);
108 static const struct object_ops key_ops
=
110 sizeof(struct key
), /* size */
112 no_add_queue
, /* add_queue */
113 NULL
, /* remove_queue */
115 NULL
, /* satisfied */
116 NULL
, /* get_poll_events */
117 NULL
, /* poll_event */
118 no_read_fd
, /* get_read_fd */
119 no_write_fd
, /* get_write_fd */
120 no_flush
, /* flush */
121 no_get_file_info
, /* get_file_info */
122 key_destroy
/* destroy */
127 * The registry text file format v2 used by this code is similar to the one
128 * used by REGEDIT import/export functionality, with the following differences:
129 * - strings and key names can contain \x escapes for Unicode
130 * - key names use escapes too in order to support Unicode
131 * - the modification time optionally follows the key name
132 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
135 static inline char to_hex( char ch
)
137 if (isdigit(ch
)) return ch
- '0';
138 return tolower(ch
) - 'a' + 10;
141 /* dump the full path of a key */
142 static void dump_path( struct key
*key
, struct key
*base
, FILE *f
)
144 if (key
->parent
&& key
!= base
)
146 dump_path( key
->parent
, base
, f
);
147 fprintf( f
, "\\\\" );
150 if (key
->name
) dump_strW( key
->name
, strlenW(key
->name
), f
, "[]" );
154 for (i
= 0; i
< NB_ROOT_KEYS
; i
++)
155 if (root_keys
[i
] == key
) fprintf( f
, "%s", root_key_names
[i
] );
159 /* dump a value to a text file */
160 static void dump_value( struct key_value
*value
, FILE *f
)
167 count
= 1 + dump_strW( value
->name
, strlenW(value
->name
), f
, "\"\"" );
168 count
+= fprintf( f
, "\"=" );
170 else count
= fprintf( f
, "@=" );
177 if (value
->type
!= REG_SZ
) fprintf( f
, "str(%d):", value
->type
);
179 if (value
->data
) dump_strW( (WCHAR
*)value
->data
, value
->len
/ sizeof(WCHAR
), f
, "\"\"" );
183 if (value
->len
== sizeof(DWORD
))
186 memcpy( &dw
, value
->data
, sizeof(DWORD
) );
187 fprintf( f
, "dword:%08lx", dw
);
190 /* else fall through */
192 if (value
->type
== REG_BINARY
) count
+= fprintf( f
, "hex:" );
193 else count
+= fprintf( f
, "hex(%x):", value
->type
);
194 for (i
= 0; i
< value
->len
; i
++)
196 count
+= fprintf( f
, "%02x", *((unsigned char *)value
->data
+ i
) );
197 if (i
< value
->len
-1)
202 fprintf( f
, "\\\n " );
212 /* save a registry and all its subkeys to a text file */
213 static void save_subkeys( struct key
*key
, struct key
*base
, FILE *f
)
217 if (key
->flags
& KEY_VOLATILE
) return;
218 /* save key if it has the proper level, and has either some values or no subkeys */
219 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
220 if ((key
->level
>= saving_level
) && ((key
->last_value
>= 0) || (key
->last_subkey
== -1)))
223 dump_path( key
, base
, f
);
224 fprintf( f
, "] %ld\n", key
->modif
);
225 for (i
= 0; i
<= key
->last_value
; i
++) dump_value( &key
->values
[i
], f
);
227 for (i
= 0; i
<= key
->last_subkey
; i
++) save_subkeys( key
->subkeys
[i
], base
, f
);
230 static void dump_operation( struct key
*key
, struct key_value
*value
, const char *op
)
232 fprintf( stderr
, "%s key ", op
);
233 if (key
) dump_path( key
, NULL
, stderr
);
234 else fprintf( stderr
, "ERROR" );
237 fprintf( stderr
, " value ");
238 dump_value( value
, stderr
);
240 else fprintf( stderr
, "\n" );
243 static void key_dump( struct object
*obj
, int verbose
)
245 struct key
*key
= (struct key
*)obj
;
246 assert( obj
->ops
== &key_ops
);
247 fprintf( stderr
, "Key flags=%x ", key
->flags
);
248 dump_path( key
, NULL
, stderr
);
249 fprintf( stderr
, "\n" );
252 static void key_destroy( struct object
*obj
)
255 struct key
*key
= (struct key
*)obj
;
256 assert( obj
->ops
== &key_ops
);
259 if (key
->class) free( key
->class );
260 for (i
= 0; i
<= key
->last_value
; i
++)
262 free( key
->values
[i
].name
);
263 if (key
->values
[i
].data
) free( key
->values
[i
].data
);
265 for (i
= 0; i
<= key
->last_subkey
; i
++)
267 key
->subkeys
[i
]->parent
= NULL
;
268 release_object( key
->subkeys
[i
] );
272 /* duplicate a key path from the request buffer */
273 /* returns a pointer to a static buffer, so only useable once per request */
274 static WCHAR
*copy_path( const path_t path
)
276 static WCHAR buffer
[MAX_PATH
+1];
279 while (p
< buffer
+ sizeof(buffer
) - 1) if (!(*p
++ = *path
++)) break;
284 /* return the next token in a given path */
285 /* returns a pointer to a static buffer, so only useable once per request */
286 static WCHAR
*get_path_token( const WCHAR
*initpath
, size_t maxlen
)
288 static const WCHAR
*path
;
289 static const WCHAR
*end
;
290 static WCHAR buffer
[MAX_PATH
+1];
296 end
= path
+ maxlen
/ sizeof(WCHAR
);
298 while ((path
< end
) && (*path
== '\\')) path
++;
299 while ((path
< end
) && (p
< buffer
+ sizeof(buffer
) - 1))
302 if (!ch
|| (ch
== '\\')) break;
310 /* duplicate a Unicode string from the request buffer */
311 static WCHAR
*req_strdupW( const WCHAR
*str
)
314 size_t len
= get_req_strlenW( str
);
315 if ((name
= mem_alloc( (len
+ 1) * sizeof(WCHAR
) )) != NULL
)
317 memcpy( name
, str
, len
* sizeof(WCHAR
) );
323 /* allocate a key object */
324 static struct key
*alloc_key( const WCHAR
*name
, time_t modif
)
327 if ((key
= (struct key
*)alloc_object( &key_ops
, -1 )))
332 key
->last_subkey
= -1;
336 key
->last_value
= -1;
338 key
->level
= current_level
;
341 if (name
&& !(key
->name
= strdupW( name
)))
343 release_object( key
);
350 /* update key modification time */
351 static void touch_key( struct key
*key
)
353 key
->modif
= time(NULL
);
354 key
->level
= max( key
->level
, current_level
);
357 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
358 static int grow_subkeys( struct key
*key
)
360 struct key
**new_subkeys
;
365 nb_subkeys
= key
->nb_subkeys
+ (key
->nb_subkeys
/ 2); /* grow by 50% */
366 if (!(new_subkeys
= realloc( key
->subkeys
, nb_subkeys
* sizeof(*new_subkeys
) )))
368 set_error( STATUS_NO_MEMORY
);
374 nb_subkeys
= MIN_VALUES
;
375 if (!(new_subkeys
= mem_alloc( nb_subkeys
* sizeof(*new_subkeys
) ))) return 0;
377 key
->subkeys
= new_subkeys
;
378 key
->nb_subkeys
= nb_subkeys
;
382 /* allocate a subkey for a given key, and return its index */
383 static struct key
*alloc_subkey( struct key
*parent
, const WCHAR
*name
, int index
, time_t modif
)
388 if (parent
->last_subkey
+ 1 == parent
->nb_subkeys
)
390 /* need to grow the array */
391 if (!grow_subkeys( parent
)) return NULL
;
393 if ((key
= alloc_key( name
, modif
)) != NULL
)
395 key
->parent
= parent
;
396 for (i
= ++parent
->last_subkey
; i
> index
; i
--)
397 parent
->subkeys
[i
] = parent
->subkeys
[i
-1];
398 parent
->subkeys
[index
] = key
;
403 /* free a subkey of a given key */
404 static void free_subkey( struct key
*parent
, int index
)
409 assert( index
>= 0 );
410 assert( index
<= parent
->last_subkey
);
412 key
= parent
->subkeys
[index
];
413 for (i
= index
; i
< parent
->last_subkey
; i
++) parent
->subkeys
[i
] = parent
->subkeys
[i
+ 1];
414 parent
->last_subkey
--;
415 key
->flags
|= KEY_DELETED
;
417 release_object( key
);
419 /* try to shrink the array */
420 nb_subkeys
= key
->nb_subkeys
;
421 if (nb_subkeys
> MIN_SUBKEYS
&& key
->last_subkey
< nb_subkeys
/ 2)
423 struct key
**new_subkeys
;
424 nb_subkeys
-= nb_subkeys
/ 3; /* shrink by 33% */
425 if (nb_subkeys
< MIN_SUBKEYS
) nb_subkeys
= MIN_SUBKEYS
;
426 if (!(new_subkeys
= realloc( key
->subkeys
, nb_subkeys
* sizeof(*new_subkeys
) ))) return;
427 key
->subkeys
= new_subkeys
;
428 key
->nb_subkeys
= nb_subkeys
;
432 /* find the named child of a given key and return its index */
433 static struct key
*find_subkey( struct key
*key
, const WCHAR
*name
, int *index
)
435 int i
, min
, max
, res
;
438 max
= key
->last_subkey
;
442 if (!(res
= strcmpiW( key
->subkeys
[i
]->name
, name
)))
445 return key
->subkeys
[i
];
447 if (res
> 0) max
= i
- 1;
450 *index
= min
; /* this is where we should insert it */
455 static struct key
*open_key( struct key
*key
, const WCHAR
*name
, size_t maxlen
)
460 path
= get_path_token( name
, maxlen
);
463 if (!(key
= find_subkey( key
, path
, &index
)))
465 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
468 path
= get_path_token( NULL
, 0 );
471 if (debug_level
> 1) dump_operation( key
, NULL
, "Open" );
472 if (key
) grab_object( key
);
476 /* create a subkey */
477 static struct key
*create_key( struct key
*key
, const WCHAR
*name
, size_t maxlen
, WCHAR
*class,
478 unsigned int options
, time_t modif
, int *created
)
481 int base_idx
, index
, flags
= 0;
484 if (key
->flags
& KEY_DELETED
) /* we cannot create a subkey under a deleted key */
486 set_error( STATUS_KEY_DELETED
);
489 if (options
& REG_OPTION_VOLATILE
) flags
|= KEY_VOLATILE
;
490 else if (key
->flags
& KEY_VOLATILE
)
492 set_error( STATUS_CHILD_MUST_BE_VOLATILE
);
496 path
= get_path_token( name
, maxlen
);
501 if (!(subkey
= find_subkey( key
, path
, &index
))) break;
503 path
= get_path_token( NULL
, 0 );
506 /* create the remaining part */
508 if (!*path
) goto done
;
512 key
= alloc_subkey( key
, path
, index
, modif
);
516 path
= get_path_token( NULL
, 0 );
517 if (!*path
) goto done
;
518 /* we know the index is always 0 in a new key */
519 key
= alloc_subkey( key
, path
, 0, modif
);
521 if (base_idx
!= -1) free_subkey( base
, base_idx
);
525 if (debug_level
> 1) dump_operation( key
, NULL
, "Create" );
526 if (class) key
->class = strdupW(class);
531 /* find a subkey of a given key by its index */
532 static void enum_key( struct key
*parent
, int index
, WCHAR
*name
, WCHAR
*class, time_t *modif
)
536 if ((index
< 0) || (index
> parent
->last_subkey
)) set_error( STATUS_NO_MORE_ENTRIES
);
539 key
= parent
->subkeys
[index
];
541 strcpyW( name
, key
->name
);
542 if (key
->class) strcpyW( class, key
->class ); /* FIXME: length */
544 if (debug_level
> 1) dump_operation( key
, NULL
, "Enum" );
548 /* query information about a key */
549 static void query_key( struct key
*key
, struct query_key_info_request
*req
)
552 int max_subkey
= 0, max_class
= 0;
553 int max_value
= 0, max_data
= 0;
555 for (i
= 0; i
<= key
->last_subkey
; i
++)
557 struct key
*subkey
= key
->subkeys
[i
];
558 len
= strlenW( subkey
->name
);
559 if (len
> max_subkey
) max_subkey
= len
;
560 if (!subkey
->class) continue;
561 len
= strlenW( subkey
->class );
562 if (len
> max_class
) max_class
= len
;
564 for (i
= 0; i
<= key
->last_value
; i
++)
566 len
= strlenW( key
->values
[i
].name
);
567 if (len
> max_value
) max_value
= len
;
568 len
= key
->values
[i
].len
;
569 if (len
> max_data
) max_data
= len
;
571 req
->subkeys
= key
->last_subkey
+ 1;
572 req
->max_subkey
= max_subkey
;
573 req
->max_class
= max_class
;
574 req
->values
= key
->last_value
+ 1;
575 req
->max_value
= max_value
;
576 req
->max_data
= max_data
;
577 req
->modif
= key
->modif
;
578 strcpyW( req
->name
, key
->name
);
579 if (key
->class) strcpyW( req
->class, key
->class ); /* FIXME: length */
580 else req
->class[0] = 0;
581 if (debug_level
> 1) dump_operation( key
, NULL
, "Query" );
584 /* delete a key and its values */
585 static void delete_key( struct key
*key
, const WCHAR
*name
, size_t maxlen
)
591 path
= get_path_token( name
, maxlen
);
594 /* deleting this key, must find parent and index */
595 if (key
->flags
& KEY_ROOT
)
597 set_error( STATUS_ACCESS_DENIED
);
600 if (!(parent
= key
->parent
) || (key
->flags
& KEY_DELETED
))
602 set_error( STATUS_KEY_DELETED
);
605 for (index
= 0; index
<= parent
->last_subkey
; index
++)
606 if (parent
->subkeys
[index
] == key
) break;
607 assert( index
<= parent
->last_subkey
);
612 if (!(key
= find_subkey( parent
, path
, &index
)))
614 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
617 path
= get_path_token( NULL
, 0 );
620 /* we can only delete a key that has no subkeys (FIXME) */
621 if ((key
->flags
& KEY_ROOT
) || (key
->last_subkey
>= 0))
623 set_error( STATUS_ACCESS_DENIED
);
626 if (debug_level
> 1) dump_operation( key
, NULL
, "Delete" );
627 free_subkey( parent
, index
);
631 /* try to grow the array of values; return 1 if OK, 0 on error */
632 static int grow_values( struct key
*key
)
634 struct key_value
*new_val
;
639 nb_values
= key
->nb_values
+ (key
->nb_values
/ 2); /* grow by 50% */
640 if (!(new_val
= realloc( key
->values
, nb_values
* sizeof(*new_val
) )))
642 set_error( STATUS_NO_MEMORY
);
648 nb_values
= MIN_VALUES
;
649 if (!(new_val
= mem_alloc( nb_values
* sizeof(*new_val
) ))) return 0;
651 key
->values
= new_val
;
652 key
->nb_values
= nb_values
;
656 /* find the named value of a given key and return its index in the array */
657 static struct key_value
*find_value( const struct key
*key
, const WCHAR
*name
, int *index
)
659 int i
, min
, max
, res
;
662 max
= key
->last_value
;
666 if (!(res
= strcmpiW( key
->values
[i
].name
, name
)))
669 return &key
->values
[i
];
671 if (res
> 0) max
= i
- 1;
674 *index
= min
; /* this is where we should insert it */
678 /* insert a new value or return a pointer to an existing one */
679 static struct key_value
*insert_value( struct key
*key
, const WCHAR
*name
)
681 struct key_value
*value
;
685 if (!(value
= find_value( key
, name
, &index
)))
687 /* not found, add it */
688 if (key
->last_value
+ 1 == key
->nb_values
)
690 if (!grow_values( key
)) return NULL
;
692 if (!(new_name
= strdupW(name
))) return NULL
;
693 for (i
= ++key
->last_value
; i
> index
; i
--) key
->values
[i
] = key
->values
[i
- 1];
694 value
= &key
->values
[index
];
695 value
->name
= new_name
;
702 /* set a key value */
703 static void set_value( struct key
*key
, WCHAR
*name
, int type
, int datalen
, void *data
)
705 struct key_value
*value
;
708 /* first copy the data */
711 if (!(ptr
= mem_alloc( datalen
))) return;
712 memcpy( ptr
, data
, datalen
);
715 if (!(value
= insert_value( key
, name
)))
717 if (ptr
) free( ptr
);
720 if (value
->data
) free( value
->data
); /* already existing, free previous data */
722 value
->len
= datalen
;
725 if (debug_level
> 1) dump_operation( key
, value
, "Set" );
728 /* get a key value */
729 static void get_value( struct key
*key
, WCHAR
*name
, int *type
, int *len
, void *data
)
731 struct key_value
*value
;
734 if ((value
= find_value( key
, name
, &index
)))
738 if (value
->data
) memcpy( data
, value
->data
, value
->len
);
739 if (debug_level
> 1) dump_operation( key
, value
, "Get" );
745 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
749 /* enumerate a key value */
750 static void enum_value( struct key
*key
, int i
, WCHAR
*name
, int *type
, int *len
, void *data
)
752 struct key_value
*value
;
754 if (i
< 0 || i
> key
->last_value
)
758 set_error( STATUS_NO_MORE_ENTRIES
);
762 value
= &key
->values
[i
];
763 strcpyW( name
, value
->name
);
766 if (value
->data
) memcpy( data
, value
->data
, value
->len
);
767 if (debug_level
> 1) dump_operation( key
, value
, "Enum" );
772 static void delete_value( struct key
*key
, const WCHAR
*name
)
774 struct key_value
*value
;
775 int i
, index
, nb_values
;
777 if (!(value
= find_value( key
, name
, &index
)))
779 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
782 if (debug_level
> 1) dump_operation( key
, value
, "Delete" );
784 if (value
->data
) free( value
->data
);
785 for (i
= index
; i
< key
->last_value
; i
++) key
->values
[i
] = key
->values
[i
+ 1];
789 /* try to shrink the array */
790 nb_values
= key
->nb_values
;
791 if (nb_values
> MIN_VALUES
&& key
->last_value
< nb_values
/ 2)
793 struct key_value
*new_val
;
794 nb_values
-= nb_values
/ 3; /* shrink by 33% */
795 if (nb_values
< MIN_VALUES
) nb_values
= MIN_VALUES
;
796 if (!(new_val
= realloc( key
->values
, nb_values
* sizeof(*new_val
) ))) return;
797 key
->values
= new_val
;
798 key
->nb_values
= nb_values
;
802 static struct key
*get_hkey_obj( int hkey
, unsigned int access
);
804 static struct key
*create_root_key( int hkey
)
811 /* the two real root-keys */
812 case HKEY_LOCAL_MACHINE
:
814 static const WCHAR name
[] = { 'M','A','C','H','I','N','E',0 };
815 key
= alloc_key( name
, time(NULL
) );
820 static const WCHAR name
[] = { 'U','S','E','R',0 };
821 key
= alloc_key( name
, time(NULL
) );
824 /* special subkeys */
825 case HKEY_CLASSES_ROOT
:
827 static const WCHAR name
[] =
828 { 'S','O','F','T','W','A','R','E','\\','C','l','a','s','s','e','s',0 };
830 struct key
*root
= get_hkey_obj( HKEY_LOCAL_MACHINE
, 0 );
832 key
= create_key( root
, name
, sizeof(name
), NULL
, 0, time(NULL
), &dummy
);
833 release_object( root
);
836 case HKEY_CURRENT_CONFIG
:
838 static const WCHAR name
[] = {
839 'S','Y','S','T','E','M','\\',
840 'C','U','R','R','E','N','T','C','O','N','T','R','O','L','S','E','T','\\',
841 'H','A','R','D','W','A','R','E','P','R','O','F','I','L','E','S','\\',
842 'C','U','R','R','E','N','T',0};
843 struct key
*root
= get_hkey_obj( HKEY_LOCAL_MACHINE
, 0 );
845 key
= create_key( root
, name
, sizeof(name
), NULL
, 0, time(NULL
), &dummy
);
846 release_object( root
);
849 case HKEY_CURRENT_USER
:
851 /* get the current user name */
856 struct passwd
*pwd
= getpwuid( getuid() );
858 if (pwd
) p
= pwd
->pw_name
;
861 sprintf( buffer
, "%ld", (long) getuid() );
865 if ((name
= mem_alloc( (len
+1) * sizeof(WCHAR
) )))
867 struct key
*root
= get_hkey_obj( HKEY_USERS
, 0 );
869 for (i
= 0; i
<= len
; i
++) name
[i
] = p
[i
];
870 key
= create_key( root
, name
, (len
+1) * sizeof(WCHAR
),
871 NULL
, 0, time(NULL
), &dummy
);
872 release_object( root
);
878 /* dynamically generated keys */
879 case HKEY_PERFORMANCE_DATA
:
881 key
= alloc_key( NULL
, time(NULL
) );
889 root_keys
[hkey
- HKEY_ROOT_FIRST
] = key
;
890 key
->flags
|= KEY_ROOT
;
895 /* close the top-level keys; used on server exit */
896 void close_registry(void)
899 for (i
= 0; i
< NB_ROOT_KEYS
; i
++)
901 if (root_keys
[i
]) release_object( root_keys
[i
] );
905 /* get the registry key corresponding to an hkey handle */
906 static struct key
*get_hkey_obj( int hkey
, unsigned int access
)
910 if (IS_ROOT_HKEY(hkey
))
912 if (!(key
= root_keys
[hkey
- HKEY_ROOT_FIRST
])) key
= create_root_key( hkey
);
916 key
= (struct key
*)get_handle_obj( current
->process
, hkey
, access
, &key_ops
);
920 /* read a line from the input file */
921 static int read_next_line( struct file_load_info
*info
)
929 if (!fgets( info
->buffer
+ pos
, info
->len
- pos
, info
->file
))
930 return (pos
!= 0); /* EOF */
931 pos
= strlen(info
->buffer
);
932 if (info
->buffer
[pos
-1] == '\n')
934 /* got a full line */
935 info
->buffer
[--pos
] = 0;
936 if (pos
> 0 && info
->buffer
[pos
-1] == '\r') info
->buffer
[pos
-1] = 0;
939 if (pos
< info
->len
- 1) return 1; /* EOF but something was read */
941 /* need to enlarge the buffer */
942 newlen
= info
->len
+ info
->len
/ 2;
943 if (!(newbuf
= realloc( info
->buffer
, newlen
)))
945 set_error( STATUS_NO_MEMORY
);
948 info
->buffer
= newbuf
;
953 /* make sure the temp buffer holds enough space */
954 static int get_file_tmp_space( struct file_load_info
*info
, int size
)
957 if (info
->tmplen
>= size
) return 1;
958 if (!(tmp
= realloc( info
->tmp
, size
)))
960 set_error( STATUS_NO_MEMORY
);
968 /* report an error while loading an input file */
969 static void file_read_error( const char *err
, struct file_load_info
*info
)
971 fprintf( stderr
, "Line %d: %s '%s'\n", info
->line
, err
, info
->buffer
);
974 /* parse an escaped string back into Unicode */
975 /* return the number of chars read from the input, or -1 on output overflow */
976 static int parse_strW( WCHAR
*dest
, int *len
, const char *src
, char endchar
)
978 int count
= sizeof(WCHAR
); /* for terminating null */
980 while (*p
&& *p
!= endchar
)
982 if (*p
!= '\\') *dest
= (WCHAR
)*p
++;
988 case 'a': *dest
= '\a'; p
++; break;
989 case 'b': *dest
= '\b'; p
++; break;
990 case 'e': *dest
= '\e'; p
++; break;
991 case 'f': *dest
= '\f'; p
++; break;
992 case 'n': *dest
= '\n'; p
++; break;
993 case 'r': *dest
= '\r'; p
++; break;
994 case 't': *dest
= '\t'; p
++; break;
995 case 'v': *dest
= '\v'; p
++; break;
996 case 'x': /* hex escape */
998 if (!isxdigit(*p
)) *dest
= 'x';
1001 *dest
= to_hex(*p
++);
1002 if (isxdigit(*p
)) *dest
= (*dest
* 16) + to_hex(*p
++);
1003 if (isxdigit(*p
)) *dest
= (*dest
* 16) + to_hex(*p
++);
1004 if (isxdigit(*p
)) *dest
= (*dest
* 16) + to_hex(*p
++);
1014 case '7': /* octal escape */
1016 if (*p
>= '0' && *p
<= '7') *dest
= (*dest
* 8) + (*p
++ - '0');
1017 if (*p
>= '0' && *p
<= '7') *dest
= (*dest
* 8) + (*p
++ - '0');
1020 *dest
= (WCHAR
)*p
++;
1024 if ((count
+= sizeof(WCHAR
)) > *len
) return -1; /* dest buffer overflow */
1028 if (!*p
) return -1; /* delimiter not found */
1033 /* convert a data type tag to a value type */
1034 static int get_data_type( const char *buffer
, int *type
, int *parse_type
)
1036 struct data_type
{ const char *tag
; int len
; int type
; int parse_type
; };
1038 static const struct data_type data_types
[] =
1039 { /* actual type */ /* type to assume for parsing */
1040 { "\"", 1, REG_SZ
, REG_SZ
},
1041 { "str:\"", 5, REG_SZ
, REG_SZ
},
1042 { "str(2):\"", 8, REG_EXPAND_SZ
, REG_SZ
},
1043 { "str(7):\"", 8, REG_MULTI_SZ
, REG_SZ
},
1044 { "hex:", 4, REG_BINARY
, REG_BINARY
},
1045 { "dword:", 6, REG_DWORD
, REG_DWORD
},
1046 { "hex(", 4, -1, REG_BINARY
},
1050 const struct data_type
*ptr
;
1053 for (ptr
= data_types
; ptr
->tag
; ptr
++)
1055 if (memcmp( ptr
->tag
, buffer
, ptr
->len
)) continue;
1056 *parse_type
= ptr
->parse_type
;
1057 if ((*type
= ptr
->type
) != -1) return ptr
->len
;
1058 /* "hex(xx):" is special */
1059 *type
= (int)strtoul( buffer
+ 4, &end
, 16 );
1060 if ((end
<= buffer
) || memcmp( end
, "):", 2 )) return 0;
1061 return end
+ 2 - buffer
;
1066 /* load and create a key from the input file */
1067 static struct key
*load_key( struct key
*base
, const char *buffer
, struct file_load_info
*info
)
1070 int res
, len
, modif
;
1072 len
= strlen(buffer
) * sizeof(WCHAR
);
1073 if (!get_file_tmp_space( info
, len
)) return NULL
;
1075 if ((res
= parse_strW( (WCHAR
*)info
->tmp
, &len
, buffer
, ']' )) == -1)
1077 file_read_error( "Malformed key", info
);
1080 if (!sscanf( buffer
+ res
, " %d", &modif
)) modif
= time(NULL
);
1082 for (p
= (WCHAR
*)info
->tmp
; *p
; p
++) if (*p
== '\\') { p
++; break; }
1083 return create_key( base
, p
, len
- ((char *)p
- info
->tmp
), NULL
, 0, modif
, &res
);
1086 /* parse a comma-separated list of hex digits */
1087 static int parse_hex( unsigned char *dest
, int *len
, const char *buffer
)
1089 const char *p
= buffer
;
1091 while (isxdigit(*p
))
1095 memcpy( buf
, p
, 2 );
1097 sscanf( buf
, "%x", &val
);
1098 if (count
++ >= *len
) return -1; /* dest buffer overflow */
1099 *dest
++ = (unsigned char )val
;
1107 /* parse a value name and create the corresponding value */
1108 static struct key_value
*parse_value_name( struct key
*key
, const char *buffer
, int *len
,
1109 struct file_load_info
*info
)
1111 int maxlen
= strlen(buffer
) * sizeof(WCHAR
);
1112 if (!get_file_tmp_space( info
, maxlen
)) return NULL
;
1113 if (buffer
[0] == '@')
1115 info
->tmp
[0] = info
->tmp
[1] = 0;
1120 if ((*len
= parse_strW( (WCHAR
*)info
->tmp
, &maxlen
, buffer
+ 1, '\"' )) == -1) goto error
;
1121 (*len
)++; /* for initial quote */
1123 if (buffer
[*len
] != '=') goto error
;
1125 return insert_value( key
, (WCHAR
*)info
->tmp
);
1128 file_read_error( "Malformed value name", info
);
1132 /* load a value from the input file */
1133 static int load_value( struct key
*key
, const char *buffer
, struct file_load_info
*info
)
1137 int maxlen
, len
, res
;
1138 int type
, parse_type
;
1139 struct key_value
*value
;
1141 if (!(value
= parse_value_name( key
, buffer
, &len
, info
))) return 0;
1142 if (!(res
= get_data_type( buffer
+ len
, &type
, &parse_type
))) goto error
;
1143 buffer
+= len
+ res
;
1148 len
= strlen(buffer
) * sizeof(WCHAR
);
1149 if (!get_file_tmp_space( info
, len
)) return 0;
1150 if ((res
= parse_strW( (WCHAR
*)info
->tmp
, &len
, buffer
, '\"' )) == -1) goto error
;
1154 dw
= strtoul( buffer
, NULL
, 16 );
1158 case REG_BINARY
: /* hex digits */
1162 maxlen
= 1 + strlen(buffer
)/3; /* 3 chars for one hex byte */
1163 if (!get_file_tmp_space( info
, len
+ maxlen
)) return 0;
1164 if ((res
= parse_hex( info
->tmp
+ len
, &maxlen
, buffer
)) == -1) goto error
;
1167 while (isspace(*buffer
)) buffer
++;
1168 if (!*buffer
) break;
1169 if (*buffer
!= '\\') goto error
;
1170 if (read_next_line( info
) != 1) goto error
;
1171 buffer
= info
->buffer
;
1172 while (isspace(*buffer
)) buffer
++;
1178 ptr
= NULL
; /* keep compiler quiet */
1182 if (!len
) newptr
= NULL
;
1183 else if (!(newptr
= memdup( ptr
, len
))) return 0;
1185 if (value
->data
) free( value
->data
);
1186 value
->data
= newptr
;
1189 /* update the key level but not the modification time */
1190 key
->level
= max( key
->level
, current_level
);
1194 file_read_error( "Malformed value", info
);
1198 /* load all the keys from the input file */
1199 static void load_keys( struct key
*key
, FILE *f
)
1201 struct key
*subkey
= NULL
;
1202 struct file_load_info info
;
1209 if (!(info
.buffer
= mem_alloc( info
.len
))) return;
1210 if (!(info
.tmp
= mem_alloc( info
.tmplen
)))
1212 free( info
.buffer
);
1216 if ((read_next_line( &info
) != 1) ||
1217 strcmp( info
.buffer
, "WINE REGISTRY Version 2" ))
1219 set_error( STATUS_NOT_REGISTRY_FILE
);
1223 while (read_next_line( &info
) == 1)
1225 for (p
= info
.buffer
; *p
&& isspace(*p
); p
++);
1228 case '[': /* new key */
1229 if (subkey
) release_object( subkey
);
1230 subkey
= load_key( key
, p
+ 1, &info
);
1232 case '@': /* default value */
1233 case '\"': /* value */
1234 if (subkey
) load_value( subkey
, p
, &info
);
1235 else file_read_error( "Value without key", &info
);
1237 case '#': /* comment */
1238 case ';': /* comment */
1239 case 0: /* empty line */
1242 file_read_error( "Unrecognized input", &info
);
1248 if (subkey
) release_object( subkey
);
1249 free( info
.buffer
);
1253 /* load a part of the registry from a file */
1254 static void load_registry( struct key
*key
, int handle
)
1259 if (!(obj
= get_handle_obj( current
->process
, handle
, GENERIC_READ
, NULL
))) return;
1260 fd
= obj
->ops
->get_read_fd( obj
);
1261 release_object( obj
);
1264 FILE *f
= fdopen( fd
, "r" );
1267 load_keys( key
, f
);
1270 else file_set_error();
1274 /* update the level of the parents of a key (only needed for the old format) */
1275 static int update_level( struct key
*key
)
1278 int max
= key
->level
;
1279 for (i
= 0; i
<= key
->last_subkey
; i
++)
1281 int sub
= update_level( key
->subkeys
[i
] );
1282 if (sub
> max
) max
= sub
;
1288 /* dump a string to a registry save file in the old v1 format */
1289 static void save_string_v1( LPCWSTR str
, FILE *f
, int len
)
1292 while ((len
== -1) ? *str
: (*str
&& len
--))
1294 if ((*str
> 0x7f) || (*str
== '\n') || (*str
== '='))
1295 fprintf( f
, "\\u%04x", *str
);
1298 if (*str
== '\\') fputc( '\\', f
);
1299 fputc( (char)*str
, f
);
1305 /* save a registry and all its subkeys to a text file in the old v1 format */
1306 static void save_subkeys_v1( struct key
*key
, int nesting
, FILE *f
)
1310 if (key
->flags
& KEY_VOLATILE
) return;
1311 if (key
->level
< saving_level
) return;
1312 for (i
= 0; i
<= key
->last_value
; i
++)
1314 struct key_value
*value
= &key
->values
[i
];
1315 for (j
= nesting
; j
> 0; j
--) fputc( '\t', f
);
1316 save_string_v1( value
->name
, f
, -1 );
1317 fprintf( f
, "=%d,%d,", value
->type
, 0 );
1318 if (value
->type
== REG_SZ
|| value
->type
== REG_EXPAND_SZ
)
1319 save_string_v1( (LPWSTR
)value
->data
, f
, value
->len
/ 2 );
1321 for (j
= 0; j
< value
->len
; j
++)
1322 fprintf( f
, "%02x", *((unsigned char *)value
->data
+ j
) );
1325 for (i
= 0; i
<= key
->last_subkey
; i
++)
1327 for (j
= nesting
; j
> 0; j
--) fputc( '\t', f
);
1328 save_string_v1( key
->subkeys
[i
]->name
, f
, -1 );
1330 save_subkeys_v1( key
->subkeys
[i
], nesting
+ 1, f
);
1334 /* save a registry branch to a file handle */
1335 static void save_registry( struct key
*key
, int handle
)
1340 if (key
->flags
& KEY_DELETED
)
1342 set_error( STATUS_KEY_DELETED
);
1345 if (!(obj
= get_handle_obj( current
->process
, handle
, GENERIC_WRITE
, NULL
))) return;
1346 fd
= obj
->ops
->get_write_fd( obj
);
1347 release_object( obj
);
1350 FILE *f
= fdopen( fd
, "w" );
1353 fprintf( f
, "WINE REGISTRY Version %d\n", saving_version
);
1354 if (saving_version
== 2) save_subkeys( key
, key
, f
);
1357 update_level( key
);
1358 save_subkeys_v1( key
, 0, f
);
1360 if (fclose( f
)) file_set_error();
1370 /* create a registry key */
1371 DECL_HANDLER(create_key
)
1373 struct key
*key
, *parent
;
1375 unsigned int access
= req
->access
;
1377 if (access
& MAXIMUM_ALLOWED
) access
= KEY_ALL_ACCESS
; /* FIXME: needs general solution */
1379 if ((parent
= get_hkey_obj( req
->parent
, KEY_CREATE_SUB_KEY
)))
1381 if ((class = req_strdupW( req
->class )))
1383 if ((key
= create_key( parent
, req
->name
, sizeof(req
->name
), class, req
->options
,
1384 req
->modif
, &req
->created
)))
1386 req
->hkey
= alloc_handle( current
->process
, key
, access
, 0 );
1387 release_object( key
);
1391 release_object( parent
);
1395 /* open a registry key */
1396 DECL_HANDLER(open_key
)
1398 struct key
*key
, *parent
;
1399 unsigned int access
= req
->access
;
1401 if (access
& MAXIMUM_ALLOWED
) access
= KEY_ALL_ACCESS
; /* FIXME: needs general solution */
1403 if ((parent
= get_hkey_obj( req
->parent
, 0 /*FIXME*/ )))
1405 if ((key
= open_key( parent
, req
->name
, sizeof(req
->name
) )))
1407 req
->hkey
= alloc_handle( current
->process
, key
, access
, 0 );
1408 release_object( key
);
1410 release_object( parent
);
1414 /* delete a registry key */
1415 DECL_HANDLER(delete_key
)
1419 if ((key
= get_hkey_obj( req
->hkey
, KEY_CREATE_SUB_KEY
/*FIXME*/ )))
1421 delete_key( key
, req
->name
, sizeof(req
->name
) );
1422 release_object( key
);
1426 /* close a registry key */
1427 DECL_HANDLER(close_key
)
1429 int hkey
= req
->hkey
;
1430 /* ignore attempts to close a root key */
1431 if (!IS_ROOT_HKEY(hkey
)) close_handle( current
->process
, hkey
);
1434 /* enumerate registry subkeys */
1435 DECL_HANDLER(enum_key
)
1439 if ((key
= get_hkey_obj( req
->hkey
, KEY_ENUMERATE_SUB_KEYS
)))
1441 enum_key( key
, req
->index
, req
->name
, req
->class, &req
->modif
);
1442 release_object( key
);
1446 /* query information about a registry key */
1447 DECL_HANDLER(query_key_info
)
1451 if ((key
= get_hkey_obj( req
->hkey
, KEY_QUERY_VALUE
)))
1453 query_key( key
, req
);
1454 release_object( key
);
1458 /* set a value of a registry key */
1459 DECL_HANDLER(set_key_value
)
1462 int max
= get_req_size( req
->data
, sizeof(req
->data
[0]) );
1463 int datalen
= req
->len
;
1466 set_error( STATUS_NO_MEMORY
); /* FIXME */
1469 if ((key
= get_hkey_obj( req
->hkey
, KEY_SET_VALUE
)))
1471 set_value( key
, copy_path( req
->name
), req
->type
, datalen
, req
->data
);
1472 release_object( key
);
1476 /* retrieve the value of a registry key */
1477 DECL_HANDLER(get_key_value
)
1481 if ((key
= get_hkey_obj( req
->hkey
, KEY_QUERY_VALUE
)))
1483 get_value( key
, copy_path( req
->name
), &req
->type
, &req
->len
, req
->data
);
1484 release_object( key
);
1488 /* enumerate the value of a registry key */
1489 DECL_HANDLER(enum_key_value
)
1493 if ((key
= get_hkey_obj( req
->hkey
, KEY_QUERY_VALUE
)))
1495 enum_value( key
, req
->index
, req
->name
, &req
->type
, &req
->len
, req
->data
);
1496 release_object( key
);
1500 /* delete a value of a registry key */
1501 DECL_HANDLER(delete_key_value
)
1506 if ((key
= get_hkey_obj( req
->hkey
, KEY_SET_VALUE
)))
1508 if ((name
= req_strdupW( req
->name
)))
1510 delete_value( key
, name
);
1513 release_object( key
);
1517 /* load a registry branch from a file */
1518 DECL_HANDLER(load_registry
)
1522 if ((key
= get_hkey_obj( req
->hkey
, KEY_SET_VALUE
| KEY_CREATE_SUB_KEY
)))
1524 /* FIXME: use subkey name */
1525 load_registry( key
, req
->file
);
1526 release_object( key
);
1530 /* save a registry branch to a file */
1531 DECL_HANDLER(save_registry
)
1535 if ((key
= get_hkey_obj( req
->hkey
, KEY_QUERY_VALUE
| KEY_ENUMERATE_SUB_KEYS
)))
1537 save_registry( key
, req
->file
);
1538 release_object( key
);
1542 /* set the current and saving level for the registry */
1543 DECL_HANDLER(set_registry_levels
)
1545 current_level
= req
->current
;
1546 saving_level
= req
->saving
;
1547 saving_version
= req
->version
;