5 // Created by Yakov Ben Zaken on 22/03/2018.
11 #include "lf_hfs_endian.h"
12 #include "lf_hfs_btrees_private.h"
13 #include "lf_hfs_vfsops.h"
14 #include "lf_hfs_utils.h"
15 #include "lf_hfs_generic_buf.h"
17 #define DEBUG_BTNODE_SWAP 0
20 * Internal swapping routines
22 * These routines handle swapping the records of leaf and index nodes. The
23 * layout of the keys and records varies depending on the kind of B-tree
24 * (determined by fileID).
26 * The direction parameter must be kSwapBTNodeBigToHost or kSwapBTNodeHostToBig.
27 * The kSwapBTNodeHeaderRecordOnly "direction" is not valid for these routines.
29 int hfs_swap_HFSPlusBTInternalNode (BlockDescriptor
*src
, HFSCatalogNodeID fileID
, enum HFSBTSwapDirection direction
);
30 void hfs_swap_HFSPlusForkData (HFSPlusForkData
*src
);
33 * hfs_swap_HFSPlusForkData
36 hfs_swap_HFSPlusForkData (
42 src
->logicalSize
= SWAP_BE64 (src
->logicalSize
);
44 src
->clumpSize
= SWAP_BE32 (src
->clumpSize
);
45 src
->totalBlocks
= SWAP_BE32 (src
->totalBlocks
);
47 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
48 src
->extents
[i
].startBlock
= SWAP_BE32 (src
->extents
[i
].startBlock
);
49 src
->extents
[i
].blockCount
= SWAP_BE32 (src
->extents
[i
].blockCount
);
56 * NOTE: This operation is not naturally symmetric.
57 * We have to determine which way we're swapping things.
63 enum HFSBTSwapDirection direction
,
64 u_int8_t allow_empty_node
68 GenericLFBuf
*psBuf
= src
->blockHeader
;
69 lf_hfs_generic_buf_lock(psBuf
);
72 case kSwapBTNodeBigToHost
:
73 lf_hfs_generic_buf_set_cache_flag(psBuf
, GEN_BUF_LITTLE_ENDIAN
);
75 case kSwapBTNodeHostToBig
:
76 lf_hfs_generic_buf_clear_cache_flag(psBuf
, GEN_BUF_LITTLE_ENDIAN
);
78 case kSwapBTNodeHeaderRecordOnly
:
81 panic("invalid direction");
85 BTNodeDescriptor
*srcDesc
= src
->buffer
;
86 u_int16_t
*srcOffs
= NULL
;
87 BTreeControlBlockPtr btcb
= (BTreeControlBlockPtr
)VTOF(vp
)->fcbBTCBPtr
;
88 u_int16_t i
; /* index to match srcDesc->numRecords */
92 printf("hfs_swap_BTNode: direction %u (%s), psVnode %p, blockNum %llu uPhyCluster %llu\n", direction
, (direction
==0)?"RD":(direction
==1)?"WR":"NA", vp
, src
->blockNum
, psBuf
->uPhyCluster
);
93 uint32_t *pData
= src
->buffer
;
94 printf("hfs_swap_BTNode: %p before: 0x%x, 0x%x, 0x%x, 0x%x\n", pData
, pData
[0], pData
[1], pData
[2], pData
[3]);
98 if (direction
== kSwapBTNodeBigToHost
) {
99 LFHFS_LOG(LEVEL_DEBUG
, "hfs: BE -> Native Swap\n");
100 } else if (direction
== kSwapBTNodeHostToBig
) {
101 LFHFS_LOG(LEVEL_DEBUG
, "hfs: Native -> BE Swap\n");
102 } else if (direction
== kSwapBTNodeHeaderRecordOnly
) {
103 LFHFS_LOG(LEVEL_DEBUG
, "hfs: Not swapping descriptors\n");
105 LFHFS_LOG(LEVEL_ERROR
, "hfs_swap_BTNode: This is impossible");
111 * If we are doing a swap from on-disk to in-memory, then swap the node
112 * descriptor and record offsets before we need to use them.
114 if (direction
== kSwapBTNodeBigToHost
) {
115 srcDesc
->fLink
= SWAP_BE32 (srcDesc
->fLink
);
116 srcDesc
->bLink
= SWAP_BE32 (srcDesc
->bLink
);
119 * When first opening a BTree, we have to read the header node before the
120 * control block is initialized. In this case, totalNodes will be zero,
121 * so skip the bounds checking. Also, we should ignore the header node when
122 * checking for invalid forwards and backwards links, since the header node's
123 * links can point back to itself legitimately.
125 if (btcb
->totalNodes
!= 0) {
126 if (srcDesc
->fLink
>= btcb
->totalNodes
) {
127 LFHFS_LOG( LEVEL_ERROR
, "hfs_swap_BTNode: invalid forward link (0x%08x >= 0x%08x)\n", srcDesc
->fLink
, btcb
->totalNodes
);
128 error
= fsBTInvalidHeaderErr
;
131 if (srcDesc
->bLink
>= btcb
->totalNodes
) {
132 LFHFS_LOG( LEVEL_ERROR
, "hfs_swap_BTNode: invalid backward link (0x%08x >= 0x%08x)\n", srcDesc
->bLink
, btcb
->totalNodes
);
133 error
= fsBTInvalidHeaderErr
;
137 if ((src
->blockNum
!= 0) && (srcDesc
->fLink
== (u_int32_t
) src
->blockNum
)) {
138 LFHFS_LOG( LEVEL_ERROR
, "hfs_swap_BTNode: invalid forward link (0x%08x == 0x%08x)\n", srcDesc
->fLink
, (u_int32_t
) src
->blockNum
);
139 error
= fsBTInvalidHeaderErr
;
142 if ((src
->blockNum
!= 0) && (srcDesc
->bLink
== (u_int32_t
) src
->blockNum
)) {
143 LFHFS_LOG( LEVEL_ERROR
, "hfs_swap_BTNode: invalid backward link (0x%08x == 0x%08x)\n", srcDesc
->bLink
, (u_int32_t
) src
->blockNum
);
144 error
= fsBTInvalidHeaderErr
;
152 * Check srcDesc->kind. Don't swap it because it's only one byte.
154 if (srcDesc
->kind
< kBTLeafNode
|| srcDesc
->kind
> kBTMapNode
) {
155 LFHFS_LOG(LEVEL_ERROR
, "hfs_swap_BTNode: invalid node kind (%d)\n", srcDesc
->kind
);
156 error
= fsBTInvalidHeaderErr
;
161 * Check srcDesc->height. Don't swap it because it's only one byte.
163 if (srcDesc
->height
> kMaxTreeDepth
) {
164 LFHFS_LOG(LEVEL_ERROR
, "hfs_swap_BTNode: invalid node height (%d)\n", srcDesc
->height
);
165 error
= fsBTInvalidHeaderErr
;
169 /* Don't swap srcDesc->reserved */
171 srcDesc
->numRecords
= SWAP_BE16 (srcDesc
->numRecords
);
174 * Swap the node offsets (including the free space one!).
176 srcOffs
= (u_int16_t
*)((char *)src
->buffer
+ (src
->blockSize
- ((srcDesc
->numRecords
+ 1) * sizeof (u_int16_t
))));
179 * Sanity check that the record offsets are within the node itself.
181 if ((char *)srcOffs
> ((char *)src
->buffer
+ src
->blockSize
) ||
182 (char *)srcOffs
< ((char *)src
->buffer
+ sizeof(BTNodeDescriptor
))) {
183 LFHFS_LOG(LEVEL_ERROR
, "hfs_swap_BTNode: invalid record count (0x%04X)\n", srcDesc
->numRecords
);
184 error
= fsBTInvalidHeaderErr
;
189 * Swap and sanity check each of the record offsets.
191 for (i
= 0; i
<= srcDesc
->numRecords
; i
++) {
192 srcOffs
[i
] = SWAP_BE16 (srcOffs
[i
]);
195 * Sanity check: must be even, and within the node itself.
197 * We may be called to swap an unused node, which contains all zeroes.
198 * Unused nodes are expected only when allow_empty_node is true.
199 * If it is false and record offset is zero, return error.
201 if ((srcOffs
[i
] & 1) || (
202 (allow_empty_node
== false) && (srcOffs
[i
] == 0)) ||
203 (srcOffs
[i
] < sizeof(BTNodeDescriptor
) && srcOffs
[i
] != 0) ||
204 (srcOffs
[i
] > (src
->blockSize
- 2 * (srcDesc
->numRecords
+ 1)))) {
205 LFHFS_LOG(LEVEL_ERROR
, "hfs_swap_BTNode: offset #%d invalid (0x%04X) (blockSize 0x%x numRecords %d)\n",
206 i
, srcOffs
[i
], (int32_t)src
->blockSize
, srcDesc
->numRecords
);
207 error
= fsBTInvalidHeaderErr
;
212 * Make sure the offsets are strictly increasing. Note that we're looping over
213 * them backwards, hence the order in the comparison.
215 if ((i
!= 0) && (srcOffs
[i
] >= srcOffs
[i
-1])) {
216 LFHFS_LOG(LEVEL_ERROR
, "hfs_swap_BTNode: offsets %d and %d out of order (0x%04X, 0x%04X)\n",
217 i
, i
-1, srcOffs
[i
], srcOffs
[i
-1]);
219 error
= fsBTInvalidHeaderErr
;
226 * Swap the records (ordered by frequency of access)
228 if ((srcDesc
->kind
== kBTIndexNode
) ||
229 (srcDesc
->kind
== kBTLeafNode
)) {
231 error
= hfs_swap_HFSPlusBTInternalNode (src
, VTOC(vp
)->c_fileid
, direction
);
232 if (error
) goto fail
;
234 } else if (srcDesc
-> kind
== kBTMapNode
) {
235 /* Don't swap the bitmaps, they'll be done in the bitmap routines */
237 } else if (srcDesc
-> kind
== kBTHeaderNode
) {
238 /* The header's offset is hard-wired because we cannot trust the offset pointers. */
239 BTHeaderRec
*srcHead
= (BTHeaderRec
*)((char *)src
->buffer
+ sizeof(BTNodeDescriptor
));
241 srcHead
->treeDepth
= SWAP_BE16 (srcHead
->treeDepth
);
243 srcHead
->rootNode
= SWAP_BE32 (srcHead
->rootNode
);
244 srcHead
->leafRecords
= SWAP_BE32 (srcHead
->leafRecords
);
245 srcHead
->firstLeafNode
= SWAP_BE32 (srcHead
->firstLeafNode
);
246 srcHead
->lastLeafNode
= SWAP_BE32 (srcHead
->lastLeafNode
);
248 srcHead
->nodeSize
= SWAP_BE16 (srcHead
->nodeSize
);
249 srcHead
->maxKeyLength
= SWAP_BE16 (srcHead
->maxKeyLength
);
251 srcHead
->totalNodes
= SWAP_BE32 (srcHead
->totalNodes
);
252 srcHead
->freeNodes
= SWAP_BE32 (srcHead
->freeNodes
);
254 srcHead
->clumpSize
= SWAP_BE32 (srcHead
->clumpSize
);
255 srcHead
->attributes
= SWAP_BE32 (srcHead
->attributes
);
257 /* Don't swap srcHead->reserved1 */
258 /* Don't swap srcHead->btreeType; it's only one byte */
259 /* Don't swap srcHead->reserved2 */
260 /* Don't swap srcHead->reserved3 */
261 /* Don't swap bitmap */
265 * If we are doing a swap from in-memory to on-disk, then swap the node
266 * descriptor and record offsets after we're done using them.
268 if (direction
== kSwapBTNodeHostToBig
) {
270 * Sanity check and swap the forward and backward links.
271 * Ignore the header node since its forward and backwards links can legitimately
274 if (srcDesc
->fLink
>= btcb
->totalNodes
) {
275 LFHFS_LOG(LEVEL_ERROR
, "hfs_UNswap_BTNode: invalid forward link (0x%08X)\n", srcDesc
->fLink
);
276 error
= fsBTInvalidHeaderErr
;
279 if ((src
->blockNum
!= 0) && (srcDesc
->fLink
== (u_int32_t
) src
->blockNum
)) {
280 LFHFS_LOG(LEVEL_ERROR
, "hfs_UNswap_BTNode: invalid forward link (0x%08x == 0x%08x)\n",
281 srcDesc
->fLink
, (u_int32_t
) src
->blockNum
);
282 error
= fsBTInvalidHeaderErr
;
286 if (srcDesc
->bLink
>= btcb
->totalNodes
) {
287 LFHFS_LOG(LEVEL_ERROR
, "hfs_UNswap_BTNode: invalid backward link (0x%08X)\n", srcDesc
->bLink
);
288 error
= fsBTInvalidHeaderErr
;
291 if ((src
->blockNum
!= 0) && (srcDesc
->bLink
== (u_int32_t
) src
->blockNum
)) {
292 LFHFS_LOG(LEVEL_ERROR
, "hfs_UNswap_BTNode: invalid backward link (0x%08x == 0x%08x)\n",
293 srcDesc
->bLink
, (u_int32_t
) src
->blockNum
);
294 error
= fsBTInvalidHeaderErr
;
299 srcDesc
->fLink
= SWAP_BE32 (srcDesc
->fLink
);
300 srcDesc
->bLink
= SWAP_BE32 (srcDesc
->bLink
);
303 * Check srcDesc->kind. Don't swap it because it's only one byte.
305 if (srcDesc
->kind
< kBTLeafNode
|| srcDesc
->kind
> kBTMapNode
) {
306 LFHFS_LOG(LEVEL_ERROR
, "hfs_UNswap_BTNode: invalid node kind (%d)\n", srcDesc
->kind
);
307 error
= fsBTInvalidHeaderErr
;
312 * Check srcDesc->height. Don't swap it because it's only one byte.
314 if (srcDesc
->height
> kMaxTreeDepth
) {
315 LFHFS_LOG(LEVEL_ERROR
, "hfs_UNswap_BTNode: invalid node height (%d)\n", srcDesc
->height
);
316 error
= fsBTInvalidHeaderErr
;
320 /* Don't swap srcDesc->reserved */
323 * Swap the node offsets (including the free space one!).
325 srcOffs
= (u_int16_t
*)((char *)src
->buffer
+ (src
->blockSize
- ((srcDesc
->numRecords
+ 1) * sizeof (u_int16_t
))));
328 * Sanity check that the record offsets are within the node itself.
330 if ((char *)srcOffs
> ((char *)src
->buffer
+ src
->blockSize
) ||
331 (char *)srcOffs
< ((char *)src
->buffer
+ sizeof(BTNodeDescriptor
))) {
332 LFHFS_LOG(LEVEL_ERROR
, "hfs_UNswap_BTNode: invalid record count (0x%04X)\n", srcDesc
->numRecords
);
333 error
= fsBTInvalidHeaderErr
;
338 * Swap and sanity check each of the record offsets.
340 for (i
= 0; i
<= srcDesc
->numRecords
; i
++) {
342 * Sanity check: must be even, and within the node itself.
344 * We may be called to swap an unused node, which contains all zeroes.
345 * This can happen when the last record from a node gets deleted.
346 * This is why we allow the record offset to be zero.
347 * Unused nodes are expected only when allow_empty_node is true
348 * (the caller should set it to true for kSwapBTNodeBigToHost).
350 if ((srcOffs
[i
] & 1) ||
351 ((allow_empty_node
== false) && (srcOffs
[i
] == 0)) ||
352 (srcOffs
[i
] < sizeof(BTNodeDescriptor
) && srcOffs
[i
] != 0) ||
353 (srcOffs
[i
] > (src
->blockSize
- 2 * (srcDesc
->numRecords
+ 1)))) {
354 LFHFS_LOG(LEVEL_ERROR
, "hfs_UNswap_BTNode: offset #%d invalid (0x%04X) (blockSize 0x%lx numRecords %d)\n",
355 i
, srcOffs
[i
], src
->blockSize
, srcDesc
->numRecords
);
356 error
= fsBTInvalidHeaderErr
;
361 * Make sure the offsets are strictly increasing. Note that we're looping over
362 * them backwards, hence the order in the comparison.
364 if ((i
< srcDesc
->numRecords
) && (srcOffs
[i
+1] >= srcOffs
[i
])) {
365 LFHFS_LOG(LEVEL_ERROR
, "hfs_UNswap_BTNode: offsets %d and %d out of order (0x%04X, 0x%04X)\n",
366 i
+1, i
, srcOffs
[i
+1], srcOffs
[i
]);
367 error
= fsBTInvalidHeaderErr
;
371 srcOffs
[i
] = SWAP_BE16 (srcOffs
[i
]);
374 srcDesc
->numRecords
= SWAP_BE16 (srcDesc
->numRecords
);
378 lf_hfs_generic_buf_unlock(psBuf
);
381 * Log some useful information about where the corrupt node is.
383 LFHFS_LOG( LEVEL_ERROR
, "lf_hfs: node=%lld fileID=%u volume=%s\n", src
->blockNum
, VTOC(vp
)->c_fileid
, VTOVCB(vp
)->vcbVN
);
384 hfs_mark_inconsistent(VTOVCB(vp
), HFS_INCONSISTENCY_DETECTED
);
386 #if DEBUG_BTNODE_SWAP
387 printf("hfs_swap_BTNode: after: 0x%x, 0x%x, 0x%x, 0x%x\n", pData
[0], pData
[1], pData
[2], pData
[3]);
394 hfs_swap_HFSPlusBTInternalNode (
395 BlockDescriptor
*src
,
396 HFSCatalogNodeID fileID
,
397 enum HFSBTSwapDirection direction
400 BTNodeDescriptor
*srcDesc
= src
->buffer
;
401 u_int16_t
*srcOffs
= (u_int16_t
*)((char *)src
->buffer
+ (src
->blockSize
- (srcDesc
->numRecords
* sizeof (u_int16_t
))));
402 char *nextRecord
; /* Points to start of record following current one */
405 * i is an int32 because it needs to be negative to index the offset to free space.
406 * srcDesc->numRecords is a u_int16_t and is unlikely to become 32-bit so this should be ok.
412 if (fileID
== kHFSExtentsFileID
) {
413 HFSPlusExtentKey
*srcKey
;
414 HFSPlusExtentDescriptor
*srcRec
;
415 size_t recordSize
; /* Size of the data part of the record, or node number for index nodes */
417 if (srcDesc
->kind
== kBTIndexNode
)
418 recordSize
= sizeof(u_int32_t
);
420 recordSize
= sizeof(HFSPlusExtentDescriptor
);
422 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
423 /* Point to the start of the record we're currently checking. */
424 srcKey
= (HFSPlusExtentKey
*)((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 the key and data are within the buffer. Since both key
435 * and data are fixed size, this is relatively easy. Note that this
436 * relies on the keyLength being a constant; we verify the keyLength
439 if ((char *)srcKey
+ sizeof(HFSPlusExtentKey
) + recordSize
> nextRecord
) {
441 LFHFS_LOG((direction
== kSwapBTNodeHostToBig
) ? LEVEL_ERROR
: LEVEL_DEBUG
, "hfs_swap_HFSPlusBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
442 if (direction
== kSwapBTNodeHostToBig
) {
445 return fsBTInvalidNodeErr
;
448 if (direction
== kSwapBTNodeBigToHost
)
449 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
450 if (srcKey
->keyLength
!= sizeof(*srcKey
) - sizeof(srcKey
->keyLength
)) {
452 LFHFS_LOG((direction
== kSwapBTNodeHostToBig
) ? LEVEL_ERROR
: LEVEL_DEBUG
, "hfs_swap_HFSPlusBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, srcKey
->keyLength
);
453 if (direction
== kSwapBTNodeHostToBig
) {
456 return fsBTInvalidNodeErr
;
458 srcRec
= (HFSPlusExtentDescriptor
*)((char *)srcKey
+ srcKey
->keyLength
+ sizeof(srcKey
->keyLength
));
459 if (direction
== kSwapBTNodeHostToBig
)
460 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
462 /* Don't swap srcKey->forkType; it's only one byte */
463 /* Don't swap srcKey->pad */
465 srcKey
->fileID
= SWAP_BE32 (srcKey
->fileID
);
466 srcKey
->startBlock
= SWAP_BE32 (srcKey
->startBlock
);
468 if (srcDesc
->kind
== kBTIndexNode
) {
469 /* For index nodes, the record data is just a child node number. */
470 *((u_int32_t
*)srcRec
) = SWAP_BE32 (*((u_int32_t
*)srcRec
));
472 /* Swap the extent data */
473 for (j
= 0; j
< kHFSPlusExtentDensity
; j
++) {
474 srcRec
[j
].startBlock
= SWAP_BE32 (srcRec
[j
].startBlock
);
475 srcRec
[j
].blockCount
= SWAP_BE32 (srcRec
[j
].blockCount
);
480 } else if (fileID
== kHFSCatalogFileID
) {
481 HFSPlusCatalogKey
*srcKey
;
485 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
486 /* Point to the start of the record we're currently checking. */
487 srcKey
= (HFSPlusCatalogKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
490 * Point to start of next (larger offset) record. We'll use this
491 * to be sure the current record doesn't overflow into the next
494 nextRecord
= (char *)src
->buffer
+ (uintptr_t)(srcOffs
[i
-1]);
497 * Make sure we can safely dereference the keyLength and parentID fields.
499 if ((char *)srcKey
+ offsetof(HFSPlusCatalogKey
, nodeName
.unicode
[0]) > nextRecord
) {
500 LFHFS_LOG((direction
== kSwapBTNodeHostToBig
) ? LEVEL_ERROR
: LEVEL_DEBUG
, "hfs_swap_HFSPlusBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
501 if (direction
== kSwapBTNodeHostToBig
) {
504 return fsBTInvalidNodeErr
;
508 * Swap and sanity check the key length
510 if (direction
== kSwapBTNodeBigToHost
)
511 srcKey
->keyLength
= SWAP_BE16 (srcKey
->keyLength
);
512 keyLength
= srcKey
->keyLength
; /* Put it in a local (native order) because we use it several times */
513 if (direction
== kSwapBTNodeHostToBig
)
514 srcKey
->keyLength
= SWAP_BE16 (keyLength
);
516 /* Sanity check the key length */
517 if (keyLength
< kHFSPlusCatalogKeyMinimumLength
|| keyLength
> kHFSPlusCatalogKeyMaximumLength
) {
519 LFHFS_LOG((direction
== kSwapBTNodeHostToBig
) ? LEVEL_ERROR
: LEVEL_DEBUG
, "hfs_swap_HFSPlusBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
520 if (direction
== kSwapBTNodeHostToBig
) {
523 return fsBTInvalidNodeErr
;
527 * Make sure that we can safely dereference the record's type field or
528 * an index node's child node number.
530 srcPtr
= (int16_t *)((char *)srcKey
+ keyLength
+ sizeof(srcKey
->keyLength
));
531 if ((char *)srcPtr
+ sizeof(u_int32_t
) > nextRecord
) {
533 LFHFS_LOG((direction
== kSwapBTNodeHostToBig
) ? LEVEL_ERROR
: LEVEL_DEBUG
, "hfs_swap_HFSPlusBTInternalNode: catalog key #%d too big\n", srcDesc
->numRecords
-i
-1);
534 if (direction
== kSwapBTNodeHostToBig
) {
537 return fsBTInvalidNodeErr
;
540 srcKey
->parentID
= SWAP_BE32 (srcKey
->parentID
);
543 * Swap and sanity check the key's node name
545 if (direction
== kSwapBTNodeBigToHost
)
546 srcKey
->nodeName
.length
= SWAP_BE16 (srcKey
->nodeName
.length
);
547 /* Make sure name length is consistent with key length */
548 if (keyLength
< sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
549 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0])) {
550 LFHFS_LOG((direction
== kSwapBTNodeHostToBig
) ? LEVEL_ERROR
: LEVEL_DEBUG
, "hfs_swap_HFSPlusBTInternalNode: catalog record #%d keyLength=%d expected=%lu\n",
551 srcDesc
->numRecords
-i
, keyLength
, sizeof(srcKey
->parentID
) + sizeof(srcKey
->nodeName
.length
) +
552 srcKey
->nodeName
.length
*sizeof(srcKey
->nodeName
.unicode
[0]));
553 if (direction
== kSwapBTNodeHostToBig
) {
556 return fsBTInvalidNodeErr
;
558 for (j
= 0; j
< srcKey
->nodeName
.length
; j
++) {
559 srcKey
->nodeName
.unicode
[j
] = SWAP_BE16 (srcKey
->nodeName
.unicode
[j
]);
561 if (direction
== kSwapBTNodeHostToBig
)
562 srcKey
->nodeName
.length
= SWAP_BE16 (srcKey
->nodeName
.length
);
565 * For index nodes, the record data is just the child's node number.
566 * Skip over swapping the various types of catalog record.
568 if (srcDesc
->kind
== kBTIndexNode
) {
569 *((u_int32_t
*)srcPtr
) = SWAP_BE32 (*((u_int32_t
*)srcPtr
));
573 /* Make sure the recordType is in native order before using it. */
574 if (direction
== kSwapBTNodeBigToHost
)
575 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
577 if (srcPtr
[0] == kHFSPlusFolderRecord
) {
578 HFSPlusCatalogFolder
*srcRec
= (HFSPlusCatalogFolder
*)srcPtr
;
579 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
581 LFHFS_LOG((direction
== kSwapBTNodeHostToBig
) ? LEVEL_ERROR
: LEVEL_DEBUG
, "hfs_swap_HFSPlusBTInternalNode: catalog folder record #%d too big\n", srcDesc
->numRecords
-i
-1);
582 if (direction
== kSwapBTNodeHostToBig
) {
585 return fsBTInvalidNodeErr
;
588 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
589 srcRec
->valence
= SWAP_BE32 (srcRec
->valence
);
590 srcRec
->folderID
= SWAP_BE32 (srcRec
->folderID
);
591 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
592 srcRec
->contentModDate
= SWAP_BE32 (srcRec
->contentModDate
);
593 srcRec
->attributeModDate
= SWAP_BE32 (srcRec
->attributeModDate
);
594 srcRec
->accessDate
= SWAP_BE32 (srcRec
->accessDate
);
595 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
597 srcRec
->bsdInfo
.ownerID
= SWAP_BE32 (srcRec
->bsdInfo
.ownerID
);
598 srcRec
->bsdInfo
.groupID
= SWAP_BE32 (srcRec
->bsdInfo
.groupID
);
600 /* Don't swap srcRec->bsdInfo.adminFlags; it's only one byte */
601 /* Don't swap srcRec->bsdInfo.ownerFlags; it's only one byte */
603 srcRec
->bsdInfo
.fileMode
= SWAP_BE16 (srcRec
->bsdInfo
.fileMode
);
604 srcRec
->bsdInfo
.special
.iNodeNum
= SWAP_BE32 (srcRec
->bsdInfo
.special
.iNodeNum
);
606 srcRec
->textEncoding
= SWAP_BE32 (srcRec
->textEncoding
);
608 /* Don't swap srcRec->userInfo */
609 /* Don't swap srcRec->finderInfo */
610 srcRec
->folderCount
= SWAP_BE32 (srcRec
->folderCount
);
612 } else if (srcPtr
[0] == kHFSPlusFileRecord
) {
613 HFSPlusCatalogFile
*srcRec
= (HFSPlusCatalogFile
*)srcPtr
;
614 if ((char *)srcRec
+ sizeof(*srcRec
) > nextRecord
) {
616 LFHFS_LOG((direction
== kSwapBTNodeHostToBig
) ? LEVEL_ERROR
: LEVEL_DEBUG
, "hfs_swap_HFSPlusBTInternalNode: catalog file record #%d too big\n", srcDesc
->numRecords
-i
-1);
617 if (direction
== kSwapBTNodeHostToBig
) {
620 return fsBTInvalidNodeErr
;
623 srcRec
->flags
= SWAP_BE16 (srcRec
->flags
);
625 srcRec
->fileID
= SWAP_BE32 (srcRec
->fileID
);
627 srcRec
->createDate
= SWAP_BE32 (srcRec
->createDate
);
628 srcRec
->contentModDate
= SWAP_BE32 (srcRec
->contentModDate
);
629 srcRec
->attributeModDate
= SWAP_BE32 (srcRec
->attributeModDate
);
630 srcRec
->accessDate
= SWAP_BE32 (srcRec
->accessDate
);
631 srcRec
->backupDate
= SWAP_BE32 (srcRec
->backupDate
);
633 srcRec
->bsdInfo
.ownerID
= SWAP_BE32 (srcRec
->bsdInfo
.ownerID
);
634 srcRec
->bsdInfo
.groupID
= SWAP_BE32 (srcRec
->bsdInfo
.groupID
);
636 /* Don't swap srcRec->bsdInfo.adminFlags; it's only one byte */
637 /* Don't swap srcRec->bsdInfo.ownerFlags; it's only one byte */
639 srcRec
->bsdInfo
.fileMode
= SWAP_BE16 (srcRec
->bsdInfo
.fileMode
);
640 srcRec
->bsdInfo
.special
.iNodeNum
= SWAP_BE32 (srcRec
->bsdInfo
.special
.iNodeNum
);
642 srcRec
->textEncoding
= SWAP_BE32 (srcRec
->textEncoding
);
644 /* If kHFSHasLinkChainBit is set, reserved1 is hl_FirstLinkID.
645 * In all other context, it is expected to be zero.
647 srcRec
->reserved1
= SWAP_BE32 (srcRec
->reserved1
);
649 /* Don't swap srcRec->userInfo */
650 /* Don't swap srcRec->finderInfo */
651 /* Don't swap srcRec->reserved2 */
653 hfs_swap_HFSPlusForkData (&srcRec
->dataFork
);
654 hfs_swap_HFSPlusForkData (&srcRec
->resourceFork
);
656 } else if ((srcPtr
[0] == kHFSPlusFolderThreadRecord
) ||
657 (srcPtr
[0] == kHFSPlusFileThreadRecord
)) {
660 * Make sure there is room for parentID and name length.
662 HFSPlusCatalogThread
*srcRec
= (HFSPlusCatalogThread
*)srcPtr
;
663 if ((char *) &srcRec
->nodeName
.unicode
[0] > nextRecord
) {
664 LFHFS_LOG((direction
== kSwapBTNodeHostToBig
) ? LEVEL_ERROR
: LEVEL_DEBUG
, "hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d too big\n", srcDesc
->numRecords
-i
-1);
665 if (direction
== kSwapBTNodeHostToBig
) {
668 return fsBTInvalidNodeErr
;
671 /* Don't swap srcRec->reserved */
673 srcRec
->parentID
= SWAP_BE32 (srcRec
->parentID
);
675 if (direction
== kSwapBTNodeBigToHost
)
676 srcRec
->nodeName
.length
= SWAP_BE16 (srcRec
->nodeName
.length
);
679 * Make sure there is room for the name in the buffer.
680 * Then swap the characters of the name itself.
682 if ((char *) &srcRec
->nodeName
.unicode
[srcRec
->nodeName
.length
] > nextRecord
) {
683 LFHFS_LOG((direction
== kSwapBTNodeHostToBig
) ? LEVEL_ERROR
: LEVEL_DEBUG
, "hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d name too big\n", srcDesc
->numRecords
-i
-1);
684 if (direction
== kSwapBTNodeHostToBig
) {
687 return fsBTInvalidNodeErr
;
689 for (j
= 0; j
< srcRec
->nodeName
.length
; j
++) {
690 srcRec
->nodeName
.unicode
[j
] = SWAP_BE16 (srcRec
->nodeName
.unicode
[j
]);
693 if (direction
== kSwapBTNodeHostToBig
)
694 srcRec
->nodeName
.length
= SWAP_BE16 (srcRec
->nodeName
.length
);
697 LFHFS_LOG((direction
== kSwapBTNodeHostToBig
) ? LEVEL_ERROR
: LEVEL_DEBUG
, "hfs_swap_HFSPlusBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr
[0], srcDesc
->numRecords
-i
-1);
698 if (direction
== kSwapBTNodeHostToBig
) {
701 return fsBTInvalidNodeErr
;
704 /* We can swap the record type now that we're done using it. */
705 if (direction
== kSwapBTNodeHostToBig
)
706 srcPtr
[0] = SWAP_BE16 (srcPtr
[0]);
709 } else if (fileID
== kHFSAttributesFileID
) {
710 HFSPlusAttrKey
*srcKey
;
711 HFSPlusAttrRecord
*srcRec
;
713 u_int32_t attrSize
= 0;
715 for (i
= 0; i
< srcDesc
->numRecords
; i
++) {
716 /* Point to the start of the record we're currently checking. */
717 srcKey
= (HFSPlusAttrKey
*)((char *)src
->buffer
+ srcOffs
[i
]);
720 * Point to start of next (larger offset) record. We'll use this
721 * to be sure the current record doesn't overflow into the next
724 nextRecord
= (char *)src
->buffer
+ srcOffs
[i
-1];
726 /* Make sure there is room in the buffer for a minimal key */
727 if ((char *) &srcKey
->attrName
[1] > nextRecord
) {
728 LFHFS_LOG((direction
== kSwapBTNodeHostToBig
) ? LEVEL_ERROR
: LEVEL_DEBUG
, "hfs_swap_HFSPlusBTInternalNode: attr key #%d offset too big (0x%04X)\n", srcDesc
->numRecords
-i
-1, srcOffs
[i
]);
729 if (direction
== kSwapBTNodeHostToBig
) {
732 return fsBTInvalidNodeErr
;
735 /* Swap the key length field */
736 if (direction
== kSwapBTNodeBigToHost
)
737 srcKey
->keyLength
= SWAP_BE16(srcKey
->keyLength
);
738 keyLength
= srcKey
->keyLength
; /* Keep a copy in native order */
739 if (direction
== kSwapBTNodeHostToBig
)
740 srcKey
->keyLength
= SWAP_BE16(srcKey
->keyLength
);
743 * Make sure that we can safely dereference the record's type field or
744 * an index node's child node number.
746 srcRec
= (HFSPlusAttrRecord
*)((char *)srcKey
+ keyLength
+ sizeof(srcKey
->keyLength
));
747 if ((char *)srcRec
+ sizeof(u_int32_t
) > nextRecord
) {
748 LFHFS_LOG((direction
== kSwapBTNodeHostToBig
) ? LEVEL_ERROR
: LEVEL_DEBUG
, "hfs_swap_HFSPlusBTInternalNode: attr key #%d too big (%d)\n", srcDesc
->numRecords
-i
-1, keyLength
);
749 if (direction
== kSwapBTNodeHostToBig
) {
752 return fsBTInvalidNodeErr
;
755 srcKey
->fileID
= SWAP_BE32(srcKey
->fileID
);
756 srcKey
->startBlock
= SWAP_BE32(srcKey
->startBlock
);
759 * Swap and check the attribute name
761 if (direction
== kSwapBTNodeBigToHost
)
762 srcKey
->attrNameLen
= SWAP_BE16(srcKey
->attrNameLen
);
763 /* Sanity check the attribute name length */
764 if (srcKey
->attrNameLen
> kHFSMaxAttrNameLen
|| keyLength
< (kHFSPlusAttrKeyMinimumLength
+ sizeof(u_int16_t
)*srcKey
->attrNameLen
)) {
766 LFHFS_LOG((direction
== kSwapBTNodeHostToBig
) ? LEVEL_ERROR
: LEVEL_DEBUG
, "hfs_swap_HFSPlusBTInternalNode: attr key #%d keyLength=%d attrNameLen=%d\n", srcDesc
->numRecords
-i
-1, keyLength
, srcKey
->attrNameLen
);
767 if (direction
== kSwapBTNodeHostToBig
) {
770 return fsBTInvalidNodeErr
;
772 for (j
= 0; j
< srcKey
->attrNameLen
; j
++)
773 srcKey
->attrName
[j
] = SWAP_BE16(srcKey
->attrName
[j
]);
774 if (direction
== kSwapBTNodeHostToBig
)
775 srcKey
->attrNameLen
= SWAP_BE16(srcKey
->attrNameLen
);
778 * For index nodes, the record data is just the child's node number.
779 * Skip over swapping the various types of attribute record.
781 if (srcDesc
->kind
== kBTIndexNode
) {
782 *((u_int32_t
*)srcRec
) = SWAP_BE32 (*((u_int32_t
*)srcRec
));
786 /* Swap the record data */
787 if (direction
== kSwapBTNodeBigToHost
)
788 srcRec
->recordType
= SWAP_BE32(srcRec
->recordType
);
789 switch (srcRec
->recordType
) {
790 case kHFSPlusAttrInlineData
:
791 /* Is there room for the inline data header? */
792 if ((char *) &srcRec
->attrData
.attrData
[0] > nextRecord
) {
794 LFHFS_LOG((direction
== kSwapBTNodeHostToBig
) ? LEVEL_ERROR
: LEVEL_DEBUG
, "hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big\n", srcDesc
->numRecords
-i
-1);
795 if (direction
== kSwapBTNodeHostToBig
) {
798 return fsBTInvalidNodeErr
;
801 /* We're not swapping the reserved fields */
803 /* Swap the attribute size */
804 if (direction
== kSwapBTNodeHostToBig
)
805 attrSize
= srcRec
->attrData
.attrSize
;
806 srcRec
->attrData
.attrSize
= SWAP_BE32(srcRec
->attrData
.attrSize
);
807 if (direction
== kSwapBTNodeBigToHost
)
808 attrSize
= srcRec
->attrData
.attrSize
;
810 /* Is there room for the inline attribute data? */
811 if ((char *) &srcRec
->attrData
.attrData
[attrSize
] > nextRecord
) {
812 LFHFS_LOG((direction
== kSwapBTNodeHostToBig
) ? LEVEL_ERROR
: LEVEL_DEBUG
, "hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big (attrSize=%u)\n", srcDesc
->numRecords
-i
-1, attrSize
);
813 if (direction
== kSwapBTNodeHostToBig
) {
816 return fsBTInvalidNodeErr
;
819 /* Not swapping the attribute data itself */
822 case kHFSPlusAttrForkData
:
823 /* Is there room for the fork data record? */
824 if ((char *)srcRec
+ sizeof(HFSPlusAttrForkData
) > nextRecord
) {
825 LFHFS_LOG((direction
== kSwapBTNodeHostToBig
) ? LEVEL_ERROR
: LEVEL_DEBUG
, "hfs_swap_HFSPlusBTInternalNode: attr fork data #%d too big\n", srcDesc
->numRecords
-i
-1);
826 if (direction
== kSwapBTNodeHostToBig
) {
829 return fsBTInvalidNodeErr
;
832 /* We're not swapping the reserved field */
834 hfs_swap_HFSPlusForkData(&srcRec
->forkData
.theFork
);
837 case kHFSPlusAttrExtents
:
838 /* Is there room for an extent record? */
839 if ((char *)srcRec
+ sizeof(HFSPlusAttrExtents
) > nextRecord
) {
840 LFHFS_LOG((direction
== kSwapBTNodeHostToBig
) ? LEVEL_ERROR
: LEVEL_DEBUG
, "hfs_swap_HFSPlusBTInternalNode: attr extents #%d too big\n", srcDesc
->numRecords
-i
-1);
841 if (direction
== kSwapBTNodeHostToBig
) {
844 return fsBTInvalidNodeErr
;
847 /* We're not swapping the reserved field */
849 for (j
= 0; j
< kHFSPlusExtentDensity
; j
++) {
850 srcRec
->overflowExtents
.extents
[j
].startBlock
=
851 SWAP_BE32(srcRec
->overflowExtents
.extents
[j
].startBlock
);
852 srcRec
->overflowExtents
.extents
[j
].blockCount
=
853 SWAP_BE32(srcRec
->overflowExtents
.extents
[j
].blockCount
);
857 if (direction
== kSwapBTNodeHostToBig
)
858 srcRec
->recordType
= SWAP_BE32(srcRec
->recordType
);
862 LFHFS_LOG(LEVEL_ERROR
, "hfs_swap_HFSPlusBTInternalNode: fileID %u is not a system B-tree\n", fileID
);