Updated module definitions for Perl unit tests.
[wine.git] / misc / registry.c
blob49ef205ee3c6eb64034b9b3b489b4d225898c9d3
1 /*
2 * Registry Functions
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
26 * NOTES
27 * When changing this file, please re-run the regtest program to ensure
28 * the conditions are handled properly.
30 * TODO
31 * Security access
32 * Option handling
33 * Time for RegEnumKey*, RegQueryInfoKey*
36 #include "config.h"
37 #include "wine/port.h"
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <unistd.h>
43 #include <errno.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <fcntl.h>
47 #ifdef HAVE_SYS_MMAN_H
48 # include <sys/mman.h>
49 #endif
51 #include "winerror.h"
52 #include "winnt.h"
53 #include "winreg.h"
55 #include "wine/winbase16.h"
56 #include "wine/server.h"
57 #include "wine/unicode.h"
58 #include "file.h"
59 #include "options.h"
61 #include "wine/debug.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(reg);
65 /* FIXME: following defines should be configured global */
66 #define SAVE_GLOBAL_REGBRANCH_USER_DEFAULT ETCDIR"/wine.userreg"
67 #define SAVE_GLOBAL_REGBRANCH_LOCAL_MACHINE ETCDIR"/wine.systemreg"
69 /* relative in ~user/.wine/ : */
70 #define SAVE_LOCAL_REGBRANCH_CURRENT_USER "user.reg"
71 #define SAVE_LOCAL_REGBRANCH_USER_DEFAULT "userdef.reg"
72 #define SAVE_LOCAL_REGBRANCH_LOCAL_MACHINE "system.reg"
74 /* _xmalloc [Internal] */
75 static void *_xmalloc( size_t size )
77 void *res;
79 res = malloc (size ? size : 1);
80 if (res == NULL) {
81 WARN("Virtual memory exhausted.\n");
82 exit (1);
84 return res;
87 /* _strdupnA [Internal] */
88 static LPSTR _strdupnA(LPCSTR str,size_t len)
90 LPSTR ret;
92 if (!str) return NULL;
93 ret = _xmalloc( len + 1 );
94 memcpy( ret, str, len );
95 ret[len] = 0x00;
96 return ret;
99 /* convert ansi string to unicode [Internal] */
100 static LPWSTR _strdupnAtoW(LPCSTR strA,size_t lenA)
102 LPWSTR ret;
103 size_t lenW;
105 if (!strA) return NULL;
106 lenW = MultiByteToWideChar(CP_ACP,0,strA,lenA,NULL,0);
107 ret = _xmalloc(lenW*sizeof(WCHAR)+sizeof(WCHAR));
108 MultiByteToWideChar(CP_ACP,0,strA,lenA,ret,lenW);
109 ret[lenW] = 0;
110 return ret;
113 /* dump a Unicode string with proper escaping [Internal] */
114 /* FIXME: this code duplicates server/unicode.c */
115 static int _dump_strW(const WCHAR *str,size_t len,FILE *f,char escape[2])
117 static const char escapes[32] = ".......abtnvfr.............e....";
118 char buffer[256];
119 LPSTR pos = buffer;
120 int count = 0;
122 for (; len; str++, len--)
124 if (pos > buffer + sizeof(buffer) - 8)
126 fwrite( buffer, pos - buffer, 1, f );
127 count += pos - buffer;
128 pos = buffer;
130 if (*str > 127) /* hex escape */
132 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
133 pos += sprintf( pos, "\\x%04x", *str );
134 else
135 pos += sprintf( pos, "\\x%x", *str );
136 continue;
138 if (*str < 32) /* octal or C escape */
140 if (!*str && len == 1) continue; /* do not output terminating NULL */
141 if (escapes[*str] != '.')
142 pos += sprintf( pos, "\\%c", escapes[*str] );
143 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
144 pos += sprintf( pos, "\\%03o", *str );
145 else
146 pos += sprintf( pos, "\\%o", *str );
147 continue;
149 if (*str == '\\' || *str == escape[0] || *str == escape[1]) *pos++ = '\\';
150 *pos++ = *str;
152 fwrite( buffer, pos - buffer, 1, f );
153 count += pos - buffer;
154 return count;
157 /* convert ansi string to unicode and dump with proper escaping [Internal] */
158 static int _dump_strAtoW(LPCSTR strA,size_t len,FILE *f,char escape[2])
160 WCHAR *strW;
161 int ret;
163 if (strA == NULL) return 0;
164 strW = _strdupnAtoW(strA,len);
165 ret = _dump_strW(strW,len,f,escape);
166 free(strW);
167 return ret;
170 /* a key value */
171 /* FIXME: this code duplicates server/registry.c */
172 struct key_value {
173 WCHAR *nameW; /* value name */
174 int type; /* value type */
175 size_t len; /* value data length in bytes */
176 void *data; /* pointer to value data */
179 /* dump a value to a text file */
180 /* FIXME: this code duplicates server/registry.c */
181 static void _dump_value(struct key_value *value,FILE *f)
183 int i, count;
185 if (value->nameW[0]) {
186 fputc( '\"', f );
187 count = 1 + _dump_strW(value->nameW,strlenW(value->nameW),f,"\"\"");
188 count += fprintf( f, "\"=" );
190 else count = fprintf( f, "@=" );
192 switch(value->type) {
193 case REG_SZ:
194 case REG_EXPAND_SZ:
195 case REG_MULTI_SZ:
196 if (value->type != REG_SZ) fprintf( f, "str(%d):", value->type );
197 fputc( '\"', f );
198 if (value->data) _dump_strW(value->data,value->len/sizeof(WCHAR),f,"\"\"");
199 fputc( '\"', f );
200 break;
201 case REG_DWORD:
202 if (value->len == sizeof(DWORD)) {
203 DWORD dw;
204 memcpy( &dw, value->data, sizeof(DWORD) );
205 fprintf( f, "dword:%08lx", dw );
206 break;
208 /* else fall through */
209 default:
210 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
211 else count += fprintf( f, "hex(%x):", value->type );
212 for (i = 0; i < value->len; i++) {
213 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
214 if (i < value->len-1) {
215 fputc( ',', f );
216 if (++count > 76) {
217 fprintf( f, "\\\n " );
218 count = 2;
222 break;
224 fputc( '\n', f );
227 /******************************************************************/
228 /* WINDOWS 31 REGISTRY LOADER, supplied by Tor Sjøwall, tor@sn.no */
230 reghack - windows 3.11 registry data format demo program.
232 The reg.dat file has 3 parts, a header, a table of 8-byte entries that is
233 a combined hash table and tree description, and finally a text table.
235 The header is obvious from the struct header. The taboff1 and taboff2
236 fields are always 0x20, and their usage is unknown.
238 The 8-byte entry table has various entry types.
240 tabent[0] is a root index. The second word has the index of the root of
241 the directory.
242 tabent[1..hashsize] is a hash table. The first word in the hash entry is
243 the index of the key/value that has that hash. Data with the same
244 hash value are on a circular list. The other three words in the
245 hash entry are always zero.
246 tabent[hashsize..tabcnt] is the tree structure. There are two kinds of
247 entry: dirent and keyent/valent. They are identified by context.
248 tabent[freeidx] is the first free entry. The first word in a free entry
249 is the index of the next free entry. The last has 0 as a link.
250 The other three words in the free list are probably irrelevant.
252 Entries in text table are preceded by a word at offset-2. This word
253 has the value (2*index)+1, where index is the referring keyent/valent
254 entry in the table. I have no suggestion for the 2* and the +1.
255 Following the word, there are N bytes of data, as per the keyent/valent
256 entry length. The offset of the keyent/valent entry is from the start
257 of the text table to the first data byte.
259 This information is not available from Microsoft. The data format is
260 deduced from the reg.dat file by me. Mistakes may
261 have been made. I claim no rights and give no guarantees for this program.
263 Tor Sjøwall, tor@sn.no
266 /* reg.dat header format */
267 struct _w31_header {
268 char cookie[8]; /* 'SHCC3.10' */
269 unsigned long taboff1; /* offset of hash table (??) = 0x20 */
270 unsigned long taboff2; /* offset of index table (??) = 0x20 */
271 unsigned long tabcnt; /* number of entries in index table */
272 unsigned long textoff; /* offset of text part */
273 unsigned long textsize; /* byte size of text part */
274 unsigned short hashsize; /* hash size */
275 unsigned short freeidx; /* free index */
278 /* generic format of table entries */
279 struct _w31_tabent {
280 unsigned short w0, w1, w2, w3;
283 /* directory tabent: */
284 struct _w31_dirent {
285 unsigned short sibling_idx; /* table index of sibling dirent */
286 unsigned short child_idx; /* table index of child dirent */
287 unsigned short key_idx; /* table index of key keyent */
288 unsigned short value_idx; /* table index of value valent */
291 /* key tabent: */
292 struct _w31_keyent {
293 unsigned short hash_idx; /* hash chain index for string */
294 unsigned short refcnt; /* reference count */
295 unsigned short length; /* length of string */
296 unsigned short string_off; /* offset of string in text table */
299 /* value tabent: */
300 struct _w31_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 */
307 /* recursive helper function to display a directory tree [Internal] */
308 void _w31_dumptree(unsigned short idx,unsigned char *txt,struct _w31_tabent *tab,struct _w31_header *head,HKEY hkey,time_t lastmodified, int level)
310 struct _w31_dirent *dir;
311 struct _w31_keyent *key;
312 struct _w31_valent *val;
313 HKEY subkey = 0;
314 static char tail[400];
316 while (idx!=0) {
317 dir=(struct _w31_dirent*)&tab[idx];
319 if (dir->key_idx) {
320 key = (struct _w31_keyent*)&tab[dir->key_idx];
322 memcpy(tail,&txt[key->string_off],key->length);
323 tail[key->length]='\0';
324 /* all toplevel entries AND the entries in the
325 * toplevel subdirectory belong to \SOFTWARE\Classes
327 if (!level && !strcmp(tail,".classes")) {
328 _w31_dumptree(dir->child_idx,txt,tab,head,hkey,lastmodified,level+1);
329 idx=dir->sibling_idx;
330 continue;
332 if (subkey) RegCloseKey( subkey );
333 if (RegCreateKeyA( hkey, tail, &subkey ) != ERROR_SUCCESS) subkey = 0;
334 /* only add if leaf node or valued node */
335 if (dir->value_idx!=0||dir->child_idx==0) {
336 if (dir->value_idx) {
337 val=(struct _w31_valent*)&tab[dir->value_idx];
338 memcpy(tail,&txt[val->string_off],val->length);
339 tail[val->length]='\0';
340 RegSetValueA( subkey, NULL, REG_SZ, tail, 0 );
343 } else TRACE("strange: no directory key name, idx=%04x\n", idx);
344 _w31_dumptree(dir->child_idx,txt,tab,head,subkey,lastmodified,level+1);
345 idx=dir->sibling_idx;
347 if (subkey) RegCloseKey( subkey );
351 /******************************************************************************
352 * _w31_loadreg [Internal]
354 void _w31_loadreg(void)
356 HFILE hf;
357 struct _w31_header head;
358 struct _w31_tabent *tab;
359 unsigned char *txt;
360 unsigned int len;
361 OFSTRUCT ofs;
362 BY_HANDLE_FILE_INFORMATION hfinfo;
363 time_t lastmodified;
365 TRACE("(void)\n");
367 hf = OpenFile("reg.dat",&ofs,OF_READ);
368 if (hf==HFILE_ERROR) return;
370 /* read & dump header */
371 if (sizeof(head)!=_lread(hf,&head,sizeof(head))) {
372 ERR("reg.dat is too short.\n");
373 _lclose(hf);
374 return;
376 if (memcmp(head.cookie, "SHCC3.10", sizeof(head.cookie))!=0) {
377 ERR("reg.dat has bad signature.\n");
378 _lclose(hf);
379 return;
382 len = head.tabcnt * sizeof(struct _w31_tabent);
383 /* read and dump index table */
384 tab = _xmalloc(len);
385 if (len!=_lread(hf,tab,len)) {
386 ERR("couldn't read %d bytes.\n",len);
387 free(tab);
388 _lclose(hf);
389 return;
392 /* read text */
393 txt = _xmalloc(head.textsize);
394 if (-1==_llseek(hf,head.textoff,SEEK_SET)) {
395 ERR("couldn't seek to textblock.\n");
396 free(tab);
397 free(txt);
398 _lclose(hf);
399 return;
401 if (head.textsize!=_lread(hf,txt,head.textsize)) {
402 ERR("textblock too short (%d instead of %ld).\n",len,head.textsize);
403 free(tab);
404 free(txt);
405 _lclose(hf);
406 return;
409 if (!GetFileInformationByHandle(hf,&hfinfo)) {
410 ERR("GetFileInformationByHandle failed?.\n");
411 free(tab);
412 free(txt);
413 _lclose(hf);
414 return;
416 lastmodified = DOSFS_FileTimeToUnixTime(&hfinfo.ftLastWriteTime,NULL);
417 _w31_dumptree(tab[0].w1,txt,tab,&head,HKEY_CLASSES_ROOT,lastmodified,0);
418 free(tab);
419 free(txt);
420 _lclose(hf);
421 return;
424 /***********************************************************************************/
425 /* windows 95 registry loader */
426 /***********************************************************************************/
428 /* SECTION 1: main header
430 * once at offset 0
432 #define W95_REG_CREG_ID 0x47455243
434 typedef struct {
435 DWORD id; /* "CREG" = W95_REG_CREG_ID */
436 DWORD version; /* ???? 0x00010000 */
437 DWORD rgdb_off; /* 0x08 Offset of 1st RGDB-block */
438 DWORD uk2; /* 0x0c */
439 WORD rgdb_num; /* 0x10 # of RGDB-blocks */
440 WORD uk3;
441 DWORD uk[3];
442 /* rgkn */
443 } _w95creg;
445 /* SECTION 2: Directory information (tree structure)
447 * once on offset 0x20
449 * structure: [rgkn][dke]* (repeat till last_dke is reached)
451 #define W95_REG_RGKN_ID 0x4e4b4752
453 typedef struct {
454 DWORD id; /*"RGKN" = W95_REG_RGKN_ID */
455 DWORD size; /* Size of the RGKN-block */
456 DWORD root_off; /* Rel. Offset of the root-record */
457 DWORD last_dke; /* Offset to last DKE ? */
458 DWORD uk[4];
459 } _w95rgkn;
461 /* Disk Key Entry Structure
463 * the 1st entry in a "usual" registry file is a nul-entry with subkeys: the
464 * hive itself. It looks the same like other keys. Even the ID-number can
465 * be any value.
467 * The "hash"-value is a value representing the key's name. Windows will not
468 * search for the name, but for a matching hash-value. if it finds one, it
469 * will compare the actual string info, otherwise continue with the next key.
470 * To calculate the hash initialize a D-Word with 0 and add all ASCII-values
471 * of the string which are smaller than 0x80 (128) to this D-Word.
473 * If you want to modify key names, also modify the hash-values, since they
474 * cannot be found again (although they would be displayed in REGEDIT)
475 * End of list-pointers are filled with 0xFFFFFFFF
477 * Disk keys are layed out flat ... But, sometimes, nrLS and nrMS are both
478 * 0xFFFF, which means skipping over nextkeyoffset bytes (including this
479 * structure) and reading another RGDB_section.
481 * The last DKE (see field last_dke in _w95_rgkn) has only 3 DWORDs with
482 * 0x80000000 (EOL indicator ?) as x1, the hash value and 0xFFFFFFFF as x3.
483 * The remaining space between last_dke and the offset calculated from
484 * rgkn->size seems to be free for use for more dke:s.
485 * So it seems if more dke:s are added, they are added to that space and
486 * last_dke is grown, and in case that "free" space is out, the space
487 * gets grown and rgkn->size gets adjusted.
489 * there is a one to one relationship between dke and dkh
491 /* key struct, once per key */
492 typedef struct {
493 DWORD x1; /* Free entry indicator(?) */
494 DWORD hash; /* sum of bytes of keyname */
495 DWORD x3; /* Root key indicator? usually 0xFFFFFFFF */
496 DWORD prevlvl; /* offset of previous key */
497 DWORD nextsub; /* offset of child key */
498 DWORD next; /* offset of sibling key */
499 WORD nrLS; /* id inside the rgdb block */
500 WORD nrMS; /* number of the rgdb block */
501 } _w95dke;
503 /* SECTION 3: key information, values and data
505 * structure:
506 * section: [blocks]* (repeat creg->rgdb_num times)
507 * blocks: [rgdb] [subblocks]* (repeat till block size reached )
508 * subblocks: [dkh] [dkv]* (repeat dkh->values times )
510 * An interesting relationship exists in RGDB_section. The DWORD value
511 * at offset 0x10 equals the one at offset 0x04 minus the one at offset 0x08.
512 * I have no idea at the moment what this means. (Kevin Cozens)
515 /* block header, once per block */
516 #define W95_REG_RGDB_ID 0x42444752
518 typedef struct {
519 DWORD id; /* 0x00 'RGDB' = W95_REG_RGDB_ID */
520 DWORD size; /* 0x04 */
521 DWORD uk1; /* 0x08 */
522 DWORD uk2; /* 0x0c */
523 DWORD uk3; /* 0x10 */
524 DWORD uk4; /* 0x14 */
525 DWORD uk5; /* 0x18 */
526 DWORD uk6; /* 0x1c */
527 /* dkh */
528 } _w95rgdb;
530 /* Disk Key Header structure (RGDB part), once per key */
531 typedef struct {
532 DWORD nextkeyoff; /* 0x00 offset to next dkh */
533 WORD nrLS; /* 0x04 id inside the rgdb block */
534 WORD nrMS; /* 0x06 number of the rgdb block */
535 DWORD bytesused; /* 0x08 */
536 WORD keynamelen; /* 0x0c len of name */
537 WORD values; /* 0x0e number of values */
538 DWORD xx1; /* 0x10 */
539 char name[1]; /* 0x14 */
540 /* dkv */ /* 0x14 + keynamelen */
541 } _w95dkh;
543 /* Disk Key Value structure, once per value */
544 typedef struct {
545 DWORD type; /* 0x00 */
546 DWORD x1; /* 0x04 */
547 WORD valnamelen; /* 0x08 length of name, 0 is default key */
548 WORD valdatalen; /* 0x0A length of data */
549 char name[1]; /* 0x0c */
550 /* raw data */ /* 0x0c + valnamelen */
551 } _w95dkv;
553 /******************************************************************************
554 * _w95_lookup_dkh [Internal]
556 * seeks the dkh belonging to a dke
558 static _w95dkh *_w95_lookup_dkh(_w95creg *creg,int nrLS,int nrMS)
560 _w95rgdb * rgdb;
561 _w95dkh * dkh;
562 int i;
564 /* get the beginning of the rgdb datastore */
565 rgdb = (_w95rgdb*)((char*)creg+creg->rgdb_off);
567 /* check: requested block < last_block) */
568 if (creg->rgdb_num <= nrMS) {
569 ERR("registry file corrupt! requested block no. beyond end.\n");
570 goto error;
573 /* find the right block */
574 for(i=0; i<nrMS ;i++) {
575 if(rgdb->id != W95_REG_RGDB_ID) { /* check the magic */
576 ERR("registry file corrupt! bad magic 0x%08lx\n", rgdb->id);
577 goto error;
579 rgdb = (_w95rgdb*) ((char*)rgdb+rgdb->size); /* find next block */
582 dkh = (_w95dkh*)(rgdb + 1); /* first sub block within the rgdb */
584 do {
585 if(nrLS==dkh->nrLS ) return dkh;
586 dkh = (_w95dkh*)((char*)dkh + dkh->nextkeyoff); /* find next subblock */
587 } while ((char *)dkh < ((char*)rgdb+rgdb->size));
589 error:
590 return NULL;
593 /******************************************************************************
594 * _w95_dump_dkv [Internal]
596 static int _w95_dump_dkv(_w95dkh *dkh,int nrLS,int nrMS,FILE *f)
598 _w95dkv * dkv;
599 int i;
601 /* first value block */
602 dkv = (_w95dkv*)((char*)dkh+dkh->keynamelen+0x14);
604 /* loop through the values */
605 for (i=0; i< dkh->values; i++) {
606 struct key_value value;
607 WCHAR *pdata;
609 value.nameW = _strdupnAtoW(dkv->name,dkv->valnamelen);
610 value.type = dkv->type;
611 value.len = dkv->valdatalen;
613 value.data = &(dkv->name[dkv->valnamelen]);
614 pdata = NULL;
615 if ( (value.type==REG_SZ) || (value.type==REG_EXPAND_SZ) || (value.type==REG_MULTI_SZ) ) {
616 pdata = _strdupnAtoW(value.data,value.len);
617 value.len *= 2;
619 if (pdata != NULL) value.data = pdata;
621 _dump_value(&value,f);
622 free(value.nameW);
623 if (pdata != NULL) free(pdata);
625 /* next value */
626 dkv = (_w95dkv*)((char*)dkv+dkv->valnamelen+dkv->valdatalen+0x0c);
628 return TRUE;
631 /******************************************************************************
632 * _w95_dump_dke [Internal]
634 static int _w95_dump_dke(LPSTR key_name,_w95creg *creg,_w95rgkn *rgkn,_w95dke *dke,FILE *f,int level)
636 _w95dkh * dkh;
637 LPSTR new_key_name = NULL;
639 /* special root key */
640 if (dke->nrLS == 0xffff || dke->nrMS==0xffff) /* eg. the root key has no name */
642 /* parse the one subkey */
643 if (dke->nextsub != 0xffffffff) return _w95_dump_dke(key_name, creg, rgkn, (_w95dke*)((char*)rgkn+dke->nextsub),f,level);
644 /* has no sibling keys */
645 return FALSE;
648 /* search subblock */
649 if (!(dkh = _w95_lookup_dkh(creg, dke->nrLS, dke->nrMS))) {
650 ERR("dke pointing to missing dkh !\n");
651 return FALSE;
654 if (level <= 0) {
655 /* create new subkey name */
656 new_key_name = _strdupnA(key_name,strlen(key_name)+dkh->keynamelen+1);
657 if (strcmp(new_key_name,"") != 0) strcat(new_key_name,"\\");
658 strncat(new_key_name,dkh->name,dkh->keynamelen);
660 /* walk sibling keys */
661 if (dke->next != 0xffffffff ) {
662 if (!_w95_dump_dke(key_name, creg, rgkn, (_w95dke*)((char*)rgkn+dke->next),f,level)) {
663 free(new_key_name);
664 return FALSE;
668 /* write the key path (something like [Software\\Microsoft\\..]) only if:
669 1) key has some values
670 2) key has no values and no subkeys
672 if (dkh->values > 0) {
673 /* there are some values */
674 fprintf(f,"\n[");
675 _dump_strAtoW(new_key_name,strlen(new_key_name),f,"[]");
676 fprintf(f,"]\n");
677 if (!_w95_dump_dkv(dkh, dke->nrLS, dke->nrMS,f)) {
678 free(new_key_name);
679 return FALSE;
682 if ((dke->nextsub == 0xffffffff) && (dkh->values == 0)) {
683 /* no subkeys and no values */
684 fprintf(f,"\n[");
685 _dump_strAtoW(new_key_name,strlen(new_key_name),f,"[]");
686 fprintf(f,"]\n");
688 } else new_key_name = _strdupnA(key_name,strlen(key_name));
690 /* next sub key */
691 if (dke->nextsub != 0xffffffff) {
692 if (!_w95_dump_dke(new_key_name, creg, rgkn, (_w95dke*)((char*)rgkn+dke->nextsub),f,level-1)) {
693 free(new_key_name);
694 return FALSE;
698 free(new_key_name);
699 return TRUE;
701 /* end windows 95 loader */
703 /***********************************************************************************/
704 /* windows NT registry loader */
705 /***********************************************************************************/
707 /* NT REGISTRY LOADER */
709 #ifdef HAVE_SYS_MMAN_H
710 # include <sys/mman.h>
711 #endif
713 #ifndef MAP_FAILED
714 #define MAP_FAILED ((LPVOID)-1)
715 #endif
717 #define NT_REG_BLOCK_SIZE 0x1000
719 #define NT_REG_HEADER_BLOCK_ID 0x66676572 /* regf */
720 #define NT_REG_POOL_BLOCK_ID 0x6E696268 /* hbin */
721 #define NT_REG_KEY_BLOCK_ID 0x6b6e /* nk */
722 #define NT_REG_VALUE_BLOCK_ID 0x6b76 /* vk */
724 /* subblocks of nk */
725 #define NT_REG_HASH_BLOCK_ID 0x666c /* lf */
726 #define NT_REG_NOHASH_BLOCK_ID 0x696c /* li */
727 #define NT_REG_RI_BLOCK_ID 0x6972 /* ri */
729 #define NT_REG_KEY_BLOCK_TYPE 0x20
730 #define NT_REG_ROOT_KEY_BLOCK_TYPE 0x2c
732 typedef struct {
733 DWORD id; /* 0x66676572 'regf'*/
734 DWORD uk1; /* 0x04 */
735 DWORD uk2; /* 0x08 */
736 FILETIME DateModified; /* 0x0c */
737 DWORD uk3; /* 0x14 */
738 DWORD uk4; /* 0x18 */
739 DWORD uk5; /* 0x1c */
740 DWORD uk6; /* 0x20 */
741 DWORD RootKeyBlock; /* 0x24 */
742 DWORD BlockSize; /* 0x28 */
743 DWORD uk7[116];
744 DWORD Checksum; /* at offset 0x1FC */
745 } nt_regf;
747 typedef struct {
748 DWORD blocksize;
749 BYTE data[1];
750 } nt_hbin_sub;
752 typedef struct {
753 DWORD id; /* 0x6E696268 'hbin' */
754 DWORD off_prev;
755 DWORD off_next;
756 DWORD uk1;
757 DWORD uk2; /* 0x10 */
758 DWORD uk3; /* 0x14 */
759 DWORD uk4; /* 0x18 */
760 DWORD size; /* 0x1C */
761 nt_hbin_sub hbin_sub; /* 0x20 */
762 } nt_hbin;
765 * the value_list consists of offsets to the values (vk)
767 typedef struct {
768 WORD SubBlockId; /* 0x00 0x6B6E */
769 WORD Type; /* 0x02 for the root-key: 0x2C, otherwise 0x20*/
770 FILETIME writetime; /* 0x04 */
771 DWORD uk1; /* 0x0C */
772 DWORD parent_off; /* 0x10 Offset of Owner/Parent key */
773 DWORD nr_subkeys; /* 0x14 number of sub-Keys */
774 DWORD uk8; /* 0x18 */
775 DWORD lf_off; /* 0x1C Offset of the sub-key lf-Records */
776 DWORD uk2; /* 0x20 */
777 DWORD nr_values; /* 0x24 number of values */
778 DWORD valuelist_off; /* 0x28 Offset of the Value-List */
779 DWORD off_sk; /* 0x2c Offset of the sk-Record */
780 DWORD off_class; /* 0x30 Offset of the Class-Name */
781 DWORD uk3; /* 0x34 */
782 DWORD uk4; /* 0x38 */
783 DWORD uk5; /* 0x3c */
784 DWORD uk6; /* 0x40 */
785 DWORD uk7; /* 0x44 */
786 WORD name_len; /* 0x48 name-length */
787 WORD class_len; /* 0x4a class-name length */
788 char name[1]; /* 0x4c key-name */
789 } nt_nk;
791 typedef struct {
792 DWORD off_nk; /* 0x00 */
793 DWORD name; /* 0x04 */
794 } hash_rec;
796 typedef struct {
797 WORD id; /* 0x00 0x666c */
798 WORD nr_keys; /* 0x06 */
799 hash_rec hash_rec[1];
800 } nt_lf;
803 list of subkeys without hash
805 li --+-->nk
807 +-->nk
809 typedef struct {
810 WORD id; /* 0x00 0x696c */
811 WORD nr_keys;
812 DWORD off_nk[1];
813 } nt_li;
816 this is a intermediate node
818 ri --+-->li--+-->nk
820 | +-->nk
822 +-->li--+-->nk
824 +-->nk
826 typedef struct {
827 WORD id; /* 0x00 0x6972 */
828 WORD nr_li; /* 0x02 number off offsets */
829 DWORD off_li[1]; /* 0x04 points to li */
830 } nt_ri;
832 typedef struct {
833 WORD id; /* 0x00 'vk' */
834 WORD nam_len;
835 DWORD data_len;
836 DWORD data_off;
837 DWORD type;
838 WORD flag;
839 WORD uk1;
840 char name[1];
841 } nt_vk;
844 * gets a value
846 * vk->flag:
847 * 0 value is a default value
848 * 1 the value has a name
850 * vk->data_len
851 * len of the whole data block
852 * - reg_sz (unicode)
853 * bytes including the terminating \0 = 2*(number_of_chars+1)
854 * - reg_dword, reg_binary:
855 * if highest bit of data_len is set data_off contains the value
857 static int _nt_dump_vk(LPSTR key_name, char *base, nt_vk *vk,FILE *f)
859 BYTE *pdata = (BYTE *)(base+vk->data_off+4); /* start of data */
860 struct key_value value;
862 if (vk->id != NT_REG_VALUE_BLOCK_ID) {
863 ERR("unknown block found (0x%04x), please report!\n", vk->id);
864 return FALSE;
867 value.nameW = _strdupnAtoW(vk->name,vk->nam_len);
868 value.type = vk->type;
869 value.len = (vk->data_len & 0x7fffffff);
870 value.data = (vk->data_len & 0x80000000) ? (LPBYTE)&(vk->data_off): pdata;
872 _dump_value(&value,f);
873 free(value.nameW);
875 return TRUE;
878 /* it's called from _nt_dump_lf() */
879 static int _nt_dump_nk(LPSTR key_name,char *base,nt_nk *nk,FILE *f,int level);
882 * get the subkeys
884 * this structure contains the hash of a keyname and points to all
885 * subkeys
887 * exception: if the id is 'il' there are no hash values and every
888 * dword is a offset
890 static int _nt_dump_lf(LPSTR key_name, char *base, int subkeys, nt_lf *lf, FILE *f, int level)
892 int i;
894 if (lf->id == NT_REG_HASH_BLOCK_ID) {
895 if (subkeys != lf->nr_keys) goto error1;
897 for (i=0; i<lf->nr_keys; i++)
898 if (!_nt_dump_nk(key_name, base, (nt_nk*)(base+lf->hash_rec[i].off_nk+4), f, level)) goto error;
899 } else if (lf->id == NT_REG_NOHASH_BLOCK_ID) {
900 nt_li * li = (nt_li*)lf;
901 if (subkeys != li->nr_keys) goto error1;
903 for (i=0; i<li->nr_keys; i++)
904 if (!_nt_dump_nk(key_name, base, (nt_nk*)(base+li->off_nk[i]+4), f, level)) goto error;
905 } else if (lf->id == NT_REG_RI_BLOCK_ID) { /* ri */
906 nt_ri * ri = (nt_ri*)lf;
907 int li_subkeys = 0;
909 /* count all subkeys */
910 for (i=0; i<ri->nr_li; i++) {
911 nt_li * li = (nt_li*)(base+ri->off_li[i]+4);
912 if(li->id != NT_REG_NOHASH_BLOCK_ID) goto error2;
913 li_subkeys += li->nr_keys;
916 /* check number */
917 if (subkeys != li_subkeys) goto error1;
919 /* loop through the keys */
920 for (i=0; i<ri->nr_li; i++) {
921 nt_li *li = (nt_li*)(base+ri->off_li[i]+4);
922 if (!_nt_dump_lf(key_name, base, li->nr_keys, (nt_lf*)li, f, level)) goto error;
924 } else goto error2;
926 return TRUE;
928 error2:
929 if (lf->id == 0x686c)
930 FIXME("unknown Win XP node id 0x686c: do we need to add support for it ?\n");
931 else
932 ERR("unknown node id 0x%04x, please report!\n", lf->id);
933 return TRUE;
935 error1:
936 ERR("registry file corrupt! (inconsistent number of subkeys)\n");
937 return FALSE;
939 error:
940 ERR("error reading lf block\n");
941 return FALSE;
944 /* _nt_dump_nk [Internal] */
945 static int _nt_dump_nk(LPSTR key_name,char *base,nt_nk *nk,FILE *f,int level)
947 unsigned int n;
948 DWORD *vl;
949 LPSTR new_key_name = NULL;
952 if (nk->SubBlockId != NT_REG_KEY_BLOCK_ID) {
953 ERR("unknown node id 0x%04x, please report!\n", nk->SubBlockId);
954 return FALSE;
957 if ((nk->Type!=NT_REG_ROOT_KEY_BLOCK_TYPE) && (((nt_nk*)(base+nk->parent_off+4))->SubBlockId != NT_REG_KEY_BLOCK_ID)) {
958 ERR("registry file corrupt!\n");
959 return FALSE;
962 /* create the new key */
963 if (level <= 0) {
964 /* create new subkey name */
965 new_key_name = _strdupnA(key_name,strlen(key_name)+nk->name_len+1);
966 if (strcmp(new_key_name,"") != 0) strcat(new_key_name,"\\");
967 strncat(new_key_name,nk->name,nk->name_len);
969 /* write the key path (something like [Software\\Microsoft\\..]) only if:
970 1) key has some values
971 2) key has no values and no subkeys
973 if (nk->nr_values > 0) {
974 /* there are some values */
975 fprintf(f,"\n[");
976 _dump_strAtoW(new_key_name,strlen(new_key_name),f,"[]");
977 fprintf(f,"]\n");
979 if ((nk->nr_subkeys == 0) && (nk->nr_values == 0)) {
980 /* no subkeys and no values */
981 fprintf(f,"\n[");
982 _dump_strAtoW(new_key_name,strlen(new_key_name),f,"[]");
983 fprintf(f,"]\n");
986 /* loop trough the value list */
987 vl = (DWORD *)(base+nk->valuelist_off+4);
988 for (n=0; n<nk->nr_values; n++) {
989 nt_vk * vk = (nt_vk*)(base+vl[n]+4);
990 if (!_nt_dump_vk(new_key_name, base, vk, f)) {
991 free(new_key_name);
992 return FALSE;
995 } else new_key_name = _strdupnA(key_name,strlen(key_name));
997 /* loop through the subkeys */
998 if (nk->nr_subkeys) {
999 nt_lf *lf = (nt_lf*)(base+nk->lf_off+4);
1000 if (!_nt_dump_lf(new_key_name, base, nk->nr_subkeys, lf, f, level-1)) {
1001 free(new_key_name);
1002 return FALSE;
1006 free(new_key_name);
1007 return TRUE;
1010 /* end nt loader */
1012 /**********************************************************************************
1013 * _set_registry_levels [Internal]
1015 * set level to 0 for loading system files
1016 * set level to 1 for loading user files
1018 static void _set_registry_levels(int level,int saving,int period)
1020 SERVER_START_REQ( set_registry_levels )
1022 req->current = level;
1023 req->saving = saving;
1024 req->period = period;
1025 wine_server_call( req );
1027 SERVER_END_REQ;
1030 /* _save_at_exit [Internal] */
1031 static void _save_at_exit(HKEY hkey,LPCSTR path)
1033 LPCSTR confdir = get_config_dir();
1035 SERVER_START_REQ( save_registry_atexit )
1037 req->hkey = hkey;
1038 wine_server_add_data( req, confdir, strlen(confdir) );
1039 wine_server_add_data( req, path, strlen(path)+1 );
1040 wine_server_call( req );
1042 SERVER_END_REQ;
1045 /* configure save files and start the periodic saving timer [Internal] */
1046 static void _init_registry_saving( HKEY hkey_users_default )
1048 int all;
1049 int period;
1051 all = PROFILE_GetWineIniBool("registry","SaveOnlyUpdatedKeys",1);
1052 period = PROFILE_GetWineIniInt("registry","PeriodicSave",0);
1054 /* set saving level (0 for saving everything, 1 for saving only modified keys) */
1055 _set_registry_levels(1,!all,period*1000);
1057 if (PROFILE_GetWineIniBool("registry","WritetoHomeRegistryFiles",1))
1059 _save_at_exit(HKEY_CURRENT_USER,"/" SAVE_LOCAL_REGBRANCH_CURRENT_USER );
1060 _save_at_exit(HKEY_LOCAL_MACHINE,"/" SAVE_LOCAL_REGBRANCH_LOCAL_MACHINE);
1061 _save_at_exit(hkey_users_default,"/" SAVE_LOCAL_REGBRANCH_USER_DEFAULT);
1066 /******************************************************************************
1067 * _allocate_default_keys [Internal]
1068 * Registry initialisation, allocates some default keys.
1070 static void _allocate_default_keys(void) {
1071 HKEY hkey;
1072 char buf[200];
1074 TRACE("(void)\n");
1076 RegCreateKeyA(HKEY_DYN_DATA,"PerfStats\\StatData",&hkey);
1077 RegCloseKey(hkey);
1079 /* This was an Open, but since it is called before the real registries
1080 are loaded, it was changed to a Create - MTB 980507*/
1081 RegCreateKeyA(HKEY_LOCAL_MACHINE,"HARDWARE\\DESCRIPTION\\System",&hkey);
1082 RegSetValueExA(hkey,"Identifier",0,REG_SZ,"SystemType WINE",strlen("SystemType WINE"));
1083 RegCloseKey(hkey);
1085 /* \\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion
1086 * CurrentVersion
1087 * CurrentBuildNumber
1088 * CurrentType
1089 * string RegisteredOwner
1090 * string RegisteredOrganization
1093 /* System\\CurrentControlSet\\Services\\SNMP\\Parameters\\RFC1156Agent
1094 * string SysContact
1095 * string SysLocation
1096 * SysServices
1098 if (-1!=gethostname(buf,200)) {
1099 RegCreateKeyA(HKEY_LOCAL_MACHINE,"System\\CurrentControlSet\\Control\\ComputerName\\ComputerName",&hkey);
1100 RegSetValueExA(hkey,"ComputerName",0,REG_SZ,buf,strlen(buf)+1);
1101 RegCloseKey(hkey);
1104 RegCreateKeyA(HKEY_USERS,".Default",&hkey);
1105 RegCloseKey(hkey);
1108 #define REG_DONTLOAD -1
1109 #define REG_WIN31 0
1110 #define REG_WIN95 1
1111 #define REG_WINNT 2
1113 /* return the type of native registry [Internal] */
1114 static int _get_reg_type(void)
1116 char windir[MAX_PATHNAME_LEN];
1117 char tmp[MAX_PATHNAME_LEN];
1118 int ret = REG_WIN31;
1120 GetWindowsDirectoryA(windir,MAX_PATHNAME_LEN);
1122 /* test %windir%/system32/config/system --> winnt */
1123 strcpy(tmp, windir);
1124 strncat(tmp, "\\system32\\config\\system", MAX_PATHNAME_LEN - strlen(tmp) - 1);
1125 if(GetFileAttributesA(tmp) != (DWORD)-1) {
1126 ret = REG_WINNT;
1128 else
1130 /* test %windir%/system.dat --> win95 */
1131 strcpy(tmp, windir);
1132 strncat(tmp, "\\system.dat", MAX_PATHNAME_LEN - strlen(tmp) - 1);
1133 if(GetFileAttributesA(tmp) != (DWORD)-1) {
1134 ret = REG_WIN95;
1138 if ((ret == REG_WINNT) && (!PROFILE_GetWineIniString( "Wine", "Profile", "", tmp, MAX_PATHNAME_LEN))) {
1139 MESSAGE("When you are running with a native NT directory specify\n");
1140 MESSAGE("'Profile=<profiledirectory>' or disable loading of Windows\n");
1141 MESSAGE("registry (LoadWindowsRegistryFiles=N)\n");
1142 ret = REG_DONTLOAD;
1145 return ret;
1148 #define WINE_REG_VER_ERROR -1
1149 #define WINE_REG_VER_1 0
1150 #define WINE_REG_VER_2 1
1151 #define WINE_REG_VER_OLD 2
1152 #define WINE_REG_VER_UNKNOWN 3
1154 /* return the version of wine registry file [Internal] */
1155 static int _get_wine_registry_file_format_version(LPCSTR fn)
1157 FILE *f;
1158 char tmp[50];
1159 int ver;
1161 if ((f=fopen(fn,"rt")) == NULL) {
1162 WARN("Couldn't open %s for reading: %s\n",fn,strerror(errno));
1163 return WINE_REG_VER_ERROR;
1166 if (fgets(tmp,50,f) == NULL) {
1167 WARN("Error reading %s: %s\n",fn,strerror(errno));
1168 fclose(f);
1169 return WINE_REG_VER_ERROR;
1171 fclose(f);
1173 if (sscanf(tmp,"WINE REGISTRY Version %d",&ver) != 1) return WINE_REG_VER_UNKNOWN;
1174 switch (ver) {
1175 case 1:
1176 return WINE_REG_VER_1;
1177 break;
1178 case 2:
1179 return WINE_REG_VER_2;
1180 break;
1181 default:
1182 return WINE_REG_VER_UNKNOWN;
1186 /* load the registry file in wine format [Internal] */
1187 static void load_wine_registry(HKEY hkey,LPCSTR fn)
1189 int file_format;
1191 file_format = _get_wine_registry_file_format_version(fn);
1192 switch (file_format) {
1194 case WINE_REG_VER_1:
1195 WARN("Unable to load registry file %s: old format which is no longer supported.\n",fn);
1196 break;
1198 case WINE_REG_VER_2: {
1199 HANDLE file;
1200 if ((file = FILE_CreateFile( fn, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1201 FILE_ATTRIBUTE_NORMAL, 0, TRUE, DRIVE_UNKNOWN )))
1203 SERVER_START_REQ( load_registry )
1205 req->hkey = hkey;
1206 req->file = file;
1207 wine_server_call( req );
1209 SERVER_END_REQ;
1210 CloseHandle( file );
1212 break;
1215 case WINE_REG_VER_UNKNOWN:
1216 WARN("Unable to load registry file %s: unknown format.\n",fn);
1217 break;
1219 case WINE_REG_VER_ERROR:
1220 break;
1224 /* generate and return the name of the tmp file and associated stream [Internal] */
1225 static LPSTR _get_tmp_fn(FILE **f)
1227 LPSTR ret;
1228 int tmp_fd,count;
1230 ret = _xmalloc(50);
1231 for (count = 0;;) {
1232 sprintf(ret,"/tmp/reg%lx%04x.tmp",(long)getpid(),count++);
1233 if ((tmp_fd = open(ret,O_CREAT | O_EXCL | O_WRONLY,0666)) != -1) break;
1234 if (errno != EEXIST) {
1235 ERR("Unexpected error while open() call: %s\n",strerror(errno));
1236 free(ret);
1237 *f = NULL;
1238 return NULL;
1242 if ((*f = fdopen(tmp_fd,"w")) == NULL) {
1243 ERR("Unexpected error while fdopen() call: %s\n",strerror(errno));
1244 close(tmp_fd);
1245 free(ret);
1246 return NULL;
1249 return ret;
1252 /* convert win95 native registry file to wine format [Internal] */
1253 static LPSTR _convert_win95_registry_to_wine_format(LPCSTR fn,int level)
1255 int fd;
1256 FILE *f;
1257 DOS_FULL_NAME full_name;
1258 void *base;
1259 LPSTR ret = NULL;
1260 struct stat st;
1262 _w95creg *creg;
1263 _w95rgkn *rgkn;
1264 _w95dke *dke, *root_dke;
1266 if (!DOSFS_GetFullName( fn, 0, &full_name )) return NULL;
1268 /* map the registry into the memory */
1269 if ((fd = open(full_name.long_name, O_RDONLY | O_NONBLOCK)) == -1) return NULL;
1270 if ((fstat(fd, &st) == -1)) goto error1;
1271 if (!st.st_size) goto error1;
1272 if ((base = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) goto error1;
1274 /* control signature */
1275 if (*(LPDWORD)base != W95_REG_CREG_ID) {
1276 ERR("unable to load native win95 registry file %s: unknown signature.\n",fn);
1277 goto error;
1280 creg = base;
1281 /* load the header (rgkn) */
1282 rgkn = (_w95rgkn*)(creg + 1);
1283 if (rgkn->id != W95_REG_RGKN_ID) {
1284 ERR("second IFF header not RGKN, but %lx\n", rgkn->id);
1285 goto error;
1287 if (rgkn->root_off != 0x20) {
1288 ERR("rgkn->root_off not 0x20, please report !\n");
1289 goto error;
1291 if (rgkn->last_dke > rgkn->size)
1293 ERR("registry file corrupt! last_dke > size!\n");
1294 goto error;
1296 /* verify last dke */
1297 dke = (_w95dke*)((char*)rgkn + rgkn->last_dke);
1298 if (dke->x1 != 0x80000000)
1299 { /* wrong magic */
1300 ERR("last dke invalid !\n");
1301 goto error;
1303 if (rgkn->size > creg->rgdb_off)
1305 ERR("registry file corrupt! rgkn size > rgdb_off !\n");
1306 goto error;
1308 root_dke = (_w95dke*)((char*)rgkn + rgkn->root_off);
1309 if ( (root_dke->prevlvl != 0xffffffff) || (root_dke->next != 0xffffffff) )
1311 ERR("registry file corrupt! invalid root dke !\n");
1312 goto error;
1315 if ( (ret = _get_tmp_fn(&f)) == NULL) goto error;
1316 fprintf(f,"WINE REGISTRY Version 2");
1317 _w95_dump_dke("",creg,rgkn,root_dke,f,level);
1318 fclose(f);
1320 error:
1321 if(ret == NULL) {
1322 ERR("Unable to load native win95 registry file %s.\n",fn);
1323 ERR("Please report to a.mohr@mailto.de.\n");
1324 ERR("Make a backup of the file, run a good reg cleaner program and try again!\n");
1327 munmap(base, st.st_size);
1328 error1:
1329 close(fd);
1330 return ret;
1333 /* convert winnt native registry file to wine format [Internal] */
1334 static LPSTR _convert_winnt_registry_to_wine_format(LPCSTR fn,int level)
1336 int fd;
1337 FILE *f;
1338 DOS_FULL_NAME full_name;
1339 void *base;
1340 LPSTR ret = NULL;
1341 struct stat st;
1343 nt_regf *regf;
1344 nt_hbin *hbin;
1345 nt_hbin_sub *hbin_sub;
1346 nt_nk *nk;
1348 if (!DOSFS_GetFullName( fn, 0, &full_name )) return NULL;
1350 /* map the registry into the memory */
1351 if ((fd = open(full_name.long_name, O_RDONLY | O_NONBLOCK)) == -1) return NULL;
1352 if ((fstat(fd, &st) == -1)) goto error1;
1353 if (!st.st_size) goto error1;
1354 if ((base = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) goto error1;
1356 /* control signature */
1357 if (*(LPDWORD)base != NT_REG_HEADER_BLOCK_ID) {
1358 ERR("unable to load native winnt registry file %s: unknown signature.\n",fn);
1359 goto error;
1362 /* start block */
1363 regf = base;
1365 /* hbin block */
1366 hbin = (nt_hbin*)((char*) base + 0x1000);
1367 if (hbin->id != NT_REG_POOL_BLOCK_ID) {
1368 ERR( "hbin block invalid\n");
1369 goto error;
1372 /* hbin_sub block */
1373 hbin_sub = (nt_hbin_sub*)&(hbin->hbin_sub);
1374 if ((hbin_sub->data[0] != 'n') || (hbin_sub->data[1] != 'k')) {
1375 ERR( "hbin_sub block invalid\n");
1376 goto error;
1379 /* nk block */
1380 nk = (nt_nk*)&(hbin_sub->data[0]);
1381 if (nk->Type != NT_REG_ROOT_KEY_BLOCK_TYPE) {
1382 ERR( "special nk block not found\n");
1383 goto error;
1386 if ( (ret = _get_tmp_fn(&f)) == NULL) goto error;
1387 fprintf(f,"WINE REGISTRY Version 2");
1388 _nt_dump_nk("",(char*)base+0x1000,nk,f,level);
1389 fclose(f);
1391 error:
1392 munmap(base,st.st_size);
1393 error1:
1394 close(fd);
1395 return ret;
1398 /* convert native registry to wine format and load it via server call [Internal] */
1399 static void _convert_and_load_native_registry(LPCSTR fn,HKEY hkey,int reg_type,int level)
1401 LPSTR tmp = NULL;
1403 switch (reg_type) {
1404 case REG_WINNT:
1405 /* FIXME: following function doesn't really convert yet */
1406 tmp = _convert_winnt_registry_to_wine_format(fn,level);
1407 break;
1408 case REG_WIN95:
1409 tmp = _convert_win95_registry_to_wine_format(fn,level);
1410 break;
1411 case REG_WIN31:
1412 ERR("Don't know how to convert native 3.1 registry yet.\n");
1413 break;
1414 default:
1415 ERR("Unknown registry format parameter (%d)\n",reg_type);
1416 break;
1419 if (tmp != NULL) {
1420 load_wine_registry(hkey,tmp);
1421 TRACE("File %s successfully converted to %s and loaded to registry.\n",fn,tmp);
1422 unlink(tmp);
1424 else WARN("Unable to convert %s (doesn't exist?)\n",fn);
1425 free(tmp);
1428 /* load all native windows registry files [Internal] */
1429 static void _load_windows_registry( HKEY hkey_users_default )
1431 int reg_type;
1432 char windir[MAX_PATHNAME_LEN];
1433 char path[MAX_PATHNAME_LEN];
1435 GetWindowsDirectoryA(windir,MAX_PATHNAME_LEN);
1437 reg_type = _get_reg_type();
1438 switch (reg_type) {
1439 case REG_WINNT: {
1440 HKEY hkey;
1442 /* user specific ntuser.dat */
1443 if (PROFILE_GetWineIniString( "Wine", "Profile", "", path, MAX_PATHNAME_LEN)) {
1444 strcat(path,"\\ntuser.dat");
1445 _convert_and_load_native_registry(path,HKEY_CURRENT_USER,REG_WINNT,1);
1448 /* default user.dat */
1449 if (hkey_users_default) {
1450 strcpy(path,windir);
1451 strcat(path,"\\system32\\config\\default");
1452 _convert_and_load_native_registry(path,hkey_users_default,REG_WINNT,1);
1456 * FIXME
1457 * map HLM\System\ControlSet001 to HLM\System\CurrentControlSet
1460 if (!RegCreateKeyA(HKEY_LOCAL_MACHINE, "SYSTEM", &hkey)) {
1461 strcpy(path,windir);
1462 strcat(path,"\\system32\\config\\system");
1463 _convert_and_load_native_registry(path,hkey,REG_WINNT,1);
1464 RegCloseKey(hkey);
1467 if (!RegCreateKeyA(HKEY_LOCAL_MACHINE, "SOFTWARE", &hkey)) {
1468 strcpy(path,windir);
1469 strcat(path,"\\system32\\config\\software");
1470 _convert_and_load_native_registry(path,hkey,REG_WINNT,1);
1471 RegCloseKey(hkey);
1474 strcpy(path,windir);
1475 strcat(path,"\\system32\\config\\sam");
1476 _convert_and_load_native_registry(path,HKEY_LOCAL_MACHINE,REG_WINNT,0);
1478 strcpy(path,windir);
1479 strcat(path,"\\system32\\config\\security");
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 if (!RegCreateKeyA(HKEY_LOCAL_MACHINE,"System\\Clone",&hkey)) RegCloseKey(hkey);
1484 break;
1487 case REG_WIN95:
1488 _convert_and_load_native_registry("c:\\system.1st",HKEY_LOCAL_MACHINE,REG_WIN95,0);
1490 strcpy(path,windir);
1491 strcat(path,"\\system.dat");
1492 _convert_and_load_native_registry(path,HKEY_LOCAL_MACHINE,REG_WIN95,0);
1494 strcpy(path,windir);
1495 strcat(path,"\\classes.dat");
1496 _convert_and_load_native_registry(path,HKEY_CLASSES_ROOT,REG_WIN95,0);
1498 if (PROFILE_GetWineIniString("Wine","Profile","",path,MAX_PATHNAME_LEN)) {
1499 /* user specific user.dat */
1500 strncat(path, "\\user.dat", MAX_PATHNAME_LEN - strlen(path) - 1);
1501 _convert_and_load_native_registry(path,HKEY_CURRENT_USER,REG_WIN95,1);
1503 /* default user.dat */
1504 if (hkey_users_default) {
1505 strcpy(path,windir);
1506 strcat(path,"\\user.dat");
1507 _convert_and_load_native_registry(path,hkey_users_default,REG_WIN95,1);
1509 } else {
1510 strcpy(path,windir);
1511 strcat(path,"\\user.dat");
1512 _convert_and_load_native_registry(path,HKEY_CURRENT_USER,REG_WIN95,1);
1514 break;
1516 case REG_WIN31:
1517 /* FIXME: here we should convert to *.reg file supported by server and call REQ_LOAD_REGISTRY, see REG_WIN95 case */
1518 _w31_loadreg();
1519 break;
1521 case REG_DONTLOAD:
1522 TRACE("REG_DONTLOAD\n");
1523 break;
1525 default:
1526 ERR("switch: no match (%d)\n",reg_type);
1527 break;
1532 /* load global registry files (stored in /etc/wine) [Internal] */
1533 static void _load_global_registry(void)
1535 TRACE("(void)\n");
1537 /* Load the global HKU hive directly from sysconfdir */
1538 load_wine_registry( HKEY_USERS, SAVE_GLOBAL_REGBRANCH_USER_DEFAULT );
1540 /* Load the global machine defaults directly from sysconfdir */
1541 load_wine_registry( HKEY_LOCAL_MACHINE, SAVE_GLOBAL_REGBRANCH_LOCAL_MACHINE );
1544 /* load home registry files (stored in ~/.wine) [Internal] */
1545 static void _load_home_registry( HKEY hkey_users_default )
1547 LPCSTR confdir = get_config_dir();
1548 LPSTR tmp = _xmalloc(strlen(confdir)+20);
1550 strcpy(tmp,confdir);
1551 strcat(tmp,"/" SAVE_LOCAL_REGBRANCH_USER_DEFAULT);
1552 load_wine_registry(hkey_users_default,tmp);
1554 strcpy(tmp,confdir);
1555 strcat(tmp,"/" SAVE_LOCAL_REGBRANCH_CURRENT_USER);
1556 load_wine_registry(HKEY_CURRENT_USER,tmp);
1558 strcpy(tmp,confdir);
1559 strcat(tmp,"/" SAVE_LOCAL_REGBRANCH_LOCAL_MACHINE);
1560 load_wine_registry(HKEY_LOCAL_MACHINE,tmp);
1562 free(tmp);
1565 /* load all registry (native and global and home) */
1566 void SHELL_LoadRegistry( void )
1568 HKEY hkey_users_default;
1570 TRACE("(void)\n");
1572 if (!CLIENT_IsBootThread()) return; /* already loaded */
1574 if (RegCreateKeyA(HKEY_USERS,".Default",&hkey_users_default))
1576 ERR("Cannot create HKEY_USERS/.Default\n" );
1577 ExitProcess(1);
1580 _allocate_default_keys();
1581 _set_registry_levels(0,0,0);
1582 if (PROFILE_GetWineIniBool("Registry","LoadWindowsRegistryFiles",1))
1583 _load_windows_registry( hkey_users_default );
1584 if (PROFILE_GetWineIniBool("Registry","LoadGlobalRegistryFiles",1))
1585 _load_global_registry();
1586 _set_registry_levels(1,0,0);
1587 if (PROFILE_GetWineIniBool("Registry","LoadHomeRegistryFiles",1))
1588 _load_home_registry( hkey_users_default );
1589 _init_registry_saving( hkey_users_default );
1590 RegCloseKey(hkey_users_default);
1593 /***************************************************************************/
1594 /* API FUNCTIONS */
1595 /***************************************************************************/
1597 /******************************************************************************
1598 * RegFlushKey [ADVAPI32.@]
1599 * Immediately writes key to registry.
1600 * Only returns after data has been written to disk.
1602 * FIXME: does it really wait until data is written ?
1604 * PARAMS
1605 * hkey [I] Handle of key to write
1607 * RETURNS
1608 * Success: ERROR_SUCCESS
1609 * Failure: Error code
1611 DWORD WINAPI RegFlushKey( HKEY hkey )
1613 FIXME( "(%x): stub\n", hkey );
1614 return ERROR_SUCCESS;
1618 /******************************************************************************
1619 * RegUnLoadKeyA [ADVAPI32.@]
1621 LONG WINAPI RegUnLoadKeyA( HKEY hkey, LPCSTR lpSubKey )
1623 FIXME("(%x,%s): stub\n",hkey, debugstr_a(lpSubKey));
1624 return ERROR_SUCCESS;
1628 /******************************************************************************
1629 * RegReplaceKeyA [ADVAPI32.@]
1631 LONG WINAPI RegReplaceKeyA( HKEY hkey, LPCSTR lpSubKey, LPCSTR lpNewFile,
1632 LPCSTR lpOldFile )
1634 FIXME("(%x,%s,%s,%s): stub\n", hkey, debugstr_a(lpSubKey),
1635 debugstr_a(lpNewFile),debugstr_a(lpOldFile));
1636 return ERROR_SUCCESS;
1644 /* 16-bit functions */
1646 /* 0 and 1 are valid rootkeys in win16 shell.dll and are used by
1647 * some programs. Do not remove those cases. -MM
1649 static inline void fix_win16_hkey( HKEY *hkey )
1651 if (*hkey == 0 || *hkey == 1) *hkey = HKEY_CLASSES_ROOT;
1654 /******************************************************************************
1655 * RegEnumKey [KERNEL.216]
1656 * RegEnumKey [SHELL.7]
1658 DWORD WINAPI RegEnumKey16( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
1660 fix_win16_hkey( &hkey );
1661 return RegEnumKeyA( hkey, index, name, name_len );
1664 /******************************************************************************
1665 * RegOpenKey [KERNEL.217]
1666 * RegOpenKey [SHELL.1]
1668 DWORD WINAPI RegOpenKey16( HKEY hkey, LPCSTR name, LPHKEY retkey )
1670 fix_win16_hkey( &hkey );
1671 return RegOpenKeyA( hkey, name, retkey );
1674 /******************************************************************************
1675 * RegCreateKey [KERNEL.218]
1676 * RegCreateKey [SHELL.2]
1678 DWORD WINAPI RegCreateKey16( HKEY hkey, LPCSTR name, LPHKEY retkey )
1680 fix_win16_hkey( &hkey );
1681 return RegCreateKeyA( hkey, name, retkey );
1684 /******************************************************************************
1685 * RegDeleteKey [KERNEL.219]
1686 * RegDeleteKey [SHELL.4]
1688 DWORD WINAPI RegDeleteKey16( HKEY hkey, LPCSTR name )
1690 fix_win16_hkey( &hkey );
1691 return RegDeleteKeyA( hkey, name );
1694 /******************************************************************************
1695 * RegCloseKey [KERNEL.220]
1696 * RegCloseKey [SHELL.3]
1698 DWORD WINAPI RegCloseKey16( HKEY hkey )
1700 fix_win16_hkey( &hkey );
1701 return RegCloseKey( hkey );
1704 /******************************************************************************
1705 * RegSetValue [KERNEL.221]
1706 * RegSetValue [SHELL.5]
1708 DWORD WINAPI RegSetValue16( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
1710 fix_win16_hkey( &hkey );
1711 return RegSetValueA( hkey, name, type, data, count );
1714 /******************************************************************************
1715 * RegDeleteValue [KERNEL.222]
1717 DWORD WINAPI RegDeleteValue16( HKEY hkey, LPSTR name )
1719 fix_win16_hkey( &hkey );
1720 return RegDeleteValueA( hkey, name );
1723 /******************************************************************************
1724 * RegEnumValue [KERNEL.223]
1726 DWORD WINAPI RegEnumValue16( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
1727 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1729 fix_win16_hkey( &hkey );
1730 return RegEnumValueA( hkey, index, value, val_count, reserved, type, data, count );
1733 /******************************************************************************
1734 * RegQueryValue [KERNEL.224]
1735 * RegQueryValue [SHELL.6]
1737 * NOTES
1738 * Is this HACK still applicable?
1740 * HACK
1741 * The 16bit RegQueryValue doesn't handle selectorblocks anyway, so we just
1742 * mask out the high 16 bit. This (not so much incidently) hopefully fixes
1743 * Aldus FH4)
1745 DWORD WINAPI RegQueryValue16( HKEY hkey, LPCSTR name, LPSTR data, LPDWORD count )
1747 fix_win16_hkey( &hkey );
1748 if (count) *count &= 0xffff;
1749 return RegQueryValueA( hkey, name, data, count );
1752 /******************************************************************************
1753 * RegQueryValueEx [KERNEL.225]
1755 DWORD WINAPI RegQueryValueEx16( HKEY hkey, LPCSTR name, LPDWORD reserved, LPDWORD type,
1756 LPBYTE data, LPDWORD count )
1758 fix_win16_hkey( &hkey );
1759 return RegQueryValueExA( hkey, name, reserved, type, data, count );
1762 /******************************************************************************
1763 * RegSetValueEx [KERNEL.226]
1765 DWORD WINAPI RegSetValueEx16( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
1766 CONST BYTE *data, DWORD count )
1768 fix_win16_hkey( &hkey );
1769 if (!count && (type==REG_SZ)) count = strlen(data);
1770 return RegSetValueExA( hkey, name, reserved, type, data, count );
1773 /******************************************************************************
1774 * RegFlushKey [KERNEL.227]
1776 DWORD WINAPI RegFlushKey16( HKEY hkey )
1778 fix_win16_hkey( &hkey );
1779 return RegFlushKey( hkey );