Add an internal hivex header file.
[hivex.git] / lib / hivex-internal.h
1 /* hivex internal header
2  * Copyright (C) 2009-2011 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation;
7  * version 2.1 of the License only.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #ifndef HIVEX_INTERNAL_H_
20 #define HIVEX_INTERNAL_H_
21
22 #include <stddef.h>
23
24 struct hive_h {
25   char *filename;
26   int fd;
27   size_t size;
28   int msglvl;
29   int writable;
30
31   /* Registry file, memory mapped if read-only, or malloc'd if writing. */
32   union {
33     char *addr;
34     struct ntreg_header *hdr;
35   };
36
37   /* Use a bitmap to store which file offsets are valid (point to a
38    * used block).  We only need to store 1 bit per 32 bits of the file
39    * (because blocks are 4-byte aligned).  We found that the average
40    * block size in a registry file is ~50 bytes.  So roughly 1 in 12
41    * bits in the bitmap will be set, making it likely a more efficient
42    * structure than a hash table.
43    */
44   char *bitmap;
45 #define BITMAP_SET(bitmap,off) (bitmap[(off)>>5] |= 1 << (((off)>>2)&7))
46 #define BITMAP_CLR(bitmap,off) (bitmap[(off)>>5] &= ~ (1 << (((off)>>2)&7)))
47 #define BITMAP_TST(bitmap,off) (bitmap[(off)>>5] & (1 << (((off)>>2)&7)))
48 #define IS_VALID_BLOCK(h,off)               \
49   (((off) & 3) == 0 &&                      \
50    (off) >= 0x1000 &&                       \
51    (off) < (h)->size &&                     \
52    BITMAP_TST((h)->bitmap,(off)))
53
54   /* Fields from the header, extracted from little-endianness hell. */
55   size_t rootoffs;              /* Root key offset (always an nk-block). */
56   size_t endpages;              /* Offset of end of pages. */
57   int64_t last_modified;        /* mtime of base block. */
58
59   /* For writing. */
60   size_t endblocks;             /* Offset to next block allocation (0
61                                    if not allocated anything yet). */
62 };
63
64 #define STREQ(a,b) (strcmp((a),(b)) == 0)
65 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
66 #define STRNEQ(a,b) (strcmp((a),(b)) != 0)
67 #define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
68 #define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
69 #define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
70 #define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
71 #define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
72 #define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
73
74 #endif /* HIVEX_INTERNAL_H_ */