1 /*
2  * libhfs - library for reading and writing Macintosh HFS volumes
3  * Copyright (C) 2000 Klaus Halfmann (khalfmann@libra.de)
4  * Original work by 1996-1998 Robert Leslie (rob@mars.org)
5  *
6  * This file defines constants,structs etc needed for this library.
7  * Everything found here is usually not related to Apple defintions.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22  * MA 02110-1301, USA.
23  *
24  * $Id: libhfsp.h,v 1.17 2000/10/20 06:16:52 hasi Exp $
25  */
26 
27 # include "apple.h"
28 # include "hfs.h"
29 # include "hfsp.h"
30 
31 /* Last error is eventually found here */
32 extern const char *hfsp_error;
33 
34 # define HFSP_ERROR(code, str)  \
35     do { hfsp_error = (str), errno = (code); goto fail; } while (0)
36 
37 # ifdef DEBUG
38 #  define ASSERT(cond)	do { if (! (cond)) abort(); } while (0)
39 # else
40 #  define ASSERT(cond)	/* nothing */
41 # endif
42 
43 # define SIZE(type, n)		((size_t) (sizeof(type) * (n)))
44 # define ALLOC(type, n)		((type *) malloc(SIZE(type, n)))
45 # define ALLOCX(type, n)	((n) ? ALLOC(type, n) : (type *) 0)
46 # define FREE(ptr)		((ptr) ? (void) free((void *) ptr) : (void) 0)
47 
48 # define REALLOC(ptr, type, n)  \
49     ((type *) ((ptr) ? realloc(ptr, SIZE(type, n)) : malloc(SIZE(type, n))))
50 # define REALLOCX(ptr, type, n)  \
51     ((n) ? REALLOC(ptr, type, n) : (FREE(ptr), (type *) 0))
52 
53 # define BMTST(bm, num)  \
54     (((const byte *) (bm))[(num) >> 3]  &  (0x80 >> ((num) & 0x07)))
55 # define BMSET(bm, num)  \
56           (((byte *) (bm))[(num) >> 3] |=  (0x80 >> ((num) & 0x07)))
57 # define BMCLR(bm, num)  \
58           (((byte *) (bm))[(num) >> 3] &= ~(0x80 >> ((num) & 0x07)))
59 
60 # define STRINGIZE(x)		#x
61 # define STR(x)			STRINGIZE(x)
62 
63 /* used by internal routines to specify the open modes */
64 # define HFSP_MODE_RDONLY        0
65 # define HFSP_MODE_RDWR          1
66 # define HFSP_MODE_ANY           2
67 
68 /* Signatures registered with Apple to identify this driver */
69     /* Identifies the userland implementation */
70 # define HPLS_SIGNATURE 0x482B4C58	// 'H+LX'
71     /* Identifies the kernel module by Brad Boyer (flar@pants.nu) */
72 # define HPLS_SIGRES1	0x482B4C78	// 'H+Lx'
73     /* not jet in use ... */
74 # define HPLS_SIGRES2	0x482B6C78	// 'H+lx'
75     /* Signature used by Apple */
76 # define HPAPPLE_SIGNATURE  0x382e3130	// '8.10'
77 
78 /* Version used for this implementation of HFS+. This is not related
79  * to the VERSION file found at the top-level of this package,
80  * but designates the version of the low level code */
81 #define HPLS_VERSION	1   /* must fit in a short */
82 
83 
84 /* Othe Signatures may follow for informational purpos */
85 
86 /* prototype for key comparing functions. */
87 typedef int (*hfsp_key_compare) (void* key1, void* key2);
88 
89 /* prototype for key reading (necessary for byte swapping) */
90 typedef void* (*hfsp_key_read) (void* p, void* key);
91 
92 struct volume; /* foreward declaration for btree needed */
93 
94 /* Structures for a node cache. The cache is an array
95  * with linear search. (So making it to big may make
96  * things slower). It is searched in a round robin
97  * fashion.
98  */
99 
100 typedef struct
101 {
102     UInt32		priority;
103 	// as lower this number as higher the priority.
104 	// decremetned on any sucessfull usage
105 	// incremented else, intial value height*DEPTHFACTOR
106     UInt16		index;	// of node in fork
107 	// 0 means empty, since first node is node header
108 	// contents of node in original byte order
109     UInt16		flags;	// like DIRTY etc.
110 } node_entry;
111 
112 typedef struct
113 {
114     UInt32		index;	    // duplicate of above
115     btree_node_desc	desc;	    // header of node
116     char		node[0];    // actual node_size
117 	// contents of node in original byte order
118 } node_buf;
119 
120 typedef struct
121 {
122     int		size;	     // number of nodes in the cache
123     int		currindex;   // round robin index
124     int		nodebufsize; // size of complete node_buf, including node
125     node_entry	*entries;
126     char	*buffers;   // actually *node_buf
127 } node_cache;
128 
129 typedef struct
130 {
131     struct volume*	vol;	/* pointer to volume this tree is part of */
132     hfsp_fork_raw*	fork;	/* pointer to fork this tree is part of */
133     UInt32		cnid;	/* (pseudo) file id for the fork */
134     hfsp_key_compare	kcomp;
135 	/* function used for key compare in _this_ btree */
136     hfsp_key_read	kread;
137 	/* fucntion used to read a key int _this_ btree */
138     btree_head		head;
139 
140     UInt16		blkpernode;
141 	 /* Number of volume blocks per node (usually 1-4) */
142     node_cache		cache;
143     /* Warning all functions of btrees and records may modify
144        the following values ! */
145     // UInt16		node_index; /* index of node in fork */
146     // btree_node_desc	node;	/* current node under examination */
147     // char*		buf;	/* buf with size of a node */
148 } btree;
149 
150 /* Function on btrees are defined in btree.h */
151 
152 /* A Wrapper around the raw hfs+ volume header for additional information
153  * needed by this library.
154  */
155 
156 typedef struct volume
157 {
158     int		os_fd;		/* OS dependend reference to device */
159     UInt16	blksize_bits;   /* blocksize of device = 1 << blksize_bits */
160     UInt16	filler;
161     UInt32	blksize;	/* always 1 << blksize_bits */
162     UInt32	startblock;
163 	/* Offset from physical to logical blocks,
164 	   eventually intodruced by HFS wrapper */
165     UInt32  	maxblocks;	/* maximum number of blocks in device */
166     // UInt32	currblock;	/* value of current block, to cache blocks */
167     hfsp_vh	vol;		/* raw volume data */
168     // void*	blockbuf;	/* (single) buffer for fetching one block */
169      /* Buffer has double size of blksize to allow cross block reading */
170 
171     btree*	extents;	/* is NULL by default and intialized when needed */
172     btree	catalog;	/* This is always neeeded */
173 } volume;
174 
175 /* Functions on volumes are defined in volume.h */
176 
177 typedef struct {    // may not be used as found here
178     btree*		tree;	// tree where this record is contained in.
179     UInt16		node_index; /* index of record in btree */
180     UInt16		keyind;	/* index of current key in btree */
181     hfsp_cat_key	key;	/* current key */
182     UInt32		child;	/* child node belonging to this key */
183 } index_record;
184 
185 typedef struct {
186     btree*		tree;	// tree where this record is contained in.
187     UInt16		node_index; /* index of record in btree */
188     UInt16		keyind;	/* index of current key in btree */
189     hfsp_extent_key	key;	/* current key */
190     hfsp_extent_rec	extent; /* The payload carried around */
191 } extent_record;
192 
193 typedef struct {
194     btree*		tree;	// tree where this record is contained in.
195     UInt16		node_index; /* index of record in btree */
196     UInt16		keyind;	/* index of current key in btree */
197     hfsp_cat_key	key;	/* current key */
198     hfsp_cat_entry	record;	/* current record */
199 } record;
200 
201 /* Functions on records are defined in record.h */
202