2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 * This file implements endian swapping routines for the HFS/HFS Plus
31 #include <architecture/byte_order.h>
33 #include "hfs_endian.h"
35 #include "hfscommon/headers/BTreesPrivate.h"
40 * Internal swapping routines
42 * These routines handle swapping the records of leaf and index nodes. The
43 * layout of the keys and records varies depending on the kind of B-tree
44 * (determined by fileID).
46 * The direction parameter must be kSwapBTNodeBigToHost or kSwapBTNodeHostToBig.
47 * The kSwapBTNodeHeaderRecordOnly "direction" is not valid for these routines.
49 static int hfs_swap_HFSPlusBTInternalNode (BlockDescriptor
*src
, HFSCatalogNodeID fileID
, enum HFSBTSwapDirection direction
);
50 static int hfs_swap_HFSBTInternalNode (BlockDescriptor
*src
, HFSCatalogNodeID fileID
, enum HFSBTSwapDirection direction
);
53 * hfs_swap_HFSPlusForkData
56 hfs_swap_HFSPlusForkData (
62 src
->logicalSize
= SWAP_BE64 (src
->logicalSize
);
64 src
->clumpSize
= SWAP_BE32 (src
->clumpSize
);
65 src
->totalBlocks
= SWAP_BE32 (src
->totalBlocks
);
67 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
68 src
->extents
[i
].startBlock
= SWAP_BE32 (src
->extents
[i
].startBlock
);
69 src
->extents
[i
].blockCount
= SWAP_BE32 (src
->extents
[i
].blockCount
);
76 * NOTE: This operation is not naturally symmetric.
77 * We have to determine which way we're swapping things.
83 enum HFSBTSwapDirection direction
86 BTNodeDescriptor
*srcDesc
= src
->buffer
;
87 UInt16
*srcOffs
= NULL
;
88 BTreeControlBlockPtr btcb
= (BTreeControlBlockPtr
)VTOF(vp
)->fcbBTCBPtr
;
93 if (direction
== kSwapBTNodeBigToHost
) {
94 printf ("BE -> Native Swap\n");
95 } else if (direction
== kSwapBTNodeHostToBig
) {
96 printf ("Native -> BE Swap\n");
97 } else if (direction
== kSwapBTNodeHeaderRecordOnly
) {
98 printf ("Not swapping descriptors\n");
100 panic ("hfs_swap_BTNode: This is impossible");
105 * If we are doing a swap from on-disk to in-memory, then swap the node
106 * descriptor and record offsets before we need to use them.
108 if (direction
== kSwapBTNodeBigToHost
) {
109 srcDesc
->fLink
= SWAP_BE32 (srcDesc
->fLink
);
110 srcDesc
->bLink
= SWAP_BE32 (srcDesc
->bLink
);
113 * When first opening a BTree, we have to read the header node before the
114 * control block is initialized. In this case, totalNodes will be zero,
115 * so skip the bounds checking.
117 if (btcb
->totalNodes
!= 0) {
118 if (srcDesc
->fLink
>= btcb
->totalNodes
) {
119 printf("hfs_swap_BTNode: invalid forward link (0x%08X)\n", srcDesc
->fLink
);
120 error
= fsBTInvalidHeaderErr
;
123 if (srcDesc
->bLink
>= btcb
->totalNodes
) {
124 printf("hfs_swap_BTNode: invalid backward link (0x%08X)\n", srcDesc
->bLink
);
125 error
= fsBTInvalidHeaderErr
;
131 * Check srcDesc->kind. Don't swap it because it's only one byte.
133 if (srcDesc
->kind
< kBTLeafNode
|| srcDesc
->kind
> kBTMapNode
) {
134 printf("hfs_swap_BTNode: invalid node kind (%d)\n", srcDesc
->kind
);
135 error
= fsBTInvalidHeaderErr
;
140 * Check srcDesc->height. Don't swap it because it's only one byte.
142 if (srcDesc
->height
> btcb
->treeDepth
) {
143 printf("hfs_swap_BTNode: invalid node height (%d)\n", srcDesc
->height
);
144 error
= fsBTInvalidHeaderErr
;
148 /* Don't swap srcDesc->reserved */
150 srcDesc
->numRecords
= SWAP_BE16 (srcDesc
->numRecords
);
153 * Swap the node offsets (including the free space one!).
155 srcOffs
= (UInt16
*)((char *)src
->buffer
+ (src
->blockSize
- ((srcDesc
->numRecords
+ 1) * sizeof (UInt16
))));
158 * Sanity check that the record offsets are within the node itself.
160 if ((char *)srcOffs
> ((char *)src
->buffer
+ src
->blockSize
) ||
161 (char *)srcOffs
< ((char *)src
->buffer
+ sizeof(BTNodeDescriptor
))) {
162 printf("hfs_swap_BTNode: invalid record count (0x%04X)\n", srcDesc
->numRecords
);
163 error
= fsBTInvalidHeaderErr
;
168 * Swap and sanity check each of the record offsets.
170 for (i
= 0; i
<= srcDesc
->numRecords
; i
++) {
171 srcOffs
[i
] = SWAP_BE16 (srcOffs
[i
]);
174 * Sanity check: must be even, and within the node itself.
176 * We may be called to swap an unused node, which contains all zeroes.
177 * This is why we allow the record offset to be zero.
179 if ((srcOffs
[i
] & 1) || (srcOffs
[i
] < sizeof(BTNodeDescriptor
) && srcOffs
[i
] != 0) || (srcOffs
[i
] >= src
->blockSize
)) {
180 printf("hfs_swap_BTNode: record #%d invalid offset (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
181 error
= fsBTInvalidHeaderErr
;
186 * Make sure the offsets are strictly increasing. Note that we're looping over
187 * them backwards, hence the order in the comparison.
189 if ((i
!= 0) && (srcOffs
[i
] >= srcOffs
[i
-1])) {
190 printf("hfs_swap_BTNode: offsets %d and %d out of order (0x%04X, 0x%04X)\n",
191 srcDesc
->numRecords
-i
-1, srcDesc
->numRecords
-i
, srcOffs
[i
], srcOffs
[i
-1]);
192 error
= fsBTInvalidHeaderErr
;
199 * Swap the records (ordered by frequency of access)
201 if ((srcDesc
->kind
== kBTIndexNode
) ||
202 (srcDesc
-> kind
== kBTLeafNode
)) {
204 if (VTOVCB(vp
)->vcbSigWord
== kHFSPlusSigWord
) {
205 error
= hfs_swap_HFSPlusBTInternalNode (src
, VTOC(vp
)->c_fileid
, direction
);
207 error
= hfs_swap_HFSBTInternalNode (src
, VTOC(vp
)->c_fileid
, direction
);
210 if (error
) goto fail
;
212 } else if (srcDesc
-> kind
== kBTMapNode
) {
213 /* Don't swap the bitmaps, they'll be done in the bitmap routines */
215 } else if (srcDesc
-> kind
== kBTHeaderNode
) {
216 /* The header's offset is hard-wired because we cannot trust the offset pointers. */
217 BTHeaderRec
*srcHead
= (BTHeaderRec
*)((char *)src
->buffer
+ sizeof(BTNodeDescriptor
));
219 srcHead
->treeDepth
= SWAP_BE16 (srcHead
->treeDepth
);
221 srcHead
->rootNode
= SWAP_BE32 (srcHead
->rootNode
);
222 srcHead
->leafRecords
= SWAP_BE32 (srcHead
->leafRecords
);
223 srcHead
->firstLeafNode
= SWAP_BE32 (srcHead
->firstLeafNode
);
224 srcHead
->lastLeafNode
= SWAP_BE32 (srcHead
->lastLeafNode
);
226 srcHead
->nodeSize
= SWAP_BE16 (srcHead
->nodeSize
);
227 srcHead
->maxKeyLength
= SWAP_BE16 (srcHead
->maxKeyLength
);
229 srcHead
->totalNodes
= SWAP_BE32 (srcHead
->totalNodes
);
230 srcHead
->freeNodes
= SWAP_BE32 (srcHead
->freeNodes
);
232 srcHead
->clumpSize
= SWAP_BE32 (srcHead
->clumpSize
);
233 srcHead
->attributes
= SWAP_BE32 (srcHead
->attributes
);
235 /* Don't swap srcHead->reserved1 */
236 /* Don't swap srcHead->btreeType; it's only one byte */
237 /* Don't swap srcHead->reserved2 */
238 /* Don't swap srcHead->reserved3 */
239 /* Don't swap bitmap */
243 * If we are doing a swap from in-memory to on-disk, then swap the node
244 * descriptor and record offsets after we're done using them.
246 if (direction
== kSwapBTNodeHostToBig
) {
248 * Sanity check and swap the forkward and backward links.
250 if (srcDesc
->fLink
>= btcb
->totalNodes
) {
251 printf("hfs_UNswap_BTNode: invalid forward link (0x%08X)\n", srcDesc
->fLink
);
252 error
= fsBTInvalidHeaderErr
;
255 if (srcDesc
->bLink
>= btcb
->totalNodes
) {
256 printf("hfs_UNswap_BTNode: invalid backward link (0x%08X)\n", srcDesc
->bLink
);
257 error
= fsBTInvalidHeaderErr
;
260 srcDesc
->fLink
= SWAP_BE32 (srcDesc
->fLink
);
261 srcDesc
->bLink
= SWAP_BE32 (srcDesc
->bLink
);
264 * Check srcDesc->kind. Don't swap it because it's only one byte.
266 if (srcDesc
->kind
< kBTLeafNode
|| srcDesc
->kind
> kBTMapNode
) {
267 printf("hfs_UNswap_BTNode: invalid node kind (%d)\n", srcDesc
->kind
);
268 error
= fsBTInvalidHeaderErr
;
273 * Check srcDesc->height. Don't swap it because it's only one byte.
275 if (srcDesc
->height
> btcb
->treeDepth
) {
276 printf("hfs_UNswap_BTNode: invalid node height (%d)\n", srcDesc
->height
);
277 error
= fsBTInvalidHeaderErr
;
281 /* Don't swap srcDesc->reserved */
284 * Swap the node offsets (including the free space one!).
286 srcOffs
= (UInt16
*)((char *)src
->buffer
+ (src
->blockSize
- ((srcDesc
->numRecords
+ 1) * sizeof (UInt16
))));
289 * Sanity check that the record offsets are within the node itself.
291 if ((char *)srcOffs
> ((char *)src
->buffer
+ src
->blockSize
) ||
292 (char *)srcOffs
< ((char *)src
->buffer
+ sizeof(BTNodeDescriptor
))) {
293 printf("hfs_UNswap_BTNode: invalid record count (0x%04X)\n", srcDesc
->numRecords
);
294 error
= fsBTInvalidHeaderErr
;
299 * Swap and sanity check each of the record offsets.
301 for (i
= 0; i
<= srcDesc
->numRecords
; i
++) {
303 * Sanity check: must be even, and within the node itself.
305 * We may be called to swap an unused node, which contains all zeroes.
306 * This is why we allow the record offset to be zero.
308 if ((srcOffs
[i
] & 1) || (srcOffs
[i
] < sizeof(BTNodeDescriptor
) && srcOffs
[i
] != 0) || (srcOffs
[i
] >= src
->blockSize
)) {
309 printf("hfs_UNswap_BTNode: record #%d invalid offset (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
310 error
= fsBTInvalidHeaderErr
;
315 * Make sure the offsets are strictly increasing. Note that we're looping over
316 * them backwards, hence the order in the comparison.
318 if ((i
< srcDesc
->numRecords
) && (srcOffs
[i
+1] >= srcOffs
[i
])) {
319 printf("hfs_UNswap_BTNode: offsets %d and %d out of order (0x%04X, 0x%04X)\n",
320 srcDesc
->numRecords
-i
-2, srcDesc
->numRecords
-i
-1, srcOffs
[i
+1], srcOffs
[i
]);
321 error
= fsBTInvalidHeaderErr
;
325 srcOffs
[i
] = SWAP_BE16 (srcOffs
[i
]);
328 srcDesc
->numRecords
= SWAP_BE16 (srcDesc
->numRecords
);
334 * Log some useful information about where the corrupt node is.
336 printf("node=%lld fileID=%u volume=%s device=%s\n", src
->blockNum
, VTOC(vp
)->c_fileid
,
337 VTOVCB(vp
)->vcbVN
, vfs_statfs(vnode_mount(vp
))->f_mntfromname
);
338 VTOVCB(vp
)->vcbFlags
|= kHFS_DamagedVolume
;
345 hfs_swap_HFSPlusBTInternalNode (
346 BlockDescriptor
*src
,
347 HFSCatalogNodeID fileID
,
348 enum HFSBTSwapDirection direction
351 BTNodeDescriptor
*srcDesc
= src
->buffer
;
352 UInt16
*srcOffs
= (UInt16
*)((char *)src
->buffer
+ (src
->blockSize
- (srcDesc
->numRecords
* sizeof (UInt16
))));
353 char *nextRecord
; /* Points to start of record following current one */
357 if (fileID
== kHFSExtentsFileID
) {
358 HFSPlusExtentKey
*srcKey
;
359 HFSPlusExtentDescriptor
*srcRec
;
360 size_t recordSize
; /* Size of the data part of the record, or node number for index nodes */
362 if (srcDesc
->kind
== kBTIndexNode
)
363 recordSize
= sizeof(UInt32
);
365 recordSize
= sizeof(HFSPlusExtentDescriptor
);
367 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
368 /* Point to the start of the record we're currently checking. */
369 srcKey
= (HFSPlusExtentKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
372 * Point to start of next (larger offset) record. We'll use this
373 * to be sure the current record doesn't overflow into the next
376 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
379 * Make sure the key and data are within the buffer. Since both key
380 * and data are fixed size, this is relatively easy. Note that this
381 * relies on the keyLength being a constant; we verify the keyLength
384 if ((char *)srcKey
+ sizeof(HFSPlusExtentKey
) + recordSize
> nextRecord
) {
385 printf("hfs_swap_HFSPlusBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
386 return fsBTInvalidNodeErr
;
389 if (direction
== kSwapBTNodeBigToHost
)
390 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
391 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
392 printf("hfs_swap_HFSPlusBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
393 return fsBTInvalidNodeErr
;
395 srcRec
= (HFSPlusExtentDescriptor
*)((char *)srcKey
+ srcKey
->keyLength
+ sizeof(srcKey
->keyLength
));
396 if (direction
== kSwapBTNodeHostToBig
)
397 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
399 /* Don't swap srcKey->forkType; it's only one byte */
400 /* Don't swap srcKey->pad */
402 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
403 srcKey
->startBlock
= SWAP_BE32 (srcKey
->startBlock
);
405 if (srcDesc
->kind
== kBTIndexNode
) {
406 /* For index nodes, the record data is just a child node number. */
407 *((UInt32
*)srcRec
) = SWAP_BE32 (*((UInt32
*)srcRec
));
409 /* Swap the extent data */
410 for (j
= 0; j
< kHFSPlusExtentDensity
; j
++) {
411 srcRec
[j
].startBlock
= SWAP_BE32 (srcRec
[j
].startBlock
);
412 srcRec
[j
].blockCount
= SWAP_BE32 (srcRec
[j
].blockCount
);
417 } else if (fileID
== kHFSCatalogFileID
) {
418 HFSPlusCatalogKey
*srcKey
;
422 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
423 /* Point to the start of the record we're currently checking. */
424 srcKey
= (HFSPlusCatalogKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
427 * Point to start of next (larger offset) record. We'll use this
428 * to be sure the current record doesn't overflow into the next
431 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
434 * Make sure we can safely dereference the keyLength and parentID fields. */
435 if ((char *)srcKey
+ offsetof(HFSPlusCatalogKey
, nodeName
.unicode
[0]) > nextRecord
) {
436 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
437 return fsBTInvalidNodeErr
;
441 * Swap and sanity check the key length
443 if (direction
== kSwapBTNodeBigToHost
)
444 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
445 keyLength
= srcKey
->keyLength
; /* Put it in a local (native order) because we use it several times */
446 if (direction
== kSwapBTNodeHostToBig
)
447 srcKey
->keyLength
= SWAP_BE16 (keyLength
);
449 /* Sanity check the key length */
450 if (keyLength
< kHFSPlusCatalogKeyMinimumLength
|| keyLength
> kHFSPlusCatalogKeyMaximumLength
) {
451 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
452 return fsBTInvalidNodeErr
;
456 * Make sure that we can safely dereference the record's type field or
457 * an index node's child node number.
459 srcPtr
= (SInt16
*)((char *)srcKey
+ keyLength
+ sizeof(srcKey
->keyLength
));
460 if ((char *)srcPtr
+ sizeof(UInt32
) > nextRecord
) {
461 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
462 return fsBTInvalidNodeErr
;
465 srcKey
->parentID
= SWAP_BE32 (srcKey
->parentID
);
468 * Swap and sanity check the key's node name
470 if (direction
== kSwapBTNodeBigToHost
)
471 srcKey
->nodeName
.length
= SWAP_BE16 (srcKey
->nodeName
.length
);
472 /* Make sure name length is consistent with key length */
473 if (keyLength
< sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
474 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0])) {
475 printf("hfs_swap_HFSPlusBTInternalNode: catalog record #%d keyLength=%d expected=%d\n",
476 srcDesc
->numRecords
-i
, keyLength
, sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
477 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0]));
478 return fsBTInvalidNodeErr
;
480 for (j
= 0; j
< srcKey
->nodeName
.length
; j
++) {
481 srcKey
->nodeName
.unicode
[j
] = SWAP_BE16 (srcKey
->nodeName
.unicode
[j
]);
483 if (direction
== kSwapBTNodeHostToBig
)
484 srcKey
->nodeName
.length
= SWAP_BE16 (srcKey
->nodeName
.length
);
487 * For index nodes, the record data is just the child's node number.
488 * Skip over swapping the various types of catalog record.
490 if (srcDesc
->kind
== kBTIndexNode
) {
491 *((UInt32
*)srcPtr
) = SWAP_BE32 (*((UInt32
*)srcPtr
));
495 /* Make sure the recordType is in native order before using it. */
496 if (direction
== kSwapBTNodeBigToHost
)
497 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
499 if (srcPtr
[0] == kHFSPlusFolderRecord
) {
500 HFSPlusCatalogFolder
*srcRec
= (HFSPlusCatalogFolder
*)srcPtr
;
501 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
502 printf("hfs_swap_HFSPlusBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
503 return fsBTInvalidNodeErr
;
506 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
507 srcRec
->valence
= SWAP_BE32 (srcRec
->valence
);
508 srcRec
->folderID
= SWAP_BE32 (srcRec
->folderID
);
509 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
510 srcRec
->contentModDate
= SWAP_BE32 (srcRec
->contentModDate
);
511 srcRec
->attributeModDate
= SWAP_BE32 (srcRec
->attributeModDate
);
512 srcRec
->accessDate
= SWAP_BE32 (srcRec
->accessDate
);
513 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
515 srcRec
->bsdInfo
.ownerID
= SWAP_BE32 (srcRec
->bsdInfo
.ownerID
);
516 srcRec
->bsdInfo
.groupID
= SWAP_BE32 (srcRec
->bsdInfo
.groupID
);
518 /* Don't swap srcRec->bsdInfo.adminFlags; it's only one byte */
519 /* Don't swap srcRec->bsdInfo.ownerFlags; it's only one byte */
521 srcRec
->bsdInfo
.fileMode
= SWAP_BE16 (srcRec
->bsdInfo
.fileMode
);
522 srcRec
->bsdInfo
.special
.iNodeNum
= SWAP_BE32 (srcRec
->bsdInfo
.special
.iNodeNum
);
524 srcRec
->textEncoding
= SWAP_BE32 (srcRec
->textEncoding
);
526 /* Don't swap srcRec->userInfo */
527 /* Don't swap srcRec->finderInfo */
528 /* Don't swap srcRec->reserved */
530 } else if (srcPtr
[0] == kHFSPlusFileRecord
) {
531 HFSPlusCatalogFile
*srcRec
= (HFSPlusCatalogFile
*)srcPtr
;
532 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
533 printf("hfs_swap_HFSPlusBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
534 return fsBTInvalidNodeErr
;
537 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
539 srcRec
->fileID
= SWAP_BE32 (srcRec
->fileID
);
541 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
542 srcRec
->contentModDate
= SWAP_BE32 (srcRec
->contentModDate
);
543 srcRec
->attributeModDate
= SWAP_BE32 (srcRec
->attributeModDate
);
544 srcRec
->accessDate
= SWAP_BE32 (srcRec
->accessDate
);
545 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
547 srcRec
->bsdInfo
.ownerID
= SWAP_BE32 (srcRec
->bsdInfo
.ownerID
);
548 srcRec
->bsdInfo
.groupID
= SWAP_BE32 (srcRec
->bsdInfo
.groupID
);
550 /* Don't swap srcRec->bsdInfo.adminFlags; it's only one byte */
551 /* Don't swap srcRec->bsdInfo.ownerFlags; it's only one byte */
553 srcRec
->bsdInfo
.fileMode
= SWAP_BE16 (srcRec
->bsdInfo
.fileMode
);
554 srcRec
->bsdInfo
.special
.iNodeNum
= SWAP_BE32 (srcRec
->bsdInfo
.special
.iNodeNum
);
556 srcRec
->textEncoding
= SWAP_BE32 (srcRec
->textEncoding
);
558 /* Don't swap srcRec->reserved1 */
559 /* Don't swap srcRec->userInfo */
560 /* Don't swap srcRec->finderInfo */
561 /* Don't swap srcRec->reserved2 */
563 hfs_swap_HFSPlusForkData (&srcRec
->dataFork
);
564 hfs_swap_HFSPlusForkData (&srcRec
->resourceFork
);
566 } else if ((srcPtr
[0] == kHFSPlusFolderThreadRecord
) ||
567 (srcPtr
[0] == kHFSPlusFileThreadRecord
)) {
570 * Make sure there is room for parentID and name length.
572 HFSPlusCatalogThread
*srcRec
= (HFSPlusCatalogThread
*)srcPtr
;
573 if ((char *) &srcRec
->nodeName
.unicode
[0] > nextRecord
) {
574 printf("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
575 return fsBTInvalidNodeErr
;
578 /* Don't swap srcRec->reserved */
580 srcRec
->parentID
= SWAP_BE32 (srcRec
->parentID
);
582 if (direction
== kSwapBTNodeBigToHost
)
583 srcRec
->nodeName
.length
= SWAP_BE16 (srcRec
->nodeName
.length
);
586 * Make sure there is room for the name in the buffer.
587 * Then swap the characters of the name itself.
589 if ((char *) &srcRec
->nodeName
.unicode
[srcRec
->nodeName
.length
] > nextRecord
) {
590 printf("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
591 return fsBTInvalidNodeErr
;
593 for (j
= 0; j
< srcRec
->nodeName
.length
; j
++) {
594 srcRec
->nodeName
.unicode
[j
] = SWAP_BE16 (srcRec
->nodeName
.unicode
[j
]);
597 if (direction
== kSwapBTNodeHostToBig
)
598 srcRec
->nodeName
.length
= SWAP_BE16 (srcRec
->nodeName
.length
);
601 printf("hfs_swap_HFSPlusBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
602 return fsBTInvalidNodeErr
;
605 /* We can swap the record type now that we're done using it. */
606 if (direction
== kSwapBTNodeHostToBig
)
607 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
610 } else if (fileID
== kHFSAttributesFileID
) {
611 HFSPlusAttrKey
*srcKey
;
612 HFSPlusAttrRecord
*srcRec
;
614 u_int32_t attrSize
= 0;
616 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
617 /* Point to the start of the record we're currently checking. */
618 srcKey
= (HFSPlusAttrKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
621 * Point to start of next (larger offset) record. We'll use this
622 * to be sure the current record doesn't overflow into the next
625 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
627 /* Make sure there is room in the buffer for a minimal key */
628 if ((char *) &srcKey
->attrName
[1] > nextRecord
) {
629 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
630 return fsBTInvalidNodeErr
;
633 /* Swap the key length field */
634 if (direction
== kSwapBTNodeBigToHost
)
635 srcKey
->keyLength
= SWAP_BE16(srcKey
->keyLength
);
636 keyLength
= srcKey
->keyLength
; /* Keep a copy in native order */
637 if (direction
== kSwapBTNodeHostToBig
)
638 srcKey
->keyLength
= SWAP_BE16(srcKey
->keyLength
);
641 * Make sure that we can safely dereference the record's type field or
642 * an index node's child node number.
644 srcRec
= (HFSPlusAttrRecord
*)((char *)srcKey
+ keyLength
+ sizeof(srcKey
->keyLength
));
645 if ((char *)srcRec
+ sizeof(u_int32_t
) > nextRecord
) {
646 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d too big (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
647 return fsBTInvalidNodeErr
;
650 srcKey
->fileID
= SWAP_BE32(srcKey
->fileID
);
651 srcKey
->startBlock
= SWAP_BE32(srcKey
->startBlock
);
654 * Swap and check the attribute name
656 if (direction
== kSwapBTNodeBigToHost
)
657 srcKey
->attrNameLen
= SWAP_BE16(srcKey
->attrNameLen
);
658 /* Sanity check the attribute name length */
659 if (srcKey
->attrNameLen
> kHFSMaxAttrNameLen
|| keyLength
< (kHFSPlusAttrKeyMinimumLength
+ sizeof(u_int16_t
)*srcKey
->attrNameLen
)) {
660 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d keyLength=%d attrNameLen=%d\n", srcDesc
->numRecords
-i
-1, keyLength
, srcKey
->attrNameLen
);
661 return fsBTInvalidNodeErr
;
663 for (j
= 0; j
< srcKey
->attrNameLen
; j
++)
664 srcKey
->attrName
[j
] = SWAP_BE16(srcKey
->attrName
[j
]);
665 if (direction
== kSwapBTNodeHostToBig
)
666 srcKey
->attrNameLen
= SWAP_BE16(srcKey
->attrNameLen
);
669 * For index nodes, the record data is just the child's node number.
670 * Skip over swapping the various types of attribute record.
672 if (srcDesc
->kind
== kBTIndexNode
) {
673 *((UInt32
*)srcRec
) = SWAP_BE32 (*((UInt32
*)srcRec
));
677 /* Swap the record data */
678 if (direction
== kSwapBTNodeBigToHost
)
679 srcRec
->recordType
= SWAP_BE32(srcRec
->recordType
);
680 switch (srcRec
->recordType
) {
681 case kHFSPlusAttrInlineData
:
682 /* Is there room for the inline data header? */
683 if ((char *) &srcRec
->attrData
.attrData
[0] > nextRecord
) {
684 printf("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big\n", srcDesc
->numRecords
-i
-1);
685 return fsBTInvalidNodeErr
;
688 /* We're not swapping the reserved fields */
690 /* Swap the attribute size */
691 if (direction
== kSwapBTNodeHostToBig
)
692 attrSize
= srcRec
->attrData
.attrSize
;
693 srcRec
->attrData
.attrSize
= SWAP_BE32(srcRec
->attrData
.attrSize
);
694 if (direction
== kSwapBTNodeBigToHost
)
695 attrSize
= srcRec
->attrData
.attrSize
;
697 /* Is there room for the inline attribute data? */
698 if ((char *) &srcRec
->attrData
.attrData
[attrSize
] > nextRecord
) {
699 printf("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big (attrSize=%u)\n", srcDesc
->numRecords
-i
-1, attrSize
);
700 return fsBTInvalidNodeErr
;
703 /* Not swapping the attribute data itself */
706 case kHFSPlusAttrForkData
:
707 /* Is there room for the fork data record? */
708 if ((char *)srcRec
+ sizeof(HFSPlusAttrForkData
) > nextRecord
) {
709 printf("hfs_swap_HFSPlusBTInternalNode: attr fork data #%d too big\n", srcDesc
->numRecords
-i
-1);
710 return fsBTInvalidNodeErr
;
713 /* We're not swapping the reserved field */
715 hfs_swap_HFSPlusForkData(&srcRec
->forkData
.theFork
);
718 case kHFSPlusAttrExtents
:
719 /* Is there room for an extent record? */
720 if ((char *)srcRec
+ sizeof(HFSPlusAttrExtents
) > nextRecord
) {
721 printf("hfs_swap_HFSPlusBTInternalNode: attr extents #%d too big\n", srcDesc
->numRecords
-i
-1);
722 return fsBTInvalidNodeErr
;
725 /* We're not swapping the reserved field */
727 for (j
= 0; j
< kHFSPlusExtentDensity
; j
++) {
728 srcRec
->overflowExtents
.extents
[j
].startBlock
=
729 SWAP_BE32(srcRec
->overflowExtents
.extents
[j
].startBlock
);
730 srcRec
->overflowExtents
.extents
[j
].blockCount
=
731 SWAP_BE32(srcRec
->overflowExtents
.extents
[j
].blockCount
);
735 if (direction
== kSwapBTNodeHostToBig
)
736 srcRec
->recordType
= SWAP_BE32(srcRec
->recordType
);
738 } else if (fileID
> kHFSFirstUserCatalogNodeID
) {
739 /* The only B-tree with a non-system CNID that we use is the hotfile B-tree */
743 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
744 /* Point to the start of the record we're currently checking. */
745 srcKey
= (HotFileKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
748 * Point to start of next (larger offset) record. We'll use this
749 * to be sure the current record doesn't overflow into the next
752 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
754 /* Make sure there is room for the key (HotFileKey) and data (UInt32) */
755 if ((char *)srcKey
+ sizeof(HotFileKey
) + sizeof(UInt32
) > nextRecord
) {
756 printf("hfs_swap_HFSPlusBTInternalNode: hotfile #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
757 return fsBTInvalidNodeErr
;
760 /* Swap and sanity check the key length field */
761 if (direction
== kSwapBTNodeBigToHost
)
762 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
763 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
764 printf("hfs_swap_HFSPlusBTInternalNode: hotfile #%d incorrect keyLength %d\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
765 return fsBTInvalidNodeErr
;
767 srcRec
= (u_int32_t
*)((char *)srcKey
+ srcKey
->keyLength
+ sizeof(srcKey
->keyLength
));
768 if (direction
== kSwapBTNodeHostToBig
)
769 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
771 /* Don't swap srcKey->forkType */
772 /* Don't swap srcKey->pad */
774 srcKey
->temperature
= SWAP_BE32 (srcKey
->temperature
);
775 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
777 *((UInt32
*)srcRec
) = SWAP_BE32 (*((UInt32
*)srcRec
));
780 panic ("hfs_swap_HFSPlusBTInternalNode: fileID %u is not a system B-tree\n", fileID
);
788 hfs_swap_HFSBTInternalNode (
789 BlockDescriptor
*src
,
790 HFSCatalogNodeID fileID
,
791 enum HFSBTSwapDirection direction
794 BTNodeDescriptor
*srcDesc
= src
->buffer
;
795 UInt16
*srcOffs
= (UInt16
*)((char *)src
->buffer
+ (src
->blockSize
- (srcDesc
->numRecords
* sizeof (UInt16
))));
796 char *nextRecord
; /* Points to start of record following current one */
801 if (fileID
== kHFSExtentsFileID
) {
802 HFSExtentKey
*srcKey
;
803 HFSExtentDescriptor
*srcRec
;
804 size_t recordSize
; /* Size of the data part of the record, or node number for index nodes */
806 if (srcDesc
->kind
== kBTIndexNode
)
807 recordSize
= sizeof(UInt32
);
809 recordSize
= sizeof(HFSExtentDescriptor
);
811 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
812 /* Point to the start of the record we're currently checking. */
813 srcKey
= (HFSExtentKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
816 * Point to start of next (larger offset) record. We'll use this
817 * to be sure the current record doesn't overflow into the next
820 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
823 * Make sure the key and data are within the buffer. Since both key
824 * and data are fixed size, this is relatively easy. Note that this
825 * relies on the keyLength being a constant; we verify the keyLength
828 if ((char *)srcKey
+ sizeof(HFSExtentKey
) + recordSize
> nextRecord
) {
829 printf("hfs_swap_HFSBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
830 return fsBTInvalidNodeErr
;
833 /* Don't swap srcKey->keyLength (it's only one byte), but do sanity check it */
834 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
835 printf("hfs_swap_HFSBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
836 return fsBTInvalidNodeErr
;
839 /* Don't swap srcKey->forkType; it's only one byte */
841 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
842 srcKey
->startBlock
= SWAP_BE16 (srcKey
->startBlock
);
844 /* Point to record data (round up to even byte boundary) */
845 srcRec
= (HFSExtentDescriptor
*)((char *)srcKey
+ ((srcKey
->keyLength
+ 2) & ~1));
847 if (srcDesc
->kind
== kBTIndexNode
) {
848 /* For index nodes, the record data is just a child node number. */
849 *((UInt32
*)srcRec
) = SWAP_BE32 (*((UInt32
*)srcRec
));
851 /* Swap the extent data */
852 for (j
= 0; j
< kHFSExtentDensity
; j
++) {
853 srcRec
[j
].startBlock
= SWAP_BE16 (srcRec
[j
].startBlock
);
854 srcRec
[j
].blockCount
= SWAP_BE16 (srcRec
[j
].blockCount
);
859 } else if (fileID
== kHFSCatalogFileID
) {
860 HFSCatalogKey
*srcKey
;
862 unsigned expectedKeyLength
;
864 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
865 /* Point to the start of the record we're currently checking. */
866 srcKey
= (HFSCatalogKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
869 * Point to start of next (larger offset) record. We'll use this
870 * to be sure the current record doesn't overflow into the next
873 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
876 * Make sure we can safely dereference the keyLength and parentID fields.
877 * The value 8 below is 1 bytes for keyLength + 1 byte reserved + 4 bytes
878 * for parentID + 1 byte for nodeName's length + 1 byte to round up the
879 * record start to an even offset, which forms a minimal key.
881 if ((char *)srcKey
+ 8 > nextRecord
) {
882 printf("hfs_swap_HFSBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
883 return fsBTInvalidNodeErr
;
886 /* Don't swap srcKey->keyLength (it's only one byte), but do sanity check it */
887 if (srcKey
->keyLength
< kHFSCatalogKeyMinimumLength
|| srcKey
->keyLength
> kHFSCatalogKeyMaximumLength
) {
888 printf("hfs_swap_HFSBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
889 return fsBTInvalidNodeErr
;
892 /* Don't swap srcKey->reserved */
894 srcKey
->parentID
= SWAP_BE32 (srcKey
->parentID
);
896 /* Don't swap srcKey->nodeName */
898 /* Make sure the keyLength is big enough for the key's content */
899 if (srcDesc
->kind
== kBTIndexNode
)
900 expectedKeyLength
= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
);
902 expectedKeyLength
= srcKey
->nodeName
[0] + kHFSCatalogKeyMinimumLength
;
903 if (srcKey
->keyLength
< expectedKeyLength
) {
904 printf("hfs_swap_HFSBTInternalNode: catalog record #%d keyLength=%u expected=%u\n",
905 srcDesc
->numRecords
-i
, srcKey
->keyLength
, expectedKeyLength
);
906 return fsBTInvalidNodeErr
;
909 /* Point to record data (round up to even byte boundary) */
910 srcPtr
= (SInt16
*)((char *)srcKey
+ ((srcKey
->keyLength
+ 2) & ~1));
913 * Make sure that we can safely dereference the record's type field or
914 * and index node's child node number.
916 if ((char *)srcPtr
+ sizeof(UInt32
) > nextRecord
) {
917 printf("hfs_swap_HFSBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
918 return fsBTInvalidNodeErr
;
922 * For index nodes, the record data is just the child's node number.
923 * Skip over swapping the various types of catalog record.
925 if (srcDesc
->kind
== kBTIndexNode
) {
926 *((UInt32
*)srcPtr
) = SWAP_BE32 (*((UInt32
*)srcPtr
));
930 /* Make sure the recordType is in native order before using it. */
931 if (direction
== kSwapBTNodeBigToHost
)
932 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
934 if (srcPtr
[0] == kHFSFolderRecord
) {
935 HFSCatalogFolder
*srcRec
= (HFSCatalogFolder
*)srcPtr
;
936 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
937 printf("hfs_swap_HFSBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
938 return fsBTInvalidNodeErr
;
941 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
942 srcRec
->valence
= SWAP_BE16 (srcRec
->valence
);
944 srcRec
->folderID
= SWAP_BE32 (srcRec
->folderID
);
945 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
946 srcRec
->modifyDate
= SWAP_BE32 (srcRec
->modifyDate
);
947 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
949 /* Don't swap srcRec->userInfo */
950 /* Don't swap srcRec->finderInfo */
951 /* Don't swap resserved array */
953 } else if (srcPtr
[0] == kHFSFileRecord
) {
954 HFSCatalogFile
*srcRec
= (HFSCatalogFile
*)srcPtr
;
955 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
956 printf("hfs_swap_HFSBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
957 return fsBTInvalidNodeErr
;
960 srcRec
->flags
= srcRec
->flags
;
961 srcRec
->fileType
= srcRec
->fileType
;
963 /* Don't swap srcRec->userInfo */
965 srcRec
->fileID
= SWAP_BE32 (srcRec
->fileID
);
967 srcRec
->dataStartBlock
= SWAP_BE16 (srcRec
->dataStartBlock
);
968 srcRec
->dataLogicalSize
= SWAP_BE32 (srcRec
->dataLogicalSize
);
969 srcRec
->dataPhysicalSize
= SWAP_BE32 (srcRec
->dataPhysicalSize
);
971 srcRec
->rsrcStartBlock
= SWAP_BE16 (srcRec
->rsrcStartBlock
);
972 srcRec
->rsrcLogicalSize
= SWAP_BE32 (srcRec
->rsrcLogicalSize
);
973 srcRec
->rsrcPhysicalSize
= SWAP_BE32 (srcRec
->rsrcPhysicalSize
);
975 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
976 srcRec
->modifyDate
= SWAP_BE32 (srcRec
->modifyDate
);
977 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
979 /* Don't swap srcRec->finderInfo */
981 srcRec
->clumpSize
= SWAP_BE16 (srcRec
->clumpSize
);
983 /* Swap the two sets of extents as an array of six (three each) UInt16 */
984 for (j
= 0; j
< kHFSExtentDensity
* 2; j
++) {
985 srcRec
->dataExtents
[j
].startBlock
= SWAP_BE16 (srcRec
->dataExtents
[j
].startBlock
);
986 srcRec
->dataExtents
[j
].blockCount
= SWAP_BE16 (srcRec
->dataExtents
[j
].blockCount
);
989 /* Don't swap srcRec->reserved */
991 } else if ((srcPtr
[0] == kHFSFolderThreadRecord
) ||
992 (srcPtr
[0] == kHFSFileThreadRecord
)) {
993 HFSCatalogThread
*srcRec
= (HFSCatalogThread
*)srcPtr
;
995 /* Make sure there is room for parentID and name length */
996 if ((char *) &srcRec
->nodeName
[1] > nextRecord
) {
997 printf("hfs_swap_HFSBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
998 return fsBTInvalidNodeErr
;
1001 /* Don't swap srcRec->reserved array */
1003 srcRec
->parentID
= SWAP_BE32 (srcRec
->parentID
);
1005 /* Don't swap srcRec->nodeName */
1007 /* Make sure there is room for the name in the buffer */
1008 if ((char *) &srcRec
->nodeName
[srcRec
->nodeName
[0]] > nextRecord
) {
1009 printf("hfs_swap_HFSBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
1010 return fsBTInvalidNodeErr
;
1013 printf("hfs_swap_HFSBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
1014 return fsBTInvalidNodeErr
;
1017 /* We can swap the record type now that we're done using it */
1018 if (direction
== kSwapBTNodeHostToBig
)
1019 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
1023 panic ("hfs_swap_HFSBTInternalNode: fileID %u is not a system B-tree\n", fileID
);