2 * Unix SMB/CIFS implementation.
3 * Windows NT registry I/O library
4 * Copyright (c) Gerald (Jerry) Carter 2005
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #define DBGC_CLASS DBGC_REGISTRY
26 /*******************************************************************
28 * TODO : Right now this code basically ignores classnames.
30 ******************************************************************/
33 /*******************************************************************
34 *******************************************************************/
36 static int write_block( REGF_FILE *file, prs_struct *ps, uint32 offset )
38 int bytes_written, returned;
39 char *buffer = prs_data_p( ps );
40 uint32 buffer_size = prs_data_size( ps );
46 /* check for end of file */
48 if ( sys_fstat( file->fd, &sbuf ) ) {
49 DEBUG(0,("write_block: stat() failed! (%s)\n", strerror(errno)));
53 if ( lseek( file->fd, offset, SEEK_SET ) == -1 ) {
54 DEBUG(0,("write_block: lseek() failed! (%s)\n", strerror(errno) ));
58 bytes_written = returned = 0;
59 while ( bytes_written < buffer_size ) {
60 if ( (returned = write( file->fd, buffer+bytes_written, buffer_size-bytes_written )) == -1 ) {
61 DEBUG(0,("write_block: write() failed! (%s)\n", strerror(errno) ));
65 bytes_written += returned;
71 /*******************************************************************
72 *******************************************************************/
74 static int read_block( REGF_FILE *file, prs_struct *ps, uint32 file_offset, uint32 block_size )
76 int bytes_read, returned;
80 /* check for end of file */
82 if ( sys_fstat( file->fd, &sbuf ) ) {
83 DEBUG(0,("read_block: stat() failed! (%s)\n", strerror(errno)));
87 if ( (size_t)file_offset >= sbuf.st_size )
90 /* if block_size == 0, we are parsing HBIN records and need
91 to read some of the header to get the block_size from there */
93 if ( block_size == 0 ) {
96 if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) {
97 DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) ));
101 returned = read( file->fd, hdr, 0x20 );
102 if ( (returned == -1) || (returned < 0x20) ) {
103 DEBUG(0,("read_block: failed to read in HBIN header. Is the file corrupt?\n"));
107 /* make sure this is an hbin header */
109 if ( strncmp( hdr, "hbin", HBIN_HDR_SIZE ) != 0 ) {
110 DEBUG(0,("read_block: invalid block header!\n"));
114 block_size = IVAL( hdr, 0x08 );
117 DEBUG(10,("read_block: block_size == 0x%x\n", block_size ));
119 /* set the offset, initialize the buffer, and read the block from disk */
121 if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) {
122 DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) ));
126 prs_init( ps, block_size, file->mem_ctx, UNMARSHALL );
127 buffer = prs_data_p( ps );
128 bytes_read = returned = 0;
130 while ( bytes_read < block_size ) {
131 if ( (returned = read( file->fd, buffer+bytes_read, block_size-bytes_read )) == -1 ) {
132 DEBUG(0,("read_block: read() failed (%s)\n", strerror(errno) ));
135 if ( (returned == 0) && (bytes_read < block_size) ) {
136 DEBUG(0,("read_block: not a vald registry file ?\n" ));
140 bytes_read += returned;
146 /*******************************************************************
147 *******************************************************************/
149 static BOOL write_hbin_block( REGF_FILE *file, REGF_HBIN *hbin )
154 /* write free space record if any is available */
156 if ( hbin->free_off != REGF_OFFSET_NONE ) {
157 uint32 header = 0xffffffff;
159 if ( !prs_set_offset( &hbin->ps, hbin->free_off-sizeof(uint32) ) )
161 if ( !prs_uint32( "free_size", &hbin->ps, 0, &hbin->free_size ) )
163 if ( !prs_uint32( "free_header", &hbin->ps, 0, &header ) )
167 hbin->dirty = (write_block( file, &hbin->ps, hbin->file_off ) != -1);
172 /*******************************************************************
173 *******************************************************************/
175 static BOOL hbin_block_close( REGF_FILE *file, REGF_HBIN *hbin )
179 /* remove the block from the open list and flush it to disk */
181 for ( p=file->block_list; p && p!=hbin; p=p->next )
185 DLIST_REMOVE( file->block_list, hbin );
188 DEBUG(0,("hbin_block_close: block not in open list!\n"));
190 if ( !write_hbin_block( file, hbin ) )
196 /*******************************************************************
197 *******************************************************************/
199 static BOOL prs_regf_block( const char *desc, prs_struct *ps, int depth, REGF_FILE *file )
201 prs_debug(ps, depth, desc, "prs_regf_block");
204 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)file->header, sizeof( file->header )) )
207 /* yes, these values are always identical so store them only once */
209 if ( !prs_uint32( "unknown1", ps, depth, &file->unknown1 ))
211 if ( !prs_uint32( "unknown1 (again)", ps, depth, &file->unknown1 ))
214 /* get the modtime */
216 if ( !prs_set_offset( ps, 0x0c ) )
218 if ( !smb_io_time( "modtime", &file->mtime, ps, depth ) )
223 if ( !prs_uint32( "unknown2", ps, depth, &file->unknown2 ))
225 if ( !prs_uint32( "unknown3", ps, depth, &file->unknown3 ))
227 if ( !prs_uint32( "unknown4", ps, depth, &file->unknown4 ))
229 if ( !prs_uint32( "unknown5", ps, depth, &file->unknown5 ))
232 /* get file offsets */
234 if ( !prs_set_offset( ps, 0x24 ) )
236 if ( !prs_uint32( "data_offset", ps, depth, &file->data_offset ))
238 if ( !prs_uint32( "last_block", ps, depth, &file->last_block ))
241 /* one more constant */
243 if ( !prs_uint32( "unknown6", ps, depth, &file->unknown6 ))
246 /* get the checksum */
248 if ( !prs_set_offset( ps, 0x01fc ) )
250 if ( !prs_uint32( "checksum", ps, depth, &file->checksum ))
256 /*******************************************************************
257 *******************************************************************/
259 static BOOL prs_hbin_block( const char *desc, prs_struct *ps, int depth, REGF_HBIN *hbin )
263 prs_debug(ps, depth, desc, "prs_regf_block");
266 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)hbin->header, sizeof( hbin->header )) )
269 if ( !prs_uint32( "first_hbin_off", ps, depth, &hbin->first_hbin_off ))
272 /* The dosreg.cpp comments say that the block size is at 0x1c.
273 According to a WINXP NTUSER.dat file, this is wrong. The block_size
276 if ( !prs_uint32( "block_size", ps, depth, &hbin->block_size ))
279 block_size2 = hbin->block_size;
280 prs_set_offset( ps, 0x1c );
281 if ( !prs_uint32( "block_size2", ps, depth, &block_size2 ))
284 if ( MARSHALLING(ps) )
291 /*******************************************************************
292 *******************************************************************/
294 static BOOL prs_nk_rec( const char *desc, prs_struct *ps, int depth, REGF_NK_REC *nk )
296 uint16 class_length, name_length;
298 uint32 data_size, start_off, end_off;
299 uint32 unknown_off = REGF_OFFSET_NONE;
301 nk->hbin_off = prs_offset( ps );
302 start = nk->hbin_off;
304 prs_debug(ps, depth, desc, "prs_nk_rec");
307 /* back up and get the data_size */
309 if ( !prs_set_offset( ps, prs_offset(ps)-sizeof(uint32)) )
311 start_off = prs_offset( ps );
312 if ( !prs_uint32( "rec_size", ps, depth, &nk->rec_size ))
315 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)nk->header, sizeof( nk->header )) )
318 if ( !prs_uint16( "key_type", ps, depth, &nk->key_type ))
320 if ( !smb_io_time( "mtime", &nk->mtime, ps, depth ))
323 if ( !prs_set_offset( ps, start+0x0010 ) )
325 if ( !prs_uint32( "parent_off", ps, depth, &nk->parent_off ))
327 if ( !prs_uint32( "num_subkeys", ps, depth, &nk->num_subkeys ))
330 if ( !prs_set_offset( ps, start+0x001c ) )
332 if ( !prs_uint32( "subkeys_off", ps, depth, &nk->subkeys_off ))
334 if ( !prs_uint32( "unknown_off", ps, depth, &unknown_off) )
337 if ( !prs_set_offset( ps, start+0x0024 ) )
339 if ( !prs_uint32( "num_values", ps, depth, &nk->num_values ))
341 if ( !prs_uint32( "values_off", ps, depth, &nk->values_off ))
343 if ( !prs_uint32( "sk_off", ps, depth, &nk->sk_off ))
345 if ( !prs_uint32( "classname_off", ps, depth, &nk->classname_off ))
348 if ( !prs_uint32( "max_bytes_subkeyname", ps, depth, &nk->max_bytes_subkeyname))
350 if ( !prs_uint32( "max_bytes_subkeyclassname", ps, depth, &nk->max_bytes_subkeyclassname))
352 if ( !prs_uint32( "max_bytes_valuename", ps, depth, &nk->max_bytes_valuename))
354 if ( !prs_uint32( "max_bytes_value", ps, depth, &nk->max_bytes_value))
356 if ( !prs_uint32( "unknown index", ps, depth, &nk->unk_index))
359 name_length = nk->keyname ? strlen(nk->keyname) : 0 ;
360 class_length = nk->classname ? strlen(nk->classname) : 0 ;
361 if ( !prs_uint16( "name_length", ps, depth, &name_length ))
363 if ( !prs_uint16( "class_length", ps, depth, &class_length ))
366 if ( class_length ) {
371 if ( UNMARSHALLING(ps) ) {
372 if ( !(nk->keyname = PRS_ALLOC_MEM( ps, char, name_length+1 )) )
376 if ( !prs_uint8s( True, "name", ps, depth, (uint8*)nk->keyname, name_length) )
379 if ( UNMARSHALLING(ps) )
380 nk->keyname[name_length] = '\0';
383 end_off = prs_offset( ps );
385 /* data_size must be divisible by 8 and large enough to hold the original record */
387 data_size = ((start_off - end_off) & 0xfffffff8 );
388 if ( data_size > nk->rec_size )
389 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, nk->rec_size));
391 if ( MARSHALLING(ps) )
392 nk->hbin->dirty = True;
397 /*******************************************************************
398 *******************************************************************/
400 static uint32 regf_block_checksum( prs_struct *ps )
402 char *buffer = prs_data_p( ps );
406 /* XOR of all bytes 0x0000 - 0x01FB */
410 for ( i=0; i<0x01FB; i+=4 ) {
411 x = IVAL(buffer, i );
418 /*******************************************************************
419 *******************************************************************/
421 static BOOL read_regf_block( REGF_FILE *file )
426 /* grab the first block from the file */
428 if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) == -1 )
431 /* parse the block and verify the checksum */
433 if ( !prs_regf_block( "regf_header", &ps, 0, file ) )
436 checksum = regf_block_checksum( &ps );
440 if ( file->checksum != checksum ) {
441 DEBUG(0,("read_regf_block: invalid checksum\n" ));
448 /*******************************************************************
449 *******************************************************************/
451 static REGF_HBIN* read_hbin_block( REGF_FILE *file, off_t offset )
454 uint32 record_size, curr_off, block_size, header;
456 if ( !(hbin = TALLOC_ZERO_P(file->mem_ctx, REGF_HBIN)) )
458 hbin->file_off = offset;
461 if ( read_block( file, &hbin->ps, offset, 0 ) == -1 )
464 if ( !prs_hbin_block( "hbin", &hbin->ps, 0, hbin ) )
467 /* this should be the same thing as hbin->block_size but just in case */
469 block_size = prs_data_size( &hbin->ps );
471 /* Find the available free space offset. Always at the end,
472 so walk the record list and stop when you get to the end.
473 The end is defined by a record header of 0xffffffff. The
474 previous 4 bytes contains the amount of free space remaining
475 in the hbin block. */
477 /* remember that the record_size is in the 4 bytes preceeding the record itself */
479 if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE-sizeof(uint32) ) )
484 curr_off = prs_offset( &hbin->ps );
485 while ( header != 0xffffffff ) {
486 /* not done yet so reset the current offset to the
487 next record_size field */
489 curr_off = curr_off+record_size;
491 /* for some reason the record_size of the last record in
492 an hbin block can extend past the end of the block
493 even though the record fits within the remaining
494 space....aaarrrgggghhhhhh */
496 if ( curr_off >= block_size ) {
502 if ( !prs_set_offset( &hbin->ps, curr_off) )
505 if ( !prs_uint32( "rec_size", &hbin->ps, 0, &record_size ) )
507 if ( !prs_uint32( "header", &hbin->ps, 0, &header ) )
510 SMB_ASSERT( record_size != 0 );
512 if ( record_size & 0x80000000 ) {
513 /* absolute_value(record_size) */
514 record_size = (record_size ^ 0xffffffff) + 1;
518 /* save the free space offset */
520 if ( header == 0xffffffff ) {
522 /* account for the fact that the curr_off is 4 bytes behind the actual
525 hbin->free_off = curr_off + sizeof(uint32);
526 hbin->free_size = record_size;
529 DEBUG(10,("read_hbin_block: free space offset == 0x%x\n", hbin->free_off));
531 if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE ) )
537 /*******************************************************************
538 Input a random offset and receive the corresponding HBIN
540 *******************************************************************/
542 static BOOL hbin_contains_offset( REGF_HBIN *hbin, uint32 offset )
547 if ( (offset > hbin->first_hbin_off) && (offset < (hbin->first_hbin_off+hbin->block_size)) )
553 /*******************************************************************
554 Input a random offset and receive the corresponding HBIN
556 *******************************************************************/
558 static REGF_HBIN* lookup_hbin_block( REGF_FILE *file, uint32 offset )
560 REGF_HBIN *hbin = NULL;
563 /* start with the open list */
565 for ( hbin=file->block_list; hbin; hbin=hbin->next ) {
566 DEBUG(10,("lookup_hbin_block: address = 0x%x [0x%lx]\n", hbin->file_off, (unsigned long)hbin ));
567 if ( hbin_contains_offset( hbin, offset ) )
572 /* start at the beginning */
574 block_off = REGF_BLOCKSIZE;
576 /* cleanup before the next round */
578 prs_mem_free( &hbin->ps );
580 hbin = read_hbin_block( file, block_off );
583 block_off = hbin->file_off + hbin->block_size;
585 } while ( hbin && !hbin_contains_offset( hbin, offset ) );
589 DLIST_ADD( file->block_list, hbin );
594 /*******************************************************************
595 *******************************************************************/
597 static BOOL prs_hash_rec( const char *desc, prs_struct *ps, int depth, REGF_HASH_REC *hash )
599 prs_debug(ps, depth, desc, "prs_hash_rec");
602 if ( !prs_uint32( "nk_off", ps, depth, &hash->nk_off ))
604 if ( !prs_uint8s( True, "keycheck", ps, depth, hash->keycheck, sizeof( hash->keycheck )) )
610 /*******************************************************************
611 *******************************************************************/
613 static BOOL hbin_prs_lf_records( const char *desc, REGF_HBIN *hbin, int depth, REGF_NK_REC *nk )
616 REGF_LF_REC *lf = &nk->subkeys;
617 uint32 data_size, start_off, end_off;
619 prs_debug(&hbin->ps, depth, desc, "prs_lf_records");
622 /* check if we have anything to do first */
624 if ( nk->num_subkeys == 0 )
627 /* move to the LF record */
629 if ( !prs_set_offset( &hbin->ps, nk->subkeys_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
632 /* backup and get the data_size */
634 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) )
636 start_off = prs_offset( &hbin->ps );
637 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &lf->rec_size ))
640 if ( !prs_uint8s( True, "header", &hbin->ps, depth, (uint8*)lf->header, sizeof( lf->header )) )
643 if ( !prs_uint16( "num_keys", &hbin->ps, depth, &lf->num_keys))
646 if ( UNMARSHALLING(&hbin->ps) ) {
648 if ( !(lf->hashes = PRS_ALLOC_MEM( &hbin->ps, REGF_HASH_REC, lf->num_keys )) )
655 for ( i=0; i<lf->num_keys; i++ ) {
656 if ( !prs_hash_rec( "hash_rec", &hbin->ps, depth, &lf->hashes[i] ) )
660 end_off = prs_offset( &hbin->ps );
662 /* data_size must be divisible by 8 and large enough to hold the original record */
664 data_size = ((start_off - end_off) & 0xfffffff8 );
665 if ( data_size > lf->rec_size )
666 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, lf->rec_size));
668 if ( MARSHALLING(&hbin->ps) )
674 /*******************************************************************
675 *******************************************************************/
677 static BOOL hbin_prs_sk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_SK_REC *sk )
679 prs_struct *ps = &hbin->ps;
681 uint32 data_size, start_off, end_off;
684 prs_debug(ps, depth, desc, "hbin_prs_sk_rec");
687 if ( !prs_set_offset( &hbin->ps, sk->sk_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
690 /* backup and get the data_size */
692 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) )
694 start_off = prs_offset( &hbin->ps );
695 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &sk->rec_size ))
698 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)sk->header, sizeof( sk->header )) )
700 if ( !prs_uint16( "tag", ps, depth, &tag))
703 if ( !prs_uint32( "prev_sk_off", ps, depth, &sk->prev_sk_off))
705 if ( !prs_uint32( "next_sk_off", ps, depth, &sk->next_sk_off))
707 if ( !prs_uint32( "ref_count", ps, depth, &sk->ref_count))
709 if ( !prs_uint32( "size", ps, depth, &sk->size))
712 if ( !sec_io_desc( "sec_desc", &sk->sec_desc, ps, depth ))
715 end_off = prs_offset( &hbin->ps );
717 /* data_size must be divisible by 8 and large enough to hold the original record */
719 data_size = ((start_off - end_off) & 0xfffffff8 );
720 if ( data_size > sk->rec_size )
721 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, sk->rec_size));
723 if ( MARSHALLING(&hbin->ps) )
729 /*******************************************************************
730 *******************************************************************/
732 static BOOL hbin_prs_vk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_VK_REC *vk, REGF_FILE *file )
736 prs_struct *ps = &hbin->ps;
737 uint32 data_size, start_off, end_off;
739 prs_debug(ps, depth, desc, "prs_vk_rec");
742 /* backup and get the data_size */
744 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) )
746 start_off = prs_offset( &hbin->ps );
747 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &vk->rec_size ))
750 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)vk->header, sizeof( vk->header )) )
753 if ( MARSHALLING(&hbin->ps) )
754 name_length = strlen(vk->valuename);
756 if ( !prs_uint16( "name_length", ps, depth, &name_length ))
758 if ( !prs_uint32( "data_size", ps, depth, &vk->data_size ))
760 if ( !prs_uint32( "data_off", ps, depth, &vk->data_off ))
762 if ( !prs_uint32( "type", ps, depth, &vk->type))
764 if ( !prs_uint16( "flag", ps, depth, &vk->flag))
767 offset = prs_offset( ps );
768 offset += 2; /* skip 2 bytes */
769 prs_set_offset( ps, offset );
773 if ( vk->flag&VK_FLAG_NAME_PRESENT ) {
775 if ( UNMARSHALLING(&hbin->ps) ) {
776 if ( !(vk->valuename = PRS_ALLOC_MEM( ps, char, name_length+1 )))
779 if ( !prs_uint8s( True, "name", ps, depth, (uint8*)vk->valuename, name_length ) )
783 end_off = prs_offset( &hbin->ps );
785 /* get the data if necessary */
787 if ( vk->data_size != 0 ) {
788 BOOL charmode = False;
790 if ( (vk->type == REG_SZ) || (vk->type == REG_MULTI_SZ) )
793 /* the data is stored in the offset if the size <= 4 */
795 if ( !(vk->data_size & VK_DATA_IN_OFFSET) ) {
796 REGF_HBIN *hblock = hbin;
797 uint32 data_rec_size;
799 if ( UNMARSHALLING(&hbin->ps) ) {
800 if ( !(vk->data = PRS_ALLOC_MEM( ps, uint8, vk->data_size) ) )
804 /* this data can be in another hbin */
805 if ( !hbin_contains_offset( hbin, vk->data_off ) ) {
806 if ( !(hblock = lookup_hbin_block( file, vk->data_off )) )
809 if ( !(prs_set_offset( &hblock->ps, (vk->data_off+HBIN_HDR_SIZE-hblock->first_hbin_off)-sizeof(uint32) )) )
812 if ( MARSHALLING(&hblock->ps) ) {
813 data_rec_size = ( (vk->data_size+sizeof(uint32)) & 0xfffffff8 ) + 8;
814 data_rec_size = ( data_rec_size - 1 ) ^ 0xFFFFFFFF;
816 if ( !prs_uint32( "data_rec_size", &hblock->ps, depth, &data_rec_size ))
818 if ( !prs_uint8s( charmode, "data", &hblock->ps, depth, vk->data, vk->data_size) )
821 if ( MARSHALLING(&hblock->ps) )
822 hblock->dirty = True;
825 if ( !(vk->data = PRS_ALLOC_MEM( ps, uint8, 4 ) ) )
827 SIVAL( vk->data, 0, vk->data_off );
832 /* data_size must be divisible by 8 and large enough to hold the original record */
834 data_size = ((start_off - end_off ) & 0xfffffff8 );
835 if ( data_size != vk->rec_size )
836 DEBUG(10,("prs_vk_rec: data_size check failed (0x%x < 0x%x)\n", data_size, vk->rec_size));
838 if ( MARSHALLING(&hbin->ps) )
844 /*******************************************************************
845 read a VK record which is contained in the HBIN block stored
846 in the prs_struct *ps.
847 *******************************************************************/
849 static BOOL hbin_prs_vk_records( const char *desc, REGF_HBIN *hbin, int depth, REGF_NK_REC *nk, REGF_FILE *file )
854 prs_debug(&hbin->ps, depth, desc, "prs_vk_records");
857 /* check if we have anything to do first */
859 if ( nk->num_values == 0 )
862 if ( UNMARSHALLING(&hbin->ps) ) {
863 if ( !(nk->values = PRS_ALLOC_MEM( &hbin->ps, REGF_VK_REC, nk->num_values ) ) )
867 /* convert the offset to something relative to this HBIN block */
869 if ( !prs_set_offset( &hbin->ps, nk->values_off+HBIN_HDR_SIZE-hbin->first_hbin_off-sizeof(uint32)) )
872 if ( MARSHALLING( &hbin->ps) ) {
873 record_size = ( ( nk->num_values * sizeof(uint32) ) & 0xfffffff8 ) + 8;
874 record_size = (record_size - 1) ^ 0xFFFFFFFF;
877 if ( !prs_uint32( "record_size", &hbin->ps, depth, &record_size ) )
880 for ( i=0; i<nk->num_values; i++ ) {
881 if ( !prs_uint32( "vk_off", &hbin->ps, depth, &nk->values[i].rec_off ) )
885 for ( i=0; i<nk->num_values; i++ ) {
886 REGF_HBIN *sub_hbin = hbin;
889 if ( !hbin_contains_offset( hbin, nk->values[i].rec_off ) ) {
890 sub_hbin = lookup_hbin_block( file, nk->values[i].rec_off );
892 DEBUG(0,("hbin_prs_vk_records: Failed to find HBIN block containing offset [0x%x]\n",
893 nk->values[i].hbin_off));
898 new_offset = nk->values[i].rec_off + HBIN_HDR_SIZE - sub_hbin->first_hbin_off;
899 if ( !prs_set_offset( &sub_hbin->ps, new_offset ) )
901 if ( !hbin_prs_vk_rec( "vk_rec", sub_hbin, depth, &nk->values[i], file ) )
905 if ( MARSHALLING(&hbin->ps) )
913 /*******************************************************************
914 *******************************************************************/
916 static REGF_SK_REC* find_sk_record_by_offset( REGF_FILE *file, uint32 offset )
920 for ( p_sk=file->sec_desc_list; p_sk; p_sk=p_sk->next ) {
921 if ( p_sk->sk_off == offset )
928 /*******************************************************************
929 *******************************************************************/
931 static REGF_SK_REC* find_sk_record_by_sec_desc( REGF_FILE *file, SEC_DESC *sd )
935 for ( p=file->sec_desc_list; p; p=p->next ) {
936 if ( sec_desc_equal( p->sec_desc, sd ) )
945 /*******************************************************************
946 *******************************************************************/
948 static BOOL hbin_prs_key( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk )
953 prs_debug(&hbin->ps, depth, "", "fetch_key");
956 /* get the initial nk record */
958 if ( !prs_nk_rec( "nk_rec", &hbin->ps, depth, nk ))
963 if ( nk->num_values && (nk->values_off!=REGF_OFFSET_NONE) ) {
965 if ( !hbin_contains_offset( hbin, nk->values_off ) ) {
966 sub_hbin = lookup_hbin_block( file, nk->values_off );
968 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing value_list_offset [0x%x]\n",
974 if ( !hbin_prs_vk_records( "vk_rec", sub_hbin, depth, nk, file ))
978 /* now get subkeys */
980 if ( nk->num_subkeys && (nk->subkeys_off!=REGF_OFFSET_NONE) ) {
982 if ( !hbin_contains_offset( hbin, nk->subkeys_off ) ) {
983 sub_hbin = lookup_hbin_block( file, nk->subkeys_off );
985 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing subkey_offset [0x%x]\n",
991 if ( !hbin_prs_lf_records( "lf_rec", sub_hbin, depth, nk ))
995 /* get the to the security descriptor. First look if we have already parsed it */
997 if ( (nk->sk_off!=REGF_OFFSET_NONE) && !( nk->sec_desc = find_sk_record_by_offset( file, nk->sk_off )) ) {
1000 if ( !hbin_contains_offset( hbin, nk->sk_off ) ) {
1001 sub_hbin = lookup_hbin_block( file, nk->sk_off );
1003 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing sk_offset [0x%x]\n",
1009 if ( !(nk->sec_desc = TALLOC_ZERO_P( file->mem_ctx, REGF_SK_REC )) )
1011 nk->sec_desc->sk_off = nk->sk_off;
1012 if ( !hbin_prs_sk_rec( "sk_rec", sub_hbin, depth, nk->sec_desc ))
1015 /* add to the list of security descriptors (ref_count has been read from the files) */
1017 nk->sec_desc->sk_off = nk->sk_off;
1018 DLIST_ADD( file->sec_desc_list, nk->sec_desc );
1024 /*******************************************************************
1025 *******************************************************************/
1027 static BOOL next_record( REGF_HBIN *hbin, const char *hdr, BOOL *eob )
1029 uint8 header[REC_HDR_SIZE];
1031 uint32 curr_off, block_size;
1033 prs_struct *ps = &hbin->ps;
1035 curr_off = prs_offset( ps );
1036 if ( curr_off == 0 )
1037 prs_set_offset( ps, HBIN_HEADER_REC_SIZE );
1039 /* assume that the current offset is at the record header
1040 and we need to backup to read the record size */
1042 curr_off -= sizeof(uint32);
1044 block_size = prs_data_size( ps );
1046 memset( header, 0x0, sizeof(uint8)*REC_HDR_SIZE );
1049 curr_off = curr_off+record_size;
1050 if ( curr_off >= block_size )
1053 if ( !prs_set_offset( &hbin->ps, curr_off) )
1056 if ( !prs_uint32( "record_size", ps, 0, &record_size ) )
1058 if ( !prs_uint8s( True, "header", ps, 0, header, REC_HDR_SIZE ) )
1061 if ( record_size & 0x80000000 ) {
1062 /* absolute_value(record_size) */
1063 record_size = (record_size ^ 0xffffffff) + 1;
1066 if ( memcmp( header, hdr, REC_HDR_SIZE ) == 0 ) {
1068 curr_off += sizeof(uint32);
1072 /* mark prs_struct as done ( at end ) if no more SK records */
1073 /* mark end-of-block as True */
1076 prs_set_offset( &hbin->ps, prs_data_size(&hbin->ps) );
1081 if ( !prs_set_offset( ps, curr_off ) )
1087 /*******************************************************************
1088 *******************************************************************/
1090 static BOOL next_nk_record( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk, BOOL *eob )
1092 if ( next_record( hbin, "nk", eob ) && hbin_prs_key( file, hbin, nk ) )
1098 /*******************************************************************
1099 Intialize the newly created REGF_BLOCK in *file and write the
1100 block header to disk
1101 *******************************************************************/
1103 static BOOL init_regf_block( REGF_FILE *file )
1108 if ( !prs_init( &ps, REGF_BLOCKSIZE, file->mem_ctx, MARSHALL ) )
1111 memcpy( file->header, "regf", REGF_HDR_SIZE );
1112 file->data_offset = 0x20;
1113 file->last_block = 0x1000;
1117 unix_to_nt_time( &file->mtime, time(NULL) );
1119 /* hard coded values...no diea what these are ... maybe in time */
1121 file->unknown1 = 0x2;
1122 file->unknown2 = 0x1;
1123 file->unknown3 = 0x3;
1124 file->unknown4 = 0x0;
1125 file->unknown5 = 0x1;
1126 file->unknown6 = 0x1;
1128 /* write header to the buffer */
1130 if ( !prs_regf_block( "regf_header", &ps, 0, file ) ) {
1135 /* calculate the checksum, re-marshall data (to include the checksum)
1136 and write to disk */
1138 file->checksum = regf_block_checksum( &ps );
1139 prs_set_offset( &ps, 0 );
1140 if ( !prs_regf_block( "regf_header", &ps, 0, file ) ) {
1145 if ( write_block( file, &ps, 0 ) == -1 ) {
1146 DEBUG(0,("init_regf_block: Failed to initialize registry header block!\n"));
1152 prs_mem_free( &ps );
1156 /*******************************************************************
1157 Open the registry file and then read in the REGF block to get the
1159 *******************************************************************/
1161 REGF_FILE* regfio_open( const char *filename, int flags, int mode )
1165 if ( !(rb = SMB_MALLOC_P(REGF_FILE)) ) {
1166 DEBUG(0,("ERROR allocating memory\n"));
1172 if ( !(rb->mem_ctx = talloc_init( "read_regf_block" )) ) {
1177 rb->open_flags = flags;
1179 /* open and existing file */
1181 if ( (rb->fd = open(filename, flags, mode)) == -1 ) {
1182 DEBUG(0,("regfio_open: failure to open %s (%s)\n", filename, strerror(errno)));
1187 /* check if we are creating a new file or overwriting an existing one */
1189 if ( flags & (O_CREAT|O_TRUNC) ) {
1190 if ( !init_regf_block( rb ) ) {
1191 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1200 /* read in an existing file */
1202 if ( !read_regf_block( rb ) ) {
1203 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1213 /*******************************************************************
1214 *******************************************************************/
1216 static void regfio_mem_free( REGF_FILE *file )
1218 /* free any talloc()'d memory */
1220 if ( file && file->mem_ctx )
1221 talloc_destroy( file->mem_ctx );
1224 /*******************************************************************
1225 *******************************************************************/
1227 int regfio_close( REGF_FILE *file )
1231 /* cleanup for a file opened for write */
1233 if ( file->open_flags & (O_WRONLY|O_RDWR) ) {
1237 /* write of sd list */
1239 for ( sk=file->sec_desc_list; sk; sk=sk->next ) {
1240 hbin_prs_sk_rec( "sk_rec", sk->hbin, 0, sk );
1243 /* flush any dirty blocks */
1245 while ( file->block_list ) {
1246 hbin_block_close( file, file->block_list );
1251 unix_to_nt_time( &file->mtime, time(NULL) );
1253 if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) != -1 ) {
1254 /* now use for writing */
1255 prs_switch_type( &ps, MARSHALL );
1257 /* stream the block once, generate the checksum,
1258 and stream it again */
1259 prs_set_offset( &ps, 0 );
1260 prs_regf_block( "regf_blocK", &ps, 0, file );
1261 file->checksum = regf_block_checksum( &ps );
1262 prs_set_offset( &ps, 0 );
1263 prs_regf_block( "regf_blocK", &ps, 0, file );
1265 /* now we are ready to write it to disk */
1266 if ( write_block( file, &ps, 0 ) == -1 )
1267 DEBUG(0,("regfio_close: failed to update the regf header block!\n"));
1270 prs_mem_free( &ps );
1273 regfio_mem_free( file );
1275 /* nothing tdo do if there is no open file */
1277 if ( !file || (file->fd == -1) )
1287 /*******************************************************************
1288 *******************************************************************/
1290 static void regfio_flush( REGF_FILE *file )
1294 for ( hbin=file->block_list; hbin; hbin=hbin->next ) {
1295 write_hbin_block( file, hbin );
1299 /*******************************************************************
1300 There should be only *one* root key in the registry file based
1301 on my experience. --jerry
1302 *******************************************************************/
1304 REGF_NK_REC* regfio_rootkey( REGF_FILE *file )
1308 uint32 offset = REGF_BLOCKSIZE;
1315 if ( !(nk = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) ) {
1316 DEBUG(0,("regfio_rootkey: talloc() failed!\n"));
1320 /* scan through the file on HBIN block at a time looking
1321 for an NK record with a type == 0x002c.
1322 Normally this is the first nk record in the first hbin
1323 block (but I'm not assuming that for now) */
1325 while ( (hbin = read_hbin_block( file, offset )) ) {
1329 if ( next_nk_record( file, hbin, nk, &eob ) ) {
1330 if ( nk->key_type == NK_TYPE_ROOTKEY ) {
1335 prs_mem_free( &hbin->ps );
1341 offset += hbin->block_size;
1345 DEBUG(0,("regfio_rootkey: corrupt registry file ? No root key record located\n"));
1349 DLIST_ADD( file->block_list, hbin );
1354 /*******************************************************************
1355 This acts as an interator over the subkeys defined for a given
1356 NK record. Remember that offsets are from the *first* HBIN block.
1357 *******************************************************************/
1359 REGF_NK_REC* regfio_fetch_subkey( REGF_FILE *file, REGF_NK_REC *nk )
1361 REGF_NK_REC *subkey;
1365 /* see if there is anything left to report */
1367 if ( !nk || (nk->subkeys_off==REGF_OFFSET_NONE) || (nk->subkey_index >= nk->num_subkeys) )
1370 /* find the HBIN block which should contain the nk record */
1372 if ( !(hbin = lookup_hbin_block( file, nk->subkeys.hashes[nk->subkey_index].nk_off )) ) {
1373 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing offset [0x%x]\n",
1374 nk->subkeys.hashes[nk->subkey_index].nk_off));
1378 nk_offset = nk->subkeys.hashes[nk->subkey_index].nk_off;
1379 if ( !prs_set_offset( &hbin->ps, (HBIN_HDR_SIZE + nk_offset - hbin->first_hbin_off) ) )
1383 if ( !(subkey = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) )
1386 if ( !hbin_prs_key( file, hbin, subkey ) )
1393 /*******************************************************************
1394 *******************************************************************/
1396 static REGF_HBIN* regf_hbin_allocate( REGF_FILE *file, uint32 block_size )
1399 SMB_STRUCT_STAT sbuf;
1401 if ( !(hbin = TALLOC_ZERO_P( file->mem_ctx, REGF_HBIN )) )
1404 memcpy( hbin->header, "hbin", sizeof(HBIN_HDR_SIZE) );
1407 if ( sys_fstat( file->fd, &sbuf ) ) {
1408 DEBUG(0,("regf_hbin_allocate: stat() failed! (%s)\n", strerror(errno)));
1412 hbin->file_off = sbuf.st_size;
1414 hbin->free_off = HBIN_HEADER_REC_SIZE;
1415 hbin->free_size = block_size - hbin->free_off + sizeof(uint32);;
1417 hbin->block_size = block_size;
1418 hbin->first_hbin_off = hbin->file_off - REGF_BLOCKSIZE;
1420 if ( !prs_init( &hbin->ps, block_size, file->mem_ctx, MARSHALL ) )
1423 if ( !prs_hbin_block( "new_hbin", &hbin->ps, 0, hbin ) )
1426 if ( !write_hbin_block( file, hbin ) )
1429 file->last_block = hbin->file_off;
1434 /*******************************************************************
1435 *******************************************************************/
1437 static void update_free_space( REGF_HBIN *hbin, uint32 size_used )
1439 hbin->free_off += size_used;
1440 hbin->free_size -= size_used;
1442 if ( hbin->free_off >= hbin->block_size ) {
1443 hbin->free_off = REGF_OFFSET_NONE;
1449 /*******************************************************************
1450 *******************************************************************/
1452 static REGF_HBIN* find_free_space( REGF_FILE *file, uint32 size )
1454 REGF_HBIN *hbin, *p_hbin;
1458 /* check open block list */
1460 for ( hbin=file->block_list; hbin!=NULL; hbin=hbin->next ) {
1461 /* only check blocks that actually have available space */
1463 if ( hbin->free_off == REGF_OFFSET_NONE )
1466 /* check for a large enough available chunk */
1468 if ( (hbin->block_size - hbin->free_off) >= size ) {
1469 DLIST_PROMOTE( file->block_list, hbin );
1474 /* parse the file until we find a block with
1475 enough free space; save the last non-filled hbin */
1477 block_off = REGF_BLOCKSIZE;
1479 /* cleanup before the next round */
1482 prs_mem_free( &hbin->ps );
1484 hbin = read_hbin_block( file, block_off );
1488 /* make sure that we don't already have this block in memory */
1490 for ( p_hbin=file->block_list; p_hbin!=NULL; p_hbin=p_hbin->next ) {
1491 if ( p_hbin->file_off == hbin->file_off ) {
1497 block_off = hbin->file_off + hbin->block_size;
1500 prs_mem_free( &hbin->ps );
1505 /* if (cached block or (new block and not enough free space)) then continue looping */
1506 } while ( cached || (hbin && (hbin->free_size < size)) );
1508 /* no free space; allocate a new one */
1513 /* allocate in multiples of REGF_ALLOC_BLOCK; make sure (size + hbin_header) fits */
1515 alloc_size = (((size+HBIN_HEADER_REC_SIZE) / REGF_ALLOC_BLOCK ) + 1 ) * REGF_ALLOC_BLOCK;
1517 if ( !(hbin = regf_hbin_allocate( file, alloc_size )) ) {
1518 DEBUG(0,("find_free_space: regf_hbin_allocate() failed!\n"));
1521 DLIST_ADD( file->block_list, hbin );
1525 /* set the offset to be ready to write */
1527 if ( !prs_set_offset( &hbin->ps, hbin->free_off-sizeof(uint32) ) )
1530 /* write the record size as a placeholder for now, it should be
1531 probably updated by the caller once it all of the data necessary
1534 if ( !prs_uint32("allocated_size", &hbin->ps, 0, &size) )
1537 update_free_space( hbin, size );
1542 /*******************************************************************
1543 *******************************************************************/
1545 static uint32 sk_record_data_size( SEC_DESC * sd )
1547 uint32 size, size_mod8;
1551 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1553 size = sizeof(uint32)*5 + sec_desc_size( sd ) + sizeof(uint32);
1556 size_mod8 = size & 0xfffffff8;
1557 if ( size_mod8 < size )
1563 /*******************************************************************
1564 *******************************************************************/
1566 static uint32 vk_record_data_size( REGF_VK_REC *vk )
1568 uint32 size, size_mod8;
1572 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1574 size = REC_HDR_SIZE + (sizeof(uint16)*3) + (sizeof(uint32)*3) + sizeof(uint32);
1576 if ( vk->valuename )
1577 size += strlen(vk->valuename);
1580 size_mod8 = size & 0xfffffff8;
1581 if ( size_mod8 < size )
1587 /*******************************************************************
1588 *******************************************************************/
1590 static uint32 lf_record_data_size( uint32 num_keys )
1592 uint32 size, size_mod8;
1596 /* the record size is sizeof(hdr) + num_keys + sizeof of hash_array + data_size_uint32 */
1598 size = REC_HDR_SIZE + sizeof(uint16) + (sizeof(REGF_HASH_REC) * num_keys) + sizeof(uint32);
1601 size_mod8 = size & 0xfffffff8;
1602 if ( size_mod8 < size )
1608 /*******************************************************************
1609 *******************************************************************/
1611 static uint32 nk_record_data_size( REGF_NK_REC *nk )
1613 uint32 size, size_mod8;
1617 /* the record size is static + length_of_keyname + length_of_classname + data_size_uint32 */
1619 size = 0x4c + strlen(nk->keyname) + sizeof(uint32);
1621 if ( nk->classname )
1622 size += strlen( nk->classname );
1625 size_mod8 = size & 0xfffffff8;
1626 if ( size_mod8 < size )
1632 /*******************************************************************
1633 *******************************************************************/
1635 static BOOL create_vk_record( REGF_FILE *file, REGF_VK_REC *vk, REGISTRY_VALUE *value )
1637 char *name = regval_name(value);
1638 REGF_HBIN *data_hbin;
1642 memcpy( vk->header, "vk", REC_HDR_SIZE );
1645 vk->valuename = talloc_strdup( file->mem_ctx, regval_name(value) );
1646 vk->flag = VK_FLAG_NAME_PRESENT;
1649 vk->data_size = regval_size( value );
1650 vk->type = regval_type( value );
1652 if ( vk->data_size > sizeof(uint32) ) {
1653 uint32 data_size = ( (vk->data_size+sizeof(uint32)) & 0xfffffff8 ) + 8;
1655 vk->data = (uint8 *)TALLOC_MEMDUP( file->mem_ctx,
1656 regval_data_p(value),
1658 if (vk->data == NULL) {
1662 /* go ahead and store the offset....we'll pick this hbin block back up when
1663 we stream the data */
1665 if ((data_hbin = find_free_space(file, data_size )) == NULL) {
1668 vk->data_off = prs_offset( &data_hbin->ps ) + data_hbin->first_hbin_off - HBIN_HDR_SIZE;
1671 /* make sure we don't try to copy from a NULL value pointer */
1673 if ( vk->data_size != 0 )
1674 memcpy( &vk->data_off, regval_data_p(value), sizeof(uint32) );
1675 vk->data_size |= VK_DATA_IN_OFFSET;
1681 /*******************************************************************
1682 *******************************************************************/
1684 static int hashrec_cmp( REGF_HASH_REC *h1, REGF_HASH_REC *h2 )
1686 return StrCaseCmp( h1->fullname, h2->fullname );
1689 /*******************************************************************
1690 *******************************************************************/
1692 REGF_NK_REC* regfio_write_key( REGF_FILE *file, const char *name,
1693 REGVAL_CTR *values, REGSUBKEY_CTR *subkeys,
1694 SEC_DESC *sec_desc, REGF_NK_REC *parent )
1697 REGF_HBIN *vlist_hbin = NULL;
1700 if ( !(nk = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) )
1703 memcpy( nk->header, "nk", REC_HDR_SIZE );
1706 nk->key_type = NK_TYPE_ROOTKEY;
1708 nk->key_type = NK_TYPE_NORMALKEY;
1710 /* store the parent offset (or -1 if a the root key */
1712 nk->parent_off = parent ? (parent->hbin_off + parent->hbin->file_off - REGF_BLOCKSIZE - HBIN_HDR_SIZE ) : REGF_OFFSET_NONE;
1714 /* no classname currently */
1716 nk->classname_off = REGF_OFFSET_NONE;
1717 nk->classname = NULL;
1718 nk->keyname = talloc_strdup( file->mem_ctx, name );
1720 /* current modification time */
1722 unix_to_nt_time( &nk->mtime, time(NULL) );
1724 /* allocate the record on disk */
1726 size = nk_record_data_size( nk );
1727 nk->rec_size = ( size - 1 ) ^ 0XFFFFFFFF;
1728 if ((nk->hbin = find_free_space( file, size )) == NULL) {
1731 nk->hbin_off = prs_offset( &nk->hbin->ps );
1733 /* Update the hash record in the parent */
1736 REGF_HASH_REC *hash = &parent->subkeys.hashes[parent->subkey_index];
1738 hash->nk_off = prs_offset( &nk->hbin->ps ) + nk->hbin->first_hbin_off - HBIN_HDR_SIZE;
1739 memcpy( hash->keycheck, name, sizeof(uint32) );
1740 hash->fullname = talloc_strdup( file->mem_ctx, name );
1741 parent->subkey_index++;
1743 /* sort the list by keyname */
1745 qsort( parent->subkeys.hashes, parent->subkey_index, sizeof(REGF_HASH_REC), QSORT_CAST hashrec_cmp );
1747 if ( !hbin_prs_lf_records( "lf_rec", parent->subkeys.hbin, 0, parent ) )
1751 /* write the security descriptor */
1753 nk->sk_off = REGF_OFFSET_NONE;
1755 uint32 sk_size = sk_record_data_size( sec_desc );
1758 /* search for it in the existing list of sd's */
1760 if ( (nk->sec_desc = find_sk_record_by_sec_desc( file, sec_desc )) == NULL ) {
1761 /* not found so add it to the list */
1763 if (!(sk_hbin = find_free_space( file, sk_size ))) {
1767 if ( !(nk->sec_desc = TALLOC_ZERO_P( file->mem_ctx, REGF_SK_REC )) )
1770 /* now we have to store the security descriptor in the list and
1771 update the offsets */
1773 memcpy( nk->sec_desc->header, "sk", REC_HDR_SIZE );
1774 nk->sec_desc->hbin = sk_hbin;
1775 nk->sec_desc->hbin_off = prs_offset( &sk_hbin->ps );
1776 nk->sec_desc->sk_off = prs_offset( &sk_hbin->ps ) + sk_hbin->first_hbin_off - HBIN_HDR_SIZE;
1777 nk->sec_desc->rec_size = (sk_size-1) ^ 0xFFFFFFFF;
1779 nk->sec_desc->sec_desc = sec_desc;
1780 nk->sec_desc->ref_count = 0;
1782 /* size value must be self-inclusive */
1783 nk->sec_desc->size = sec_desc_size(sec_desc) + sizeof(uint32);
1785 DLIST_ADD_END( file->sec_desc_list, nk->sec_desc, REGF_SK_REC *);
1787 /* update the offsets for us and the previous sd in the list.
1788 if this is the first record, then just set the next and prev
1789 offsets to ourself. */
1791 if ( nk->sec_desc->prev ) {
1792 REGF_SK_REC *prev = nk->sec_desc->prev;
1794 nk->sec_desc->prev_sk_off = prev->hbin_off + prev->hbin->first_hbin_off - HBIN_HDR_SIZE;
1795 prev->next_sk_off = nk->sec_desc->sk_off;
1797 /* the end must loop around to the front */
1798 nk->sec_desc->next_sk_off = file->sec_desc_list->sk_off;
1800 /* and first must loop around to the tail */
1801 file->sec_desc_list->prev_sk_off = nk->sec_desc->sk_off;
1803 nk->sec_desc->prev_sk_off = nk->sec_desc->sk_off;
1804 nk->sec_desc->next_sk_off = nk->sec_desc->sk_off;
1808 /* bump the reference count +1 */
1810 nk->sk_off = nk->sec_desc->sk_off;
1811 nk->sec_desc->ref_count++;
1814 /* write the subkeys */
1816 nk->subkeys_off = REGF_OFFSET_NONE;
1817 if ( (nk->num_subkeys = regsubkey_ctr_numkeys( subkeys )) != 0 ) {
1818 uint32 lf_size = lf_record_data_size( nk->num_subkeys );
1822 if (!(nk->subkeys.hbin = find_free_space( file, lf_size ))) {
1825 nk->subkeys.hbin_off = prs_offset( &nk->subkeys.hbin->ps );
1826 nk->subkeys.rec_size = (lf_size-1) ^ 0xFFFFFFFF;
1827 nk->subkeys_off = prs_offset( &nk->subkeys.hbin->ps ) + nk->subkeys.hbin->first_hbin_off - HBIN_HDR_SIZE;
1829 memcpy( nk->subkeys.header, "lf", REC_HDR_SIZE );
1831 nk->subkeys.num_keys = nk->num_subkeys;
1832 if (nk->subkeys.num_keys) {
1833 if ( !(nk->subkeys.hashes = TALLOC_ZERO_ARRAY( file->mem_ctx, REGF_HASH_REC, nk->subkeys.num_keys )) )
1836 nk->subkeys.hashes = NULL;
1838 nk->subkey_index = 0;
1840 /* update the max_bytes_subkey{name,classname} fields */
1841 for ( i=0; i<nk->num_subkeys; i++ ) {
1842 namelen = strlen( regsubkey_ctr_specific_key(subkeys, i) );
1843 if ( namelen*2 > nk->max_bytes_subkeyname )
1844 nk->max_bytes_subkeyname = namelen * 2;
1848 /* write the values */
1850 nk->values_off = REGF_OFFSET_NONE;
1851 if ( (nk->num_values = regval_ctr_numvals( values )) != 0 ) {
1852 uint32 vlist_size = ( ( nk->num_values * sizeof(uint32) ) & 0xfffffff8 ) + 8;
1855 if (!(vlist_hbin = find_free_space( file, vlist_size ))) {
1858 nk->values_off = prs_offset( &vlist_hbin->ps ) + vlist_hbin->first_hbin_off - HBIN_HDR_SIZE;
1860 if (nk->num_values) {
1861 if ( !(nk->values = TALLOC_ARRAY( file->mem_ctx, REGF_VK_REC, nk->num_values )) )
1867 /* create the vk records */
1869 for ( i=0; i<nk->num_values; i++ ) {
1870 uint32 vk_size, namelen, datalen;
1873 r = regval_ctr_specific_value( values, i );
1874 create_vk_record( file, &nk->values[i], r );
1875 vk_size = vk_record_data_size( &nk->values[i] );
1876 nk->values[i].hbin = find_free_space( file, vk_size );
1877 nk->values[i].hbin_off = prs_offset( &nk->values[i].hbin->ps );
1878 nk->values[i].rec_size = ( vk_size - 1 ) ^ 0xFFFFFFFF;
1879 nk->values[i].rec_off = prs_offset( &nk->values[i].hbin->ps )
1880 + nk->values[i].hbin->first_hbin_off
1883 /* update the max bytes fields if necessary */
1885 namelen = strlen( regval_name(r) );
1886 if ( namelen*2 > nk->max_bytes_valuename )
1887 nk->max_bytes_valuename = namelen * 2;
1889 datalen = regval_size( r );
1890 if ( datalen > nk->max_bytes_value )
1891 nk->max_bytes_value = datalen;
1895 /* stream the records */
1897 prs_set_offset( &nk->hbin->ps, nk->hbin_off );
1898 if ( !prs_nk_rec( "nk_rec", &nk->hbin->ps, 0, nk ) )
1901 if ( nk->num_values ) {
1902 if ( !hbin_prs_vk_records( "vk_records", vlist_hbin, 0, nk, file ) )
1907 regfio_flush( file );