1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24/*
25 * hfs_endian.c
26 *
27 * This file implements endian swapping routines for the HFS/HFS Plus
28 * volume format.
29 */
30
31#include <sys/types.h>
32#include <sys/stat.h>
33
34#include <libkern/OSByteOrder.h>
35#include <hfs/hfs_format.h>
36
37#include "hfs_endian.h"
38
39#undef ENDIAN_DEBUG
40#if 0
41/* Private swapping routines */
42int hfs_swap_HFSPlusBTInternalNode (BlockDescriptor *src, HFSCatalogNodeID fileID, int unswap);
43int hfs_swap_HFSBTInternalNode (BlockDescriptor *src, HFSCatalogNodeID fileID, int unswap);
44#endif
45
46static void hfs_swap_HFSPlusForkData (HFSPlusForkData *src);
47
48/*
49 * hfs_swap_HFSMasterDirectoryBlock
50 *
51 *  Specially modified to swap parts of the finder info
52 */
53void
54hfs_swap_HFSMasterDirectoryBlock (
55    void *buf
56)
57{
58    HFSMasterDirectoryBlock *src = (HFSMasterDirectoryBlock *)buf;
59
60    src->drSigWord		= SWAP_BE16 (src->drSigWord);
61    src->drCrDate		= SWAP_BE32 (src->drCrDate);
62    src->drLsMod		= SWAP_BE32 (src->drLsMod);
63    src->drAtrb			= SWAP_BE16 (src->drAtrb);
64    src->drNmFls		= SWAP_BE16 (src->drNmFls);
65    src->drVBMSt		= SWAP_BE16 (src->drVBMSt);
66    src->drAllocPtr		= SWAP_BE16 (src->drAllocPtr);
67    src->drNmAlBlks		= SWAP_BE16 (src->drNmAlBlks);
68    src->drAlBlkSiz		= SWAP_BE32 (src->drAlBlkSiz);
69    src->drClpSiz		= SWAP_BE32 (src->drClpSiz);
70    src->drAlBlSt		= SWAP_BE16 (src->drAlBlSt);
71    src->drNxtCNID		= SWAP_BE32 (src->drNxtCNID);
72    src->drFreeBks		= SWAP_BE16 (src->drFreeBks);
73
74    /* Don't swap drVN */
75
76    src->drVolBkUp		= SWAP_BE32 (src->drVolBkUp);
77    src->drVSeqNum		= SWAP_BE16 (src->drVSeqNum);
78    src->drWrCnt		= SWAP_BE32 (src->drWrCnt);
79    src->drXTClpSiz		= SWAP_BE32 (src->drXTClpSiz);
80    src->drCTClpSiz		= SWAP_BE32 (src->drCTClpSiz);
81    src->drNmRtDirs		= SWAP_BE16 (src->drNmRtDirs);
82    src->drFilCnt		= SWAP_BE32 (src->drFilCnt);
83    src->drDirCnt		= SWAP_BE32 (src->drDirCnt);
84
85    /* Swap just the 'blessed folder' in drFndrInfo */
86    src->drFndrInfo[0]	= SWAP_BE32 (src->drFndrInfo[0]);
87
88    src->drEmbedSigWord	= SWAP_BE16 (src->drEmbedSigWord);
89	src->drEmbedExtent.startBlock = SWAP_BE16 (src->drEmbedExtent.startBlock);
90	src->drEmbedExtent.blockCount = SWAP_BE16 (src->drEmbedExtent.blockCount);
91
92    src->drXTFlSize		= SWAP_BE32 (src->drXTFlSize);
93	src->drXTExtRec[0].startBlock = SWAP_BE16 (src->drXTExtRec[0].startBlock);
94	src->drXTExtRec[0].blockCount = SWAP_BE16 (src->drXTExtRec[0].blockCount);
95	src->drXTExtRec[1].startBlock = SWAP_BE16 (src->drXTExtRec[1].startBlock);
96	src->drXTExtRec[1].blockCount = SWAP_BE16 (src->drXTExtRec[1].blockCount);
97	src->drXTExtRec[2].startBlock = SWAP_BE16 (src->drXTExtRec[2].startBlock);
98	src->drXTExtRec[2].blockCount = SWAP_BE16 (src->drXTExtRec[2].blockCount);
99
100    src->drCTFlSize		= SWAP_BE32 (src->drCTFlSize);
101	src->drCTExtRec[0].startBlock = SWAP_BE16 (src->drCTExtRec[0].startBlock);
102	src->drCTExtRec[0].blockCount = SWAP_BE16 (src->drCTExtRec[0].blockCount);
103	src->drCTExtRec[1].startBlock = SWAP_BE16 (src->drCTExtRec[1].startBlock);
104	src->drCTExtRec[1].blockCount = SWAP_BE16 (src->drCTExtRec[1].blockCount);
105	src->drCTExtRec[2].startBlock = SWAP_BE16 (src->drCTExtRec[2].startBlock);
106	src->drCTExtRec[2].blockCount = SWAP_BE16 (src->drCTExtRec[2].blockCount);
107}
108
109/*
110 * hfs_swap_HFSPlusVolumeHeader
111 */
112void
113hfs_swap_HFSPlusVolumeHeader (
114    void *buf
115)
116{
117    HFSPlusVolumeHeader *src = (HFSPlusVolumeHeader *)buf;
118
119    src->signature			= SWAP_BE16 (src->signature);
120    src->version			= SWAP_BE16 (src->version);
121    src->attributes			= SWAP_BE32 (src->attributes);
122    src->lastMountedVersion	= SWAP_BE32 (src->lastMountedVersion);
123
124    src->journalInfoBlock	= SWAP_BE32 (src->journalInfoBlock);
125
126    src->createDate			= SWAP_BE32 (src->createDate);
127    src->modifyDate			= SWAP_BE32 (src->modifyDate);
128    src->backupDate			= SWAP_BE32 (src->backupDate);
129    src->checkedDate		= SWAP_BE32 (src->checkedDate);
130    src->fileCount			= SWAP_BE32 (src->fileCount);
131    src->folderCount		= SWAP_BE32 (src->folderCount);
132    src->blockSize			= SWAP_BE32 (src->blockSize);
133    src->totalBlocks		= SWAP_BE32 (src->totalBlocks);
134    src->freeBlocks			= SWAP_BE32 (src->freeBlocks);
135    src->nextAllocation		= SWAP_BE32 (src->nextAllocation);
136    src->rsrcClumpSize		= SWAP_BE32 (src->rsrcClumpSize);
137    src->dataClumpSize		= SWAP_BE32 (src->dataClumpSize);
138    src->nextCatalogID		= SWAP_BE32 (src->nextCatalogID);
139    src->writeCount			= SWAP_BE32 (src->writeCount);
140    src->encodingsBitmap	= SWAP_BE64 (src->encodingsBitmap);
141
142    /* Don't swap finderInfo */
143
144    hfs_swap_HFSPlusForkData (&src->allocationFile);
145    hfs_swap_HFSPlusForkData (&src->extentsFile);
146    hfs_swap_HFSPlusForkData (&src->catalogFile);
147    hfs_swap_HFSPlusForkData (&src->attributesFile);
148    hfs_swap_HFSPlusForkData (&src->startupFile);
149}
150
151/*
152 * hfs_swap_HFSPlusForkData
153 *
154 *  There's still a few spots where we still need to swap the fork data.
155 */
156void
157hfs_swap_HFSPlusForkData (
158    HFSPlusForkData *src
159)
160{
161    int i;
162
163	src->logicalSize		= SWAP_BE64 (src->logicalSize);
164
165	src->clumpSize			= SWAP_BE32 (src->clumpSize);
166	src->totalBlocks		= SWAP_BE32 (src->totalBlocks);
167
168    for (i = 0; i < kHFSPlusExtentDensity; i++) {
169        src->extents[i].startBlock	= SWAP_BE32 (src->extents[i].startBlock);
170        src->extents[i].blockCount	= SWAP_BE32 (src->extents[i].blockCount);
171    }
172}
173