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.
121 if (btcb
->totalNodes
!= 0) {
122 if (srcDesc
->fLink
>= btcb
->totalNodes
) {
123 printf("hfs_swap_BTNode: invalid forward link (0x%08x >= 0x%08x)\n", srcDesc
->fLink
, btcb
->totalNodes
);
124 error
= fsBTInvalidHeaderErr
;
127 if (srcDesc
->bLink
>= btcb
->totalNodes
) {
128 printf("hfs_swap_BTNode: invalid backward link (0x%08x >= 0x%08x)\n", srcDesc
->bLink
, btcb
->totalNodes
);
129 error
= fsBTInvalidHeaderErr
;
135 * Check srcDesc->kind. Don't swap it because it's only one byte.
137 if (srcDesc
->kind
< kBTLeafNode
|| srcDesc
->kind
> kBTMapNode
) {
138 printf("hfs_swap_BTNode: invalid node kind (%d)\n", srcDesc
->kind
);
139 error
= fsBTInvalidHeaderErr
;
144 * Check srcDesc->height. Don't swap it because it's only one byte.
146 if (srcDesc
->height
> btcb
->treeDepth
) {
147 printf("hfs_swap_BTNode: invalid node height (%d)\n", srcDesc
->height
);
148 error
= fsBTInvalidHeaderErr
;
152 /* Don't swap srcDesc->reserved */
154 srcDesc
->numRecords
= SWAP_BE16 (srcDesc
->numRecords
);
157 * Swap the node offsets (including the free space one!).
159 srcOffs
= (u_int16_t
*)((char *)src
->buffer
+ (src
->blockSize
- ((srcDesc
->numRecords
+ 1) * sizeof (u_int16_t
))));
162 * Sanity check that the record offsets are within the node itself.
164 if ((char *)srcOffs
> ((char *)src
->buffer
+ src
->blockSize
) ||
165 (char *)srcOffs
< ((char *)src
->buffer
+ sizeof(BTNodeDescriptor
))) {
166 printf("hfs_swap_BTNode: invalid record count (0x%04X)\n", srcDesc
->numRecords
);
167 error
= fsBTInvalidHeaderErr
;
172 * Swap and sanity check each of the record offsets.
174 for (i
= 0; i
<= srcDesc
->numRecords
; i
++) {
175 srcOffs
[i
] = SWAP_BE16 (srcOffs
[i
]);
178 * Sanity check: must be even, and within the node itself.
180 * We may be called to swap an unused node, which contains all zeroes.
181 * Unused nodes are expected only when allow_empty_node is true.
182 * If it is false and record offset is zero, return error.
184 if ((srcOffs
[i
] & 1) || (
185 (allow_empty_node
== false) && (srcOffs
[i
] == 0)) ||
186 (srcOffs
[i
] < sizeof(BTNodeDescriptor
) && srcOffs
[i
] != 0) ||
187 (srcOffs
[i
] >= src
->blockSize
)) {
188 printf("hfs_swap_BTNode: record #%d invalid offset (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
189 error
= fsBTInvalidHeaderErr
;
194 * Make sure the offsets are strictly increasing. Note that we're looping over
195 * them backwards, hence the order in the comparison.
197 if ((i
!= 0) && (srcOffs
[i
] >= srcOffs
[i
-1])) {
198 printf("hfs_swap_BTNode: offsets %d and %d out of order (0x%04X, 0x%04X)\n",
199 srcDesc
->numRecords
-i
-1, srcDesc
->numRecords
-i
, srcOffs
[i
], srcOffs
[i
-1]);
200 error
= fsBTInvalidHeaderErr
;
207 * Swap the records (ordered by frequency of access)
209 if ((srcDesc
->kind
== kBTIndexNode
) ||
210 (srcDesc
-> kind
== kBTLeafNode
)) {
212 if (VTOVCB(vp
)->vcbSigWord
== kHFSPlusSigWord
) {
213 error
= hfs_swap_HFSPlusBTInternalNode (src
, VTOC(vp
)->c_fileid
, direction
);
215 error
= hfs_swap_HFSBTInternalNode (src
, VTOC(vp
)->c_fileid
, direction
);
218 if (error
) goto fail
;
220 } else if (srcDesc
-> kind
== kBTMapNode
) {
221 /* Don't swap the bitmaps, they'll be done in the bitmap routines */
223 } else if (srcDesc
-> kind
== kBTHeaderNode
) {
224 /* The header's offset is hard-wired because we cannot trust the offset pointers. */
225 BTHeaderRec
*srcHead
= (BTHeaderRec
*)((char *)src
->buffer
+ sizeof(BTNodeDescriptor
));
227 srcHead
->treeDepth
= SWAP_BE16 (srcHead
->treeDepth
);
229 srcHead
->rootNode
= SWAP_BE32 (srcHead
->rootNode
);
230 srcHead
->leafRecords
= SWAP_BE32 (srcHead
->leafRecords
);
231 srcHead
->firstLeafNode
= SWAP_BE32 (srcHead
->firstLeafNode
);
232 srcHead
->lastLeafNode
= SWAP_BE32 (srcHead
->lastLeafNode
);
234 srcHead
->nodeSize
= SWAP_BE16 (srcHead
->nodeSize
);
235 srcHead
->maxKeyLength
= SWAP_BE16 (srcHead
->maxKeyLength
);
237 srcHead
->totalNodes
= SWAP_BE32 (srcHead
->totalNodes
);
238 srcHead
->freeNodes
= SWAP_BE32 (srcHead
->freeNodes
);
240 srcHead
->clumpSize
= SWAP_BE32 (srcHead
->clumpSize
);
241 srcHead
->attributes
= SWAP_BE32 (srcHead
->attributes
);
243 /* Don't swap srcHead->reserved1 */
244 /* Don't swap srcHead->btreeType; it's only one byte */
245 /* Don't swap srcHead->reserved2 */
246 /* Don't swap srcHead->reserved3 */
247 /* Don't swap bitmap */
251 * If we are doing a swap from in-memory to on-disk, then swap the node
252 * descriptor and record offsets after we're done using them.
254 if (direction
== kSwapBTNodeHostToBig
) {
256 * Sanity check and swap the forward and backward links.
258 if (srcDesc
->fLink
>= btcb
->totalNodes
) {
259 panic("hfs_UNswap_BTNode: invalid forward link (0x%08X)\n", srcDesc
->fLink
);
260 error
= fsBTInvalidHeaderErr
;
263 if (srcDesc
->bLink
>= btcb
->totalNodes
) {
264 panic("hfs_UNswap_BTNode: invalid backward link (0x%08X)\n", srcDesc
->bLink
);
265 error
= fsBTInvalidHeaderErr
;
268 srcDesc
->fLink
= SWAP_BE32 (srcDesc
->fLink
);
269 srcDesc
->bLink
= SWAP_BE32 (srcDesc
->bLink
);
272 * Check srcDesc->kind. Don't swap it because it's only one byte.
274 if (srcDesc
->kind
< kBTLeafNode
|| srcDesc
->kind
> kBTMapNode
) {
275 panic("hfs_UNswap_BTNode: invalid node kind (%d)\n", srcDesc
->kind
);
276 error
= fsBTInvalidHeaderErr
;
281 * Check srcDesc->height. Don't swap it because it's only one byte.
283 if (srcDesc
->height
> btcb
->treeDepth
) {
284 panic("hfs_UNswap_BTNode: invalid node height (%d)\n", srcDesc
->height
);
285 error
= fsBTInvalidHeaderErr
;
289 /* Don't swap srcDesc->reserved */
292 * Swap the node offsets (including the free space one!).
294 srcOffs
= (u_int16_t
*)((char *)src
->buffer
+ (src
->blockSize
- ((srcDesc
->numRecords
+ 1) * sizeof (u_int16_t
))));
297 * Sanity check that the record offsets are within the node itself.
299 if ((char *)srcOffs
> ((char *)src
->buffer
+ src
->blockSize
) ||
300 (char *)srcOffs
< ((char *)src
->buffer
+ sizeof(BTNodeDescriptor
))) {
301 panic("hfs_UNswap_BTNode: invalid record count (0x%04X)\n", srcDesc
->numRecords
);
302 error
= fsBTInvalidHeaderErr
;
307 * Swap and sanity check each of the record offsets.
309 for (i
= 0; i
<= srcDesc
->numRecords
; i
++) {
311 * Sanity check: must be even, and within the node itself.
313 * We may be called to swap an unused node, which contains all zeroes.
314 * This can happen when the last record from a node gets deleted.
315 * This is why we allow the record offset to be zero.
316 * Unused nodes are expected only when allow_empty_node is true
317 * (the caller should set it to true for kSwapBTNodeBigToHost).
319 if ((srcOffs
[i
] & 1) ||
320 ((allow_empty_node
== false) && (srcOffs
[i
] == 0)) ||
321 (srcOffs
[i
] < sizeof(BTNodeDescriptor
) && srcOffs
[i
] != 0) ||
322 (srcOffs
[i
] >= src
->blockSize
)) {
323 panic("hfs_UNswap_BTNode: record #%d invalid offset (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
324 error
= fsBTInvalidHeaderErr
;
329 * Make sure the offsets are strictly increasing. Note that we're looping over
330 * them backwards, hence the order in the comparison.
332 if ((i
< srcDesc
->numRecords
) && (srcOffs
[i
+1] >= srcOffs
[i
])) {
333 panic("hfs_UNswap_BTNode: offsets %d and %d out of order (0x%04X, 0x%04X)\n",
334 srcDesc
->numRecords
-i
-2, srcDesc
->numRecords
-i
-1, srcOffs
[i
+1], srcOffs
[i
]);
335 error
= fsBTInvalidHeaderErr
;
339 srcOffs
[i
] = SWAP_BE16 (srcOffs
[i
]);
342 srcDesc
->numRecords
= SWAP_BE16 (srcDesc
->numRecords
);
348 * Log some useful information about where the corrupt node is.
350 printf("node=%lld fileID=%u volume=%s device=%s\n", src
->blockNum
, VTOC(vp
)->c_fileid
,
351 VTOVCB(vp
)->vcbVN
, vfs_statfs(vnode_mount(vp
))->f_mntfromname
);
352 hfs_mark_volume_inconsistent(VTOVCB(vp
));
359 hfs_swap_HFSPlusBTInternalNode (
360 BlockDescriptor
*src
,
361 HFSCatalogNodeID fileID
,
362 enum HFSBTSwapDirection direction
365 BTNodeDescriptor
*srcDesc
= src
->buffer
;
366 u_int16_t
*srcOffs
= (u_int16_t
*)((char *)src
->buffer
+ (src
->blockSize
- (srcDesc
->numRecords
* sizeof (u_int16_t
))));
367 char *nextRecord
; /* Points to start of record following current one */
370 * i is an int32 because it needs to be negative to index the offset to free space.
371 * srcDesc->numRecords is a u_int16_t and is unlikely to become 32-bit so this should be ok.
377 if (fileID
== kHFSExtentsFileID
) {
378 HFSPlusExtentKey
*srcKey
;
379 HFSPlusExtentDescriptor
*srcRec
;
380 size_t recordSize
; /* Size of the data part of the record, or node number for index nodes */
382 if (srcDesc
->kind
== kBTIndexNode
)
383 recordSize
= sizeof(u_int32_t
);
385 recordSize
= sizeof(HFSPlusExtentDescriptor
);
387 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
388 /* Point to the start of the record we're currently checking. */
389 srcKey
= (HFSPlusExtentKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
392 * Point to start of next (larger offset) record. We'll use this
393 * to be sure the current record doesn't overflow into the next
396 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
399 * Make sure the key and data are within the buffer. Since both key
400 * and data are fixed size, this is relatively easy. Note that this
401 * relies on the keyLength being a constant; we verify the keyLength
404 if ((char *)srcKey
+ sizeof(HFSPlusExtentKey
) + recordSize
> nextRecord
) {
405 if (direction
== kSwapBTNodeHostToBig
) {
406 panic("hfs_swap_HFSPlusBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
408 printf("hfs_swap_HFSPlusBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
410 return fsBTInvalidNodeErr
;
413 if (direction
== kSwapBTNodeBigToHost
)
414 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
415 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
416 if (direction
== kSwapBTNodeHostToBig
) {
417 panic("hfs_swap_HFSPlusBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
419 printf("hfs_swap_HFSPlusBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
421 return fsBTInvalidNodeErr
;
423 srcRec
= (HFSPlusExtentDescriptor
*)((char *)srcKey
+ srcKey
->keyLength
+ sizeof(srcKey
->keyLength
));
424 if (direction
== kSwapBTNodeHostToBig
)
425 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
427 /* Don't swap srcKey->forkType; it's only one byte */
428 /* Don't swap srcKey->pad */
430 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
431 srcKey
->startBlock
= SWAP_BE32 (srcKey
->startBlock
);
433 if (srcDesc
->kind
== kBTIndexNode
) {
434 /* For index nodes, the record data is just a child node number. */
435 *((u_int32_t
*)srcRec
) = SWAP_BE32 (*((u_int32_t
*)srcRec
));
437 /* Swap the extent data */
438 for (j
= 0; j
< kHFSPlusExtentDensity
; j
++) {
439 srcRec
[j
].startBlock
= SWAP_BE32 (srcRec
[j
].startBlock
);
440 srcRec
[j
].blockCount
= SWAP_BE32 (srcRec
[j
].blockCount
);
445 } else if (fileID
== kHFSCatalogFileID
) {
446 HFSPlusCatalogKey
*srcKey
;
450 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
451 /* Point to the start of the record we're currently checking. */
452 srcKey
= (HFSPlusCatalogKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
455 * Point to start of next (larger offset) record. We'll use this
456 * to be sure the current record doesn't overflow into the next
459 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
462 * Make sure we can safely dereference the keyLength and parentID fields.
464 if ((char *)srcKey
+ offsetof(HFSPlusCatalogKey
, nodeName
.unicode
[0]) > nextRecord
) {
465 if (direction
== kSwapBTNodeHostToBig
) {
466 panic("hfs_swap_HFSPlusBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
468 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
470 return fsBTInvalidNodeErr
;
474 * Swap and sanity check the key length
476 if (direction
== kSwapBTNodeBigToHost
)
477 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
478 keyLength
= srcKey
->keyLength
; /* Put it in a local (native order) because we use it several times */
479 if (direction
== kSwapBTNodeHostToBig
)
480 srcKey
->keyLength
= SWAP_BE16 (keyLength
);
482 /* Sanity check the key length */
483 if (keyLength
< kHFSPlusCatalogKeyMinimumLength
|| keyLength
> kHFSPlusCatalogKeyMaximumLength
) {
484 if (direction
== kSwapBTNodeHostToBig
) {
485 panic("hfs_swap_HFSPlusBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
487 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
489 return fsBTInvalidNodeErr
;
493 * Make sure that we can safely dereference the record's type field or
494 * an index node's child node number.
496 srcPtr
= (int16_t *)((char *)srcKey
+ keyLength
+ sizeof(srcKey
->keyLength
));
497 if ((char *)srcPtr
+ sizeof(u_int32_t
) > nextRecord
) {
498 if (direction
== kSwapBTNodeHostToBig
) {
499 panic("hfs_swap_HFSPlusBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
501 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
503 return fsBTInvalidNodeErr
;
506 srcKey
->parentID
= SWAP_BE32 (srcKey
->parentID
);
509 * Swap and sanity check the key's node name
511 if (direction
== kSwapBTNodeBigToHost
)
512 srcKey
->nodeName
.length
= SWAP_BE16 (srcKey
->nodeName
.length
);
513 /* Make sure name length is consistent with key length */
514 if (keyLength
< sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
515 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0])) {
516 if (direction
== kSwapBTNodeHostToBig
) {
517 panic("hfs_swap_HFSPlusBTInternalNode: catalog record #%d keyLength=%d expected=%lu\n",
518 srcDesc
->numRecords
-i
, keyLength
, sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
519 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0]));
521 printf("hfs_swap_HFSPlusBTInternalNode: catalog record #%d keyLength=%d expected=%lu\n",
522 srcDesc
->numRecords
-i
, keyLength
, sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
523 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0]));
525 return fsBTInvalidNodeErr
;
527 for (j
= 0; j
< srcKey
->nodeName
.length
; j
++) {
528 srcKey
->nodeName
.unicode
[j
] = SWAP_BE16 (srcKey
->nodeName
.unicode
[j
]);
530 if (direction
== kSwapBTNodeHostToBig
)
531 srcKey
->nodeName
.length
= SWAP_BE16 (srcKey
->nodeName
.length
);
534 * For index nodes, the record data is just the child's node number.
535 * Skip over swapping the various types of catalog record.
537 if (srcDesc
->kind
== kBTIndexNode
) {
538 *((u_int32_t
*)srcPtr
) = SWAP_BE32 (*((u_int32_t
*)srcPtr
));
542 /* Make sure the recordType is in native order before using it. */
543 if (direction
== kSwapBTNodeBigToHost
)
544 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
546 if (srcPtr
[0] == kHFSPlusFolderRecord
) {
547 HFSPlusCatalogFolder
*srcRec
= (HFSPlusCatalogFolder
*)srcPtr
;
548 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
549 if (direction
== kSwapBTNodeHostToBig
) {
550 panic("hfs_swap_HFSPlusBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
552 printf("hfs_swap_HFSPlusBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
554 return fsBTInvalidNodeErr
;
557 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
558 srcRec
->valence
= SWAP_BE32 (srcRec
->valence
);
559 srcRec
->folderID
= SWAP_BE32 (srcRec
->folderID
);
560 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
561 srcRec
->contentModDate
= SWAP_BE32 (srcRec
->contentModDate
);
562 srcRec
->attributeModDate
= SWAP_BE32 (srcRec
->attributeModDate
);
563 srcRec
->accessDate
= SWAP_BE32 (srcRec
->accessDate
);
564 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
566 srcRec
->bsdInfo
.ownerID
= SWAP_BE32 (srcRec
->bsdInfo
.ownerID
);
567 srcRec
->bsdInfo
.groupID
= SWAP_BE32 (srcRec
->bsdInfo
.groupID
);
569 /* Don't swap srcRec->bsdInfo.adminFlags; it's only one byte */
570 /* Don't swap srcRec->bsdInfo.ownerFlags; it's only one byte */
572 srcRec
->bsdInfo
.fileMode
= SWAP_BE16 (srcRec
->bsdInfo
.fileMode
);
573 srcRec
->bsdInfo
.special
.iNodeNum
= SWAP_BE32 (srcRec
->bsdInfo
.special
.iNodeNum
);
575 srcRec
->textEncoding
= SWAP_BE32 (srcRec
->textEncoding
);
577 /* Don't swap srcRec->userInfo */
578 /* Don't swap srcRec->finderInfo */
579 srcRec
->folderCount
= SWAP_BE32 (srcRec
->folderCount
);
581 } else if (srcPtr
[0] == kHFSPlusFileRecord
) {
582 HFSPlusCatalogFile
*srcRec
= (HFSPlusCatalogFile
*)srcPtr
;
583 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
584 if (direction
== kSwapBTNodeHostToBig
) {
585 panic("hfs_swap_HFSPlusBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
587 printf("hfs_swap_HFSPlusBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
589 return fsBTInvalidNodeErr
;
592 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
594 srcRec
->fileID
= SWAP_BE32 (srcRec
->fileID
);
596 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
597 srcRec
->contentModDate
= SWAP_BE32 (srcRec
->contentModDate
);
598 srcRec
->attributeModDate
= SWAP_BE32 (srcRec
->attributeModDate
);
599 srcRec
->accessDate
= SWAP_BE32 (srcRec
->accessDate
);
600 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
602 srcRec
->bsdInfo
.ownerID
= SWAP_BE32 (srcRec
->bsdInfo
.ownerID
);
603 srcRec
->bsdInfo
.groupID
= SWAP_BE32 (srcRec
->bsdInfo
.groupID
);
605 /* Don't swap srcRec->bsdInfo.adminFlags; it's only one byte */
606 /* Don't swap srcRec->bsdInfo.ownerFlags; it's only one byte */
608 srcRec
->bsdInfo
.fileMode
= SWAP_BE16 (srcRec
->bsdInfo
.fileMode
);
609 srcRec
->bsdInfo
.special
.iNodeNum
= SWAP_BE32 (srcRec
->bsdInfo
.special
.iNodeNum
);
611 srcRec
->textEncoding
= SWAP_BE32 (srcRec
->textEncoding
);
613 /* If kHFSHasLinkChainBit is set, reserved1 is hl_FirstLinkID.
614 * In all other context, it is expected to be zero.
616 srcRec
->reserved1
= SWAP_BE32 (srcRec
->reserved1
);
618 /* Don't swap srcRec->userInfo */
619 /* Don't swap srcRec->finderInfo */
620 /* Don't swap srcRec->reserved2 */
622 hfs_swap_HFSPlusForkData (&srcRec
->dataFork
);
623 hfs_swap_HFSPlusForkData (&srcRec
->resourceFork
);
625 } else if ((srcPtr
[0] == kHFSPlusFolderThreadRecord
) ||
626 (srcPtr
[0] == kHFSPlusFileThreadRecord
)) {
629 * Make sure there is room for parentID and name length.
631 HFSPlusCatalogThread
*srcRec
= (HFSPlusCatalogThread
*)srcPtr
;
632 if ((char *) &srcRec
->nodeName
.unicode
[0] > nextRecord
) {
633 if (direction
== kSwapBTNodeHostToBig
) {
634 panic("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
636 printf("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
638 return fsBTInvalidNodeErr
;
641 /* Don't swap srcRec->reserved */
643 srcRec
->parentID
= SWAP_BE32 (srcRec
->parentID
);
645 if (direction
== kSwapBTNodeBigToHost
)
646 srcRec
->nodeName
.length
= SWAP_BE16 (srcRec
->nodeName
.length
);
649 * Make sure there is room for the name in the buffer.
650 * Then swap the characters of the name itself.
652 if ((char *) &srcRec
->nodeName
.unicode
[srcRec
->nodeName
.length
] > nextRecord
) {
653 if (direction
== kSwapBTNodeHostToBig
) {
654 panic("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
656 printf("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
658 return fsBTInvalidNodeErr
;
660 for (j
= 0; j
< srcRec
->nodeName
.length
; j
++) {
661 srcRec
->nodeName
.unicode
[j
] = SWAP_BE16 (srcRec
->nodeName
.unicode
[j
]);
664 if (direction
== kSwapBTNodeHostToBig
)
665 srcRec
->nodeName
.length
= SWAP_BE16 (srcRec
->nodeName
.length
);
668 if (direction
== kSwapBTNodeHostToBig
) {
669 panic("hfs_swap_HFSPlusBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
671 printf("hfs_swap_HFSPlusBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
673 return fsBTInvalidNodeErr
;
676 /* We can swap the record type now that we're done using it. */
677 if (direction
== kSwapBTNodeHostToBig
)
678 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
681 } else if (fileID
== kHFSAttributesFileID
) {
682 HFSPlusAttrKey
*srcKey
;
683 HFSPlusAttrRecord
*srcRec
;
685 u_int32_t attrSize
= 0;
687 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
688 /* Point to the start of the record we're currently checking. */
689 srcKey
= (HFSPlusAttrKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
692 * Point to start of next (larger offset) record. We'll use this
693 * to be sure the current record doesn't overflow into the next
696 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
698 /* Make sure there is room in the buffer for a minimal key */
699 if ((char *) &srcKey
->attrName
[1] > nextRecord
) {
700 if (direction
== kSwapBTNodeHostToBig
) {
701 panic("hfs_swap_HFSPlusBTInternalNode: attr key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
703 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
705 return fsBTInvalidNodeErr
;
708 /* Swap the key length field */
709 if (direction
== kSwapBTNodeBigToHost
)
710 srcKey
->keyLength
= SWAP_BE16(srcKey
->keyLength
);
711 keyLength
= srcKey
->keyLength
; /* Keep a copy in native order */
712 if (direction
== kSwapBTNodeHostToBig
)
713 srcKey
->keyLength
= SWAP_BE16(srcKey
->keyLength
);
716 * Make sure that we can safely dereference the record's type field or
717 * an index node's child node number.
719 srcRec
= (HFSPlusAttrRecord
*)((char *)srcKey
+ keyLength
+ sizeof(srcKey
->keyLength
));
720 if ((char *)srcRec
+ sizeof(u_int32_t
) > nextRecord
) {
721 if (direction
== kSwapBTNodeHostToBig
) {
722 panic("hfs_swap_HFSPlusBTInternalNode: attr key #%d too big (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
724 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d too big (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
726 return fsBTInvalidNodeErr
;
729 srcKey
->fileID
= SWAP_BE32(srcKey
->fileID
);
730 srcKey
->startBlock
= SWAP_BE32(srcKey
->startBlock
);
733 * Swap and check the attribute name
735 if (direction
== kSwapBTNodeBigToHost
)
736 srcKey
->attrNameLen
= SWAP_BE16(srcKey
->attrNameLen
);
737 /* Sanity check the attribute name length */
738 if (srcKey
->attrNameLen
> kHFSMaxAttrNameLen
|| keyLength
< (kHFSPlusAttrKeyMinimumLength
+ sizeof(u_int16_t
)*srcKey
->attrNameLen
)) {
739 if (direction
== kSwapBTNodeHostToBig
) {
740 panic("hfs_swap_HFSPlusBTInternalNode: attr key #%d keyLength=%d attrNameLen=%d\n", srcDesc
->numRecords
-i
-1, keyLength
, srcKey
->attrNameLen
);
742 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d keyLength=%d attrNameLen=%d\n", srcDesc
->numRecords
-i
-1, keyLength
, srcKey
->attrNameLen
);
744 return fsBTInvalidNodeErr
;
746 for (j
= 0; j
< srcKey
->attrNameLen
; j
++)
747 srcKey
->attrName
[j
] = SWAP_BE16(srcKey
->attrName
[j
]);
748 if (direction
== kSwapBTNodeHostToBig
)
749 srcKey
->attrNameLen
= SWAP_BE16(srcKey
->attrNameLen
);
752 * For index nodes, the record data is just the child's node number.
753 * Skip over swapping the various types of attribute record.
755 if (srcDesc
->kind
== kBTIndexNode
) {
756 *((u_int32_t
*)srcRec
) = SWAP_BE32 (*((u_int32_t
*)srcRec
));
760 /* Swap the record data */
761 if (direction
== kSwapBTNodeBigToHost
)
762 srcRec
->recordType
= SWAP_BE32(srcRec
->recordType
);
763 switch (srcRec
->recordType
) {
764 case kHFSPlusAttrInlineData
:
765 /* Is there room for the inline data header? */
766 if ((char *) &srcRec
->attrData
.attrData
[0] > nextRecord
) {
767 if (direction
== kSwapBTNodeHostToBig
) {
768 panic("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big\n", srcDesc
->numRecords
-i
-1);
770 printf("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big\n", srcDesc
->numRecords
-i
-1);
772 return fsBTInvalidNodeErr
;
775 /* We're not swapping the reserved fields */
777 /* Swap the attribute size */
778 if (direction
== kSwapBTNodeHostToBig
)
779 attrSize
= srcRec
->attrData
.attrSize
;
780 srcRec
->attrData
.attrSize
= SWAP_BE32(srcRec
->attrData
.attrSize
);
781 if (direction
== kSwapBTNodeBigToHost
)
782 attrSize
= srcRec
->attrData
.attrSize
;
784 /* Is there room for the inline attribute data? */
785 if ((char *) &srcRec
->attrData
.attrData
[attrSize
] > nextRecord
) {
786 if (direction
== kSwapBTNodeHostToBig
) {
787 panic("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big (attrSize=%u)\n", srcDesc
->numRecords
-i
-1, attrSize
);
789 printf("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big (attrSize=%u)\n", srcDesc
->numRecords
-i
-1, attrSize
);
791 return fsBTInvalidNodeErr
;
794 /* Not swapping the attribute data itself */
797 case kHFSPlusAttrForkData
:
798 /* Is there room for the fork data record? */
799 if ((char *)srcRec
+ sizeof(HFSPlusAttrForkData
) > nextRecord
) {
800 if (direction
== kSwapBTNodeHostToBig
) {
801 panic("hfs_swap_HFSPlusBTInternalNode: attr fork data #%d too big\n", srcDesc
->numRecords
-i
-1);
803 printf("hfs_swap_HFSPlusBTInternalNode: attr fork data #%d too big\n", srcDesc
->numRecords
-i
-1);
805 return fsBTInvalidNodeErr
;
808 /* We're not swapping the reserved field */
810 hfs_swap_HFSPlusForkData(&srcRec
->forkData
.theFork
);
813 case kHFSPlusAttrExtents
:
814 /* Is there room for an extent record? */
815 if ((char *)srcRec
+ sizeof(HFSPlusAttrExtents
) > nextRecord
) {
816 if (direction
== kSwapBTNodeHostToBig
) {
817 panic("hfs_swap_HFSPlusBTInternalNode: attr extents #%d too big\n", srcDesc
->numRecords
-i
-1);
819 printf("hfs_swap_HFSPlusBTInternalNode: attr extents #%d too big\n", srcDesc
->numRecords
-i
-1);
821 return fsBTInvalidNodeErr
;
824 /* We're not swapping the reserved field */
826 for (j
= 0; j
< kHFSPlusExtentDensity
; j
++) {
827 srcRec
->overflowExtents
.extents
[j
].startBlock
=
828 SWAP_BE32(srcRec
->overflowExtents
.extents
[j
].startBlock
);
829 srcRec
->overflowExtents
.extents
[j
].blockCount
=
830 SWAP_BE32(srcRec
->overflowExtents
.extents
[j
].blockCount
);
834 if (direction
== kSwapBTNodeHostToBig
)
835 srcRec
->recordType
= SWAP_BE32(srcRec
->recordType
);
837 } else if (fileID
> kHFSFirstUserCatalogNodeID
) {
838 /* The only B-tree with a non-system CNID that we use is the hotfile B-tree */
842 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
843 /* Point to the start of the record we're currently checking. */
844 srcKey
= (HotFileKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
847 * Point to start of next (larger offset) record. We'll use this
848 * to be sure the current record doesn't overflow into the next
851 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
853 /* Make sure there is room for the key (HotFileKey) and data (u_int32_t) */
854 if ((char *)srcKey
+ sizeof(HotFileKey
) + sizeof(u_int32_t
) > nextRecord
) {
855 if (direction
== kSwapBTNodeHostToBig
) {
856 panic("hfs_swap_HFSPlusBTInternalNode: hotfile #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
858 printf("hfs_swap_HFSPlusBTInternalNode: hotfile #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
860 return fsBTInvalidNodeErr
;
863 /* Swap and sanity check the key length field */
864 if (direction
== kSwapBTNodeBigToHost
)
865 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
866 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
867 if (direction
== kSwapBTNodeHostToBig
) {
868 panic("hfs_swap_HFSPlusBTInternalNode: hotfile #%d incorrect keyLength %d\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
870 printf("hfs_swap_HFSPlusBTInternalNode: hotfile #%d incorrect keyLength %d\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
872 return fsBTInvalidNodeErr
;
874 srcRec
= (u_int32_t
*)((char *)srcKey
+ srcKey
->keyLength
+ sizeof(srcKey
->keyLength
));
875 if (direction
== kSwapBTNodeHostToBig
)
876 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
878 /* Don't swap srcKey->forkType */
879 /* Don't swap srcKey->pad */
881 srcKey
->temperature
= SWAP_BE32 (srcKey
->temperature
);
882 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
884 *((u_int32_t
*)srcRec
) = SWAP_BE32 (*((u_int32_t
*)srcRec
));
887 panic ("hfs_swap_HFSPlusBTInternalNode: fileID %u is not a system B-tree\n", fileID
);
895 hfs_swap_HFSBTInternalNode (
896 BlockDescriptor
*src
,
897 HFSCatalogNodeID fileID
,
898 enum HFSBTSwapDirection direction
901 BTNodeDescriptor
*srcDesc
= src
->buffer
;
902 u_int16_t
*srcOffs
= (u_int16_t
*)((char *)src
->buffer
+ (src
->blockSize
- (srcDesc
->numRecords
* sizeof (u_int16_t
))));
903 char *nextRecord
; /* Points to start of record following current one */
906 * i is an int32 because it needs to be negative to index the offset to free space.
907 * srcDesc->numRecords is a u_int16_t and is unlikely to become 32-bit so this should be ok.
912 if (fileID
== kHFSExtentsFileID
) {
913 HFSExtentKey
*srcKey
;
914 HFSExtentDescriptor
*srcRec
;
915 size_t recordSize
; /* Size of the data part of the record, or node number for index nodes */
917 if (srcDesc
->kind
== kBTIndexNode
)
918 recordSize
= sizeof(u_int32_t
);
920 recordSize
= sizeof(HFSExtentDescriptor
);
922 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
923 /* Point to the start of the record we're currently checking. */
924 srcKey
= (HFSExtentKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
927 * Point to start of next (larger offset) record. We'll use this
928 * to be sure the current record doesn't overflow into the next
931 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
934 * Make sure the key and data are within the buffer. Since both key
935 * and data are fixed size, this is relatively easy. Note that this
936 * relies on the keyLength being a constant; we verify the keyLength
939 if ((char *)srcKey
+ sizeof(HFSExtentKey
) + recordSize
> nextRecord
) {
940 if (direction
== kSwapBTNodeHostToBig
) {
941 panic("hfs_swap_HFSBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
943 printf("hfs_swap_HFSBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
945 return fsBTInvalidNodeErr
;
948 /* Don't swap srcKey->keyLength (it's only one byte), but do sanity check it */
949 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
950 if (direction
== kSwapBTNodeHostToBig
) {
951 panic("hfs_swap_HFSBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
953 printf("hfs_swap_HFSBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
955 return fsBTInvalidNodeErr
;
958 /* Don't swap srcKey->forkType; it's only one byte */
960 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
961 srcKey
->startBlock
= SWAP_BE16 (srcKey
->startBlock
);
963 /* Point to record data (round up to even byte boundary) */
964 srcRec
= (HFSExtentDescriptor
*)((char *)srcKey
+ ((srcKey
->keyLength
+ 2) & ~1));
966 if (srcDesc
->kind
== kBTIndexNode
) {
967 /* For index nodes, the record data is just a child node number. */
968 *((u_int32_t
*)srcRec
) = SWAP_BE32 (*((u_int32_t
*)srcRec
));
970 /* Swap the extent data */
971 for (j
= 0; j
< kHFSExtentDensity
; j
++) {
972 srcRec
[j
].startBlock
= SWAP_BE16 (srcRec
[j
].startBlock
);
973 srcRec
[j
].blockCount
= SWAP_BE16 (srcRec
[j
].blockCount
);
978 } else if (fileID
== kHFSCatalogFileID
) {
979 HFSCatalogKey
*srcKey
;
981 unsigned expectedKeyLength
;
983 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
984 /* Point to the start of the record we're currently checking. */
985 srcKey
= (HFSCatalogKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
988 * Point to start of next (larger offset) record. We'll use this
989 * to be sure the current record doesn't overflow into the next
992 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
995 * Make sure we can safely dereference the keyLength and parentID fields.
996 * The value 8 below is 1 bytes for keyLength + 1 byte reserved + 4 bytes
997 * for parentID + 1 byte for nodeName's length + 1 byte to round up the
998 * record start to an even offset, which forms a minimal key.
1000 if ((char *)srcKey
+ 8 > nextRecord
) {
1001 if (direction
== kSwapBTNodeHostToBig
) {
1002 panic("hfs_swap_HFSBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
1004 printf("hfs_swap_HFSBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
1006 return fsBTInvalidNodeErr
;
1009 /* Don't swap srcKey->keyLength (it's only one byte), but do sanity check it */
1010 if (srcKey
->keyLength
< kHFSCatalogKeyMinimumLength
|| srcKey
->keyLength
> kHFSCatalogKeyMaximumLength
) {
1011 if (direction
== kSwapBTNodeHostToBig
) {
1012 panic("hfs_swap_HFSBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
1014 printf("hfs_swap_HFSBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
1016 return fsBTInvalidNodeErr
;
1019 /* Don't swap srcKey->reserved */
1021 srcKey
->parentID
= SWAP_BE32 (srcKey
->parentID
);
1023 /* Don't swap srcKey->nodeName */
1025 /* Make sure the keyLength is big enough for the key's content */
1026 if (srcDesc
->kind
== kBTIndexNode
)
1027 expectedKeyLength
= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
);
1029 expectedKeyLength
= srcKey
->nodeName
[0] + kHFSCatalogKeyMinimumLength
;
1030 if (srcKey
->keyLength
< expectedKeyLength
) {
1031 if (direction
== kSwapBTNodeHostToBig
) {
1032 panic("hfs_swap_HFSBTInternalNode: catalog record #%d keyLength=%u expected=%u\n",
1033 srcDesc
->numRecords
-i
, srcKey
->keyLength
, expectedKeyLength
);
1035 printf("hfs_swap_HFSBTInternalNode: catalog record #%d keyLength=%u expected=%u\n",
1036 srcDesc
->numRecords
-i
, srcKey
->keyLength
, expectedKeyLength
);
1038 return fsBTInvalidNodeErr
;
1041 /* Point to record data (round up to even byte boundary) */
1042 srcPtr
= (int16_t *)((char *)srcKey
+ ((srcKey
->keyLength
+ 2) & ~1));
1045 * Make sure that we can safely dereference the record's type field or
1046 * and index node's child node number.
1048 if ((char *)srcPtr
+ sizeof(u_int32_t
) > nextRecord
) {
1049 if (direction
== kSwapBTNodeHostToBig
) {
1050 panic("hfs_swap_HFSBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
1052 printf("hfs_swap_HFSBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
1054 return fsBTInvalidNodeErr
;
1058 * For index nodes, the record data is just the child's node number.
1059 * Skip over swapping the various types of catalog record.
1061 if (srcDesc
->kind
== kBTIndexNode
) {
1062 *((u_int32_t
*)srcPtr
) = SWAP_BE32 (*((u_int32_t
*)srcPtr
));
1066 /* Make sure the recordType is in native order before using it. */
1067 if (direction
== kSwapBTNodeBigToHost
)
1068 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
1070 if (srcPtr
[0] == kHFSFolderRecord
) {
1071 HFSCatalogFolder
*srcRec
= (HFSCatalogFolder
*)srcPtr
;
1072 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
1073 if (direction
== kSwapBTNodeHostToBig
) {
1074 panic("hfs_swap_HFSBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
1076 printf("hfs_swap_HFSBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
1078 return fsBTInvalidNodeErr
;
1081 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
1082 srcRec
->valence
= SWAP_BE16 (srcRec
->valence
);
1084 srcRec
->folderID
= SWAP_BE32 (srcRec
->folderID
);
1085 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
1086 srcRec
->modifyDate
= SWAP_BE32 (srcRec
->modifyDate
);
1087 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
1089 /* Don't swap srcRec->userInfo */
1090 /* Don't swap srcRec->finderInfo */
1091 /* Don't swap resserved array */
1093 } else if (srcPtr
[0] == kHFSFileRecord
) {
1094 HFSCatalogFile
*srcRec
= (HFSCatalogFile
*)srcPtr
;
1095 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
1096 if (direction
== kSwapBTNodeHostToBig
) {
1097 panic("hfs_swap_HFSBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
1099 printf("hfs_swap_HFSBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
1101 return fsBTInvalidNodeErr
;
1104 srcRec
->flags
= srcRec
->flags
;
1105 srcRec
->fileType
= srcRec
->fileType
;
1107 /* Don't swap srcRec->userInfo */
1109 srcRec
->fileID
= SWAP_BE32 (srcRec
->fileID
);
1111 srcRec
->dataStartBlock
= SWAP_BE16 (srcRec
->dataStartBlock
);
1112 srcRec
->dataLogicalSize
= SWAP_BE32 (srcRec
->dataLogicalSize
);
1113 srcRec
->dataPhysicalSize
= SWAP_BE32 (srcRec
->dataPhysicalSize
);
1115 srcRec
->rsrcStartBlock
= SWAP_BE16 (srcRec
->rsrcStartBlock
);
1116 srcRec
->rsrcLogicalSize
= SWAP_BE32 (srcRec
->rsrcLogicalSize
);
1117 srcRec
->rsrcPhysicalSize
= SWAP_BE32 (srcRec
->rsrcPhysicalSize
);
1119 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
1120 srcRec
->modifyDate
= SWAP_BE32 (srcRec
->modifyDate
);
1121 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
1123 /* Don't swap srcRec->finderInfo */
1125 srcRec
->clumpSize
= SWAP_BE16 (srcRec
->clumpSize
);
1127 /* Swap the two sets of extents as an array of six (three each) u_int16_t */
1128 for (j
= 0; j
< kHFSExtentDensity
* 2; j
++) {
1129 srcRec
->dataExtents
[j
].startBlock
= SWAP_BE16 (srcRec
->dataExtents
[j
].startBlock
);
1130 srcRec
->dataExtents
[j
].blockCount
= SWAP_BE16 (srcRec
->dataExtents
[j
].blockCount
);
1133 /* Don't swap srcRec->reserved */
1135 } else if ((srcPtr
[0] == kHFSFolderThreadRecord
) ||
1136 (srcPtr
[0] == kHFSFileThreadRecord
)) {
1137 HFSCatalogThread
*srcRec
= (HFSCatalogThread
*)srcPtr
;
1139 /* Make sure there is room for parentID and name length */
1140 if ((char *) &srcRec
->nodeName
[1] > nextRecord
) {
1141 if (direction
== kSwapBTNodeHostToBig
) {
1142 panic("hfs_swap_HFSBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
1144 printf("hfs_swap_HFSBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
1146 return fsBTInvalidNodeErr
;
1149 /* Don't swap srcRec->reserved array */
1151 srcRec
->parentID
= SWAP_BE32 (srcRec
->parentID
);
1153 /* Don't swap srcRec->nodeName */
1155 /* Make sure there is room for the name in the buffer */
1156 if ((char *) &srcRec
->nodeName
[srcRec
->nodeName
[0]] > nextRecord
) {
1157 if (direction
== kSwapBTNodeHostToBig
) {
1158 panic("hfs_swap_HFSBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
1160 printf("hfs_swap_HFSBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
1162 return fsBTInvalidNodeErr
;
1165 if (direction
== kSwapBTNodeHostToBig
) {
1166 panic("hfs_swap_HFSBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
1168 printf("hfs_swap_HFSBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
1170 return fsBTInvalidNodeErr
;
1173 /* We can swap the record type now that we're done using it */
1174 if (direction
== kSwapBTNodeHostToBig
)
1175 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
1179 panic ("hfs_swap_HFSBTInternalNode: fileID %u is not a system B-tree\n", fileID
);