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