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 static int hfs_swap_HFSPlusBTInternalNode (BlockDescriptor
*src
, HFSCatalogNodeID fileID
, enum HFSBTSwapDirection direction
);
53 static int hfs_swap_HFSBTInternalNode (BlockDescriptor
*src
, HFSCatalogNodeID fileID
, enum HFSBTSwapDirection direction
);
56 * hfs_swap_HFSPlusForkData
59 hfs_swap_HFSPlusForkData (
65 src
->logicalSize
= SWAP_BE64 (src
->logicalSize
);
67 src
->clumpSize
= SWAP_BE32 (src
->clumpSize
);
68 src
->totalBlocks
= SWAP_BE32 (src
->totalBlocks
);
70 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
71 src
->extents
[i
].startBlock
= SWAP_BE32 (src
->extents
[i
].startBlock
);
72 src
->extents
[i
].blockCount
= SWAP_BE32 (src
->extents
[i
].blockCount
);
79 * NOTE: This operation is not naturally symmetric.
80 * We have to determine which way we're swapping things.
86 enum HFSBTSwapDirection direction
,
87 u_int8_t allow_empty_node
90 BTNodeDescriptor
*srcDesc
= src
->buffer
;
91 u_int16_t
*srcOffs
= NULL
;
92 BTreeControlBlockPtr btcb
= (BTreeControlBlockPtr
)VTOF(vp
)->fcbBTCBPtr
;
97 if (direction
== kSwapBTNodeBigToHost
) {
98 printf ("BE -> Native Swap\n");
99 } else if (direction
== kSwapBTNodeHostToBig
) {
100 printf ("Native -> BE Swap\n");
101 } else if (direction
== kSwapBTNodeHeaderRecordOnly
) {
102 printf ("Not swapping descriptors\n");
104 panic ("hfs_swap_BTNode: This is impossible");
109 * If we are doing a swap from on-disk to in-memory, then swap the node
110 * descriptor and record offsets before we need to use them.
112 if (direction
== kSwapBTNodeBigToHost
) {
113 srcDesc
->fLink
= SWAP_BE32 (srcDesc
->fLink
);
114 srcDesc
->bLink
= SWAP_BE32 (srcDesc
->bLink
);
117 * When first opening a BTree, we have to read the header node before the
118 * control block is initialized. In this case, totalNodes will be zero,
119 * so skip the bounds checking. Also, we should ignore the header node when
120 * checking for invalid forwards and backwards links, since the header node's
121 * links can point back to itself legitimately.
123 if (btcb
->totalNodes
!= 0) {
124 if (srcDesc
->fLink
>= btcb
->totalNodes
) {
125 printf("hfs_swap_BTNode: invalid forward link (0x%08x >= 0x%08x)\n", srcDesc
->fLink
, btcb
->totalNodes
);
126 error
= fsBTInvalidHeaderErr
;
129 if (srcDesc
->bLink
>= btcb
->totalNodes
) {
130 printf("hfs_swap_BTNode: invalid backward link (0x%08x >= 0x%08x)\n", srcDesc
->bLink
, btcb
->totalNodes
);
131 error
= fsBTInvalidHeaderErr
;
135 if ((src
->blockNum
!= 0) && (srcDesc
->fLink
== (u_int32_t
) src
->blockNum
)) {
136 printf("hfs_swap_BTNode: invalid forward link (0x%08x == 0x%08x)\n",
137 srcDesc
->fLink
, (u_int32_t
) src
->blockNum
);
138 error
= fsBTInvalidHeaderErr
;
141 if ((src
->blockNum
!= 0) && (srcDesc
->bLink
== (u_int32_t
) src
->blockNum
)) {
142 printf("hfs_swap_BTNode: invalid backward link (0x%08x == 0x%08x)\n",
143 srcDesc
->bLink
, (u_int32_t
) src
->blockNum
);
144 error
= fsBTInvalidHeaderErr
;
151 * Check srcDesc->kind. Don't swap it because it's only one byte.
153 if (srcDesc
->kind
< kBTLeafNode
|| srcDesc
->kind
> kBTMapNode
) {
154 printf("hfs_swap_BTNode: invalid node kind (%d)\n", srcDesc
->kind
);
155 error
= fsBTInvalidHeaderErr
;
160 * Check srcDesc->height. Don't swap it because it's only one byte.
162 if (srcDesc
->height
> btcb
->treeDepth
) {
163 printf("hfs_swap_BTNode: invalid node height (%d)\n", srcDesc
->height
);
164 error
= fsBTInvalidHeaderErr
;
168 /* Don't swap srcDesc->reserved */
170 srcDesc
->numRecords
= SWAP_BE16 (srcDesc
->numRecords
);
173 * Swap the node offsets (including the free space one!).
175 srcOffs
= (u_int16_t
*)((char *)src
->buffer
+ (src
->blockSize
- ((srcDesc
->numRecords
+ 1) * sizeof (u_int16_t
))));
178 * Sanity check that the record offsets are within the node itself.
180 if ((char *)srcOffs
> ((char *)src
->buffer
+ src
->blockSize
) ||
181 (char *)srcOffs
< ((char *)src
->buffer
+ sizeof(BTNodeDescriptor
))) {
182 printf("hfs_swap_BTNode: invalid record count (0x%04X)\n", srcDesc
->numRecords
);
183 error
= fsBTInvalidHeaderErr
;
188 * Swap and sanity check each of the record offsets.
190 for (i
= 0; i
<= srcDesc
->numRecords
; i
++) {
191 srcOffs
[i
] = SWAP_BE16 (srcOffs
[i
]);
194 * Sanity check: must be even, and within the node itself.
196 * We may be called to swap an unused node, which contains all zeroes.
197 * Unused nodes are expected only when allow_empty_node is true.
198 * If it is false and record offset is zero, return error.
200 if ((srcOffs
[i
] & 1) || (
201 (allow_empty_node
== false) && (srcOffs
[i
] == 0)) ||
202 (srcOffs
[i
] < sizeof(BTNodeDescriptor
) && srcOffs
[i
] != 0) ||
203 (srcOffs
[i
] >= src
->blockSize
)) {
204 printf("hfs_swap_BTNode: record #%d invalid offset (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
205 error
= fsBTInvalidHeaderErr
;
210 * Make sure the offsets are strictly increasing. Note that we're looping over
211 * them backwards, hence the order in the comparison.
213 if ((i
!= 0) && (srcOffs
[i
] >= srcOffs
[i
-1])) {
214 printf("hfs_swap_BTNode: offsets %d and %d out of order (0x%04X, 0x%04X)\n",
215 srcDesc
->numRecords
-i
-1, srcDesc
->numRecords
-i
, srcOffs
[i
], srcOffs
[i
-1]);
216 error
= fsBTInvalidHeaderErr
;
223 * Swap the records (ordered by frequency of access)
225 if ((srcDesc
->kind
== kBTIndexNode
) ||
226 (srcDesc
-> kind
== kBTLeafNode
)) {
228 if (VTOVCB(vp
)->vcbSigWord
== kHFSPlusSigWord
) {
229 error
= hfs_swap_HFSPlusBTInternalNode (src
, VTOC(vp
)->c_fileid
, direction
);
231 error
= hfs_swap_HFSBTInternalNode (src
, VTOC(vp
)->c_fileid
, direction
);
234 if (error
) goto fail
;
236 } else if (srcDesc
-> kind
== kBTMapNode
) {
237 /* Don't swap the bitmaps, they'll be done in the bitmap routines */
239 } else if (srcDesc
-> kind
== kBTHeaderNode
) {
240 /* The header's offset is hard-wired because we cannot trust the offset pointers. */
241 BTHeaderRec
*srcHead
= (BTHeaderRec
*)((char *)src
->buffer
+ sizeof(BTNodeDescriptor
));
243 srcHead
->treeDepth
= SWAP_BE16 (srcHead
->treeDepth
);
245 srcHead
->rootNode
= SWAP_BE32 (srcHead
->rootNode
);
246 srcHead
->leafRecords
= SWAP_BE32 (srcHead
->leafRecords
);
247 srcHead
->firstLeafNode
= SWAP_BE32 (srcHead
->firstLeafNode
);
248 srcHead
->lastLeafNode
= SWAP_BE32 (srcHead
->lastLeafNode
);
250 srcHead
->nodeSize
= SWAP_BE16 (srcHead
->nodeSize
);
251 srcHead
->maxKeyLength
= SWAP_BE16 (srcHead
->maxKeyLength
);
253 srcHead
->totalNodes
= SWAP_BE32 (srcHead
->totalNodes
);
254 srcHead
->freeNodes
= SWAP_BE32 (srcHead
->freeNodes
);
256 srcHead
->clumpSize
= SWAP_BE32 (srcHead
->clumpSize
);
257 srcHead
->attributes
= SWAP_BE32 (srcHead
->attributes
);
259 /* Don't swap srcHead->reserved1 */
260 /* Don't swap srcHead->btreeType; it's only one byte */
261 /* Don't swap srcHead->reserved2 */
262 /* Don't swap srcHead->reserved3 */
263 /* Don't swap bitmap */
267 * If we are doing a swap from in-memory to on-disk, then swap the node
268 * descriptor and record offsets after we're done using them.
270 if (direction
== kSwapBTNodeHostToBig
) {
272 * Sanity check and swap the forward and backward links.
273 * Ignore the header node since its forward and backwards links can legitimately
276 if (srcDesc
->fLink
>= btcb
->totalNodes
) {
277 panic("hfs_UNswap_BTNode: invalid forward link (0x%08X)\n", srcDesc
->fLink
);
278 error
= fsBTInvalidHeaderErr
;
281 if ((src
->blockNum
!= 0) && (srcDesc
->fLink
== (u_int32_t
) src
->blockNum
)) {
282 panic ("hfs_UNswap_BTNode: invalid forward link (0x%08x == 0x%08x)\n",
283 srcDesc
->fLink
, (u_int32_t
) src
->blockNum
);
284 error
= fsBTInvalidHeaderErr
;
288 if (srcDesc
->bLink
>= btcb
->totalNodes
) {
289 panic("hfs_UNswap_BTNode: invalid backward link (0x%08X)\n", srcDesc
->bLink
);
290 error
= fsBTInvalidHeaderErr
;
293 if ((src
->blockNum
!= 0) && (srcDesc
->bLink
== (u_int32_t
) src
->blockNum
)) {
294 panic ("hfs_UNswap_BTNode: invalid backward link (0x%08x == 0x%08x)\n",
295 srcDesc
->bLink
, (u_int32_t
) src
->blockNum
);
296 error
= fsBTInvalidHeaderErr
;
301 srcDesc
->fLink
= SWAP_BE32 (srcDesc
->fLink
);
302 srcDesc
->bLink
= SWAP_BE32 (srcDesc
->bLink
);
305 * Check srcDesc->kind. Don't swap it because it's only one byte.
307 if (srcDesc
->kind
< kBTLeafNode
|| srcDesc
->kind
> kBTMapNode
) {
308 panic("hfs_UNswap_BTNode: invalid node kind (%d)\n", srcDesc
->kind
);
309 error
= fsBTInvalidHeaderErr
;
314 * Check srcDesc->height. Don't swap it because it's only one byte.
316 if (srcDesc
->height
> btcb
->treeDepth
) {
317 panic("hfs_UNswap_BTNode: invalid node height (%d)\n", srcDesc
->height
);
318 error
= fsBTInvalidHeaderErr
;
322 /* Don't swap srcDesc->reserved */
325 * Swap the node offsets (including the free space one!).
327 srcOffs
= (u_int16_t
*)((char *)src
->buffer
+ (src
->blockSize
- ((srcDesc
->numRecords
+ 1) * sizeof (u_int16_t
))));
330 * Sanity check that the record offsets are within the node itself.
332 if ((char *)srcOffs
> ((char *)src
->buffer
+ src
->blockSize
) ||
333 (char *)srcOffs
< ((char *)src
->buffer
+ sizeof(BTNodeDescriptor
))) {
334 panic("hfs_UNswap_BTNode: invalid record count (0x%04X)\n", srcDesc
->numRecords
);
335 error
= fsBTInvalidHeaderErr
;
340 * Swap and sanity check each of the record offsets.
342 for (i
= 0; i
<= srcDesc
->numRecords
; i
++) {
344 * Sanity check: must be even, and within the node itself.
346 * We may be called to swap an unused node, which contains all zeroes.
347 * This can happen when the last record from a node gets deleted.
348 * This is why we allow the record offset to be zero.
349 * Unused nodes are expected only when allow_empty_node is true
350 * (the caller should set it to true for kSwapBTNodeBigToHost).
352 if ((srcOffs
[i
] & 1) ||
353 ((allow_empty_node
== false) && (srcOffs
[i
] == 0)) ||
354 (srcOffs
[i
] < sizeof(BTNodeDescriptor
) && srcOffs
[i
] != 0) ||
355 (srcOffs
[i
] >= src
->blockSize
)) {
356 panic("hfs_UNswap_BTNode: record #%d invalid offset (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
357 error
= fsBTInvalidHeaderErr
;
362 * Make sure the offsets are strictly increasing. Note that we're looping over
363 * them backwards, hence the order in the comparison.
365 if ((i
< srcDesc
->numRecords
) && (srcOffs
[i
+1] >= srcOffs
[i
])) {
366 panic("hfs_UNswap_BTNode: offsets %d and %d out of order (0x%04X, 0x%04X)\n",
367 srcDesc
->numRecords
-i
-2, srcDesc
->numRecords
-i
-1, srcOffs
[i
+1], srcOffs
[i
]);
368 error
= fsBTInvalidHeaderErr
;
372 srcOffs
[i
] = SWAP_BE16 (srcOffs
[i
]);
375 srcDesc
->numRecords
= SWAP_BE16 (srcDesc
->numRecords
);
381 * Log some useful information about where the corrupt node is.
383 printf("node=%lld fileID=%u volume=%s device=%s\n", src
->blockNum
, VTOC(vp
)->c_fileid
,
384 VTOVCB(vp
)->vcbVN
, vfs_statfs(vnode_mount(vp
))->f_mntfromname
);
385 hfs_mark_volume_inconsistent(VTOVCB(vp
));
392 hfs_swap_HFSPlusBTInternalNode (
393 BlockDescriptor
*src
,
394 HFSCatalogNodeID fileID
,
395 enum HFSBTSwapDirection direction
398 BTNodeDescriptor
*srcDesc
= src
->buffer
;
399 u_int16_t
*srcOffs
= (u_int16_t
*)((char *)src
->buffer
+ (src
->blockSize
- (srcDesc
->numRecords
* sizeof (u_int16_t
))));
400 char *nextRecord
; /* Points to start of record following current one */
403 * i is an int32 because it needs to be negative to index the offset to free space.
404 * srcDesc->numRecords is a u_int16_t and is unlikely to become 32-bit so this should be ok.
410 if (fileID
== kHFSExtentsFileID
) {
411 HFSPlusExtentKey
*srcKey
;
412 HFSPlusExtentDescriptor
*srcRec
;
413 size_t recordSize
; /* Size of the data part of the record, or node number for index nodes */
415 if (srcDesc
->kind
== kBTIndexNode
)
416 recordSize
= sizeof(u_int32_t
);
418 recordSize
= sizeof(HFSPlusExtentDescriptor
);
420 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
421 /* Point to the start of the record we're currently checking. */
422 srcKey
= (HFSPlusExtentKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
425 * Point to start of next (larger offset) record. We'll use this
426 * to be sure the current record doesn't overflow into the next
429 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
432 * Make sure the key and data are within the buffer. Since both key
433 * and data are fixed size, this is relatively easy. Note that this
434 * relies on the keyLength being a constant; we verify the keyLength
437 if ((char *)srcKey
+ sizeof(HFSPlusExtentKey
) + recordSize
> nextRecord
) {
438 if (direction
== kSwapBTNodeHostToBig
) {
439 panic("hfs_swap_HFSPlusBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
441 printf("hfs_swap_HFSPlusBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
443 return fsBTInvalidNodeErr
;
446 if (direction
== kSwapBTNodeBigToHost
)
447 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
448 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
449 if (direction
== kSwapBTNodeHostToBig
) {
450 panic("hfs_swap_HFSPlusBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
452 printf("hfs_swap_HFSPlusBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
454 return fsBTInvalidNodeErr
;
456 srcRec
= (HFSPlusExtentDescriptor
*)((char *)srcKey
+ srcKey
->keyLength
+ sizeof(srcKey
->keyLength
));
457 if (direction
== kSwapBTNodeHostToBig
)
458 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
460 /* Don't swap srcKey->forkType; it's only one byte */
461 /* Don't swap srcKey->pad */
463 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
464 srcKey
->startBlock
= SWAP_BE32 (srcKey
->startBlock
);
466 if (srcDesc
->kind
== kBTIndexNode
) {
467 /* For index nodes, the record data is just a child node number. */
468 *((u_int32_t
*)srcRec
) = SWAP_BE32 (*((u_int32_t
*)srcRec
));
470 /* Swap the extent data */
471 for (j
= 0; j
< kHFSPlusExtentDensity
; j
++) {
472 srcRec
[j
].startBlock
= SWAP_BE32 (srcRec
[j
].startBlock
);
473 srcRec
[j
].blockCount
= SWAP_BE32 (srcRec
[j
].blockCount
);
478 } else if (fileID
== kHFSCatalogFileID
) {
479 HFSPlusCatalogKey
*srcKey
;
483 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
484 /* Point to the start of the record we're currently checking. */
485 srcKey
= (HFSPlusCatalogKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
488 * Point to start of next (larger offset) record. We'll use this
489 * to be sure the current record doesn't overflow into the next
492 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
495 * Make sure we can safely dereference the keyLength and parentID fields.
497 if ((char *)srcKey
+ offsetof(HFSPlusCatalogKey
, nodeName
.unicode
[0]) > nextRecord
) {
498 if (direction
== kSwapBTNodeHostToBig
) {
499 panic("hfs_swap_HFSPlusBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
501 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
503 return fsBTInvalidNodeErr
;
507 * Swap and sanity check the key length
509 if (direction
== kSwapBTNodeBigToHost
)
510 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
511 keyLength
= srcKey
->keyLength
; /* Put it in a local (native order) because we use it several times */
512 if (direction
== kSwapBTNodeHostToBig
)
513 srcKey
->keyLength
= SWAP_BE16 (keyLength
);
515 /* Sanity check the key length */
516 if (keyLength
< kHFSPlusCatalogKeyMinimumLength
|| keyLength
> kHFSPlusCatalogKeyMaximumLength
) {
517 if (direction
== kSwapBTNodeHostToBig
) {
518 panic("hfs_swap_HFSPlusBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
520 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
522 return fsBTInvalidNodeErr
;
526 * Make sure that we can safely dereference the record's type field or
527 * an index node's child node number.
529 srcPtr
= (int16_t *)((char *)srcKey
+ keyLength
+ sizeof(srcKey
->keyLength
));
530 if ((char *)srcPtr
+ sizeof(u_int32_t
) > nextRecord
) {
531 if (direction
== kSwapBTNodeHostToBig
) {
532 panic("hfs_swap_HFSPlusBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
534 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
536 return fsBTInvalidNodeErr
;
539 srcKey
->parentID
= SWAP_BE32 (srcKey
->parentID
);
542 * Swap and sanity check the key's node name
544 if (direction
== kSwapBTNodeBigToHost
)
545 srcKey
->nodeName
.length
= SWAP_BE16 (srcKey
->nodeName
.length
);
546 /* Make sure name length is consistent with key length */
547 if (keyLength
< sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
548 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0])) {
549 if (direction
== kSwapBTNodeHostToBig
) {
550 panic("hfs_swap_HFSPlusBTInternalNode: catalog record #%d keyLength=%d expected=%lu\n",
551 srcDesc
->numRecords
-i
, keyLength
, sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
552 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0]));
554 printf("hfs_swap_HFSPlusBTInternalNode: catalog record #%d keyLength=%d expected=%lu\n",
555 srcDesc
->numRecords
-i
, keyLength
, sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
556 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0]));
558 return fsBTInvalidNodeErr
;
560 for (j
= 0; j
< srcKey
->nodeName
.length
; j
++) {
561 srcKey
->nodeName
.unicode
[j
] = SWAP_BE16 (srcKey
->nodeName
.unicode
[j
]);
563 if (direction
== kSwapBTNodeHostToBig
)
564 srcKey
->nodeName
.length
= SWAP_BE16 (srcKey
->nodeName
.length
);
567 * For index nodes, the record data is just the child's node number.
568 * Skip over swapping the various types of catalog record.
570 if (srcDesc
->kind
== kBTIndexNode
) {
571 *((u_int32_t
*)srcPtr
) = SWAP_BE32 (*((u_int32_t
*)srcPtr
));
575 /* Make sure the recordType is in native order before using it. */
576 if (direction
== kSwapBTNodeBigToHost
)
577 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
579 if (srcPtr
[0] == kHFSPlusFolderRecord
) {
580 HFSPlusCatalogFolder
*srcRec
= (HFSPlusCatalogFolder
*)srcPtr
;
581 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
582 if (direction
== kSwapBTNodeHostToBig
) {
583 panic("hfs_swap_HFSPlusBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
585 printf("hfs_swap_HFSPlusBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
587 return fsBTInvalidNodeErr
;
590 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
591 srcRec
->valence
= SWAP_BE32 (srcRec
->valence
);
592 srcRec
->folderID
= SWAP_BE32 (srcRec
->folderID
);
593 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
594 srcRec
->contentModDate
= SWAP_BE32 (srcRec
->contentModDate
);
595 srcRec
->attributeModDate
= SWAP_BE32 (srcRec
->attributeModDate
);
596 srcRec
->accessDate
= SWAP_BE32 (srcRec
->accessDate
);
597 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
599 srcRec
->bsdInfo
.ownerID
= SWAP_BE32 (srcRec
->bsdInfo
.ownerID
);
600 srcRec
->bsdInfo
.groupID
= SWAP_BE32 (srcRec
->bsdInfo
.groupID
);
602 /* Don't swap srcRec->bsdInfo.adminFlags; it's only one byte */
603 /* Don't swap srcRec->bsdInfo.ownerFlags; it's only one byte */
605 srcRec
->bsdInfo
.fileMode
= SWAP_BE16 (srcRec
->bsdInfo
.fileMode
);
606 srcRec
->bsdInfo
.special
.iNodeNum
= SWAP_BE32 (srcRec
->bsdInfo
.special
.iNodeNum
);
608 srcRec
->textEncoding
= SWAP_BE32 (srcRec
->textEncoding
);
610 /* Don't swap srcRec->userInfo */
611 /* Don't swap srcRec->finderInfo */
612 srcRec
->folderCount
= SWAP_BE32 (srcRec
->folderCount
);
614 } else if (srcPtr
[0] == kHFSPlusFileRecord
) {
615 HFSPlusCatalogFile
*srcRec
= (HFSPlusCatalogFile
*)srcPtr
;
616 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
617 if (direction
== kSwapBTNodeHostToBig
) {
618 panic("hfs_swap_HFSPlusBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
620 printf("hfs_swap_HFSPlusBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
622 return fsBTInvalidNodeErr
;
625 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
627 srcRec
->fileID
= SWAP_BE32 (srcRec
->fileID
);
629 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
630 srcRec
->contentModDate
= SWAP_BE32 (srcRec
->contentModDate
);
631 srcRec
->attributeModDate
= SWAP_BE32 (srcRec
->attributeModDate
);
632 srcRec
->accessDate
= SWAP_BE32 (srcRec
->accessDate
);
633 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
635 srcRec
->bsdInfo
.ownerID
= SWAP_BE32 (srcRec
->bsdInfo
.ownerID
);
636 srcRec
->bsdInfo
.groupID
= SWAP_BE32 (srcRec
->bsdInfo
.groupID
);
638 /* Don't swap srcRec->bsdInfo.adminFlags; it's only one byte */
639 /* Don't swap srcRec->bsdInfo.ownerFlags; it's only one byte */
641 srcRec
->bsdInfo
.fileMode
= SWAP_BE16 (srcRec
->bsdInfo
.fileMode
);
642 srcRec
->bsdInfo
.special
.iNodeNum
= SWAP_BE32 (srcRec
->bsdInfo
.special
.iNodeNum
);
644 srcRec
->textEncoding
= SWAP_BE32 (srcRec
->textEncoding
);
646 /* If kHFSHasLinkChainBit is set, reserved1 is hl_FirstLinkID.
647 * In all other context, it is expected to be zero.
649 srcRec
->reserved1
= SWAP_BE32 (srcRec
->reserved1
);
651 /* Don't swap srcRec->userInfo */
652 /* Don't swap srcRec->finderInfo */
653 /* Don't swap srcRec->reserved2 */
655 hfs_swap_HFSPlusForkData (&srcRec
->dataFork
);
656 hfs_swap_HFSPlusForkData (&srcRec
->resourceFork
);
658 } else if ((srcPtr
[0] == kHFSPlusFolderThreadRecord
) ||
659 (srcPtr
[0] == kHFSPlusFileThreadRecord
)) {
662 * Make sure there is room for parentID and name length.
664 HFSPlusCatalogThread
*srcRec
= (HFSPlusCatalogThread
*)srcPtr
;
665 if ((char *) &srcRec
->nodeName
.unicode
[0] > nextRecord
) {
666 if (direction
== kSwapBTNodeHostToBig
) {
667 panic("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
669 printf("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
671 return fsBTInvalidNodeErr
;
674 /* Don't swap srcRec->reserved */
676 srcRec
->parentID
= SWAP_BE32 (srcRec
->parentID
);
678 if (direction
== kSwapBTNodeBigToHost
)
679 srcRec
->nodeName
.length
= SWAP_BE16 (srcRec
->nodeName
.length
);
682 * Make sure there is room for the name in the buffer.
683 * Then swap the characters of the name itself.
685 if ((char *) &srcRec
->nodeName
.unicode
[srcRec
->nodeName
.length
] > nextRecord
) {
686 if (direction
== kSwapBTNodeHostToBig
) {
687 panic("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
689 printf("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
691 return fsBTInvalidNodeErr
;
693 for (j
= 0; j
< srcRec
->nodeName
.length
; j
++) {
694 srcRec
->nodeName
.unicode
[j
] = SWAP_BE16 (srcRec
->nodeName
.unicode
[j
]);
697 if (direction
== kSwapBTNodeHostToBig
)
698 srcRec
->nodeName
.length
= SWAP_BE16 (srcRec
->nodeName
.length
);
701 if (direction
== kSwapBTNodeHostToBig
) {
702 panic("hfs_swap_HFSPlusBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
704 printf("hfs_swap_HFSPlusBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
706 return fsBTInvalidNodeErr
;
709 /* We can swap the record type now that we're done using it. */
710 if (direction
== kSwapBTNodeHostToBig
)
711 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
714 } else if (fileID
== kHFSAttributesFileID
) {
715 HFSPlusAttrKey
*srcKey
;
716 HFSPlusAttrRecord
*srcRec
;
718 u_int32_t attrSize
= 0;
720 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
721 /* Point to the start of the record we're currently checking. */
722 srcKey
= (HFSPlusAttrKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
725 * Point to start of next (larger offset) record. We'll use this
726 * to be sure the current record doesn't overflow into the next
729 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
731 /* Make sure there is room in the buffer for a minimal key */
732 if ((char *) &srcKey
->attrName
[1] > nextRecord
) {
733 if (direction
== kSwapBTNodeHostToBig
) {
734 panic("hfs_swap_HFSPlusBTInternalNode: attr key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
736 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
738 return fsBTInvalidNodeErr
;
741 /* Swap the key length field */
742 if (direction
== kSwapBTNodeBigToHost
)
743 srcKey
->keyLength
= SWAP_BE16(srcKey
->keyLength
);
744 keyLength
= srcKey
->keyLength
; /* Keep a copy in native order */
745 if (direction
== kSwapBTNodeHostToBig
)
746 srcKey
->keyLength
= SWAP_BE16(srcKey
->keyLength
);
749 * Make sure that we can safely dereference the record's type field or
750 * an index node's child node number.
752 srcRec
= (HFSPlusAttrRecord
*)((char *)srcKey
+ keyLength
+ sizeof(srcKey
->keyLength
));
753 if ((char *)srcRec
+ sizeof(u_int32_t
) > nextRecord
) {
754 if (direction
== kSwapBTNodeHostToBig
) {
755 panic("hfs_swap_HFSPlusBTInternalNode: attr key #%d too big (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
757 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d too big (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
759 return fsBTInvalidNodeErr
;
762 srcKey
->fileID
= SWAP_BE32(srcKey
->fileID
);
763 srcKey
->startBlock
= SWAP_BE32(srcKey
->startBlock
);
766 * Swap and check the attribute name
768 if (direction
== kSwapBTNodeBigToHost
)
769 srcKey
->attrNameLen
= SWAP_BE16(srcKey
->attrNameLen
);
770 /* Sanity check the attribute name length */
771 if (srcKey
->attrNameLen
> kHFSMaxAttrNameLen
|| keyLength
< (kHFSPlusAttrKeyMinimumLength
+ sizeof(u_int16_t
)*srcKey
->attrNameLen
)) {
772 if (direction
== kSwapBTNodeHostToBig
) {
773 panic("hfs_swap_HFSPlusBTInternalNode: attr key #%d keyLength=%d attrNameLen=%d\n", srcDesc
->numRecords
-i
-1, keyLength
, srcKey
->attrNameLen
);
775 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d keyLength=%d attrNameLen=%d\n", srcDesc
->numRecords
-i
-1, keyLength
, srcKey
->attrNameLen
);
777 return fsBTInvalidNodeErr
;
779 for (j
= 0; j
< srcKey
->attrNameLen
; j
++)
780 srcKey
->attrName
[j
] = SWAP_BE16(srcKey
->attrName
[j
]);
781 if (direction
== kSwapBTNodeHostToBig
)
782 srcKey
->attrNameLen
= SWAP_BE16(srcKey
->attrNameLen
);
785 * For index nodes, the record data is just the child's node number.
786 * Skip over swapping the various types of attribute record.
788 if (srcDesc
->kind
== kBTIndexNode
) {
789 *((u_int32_t
*)srcRec
) = SWAP_BE32 (*((u_int32_t
*)srcRec
));
793 /* Swap the record data */
794 if (direction
== kSwapBTNodeBigToHost
)
795 srcRec
->recordType
= SWAP_BE32(srcRec
->recordType
);
796 switch (srcRec
->recordType
) {
797 case kHFSPlusAttrInlineData
:
798 /* Is there room for the inline data header? */
799 if ((char *) &srcRec
->attrData
.attrData
[0] > nextRecord
) {
800 if (direction
== kSwapBTNodeHostToBig
) {
801 panic("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big\n", srcDesc
->numRecords
-i
-1);
803 printf("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big\n", srcDesc
->numRecords
-i
-1);
805 return fsBTInvalidNodeErr
;
808 /* We're not swapping the reserved fields */
810 /* Swap the attribute size */
811 if (direction
== kSwapBTNodeHostToBig
)
812 attrSize
= srcRec
->attrData
.attrSize
;
813 srcRec
->attrData
.attrSize
= SWAP_BE32(srcRec
->attrData
.attrSize
);
814 if (direction
== kSwapBTNodeBigToHost
)
815 attrSize
= srcRec
->attrData
.attrSize
;
817 /* Is there room for the inline attribute data? */
818 if ((char *) &srcRec
->attrData
.attrData
[attrSize
] > nextRecord
) {
819 if (direction
== kSwapBTNodeHostToBig
) {
820 panic("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big (attrSize=%u)\n", srcDesc
->numRecords
-i
-1, attrSize
);
822 printf("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big (attrSize=%u)\n", srcDesc
->numRecords
-i
-1, attrSize
);
824 return fsBTInvalidNodeErr
;
827 /* Not swapping the attribute data itself */
830 case kHFSPlusAttrForkData
:
831 /* Is there room for the fork data record? */
832 if ((char *)srcRec
+ sizeof(HFSPlusAttrForkData
) > nextRecord
) {
833 if (direction
== kSwapBTNodeHostToBig
) {
834 panic("hfs_swap_HFSPlusBTInternalNode: attr fork data #%d too big\n", srcDesc
->numRecords
-i
-1);
836 printf("hfs_swap_HFSPlusBTInternalNode: attr fork data #%d too big\n", srcDesc
->numRecords
-i
-1);
838 return fsBTInvalidNodeErr
;
841 /* We're not swapping the reserved field */
843 hfs_swap_HFSPlusForkData(&srcRec
->forkData
.theFork
);
846 case kHFSPlusAttrExtents
:
847 /* Is there room for an extent record? */
848 if ((char *)srcRec
+ sizeof(HFSPlusAttrExtents
) > nextRecord
) {
849 if (direction
== kSwapBTNodeHostToBig
) {
850 panic("hfs_swap_HFSPlusBTInternalNode: attr extents #%d too big\n", srcDesc
->numRecords
-i
-1);
852 printf("hfs_swap_HFSPlusBTInternalNode: attr extents #%d too big\n", srcDesc
->numRecords
-i
-1);
854 return fsBTInvalidNodeErr
;
857 /* We're not swapping the reserved field */
859 for (j
= 0; j
< kHFSPlusExtentDensity
; j
++) {
860 srcRec
->overflowExtents
.extents
[j
].startBlock
=
861 SWAP_BE32(srcRec
->overflowExtents
.extents
[j
].startBlock
);
862 srcRec
->overflowExtents
.extents
[j
].blockCount
=
863 SWAP_BE32(srcRec
->overflowExtents
.extents
[j
].blockCount
);
867 if (direction
== kSwapBTNodeHostToBig
)
868 srcRec
->recordType
= SWAP_BE32(srcRec
->recordType
);
870 } else if (fileID
> kHFSFirstUserCatalogNodeID
) {
871 /* The only B-tree with a non-system CNID that we use is the hotfile B-tree */
875 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
876 /* Point to the start of the record we're currently checking. */
877 srcKey
= (HotFileKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
880 * Point to start of next (larger offset) record. We'll use this
881 * to be sure the current record doesn't overflow into the next
884 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
886 /* Make sure there is room for the key (HotFileKey) and data (u_int32_t) */
887 if ((char *)srcKey
+ sizeof(HotFileKey
) + sizeof(u_int32_t
) > nextRecord
) {
888 if (direction
== kSwapBTNodeHostToBig
) {
889 panic("hfs_swap_HFSPlusBTInternalNode: hotfile #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
891 printf("hfs_swap_HFSPlusBTInternalNode: hotfile #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
893 return fsBTInvalidNodeErr
;
896 /* Swap and sanity check the key length field */
897 if (direction
== kSwapBTNodeBigToHost
)
898 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
899 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
900 if (direction
== kSwapBTNodeHostToBig
) {
901 panic("hfs_swap_HFSPlusBTInternalNode: hotfile #%d incorrect keyLength %d\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
903 printf("hfs_swap_HFSPlusBTInternalNode: hotfile #%d incorrect keyLength %d\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
905 return fsBTInvalidNodeErr
;
907 srcRec
= (u_int32_t
*)((char *)srcKey
+ srcKey
->keyLength
+ sizeof(srcKey
->keyLength
));
908 if (direction
== kSwapBTNodeHostToBig
)
909 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
911 /* Don't swap srcKey->forkType */
912 /* Don't swap srcKey->pad */
914 srcKey
->temperature
= SWAP_BE32 (srcKey
->temperature
);
915 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
917 *((u_int32_t
*)srcRec
) = SWAP_BE32 (*((u_int32_t
*)srcRec
));
920 panic ("hfs_swap_HFSPlusBTInternalNode: fileID %u is not a system B-tree\n", fileID
);
928 hfs_swap_HFSBTInternalNode (
929 BlockDescriptor
*src
,
930 HFSCatalogNodeID fileID
,
931 enum HFSBTSwapDirection direction
934 BTNodeDescriptor
*srcDesc
= src
->buffer
;
935 u_int16_t
*srcOffs
= (u_int16_t
*)((char *)src
->buffer
+ (src
->blockSize
- (srcDesc
->numRecords
* sizeof (u_int16_t
))));
936 char *nextRecord
; /* Points to start of record following current one */
939 * i is an int32 because it needs to be negative to index the offset to free space.
940 * srcDesc->numRecords is a u_int16_t and is unlikely to become 32-bit so this should be ok.
945 if (fileID
== kHFSExtentsFileID
) {
946 HFSExtentKey
*srcKey
;
947 HFSExtentDescriptor
*srcRec
;
948 size_t recordSize
; /* Size of the data part of the record, or node number for index nodes */
950 if (srcDesc
->kind
== kBTIndexNode
)
951 recordSize
= sizeof(u_int32_t
);
953 recordSize
= sizeof(HFSExtentDescriptor
);
955 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
956 /* Point to the start of the record we're currently checking. */
957 srcKey
= (HFSExtentKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
960 * Point to start of next (larger offset) record. We'll use this
961 * to be sure the current record doesn't overflow into the next
964 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
967 * Make sure the key and data are within the buffer. Since both key
968 * and data are fixed size, this is relatively easy. Note that this
969 * relies on the keyLength being a constant; we verify the keyLength
972 if ((char *)srcKey
+ sizeof(HFSExtentKey
) + recordSize
> nextRecord
) {
973 if (direction
== kSwapBTNodeHostToBig
) {
974 panic("hfs_swap_HFSBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
976 printf("hfs_swap_HFSBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
978 return fsBTInvalidNodeErr
;
981 /* Don't swap srcKey->keyLength (it's only one byte), but do sanity check it */
982 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
983 if (direction
== kSwapBTNodeHostToBig
) {
984 panic("hfs_swap_HFSBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
986 printf("hfs_swap_HFSBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
988 return fsBTInvalidNodeErr
;
991 /* Don't swap srcKey->forkType; it's only one byte */
993 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
994 srcKey
->startBlock
= SWAP_BE16 (srcKey
->startBlock
);
996 /* Point to record data (round up to even byte boundary) */
997 srcRec
= (HFSExtentDescriptor
*)((char *)srcKey
+ ((srcKey
->keyLength
+ 2) & ~1));
999 if (srcDesc
->kind
== kBTIndexNode
) {
1000 /* For index nodes, the record data is just a child node number. */
1001 *((u_int32_t
*)srcRec
) = SWAP_BE32 (*((u_int32_t
*)srcRec
));
1003 /* Swap the extent data */
1004 for (j
= 0; j
< kHFSExtentDensity
; j
++) {
1005 srcRec
[j
].startBlock
= SWAP_BE16 (srcRec
[j
].startBlock
);
1006 srcRec
[j
].blockCount
= SWAP_BE16 (srcRec
[j
].blockCount
);
1011 } else if (fileID
== kHFSCatalogFileID
) {
1012 HFSCatalogKey
*srcKey
;
1014 unsigned expectedKeyLength
;
1016 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
1017 /* Point to the start of the record we're currently checking. */
1018 srcKey
= (HFSCatalogKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
1021 * Point to start of next (larger offset) record. We'll use this
1022 * to be sure the current record doesn't overflow into the next
1025 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
1028 * Make sure we can safely dereference the keyLength and parentID fields.
1029 * The value 8 below is 1 bytes for keyLength + 1 byte reserved + 4 bytes
1030 * for parentID + 1 byte for nodeName's length + 1 byte to round up the
1031 * record start to an even offset, which forms a minimal key.
1033 if ((char *)srcKey
+ 8 > nextRecord
) {
1034 if (direction
== kSwapBTNodeHostToBig
) {
1035 panic("hfs_swap_HFSBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
1037 printf("hfs_swap_HFSBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
1039 return fsBTInvalidNodeErr
;
1042 /* Don't swap srcKey->keyLength (it's only one byte), but do sanity check it */
1043 if (srcKey
->keyLength
< kHFSCatalogKeyMinimumLength
|| srcKey
->keyLength
> kHFSCatalogKeyMaximumLength
) {
1044 if (direction
== kSwapBTNodeHostToBig
) {
1045 panic("hfs_swap_HFSBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
1047 printf("hfs_swap_HFSBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
1049 return fsBTInvalidNodeErr
;
1052 /* Don't swap srcKey->reserved */
1054 srcKey
->parentID
= SWAP_BE32 (srcKey
->parentID
);
1056 /* Don't swap srcKey->nodeName */
1058 /* Make sure the keyLength is big enough for the key's content */
1059 if (srcDesc
->kind
== kBTIndexNode
)
1060 expectedKeyLength
= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
);
1062 expectedKeyLength
= srcKey
->nodeName
[0] + kHFSCatalogKeyMinimumLength
;
1063 if (srcKey
->keyLength
< expectedKeyLength
) {
1064 if (direction
== kSwapBTNodeHostToBig
) {
1065 panic("hfs_swap_HFSBTInternalNode: catalog record #%d keyLength=%u expected=%u\n",
1066 srcDesc
->numRecords
-i
, srcKey
->keyLength
, expectedKeyLength
);
1068 printf("hfs_swap_HFSBTInternalNode: catalog record #%d keyLength=%u expected=%u\n",
1069 srcDesc
->numRecords
-i
, srcKey
->keyLength
, expectedKeyLength
);
1071 return fsBTInvalidNodeErr
;
1074 /* Point to record data (round up to even byte boundary) */
1075 srcPtr
= (int16_t *)((char *)srcKey
+ ((srcKey
->keyLength
+ 2) & ~1));
1078 * Make sure that we can safely dereference the record's type field or
1079 * and index node's child node number.
1081 if ((char *)srcPtr
+ sizeof(u_int32_t
) > nextRecord
) {
1082 if (direction
== kSwapBTNodeHostToBig
) {
1083 panic("hfs_swap_HFSBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
1085 printf("hfs_swap_HFSBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
1087 return fsBTInvalidNodeErr
;
1091 * For index nodes, the record data is just the child's node number.
1092 * Skip over swapping the various types of catalog record.
1094 if (srcDesc
->kind
== kBTIndexNode
) {
1095 *((u_int32_t
*)srcPtr
) = SWAP_BE32 (*((u_int32_t
*)srcPtr
));
1099 /* Make sure the recordType is in native order before using it. */
1100 if (direction
== kSwapBTNodeBigToHost
)
1101 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
1103 if (srcPtr
[0] == kHFSFolderRecord
) {
1104 HFSCatalogFolder
*srcRec
= (HFSCatalogFolder
*)srcPtr
;
1105 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
1106 if (direction
== kSwapBTNodeHostToBig
) {
1107 panic("hfs_swap_HFSBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
1109 printf("hfs_swap_HFSBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
1111 return fsBTInvalidNodeErr
;
1114 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
1115 srcRec
->valence
= SWAP_BE16 (srcRec
->valence
);
1117 srcRec
->folderID
= SWAP_BE32 (srcRec
->folderID
);
1118 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
1119 srcRec
->modifyDate
= SWAP_BE32 (srcRec
->modifyDate
);
1120 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
1122 /* Don't swap srcRec->userInfo */
1123 /* Don't swap srcRec->finderInfo */
1124 /* Don't swap resserved array */
1126 } else if (srcPtr
[0] == kHFSFileRecord
) {
1127 HFSCatalogFile
*srcRec
= (HFSCatalogFile
*)srcPtr
;
1128 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
1129 if (direction
== kSwapBTNodeHostToBig
) {
1130 panic("hfs_swap_HFSBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
1132 printf("hfs_swap_HFSBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
1134 return fsBTInvalidNodeErr
;
1137 srcRec
->flags
= srcRec
->flags
;
1138 srcRec
->fileType
= srcRec
->fileType
;
1140 /* Don't swap srcRec->userInfo */
1142 srcRec
->fileID
= SWAP_BE32 (srcRec
->fileID
);
1144 srcRec
->dataStartBlock
= SWAP_BE16 (srcRec
->dataStartBlock
);
1145 srcRec
->dataLogicalSize
= SWAP_BE32 (srcRec
->dataLogicalSize
);
1146 srcRec
->dataPhysicalSize
= SWAP_BE32 (srcRec
->dataPhysicalSize
);
1148 srcRec
->rsrcStartBlock
= SWAP_BE16 (srcRec
->rsrcStartBlock
);
1149 srcRec
->rsrcLogicalSize
= SWAP_BE32 (srcRec
->rsrcLogicalSize
);
1150 srcRec
->rsrcPhysicalSize
= SWAP_BE32 (srcRec
->rsrcPhysicalSize
);
1152 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
1153 srcRec
->modifyDate
= SWAP_BE32 (srcRec
->modifyDate
);
1154 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
1156 /* Don't swap srcRec->finderInfo */
1158 srcRec
->clumpSize
= SWAP_BE16 (srcRec
->clumpSize
);
1160 /* Swap the two sets of extents as an array of six (three each) u_int16_t */
1161 for (j
= 0; j
< kHFSExtentDensity
* 2; j
++) {
1162 srcRec
->dataExtents
[j
].startBlock
= SWAP_BE16 (srcRec
->dataExtents
[j
].startBlock
);
1163 srcRec
->dataExtents
[j
].blockCount
= SWAP_BE16 (srcRec
->dataExtents
[j
].blockCount
);
1166 /* Don't swap srcRec->reserved */
1168 } else if ((srcPtr
[0] == kHFSFolderThreadRecord
) ||
1169 (srcPtr
[0] == kHFSFileThreadRecord
)) {
1170 HFSCatalogThread
*srcRec
= (HFSCatalogThread
*)srcPtr
;
1172 /* Make sure there is room for parentID and name length */
1173 if ((char *) &srcRec
->nodeName
[1] > nextRecord
) {
1174 if (direction
== kSwapBTNodeHostToBig
) {
1175 panic("hfs_swap_HFSBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
1177 printf("hfs_swap_HFSBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
1179 return fsBTInvalidNodeErr
;
1182 /* Don't swap srcRec->reserved array */
1184 srcRec
->parentID
= SWAP_BE32 (srcRec
->parentID
);
1186 /* Don't swap srcRec->nodeName */
1188 /* Make sure there is room for the name in the buffer */
1189 if ((char *) &srcRec
->nodeName
[srcRec
->nodeName
[0]] > nextRecord
) {
1190 if (direction
== kSwapBTNodeHostToBig
) {
1191 panic("hfs_swap_HFSBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
1193 printf("hfs_swap_HFSBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
1195 return fsBTInvalidNodeErr
;
1198 if (direction
== kSwapBTNodeHostToBig
) {
1199 panic("hfs_swap_HFSBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
1201 printf("hfs_swap_HFSBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
1203 return fsBTInvalidNodeErr
;
1206 /* We can swap the record type now that we're done using it */
1207 if (direction
== kSwapBTNodeHostToBig
)
1208 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
1212 panic ("hfs_swap_HFSBTInternalNode: fileID %u is not a system B-tree\n", fileID
);