libhfs.h revision 1.2 1 /* $NetBSD: libhfs.h,v 1.2 2007/03/06 11:28:48 dillo Exp $ */
2
3 /*-
4 * Copyright (c) 2005, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Yevgeny Binder and Dieter Baron.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _FS_HFS_LIBHFS_H_
33 #define _FS_HFS_LIBHFS_H_
34
35 #include <sys/endian.h>
36 #include <sys/param.h>
37 #include <sys/mount.h> /* needs to go after sys/param.h or compile fails */
38 #include <sys/types.h>
39 #if defined(_KERNEL)
40 #include <sys/kernel.h>
41 #include <sys/systm.h>
42 #include <machine/stdarg.h>
43 #include <sys/fcntl.h>
44 #endif /* defined(_KERNEL) */
45
46 #if !defined(_KERNEL) && !defined(STANDALONE)
47 #include <fcntl.h>
48 #include <iconv.h>
49 #include <stdarg.h>
50 #include <stdint.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #endif /* !defined(_KERNEL) && !defined(STANDALONE) */
55
56 #ifndef va_list
57 #define va_list _BSD_VA_LIST_
58 #endif /* !va_list */
59
60 #define max(A,B) ((A) > (B) ? (A):(B))
61 #define min(A,B) ((A) < (B) ? (A):(B))
62
63
64 /* Macros to handle errors in this library. Not recommended outside libhfs.c */
65 #define HFS_LIBERR(format, ...) \
66 do{ hfslib_error(format, __FILE__, __LINE__ , ##__VA_ARGS__); \
67 goto error; } while(/*CONSTCOND*/ 0)
68
69 #if 0
70 #pragma mark Constants (on-disk)
71 #endif
72
73
74 enum
75 {
76 HFS_SIG_HFSP = 0x482B, /* 'H+' */
77 HFS_SIG_HFSX = 0x4858, /* 'HX' */
78 HFS_SIG_HFS = 0x4244 /* 'BD' */
79 }; /* volume signatures */
80
81 typedef enum
82 {
83 /* bits 0-6 are reserved */
84 HFS_VOL_HWLOCK = 7,
85 HFS_VOL_UNMOUNTED = 8,
86 HFS_VOL_BADBLOCKS = 9,
87 HFS_VOL_NOCACHE = 10,
88 HFS_VOL_DIRTY = 11,
89 HFS_VOL_CNIDS_RECYCLED = 12,
90 HFS_VOL_JOURNALED = 13,
91 /* bit 14 is reserved */
92 HFS_VOL_SWLOCK = 15
93 /* bits 16-31 are reserved */
94 } hfs_volume_attribute_bit; /* volume header attribute bits */
95
96 typedef enum
97 {
98 HFS_LEAFNODE = -1,
99 HFS_INDEXNODE = 0,
100 HFS_HEADERNODE = 1,
101 HFS_MAPNODE = 2
102 } hfs_node_kind; /* btree node kinds */
103
104 enum
105 {
106 HFS_BAD_CLOSE_MASK = 0x00000001,
107 HFS_BIG_KEYS_MASK = 0x00000002,
108 HFS_VAR_INDEX_KEYS_MASK = 0x00000004
109 }; /* btree header attribute masks */
110
111 typedef enum
112 {
113 HFS_CNID_ROOT_PARENT = 1,
114 HFS_CNID_ROOT_FOLDER = 2,
115 HFS_CNID_EXTENTS = 3,
116 HFS_CNID_CATALOG = 4,
117 HFS_CNID_BADBLOCKS = 5,
118 HFS_CNID_ALLOCATION = 6,
119 HFS_CNID_STARTUP = 7,
120 HFS_CNID_ATTRIBUTES = 8,
121 /* CNIDs 9-13 are reserved */
122 HFS_CNID_REPAIR = 14,
123 HFS_CNID_TEMP = 15,
124 HFS_CNID_USER = 16
125 } hfs_special_cnid; /* special CNID values */
126
127 typedef enum
128 {
129 HFS_REC_FLDR = 0x0001,
130 HFS_REC_FILE = 0x0002,
131 HFS_REC_FLDR_THREAD = 0x0003,
132 HFS_REC_FILE_THREAD = 0x0004
133 } hfs_catalog_rec_kind; /* catalog record types */
134
135 enum
136 {
137 HFS_JOURNAL_ON_DISK_MASK = 0x00000001, /* journal on same volume */
138 HFS_JOURNAL_ON_OTHER_MASK = 0x00000002, /* journal elsewhere */
139 HFS_JOURNAL_NEEDS_INIT_MASK = 0x00000004
140 }; /* journal flag masks */
141
142 enum
143 {
144 HFS_JOURNAL_HEADER_MAGIC = 0x4a4e4c78,
145 HFS_JOURNAL_ENDIAN_MAGIC = 0x12345678
146 }; /* journal magic numbers */
147
148 enum
149 {
150 HFS_DATAFORK = 0x00,
151 HFS_RSRCFORK = 0xFF
152 }; /* common fork types */
153
154 enum
155 {
156 HFS_KEY_CASEFOLD = 0xCF,
157 HFS_KEY_BINARY = 0XBC
158 }; /* catalog key comparison method types */
159
160 enum
161 {
162 HFS_MIN_CAT_KEY_LEN = 6,
163 HFS_MAX_CAT_KEY_LEN = 516,
164 HFS_MAX_EXT_KEY_LEN = 10
165 };
166
167 enum {
168 HFS_HARD_LINK_FILE_TYPE = 0x686C6E6B, /* 'hlnk' */
169 HFS_HFSLUS_CREATOR = 0x6866732B /* 'hfs+' */
170 };
171
172
173 #if 0
174 #pragma mark -
175 #pragma mark Constants (custom)
176 #endif
177
178
179 /* number of bytes between start of volume and volume header */
180 #define HFS_VOLUME_HEAD_RESERVE_SIZE 1024
181
182 typedef enum
183 {
184 HFS_CATALOG_FILE = 1,
185 HFS_EXTENTS_FILE = 2,
186 HFS_ATTRIBUTES_FILE = 3
187 } hfs_btree_file_type; /* btree file kinds */
188
189
190 #if 0
191 #pragma mark -
192 #pragma mark On-Disk Types (Mac OS specific)
193 #endif
194
195 typedef uint32_t hfs_macos_type_code; /* four 1-byte char field */
196
197 typedef struct
198 {
199 int16_t v;
200 int16_t h;
201 } hfs_macos_point_t;
202
203 typedef struct
204 {
205 int16_t t; /* top */
206 int16_t l; /* left */
207 int16_t b; /* bottom */
208 int16_t r; /* right */
209 } hfs_macos_rect_t;
210
211 typedef struct
212 {
213 hfs_macos_type_code file_type;
214 hfs_macos_type_code file_creator;
215 uint16_t finder_flags;
216 hfs_macos_point_t location;
217 uint16_t reserved;
218 } hfs_macos_file_info_t;
219
220 typedef struct
221 {
222 int16_t reserved[4];
223 uint16_t extended_finder_flags;
224 int16_t reserved2;
225 int32_t put_away_folder_cnid;
226 } hfs_macos_extended_file_info_t;
227
228 typedef struct
229 {
230 hfs_macos_rect_t window_bounds;
231 uint16_t finder_flags;
232 hfs_macos_point_t location;
233 uint16_t reserved;
234 } hfs_macos_folder_info_t;
235
236 typedef struct
237 {
238 hfs_macos_point_t scroll_position;
239 int32_t reserved;
240 uint16_t extended_finder_flags;
241 int16_t reserved2;
242 int32_t put_away_folder_cnid;
243 } hfs_macos_extended_folder_info_t;
244
245
246 #if 0
247 #pragma mark -
248 #pragma mark On-Disk Types
249 #endif
250
251 typedef uint16_t unichar_t;
252
253 typedef uint32_t hfs_cnid_t;
254
255
256 typedef struct
257 {
258 uint16_t length;
259 unichar_t unicode[255];
260 } hfs_unistr255_t;
261
262 typedef struct
263 {
264 uint32_t start_block;
265 uint32_t block_count;
266 } hfs_extent_descriptor_t;
267
268 typedef hfs_extent_descriptor_t hfs_extent_record_t[8];
269
270 typedef struct hfs_fork_t
271 {
272 uint64_t logical_size;
273 uint32_t clump_size;
274 uint32_t total_blocks;
275 hfs_extent_record_t extents;
276 } hfs_fork_t;
277
278 typedef struct
279 {
280 uint16_t signature;
281 uint16_t version;
282 uint32_t attributes;
283 uint32_t last_mounting_version;
284 uint32_t journal_info_block;
285
286 uint32_t date_created;
287 uint32_t date_modified;
288 uint32_t date_backedup;
289 uint32_t date_checked;
290
291 uint32_t file_count;
292 uint32_t folder_count;
293
294 uint32_t block_size;
295 uint32_t total_blocks;
296 uint32_t free_blocks;
297
298 uint32_t next_alloc_block;
299 uint32_t rsrc_clump_size;
300 uint32_t data_clump_size;
301 hfs_cnid_t next_cnid;
302
303 uint32_t write_count;
304 uint64_t encodings;
305
306 uint32_t finder_info[8];
307
308 hfs_fork_t allocation_file;
309 hfs_fork_t extents_file;
310 hfs_fork_t catalog_file;
311 hfs_fork_t attributes_file;
312 hfs_fork_t startup_file;
313 } hfs_volume_header_t;
314
315 typedef struct
316 {
317 uint32_t flink;
318 uint32_t blink;
319 int8_t kind;
320 uint8_t height;
321 uint16_t num_recs;
322 uint16_t reserved;
323 } hfs_node_descriptor_t;
324
325 typedef struct
326 {
327 uint16_t tree_depth;
328 uint32_t root_node;
329 uint32_t leaf_recs;
330 uint32_t first_leaf;
331 uint32_t last_leaf;
332 uint16_t node_size;
333 uint16_t max_key_len;
334 uint32_t total_nodes;
335 uint32_t free_nodes;
336 uint16_t reserved;
337 uint32_t clump_size; /* misaligned */
338 uint8_t btree_type;
339 uint8_t keycomp_type;
340 uint32_t attributes; /* long aligned again */
341 uint32_t reserved2[16];
342 } hfs_header_record_t;
343
344 typedef struct
345 {
346 uint16_t key_len;
347 hfs_cnid_t parent_cnid;
348 hfs_unistr255_t name;
349 } hfs_catalog_key_t;
350
351 typedef struct
352 {
353 uint16_t key_length;
354 uint8_t fork_type;
355 uint8_t padding;
356 hfs_cnid_t file_cnid;
357 uint32_t start_block;
358 } hfs_extent_key_t;
359
360 typedef struct
361 {
362 uint32_t owner_id;
363 uint32_t group_id;
364 uint8_t admin_flags;
365 uint8_t owner_flags;
366 uint16_t file_mode;
367 union
368 {
369 uint32_t inode_num;
370 uint32_t link_count;
371 uint32_t raw_device;
372 } special;
373 } hfs_bsd_data_t;
374
375 typedef struct
376 {
377 int16_t rec_type;
378 uint16_t flags;
379 uint32_t valence;
380 hfs_cnid_t cnid;
381 uint32_t date_created;
382 uint32_t date_content_mod;
383 uint32_t date_attrib_mod;
384 uint32_t date_accessed;
385 uint32_t date_backedup;
386 hfs_bsd_data_t bsd;
387 hfs_macos_folder_info_t user_info;
388 hfs_macos_extended_folder_info_t finder_info;
389 uint32_t text_encoding;
390 uint32_t reserved;
391 } hfs_folder_record_t;
392
393 typedef struct
394 {
395 int16_t rec_type;
396 uint16_t flags;
397 uint32_t reserved;
398 hfs_cnid_t cnid;
399 uint32_t date_created;
400 uint32_t date_content_mod;
401 uint32_t date_attrib_mod;
402 uint32_t date_accessed;
403 uint32_t date_backedup;
404 hfs_bsd_data_t bsd;
405 hfs_macos_file_info_t user_info;
406 hfs_macos_extended_file_info_t finder_info;
407 uint32_t text_encoding;
408 uint32_t reserved2;
409 hfs_fork_t data_fork;
410 hfs_fork_t rsrc_fork;
411 } hfs_file_record_t;
412
413 typedef struct
414 {
415 int16_t rec_type;
416 int16_t reserved;
417 hfs_cnid_t parent_cnid;
418 hfs_unistr255_t name;
419 } hfs_thread_record_t;
420
421 typedef struct
422 {
423 uint32_t flags;
424 uint32_t device_signature[8];
425 uint64_t offset;
426 uint64_t size;
427 uint64_t reserved[32];
428 } hfs_journal_info_t;
429
430 typedef struct
431 {
432 uint32_t magic;
433 uint32_t endian;
434 uint64_t start;
435 uint64_t end;
436 uint64_t size;
437 uint32_t blocklist_header_size;
438 uint32_t checksum;
439 uint32_t journal_header_size;
440 } hfs_journal_header_t;
441
442 #if 0
443 #pragma mark -
444 #pragma mark Custom Types
445 #endif
446
447 typedef struct
448 {
449 hfs_volume_header_t vh; /* volume header */
450 hfs_header_record_t chr; /* catalog file header node record*/
451 hfs_header_record_t ehr; /* extent overflow file header node record*/
452 uint8_t catkeysizefieldsize; /* size of catalog file key_len field in
453 * bytes (1 or 2); always 2 for HFS+ */
454 uint8_t extkeysizefieldsize; /* size of extent file key_len field in
455 * bytes (1 or 2); always 2 for HFS+ */
456 hfs_unistr255_t name; /* volume name */
457
458 /* pointer to catalog file key comparison function */
459 int (*keycmp) (const void*, const void*);
460
461 int journaled; /* 1 if volume is journaled, else 0 */
462 hfs_journal_info_t jib; /* journal info block */
463 hfs_journal_header_t jh; /* journal header */
464
465 int readonly; /* 0 if mounted r/w, 1 if mounted r/o */
466 void* cbdata; /* application-specific data; allocated, defined and
467 * used (if desired) by the program, usually within
468 * callback routines */
469 } hfs_volume;
470
471 typedef union
472 {
473 /* for leaf nodes */
474 int16_t type; /* type of record: folder, file, or thread */
475 hfs_folder_record_t folder;
476 hfs_file_record_t file;
477 hfs_thread_record_t thread;
478
479 /* for pointer nodes */
480 /* (using this large union for just one tiny field is not memory-efficient,
481 * so change this if it becomes problematic) */
482 uint32_t child; /* node number of this node's child node */
483 } hfs_catalog_keyed_record_t;
484
485 /*
486 * These arguments are passed among libhfs without any inspection. This struct
487 * is accepted by all public functions of libhfs, and passed to each callback.
488 * An application dereferences each pointer to its own specific struct of
489 * arguments. Callbacks must be prepared to deal with NULL values for any of
490 * these fields (by providing default values to be used in lieu of that
491 * argument). However, a NULL pointer to this struct is an error.
492 *
493 * It was decided to make one unified argument structure, rather than many
494 * separate, operand-specific structures, because, when this structure is passed
495 * to a public function (e.g., hfslib_open_volume()), the function may make
496 * several calls (and subcalls) to various facilities, e.g., read(), malloc(),
497 * and free(), all of which require their own particular arguments. The
498 * facilities to be used are quite impractical to foreshadow, so the application
499 * takes care of all possible calls at once. This also reinforces the idea that
500 * a public call is an umbrella to a set of system calls, and all of these calls
501 * must be passed arguments which do not change within the context of this
502 * umbrella. (E.g., if a public function makes two calls to read(), one call
503 * should not be passed a uid of root and the other passed a uid of daemon.)
504 */
505 typedef struct
506 {
507 /* The 'error' function does not take an argument. All others do. */
508
509 void* allocmem;
510 void* reallocmem;
511 void* freemem;
512 void* openvol;
513 void* closevol;
514 void* read;
515 } hfs_callback_args;
516
517 typedef struct
518 {
519 /* error(in_format, in_file, in_line, in_args) */
520 void (*error) (const char*, const char*, int, va_list);
521
522 /* allocmem(in_size, cbargs) */
523 void* (*allocmem) (size_t, hfs_callback_args*);
524
525 /* reallocmem(in_ptr, in_size, cbargs) */
526 void* (*reallocmem) (void*, size_t, hfs_callback_args*);
527
528 /* freemem(in_ptr, cbargs) */
529 void (*freemem) (void*, hfs_callback_args*);
530
531 /* openvol(in_volume, in_devicepath, in_offset, cbargs)
532 * returns 0 on success */
533 int (*openvol) (hfs_volume*, const char*, uint64_t, hfs_callback_args*);
534
535 /* closevol(in_volume, cbargs) */
536 void (*closevol) (hfs_volume*, hfs_callback_args*);
537
538 /* read(in_volume, out_buffer, in_length, in_offset, cbargs)
539 * returns 0 on success */
540 int (*read) (hfs_volume*, void*, uint64_t, uint64_t,
541 hfs_callback_args*);
542
543 } hfs_callbacks;
544
545 hfs_callbacks hfs_gcb; /* global callbacks */
546
547 /*
548 * global case folding table
549 * (lazily initialized; see comments at bottom of hfs_open_volume())
550 */
551 unichar_t* hfs_gcft;
552
553 #if 0
554 #pragma mark -
555 #pragma mark Functions
556 #endif
557
558 void hfslib_init(hfs_callbacks*);
559 void hfslib_done(void);
560 void hfslib_init_cbargs(hfs_callback_args*);
561
562 int hfslib_open_volume(const char*, uint64_t, int, hfs_volume*,
563 hfs_callback_args*);
564 void hfslib_close_volume(hfs_volume*, hfs_callback_args*);
565
566 int hfslib_path_to_cnid(hfs_volume*, hfs_cnid_t, char**, uint16_t*,
567 hfs_callback_args*);
568 hfs_cnid_t hfslib_find_parent_thread(hfs_volume*, hfs_cnid_t,
569 hfs_thread_record_t*, hfs_callback_args*);
570 int hfslib_find_catalog_record_with_cnid(hfs_volume*, hfs_cnid_t,
571 hfs_catalog_keyed_record_t*, hfs_catalog_key_t*, hfs_callback_args*);
572 int hfslib_find_catalog_record_with_key(hfs_volume*, hfs_catalog_key_t*,
573 hfs_catalog_keyed_record_t*, hfs_callback_args*);
574 int hfslib_find_extent_record_with_key(hfs_volume*, hfs_extent_key_t*,
575 hfs_extent_record_t*, hfs_callback_args*);
576 int hfslib_get_directory_contents(hfs_volume*, hfs_cnid_t,
577 hfs_catalog_keyed_record_t**, hfs_unistr255_t**, uint32_t*,
578 hfs_callback_args*);
579 int hfslib_is_journal_clean(hfs_volume*);
580 int hfslib_is_private_file(hfs_catalog_key_t*);
581
582 int hfslib_get_hardlink(hfs_volume *, uint32_t,
583 hfs_catalog_keyed_record_t *, hfs_callback_args *);
584
585 size_t hfslib_read_volume_header(void*, hfs_volume_header_t*);
586 size_t hfslib_reada_node(void*, hfs_node_descriptor_t*, void***, uint16_t**,
587 hfs_btree_file_type, hfs_volume*, hfs_callback_args*);
588 size_t hfslib_reada_node_offsets(void*, uint16_t*);
589 size_t hfslib_read_header_node(void**, uint16_t*, uint16_t,
590 hfs_header_record_t*, void*, void*);
591 size_t hfslib_read_catalog_keyed_record(void*, hfs_catalog_keyed_record_t*,
592 int16_t*, hfs_catalog_key_t*, hfs_volume*);
593 size_t hfslib_read_extent_record(void*, hfs_extent_record_t*, hfs_node_kind,
594 hfs_extent_key_t*, hfs_volume*);
595 void hfslib_free_recs(void***, uint16_t**, uint16_t*, hfs_callback_args*);
596
597 size_t hfslib_read_fork_descriptor(void*, hfs_fork_t*);
598 size_t hfslib_read_extent_descriptors(void*, hfs_extent_record_t*);
599 size_t hfslib_read_unistr255(void*, hfs_unistr255_t*);
600 size_t hfslib_read_bsd_data(void*, hfs_bsd_data_t*);
601 size_t hfslib_read_file_userinfo(void*, hfs_macos_file_info_t*);
602 size_t hfslib_read_file_finderinfo(void*, hfs_macos_extended_file_info_t*);
603 size_t hfslib_read_folder_userinfo(void*, hfs_macos_folder_info_t*);
604 size_t hfslib_read_folder_finderinfo(void*, hfs_macos_extended_folder_info_t*);
605 size_t hfslib_read_journal_info(void*, hfs_journal_info_t*);
606 size_t hfslib_read_journal_header(void*, hfs_journal_header_t*);
607
608 uint16_t hfslib_make_catalog_key(hfs_cnid_t, uint16_t, unichar_t*,
609 hfs_catalog_key_t*);
610 uint16_t hfslib_make_extent_key(hfs_cnid_t, uint8_t, uint32_t,
611 hfs_extent_key_t*);
612 uint16_t hfslib_get_file_extents(hfs_volume*, hfs_cnid_t, uint8_t,
613 hfs_extent_descriptor_t**, hfs_callback_args*);
614 int hfslib_readd_with_extents(hfs_volume*, void*, uint64_t*, uint64_t,
615 uint64_t, hfs_extent_descriptor_t*, uint16_t, hfs_callback_args*);
616
617 int hfslib_compare_catalog_keys_cf(const void*, const void*);
618 int hfslib_compare_catalog_keys_bc(const void*, const void*);
619 int hfslib_compare_extent_keys(const void*, const void*);
620
621
622 /* callback wrappers */
623 void hfslib_error(const char*, const char*, int, ...) __attribute__ ((format (printf, 1, 4)));
624 void* hfslib_malloc(size_t, hfs_callback_args*);
625 void* hfslib_realloc(void*, size_t, hfs_callback_args*);
626 void hfslib_free(void*, hfs_callback_args*);
627 int hfslib_openvoldevice(hfs_volume*, const char*, uint64_t,hfs_callback_args*);
628 void hfslib_closevoldevice(hfs_volume*, hfs_callback_args*);
629 int hfslib_readd(hfs_volume*, void*, uint64_t, uint64_t, hfs_callback_args*);
630
631 #endif /* !_FS_HFS_LIBHFS_H_ */
632