4 * Copyright 1996 Marcus Meissner
5 * Copyright 1998 Matthew Becker
6 * Copyright 1999 Sylvain St-Germain
8 * December 21, 1997 - Kevin Cozens
9 * Fixed bugs in the _w95_loadreg() function. Added extra information
10 * regarding the format of the Windows '95 registry files.
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * When changing this file, please re-run the regtest program to ensure
28 * the conditions are handled properly.
33 * Time for RegEnumKey*, RegQueryInfoKey*
37 #include "wine/port.h"
46 #include <sys/types.h>
49 #ifdef HAVE_SYS_MMAN_H
50 # include <sys/mman.h>
57 #include "wine/winbase16.h"
58 #include "wine/library.h"
59 #include "wine/server.h"
60 #include "wine/unicode.h"
63 #include "wine/debug.h"
65 WINE_DEFAULT_DEBUG_CHANNEL(reg);
67 #define SAVE_GLOBAL_REGBRANCH_USER_DEFAULT "/wine.userreg"
68 #define SAVE_GLOBAL_REGBRANCH_LOCAL_MACHINE "/wine.systemreg"
70 /* relative in ~user/.wine/ : */
71 #define SAVE_LOCAL_REGBRANCH_CURRENT_USER "user.reg"
72 #define SAVE_LOCAL_REGBRANCH_USER_DEFAULT "userdef.reg"
73 #define SAVE_LOCAL_REGBRANCH_LOCAL_MACHINE "system.reg"
75 static const WCHAR ClassesRootW[] = {'M','a','c','h','i','n','e','\\',
76 'S','o','f','t','w','a','r','e','\\',
77 'C','l','a','s','s','e','s',0};
79 #define IS_OPTION_FALSE(ch) \
80 ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
82 /* _xmalloc [Internal] */
83 static void *_xmalloc( size_t size )
87 res = malloc (size ? size : 1);
89 WARN("Virtual memory exhausted.\n");
95 /* _strdupnA [Internal] */
96 static LPSTR _strdupnA(LPCSTR str,size_t len)
100 if (!str) return NULL;
101 ret = _xmalloc( len + 1 );
102 memcpy( ret, str, len );
107 /* convert ansi string to unicode [Internal] */
108 static LPWSTR _strdupnAtoW(LPCSTR strA,size_t lenA)
113 if (!strA) return NULL;
114 lenW = MultiByteToWideChar(CP_ACP,0,strA,lenA,NULL,0);
115 ret = _xmalloc(lenW*sizeof(WCHAR)+sizeof(WCHAR));
116 MultiByteToWideChar(CP_ACP,0,strA,lenA,ret,lenW);
121 /* dump a Unicode string with proper escaping [Internal] */
122 /* FIXME: this code duplicates server/unicode.c */
123 static int _dump_strW(const WCHAR *str,size_t len,FILE *f,char escape[2])
125 static const char escapes[32] = ".......abtnvfr.............e....";
130 for (; len; str++, len--)
132 if (pos > buffer + sizeof(buffer) - 8)
134 fwrite( buffer, pos - buffer, 1, f );
135 count += pos - buffer;
138 if (*str > 127) /* hex escape */
140 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
141 pos += sprintf( pos, "\\x%04x", *str );
143 pos += sprintf( pos, "\\x%x", *str );
146 if (*str < 32) /* octal or C escape */
148 if (!*str && len == 1) continue; /* do not output terminating NULL */
149 if (escapes[*str] != '.')
150 pos += sprintf( pos, "\\%c", escapes[*str] );
151 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
152 pos += sprintf( pos, "\\%03o", *str );
154 pos += sprintf( pos, "\\%o", *str );
157 if (*str == '\\' || *str == escape[0] || *str == escape[1]) *pos++ = '\\';
160 fwrite( buffer, pos - buffer, 1, f );
161 count += pos - buffer;
165 /* convert ansi string to unicode and dump with proper escaping [Internal] */
166 static int _dump_strAtoW(LPCSTR strA,size_t len,FILE *f,char escape[2])
171 if (strA == NULL) return 0;
172 strW = _strdupnAtoW(strA,len);
173 ret = _dump_strW(strW,len,f,escape);
179 /* FIXME: this code duplicates server/registry.c */
181 WCHAR *nameW; /* value name */
182 int type; /* value type */
183 size_t len; /* value data length in bytes */
184 void *data; /* pointer to value data */
187 /* dump a value to a text file */
188 /* FIXME: this code duplicates server/registry.c */
189 static void _dump_value(struct key_value *value,FILE *f)
193 if (value->nameW[0]) {
195 count = 1 + _dump_strW(value->nameW,strlenW(value->nameW),f,"\"\"");
196 count += fprintf( f, "\"=" );
198 else count = fprintf( f, "@=" );
200 switch(value->type) {
204 if (value->type != REG_SZ) fprintf( f, "str(%d):", value->type );
206 if (value->data) _dump_strW(value->data,value->len/sizeof(WCHAR),f,"\"\"");
210 if (value->len == sizeof(DWORD)) {
212 memcpy( &dw, value->data, sizeof(DWORD) );
213 fprintf( f, "dword:%08lx", dw );
216 /* else fall through */
218 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
219 else count += fprintf( f, "hex(%x):", value->type );
220 for (i = 0; i < value->len; i++) {
221 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
222 if (i < value->len-1) {
225 fprintf( f, "\\\n " );
235 /******************************************************************/
236 /* WINDOWS 31 REGISTRY LOADER, supplied by Tor Sjøwall, tor@sn.no */
238 reghack - windows 3.11 registry data format demo program.
240 The reg.dat file has 3 parts, a header, a table of 8-byte entries that is
241 a combined hash table and tree description, and finally a text table.
243 The header is obvious from the struct header. The taboff1 and taboff2
244 fields are always 0x20, and their usage is unknown.
246 The 8-byte entry table has various entry types.
248 tabent[0] is a root index. The second word has the index of the root of
250 tabent[1..hashsize] is a hash table. The first word in the hash entry is
251 the index of the key/value that has that hash. Data with the same
252 hash value are on a circular list. The other three words in the
253 hash entry are always zero.
254 tabent[hashsize..tabcnt] is the tree structure. There are two kinds of
255 entry: dirent and keyent/valent. They are identified by context.
256 tabent[freeidx] is the first free entry. The first word in a free entry
257 is the index of the next free entry. The last has 0 as a link.
258 The other three words in the free list are probably irrelevant.
260 Entries in text table are preceded by a word at offset-2. This word
261 has the value (2*index)+1, where index is the referring keyent/valent
262 entry in the table. I have no suggestion for the 2* and the +1.
263 Following the word, there are N bytes of data, as per the keyent/valent
264 entry length. The offset of the keyent/valent entry is from the start
265 of the text table to the first data byte.
267 This information is not available from Microsoft. The data format is
268 deduced from the reg.dat file by me. Mistakes may
269 have been made. I claim no rights and give no guarantees for this program.
271 Tor Sjøwall, tor@sn.no
274 /* reg.dat header format */
276 char cookie[8]; /* 'SHCC3.10' */
277 unsigned long taboff1; /* offset of hash table (??) = 0x20 */
278 unsigned long taboff2; /* offset of index table (??) = 0x20 */
279 unsigned long tabcnt; /* number of entries in index table */
280 unsigned long textoff; /* offset of text part */
281 unsigned long textsize; /* byte size of text part */
282 unsigned short hashsize; /* hash size */
283 unsigned short freeidx; /* free index */
286 /* generic format of table entries */
288 unsigned short w0, w1, w2, w3;
291 /* directory tabent: */
293 unsigned short sibling_idx; /* table index of sibling dirent */
294 unsigned short child_idx; /* table index of child dirent */
295 unsigned short key_idx; /* table index of key keyent */
296 unsigned short value_idx; /* table index of value valent */
301 unsigned short hash_idx; /* hash chain index for string */
302 unsigned short refcnt; /* reference count */
303 unsigned short length; /* length of string */
304 unsigned short string_off; /* offset of string in text table */
309 unsigned short hash_idx; /* hash chain index for string */
310 unsigned short refcnt; /* reference count */
311 unsigned short length; /* length of string */
312 unsigned short string_off; /* offset of string in text table */
315 /* recursive helper function to display a directory tree [Internal] */
316 void _w31_dumptree(unsigned short idx,unsigned char *txt,struct _w31_tabent *tab,struct _w31_header *head,HKEY hkey,time_t lastmodified, int level)
318 static const WCHAR classesW[] = {'.','c','l','a','s','s','e','s',0};
319 struct _w31_dirent *dir;
320 struct _w31_keyent *key;
321 struct _w31_valent *val;
323 OBJECT_ATTRIBUTES attr;
324 UNICODE_STRING nameW, valueW;
325 static WCHAR tail[400];
327 attr.Length = sizeof(attr);
328 attr.RootDirectory = hkey;
329 attr.ObjectName = &nameW;
331 attr.SecurityDescriptor = NULL;
332 attr.SecurityQualityOfService = NULL;
333 RtlInitUnicodeString( &valueW, NULL );
336 dir=(struct _w31_dirent*)&tab[idx];
340 key = (struct _w31_keyent*)&tab[dir->key_idx];
342 RtlMultiByteToUnicodeN( tail, sizeof(tail)-sizeof(WCHAR), &len,
343 &txt[key->string_off], key->length);
344 tail[len/sizeof(WCHAR)] = 0;
346 /* all toplevel entries AND the entries in the
347 * toplevel subdirectory belong to \SOFTWARE\Classes
349 if (!level && !strcmpW(tail,classesW))
351 _w31_dumptree(dir->child_idx,txt,tab,head,hkey,lastmodified,level+1);
352 idx=dir->sibling_idx;
356 if (subkey) NtClose( subkey );
357 RtlInitUnicodeString( &nameW, tail );
358 if (NtCreateKey( &subkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) subkey = 0;
360 /* only add if leaf node or valued node */
361 if (dir->value_idx!=0||dir->child_idx==0) {
362 if (dir->value_idx) {
364 val=(struct _w31_valent*)&tab[dir->value_idx];
365 RtlMultiByteToUnicodeN( tail, sizeof(tail) - sizeof(WCHAR), &len,
366 &txt[val->string_off], val->length);
367 tail[len/sizeof(WCHAR)] = 0;
368 NtSetValueKey( subkey, &valueW, 0, REG_SZ, tail, len + sizeof(WCHAR) );
371 } else TRACE("strange: no directory key name, idx=%04x\n", idx);
372 _w31_dumptree(dir->child_idx,txt,tab,head,subkey,lastmodified,level+1);
373 idx=dir->sibling_idx;
375 if (subkey) NtClose( subkey );
379 /******************************************************************************
380 * _w31_loadreg [Internal]
382 void _w31_loadreg(void)
386 OBJECT_ATTRIBUTES attr;
387 UNICODE_STRING nameW;
388 struct _w31_header head;
389 struct _w31_tabent *tab;
393 BY_HANDLE_FILE_INFORMATION hfinfo;
398 hf = OpenFile("reg.dat",&ofs,OF_READ);
399 if (hf==HFILE_ERROR) return;
401 /* read & dump header */
402 if (sizeof(head)!=_lread(hf,&head,sizeof(head))) {
403 ERR("reg.dat is too short.\n");
407 if (memcmp(head.cookie, "SHCC3.10", sizeof(head.cookie))!=0) {
408 ERR("reg.dat has bad signature.\n");
413 len = head.tabcnt * sizeof(struct _w31_tabent);
414 /* read and dump index table */
416 if (len!=_lread(hf,tab,len)) {
417 ERR("couldn't read %d bytes.\n",len);
424 txt = _xmalloc(head.textsize);
425 if (-1==_llseek(hf,head.textoff,SEEK_SET)) {
426 ERR("couldn't seek to textblock.\n");
432 if (head.textsize!=_lread(hf,txt,head.textsize)) {
433 ERR("textblock too short (%d instead of %ld).\n",len,head.textsize);
440 if (!GetFileInformationByHandle((HANDLE)hf,&hfinfo)) {
441 ERR("GetFileInformationByHandle failed?.\n");
447 lastmodified = DOSFS_FileTimeToUnixTime(&hfinfo.ftLastWriteTime,NULL);
449 attr.Length = sizeof(attr);
450 attr.RootDirectory = 0;
451 attr.ObjectName = &nameW;
453 attr.SecurityDescriptor = NULL;
454 attr.SecurityQualityOfService = NULL;
455 RtlInitUnicodeString( &nameW, ClassesRootW );
457 if (!NtCreateKey( &root, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
459 _w31_dumptree(tab[0].w1,txt,tab,&head,root,lastmodified,0);
468 /***********************************************************************************/
469 /* windows 95 registry loader */
470 /***********************************************************************************/
472 /* SECTION 1: main header
476 #define W95_REG_CREG_ID 0x47455243
479 DWORD id; /* "CREG" = W95_REG_CREG_ID */
480 DWORD version; /* ???? 0x00010000 */
481 DWORD rgdb_off; /* 0x08 Offset of 1st RGDB-block */
482 DWORD uk2; /* 0x0c */
483 WORD rgdb_num; /* 0x10 # of RGDB-blocks */
489 /* SECTION 2: Directory information (tree structure)
491 * once on offset 0x20
493 * structure: [rgkn][dke]* (repeat till last_dke is reached)
495 #define W95_REG_RGKN_ID 0x4e4b4752
498 DWORD id; /*"RGKN" = W95_REG_RGKN_ID */
499 DWORD size; /* Size of the RGKN-block */
500 DWORD root_off; /* Rel. Offset of the root-record */
501 DWORD last_dke; /* Offset to last DKE ? */
505 /* Disk Key Entry Structure
507 * the 1st entry in a "usual" registry file is a nul-entry with subkeys: the
508 * hive itself. It looks the same like other keys. Even the ID-number can
511 * The "hash"-value is a value representing the key's name. Windows will not
512 * search for the name, but for a matching hash-value. if it finds one, it
513 * will compare the actual string info, otherwise continue with the next key.
514 * To calculate the hash initialize a D-Word with 0 and add all ASCII-values
515 * of the string which are smaller than 0x80 (128) to this D-Word.
517 * If you want to modify key names, also modify the hash-values, since they
518 * cannot be found again (although they would be displayed in REGEDIT)
519 * End of list-pointers are filled with 0xFFFFFFFF
521 * Disk keys are layed out flat ... But, sometimes, nrLS and nrMS are both
522 * 0xFFFF, which means skipping over nextkeyoffset bytes (including this
523 * structure) and reading another RGDB_section.
525 * The last DKE (see field last_dke in _w95_rgkn) has only 3 DWORDs with
526 * 0x80000000 (EOL indicator ?) as x1, the hash value and 0xFFFFFFFF as x3.
527 * The remaining space between last_dke and the offset calculated from
528 * rgkn->size seems to be free for use for more dke:s.
529 * So it seems if more dke:s are added, they are added to that space and
530 * last_dke is grown, and in case that "free" space is out, the space
531 * gets grown and rgkn->size gets adjusted.
533 * there is a one to one relationship between dke and dkh
535 /* key struct, once per key */
537 DWORD x1; /* Free entry indicator(?) */
538 DWORD hash; /* sum of bytes of keyname */
539 DWORD x3; /* Root key indicator? usually 0xFFFFFFFF */
540 DWORD prevlvl; /* offset of previous key */
541 DWORD nextsub; /* offset of child key */
542 DWORD next; /* offset of sibling key */
543 WORD nrLS; /* id inside the rgdb block */
544 WORD nrMS; /* number of the rgdb block */
547 /* SECTION 3: key information, values and data
550 * section: [blocks]* (repeat creg->rgdb_num times)
551 * blocks: [rgdb] [subblocks]* (repeat till block size reached )
552 * subblocks: [dkh] [dkv]* (repeat dkh->values times )
554 * An interesting relationship exists in RGDB_section. The DWORD value
555 * at offset 0x10 equals the one at offset 0x04 minus the one at offset 0x08.
556 * I have no idea at the moment what this means. (Kevin Cozens)
559 /* block header, once per block */
560 #define W95_REG_RGDB_ID 0x42444752
563 DWORD id; /* 0x00 'RGDB' = W95_REG_RGDB_ID */
564 DWORD size; /* 0x04 */
565 DWORD uk1; /* 0x08 */
566 DWORD uk2; /* 0x0c */
567 DWORD uk3; /* 0x10 */
568 DWORD uk4; /* 0x14 */
569 DWORD uk5; /* 0x18 */
570 DWORD uk6; /* 0x1c */
574 /* Disk Key Header structure (RGDB part), once per key */
576 DWORD nextkeyoff; /* 0x00 offset to next dkh */
577 WORD nrLS; /* 0x04 id inside the rgdb block */
578 WORD nrMS; /* 0x06 number of the rgdb block */
579 DWORD bytesused; /* 0x08 */
580 WORD keynamelen; /* 0x0c len of name */
581 WORD values; /* 0x0e number of values */
582 DWORD xx1; /* 0x10 */
583 char name[1]; /* 0x14 */
584 /* dkv */ /* 0x14 + keynamelen */
587 /* Disk Key Value structure, once per value */
589 DWORD type; /* 0x00 */
591 WORD valnamelen; /* 0x08 length of name, 0 is default key */
592 WORD valdatalen; /* 0x0A length of data */
593 char name[1]; /* 0x0c */
594 /* raw data */ /* 0x0c + valnamelen */
597 /******************************************************************************
598 * _w95_lookup_dkh [Internal]
600 * seeks the dkh belonging to a dke
602 static _w95dkh *_w95_lookup_dkh(_w95creg *creg,int nrLS,int nrMS)
608 /* get the beginning of the rgdb datastore */
609 rgdb = (_w95rgdb*)((char*)creg+creg->rgdb_off);
611 /* check: requested block < last_block) */
612 if (creg->rgdb_num <= nrMS) {
613 ERR("registry file corrupt! requested block no. beyond end.\n");
617 /* find the right block */
618 for(i=0; i<nrMS ;i++) {
619 if(rgdb->id != W95_REG_RGDB_ID) { /* check the magic */
620 ERR("registry file corrupt! bad magic 0x%08lx\n", rgdb->id);
623 rgdb = (_w95rgdb*) ((char*)rgdb+rgdb->size); /* find next block */
626 dkh = (_w95dkh*)(rgdb + 1); /* first sub block within the rgdb */
629 if(nrLS==dkh->nrLS ) return dkh;
630 dkh = (_w95dkh*)((char*)dkh + dkh->nextkeyoff); /* find next subblock */
631 } while ((char *)dkh < ((char*)rgdb+rgdb->size));
637 /******************************************************************************
638 * _w95_dump_dkv [Internal]
640 static int _w95_dump_dkv(_w95dkh *dkh,int nrLS,int nrMS,FILE *f)
645 /* first value block */
646 dkv = (_w95dkv*)((char*)dkh+dkh->keynamelen+0x14);
648 /* loop through the values */
649 for (i=0; i< dkh->values; i++) {
650 struct key_value value;
653 value.nameW = _strdupnAtoW(dkv->name,dkv->valnamelen);
654 value.type = dkv->type;
655 value.len = dkv->valdatalen;
657 value.data = &(dkv->name[dkv->valnamelen]);
659 if ( (value.type==REG_SZ) || (value.type==REG_EXPAND_SZ) || (value.type==REG_MULTI_SZ) ) {
660 pdata = _strdupnAtoW(value.data,value.len);
663 if (pdata != NULL) value.data = pdata;
665 _dump_value(&value,f);
667 if (pdata != NULL) free(pdata);
670 dkv = (_w95dkv*)((char*)dkv+dkv->valnamelen+dkv->valdatalen+0x0c);
675 /******************************************************************************
676 * _w95_dump_dke [Internal]
678 static int _w95_dump_dke(LPSTR key_name,_w95creg *creg,_w95rgkn *rgkn,_w95dke *dke,FILE *f,int level)
681 LPSTR new_key_name = NULL;
683 /* special root key */
684 if (dke->nrLS == 0xffff || dke->nrMS==0xffff) /* eg. the root key has no name */
686 /* parse the one subkey */
687 if (dke->nextsub != 0xffffffff) return _w95_dump_dke(key_name, creg, rgkn, (_w95dke*)((char*)rgkn+dke->nextsub),f,level);
688 /* has no sibling keys */
692 /* search subblock */
693 if (!(dkh = _w95_lookup_dkh(creg, dke->nrLS, dke->nrMS))) {
694 ERR("dke pointing to missing dkh !\n");
699 /* create new subkey name */
700 new_key_name = _strdupnA(key_name,strlen(key_name)+dkh->keynamelen+1);
701 if (strcmp(new_key_name,"") != 0) strcat(new_key_name,"\\");
702 strncat(new_key_name,dkh->name,dkh->keynamelen);
704 /* walk sibling keys */
705 if (dke->next != 0xffffffff ) {
706 if (!_w95_dump_dke(key_name, creg, rgkn, (_w95dke*)((char*)rgkn+dke->next),f,level)) {
712 /* write the key path (something like [Software\\Microsoft\\..]) only if:
713 1) key has some values
714 2) key has no values and no subkeys
716 if (dkh->values > 0) {
717 /* there are some values */
719 _dump_strAtoW(new_key_name,strlen(new_key_name),f,"[]");
721 if (!_w95_dump_dkv(dkh, dke->nrLS, dke->nrMS,f)) {
726 if ((dke->nextsub == 0xffffffff) && (dkh->values == 0)) {
727 /* no subkeys and no values */
729 _dump_strAtoW(new_key_name,strlen(new_key_name),f,"[]");
732 } else new_key_name = _strdupnA(key_name,strlen(key_name));
735 if (dke->nextsub != 0xffffffff) {
736 if (!_w95_dump_dke(new_key_name, creg, rgkn, (_w95dke*)((char*)rgkn+dke->nextsub),f,level-1)) {
745 /* end windows 95 loader */
747 /***********************************************************************************/
748 /* windows NT registry loader */
749 /***********************************************************************************/
751 /* NT REGISTRY LOADER */
753 #ifdef HAVE_SYS_MMAN_H
754 # include <sys/mman.h>
758 #define MAP_FAILED ((LPVOID)-1)
761 #define NT_REG_BLOCK_SIZE 0x1000
763 #define NT_REG_HEADER_BLOCK_ID 0x66676572 /* regf */
764 #define NT_REG_POOL_BLOCK_ID 0x6E696268 /* hbin */
765 #define NT_REG_KEY_BLOCK_ID 0x6b6e /* nk */
766 #define NT_REG_VALUE_BLOCK_ID 0x6b76 /* vk */
768 /* subblocks of nk */
769 #define NT_REG_HASH_BLOCK_ID 0x666c /* lf */
770 #define NT_REG_NOHASH_BLOCK_ID 0x696c /* li */
771 #define NT_REG_RI_BLOCK_ID 0x6972 /* ri */
773 #define NT_REG_KEY_BLOCK_TYPE 0x20
774 #define NT_REG_ROOT_KEY_BLOCK_TYPE 0x2c
777 DWORD id; /* 0x66676572 'regf'*/
778 DWORD uk1; /* 0x04 */
779 DWORD uk2; /* 0x08 */
780 FILETIME DateModified; /* 0x0c */
781 DWORD uk3; /* 0x14 */
782 DWORD uk4; /* 0x18 */
783 DWORD uk5; /* 0x1c */
784 DWORD uk6; /* 0x20 */
785 DWORD RootKeyBlock; /* 0x24 */
786 DWORD BlockSize; /* 0x28 */
788 DWORD Checksum; /* at offset 0x1FC */
797 DWORD id; /* 0x6E696268 'hbin' */
801 DWORD uk2; /* 0x10 */
802 DWORD uk3; /* 0x14 */
803 DWORD uk4; /* 0x18 */
804 DWORD size; /* 0x1C */
805 nt_hbin_sub hbin_sub; /* 0x20 */
809 * the value_list consists of offsets to the values (vk)
812 WORD SubBlockId; /* 0x00 0x6B6E */
813 WORD Type; /* 0x02 for the root-key: 0x2C, otherwise 0x20*/
814 FILETIME writetime; /* 0x04 */
815 DWORD uk1; /* 0x0C */
816 DWORD parent_off; /* 0x10 Offset of Owner/Parent key */
817 DWORD nr_subkeys; /* 0x14 number of sub-Keys */
818 DWORD uk8; /* 0x18 */
819 DWORD lf_off; /* 0x1C Offset of the sub-key lf-Records */
820 DWORD uk2; /* 0x20 */
821 DWORD nr_values; /* 0x24 number of values */
822 DWORD valuelist_off; /* 0x28 Offset of the Value-List */
823 DWORD off_sk; /* 0x2c Offset of the sk-Record */
824 DWORD off_class; /* 0x30 Offset of the Class-Name */
825 DWORD uk3; /* 0x34 */
826 DWORD uk4; /* 0x38 */
827 DWORD uk5; /* 0x3c */
828 DWORD uk6; /* 0x40 */
829 DWORD uk7; /* 0x44 */
830 WORD name_len; /* 0x48 name-length */
831 WORD class_len; /* 0x4a class-name length */
832 char name[1]; /* 0x4c key-name */
836 DWORD off_nk; /* 0x00 */
837 DWORD name; /* 0x04 */
841 WORD id; /* 0x00 0x666c */
842 WORD nr_keys; /* 0x06 */
843 hash_rec hash_rec[1];
847 list of subkeys without hash
854 WORD id; /* 0x00 0x696c */
860 this is a intermediate node
871 WORD id; /* 0x00 0x6972 */
872 WORD nr_li; /* 0x02 number off offsets */
873 DWORD off_li[1]; /* 0x04 points to li */
877 WORD id; /* 0x00 'vk' */
891 * 0 value is a default value
892 * 1 the value has a name
895 * len of the whole data block
897 * bytes including the terminating \0 = 2*(number_of_chars+1)
898 * - reg_dword, reg_binary:
899 * if highest bit of data_len is set data_off contains the value
901 static int _nt_dump_vk(LPSTR key_name, char *base, nt_vk *vk,FILE *f)
903 BYTE *pdata = (BYTE *)(base+vk->data_off+4); /* start of data */
904 struct key_value value;
906 if (vk->id != NT_REG_VALUE_BLOCK_ID) {
907 ERR("unknown block found (0x%04x), please report!\n", vk->id);
911 value.nameW = _strdupnAtoW(vk->name,vk->nam_len);
912 value.type = vk->type;
913 value.len = (vk->data_len & 0x7fffffff);
914 value.data = (vk->data_len & 0x80000000) ? (LPBYTE)&(vk->data_off): pdata;
916 _dump_value(&value,f);
922 /* it's called from _nt_dump_lf() */
923 static int _nt_dump_nk(LPSTR key_name,char *base,nt_nk *nk,FILE *f,int level);
928 * this structure contains the hash of a keyname and points to all
931 * exception: if the id is 'il' there are no hash values and every
934 static int _nt_dump_lf(LPSTR key_name, char *base, int subkeys, nt_lf *lf, FILE *f, int level)
938 if (lf->id == NT_REG_HASH_BLOCK_ID) {
939 if (subkeys != lf->nr_keys) goto error1;
941 for (i=0; i<lf->nr_keys; i++)
942 if (!_nt_dump_nk(key_name, base, (nt_nk*)(base+lf->hash_rec[i].off_nk+4), f, level)) goto error;
943 } else if (lf->id == NT_REG_NOHASH_BLOCK_ID) {
944 nt_li * li = (nt_li*)lf;
945 if (subkeys != li->nr_keys) goto error1;
947 for (i=0; i<li->nr_keys; i++)
948 if (!_nt_dump_nk(key_name, base, (nt_nk*)(base+li->off_nk[i]+4), f, level)) goto error;
949 } else if (lf->id == NT_REG_RI_BLOCK_ID) { /* ri */
950 nt_ri * ri = (nt_ri*)lf;
953 /* count all subkeys */
954 for (i=0; i<ri->nr_li; i++) {
955 nt_li * li = (nt_li*)(base+ri->off_li[i]+4);
956 if(li->id != NT_REG_NOHASH_BLOCK_ID) goto error2;
957 li_subkeys += li->nr_keys;
961 if (subkeys != li_subkeys) goto error1;
963 /* loop through the keys */
964 for (i=0; i<ri->nr_li; i++) {
965 nt_li *li = (nt_li*)(base+ri->off_li[i]+4);
966 if (!_nt_dump_lf(key_name, base, li->nr_keys, (nt_lf*)li, f, level)) goto error;
973 if (lf->id == 0x686c)
974 FIXME("unknown Win XP node id 0x686c: do we need to add support for it ?\n");
976 ERR("unknown node id 0x%04x, please report!\n", lf->id);
980 ERR("registry file corrupt! (inconsistent number of subkeys)\n");
984 ERR("error reading lf block\n");
988 /* _nt_dump_nk [Internal] */
989 static int _nt_dump_nk(LPSTR key_name,char *base,nt_nk *nk,FILE *f,int level)
993 LPSTR new_key_name = NULL;
995 TRACE("%s\n", key_name);
997 if (nk->SubBlockId != NT_REG_KEY_BLOCK_ID) {
998 ERR("unknown node id 0x%04x, please report!\n", nk->SubBlockId);
1002 if ((nk->Type!=NT_REG_ROOT_KEY_BLOCK_TYPE) && (((nt_nk*)(base+nk->parent_off+4))->SubBlockId != NT_REG_KEY_BLOCK_ID)) {
1003 ERR("registry file corrupt!\n");
1007 /* create the new key */
1009 /* create new subkey name */
1010 new_key_name = _strdupnA(key_name,strlen(key_name)+nk->name_len+1);
1011 if (strcmp(new_key_name,"") != 0) strcat(new_key_name,"\\");
1012 strncat(new_key_name,nk->name,nk->name_len);
1014 /* write the key path (something like [Software\\Microsoft\\..]) only if:
1015 1) key has some values
1016 2) key has no values and no subkeys
1018 if (nk->nr_values > 0) {
1019 /* there are some values */
1021 _dump_strAtoW(new_key_name,strlen(new_key_name),f,"[]");
1024 if ((nk->nr_subkeys == 0) && (nk->nr_values == 0)) {
1025 /* no subkeys and no values */
1027 _dump_strAtoW(new_key_name,strlen(new_key_name),f,"[]");
1031 /* loop trough the value list */
1032 vl = (DWORD *)(base+nk->valuelist_off+4);
1033 for (n=0; n<nk->nr_values; n++) {
1034 nt_vk * vk = (nt_vk*)(base+vl[n]+4);
1035 if (!_nt_dump_vk(new_key_name, base, vk, f)) {
1040 } else new_key_name = _strdupnA(key_name,strlen(key_name));
1042 /* loop through the subkeys */
1043 if (nk->nr_subkeys) {
1044 nt_lf *lf = (nt_lf*)(base+nk->lf_off+4);
1045 if (!_nt_dump_lf(new_key_name, base, nk->nr_subkeys, lf, f, level-1)) {
1057 /**********************************************************************************
1058 * _set_registry_levels [Internal]
1060 * set level to 0 for loading system files
1061 * set level to 1 for loading user files
1063 static void _set_registry_levels(int level,int saving,int period)
1065 SERVER_START_REQ( set_registry_levels )
1067 req->current = level;
1068 req->saving = saving;
1069 req->period = period;
1070 wine_server_call( req );
1075 /* _save_at_exit [Internal] */
1076 static void _save_at_exit(HKEY hkey,LPCSTR path)
1078 LPCSTR confdir = wine_get_config_dir();
1080 SERVER_START_REQ( save_registry_atexit )
1083 wine_server_add_data( req, confdir, strlen(confdir) );
1084 wine_server_add_data( req, path, strlen(path)+1 );
1085 wine_server_call( req );
1090 /******************************************************************************
1091 * _allocate_default_keys [Internal]
1092 * Registry initialisation, allocates some default keys.
1094 static void _allocate_default_keys(void)
1096 static const WCHAR StatDataW[] = {'D','y','n','D','a','t','a','\\',
1097 'P','e','r','f','S','t','a','t','s','\\',
1098 'S','t','a','t','D','a','t','a',0};
1100 OBJECT_ATTRIBUTES attr;
1101 UNICODE_STRING nameW;
1105 attr.Length = sizeof(attr);
1106 attr.RootDirectory = 0;
1107 attr.ObjectName = &nameW;
1108 attr.Attributes = 0;
1109 attr.SecurityDescriptor = NULL;
1110 attr.SecurityQualityOfService = NULL;
1112 RtlInitUnicodeString( &nameW, StatDataW );
1113 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) NtClose( hkey );
1116 #define REG_DONTLOAD -1
1121 /* return the type of native registry [Internal] */
1122 static int _get_reg_type(void)
1124 WCHAR windir[MAX_PATHNAME_LEN];
1125 WCHAR tmp[MAX_PATHNAME_LEN];
1126 int ret = REG_WIN31;
1127 static const WCHAR nt_reg_pathW[] = {'\\','s','y','s','t','e','m','3','2','\\','c','o','n','f','i','g','\\','s','y','s','t','e','m',0};
1128 static const WCHAR win9x_reg_pathW[] = {'\\','s','y','s','t','e','m','.','d','a','t',0};
1130 GetWindowsDirectoryW(windir, MAX_PATHNAME_LEN);
1132 /* test %windir%/system32/config/system --> winnt */
1133 strcpyW(tmp, windir);
1134 strcatW(tmp, nt_reg_pathW);
1135 if(GetFileAttributesW(tmp) != (DWORD)-1)
1139 /* test %windir%/system.dat --> win95 */
1140 strcpyW(tmp, windir);
1141 strcatW(tmp, win9x_reg_pathW);
1142 if(GetFileAttributesW(tmp) != (DWORD)-1)
1149 /* load the registry file in wine format [Internal] */
1150 static void load_wine_registry(HKEY hkey,LPCSTR fn)
1153 if ((file = FILE_CreateFile( fn, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1154 FILE_ATTRIBUTE_NORMAL, 0, TRUE, DRIVE_UNKNOWN )))
1156 SERVER_START_REQ( load_registry )
1160 wine_server_call( req );
1163 CloseHandle( file );
1167 /* generate and return the name of the tmp file and associated stream [Internal] */
1168 static LPSTR _get_tmp_fn(FILE **f)
1175 sprintf(ret,"/tmp/reg%lx%04x.tmp",(long)getpid(),count++);
1176 if ((tmp_fd = open(ret,O_CREAT | O_EXCL | O_WRONLY,0666)) != -1) break;
1177 if (errno != EEXIST) {
1178 ERR("Unexpected error while open() call: %s\n",strerror(errno));
1185 if ((*f = fdopen(tmp_fd,"w")) == NULL) {
1186 ERR("Unexpected error while fdopen() call: %s\n",strerror(errno));
1195 /* convert win95 native registry file to wine format [Internal] */
1196 static LPSTR _convert_win95_registry_to_wine_format(LPCWSTR fn, int level)
1200 DOS_FULL_NAME full_name;
1207 _w95dke *dke, *root_dke;
1209 if (!DOSFS_GetFullName( fn, 0, &full_name )) return NULL;
1211 /* map the registry into the memory */
1212 if ((fd = open(full_name.long_name, O_RDONLY | O_NONBLOCK)) == -1) return NULL;
1213 if ((fstat(fd, &st) == -1)) goto error1;
1214 if (!st.st_size) goto error1;
1215 if ((base = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) goto error1;
1217 /* control signature */
1218 if (*(LPDWORD)base != W95_REG_CREG_ID) {
1219 ERR("unable to load native win95 registry file %s: unknown signature.\n",
1225 /* load the header (rgkn) */
1226 rgkn = (_w95rgkn*)(creg + 1);
1227 if (rgkn->id != W95_REG_RGKN_ID) {
1228 ERR("second IFF header not RGKN, but %lx\n", rgkn->id);
1231 if (rgkn->root_off != 0x20) {
1232 ERR("rgkn->root_off not 0x20, please report !\n");
1235 if (rgkn->last_dke > rgkn->size)
1237 ERR("registry file corrupt! last_dke > size!\n");
1240 /* verify last dke */
1241 dke = (_w95dke*)((char*)rgkn + rgkn->last_dke);
1242 if (dke->x1 != 0x80000000)
1244 ERR("last dke invalid !\n");
1247 if (rgkn->size > creg->rgdb_off)
1249 ERR("registry file corrupt! rgkn size > rgdb_off !\n");
1252 root_dke = (_w95dke*)((char*)rgkn + rgkn->root_off);
1253 if ( (root_dke->prevlvl != 0xffffffff) || (root_dke->next != 0xffffffff) )
1255 ERR("registry file corrupt! invalid root dke !\n");
1259 if ( (ret = _get_tmp_fn(&f)) == NULL) goto error;
1260 fprintf(f,"WINE REGISTRY Version 2");
1261 _w95_dump_dke("",creg,rgkn,root_dke,f,level);
1266 ERR("Unable to load native win95 registry file %s.\n", debugstr_w(fn));
1267 ERR("Please report this.\n");
1268 ERR("Make a backup of the file, run a good reg cleaner program and try again!\n");
1271 munmap(base, st.st_size);
1277 /* convert winnt native registry file to wine format [Internal] */
1278 static LPSTR _convert_winnt_registry_to_wine_format(LPCWSTR fn, int level)
1285 OBJECT_ATTRIBUTES attr;
1286 LARGE_INTEGER lg_int;
1292 nt_hbin_sub *hbin_sub;
1295 TRACE("%s\n", debugstr_w(fn));
1297 hFile = CreateFileW( fn, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1298 if ( hFile == INVALID_HANDLE_VALUE ) return NULL;
1299 attr.Length = sizeof(attr);
1300 attr.RootDirectory = 0;
1301 attr.ObjectName = NULL;
1302 attr.Attributes = 0;
1303 attr.SecurityDescriptor = NULL;
1304 attr.SecurityQualityOfService = NULL;
1306 lg_int.QuadPart = 0;
1307 nts = NtCreateSection( &hMapping,
1308 STANDARD_RIGHTS_REQUIRED|SECTION_QUERY|SECTION_MAP_READ,
1309 &attr, &lg_int, PAGE_READONLY, SEC_COMMIT, hFile );
1310 if (nts != STATUS_SUCCESS) goto error1;
1312 base = NULL; len = 0;
1313 nts = NtMapViewOfSection( hMapping, GetCurrentProcess(),
1314 &base, 0, 0, &lg_int, &len, ViewShare, 0,
1316 NtClose( hMapping );
1317 if (nts != STATUS_SUCCESS) goto error1;
1319 /* control signature */
1320 if (*(LPDWORD)base != NT_REG_HEADER_BLOCK_ID) {
1321 ERR("unable to load native winnt registry file %s: unknown signature.\n",
1330 hbin = (nt_hbin*)((char*) base + 0x1000);
1331 if (hbin->id != NT_REG_POOL_BLOCK_ID) {
1332 ERR( "hbin block invalid\n");
1336 /* hbin_sub block */
1337 hbin_sub = (nt_hbin_sub*)&(hbin->hbin_sub);
1338 if ((hbin_sub->data[0] != 'n') || (hbin_sub->data[1] != 'k')) {
1339 ERR( "hbin_sub block invalid\n");
1344 nk = (nt_nk*)&(hbin_sub->data[0]);
1345 if (nk->Type != NT_REG_ROOT_KEY_BLOCK_TYPE) {
1346 ERR( "special nk block not found\n");
1350 if ( (ret = _get_tmp_fn(&f)) == NULL) goto error;
1351 fprintf(f,"WINE REGISTRY Version 2");
1352 _nt_dump_nk("",(char*)base+0x1000,nk,f,level);
1356 NtUnmapViewOfSection( GetCurrentProcess(), base );
1362 /* convert native registry to wine format and load it via server call [Internal] */
1363 static void _convert_and_load_native_registry(LPCWSTR fn, HKEY hkey, int reg_type, int level)
1369 /* FIXME: following function doesn't really convert yet */
1370 tmp = _convert_winnt_registry_to_wine_format(fn,level);
1373 tmp = _convert_win95_registry_to_wine_format(fn,level);
1376 ERR("Don't know how to convert native 3.1 registry yet.\n");
1379 ERR("Unknown registry format parameter (%d)\n",reg_type);
1384 load_wine_registry(hkey,tmp);
1385 TRACE("File %s successfully converted to %s and loaded to registry.\n",
1386 debugstr_w(fn), tmp);
1389 else WARN("Unable to convert %s (doesn't exist?)\n", debugstr_w(fn));
1393 /* load all native windows registry files [Internal] */
1394 static void _load_windows_registry( HKEY hkey_local_machine, HKEY hkey_current_user,
1395 HKEY hkey_users_default )
1398 WCHAR windir[MAX_PATHNAME_LEN];
1399 WCHAR path[MAX_PATHNAME_LEN];
1400 OBJECT_ATTRIBUTES attr;
1401 UNICODE_STRING nameW;
1404 static const WCHAR WineW[] = {'W','i','n','e',0};
1405 static const WCHAR ProfileW[] = {'P','r','o','f','i','l','e',0};
1406 static const WCHAR empty_strW[] = { 0 };
1407 static const WCHAR System[] = {'M','a','c','h','i','n','e','\\','S','y','s','t','e','m',0};
1408 static const WCHAR Software[] = {'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e',0};
1409 static const WCHAR Clone[] = {'M','a','c','h','i','n','e','\\',
1410 'S','y','s','t','e','m','\\',
1411 'C','l','o','n','e',0};
1413 attr.Length = sizeof(attr);
1414 attr.RootDirectory = 0;
1415 attr.ObjectName = &nameW;
1416 attr.Attributes = 0;
1417 attr.SecurityDescriptor = NULL;
1418 attr.SecurityQualityOfService = NULL;
1420 GetWindowsDirectoryW(windir, MAX_PATHNAME_LEN);
1422 reg_type = _get_reg_type();
1426 static const WCHAR ntuser_datW[] = {'\\','n','t','u','s','e','r','.','d','a','t',0};
1427 static const WCHAR defaultW[] = {'\\','s','y','s','t','e','m','3','2','\\','c','o','n','f','i','g','\\','d','e','f','a','u','l','t',0};
1428 static const WCHAR systemW[] = {'\\','s','y','s','t','e','m','3','2','\\','c','o','n','f','i','g','\\','s','y','s','t','e','m',0};
1429 static const WCHAR softwareW[] = {'\\','s','y','s','t','e','m','3','2','\\','c','o','n','f','i','g','\\','s','o','f','t','w','a','r','e',0};
1430 static const WCHAR samW[] = {'\\','s','y','s','t','e','m','3','2','\\','c','o','n','f','i','g','\\','s','a','m',0};
1431 static const WCHAR securityW[] = {'\\','s','y','s','t','e','m','3','2','\\','c','o','n','f','i','g','\\','s','e','c','u','r','i','t','y',0};
1433 /* user specific ntuser.dat */
1434 if (PROFILE_GetWineIniString( WineW, ProfileW, empty_strW, path, MAX_PATHNAME_LEN )) {
1435 strcatW(path, ntuser_datW);
1436 _convert_and_load_native_registry(path,hkey_current_user,REG_WINNT,1);
1440 MESSAGE("When you are running with a native NT directory specify\n");
1441 MESSAGE("'Profile=<profiledirectory>' or disable loading of Windows\n");
1442 MESSAGE("registry (LoadWindowsRegistryFiles=N)\n");
1446 /* default user.dat */
1447 if (hkey_users_default) {
1448 strcpyW(path, windir);
1449 strcatW(path, defaultW);
1450 _convert_and_load_native_registry(path,hkey_users_default,REG_WINNT,1);
1455 * map HLM\System\ControlSet001 to HLM\System\CurrentControlSet
1457 RtlInitUnicodeString( &nameW, System );
1458 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
1460 strcpyW(path, windir);
1461 strcatW(path, systemW);
1462 _convert_and_load_native_registry(path,hkey,REG_WINNT,1);
1465 RtlInitUnicodeString( &nameW, Software );
1466 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
1468 strcpyW(path, windir);
1469 strcatW(path, softwareW);
1470 _convert_and_load_native_registry(path,hkey,REG_WINNT,1);
1474 strcpyW(path, windir);
1475 strcatW(path, samW);
1476 _convert_and_load_native_registry(path,hkey_local_machine,REG_WINNT,0);
1478 strcpyW(path,windir);
1479 strcatW(path, securityW);
1480 _convert_and_load_native_registry(path,hkey_local_machine,REG_WINNT,0);
1482 /* this key is generated when the nt-core booted successfully */
1483 RtlInitUnicodeString( &nameW, Clone );
1484 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) NtClose( hkey );
1490 static const WCHAR system_1stW[] = {'c',':','\\','s','y','s','t','e','m','.','1','s','t',0};
1491 static const WCHAR system_datW[] = {'\\','s','y','s','t','e','m','.','d','a','t',0};
1492 static const WCHAR classes_datW[] = {'\\','c','l','a','s','s','e','s','.','d','a','t',0};
1493 static const WCHAR user_datW[] = {'\\','u','s','e','r','.','d','a','t',0};
1495 _convert_and_load_native_registry(system_1stW,hkey_local_machine,REG_WIN95,0);
1497 strcpyW(path, windir);
1498 strcatW(path, system_datW);
1499 _convert_and_load_native_registry(path,hkey_local_machine,REG_WIN95,0);
1501 RtlInitUnicodeString( &nameW, ClassesRootW );
1502 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
1504 strcpyW(path, windir);
1505 strcatW(path, classes_datW);
1506 _convert_and_load_native_registry(path,hkey,REG_WIN95,0);
1510 if (PROFILE_GetWineIniString(WineW, ProfileW, empty_strW, path, MAX_PATHNAME_LEN)) {
1511 /* user specific user.dat */
1512 strcatW(path, user_datW);
1513 _convert_and_load_native_registry(path,hkey_current_user,REG_WIN95,1);
1515 /* default user.dat */
1516 if (hkey_users_default) {
1517 strcpyW(path, windir);
1518 strcatW(path, user_datW);
1519 _convert_and_load_native_registry(path,hkey_users_default,REG_WIN95,1);
1522 strcpyW(path, windir);
1523 strcatW(path, user_datW);
1524 _convert_and_load_native_registry(path,hkey_current_user,REG_WIN95,1);
1530 /* FIXME: here we should convert to *.reg file supported by server and call REQ_LOAD_REGISTRY, see REG_WIN95 case */
1535 TRACE("REG_DONTLOAD\n");
1539 ERR("switch: no match (%d)\n",reg_type);
1545 /* load home registry files (stored in ~/.wine) [Internal] */
1546 static void _load_home_registry( HKEY hkey_local_machine, HKEY hkey_current_user,
1547 HKEY hkey_users_default )
1549 LPCSTR confdir = wine_get_config_dir();
1550 LPSTR tmp = _xmalloc(strlen(confdir)+20);
1552 strcpy(tmp,confdir);
1553 strcat(tmp,"/" SAVE_LOCAL_REGBRANCH_USER_DEFAULT);
1554 load_wine_registry(hkey_users_default,tmp);
1556 strcpy(tmp,confdir);
1557 strcat(tmp,"/" SAVE_LOCAL_REGBRANCH_CURRENT_USER);
1558 load_wine_registry(hkey_current_user,tmp);
1560 strcpy(tmp,confdir);
1561 strcat(tmp,"/" SAVE_LOCAL_REGBRANCH_LOCAL_MACHINE);
1562 load_wine_registry(hkey_local_machine,tmp);
1568 /* load all registry (native and global and home) */
1569 void SHELL_LoadRegistry( void )
1571 HKEY hkey_local_machine, hkey_users, hkey_users_default, hkey_current_user, hkey_config;
1572 OBJECT_ATTRIBUTES attr;
1573 UNICODE_STRING nameW;
1579 static const WCHAR MachineW[] = {'M','a','c','h','i','n','e',0};
1580 static const WCHAR UserW[] = {'U','s','e','r',0};
1581 static const WCHAR DefaultW[] = {'.','D','e','f','a','u','l','t',0};
1582 static const WCHAR RegistryW[] = {'M','a','c','h','i','n','e','\\',
1583 'S','o','f','t','w','a','r','e','\\',
1584 'W','i','n','e','\\',
1585 'W','i','n','e','\\',
1586 'C','o','n','f','i','g','\\',
1587 'R','e','g','i','s','t','r','y',0};
1588 static const WCHAR load_win_reg_filesW[] = {'L','o','a','d','W','i','n','d','o','w','s','R','e','g','i','s','t','r','y','F','i','l','e','s',0};
1589 static const WCHAR load_global_reg_filesW[] = {'L','o','a','d','G','l','o','b','a','l','R','e','g','i','s','t','r','y','F','i','l','e','s',0};
1590 static const WCHAR load_home_reg_filesW[] = {'L','o','a','d','H','o','m','e','R','e','g','i','s','t','r','y','F','i','l','e','s',0};
1591 static const WCHAR SaveOnlyUpdatedKeysW[] = {'S','a','v','e','O','n','l','y','U','p','d','a','t','e','d','K','e','y','s',0};
1592 static const WCHAR PeriodicSaveW[] = {'P','e','r','i','o','d','i','c','S','a','v','e',0};
1593 static const WCHAR WritetoHomeRegistryFilesW[] = {'W','r','i','t','e','t','o','H','o','m','e','R','e','g','i','s','t','r','y','F','i','l','e','s',0};
1594 static const WCHAR GlobalRegistryDirW[] = {'G','l','o','b','a','l','R','e','g','i','s','t','r','y','D','i','r',0};
1598 if (!CLIENT_IsBootThread()) return; /* already loaded */
1600 attr.Length = sizeof(attr);
1601 attr.RootDirectory = 0;
1602 attr.ObjectName = &nameW;
1603 attr.Attributes = 0;
1604 attr.SecurityDescriptor = NULL;
1605 attr.SecurityQualityOfService = NULL;
1607 RtlInitUnicodeString( &nameW, MachineW );
1608 NtCreateKey( &hkey_local_machine, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL );
1609 RtlInitUnicodeString( &nameW, UserW );
1610 NtCreateKey( &hkey_users, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL );
1612 attr.RootDirectory = hkey_users;
1613 RtlInitUnicodeString( &nameW, DefaultW );
1614 if (NtCreateKey( &hkey_users_default, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
1616 ERR("Cannot create HKEY_USERS/.Default\n" );
1619 RtlOpenCurrentUser( KEY_ALL_ACCESS, &hkey_current_user );
1621 _set_registry_levels(0,0,0);
1622 _allocate_default_keys();
1624 attr.RootDirectory = 0;
1625 RtlInitUnicodeString( &nameW, RegistryW );
1626 if (NtOpenKey( &hkey_config, KEY_ALL_ACCESS, &attr )) hkey_config = 0;
1628 /* load windows registry if required */
1631 attr.RootDirectory = hkey_config;
1632 RtlInitUnicodeString( &nameW, load_win_reg_filesW );
1633 if (!NtQueryValueKey( hkey_config, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &count ))
1635 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
1636 res = !IS_OPTION_FALSE(str[0]);
1638 if (res) _load_windows_registry( hkey_local_machine, hkey_current_user, hkey_users_default );
1640 /* load global registry if required */
1643 RtlInitUnicodeString( &nameW, load_global_reg_filesW );
1644 if (!NtQueryValueKey( hkey_config, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &count ))
1646 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
1647 res = !IS_OPTION_FALSE(str[0]);
1651 /* load global registry files (stored in /etc/wine) */
1652 char *p, configfile[MAX_PATHNAME_LEN];
1654 /* Override ETCDIR? */
1656 RtlInitUnicodeString( &nameW, GlobalRegistryDirW );
1657 if (!NtQueryValueKey( hkey_config, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &count ))
1659 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
1660 WideCharToMultiByte(CP_ACP, 0, str, -1, configfile, sizeof(configfile), NULL, NULL);
1662 if (configfile[0] != '/') strcpy(configfile, ETCDIR);
1664 TRACE("GlobalRegistryDir is '%s'.\n", configfile);
1666 /* Load the global HKU hive directly from sysconfdir */
1667 p = configfile + strlen(configfile);
1668 strcpy(p, SAVE_GLOBAL_REGBRANCH_USER_DEFAULT);
1669 load_wine_registry( hkey_users, configfile );
1671 /* Load the global machine defaults directly from sysconfdir */
1672 strcpy(p, SAVE_GLOBAL_REGBRANCH_LOCAL_MACHINE);
1673 load_wine_registry( hkey_local_machine, configfile );
1676 _set_registry_levels(1,0,0);
1678 /* load home registry if required */
1681 RtlInitUnicodeString( &nameW, load_home_reg_filesW );
1682 if (!NtQueryValueKey( hkey_config, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &count ))
1684 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
1685 res = !IS_OPTION_FALSE(str[0]);
1687 if (res) _load_home_registry( hkey_local_machine, hkey_current_user, hkey_users_default );
1689 /* setup registry saving */
1692 RtlInitUnicodeString( &nameW, SaveOnlyUpdatedKeysW );
1693 if (!NtQueryValueKey( hkey_config, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &count ))
1695 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
1696 all = IS_OPTION_FALSE(str[0]);
1700 RtlInitUnicodeString( &nameW, PeriodicSaveW );
1701 if (!NtQueryValueKey( hkey_config, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &count ))
1703 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
1704 period = (int)strtolW(str, NULL, 10);
1707 /* set saving level (0 for saving everything, 1 for saving only modified keys) */
1708 _set_registry_levels(1,!all,period*1000);
1710 /* setup keys to save */
1713 RtlInitUnicodeString( &nameW, WritetoHomeRegistryFilesW );
1714 if (!NtQueryValueKey( hkey_config, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &count ))
1716 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
1717 res = !IS_OPTION_FALSE(str[0]);
1721 _save_at_exit(hkey_current_user,"/" SAVE_LOCAL_REGBRANCH_CURRENT_USER );
1722 _save_at_exit(hkey_local_machine,"/" SAVE_LOCAL_REGBRANCH_LOCAL_MACHINE);
1723 _save_at_exit(hkey_users_default,"/" SAVE_LOCAL_REGBRANCH_USER_DEFAULT);
1726 NtClose(hkey_users_default);
1727 NtClose(hkey_current_user);
1728 NtClose(hkey_users);
1729 NtClose(hkey_local_machine);
1730 NtClose(hkey_config);