2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * This file implements endian swapping routines for the HFS/HFS Plus
36 #include "hfs_endian.h"
38 #include "hfscommon/headers/BTreesPrivate.h"
43 * Internal swapping routines
45 * These routines handle swapping the records of leaf and index nodes. The
46 * layout of the keys and records varies depending on the kind of B-tree
47 * (determined by fileID).
49 * The direction parameter must be kSwapBTNodeBigToHost or kSwapBTNodeHostToBig.
50 * The kSwapBTNodeHeaderRecordOnly "direction" is not valid for these routines.
52 int hfs_swap_HFSPlusBTInternalNode (BlockDescriptor
*src
, HFSCatalogNodeID fileID
, enum HFSBTSwapDirection direction
);
53 int hfs_swap_HFSBTInternalNode (BlockDescriptor
*src
, HFSCatalogNodeID fileID
, enum HFSBTSwapDirection direction
);
54 void hfs_swap_HFSPlusForkData (HFSPlusForkData
*src
);
57 * hfs_swap_HFSPlusForkData
60 hfs_swap_HFSPlusForkData (
66 src
->logicalSize
= SWAP_BE64 (src
->logicalSize
);
68 src
->clumpSize
= SWAP_BE32 (src
->clumpSize
);
69 src
->totalBlocks
= SWAP_BE32 (src
->totalBlocks
);
71 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
72 src
->extents
[i
].startBlock
= SWAP_BE32 (src
->extents
[i
].startBlock
);
73 src
->extents
[i
].blockCount
= SWAP_BE32 (src
->extents
[i
].blockCount
);
80 * NOTE: This operation is not naturally symmetric.
81 * We have to determine which way we're swapping things.
87 enum HFSBTSwapDirection direction
,
88 u_int8_t allow_empty_node
91 BTNodeDescriptor
*srcDesc
= src
->buffer
;
92 u_int16_t
*srcOffs
= NULL
;
93 BTreeControlBlockPtr btcb
= (BTreeControlBlockPtr
)VTOF(vp
)->fcbBTCBPtr
;
94 u_int16_t i
; /* index to match srcDesc->numRecords */
98 if (direction
== kSwapBTNodeBigToHost
) {
99 printf ("hfs: BE -> Native Swap\n");
100 } else if (direction
== kSwapBTNodeHostToBig
) {
101 printf ("hfs: Native -> BE Swap\n");
102 } else if (direction
== kSwapBTNodeHeaderRecordOnly
) {
103 printf ("hfs: Not swapping descriptors\n");
105 panic ("hfs_swap_BTNode: This is impossible");
110 * If we are doing a swap from on-disk to in-memory, then swap the node
111 * descriptor and record offsets before we need to use them.
113 if (direction
== kSwapBTNodeBigToHost
) {
114 srcDesc
->fLink
= SWAP_BE32 (srcDesc
->fLink
);
115 srcDesc
->bLink
= SWAP_BE32 (srcDesc
->bLink
);
118 * When first opening a BTree, we have to read the header node before the
119 * control block is initialized. In this case, totalNodes will be zero,
120 * so skip the bounds checking. Also, we should ignore the header node when
121 * checking for invalid forwards and backwards links, since the header node's
122 * links can point back to itself legitimately.
124 if (btcb
->totalNodes
!= 0) {
125 if (srcDesc
->fLink
>= btcb
->totalNodes
) {
126 printf("hfs_swap_BTNode: invalid forward link (0x%08x >= 0x%08x)\n", srcDesc
->fLink
, btcb
->totalNodes
);
127 error
= fsBTInvalidHeaderErr
;
130 if (srcDesc
->bLink
>= btcb
->totalNodes
) {
131 printf("hfs_swap_BTNode: invalid backward link (0x%08x >= 0x%08x)\n", srcDesc
->bLink
, btcb
->totalNodes
);
132 error
= fsBTInvalidHeaderErr
;
136 if ((src
->blockNum
!= 0) && (srcDesc
->fLink
== (u_int32_t
) src
->blockNum
)) {
137 printf("hfs_swap_BTNode: invalid forward link (0x%08x == 0x%08x)\n",
138 srcDesc
->fLink
, (u_int32_t
) src
->blockNum
);
139 error
= fsBTInvalidHeaderErr
;
142 if ((src
->blockNum
!= 0) && (srcDesc
->bLink
== (u_int32_t
) src
->blockNum
)) {
143 printf("hfs_swap_BTNode: invalid backward link (0x%08x == 0x%08x)\n",
144 srcDesc
->bLink
, (u_int32_t
) src
->blockNum
);
145 error
= fsBTInvalidHeaderErr
;
153 * Check srcDesc->kind. Don't swap it because it's only one byte.
155 if (srcDesc
->kind
< kBTLeafNode
|| srcDesc
->kind
> kBTMapNode
) {
156 printf("hfs_swap_BTNode: invalid node kind (%d)\n", srcDesc
->kind
);
157 error
= fsBTInvalidHeaderErr
;
162 * Check srcDesc->height. Don't swap it because it's only one byte.
164 if (srcDesc
->height
> kMaxTreeDepth
) {
165 printf("hfs_swap_BTNode: invalid node height (%d)\n", srcDesc
->height
);
166 error
= fsBTInvalidHeaderErr
;
170 /* Don't swap srcDesc->reserved */
172 srcDesc
->numRecords
= SWAP_BE16 (srcDesc
->numRecords
);
175 * Swap the node offsets (including the free space one!).
177 srcOffs
= (u_int16_t
*)((char *)src
->buffer
+ (src
->blockSize
- ((srcDesc
->numRecords
+ 1) * sizeof (u_int16_t
))));
180 * Sanity check that the record offsets are within the node itself.
182 if ((char *)srcOffs
> ((char *)src
->buffer
+ src
->blockSize
) ||
183 (char *)srcOffs
< ((char *)src
->buffer
+ sizeof(BTNodeDescriptor
))) {
184 printf("hfs_swap_BTNode: invalid record count (0x%04X)\n", srcDesc
->numRecords
);
185 error
= fsBTInvalidHeaderErr
;
190 * Swap and sanity check each of the record offsets.
192 for (i
= 0; i
<= srcDesc
->numRecords
; i
++) {
193 srcOffs
[i
] = SWAP_BE16 (srcOffs
[i
]);
196 * Sanity check: must be even, and within the node itself.
198 * We may be called to swap an unused node, which contains all zeroes.
199 * Unused nodes are expected only when allow_empty_node is true.
200 * If it is false and record offset is zero, return error.
202 if ((srcOffs
[i
] & 1) || (
203 (allow_empty_node
== false) && (srcOffs
[i
] == 0)) ||
204 (srcOffs
[i
] < sizeof(BTNodeDescriptor
) && srcOffs
[i
] != 0) ||
205 (srcOffs
[i
] >= src
->blockSize
)) {
206 printf("hfs_swap_BTNode: record #%d invalid offset (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
207 error
= fsBTInvalidHeaderErr
;
212 * Make sure the offsets are strictly increasing. Note that we're looping over
213 * them backwards, hence the order in the comparison.
215 if ((i
!= 0) && (srcOffs
[i
] >= srcOffs
[i
-1])) {
216 printf("hfs_swap_BTNode: offsets %d and %d out of order (0x%04X, 0x%04X)\n",
217 srcDesc
->numRecords
-i
-1, srcDesc
->numRecords
-i
, srcOffs
[i
], srcOffs
[i
-1]);
218 error
= fsBTInvalidHeaderErr
;
225 * Swap the records (ordered by frequency of access)
227 if ((srcDesc
->kind
== kBTIndexNode
) ||
228 (srcDesc
-> kind
== kBTLeafNode
)) {
230 if (VTOVCB(vp
)->vcbSigWord
== kHFSPlusSigWord
) {
231 error
= hfs_swap_HFSPlusBTInternalNode (src
, VTOC(vp
)->c_fileid
, direction
);
233 error
= hfs_swap_HFSBTInternalNode (src
, VTOC(vp
)->c_fileid
, direction
);
236 if (error
) goto fail
;
238 } else if (srcDesc
-> kind
== kBTMapNode
) {
239 /* Don't swap the bitmaps, they'll be done in the bitmap routines */
241 } else if (srcDesc
-> kind
== kBTHeaderNode
) {
242 /* The header's offset is hard-wired because we cannot trust the offset pointers. */
243 BTHeaderRec
*srcHead
= (BTHeaderRec
*)((char *)src
->buffer
+ sizeof(BTNodeDescriptor
));
245 srcHead
->treeDepth
= SWAP_BE16 (srcHead
->treeDepth
);
247 srcHead
->rootNode
= SWAP_BE32 (srcHead
->rootNode
);
248 srcHead
->leafRecords
= SWAP_BE32 (srcHead
->leafRecords
);
249 srcHead
->firstLeafNode
= SWAP_BE32 (srcHead
->firstLeafNode
);
250 srcHead
->lastLeafNode
= SWAP_BE32 (srcHead
->lastLeafNode
);
252 srcHead
->nodeSize
= SWAP_BE16 (srcHead
->nodeSize
);
253 srcHead
->maxKeyLength
= SWAP_BE16 (srcHead
->maxKeyLength
);
255 srcHead
->totalNodes
= SWAP_BE32 (srcHead
->totalNodes
);
256 srcHead
->freeNodes
= SWAP_BE32 (srcHead
->freeNodes
);
258 srcHead
->clumpSize
= SWAP_BE32 (srcHead
->clumpSize
);
259 srcHead
->attributes
= SWAP_BE32 (srcHead
->attributes
);
261 /* Don't swap srcHead->reserved1 */
262 /* Don't swap srcHead->btreeType; it's only one byte */
263 /* Don't swap srcHead->reserved2 */
264 /* Don't swap srcHead->reserved3 */
265 /* Don't swap bitmap */
269 * If we are doing a swap from in-memory to on-disk, then swap the node
270 * descriptor and record offsets after we're done using them.
272 if (direction
== kSwapBTNodeHostToBig
) {
274 * Sanity check and swap the forward and backward links.
275 * Ignore the header node since its forward and backwards links can legitimately
278 if (srcDesc
->fLink
>= btcb
->totalNodes
) {
279 panic("hfs_UNswap_BTNode: invalid forward link (0x%08X)\n", srcDesc
->fLink
);
280 error
= fsBTInvalidHeaderErr
;
283 if ((src
->blockNum
!= 0) && (srcDesc
->fLink
== (u_int32_t
) src
->blockNum
)) {
284 panic ("hfs_UNswap_BTNode: invalid forward link (0x%08x == 0x%08x)\n",
285 srcDesc
->fLink
, (u_int32_t
) src
->blockNum
);
286 error
= fsBTInvalidHeaderErr
;
290 if (srcDesc
->bLink
>= btcb
->totalNodes
) {
291 panic("hfs_UNswap_BTNode: invalid backward link (0x%08X)\n", srcDesc
->bLink
);
292 error
= fsBTInvalidHeaderErr
;
295 if ((src
->blockNum
!= 0) && (srcDesc
->bLink
== (u_int32_t
) src
->blockNum
)) {
296 panic ("hfs_UNswap_BTNode: invalid backward link (0x%08x == 0x%08x)\n",
297 srcDesc
->bLink
, (u_int32_t
) src
->blockNum
);
298 error
= fsBTInvalidHeaderErr
;
303 srcDesc
->fLink
= SWAP_BE32 (srcDesc
->fLink
);
304 srcDesc
->bLink
= SWAP_BE32 (srcDesc
->bLink
);
307 * Check srcDesc->kind. Don't swap it because it's only one byte.
309 if (srcDesc
->kind
< kBTLeafNode
|| srcDesc
->kind
> kBTMapNode
) {
310 panic("hfs_UNswap_BTNode: invalid node kind (%d)\n", srcDesc
->kind
);
311 error
= fsBTInvalidHeaderErr
;
316 * Check srcDesc->height. Don't swap it because it's only one byte.
318 if (srcDesc
->height
> kMaxTreeDepth
) {
319 panic("hfs_UNswap_BTNode: invalid node height (%d)\n", srcDesc
->height
);
320 error
= fsBTInvalidHeaderErr
;
324 /* Don't swap srcDesc->reserved */
327 * Swap the node offsets (including the free space one!).
329 srcOffs
= (u_int16_t
*)((char *)src
->buffer
+ (src
->blockSize
- ((srcDesc
->numRecords
+ 1) * sizeof (u_int16_t
))));
332 * Sanity check that the record offsets are within the node itself.
334 if ((char *)srcOffs
> ((char *)src
->buffer
+ src
->blockSize
) ||
335 (char *)srcOffs
< ((char *)src
->buffer
+ sizeof(BTNodeDescriptor
))) {
336 panic("hfs_UNswap_BTNode: invalid record count (0x%04X)\n", srcDesc
->numRecords
);
337 error
= fsBTInvalidHeaderErr
;
342 * Swap and sanity check each of the record offsets.
344 for (i
= 0; i
<= srcDesc
->numRecords
; i
++) {
346 * Sanity check: must be even, and within the node itself.
348 * We may be called to swap an unused node, which contains all zeroes.
349 * This can happen when the last record from a node gets deleted.
350 * This is why we allow the record offset to be zero.
351 * Unused nodes are expected only when allow_empty_node is true
352 * (the caller should set it to true for kSwapBTNodeBigToHost).
354 if ((srcOffs
[i
] & 1) ||
355 ((allow_empty_node
== false) && (srcOffs
[i
] == 0)) ||
356 (srcOffs
[i
] < sizeof(BTNodeDescriptor
) && srcOffs
[i
] != 0) ||
357 (srcOffs
[i
] >= src
->blockSize
)) {
358 panic("hfs_UNswap_BTNode: record #%d invalid offset (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
359 error
= fsBTInvalidHeaderErr
;
364 * Make sure the offsets are strictly increasing. Note that we're looping over
365 * them backwards, hence the order in the comparison.
367 if ((i
< srcDesc
->numRecords
) && (srcOffs
[i
+1] >= srcOffs
[i
])) {
368 panic("hfs_UNswap_BTNode: offsets %d and %d out of order (0x%04X, 0x%04X)\n",
369 srcDesc
->numRecords
-i
-2, srcDesc
->numRecords
-i
-1, srcOffs
[i
+1], srcOffs
[i
]);
370 error
= fsBTInvalidHeaderErr
;
374 srcOffs
[i
] = SWAP_BE16 (srcOffs
[i
]);
377 srcDesc
->numRecords
= SWAP_BE16 (srcDesc
->numRecords
);
383 * Log some useful information about where the corrupt node is.
385 printf("hfs: node=%lld fileID=%u volume=%s device=%s\n", src
->blockNum
, VTOC(vp
)->c_fileid
,
386 VTOVCB(vp
)->vcbVN
, vfs_statfs(vnode_mount(vp
))->f_mntfromname
);
387 hfs_mark_volume_inconsistent(VTOVCB(vp
));
394 hfs_swap_HFSPlusBTInternalNode (
395 BlockDescriptor
*src
,
396 HFSCatalogNodeID fileID
,
397 enum HFSBTSwapDirection direction
400 BTNodeDescriptor
*srcDesc
= src
->buffer
;
401 u_int16_t
*srcOffs
= (u_int16_t
*)((char *)src
->buffer
+ (src
->blockSize
- (srcDesc
->numRecords
* sizeof (u_int16_t
))));
402 char *nextRecord
; /* Points to start of record following current one */
405 * i is an int32 because it needs to be negative to index the offset to free space.
406 * srcDesc->numRecords is a u_int16_t and is unlikely to become 32-bit so this should be ok.
412 if (fileID
== kHFSExtentsFileID
) {
413 HFSPlusExtentKey
*srcKey
;
414 HFSPlusExtentDescriptor
*srcRec
;
415 size_t recordSize
; /* Size of the data part of the record, or node number for index nodes */
417 if (srcDesc
->kind
== kBTIndexNode
)
418 recordSize
= sizeof(u_int32_t
);
420 recordSize
= sizeof(HFSPlusExtentDescriptor
);
422 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
423 /* Point to the start of the record we're currently checking. */
424 srcKey
= (HFSPlusExtentKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
427 * Point to start of next (larger offset) record. We'll use this
428 * to be sure the current record doesn't overflow into the next
431 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
434 * Make sure the key and data are within the buffer. Since both key
435 * and data are fixed size, this is relatively easy. Note that this
436 * relies on the keyLength being a constant; we verify the keyLength
439 if ((char *)srcKey
+ sizeof(HFSPlusExtentKey
) + recordSize
> nextRecord
) {
440 if (direction
== kSwapBTNodeHostToBig
) {
441 panic("hfs_swap_HFSPlusBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
443 printf("hfs_swap_HFSPlusBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
445 return fsBTInvalidNodeErr
;
448 if (direction
== kSwapBTNodeBigToHost
)
449 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
450 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
451 if (direction
== kSwapBTNodeHostToBig
) {
452 panic("hfs_swap_HFSPlusBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
454 printf("hfs_swap_HFSPlusBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
456 return fsBTInvalidNodeErr
;
458 srcRec
= (HFSPlusExtentDescriptor
*)((char *)srcKey
+ srcKey
->keyLength
+ sizeof(srcKey
->keyLength
));
459 if (direction
== kSwapBTNodeHostToBig
)
460 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
462 /* Don't swap srcKey->forkType; it's only one byte */
463 /* Don't swap srcKey->pad */
465 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
466 srcKey
->startBlock
= SWAP_BE32 (srcKey
->startBlock
);
468 if (srcDesc
->kind
== kBTIndexNode
) {
469 /* For index nodes, the record data is just a child node number. */
470 *((u_int32_t
*)srcRec
) = SWAP_BE32 (*((u_int32_t
*)srcRec
));
472 /* Swap the extent data */
473 for (j
= 0; j
< kHFSPlusExtentDensity
; j
++) {
474 srcRec
[j
].startBlock
= SWAP_BE32 (srcRec
[j
].startBlock
);
475 srcRec
[j
].blockCount
= SWAP_BE32 (srcRec
[j
].blockCount
);
480 } else if (fileID
== kHFSCatalogFileID
) {
481 HFSPlusCatalogKey
*srcKey
;
485 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
486 /* Point to the start of the record we're currently checking. */
487 srcKey
= (HFSPlusCatalogKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
490 * Point to start of next (larger offset) record. We'll use this
491 * to be sure the current record doesn't overflow into the next
494 nextRecord
= (char *)src
->buffer
+ (uintptr_t)(srcOffs
[i
-1]);
497 * Make sure we can safely dereference the keyLength and parentID fields.
499 if ((char *)srcKey
+ offsetof(HFSPlusCatalogKey
, nodeName
.unicode
[0]) > nextRecord
) {
500 if (direction
== kSwapBTNodeHostToBig
) {
501 panic("hfs_swap_HFSPlusBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
503 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
505 return fsBTInvalidNodeErr
;
509 * Swap and sanity check the key length
511 if (direction
== kSwapBTNodeBigToHost
)
512 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
513 keyLength
= srcKey
->keyLength
; /* Put it in a local (native order) because we use it several times */
514 if (direction
== kSwapBTNodeHostToBig
)
515 srcKey
->keyLength
= SWAP_BE16 (keyLength
);
517 /* Sanity check the key length */
518 if (keyLength
< kHFSPlusCatalogKeyMinimumLength
|| keyLength
> kHFSPlusCatalogKeyMaximumLength
) {
519 if (direction
== kSwapBTNodeHostToBig
) {
520 panic("hfs_swap_HFSPlusBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
522 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
524 return fsBTInvalidNodeErr
;
528 * Make sure that we can safely dereference the record's type field or
529 * an index node's child node number.
531 srcPtr
= (int16_t *)((char *)srcKey
+ keyLength
+ sizeof(srcKey
->keyLength
));
532 if ((char *)srcPtr
+ sizeof(u_int32_t
) > nextRecord
) {
533 if (direction
== kSwapBTNodeHostToBig
) {
534 panic("hfs_swap_HFSPlusBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
536 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
538 return fsBTInvalidNodeErr
;
541 srcKey
->parentID
= SWAP_BE32 (srcKey
->parentID
);
544 * Swap and sanity check the key's node name
546 if (direction
== kSwapBTNodeBigToHost
)
547 srcKey
->nodeName
.length
= SWAP_BE16 (srcKey
->nodeName
.length
);
548 /* Make sure name length is consistent with key length */
549 if (keyLength
< sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
550 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0])) {
551 if (direction
== kSwapBTNodeHostToBig
) {
552 panic("hfs_swap_HFSPlusBTInternalNode: catalog record #%d keyLength=%d expected=%lu\n",
553 srcDesc
->numRecords
-i
, keyLength
, sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
554 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0]));
556 printf("hfs_swap_HFSPlusBTInternalNode: catalog record #%d keyLength=%d expected=%lu\n",
557 srcDesc
->numRecords
-i
, keyLength
, sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
558 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0]));
560 return fsBTInvalidNodeErr
;
562 for (j
= 0; j
< srcKey
->nodeName
.length
; j
++) {
563 srcKey
->nodeName
.unicode
[j
] = SWAP_BE16 (srcKey
->nodeName
.unicode
[j
]);
565 if (direction
== kSwapBTNodeHostToBig
)
566 srcKey
->nodeName
.length
= SWAP_BE16 (srcKey
->nodeName
.length
);
569 * For index nodes, the record data is just the child's node number.
570 * Skip over swapping the various types of catalog record.
572 if (srcDesc
->kind
== kBTIndexNode
) {
573 *((u_int32_t
*)srcPtr
) = SWAP_BE32 (*((u_int32_t
*)srcPtr
));
577 /* Make sure the recordType is in native order before using it. */
578 if (direction
== kSwapBTNodeBigToHost
)
579 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
581 if (srcPtr
[0] == kHFSPlusFolderRecord
) {
582 HFSPlusCatalogFolder
*srcRec
= (HFSPlusCatalogFolder
*)srcPtr
;
583 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
584 if (direction
== kSwapBTNodeHostToBig
) {
585 panic("hfs_swap_HFSPlusBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
587 printf("hfs_swap_HFSPlusBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
589 return fsBTInvalidNodeErr
;
592 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
593 srcRec
->valence
= SWAP_BE32 (srcRec
->valence
);
594 srcRec
->folderID
= SWAP_BE32 (srcRec
->folderID
);
595 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
596 srcRec
->contentModDate
= SWAP_BE32 (srcRec
->contentModDate
);
597 srcRec
->attributeModDate
= SWAP_BE32 (srcRec
->attributeModDate
);
598 srcRec
->accessDate
= SWAP_BE32 (srcRec
->accessDate
);
599 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
601 srcRec
->bsdInfo
.ownerID
= SWAP_BE32 (srcRec
->bsdInfo
.ownerID
);
602 srcRec
->bsdInfo
.groupID
= SWAP_BE32 (srcRec
->bsdInfo
.groupID
);
604 /* Don't swap srcRec->bsdInfo.adminFlags; it's only one byte */
605 /* Don't swap srcRec->bsdInfo.ownerFlags; it's only one byte */
607 srcRec
->bsdInfo
.fileMode
= SWAP_BE16 (srcRec
->bsdInfo
.fileMode
);
608 srcRec
->bsdInfo
.special
.iNodeNum
= SWAP_BE32 (srcRec
->bsdInfo
.special
.iNodeNum
);
610 srcRec
->textEncoding
= SWAP_BE32 (srcRec
->textEncoding
);
612 /* Don't swap srcRec->userInfo */
613 /* Don't swap srcRec->finderInfo */
614 srcRec
->folderCount
= SWAP_BE32 (srcRec
->folderCount
);
616 } else if (srcPtr
[0] == kHFSPlusFileRecord
) {
617 HFSPlusCatalogFile
*srcRec
= (HFSPlusCatalogFile
*)srcPtr
;
618 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
619 if (direction
== kSwapBTNodeHostToBig
) {
620 panic("hfs_swap_HFSPlusBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
622 printf("hfs_swap_HFSPlusBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
624 return fsBTInvalidNodeErr
;
627 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
629 srcRec
->fileID
= SWAP_BE32 (srcRec
->fileID
);
631 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
632 srcRec
->contentModDate
= SWAP_BE32 (srcRec
->contentModDate
);
633 srcRec
->attributeModDate
= SWAP_BE32 (srcRec
->attributeModDate
);
634 srcRec
->accessDate
= SWAP_BE32 (srcRec
->accessDate
);
635 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
637 srcRec
->bsdInfo
.ownerID
= SWAP_BE32 (srcRec
->bsdInfo
.ownerID
);
638 srcRec
->bsdInfo
.groupID
= SWAP_BE32 (srcRec
->bsdInfo
.groupID
);
640 /* Don't swap srcRec->bsdInfo.adminFlags; it's only one byte */
641 /* Don't swap srcRec->bsdInfo.ownerFlags; it's only one byte */
643 srcRec
->bsdInfo
.fileMode
= SWAP_BE16 (srcRec
->bsdInfo
.fileMode
);
644 srcRec
->bsdInfo
.special
.iNodeNum
= SWAP_BE32 (srcRec
->bsdInfo
.special
.iNodeNum
);
646 srcRec
->textEncoding
= SWAP_BE32 (srcRec
->textEncoding
);
648 /* If kHFSHasLinkChainBit is set, reserved1 is hl_FirstLinkID.
649 * In all other context, it is expected to be zero.
651 srcRec
->reserved1
= SWAP_BE32 (srcRec
->reserved1
);
653 /* Don't swap srcRec->userInfo */
654 /* Don't swap srcRec->finderInfo */
655 /* Don't swap srcRec->reserved2 */
657 hfs_swap_HFSPlusForkData (&srcRec
->dataFork
);
658 hfs_swap_HFSPlusForkData (&srcRec
->resourceFork
);
660 } else if ((srcPtr
[0] == kHFSPlusFolderThreadRecord
) ||
661 (srcPtr
[0] == kHFSPlusFileThreadRecord
)) {
664 * Make sure there is room for parentID and name length.
666 HFSPlusCatalogThread
*srcRec
= (HFSPlusCatalogThread
*)srcPtr
;
667 if ((char *) &srcRec
->nodeName
.unicode
[0] > nextRecord
) {
668 if (direction
== kSwapBTNodeHostToBig
) {
669 panic("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
671 printf("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
673 return fsBTInvalidNodeErr
;
676 /* Don't swap srcRec->reserved */
678 srcRec
->parentID
= SWAP_BE32 (srcRec
->parentID
);
680 if (direction
== kSwapBTNodeBigToHost
)
681 srcRec
->nodeName
.length
= SWAP_BE16 (srcRec
->nodeName
.length
);
684 * Make sure there is room for the name in the buffer.
685 * Then swap the characters of the name itself.
687 if ((char *) &srcRec
->nodeName
.unicode
[srcRec
->nodeName
.length
] > nextRecord
) {
688 if (direction
== kSwapBTNodeHostToBig
) {
689 panic("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
691 printf("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
693 return fsBTInvalidNodeErr
;
695 for (j
= 0; j
< srcRec
->nodeName
.length
; j
++) {
696 srcRec
->nodeName
.unicode
[j
] = SWAP_BE16 (srcRec
->nodeName
.unicode
[j
]);
699 if (direction
== kSwapBTNodeHostToBig
)
700 srcRec
->nodeName
.length
= SWAP_BE16 (srcRec
->nodeName
.length
);
703 if (direction
== kSwapBTNodeHostToBig
) {
704 panic("hfs_swap_HFSPlusBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
706 printf("hfs_swap_HFSPlusBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
708 return fsBTInvalidNodeErr
;
711 /* We can swap the record type now that we're done using it. */
712 if (direction
== kSwapBTNodeHostToBig
)
713 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
716 } else if (fileID
== kHFSAttributesFileID
) {
717 HFSPlusAttrKey
*srcKey
;
718 HFSPlusAttrRecord
*srcRec
;
720 u_int32_t attrSize
= 0;
722 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
723 /* Point to the start of the record we're currently checking. */
724 srcKey
= (HFSPlusAttrKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
727 * Point to start of next (larger offset) record. We'll use this
728 * to be sure the current record doesn't overflow into the next
731 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
733 /* Make sure there is room in the buffer for a minimal key */
734 if ((char *) &srcKey
->attrName
[1] > nextRecord
) {
735 if (direction
== kSwapBTNodeHostToBig
) {
736 panic("hfs_swap_HFSPlusBTInternalNode: attr key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
738 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
740 return fsBTInvalidNodeErr
;
743 /* Swap the key length field */
744 if (direction
== kSwapBTNodeBigToHost
)
745 srcKey
->keyLength
= SWAP_BE16(srcKey
->keyLength
);
746 keyLength
= srcKey
->keyLength
; /* Keep a copy in native order */
747 if (direction
== kSwapBTNodeHostToBig
)
748 srcKey
->keyLength
= SWAP_BE16(srcKey
->keyLength
);
751 * Make sure that we can safely dereference the record's type field or
752 * an index node's child node number.
754 srcRec
= (HFSPlusAttrRecord
*)((char *)srcKey
+ keyLength
+ sizeof(srcKey
->keyLength
));
755 if ((char *)srcRec
+ sizeof(u_int32_t
) > nextRecord
) {
756 if (direction
== kSwapBTNodeHostToBig
) {
757 panic("hfs_swap_HFSPlusBTInternalNode: attr key #%d too big (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
759 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d too big (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
761 return fsBTInvalidNodeErr
;
764 srcKey
->fileID
= SWAP_BE32(srcKey
->fileID
);
765 srcKey
->startBlock
= SWAP_BE32(srcKey
->startBlock
);
768 * Swap and check the attribute name
770 if (direction
== kSwapBTNodeBigToHost
)
771 srcKey
->attrNameLen
= SWAP_BE16(srcKey
->attrNameLen
);
772 /* Sanity check the attribute name length */
773 if (srcKey
->attrNameLen
> kHFSMaxAttrNameLen
|| keyLength
< (kHFSPlusAttrKeyMinimumLength
+ sizeof(u_int16_t
)*srcKey
->attrNameLen
)) {
774 if (direction
== kSwapBTNodeHostToBig
) {
775 panic("hfs_swap_HFSPlusBTInternalNode: attr key #%d keyLength=%d attrNameLen=%d\n", srcDesc
->numRecords
-i
-1, keyLength
, srcKey
->attrNameLen
);
777 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d keyLength=%d attrNameLen=%d\n", srcDesc
->numRecords
-i
-1, keyLength
, srcKey
->attrNameLen
);
779 return fsBTInvalidNodeErr
;
781 for (j
= 0; j
< srcKey
->attrNameLen
; j
++)
782 srcKey
->attrName
[j
] = SWAP_BE16(srcKey
->attrName
[j
]);
783 if (direction
== kSwapBTNodeHostToBig
)
784 srcKey
->attrNameLen
= SWAP_BE16(srcKey
->attrNameLen
);
787 * For index nodes, the record data is just the child's node number.
788 * Skip over swapping the various types of attribute record.
790 if (srcDesc
->kind
== kBTIndexNode
) {
791 *((u_int32_t
*)srcRec
) = SWAP_BE32 (*((u_int32_t
*)srcRec
));
795 /* Swap the record data */
796 if (direction
== kSwapBTNodeBigToHost
)
797 srcRec
->recordType
= SWAP_BE32(srcRec
->recordType
);
798 switch (srcRec
->recordType
) {
799 case kHFSPlusAttrInlineData
:
800 /* Is there room for the inline data header? */
801 if ((char *) &srcRec
->attrData
.attrData
[0] > nextRecord
) {
802 if (direction
== kSwapBTNodeHostToBig
) {
803 panic("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big\n", srcDesc
->numRecords
-i
-1);
805 printf("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big\n", srcDesc
->numRecords
-i
-1);
807 return fsBTInvalidNodeErr
;
810 /* We're not swapping the reserved fields */
812 /* Swap the attribute size */
813 if (direction
== kSwapBTNodeHostToBig
)
814 attrSize
= srcRec
->attrData
.attrSize
;
815 srcRec
->attrData
.attrSize
= SWAP_BE32(srcRec
->attrData
.attrSize
);
816 if (direction
== kSwapBTNodeBigToHost
)
817 attrSize
= srcRec
->attrData
.attrSize
;
819 /* Is there room for the inline attribute data? */
820 if ((char *) &srcRec
->attrData
.attrData
[attrSize
] > nextRecord
) {
821 if (direction
== kSwapBTNodeHostToBig
) {
822 panic("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big (attrSize=%u)\n", srcDesc
->numRecords
-i
-1, attrSize
);
824 printf("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big (attrSize=%u)\n", srcDesc
->numRecords
-i
-1, attrSize
);
826 return fsBTInvalidNodeErr
;
829 /* Not swapping the attribute data itself */
832 case kHFSPlusAttrForkData
:
833 /* Is there room for the fork data record? */
834 if ((char *)srcRec
+ sizeof(HFSPlusAttrForkData
) > nextRecord
) {
835 if (direction
== kSwapBTNodeHostToBig
) {
836 panic("hfs_swap_HFSPlusBTInternalNode: attr fork data #%d too big\n", srcDesc
->numRecords
-i
-1);
838 printf("hfs_swap_HFSPlusBTInternalNode: attr fork data #%d too big\n", srcDesc
->numRecords
-i
-1);
840 return fsBTInvalidNodeErr
;
843 /* We're not swapping the reserved field */
845 hfs_swap_HFSPlusForkData(&srcRec
->forkData
.theFork
);
848 case kHFSPlusAttrExtents
:
849 /* Is there room for an extent record? */
850 if ((char *)srcRec
+ sizeof(HFSPlusAttrExtents
) > nextRecord
) {
851 if (direction
== kSwapBTNodeHostToBig
) {
852 panic("hfs_swap_HFSPlusBTInternalNode: attr extents #%d too big\n", srcDesc
->numRecords
-i
-1);
854 printf("hfs_swap_HFSPlusBTInternalNode: attr extents #%d too big\n", srcDesc
->numRecords
-i
-1);
856 return fsBTInvalidNodeErr
;
859 /* We're not swapping the reserved field */
861 for (j
= 0; j
< kHFSPlusExtentDensity
; j
++) {
862 srcRec
->overflowExtents
.extents
[j
].startBlock
=
863 SWAP_BE32(srcRec
->overflowExtents
.extents
[j
].startBlock
);
864 srcRec
->overflowExtents
.extents
[j
].blockCount
=
865 SWAP_BE32(srcRec
->overflowExtents
.extents
[j
].blockCount
);
869 if (direction
== kSwapBTNodeHostToBig
)
870 srcRec
->recordType
= SWAP_BE32(srcRec
->recordType
);
872 } else if (fileID
> kHFSFirstUserCatalogNodeID
) {
873 /* The only B-tree with a non-system CNID that we use is the hotfile B-tree */
877 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
878 /* Point to the start of the record we're currently checking. */
879 srcKey
= (HotFileKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
882 * Point to start of next (larger offset) record. We'll use this
883 * to be sure the current record doesn't overflow into the next
886 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
888 /* Make sure there is room for the key (HotFileKey) and data (u_int32_t) */
889 if ((char *)srcKey
+ sizeof(HotFileKey
) + sizeof(u_int32_t
) > nextRecord
) {
890 if (direction
== kSwapBTNodeHostToBig
) {
891 panic("hfs_swap_HFSPlusBTInternalNode: hotfile #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
893 printf("hfs_swap_HFSPlusBTInternalNode: hotfile #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
895 return fsBTInvalidNodeErr
;
898 /* Swap and sanity check the key length field */
899 if (direction
== kSwapBTNodeBigToHost
)
900 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
901 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
902 if (direction
== kSwapBTNodeHostToBig
) {
903 panic("hfs_swap_HFSPlusBTInternalNode: hotfile #%d incorrect keyLength %d\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
905 printf("hfs_swap_HFSPlusBTInternalNode: hotfile #%d incorrect keyLength %d\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
907 return fsBTInvalidNodeErr
;
909 srcRec
= (u_int32_t
*)((char *)srcKey
+ srcKey
->keyLength
+ sizeof(srcKey
->keyLength
));
910 if (direction
== kSwapBTNodeHostToBig
)
911 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
913 /* Don't swap srcKey->forkType */
914 /* Don't swap srcKey->pad */
916 srcKey
->temperature
= SWAP_BE32 (srcKey
->temperature
);
917 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
919 *((u_int32_t
*)srcRec
) = SWAP_BE32 (*((u_int32_t
*)srcRec
));
922 panic ("hfs_swap_HFSPlusBTInternalNode: fileID %u is not a system B-tree\n", fileID
);
930 hfs_swap_HFSBTInternalNode (
931 BlockDescriptor
*src
,
932 HFSCatalogNodeID fileID
,
933 enum HFSBTSwapDirection direction
936 BTNodeDescriptor
*srcDesc
= src
->buffer
;
937 u_int16_t
*srcOffs
= (u_int16_t
*)((char *)src
->buffer
+ (src
->blockSize
- (srcDesc
->numRecords
* sizeof (u_int16_t
))));
938 char *nextRecord
; /* Points to start of record following current one */
941 * i is an int32 because it needs to be negative to index the offset to free space.
942 * srcDesc->numRecords is a u_int16_t and is unlikely to become 32-bit so this should be ok.
947 if (fileID
== kHFSExtentsFileID
) {
948 HFSExtentKey
*srcKey
;
949 HFSExtentDescriptor
*srcRec
;
950 size_t recordSize
; /* Size of the data part of the record, or node number for index nodes */
952 if (srcDesc
->kind
== kBTIndexNode
)
953 recordSize
= sizeof(u_int32_t
);
955 recordSize
= sizeof(HFSExtentDescriptor
);
957 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
958 /* Point to the start of the record we're currently checking. */
959 srcKey
= (HFSExtentKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
962 * Point to start of next (larger offset) record. We'll use this
963 * to be sure the current record doesn't overflow into the next
966 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
969 * Make sure the key and data are within the buffer. Since both key
970 * and data are fixed size, this is relatively easy. Note that this
971 * relies on the keyLength being a constant; we verify the keyLength
974 if ((char *)srcKey
+ sizeof(HFSExtentKey
) + recordSize
> nextRecord
) {
975 if (direction
== kSwapBTNodeHostToBig
) {
976 panic("hfs_swap_HFSBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
978 printf("hfs_swap_HFSBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
980 return fsBTInvalidNodeErr
;
983 /* Don't swap srcKey->keyLength (it's only one byte), but do sanity check it */
984 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
985 if (direction
== kSwapBTNodeHostToBig
) {
986 panic("hfs_swap_HFSBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
988 printf("hfs_swap_HFSBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
990 return fsBTInvalidNodeErr
;
993 /* Don't swap srcKey->forkType; it's only one byte */
995 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
996 srcKey
->startBlock
= SWAP_BE16 (srcKey
->startBlock
);
998 /* Point to record data (round up to even byte boundary) */
999 srcRec
= (HFSExtentDescriptor
*)((char *)srcKey
+ ((srcKey
->keyLength
+ 2) & ~1));
1001 if (srcDesc
->kind
== kBTIndexNode
) {
1002 /* For index nodes, the record data is just a child node number. */
1003 *((u_int32_t
*)srcRec
) = SWAP_BE32 (*((u_int32_t
*)srcRec
));
1005 /* Swap the extent data */
1006 for (j
= 0; j
< kHFSExtentDensity
; j
++) {
1007 srcRec
[j
].startBlock
= SWAP_BE16 (srcRec
[j
].startBlock
);
1008 srcRec
[j
].blockCount
= SWAP_BE16 (srcRec
[j
].blockCount
);
1013 } else if (fileID
== kHFSCatalogFileID
) {
1014 HFSCatalogKey
*srcKey
;
1016 unsigned expectedKeyLength
;
1018 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
1019 /* Point to the start of the record we're currently checking. */
1020 srcKey
= (HFSCatalogKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
1023 * Point to start of next (larger offset) record. We'll use this
1024 * to be sure the current record doesn't overflow into the next
1027 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
1030 * Make sure we can safely dereference the keyLength and parentID fields.
1031 * The value 8 below is 1 bytes for keyLength + 1 byte reserved + 4 bytes
1032 * for parentID + 1 byte for nodeName's length + 1 byte to round up the
1033 * record start to an even offset, which forms a minimal key.
1035 if ((char *)srcKey
+ 8 > nextRecord
) {
1036 if (direction
== kSwapBTNodeHostToBig
) {
1037 panic("hfs_swap_HFSBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
1039 printf("hfs_swap_HFSBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
1041 return fsBTInvalidNodeErr
;
1044 /* Don't swap srcKey->keyLength (it's only one byte), but do sanity check it */
1045 if (srcKey
->keyLength
< kHFSCatalogKeyMinimumLength
|| srcKey
->keyLength
> kHFSCatalogKeyMaximumLength
) {
1046 if (direction
== kSwapBTNodeHostToBig
) {
1047 panic("hfs_swap_HFSBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
1049 printf("hfs_swap_HFSBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
1051 return fsBTInvalidNodeErr
;
1054 /* Don't swap srcKey->reserved */
1056 srcKey
->parentID
= SWAP_BE32 (srcKey
->parentID
);
1058 /* Don't swap srcKey->nodeName */
1060 /* Make sure the keyLength is big enough for the key's content */
1061 if (srcDesc
->kind
== kBTIndexNode
)
1062 expectedKeyLength
= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
);
1064 expectedKeyLength
= srcKey
->nodeName
[0] + kHFSCatalogKeyMinimumLength
;
1065 if (srcKey
->keyLength
< expectedKeyLength
) {
1066 if (direction
== kSwapBTNodeHostToBig
) {
1067 panic("hfs_swap_HFSBTInternalNode: catalog record #%d keyLength=%u expected=%u\n",
1068 srcDesc
->numRecords
-i
, srcKey
->keyLength
, expectedKeyLength
);
1070 printf("hfs_swap_HFSBTInternalNode: catalog record #%d keyLength=%u expected=%u\n",
1071 srcDesc
->numRecords
-i
, srcKey
->keyLength
, expectedKeyLength
);
1073 return fsBTInvalidNodeErr
;
1076 /* Point to record data (round up to even byte boundary) */
1077 srcPtr
= (int16_t *)((char *)srcKey
+ ((srcKey
->keyLength
+ 2) & ~1));
1080 * Make sure that we can safely dereference the record's type field or
1081 * and index node's child node number.
1083 if ((char *)srcPtr
+ sizeof(u_int32_t
) > nextRecord
) {
1084 if (direction
== kSwapBTNodeHostToBig
) {
1085 panic("hfs_swap_HFSBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
1087 printf("hfs_swap_HFSBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
1089 return fsBTInvalidNodeErr
;
1093 * For index nodes, the record data is just the child's node number.
1094 * Skip over swapping the various types of catalog record.
1096 if (srcDesc
->kind
== kBTIndexNode
) {
1097 *((u_int32_t
*)srcPtr
) = SWAP_BE32 (*((u_int32_t
*)srcPtr
));
1101 /* Make sure the recordType is in native order before using it. */
1102 if (direction
== kSwapBTNodeBigToHost
)
1103 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
1105 if (srcPtr
[0] == kHFSFolderRecord
) {
1106 HFSCatalogFolder
*srcRec
= (HFSCatalogFolder
*)srcPtr
;
1107 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
1108 if (direction
== kSwapBTNodeHostToBig
) {
1109 panic("hfs_swap_HFSBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
1111 printf("hfs_swap_HFSBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
1113 return fsBTInvalidNodeErr
;
1116 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
1117 srcRec
->valence
= SWAP_BE16 (srcRec
->valence
);
1119 srcRec
->folderID
= SWAP_BE32 (srcRec
->folderID
);
1120 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
1121 srcRec
->modifyDate
= SWAP_BE32 (srcRec
->modifyDate
);
1122 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
1124 /* Don't swap srcRec->userInfo */
1125 /* Don't swap srcRec->finderInfo */
1126 /* Don't swap resserved array */
1128 } else if (srcPtr
[0] == kHFSFileRecord
) {
1129 HFSCatalogFile
*srcRec
= (HFSCatalogFile
*)srcPtr
;
1130 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
1131 if (direction
== kSwapBTNodeHostToBig
) {
1132 panic("hfs_swap_HFSBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
1134 printf("hfs_swap_HFSBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
1136 return fsBTInvalidNodeErr
;
1139 srcRec
->flags
= srcRec
->flags
;
1140 srcRec
->fileType
= srcRec
->fileType
;
1142 /* Don't swap srcRec->userInfo */
1144 srcRec
->fileID
= SWAP_BE32 (srcRec
->fileID
);
1146 srcRec
->dataStartBlock
= SWAP_BE16 (srcRec
->dataStartBlock
);
1147 srcRec
->dataLogicalSize
= SWAP_BE32 (srcRec
->dataLogicalSize
);
1148 srcRec
->dataPhysicalSize
= SWAP_BE32 (srcRec
->dataPhysicalSize
);
1150 srcRec
->rsrcStartBlock
= SWAP_BE16 (srcRec
->rsrcStartBlock
);
1151 srcRec
->rsrcLogicalSize
= SWAP_BE32 (srcRec
->rsrcLogicalSize
);
1152 srcRec
->rsrcPhysicalSize
= SWAP_BE32 (srcRec
->rsrcPhysicalSize
);
1154 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
1155 srcRec
->modifyDate
= SWAP_BE32 (srcRec
->modifyDate
);
1156 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
1158 /* Don't swap srcRec->finderInfo */
1160 srcRec
->clumpSize
= SWAP_BE16 (srcRec
->clumpSize
);
1162 /* Swap the two sets of extents as an array of six (three each) u_int16_t */
1163 for (j
= 0; j
< kHFSExtentDensity
* 2; j
++) {
1164 srcRec
->dataExtents
[j
].startBlock
= SWAP_BE16 (srcRec
->dataExtents
[j
].startBlock
);
1165 srcRec
->dataExtents
[j
].blockCount
= SWAP_BE16 (srcRec
->dataExtents
[j
].blockCount
);
1168 /* Don't swap srcRec->reserved */
1170 } else if ((srcPtr
[0] == kHFSFolderThreadRecord
) ||
1171 (srcPtr
[0] == kHFSFileThreadRecord
)) {
1172 HFSCatalogThread
*srcRec
= (HFSCatalogThread
*)srcPtr
;
1174 /* Make sure there is room for parentID and name length */
1175 if ((char *) &srcRec
->nodeName
[1] > nextRecord
) {
1176 if (direction
== kSwapBTNodeHostToBig
) {
1177 panic("hfs_swap_HFSBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
1179 printf("hfs_swap_HFSBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
1181 return fsBTInvalidNodeErr
;
1184 /* Don't swap srcRec->reserved array */
1186 srcRec
->parentID
= SWAP_BE32 (srcRec
->parentID
);
1188 /* Don't swap srcRec->nodeName */
1190 /* Make sure there is room for the name in the buffer */
1191 if ((char *) &srcRec
->nodeName
[srcRec
->nodeName
[0]] > nextRecord
) {
1192 if (direction
== kSwapBTNodeHostToBig
) {
1193 panic("hfs_swap_HFSBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
1195 printf("hfs_swap_HFSBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
1197 return fsBTInvalidNodeErr
;
1200 if (direction
== kSwapBTNodeHostToBig
) {
1201 panic("hfs_swap_HFSBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
1203 printf("hfs_swap_HFSBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
1205 return fsBTInvalidNodeErr
;
1208 /* We can swap the record type now that we're done using it */
1209 if (direction
== kSwapBTNodeHostToBig
)
1210 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
1214 panic ("hfs_swap_HFSBTInternalNode: fileID %u is not a system B-tree\n", fileID
);