2 * Copyright (c) 2002, 2004, 2005, 2007-2008 Apple 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
32 #include <sys/types.h>
35 #include <libkern/OSByteOrder.h>
36 #include <hfs/hfs_format.h>
38 #include "Scavenger.h"
39 #include "BTreePrivate.h"
40 #include "hfs_endian.h"
41 #include "../fsck_hfs.h"
46 * Internal swapping routines
48 * These routines handle swapping the records of leaf and index nodes. The
49 * layout of the keys and records varies depending on the kind of B-tree
50 * (determined by fileID).
52 * The direction parameter must be kSwapBTNodeBigToHost or kSwapBTNodeHostToBig.
53 * The kSwapBTNodeHeaderRecordOnly "direction" is not valid for these routines.
55 static int hfs_swap_HFSPlusBTInternalNode (BlockDescriptor
*src
, SFCB
*fcb
, enum HFSBTSwapDirection direction
);
56 static int hfs_swap_HFSBTInternalNode (BlockDescriptor
*src
, SFCB
*fcb
, enum HFSBTSwapDirection direction
);
59 * hfs_swap_HFSPlusForkData
62 hfs_swap_HFSPlusForkData (
68 src
->logicalSize
= SWAP_BE64 (src
->logicalSize
);
70 src
->clumpSize
= SWAP_BE32 (src
->clumpSize
);
71 src
->totalBlocks
= SWAP_BE32 (src
->totalBlocks
);
73 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
74 src
->extents
[i
].startBlock
= SWAP_BE32 (src
->extents
[i
].startBlock
);
75 src
->extents
[i
].blockCount
= SWAP_BE32 (src
->extents
[i
].blockCount
);
80 * hfs_swap_HFSMasterDirectoryBlock
82 * Specially modified to swap parts of the finder info
85 hfs_swap_HFSMasterDirectoryBlock (
89 HFSMasterDirectoryBlock
*src
= (HFSMasterDirectoryBlock
*)buf
;
91 src
->drSigWord
= SWAP_BE16 (src
->drSigWord
);
92 src
->drCrDate
= SWAP_BE32 (src
->drCrDate
);
93 src
->drLsMod
= SWAP_BE32 (src
->drLsMod
);
94 src
->drAtrb
= SWAP_BE16 (src
->drAtrb
);
95 src
->drNmFls
= SWAP_BE16 (src
->drNmFls
);
96 src
->drVBMSt
= SWAP_BE16 (src
->drVBMSt
);
97 src
->drAllocPtr
= SWAP_BE16 (src
->drAllocPtr
);
98 src
->drNmAlBlks
= SWAP_BE16 (src
->drNmAlBlks
);
99 src
->drAlBlkSiz
= SWAP_BE32 (src
->drAlBlkSiz
);
100 src
->drClpSiz
= SWAP_BE32 (src
->drClpSiz
);
101 src
->drAlBlSt
= SWAP_BE16 (src
->drAlBlSt
);
102 src
->drNxtCNID
= SWAP_BE32 (src
->drNxtCNID
);
103 src
->drFreeBks
= SWAP_BE16 (src
->drFreeBks
);
105 /* Don't swap drVN */
107 src
->drVolBkUp
= SWAP_BE32 (src
->drVolBkUp
);
108 src
->drVSeqNum
= SWAP_BE16 (src
->drVSeqNum
);
109 src
->drWrCnt
= SWAP_BE32 (src
->drWrCnt
);
110 src
->drXTClpSiz
= SWAP_BE32 (src
->drXTClpSiz
);
111 src
->drCTClpSiz
= SWAP_BE32 (src
->drCTClpSiz
);
112 src
->drNmRtDirs
= SWAP_BE16 (src
->drNmRtDirs
);
113 src
->drFilCnt
= SWAP_BE32 (src
->drFilCnt
);
114 src
->drDirCnt
= SWAP_BE32 (src
->drDirCnt
);
116 /* Swap just the 'blessed folder' in drFndrInfo */
117 src
->drFndrInfo
[0] = SWAP_BE32 (src
->drFndrInfo
[0]);
119 src
->drEmbedSigWord
= SWAP_BE16 (src
->drEmbedSigWord
);
120 src
->drEmbedExtent
.startBlock
= SWAP_BE16 (src
->drEmbedExtent
.startBlock
);
121 src
->drEmbedExtent
.blockCount
= SWAP_BE16 (src
->drEmbedExtent
.blockCount
);
123 src
->drXTFlSize
= SWAP_BE32 (src
->drXTFlSize
);
124 src
->drXTExtRec
[0].startBlock
= SWAP_BE16 (src
->drXTExtRec
[0].startBlock
);
125 src
->drXTExtRec
[0].blockCount
= SWAP_BE16 (src
->drXTExtRec
[0].blockCount
);
126 src
->drXTExtRec
[1].startBlock
= SWAP_BE16 (src
->drXTExtRec
[1].startBlock
);
127 src
->drXTExtRec
[1].blockCount
= SWAP_BE16 (src
->drXTExtRec
[1].blockCount
);
128 src
->drXTExtRec
[2].startBlock
= SWAP_BE16 (src
->drXTExtRec
[2].startBlock
);
129 src
->drXTExtRec
[2].blockCount
= SWAP_BE16 (src
->drXTExtRec
[2].blockCount
);
131 src
->drCTFlSize
= SWAP_BE32 (src
->drCTFlSize
);
132 src
->drCTExtRec
[0].startBlock
= SWAP_BE16 (src
->drCTExtRec
[0].startBlock
);
133 src
->drCTExtRec
[0].blockCount
= SWAP_BE16 (src
->drCTExtRec
[0].blockCount
);
134 src
->drCTExtRec
[1].startBlock
= SWAP_BE16 (src
->drCTExtRec
[1].startBlock
);
135 src
->drCTExtRec
[1].blockCount
= SWAP_BE16 (src
->drCTExtRec
[1].blockCount
);
136 src
->drCTExtRec
[2].startBlock
= SWAP_BE16 (src
->drCTExtRec
[2].startBlock
);
137 src
->drCTExtRec
[2].blockCount
= SWAP_BE16 (src
->drCTExtRec
[2].blockCount
);
141 * hfs_swap_HFSPlusVolumeHeader
144 hfs_swap_HFSPlusVolumeHeader (
148 HFSPlusVolumeHeader
*src
= (HFSPlusVolumeHeader
*)buf
;
150 src
->signature
= SWAP_BE16 (src
->signature
);
151 src
->version
= SWAP_BE16 (src
->version
);
152 src
->attributes
= SWAP_BE32 (src
->attributes
);
153 src
->lastMountedVersion
= SWAP_BE32 (src
->lastMountedVersion
);
155 /* Don't swap reserved */
157 src
->createDate
= SWAP_BE32 (src
->createDate
);
158 src
->modifyDate
= SWAP_BE32 (src
->modifyDate
);
159 src
->backupDate
= SWAP_BE32 (src
->backupDate
);
160 src
->checkedDate
= SWAP_BE32 (src
->checkedDate
);
161 src
->fileCount
= SWAP_BE32 (src
->fileCount
);
162 src
->folderCount
= SWAP_BE32 (src
->folderCount
);
163 src
->blockSize
= SWAP_BE32 (src
->blockSize
);
164 src
->totalBlocks
= SWAP_BE32 (src
->totalBlocks
);
165 src
->freeBlocks
= SWAP_BE32 (src
->freeBlocks
);
166 src
->nextAllocation
= SWAP_BE32 (src
->nextAllocation
);
167 src
->rsrcClumpSize
= SWAP_BE32 (src
->rsrcClumpSize
);
168 src
->dataClumpSize
= SWAP_BE32 (src
->dataClumpSize
);
169 src
->nextCatalogID
= SWAP_BE32 (src
->nextCatalogID
);
170 src
->writeCount
= SWAP_BE32 (src
->writeCount
);
171 src
->encodingsBitmap
= SWAP_BE64 (src
->encodingsBitmap
);
173 /* Don't swap finderInfo */
175 hfs_swap_HFSPlusForkData (&src
->allocationFile
);
176 hfs_swap_HFSPlusForkData (&src
->extentsFile
);
177 hfs_swap_HFSPlusForkData (&src
->catalogFile
);
178 hfs_swap_HFSPlusForkData (&src
->attributesFile
);
179 hfs_swap_HFSPlusForkData (&src
->startupFile
);
185 * NOTE: This operation is not naturally symmetric.
186 * We have to determine which way we're swapping things.
190 BlockDescriptor
*src
,
192 enum HFSBTSwapDirection direction
195 BTNodeDescriptor
*srcDesc
= src
->buffer
;
196 BTreeControlBlockPtr btcb
= fcb
->fcbBtree
;
197 UInt16
*srcOffs
= NULL
;
201 // WriteError(fcb->fcbVolume->vcbGPtr, E_BadNode, fcb->fcbFileID, src->blockNum);
204 if (direction
== kSwapBTNodeBigToHost
) {
205 if (debug
) plog ("BE -> Native Swap\n");
206 } else if (direction
== kSwapBTNodeHostToBig
) {
207 if (debug
) plog ("Native -> BE Swap\n");
208 } else if (direction
== kSwapBTNodeHeaderRecordOnly
) {
209 if (debug
) plog ("Not swapping descriptors\n");
211 if (debug
) plog ("hfs_swap_BTNode: This is impossible");
217 * If we are doing a swap from on-disk to in-memory, then swap the node
218 * descriptor and record offsets before we need to use them.
220 if (direction
== kSwapBTNodeBigToHost
) {
221 srcDesc
->fLink
= SWAP_BE32 (srcDesc
->fLink
);
222 srcDesc
->bLink
= SWAP_BE32 (srcDesc
->bLink
);
223 if (srcDesc
->fLink
>= btcb
->totalNodes
) {
224 if (debug
) plog("hfs_swap_BTNode: invalid forward link (0x%08X)\n", srcDesc
->fLink
);
226 if (srcDesc
->bLink
>= btcb
->totalNodes
) {
227 if (debug
) plog("hfs_swap_BTNode: invalid backward link (0x%08X)\n", srcDesc
->bLink
);
231 * Don't swap srcDesc->kind or srcDesc->height because they are only one byte.
232 * We don't check them here because the upper layers will check (and possibly
233 * repair) them more effectively.
235 if (srcDesc
->kind
< kBTLeafNode
|| srcDesc
->kind
> kBTMapNode
) {
236 if (debug
) plog("hfs_swap_BTNode: invalid node kind (%d)\n", srcDesc
->kind
);
238 if (srcDesc
->height
> btcb
->treeDepth
) {
239 if (debug
) plog("hfs_swap_BTNode: invalid node height (%d)\n", srcDesc
->height
);
242 /* Don't swap srcDesc->reserved */
244 srcDesc
->numRecords
= SWAP_BE16 (srcDesc
->numRecords
);
247 * Swap the node offsets (including the free space one!).
249 srcOffs
= (UInt16
*)((char *)src
->buffer
+ (src
->blockSize
- ((srcDesc
->numRecords
+ 1) * sizeof (UInt16
))));
252 * Sanity check that the record offsets are within the node itself.
254 if ((char *)srcOffs
> ((char *)src
->buffer
+ src
->blockSize
) ||
255 (char *)srcOffs
< ((char *)src
->buffer
+ sizeof(BTNodeDescriptor
))) {
256 if (debug
) plog("hfs_swap_BTNode: invalid record count (0x%04X)\n", srcDesc
->numRecords
);
257 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_NRecs
, fcb
->fcbFileID
, src
->blockNum
);
263 * Swap and sanity check each of the record offsets.
265 for (i
= 0; i
<= srcDesc
->numRecords
; i
++) {
266 srcOffs
[i
] = SWAP_BE16 (srcOffs
[i
]);
269 * Sanity check: must be even, and within the node itself.
271 * We may be called to swap an unused node, which contains all zeroes.
272 * This is why we allow the record offset to be zero.
274 if ((srcOffs
[i
] & 1) || (srcOffs
[i
] < sizeof(BTNodeDescriptor
) && srcOffs
[i
] != 0) || (srcOffs
[i
] >= src
->blockSize
)) {
275 if (debug
) plog("hfs_swap_BTNode: offset #%d invalid (0x%04X) (blockSize 0x%x numRecords %d)\n",
276 i
, srcOffs
[i
], src
->blockSize
, srcDesc
->numRecords
);
277 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
283 * Make sure the offsets are strictly increasing. Note that we're looping over
284 * them backwards, hence the order in the comparison.
286 if ((i
!= 0) && (srcOffs
[i
] >= srcOffs
[i
-1])) {
287 if (debug
) plog("hfs_swap_BTNode: offsets %d and %d out of order (0x%04X, 0x%04X)\n",
288 i
, i
-1, srcOffs
[i
], srcOffs
[i
-1]);
289 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
297 * Swap the records (ordered by frequency of access)
299 if ((srcDesc
->kind
== kBTIndexNode
) ||
300 (srcDesc
-> kind
== kBTLeafNode
)) {
302 if (fcb
->fcbVolume
->vcbSignature
== kHFSPlusSigWord
) {
303 error
= hfs_swap_HFSPlusBTInternalNode (src
, fcb
, direction
);
305 error
= hfs_swap_HFSBTInternalNode (src
, fcb
, direction
);
308 if (error
) goto fail
;
310 } else if (srcDesc
-> kind
== kBTMapNode
) {
311 /* Don't swap the bitmaps, they'll be done in the bitmap routines */
313 } else if (srcDesc
-> kind
== kBTHeaderNode
) {
314 /* The header's offset is hard-wired because we cannot trust the offset pointers. */
315 BTHeaderRec
*srcHead
= (BTHeaderRec
*)((char *)src
->buffer
+ sizeof(BTNodeDescriptor
));
317 srcHead
->treeDepth
= SWAP_BE16 (srcHead
->treeDepth
);
319 srcHead
->rootNode
= SWAP_BE32 (srcHead
->rootNode
);
320 srcHead
->leafRecords
= SWAP_BE32 (srcHead
->leafRecords
);
321 srcHead
->firstLeafNode
= SWAP_BE32 (srcHead
->firstLeafNode
);
322 srcHead
->lastLeafNode
= SWAP_BE32 (srcHead
->lastLeafNode
);
324 srcHead
->nodeSize
= SWAP_BE16 (srcHead
->nodeSize
);
325 srcHead
->maxKeyLength
= SWAP_BE16 (srcHead
->maxKeyLength
);
327 srcHead
->totalNodes
= SWAP_BE32 (srcHead
->totalNodes
);
328 srcHead
->freeNodes
= SWAP_BE32 (srcHead
->freeNodes
);
330 srcHead
->clumpSize
= SWAP_BE32 (srcHead
->clumpSize
);
331 srcHead
->attributes
= SWAP_BE32 (srcHead
->attributes
);
333 /* Don't swap srcHead->reserved1 */
334 /* Don't swap srcHead->btreeType; it's only one byte */
335 /* Don't swap srcHead->reserved2 */
336 /* Don't swap srcHead->reserved3 */
337 /* Don't swap bitmap */
339 /* Else: other node kinds will be caught by upper layers */
342 * If we are doing a swap from in-memory to on-disk, then swap the node
343 * descriptor and record offsets after we're done using them.
345 if (direction
== kSwapBTNodeHostToBig
) {
347 * Swap the forward and backward links.
349 if (srcDesc
->fLink
>= btcb
->totalNodes
) {
350 if (debug
) plog("hfs_UNswap_BTNode: invalid forward link (0x%08X)\n", srcDesc
->fLink
);
352 if (srcDesc
->bLink
>= btcb
->totalNodes
) {
353 if (debug
) plog("hfs_UNswap_BTNode: invalid backward link (0x%08X)\n", srcDesc
->bLink
);
355 srcDesc
->fLink
= SWAP_BE32 (srcDesc
->fLink
);
356 srcDesc
->bLink
= SWAP_BE32 (srcDesc
->bLink
);
359 * Don't swap srcDesc->kind or srcDesc->height because they are only one byte.
360 * We don't check them here because the upper layers will check (and possibly
361 * repair) them more effectively.
363 if (srcDesc
->kind
< kBTLeafNode
|| srcDesc
->kind
> kBTMapNode
) {
364 if (debug
) plog("hfs_UNswap_BTNode: invalid node kind (%d)\n", srcDesc
->kind
);
366 if (srcDesc
->height
> btcb
->treeDepth
) {
367 if (debug
) plog("hfs_UNswap_BTNode: invalid node height (%d)\n", srcDesc
->height
);
370 /* Don't swap srcDesc->reserved */
373 * Swap the node offsets (including the free space one!).
375 srcOffs
= (UInt16
*)((char *)src
->buffer
+ (src
->blockSize
- ((srcDesc
->numRecords
+ 1) * sizeof (UInt16
))));
378 * Sanity check that the record offsets are within the node itself.
380 if ((char *)srcOffs
> ((char *)src
->buffer
+ src
->blockSize
) ||
381 (char *)srcOffs
< ((char *)src
->buffer
+ sizeof(BTNodeDescriptor
))) {
382 if (debug
) plog("hfs_UNswap_BTNode: invalid record count (0x%04X)\n", srcDesc
->numRecords
);
383 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_NRecs
, fcb
->fcbFileID
, src
->blockNum
);
389 * Swap and sanity check each of the record offsets.
391 for (i
= 0; i
<= srcDesc
->numRecords
; i
++) {
393 * Sanity check: must be even, and within the node itself.
395 * We may be called to swap an unused node, which contains all zeroes.
396 * This is why we allow the record offset to be zero.
398 if ((srcOffs
[i
] & 1) || (srcOffs
[i
] < sizeof(BTNodeDescriptor
) && srcOffs
[i
] != 0) || (srcOffs
[i
] >= src
->blockSize
)) {
399 if (debug
) plog("hfs_UNswap_BTNode: offset #%d invalid (0x%04X) (blockSize 0x%x numRecords %d)\n",
400 i
, srcOffs
[i
], src
->blockSize
, srcDesc
->numRecords
);
401 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
407 * Make sure the offsets are strictly increasing. Note that we're looping over
408 * them backwards, hence the order in the comparison.
410 if ((i
< srcDesc
->numRecords
) && (srcOffs
[i
+1] >= srcOffs
[i
])) {
411 if (debug
) plog("hfs_UNswap_BTNode: offsets %d and %d out of order (0x%04X, 0x%04X)\n",
412 i
+1, i
, srcOffs
[i
+1], srcOffs
[i
]);
413 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
418 srcOffs
[i
] = SWAP_BE16 (srcOffs
[i
]);
421 srcDesc
->numRecords
= SWAP_BE16 (srcDesc
->numRecords
);
425 if (error
&& (cur_debug_level
& d_dump_node
))
427 plog("Node %u:\n", src
->blockNum
);
428 HexDump(src
->buffer
, src
->blockSize
, TRUE
);
434 hfs_swap_HFSPlusBTInternalNode (
435 BlockDescriptor
*src
,
437 enum HFSBTSwapDirection direction
440 HFSCatalogNodeID fileID
=fcb
->fcbFileID
;
441 BTNodeDescriptor
*srcDesc
= src
->buffer
;
442 UInt16
*srcOffs
= (UInt16
*)((char *)src
->buffer
+ (src
->blockSize
- (srcDesc
->numRecords
* sizeof (UInt16
))));
443 char *nextRecord
; /* Points to start of record following current one */
447 if (fileID
== kHFSExtentsFileID
) {
448 HFSPlusExtentKey
*srcKey
;
449 HFSPlusExtentDescriptor
*srcRec
;
450 size_t recordSize
; /* Size of the data part of the record, or node number for index nodes */
452 if (srcDesc
->kind
== kBTIndexNode
)
453 recordSize
= sizeof(UInt32
);
455 recordSize
= sizeof(HFSPlusExtentDescriptor
);
457 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
458 /* Point to the start of the record we're currently checking. */
459 srcKey
= (HFSPlusExtentKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
462 * Point to start of next (larger offset) record. We'll use this
463 * to be sure the current record doesn't overflow into the next
466 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
469 * Make sure the key and data are within the buffer. Since both key
470 * and data are fixed size, this is relatively easy. Note that this
471 * relies on the keyLength being a constant; we verify the keyLength
474 if ((char *)srcKey
+ sizeof(HFSPlusExtentKey
) + recordSize
> nextRecord
) {
475 if (debug
) plog("hfs_swap_HFSPlusBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
476 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
480 if (direction
== kSwapBTNodeBigToHost
)
481 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
482 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
483 if (debug
) plog("hfs_swap_HFSPlusBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
484 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_KeyLen
, fcb
->fcbFileID
, src
->blockNum
);
487 srcRec
= (HFSPlusExtentDescriptor
*)((char *)srcKey
+ srcKey
->keyLength
+ sizeof(srcKey
->keyLength
));
488 if (direction
== kSwapBTNodeHostToBig
)
489 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
491 /* Don't swap srcKey->forkType; it's only one byte */
492 /* Don't swap srcKey->pad */
494 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
495 srcKey
->startBlock
= SWAP_BE32 (srcKey
->startBlock
);
497 if (srcDesc
->kind
== kBTIndexNode
) {
498 /* For index nodes, the record data is just a child node number. */
499 *((UInt32
*)srcRec
) = SWAP_BE32 (*((UInt32
*)srcRec
));
501 /* Swap the extent data */
502 for (j
= 0; j
< kHFSPlusExtentDensity
; j
++) {
503 srcRec
[j
].startBlock
= SWAP_BE32 (srcRec
[j
].startBlock
);
504 srcRec
[j
].blockCount
= SWAP_BE32 (srcRec
[j
].blockCount
);
509 } else if (fileID
== kHFSCatalogFileID
|| fileID
== kHFSRepairCatalogFileID
) {
510 HFSPlusCatalogKey
*srcKey
;
514 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
515 /* Point to the start of the record we're currently checking. */
516 srcKey
= (HFSPlusCatalogKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
519 * Point to start of next (larger offset) record. We'll use this
520 * to be sure the current record doesn't overflow into the next
523 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
526 * Make sure we can safely dereference the keyLength and parentID fields. */
527 if ((char *)srcKey
+ offsetof(HFSPlusCatalogKey
, nodeName
.unicode
[0]) > nextRecord
) {
528 if (debug
) plog("hfs_swap_HFSPlusBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
529 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
534 * Swap and sanity check the key length
536 if (direction
== kSwapBTNodeBigToHost
)
537 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
538 keyLength
= srcKey
->keyLength
; /* Put it in a local (native order) because we use it several times */
539 if (direction
== kSwapBTNodeHostToBig
)
540 srcKey
->keyLength
= SWAP_BE16 (keyLength
);
542 /* Sanity check the key length */
543 if (keyLength
< kHFSPlusCatalogKeyMinimumLength
|| keyLength
> kHFSPlusCatalogKeyMaximumLength
) {
544 if (debug
) plog("hfs_swap_HFSPlusBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
545 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_KeyLen
, fcb
->fcbFileID
, src
->blockNum
);
550 * Make sure that we can safely dereference the record's type field or
551 * an index node's child node number.
553 srcPtr
= (SInt16
*)((char *)srcKey
+ keyLength
+ sizeof(srcKey
->keyLength
));
554 if ((char *)srcPtr
+ sizeof(UInt32
) > nextRecord
) {
555 if (debug
) plog("hfs_swap_HFSPlusBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
556 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_KeyLen
, fcb
->fcbFileID
, src
->blockNum
);
560 srcKey
->parentID
= SWAP_BE32 (srcKey
->parentID
);
563 * Swap and sanity check the key's node name
565 if (direction
== kSwapBTNodeBigToHost
)
566 srcKey
->nodeName
.length
= SWAP_BE16 (srcKey
->nodeName
.length
);
567 /* Make sure name length is consistent with key length */
568 if (keyLength
< sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
569 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0])) {
571 uintptr_t keyOffset
= (uintptr_t)srcKey
- (uintptr_t)src
->buffer
;
572 uintptr_t recordSize
= (uintptr_t)nextRecord
- (uintptr_t)srcKey
;
573 unsigned recordIndex
= srcDesc
->numRecords
- i
;
575 plog("hfs_swap_HFSPlusBTInternalNode: catalog record #%d (0-based, offset 0x%lX) keyLength=%d expected=%lu\n",
576 recordIndex
, keyOffset
, keyLength
, sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
577 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0]));
578 if (cur_debug_level
& d_dump_record
) {
579 plog("Record %u (offset 0x%04X):\n", recordIndex
, keyOffset
);
580 HexDump(srcKey
, recordSize
, FALSE
);
583 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_KeyLen
, fcb
->fcbFileID
, src
->blockNum
);
586 for (j
= 0; j
< srcKey
->nodeName
.length
; j
++) {
587 srcKey
->nodeName
.unicode
[j
] = SWAP_BE16 (srcKey
->nodeName
.unicode
[j
]);
589 if (direction
== kSwapBTNodeHostToBig
)
590 srcKey
->nodeName
.length
= SWAP_BE16 (srcKey
->nodeName
.length
);
593 * For index nodes, the record data is just the child's node number.
594 * Skip over swapping the various types of catalog record.
596 if (srcDesc
->kind
== kBTIndexNode
) {
597 *((UInt32
*)srcPtr
) = SWAP_BE32 (*((UInt32
*)srcPtr
));
601 /* Make sure the recordType is in native order before using it. */
602 if (direction
== kSwapBTNodeBigToHost
)
603 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
605 if (srcPtr
[0] == kHFSPlusFolderRecord
) {
606 HFSPlusCatalogFolder
*srcRec
= (HFSPlusCatalogFolder
*)srcPtr
;
607 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
608 if (debug
) plog("hfs_swap_HFSPlusBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
609 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
613 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
614 srcRec
->valence
= SWAP_BE32 (srcRec
->valence
);
615 srcRec
->folderID
= SWAP_BE32 (srcRec
->folderID
);
616 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
617 srcRec
->contentModDate
= SWAP_BE32 (srcRec
->contentModDate
);
618 srcRec
->attributeModDate
= SWAP_BE32 (srcRec
->attributeModDate
);
619 srcRec
->accessDate
= SWAP_BE32 (srcRec
->accessDate
);
620 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
622 srcRec
->bsdInfo
.ownerID
= SWAP_BE32 (srcRec
->bsdInfo
.ownerID
);
623 srcRec
->bsdInfo
.groupID
= SWAP_BE32 (srcRec
->bsdInfo
.groupID
);
625 /* Don't swap srcRec->bsdInfo.adminFlags; it's only one byte */
626 /* Don't swap srcRec->bsdInfo.ownerFlags; it's only one byte */
628 srcRec
->bsdInfo
.fileMode
= SWAP_BE16 (srcRec
->bsdInfo
.fileMode
);
629 srcRec
->bsdInfo
.special
.iNodeNum
= SWAP_BE32 (srcRec
->bsdInfo
.special
.iNodeNum
);
631 srcRec
->textEncoding
= SWAP_BE32 (srcRec
->textEncoding
);
633 /* The only field we use in srcRec->userInfo is frFlags (used in VLockedChk). */
634 srcRec
->userInfo
.frFlags
= SWAP_BE16 (srcRec
->userInfo
.frFlags
);
636 /* Don't swap srcRec->finderInfo */
637 srcRec
->folderCount
= SWAP_BE32 (srcRec
->folderCount
);
639 } else if (srcPtr
[0] == kHFSPlusFileRecord
) {
640 HFSPlusCatalogFile
*srcRec
= (HFSPlusCatalogFile
*)srcPtr
;
641 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
642 if (debug
) plog("hfs_swap_HFSPlusBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
643 return fsBTInvalidNodeErr
;
646 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
648 srcRec
->fileID
= SWAP_BE32 (srcRec
->fileID
);
650 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
651 srcRec
->contentModDate
= SWAP_BE32 (srcRec
->contentModDate
);
652 srcRec
->attributeModDate
= SWAP_BE32 (srcRec
->attributeModDate
);
653 srcRec
->accessDate
= SWAP_BE32 (srcRec
->accessDate
);
654 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
656 srcRec
->bsdInfo
.ownerID
= SWAP_BE32 (srcRec
->bsdInfo
.ownerID
);
657 srcRec
->bsdInfo
.groupID
= SWAP_BE32 (srcRec
->bsdInfo
.groupID
);
659 /* Don't swap srcRec->bsdInfo.adminFlags; it's only one byte */
660 /* Don't swap srcRec->bsdInfo.ownerFlags; it's only one byte */
662 srcRec
->bsdInfo
.fileMode
= SWAP_BE16 (srcRec
->bsdInfo
.fileMode
);
663 srcRec
->bsdInfo
.special
.iNodeNum
= SWAP_BE32 (srcRec
->bsdInfo
.special
.iNodeNum
);
665 srcRec
->textEncoding
= SWAP_BE32 (srcRec
->textEncoding
);
667 srcRec
->hl_firstLinkID
= SWAP_BE32 (srcRec
->hl_firstLinkID
);
669 srcRec
->userInfo
.fdType
= SWAP_BE32 (srcRec
->userInfo
.fdType
);
670 srcRec
->userInfo
.fdCreator
= SWAP_BE32 (srcRec
->userInfo
.fdCreator
);
671 srcRec
->userInfo
.fdFlags
= SWAP_BE16 (srcRec
->userInfo
.fdFlags
);
672 srcRec
->userInfo
.fdLocation
.v
= SWAP_BE16 (srcRec
->userInfo
.fdLocation
.v
);
673 srcRec
->userInfo
.fdLocation
.h
= SWAP_BE16 (srcRec
->userInfo
.fdLocation
.h
);
674 srcRec
->userInfo
.opaque
= SWAP_BE16 (srcRec
->userInfo
.opaque
);
676 /* Don't swap srcRec->finderInfo */
677 /* Don't swap srcRec->reserved2 */
679 hfs_swap_HFSPlusForkData (&srcRec
->dataFork
);
680 hfs_swap_HFSPlusForkData (&srcRec
->resourceFork
);
682 } else if ((srcPtr
[0] == kHFSPlusFolderThreadRecord
) ||
683 (srcPtr
[0] == kHFSPlusFileThreadRecord
)) {
686 * Make sure there is room for parentID and name length.
688 HFSPlusCatalogThread
*srcRec
= (HFSPlusCatalogThread
*)srcPtr
;
689 if ((char *) &srcRec
->nodeName
.unicode
[0] > nextRecord
) {
690 if (debug
) plog("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
691 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
695 /* Don't swap srcRec->reserved */
697 srcRec
->parentID
= SWAP_BE32 (srcRec
->parentID
);
699 if (direction
== kSwapBTNodeBigToHost
)
700 srcRec
->nodeName
.length
= SWAP_BE16 (srcRec
->nodeName
.length
);
703 * Make sure there is room for the name in the buffer.
704 * Then swap the characters of the name itself.
706 if ((char *) &srcRec
->nodeName
.unicode
[srcRec
->nodeName
.length
] > nextRecord
) {
707 if (debug
) plog("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
708 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
711 for (j
= 0; j
< srcRec
->nodeName
.length
; j
++) {
712 srcRec
->nodeName
.unicode
[j
] = SWAP_BE16 (srcRec
->nodeName
.unicode
[j
]);
715 if (direction
== kSwapBTNodeHostToBig
)
716 srcRec
->nodeName
.length
= SWAP_BE16 (srcRec
->nodeName
.length
);
719 if (debug
) plog("hfs_swap_HFSPlusBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
722 /* We can swap the record type now that we're done using it. */
723 if (direction
== kSwapBTNodeHostToBig
)
724 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
727 } else if (fileID
== kHFSAttributesFileID
) {
728 HFSPlusAttrKey
*srcKey
;
729 HFSPlusAttrRecord
*srcRec
;
731 u_int32_t attrSize
= 0;
733 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
734 /* Point to the start of the record we're currently checking. */
735 srcKey
= (HFSPlusAttrKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
738 * Point to start of next (larger offset) record. We'll use this
739 * to be sure the current record doesn't overflow into the next
742 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
744 /* Make sure there is room in the buffer for a minimal key */
745 if ((char *) &srcKey
->attrName
[1] > nextRecord
) {
746 if (debug
) plog("hfs_swap_HFSPlusBTInternalNode: attr key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
747 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
751 /* Swap the key length field */
752 if (direction
== kSwapBTNodeBigToHost
)
753 srcKey
->keyLength
= SWAP_BE16(srcKey
->keyLength
);
754 keyLength
= srcKey
->keyLength
; /* Keep a copy in native order */
755 if (direction
== kSwapBTNodeHostToBig
)
756 srcKey
->keyLength
= SWAP_BE16(srcKey
->keyLength
);
759 * Make sure that we can safely dereference the record's type field or
760 * an index node's child node number.
762 srcRec
= (HFSPlusAttrRecord
*)((char *)srcKey
+ keyLength
+ sizeof(srcKey
->keyLength
));
763 if ((char *)srcRec
+ sizeof(u_int32_t
) > nextRecord
) {
764 if (debug
) plog("hfs_swap_HFSPlusBTInternalNode: attr key #%d too big (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
765 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_KeyLen
, fcb
->fcbFileID
, src
->blockNum
);
769 srcKey
->fileID
= SWAP_BE32(srcKey
->fileID
);
770 srcKey
->startBlock
= SWAP_BE32(srcKey
->startBlock
);
773 * Swap and check the attribute name
775 if (direction
== kSwapBTNodeBigToHost
)
776 srcKey
->attrNameLen
= SWAP_BE16(srcKey
->attrNameLen
);
777 /* Sanity check the attribute name length */
778 if (srcKey
->attrNameLen
> kHFSMaxAttrNameLen
|| keyLength
< (kHFSPlusAttrKeyMinimumLength
+ sizeof(u_int16_t
)*srcKey
->attrNameLen
)) {
779 if (debug
) plog("hfs_swap_HFSPlusBTInternalNode: attr key #%d keyLength=%d attrNameLen=%d\n", srcDesc
->numRecords
-i
-1, keyLength
, srcKey
->attrNameLen
);
780 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
783 for (j
= 0; j
< srcKey
->attrNameLen
; j
++)
784 srcKey
->attrName
[j
] = SWAP_BE16(srcKey
->attrName
[j
]);
785 if (direction
== kSwapBTNodeHostToBig
)
786 srcKey
->attrNameLen
= SWAP_BE16(srcKey
->attrNameLen
);
789 * For index nodes, the record data is just the child's node number.
790 * Skip over swapping the various types of attribute record.
792 if (srcDesc
->kind
== kBTIndexNode
) {
793 *((UInt32
*)srcRec
) = SWAP_BE32 (*((UInt32
*)srcRec
));
797 /* Swap the record data */
798 if (direction
== kSwapBTNodeBigToHost
)
799 srcRec
->recordType
= SWAP_BE32(srcRec
->recordType
);
800 switch (srcRec
->recordType
) {
801 case kHFSPlusAttrInlineData
:
802 /* Is there room for the inline data header? */
803 if ((char *) &srcRec
->attrData
.attrData
[0] > nextRecord
) {
804 if (debug
) plog("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big\n", srcDesc
->numRecords
-i
-1);
805 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
809 /* We're not swapping the reserved fields */
811 /* Swap the attribute size */
812 if (direction
== kSwapBTNodeHostToBig
)
813 attrSize
= srcRec
->attrData
.attrSize
;
814 srcRec
->attrData
.attrSize
= SWAP_BE32(srcRec
->attrData
.attrSize
);
815 if (direction
== kSwapBTNodeBigToHost
)
816 attrSize
= srcRec
->attrData
.attrSize
;
818 /* Is there room for the inline attribute data? */
819 if ((char *) &srcRec
->attrData
.attrData
[attrSize
] > nextRecord
) {
820 if (debug
) plog("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big (attrSize=%u)\n", srcDesc
->numRecords
-i
-1, attrSize
);
821 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
825 /* Not swapping the attribute data itself */
828 case kHFSPlusAttrForkData
:
829 /* Is there room for the fork data record? */
830 if ((char *)srcRec
+ sizeof(HFSPlusAttrForkData
) > nextRecord
) {
831 if (debug
) plog("hfs_swap_HFSPlusBTInternalNode: attr fork data #%d too big\n", srcDesc
->numRecords
-i
-1);
832 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
836 /* We're not swapping the reserved field */
838 hfs_swap_HFSPlusForkData(&srcRec
->forkData
.theFork
);
841 case kHFSPlusAttrExtents
:
842 /* Is there room for an extent record? */
843 if ((char *)srcRec
+ sizeof(HFSPlusAttrExtents
) > nextRecord
) {
844 if (debug
) plog("hfs_swap_HFSPlusBTInternalNode: attr extents #%d too big\n", srcDesc
->numRecords
-i
-1);
845 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
849 /* We're not swapping the reserved field */
851 for (j
= 0; j
< kHFSPlusExtentDensity
; j
++) {
852 srcRec
->overflowExtents
.extents
[j
].startBlock
=
853 SWAP_BE32(srcRec
->overflowExtents
.extents
[j
].startBlock
);
854 srcRec
->overflowExtents
.extents
[j
].blockCount
=
855 SWAP_BE32(srcRec
->overflowExtents
.extents
[j
].blockCount
);
859 if (debug
) plog ("hfs_swap_BTNode: unrecognized attribute record type (%d)\n", srcRec
->recordType
);
861 if (direction
== kSwapBTNodeHostToBig
)
862 srcRec
->recordType
= SWAP_BE32(srcRec
->recordType
);
865 if (debug
) plog("hfs_swap_HFSPlusBTInternalNode: fileID %u is not a system B-tree\n", fileID
);
873 hfs_swap_HFSBTInternalNode (
874 BlockDescriptor
*src
,
876 enum HFSBTSwapDirection direction
879 HFSCatalogNodeID fileID
=fcb
->fcbFileID
;
880 BTNodeDescriptor
*srcDesc
= src
->buffer
;
881 UInt16
*srcOffs
= (UInt16
*)((char *)src
->buffer
+ (src
->blockSize
- (srcDesc
->numRecords
* sizeof (UInt16
))));
882 char *nextRecord
; /* Points to start of record following current one */
887 if (fileID
== kHFSExtentsFileID
) {
888 HFSExtentKey
*srcKey
;
889 HFSExtentDescriptor
*srcRec
;
890 size_t recordSize
; /* Size of the data part of the record, or node number for index nodes */
892 if (srcDesc
->kind
== kBTIndexNode
)
893 recordSize
= sizeof(UInt32
);
895 recordSize
= sizeof(HFSExtentDescriptor
);
897 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
898 /* Point to the start of the record we're currently checking. */
899 srcKey
= (HFSExtentKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
902 * Point to start of next (larger offset) record. We'll use this
903 * to be sure the current record doesn't overflow into the next
906 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
909 * Make sure the key and data are within the buffer. Since both key
910 * and data are fixed size, this is relatively easy. Note that this
911 * relies on the keyLength being a constant; we verify the keyLength
914 if ((char *)srcKey
+ sizeof(HFSExtentKey
) + recordSize
> nextRecord
) {
915 if (debug
) plog("hfs_swap_HFSBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
916 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
920 /* Don't swap srcKey->keyLength (it's only one byte), but do sanity check it */
921 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
922 if (debug
) plog("hfs_swap_HFSBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
925 /* Don't swap srcKey->forkType; it's only one byte */
927 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
928 srcKey
->startBlock
= SWAP_BE16 (srcKey
->startBlock
);
930 /* Point to record data (round up to even byte boundary) */
931 srcRec
= (HFSExtentDescriptor
*)((char *)srcKey
+ ((srcKey
->keyLength
+ 2) & ~1));
933 if (srcDesc
->kind
== kBTIndexNode
) {
934 /* For index nodes, the record data is just a child node number. */
935 *((UInt32
*)srcRec
) = SWAP_BE32 (*((UInt32
*)srcRec
));
937 /* Swap the extent data */
938 for (j
= 0; j
< kHFSExtentDensity
; j
++) {
939 srcRec
[j
].startBlock
= SWAP_BE16 (srcRec
[j
].startBlock
);
940 srcRec
[j
].blockCount
= SWAP_BE16 (srcRec
[j
].blockCount
);
945 } else if (fileID
== kHFSCatalogFileID
|| fileID
== kHFSRepairCatalogFileID
) {
946 HFSCatalogKey
*srcKey
;
948 size_t expectedKeyLength
;
950 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
951 /* Point to the start of the record we're currently checking. */
952 srcKey
= (HFSCatalogKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
955 * Point to start of next (larger offset) record. We'll use this
956 * to be sure the current record doesn't overflow into the next
959 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
962 * Make sure we can safely dereference the keyLength and parentID fields.
963 * The value 8 below is 1 bytes for keyLength + 1 byte reserved + 4 bytes
964 * for parentID + 1 byte for nodeName's length + 1 byte to round up the
965 * record start to an even offset, which forms a minimal key.
967 if ((char *)srcKey
+ 8 > nextRecord
) {
968 if (debug
) plog("hfs_swap_HFSBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
969 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
973 /* Don't swap srcKey->keyLength (it's only one byte), but do sanity check it */
974 if (srcKey
->keyLength
< kHFSCatalogKeyMinimumLength
|| srcKey
->keyLength
> kHFSCatalogKeyMaximumLength
) {
975 if (debug
) plog("hfs_swap_HFSBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
978 /* Don't swap srcKey->reserved */
980 srcKey
->parentID
= SWAP_BE32 (srcKey
->parentID
);
982 /* Don't swap srcKey->nodeName */
984 /* Make sure the keyLength is big enough for the key's content */
985 if (srcDesc
->kind
== kBTIndexNode
)
986 expectedKeyLength
= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
);
988 expectedKeyLength
= srcKey
->nodeName
[0] + kHFSCatalogKeyMinimumLength
;
989 if (srcKey
->keyLength
< expectedKeyLength
) {
990 if (debug
) plog("hfs_swap_HFSBTInternalNode: catalog record #%d keyLength=%u expected=%u\n",
991 srcDesc
->numRecords
-i
, srcKey
->keyLength
, expectedKeyLength
);
992 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
996 /* Point to record data (round up to even byte boundary) */
997 srcPtr
= (SInt16
*)((char *)srcKey
+ ((srcKey
->keyLength
+ 2) & ~1));
1000 * Make sure that we can safely dereference the record's type field or
1001 * and index node's child node number.
1003 if ((char *)srcPtr
+ sizeof(UInt32
) > nextRecord
) {
1004 if (debug
) plog("hfs_swap_HFSBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
1005 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
1010 * For index nodes, the record data is just the child's node number.
1011 * Skip over swapping the various types of catalog record.
1013 if (srcDesc
->kind
== kBTIndexNode
) {
1014 *((UInt32
*)srcPtr
) = SWAP_BE32 (*((UInt32
*)srcPtr
));
1018 /* Make sure the recordType is in native order before using it. */
1019 if (direction
== kSwapBTNodeBigToHost
)
1020 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
1022 if (srcPtr
[0] == kHFSFolderRecord
) {
1023 HFSCatalogFolder
*srcRec
= (HFSCatalogFolder
*)srcPtr
;
1024 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
1025 if (debug
) plog("hfs_swap_HFSBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
1026 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
1030 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
1031 srcRec
->valence
= SWAP_BE16 (srcRec
->valence
);
1033 srcRec
->folderID
= SWAP_BE32 (srcRec
->folderID
);
1034 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
1035 srcRec
->modifyDate
= SWAP_BE32 (srcRec
->modifyDate
);
1036 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
1038 /* The only field we use in srcRec->userInfo is frFlags (used in VLockedChk). */
1039 srcRec
->userInfo
.frFlags
= SWAP_BE16 (srcRec
->userInfo
.frFlags
);
1041 /* Don't swap srcRec->finderInfo */
1042 /* Don't swap resserved array */
1044 } else if (srcPtr
[0] == kHFSFileRecord
) {
1045 HFSCatalogFile
*srcRec
= (HFSCatalogFile
*)srcPtr
;
1046 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
1047 if (debug
) plog("hfs_swap_HFSBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
1048 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
1052 srcRec
->flags
= srcRec
->flags
;
1053 srcRec
->fileType
= srcRec
->fileType
;
1055 /* Don't swap srcRec->userInfo */
1057 srcRec
->fileID
= SWAP_BE32 (srcRec
->fileID
);
1059 srcRec
->dataStartBlock
= SWAP_BE16 (srcRec
->dataStartBlock
);
1060 srcRec
->dataLogicalSize
= SWAP_BE32 (srcRec
->dataLogicalSize
);
1061 srcRec
->dataPhysicalSize
= SWAP_BE32 (srcRec
->dataPhysicalSize
);
1063 srcRec
->rsrcStartBlock
= SWAP_BE16 (srcRec
->rsrcStartBlock
);
1064 srcRec
->rsrcLogicalSize
= SWAP_BE32 (srcRec
->rsrcLogicalSize
);
1065 srcRec
->rsrcPhysicalSize
= SWAP_BE32 (srcRec
->rsrcPhysicalSize
);
1067 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
1068 srcRec
->modifyDate
= SWAP_BE32 (srcRec
->modifyDate
);
1069 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
1071 /* Don't swap srcRec->finderInfo */
1073 srcRec
->clumpSize
= SWAP_BE16 (srcRec
->clumpSize
);
1075 /* Swap the two sets of extents as an array of six (three each) UInt16 */
1076 for (j
= 0; j
< kHFSExtentDensity
* 2; j
++) {
1077 srcRec
->dataExtents
[j
].startBlock
= SWAP_BE16 (srcRec
->dataExtents
[j
].startBlock
);
1078 srcRec
->dataExtents
[j
].blockCount
= SWAP_BE16 (srcRec
->dataExtents
[j
].blockCount
);
1081 /* Don't swap srcRec->reserved */
1083 } else if ((srcPtr
[0] == kHFSFolderThreadRecord
) ||
1084 (srcPtr
[0] == kHFSFileThreadRecord
)) {
1085 HFSCatalogThread
*srcRec
= (HFSCatalogThread
*)srcPtr
;
1087 /* Make sure there is room for parentID and name length */
1088 if ((char *) &srcRec
->nodeName
[1] > nextRecord
) {
1089 if (debug
) plog("hfs_swap_HFSBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
1090 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
1094 /* Don't swap srcRec->reserved array */
1096 srcRec
->parentID
= SWAP_BE32 (srcRec
->parentID
);
1098 /* Don't swap srcRec->nodeName */
1100 /* Make sure there is room for the name in the buffer */
1101 if ((char *) &srcRec
->nodeName
[srcRec
->nodeName
[0]] > nextRecord
) {
1102 if (debug
) plog("hfs_swap_HFSBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
1103 WriteError(fcb
->fcbVolume
->vcbGPtr
, E_BadNode
, fcb
->fcbFileID
, src
->blockNum
);
1107 if (debug
) plog("hfs_swap_HFSBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
1110 /* We can swap the record type now that we're done using it */
1111 if (direction
== kSwapBTNodeHostToBig
)
1112 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
1116 if (debug
) plog("hfs_swap_HFSBTInternalNode: fileID %u is not a system B-tree\n", fileID
);