2 * Copyright (c) 2000-2014 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 void hfs_swap_HFSPlusForkData (HFSPlusForkData
*src
);
56 int hfs_swap_HFSBTInternalNode (BlockDescriptor
*src
, HFSCatalogNodeID fileID
, enum HFSBTSwapDirection direction
);
60 * hfs_swap_HFSPlusForkData
63 hfs_swap_HFSPlusForkData (
69 src
->logicalSize
= SWAP_BE64 (src
->logicalSize
);
71 src
->clumpSize
= SWAP_BE32 (src
->clumpSize
);
72 src
->totalBlocks
= SWAP_BE32 (src
->totalBlocks
);
74 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
75 src
->extents
[i
].startBlock
= SWAP_BE32 (src
->extents
[i
].startBlock
);
76 src
->extents
[i
].blockCount
= SWAP_BE32 (src
->extents
[i
].blockCount
);
83 * NOTE: This operation is not naturally symmetric.
84 * We have to determine which way we're swapping things.
90 enum HFSBTSwapDirection direction
,
91 u_int8_t allow_empty_node
94 BTNodeDescriptor
*srcDesc
= src
->buffer
;
95 u_int16_t
*srcOffs
= NULL
;
96 BTreeControlBlockPtr btcb
= (BTreeControlBlockPtr
)VTOF(vp
)->fcbBTCBPtr
;
97 u_int16_t i
; /* index to match srcDesc->numRecords */
101 if (direction
== kSwapBTNodeBigToHost
) {
102 printf ("hfs: BE -> Native Swap\n");
103 } else if (direction
== kSwapBTNodeHostToBig
) {
104 printf ("hfs: Native -> BE Swap\n");
105 } else if (direction
== kSwapBTNodeHeaderRecordOnly
) {
106 printf ("hfs: Not swapping descriptors\n");
108 panic ("hfs_swap_BTNode: This is impossible");
113 * If we are doing a swap from on-disk to in-memory, then swap the node
114 * descriptor and record offsets before we need to use them.
116 if (direction
== kSwapBTNodeBigToHost
) {
117 srcDesc
->fLink
= SWAP_BE32 (srcDesc
->fLink
);
118 srcDesc
->bLink
= SWAP_BE32 (srcDesc
->bLink
);
121 * When first opening a BTree, we have to read the header node before the
122 * control block is initialized. In this case, totalNodes will be zero,
123 * so skip the bounds checking. Also, we should ignore the header node when
124 * checking for invalid forwards and backwards links, since the header node's
125 * links can point back to itself legitimately.
127 if (btcb
->totalNodes
!= 0) {
128 if (srcDesc
->fLink
>= btcb
->totalNodes
) {
129 printf("hfs_swap_BTNode: invalid forward link (0x%08x >= 0x%08x)\n", srcDesc
->fLink
, btcb
->totalNodes
);
130 error
= fsBTInvalidHeaderErr
;
133 if (srcDesc
->bLink
>= btcb
->totalNodes
) {
134 printf("hfs_swap_BTNode: invalid backward link (0x%08x >= 0x%08x)\n", srcDesc
->bLink
, btcb
->totalNodes
);
135 error
= fsBTInvalidHeaderErr
;
139 if ((src
->blockNum
!= 0) && (srcDesc
->fLink
== (u_int32_t
) src
->blockNum
)) {
140 printf("hfs_swap_BTNode: invalid forward link (0x%08x == 0x%08x)\n",
141 srcDesc
->fLink
, (u_int32_t
) src
->blockNum
);
142 error
= fsBTInvalidHeaderErr
;
145 if ((src
->blockNum
!= 0) && (srcDesc
->bLink
== (u_int32_t
) src
->blockNum
)) {
146 printf("hfs_swap_BTNode: invalid backward link (0x%08x == 0x%08x)\n",
147 srcDesc
->bLink
, (u_int32_t
) src
->blockNum
);
148 error
= fsBTInvalidHeaderErr
;
156 * Check srcDesc->kind. Don't swap it because it's only one byte.
158 if (srcDesc
->kind
< kBTLeafNode
|| srcDesc
->kind
> kBTMapNode
) {
159 printf("hfs_swap_BTNode: invalid node kind (%d)\n", srcDesc
->kind
);
160 error
= fsBTInvalidHeaderErr
;
165 * Check srcDesc->height. Don't swap it because it's only one byte.
167 if (srcDesc
->height
> kMaxTreeDepth
) {
168 printf("hfs_swap_BTNode: invalid node height (%d)\n", srcDesc
->height
);
169 error
= fsBTInvalidHeaderErr
;
173 /* Don't swap srcDesc->reserved */
175 srcDesc
->numRecords
= SWAP_BE16 (srcDesc
->numRecords
);
178 * Swap the node offsets (including the free space one!).
180 srcOffs
= (u_int16_t
*)((char *)src
->buffer
+ (src
->blockSize
- ((srcDesc
->numRecords
+ 1) * sizeof (u_int16_t
))));
183 * Sanity check that the record offsets are within the node itself.
185 if ((char *)srcOffs
> ((char *)src
->buffer
+ src
->blockSize
) ||
186 (char *)srcOffs
< ((char *)src
->buffer
+ sizeof(BTNodeDescriptor
))) {
187 printf("hfs_swap_BTNode: invalid record count (0x%04X)\n", srcDesc
->numRecords
);
188 error
= fsBTInvalidHeaderErr
;
193 * Swap and sanity check each of the record offsets.
195 for (i
= 0; i
<= srcDesc
->numRecords
; i
++) {
196 srcOffs
[i
] = SWAP_BE16 (srcOffs
[i
]);
199 * Sanity check: must be even, and within the node itself.
201 * We may be called to swap an unused node, which contains all zeroes.
202 * Unused nodes are expected only when allow_empty_node is true.
203 * If it is false and record offset is zero, return error.
205 if ((srcOffs
[i
] & 1) || (
206 (allow_empty_node
== false) && (srcOffs
[i
] == 0)) ||
207 (srcOffs
[i
] < sizeof(BTNodeDescriptor
) && srcOffs
[i
] != 0) ||
208 (srcOffs
[i
] >= src
->blockSize
)) {
209 printf("hfs_swap_BTNode: record #%d invalid offset (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
210 error
= fsBTInvalidHeaderErr
;
215 * Make sure the offsets are strictly increasing. Note that we're looping over
216 * them backwards, hence the order in the comparison.
218 if ((i
!= 0) && (srcOffs
[i
] >= srcOffs
[i
-1])) {
219 printf("hfs_swap_BTNode: offsets %d and %d out of order (0x%04X, 0x%04X)\n",
220 srcDesc
->numRecords
-i
-1, srcDesc
->numRecords
-i
, srcOffs
[i
], srcOffs
[i
-1]);
221 error
= fsBTInvalidHeaderErr
;
228 * Swap the records (ordered by frequency of access)
230 if ((srcDesc
->kind
== kBTIndexNode
) ||
231 (srcDesc
-> kind
== kBTLeafNode
)) {
233 if (VTOVCB(vp
)->vcbSigWord
== kHFSPlusSigWord
) {
234 error
= hfs_swap_HFSPlusBTInternalNode (src
, VTOC(vp
)->c_fileid
, direction
);
238 error
= hfs_swap_HFSBTInternalNode (src
, VTOC(vp
)->c_fileid
, direction
);
242 if (error
) goto fail
;
244 } else if (srcDesc
-> kind
== kBTMapNode
) {
245 /* Don't swap the bitmaps, they'll be done in the bitmap routines */
247 } else if (srcDesc
-> kind
== kBTHeaderNode
) {
248 /* The header's offset is hard-wired because we cannot trust the offset pointers. */
249 BTHeaderRec
*srcHead
= (BTHeaderRec
*)((char *)src
->buffer
+ sizeof(BTNodeDescriptor
));
251 srcHead
->treeDepth
= SWAP_BE16 (srcHead
->treeDepth
);
253 srcHead
->rootNode
= SWAP_BE32 (srcHead
->rootNode
);
254 srcHead
->leafRecords
= SWAP_BE32 (srcHead
->leafRecords
);
255 srcHead
->firstLeafNode
= SWAP_BE32 (srcHead
->firstLeafNode
);
256 srcHead
->lastLeafNode
= SWAP_BE32 (srcHead
->lastLeafNode
);
258 srcHead
->nodeSize
= SWAP_BE16 (srcHead
->nodeSize
);
259 srcHead
->maxKeyLength
= SWAP_BE16 (srcHead
->maxKeyLength
);
261 srcHead
->totalNodes
= SWAP_BE32 (srcHead
->totalNodes
);
262 srcHead
->freeNodes
= SWAP_BE32 (srcHead
->freeNodes
);
264 srcHead
->clumpSize
= SWAP_BE32 (srcHead
->clumpSize
);
265 srcHead
->attributes
= SWAP_BE32 (srcHead
->attributes
);
267 /* Don't swap srcHead->reserved1 */
268 /* Don't swap srcHead->btreeType; it's only one byte */
269 /* Don't swap srcHead->reserved2 */
270 /* Don't swap srcHead->reserved3 */
271 /* Don't swap bitmap */
275 * If we are doing a swap from in-memory to on-disk, then swap the node
276 * descriptor and record offsets after we're done using them.
278 if (direction
== kSwapBTNodeHostToBig
) {
280 * Sanity check and swap the forward and backward links.
281 * Ignore the header node since its forward and backwards links can legitimately
284 if (srcDesc
->fLink
>= btcb
->totalNodes
) {
285 panic("hfs_UNswap_BTNode: invalid forward link (0x%08X)\n", srcDesc
->fLink
);
286 error
= fsBTInvalidHeaderErr
;
289 if ((src
->blockNum
!= 0) && (srcDesc
->fLink
== (u_int32_t
) src
->blockNum
)) {
290 panic ("hfs_UNswap_BTNode: invalid forward link (0x%08x == 0x%08x)\n",
291 srcDesc
->fLink
, (u_int32_t
) src
->blockNum
);
292 error
= fsBTInvalidHeaderErr
;
296 if (srcDesc
->bLink
>= btcb
->totalNodes
) {
297 panic("hfs_UNswap_BTNode: invalid backward link (0x%08X)\n", srcDesc
->bLink
);
298 error
= fsBTInvalidHeaderErr
;
301 if ((src
->blockNum
!= 0) && (srcDesc
->bLink
== (u_int32_t
) src
->blockNum
)) {
302 panic ("hfs_UNswap_BTNode: invalid backward link (0x%08x == 0x%08x)\n",
303 srcDesc
->bLink
, (u_int32_t
) src
->blockNum
);
304 error
= fsBTInvalidHeaderErr
;
309 srcDesc
->fLink
= SWAP_BE32 (srcDesc
->fLink
);
310 srcDesc
->bLink
= SWAP_BE32 (srcDesc
->bLink
);
313 * Check srcDesc->kind. Don't swap it because it's only one byte.
315 if (srcDesc
->kind
< kBTLeafNode
|| srcDesc
->kind
> kBTMapNode
) {
316 panic("hfs_UNswap_BTNode: invalid node kind (%d)\n", srcDesc
->kind
);
317 error
= fsBTInvalidHeaderErr
;
322 * Check srcDesc->height. Don't swap it because it's only one byte.
324 if (srcDesc
->height
> kMaxTreeDepth
) {
325 panic("hfs_UNswap_BTNode: invalid node height (%d)\n", srcDesc
->height
);
326 error
= fsBTInvalidHeaderErr
;
330 /* Don't swap srcDesc->reserved */
333 * Swap the node offsets (including the free space one!).
335 srcOffs
= (u_int16_t
*)((char *)src
->buffer
+ (src
->blockSize
- ((srcDesc
->numRecords
+ 1) * sizeof (u_int16_t
))));
338 * Sanity check that the record offsets are within the node itself.
340 if ((char *)srcOffs
> ((char *)src
->buffer
+ src
->blockSize
) ||
341 (char *)srcOffs
< ((char *)src
->buffer
+ sizeof(BTNodeDescriptor
))) {
342 panic("hfs_UNswap_BTNode: invalid record count (0x%04X)\n", srcDesc
->numRecords
);
343 error
= fsBTInvalidHeaderErr
;
348 * Swap and sanity check each of the record offsets.
350 for (i
= 0; i
<= srcDesc
->numRecords
; i
++) {
352 * Sanity check: must be even, and within the node itself.
354 * We may be called to swap an unused node, which contains all zeroes.
355 * This can happen when the last record from a node gets deleted.
356 * This is why we allow the record offset to be zero.
357 * Unused nodes are expected only when allow_empty_node is true
358 * (the caller should set it to true for kSwapBTNodeBigToHost).
360 if ((srcOffs
[i
] & 1) ||
361 ((allow_empty_node
== false) && (srcOffs
[i
] == 0)) ||
362 (srcOffs
[i
] < sizeof(BTNodeDescriptor
) && srcOffs
[i
] != 0) ||
363 (srcOffs
[i
] >= src
->blockSize
)) {
364 panic("hfs_UNswap_BTNode: record #%d invalid offset (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
365 error
= fsBTInvalidHeaderErr
;
370 * Make sure the offsets are strictly increasing. Note that we're looping over
371 * them backwards, hence the order in the comparison.
373 if ((i
< srcDesc
->numRecords
) && (srcOffs
[i
+1] >= srcOffs
[i
])) {
374 panic("hfs_UNswap_BTNode: offsets %d and %d out of order (0x%04X, 0x%04X)\n",
375 srcDesc
->numRecords
-i
-2, srcDesc
->numRecords
-i
-1, srcOffs
[i
+1], srcOffs
[i
]);
376 error
= fsBTInvalidHeaderErr
;
380 srcOffs
[i
] = SWAP_BE16 (srcOffs
[i
]);
383 srcDesc
->numRecords
= SWAP_BE16 (srcDesc
->numRecords
);
389 * Log some useful information about where the corrupt node is.
391 printf("hfs: node=%lld fileID=%u volume=%s device=%s\n", src
->blockNum
, VTOC(vp
)->c_fileid
,
392 VTOVCB(vp
)->vcbVN
, vfs_statfs(vnode_mount(vp
))->f_mntfromname
);
393 hfs_mark_inconsistent(VTOVCB(vp
), HFS_INCONSISTENCY_DETECTED
);
400 hfs_swap_HFSPlusBTInternalNode (
401 BlockDescriptor
*src
,
402 HFSCatalogNodeID fileID
,
403 enum HFSBTSwapDirection direction
406 BTNodeDescriptor
*srcDesc
= src
->buffer
;
407 u_int16_t
*srcOffs
= (u_int16_t
*)((char *)src
->buffer
+ (src
->blockSize
- (srcDesc
->numRecords
* sizeof (u_int16_t
))));
408 char *nextRecord
; /* Points to start of record following current one */
411 * i is an int32 because it needs to be negative to index the offset to free space.
412 * srcDesc->numRecords is a u_int16_t and is unlikely to become 32-bit so this should be ok.
418 if (fileID
== kHFSExtentsFileID
) {
419 HFSPlusExtentKey
*srcKey
;
420 HFSPlusExtentDescriptor
*srcRec
;
421 size_t recordSize
; /* Size of the data part of the record, or node number for index nodes */
423 if (srcDesc
->kind
== kBTIndexNode
)
424 recordSize
= sizeof(u_int32_t
);
426 recordSize
= sizeof(HFSPlusExtentDescriptor
);
428 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
429 /* Point to the start of the record we're currently checking. */
430 srcKey
= (HFSPlusExtentKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
433 * Point to start of next (larger offset) record. We'll use this
434 * to be sure the current record doesn't overflow into the next
437 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
440 * Make sure the key and data are within the buffer. Since both key
441 * and data are fixed size, this is relatively easy. Note that this
442 * relies on the keyLength being a constant; we verify the keyLength
445 if ((char *)srcKey
+ sizeof(HFSPlusExtentKey
) + recordSize
> nextRecord
) {
446 if (direction
== kSwapBTNodeHostToBig
) {
447 panic("hfs_swap_HFSPlusBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
449 printf("hfs_swap_HFSPlusBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
451 return fsBTInvalidNodeErr
;
454 if (direction
== kSwapBTNodeBigToHost
)
455 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
456 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
457 if (direction
== kSwapBTNodeHostToBig
) {
458 panic("hfs_swap_HFSPlusBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
460 printf("hfs_swap_HFSPlusBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
462 return fsBTInvalidNodeErr
;
464 srcRec
= (HFSPlusExtentDescriptor
*)((char *)srcKey
+ srcKey
->keyLength
+ sizeof(srcKey
->keyLength
));
465 if (direction
== kSwapBTNodeHostToBig
)
466 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
468 /* Don't swap srcKey->forkType; it's only one byte */
469 /* Don't swap srcKey->pad */
471 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
472 srcKey
->startBlock
= SWAP_BE32 (srcKey
->startBlock
);
474 if (srcDesc
->kind
== kBTIndexNode
) {
475 /* For index nodes, the record data is just a child node number. */
476 *((u_int32_t
*)srcRec
) = SWAP_BE32 (*((u_int32_t
*)srcRec
));
478 /* Swap the extent data */
479 for (j
= 0; j
< kHFSPlusExtentDensity
; j
++) {
480 srcRec
[j
].startBlock
= SWAP_BE32 (srcRec
[j
].startBlock
);
481 srcRec
[j
].blockCount
= SWAP_BE32 (srcRec
[j
].blockCount
);
486 } else if (fileID
== kHFSCatalogFileID
) {
487 HFSPlusCatalogKey
*srcKey
;
491 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
492 /* Point to the start of the record we're currently checking. */
493 srcKey
= (HFSPlusCatalogKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
496 * Point to start of next (larger offset) record. We'll use this
497 * to be sure the current record doesn't overflow into the next
500 nextRecord
= (char *)src
->buffer
+ (uintptr_t)(srcOffs
[i
-1]);
503 * Make sure we can safely dereference the keyLength and parentID fields.
505 if ((char *)srcKey
+ offsetof(HFSPlusCatalogKey
, nodeName
.unicode
[0]) > nextRecord
) {
506 if (direction
== kSwapBTNodeHostToBig
) {
507 panic("hfs_swap_HFSPlusBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
509 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
511 return fsBTInvalidNodeErr
;
515 * Swap and sanity check the key length
517 if (direction
== kSwapBTNodeBigToHost
)
518 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
519 keyLength
= srcKey
->keyLength
; /* Put it in a local (native order) because we use it several times */
520 if (direction
== kSwapBTNodeHostToBig
)
521 srcKey
->keyLength
= SWAP_BE16 (keyLength
);
523 /* Sanity check the key length */
524 if (keyLength
< kHFSPlusCatalogKeyMinimumLength
|| keyLength
> kHFSPlusCatalogKeyMaximumLength
) {
525 if (direction
== kSwapBTNodeHostToBig
) {
526 panic("hfs_swap_HFSPlusBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
528 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
530 return fsBTInvalidNodeErr
;
534 * Make sure that we can safely dereference the record's type field or
535 * an index node's child node number.
537 srcPtr
= (int16_t *)((char *)srcKey
+ keyLength
+ sizeof(srcKey
->keyLength
));
538 if ((char *)srcPtr
+ sizeof(u_int32_t
) > nextRecord
) {
539 if (direction
== kSwapBTNodeHostToBig
) {
540 panic("hfs_swap_HFSPlusBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
542 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
544 return fsBTInvalidNodeErr
;
547 srcKey
->parentID
= SWAP_BE32 (srcKey
->parentID
);
550 * Swap and sanity check the key's node name
552 if (direction
== kSwapBTNodeBigToHost
)
553 srcKey
->nodeName
.length
= SWAP_BE16 (srcKey
->nodeName
.length
);
554 /* Make sure name length is consistent with key length */
555 if (keyLength
< sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
556 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0])) {
557 if (direction
== kSwapBTNodeHostToBig
) {
558 panic("hfs_swap_HFSPlusBTInternalNode: catalog record #%d keyLength=%d expected=%lu\n",
559 srcDesc
->numRecords
-i
, keyLength
, sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
560 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0]));
562 printf("hfs_swap_HFSPlusBTInternalNode: catalog record #%d keyLength=%d expected=%lu\n",
563 srcDesc
->numRecords
-i
, keyLength
, sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
564 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0]));
566 return fsBTInvalidNodeErr
;
568 for (j
= 0; j
< srcKey
->nodeName
.length
; j
++) {
569 srcKey
->nodeName
.unicode
[j
] = SWAP_BE16 (srcKey
->nodeName
.unicode
[j
]);
571 if (direction
== kSwapBTNodeHostToBig
)
572 srcKey
->nodeName
.length
= SWAP_BE16 (srcKey
->nodeName
.length
);
575 * For index nodes, the record data is just the child's node number.
576 * Skip over swapping the various types of catalog record.
578 if (srcDesc
->kind
== kBTIndexNode
) {
579 *((u_int32_t
*)srcPtr
) = SWAP_BE32 (*((u_int32_t
*)srcPtr
));
583 /* Make sure the recordType is in native order before using it. */
584 if (direction
== kSwapBTNodeBigToHost
)
585 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
587 if (srcPtr
[0] == kHFSPlusFolderRecord
) {
588 HFSPlusCatalogFolder
*srcRec
= (HFSPlusCatalogFolder
*)srcPtr
;
589 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
590 if (direction
== kSwapBTNodeHostToBig
) {
591 panic("hfs_swap_HFSPlusBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
593 printf("hfs_swap_HFSPlusBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
595 return fsBTInvalidNodeErr
;
598 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
599 srcRec
->valence
= SWAP_BE32 (srcRec
->valence
);
600 srcRec
->folderID
= SWAP_BE32 (srcRec
->folderID
);
601 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
602 srcRec
->contentModDate
= SWAP_BE32 (srcRec
->contentModDate
);
603 srcRec
->attributeModDate
= SWAP_BE32 (srcRec
->attributeModDate
);
604 srcRec
->accessDate
= SWAP_BE32 (srcRec
->accessDate
);
605 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
607 srcRec
->bsdInfo
.ownerID
= SWAP_BE32 (srcRec
->bsdInfo
.ownerID
);
608 srcRec
->bsdInfo
.groupID
= SWAP_BE32 (srcRec
->bsdInfo
.groupID
);
610 /* Don't swap srcRec->bsdInfo.adminFlags; it's only one byte */
611 /* Don't swap srcRec->bsdInfo.ownerFlags; it's only one byte */
613 srcRec
->bsdInfo
.fileMode
= SWAP_BE16 (srcRec
->bsdInfo
.fileMode
);
614 srcRec
->bsdInfo
.special
.iNodeNum
= SWAP_BE32 (srcRec
->bsdInfo
.special
.iNodeNum
);
616 srcRec
->textEncoding
= SWAP_BE32 (srcRec
->textEncoding
);
618 /* Don't swap srcRec->userInfo */
619 /* Don't swap srcRec->finderInfo */
620 srcRec
->folderCount
= SWAP_BE32 (srcRec
->folderCount
);
622 } else if (srcPtr
[0] == kHFSPlusFileRecord
) {
623 HFSPlusCatalogFile
*srcRec
= (HFSPlusCatalogFile
*)srcPtr
;
624 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
625 if (direction
== kSwapBTNodeHostToBig
) {
626 panic("hfs_swap_HFSPlusBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
628 printf("hfs_swap_HFSPlusBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
630 return fsBTInvalidNodeErr
;
633 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
635 srcRec
->fileID
= SWAP_BE32 (srcRec
->fileID
);
637 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
638 srcRec
->contentModDate
= SWAP_BE32 (srcRec
->contentModDate
);
639 srcRec
->attributeModDate
= SWAP_BE32 (srcRec
->attributeModDate
);
640 srcRec
->accessDate
= SWAP_BE32 (srcRec
->accessDate
);
641 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
643 srcRec
->bsdInfo
.ownerID
= SWAP_BE32 (srcRec
->bsdInfo
.ownerID
);
644 srcRec
->bsdInfo
.groupID
= SWAP_BE32 (srcRec
->bsdInfo
.groupID
);
646 /* Don't swap srcRec->bsdInfo.adminFlags; it's only one byte */
647 /* Don't swap srcRec->bsdInfo.ownerFlags; it's only one byte */
649 srcRec
->bsdInfo
.fileMode
= SWAP_BE16 (srcRec
->bsdInfo
.fileMode
);
650 srcRec
->bsdInfo
.special
.iNodeNum
= SWAP_BE32 (srcRec
->bsdInfo
.special
.iNodeNum
);
652 srcRec
->textEncoding
= SWAP_BE32 (srcRec
->textEncoding
);
654 /* If kHFSHasLinkChainBit is set, reserved1 is hl_FirstLinkID.
655 * In all other context, it is expected to be zero.
657 srcRec
->reserved1
= SWAP_BE32 (srcRec
->reserved1
);
659 /* Don't swap srcRec->userInfo */
660 /* Don't swap srcRec->finderInfo */
661 /* Don't swap srcRec->reserved2 */
663 hfs_swap_HFSPlusForkData (&srcRec
->dataFork
);
664 hfs_swap_HFSPlusForkData (&srcRec
->resourceFork
);
666 } else if ((srcPtr
[0] == kHFSPlusFolderThreadRecord
) ||
667 (srcPtr
[0] == kHFSPlusFileThreadRecord
)) {
670 * Make sure there is room for parentID and name length.
672 HFSPlusCatalogThread
*srcRec
= (HFSPlusCatalogThread
*)srcPtr
;
673 if ((char *) &srcRec
->nodeName
.unicode
[0] > nextRecord
) {
674 if (direction
== kSwapBTNodeHostToBig
) {
675 panic("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
677 printf("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
679 return fsBTInvalidNodeErr
;
682 /* Don't swap srcRec->reserved */
684 srcRec
->parentID
= SWAP_BE32 (srcRec
->parentID
);
686 if (direction
== kSwapBTNodeBigToHost
)
687 srcRec
->nodeName
.length
= SWAP_BE16 (srcRec
->nodeName
.length
);
690 * Make sure there is room for the name in the buffer.
691 * Then swap the characters of the name itself.
693 if ((char *) &srcRec
->nodeName
.unicode
[srcRec
->nodeName
.length
] > nextRecord
) {
694 if (direction
== kSwapBTNodeHostToBig
) {
695 panic("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
697 printf("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
699 return fsBTInvalidNodeErr
;
701 for (j
= 0; j
< srcRec
->nodeName
.length
; j
++) {
702 srcRec
->nodeName
.unicode
[j
] = SWAP_BE16 (srcRec
->nodeName
.unicode
[j
]);
705 if (direction
== kSwapBTNodeHostToBig
)
706 srcRec
->nodeName
.length
= SWAP_BE16 (srcRec
->nodeName
.length
);
709 if (direction
== kSwapBTNodeHostToBig
) {
710 panic("hfs_swap_HFSPlusBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
712 printf("hfs_swap_HFSPlusBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
714 return fsBTInvalidNodeErr
;
717 /* We can swap the record type now that we're done using it. */
718 if (direction
== kSwapBTNodeHostToBig
)
719 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
722 } else if (fileID
== kHFSAttributesFileID
) {
723 HFSPlusAttrKey
*srcKey
;
724 HFSPlusAttrRecord
*srcRec
;
726 u_int32_t attrSize
= 0;
728 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
729 /* Point to the start of the record we're currently checking. */
730 srcKey
= (HFSPlusAttrKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
733 * Point to start of next (larger offset) record. We'll use this
734 * to be sure the current record doesn't overflow into the next
737 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
739 /* Make sure there is room in the buffer for a minimal key */
740 if ((char *) &srcKey
->attrName
[1] > nextRecord
) {
741 if (direction
== kSwapBTNodeHostToBig
) {
742 panic("hfs_swap_HFSPlusBTInternalNode: attr key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
744 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
746 return fsBTInvalidNodeErr
;
749 /* Swap the key length field */
750 if (direction
== kSwapBTNodeBigToHost
)
751 srcKey
->keyLength
= SWAP_BE16(srcKey
->keyLength
);
752 keyLength
= srcKey
->keyLength
; /* Keep a copy in native order */
753 if (direction
== kSwapBTNodeHostToBig
)
754 srcKey
->keyLength
= SWAP_BE16(srcKey
->keyLength
);
757 * Make sure that we can safely dereference the record's type field or
758 * an index node's child node number.
760 srcRec
= (HFSPlusAttrRecord
*)((char *)srcKey
+ keyLength
+ sizeof(srcKey
->keyLength
));
761 if ((char *)srcRec
+ sizeof(u_int32_t
) > nextRecord
) {
762 if (direction
== kSwapBTNodeHostToBig
) {
763 panic("hfs_swap_HFSPlusBTInternalNode: attr key #%d too big (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
765 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d too big (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
767 return fsBTInvalidNodeErr
;
770 srcKey
->fileID
= SWAP_BE32(srcKey
->fileID
);
771 srcKey
->startBlock
= SWAP_BE32(srcKey
->startBlock
);
774 * Swap and check the attribute name
776 if (direction
== kSwapBTNodeBigToHost
)
777 srcKey
->attrNameLen
= SWAP_BE16(srcKey
->attrNameLen
);
778 /* Sanity check the attribute name length */
779 if (srcKey
->attrNameLen
> kHFSMaxAttrNameLen
|| keyLength
< (kHFSPlusAttrKeyMinimumLength
+ sizeof(u_int16_t
)*srcKey
->attrNameLen
)) {
780 if (direction
== kSwapBTNodeHostToBig
) {
781 panic("hfs_swap_HFSPlusBTInternalNode: attr key #%d keyLength=%d attrNameLen=%d\n", srcDesc
->numRecords
-i
-1, keyLength
, srcKey
->attrNameLen
);
783 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d keyLength=%d attrNameLen=%d\n", srcDesc
->numRecords
-i
-1, keyLength
, srcKey
->attrNameLen
);
785 return fsBTInvalidNodeErr
;
787 for (j
= 0; j
< srcKey
->attrNameLen
; j
++)
788 srcKey
->attrName
[j
] = SWAP_BE16(srcKey
->attrName
[j
]);
789 if (direction
== kSwapBTNodeHostToBig
)
790 srcKey
->attrNameLen
= SWAP_BE16(srcKey
->attrNameLen
);
793 * For index nodes, the record data is just the child's node number.
794 * Skip over swapping the various types of attribute record.
796 if (srcDesc
->kind
== kBTIndexNode
) {
797 *((u_int32_t
*)srcRec
) = SWAP_BE32 (*((u_int32_t
*)srcRec
));
801 /* Swap the record data */
802 if (direction
== kSwapBTNodeBigToHost
)
803 srcRec
->recordType
= SWAP_BE32(srcRec
->recordType
);
804 switch (srcRec
->recordType
) {
805 case kHFSPlusAttrInlineData
:
806 /* Is there room for the inline data header? */
807 if ((char *) &srcRec
->attrData
.attrData
[0] > nextRecord
) {
808 if (direction
== kSwapBTNodeHostToBig
) {
809 panic("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big\n", srcDesc
->numRecords
-i
-1);
811 printf("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big\n", srcDesc
->numRecords
-i
-1);
813 return fsBTInvalidNodeErr
;
816 /* We're not swapping the reserved fields */
818 /* Swap the attribute size */
819 if (direction
== kSwapBTNodeHostToBig
)
820 attrSize
= srcRec
->attrData
.attrSize
;
821 srcRec
->attrData
.attrSize
= SWAP_BE32(srcRec
->attrData
.attrSize
);
822 if (direction
== kSwapBTNodeBigToHost
)
823 attrSize
= srcRec
->attrData
.attrSize
;
825 /* Is there room for the inline attribute data? */
826 if ((char *) &srcRec
->attrData
.attrData
[attrSize
] > nextRecord
) {
827 if (direction
== kSwapBTNodeHostToBig
) {
828 panic("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big (attrSize=%u)\n", srcDesc
->numRecords
-i
-1, attrSize
);
830 printf("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big (attrSize=%u)\n", srcDesc
->numRecords
-i
-1, attrSize
);
832 return fsBTInvalidNodeErr
;
835 /* Not swapping the attribute data itself */
838 case kHFSPlusAttrForkData
:
839 /* Is there room for the fork data record? */
840 if ((char *)srcRec
+ sizeof(HFSPlusAttrForkData
) > nextRecord
) {
841 if (direction
== kSwapBTNodeHostToBig
) {
842 panic("hfs_swap_HFSPlusBTInternalNode: attr fork data #%d too big\n", srcDesc
->numRecords
-i
-1);
844 printf("hfs_swap_HFSPlusBTInternalNode: attr fork data #%d too big\n", srcDesc
->numRecords
-i
-1);
846 return fsBTInvalidNodeErr
;
849 /* We're not swapping the reserved field */
851 hfs_swap_HFSPlusForkData(&srcRec
->forkData
.theFork
);
854 case kHFSPlusAttrExtents
:
855 /* Is there room for an extent record? */
856 if ((char *)srcRec
+ sizeof(HFSPlusAttrExtents
) > nextRecord
) {
857 if (direction
== kSwapBTNodeHostToBig
) {
858 panic("hfs_swap_HFSPlusBTInternalNode: attr extents #%d too big\n", srcDesc
->numRecords
-i
-1);
860 printf("hfs_swap_HFSPlusBTInternalNode: attr extents #%d too big\n", srcDesc
->numRecords
-i
-1);
862 return fsBTInvalidNodeErr
;
865 /* We're not swapping the reserved field */
867 for (j
= 0; j
< kHFSPlusExtentDensity
; j
++) {
868 srcRec
->overflowExtents
.extents
[j
].startBlock
=
869 SWAP_BE32(srcRec
->overflowExtents
.extents
[j
].startBlock
);
870 srcRec
->overflowExtents
.extents
[j
].blockCount
=
871 SWAP_BE32(srcRec
->overflowExtents
.extents
[j
].blockCount
);
875 if (direction
== kSwapBTNodeHostToBig
)
876 srcRec
->recordType
= SWAP_BE32(srcRec
->recordType
);
878 } else if (fileID
> kHFSFirstUserCatalogNodeID
) {
879 /* The only B-tree with a non-system CNID that we use is the hotfile B-tree */
883 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
884 /* Point to the start of the record we're currently checking. */
885 srcKey
= (HotFileKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
888 * Point to start of next (larger offset) record. We'll use this
889 * to be sure the current record doesn't overflow into the next
892 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
894 /* Make sure there is room for the key (HotFileKey) and data (u_int32_t) */
895 if ((char *)srcKey
+ sizeof(HotFileKey
) + sizeof(u_int32_t
) > nextRecord
) {
896 if (direction
== kSwapBTNodeHostToBig
) {
897 panic("hfs_swap_HFSPlusBTInternalNode: hotfile #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
899 printf("hfs_swap_HFSPlusBTInternalNode: hotfile #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
901 return fsBTInvalidNodeErr
;
904 /* Swap and sanity check the key length field */
905 if (direction
== kSwapBTNodeBigToHost
)
906 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
907 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
908 if (direction
== kSwapBTNodeHostToBig
) {
909 panic("hfs_swap_HFSPlusBTInternalNode: hotfile #%d incorrect keyLength %d\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
911 printf("hfs_swap_HFSPlusBTInternalNode: hotfile #%d incorrect keyLength %d\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
913 return fsBTInvalidNodeErr
;
915 srcRec
= (u_int32_t
*)((char *)srcKey
+ srcKey
->keyLength
+ sizeof(srcKey
->keyLength
));
916 if (direction
== kSwapBTNodeHostToBig
)
917 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
919 /* Don't swap srcKey->forkType */
920 /* Don't swap srcKey->pad */
922 srcKey
->temperature
= SWAP_BE32 (srcKey
->temperature
);
923 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
925 *((u_int32_t
*)srcRec
) = SWAP_BE32 (*((u_int32_t
*)srcRec
));
928 panic ("hfs_swap_HFSPlusBTInternalNode: fileID %u is not a system B-tree\n", fileID
);
937 hfs_swap_HFSBTInternalNode (
938 BlockDescriptor
*src
,
939 HFSCatalogNodeID fileID
,
940 enum HFSBTSwapDirection direction
943 BTNodeDescriptor
*srcDesc
= src
->buffer
;
944 u_int16_t
*srcOffs
= (u_int16_t
*)((char *)src
->buffer
+ (src
->blockSize
- (srcDesc
->numRecords
* sizeof (u_int16_t
))));
945 char *nextRecord
; /* Points to start of record following current one */
948 * i is an int32 because it needs to be negative to index the offset to free space.
949 * srcDesc->numRecords is a u_int16_t and is unlikely to become 32-bit so this should be ok.
954 if (fileID
== kHFSExtentsFileID
) {
955 HFSExtentKey
*srcKey
;
956 HFSExtentDescriptor
*srcRec
;
957 size_t recordSize
; /* Size of the data part of the record, or node number for index nodes */
959 if (srcDesc
->kind
== kBTIndexNode
)
960 recordSize
= sizeof(u_int32_t
);
962 recordSize
= sizeof(HFSExtentDescriptor
);
964 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
965 /* Point to the start of the record we're currently checking. */
966 srcKey
= (HFSExtentKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
969 * Point to start of next (larger offset) record. We'll use this
970 * to be sure the current record doesn't overflow into the next
973 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
976 * Make sure the key and data are within the buffer. Since both key
977 * and data are fixed size, this is relatively easy. Note that this
978 * relies on the keyLength being a constant; we verify the keyLength
981 if ((char *)srcKey
+ sizeof(HFSExtentKey
) + recordSize
> nextRecord
) {
982 if (direction
== kSwapBTNodeHostToBig
) {
983 panic("hfs_swap_HFSBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
985 printf("hfs_swap_HFSBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
987 return fsBTInvalidNodeErr
;
990 /* Don't swap srcKey->keyLength (it's only one byte), but do sanity check it */
991 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
992 if (direction
== kSwapBTNodeHostToBig
) {
993 panic("hfs_swap_HFSBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
995 printf("hfs_swap_HFSBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
997 return fsBTInvalidNodeErr
;
1000 /* Don't swap srcKey->forkType; it's only one byte */
1002 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
1003 srcKey
->startBlock
= SWAP_BE16 (srcKey
->startBlock
);
1005 /* Point to record data (round up to even byte boundary) */
1006 srcRec
= (HFSExtentDescriptor
*)((char *)srcKey
+ ((srcKey
->keyLength
+ 2) & ~1));
1008 if (srcDesc
->kind
== kBTIndexNode
) {
1009 /* For index nodes, the record data is just a child node number. */
1010 *((u_int32_t
*)srcRec
) = SWAP_BE32 (*((u_int32_t
*)srcRec
));
1012 /* Swap the extent data */
1013 for (j
= 0; j
< kHFSExtentDensity
; j
++) {
1014 srcRec
[j
].startBlock
= SWAP_BE16 (srcRec
[j
].startBlock
);
1015 srcRec
[j
].blockCount
= SWAP_BE16 (srcRec
[j
].blockCount
);
1020 } else if (fileID
== kHFSCatalogFileID
) {
1021 HFSCatalogKey
*srcKey
;
1023 unsigned expectedKeyLength
;
1025 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
1026 /* Point to the start of the record we're currently checking. */
1027 srcKey
= (HFSCatalogKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
1030 * Point to start of next (larger offset) record. We'll use this
1031 * to be sure the current record doesn't overflow into the next
1034 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
1037 * Make sure we can safely dereference the keyLength and parentID fields.
1038 * The value 8 below is 1 bytes for keyLength + 1 byte reserved + 4 bytes
1039 * for parentID + 1 byte for nodeName's length + 1 byte to round up the
1040 * record start to an even offset, which forms a minimal key.
1042 if ((char *)srcKey
+ 8 > nextRecord
) {
1043 if (direction
== kSwapBTNodeHostToBig
) {
1044 panic("hfs_swap_HFSBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
1046 printf("hfs_swap_HFSBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
1048 return fsBTInvalidNodeErr
;
1051 /* Don't swap srcKey->keyLength (it's only one byte), but do sanity check it */
1052 if (srcKey
->keyLength
< kHFSCatalogKeyMinimumLength
|| srcKey
->keyLength
> kHFSCatalogKeyMaximumLength
) {
1053 if (direction
== kSwapBTNodeHostToBig
) {
1054 panic("hfs_swap_HFSBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
1056 printf("hfs_swap_HFSBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
1058 return fsBTInvalidNodeErr
;
1061 /* Don't swap srcKey->reserved */
1063 srcKey
->parentID
= SWAP_BE32 (srcKey
->parentID
);
1065 /* Don't swap srcKey->nodeName */
1067 /* Make sure the keyLength is big enough for the key's content */
1068 if (srcDesc
->kind
== kBTIndexNode
)
1069 expectedKeyLength
= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
);
1071 expectedKeyLength
= srcKey
->nodeName
[0] + kHFSCatalogKeyMinimumLength
;
1072 if (srcKey
->keyLength
< expectedKeyLength
) {
1073 if (direction
== kSwapBTNodeHostToBig
) {
1074 panic("hfs_swap_HFSBTInternalNode: catalog record #%d keyLength=%u expected=%u\n",
1075 srcDesc
->numRecords
-i
, srcKey
->keyLength
, expectedKeyLength
);
1077 printf("hfs_swap_HFSBTInternalNode: catalog record #%d keyLength=%u expected=%u\n",
1078 srcDesc
->numRecords
-i
, srcKey
->keyLength
, expectedKeyLength
);
1080 return fsBTInvalidNodeErr
;
1083 /* Point to record data (round up to even byte boundary) */
1084 srcPtr
= (int16_t *)((char *)srcKey
+ ((srcKey
->keyLength
+ 2) & ~1));
1087 * Make sure that we can safely dereference the record's type field or
1088 * and index node's child node number.
1090 if ((char *)srcPtr
+ sizeof(u_int32_t
) > nextRecord
) {
1091 if (direction
== kSwapBTNodeHostToBig
) {
1092 panic("hfs_swap_HFSBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
1094 printf("hfs_swap_HFSBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
1096 return fsBTInvalidNodeErr
;
1100 * For index nodes, the record data is just the child's node number.
1101 * Skip over swapping the various types of catalog record.
1103 if (srcDesc
->kind
== kBTIndexNode
) {
1104 *((u_int32_t
*)srcPtr
) = SWAP_BE32 (*((u_int32_t
*)srcPtr
));
1108 /* Make sure the recordType is in native order before using it. */
1109 if (direction
== kSwapBTNodeBigToHost
)
1110 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
1112 if (srcPtr
[0] == kHFSFolderRecord
) {
1113 HFSCatalogFolder
*srcRec
= (HFSCatalogFolder
*)srcPtr
;
1114 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
1115 if (direction
== kSwapBTNodeHostToBig
) {
1116 panic("hfs_swap_HFSBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
1118 printf("hfs_swap_HFSBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
1120 return fsBTInvalidNodeErr
;
1123 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
1124 srcRec
->valence
= SWAP_BE16 (srcRec
->valence
);
1126 srcRec
->folderID
= SWAP_BE32 (srcRec
->folderID
);
1127 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
1128 srcRec
->modifyDate
= SWAP_BE32 (srcRec
->modifyDate
);
1129 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
1131 /* Don't swap srcRec->userInfo */
1132 /* Don't swap srcRec->finderInfo */
1133 /* Don't swap resserved array */
1135 } else if (srcPtr
[0] == kHFSFileRecord
) {
1136 HFSCatalogFile
*srcRec
= (HFSCatalogFile
*)srcPtr
;
1137 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
1138 if (direction
== kSwapBTNodeHostToBig
) {
1139 panic("hfs_swap_HFSBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
1141 printf("hfs_swap_HFSBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
1143 return fsBTInvalidNodeErr
;
1146 srcRec
->flags
= srcRec
->flags
;
1147 srcRec
->fileType
= srcRec
->fileType
;
1149 /* Don't swap srcRec->userInfo */
1151 srcRec
->fileID
= SWAP_BE32 (srcRec
->fileID
);
1153 srcRec
->dataStartBlock
= SWAP_BE16 (srcRec
->dataStartBlock
);
1154 srcRec
->dataLogicalSize
= SWAP_BE32 (srcRec
->dataLogicalSize
);
1155 srcRec
->dataPhysicalSize
= SWAP_BE32 (srcRec
->dataPhysicalSize
);
1157 srcRec
->rsrcStartBlock
= SWAP_BE16 (srcRec
->rsrcStartBlock
);
1158 srcRec
->rsrcLogicalSize
= SWAP_BE32 (srcRec
->rsrcLogicalSize
);
1159 srcRec
->rsrcPhysicalSize
= SWAP_BE32 (srcRec
->rsrcPhysicalSize
);
1161 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
1162 srcRec
->modifyDate
= SWAP_BE32 (srcRec
->modifyDate
);
1163 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
1165 /* Don't swap srcRec->finderInfo */
1167 srcRec
->clumpSize
= SWAP_BE16 (srcRec
->clumpSize
);
1169 /* Swap the two sets of extents as an array of six (three each) u_int16_t */
1170 for (j
= 0; j
< kHFSExtentDensity
* 2; j
++) {
1171 srcRec
->dataExtents
[j
].startBlock
= SWAP_BE16 (srcRec
->dataExtents
[j
].startBlock
);
1172 srcRec
->dataExtents
[j
].blockCount
= SWAP_BE16 (srcRec
->dataExtents
[j
].blockCount
);
1175 /* Don't swap srcRec->reserved */
1177 } else if ((srcPtr
[0] == kHFSFolderThreadRecord
) ||
1178 (srcPtr
[0] == kHFSFileThreadRecord
)) {
1179 HFSCatalogThread
*srcRec
= (HFSCatalogThread
*)srcPtr
;
1181 /* Make sure there is room for parentID and name length */
1182 if ((char *) &srcRec
->nodeName
[1] > nextRecord
) {
1183 if (direction
== kSwapBTNodeHostToBig
) {
1184 panic("hfs_swap_HFSBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
1186 printf("hfs_swap_HFSBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
1188 return fsBTInvalidNodeErr
;
1191 /* Don't swap srcRec->reserved array */
1193 srcRec
->parentID
= SWAP_BE32 (srcRec
->parentID
);
1195 /* Don't swap srcRec->nodeName */
1197 /* Make sure there is room for the name in the buffer */
1198 if ((char *) &srcRec
->nodeName
[srcRec
->nodeName
[0]] > nextRecord
) {
1199 if (direction
== kSwapBTNodeHostToBig
) {
1200 panic("hfs_swap_HFSBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
1202 printf("hfs_swap_HFSBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
1204 return fsBTInvalidNodeErr
;
1207 if (direction
== kSwapBTNodeHostToBig
) {
1208 panic("hfs_swap_HFSBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
1210 printf("hfs_swap_HFSBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
1212 return fsBTInvalidNodeErr
;
1215 /* We can swap the record type now that we're done using it */
1216 if (direction
== kSwapBTNodeHostToBig
)
1217 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
1221 panic ("hfs_swap_HFSBTInternalNode: fileID %u is not a system B-tree\n", fileID
);