1 /* $NetBSD: libhfs.c,v 1.8 2009/07/14 21:12:18 apb Exp $ */
4 * Copyright (c) 2005, 2007 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Yevgeny Binder, Dieter Baron, and Pelle Johansson.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
33 * All functions and variable types have the prefix "hfs_". All constants
34 * have the prefix "HFS_".
36 * Naming convention for functions which read/write raw, linear data
37 * into/from a structured form:
39 * hfs_read/write[d][a]_foo_bar
40 * [d] - read/write from/to [d]isk instead of a memory buffer
41 * [a] - [a]llocate output buffer instead of using an existing one
42 * (not applicable for writing functions)
44 * Most functions do not have either of these options, so they will read from
45 * or write to a memory buffer, which has been previously allocated by the
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: libhfs.c,v 1.8 2009/07/14 21:12:18 apb Exp $");
54 /* global private file/folder keys */
55 hfs_catalog_key_t hfs_gMetadataDirectoryKey
; /* contains HFS+ inodes */
56 hfs_catalog_key_t hfs_gJournalInfoBlockFileKey
;
57 hfs_catalog_key_t hfs_gJournalBufferFileKey
;
58 hfs_catalog_key_t
* hfs_gPrivateObjectKeys
[4] = {
59 &hfs_gMetadataDirectoryKey
,
60 &hfs_gJournalInfoBlockFileKey
,
61 &hfs_gJournalBufferFileKey
,
65 extern uint16_t be16tohp(void** inout_ptr
);
66 extern uint32_t be32tohp(void** inout_ptr
);
67 extern uint64_t be64tohp(void** inout_ptr
);
69 int hfslib_create_casefolding_table(void);
74 dlo_print_key(hfs_catalog_key_t
*key
)
78 printf("%ld:[", (long)key
->parent_cnid
);
79 for (i
=0; i
<key
->name
.length
; i
++) {
80 if (key
->name
.unicode
[i
] < 256
81 && isprint(key
->name
.unicode
[i
]))
82 putchar(key
->name
.unicode
[i
]);
84 printf("<%04x>", key
->name
.unicode
[i
]);
91 hfslib_init(hfs_callbacks
* in_callbacks
)
95 if(in_callbacks
!=NULL
)
96 memcpy(&hfs_gcb
, in_callbacks
, sizeof(hfs_callbacks
));
101 * Create keys for the HFS+ "private" files so we can reuse them whenever
102 * we perform a user-visible operation, such as listing directory contents.
105 #define ATOU(str, len) /* quick & dirty ascii-to-unicode conversion */ \
106 do{ int i; for(i=0; i<len; i++) temp[i]=str[i]; } \
107 while( /*CONSTCOND*/ 0)
109 ATOU("\0\0\0\0HFS+ Private Data", 21);
110 hfslib_make_catalog_key(HFS_CNID_ROOT_FOLDER
, 21, temp
,
111 &hfs_gMetadataDirectoryKey
);
113 ATOU(".journal_info_block", 19);
114 hfslib_make_catalog_key(HFS_CNID_ROOT_FOLDER
, 19, temp
,
115 &hfs_gJournalInfoBlockFileKey
);
118 hfslib_make_catalog_key(HFS_CNID_ROOT_FOLDER
, 8, temp
,
119 &hfs_gJournalBufferFileKey
);
127 hfs_callback_args cbargs
;
130 hfslib_init_cbargs(&cbargs
);
131 hfslib_free(hfs_gcft
, &cbargs
);
139 hfslib_init_cbargs(hfs_callback_args
* ptr
)
141 memset(ptr
, 0, sizeof(hfs_callback_args
));
146 #pragma mark High-Level Routines
151 const char* in_device
,
154 hfs_callback_args
* cbargs
)
156 hfs_catalog_key_t rootkey
;
157 hfs_thread_record_t rootthread
;
158 hfs_hfs_master_directory_block_t mdb
;
159 uint16_t node_rec_sizes
[1];
162 void* buffer2
; /* used as temporary pointer for realloc() */
169 if(in_device
==NULL
|| out_vol
==NULL
)
172 out_vol
->readonly
= in_readonly
;
175 if(hfslib_openvoldevice(out_vol
, in_device
, cbargs
) != 0)
176 HFS_LIBERR("could not open device");
180 * Read the volume header.
182 buffer
= hfslib_malloc(max(sizeof(hfs_volume_header_t
),
183 sizeof(hfs_hfs_master_directory_block_t
)), cbargs
);
185 HFS_LIBERR("could not allocate volume header");
186 if(hfslib_readd(out_vol
, buffer
, max(sizeof(hfs_volume_header_t
),
187 sizeof(hfs_hfs_master_directory_block_t
)),
188 HFS_VOLUME_HEAD_RESERVE_SIZE
, cbargs
)!=0)
189 HFS_LIBERR("could not read volume header");
191 if (be16toh(*((uint16_t *)buffer
)) == HFS_SIG_HFS
) {
192 if (hfslib_read_master_directory_block(buffer
, &mdb
) == 0)
193 HFS_LIBERR("could not parse master directory block");
194 if (mdb
.embedded_signature
== HFS_SIG_HFSP
)
196 /* XXX: is 512 always correct? */
198 mdb
.first_block
* 512
199 + mdb
.embedded_extent
.start_block
200 * (uint64_t)mdb
.block_size
;
202 if(hfslib_readd(out_vol
, buffer
,
203 sizeof(hfs_volume_header_t
),
204 HFS_VOLUME_HEAD_RESERVE_SIZE
, cbargs
)!=0)
205 HFS_LIBERR("could not read volume header");
208 HFS_LIBERR("Plain HFS volumes not currently supported");
211 if(hfslib_read_volume_header(buffer
, &(out_vol
->vh
))==0)
212 HFS_LIBERR("could not parse volume header");
215 * Check the volume signature to see if this is a legitimate HFS+ or HFSX
216 * volume. If so, set the key comparison function pointers appropriately.
218 switch(out_vol
->vh
.signature
)
221 out_vol
->keycmp
= hfslib_compare_catalog_keys_cf
;
225 out_vol
->keycmp
= NULL
; /* will be set below */
229 /* HFS_LIBERR("unrecognized volume format"); */
236 * Read the catalog header.
238 buffer2
= hfslib_realloc(buffer
, 512, cbargs
);
240 HFS_LIBERR("could not allocate catalog header node");
244 We are only interested in the node header, so read the first
245 512 bytes and construct the node descriptor by hand.
247 if(hfslib_readd(out_vol
, buffer
, 512,
248 out_vol
->vh
.catalog_file
.extents
[0].start_block
249 *(uint64_t)out_vol
->vh
.block_size
,
251 HFS_LIBERR("could not read catalog header node");
252 node_recs
[0] = (char *)buffer
+14;
253 node_rec_sizes
[0] = 120;
254 if(hfslib_read_header_node(node_recs
, node_rec_sizes
, 1,
255 &out_vol
->chr
, NULL
, NULL
)==0)
256 HFS_LIBERR("could not parse catalog header node");
258 /* If this is an HFSX volume, the catalog header specifies the type of
259 * key comparison method (case-folding or binary compare) we should use. */
260 if(out_vol
->keycmp
== NULL
)
262 if(out_vol
->chr
.keycomp_type
== HFS_KEY_CASEFOLD
)
263 out_vol
->keycmp
= hfslib_compare_catalog_keys_cf
;
264 else if(out_vol
->chr
.keycomp_type
== HFS_KEY_BINARY
)
265 out_vol
->keycmp
= hfslib_compare_catalog_keys_bc
;
267 HFS_LIBERR("undefined key compare method");
270 out_vol
->catkeysizefieldsize
271 = (out_vol
->chr
.attributes
& HFS_BIG_KEYS_MASK
) ?
272 sizeof(uint16_t) : sizeof(uint8_t);
275 * Read the extent overflow header.
278 We are only interested in the node header, so read the first
279 512 bytes and construct the node descriptor by hand.
280 buffer is already 512 bytes long.
282 if(hfslib_readd(out_vol
, buffer
, 512,
283 out_vol
->vh
.extents_file
.extents
[0].start_block
284 *(uint64_t)out_vol
->vh
.block_size
,
286 HFS_LIBERR("could not read extent header node");
288 node_recs
[0] = (char *)buffer
+14;
289 node_rec_sizes
[0] = 120;
290 if(hfslib_read_header_node(node_recs
, node_rec_sizes
, 1,
291 &out_vol
->ehr
, NULL
, NULL
)==0)
292 HFS_LIBERR("could not parse extent header node");
293 out_vol
->extkeysizefieldsize
294 = (out_vol
->ehr
.attributes
& HFS_BIG_KEYS_MASK
) ?
295 sizeof(uint16_t):sizeof(uint8_t);
297 * Read the journal info block and journal header (if volume journaled).
299 if(out_vol
->vh
.attributes
& (1<<HFS_VOL_JOURNALED
))
301 /* journal info block */
302 buffer2
= hfslib_realloc(buffer
, sizeof(hfs_journal_info_t
), cbargs
);
304 HFS_LIBERR("could not allocate journal info block");
307 if(hfslib_readd(out_vol
, buffer
, sizeof(hfs_journal_info_t
),
308 out_vol
->vh
.journal_info_block
* out_vol
->vh
.block_size
,
310 HFS_LIBERR("could not read journal info block");
312 if(hfslib_read_journal_info(buffer
, &out_vol
->jib
)==0)
313 HFS_LIBERR("could not parse journal info block");
316 buffer2
= hfslib_realloc(buffer
, sizeof(hfs_journal_header_t
),cbargs
);
318 HFS_LIBERR("could not allocate journal header");
321 if(hfslib_readd(out_vol
, buffer
, sizeof(hfs_journal_header_t
),
322 out_vol
->jib
.offset
, cbargs
) != 0)
323 HFS_LIBERR("could not read journal header");
325 if(hfslib_read_journal_header(buffer
, &out_vol
->jh
)==0)
326 HFS_LIBERR("could not parse journal header");
328 out_vol
->journaled
= 1;
332 out_vol
->journaled
= 0;
336 * If this volume uses case-folding comparison and the folding table hasn't
337 * been created yet, do that here. (We don't do this in hfslib_init()
338 * because the table is large and we might never even need to use it.)
340 if(out_vol
->keycmp
==hfslib_compare_catalog_keys_cf
&& hfs_gcft
==NULL
)
341 result
= hfslib_create_casefolding_table();
346 * Find and store the volume name.
348 if(hfslib_make_catalog_key(HFS_CNID_ROOT_FOLDER
, 0, NULL
, &rootkey
)==0)
349 HFS_LIBERR("could not make root search key");
351 if(hfslib_find_catalog_record_with_key(out_vol
, &rootkey
,
352 (hfs_catalog_keyed_record_t
*)&rootthread
, cbargs
)!=0)
353 HFS_LIBERR("could not find root parent");
355 memcpy(&out_vol
->name
, &rootthread
.name
, sizeof(hfs_unistr255_t
));
360 if (result
!= 0 && isopen
)
361 hfslib_close_volume(out_vol
, cbargs
);
363 hfslib_free(buffer
, cbargs
);
369 hfslib_close_volume(hfs_volume
* in_vol
, hfs_callback_args
* cbargs
)
374 hfslib_closevoldevice(in_vol
, cbargs
);
378 hfslib_path_to_cnid(hfs_volume
* in_vol
,
381 uint16_t* out_length
,
382 hfs_callback_args
* cbargs
)
384 hfs_thread_record_t parent_thread
;
385 hfs_cnid_t parent_cnid
, child_cnid
;
390 uint16_t* ptr
; /* dummy var */
391 uint16_t uchar
; /* dummy var */
392 uint16_t total_path_length
;
394 if(in_vol
==NULL
|| in_cnid
==0 || out_unicode
==NULL
|| out_length
==NULL
)
401 total_path_length
= 0;
403 path
= hfslib_malloc(514, cbargs
); /* 256 unichars plus a forward slash */
407 child_cnid
= in_cnid
;
408 parent_cnid
= child_cnid
; /* skips loop in case in_cnid is root id */
409 while(parent_cnid
!= HFS_CNID_ROOT_FOLDER
410 && parent_cnid
!= HFS_CNID_ROOT_PARENT
)
412 if(child_cnid
!=in_cnid
)
414 newpath
= hfslib_realloc(path
, 514 + total_path_length
*2, cbargs
);
420 memmove(path
+ 514, path
+ path_offset
, total_path_length
*2);
423 parent_cnid
= hfslib_find_parent_thread(in_vol
, child_cnid
,
424 &parent_thread
, cbargs
);
428 path_offset
= 512 - parent_thread
.name
.length
*2;
430 memcpy(path
+ path_offset
, parent_thread
.name
.unicode
,
431 parent_thread
.name
.length
*2);
433 /* Add a forward slash. The unicode string was specified in big endian
434 * format, so convert to core format if necessary. */
438 ptr
= (uint16_t*)path
+ 256;
439 uchar
= be16tohp((void*)&ptr
);
442 total_path_length
+= parent_thread
.name
.length
+ 1;
444 child_cnid
= parent_cnid
;
448 * At this point, 'path' holds a sequence of unicode characters which
449 * represent the absolute path to the given cnid. This string is missing
450 * a terminating null char and an initial forward slash that represents
451 * the root of the filesystem. It most likely also has extra space in
452 * the beginning, due to the fact that we reserve 512 bytes for each path
453 * component and won't usually use all that space. So, we allocate the
454 * final string based on the actual length of the absolute path, plus four
455 * additional bytes (two unichars) for the forward slash and the null char.
458 *out_unicode
= hfslib_malloc((total_path_length
+2)*2, cbargs
);
459 if(*out_unicode
== NULL
)
462 /* copy only the bytes that are actually used */
463 memcpy(*out_unicode
+2, path
+ path_offset
, total_path_length
*2);
465 /* insert forward slash at start */
466 (*out_unicode
)[0] = 0x00;
467 (*out_unicode
)[1] = 0x2F;
468 ptr
= (uint16_t*)*out_unicode
;
469 uchar
= be16tohp((void*)&ptr
);
472 /* insert null char at end */
473 (*out_unicode
)[total_path_length
*2+2] = 0x00;
474 (*out_unicode
)[total_path_length
*2+3] = 0x00;
476 *out_length
= total_path_length
+ 1 /* extra for forward slash */ ;
482 hfslib_free(path
, cbargs
);
488 hfslib_find_parent_thread(
491 hfs_thread_record_t
* out_thread
,
492 hfs_callback_args
* cbargs
)
494 hfs_catalog_key_t childkey
;
496 if(in_vol
==NULL
|| in_child
==0 || out_thread
==NULL
)
499 if(hfslib_make_catalog_key(in_child
, 0, NULL
, &childkey
)==0)
502 if(hfslib_find_catalog_record_with_key(in_vol
, &childkey
,
503 (hfs_catalog_keyed_record_t
*)out_thread
, cbargs
)!=0)
506 return out_thread
->parent_cnid
;
510 * hfslib_find_catalog_record_with_cnid()
512 * Looks up a catalog record by calling hfslib_find_parent_thread() and
513 * hfslib_find_catalog_record_with_key(). out_key may be NULL; if not, the key
514 * corresponding to this cnid is stuffed in it. Returns 0 on success.
517 hfslib_find_catalog_record_with_cnid(
520 hfs_catalog_keyed_record_t
* out_rec
,
521 hfs_catalog_key_t
* out_key
,
522 hfs_callback_args
* cbargs
)
524 hfs_cnid_t parentcnid
;
525 hfs_thread_record_t parentthread
;
526 hfs_catalog_key_t key
;
528 if(in_vol
==NULL
|| in_cnid
==0 || out_rec
==NULL
)
532 hfslib_find_parent_thread(in_vol
, in_cnid
, &parentthread
, cbargs
);
534 HFS_LIBERR("could not find parent thread for cnid %i", in_cnid
);
536 if(hfslib_make_catalog_key(parentthread
.parent_cnid
,
537 parentthread
.name
.length
, parentthread
.name
.unicode
, &key
) == 0)
538 HFS_LIBERR("could not make catalog search key");
541 memcpy(out_key
, &key
, sizeof(key
));
543 return hfslib_find_catalog_record_with_key(in_vol
, &key
, out_rec
, cbargs
);
549 /* Returns 0 on success, 1 on error, and -1 if record was not found. */
551 hfslib_find_catalog_record_with_key(
553 hfs_catalog_key_t
* in_key
,
554 hfs_catalog_keyed_record_t
* out_rec
,
555 hfs_callback_args
* cbargs
)
557 hfs_node_descriptor_t nd
;
558 hfs_extent_descriptor_t
* extents
;
559 hfs_catalog_keyed_record_t lastrec
;
560 hfs_catalog_key_t
* curkey
;
572 if(in_key
==NULL
|| out_rec
==NULL
|| in_vol
==NULL
)
582 /* The key takes up over half a kb of ram, which is a lot for the BSD
583 * kernel stack. So allocate it in the heap instead to play it safe. */
584 curkey
= hfslib_malloc(sizeof(hfs_catalog_key_t
), cbargs
);
586 HFS_LIBERR("could not allocate catalog search key");
588 buffer
= hfslib_malloc(in_vol
->chr
.node_size
, cbargs
);
590 HFS_LIBERR("could not allocate node buffer");
592 numextents
= hfslib_get_file_extents(in_vol
, HFS_CNID_CATALOG
,
593 HFS_DATAFORK
, &extents
, cbargs
);
595 HFS_LIBERR("could not locate fork extents");
598 curnode
= in_vol
->chr
.root_node
;
602 dlo_print_key(in_key
);
609 printf("--> node %d\n", curnode
);
612 if(hfslib_readd_with_extents(in_vol
, buffer
,
613 &bytesread
,in_vol
->chr
.node_size
, curnode
* in_vol
->chr
.node_size
,
614 extents
, numextents
, cbargs
)!=0)
615 HFS_LIBERR("could not read catalog node #%i", curnode
);
617 if(hfslib_reada_node(buffer
, &nd
, &recs
, &recsizes
, HFS_CATALOG_FILE
,
619 HFS_LIBERR("could not parse catalog node #%i", curnode
);
621 for(recnum
=0; recnum
<nd
.num_recs
; recnum
++)
624 if(hfslib_read_catalog_keyed_record(recs
[recnum
], out_rec
,
625 &leaftype
, curkey
, in_vol
)==0)
626 HFS_LIBERR("could not read catalog record #%i",recnum
);
629 printf("---> record %d: ", recnum
);
630 dlo_print_key(curkey
);
633 keycompare
= in_vol
->keycmp(in_key
, curkey
);
637 : keycompare
== 0 ? '=' : '>');
642 /* Check if key is less than *every* record, which should never
643 * happen if the volume is consistent and the key legit. */
645 HFS_LIBERR("all records greater than key");
647 /* Otherwise, we've found the first record that exceeds our key,
648 * so retrieve the previous record, which is still less... */
649 memcpy(out_rec
, &lastrec
,
650 sizeof(hfs_catalog_keyed_record_t
));
652 /* ...unless this is a leaf node, which means we've gone from
653 * a key which is smaller than the search key, in the previous
654 * loop, to a key which is larger, in this loop, and that
655 * implies that our search key does not exist on the volume. */
656 if(nd
.kind
==HFS_LEAFNODE
)
661 else if(keycompare
== 0)
663 /* If leaf node, found an exact match. */
667 else if(recnum
==nd
.num_recs
-1 && keycompare
> 0)
669 /* If leaf node, we've reached the last record with no match,
670 * which means this key is not present on the volume. */
675 memcpy(&lastrec
, out_rec
, sizeof(hfs_catalog_keyed_record_t
));
678 if(nd
.kind
==HFS_INDEXNODE
)
679 curnode
= out_rec
->child
;
680 else if(nd
.kind
==HFS_LEAFNODE
)
683 hfslib_free_recs(&recs
, &recsizes
, &nd
.num_recs
, cbargs
);
685 while(nd
.kind
!=HFS_LEAFNODE
);
690 hfslib_free(extents
, cbargs
);
691 hfslib_free_recs(&recs
, &recsizes
, &nd
.num_recs
, cbargs
);
693 hfslib_free(curkey
, cbargs
);
695 hfslib_free(buffer
, cbargs
);
700 /* returns 0 on success */
701 /* XXX Need to look this over and make sure it gracefully handles cases where
702 * XXX the key is not found. */
704 hfslib_find_extent_record_with_key(hfs_volume
* in_vol
,
705 hfs_extent_key_t
* in_key
,
706 hfs_extent_record_t
* out_rec
,
707 hfs_callback_args
* cbargs
)
709 hfs_node_descriptor_t nd
;
710 hfs_extent_descriptor_t
* extents
;
711 hfs_extent_record_t lastrec
;
712 hfs_extent_key_t curkey
;
723 if(in_vol
==NULL
|| in_key
==NULL
|| out_rec
==NULL
)
732 buffer
= hfslib_malloc(in_vol
->ehr
.node_size
, cbargs
);
734 HFS_LIBERR("could not allocate node buffer");
736 numextents
= hfslib_get_file_extents(in_vol
, HFS_CNID_EXTENTS
,
737 HFS_DATAFORK
, &extents
, cbargs
);
739 HFS_LIBERR("could not locate fork extents");
742 curnode
= in_vol
->ehr
.root_node
;
746 hfslib_free_recs(&recs
, &recsizes
, &nd
.num_recs
, cbargs
);
749 if(hfslib_readd_with_extents(in_vol
, buffer
, &bytesread
,
750 in_vol
->ehr
.node_size
, curnode
* in_vol
->ehr
.node_size
, extents
,
751 numextents
, cbargs
)!=0)
752 HFS_LIBERR("could not read extents overflow node #%i", curnode
);
754 if(hfslib_reada_node(buffer
, &nd
, &recs
, &recsizes
, HFS_EXTENTS_FILE
,
756 HFS_LIBERR("could not parse extents overflow node #%i",curnode
);
758 for(recnum
=0; recnum
<nd
.num_recs
; recnum
++)
760 memcpy(&lastrec
, out_rec
, sizeof(hfs_extent_record_t
));
762 if(hfslib_read_extent_record(recs
[recnum
], out_rec
, nd
.kind
,
764 HFS_LIBERR("could not read extents record #%i",recnum
);
766 keycompare
= hfslib_compare_extent_keys(in_key
, &curkey
);
769 /* this should never happen for any legitimate key */
773 memcpy(out_rec
, &lastrec
, sizeof(hfs_extent_record_t
));
777 else if(keycompare
== 0 ||
778 (recnum
==nd
.num_recs
-1 && keycompare
> 0))
782 if(nd
.kind
==HFS_INDEXNODE
)
783 curnode
= *((uint32_t *)out_rec
); /* out_rec is a node ptr in this case */
784 else if(nd
.kind
==HFS_LEAFNODE
)
787 HFS_LIBERR("unknwon node type for extents overflow node #%i",curnode
);
789 while(nd
.kind
!=HFS_LEAFNODE
);
797 hfslib_free(buffer
, cbargs
);
799 hfslib_free(extents
, cbargs
);
800 hfslib_free_recs(&recs
, &recsizes
, &nd
.num_recs
, cbargs
);
805 /* out_extents may be NULL. */
807 hfslib_get_file_extents(hfs_volume
* in_vol
,
810 hfs_extent_descriptor_t
** out_extents
,
811 hfs_callback_args
* cbargs
)
813 hfs_extent_descriptor_t
* dummy
;
814 hfs_extent_key_t extentkey
;
815 hfs_file_record_t file
;
816 hfs_catalog_key_t filekey
;
817 hfs_thread_record_t fileparent
;
818 hfs_fork_t fork
= {.logical_size
= 0};
819 hfs_extent_record_t nextextentrec
;
821 uint16_t numextents
, n
;
823 if(in_vol
==NULL
|| in_cnid
==0)
826 if(out_extents
!=NULL
)
828 *out_extents
= hfslib_malloc(sizeof(hfs_extent_descriptor_t
), cbargs
);
829 if(*out_extents
==NULL
)
835 case HFS_CNID_CATALOG
:
836 fork
= in_vol
->vh
.catalog_file
;
839 case HFS_CNID_EXTENTS
:
840 fork
= in_vol
->vh
.extents_file
;
843 case HFS_CNID_ALLOCATION
:
844 fork
= in_vol
->vh
.allocation_file
;
847 case HFS_CNID_ATTRIBUTES
:
848 fork
= in_vol
->vh
.attributes_file
;
851 case HFS_CNID_STARTUP
:
852 fork
= in_vol
->vh
.startup_file
;
856 if(hfslib_find_parent_thread(in_vol
, in_cnid
, &fileparent
,
860 if(hfslib_make_catalog_key(fileparent
.parent_cnid
,
861 fileparent
.name
.length
, fileparent
.name
.unicode
, &filekey
)==0)
864 if(hfslib_find_catalog_record_with_key(in_vol
, &filekey
,
865 (hfs_catalog_keyed_record_t
*)&file
, cbargs
)!=0)
868 /* only files have extents, not folders or threads */
869 if(file
.rec_type
!=HFS_REC_FILE
)
872 if(in_forktype
==HFS_DATAFORK
)
873 fork
= file
.data_fork
;
874 else if(in_forktype
==HFS_RSRCFORK
)
875 fork
= file
.rsrc_fork
;
880 memcpy(&nextextentrec
, &fork
.extents
, sizeof(hfs_extent_record_t
));
886 if(nextextentrec
[n
].block_count
==0)
889 numblocks
+= nextextentrec
[n
].block_count
;
892 if(out_extents
!=NULL
)
894 dummy
= hfslib_realloc(*out_extents
,
895 (numextents
+n
) * sizeof(hfs_extent_descriptor_t
),
899 *out_extents
= dummy
;
901 memcpy(*out_extents
+ numextents
,
902 &nextextentrec
, n
*sizeof(hfs_extent_descriptor_t
));
906 if(numblocks
>= fork
.total_blocks
)
909 if(hfslib_make_extent_key(in_cnid
, in_forktype
, numblocks
,
913 if(hfslib_find_extent_record_with_key(in_vol
, &extentkey
,
914 &nextextentrec
, cbargs
)!=0)
921 if(out_extents
!=NULL
&& *out_extents
!=NULL
)
923 hfslib_free(*out_extents
, cbargs
);
933 * hfslib_get_directory_contents()
935 * Finds the immediate children of a given directory CNID and places their
936 * CNIDs in an array allocated here. The first child is found by doing a
937 * catalog search that only compares parent CNIDs (ignoring file/folder names)
938 * and skips over thread records. Then the remaining children are listed in
939 * ascending order by name, according to the HFS+ spec, so just read off each
940 * successive leaf node until a different parent CNID is found.
942 * If out_childnames is not NULL, it will be allocated and set to an array of
943 * hfs_unistr255_t's which correspond to the name of the child with that same
946 * out_children may be NULL.
948 * Returns 0 on success.
951 hfslib_get_directory_contents(
954 hfs_catalog_keyed_record_t
** out_children
,
955 hfs_unistr255_t
** out_childnames
,
956 uint32_t* out_numchildren
,
957 hfs_callback_args
* cbargs
)
959 hfs_node_descriptor_t nd
;
960 hfs_extent_descriptor_t
* extents
;
961 hfs_catalog_keyed_record_t currec
;
962 hfs_catalog_key_t curkey
;
965 void* ptr
; /* temporary pointer for realloc() */
976 if(in_vol
==NULL
|| in_dir
==0 || out_numchildren
==NULL
)
985 *out_numchildren
= 0;
986 if(out_children
!=NULL
)
987 *out_children
= NULL
;
988 if(out_childnames
!=NULL
)
989 *out_childnames
= NULL
;
991 buffer
= hfslib_malloc(in_vol
->chr
.node_size
, cbargs
);
993 HFS_LIBERR("could not allocate node buffer");
995 numextents
= hfslib_get_file_extents(in_vol
, HFS_CNID_CATALOG
,
996 HFS_DATAFORK
, &extents
, cbargs
);
998 HFS_LIBERR("could not locate fork extents");
1001 curnode
= in_vol
->chr
.root_node
;
1005 hfslib_free_recs(&recs
, &recsizes
, &nd
.num_recs
, cbargs
);
1008 if(hfslib_readd_with_extents(in_vol
, buffer
, &bytesread
,
1009 in_vol
->chr
.node_size
, curnode
* in_vol
->chr
.node_size
, extents
,
1010 numextents
, cbargs
)!=0)
1011 HFS_LIBERR("could not read catalog node #%i", curnode
);
1013 if(hfslib_reada_node(buffer
, &nd
, &recs
, &recsizes
, HFS_CATALOG_FILE
,
1015 HFS_LIBERR("could not parse catalog node #%i", curnode
);
1017 for(recnum
=0; recnum
<nd
.num_recs
; recnum
++)
1019 leaftype
= nd
.kind
; /* needed b/c leaftype might be modified now */
1020 if(hfslib_read_catalog_keyed_record(recs
[recnum
], &currec
,
1021 &leaftype
, &curkey
, in_vol
)==0)
1022 HFS_LIBERR("could not read cat record %i:%i", curnode
, recnum
);
1024 if(nd
.kind
==HFS_INDEXNODE
)
1026 keycompare
= in_dir
- curkey
.parent_cnid
;
1029 /* Check if key is less than *every* record, which should
1030 * never happen if the volume and key are good. */
1032 HFS_LIBERR("all records greater than key");
1034 /* Otherwise, we've found the first record that exceeds our
1035 * key, so retrieve the previous, lesser record. */
1039 else if(keycompare
== 0)
1042 * Normally, if we were doing a typical catalog lookup with
1043 * both a parent cnid AND a name, keycompare==0 would be an
1044 * exact match. However, since we are ignoring object names
1045 * in this case and only comparing parent cnids, a direct
1046 * match on only a parent cnid could mean that we've found
1047 * an object with that parent cnid BUT which is NOT the
1048 * first object (according to the HFS+ spec) with that
1049 * parent cnid. Thus, when we find a parent cnid match, we
1050 * still go back to the previously found leaf node and start
1051 * checking it for a possible prior instance of an object
1052 * with our desired parent cnid.
1057 else if (recnum
==nd
.num_recs
-1 && keycompare
> 0)
1059 /* Descend to child node if we found an exact match, or if
1060 * this is the last pointer record. */
1061 curnode
= currec
.child
;
1065 lastnode
= currec
.child
;
1070 * We have now descended down the hierarchy of index nodes into
1071 * the leaf node that contains the first catalog record with a
1072 * matching parent CNID. Since all leaf nodes are chained
1073 * through their flink/blink, we can simply walk forward through
1074 * this chain, copying every matching non-thread record, until
1075 * we hit a record with a different parent CNID. At that point,
1076 * we've retrieved all of our directory's items, if any.
1080 if(curkey
.parent_cnid
<in_dir
)
1082 else if(curkey
.parent_cnid
==in_dir
)
1084 /* Hide files/folders which are supposed to be invisible
1085 * to users, according to the hfs+ spec. */
1086 if(hfslib_is_private_file(&curkey
))
1089 /* leaftype has now been set to the catalog record type */
1090 if(leaftype
==HFS_REC_FLDR
|| leaftype
==HFS_REC_FILE
)
1092 (*out_numchildren
)++;
1094 if(out_children
!=NULL
)
1096 ptr
= hfslib_realloc(*out_children
,
1098 sizeof(hfs_catalog_keyed_record_t
), cbargs
);
1100 HFS_LIBERR("could not allocate child record");
1101 *out_children
= ptr
;
1103 memcpy(&((*out_children
)[*out_numchildren
-1]),
1104 &currec
, sizeof(hfs_catalog_keyed_record_t
));
1107 if(out_childnames
!=NULL
)
1109 ptr
= hfslib_realloc(*out_childnames
,
1110 *out_numchildren
* sizeof(hfs_unistr255_t
),
1113 HFS_LIBERR("could not allocate child name");
1114 *out_childnames
= ptr
;
1116 memcpy(&((*out_childnames
)[*out_numchildren
-1]),
1117 &curkey
.name
, sizeof(hfs_unistr255_t
));
1122 /* We have just now passed the last item in the desired
1123 * folder (or the folder was empty), so exit. */
1135 if(out_children
!=NULL
&& *out_children
!=NULL
)
1136 hfslib_free(*out_children
, cbargs
);
1137 if(out_childnames
!=NULL
&& *out_childnames
!=NULL
)
1138 hfslib_free(*out_childnames
, cbargs
);
1144 hfslib_free(extents
, cbargs
);
1145 hfslib_free_recs(&recs
, &recsizes
, &nd
.num_recs
, cbargs
);
1147 hfslib_free(buffer
, cbargs
);
1153 hfslib_is_journal_clean(hfs_volume
* in_vol
)
1158 /* return true if no journal */
1159 if(!(in_vol
->vh
.attributes
& (1<<HFS_VOL_JOURNALED
)))
1162 return (in_vol
->jh
.start
== in_vol
->jh
.end
);
1166 * hfslib_is_private_file()
1168 * Given a file/folder's key and parent CNID, determines if it should be hidden
1169 * from the user (e.g., the journal header file or the HFS+ Private Data folder)
1172 hfslib_is_private_file(hfs_catalog_key_t
*filekey
)
1174 hfs_catalog_key_t
* curkey
= NULL
;
1178 * According to the HFS+ spec to date, all special objects are located in
1179 * the root directory of the volume, so don't bother going further if the
1180 * requested object is not.
1182 if(filekey
->parent_cnid
!= HFS_CNID_ROOT_FOLDER
)
1185 while((curkey
= hfs_gPrivateObjectKeys
[i
]) != NULL
)
1187 /* XXX Always use binary compare here, or use volume's specific key
1188 * XXX comparison routine? */
1189 if(filekey
->name
.length
== curkey
->name
.length
1190 && memcmp(filekey
->name
.unicode
, curkey
->name
.unicode
,
1191 2 * curkey
->name
.length
)==0)
1202 hfslib_is_journal_valid(hfs_volume* in_vol)
1204 - check magic numbers
1205 - check Other Things
1210 #pragma mark Major Structures
1214 * hfslib_read_volume_header()
1216 * Reads in_bytes, formats the data appropriately, and places the result
1217 * in out_header, which is assumed to be previously allocated. Returns number
1218 * of bytes read, 0 if failed.
1222 hfslib_read_volume_header(void* in_bytes
, hfs_volume_header_t
* out_header
)
1225 size_t last_bytes_read
;
1228 if(in_bytes
==NULL
|| out_header
==NULL
)
1233 out_header
->signature
= be16tohp(&ptr
);
1234 out_header
->version
= be16tohp(&ptr
);
1235 out_header
->attributes
= be32tohp(&ptr
);
1236 out_header
->last_mounting_version
= be32tohp(&ptr
);
1237 out_header
->journal_info_block
= be32tohp(&ptr
);
1239 out_header
->date_created
= be32tohp(&ptr
);
1240 out_header
->date_modified
= be32tohp(&ptr
);
1241 out_header
->date_backedup
= be32tohp(&ptr
);
1242 out_header
->date_checked
= be32tohp(&ptr
);
1244 out_header
->file_count
= be32tohp(&ptr
);
1245 out_header
->folder_count
= be32tohp(&ptr
);
1247 out_header
->block_size
= be32tohp(&ptr
);
1248 out_header
->total_blocks
= be32tohp(&ptr
);
1249 out_header
->free_blocks
= be32tohp(&ptr
);
1250 out_header
->next_alloc_block
= be32tohp(&ptr
);
1251 out_header
->rsrc_clump_size
= be32tohp(&ptr
);
1252 out_header
->data_clump_size
= be32tohp(&ptr
);
1253 out_header
->next_cnid
= be32tohp(&ptr
);
1255 out_header
->write_count
= be32tohp(&ptr
);
1256 out_header
->encodings
= be64tohp(&ptr
);
1259 out_header
->finder_info
[i
] = be32tohp(&ptr
);
1261 if((last_bytes_read
= hfslib_read_fork_descriptor(ptr
,
1262 &out_header
->allocation_file
))==0)
1264 ptr
= (uint8_t*)ptr
+ last_bytes_read
;
1266 if((last_bytes_read
= hfslib_read_fork_descriptor(ptr
,
1267 &out_header
->extents_file
))==0)
1269 ptr
= (uint8_t*)ptr
+ last_bytes_read
;
1271 if((last_bytes_read
= hfslib_read_fork_descriptor(ptr
,
1272 &out_header
->catalog_file
))==0)
1274 ptr
= (uint8_t*)ptr
+ last_bytes_read
;
1276 if((last_bytes_read
= hfslib_read_fork_descriptor(ptr
,
1277 &out_header
->attributes_file
))==0)
1279 ptr
= (uint8_t*)ptr
+ last_bytes_read
;
1281 if((last_bytes_read
= hfslib_read_fork_descriptor(ptr
,
1282 &out_header
->startup_file
))==0)
1284 ptr
= (uint8_t*)ptr
+ last_bytes_read
;
1286 return ((uint8_t*)ptr
- (uint8_t*)in_bytes
);
1290 * hfsplib_read_master_directory_block()
1292 * Reads in_bytes, formats the data appropriately, and places the result
1293 * in out_header, which is assumed to be previously allocated. Returns numb
1295 * of bytes read, 0 if failed.
1299 hfslib_read_master_directory_block(void* in_bytes
,
1300 hfs_hfs_master_directory_block_t
* out_mdr
)
1305 if(in_bytes
==NULL
|| out_mdr
==NULL
)
1310 out_mdr
->signature
= be16tohp(&ptr
);
1312 out_mdr
->date_created
= be32tohp(&ptr
);
1313 out_mdr
->date_modified
= be32tohp(&ptr
);
1315 out_mdr
->attributes
= be16tohp(&ptr
);
1316 out_mdr
->root_file_count
= be16tohp(&ptr
);
1317 out_mdr
->volume_bitmap
= be16tohp(&ptr
);
1319 out_mdr
->next_alloc_block
= be16tohp(&ptr
);
1320 out_mdr
->total_blocks
= be16tohp(&ptr
);
1321 out_mdr
->block_size
= be32tohp(&ptr
);
1323 out_mdr
->clump_size
= be32tohp(&ptr
);
1324 out_mdr
->first_block
= be16tohp(&ptr
);
1325 out_mdr
->next_cnid
= be32tohp(&ptr
);
1326 out_mdr
->free_blocks
= be16tohp(&ptr
);
1328 memcpy(out_mdr
->volume_name
, ptr
, 28);
1329 ptr
= (char *)ptr
+ 28;
1331 out_mdr
->date_backedup
= be32tohp(&ptr
);
1332 out_mdr
->backup_seqnum
= be16tohp(&ptr
);
1334 out_mdr
->write_count
= be32tohp(&ptr
);
1336 out_mdr
->extents_clump_size
= be32tohp(&ptr
);
1337 out_mdr
->catalog_clump_size
= be32tohp(&ptr
);
1339 out_mdr
->root_folder_count
= be16tohp(&ptr
);
1340 out_mdr
->file_count
= be32tohp(&ptr
);
1341 out_mdr
->folder_count
= be32tohp(&ptr
);
1344 out_mdr
->finder_info
[i
] = be32tohp(&ptr
);
1346 out_mdr
->embedded_signature
= be16tohp(&ptr
);
1347 out_mdr
->embedded_extent
.start_block
= be16tohp(&ptr
);
1348 out_mdr
->embedded_extent
.block_count
= be16tohp(&ptr
);
1350 out_mdr
->extents_size
= be32tohp(&ptr
);
1351 for (i
= 0; i
< 3; i
++)
1353 out_mdr
->extents_extents
[i
].start_block
= be16tohp(&ptr
);
1354 out_mdr
->extents_extents
[i
].block_count
= be16tohp(&ptr
);
1357 out_mdr
->catalog_size
= be32tohp(&ptr
);
1358 for (i
= 0; i
< 3; i
++)
1360 out_mdr
->catalog_extents
[i
].start_block
= be16tohp(&ptr
);
1361 out_mdr
->catalog_extents
[i
].block_count
= be16tohp(&ptr
);
1364 return ((uint8_t*)ptr
- (uint8_t*)in_bytes
);
1368 * hfslib_reada_node()
1370 * Given the pointer to and size of a buffer containing the entire, raw
1371 * contents of any b-tree node from the disk, this function will:
1373 * 1. determine the type of node and read its contents
1374 * 2. allocate memory for each record and fill it appropriately
1375 * 3. set out_record_ptrs_array to point to an array (which it allocates)
1376 * which has out_node_descriptor->num_recs many pointers to the
1377 * records themselves
1378 * 4. allocate out_record_ptr_sizes_array and fill it with the sizes of
1380 * 5. return the number of bytes read (i.e., the size of the node)
1383 * out_node_descriptor must be allocated by the caller and may not be NULL.
1385 * out_record_ptrs_array and out_record_ptr_sizes_array must both be specified,
1386 * or both be NULL if the caller is not interested in reading the records.
1388 * out_record_ptr_sizes_array may be NULL if the caller is not interested in
1389 * reading the records, but must not be NULL if out_record_ptrs_array is not.
1391 * in_parent_file is HFS_CATALOG_FILE, HFS_EXTENTS_FILE, or
1392 * HFS_ATTRIBUTES_FILE, depending on the special file in which this node
1395 * inout_volume must have its catnodesize or extnodesize field (depending on
1396 * the parent file) set to the correct value if this is an index, leaf, or map
1397 * node. If this is a header node, the field will be set to its correct value.
1400 hfslib_reada_node(void* in_bytes
,
1401 hfs_node_descriptor_t
* out_node_descriptor
,
1402 void** out_record_ptrs_array
[],
1403 uint16_t* out_record_ptr_sizes_array
[],
1404 hfs_btree_file_type in_parent_file
,
1405 hfs_volume
* inout_volume
,
1406 hfs_callback_args
* cbargs
)
1409 uint16_t* rec_offsets
;
1410 size_t last_bytes_read
;
1412 uint16_t numrecords
;
1413 uint16_t free_space_offset
; /* offset to free space in node */
1414 int keysizefieldsize
;
1419 if(out_record_ptrs_array
!=NULL
)
1420 *out_record_ptrs_array
= NULL
;
1421 if(out_record_ptr_sizes_array
!=NULL
)
1422 *out_record_ptr_sizes_array
= NULL
;
1424 if(in_bytes
==NULL
|| inout_volume
==NULL
|| out_node_descriptor
==NULL
1425 || (out_record_ptrs_array
==NULL
&& out_record_ptr_sizes_array
!=NULL
)
1426 || (out_record_ptrs_array
!=NULL
&& out_record_ptr_sizes_array
==NULL
) )
1431 out_node_descriptor
->flink
= be32tohp(&ptr
);
1432 out_node_descriptor
->blink
= be32tohp(&ptr
);
1433 out_node_descriptor
->kind
= *(((int8_t*)ptr
));
1434 ptr
= (uint8_t*)ptr
+ 1;
1435 out_node_descriptor
->height
= *(((uint8_t*)ptr
));
1436 ptr
= (uint8_t*)ptr
+ 1;
1437 out_node_descriptor
->num_recs
= be16tohp(&ptr
);
1438 out_node_descriptor
->reserved
= be16tohp(&ptr
);
1440 numrecords
= out_node_descriptor
->num_recs
;
1443 * To go any further, we will need to know the size of this node, as well
1444 * as the width of keyed records' key_len parameters for this btree. If
1445 * this is an index, leaf, or map node, inout_volume already has the node
1446 * size set in its catnodesize or extnodesize field and the key length set
1447 * in the catkeysizefieldsize or extkeysizefieldsize for catalog files and
1448 * extent files, respectively. However, if this is a header node, this
1449 * information has not yet been determined, so this is the place to do it.
1451 if(out_node_descriptor
->kind
== HFS_HEADERNODE
)
1453 hfs_header_record_t hr
;
1454 void* header_rec_offset
[1];
1455 uint16_t header_rec_size
[1];
1457 /* sanity check to ensure this is a good header node */
1459 HFS_LIBERR("header node does not have exactly 3 records");
1461 header_rec_offset
[0] = ptr
;
1462 header_rec_size
[0] = sizeof(hfs_header_record_t
);
1464 last_bytes_read
= hfslib_read_header_node(header_rec_offset
,
1465 header_rec_size
, 1, &hr
, NULL
, NULL
);
1466 if(last_bytes_read
==0)
1467 HFS_LIBERR("could not read header node");
1469 switch(in_parent_file
)
1471 case HFS_CATALOG_FILE
:
1472 inout_volume
->chr
.node_size
= hr
.node_size
;
1473 inout_volume
->catkeysizefieldsize
=
1474 (hr
.attributes
& HFS_BIG_KEYS_MASK
) ?
1475 sizeof(uint16_t):sizeof(uint8_t);
1478 case HFS_EXTENTS_FILE
:
1479 inout_volume
->ehr
.node_size
= hr
.node_size
;
1480 inout_volume
->extkeysizefieldsize
=
1481 (hr
.attributes
& HFS_BIG_KEYS_MASK
) ?
1482 sizeof(uint16_t):sizeof(uint8_t);
1485 case HFS_ATTRIBUTES_FILE
:
1487 HFS_LIBERR("invalid parent file type specified");
1492 switch(in_parent_file
)
1494 case HFS_CATALOG_FILE
:
1495 nodesize
= inout_volume
->chr
.node_size
;
1496 keysizefieldsize
= inout_volume
->catkeysizefieldsize
;
1499 case HFS_EXTENTS_FILE
:
1500 nodesize
= inout_volume
->ehr
.node_size
;
1501 keysizefieldsize
= inout_volume
->extkeysizefieldsize
;
1504 case HFS_ATTRIBUTES_FILE
:
1506 HFS_LIBERR("invalid parent file type specified");
1511 * Don't care about records so just exit after getting the node descriptor.
1512 * Note: This happens after the header node code, and not before it, in
1513 * case the caller calls this function and ignores the record data just to
1514 * get at the node descriptor, but then tries to call it again on a non-
1515 * header node without first setting inout_volume->cat/extnodesize.
1517 if(out_record_ptrs_array
==NULL
)
1518 return ((uint8_t*)ptr
- (uint8_t*)in_bytes
);
1520 rec_offsets
= hfslib_malloc(numrecords
* sizeof(uint16_t), cbargs
);
1521 *out_record_ptr_sizes_array
=
1522 hfslib_malloc(numrecords
* sizeof(uint16_t), cbargs
);
1523 if(rec_offsets
==NULL
|| *out_record_ptr_sizes_array
==NULL
)
1524 HFS_LIBERR("could not allocate node record offsets");
1526 *out_record_ptrs_array
= hfslib_malloc(numrecords
* sizeof(void*), cbargs
);
1527 if(*out_record_ptrs_array
==NULL
)
1528 HFS_LIBERR("could not allocate node records");
1530 last_bytes_read
= hfslib_reada_node_offsets((uint8_t*)in_bytes
+ nodesize
-
1531 numrecords
* sizeof(uint16_t), rec_offsets
);
1532 if(last_bytes_read
==0)
1533 HFS_LIBERR("could not read node record offsets");
1535 /* The size of the last record (i.e. the first one listed in the offsets)
1536 * must be determined using the offset to the node's free space. */
1537 free_space_offset
= be16toh(*(uint16_t*)((uint8_t*)in_bytes
+ nodesize
-
1538 (numrecords
+1) * sizeof(uint16_t)));
1540 (*out_record_ptr_sizes_array
)[numrecords
-1] =
1541 free_space_offset
- rec_offsets
[0];
1542 for(i
=1;i
<numrecords
;i
++)
1544 (*out_record_ptr_sizes_array
)[numrecords
-i
-1] =
1545 rec_offsets
[i
-1] - rec_offsets
[i
];
1548 for(i
=0;i
<numrecords
;i
++)
1550 (*out_record_ptrs_array
)[i
] =
1551 hfslib_malloc((*out_record_ptr_sizes_array
)[i
], cbargs
);
1553 if((*out_record_ptrs_array
)[i
]==NULL
)
1554 HFS_LIBERR("could not allocate node record #%i",i
);
1557 * If this is a keyed node (i.e., a leaf or index node), there are two
1558 * boundary rules that each record must obey:
1560 * 1. A pad byte must be placed between the key and data if the
1561 * size of the key plus the size of the key_len field is odd.
1563 * 2. A pad byte must be placed after the data if the data size
1566 * So in the first case we increment the starting point of the data
1567 * and correspondingly decrement the record size. In the second case
1568 * we decrement the record size.
1570 if(out_node_descriptor
->kind
== HFS_LEAFNODE
||
1571 out_node_descriptor
->kind
== HFS_INDEXNODE
)
1573 hfs_catalog_key_t reckey
;
1576 rectype
= out_node_descriptor
->kind
;
1577 last_bytes_read
= hfslib_read_catalog_keyed_record(ptr
, NULL
,
1578 &rectype
, &reckey
, inout_volume
);
1579 if(last_bytes_read
==0)
1580 HFS_LIBERR("could not read node record");
1582 if((reckey
.key_len
+ keysizefieldsize
) % 2 == 1)
1584 ptr
= (uint8_t*)ptr
+ 1;
1585 (*out_record_ptr_sizes_array
)[i
]--;
1588 if((*out_record_ptr_sizes_array
)[i
] % 2 == 1)
1589 (*out_record_ptr_sizes_array
)[i
]--;
1592 memcpy((*out_record_ptrs_array
)[i
], ptr
,
1593 (*out_record_ptr_sizes_array
)[i
]);
1594 ptr
= (uint8_t*)ptr
+ (*out_record_ptr_sizes_array
)[i
];
1600 hfslib_free_recs(out_record_ptrs_array
, out_record_ptr_sizes_array
,
1601 &numrecords
, cbargs
);
1605 /* warn("error occurred in hfslib_reada_node()"); */
1610 if(rec_offsets
!=NULL
)
1611 hfslib_free(rec_offsets
, cbargs
);
1613 return ((uint8_t*)ptr
- (uint8_t*)in_bytes
);
1617 * hfslib_reada_node_offsets()
1619 * Sets out_offset_array to contain the offsets to each record in the node,
1620 * in reverse order. Does not read the free space offset.
1623 hfslib_reada_node_offsets(void* in_bytes
, uint16_t* out_offset_array
)
1627 if(in_bytes
==NULL
|| out_offset_array
==NULL
)
1633 * The offset for record 0 (which is the very last offset in the node) is
1634 * always equal to 14, the size of the node descriptor. So, once we hit
1635 * offset=14, we know this is the last offset. In this way, we don't need
1636 * to know the number of records beforehand.
1642 *out_offset_array
= be16tohp(&ptr
);
1644 while(*out_offset_array
!= (uint16_t)14);
1646 return ((uint8_t*)ptr
- (uint8_t*)in_bytes
);
1649 /* hfslib_read_header_node()
1651 * out_header_record and/or out_map_record may be NULL if the caller doesn't
1652 * care about their contents.
1655 hfslib_read_header_node(void** in_recs
,
1656 uint16_t* in_rec_sizes
,
1657 uint16_t in_num_recs
,
1658 hfs_header_record_t
* out_hr
,
1665 if(in_recs
==NULL
|| in_rec_sizes
==NULL
)
1672 out_hr
->tree_depth
= be16tohp(&ptr
);
1673 out_hr
->root_node
= be32tohp(&ptr
);
1674 out_hr
->leaf_recs
= be32tohp(&ptr
);
1675 out_hr
->first_leaf
= be32tohp(&ptr
);
1676 out_hr
->last_leaf
= be32tohp(&ptr
);
1677 out_hr
->node_size
= be16tohp(&ptr
);
1678 out_hr
->max_key_len
= be16tohp(&ptr
);
1679 out_hr
->total_nodes
= be32tohp(&ptr
);
1680 out_hr
->free_nodes
= be32tohp(&ptr
);
1681 out_hr
->reserved
= be16tohp(&ptr
);
1682 out_hr
->clump_size
= be32tohp(&ptr
);
1683 out_hr
->btree_type
= *(((uint8_t*)ptr
));
1684 ptr
= (uint8_t*)ptr
+ 1;
1685 out_hr
->keycomp_type
= *(((uint8_t*)ptr
));
1686 ptr
= (uint8_t*)ptr
+ 1;
1687 out_hr
->attributes
= be32tohp(&ptr
);
1689 out_hr
->reserved2
[i
] = be32tohp(&ptr
);
1692 if(out_userdata
!=NULL
)
1694 memcpy(out_userdata
, in_recs
[1], in_rec_sizes
[1]);
1696 ptr
= (uint8_t*)ptr
+ in_rec_sizes
[1]; /* size of user data record */
1700 memcpy(out_map
, in_recs
[2], in_rec_sizes
[2]);
1702 ptr
= (uint8_t*)ptr
+ in_rec_sizes
[2]; /* size of map record */
1704 return ((uint8_t*)ptr
- (uint8_t*)in_recs
[0]);
1708 * hfslib_read_catalog_keyed_record()
1710 * out_recdata can be NULL. inout_rectype must be set to either HFS_LEAFNODE
1711 * or HFS_INDEXNODE upon calling this function, and will be set by the
1712 * function to one of HFS_REC_FLDR, HFS_REC_FILE, HFS_REC_FLDR_THREAD, or
1713 * HFS_REC_FLDR_THREAD upon return if the node is a leaf node. If it is an
1714 * index node, inout_rectype will not be changed.
1717 hfslib_read_catalog_keyed_record(
1719 hfs_catalog_keyed_record_t
* out_recdata
,
1720 int16_t* inout_rectype
,
1721 hfs_catalog_key_t
* out_key
,
1722 hfs_volume
* in_volume
)
1725 size_t last_bytes_read
;
1727 if(in_bytes
==NULL
|| out_key
==NULL
|| inout_rectype
==NULL
)
1732 /* For HFS+, the key length is always a 2-byte number. This is indicated
1733 * by the HFS_BIG_KEYS_MASK bit in the attributes field of the catalog
1734 * header record. However, we just assume this bit is set, since all HFS+
1735 * volumes should have it set anyway. */
1736 if(in_volume
->catkeysizefieldsize
== sizeof(uint16_t))
1737 out_key
->key_len
= be16tohp(&ptr
);
1738 else if (in_volume
->catkeysizefieldsize
== sizeof(uint8_t)) {
1739 out_key
->key_len
= *(((uint8_t*)ptr
));
1740 ptr
= (uint8_t*)ptr
+ 1;
1743 out_key
->parent_cnid
= be32tohp(&ptr
);
1745 last_bytes_read
= hfslib_read_unistr255(ptr
, &out_key
->name
);
1746 if(last_bytes_read
==0)
1748 ptr
= (uint8_t*)ptr
+ last_bytes_read
;
1750 /* don't waste time if the user just wanted the key and/or record type */
1751 if(out_recdata
==NULL
)
1753 if(*inout_rectype
== HFS_LEAFNODE
)
1754 *inout_rectype
= be16tohp(&ptr
);
1755 else if(*inout_rectype
!= HFS_INDEXNODE
)
1756 return 0; /* should not happen if we were given valid arguments */
1758 return ((uint8_t*)ptr
- (uint8_t*)in_bytes
);
1761 if(*inout_rectype
== HFS_INDEXNODE
)
1763 out_recdata
->child
= be32tohp(&ptr
);
1767 /* first need to determine what kind of record this is */
1768 *inout_rectype
= be16tohp(&ptr
);
1769 out_recdata
->type
= *inout_rectype
;
1771 switch(out_recdata
->type
)
1775 out_recdata
->folder
.flags
= be16tohp(&ptr
);
1776 out_recdata
->folder
.valence
= be32tohp(&ptr
);
1777 out_recdata
->folder
.cnid
= be32tohp(&ptr
);
1778 out_recdata
->folder
.date_created
= be32tohp(&ptr
);
1779 out_recdata
->folder
.date_content_mod
= be32tohp(&ptr
);
1780 out_recdata
->folder
.date_attrib_mod
= be32tohp(&ptr
);
1781 out_recdata
->folder
.date_accessed
= be32tohp(&ptr
);
1782 out_recdata
->folder
.date_backedup
= be32tohp(&ptr
);
1784 last_bytes_read
= hfslib_read_bsd_data(ptr
,
1785 &out_recdata
->folder
.bsd
);
1786 if(last_bytes_read
==0)
1788 ptr
= (uint8_t*)ptr
+ last_bytes_read
;
1790 last_bytes_read
= hfslib_read_folder_userinfo(ptr
,
1791 &out_recdata
->folder
.user_info
);
1792 if(last_bytes_read
==0)
1794 ptr
= (uint8_t*)ptr
+ last_bytes_read
;
1796 last_bytes_read
= hfslib_read_folder_finderinfo(ptr
,
1797 &out_recdata
->folder
.finder_info
);
1798 if(last_bytes_read
==0)
1800 ptr
= (uint8_t*)ptr
+ last_bytes_read
;
1802 out_recdata
->folder
.text_encoding
= be32tohp(&ptr
);
1803 out_recdata
->folder
.reserved
= be32tohp(&ptr
);
1809 out_recdata
->file
.flags
= be16tohp(&ptr
);
1810 out_recdata
->file
.reserved
= be32tohp(&ptr
);
1811 out_recdata
->file
.cnid
= be32tohp(&ptr
);
1812 out_recdata
->file
.date_created
= be32tohp(&ptr
);
1813 out_recdata
->file
.date_content_mod
= be32tohp(&ptr
);
1814 out_recdata
->file
.date_attrib_mod
= be32tohp(&ptr
);
1815 out_recdata
->file
.date_accessed
= be32tohp(&ptr
);
1816 out_recdata
->file
.date_backedup
= be32tohp(&ptr
);
1818 last_bytes_read
= hfslib_read_bsd_data(ptr
,
1819 &out_recdata
->file
.bsd
);
1820 if(last_bytes_read
==0)
1822 ptr
= (uint8_t*)ptr
+ last_bytes_read
;
1824 last_bytes_read
= hfslib_read_file_userinfo(ptr
,
1825 &out_recdata
->file
.user_info
);
1826 if(last_bytes_read
==0)
1828 ptr
= (uint8_t*)ptr
+ last_bytes_read
;
1830 last_bytes_read
= hfslib_read_file_finderinfo(ptr
,
1831 &out_recdata
->file
.finder_info
);
1832 if(last_bytes_read
==0)
1834 ptr
= (uint8_t*)ptr
+ last_bytes_read
;
1836 out_recdata
->file
.text_encoding
= be32tohp(&ptr
);
1837 out_recdata
->file
.reserved2
= be32tohp(&ptr
);
1839 last_bytes_read
= hfslib_read_fork_descriptor(ptr
,
1840 &out_recdata
->file
.data_fork
);
1841 if(last_bytes_read
==0)
1843 ptr
= (uint8_t*)ptr
+ last_bytes_read
;
1845 last_bytes_read
= hfslib_read_fork_descriptor(ptr
,
1846 &out_recdata
->file
.rsrc_fork
);
1847 if(last_bytes_read
==0)
1849 ptr
= (uint8_t*)ptr
+ last_bytes_read
;
1853 case HFS_REC_FLDR_THREAD
:
1854 case HFS_REC_FILE_THREAD
:
1856 out_recdata
->thread
.reserved
= be16tohp(&ptr
);
1857 out_recdata
->thread
.parent_cnid
= be32tohp(&ptr
);
1859 last_bytes_read
= hfslib_read_unistr255(ptr
,
1860 &out_recdata
->thread
.name
);
1861 if(last_bytes_read
==0)
1863 ptr
= (uint8_t*)ptr
+ last_bytes_read
;
1873 return ((uint8_t*)ptr
- (uint8_t*)in_bytes
);
1876 /* out_rec may be NULL */
1878 hfslib_read_extent_record(
1880 hfs_extent_record_t
* out_rec
,
1881 hfs_node_kind in_nodekind
,
1882 hfs_extent_key_t
* out_key
,
1883 hfs_volume
* in_volume
)
1886 size_t last_bytes_read
;
1888 if(in_bytes
==NULL
|| out_key
==NULL
1889 || (in_nodekind
!=HFS_LEAFNODE
&& in_nodekind
!=HFS_INDEXNODE
))
1894 /* For HFS+, the key length is always a 2-byte number. This is indicated
1895 * by the HFS_BIG_KEYS_MASK bit in the attributes field of the extent
1896 * overflow header record. However, we just assume this bit is set, since
1897 * all HFS+ volumes should have it set anyway. */
1898 if(in_volume
->extkeysizefieldsize
== sizeof(uint16_t))
1899 out_key
->key_length
= be16tohp(&ptr
);
1900 else if (in_volume
->extkeysizefieldsize
== sizeof(uint8_t)) {
1901 out_key
->key_length
= *(((uint8_t*)ptr
));
1902 ptr
= (uint8_t*)ptr
+ 1;
1905 out_key
->fork_type
= *(((uint8_t*)ptr
));
1906 ptr
= (uint8_t*)ptr
+ 1;
1907 out_key
->padding
= *(((uint8_t*)ptr
));
1908 ptr
= (uint8_t*)ptr
+ 1;
1909 out_key
->file_cnid
= be32tohp(&ptr
);
1910 out_key
->start_block
= be32tohp(&ptr
);
1912 /* don't waste time if the user just wanted the key */
1914 return ((uint8_t*)ptr
- (uint8_t*)in_bytes
);
1916 if(in_nodekind
==HFS_LEAFNODE
)
1918 last_bytes_read
= hfslib_read_extent_descriptors(ptr
, out_rec
);
1919 if(last_bytes_read
==0)
1921 ptr
= (uint8_t*)ptr
+ last_bytes_read
;
1925 /* XXX: this is completely bogus */
1926 /* (uint32_t*)*out_rec = be32tohp(&ptr); */
1927 uint32_t *ptr_32
= (uint32_t *)out_rec
;
1928 *ptr_32
= be32tohp(&ptr
);
1929 /* (*out_rec)[0].start_block = be32tohp(&ptr); */
1932 return ((uint8_t*)ptr
- (uint8_t*)in_bytes
);
1937 void*** inout_node_recs
,
1938 uint16_t** inout_rec_sizes
,
1939 uint16_t* inout_num_recs
,
1940 hfs_callback_args
* cbargs
)
1944 if(inout_num_recs
==NULL
|| *inout_num_recs
==0)
1947 if(inout_node_recs
!=NULL
&& *inout_node_recs
!=NULL
)
1949 for(i
=0;i
<*inout_num_recs
;i
++)
1951 if((*inout_node_recs
)[i
]!=NULL
)
1953 hfslib_free((*inout_node_recs
)[i
], cbargs
);
1954 (*inout_node_recs
)[i
] = NULL
;
1958 hfslib_free(*inout_node_recs
, cbargs
);
1959 *inout_node_recs
= NULL
;
1962 if(inout_rec_sizes
!=NULL
&& *inout_rec_sizes
!=NULL
)
1964 hfslib_free(*inout_rec_sizes
, cbargs
);
1965 *inout_rec_sizes
= NULL
;
1968 *inout_num_recs
= 0;
1973 #pragma mark Individual Fields
1977 hfslib_read_fork_descriptor(void* in_bytes
, hfs_fork_t
* out_forkdata
)
1980 size_t last_bytes_read
;
1982 if(in_bytes
==NULL
|| out_forkdata
==NULL
)
1987 out_forkdata
->logical_size
= be64tohp(&ptr
);
1988 out_forkdata
->clump_size
= be32tohp(&ptr
);
1989 out_forkdata
->total_blocks
= be32tohp(&ptr
);
1991 if((last_bytes_read
= hfslib_read_extent_descriptors(ptr
,
1992 &out_forkdata
->extents
))==0)
1994 ptr
= (uint8_t*)ptr
+ last_bytes_read
;
1996 return ((uint8_t*)ptr
- (uint8_t*)in_bytes
);
2000 hfslib_read_extent_descriptors(
2002 hfs_extent_record_t
* out_extentrecord
)
2007 if(in_bytes
==NULL
|| out_extentrecord
==NULL
)
2014 (((hfs_extent_descriptor_t
*)*out_extentrecord
)[i
]).start_block
=
2016 (((hfs_extent_descriptor_t
*)*out_extentrecord
)[i
]).block_count
=
2020 return ((uint8_t*)ptr
- (uint8_t*)in_bytes
);
2024 hfslib_read_unistr255(void* in_bytes
, hfs_unistr255_t
* out_string
)
2029 if(in_bytes
==NULL
|| out_string
==NULL
)
2034 length
= be16tohp(&ptr
);
2036 length
= 255; /* hfs+ folder/file names have a limit of 255 chars */
2037 out_string
->length
= length
;
2039 for(i
=0; i
<length
; i
++)
2041 out_string
->unicode
[i
] = be16tohp(&ptr
);
2044 return ((uint8_t*)ptr
- (uint8_t*)in_bytes
);
2048 hfslib_read_bsd_data(void* in_bytes
, hfs_bsd_data_t
* out_perms
)
2052 if(in_bytes
==NULL
|| out_perms
==NULL
)
2057 out_perms
->owner_id
= be32tohp(&ptr
);
2058 out_perms
->group_id
= be32tohp(&ptr
);
2059 out_perms
->admin_flags
= *(((uint8_t*)ptr
));
2060 ptr
= (uint8_t*)ptr
+ 1;
2061 out_perms
->owner_flags
= *(((uint8_t*)ptr
));
2062 ptr
= (uint8_t*)ptr
+ 1;
2063 out_perms
->file_mode
= be16tohp(&ptr
);
2064 out_perms
->special
.inode_num
= be32tohp(&ptr
); /* this field is a union */
2066 return ((uint8_t*)ptr
- (uint8_t*)in_bytes
);
2070 hfslib_read_file_userinfo(void* in_bytes
, hfs_macos_file_info_t
* out_info
)
2074 if(in_bytes
==NULL
|| out_info
==NULL
)
2079 out_info
->file_type
= be32tohp(&ptr
);
2080 out_info
->file_creator
= be32tohp(&ptr
);
2081 out_info
->finder_flags
= be16tohp(&ptr
);
2082 out_info
->location
.v
= be16tohp(&ptr
);
2083 out_info
->location
.h
= be16tohp(&ptr
);
2084 out_info
->reserved
= be16tohp(&ptr
);
2086 return ((uint8_t*)ptr
- (uint8_t*)in_bytes
);
2090 hfslib_read_file_finderinfo(
2092 hfs_macos_extended_file_info_t
* out_info
)
2096 if(in_bytes
==NULL
|| out_info
==NULL
)
2102 #pragma warn Fill in with real code!
2104 /* FIXME: Fill in with real code! */
2105 memset(out_info
, 0, sizeof(*out_info
));
2106 ptr
= (uint8_t*)ptr
+ sizeof(hfs_macos_extended_file_info_t
);
2108 return ((uint8_t*)ptr
- (uint8_t*)in_bytes
);
2112 hfslib_read_folder_userinfo(void* in_bytes
, hfs_macos_folder_info_t
* out_info
)
2116 if(in_bytes
==NULL
|| out_info
==NULL
)
2122 #pragma warn Fill in with real code!
2124 /* FIXME: Fill in with real code! */
2125 memset(out_info
, 0, sizeof(*out_info
));
2126 ptr
= (uint8_t*)ptr
+ sizeof(hfs_macos_folder_info_t
);
2128 return ((uint8_t*)ptr
- (uint8_t*)in_bytes
);
2132 hfslib_read_folder_finderinfo(
2134 hfs_macos_extended_folder_info_t
* out_info
)
2138 if(in_bytes
==NULL
|| out_info
==NULL
)
2144 #pragma warn Fill in with real code!
2146 /* FIXME: Fill in with real code! */
2147 memset(out_info
, 0, sizeof(*out_info
));
2148 ptr
= (uint8_t*)ptr
+ sizeof(hfs_macos_extended_folder_info_t
);
2150 return ((uint8_t*)ptr
- (uint8_t*)in_bytes
);
2154 hfslib_read_journal_info(void* in_bytes
, hfs_journal_info_t
* out_info
)
2159 if(in_bytes
==NULL
|| out_info
==NULL
)
2164 out_info
->flags
= be32tohp(&ptr
);
2167 out_info
->device_signature
[i
] = be32tohp(&ptr
);
2169 out_info
->offset
= be64tohp(&ptr
);
2170 out_info
->size
= be64tohp(&ptr
);
2173 out_info
->reserved
[i
] = be64tohp(&ptr
);
2176 return ((uint8_t*)ptr
- (uint8_t*)in_bytes
);
2180 hfslib_read_journal_header(void* in_bytes
, hfs_journal_header_t
* out_header
)
2184 if(in_bytes
==NULL
|| out_header
==NULL
)
2189 out_header
->magic
= be32tohp(&ptr
);
2190 out_header
->endian
= be32tohp(&ptr
);
2191 out_header
->start
= be64tohp(&ptr
);
2192 out_header
->end
= be64tohp(&ptr
);
2193 out_header
->size
= be64tohp(&ptr
);
2194 out_header
->blocklist_header_size
= be32tohp(&ptr
);
2195 out_header
->checksum
= be32tohp(&ptr
);
2196 out_header
->journal_header_size
= be32tohp(&ptr
);
2198 return ((uint8_t*)ptr
- (uint8_t*)in_bytes
);
2203 #pragma mark Disk Access
2207 * hfslib_readd_with_extents()
2209 * This function reads the contents of a file from the volume, given an array
2210 * of extent descriptors which specify where every extent of the file is
2211 * located (in addition to the usual pread() arguments). out_bytes is presumed
2212 * to exist and be large enough to hold in_length number of bytes. Returns 0
2216 hfslib_readd_with_extents(
2219 uint64_t* out_bytesread
,
2222 hfs_extent_descriptor_t in_extents
[],
2223 uint16_t in_numextents
,
2224 hfs_callback_args
* cbargs
)
2226 uint64_t ext_length
, last_offset
;
2230 if(in_vol
==NULL
|| out_bytes
==NULL
|| in_extents
==NULL
|| in_numextents
==0
2231 || out_bytesread
==NULL
)
2237 for(i
=0; i
<in_numextents
; i
++)
2239 if(in_extents
[i
].block_count
==0)
2242 ext_length
= in_extents
[i
].block_count
* in_vol
->vh
.block_size
;
2244 if(in_offset
< last_offset
+ext_length
2245 && in_offset
+in_length
>= last_offset
)
2247 uint64_t isect_start
, isect_end
;
2249 isect_start
= max(in_offset
, last_offset
);
2250 isect_end
= min(in_offset
+in_length
, last_offset
+ext_length
);
2251 error
= hfslib_readd(in_vol
, out_bytes
, isect_end
-isect_start
,
2252 isect_start
- last_offset
+ (uint64_t)in_extents
[i
].start_block
2253 * in_vol
->vh
.block_size
, cbargs
);
2258 *out_bytesread
+= isect_end
-isect_start
;
2259 out_bytes
= (uint8_t*)out_bytes
+ isect_end
-isect_start
;
2262 last_offset
+= ext_length
;
2271 #pragma mark Callback Wrappers
2275 hfslib_error(const char* in_format
, const char* in_file
, int in_line
, ...)
2282 if(hfs_gcb
.error
!=NULL
)
2284 va_start(ap
, in_line
);
2286 hfs_gcb
.error(in_format
, in_file
, in_line
, ap
);
2293 hfslib_malloc(size_t size
, hfs_callback_args
* cbargs
)
2295 if(hfs_gcb
.allocmem
!=NULL
)
2296 return hfs_gcb
.allocmem(size
, cbargs
);
2302 hfslib_realloc(void* ptr
, size_t size
, hfs_callback_args
* cbargs
)
2304 if(hfs_gcb
.reallocmem
!=NULL
)
2305 return hfs_gcb
.reallocmem(ptr
, size
, cbargs
);
2311 hfslib_free(void* ptr
, hfs_callback_args
* cbargs
)
2313 if(hfs_gcb
.freemem
!=NULL
&& ptr
!=NULL
)
2314 hfs_gcb
.freemem(ptr
, cbargs
);
2318 hfslib_openvoldevice(
2320 const char* in_device
,
2321 hfs_callback_args
* cbargs
)
2323 if(hfs_gcb
.openvol
!=NULL
&& in_device
!=NULL
)
2324 return hfs_gcb
.openvol(in_vol
, in_device
, cbargs
);
2330 hfslib_closevoldevice(hfs_volume
* in_vol
, hfs_callback_args
* cbargs
)
2332 if(hfs_gcb
.closevol
!=NULL
)
2333 hfs_gcb
.closevol(in_vol
, cbargs
);
2342 hfs_callback_args
* cbargs
)
2344 if(in_vol
==NULL
|| out_bytes
==NULL
)
2347 if(hfs_gcb
.read
!=NULL
)
2348 return hfs_gcb
.read(in_vol
, out_bytes
, in_length
, in_offset
, cbargs
);
2358 /* returns key length */
2360 hfslib_make_catalog_key(
2361 hfs_cnid_t in_parent_cnid
,
2362 uint16_t in_name_len
,
2363 unichar_t
* in_unicode
,
2364 hfs_catalog_key_t
* out_key
)
2366 if(in_parent_cnid
==0 || (in_name_len
>0 && in_unicode
==NULL
) || out_key
==0)
2372 out_key
->key_len
= 6 + 2 * in_name_len
;
2373 out_key
->parent_cnid
= in_parent_cnid
;
2374 out_key
->name
.length
= in_name_len
;
2376 memcpy(&out_key
->name
.unicode
, in_unicode
, in_name_len
*2);
2378 return out_key
->key_len
;
2381 /* returns key length */
2383 hfslib_make_extent_key(
2385 uint8_t in_forktype
,
2386 uint32_t in_startblock
,
2387 hfs_extent_key_t
* out_key
)
2389 if(in_cnid
==0 || out_key
==0)
2392 out_key
->key_length
= HFS_MAX_EXT_KEY_LEN
;
2393 out_key
->fork_type
= in_forktype
;
2394 out_key
->padding
= 0;
2395 out_key
->file_cnid
= in_cnid
;
2396 out_key
->start_block
= in_startblock
;
2398 return out_key
->key_length
;
2403 hfslib_compare_catalog_keys_cf (
2407 const hfs_catalog_key_t
*a
, *b
;
2408 unichar_t ac
, bc
; /* current character from a, b */
2409 unichar_t lc
; /* lowercase version of current character */
2410 uint8_t apos
, bpos
; /* current character indices */
2412 a
= (const hfs_catalog_key_t
*)ap
;
2413 b
= (const hfs_catalog_key_t
*)bp
;
2415 if(a
->parent_cnid
!= b
->parent_cnid
)
2417 return (a
->parent_cnid
- b
->parent_cnid
);
2422 * The following code implements the pseudocode suggested by
2423 * the HFS+ technote.
2427 * XXX These need to be revised to be endian-independent!
2429 #define hbyte(x) ((x) >> 8)
2430 #define lbyte(x) ((x) & 0x00FF)
2435 /* get next valid character from a */
2436 for (lc
=0; lc
== 0 && apos
< a
->name
.length
; apos
++) {
2437 ac
= a
->name
.unicode
[apos
];
2438 lc
= hfs_gcft
[hbyte(ac
)];
2442 lc
= hfs_gcft
[lc
+ lbyte(ac
)];
2446 /* get next valid character from b */
2447 for (lc
=0; lc
== 0 && bpos
< b
->name
.length
; bpos
++) {
2448 bc
= b
->name
.unicode
[bpos
];
2449 lc
= hfs_gcft
[hbyte(bc
)];
2453 lc
= hfs_gcft
[lc
+ lbyte(bc
)];
2457 /* on end of string ac/bc are 0, otherwise > 0 */
2458 if (ac
!= bc
|| (ac
== 0 && bc
== 0))
2466 /* binary compare (i.e., not case folding) */
2468 hfslib_compare_catalog_keys_bc (
2472 if(((const hfs_catalog_key_t
*)a
)->parent_cnid
2473 == ((const hfs_catalog_key_t
*)b
)->parent_cnid
)
2475 if(((const hfs_catalog_key_t
*)a
)->name
.length
== 0 &&
2476 ((const hfs_catalog_key_t
*)b
)->name
.length
== 0)
2479 if(((const hfs_catalog_key_t
*)a
)->name
.length
== 0)
2481 if(((const hfs_catalog_key_t
*)b
)->name
.length
== 0)
2484 /* FIXME: This does a byte-per-byte comparison, whereas the HFS spec
2485 * mandates a uint16_t chunk comparison. */
2486 return memcmp(((const hfs_catalog_key_t
*)a
)->name
.unicode
,
2487 ((const hfs_catalog_key_t
*)b
)->name
.unicode
,
2488 min(((const hfs_catalog_key_t
*)a
)->name
.length
,
2489 ((const hfs_catalog_key_t
*)b
)->name
.length
));
2493 return (((const hfs_catalog_key_t
*)a
)->parent_cnid
-
2494 ((const hfs_catalog_key_t
*)b
)->parent_cnid
);
2499 hfslib_compare_extent_keys (
2504 * Comparison order, in descending importance:
2506 * CNID -> fork type -> start block
2509 if(((const hfs_extent_key_t
*)a
)->file_cnid
2510 == ((const hfs_extent_key_t
*)b
)->file_cnid
)
2512 if(((const hfs_extent_key_t
*)a
)->fork_type
2513 == ((const hfs_extent_key_t
*)b
)->fork_type
)
2515 if(((const hfs_extent_key_t
*)a
)->start_block
2516 == ((const hfs_extent_key_t
*)b
)->start_block
)
2522 return (((const hfs_extent_key_t
*)a
)->start_block
-
2523 ((const hfs_extent_key_t
*)b
)->start_block
);
2528 return (((const hfs_extent_key_t
*)a
)->fork_type
-
2529 ((const hfs_extent_key_t
*)b
)->fork_type
);
2534 return (((const hfs_extent_key_t
*)a
)->file_cnid
-
2535 ((const hfs_extent_key_t
*)b
)->file_cnid
);
2539 /* 1+10 tables of 16 rows and 16 columns, each 2 bytes wide = 5632 bytes */
2541 hfslib_create_casefolding_table(void)
2543 hfs_callback_args cbargs
;
2544 unichar_t
* t
; /* convenience */
2545 uint16_t s
; /* current subtable * 256 */
2546 uint16_t i
; /* current subtable index (0 to 255) */
2549 return 0; /* no sweat, table already exists */
2551 hfslib_init_cbargs(&cbargs
);
2552 hfs_gcft
= hfslib_malloc(5632, &cbargs
);
2554 HFS_LIBERR("could not allocate case folding table");
2556 t
= hfs_gcft
; /* easier to type :) */
2562 memset(t
, 0x00, 512);
2575 * table 1 (high byte 0x00)
2581 for(i
=65; i
<91; i
++)
2583 for(i
=91; i
<256; i
++)
2591 * table 2 (high byte 0x01)
2594 for(i
=0; i
<256; i
++)
2595 t
[s
+i
] = i
+ 0x0100;
2648 * table 3 (high byte 0x03)
2651 for(i
=0; i
<145; i
++)
2652 t
[s
+i
] = i
+ 0x0300;
2653 for(i
=145; i
<170; i
++)
2654 t
[s
+i
] = i
+ 0x0320;
2656 for(i
=170; i
<256; i
++)
2657 t
[s
+i
] = i
+ 0x0300;
2659 for(i
=226; i
<239; i
+=2)
2660 t
[s
+i
] = i
+ 0x0301;
2663 * table 4 (high byte 0x04)
2667 t
[s
+i
] = i
+ 0x0400;
2678 for(i
=16; i
<48; i
++)
2679 t
[s
+i
] = i
+ 0x0420;
2681 for(i
=48; i
<256; i
++)
2682 t
[s
+i
] = i
+ 0x0400;
2687 for(i
=96; i
<129; i
+=2)
2688 t
[s
+i
] = i
+ 0x0401;
2690 for(i
=144; i
<191; i
+=2)
2691 t
[s
+i
] = i
+ 0x0401;
2694 * table 5 (high byte 0x05)
2698 t
[s
+i
] = i
+ 0x0500;
2699 for(i
=49; i
<87; i
++)
2700 t
[s
+i
] = i
+ 0x0530;
2701 for(i
=87; i
<256; i
++)
2702 t
[s
+i
] = i
+ 0x0500;
2705 * table 6 (high byte 0x10)
2708 for(i
=0; i
<160; i
++)
2709 t
[s
+i
] = i
+ 0x1000;
2710 for(i
=160; i
<198; i
++)
2711 t
[s
+i
] = i
+ 0x1030;
2712 for(i
=198; i
<256; i
++)
2713 t
[s
+i
] = i
+ 0x1000;
2716 * table 7 (high byte 0x20)
2719 for(i
=0; i
<256; i
++)
2720 t
[s
+i
] = i
+ 0x2000;
2722 uint8_t zi
[15] = { 12, 13, 14, 15,
2724 106, 107, 108, 109, 110, 111};
2727 t
[s
+zi
[i
]] = 0x0000;
2731 * table 8 (high byte 0x21)
2735 t
[s
+i
] = i
+ 0x2100;
2736 for(i
=96; i
<112; i
++)
2737 t
[s
+i
] = i
+ 0x2110;
2738 for(i
=112; i
<256; i
++)
2739 t
[s
+i
] = i
+ 0x2100;
2742 * table 9 (high byte 0xFE)
2745 for(i
=0; i
<256; i
++)
2746 t
[s
+i
] = i
+ 0xFE00;
2750 * table 10 (high byte 0xFF)
2754 t
[s
+i
] = i
+ 0xFF00;
2755 for(i
=33; i
<59; i
++)
2756 t
[s
+i
] = i
+ 0xFF20;
2757 for(i
=59; i
<256; i
++)
2758 t
[s
+i
] = i
+ 0xFF00;
2767 hfslib_get_hardlink(hfs_volume
*vol
, uint32_t inode_num
,
2768 hfs_catalog_keyed_record_t
*rec
,
2769 hfs_callback_args
*cbargs
)
2771 hfs_catalog_keyed_record_t metadata
;
2772 hfs_catalog_key_t key
;
2774 unichar_t name_uni
[16];
2777 /* XXX: cache this */
2778 if (hfslib_find_catalog_record_with_key(vol
,
2779 &hfs_gMetadataDirectoryKey
,
2780 &metadata
, cbargs
) != 0
2781 || metadata
.type
!= HFS_REC_FLDR
)
2784 len
= snprintf(name
, sizeof(name
), "iNode%d", inode_num
);
2785 for (i
=0; i
<len
; i
++)
2786 name_uni
[i
] = name
[i
];
2788 if (hfslib_make_catalog_key(metadata
.folder
.cnid
, len
, name_uni
,
2792 return hfslib_find_catalog_record_with_key(vol
, &key
, rec
, cbargs
);