2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * This file implements endian swapping routines for the HFS/HFS Plus
30 #include "hfs_endian.h"
32 #include "hfscommon/headers/BTreesPrivate.h"
37 * Internal swapping routines
39 * These routines handle swapping the records of leaf and index nodes. The
40 * layout of the keys and records varies depending on the kind of B-tree
41 * (determined by fileID).
43 * The direction parameter must be kSwapBTNodeBigToHost or kSwapBTNodeHostToBig.
44 * The kSwapBTNodeHeaderRecordOnly "direction" is not valid for these routines.
46 static int hfs_swap_HFSPlusBTInternalNode (BlockDescriptor
*src
, HFSCatalogNodeID fileID
, enum HFSBTSwapDirection direction
);
47 static int hfs_swap_HFSBTInternalNode (BlockDescriptor
*src
, HFSCatalogNodeID fileID
, enum HFSBTSwapDirection direction
);
50 * hfs_swap_HFSPlusForkData
53 hfs_swap_HFSPlusForkData (
59 src
->logicalSize
= SWAP_BE64 (src
->logicalSize
);
61 src
->clumpSize
= SWAP_BE32 (src
->clumpSize
);
62 src
->totalBlocks
= SWAP_BE32 (src
->totalBlocks
);
64 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
65 src
->extents
[i
].startBlock
= SWAP_BE32 (src
->extents
[i
].startBlock
);
66 src
->extents
[i
].blockCount
= SWAP_BE32 (src
->extents
[i
].blockCount
);
73 * NOTE: This operation is not naturally symmetric.
74 * We have to determine which way we're swapping things.
80 enum HFSBTSwapDirection direction
83 BTNodeDescriptor
*srcDesc
= src
->buffer
;
84 UInt16
*srcOffs
= NULL
;
85 BTreeControlBlockPtr btcb
= (BTreeControlBlockPtr
)VTOF(vp
)->fcbBTCBPtr
;
90 if (direction
== kSwapBTNodeBigToHost
) {
91 printf ("BE -> Native Swap\n");
92 } else if (direction
== kSwapBTNodeHostToBig
) {
93 printf ("Native -> BE Swap\n");
94 } else if (direction
== kSwapBTNodeHeaderRecordOnly
) {
95 printf ("Not swapping descriptors\n");
97 panic ("hfs_swap_BTNode: This is impossible");
102 * If we are doing a swap from on-disk to in-memory, then swap the node
103 * descriptor and record offsets before we need to use them.
105 if (direction
== kSwapBTNodeBigToHost
) {
106 srcDesc
->fLink
= SWAP_BE32 (srcDesc
->fLink
);
107 srcDesc
->bLink
= SWAP_BE32 (srcDesc
->bLink
);
110 * When first opening a BTree, we have to read the header node before the
111 * control block is initialized. In this case, totalNodes will be zero,
112 * so skip the bounds checking.
114 if (btcb
->totalNodes
!= 0) {
115 if (srcDesc
->fLink
>= btcb
->totalNodes
) {
116 printf("hfs_swap_BTNode: invalid forward link (0x%08X)\n", srcDesc
->fLink
);
117 error
= fsBTInvalidHeaderErr
;
120 if (srcDesc
->bLink
>= btcb
->totalNodes
) {
121 printf("hfs_swap_BTNode: invalid backward link (0x%08X)\n", srcDesc
->bLink
);
122 error
= fsBTInvalidHeaderErr
;
128 * Check srcDesc->kind. Don't swap it because it's only one byte.
130 if (srcDesc
->kind
< kBTLeafNode
|| srcDesc
->kind
> kBTMapNode
) {
131 printf("hfs_swap_BTNode: invalid node kind (%d)\n", srcDesc
->kind
);
132 error
= fsBTInvalidHeaderErr
;
137 * Check srcDesc->height. Don't swap it because it's only one byte.
139 if (srcDesc
->height
> btcb
->treeDepth
) {
140 printf("hfs_swap_BTNode: invalid node height (%d)\n", srcDesc
->height
);
141 error
= fsBTInvalidHeaderErr
;
145 /* Don't swap srcDesc->reserved */
147 srcDesc
->numRecords
= SWAP_BE16 (srcDesc
->numRecords
);
150 * Swap the node offsets (including the free space one!).
152 srcOffs
= (UInt16
*)((char *)src
->buffer
+ (src
->blockSize
- ((srcDesc
->numRecords
+ 1) * sizeof (UInt16
))));
155 * Sanity check that the record offsets are within the node itself.
157 if ((char *)srcOffs
> ((char *)src
->buffer
+ src
->blockSize
) ||
158 (char *)srcOffs
< ((char *)src
->buffer
+ sizeof(BTNodeDescriptor
))) {
159 printf("hfs_swap_BTNode: invalid record count (0x%04X)\n", srcDesc
->numRecords
);
160 error
= fsBTInvalidHeaderErr
;
165 * Swap and sanity check each of the record offsets.
167 for (i
= 0; i
<= srcDesc
->numRecords
; i
++) {
168 srcOffs
[i
] = SWAP_BE16 (srcOffs
[i
]);
171 * Sanity check: must be even, and within the node itself.
173 * We may be called to swap an unused node, which contains all zeroes.
174 * This is why we allow the record offset to be zero.
176 if ((srcOffs
[i
] & 1) || (srcOffs
[i
] < sizeof(BTNodeDescriptor
) && srcOffs
[i
] != 0) || (srcOffs
[i
] >= src
->blockSize
)) {
177 printf("hfs_swap_BTNode: record #%d invalid offset (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
178 error
= fsBTInvalidHeaderErr
;
183 * Make sure the offsets are strictly increasing. Note that we're looping over
184 * them backwards, hence the order in the comparison.
186 if ((i
!= 0) && (srcOffs
[i
] >= srcOffs
[i
-1])) {
187 printf("hfs_swap_BTNode: offsets %d and %d out of order (0x%04X, 0x%04X)\n",
188 srcDesc
->numRecords
-i
-1, srcDesc
->numRecords
-i
, srcOffs
[i
], srcOffs
[i
-1]);
189 error
= fsBTInvalidHeaderErr
;
196 * Swap the records (ordered by frequency of access)
198 if ((srcDesc
->kind
== kBTIndexNode
) ||
199 (srcDesc
-> kind
== kBTLeafNode
)) {
201 if (VTOVCB(vp
)->vcbSigWord
== kHFSPlusSigWord
) {
202 error
= hfs_swap_HFSPlusBTInternalNode (src
, VTOC(vp
)->c_fileid
, direction
);
204 error
= hfs_swap_HFSBTInternalNode (src
, VTOC(vp
)->c_fileid
, direction
);
207 if (error
) goto fail
;
209 } else if (srcDesc
-> kind
== kBTMapNode
) {
210 /* Don't swap the bitmaps, they'll be done in the bitmap routines */
212 } else if (srcDesc
-> kind
== kBTHeaderNode
) {
213 /* The header's offset is hard-wired because we cannot trust the offset pointers. */
214 BTHeaderRec
*srcHead
= (BTHeaderRec
*)((char *)src
->buffer
+ sizeof(BTNodeDescriptor
));
216 srcHead
->treeDepth
= SWAP_BE16 (srcHead
->treeDepth
);
218 srcHead
->rootNode
= SWAP_BE32 (srcHead
->rootNode
);
219 srcHead
->leafRecords
= SWAP_BE32 (srcHead
->leafRecords
);
220 srcHead
->firstLeafNode
= SWAP_BE32 (srcHead
->firstLeafNode
);
221 srcHead
->lastLeafNode
= SWAP_BE32 (srcHead
->lastLeafNode
);
223 srcHead
->nodeSize
= SWAP_BE16 (srcHead
->nodeSize
);
224 srcHead
->maxKeyLength
= SWAP_BE16 (srcHead
->maxKeyLength
);
226 srcHead
->totalNodes
= SWAP_BE32 (srcHead
->totalNodes
);
227 srcHead
->freeNodes
= SWAP_BE32 (srcHead
->freeNodes
);
229 srcHead
->clumpSize
= SWAP_BE32 (srcHead
->clumpSize
);
230 srcHead
->attributes
= SWAP_BE32 (srcHead
->attributes
);
232 /* Don't swap srcHead->reserved1 */
233 /* Don't swap srcHead->btreeType; it's only one byte */
234 /* Don't swap srcHead->reserved2 */
235 /* Don't swap srcHead->reserved3 */
236 /* Don't swap bitmap */
240 * If we are doing a swap from in-memory to on-disk, then swap the node
241 * descriptor and record offsets after we're done using them.
243 if (direction
== kSwapBTNodeHostToBig
) {
245 * Sanity check and swap the forkward and backward links.
247 if (srcDesc
->fLink
>= btcb
->totalNodes
) {
248 printf("hfs_UNswap_BTNode: invalid forward link (0x%08X)\n", srcDesc
->fLink
);
249 error
= fsBTInvalidHeaderErr
;
252 if (srcDesc
->bLink
>= btcb
->totalNodes
) {
253 printf("hfs_UNswap_BTNode: invalid backward link (0x%08X)\n", srcDesc
->bLink
);
254 error
= fsBTInvalidHeaderErr
;
257 srcDesc
->fLink
= SWAP_BE32 (srcDesc
->fLink
);
258 srcDesc
->bLink
= SWAP_BE32 (srcDesc
->bLink
);
261 * Check srcDesc->kind. Don't swap it because it's only one byte.
263 if (srcDesc
->kind
< kBTLeafNode
|| srcDesc
->kind
> kBTMapNode
) {
264 printf("hfs_UNswap_BTNode: invalid node kind (%d)\n", srcDesc
->kind
);
265 error
= fsBTInvalidHeaderErr
;
270 * Check srcDesc->height. Don't swap it because it's only one byte.
272 if (srcDesc
->height
> btcb
->treeDepth
) {
273 printf("hfs_UNswap_BTNode: invalid node height (%d)\n", srcDesc
->height
);
274 error
= fsBTInvalidHeaderErr
;
278 /* Don't swap srcDesc->reserved */
281 * Swap the node offsets (including the free space one!).
283 srcOffs
= (UInt16
*)((char *)src
->buffer
+ (src
->blockSize
- ((srcDesc
->numRecords
+ 1) * sizeof (UInt16
))));
286 * Sanity check that the record offsets are within the node itself.
288 if ((char *)srcOffs
> ((char *)src
->buffer
+ src
->blockSize
) ||
289 (char *)srcOffs
< ((char *)src
->buffer
+ sizeof(BTNodeDescriptor
))) {
290 printf("hfs_UNswap_BTNode: invalid record count (0x%04X)\n", srcDesc
->numRecords
);
291 error
= fsBTInvalidHeaderErr
;
296 * Swap and sanity check each of the record offsets.
298 for (i
= 0; i
<= srcDesc
->numRecords
; i
++) {
300 * Sanity check: must be even, and within the node itself.
302 * We may be called to swap an unused node, which contains all zeroes.
303 * This is why we allow the record offset to be zero.
305 if ((srcOffs
[i
] & 1) || (srcOffs
[i
] < sizeof(BTNodeDescriptor
) && srcOffs
[i
] != 0) || (srcOffs
[i
] >= src
->blockSize
)) {
306 printf("hfs_UNswap_BTNode: record #%d invalid offset (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
307 error
= fsBTInvalidHeaderErr
;
312 * Make sure the offsets are strictly increasing. Note that we're looping over
313 * them backwards, hence the order in the comparison.
315 if ((i
< srcDesc
->numRecords
) && (srcOffs
[i
+1] >= srcOffs
[i
])) {
316 printf("hfs_UNswap_BTNode: offsets %d and %d out of order (0x%04X, 0x%04X)\n",
317 srcDesc
->numRecords
-i
-2, srcDesc
->numRecords
-i
-1, srcOffs
[i
+1], srcOffs
[i
]);
318 error
= fsBTInvalidHeaderErr
;
322 srcOffs
[i
] = SWAP_BE16 (srcOffs
[i
]);
325 srcDesc
->numRecords
= SWAP_BE16 (srcDesc
->numRecords
);
331 * Log some useful information about where the corrupt node is.
333 printf("node=%lld fileID=%u volume=%s device=%s\n", src
->blockNum
, VTOC(vp
)->c_fileid
,
334 VTOVCB(vp
)->vcbVN
, vfs_statfs(vnode_mount(vp
))->f_mntfromname
);
335 VTOVCB(vp
)->vcbFlags
|= kHFS_DamagedVolume
;
342 hfs_swap_HFSPlusBTInternalNode (
343 BlockDescriptor
*src
,
344 HFSCatalogNodeID fileID
,
345 enum HFSBTSwapDirection direction
348 BTNodeDescriptor
*srcDesc
= src
->buffer
;
349 UInt16
*srcOffs
= (UInt16
*)((char *)src
->buffer
+ (src
->blockSize
- (srcDesc
->numRecords
* sizeof (UInt16
))));
350 char *nextRecord
; /* Points to start of record following current one */
354 if (fileID
== kHFSExtentsFileID
) {
355 HFSPlusExtentKey
*srcKey
;
356 HFSPlusExtentDescriptor
*srcRec
;
357 size_t recordSize
; /* Size of the data part of the record, or node number for index nodes */
359 if (srcDesc
->kind
== kBTIndexNode
)
360 recordSize
= sizeof(UInt32
);
362 recordSize
= sizeof(HFSPlusExtentDescriptor
);
364 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
365 /* Point to the start of the record we're currently checking. */
366 srcKey
= (HFSPlusExtentKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
369 * Point to start of next (larger offset) record. We'll use this
370 * to be sure the current record doesn't overflow into the next
373 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
376 * Make sure the key and data are within the buffer. Since both key
377 * and data are fixed size, this is relatively easy. Note that this
378 * relies on the keyLength being a constant; we verify the keyLength
381 if ((char *)srcKey
+ sizeof(HFSPlusExtentKey
) + recordSize
> nextRecord
) {
382 printf("hfs_swap_HFSPlusBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
383 return fsBTInvalidNodeErr
;
386 if (direction
== kSwapBTNodeBigToHost
)
387 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
388 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
389 printf("hfs_swap_HFSPlusBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
390 return fsBTInvalidNodeErr
;
392 srcRec
= (HFSPlusExtentDescriptor
*)((char *)srcKey
+ srcKey
->keyLength
+ sizeof(srcKey
->keyLength
));
393 if (direction
== kSwapBTNodeHostToBig
)
394 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
396 /* Don't swap srcKey->forkType; it's only one byte */
397 /* Don't swap srcKey->pad */
399 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
400 srcKey
->startBlock
= SWAP_BE32 (srcKey
->startBlock
);
402 if (srcDesc
->kind
== kBTIndexNode
) {
403 /* For index nodes, the record data is just a child node number. */
404 *((UInt32
*)srcRec
) = SWAP_BE32 (*((UInt32
*)srcRec
));
406 /* Swap the extent data */
407 for (j
= 0; j
< kHFSPlusExtentDensity
; j
++) {
408 srcRec
[j
].startBlock
= SWAP_BE32 (srcRec
[j
].startBlock
);
409 srcRec
[j
].blockCount
= SWAP_BE32 (srcRec
[j
].blockCount
);
414 } else if (fileID
== kHFSCatalogFileID
) {
415 HFSPlusCatalogKey
*srcKey
;
419 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
420 /* Point to the start of the record we're currently checking. */
421 srcKey
= (HFSPlusCatalogKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
424 * Point to start of next (larger offset) record. We'll use this
425 * to be sure the current record doesn't overflow into the next
428 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
431 * Make sure we can safely dereference the keyLength and parentID fields. */
432 if ((char *)srcKey
+ offsetof(HFSPlusCatalogKey
, nodeName
.unicode
[0]) > nextRecord
) {
433 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
434 return fsBTInvalidNodeErr
;
438 * Swap and sanity check the key length
440 if (direction
== kSwapBTNodeBigToHost
)
441 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
442 keyLength
= srcKey
->keyLength
; /* Put it in a local (native order) because we use it several times */
443 if (direction
== kSwapBTNodeHostToBig
)
444 srcKey
->keyLength
= SWAP_BE16 (keyLength
);
446 /* Sanity check the key length */
447 if (keyLength
< kHFSPlusCatalogKeyMinimumLength
|| keyLength
> kHFSPlusCatalogKeyMaximumLength
) {
448 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
449 return fsBTInvalidNodeErr
;
453 * Make sure that we can safely dereference the record's type field or
454 * an index node's child node number.
456 srcPtr
= (SInt16
*)((char *)srcKey
+ keyLength
+ sizeof(srcKey
->keyLength
));
457 if ((char *)srcPtr
+ sizeof(UInt32
) > nextRecord
) {
458 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
459 return fsBTInvalidNodeErr
;
462 srcKey
->parentID
= SWAP_BE32 (srcKey
->parentID
);
465 * Swap and sanity check the key's node name
467 if (direction
== kSwapBTNodeBigToHost
)
468 srcKey
->nodeName
.length
= SWAP_BE16 (srcKey
->nodeName
.length
);
469 /* Make sure name length is consistent with key length */
470 if (keyLength
< sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
471 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0])) {
472 printf("hfs_swap_HFSPlusBTInternalNode: catalog record #%d keyLength=%d expected=%d\n",
473 srcDesc
->numRecords
-i
, keyLength
, sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
474 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0]));
475 return fsBTInvalidNodeErr
;
477 for (j
= 0; j
< srcKey
->nodeName
.length
; j
++) {
478 srcKey
->nodeName
.unicode
[j
] = SWAP_BE16 (srcKey
->nodeName
.unicode
[j
]);
480 if (direction
== kSwapBTNodeHostToBig
)
481 srcKey
->nodeName
.length
= SWAP_BE16 (srcKey
->nodeName
.length
);
484 * For index nodes, the record data is just the child's node number.
485 * Skip over swapping the various types of catalog record.
487 if (srcDesc
->kind
== kBTIndexNode
) {
488 *((UInt32
*)srcPtr
) = SWAP_BE32 (*((UInt32
*)srcPtr
));
492 /* Make sure the recordType is in native order before using it. */
493 if (direction
== kSwapBTNodeBigToHost
)
494 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
496 if (srcPtr
[0] == kHFSPlusFolderRecord
) {
497 HFSPlusCatalogFolder
*srcRec
= (HFSPlusCatalogFolder
*)srcPtr
;
498 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
499 printf("hfs_swap_HFSPlusBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
500 return fsBTInvalidNodeErr
;
503 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
504 srcRec
->valence
= SWAP_BE32 (srcRec
->valence
);
505 srcRec
->folderID
= SWAP_BE32 (srcRec
->folderID
);
506 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
507 srcRec
->contentModDate
= SWAP_BE32 (srcRec
->contentModDate
);
508 srcRec
->attributeModDate
= SWAP_BE32 (srcRec
->attributeModDate
);
509 srcRec
->accessDate
= SWAP_BE32 (srcRec
->accessDate
);
510 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
512 srcRec
->bsdInfo
.ownerID
= SWAP_BE32 (srcRec
->bsdInfo
.ownerID
);
513 srcRec
->bsdInfo
.groupID
= SWAP_BE32 (srcRec
->bsdInfo
.groupID
);
515 /* Don't swap srcRec->bsdInfo.adminFlags; it's only one byte */
516 /* Don't swap srcRec->bsdInfo.ownerFlags; it's only one byte */
518 srcRec
->bsdInfo
.fileMode
= SWAP_BE16 (srcRec
->bsdInfo
.fileMode
);
519 srcRec
->bsdInfo
.special
.iNodeNum
= SWAP_BE32 (srcRec
->bsdInfo
.special
.iNodeNum
);
521 srcRec
->textEncoding
= SWAP_BE32 (srcRec
->textEncoding
);
523 /* Don't swap srcRec->userInfo */
524 /* Don't swap srcRec->finderInfo */
525 /* Don't swap srcRec->reserved */
527 } else if (srcPtr
[0] == kHFSPlusFileRecord
) {
528 HFSPlusCatalogFile
*srcRec
= (HFSPlusCatalogFile
*)srcPtr
;
529 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
530 printf("hfs_swap_HFSPlusBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
531 return fsBTInvalidNodeErr
;
534 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
536 srcRec
->fileID
= SWAP_BE32 (srcRec
->fileID
);
538 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
539 srcRec
->contentModDate
= SWAP_BE32 (srcRec
->contentModDate
);
540 srcRec
->attributeModDate
= SWAP_BE32 (srcRec
->attributeModDate
);
541 srcRec
->accessDate
= SWAP_BE32 (srcRec
->accessDate
);
542 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
544 srcRec
->bsdInfo
.ownerID
= SWAP_BE32 (srcRec
->bsdInfo
.ownerID
);
545 srcRec
->bsdInfo
.groupID
= SWAP_BE32 (srcRec
->bsdInfo
.groupID
);
547 /* Don't swap srcRec->bsdInfo.adminFlags; it's only one byte */
548 /* Don't swap srcRec->bsdInfo.ownerFlags; it's only one byte */
550 srcRec
->bsdInfo
.fileMode
= SWAP_BE16 (srcRec
->bsdInfo
.fileMode
);
551 srcRec
->bsdInfo
.special
.iNodeNum
= SWAP_BE32 (srcRec
->bsdInfo
.special
.iNodeNum
);
553 srcRec
->textEncoding
= SWAP_BE32 (srcRec
->textEncoding
);
555 /* Don't swap srcRec->reserved1 */
556 /* Don't swap srcRec->userInfo */
557 /* Don't swap srcRec->finderInfo */
558 /* Don't swap srcRec->reserved2 */
560 hfs_swap_HFSPlusForkData (&srcRec
->dataFork
);
561 hfs_swap_HFSPlusForkData (&srcRec
->resourceFork
);
563 } else if ((srcPtr
[0] == kHFSPlusFolderThreadRecord
) ||
564 (srcPtr
[0] == kHFSPlusFileThreadRecord
)) {
567 * Make sure there is room for parentID and name length.
569 HFSPlusCatalogThread
*srcRec
= (HFSPlusCatalogThread
*)srcPtr
;
570 if ((char *) &srcRec
->nodeName
.unicode
[0] > nextRecord
) {
571 printf("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
572 return fsBTInvalidNodeErr
;
575 /* Don't swap srcRec->reserved */
577 srcRec
->parentID
= SWAP_BE32 (srcRec
->parentID
);
579 if (direction
== kSwapBTNodeBigToHost
)
580 srcRec
->nodeName
.length
= SWAP_BE16 (srcRec
->nodeName
.length
);
583 * Make sure there is room for the name in the buffer.
584 * Then swap the characters of the name itself.
586 if ((char *) &srcRec
->nodeName
.unicode
[srcRec
->nodeName
.length
] > nextRecord
) {
587 printf("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
588 return fsBTInvalidNodeErr
;
590 for (j
= 0; j
< srcRec
->nodeName
.length
; j
++) {
591 srcRec
->nodeName
.unicode
[j
] = SWAP_BE16 (srcRec
->nodeName
.unicode
[j
]);
594 if (direction
== kSwapBTNodeHostToBig
)
595 srcRec
->nodeName
.length
= SWAP_BE16 (srcRec
->nodeName
.length
);
598 printf("hfs_swap_HFSPlusBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
599 return fsBTInvalidNodeErr
;
602 /* We can swap the record type now that we're done using it. */
603 if (direction
== kSwapBTNodeHostToBig
)
604 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
607 } else if (fileID
== kHFSAttributesFileID
) {
608 HFSPlusAttrKey
*srcKey
;
609 HFSPlusAttrRecord
*srcRec
;
611 u_int32_t attrSize
= 0;
613 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
614 /* Point to the start of the record we're currently checking. */
615 srcKey
= (HFSPlusAttrKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
618 * Point to start of next (larger offset) record. We'll use this
619 * to be sure the current record doesn't overflow into the next
622 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
624 /* Make sure there is room in the buffer for a minimal key */
625 if ((char *) &srcKey
->attrName
[1] > nextRecord
) {
626 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
627 return fsBTInvalidNodeErr
;
630 /* Swap the key length field */
631 if (direction
== kSwapBTNodeBigToHost
)
632 srcKey
->keyLength
= SWAP_BE16(srcKey
->keyLength
);
633 keyLength
= srcKey
->keyLength
; /* Keep a copy in native order */
634 if (direction
== kSwapBTNodeHostToBig
)
635 srcKey
->keyLength
= SWAP_BE16(srcKey
->keyLength
);
638 * Make sure that we can safely dereference the record's type field or
639 * an index node's child node number.
641 srcRec
= (HFSPlusAttrRecord
*)((char *)srcKey
+ keyLength
+ sizeof(srcKey
->keyLength
));
642 if ((char *)srcRec
+ sizeof(u_int32_t
) > nextRecord
) {
643 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d too big (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
644 return fsBTInvalidNodeErr
;
647 srcKey
->fileID
= SWAP_BE32(srcKey
->fileID
);
648 srcKey
->startBlock
= SWAP_BE32(srcKey
->startBlock
);
651 * Swap and check the attribute name
653 if (direction
== kSwapBTNodeBigToHost
)
654 srcKey
->attrNameLen
= SWAP_BE16(srcKey
->attrNameLen
);
655 /* Sanity check the attribute name length */
656 if (srcKey
->attrNameLen
> kHFSMaxAttrNameLen
|| keyLength
< (kHFSPlusAttrKeyMinimumLength
+ sizeof(u_int16_t
)*srcKey
->attrNameLen
)) {
657 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d keyLength=%d attrNameLen=%d\n", srcDesc
->numRecords
-i
-1, keyLength
, srcKey
->attrNameLen
);
658 return fsBTInvalidNodeErr
;
660 for (j
= 0; j
< srcKey
->attrNameLen
; j
++)
661 srcKey
->attrName
[j
] = SWAP_BE16(srcKey
->attrName
[j
]);
662 if (direction
== kSwapBTNodeHostToBig
)
663 srcKey
->attrNameLen
= SWAP_BE16(srcKey
->attrNameLen
);
666 * For index nodes, the record data is just the child's node number.
667 * Skip over swapping the various types of attribute record.
669 if (srcDesc
->kind
== kBTIndexNode
) {
670 *((UInt32
*)srcRec
) = SWAP_BE32 (*((UInt32
*)srcRec
));
674 /* Swap the record data */
675 if (direction
== kSwapBTNodeBigToHost
)
676 srcRec
->recordType
= SWAP_BE32(srcRec
->recordType
);
677 switch (srcRec
->recordType
) {
678 case kHFSPlusAttrInlineData
:
679 /* Is there room for the inline data header? */
680 if ((char *) &srcRec
->attrData
.attrData
[0] > nextRecord
) {
681 printf("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big\n", srcDesc
->numRecords
-i
-1);
682 return fsBTInvalidNodeErr
;
685 /* We're not swapping the reserved fields */
687 /* Swap the attribute size */
688 if (direction
== kSwapBTNodeHostToBig
)
689 attrSize
= srcRec
->attrData
.attrSize
;
690 srcRec
->attrData
.attrSize
= SWAP_BE32(srcRec
->attrData
.attrSize
);
691 if (direction
== kSwapBTNodeBigToHost
)
692 attrSize
= srcRec
->attrData
.attrSize
;
694 /* Is there room for the inline attribute data? */
695 if ((char *) &srcRec
->attrData
.attrData
[attrSize
] > nextRecord
) {
696 printf("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big (attrSize=%u)\n", srcDesc
->numRecords
-i
-1, attrSize
);
697 return fsBTInvalidNodeErr
;
700 /* Not swapping the attribute data itself */
703 case kHFSPlusAttrForkData
:
704 /* Is there room for the fork data record? */
705 if ((char *)srcRec
+ sizeof(HFSPlusAttrForkData
) > nextRecord
) {
706 printf("hfs_swap_HFSPlusBTInternalNode: attr fork data #%d too big\n", srcDesc
->numRecords
-i
-1);
707 return fsBTInvalidNodeErr
;
710 /* We're not swapping the reserved field */
712 hfs_swap_HFSPlusForkData(&srcRec
->forkData
.theFork
);
715 case kHFSPlusAttrExtents
:
716 /* Is there room for an extent record? */
717 if ((char *)srcRec
+ sizeof(HFSPlusAttrExtents
) > nextRecord
) {
718 printf("hfs_swap_HFSPlusBTInternalNode: attr extents #%d too big\n", srcDesc
->numRecords
-i
-1);
719 return fsBTInvalidNodeErr
;
722 /* We're not swapping the reserved field */
724 for (j
= 0; j
< kHFSPlusExtentDensity
; j
++) {
725 srcRec
->overflowExtents
.extents
[j
].startBlock
=
726 SWAP_BE32(srcRec
->overflowExtents
.extents
[j
].startBlock
);
727 srcRec
->overflowExtents
.extents
[j
].blockCount
=
728 SWAP_BE32(srcRec
->overflowExtents
.extents
[j
].blockCount
);
732 if (direction
== kSwapBTNodeHostToBig
)
733 srcRec
->recordType
= SWAP_BE32(srcRec
->recordType
);
735 } else if (fileID
> kHFSFirstUserCatalogNodeID
) {
736 /* The only B-tree with a non-system CNID that we use is the hotfile B-tree */
740 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
741 /* Point to the start of the record we're currently checking. */
742 srcKey
= (HotFileKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
745 * Point to start of next (larger offset) record. We'll use this
746 * to be sure the current record doesn't overflow into the next
749 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
751 /* Make sure there is room for the key (HotFileKey) and data (UInt32) */
752 if ((char *)srcKey
+ sizeof(HotFileKey
) + sizeof(UInt32
) > nextRecord
) {
753 printf("hfs_swap_HFSPlusBTInternalNode: hotfile #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
754 return fsBTInvalidNodeErr
;
757 /* Swap and sanity check the key length field */
758 if (direction
== kSwapBTNodeBigToHost
)
759 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
760 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
761 printf("hfs_swap_HFSPlusBTInternalNode: hotfile #%d incorrect keyLength %d\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
762 return fsBTInvalidNodeErr
;
764 srcRec
= (u_int32_t
*)((char *)srcKey
+ srcKey
->keyLength
+ sizeof(srcKey
->keyLength
));
765 if (direction
== kSwapBTNodeHostToBig
)
766 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
768 /* Don't swap srcKey->forkType */
769 /* Don't swap srcKey->pad */
771 srcKey
->temperature
= SWAP_BE32 (srcKey
->temperature
);
772 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
774 *((UInt32
*)srcRec
) = SWAP_BE32 (*((UInt32
*)srcRec
));
777 panic ("hfs_swap_HFSPlusBTInternalNode: fileID %u is not a system B-tree\n", fileID
);
785 hfs_swap_HFSBTInternalNode (
786 BlockDescriptor
*src
,
787 HFSCatalogNodeID fileID
,
788 enum HFSBTSwapDirection direction
791 BTNodeDescriptor
*srcDesc
= src
->buffer
;
792 UInt16
*srcOffs
= (UInt16
*)((char *)src
->buffer
+ (src
->blockSize
- (srcDesc
->numRecords
* sizeof (UInt16
))));
793 char *nextRecord
; /* Points to start of record following current one */
798 if (fileID
== kHFSExtentsFileID
) {
799 HFSExtentKey
*srcKey
;
800 HFSExtentDescriptor
*srcRec
;
801 size_t recordSize
; /* Size of the data part of the record, or node number for index nodes */
803 if (srcDesc
->kind
== kBTIndexNode
)
804 recordSize
= sizeof(UInt32
);
806 recordSize
= sizeof(HFSExtentDescriptor
);
808 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
809 /* Point to the start of the record we're currently checking. */
810 srcKey
= (HFSExtentKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
813 * Point to start of next (larger offset) record. We'll use this
814 * to be sure the current record doesn't overflow into the next
817 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
820 * Make sure the key and data are within the buffer. Since both key
821 * and data are fixed size, this is relatively easy. Note that this
822 * relies on the keyLength being a constant; we verify the keyLength
825 if ((char *)srcKey
+ sizeof(HFSExtentKey
) + recordSize
> nextRecord
) {
826 printf("hfs_swap_HFSBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
827 return fsBTInvalidNodeErr
;
830 /* Don't swap srcKey->keyLength (it's only one byte), but do sanity check it */
831 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
832 printf("hfs_swap_HFSBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
833 return fsBTInvalidNodeErr
;
836 /* Don't swap srcKey->forkType; it's only one byte */
838 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
839 srcKey
->startBlock
= SWAP_BE16 (srcKey
->startBlock
);
841 /* Point to record data (round up to even byte boundary) */
842 srcRec
= (HFSExtentDescriptor
*)((char *)srcKey
+ ((srcKey
->keyLength
+ 2) & ~1));
844 if (srcDesc
->kind
== kBTIndexNode
) {
845 /* For index nodes, the record data is just a child node number. */
846 *((UInt32
*)srcRec
) = SWAP_BE32 (*((UInt32
*)srcRec
));
848 /* Swap the extent data */
849 for (j
= 0; j
< kHFSExtentDensity
; j
++) {
850 srcRec
[j
].startBlock
= SWAP_BE16 (srcRec
[j
].startBlock
);
851 srcRec
[j
].blockCount
= SWAP_BE16 (srcRec
[j
].blockCount
);
856 } else if (fileID
== kHFSCatalogFileID
) {
857 HFSCatalogKey
*srcKey
;
859 unsigned expectedKeyLength
;
861 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
862 /* Point to the start of the record we're currently checking. */
863 srcKey
= (HFSCatalogKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
866 * Point to start of next (larger offset) record. We'll use this
867 * to be sure the current record doesn't overflow into the next
870 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
873 * Make sure we can safely dereference the keyLength and parentID fields.
874 * The value 8 below is 1 bytes for keyLength + 1 byte reserved + 4 bytes
875 * for parentID + 1 byte for nodeName's length + 1 byte to round up the
876 * record start to an even offset, which forms a minimal key.
878 if ((char *)srcKey
+ 8 > nextRecord
) {
879 printf("hfs_swap_HFSBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
880 return fsBTInvalidNodeErr
;
883 /* Don't swap srcKey->keyLength (it's only one byte), but do sanity check it */
884 if (srcKey
->keyLength
< kHFSCatalogKeyMinimumLength
|| srcKey
->keyLength
> kHFSCatalogKeyMaximumLength
) {
885 printf("hfs_swap_HFSBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
886 return fsBTInvalidNodeErr
;
889 /* Don't swap srcKey->reserved */
891 srcKey
->parentID
= SWAP_BE32 (srcKey
->parentID
);
893 /* Don't swap srcKey->nodeName */
895 /* Make sure the keyLength is big enough for the key's content */
896 if (srcDesc
->kind
== kBTIndexNode
)
897 expectedKeyLength
= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
);
899 expectedKeyLength
= srcKey
->nodeName
[0] + kHFSCatalogKeyMinimumLength
;
900 if (srcKey
->keyLength
< expectedKeyLength
) {
901 printf("hfs_swap_HFSBTInternalNode: catalog record #%d keyLength=%u expected=%u\n",
902 srcDesc
->numRecords
-i
, srcKey
->keyLength
, expectedKeyLength
);
903 return fsBTInvalidNodeErr
;
906 /* Point to record data (round up to even byte boundary) */
907 srcPtr
= (SInt16
*)((char *)srcKey
+ ((srcKey
->keyLength
+ 2) & ~1));
910 * Make sure that we can safely dereference the record's type field or
911 * and index node's child node number.
913 if ((char *)srcPtr
+ sizeof(UInt32
) > nextRecord
) {
914 printf("hfs_swap_HFSBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
915 return fsBTInvalidNodeErr
;
919 * For index nodes, the record data is just the child's node number.
920 * Skip over swapping the various types of catalog record.
922 if (srcDesc
->kind
== kBTIndexNode
) {
923 *((UInt32
*)srcPtr
) = SWAP_BE32 (*((UInt32
*)srcPtr
));
927 /* Make sure the recordType is in native order before using it. */
928 if (direction
== kSwapBTNodeBigToHost
)
929 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
931 if (srcPtr
[0] == kHFSFolderRecord
) {
932 HFSCatalogFolder
*srcRec
= (HFSCatalogFolder
*)srcPtr
;
933 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
934 printf("hfs_swap_HFSBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
935 return fsBTInvalidNodeErr
;
938 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
939 srcRec
->valence
= SWAP_BE16 (srcRec
->valence
);
941 srcRec
->folderID
= SWAP_BE32 (srcRec
->folderID
);
942 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
943 srcRec
->modifyDate
= SWAP_BE32 (srcRec
->modifyDate
);
944 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
946 /* Don't swap srcRec->userInfo */
947 /* Don't swap srcRec->finderInfo */
948 /* Don't swap resserved array */
950 } else if (srcPtr
[0] == kHFSFileRecord
) {
951 HFSCatalogFile
*srcRec
= (HFSCatalogFile
*)srcPtr
;
952 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
953 printf("hfs_swap_HFSBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
954 return fsBTInvalidNodeErr
;
957 srcRec
->flags
= srcRec
->flags
;
958 srcRec
->fileType
= srcRec
->fileType
;
960 /* Don't swap srcRec->userInfo */
962 srcRec
->fileID
= SWAP_BE32 (srcRec
->fileID
);
964 srcRec
->dataStartBlock
= SWAP_BE16 (srcRec
->dataStartBlock
);
965 srcRec
->dataLogicalSize
= SWAP_BE32 (srcRec
->dataLogicalSize
);
966 srcRec
->dataPhysicalSize
= SWAP_BE32 (srcRec
->dataPhysicalSize
);
968 srcRec
->rsrcStartBlock
= SWAP_BE16 (srcRec
->rsrcStartBlock
);
969 srcRec
->rsrcLogicalSize
= SWAP_BE32 (srcRec
->rsrcLogicalSize
);
970 srcRec
->rsrcPhysicalSize
= SWAP_BE32 (srcRec
->rsrcPhysicalSize
);
972 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
973 srcRec
->modifyDate
= SWAP_BE32 (srcRec
->modifyDate
);
974 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
976 /* Don't swap srcRec->finderInfo */
978 srcRec
->clumpSize
= SWAP_BE16 (srcRec
->clumpSize
);
980 /* Swap the two sets of extents as an array of six (three each) UInt16 */
981 for (j
= 0; j
< kHFSExtentDensity
* 2; j
++) {
982 srcRec
->dataExtents
[j
].startBlock
= SWAP_BE16 (srcRec
->dataExtents
[j
].startBlock
);
983 srcRec
->dataExtents
[j
].blockCount
= SWAP_BE16 (srcRec
->dataExtents
[j
].blockCount
);
986 /* Don't swap srcRec->reserved */
988 } else if ((srcPtr
[0] == kHFSFolderThreadRecord
) ||
989 (srcPtr
[0] == kHFSFileThreadRecord
)) {
990 HFSCatalogThread
*srcRec
= (HFSCatalogThread
*)srcPtr
;
992 /* Make sure there is room for parentID and name length */
993 if ((char *) &srcRec
->nodeName
[1] > nextRecord
) {
994 printf("hfs_swap_HFSBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
995 return fsBTInvalidNodeErr
;
998 /* Don't swap srcRec->reserved array */
1000 srcRec
->parentID
= SWAP_BE32 (srcRec
->parentID
);
1002 /* Don't swap srcRec->nodeName */
1004 /* Make sure there is room for the name in the buffer */
1005 if ((char *) &srcRec
->nodeName
[srcRec
->nodeName
[0]] > nextRecord
) {
1006 printf("hfs_swap_HFSBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
1007 return fsBTInvalidNodeErr
;
1010 printf("hfs_swap_HFSBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
1011 return fsBTInvalidNodeErr
;
1014 /* We can swap the record type now that we're done using it */
1015 if (direction
== kSwapBTNodeHostToBig
)
1016 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
1020 panic ("hfs_swap_HFSBTInternalNode: fileID %u is not a system B-tree\n", fileID
);