1 | /*␊ |
2 | * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | *␊ |
6 | * The contents of this file constitute Original Code as defined in and␊ |
7 | * are subject to the Apple Public Source License Version 2.0 (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.␊ |
11 | * ␊ |
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␊ |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
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.␊ |
19 | *␊ |
20 | * @APPLE_LICENSE_HEADER_END@␊ |
21 | *␊ |
22 | *␊ |
23 | * hfs.c - File System Module for HFS and HFS+.␊ |
24 | *␊ |
25 | * Copyright (c) 1999-2002 Apple Computer, Inc.␊ |
26 | *␊ |
27 | * DRI: Josh de Cesare␊ |
28 | */␊ |
29 | ␊ |
30 | #include <sl.h>␊ |
31 | #include <hfs/hfs_format.h>␊ |
32 | ␊ |
33 | #include "hfs.h"␊ |
34 | ␊ |
35 | #define kBlockSize (0x200)␊ |
36 | ␊ |
37 | #define kMDBBaseOffset (2 * kBlockSize)␊ |
38 | ␊ |
39 | #define kBTreeCatalog (0)␊ |
40 | #define kBTreeExtents (1)␊ |
41 | ␊ |
42 | #ifdef __i386__␊ |
43 | ␊ |
44 | static CICell gCurrentIH;␊ |
45 | static long long gAllocationOffset;␊ |
46 | static long gIsHFSPlus;␊ |
47 | static long gCaseSensitive;␊ |
48 | static u_int32_t gBlockSize;␊ |
49 | static u_int32_t gCacheBlockSize;␊ |
50 | static char *gBTreeHeaderBuffer;␊ |
51 | static BTHeaderRec *gBTHeaders[2];␊ |
52 | static char *gHFSMdbVib;␊ |
53 | static HFSMasterDirectoryBlock *gHFSMDB;␊ |
54 | static char *gHFSPlusHeader;␊ |
55 | static HFSPlusVolumeHeader *gHFSPlus;␊ |
56 | static char *gLinkTemp;␊ |
57 | static long long gVolID;␊ |
58 | static char *gTempStr;␊ |
59 | ␊ |
60 | #else /* !__i386__ */␊ |
61 | ␊ |
62 | static CICell␉␉␉gCurrentIH;␊ |
63 | static long long␉␉gAllocationOffset;␊ |
64 | static long␉␉␉gIsHFSPlus;␊ |
65 | static long␉␉␉gCaseSensitive;␊ |
66 | static u_int32_t␉␉gBlockSize;␊ |
67 | static u_int32_t␉␉gCacheBlockSize;␊ |
68 | static char␉␉␉gBTreeHeaderBuffer[512];␊ |
69 | static BTHeaderRec␉␉*gBTHeaders[2];␊ |
70 | static char␉␉␉gHFSMdbVib[kBlockSize];␊ |
71 | static HFSMasterDirectoryBlock␉*gHFSMDB =(HFSMasterDirectoryBlock*)gHFSMdbVib;␊ |
72 | static char␉␉␉gHFSPlusHeader[kBlockSize];␊ |
73 | static HFSPlusVolumeHeader␉*gHFSPlus =(HFSPlusVolumeHeader*)gHFSPlusHeader;␊ |
74 | static char␉␉␉gLinkTemp[64];␊ |
75 | static long long␉␉gVolID;␊ |
76 | ␊ |
77 | #endif /* !__i386__ */␊ |
78 | ␊ |
79 | static long ReadFile(void *file, u_int64_t *length, void *base, u_int64_t offset);␊ |
80 | static long GetCatalogEntryInfo(void *entry, long *flags, u_int32_t *time,␊ |
81 | FinderInfo *finderInfo, long *infoValid);␊ |
82 | static long ResolvePathToCatalogEntry(char *filePath, long *flags,␊ |
83 | void *entry, u_int32_t dirID, long long *dirIndex);␊ |
84 | ␊ |
85 | static long GetCatalogEntry(long long *dirIndex, char **name,␊ |
86 | long *flags, u_int32_t *time,␊ |
87 | FinderInfo *finderInfo, long *infoValid);␊ |
88 | static long ReadCatalogEntry(char *fileName, u_int32_t dirID, void *entry,␊ |
89 | long long *dirIndex);␊ |
90 | static long ReadExtentsEntry(u_int32_t fileID, long startBlock, void *entry);␊ |
91 | ␊ |
92 | static long ReadBTreeEntry(long btree, void *key, char *entry, long long *dirIndex);␊ |
93 | static void GetBTreeRecord(u_int16_t index, char *nodeBuffer, u_int16_t nodeSize,␊ |
94 | char **key, char **data);␊ |
95 | ␊ |
96 | static long ReadExtent(char *extent, u_int64_t extentSize, u_int32_t extentFile,␊ |
97 | u_int64_t offset, u_int64_t size, void *buffer, long cache);␊ |
98 | ␊ |
99 | static u_int32_t GetExtentStart(void *extents, u_int32_t index);␊ |
100 | static u_int32_t GetExtentSize(void *extents, u_int32_t index);␊ |
101 | ␊ |
102 | static long CompareHFSCatalogKeys(void *key, void *testKey);␊ |
103 | static long CompareHFSPlusCatalogKeys(void *key, void *testKey);␊ |
104 | static long CompareHFSExtentsKeys(void *key, void *testKey);␊ |
105 | static long CompareHFSPlusExtentsKeys(void *key, void *testKey);␊ |
106 | ␊ |
107 | extern long FastRelString(u_int8_t *str1, u_int8_t *str2);␊ |
108 | extern long BinaryUnicodeCompare(u_int16_t *uniStr1, u_int32_t len1,␊ |
109 | u_int16_t *uniStr2, u_int32_t len2);␊ |
110 | ␊ |
111 | //==============================================================================␊ |
112 | ␊ |
113 | static void SwapFinderInfo(FndrFileInfo *dst, FndrFileInfo *src)␊ |
114 | {␊ |
115 | ␉dst->fdType = SWAP_BE32(src->fdType);␊ |
116 | ␉dst->fdCreator = SWAP_BE32(src->fdCreator);␊ |
117 | ␉dst->fdFlags = SWAP_BE16(src->fdFlags);␊ |
118 | ␉// Don't bother with location␊ |
119 | }␊ |
120 | ␊ |
121 | //==============================================================================␊ |
122 | ␊ |
123 | void HFSFree(CICell ih)␊ |
124 | {␊ |
125 | ␉if(gCurrentIH == ih)␊ |
126 | ␉{␊ |
127 | ␉␉gCurrentIH = 0;␊ |
128 | ␉}␊ |
129 | ␉free(ih);␊ |
130 | }␊ |
131 | ␊ |
132 | //==============================================================================␊ |
133 | ␊ |
134 | bool HFSProbe (const void *buf)␊ |
135 | {␊ |
136 | ␉const HFSMasterDirectoryBlock *mdb;␊ |
137 | ␉const HFSPlusVolumeHeader *header;␊ |
138 | ␉mdb = (const HFSMasterDirectoryBlock *)(((const char*)buf)+kMDBBaseOffset);␊ |
139 | ␉header = (const HFSPlusVolumeHeader *)(((const char*)buf)+kMDBBaseOffset);␊ |
140 | ␉␊ |
141 | ␉if ( SWAP_BE16(mdb->drSigWord) == kHFSSigWord ) {␊ |
142 | ␉␉return true;␊ |
143 | ␉}␊ |
144 | ␊ |
145 | ␉if (SWAP_BE16(header->signature) != kHFSPlusSigWord && SWAP_BE16(header->signature) != kHFSXSigWord) {␊ |
146 | ␉␉return false;␊ |
147 | ␉}␊ |
148 | ␉return true;␊ |
149 | }␊ |
150 | ␊ |
151 | //==============================================================================␊ |
152 | ␊ |
153 | long HFSInitPartition(CICell ih)␊ |
154 | {␊ |
155 | ␉u_int64_t extentSize;␊ |
156 | ␉void *extent;␊ |
157 | ␉u_int32_t extentFile;␊ |
158 | ␉u_int16_t nodeSize;␊ |
159 | ␊ |
160 | ␉if (ih == gCurrentIH)␊ |
161 | ␉{␊ |
162 | #ifdef __i386__␊ |
163 | ␉␉CacheInit(ih, gCacheBlockSize);␊ |
164 | #endif␊ |
165 | ␉return 0;␊ |
166 | ␉}␊ |
167 | ␊ |
168 | #ifdef __i386__␊ |
169 | ␉if (!gTempStr)␊ |
170 | ␉{␊ |
171 | ␉␉gTempStr = (char *)malloc(4096);␊ |
172 | ␉}␊ |
173 | ␉if (!gLinkTemp)␊ |
174 | ␉{␊ |
175 | ␉␉gLinkTemp = (char *)malloc(64);␊ |
176 | ␉}␊ |
177 | ␉if (!gBTreeHeaderBuffer)␊ |
178 | ␉{␊ |
179 | ␉␉gBTreeHeaderBuffer = (char *)malloc(512);␊ |
180 | ␉}␊ |
181 | ␉if (!gHFSMdbVib)␊ |
182 | ␉{␊ |
183 | ␉␉gHFSMdbVib = (char *)malloc(kBlockSize);␊ |
184 | ␉␉gHFSMDB = (HFSMasterDirectoryBlock *)gHFSMdbVib;␊ |
185 | ␉}␊ |
186 | ␉if (!gHFSPlusHeader)␊ |
187 | ␉{␊ |
188 | ␉␉gHFSPlusHeader = (char *)malloc(kBlockSize);␊ |
189 | ␉␉gHFSPlus = (HFSPlusVolumeHeader *)gHFSPlusHeader;␊ |
190 | ␉}␊ |
191 | ␉if (!gTempStr || !gLinkTemp || !gBTreeHeaderBuffer || !gHFSMdbVib || !gHFSPlusHeader)␊ |
192 | ␉{␊ |
193 | ␉␉return -1;␊ |
194 | ␉}␊ |
195 | #endif /* __i386__ */␊ |
196 | ␊ |
197 | ␉gAllocationOffset = 0;␊ |
198 | ␉gIsHFSPlus = 0;␊ |
199 | ␉gCaseSensitive = 0;␊ |
200 | ␉gBTHeaders[0] = 0;␊ |
201 | ␉gBTHeaders[1] = 0;␊ |
202 | ␊ |
203 | ␉// Look for the HFS MDB␊ |
204 | ␉Seek(ih, kMDBBaseOffset);␊ |
205 | ␉Read(ih, (long)gHFSMdbVib, kBlockSize);␊ |
206 | ␊ |
207 | ␉if (SWAP_BE16(gHFSMDB->drSigWord) == kHFSSigWord)␊ |
208 | ␉{␊ |
209 | ␉␉gAllocationOffset = SWAP_BE16(gHFSMDB->drAlBlSt) * kBlockSize;␊ |
210 | ␊ |
211 | ␉␉// See if it is HFSPlus␊ |
212 | ␉␉if (SWAP_BE16(gHFSMDB->drEmbedSigWord) != kHFSPlusSigWord)␊ |
213 | ␉␉{␊ |
214 | ␉␉␉// Normal HFS;␊ |
215 | ␉␉␉gCacheBlockSize = gBlockSize = SWAP_BE32(gHFSMDB->drAlBlkSiz);␊ |
216 | ␉␉␉CacheInit(ih, gCacheBlockSize);␊ |
217 | ␉␉␉gCurrentIH = ih;␊ |
218 | ␊ |
219 | ␉␉␉// grab the 64 bit volume ID␊ |
220 | ␉␉␉bcopy(&gHFSMDB->drFndrInfo[6], &gVolID, 8);␊ |
221 | ␊ |
222 | ␉␉␉// Get the Catalog BTree node size.␊ |
223 | ␉␉␉extent = (HFSExtentDescriptor *)&gHFSMDB->drCTExtRec;␊ |
224 | ␉␉␉extentSize = SWAP_BE32(gHFSMDB->drCTFlSize);␊ |
225 | ␉␉␉extentFile = kHFSCatalogFileID;␊ |
226 | ␉␉␉ReadExtent(extent, extentSize, extentFile, 0, 256, gBTreeHeaderBuffer + kBTreeCatalog * 256, 0);␊ |
227 | ␊ |
228 | ␉␉␉nodeSize = SWAP_BE16(((BTHeaderRec *)(gBTreeHeaderBuffer + kBTreeCatalog * 256 + sizeof(BTNodeDescriptor)))->nodeSize);␊ |
229 | ␊ |
230 | ␉␉␉// If the BTree node size is larger than the block size, reset the cache.␊ |
231 | ␉␉␉if (nodeSize > gBlockSize)␊ |
232 | ␉␉␉{␊ |
233 | ␉␉␉␉gCacheBlockSize = nodeSize;␊ |
234 | ␉␉␉␉CacheInit(ih, gCacheBlockSize);␊ |
235 | ␉␉␉}␊ |
236 | ␊ |
237 | ␉␉return 0;␊ |
238 | ␉␉}␊ |
239 | ␊ |
240 | ␉␉// Calculate the offset to the embeded HFSPlus volume.␊ |
241 | ␉␉gAllocationOffset += (long long)SWAP_BE16(gHFSMDB->drEmbedExtent.startBlock) *␊ |
242 | SWAP_BE32(gHFSMDB->drAlBlkSiz);␊ |
243 | ␉}␊ |
244 | ␊ |
245 | ␉// Look for the HFSPlus Header␊ |
246 | ␉Seek(ih, gAllocationOffset + kMDBBaseOffset);␊ |
247 | ␉Read(ih, (long)gHFSPlusHeader, kBlockSize);␊ |
248 | ␊ |
249 | ␉// Not a HFS+ or HFSX volume.␊ |
250 | ␉if (SWAP_BE16(gHFSPlus->signature) != kHFSPlusSigWord && SWAP_BE16(gHFSPlus->signature) != kHFSXSigWord)␊ |
251 | ␉{␊ |
252 | ␉␉verbose("HFS signature was not present.\n");␊ |
253 | ␉␉gCurrentIH = 0;␊ |
254 | ␉␉return -1;␊ |
255 | ␉}␊ |
256 | ␊ |
257 | ␉gIsHFSPlus = 1;␊ |
258 | ␉gCacheBlockSize = gBlockSize = SWAP_BE32(gHFSPlus->blockSize);␊ |
259 | ␉CacheInit(ih, gCacheBlockSize);␊ |
260 | ␉gCurrentIH = ih;␊ |
261 | ␊ |
262 | ␉ih->modTime = SWAP_BE32(gHFSPlus->modifyDate) - 2082844800;␊ |
263 | ␊ |
264 | ␉// grab the 64 bit volume ID␊ |
265 | ␉bcopy(&gHFSPlus->finderInfo[24], &gVolID, 8);␊ |
266 | ␊ |
267 | ␉// Get the Catalog BTree node size.␊ |
268 | ␉extent = &gHFSPlus->catalogFile.extents;␊ |
269 | ␉extentSize = SWAP_BE64(gHFSPlus->catalogFile.logicalSize);␊ |
270 | ␉extentFile = kHFSCatalogFileID;␊ |
271 | ␊ |
272 | ␉ReadExtent(extent, extentSize, extentFile, 0, 256, gBTreeHeaderBuffer + kBTreeCatalog * 256, 0);␊ |
273 | ␊ |
274 | ␉nodeSize = SWAP_BE16(((BTHeaderRec *)(gBTreeHeaderBuffer + kBTreeCatalog * 256 + sizeof(BTNodeDescriptor)))->nodeSize);␊ |
275 | ␊ |
276 | ␉// If the BTree node size is larger than the block size, reset the cache.␊ |
277 | ␉if (nodeSize > gBlockSize)␊ |
278 | ␉{␊ |
279 | ␉␉gCacheBlockSize = nodeSize;␊ |
280 | ␉␉CacheInit(ih, gCacheBlockSize);␊ |
281 | ␉}␊ |
282 | ␊ |
283 | ␉return 0;␊ |
284 | }␊ |
285 | ␊ |
286 | //==============================================================================␊ |
287 | ␊ |
288 | long HFSLoadFile(CICell ih, char * filePath)␊ |
289 | {␊ |
290 | ␉return HFSReadFile(ih, filePath, (void *)gFSLoadAddress, 0, 0);␊ |
291 | }␊ |
292 | ␊ |
293 | long HFSReadFile(CICell ih, char *filePath, void *base, u_int64_t offset, u_int64_t length)␊ |
294 | {␊ |
295 | ␉char entry[512];␊ |
296 | ␉char devStr[12];␊ |
297 | ␉u_int32_t dirID;␊ |
298 | ␉long result, flags = 0;␊ |
299 | ␊ |
300 | ␉if (HFSInitPartition(ih) == -1)␊ |
301 | ␉{␊ |
302 | ␉␉return -1;␊ |
303 | ␉}␊ |
304 | ␊ |
305 | ␉dirID = kHFSRootFolderID;␊ |
306 | ␉// Skip a lead '/'. Start in the system folder if there are two.␊ |
307 | ␉if (filePath[0] == '/')␊ |
308 | ␉{␊ |
309 | ␉␉if (filePath[1] == '/')␊ |
310 | ␉␉{␊ |
311 | ␉␉␉if (gIsHFSPlus)␊ |
312 | ␉␉␉{␊ |
313 | ␉␉␉␉dirID = SWAP_BE32(((long *)gHFSPlus->finderInfo)[5]);␊ |
314 | ␉␉␉}␊ |
315 | ␉␉␉else␊ |
316 | ␉␉␉{␊ |
317 | ␉␉␉␉dirID = SWAP_BE32(gHFSMDB->drFndrInfo[5]);␊ |
318 | ␉␉␉}␊ |
319 | ␊ |
320 | ␉␉␉if (dirID == 0)␊ |
321 | ␉␉␉{␊ |
322 | ␉␉␉␉return -1;␊ |
323 | ␉␉␉}␊ |
324 | ␊ |
325 | ␉␉␉filePath++;␊ |
326 | ␉␉}␊ |
327 | ␊ |
328 | ␉␉filePath++;␊ |
329 | ␉}␊ |
330 | ␊ |
331 | ␉result = ResolvePathToCatalogEntry(filePath, &flags, entry, dirID, 0);␊ |
332 | ␊ |
333 | ␉if ((result == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
334 | ␉{␊ |
335 | ␉␉return -1;␊ |
336 | ␉}␊ |
337 | ␊ |
338 | #if UNUSED␊ |
339 | ␉// Not yet for Intel. System.config/Default.table will fail this check.␊ |
340 | ␉// Check file owner and permissions.␊ |
341 | ␉if (flags & (kOwnerNotRoot | kPermGroupWrite | kPermOtherWrite))␊ |
342 | ␉{␊ |
343 | ␉␉return -1;␊ |
344 | ␉}␊ |
345 | #endif␊ |
346 | ␊ |
347 | ␉result = ReadFile(entry, &length, base, offset);␊ |
348 | ␉if (result == -1)␊ |
349 | ␉{␊ |
350 | ␉␉return -1;␊ |
351 | ␉}␊ |
352 | ␊ |
353 | ␉getDeviceDescription(ih, devStr);␊ |
354 | ␊ |
355 | ␉verbose("Read HFS%s file: [%s/%s] %d bytes.\n",␉(gIsHFSPlus ? "+" : ""), devStr, filePath, (uint32_t)length);␊ |
356 | ␊ |
357 | ␉return length;␊ |
358 | }␊ |
359 | ␊ |
360 | long HFSGetDirEntry(CICell ih, char * dirPath, long long * dirIndex, char ** name, long * flags, u_int32_t * time, FinderInfo * finderInfo, long * infoValid)␊ |
361 | {␊ |
362 | ␉char entry[512];␊ |
363 | ␉u_int32_t dirID;␊ |
364 | ␉long dirFlags;␊ |
365 | ␊ |
366 | ␉if (HFSInitPartition(ih) == -1)␊ |
367 | ␉{␊ |
368 | ␉␉return -1;␊ |
369 | ␉}␊ |
370 | ␊ |
371 | ␉if (*dirIndex == -1)␊ |
372 | ␉{␊ |
373 | ␉␉return -1;␊ |
374 | ␉}␊ |
375 | ␊ |
376 | ␉dirID = kHFSRootFolderID;␊ |
377 | ␊ |
378 | ␉// Skip a lead '/'. Start in the system folder if there are two.␊ |
379 | ␉if (dirPath[0] == '/')␊ |
380 | ␉{␊ |
381 | ␉␉if (dirPath[1] == '/')␊ |
382 | ␉␉{␊ |
383 | ␉␉␉if (gIsHFSPlus)␊ |
384 | ␉␉␉{␊ |
385 | ␉␉␉␉dirID = SWAP_BE32(((long *)gHFSPlus->finderInfo)[5]);␊ |
386 | ␉␉␉}␊ |
387 | ␉␉␉else␊ |
388 | ␉␉␉{␊ |
389 | ␉␉␉␉dirID = SWAP_BE32(gHFSMDB->drFndrInfo[5]);␊ |
390 | ␉␉␉}␊ |
391 | ␊ |
392 | ␉␉␉if (dirID == 0)␊ |
393 | ␉␉␉{␊ |
394 | ␉␉␉␉return -1;␊ |
395 | ␉␉␉}␊ |
396 | ␊ |
397 | ␉␉␉dirPath++;␊ |
398 | ␉␉}␊ |
399 | ␊ |
400 | ␉␉dirPath++;␊ |
401 | ␉}␊ |
402 | ␊ |
403 | ␉if (*dirIndex == 0)␊ |
404 | ␉{␊ |
405 | ␉␉ResolvePathToCatalogEntry(dirPath, &dirFlags, entry, dirID, dirIndex);␊ |
406 | ␊ |
407 | ␉␉if (*dirIndex == 0)␊ |
408 | ␉␉{␊ |
409 | ␉␉␉*dirIndex = -1;␊ |
410 | ␉␉}␊ |
411 | ␊ |
412 | ␉␉if ((dirFlags & kFileTypeMask) != kFileTypeUnknown)␊ |
413 | ␉␉{␊ |
414 | ␉␉␉return -1;␊ |
415 | ␉␉}␊ |
416 | ␉}␊ |
417 | ␊ |
418 | ␉GetCatalogEntry(dirIndex, name, flags, time, finderInfo, infoValid);␊ |
419 | ␊ |
420 | ␉if (*dirIndex == 0)␊ |
421 | ␉{␊ |
422 | ␉␉*dirIndex = -1;␊ |
423 | ␉}␊ |
424 | ␊ |
425 | ␉if ((*flags & kFileTypeMask) == kFileTypeUnknown)␊ |
426 | ␉{␊ |
427 | ␉␉return -1;␊ |
428 | ␉}␊ |
429 | ␊ |
430 | ␉return 0;␊ |
431 | }␊ |
432 | ␊ |
433 | //==============================================================================␊ |
434 | ␊ |
435 | void HFSGetDescription(CICell ih, char *str, long strMaxLen)␊ |
436 | {␊ |
437 | ␉long long dirIndex = 0;␊ |
438 | ␉char *name;␊ |
439 | ␉long flags;␊ |
440 | ␉u_int32_t firstLeafNode;␊ |
441 | ␉u_int32_t time;␊ |
442 | ␉u_int16_t nodeSize;␊ |
443 | ␊ |
444 | ␉if (HFSInitPartition(ih) == -1)␊ |
445 | ␉{␊ |
446 | ␉␉return;␊ |
447 | ␉}␊ |
448 | ␊ |
449 | ␉/* Fill some crucial data structures by side effect. */␊ |
450 | ␉HFSGetDirEntry(ih, "/", &dirIndex, &name, &flags, &time, 0, 0);␊ |
451 | ␊ |
452 | ␉/* Now we can loook up the volume name node. */␊ |
453 | ␉nodeSize = SWAP_BE16(gBTHeaders[kBTreeCatalog]->nodeSize);␊ |
454 | ␉firstLeafNode = SWAP_BE32(gBTHeaders[kBTreeCatalog]->firstLeafNode);␊ |
455 | ␊ |
456 | ␉dirIndex = (long long)firstLeafNode * nodeSize;␊ |
457 | ␊ |
458 | ␉GetCatalogEntry(&dirIndex, &name, &flags, &time, 0, 0);␊ |
459 | ␊ |
460 | ␉strncpy(str, name, strMaxLen);␊ |
461 | ␉str[strMaxLen] = '\0';␊ |
462 | }␊ |
463 | ␊ |
464 | //==============================================================================␊ |
465 | ␊ |
466 | long HFSGetFileBlock(CICell ih, char *filePath, u_int64_t *firstBlock)␊ |
467 | {␊ |
468 | ␉char entry[512];␊ |
469 | ␉long result, flags;␊ |
470 | ␉u_int32_t dirID;␊ |
471 | ␉void *extents;␊ |
472 | ␉HFSCatalogFile *hfsFile = (void *)entry;␊ |
473 | ␉HFSPlusCatalogFile *hfsPlusFile = (void *)entry;␊ |
474 | ␊ |
475 | ␉if (HFSInitPartition(ih) == -1)␊ |
476 | ␉{␊ |
477 | ␉␉return -1;␊ |
478 | ␉}␊ |
479 | ␊ |
480 | ␉dirID = kHFSRootFolderID;␊ |
481 | ␉// Skip a lead '/'. Start in the system folder if there are two.␊ |
482 | ␉if (filePath[0] == '/')␊ |
483 | ␉{␊ |
484 | ␉␉if (filePath[1] == '/')␊ |
485 | ␉␉{␊ |
486 | ␉␉␉if (gIsHFSPlus)␊ |
487 | ␉␉␉{␊ |
488 | ␉␉␉␉dirID = SWAP_BE32(((long *) gHFSPlus->finderInfo)[5]);␊ |
489 | ␉␉␉}␊ |
490 | ␉␉␉else␊ |
491 | ␉␉␉{␊ |
492 | ␉␉␉␉dirID = SWAP_BE32(gHFSMDB->drFndrInfo[5]);␊ |
493 | ␉␉␉}␊ |
494 | ␊ |
495 | ␉␉␉if (dirID == 0)␊ |
496 | ␉␉␉{␊ |
497 | ␉␉␉␉return -1;␊ |
498 | ␉␉␉}␊ |
499 | ␉␉␉filePath++;␊ |
500 | ␉␉}␊ |
501 | ␉␉filePath++;␊ |
502 | ␉}␊ |
503 | ␊ |
504 | ␉result = ResolvePathToCatalogEntry(filePath, &flags, entry, dirID, 0);␊ |
505 | ␊ |
506 | ␉if ((result == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))␊ |
507 | ␉{␊ |
508 | ␉␉printf("HFS: Resolve path '%s' failed\n", filePath);␊ |
509 | ␉␉return -1;␊ |
510 | ␉}␊ |
511 | ␊ |
512 | ␉if (gIsHFSPlus) {␊ |
513 | ␉␉extents = &hfsPlusFile->dataFork.extents;␊ |
514 | ␉} else {␊ |
515 | ␉␉extents = &hfsFile->dataExtents;␊ |
516 | ␉}␊ |
517 | ␊ |
518 | #if DEBUG␊ |
519 | ␉printf("extent start 0x%x\n", GetExtentStart(extents, 0));␊ |
520 | ␉printf("block size 0x%x\n", gBlockSize);␊ |
521 | ␉printf("Allocation offset 0x%x\n", (unsigned long)gAllocationOffset);␊ |
522 | #endif␊ |
523 | ␉*firstBlock = ((u_int64_t) GetExtentStart(extents, 0) * (u_int64_t) gBlockSize + gAllocationOffset) / 512ULL;␊ |
524 | ␉return 0;␊ |
525 | }␊ |
526 | ␊ |
527 | ␊ |
528 | //==============================================================================␊ |
529 | ␊ |
530 | long HFSGetUUID(CICell ih, char *uuidStr)␊ |
531 | {␊ |
532 | ␉if (HFSInitPartition(ih) == -1)␊ |
533 | ␉{␊ |
534 | ␉␉return -1;␊ |
535 | ␉}␊ |
536 | ␊ |
537 | ␉if (gVolID == 0LL)␊ |
538 | ␉{␊ |
539 | ␉␉return -1;␊ |
540 | ␉}␊ |
541 | ␊ |
542 | ␉return CreateUUIDString((uint8_t*)(&gVolID), sizeof(gVolID), uuidStr);␊ |
543 | }␊ |
544 | ␊ |
545 | //==============================================================================␊ |
546 | // Private Functions␊ |
547 | ␊ |
548 | static long ReadFile(void * file, u_int64_t * length, void * base, u_int64_t offset)␊ |
549 | {␊ |
550 | ␉u_int64_t fileLength;␊ |
551 | ␉void *extents;␊ |
552 | ␉HFSCatalogFile *hfsFile = file;␊ |
553 | ␉HFSPlusCatalogFile *hfsPlusFile = file;␊ |
554 | ␉u_int32_t fileID;␊ |
555 | ␊ |
556 | ␉if (gIsHFSPlus)␊ |
557 | ␉{␊ |
558 | ␉␉fileID = SWAP_BE32(hfsPlusFile->fileID);␊ |
559 | ␉␉fileLength = (uint64_t)SWAP_BE64(hfsPlusFile->dataFork.logicalSize);␊ |
560 | ␉␉extents = &hfsPlusFile->dataFork.extents;␊ |
561 | ␉}␊ |
562 | ␉else␊ |
563 | ␉{␊ |
564 | ␉␉fileID = SWAP_BE32(hfsFile->fileID);␊ |
565 | ␉␉fileLength = SWAP_BE32(hfsFile->dataLogicalSize);␊ |
566 | ␉␉extents = &hfsFile->dataExtents;␊ |
567 | ␉}␊ |
568 | ␊ |
569 | ␉if (offset > fileLength)␊ |
570 | ␉{␊ |
571 | ␉␉printf("ReadFile(HFS%c): Offset is too large.\n", gIsHFSPlus ? "+" : "");␊ |
572 | ␊ |
573 | ␉␉return -1;␊ |
574 | ␉}␊ |
575 | ␊ |
576 | ␉if ((*length == 0) || ((offset + *length) > fileLength))␊ |
577 | ␉{␊ |
578 | ␉␉*length = fileLength - offset;␊ |
579 | ␉}␊ |
580 | ␊ |
581 | /*␊ |
582 | ␉if (*length > kLoadSize)␊ |
583 | ␉{␊ |
584 | ␉␉printf("File is too large.\n");␊ |
585 | ␉␉return -1;␊ |
586 | ␉}␊ |
587 | */␊ |
588 | ␊ |
589 | ␉*length = ReadExtent((char *)extents, fileLength, fileID, offset, *length, (char *)base, 0);␊ |
590 | ␊ |
591 | ␉return 0;␊ |
592 | }␊ |
593 | ␊ |
594 | //==============================================================================␊ |
595 | ␊ |
596 | static long GetCatalogEntryInfo(void * entry, long * flags, u_int32_t * time, FinderInfo * finderInfo, long * infoValid)␊ |
597 | {␊ |
598 | ␉u_int32_t tmpTime = 0;␊ |
599 | ␉long valid = 0;␊ |
600 | ␊ |
601 | ␉// Get information about the file.␊ |
602 | ␊ |
603 | ␉switch ( SWAP_BE16(*(short *)entry) )␊ |
604 | ␉{␊ |
605 | ␉case kHFSFolderRecord :␊ |
606 | ␉␉*flags = kFileTypeDirectory;␊ |
607 | ␉␉tmpTime = SWAP_BE32(((HFSCatalogFolder *)entry)->modifyDate);␊ |
608 | ␉␉break;␊ |
609 | ␊ |
610 | ␉case kHFSPlusFolderRecord :␊ |
611 | ␉␉*flags = kFileTypeDirectory | (SWAP_BE16(((HFSPlusCatalogFolder *)entry)->bsdInfo.fileMode) & kPermMask);␊ |
612 | ␉␉if (SWAP_BE32(((HFSPlusCatalogFolder *)entry)->bsdInfo.ownerID) != 0)␊ |
613 | ␉␉{␊ |
614 | ␉␉␉*flags |= kOwnerNotRoot;␊ |
615 | ␉␉}␊ |
616 | ␉␉tmpTime = SWAP_BE32(((HFSPlusCatalogFolder *)entry)->contentModDate);␊ |
617 | ␉␉break;␊ |
618 | ␊ |
619 | ␉case kHFSFileRecord :␊ |
620 | ␉␉*flags = kFileTypeFlat;␊ |
621 | ␉␉tmpTime = SWAP_BE32(((HFSCatalogFile *)entry)->modifyDate);␊ |
622 | ␉␉if (finderInfo)␊ |
623 | ␉␉{␊ |
624 | ␉␉␉SwapFinderInfo((FndrFileInfo *)finderInfo, &((HFSCatalogFile *)entry)->userInfo);␊ |
625 | ␉␉␉valid = 1;␊ |
626 | ␉␉}␊ |
627 | ␉␉break;␊ |
628 | ␊ |
629 | ␉case kHFSPlusFileRecord :␊ |
630 | ␉␉*flags = kFileTypeFlat | (SWAP_BE16(((HFSPlusCatalogFile *)entry)->bsdInfo.fileMode) & kPermMask);␊ |
631 | ␉␉if (SWAP_BE32(((HFSPlusCatalogFile *)entry)->bsdInfo.ownerID) != 0)␊ |
632 | ␉␉{␊ |
633 | ␉␉␉*flags |= kOwnerNotRoot;␊ |
634 | ␉␉}␊ |
635 | ␉␉tmpTime = SWAP_BE32(((HFSPlusCatalogFile *)entry)->contentModDate);␊ |
636 | ␉␉if (finderInfo)␊ |
637 | ␉␉{␊ |
638 | ␉␉␉SwapFinderInfo((FndrFileInfo *)finderInfo, &((HFSPlusCatalogFile *)entry)->userInfo);␊ |
639 | ␉␉␉valid = 1;␊ |
640 | ␉␉}␊ |
641 | ␉␉break;␊ |
642 | ␊ |
643 | ␉case kHFSFileThreadRecord :␊ |
644 | ␉case kHFSPlusFileThreadRecord :␊ |
645 | ␉case kHFSFolderThreadRecord :␊ |
646 | ␉case kHFSPlusFolderThreadRecord :␊ |
647 | ␉␉*flags = kFileTypeUnknown;␊ |
648 | ␉␉tmpTime = 0;␊ |
649 | ␉␉break;␊ |
650 | ␉default:␊ |
651 | ␉␉break;␊ |
652 | ␉}␊ |
653 | ␊ |
654 | ␉if (time != 0)␊ |
655 | ␉{␊ |
656 | ␉␉// Convert base time from 1904 to 1970.␊ |
657 | ␉␉*time = tmpTime - 2082844800;␊ |
658 | ␉}␊ |
659 | ␊ |
660 | ␉if (infoValid)␊ |
661 | ␉{␊ |
662 | ␉␉*infoValid = valid;␊ |
663 | ␉}␊ |
664 | ␊ |
665 | ␉return 0;␊ |
666 | }␊ |
667 | ␊ |
668 | //==============================================================================␊ |
669 | ␊ |
670 | static long ResolvePathToCatalogEntry(char * filePath, long * flags, void * entry, u_int32_t dirID, long long * dirIndex)␊ |
671 | {␊ |
672 | ␉char␉␉␉*restPath;␊ |
673 | ␉long␉␉␉result, cnt;␊ |
674 | ␉u_int32_t␉␉subFolderID = 0;␊ |
675 | ␉long long␉␉tmpDirIndex;␊ |
676 | ␉HFSPlusCatalogFile␉*hfsPlusFile;␊ |
677 | ␊ |
678 | ␉// Copy the file name to gTempStr␊ |
679 | ␉cnt = 0;␊ |
680 | ␉while ((filePath[cnt] != '/') && (filePath[cnt] != '\0'))␊ |
681 | ␉{␊ |
682 | ␉␉cnt++;␊ |
683 | ␉}␊ |
684 | ␊ |
685 | ␉strlcpy(gTempStr, filePath, cnt+1);␊ |
686 | ␊ |
687 | ␉// Move restPath to the right place.␊ |
688 | ␉if (filePath[cnt] != '\0')␊ |
689 | ␉{␊ |
690 | ␉␉cnt++;␊ |
691 | ␉}␊ |
692 | ␊ |
693 | ␉restPath = filePath + cnt;␊ |
694 | ␊ |
695 | ␉// gTempStr is a name in the current Dir.␊ |
696 | ␉// restPath is the rest of the path if any.␊ |
697 | ␊ |
698 | ␉result = ReadCatalogEntry(gTempStr, dirID, entry, dirIndex);␊ |
699 | ␊ |
700 | ␉if (result == -1)␊ |
701 | ␉{␊ |
702 | ␉␉return -1;␊ |
703 | ␉}␊ |
704 | ␊ |
705 | ␉GetCatalogEntryInfo(entry, flags, 0, 0, 0);␊ |
706 | ␊ |
707 | ␉if ((*flags & kFileTypeMask) == kFileTypeDirectory)␊ |
708 | ␉{␊ |
709 | ␉␉if (gIsHFSPlus)␊ |
710 | ␉␉{␊ |
711 | ␉␉␉subFolderID = SWAP_BE32(((HFSPlusCatalogFolder *)entry)->folderID);␊ |
712 | ␉␉}␊ |
713 | ␉␉else␊ |
714 | ␉␉{␊ |
715 | ␉␉␉subFolderID = SWAP_BE32(((HFSCatalogFolder *)entry)->folderID);␊ |
716 | ␉␉}␊ |
717 | ␉}␊ |
718 | ␊ |
719 | ␉if ((*flags & kFileTypeMask) == kFileTypeDirectory)␊ |
720 | ␉{␊ |
721 | ␉␉result = ResolvePathToCatalogEntry(restPath, flags, entry, subFolderID, dirIndex);␊ |
722 | ␉}␊ |
723 | ␊ |
724 | ␉if (gIsHFSPlus && ((*flags & kFileTypeMask) == kFileTypeFlat))␊ |
725 | ␉{␊ |
726 | ␉␉hfsPlusFile = (HFSPlusCatalogFile *)entry;␊ |
727 | ␊ |
728 | ␉␉if ((SWAP_BE32(hfsPlusFile->userInfo.fdType) == kHardLinkFileType) && (SWAP_BE32(hfsPlusFile->userInfo.fdCreator) == kHFSPlusCreator))␊ |
729 | ␉␉{␊ |
730 | ␉␉␉sprintf(gLinkTemp, "%s/%s%ld", HFSPLUSMETADATAFOLDER, HFS_INODE_PREFIX, SWAP_BE32(hfsPlusFile->bsdInfo.special.iNodeNum));␊ |
731 | ␉␉␉result = ResolvePathToCatalogEntry(gLinkTemp, flags, entry, kHFSRootFolderID, &tmpDirIndex);␊ |
732 | ␉␉}␊ |
733 | ␉}␊ |
734 | ␊ |
735 | ␉return result;␊ |
736 | }␊ |
737 | ␊ |
738 | //==============================================================================␊ |
739 | ␊ |
740 | static long GetCatalogEntry(long long * dirIndex, char ** name, long * flags, u_int32_t * time, FinderInfo * finderInfo, long * infoValid)␊ |
741 | {␊ |
742 | ␉u_int64_t extentSize;␊ |
743 | ␉void *extent;␊ |
744 | ␉char *nodeBuf, *testKey, *entry;␊ |
745 | ␉BTNodeDescriptor *node;␊ |
746 | ␉u_int32_t curNode;␊ |
747 | ␉u_int16_t nodeSize, index;␊ |
748 | ␊ |
749 | ␉if (gIsHFSPlus)␊ |
750 | ␉{␊ |
751 | ␉␉extent = &gHFSPlus->catalogFile.extents;␊ |
752 | ␉␉extentSize = SWAP_BE64(gHFSPlus->catalogFile.logicalSize);␊ |
753 | ␉}␊ |
754 | ␉else␊ |
755 | ␉{␊ |
756 | ␉␉extent = (HFSExtentDescriptor *)&gHFSMDB->drCTExtRec;␊ |
757 | ␉␉extentSize = SWAP_BE32(gHFSMDB->drCTFlSize);␊ |
758 | ␉}␊ |
759 | ␊ |
760 | ␉nodeSize = SWAP_BE16(gBTHeaders[kBTreeCatalog]->nodeSize);␊ |
761 | ␉nodeBuf = (char *)malloc(nodeSize);␊ |
762 | ␉node = (BTNodeDescriptor *)nodeBuf;␊ |
763 | ␊ |
764 | ␉index = (u_int16_t) (*dirIndex % nodeSize);␊ |
765 | ␉curNode = (u_int32_t) (*dirIndex / nodeSize);␊ |
766 | ␊ |
767 | ␉// Read the BTree node and get the record for index.␊ |
768 | ␉ReadExtent(extent, extentSize, kHFSCatalogFileID, (long long) curNode * nodeSize, nodeSize, nodeBuf, 1);␊ |
769 | ␊ |
770 | ␉GetBTreeRecord(index, nodeBuf, nodeSize, &testKey, &entry);␊ |
771 | ␊ |
772 | ␉GetCatalogEntryInfo(entry, flags, time, finderInfo, infoValid);␊ |
773 | ␊ |
774 | ␉// Get the file name.␊ |
775 | ␉if (gIsHFSPlus)␊ |
776 | ␉{␊ |
777 | ␉␉utf_encodestr(((HFSPlusCatalogKey *)testKey)->nodeName.unicode,␊ |
778 | SWAP_BE16(((HFSPlusCatalogKey *)testKey)->nodeName.length),␊ |
779 | (u_int8_t *)gTempStr, 256, OSBigEndian);␊ |
780 | ␉}␊ |
781 | ␉else␊ |
782 | ␉{␊ |
783 | ␉␉strncpy(gTempStr, (const char *)&((HFSCatalogKey *)testKey)->nodeName[1], ((HFSCatalogKey *)testKey)->nodeName[0]);␊ |
784 | ␊ |
785 | ␉␉gTempStr[((HFSCatalogKey *)testKey)->nodeName[0]] = '\0';␊ |
786 | ␉}␊ |
787 | ␉*name = gTempStr;␊ |
788 | ␊ |
789 | ␉// Update dirIndex.␊ |
790 | ␉index++;␊ |
791 | ␊ |
792 | ␉if (index == SWAP_BE16(node->numRecords))␊ |
793 | ␉{␊ |
794 | ␉␉index = 0;␊ |
795 | ␉␉curNode = SWAP_BE32(node->fLink);␊ |
796 | ␉}␊ |
797 | ␉*dirIndex = (long long) curNode * nodeSize + index;␊ |
798 | ␊ |
799 | ␉free(nodeBuf);␊ |
800 | ␊ |
801 | ␉return 0;␊ |
802 | }␊ |
803 | ␊ |
804 | //==============================================================================␊ |
805 | ␊ |
806 | static long ReadCatalogEntry(char * fileName, u_int32_t dirID, void * entry, long long * dirIndex)␊ |
807 | {␊ |
808 | ␉long length = strlen(fileName);␊ |
809 | ␉char key[sizeof(HFSPlusCatalogKey)];␊ |
810 | ␉HFSCatalogKey *hfsKey = (HFSCatalogKey *)key;␊ |
811 | ␉HFSPlusCatalogKey *hfsPlusKey = (HFSPlusCatalogKey *)key;␊ |
812 | ␊ |
813 | ␉// Make the catalog key.␊ |
814 | ␉if ( gIsHFSPlus )␊ |
815 | ␉{␊ |
816 | ␉␉if (length > kHFSPlusMaxFileNameChars)␊ |
817 | ␉␉{␊ |
818 | ␉␉␉length = kHFSPlusMaxFileNameChars;␊ |
819 | ␉␉}␊ |
820 | ␊ |
821 | ␉␉hfsPlusKey->parentID = SWAP_BE32(dirID);␊ |
822 | ␊ |
823 | ␉␉utf_decodestr((u_int8_t *)fileName, hfsPlusKey->nodeName.unicode, &(hfsPlusKey->nodeName.length), 512, OSBigEndian);␊ |
824 | ␉}␊ |
825 | ␉else␊ |
826 | ␉{␊ |
827 | ␉␉if (length > kHFSMaxFileNameChars)␊ |
828 | ␉␉{␊ |
829 | ␉␉␉length = kHFSMaxFileNameChars;␊ |
830 | ␉␉}␊ |
831 | ␊ |
832 | ␉␉hfsKey->parentID = SWAP_BE32(dirID);␊ |
833 | ␊ |
834 | ␉␉hfsKey->nodeName[0] = length;␊ |
835 | ␉␉strncpy((char *)(hfsKey->nodeName + 1), fileName, length);␊ |
836 | ␉}␊ |
837 | ␊ |
838 | ␉return ReadBTreeEntry(kBTreeCatalog, &key, entry, dirIndex);␊ |
839 | }␊ |
840 | ␊ |
841 | //==============================================================================␊ |
842 | ␊ |
843 | static long ReadExtentsEntry(u_int32_t fileID, long startBlock, void * entry)␊ |
844 | {␊ |
845 | ␉char key[sizeof(HFSPlusExtentKey)];␊ |
846 | ␉HFSExtentKey *hfsKey = (HFSExtentKey *)key;␊ |
847 | ␉HFSPlusExtentKey *hfsPlusKey = (HFSPlusExtentKey *)key;␊ |
848 | ␊ |
849 | ␉// Make the extents key.␊ |
850 | ␉if (gIsHFSPlus)␊ |
851 | ␉{␊ |
852 | ␉␉hfsPlusKey->forkType = 0;␊ |
853 | ␉␉hfsPlusKey->fileID = SWAP_BE32(fileID);␊ |
854 | ␉␉hfsPlusKey->startBlock = SWAP_BE32(startBlock);␊ |
855 | ␉}␊ |
856 | ␉else␊ |
857 | ␉{␊ |
858 | ␉␉hfsKey->forkType = 0;␊ |
859 | ␉␉hfsKey->fileID = SWAP_BE32(fileID);␊ |
860 | ␉␉hfsKey->startBlock = SWAP_BE16(startBlock);␊ |
861 | ␉}␊ |
862 | ␊ |
863 | ␉return ReadBTreeEntry(kBTreeExtents, &key, entry, 0);␊ |
864 | }␊ |
865 | ␊ |
866 | //==============================================================================␊ |
867 | ␊ |
868 | static long ReadBTreeEntry(long btree, void * key, char * entry, long long * dirIndex)␊ |
869 | {␊ |
870 | ␉u_int64_t extentSize;␊ |
871 | ␉void *extent;␊ |
872 | ␉u_int16_t extentFile;␊ |
873 | ␉char *nodeBuf;␊ |
874 | ␉BTNodeDescriptor *node;␊ |
875 | ␉long result = 0, entrySize = 0;␊ |
876 | ␉u_int32_t curNode;␊ |
877 | ␉char *testKey, *recordData;␊ |
878 | ␉u_int16_t nodeSize, index = 0, lowerBound, upperBound;␊ |
879 | ␊ |
880 | ␉// Figure out which tree is being looked at.␊ |
881 | ␉if (btree == kBTreeCatalog)␊ |
882 | ␉{␊ |
883 | ␉␉if (gIsHFSPlus) ␊ |
884 | ␉␉{␊ |
885 | ␉␉␉extent = &gHFSPlus->catalogFile.extents;␊ |
886 | ␉␉␉extentSize = SWAP_BE64(gHFSPlus->catalogFile.logicalSize);␊ |
887 | ␉␉}␊ |
888 | ␉␉else␊ |
889 | ␉␉{␊ |
890 | ␉␉␉extent = (HFSExtentDescriptor *)&gHFSMDB->drCTExtRec;␊ |
891 | ␉␉␉extentSize = SWAP_BE32(gHFSMDB->drCTFlSize);␊ |
892 | ␉␉}␊ |
893 | ␉␉extentFile = kHFSCatalogFileID;␊ |
894 | ␉}␊ |
895 | ␉else␊ |
896 | ␉{␊ |
897 | ␉␉if (gIsHFSPlus)␊ |
898 | ␉␉{␊ |
899 | ␉␉␉extent = &gHFSPlus->extentsFile.extents;␊ |
900 | ␉␉␉extentSize = SWAP_BE64(gHFSPlus->extentsFile.logicalSize);␊ |
901 | ␉␉}␊ |
902 | ␉␉else␊ |
903 | ␉␉{␊ |
904 | ␉␉␉extent = (HFSExtentDescriptor *)&gHFSMDB->drXTExtRec;␊ |
905 | ␉␉␉extentSize = SWAP_BE32(gHFSMDB->drXTFlSize);␊ |
906 | ␉␉}␊ |
907 | ␉␉extentFile = kHFSExtentsFileID;␊ |
908 | ␉}␊ |
909 | ␊ |
910 | ␉// Read the BTree Header if needed.␊ |
911 | ␉if (gBTHeaders[btree] == 0)␊ |
912 | ␉{␊ |
913 | ␉␉ReadExtent(extent, extentSize, extentFile, 0, 256, gBTreeHeaderBuffer + btree * 256, 0);␊ |
914 | ␉␉gBTHeaders[btree] = (BTHeaderRec *)(gBTreeHeaderBuffer + btree * 256 + sizeof(BTNodeDescriptor));␊ |
915 | ␊ |
916 | ␉␉if ((gIsHFSPlus && btree == kBTreeCatalog) && (gBTHeaders[btree]->keyCompareType == kHFSBinaryCompare))␊ |
917 | ␉␉{␊ |
918 | ␉␉␉gCaseSensitive = 1;␊ |
919 | ␉␉}␊ |
920 | ␉}␊ |
921 | ␊ |
922 | ␉curNode = SWAP_BE32(gBTHeaders[btree]->rootNode);␊ |
923 | ␉nodeSize = SWAP_BE16(gBTHeaders[btree]->nodeSize);␊ |
924 | ␉nodeBuf = (char *)malloc(nodeSize);␊ |
925 | ␊ |
926 | ␉if (!nodeBuf) ␊ |
927 | ␉{␊ |
928 | ␉␉return -1;␊ |
929 | ␉}␊ |
930 | ␊ |
931 | ␉node = (BTNodeDescriptor *)nodeBuf;␊ |
932 | ␊ |
933 | ␉while (1)␊ |
934 | ␉{␊ |
935 | ␉␉// Read the current node.␊ |
936 | ␉ReadExtent(extent, extentSize, extentFile, (long long) curNode * nodeSize, nodeSize, nodeBuf, 1);␊ |
937 | ␊ |
938 | ␉␉// Find the matching key.␊ |
939 | ␉␉lowerBound = 0;␊ |
940 | ␉␉upperBound = SWAP_BE16(node->numRecords) - 1;␊ |
941 | ␊ |
942 | ␉␉while (lowerBound <= upperBound)␊ |
943 | ␉␉{␊ |
944 | ␉␉␉index = (lowerBound + upperBound) / 2;␊ |
945 | ␊ |
946 | ␉␉␉GetBTreeRecord(index, nodeBuf, nodeSize, &testKey, &recordData);␊ |
947 | ␊ |
948 | ␉␉␉if (gIsHFSPlus)␊ |
949 | ␉␉␉{␊ |
950 | ␉␉␉␉if (btree == kBTreeCatalog)␊ |
951 | ␉␉␉␉{␊ |
952 | ␉␉␉␉␉result = CompareHFSPlusCatalogKeys(key, testKey);␊ |
953 | ␉␉␉␉}␊ |
954 | ␉␉␉␉else␊ |
955 | ␉␉␉␉{␊ |
956 | ␉␉␉␉␉result = CompareHFSPlusExtentsKeys(key, testKey);␊ |
957 | ␉␉␉␉}␊ |
958 | ␉␉␉}␊ |
959 | ␉␉␉else␊ |
960 | ␉␉␉{␊ |
961 | ␉␉␉␉if (btree == kBTreeCatalog)␊ |
962 | ␉␉␉␉{␊ |
963 | ␉␉␉␉␉result = CompareHFSCatalogKeys(key, testKey);␊ |
964 | ␉␉␉␉}␊ |
965 | ␉␉␉␉else␊ |
966 | ␉␉␉␉{␊ |
967 | ␉␉␉␉␉result = CompareHFSExtentsKeys(key, testKey);␊ |
968 | ␉␉␉␉}␊ |
969 | ␉␉␉}␊ |
970 | ␊ |
971 | ␉␉␉if (result < 0)␊ |
972 | ␉␉␉{␊ |
973 | ␉␉␉␉upperBound = index - 1;␉// search < trial␊ |
974 | ␉␉␉}␊ |
975 | ␉␉␉else if (result > 0)␊ |
976 | ␉␉␉{␊ |
977 | ␉␉␉␉lowerBound = index + 1;␉// search > trial␊ |
978 | ␉␉␉}␊ |
979 | ␉␉␉else␊ |
980 | ␉␉␉{␊ |
981 | ␉␉␉␉break;␉␉␉// search = trial␊ |
982 | ␉␉␉}␊ |
983 | ␉␉}␊ |
984 | ␊ |
985 | ␉␉if (result < 0)␊ |
986 | ␉␉{␊ |
987 | ␉␉␉index = upperBound;␊ |
988 | ␉␉␉GetBTreeRecord(index, nodeBuf, nodeSize, &testKey, &recordData);␊ |
989 | ␉␉}␊ |
990 | ␊ |
991 | ␉␉// Found the closest key... Recurse on it if this is an index node.␊ |
992 | ␉␉if (node->kind == kBTIndexNode)␊ |
993 | ␉␉{␊ |
994 | ␉␉␉curNode = SWAP_BE32( *((long *)recordData) );␊ |
995 | ␉␉}␊ |
996 | ␉␉else␊ |
997 | ␉␉{␊ |
998 | ␉␉␉break;␊ |
999 | ␉␉}␊ |
1000 | ␉}␊ |
1001 | ␊ |
1002 | ␉// Return error if the file was not found.␊ |
1003 | ␉if (result != 0 || !recordData)␊ |
1004 | ␉{␊ |
1005 | ␉␉free(nodeBuf);␊ |
1006 | ␉␉return -1;␊ |
1007 | ␉}␊ |
1008 | ␊ |
1009 | ␉if (btree == kBTreeCatalog)␊ |
1010 | ␉{␊ |
1011 | ␉␉switch (SWAP_BE16(*(short *)recordData))␊ |
1012 | ␉␉{␊ |
1013 | ␉␉␉case kHFSFolderRecord : entrySize = 70;␊ |
1014 | ␉␉␉␉break;␊ |
1015 | ␉␉␉case kHFSFileRecord : entrySize = 102;␊ |
1016 | ␉␉␉␉break;␊ |
1017 | ␉␉␉case kHFSFolderThreadRecord : entrySize = 46;␊ |
1018 | ␉␉␉␉break;␊ |
1019 | ␉␉␉case kHFSFileThreadRecord : entrySize = 46;␊ |
1020 | ␉␉␉␉break;␊ |
1021 | ␉␉␉case kHFSPlusFolderRecord : entrySize = 88;␊ |
1022 | ␉␉␉␉break;␊ |
1023 | ␉␉␉case kHFSPlusFileRecord : entrySize = 248;␊ |
1024 | ␉␉␉␉break;␊ |
1025 | ␉␉␉case kHFSPlusFolderThreadRecord : entrySize = 264;␊ |
1026 | ␉␉␉␉break;␊ |
1027 | ␉␉␉case kHFSPlusFileThreadRecord : entrySize = 264;␊ |
1028 | ␉␉␉␉break;␊ |
1029 | ␉␉␉default:␊ |
1030 | ␉␉␉␉break;␊ |
1031 | ␉␉}␊ |
1032 | ␉}␊ |
1033 | ␉else␊ |
1034 | ␉{␊ |
1035 | ␉␉if (gIsHFSPlus)␊ |
1036 | ␉␉{␊ |
1037 | ␉␉␉entrySize = sizeof(HFSPlusExtentRecord);␊ |
1038 | ␉␉}␊ |
1039 | ␉␉else␊ |
1040 | ␉␉{␊ |
1041 | ␉␉␉entrySize = sizeof(HFSExtentRecord);␊ |
1042 | ␉␉}␊ |
1043 | ␉}␊ |
1044 | ␊ |
1045 | ␉bcopy(recordData, entry, entrySize);␊ |
1046 | ␊ |
1047 | ␉// Update dirIndex.␊ |
1048 | ␉if (dirIndex != 0)␊ |
1049 | ␉{␊ |
1050 | ␉␉index++;␊ |
1051 | ␉␉if (index == SWAP_BE16(node->numRecords))␊ |
1052 | ␉␉{␊ |
1053 | ␉␉␉index = 0;␊ |
1054 | ␉␉␉curNode = SWAP_BE32(node->fLink);␊ |
1055 | ␉␉}␊ |
1056 | ␉␉*dirIndex = (long long) curNode * nodeSize + index;␊ |
1057 | ␉}␊ |
1058 | ␊ |
1059 | ␉free(nodeBuf);␊ |
1060 | ␊ |
1061 | ␉return 0;␊ |
1062 | }␊ |
1063 | ␊ |
1064 | //==============================================================================␊ |
1065 | ␊ |
1066 | static void GetBTreeRecord(u_int16_t index, char * nodeBuffer, u_int16_t nodeSize, char ** key, char ** data)␊ |
1067 | {␊ |
1068 | ␉u_int16_t recordOffset, keySize;␊ |
1069 | ␊ |
1070 | ␉recordOffset = SWAP_BE16(*((u_int16_t *)(nodeBuffer + (nodeSize - 2 * index - 2))));␊ |
1071 | ␉*key = nodeBuffer + recordOffset;␊ |
1072 | ␊ |
1073 | ␉if (gIsHFSPlus)␊ |
1074 | ␉{␊ |
1075 | ␉␉keySize = SWAP_BE16(*(u_int16_t *)*key);␊ |
1076 | ␉␉*data = *key + 2 + keySize;␊ |
1077 | ␉}␊ |
1078 | ␉else␊ |
1079 | ␉{␊ |
1080 | ␉␉keySize = **key;␊ |
1081 | ␉␉*data = *key + 2 + keySize - (keySize & 1);␊ |
1082 | ␉}␊ |
1083 | }␊ |
1084 | ␊ |
1085 | //==============================================================================␊ |
1086 | ␊ |
1087 | static long ReadExtent(char * extent, u_int64_t extentSize, u_int32_t extentFile, u_int64_t offset, u_int64_t size, void * buffer, long cache)␊ |
1088 | {␊ |
1089 | ␉u_int64_t lastOffset;␊ |
1090 | ␉u_int64_t blockNumber, countedBlocks = 0;␊ |
1091 | ␉u_int64_t nextExtent = 0, sizeRead = 0, readSize;␊ |
1092 | ␉u_int64_t nextExtentBlock, currentExtentBlock = 0;␊ |
1093 | ␉u_int64_t readOffset;␊ |
1094 | ␉u_int64_t extentDensity, sizeofExtent, currentExtentSize;␊ |
1095 | ␉char *currentExtent, *extentBuffer = 0, *bufferPos = buffer;␊ |
1096 | ␊ |
1097 | ␉if (offset >= extentSize)␊ |
1098 | ␉{␊ |
1099 | ␉␉return 0;␊ |
1100 | ␉}␊ |
1101 | ␊ |
1102 | ␉if (gIsHFSPlus)␊ |
1103 | ␉{␊ |
1104 | ␉␉extentDensity = kHFSPlusExtentDensity;␊ |
1105 | ␉␉sizeofExtent = sizeof(HFSPlusExtentDescriptor);␊ |
1106 | ␉}␊ |
1107 | ␉else␊ |
1108 | ␉{␊ |
1109 | ␉␉extentDensity = kHFSExtentDensity;␊ |
1110 | ␉␉sizeofExtent = sizeof(HFSExtentDescriptor);␊ |
1111 | ␉}␊ |
1112 | ␊ |
1113 | ␉lastOffset = offset + size;␊ |
1114 | ␊ |
1115 | ␉while (offset < lastOffset)␊ |
1116 | ␉{␊ |
1117 | ␉␉blockNumber = offset / gBlockSize;␊ |
1118 | ␊ |
1119 | ␉␉// Find the extent for the offset.␊ |
1120 | ␉␉for (; ; nextExtent++)␊ |
1121 | ␉␉{␊ |
1122 | ␉␉␉if (nextExtent < extentDensity)␊ |
1123 | ␉␉␉{␊ |
1124 | ␉␉␉␉if ((countedBlocks + GetExtentSize(extent, nextExtent) -1) < blockNumber)␊ |
1125 | ␉␉␉␉{␊ |
1126 | ␉␉␉␉␉countedBlocks += GetExtentSize(extent, nextExtent);␊ |
1127 | ␉␉␉␉␉continue;␊ |
1128 | ␉␉␉␉}␊ |
1129 | ␊ |
1130 | ␉␉␉␉currentExtent = extent + nextExtent * sizeofExtent;␊ |
1131 | ␉␉␉␉break;␊ |
1132 | ␉␉␉}␊ |
1133 | ␊ |
1134 | ␉␉␉if (extentBuffer == 0)␊ |
1135 | ␉␉␉{␊ |
1136 | ␉␉␉␉extentBuffer = malloc(sizeofExtent * extentDensity);␊ |
1137 | ␊ |
1138 | ␉␉␉␉if (extentBuffer == 0)␊ |
1139 | ␉␉␉␉{␊ |
1140 | ␉␉␉␉␉return -1;␊ |
1141 | ␉␉␉␉}␊ |
1142 | ␉␉␉}␊ |
1143 | ␊ |
1144 | ␉␉␉nextExtentBlock = nextExtent / extentDensity;␊ |
1145 | ␊ |
1146 | ␉␉␉if (currentExtentBlock != nextExtentBlock)␊ |
1147 | ␉␉␉{␊ |
1148 | ␉␉␉␉ReadExtentsEntry(extentFile, countedBlocks, extentBuffer);␊ |
1149 | ␉␉␉␉currentExtentBlock = nextExtentBlock;␊ |
1150 | ␉␉␉}␊ |
1151 | ␊ |
1152 | ␉␉␉currentExtentSize = GetExtentSize(extentBuffer, nextExtent % extentDensity);␊ |
1153 | ␊ |
1154 | ␉␉␉if ((countedBlocks + currentExtentSize - 1) >= blockNumber)␊ |
1155 | ␉␉␉{␊ |
1156 | ␉␉␉␉currentExtent = extentBuffer + sizeofExtent * (nextExtent % extentDensity);␊ |
1157 | ␉␉␉␉break;␊ |
1158 | ␉␉␉}␊ |
1159 | ␊ |
1160 | ␉␉␉countedBlocks += currentExtentSize;␊ |
1161 | ␉␉}␊ |
1162 | ␊ |
1163 | ␉␉readOffset = ((blockNumber - countedBlocks) * gBlockSize) + (offset % gBlockSize);␊ |
1164 | ␊ |
1165 | ␉␉// MacWen: fix overflow in multiplication by forcing 64bit multiplication␊ |
1166 | ␉␉readSize = (long long)GetExtentSize(currentExtent, 0) * gBlockSize - readOffset;␊ |
1167 | ␊ |
1168 | ␉␉if (readSize > (size - sizeRead))␊ |
1169 | ␉␉{␊ |
1170 | ␉␉␉readSize = size - sizeRead;␊ |
1171 | ␉␉}␊ |
1172 | ␊ |
1173 | ␉␉readOffset += (long long)GetExtentStart(currentExtent, 0) * gBlockSize;␊ |
1174 | ␊ |
1175 | ␉␉CacheRead(gCurrentIH, bufferPos, gAllocationOffset + readOffset, readSize, cache);␊ |
1176 | ␊ |
1177 | ␉␉sizeRead += readSize;␊ |
1178 | ␉␉offset += readSize;␊ |
1179 | ␉␉bufferPos += readSize;␊ |
1180 | ␉}␊ |
1181 | ␊ |
1182 | ␉if (extentBuffer)␊ |
1183 | ␉{␊ |
1184 | ␉␉free(extentBuffer);␊ |
1185 | ␉}␊ |
1186 | ␊ |
1187 | ␉return sizeRead;␊ |
1188 | }␊ |
1189 | ␊ |
1190 | //==============================================================================␊ |
1191 | ␊ |
1192 | static u_int32_t GetExtentStart(void * extents, u_int32_t index)␊ |
1193 | {␊ |
1194 | ␉u_int32_t start;␊ |
1195 | ␊ |
1196 | ␉HFSExtentDescriptor␉␉*hfsExtents␉␉= extents;␊ |
1197 | ␉HFSPlusExtentDescriptor␉*hfsPlusExtents␉= extents;␊ |
1198 | ␊ |
1199 | ␉if (gIsHFSPlus)␊ |
1200 | ␉{␊ |
1201 | ␉␉start = SWAP_BE32(hfsPlusExtents[index].startBlock);␊ |
1202 | ␉}␊ |
1203 | ␉else␊ |
1204 | ␉{␊ |
1205 | ␉␉start = SWAP_BE16(hfsExtents[index].startBlock);␊ |
1206 | ␉}␊ |
1207 | ␊ |
1208 | ␉return start;␊ |
1209 | }␊ |
1210 | ␊ |
1211 | //==============================================================================␊ |
1212 | ␊ |
1213 | static u_int32_t GetExtentSize(void * extents, u_int32_t index)␊ |
1214 | {␊ |
1215 | ␉u_int32_t size = 0;␊ |
1216 | ␊ |
1217 | ␉HFSExtentDescriptor *hfsExtents = extents;␊ |
1218 | ␉HFSPlusExtentDescriptor *hfsPlusExtents = extents;␊ |
1219 | ␊ |
1220 | ␉if (gIsHFSPlus)␊ |
1221 | ␉{␊ |
1222 | ␉␉size = SWAP_BE32(hfsPlusExtents[index].blockCount);␊ |
1223 | ␉}␊ |
1224 | ␉else␊ |
1225 | ␉{␊ |
1226 | ␉␉size = SWAP_BE16(hfsExtents[index].blockCount);␊ |
1227 | ␉}␊ |
1228 | ␊ |
1229 | ␉return size;␊ |
1230 | }␊ |
1231 | ␊ |
1232 | //==============================================================================␊ |
1233 | ␊ |
1234 | static long CompareHFSCatalogKeys(void * key, void * testKey)␊ |
1235 | {␊ |
1236 | ␉HFSCatalogKey *searchKey, *trialKey;␊ |
1237 | ␉long result, searchParentID, trialParentID;␊ |
1238 | ␊ |
1239 | ␉searchKey = key;␊ |
1240 | ␉trialKey = testKey;␊ |
1241 | ␊ |
1242 | ␉searchParentID = SWAP_BE32(searchKey->parentID);␊ |
1243 | ␉trialParentID = SWAP_BE32(trialKey->parentID);␊ |
1244 | ␊ |
1245 | ␉// parent dirID is unsigned␊ |
1246 | ␉if (searchParentID > trialParentID)␊ |
1247 | ␉{␊ |
1248 | ␉␉result = 1;␊ |
1249 | ␉}␊ |
1250 | ␉else if (searchParentID < trialParentID)␊ |
1251 | ␉{␊ |
1252 | ␉␉result = -1;␊ |
1253 | ␉}␊ |
1254 | ␉else␊ |
1255 | ␉{␊ |
1256 | ␉␉// parent dirID's are equal, compare names␊ |
1257 | ␉␉result = FastRelString(searchKey->nodeName, trialKey->nodeName);␊ |
1258 | ␉}␊ |
1259 | ␊ |
1260 | ␉return result;␊ |
1261 | }␊ |
1262 | ␊ |
1263 | //==============================================================================␊ |
1264 | ␊ |
1265 | static long CompareHFSPlusCatalogKeys(void * key, void * testKey)␊ |
1266 | {␊ |
1267 | ␉HFSPlusCatalogKey *searchKey, *trialKey;␊ |
1268 | ␉long result, searchParentID, trialParentID;␊ |
1269 | ␊ |
1270 | ␉searchKey = key;␊ |
1271 | ␉trialKey = testKey;␊ |
1272 | ␊ |
1273 | ␉searchParentID = SWAP_BE32(searchKey->parentID);␊ |
1274 | ␉trialParentID = SWAP_BE32(trialKey->parentID);␊ |
1275 | ␊ |
1276 | ␉// parent dirID is unsigned␊ |
1277 | ␉if (searchParentID > trialParentID)␊ |
1278 | ␉{␊ |
1279 | ␉␉result = 1;␊ |
1280 | ␉}␊ |
1281 | ␉else if (searchParentID < trialParentID) ␊ |
1282 | ␉{␊ |
1283 | ␉␉result = -1;␊ |
1284 | ␉}␊ |
1285 | ␉else␊ |
1286 | ␉{␊ |
1287 | ␉␉// parent dirID's are equal, compare names␊ |
1288 | ␉␉if ((searchKey->nodeName.length == 0) || (trialKey->nodeName.length == 0))␊ |
1289 | ␉␉{␊ |
1290 | ␉␉␉result = searchKey->nodeName.length - trialKey->nodeName.length;␊ |
1291 | ␉␉}␊ |
1292 | ␉␉else if (gCaseSensitive)␊ |
1293 | ␉␉{␊ |
1294 | ␉␉␉result = BinaryUnicodeCompare(&searchKey->nodeName.unicode[0],␊ |
1295 | SWAP_BE16(searchKey->nodeName.length),␊ |
1296 | &trialKey->nodeName.unicode[0],␊ |
1297 | SWAP_BE16(trialKey->nodeName.length));␊ |
1298 | ␉␉}␊ |
1299 | ␉␉else␊ |
1300 | ␉␉{␊ |
1301 | ␉␉␉result = FastUnicodeCompare(&searchKey->nodeName.unicode[0],␊ |
1302 | SWAP_BE16(searchKey->nodeName.length),␊ |
1303 | &trialKey->nodeName.unicode[0],␊ |
1304 | SWAP_BE16(trialKey->nodeName.length), OSBigEndian);␊ |
1305 | ␉␉}␊ |
1306 | ␉}␊ |
1307 | ␊ |
1308 | ␉return result;␊ |
1309 | }␊ |
1310 | ␊ |
1311 | //==============================================================================␊ |
1312 | ␊ |
1313 | static long CompareHFSExtentsKeys(void * key, void * testKey)␊ |
1314 | {␊ |
1315 | ␉HFSExtentKey *searchKey, *trialKey;␊ |
1316 | ␉long result;␊ |
1317 | ␊ |
1318 | ␉searchKey = key;␊ |
1319 | ␉trialKey = testKey;␊ |
1320 | ␊ |
1321 | ␉// assume searchKey < trialKey␊ |
1322 | ␉result = -1; ␊ |
1323 | ␊ |
1324 | ␉if (searchKey->fileID == trialKey->fileID)␊ |
1325 | ␉{␊ |
1326 | ␉␉// FileNum's are equal; compare fork types␊ |
1327 | ␉␉if (searchKey->forkType == trialKey->forkType)␊ |
1328 | ␉␉{␊ |
1329 | ␉␉␉// Fork types are equal; compare allocation block number␊ |
1330 | ␉␉␉if (searchKey->startBlock == trialKey->startBlock)␊ |
1331 | ␉␉␉{␊ |
1332 | ␉␉␉␉// Everything is equal␊ |
1333 | ␉␉␉␉result = 0;␊ |
1334 | ␉␉␉}␊ |
1335 | ␉␉␉else␊ |
1336 | ␉␉␉{␊ |
1337 | ␉␉␉␉// Allocation block numbers differ; determine sign␊ |
1338 | ␉␉␉␉if (SWAP_BE16(searchKey->startBlock) > SWAP_BE16(trialKey->startBlock))␊ |
1339 | ␉␉␉␉{␊ |
1340 | ␉␉␉␉␉result = 1;␊ |
1341 | ␉␉␉␉}␊ |
1342 | ␉␉␉}␊ |
1343 | ␉}␊ |
1344 | ␉␉else␊ |
1345 | ␉␉{␊ |
1346 | ␉␉␉// Fork types differ; determine sign␊ |
1347 | ␉␉␉if (searchKey->forkType > trialKey->forkType)␊ |
1348 | ␉␉␉{␊ |
1349 | ␉␉␉␉result = 1;␊ |
1350 | ␉␉␉}␊ |
1351 | ␉␉}␊ |
1352 | ␉}␊ |
1353 | ␉else␊ |
1354 | ␉{␊ |
1355 | ␉␉// FileNums differ; determine sign␊ |
1356 | ␉␉if (SWAP_BE32(searchKey->fileID) > SWAP_BE32(trialKey->fileID))␊ |
1357 | ␉␉{␊ |
1358 | ␉␉␉result = 1;␊ |
1359 | ␉␉}␊ |
1360 | ␉}␊ |
1361 | ␊ |
1362 | ␉return result;␊ |
1363 | }␊ |
1364 | ␊ |
1365 | ␊ |
1366 | //==============================================================================␊ |
1367 | ␊ |
1368 | static long CompareHFSPlusExtentsKeys(void * key, void * testKey)␊ |
1369 | {␊ |
1370 | ␉HFSPlusExtentKey␉*searchKey, *trialKey;␊ |
1371 | ␊ |
1372 | ␉long␉␉␉result;␊ |
1373 | ␊ |
1374 | ␉searchKey = key;␊ |
1375 | ␉trialKey = testKey;␊ |
1376 | ␊ |
1377 | ␉// assume searchKey < trialKey␊ |
1378 | ␉result = -1;␊ |
1379 | ␊ |
1380 | ␉if (searchKey->fileID == trialKey->fileID)␊ |
1381 | ␉{␊ |
1382 | ␉␉// FileNum's are equal; compare fork types␊ |
1383 | ␉␉if (searchKey->forkType == trialKey->forkType)␊ |
1384 | ␉␉{␊ |
1385 | ␉␉␉// Fork types are equal; compare allocation block number␊ |
1386 | ␉␉␉if (searchKey->startBlock == trialKey->startBlock)␊ |
1387 | ␉␉␉{␊ |
1388 | ␉␉␉␉// Everything is equal␊ |
1389 | ␉␉␉␉result = 0;␊ |
1390 | ␉␉␉}␊ |
1391 | ␉␉␉else␊ |
1392 | ␉␉␉{␊ |
1393 | ␉␉␉␉// Allocation block numbers differ; determine sign␊ |
1394 | ␉␉␉␉if (SWAP_BE32(searchKey->startBlock) > SWAP_BE32(trialKey->startBlock))␊ |
1395 | ␉␉␉␉{␊ |
1396 | ␉␉␉␉␉result = 1;␊ |
1397 | ␉␉␉␉}␊ |
1398 | ␉␉␉}␊ |
1399 | ␉␉}␊ |
1400 | ␉␉else␊ |
1401 | ␉␉{␊ |
1402 | ␉␉␉// Fork types differ; determine sign␊ |
1403 | ␉␉␉if (searchKey->forkType > trialKey->forkType)␊ |
1404 | ␉␉␉{␊ |
1405 | ␉␉␉␉result = 1;␊ |
1406 | ␉␉␉}␊ |
1407 | ␉␉}␊ |
1408 | ␉}␊ |
1409 | ␉else␊ |
1410 | ␉{␊ |
1411 | ␉␉// FileNums differ; determine sign␊ |
1412 | ␉␉if (SWAP_BE32(searchKey->fileID) > SWAP_BE32(trialKey->fileID))␊ |
1413 | ␉␉{␊ |
1414 | ␉␉␉result = 1;␊ |
1415 | ␉␉}␊ |
1416 | ␉}␊ |
1417 | ␊ |
1418 | return result;␊ |
1419 | }␊ |
1420 | ␊ |
1421 | |