]> git.saurik.com Git - apple/xnu.git/blame - bsd/hfs/hfs_endian.c
xnu-517.7.7.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_endian.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
e5568f75
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
1c79356b 11 *
e5568f75
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23/*
24 * hfs_endian.c
25 *
26 * This file implements endian swapping routines for the HFS/HFS Plus
27 * volume format.
28 */
29
30#include <architecture/byte_order.h>
31
32#include "hfs_endian.h"
33#include "hfs_dbg.h"
34
35#undef ENDIAN_DEBUG
36
37/* Private swapping routines */
38int hfs_swap_HFSPlusBTInternalNode (BlockDescriptor *src, HFSCatalogNodeID fileID, int unswap);
39int hfs_swap_HFSBTInternalNode (BlockDescriptor *src, HFSCatalogNodeID fileID, int unswap);
40
41/*
42 * hfs_swap_HFSPlusForkData
43 *
44 * There's still a few spots where we still need to swap the fork data.
45 */
46void
47hfs_swap_HFSPlusForkData (
48 HFSPlusForkData *src
49)
50{
51 int i;
52
1c79356b
A
53 src->logicalSize = SWAP_BE64 (src->logicalSize);
54
55 src->clumpSize = SWAP_BE32 (src->clumpSize);
56 src->totalBlocks = SWAP_BE32 (src->totalBlocks);
57
58 for (i = 0; i < kHFSPlusExtentDensity; i++) {
59 src->extents[i].startBlock = SWAP_BE32 (src->extents[i].startBlock);
60 src->extents[i].blockCount = SWAP_BE32 (src->extents[i].blockCount);
61 }
62}
63
64/*
65 * hfs_swap_BTNode
66 *
67 * NOTE: This operation is not naturally symmetric.
68 * We have to determine which way we're swapping things.
69 */
70int
71hfs_swap_BTNode (
72 BlockDescriptor *src,
73 int isHFSPlus,
74 HFSCatalogNodeID fileID,
75 int unswap
76)
77{
78 BTNodeDescriptor *srcDesc = src->buffer;
79 UInt16 *srcOffs = NULL;
80
81 UInt32 i;
82 int error = 0;
83
1c79356b
A
84
85#ifdef ENDIAN_DEBUG
86 if (unswap == 0) {
87 printf ("BE -> LE Swap\n");
88 } else if (unswap == 1) {
89 printf ("LE -> BE Swap\n");
90 } else if (unswap == 3) {
91 printf ("Not swapping descriptors\n");
92 } else {
93 panic ("%s This is impossible", "hfs_swap_BTNode:");
94 }
95#endif
96
97 /* If we are doing a swap */
98 if (unswap == 0) {
99 /* Swap the node descriptor */
100 srcDesc->fLink = SWAP_BE32 (srcDesc->fLink);
101 srcDesc->bLink = SWAP_BE32 (srcDesc->bLink);
102
103 /* Don't swap srcDesc->kind */
104 /* Don't swap srcDesc->height */
105 /* Don't swap srcDesc->reserved */
106
107 srcDesc->numRecords = SWAP_BE16 (srcDesc->numRecords);
108
109 /* Swap the node offsets (including the free space one!) */
110 srcOffs = (UInt16 *)((char *)src->buffer + (src->blockSize - ((srcDesc->numRecords + 1) * sizeof (UInt16))));
111
112 /* Sanity check */
113 if ((char *)srcOffs > ((char *)src->buffer + src->blockSize)) {
114 panic ("%s Too many records in the B-Tree node", "hfs_swap_BTNode:");
115 }
116
117 for (i = 0; i < srcDesc->numRecords + 1; i++) {
118 srcOffs[i] = SWAP_BE16 (srcOffs[i]);
119
120 /* Sanity check */
121 if (srcOffs[i] >= src->blockSize) {
122 panic ("%s B-Tree node offset out of range", "hfs_swap_BTNode:");
123 }
124 }
125 }
126
127 /* Swap the records (ordered by frequency of access) */
128 /* Swap a B-Tree internal node */
129 if ((srcDesc->kind == kBTIndexNode) ||
130 (srcDesc-> kind == kBTLeafNode)) {
131
132 if (isHFSPlus) {
133 error = hfs_swap_HFSPlusBTInternalNode (src, fileID, unswap);
134 } else {
135 error = hfs_swap_HFSBTInternalNode (src, fileID, unswap);
136 }
137
138 /* Swap a B-Tree map node */
139 } else if (srcDesc-> kind == kBTMapNode) {
140 /* Don't swap the bitmaps, they'll be done in the bitmap routines */
141
142 /* Swap a B-Tree header node */
143 } else if (srcDesc-> kind == kBTHeaderNode) {
144 /* The header's offset is hard-wired because we cannot trust the offset pointers */
145 BTHeaderRec *srcHead = (BTHeaderRec *)((char *)src->buffer + 14);
146
147 srcHead->treeDepth = SWAP_BE16 (srcHead->treeDepth);
148
149 srcHead->rootNode = SWAP_BE32 (srcHead->rootNode);
150 srcHead->leafRecords = SWAP_BE32 (srcHead->leafRecords);
151 srcHead->firstLeafNode = SWAP_BE32 (srcHead->firstLeafNode);
152 srcHead->lastLeafNode = SWAP_BE32 (srcHead->lastLeafNode);
153
154 srcHead->nodeSize = SWAP_BE16 (srcHead->nodeSize);
155 srcHead->maxKeyLength = SWAP_BE16 (srcHead->maxKeyLength);
156
157 srcHead->totalNodes = SWAP_BE32 (srcHead->totalNodes);
158 srcHead->freeNodes = SWAP_BE32 (srcHead->freeNodes);
159
160 srcHead->clumpSize = SWAP_BE32 (srcHead->clumpSize);
161 srcHead->attributes = SWAP_BE32 (srcHead->attributes);
162
163 /* Don't swap srcHead->reserved1 */
164 /* Don't swap srcHead->btreeType */
165 /* Don't swap srcHead->reserved2 */
166 /* Don't swap srcHead->reserved3 */
167 /* Don't swap bitmap */
168 }
169
170 /* If we are doing an unswap */
171 if (unswap == 1) {
172 /* Swap the node descriptor */
173 srcDesc->fLink = SWAP_BE32 (srcDesc->fLink);
174 srcDesc->bLink = SWAP_BE32 (srcDesc->bLink);
175
176 /* Don't swap srcDesc->kind */
177 /* Don't swap srcDesc->height */
178 /* Don't swap srcDesc->reserved */
179
180 /* Swap the node offsets (including the free space one!) */
181 srcOffs = (UInt16 *)((char *)src->buffer + (src->blockSize - ((srcDesc->numRecords + 1) * sizeof (UInt16))));
182
183 /* Sanity check */
184 if ((char *)srcOffs > ((char *)src->buffer + src->blockSize)) {
185 panic ("%s Too many records in the B-Tree node", "hfs_swap_BTNode:");
186 }
187
188 for (i = 0; i < srcDesc->numRecords + 1; i++) {
189 /* Sanity check */
190 if (srcOffs[i] >= src->blockSize) {
191 panic ("%s B-Tree node offset out of range", "hfs_swap_BTNode:");
192 }
193
194 srcOffs[i] = SWAP_BE16 (srcOffs[i]);
195 }
196
197 srcDesc->numRecords = SWAP_BE16 (srcDesc->numRecords);
198 }
199
200 return (error);
201}
202
203int
204hfs_swap_HFSPlusBTInternalNode (
205 BlockDescriptor *src,
206 HFSCatalogNodeID fileID,
207 int unswap
208)
209{
210 BTNodeDescriptor *srcDesc = src->buffer;
211 UInt16 *srcOffs = (UInt16 *)((char *)src->buffer + (src->blockSize - (srcDesc->numRecords * sizeof (UInt16))));
212
213 UInt32 i;
214 UInt32 j;
215
1c79356b
A
216 if (fileID == kHFSExtentsFileID) {
217 HFSPlusExtentKey *srcKey;
218 HFSPlusExtentDescriptor *srcRec;
219
220 for (i = 0; i < srcDesc->numRecords; i++) {
221 srcKey = (HFSPlusExtentKey *)((char *)src->buffer + srcOffs[i]);
222
223 if (!unswap) srcKey->keyLength = SWAP_BE16 (srcKey->keyLength);
224 srcRec = (HFSPlusExtentDescriptor *)((char *)srcKey + srcKey->keyLength + 2);
225 if (unswap) srcKey->keyLength = SWAP_BE16 (srcKey->keyLength);
226
227 /* Don't swap srcKey->forkType */
228 /* Don't swap srcKey->pad */
229
230 srcKey->fileID = SWAP_BE32 (srcKey->fileID);
231 srcKey->startBlock = SWAP_BE32 (srcKey->startBlock);
232
233 /* Stop if this is just an index node */
234 if (srcDesc->kind == kBTIndexNode) {
235 *((UInt32 *)srcRec) = SWAP_BE32 (*((UInt32 *)srcRec));
236 continue;
237 }
238
239 /* Swap the extent data */
240
241 /* Swap each extent */
242 for (j = 0; j < kHFSPlusExtentDensity; j++) {
243 srcRec[j].startBlock = SWAP_BE32 (srcRec[j].startBlock);
244 srcRec[j].blockCount = SWAP_BE32 (srcRec[j].blockCount);
245 }
246 }
247
248 } else if (fileID == kHFSCatalogFileID) {
249 HFSPlusCatalogKey *srcKey;
250 SInt16 *srcPtr;
251
252 for (i = 0; i < srcDesc->numRecords; i++) {
253 srcKey = (HFSPlusCatalogKey *)((char *)src->buffer + srcOffs[i]);
254
255 if (!unswap) srcKey->keyLength = SWAP_BE16 (srcKey->keyLength);
256 srcPtr = (SInt16 *)((char *)srcKey + srcKey->keyLength + 2);
257 if (unswap) srcKey->keyLength = SWAP_BE16 (srcKey->keyLength);
258
259 srcKey->parentID = SWAP_BE32 (srcKey->parentID);
260
261 if (!unswap) srcKey->nodeName.length = SWAP_BE16 (srcKey->nodeName.length);
262 for (j = 0; j < srcKey->nodeName.length; j++) {
263 srcKey->nodeName.unicode[j] = SWAP_BE16 (srcKey->nodeName.unicode[j]);
264 }
265 if (unswap) srcKey->nodeName.length = SWAP_BE16 (srcKey->nodeName.length);
266
267 /* Stop if this is just an index node */
268 if (srcDesc->kind == kBTIndexNode) {
269 *((UInt32 *)srcPtr) = SWAP_BE32 (*((UInt32 *)srcPtr));
270 continue;
271 }
272
273 /* Swap the recordType field, if unswapping, leave to later */
274 if (!unswap) srcPtr[0] = SWAP_BE16 (srcPtr[0]);
275
276 if (srcPtr[0] == kHFSPlusFolderRecord) {
277 HFSPlusCatalogFolder *srcRec = (HFSPlusCatalogFolder *)srcPtr;
278
279 srcRec->flags = SWAP_BE16 (srcRec->flags);
280 srcRec->valence = SWAP_BE32 (srcRec->valence);
281 srcRec->folderID = SWAP_BE32 (srcRec->folderID);
282 srcRec->createDate = SWAP_BE32 (srcRec->createDate);
283 srcRec->contentModDate = SWAP_BE32 (srcRec->contentModDate);
284 srcRec->attributeModDate = SWAP_BE32 (srcRec->attributeModDate);
285 srcRec->accessDate = SWAP_BE32 (srcRec->accessDate);
286 srcRec->backupDate = SWAP_BE32 (srcRec->backupDate);
287
288 srcRec->bsdInfo.ownerID = SWAP_BE32 (srcRec->bsdInfo.ownerID);
289 srcRec->bsdInfo.groupID = SWAP_BE32 (srcRec->bsdInfo.groupID);
290
291 /* Don't swap srcRec->bsdInfo.adminFlags */
292 /* Don't swap srcRec->bsdInfo.ownerFlags */
293
294 srcRec->bsdInfo.fileMode = SWAP_BE16 (srcRec->bsdInfo.fileMode);
295 srcRec->bsdInfo.special.iNodeNum = SWAP_BE32 (srcRec->bsdInfo.special.iNodeNum);
296
297 srcRec->textEncoding = SWAP_BE32 (srcRec->textEncoding);
298
299 /* Don't swap srcRec->userInfo */
300 /* Don't swap srcRec->finderInfo */
301 /* Don't swap srcRec->reserved */
302
303 } else if (srcPtr[0] == kHFSPlusFileRecord) {
304 HFSPlusCatalogFile *srcRec = (HFSPlusCatalogFile *)srcPtr;
305
306 srcRec->flags = SWAP_BE16 (srcRec->flags);
307
308 srcRec->fileID = SWAP_BE32 (srcRec->fileID);
309
310 srcRec->createDate = SWAP_BE32 (srcRec->createDate);
311 srcRec->contentModDate = SWAP_BE32 (srcRec->contentModDate);
312 srcRec->attributeModDate = SWAP_BE32 (srcRec->attributeModDate);
313 srcRec->accessDate = SWAP_BE32 (srcRec->accessDate);
314 srcRec->backupDate = SWAP_BE32 (srcRec->backupDate);
315
316 srcRec->bsdInfo.ownerID = SWAP_BE32 (srcRec->bsdInfo.ownerID);
317 srcRec->bsdInfo.groupID = SWAP_BE32 (srcRec->bsdInfo.groupID);
318
319 /* Don't swap srcRec->bsdInfo.adminFlags */
320 /* Don't swap srcRec->bsdInfo.ownerFlags */
321
322 srcRec->bsdInfo.fileMode = SWAP_BE16 (srcRec->bsdInfo.fileMode);
323 srcRec->bsdInfo.special.iNodeNum = SWAP_BE32 (srcRec->bsdInfo.special.iNodeNum);
324
325 srcRec->textEncoding = SWAP_BE32 (srcRec->textEncoding);
326
327 /* Don't swap srcRec->reserved1 */
328 /* Don't swap srcRec->userInfo */
329 /* Don't swap srcRec->finderInfo */
330 /* Don't swap srcRec->reserved2 */
331
332 hfs_swap_HFSPlusForkData (&srcRec->dataFork);
333 hfs_swap_HFSPlusForkData (&srcRec->resourceFork);
334
335 } else if ((srcPtr[0] == kHFSPlusFolderThreadRecord) ||
336 (srcPtr[0] == kHFSPlusFileThreadRecord)) {
337
338 HFSPlusCatalogThread *srcRec = (HFSPlusCatalogThread *)srcPtr;
339
340 /* Don't swap srcRec->reserved */
341
342 srcRec->parentID = SWAP_BE32 (srcRec->parentID);
343
344 if (!unswap) srcRec->nodeName.length = SWAP_BE16 (srcRec->nodeName.length);
345 for (j = 0; j < srcRec->nodeName.length; j++) {
346 srcRec->nodeName.unicode[j] = SWAP_BE16 (srcRec->nodeName.unicode[j]);
347 }
348 if (unswap) srcRec->nodeName.length = SWAP_BE16 (srcRec->nodeName.length);
349
350 } else {
351 panic ("%s unrecognized catalog record type", "hfs_swap_BTNode:");
352 }
353
354 /* If unswapping, we can safely unswap type field now */
355 if (unswap) srcPtr[0] = SWAP_BE16 (srcPtr[0]);
356 }
357
55e303ae
A
358 } else if (fileID > kHFSFirstUserCatalogNodeID) {
359 HotFileKey *srcKey;
360 UInt32 *srcRec;
361
362 for (i = 0; i < srcDesc->numRecords; i++) {
363 srcKey = (HotFileKey *)((char *)src->buffer + srcOffs[i]);
364
365 if (!unswap)
366 srcKey->keyLength = SWAP_BE16 (srcKey->keyLength);
367 srcRec = (u_int32_t *)((char *)srcKey + srcKey->keyLength + 2);
368 if (unswap)
369 srcKey->keyLength = SWAP_BE16 (srcKey->keyLength);
370
371 /* Don't swap srcKey->forkType */
372 /* Don't swap srcKey->pad */
373
374 srcKey->temperature = SWAP_BE32 (srcKey->temperature);
375 srcKey->fileID = SWAP_BE32 (srcKey->fileID);
376
377 *((UInt32 *)srcRec) = SWAP_BE32 (*((UInt32 *)srcRec));
378 }
1c79356b
A
379 } else {
380 panic ("%s unrecognized B-Tree type", "hfs_swap_BTNode:");
381 }
382
55e303ae 383
1c79356b
A
384 return (0);
385}
386
387int
388hfs_swap_HFSBTInternalNode (
389 BlockDescriptor *src,
390 HFSCatalogNodeID fileID,
391 int unswap
392)
393{
394 BTNodeDescriptor *srcDesc = src->buffer;
395 UInt16 *srcOffs = (UInt16 *)((char *)src->buffer + (src->blockSize - (srcDesc->numRecords * sizeof (UInt16))));
396
397 UInt32 i;
398 UInt32 j;
399
1c79356b
A
400 if (fileID == kHFSExtentsFileID) {
401 HFSExtentKey *srcKey;
402 HFSExtentDescriptor *srcRec;
403
404 for (i = 0; i < srcDesc->numRecords; i++) {
405 srcKey = (HFSExtentKey *)((char *)src->buffer + srcOffs[i]);
406
407 /* Don't swap srcKey->keyLength */
408 /* Don't swap srcKey->forkType */
409
410 srcKey->fileID = SWAP_BE32 (srcKey->fileID);
411 srcKey->startBlock = SWAP_BE16 (srcKey->startBlock);
412
413 /* Point to record data (round up to even byte boundary) */
414 srcRec = (HFSExtentDescriptor *)((char *)srcKey + ((srcKey->keyLength + 2) & ~1));
415
416 /* Stop if this is just an index node */
417 if (srcDesc->kind == kBTIndexNode) {
418 *((UInt32 *)srcRec) = SWAP_BE32 (*((UInt32 *)srcRec));
419 continue;
420 }
421
422 /* Swap each extent */
423 for (j = 0; j < kHFSExtentDensity; j++) {
424 srcRec[j].startBlock = SWAP_BE16 (srcRec[j].startBlock);
425 srcRec[j].blockCount = SWAP_BE16 (srcRec[j].blockCount);
426 }
427 }
428
429 } else if (fileID == kHFSCatalogFileID) {
430 HFSCatalogKey *srcKey;
431 SInt16 *srcPtr;
432
433 for (i = 0; i < srcDesc->numRecords; i++) {
434 srcKey = (HFSCatalogKey *)((char *)src->buffer + srcOffs[i]);
435
436 /* Don't swap srcKey->keyLength */
437 /* Don't swap srcKey->reserved */
438
439 srcKey->parentID = SWAP_BE32 (srcKey->parentID);
440
441 /* Don't swap srcKey->nodeName */
442
443 /* Point to record data (round up to even byte boundary) */
444 srcPtr = (SInt16 *)((char *)srcKey + ((srcKey->keyLength + 2) & ~1));
445
446 /* Stop if this is just an index node */
447 if (srcDesc->kind == kBTIndexNode) {
448 *((UInt32 *)srcPtr) = SWAP_BE32 (*((UInt32 *)srcPtr));
449 continue;
450 }
451
452 /* Swap the recordType field, if unswapping, leave to later */
453 if (!unswap) srcPtr[0] = SWAP_BE16 (srcPtr[0]);
454
455 if (srcPtr[0] == kHFSFolderRecord) {
456 HFSCatalogFolder *srcRec = (HFSCatalogFolder *)srcPtr;
457
458 srcRec->flags = SWAP_BE16 (srcRec->flags);
459 srcRec->valence = SWAP_BE16 (srcRec->valence);
460
461 srcRec->folderID = SWAP_BE32 (srcRec->folderID);
462 srcRec->createDate = SWAP_BE32 (srcRec->createDate);
463 srcRec->modifyDate = SWAP_BE32 (srcRec->modifyDate);
464 srcRec->backupDate = SWAP_BE32 (srcRec->backupDate);
465
466 /* Don't swap srcRec->userInfo */
467 /* Don't swap srcRec->finderInfo */
468 /* Don't swap resserved array */
469
470 } else if (srcPtr[0] == kHFSFileRecord) {
471 HFSCatalogFile *srcRec = (HFSCatalogFile *)srcPtr;
472
473 srcRec->flags = srcRec->flags;
474 srcRec->fileType = srcRec->fileType;
475
476 /* Don't swap srcRec->userInfo */
477
478 srcRec->fileID = SWAP_BE32 (srcRec->fileID);
479
480 srcRec->dataStartBlock = SWAP_BE16 (srcRec->dataStartBlock);
481 srcRec->dataLogicalSize = SWAP_BE32 (srcRec->dataLogicalSize);
482 srcRec->dataPhysicalSize = SWAP_BE32 (srcRec->dataPhysicalSize);
483
484 srcRec->rsrcStartBlock = SWAP_BE16 (srcRec->rsrcStartBlock);
485 srcRec->rsrcLogicalSize = SWAP_BE32 (srcRec->rsrcLogicalSize);
486 srcRec->rsrcPhysicalSize = SWAP_BE32 (srcRec->rsrcPhysicalSize);
487
488 srcRec->createDate = SWAP_BE32 (srcRec->createDate);
489 srcRec->modifyDate = SWAP_BE32 (srcRec->modifyDate);
490 srcRec->backupDate = SWAP_BE32 (srcRec->backupDate);
491
492 /* Don't swap srcRec->finderInfo */
493
494 srcRec->clumpSize = SWAP_BE16 (srcRec->clumpSize);
495
496 /* Swap the two sets of extents as an array of six (three each) UInt16 */
497 for (j = 0; j < kHFSExtentDensity * 2; j++) {
498 srcRec->dataExtents[j].startBlock = SWAP_BE16 (srcRec->dataExtents[j].startBlock);
499 srcRec->dataExtents[j].blockCount = SWAP_BE16 (srcRec->dataExtents[j].blockCount);
500 }
501
502 /* Don't swap srcRec->reserved */
503
504 } else if ((srcPtr[0] == kHFSFolderThreadRecord) ||
505 (srcPtr[0] == kHFSFileThreadRecord)) {
506
507 HFSCatalogThread *srcRec = (HFSCatalogThread *)srcPtr;
508
509 /* Don't swap srcRec->reserved array */
510
511 srcRec->parentID = SWAP_BE32 (srcRec->parentID);
512
513 /* Don't swap srcRec->nodeName */
514
515 } else {
516 panic ("%s unrecognized catalog record type", "hfs_swap_BTNode:");
517 }
518
519 /* If unswapping, we can safely swap type now */
520 if (unswap) srcPtr[0] = SWAP_BE16 (srcPtr[0]);
521 }
522
523 } else {
524 panic ("%s unrecognized B-Tree type", "hfs_swap_BTNode:");
525 }
526
527 return (0);
528}