]> git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_endian.c
xnu-1228.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_endian.c
1 /*
2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /*
30 * hfs_endian.c
31 *
32 * This file implements endian swapping routines for the HFS/HFS Plus
33 * volume format.
34 */
35
36 #include "hfs_endian.h"
37 #include "hfs_dbg.h"
38 #include "hfscommon/headers/BTreesPrivate.h"
39
40 #undef ENDIAN_DEBUG
41
42 /*
43 * Internal swapping routines
44 *
45 * These routines handle swapping the records of leaf and index nodes. The
46 * layout of the keys and records varies depending on the kind of B-tree
47 * (determined by fileID).
48 *
49 * The direction parameter must be kSwapBTNodeBigToHost or kSwapBTNodeHostToBig.
50 * The kSwapBTNodeHeaderRecordOnly "direction" is not valid for these routines.
51 */
52 static int hfs_swap_HFSPlusBTInternalNode (BlockDescriptor *src, HFSCatalogNodeID fileID, enum HFSBTSwapDirection direction);
53 static int hfs_swap_HFSBTInternalNode (BlockDescriptor *src, HFSCatalogNodeID fileID, enum HFSBTSwapDirection direction);
54
55 /*
56 * hfs_swap_HFSPlusForkData
57 */
58 static void
59 hfs_swap_HFSPlusForkData (
60 HFSPlusForkData *src
61 )
62 {
63 int i;
64
65 src->logicalSize = SWAP_BE64 (src->logicalSize);
66
67 src->clumpSize = SWAP_BE32 (src->clumpSize);
68 src->totalBlocks = SWAP_BE32 (src->totalBlocks);
69
70 for (i = 0; i < kHFSPlusExtentDensity; i++) {
71 src->extents[i].startBlock = SWAP_BE32 (src->extents[i].startBlock);
72 src->extents[i].blockCount = SWAP_BE32 (src->extents[i].blockCount);
73 }
74 }
75
76 /*
77 * hfs_swap_BTNode
78 *
79 * NOTE: This operation is not naturally symmetric.
80 * We have to determine which way we're swapping things.
81 */
82 int
83 hfs_swap_BTNode (
84 BlockDescriptor *src,
85 vnode_t vp,
86 enum HFSBTSwapDirection direction
87 )
88 {
89 BTNodeDescriptor *srcDesc = src->buffer;
90 u_int16_t *srcOffs = NULL;
91 BTreeControlBlockPtr btcb = (BTreeControlBlockPtr)VTOF(vp)->fcbBTCBPtr;
92 u_int32_t i;
93 int error = 0;
94
95 #ifdef ENDIAN_DEBUG
96 if (direction == kSwapBTNodeBigToHost) {
97 printf ("BE -> Native Swap\n");
98 } else if (direction == kSwapBTNodeHostToBig) {
99 printf ("Native -> BE Swap\n");
100 } else if (direction == kSwapBTNodeHeaderRecordOnly) {
101 printf ("Not swapping descriptors\n");
102 } else {
103 panic ("hfs_swap_BTNode: This is impossible");
104 }
105 #endif
106
107 /*
108 * If we are doing a swap from on-disk to in-memory, then swap the node
109 * descriptor and record offsets before we need to use them.
110 */
111 if (direction == kSwapBTNodeBigToHost) {
112 srcDesc->fLink = SWAP_BE32 (srcDesc->fLink);
113 srcDesc->bLink = SWAP_BE32 (srcDesc->bLink);
114
115 /*
116 * When first opening a BTree, we have to read the header node before the
117 * control block is initialized. In this case, totalNodes will be zero,
118 * so skip the bounds checking.
119 */
120 if (btcb->totalNodes != 0) {
121 if (srcDesc->fLink >= btcb->totalNodes) {
122 printf("hfs_swap_BTNode: invalid forward link (0x%08x >= 0x%08x)\n", srcDesc->fLink, btcb->totalNodes);
123 error = fsBTInvalidHeaderErr;
124 goto fail;
125 }
126 if (srcDesc->bLink >= btcb->totalNodes) {
127 printf("hfs_swap_BTNode: invalid backward link (0x%08x >= 0x%08x)\n", srcDesc->bLink, btcb->totalNodes);
128 error = fsBTInvalidHeaderErr;
129 goto fail;
130 }
131 }
132
133 /*
134 * Check srcDesc->kind. Don't swap it because it's only one byte.
135 */
136 if (srcDesc->kind < kBTLeafNode || srcDesc->kind > kBTMapNode) {
137 printf("hfs_swap_BTNode: invalid node kind (%d)\n", srcDesc->kind);
138 error = fsBTInvalidHeaderErr;
139 goto fail;
140 }
141
142 /*
143 * Check srcDesc->height. Don't swap it because it's only one byte.
144 */
145 if (srcDesc->height > btcb->treeDepth) {
146 printf("hfs_swap_BTNode: invalid node height (%d)\n", srcDesc->height);
147 error = fsBTInvalidHeaderErr;
148 goto fail;
149 }
150
151 /* Don't swap srcDesc->reserved */
152
153 srcDesc->numRecords = SWAP_BE16 (srcDesc->numRecords);
154
155 /*
156 * Swap the node offsets (including the free space one!).
157 */
158 srcOffs = (u_int16_t *)((char *)src->buffer + (src->blockSize - ((srcDesc->numRecords + 1) * sizeof (u_int16_t))));
159
160 /*
161 * Sanity check that the record offsets are within the node itself.
162 */
163 if ((char *)srcOffs > ((char *)src->buffer + src->blockSize) ||
164 (char *)srcOffs < ((char *)src->buffer + sizeof(BTNodeDescriptor))) {
165 printf("hfs_swap_BTNode: invalid record count (0x%04X)\n", srcDesc->numRecords);
166 error = fsBTInvalidHeaderErr;
167 goto fail;
168 }
169
170 /*
171 * Swap and sanity check each of the record offsets.
172 */
173 for (i = 0; i <= srcDesc->numRecords; i++) {
174 srcOffs[i] = SWAP_BE16 (srcOffs[i]);
175
176 /*
177 * Sanity check: must be even, and within the node itself.
178 *
179 * We may be called to swap an unused node, which contains all zeroes.
180 * This is why we allow the record offset to be zero.
181 */
182 if ((srcOffs[i] & 1) || (srcOffs[i] < sizeof(BTNodeDescriptor) && srcOffs[i] != 0) || (srcOffs[i] >= src->blockSize)) {
183 printf("hfs_swap_BTNode: record #%d invalid offset (0x%04X)\n", srcDesc->numRecords-i-1, srcOffs[i]);
184 error = fsBTInvalidHeaderErr;
185 goto fail;
186 }
187
188 /*
189 * Make sure the offsets are strictly increasing. Note that we're looping over
190 * them backwards, hence the order in the comparison.
191 */
192 if ((i != 0) && (srcOffs[i] >= srcOffs[i-1])) {
193 printf("hfs_swap_BTNode: offsets %d and %d out of order (0x%04X, 0x%04X)\n",
194 srcDesc->numRecords-i-1, srcDesc->numRecords-i, srcOffs[i], srcOffs[i-1]);
195 error = fsBTInvalidHeaderErr;
196 goto fail;
197 }
198 }
199 }
200
201 /*
202 * Swap the records (ordered by frequency of access)
203 */
204 if ((srcDesc->kind == kBTIndexNode) ||
205 (srcDesc-> kind == kBTLeafNode)) {
206
207 if (VTOVCB(vp)->vcbSigWord == kHFSPlusSigWord) {
208 error = hfs_swap_HFSPlusBTInternalNode (src, VTOC(vp)->c_fileid, direction);
209 } else {
210 error = hfs_swap_HFSBTInternalNode (src, VTOC(vp)->c_fileid, direction);
211 }
212
213 if (error) goto fail;
214
215 } else if (srcDesc-> kind == kBTMapNode) {
216 /* Don't swap the bitmaps, they'll be done in the bitmap routines */
217
218 } else if (srcDesc-> kind == kBTHeaderNode) {
219 /* The header's offset is hard-wired because we cannot trust the offset pointers. */
220 BTHeaderRec *srcHead = (BTHeaderRec *)((char *)src->buffer + sizeof(BTNodeDescriptor));
221
222 srcHead->treeDepth = SWAP_BE16 (srcHead->treeDepth);
223
224 srcHead->rootNode = SWAP_BE32 (srcHead->rootNode);
225 srcHead->leafRecords = SWAP_BE32 (srcHead->leafRecords);
226 srcHead->firstLeafNode = SWAP_BE32 (srcHead->firstLeafNode);
227 srcHead->lastLeafNode = SWAP_BE32 (srcHead->lastLeafNode);
228
229 srcHead->nodeSize = SWAP_BE16 (srcHead->nodeSize);
230 srcHead->maxKeyLength = SWAP_BE16 (srcHead->maxKeyLength);
231
232 srcHead->totalNodes = SWAP_BE32 (srcHead->totalNodes);
233 srcHead->freeNodes = SWAP_BE32 (srcHead->freeNodes);
234
235 srcHead->clumpSize = SWAP_BE32 (srcHead->clumpSize);
236 srcHead->attributes = SWAP_BE32 (srcHead->attributes);
237
238 /* Don't swap srcHead->reserved1 */
239 /* Don't swap srcHead->btreeType; it's only one byte */
240 /* Don't swap srcHead->reserved2 */
241 /* Don't swap srcHead->reserved3 */
242 /* Don't swap bitmap */
243 }
244
245 /*
246 * If we are doing a swap from in-memory to on-disk, then swap the node
247 * descriptor and record offsets after we're done using them.
248 */
249 if (direction == kSwapBTNodeHostToBig) {
250 /*
251 * Sanity check and swap the forkward and backward links.
252 */
253 if (srcDesc->fLink >= btcb->totalNodes) {
254 printf("hfs_UNswap_BTNode: invalid forward link (0x%08X)\n", srcDesc->fLink);
255 error = fsBTInvalidHeaderErr;
256 goto fail;
257 }
258 if (srcDesc->bLink >= btcb->totalNodes) {
259 printf("hfs_UNswap_BTNode: invalid backward link (0x%08X)\n", srcDesc->bLink);
260 error = fsBTInvalidHeaderErr;
261 goto fail;
262 }
263 srcDesc->fLink = SWAP_BE32 (srcDesc->fLink);
264 srcDesc->bLink = SWAP_BE32 (srcDesc->bLink);
265
266 /*
267 * Check srcDesc->kind. Don't swap it because it's only one byte.
268 */
269 if (srcDesc->kind < kBTLeafNode || srcDesc->kind > kBTMapNode) {
270 printf("hfs_UNswap_BTNode: invalid node kind (%d)\n", srcDesc->kind);
271 error = fsBTInvalidHeaderErr;
272 goto fail;
273 }
274
275 /*
276 * Check srcDesc->height. Don't swap it because it's only one byte.
277 */
278 if (srcDesc->height > btcb->treeDepth) {
279 printf("hfs_UNswap_BTNode: invalid node height (%d)\n", srcDesc->height);
280 error = fsBTInvalidHeaderErr;
281 goto fail;
282 }
283
284 /* Don't swap srcDesc->reserved */
285
286 /*
287 * Swap the node offsets (including the free space one!).
288 */
289 srcOffs = (u_int16_t *)((char *)src->buffer + (src->blockSize - ((srcDesc->numRecords + 1) * sizeof (u_int16_t))));
290
291 /*
292 * Sanity check that the record offsets are within the node itself.
293 */
294 if ((char *)srcOffs > ((char *)src->buffer + src->blockSize) ||
295 (char *)srcOffs < ((char *)src->buffer + sizeof(BTNodeDescriptor))) {
296 printf("hfs_UNswap_BTNode: invalid record count (0x%04X)\n", srcDesc->numRecords);
297 error = fsBTInvalidHeaderErr;
298 goto fail;
299 }
300
301 /*
302 * Swap and sanity check each of the record offsets.
303 */
304 for (i = 0; i <= srcDesc->numRecords; i++) {
305 /*
306 * Sanity check: must be even, and within the node itself.
307 *
308 * We may be called to swap an unused node, which contains all zeroes.
309 * This is why we allow the record offset to be zero.
310 */
311 if ((srcOffs[i] & 1) || (srcOffs[i] < sizeof(BTNodeDescriptor) && srcOffs[i] != 0) || (srcOffs[i] >= src->blockSize)) {
312 printf("hfs_UNswap_BTNode: record #%d invalid offset (0x%04X)\n", srcDesc->numRecords-i-1, srcOffs[i]);
313 error = fsBTInvalidHeaderErr;
314 goto fail;
315 }
316
317 /*
318 * Make sure the offsets are strictly increasing. Note that we're looping over
319 * them backwards, hence the order in the comparison.
320 */
321 if ((i < srcDesc->numRecords) && (srcOffs[i+1] >= srcOffs[i])) {
322 printf("hfs_UNswap_BTNode: offsets %d and %d out of order (0x%04X, 0x%04X)\n",
323 srcDesc->numRecords-i-2, srcDesc->numRecords-i-1, srcOffs[i+1], srcOffs[i]);
324 error = fsBTInvalidHeaderErr;
325 goto fail;
326 }
327
328 srcOffs[i] = SWAP_BE16 (srcOffs[i]);
329 }
330
331 srcDesc->numRecords = SWAP_BE16 (srcDesc->numRecords);
332 }
333
334 fail:
335 if (error) {
336 /*
337 * Log some useful information about where the corrupt node is.
338 */
339 printf("node=%lld fileID=%u volume=%s device=%s\n", src->blockNum, VTOC(vp)->c_fileid,
340 VTOVCB(vp)->vcbVN, vfs_statfs(vnode_mount(vp))->f_mntfromname);
341 hfs_mark_volume_inconsistent(VTOVCB(vp));
342 }
343
344 return (error);
345 }
346
347 static int
348 hfs_swap_HFSPlusBTInternalNode (
349 BlockDescriptor *src,
350 HFSCatalogNodeID fileID,
351 enum HFSBTSwapDirection direction
352 )
353 {
354 BTNodeDescriptor *srcDesc = src->buffer;
355 u_int16_t *srcOffs = (u_int16_t *)((char *)src->buffer + (src->blockSize - (srcDesc->numRecords * sizeof (u_int16_t))));
356 char *nextRecord; /* Points to start of record following current one */
357
358 /*
359 * i is an int32 because it needs to be negative to index the offset to free space.
360 * srcDesc->numRecords is a u_int16_t and is unlikely to become 32-bit so this should be ok.
361 */
362
363 int32_t i;
364 u_int32_t j;
365
366 if (fileID == kHFSExtentsFileID) {
367 HFSPlusExtentKey *srcKey;
368 HFSPlusExtentDescriptor *srcRec;
369 size_t recordSize; /* Size of the data part of the record, or node number for index nodes */
370
371 if (srcDesc->kind == kBTIndexNode)
372 recordSize = sizeof(u_int32_t);
373 else
374 recordSize = sizeof(HFSPlusExtentDescriptor);
375
376 for (i = 0; i < srcDesc->numRecords; i++) {
377 /* Point to the start of the record we're currently checking. */
378 srcKey = (HFSPlusExtentKey *)((char *)src->buffer + srcOffs[i]);
379
380 /*
381 * Point to start of next (larger offset) record. We'll use this
382 * to be sure the current record doesn't overflow into the next
383 * record.
384 */
385 nextRecord = (char *)src->buffer + srcOffs[i-1];
386
387 /*
388 * Make sure the key and data are within the buffer. Since both key
389 * and data are fixed size, this is relatively easy. Note that this
390 * relies on the keyLength being a constant; we verify the keyLength
391 * below.
392 */
393 if ((char *)srcKey + sizeof(HFSPlusExtentKey) + recordSize > nextRecord) {
394 printf("hfs_swap_HFSPlusBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc->numRecords-i-1, srcOffs[i]);
395 return fsBTInvalidNodeErr;
396 }
397
398 if (direction == kSwapBTNodeBigToHost)
399 srcKey->keyLength = SWAP_BE16 (srcKey->keyLength);
400 if (srcKey->keyLength != sizeof(*srcKey) - sizeof(srcKey->keyLength)) {
401 printf("hfs_swap_HFSPlusBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc->numRecords-i-1, srcKey->keyLength);
402 return fsBTInvalidNodeErr;
403 }
404 srcRec = (HFSPlusExtentDescriptor *)((char *)srcKey + srcKey->keyLength + sizeof(srcKey->keyLength));
405 if (direction == kSwapBTNodeHostToBig)
406 srcKey->keyLength = SWAP_BE16 (srcKey->keyLength);
407
408 /* Don't swap srcKey->forkType; it's only one byte */
409 /* Don't swap srcKey->pad */
410
411 srcKey->fileID = SWAP_BE32 (srcKey->fileID);
412 srcKey->startBlock = SWAP_BE32 (srcKey->startBlock);
413
414 if (srcDesc->kind == kBTIndexNode) {
415 /* For index nodes, the record data is just a child node number. */
416 *((u_int32_t *)srcRec) = SWAP_BE32 (*((u_int32_t *)srcRec));
417 } else {
418 /* Swap the extent data */
419 for (j = 0; j < kHFSPlusExtentDensity; j++) {
420 srcRec[j].startBlock = SWAP_BE32 (srcRec[j].startBlock);
421 srcRec[j].blockCount = SWAP_BE32 (srcRec[j].blockCount);
422 }
423 }
424 }
425
426 } else if (fileID == kHFSCatalogFileID) {
427 HFSPlusCatalogKey *srcKey;
428 int16_t *srcPtr;
429 u_int16_t keyLength;
430
431 for (i = 0; i < srcDesc->numRecords; i++) {
432 /* Point to the start of the record we're currently checking. */
433 srcKey = (HFSPlusCatalogKey *)((char *)src->buffer + srcOffs[i]);
434
435 /*
436 * Point to start of next (larger offset) record. We'll use this
437 * to be sure the current record doesn't overflow into the next
438 * record.
439 */
440 nextRecord = (char *)src->buffer + srcOffs[i-1];
441
442 /*
443 * Make sure we can safely dereference the keyLength and parentID fields. */
444 if ((char *)srcKey + offsetof(HFSPlusCatalogKey, nodeName.unicode[0]) > nextRecord) {
445 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc->numRecords-i-1, srcOffs[i]);
446 return fsBTInvalidNodeErr;
447 }
448
449 /*
450 * Swap and sanity check the key length
451 */
452 if (direction == kSwapBTNodeBigToHost)
453 srcKey->keyLength = SWAP_BE16 (srcKey->keyLength);
454 keyLength = srcKey->keyLength; /* Put it in a local (native order) because we use it several times */
455 if (direction == kSwapBTNodeHostToBig)
456 srcKey->keyLength = SWAP_BE16 (keyLength);
457
458 /* Sanity check the key length */
459 if (keyLength < kHFSPlusCatalogKeyMinimumLength || keyLength > kHFSPlusCatalogKeyMaximumLength) {
460 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc->numRecords-i-1, keyLength);
461 return fsBTInvalidNodeErr;
462 }
463
464 /*
465 * Make sure that we can safely dereference the record's type field or
466 * an index node's child node number.
467 */
468 srcPtr = (int16_t *)((char *)srcKey + keyLength + sizeof(srcKey->keyLength));
469 if ((char *)srcPtr + sizeof(u_int32_t) > nextRecord) {
470 printf("hfs_swap_HFSPlusBTInternalNode: catalog key #%d too big\n", srcDesc->numRecords-i-1);
471 return fsBTInvalidNodeErr;
472 }
473
474 srcKey->parentID = SWAP_BE32 (srcKey->parentID);
475
476 /*
477 * Swap and sanity check the key's node name
478 */
479 if (direction == kSwapBTNodeBigToHost)
480 srcKey->nodeName.length = SWAP_BE16 (srcKey->nodeName.length);
481 /* Make sure name length is consistent with key length */
482 if (keyLength < sizeof(srcKey->parentID) + sizeof(srcKey->nodeName.length) +
483 srcKey->nodeName.length*sizeof(srcKey->nodeName.unicode[0])) {
484 printf("hfs_swap_HFSPlusBTInternalNode: catalog record #%d keyLength=%d expected=%lu\n",
485 srcDesc->numRecords-i, keyLength, sizeof(srcKey->parentID) + sizeof(srcKey->nodeName.length) +
486 srcKey->nodeName.length*sizeof(srcKey->nodeName.unicode[0]));
487 return fsBTInvalidNodeErr;
488 }
489 for (j = 0; j < srcKey->nodeName.length; j++) {
490 srcKey->nodeName.unicode[j] = SWAP_BE16 (srcKey->nodeName.unicode[j]);
491 }
492 if (direction == kSwapBTNodeHostToBig)
493 srcKey->nodeName.length = SWAP_BE16 (srcKey->nodeName.length);
494
495 /*
496 * For index nodes, the record data is just the child's node number.
497 * Skip over swapping the various types of catalog record.
498 */
499 if (srcDesc->kind == kBTIndexNode) {
500 *((u_int32_t *)srcPtr) = SWAP_BE32 (*((u_int32_t *)srcPtr));
501 continue;
502 }
503
504 /* Make sure the recordType is in native order before using it. */
505 if (direction == kSwapBTNodeBigToHost)
506 srcPtr[0] = SWAP_BE16 (srcPtr[0]);
507
508 if (srcPtr[0] == kHFSPlusFolderRecord) {
509 HFSPlusCatalogFolder *srcRec = (HFSPlusCatalogFolder *)srcPtr;
510 if ((char *)srcRec + sizeof(*srcRec) > nextRecord) {
511 printf("hfs_swap_HFSPlusBTInternalNode: catalog folder record #%d too big\n", srcDesc->numRecords-i-1);
512 return fsBTInvalidNodeErr;
513 }
514
515 srcRec->flags = SWAP_BE16 (srcRec->flags);
516 srcRec->valence = SWAP_BE32 (srcRec->valence);
517 srcRec->folderID = SWAP_BE32 (srcRec->folderID);
518 srcRec->createDate = SWAP_BE32 (srcRec->createDate);
519 srcRec->contentModDate = SWAP_BE32 (srcRec->contentModDate);
520 srcRec->attributeModDate = SWAP_BE32 (srcRec->attributeModDate);
521 srcRec->accessDate = SWAP_BE32 (srcRec->accessDate);
522 srcRec->backupDate = SWAP_BE32 (srcRec->backupDate);
523
524 srcRec->bsdInfo.ownerID = SWAP_BE32 (srcRec->bsdInfo.ownerID);
525 srcRec->bsdInfo.groupID = SWAP_BE32 (srcRec->bsdInfo.groupID);
526
527 /* Don't swap srcRec->bsdInfo.adminFlags; it's only one byte */
528 /* Don't swap srcRec->bsdInfo.ownerFlags; it's only one byte */
529
530 srcRec->bsdInfo.fileMode = SWAP_BE16 (srcRec->bsdInfo.fileMode);
531 srcRec->bsdInfo.special.iNodeNum = SWAP_BE32 (srcRec->bsdInfo.special.iNodeNum);
532
533 srcRec->textEncoding = SWAP_BE32 (srcRec->textEncoding);
534
535 /* Don't swap srcRec->userInfo */
536 /* Don't swap srcRec->finderInfo */
537 srcRec->folderCount = SWAP_BE32 (srcRec->folderCount);
538
539 } else if (srcPtr[0] == kHFSPlusFileRecord) {
540 HFSPlusCatalogFile *srcRec = (HFSPlusCatalogFile *)srcPtr;
541 if ((char *)srcRec + sizeof(*srcRec) > nextRecord) {
542 printf("hfs_swap_HFSPlusBTInternalNode: catalog file record #%d too big\n", srcDesc->numRecords-i-1);
543 return fsBTInvalidNodeErr;
544 }
545
546 srcRec->flags = SWAP_BE16 (srcRec->flags);
547
548 srcRec->fileID = SWAP_BE32 (srcRec->fileID);
549
550 srcRec->createDate = SWAP_BE32 (srcRec->createDate);
551 srcRec->contentModDate = SWAP_BE32 (srcRec->contentModDate);
552 srcRec->attributeModDate = SWAP_BE32 (srcRec->attributeModDate);
553 srcRec->accessDate = SWAP_BE32 (srcRec->accessDate);
554 srcRec->backupDate = SWAP_BE32 (srcRec->backupDate);
555
556 srcRec->bsdInfo.ownerID = SWAP_BE32 (srcRec->bsdInfo.ownerID);
557 srcRec->bsdInfo.groupID = SWAP_BE32 (srcRec->bsdInfo.groupID);
558
559 /* Don't swap srcRec->bsdInfo.adminFlags; it's only one byte */
560 /* Don't swap srcRec->bsdInfo.ownerFlags; it's only one byte */
561
562 srcRec->bsdInfo.fileMode = SWAP_BE16 (srcRec->bsdInfo.fileMode);
563 srcRec->bsdInfo.special.iNodeNum = SWAP_BE32 (srcRec->bsdInfo.special.iNodeNum);
564
565 srcRec->textEncoding = SWAP_BE32 (srcRec->textEncoding);
566
567 /* If kHFSHasLinkChainBit is set, reserved1 is hl_FirstLinkID.
568 * In all other context, it is expected to be zero.
569 */
570 srcRec->reserved1 = SWAP_BE32 (srcRec->reserved1);
571
572 /* Don't swap srcRec->userInfo */
573 /* Don't swap srcRec->finderInfo */
574 /* Don't swap srcRec->reserved2 */
575
576 hfs_swap_HFSPlusForkData (&srcRec->dataFork);
577 hfs_swap_HFSPlusForkData (&srcRec->resourceFork);
578
579 } else if ((srcPtr[0] == kHFSPlusFolderThreadRecord) ||
580 (srcPtr[0] == kHFSPlusFileThreadRecord)) {
581
582 /*
583 * Make sure there is room for parentID and name length.
584 */
585 HFSPlusCatalogThread *srcRec = (HFSPlusCatalogThread *)srcPtr;
586 if ((char *) &srcRec->nodeName.unicode[0] > nextRecord) {
587 printf("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d too big\n", srcDesc->numRecords-i-1);
588 return fsBTInvalidNodeErr;
589 }
590
591 /* Don't swap srcRec->reserved */
592
593 srcRec->parentID = SWAP_BE32 (srcRec->parentID);
594
595 if (direction == kSwapBTNodeBigToHost)
596 srcRec->nodeName.length = SWAP_BE16 (srcRec->nodeName.length);
597
598 /*
599 * Make sure there is room for the name in the buffer.
600 * Then swap the characters of the name itself.
601 */
602 if ((char *) &srcRec->nodeName.unicode[srcRec->nodeName.length] > nextRecord) {
603 printf("hfs_swap_HFSPlusBTInternalNode: catalog thread record #%d name too big\n", srcDesc->numRecords-i-1);
604 return fsBTInvalidNodeErr;
605 }
606 for (j = 0; j < srcRec->nodeName.length; j++) {
607 srcRec->nodeName.unicode[j] = SWAP_BE16 (srcRec->nodeName.unicode[j]);
608 }
609
610 if (direction == kSwapBTNodeHostToBig)
611 srcRec->nodeName.length = SWAP_BE16 (srcRec->nodeName.length);
612
613 } else {
614 printf("hfs_swap_HFSPlusBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr[0], srcDesc->numRecords-i-1);
615 return fsBTInvalidNodeErr;
616 }
617
618 /* We can swap the record type now that we're done using it. */
619 if (direction == kSwapBTNodeHostToBig)
620 srcPtr[0] = SWAP_BE16 (srcPtr[0]);
621 }
622
623 } else if (fileID == kHFSAttributesFileID) {
624 HFSPlusAttrKey *srcKey;
625 HFSPlusAttrRecord *srcRec;
626 u_int16_t keyLength;
627 u_int32_t attrSize = 0;
628
629 for (i = 0; i < srcDesc->numRecords; i++) {
630 /* Point to the start of the record we're currently checking. */
631 srcKey = (HFSPlusAttrKey *)((char *)src->buffer + srcOffs[i]);
632
633 /*
634 * Point to start of next (larger offset) record. We'll use this
635 * to be sure the current record doesn't overflow into the next
636 * record.
637 */
638 nextRecord = (char *)src->buffer + srcOffs[i-1];
639
640 /* Make sure there is room in the buffer for a minimal key */
641 if ((char *) &srcKey->attrName[1] > nextRecord) {
642 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d offset too big (0x%04X)\n", srcDesc->numRecords-i-1, srcOffs[i]);
643 return fsBTInvalidNodeErr;
644 }
645
646 /* Swap the key length field */
647 if (direction == kSwapBTNodeBigToHost)
648 srcKey->keyLength = SWAP_BE16(srcKey->keyLength);
649 keyLength = srcKey->keyLength; /* Keep a copy in native order */
650 if (direction == kSwapBTNodeHostToBig)
651 srcKey->keyLength = SWAP_BE16(srcKey->keyLength);
652
653 /*
654 * Make sure that we can safely dereference the record's type field or
655 * an index node's child node number.
656 */
657 srcRec = (HFSPlusAttrRecord *)((char *)srcKey + keyLength + sizeof(srcKey->keyLength));
658 if ((char *)srcRec + sizeof(u_int32_t) > nextRecord) {
659 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d too big (%d)\n", srcDesc->numRecords-i-1, keyLength);
660 return fsBTInvalidNodeErr;
661 }
662
663 srcKey->fileID = SWAP_BE32(srcKey->fileID);
664 srcKey->startBlock = SWAP_BE32(srcKey->startBlock);
665
666 /*
667 * Swap and check the attribute name
668 */
669 if (direction == kSwapBTNodeBigToHost)
670 srcKey->attrNameLen = SWAP_BE16(srcKey->attrNameLen);
671 /* Sanity check the attribute name length */
672 if (srcKey->attrNameLen > kHFSMaxAttrNameLen || keyLength < (kHFSPlusAttrKeyMinimumLength + sizeof(u_int16_t)*srcKey->attrNameLen)) {
673 printf("hfs_swap_HFSPlusBTInternalNode: attr key #%d keyLength=%d attrNameLen=%d\n", srcDesc->numRecords-i-1, keyLength, srcKey->attrNameLen);
674 return fsBTInvalidNodeErr;
675 }
676 for (j = 0; j < srcKey->attrNameLen; j++)
677 srcKey->attrName[j] = SWAP_BE16(srcKey->attrName[j]);
678 if (direction == kSwapBTNodeHostToBig)
679 srcKey->attrNameLen = SWAP_BE16(srcKey->attrNameLen);
680
681 /*
682 * For index nodes, the record data is just the child's node number.
683 * Skip over swapping the various types of attribute record.
684 */
685 if (srcDesc->kind == kBTIndexNode) {
686 *((u_int32_t *)srcRec) = SWAP_BE32 (*((u_int32_t *)srcRec));
687 continue;
688 }
689
690 /* Swap the record data */
691 if (direction == kSwapBTNodeBigToHost)
692 srcRec->recordType = SWAP_BE32(srcRec->recordType);
693 switch (srcRec->recordType) {
694 case kHFSPlusAttrInlineData:
695 /* Is there room for the inline data header? */
696 if ((char *) &srcRec->attrData.attrData[0] > nextRecord) {
697 printf("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big\n", srcDesc->numRecords-i-1);
698 return fsBTInvalidNodeErr;
699 }
700
701 /* We're not swapping the reserved fields */
702
703 /* Swap the attribute size */
704 if (direction == kSwapBTNodeHostToBig)
705 attrSize = srcRec->attrData.attrSize;
706 srcRec->attrData.attrSize = SWAP_BE32(srcRec->attrData.attrSize);
707 if (direction == kSwapBTNodeBigToHost)
708 attrSize = srcRec->attrData.attrSize;
709
710 /* Is there room for the inline attribute data? */
711 if ((char *) &srcRec->attrData.attrData[attrSize] > nextRecord) {
712 printf("hfs_swap_HFSPlusBTInternalNode: attr inline #%d too big (attrSize=%u)\n", srcDesc->numRecords-i-1, attrSize);
713 return fsBTInvalidNodeErr;
714 }
715
716 /* Not swapping the attribute data itself */
717 break;
718
719 case kHFSPlusAttrForkData:
720 /* Is there room for the fork data record? */
721 if ((char *)srcRec + sizeof(HFSPlusAttrForkData) > nextRecord) {
722 printf("hfs_swap_HFSPlusBTInternalNode: attr fork data #%d too big\n", srcDesc->numRecords-i-1);
723 return fsBTInvalidNodeErr;
724 }
725
726 /* We're not swapping the reserved field */
727
728 hfs_swap_HFSPlusForkData(&srcRec->forkData.theFork);
729 break;
730
731 case kHFSPlusAttrExtents:
732 /* Is there room for an extent record? */
733 if ((char *)srcRec + sizeof(HFSPlusAttrExtents) > nextRecord) {
734 printf("hfs_swap_HFSPlusBTInternalNode: attr extents #%d too big\n", srcDesc->numRecords-i-1);
735 return fsBTInvalidNodeErr;
736 }
737
738 /* We're not swapping the reserved field */
739
740 for (j = 0; j < kHFSPlusExtentDensity; j++) {
741 srcRec->overflowExtents.extents[j].startBlock =
742 SWAP_BE32(srcRec->overflowExtents.extents[j].startBlock);
743 srcRec->overflowExtents.extents[j].blockCount =
744 SWAP_BE32(srcRec->overflowExtents.extents[j].blockCount);
745 }
746 break;
747 }
748 if (direction == kSwapBTNodeHostToBig)
749 srcRec->recordType = SWAP_BE32(srcRec->recordType);
750 }
751 } else if (fileID > kHFSFirstUserCatalogNodeID) {
752 /* The only B-tree with a non-system CNID that we use is the hotfile B-tree */
753 HotFileKey *srcKey;
754 u_int32_t *srcRec;
755
756 for (i = 0; i < srcDesc->numRecords; i++) {
757 /* Point to the start of the record we're currently checking. */
758 srcKey = (HotFileKey *)((char *)src->buffer + srcOffs[i]);
759
760 /*
761 * Point to start of next (larger offset) record. We'll use this
762 * to be sure the current record doesn't overflow into the next
763 * record.
764 */
765 nextRecord = (char *)src->buffer + srcOffs[i-1];
766
767 /* Make sure there is room for the key (HotFileKey) and data (u_int32_t) */
768 if ((char *)srcKey + sizeof(HotFileKey) + sizeof(u_int32_t) > nextRecord) {
769 printf("hfs_swap_HFSPlusBTInternalNode: hotfile #%d offset too big (0x%04X)\n", srcDesc->numRecords-i-1, srcOffs[i]);
770 return fsBTInvalidNodeErr;
771 }
772
773 /* Swap and sanity check the key length field */
774 if (direction == kSwapBTNodeBigToHost)
775 srcKey->keyLength = SWAP_BE16 (srcKey->keyLength);
776 if (srcKey->keyLength != sizeof(*srcKey) - sizeof(srcKey->keyLength)) {
777 printf("hfs_swap_HFSPlusBTInternalNode: hotfile #%d incorrect keyLength %d\n", srcDesc->numRecords-i-1, srcKey->keyLength);
778 return fsBTInvalidNodeErr;
779 }
780 srcRec = (u_int32_t *)((char *)srcKey + srcKey->keyLength + sizeof(srcKey->keyLength));
781 if (direction == kSwapBTNodeHostToBig)
782 srcKey->keyLength = SWAP_BE16 (srcKey->keyLength);
783
784 /* Don't swap srcKey->forkType */
785 /* Don't swap srcKey->pad */
786
787 srcKey->temperature = SWAP_BE32 (srcKey->temperature);
788 srcKey->fileID = SWAP_BE32 (srcKey->fileID);
789
790 *((u_int32_t *)srcRec) = SWAP_BE32 (*((u_int32_t *)srcRec));
791 }
792 } else {
793 panic ("hfs_swap_HFSPlusBTInternalNode: fileID %u is not a system B-tree\n", fileID);
794 }
795
796
797 return (0);
798 }
799
800 static int
801 hfs_swap_HFSBTInternalNode (
802 BlockDescriptor *src,
803 HFSCatalogNodeID fileID,
804 enum HFSBTSwapDirection direction
805 )
806 {
807 BTNodeDescriptor *srcDesc = src->buffer;
808 u_int16_t *srcOffs = (u_int16_t *)((char *)src->buffer + (src->blockSize - (srcDesc->numRecords * sizeof (u_int16_t))));
809 char *nextRecord; /* Points to start of record following current one */
810
811 /*
812 * i is an int32 because it needs to be negative to index the offset to free space.
813 * srcDesc->numRecords is a u_int16_t and is unlikely to become 32-bit so this should be ok.
814 */
815 int32_t i;
816 u_int32_t j;
817
818 if (fileID == kHFSExtentsFileID) {
819 HFSExtentKey *srcKey;
820 HFSExtentDescriptor *srcRec;
821 size_t recordSize; /* Size of the data part of the record, or node number for index nodes */
822
823 if (srcDesc->kind == kBTIndexNode)
824 recordSize = sizeof(u_int32_t);
825 else
826 recordSize = sizeof(HFSExtentDescriptor);
827
828 for (i = 0; i < srcDesc->numRecords; i++) {
829 /* Point to the start of the record we're currently checking. */
830 srcKey = (HFSExtentKey *)((char *)src->buffer + srcOffs[i]);
831
832 /*
833 * Point to start of next (larger offset) record. We'll use this
834 * to be sure the current record doesn't overflow into the next
835 * record.
836 */
837 nextRecord = (char *)src->buffer + srcOffs[i-1];
838
839 /*
840 * Make sure the key and data are within the buffer. Since both key
841 * and data are fixed size, this is relatively easy. Note that this
842 * relies on the keyLength being a constant; we verify the keyLength
843 * below.
844 */
845 if ((char *)srcKey + sizeof(HFSExtentKey) + recordSize > nextRecord) {
846 printf("hfs_swap_HFSBTInternalNode: extents key #%d offset too big (0x%04X)\n", srcDesc->numRecords-i-1, srcOffs[i]);
847 return fsBTInvalidNodeErr;
848 }
849
850 /* Don't swap srcKey->keyLength (it's only one byte), but do sanity check it */
851 if (srcKey->keyLength != sizeof(*srcKey) - sizeof(srcKey->keyLength)) {
852 printf("hfs_swap_HFSBTInternalNode: extents key #%d invalid length (%d)\n", srcDesc->numRecords-i-1, srcKey->keyLength);
853 return fsBTInvalidNodeErr;
854 }
855
856 /* Don't swap srcKey->forkType; it's only one byte */
857
858 srcKey->fileID = SWAP_BE32 (srcKey->fileID);
859 srcKey->startBlock = SWAP_BE16 (srcKey->startBlock);
860
861 /* Point to record data (round up to even byte boundary) */
862 srcRec = (HFSExtentDescriptor *)((char *)srcKey + ((srcKey->keyLength + 2) & ~1));
863
864 if (srcDesc->kind == kBTIndexNode) {
865 /* For index nodes, the record data is just a child node number. */
866 *((u_int32_t *)srcRec) = SWAP_BE32 (*((u_int32_t *)srcRec));
867 } else {
868 /* Swap the extent data */
869 for (j = 0; j < kHFSExtentDensity; j++) {
870 srcRec[j].startBlock = SWAP_BE16 (srcRec[j].startBlock);
871 srcRec[j].blockCount = SWAP_BE16 (srcRec[j].blockCount);
872 }
873 }
874 }
875
876 } else if (fileID == kHFSCatalogFileID) {
877 HFSCatalogKey *srcKey;
878 int16_t *srcPtr;
879 unsigned expectedKeyLength;
880
881 for (i = 0; i < srcDesc->numRecords; i++) {
882 /* Point to the start of the record we're currently checking. */
883 srcKey = (HFSCatalogKey *)((char *)src->buffer + srcOffs[i]);
884
885 /*
886 * Point to start of next (larger offset) record. We'll use this
887 * to be sure the current record doesn't overflow into the next
888 * record.
889 */
890 nextRecord = (char *)src->buffer + srcOffs[i-1];
891
892 /*
893 * Make sure we can safely dereference the keyLength and parentID fields.
894 * The value 8 below is 1 bytes for keyLength + 1 byte reserved + 4 bytes
895 * for parentID + 1 byte for nodeName's length + 1 byte to round up the
896 * record start to an even offset, which forms a minimal key.
897 */
898 if ((char *)srcKey + 8 > nextRecord) {
899 printf("hfs_swap_HFSBTInternalNode: catalog key #%d offset too big (0x%04X)\n", srcDesc->numRecords-i-1, srcOffs[i]);
900 return fsBTInvalidNodeErr;
901 }
902
903 /* Don't swap srcKey->keyLength (it's only one byte), but do sanity check it */
904 if (srcKey->keyLength < kHFSCatalogKeyMinimumLength || srcKey->keyLength > kHFSCatalogKeyMaximumLength) {
905 printf("hfs_swap_HFSBTInternalNode: catalog key #%d invalid length (%d)\n", srcDesc->numRecords-i-1, srcKey->keyLength);
906 return fsBTInvalidNodeErr;
907 }
908
909 /* Don't swap srcKey->reserved */
910
911 srcKey->parentID = SWAP_BE32 (srcKey->parentID);
912
913 /* Don't swap srcKey->nodeName */
914
915 /* Make sure the keyLength is big enough for the key's content */
916 if (srcDesc->kind == kBTIndexNode)
917 expectedKeyLength = sizeof(*srcKey) - sizeof(srcKey->keyLength);
918 else
919 expectedKeyLength = srcKey->nodeName[0] + kHFSCatalogKeyMinimumLength;
920 if (srcKey->keyLength < expectedKeyLength) {
921 printf("hfs_swap_HFSBTInternalNode: catalog record #%d keyLength=%u expected=%u\n",
922 srcDesc->numRecords-i, srcKey->keyLength, expectedKeyLength);
923 return fsBTInvalidNodeErr;
924 }
925
926 /* Point to record data (round up to even byte boundary) */
927 srcPtr = (int16_t *)((char *)srcKey + ((srcKey->keyLength + 2) & ~1));
928
929 /*
930 * Make sure that we can safely dereference the record's type field or
931 * and index node's child node number.
932 */
933 if ((char *)srcPtr + sizeof(u_int32_t) > nextRecord) {
934 printf("hfs_swap_HFSBTInternalNode: catalog key #%d too big\n", srcDesc->numRecords-i-1);
935 return fsBTInvalidNodeErr;
936 }
937
938 /*
939 * For index nodes, the record data is just the child's node number.
940 * Skip over swapping the various types of catalog record.
941 */
942 if (srcDesc->kind == kBTIndexNode) {
943 *((u_int32_t *)srcPtr) = SWAP_BE32 (*((u_int32_t *)srcPtr));
944 continue;
945 }
946
947 /* Make sure the recordType is in native order before using it. */
948 if (direction == kSwapBTNodeBigToHost)
949 srcPtr[0] = SWAP_BE16 (srcPtr[0]);
950
951 if (srcPtr[0] == kHFSFolderRecord) {
952 HFSCatalogFolder *srcRec = (HFSCatalogFolder *)srcPtr;
953 if ((char *)srcRec + sizeof(*srcRec) > nextRecord) {
954 printf("hfs_swap_HFSBTInternalNode: catalog folder record #%d too big\n", srcDesc->numRecords-i-1);
955 return fsBTInvalidNodeErr;
956 }
957
958 srcRec->flags = SWAP_BE16 (srcRec->flags);
959 srcRec->valence = SWAP_BE16 (srcRec->valence);
960
961 srcRec->folderID = SWAP_BE32 (srcRec->folderID);
962 srcRec->createDate = SWAP_BE32 (srcRec->createDate);
963 srcRec->modifyDate = SWAP_BE32 (srcRec->modifyDate);
964 srcRec->backupDate = SWAP_BE32 (srcRec->backupDate);
965
966 /* Don't swap srcRec->userInfo */
967 /* Don't swap srcRec->finderInfo */
968 /* Don't swap resserved array */
969
970 } else if (srcPtr[0] == kHFSFileRecord) {
971 HFSCatalogFile *srcRec = (HFSCatalogFile *)srcPtr;
972 if ((char *)srcRec + sizeof(*srcRec) > nextRecord) {
973 printf("hfs_swap_HFSBTInternalNode: catalog file record #%d too big\n", srcDesc->numRecords-i-1);
974 return fsBTInvalidNodeErr;
975 }
976
977 srcRec->flags = srcRec->flags;
978 srcRec->fileType = srcRec->fileType;
979
980 /* Don't swap srcRec->userInfo */
981
982 srcRec->fileID = SWAP_BE32 (srcRec->fileID);
983
984 srcRec->dataStartBlock = SWAP_BE16 (srcRec->dataStartBlock);
985 srcRec->dataLogicalSize = SWAP_BE32 (srcRec->dataLogicalSize);
986 srcRec->dataPhysicalSize = SWAP_BE32 (srcRec->dataPhysicalSize);
987
988 srcRec->rsrcStartBlock = SWAP_BE16 (srcRec->rsrcStartBlock);
989 srcRec->rsrcLogicalSize = SWAP_BE32 (srcRec->rsrcLogicalSize);
990 srcRec->rsrcPhysicalSize = SWAP_BE32 (srcRec->rsrcPhysicalSize);
991
992 srcRec->createDate = SWAP_BE32 (srcRec->createDate);
993 srcRec->modifyDate = SWAP_BE32 (srcRec->modifyDate);
994 srcRec->backupDate = SWAP_BE32 (srcRec->backupDate);
995
996 /* Don't swap srcRec->finderInfo */
997
998 srcRec->clumpSize = SWAP_BE16 (srcRec->clumpSize);
999
1000 /* Swap the two sets of extents as an array of six (three each) u_int16_t */
1001 for (j = 0; j < kHFSExtentDensity * 2; j++) {
1002 srcRec->dataExtents[j].startBlock = SWAP_BE16 (srcRec->dataExtents[j].startBlock);
1003 srcRec->dataExtents[j].blockCount = SWAP_BE16 (srcRec->dataExtents[j].blockCount);
1004 }
1005
1006 /* Don't swap srcRec->reserved */
1007
1008 } else if ((srcPtr[0] == kHFSFolderThreadRecord) ||
1009 (srcPtr[0] == kHFSFileThreadRecord)) {
1010 HFSCatalogThread *srcRec = (HFSCatalogThread *)srcPtr;
1011
1012 /* Make sure there is room for parentID and name length */
1013 if ((char *) &srcRec->nodeName[1] > nextRecord) {
1014 printf("hfs_swap_HFSBTInternalNode: catalog thread record #%d too big\n", srcDesc->numRecords-i-1);
1015 return fsBTInvalidNodeErr;
1016 }
1017
1018 /* Don't swap srcRec->reserved array */
1019
1020 srcRec->parentID = SWAP_BE32 (srcRec->parentID);
1021
1022 /* Don't swap srcRec->nodeName */
1023
1024 /* Make sure there is room for the name in the buffer */
1025 if ((char *) &srcRec->nodeName[srcRec->nodeName[0]] > nextRecord) {
1026 printf("hfs_swap_HFSBTInternalNode: catalog thread record #%d name too big\n", srcDesc->numRecords-i-1);
1027 return fsBTInvalidNodeErr;
1028 }
1029 } else {
1030 printf("hfs_swap_HFSBTInternalNode: unrecognized catalog record type (0x%04X; record #%d)\n", srcPtr[0], srcDesc->numRecords-i-1);
1031 return fsBTInvalidNodeErr;
1032 }
1033
1034 /* We can swap the record type now that we're done using it */
1035 if (direction == kSwapBTNodeHostToBig)
1036 srcPtr[0] = SWAP_BE16 (srcPtr[0]);
1037 }
1038
1039 } else {
1040 panic ("hfs_swap_HFSBTInternalNode: fileID %u is not a system B-tree\n", fileID);
1041 }
1042
1043 return (0);
1044 }