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 long gBlockSize;␊ |
49 | static long 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 gBlockSize;␊ |
66 | static long gCaseSensitive;␊ |
67 | static long 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, uint64_t *length, void *base, uint64_t offset);␊ |
80 | static long GetCatalogEntryInfo(void *entry, long *flags, long *time,␊ |
81 | FinderInfo *finderInfo, long *infoValid);␊ |
82 | static long ResolvePathToCatalogEntry(char *filePath, long *flags,␊ |
83 | void *entry, long dirID, long long *dirIndex);␊ |
84 | ␊ |
85 | static long GetCatalogEntry(long long *dirIndex, char **name,␊ |
86 | long *flags, long *time,␊ |
87 | FinderInfo *finderInfo, long *infoValid);␊ |
88 | static long ReadCatalogEntry(char *fileName, long dirID, void *entry,␊ |
89 | long long *dirIndex);␊ |
90 | static long ReadExtentsEntry(long fileID, long startBlock, void *entry);␊ |
91 | ␊ |
92 | static long ReadBTreeEntry(long btree, void *key, char *entry, long long *dirIndex);␊ |
93 | static void GetBTreeRecord(long index, char *nodeBuffer, long nodeSize,␊ |
94 | char **key, char **data);␊ |
95 | ␊ |
96 | static long ReadExtent(char *extent, uint64_t extentSize, long extentFile,␊ |
97 | uint64_t offset, uint64_t size, void *buffer, long cache);␊ |
98 | ␊ |
99 | static long GetExtentStart(void *extents, long index);␊ |
100 | static long GetExtentSize(void *extents, long 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 | static void SwapFinderInfo(FndrFileInfo *dst, FndrFileInfo *src)␊ |
113 | {␊ |
114 | dst->fdType = SWAP_BE32(src->fdType);␊ |
115 | dst->fdCreator = SWAP_BE32(src->fdCreator);␊ |
116 | dst->fdFlags = SWAP_BE16(src->fdFlags);␊ |
117 | // Don't bother with location␊ |
118 | }␊ |
119 | ␊ |
120 | void HFSFree(CICell ih)␊ |
121 | {␊ |
122 | if(gCurrentIH == ih)␊ |
123 | gCurrentIH = 0;␊ |
124 | free(ih);␊ |
125 | }␊ |
126 | ␊ |
127 | bool HFSProbe (const void *buf)␊ |
128 | {␊ |
129 | ␉const HFSMasterDirectoryBlock *mdb;␊ |
130 | ␉const HFSPlusVolumeHeader *header;␊ |
131 | ␉mdb=(const HFSMasterDirectoryBlock *)(((const char*)buf)+kMDBBaseOffset);␊ |
132 | ␉header=(const HFSPlusVolumeHeader *)(((const char*)buf)+kMDBBaseOffset);␊ |
133 | ␉␊ |
134 | ␉if ( SWAP_BE16(mdb->drSigWord) == kHFSSigWord )␊ |
135 | ␉␉return true;␊ |
136 | ␉if (SWAP_BE16(header->signature) != kHFSPlusSigWord &&␊ |
137 | SWAP_BE16(header->signature) != kHFSXSigWord)␊ |
138 | ␉␉return false;␊ |
139 | ␉return true;␊ |
140 | }␊ |
141 | ␊ |
142 | long HFSInitPartition(CICell ih)␊ |
143 | {␊ |
144 | long extentSize, extentFile, nodeSize;␊ |
145 | void *extent;␊ |
146 | ␊ |
147 | if (ih == gCurrentIH) {␊ |
148 | #ifdef __i386__␊ |
149 | CacheInit(ih, gCacheBlockSize);␊ |
150 | #endif␊ |
151 | return 0;␊ |
152 | }␊ |
153 | ␊ |
154 | #ifdef __i386__␊ |
155 | if (!gTempStr) gTempStr = (char *)malloc(4096);␊ |
156 | if (!gLinkTemp) gLinkTemp = (char *)malloc(64);␊ |
157 | if (!gBTreeHeaderBuffer) gBTreeHeaderBuffer = (char *)malloc(512);␊ |
158 | if (!gHFSMdbVib) {␊ |
159 | gHFSMdbVib = (char *)malloc(kBlockSize);␊ |
160 | gHFSMDB = (HFSMasterDirectoryBlock *)gHFSMdbVib;␊ |
161 | }␊ |
162 | if (!gHFSPlusHeader) {␊ |
163 | gHFSPlusHeader = (char *)malloc(kBlockSize);␊ |
164 | gHFSPlus = (HFSPlusVolumeHeader *)gHFSPlusHeader;␊ |
165 | }␊ |
166 | if (!gTempStr || !gLinkTemp || !gBTreeHeaderBuffer ||␊ |
167 | !gHFSMdbVib || !gHFSPlusHeader) return -1;␊ |
168 | #endif /* __i386__ */␊ |
169 | ␊ |
170 | gAllocationOffset = 0;␊ |
171 | gIsHFSPlus = 0;␊ |
172 | gCaseSensitive = 0;␊ |
173 | gBTHeaders[0] = 0;␊ |
174 | gBTHeaders[1] = 0;␊ |
175 | ␊ |
176 | // Look for the HFS MDB␊ |
177 | Seek(ih, kMDBBaseOffset);␊ |
178 | Read(ih, (long)gHFSMdbVib, kBlockSize);␊ |
179 | ␊ |
180 | if ( SWAP_BE16(gHFSMDB->drSigWord) == kHFSSigWord ) {␊ |
181 | gAllocationOffset = SWAP_BE16(gHFSMDB->drAlBlSt) * kBlockSize;␊ |
182 | ␊ |
183 | // See if it is HFSPlus␊ |
184 | if (SWAP_BE16(gHFSMDB->drEmbedSigWord) != kHFSPlusSigWord) {␊ |
185 | // Normal HFS;␊ |
186 | gCacheBlockSize = gBlockSize = SWAP_BE32(gHFSMDB->drAlBlkSiz);␊ |
187 | CacheInit(ih, gCacheBlockSize);␊ |
188 | gCurrentIH = ih;␊ |
189 | ␊ |
190 | // grab the 64 bit volume ID␊ |
191 | bcopy(&gHFSMDB->drFndrInfo[6], &gVolID, 8);␊ |
192 | ␊ |
193 | // Get the Catalog BTree node size.␊ |
194 | extent = (HFSExtentDescriptor *)&gHFSMDB->drCTExtRec;␊ |
195 | extentSize = SWAP_BE32(gHFSMDB->drCTFlSize);␊ |
196 | extentFile = kHFSCatalogFileID;␊ |
197 | ReadExtent(extent, extentSize, extentFile, 0, 256,␊ |
198 | gBTreeHeaderBuffer + kBTreeCatalog * 256, 0);␊ |
199 | ␊ |
200 | nodeSize = SWAP_BE16(((BTHeaderRec *)(gBTreeHeaderBuffer + kBTreeCatalog * 256 + ␊ |
201 | sizeof(BTNodeDescriptor)))->nodeSize);␊ |
202 | ␊ |
203 | // If the BTree node size is larger than the block size, reset the cache.␊ |
204 | if (nodeSize > gBlockSize) {␊ |
205 | gCacheBlockSize = nodeSize;␊ |
206 | CacheInit(ih, gCacheBlockSize);␊ |
207 | }␊ |
208 | ␊ |
209 | return 0;␊ |
210 | }␊ |
211 | ␊ |
212 | // Calculate the offset to the embeded HFSPlus volume.␊ |
213 | gAllocationOffset += (long long)SWAP_BE16(gHFSMDB->drEmbedExtent.startBlock) *␊ |
214 | SWAP_BE32(gHFSMDB->drAlBlkSiz);␊ |
215 | }␊ |
216 | ␊ |
217 | // Look for the HFSPlus Header␊ |
218 | Seek(ih, gAllocationOffset + kMDBBaseOffset);␊ |
219 | Read(ih, (long)gHFSPlusHeader, kBlockSize);␊ |
220 | ␊ |
221 | // Not a HFS+ or HFSX volume.␊ |
222 | if (SWAP_BE16(gHFSPlus->signature) != kHFSPlusSigWord &&␊ |
223 | SWAP_BE16(gHFSPlus->signature) != kHFSXSigWord) {␊ |
224 | ␉verbose("HFS signature was not present.\n");␊ |
225 | gCurrentIH = 0;␊ |
226 | ␉return -1;␊ |
227 | }␊ |
228 | ␊ |
229 | gIsHFSPlus = 1;␊ |
230 | gCacheBlockSize = gBlockSize = SWAP_BE32(gHFSPlus->blockSize);␊ |
231 | CacheInit(ih, gCacheBlockSize);␊ |
232 | gCurrentIH = ih;␊ |
233 | ␊ |
234 | ␉ih->modTime = SWAP_BE32(gHFSPlus->modifyDate) - 2082844800;␊ |
235 | ␉␊ |
236 | // grab the 64 bit volume ID␊ |
237 | bcopy(&gHFSPlus->finderInfo[24], &gVolID, 8);␊ |
238 | ␊ |
239 | // Get the Catalog BTree node size.␊ |
240 | extent = &gHFSPlus->catalogFile.extents;␊ |
241 | extentSize = SWAP_BE64(gHFSPlus->catalogFile.logicalSize);␊ |
242 | extentFile = kHFSCatalogFileID;␊ |
243 | ␊ |
244 | ReadExtent(extent, extentSize, extentFile, 0, 256,␊ |
245 | gBTreeHeaderBuffer + kBTreeCatalog * 256, 0);␊ |
246 | ␊ |
247 | nodeSize = SWAP_BE16(((BTHeaderRec *)(gBTreeHeaderBuffer + kBTreeCatalog * 256 +␊ |
248 | sizeof(BTNodeDescriptor)))->nodeSize);␊ |
249 | ␊ |
250 | // If the BTree node size is larger than the block size, reset the cache.␊ |
251 | if (nodeSize > gBlockSize) {␊ |
252 | gCacheBlockSize = nodeSize;␊ |
253 | CacheInit(ih, gCacheBlockSize);␊ |
254 | }␊ |
255 | ␊ |
256 | return 0;␊ |
257 | }␊ |
258 | ␊ |
259 | long HFSLoadFile(CICell ih, char * filePath)␊ |
260 | {␊ |
261 | return HFSReadFile(ih, filePath, (void *)gFSLoadAddress, 0, 0);␊ |
262 | }␊ |
263 | ␊ |
264 | long HFSReadFile(CICell ih, char * filePath, void *base, uint64_t offset, uint64_t length)␊ |
265 | {␊ |
266 | char entry[512];␊ |
267 | long dirID, result, flags;␊ |
268 | ␊ |
269 | if (HFSInitPartition(ih) == -1) return -1;␊ |
270 | ␊ |
271 | dirID = kHFSRootFolderID;␊ |
272 | // Skip a lead '\'. Start in the system folder if there are two.␊ |
273 | if (filePath[0] == '/') {␊ |
274 | if (filePath[1] == '/') {␊ |
275 | if (gIsHFSPlus) dirID = SWAP_BE32(((long *)gHFSPlus->finderInfo)[5]);␊ |
276 | else dirID = SWAP_BE32(gHFSMDB->drFndrInfo[5]);␊ |
277 | if (dirID == 0) {␊ |
278 | ␉␉return -1;␊ |
279 | ␉ }␊ |
280 | filePath++;␊ |
281 | }␊ |
282 | filePath++;␊ |
283 | }␊ |
284 | ␊ |
285 | result = ResolvePathToCatalogEntry(filePath, &flags, entry, dirID, 0);␊ |
286 | if ((result == -1) || ((flags & kFileTypeMask) != kFileTypeFlat)) {␊ |
287 | ␉return -1;␊ |
288 | }␊ |
289 | ␊ |
290 | #if UNUSED␊ |
291 | // Not yet for Intel. System.config/Default.table will fail this check.␊ |
292 | // Check file owner and permissions.␊ |
293 | if (flags & (kOwnerNotRoot | kPermGroupWrite | kPermOtherWrite)) return -1;␊ |
294 | #endif␊ |
295 | ␊ |
296 | result = ReadFile(entry, &length, base, offset);␊ |
297 | if (result == -1) {␊ |
298 | ␉return -1;␊ |
299 | }␊ |
300 | ␊ |
301 | verbose("Loaded HFS%s file: [%s] %d bytes from %x.\n",␊ |
302 | (gIsHFSPlus ? "+" : ""), filePath, (uint32_t)length, ih);␊ |
303 | ␉␊ |
304 | return length;␊ |
305 | }␊ |
306 | ␊ |
307 | long HFSGetDirEntry(CICell ih, char * dirPath, long long * dirIndex, char ** name,␊ |
308 | long * flags, long * time,␊ |
309 | FinderInfo * finderInfo, long * infoValid)␊ |
310 | {␊ |
311 | char entry[512];␊ |
312 | long dirID, dirFlags;␊ |
313 | ␊ |
314 | if (HFSInitPartition(ih) == -1) return -1;␊ |
315 | ␊ |
316 | if (*dirIndex == -1) return -1;␊ |
317 | ␊ |
318 | dirID = kHFSRootFolderID;␊ |
319 | // Skip a lead '\'. Start in the system folder if there are two.␊ |
320 | if (dirPath[0] == '/') {␊ |
321 | if (dirPath[1] == '/') {␊ |
322 | if (gIsHFSPlus) dirID = SWAP_BE32(((long *)gHFSPlus->finderInfo)[5]);␊ |
323 | else dirID = SWAP_BE32(gHFSMDB->drFndrInfo[5]);␊ |
324 | if (dirID == 0) return -1;␊ |
325 | dirPath++;␊ |
326 | }␊ |
327 | dirPath++;␊ |
328 | }␊ |
329 | ␊ |
330 | if (*dirIndex == 0) {␊ |
331 | ResolvePathToCatalogEntry(dirPath, &dirFlags, entry, dirID, dirIndex);␊ |
332 | if (*dirIndex == 0) *dirIndex = -1;␊ |
333 | if ((dirFlags & kFileTypeMask) != kFileTypeUnknown) return -1;␊ |
334 | }␊ |
335 | ␊ |
336 | GetCatalogEntry(dirIndex, name, flags, time, finderInfo, infoValid);␊ |
337 | if (*dirIndex == 0) *dirIndex = -1;␊ |
338 | if ((*flags & kFileTypeMask) == kFileTypeUnknown) return -1;␊ |
339 | ␊ |
340 | return 0;␊ |
341 | }␊ |
342 | ␊ |
343 | void␊ |
344 | HFSGetDescription(CICell ih, char *str, long strMaxLen)␊ |
345 | {␊ |
346 | ␊ |
347 | UInt16 nodeSize;␊ |
348 | UInt32 firstLeafNode;␊ |
349 | long long dirIndex;␊ |
350 | char *name;␊ |
351 | long flags, time;␊ |
352 | ␊ |
353 | if (HFSInitPartition(ih) == -1) { return; }␊ |
354 | ␊ |
355 | /* Fill some crucial data structures by side effect. */␊ |
356 | dirIndex = 0;␊ |
357 | HFSGetDirEntry(ih, "/", &dirIndex, &name, &flags, &time, 0, 0);␊ |
358 | ␊ |
359 | /* Now we can loook up the volume name node. */␊ |
360 | nodeSize = SWAP_BE16(gBTHeaders[kBTreeCatalog]->nodeSize);␊ |
361 | firstLeafNode = SWAP_BE32(gBTHeaders[kBTreeCatalog]->firstLeafNode);␊ |
362 | ␊ |
363 | dirIndex = (long long) firstLeafNode * nodeSize;␊ |
364 | ␊ |
365 | GetCatalogEntry(&dirIndex, &name, &flags, &time, 0, 0);␊ |
366 | ␊ |
367 | strncpy(str, name, strMaxLen);␊ |
368 | str[strMaxLen] = '\0';␊ |
369 | }␊ |
370 | ␊ |
371 | ␊ |
372 | long␊ |
373 | HFSGetFileBlock(CICell ih, char *filePath, unsigned long long *firstBlock)␊ |
374 | {␊ |
375 | char entry[512];␊ |
376 | long dirID, result, flags;␊ |
377 | void *extents;␊ |
378 | HFSCatalogFile *hfsFile = (void *)entry;␊ |
379 | HFSPlusCatalogFile *hfsPlusFile = (void *)entry;␊ |
380 | ␊ |
381 | if (HFSInitPartition(ih) == -1) return -1;␊ |
382 | ␊ |
383 | dirID = kHFSRootFolderID;␊ |
384 | // Skip a lead '\'. Start in the system folder if there are two.␊ |
385 | if (filePath[0] == '/') {␊ |
386 | if (filePath[1] == '/') {␊ |
387 | if (gIsHFSPlus) dirID = SWAP_BE32(((long *)gHFSPlus->finderInfo)[5]);␊ |
388 | else dirID = SWAP_BE32(gHFSMDB->drFndrInfo[5]);␊ |
389 | if (dirID == 0) {␊ |
390 | ␉␉return -1;␊ |
391 | ␉ }␊ |
392 | filePath++;␊ |
393 | }␊ |
394 | filePath++;␊ |
395 | }␊ |
396 | ␊ |
397 | result = ResolvePathToCatalogEntry(filePath, &flags, entry, dirID, 0);␊ |
398 | if ((result == -1) || ((flags & kFileTypeMask) != kFileTypeFlat)) {␊ |
399 | printf("HFS: Resolve path %s failed\n", filePath);␊ |
400 | ␉return -1;␊ |
401 | }␊ |
402 | ␊ |
403 | if (gIsHFSPlus) {␊ |
404 | extents = &hfsPlusFile->dataFork.extents;␊ |
405 | } else {␊ |
406 | extents = &hfsFile->dataExtents;␊ |
407 | }␊ |
408 | ␊ |
409 | #if DEBUG␊ |
410 | printf("extent start 0x%x\n", (unsigned long)GetExtentStart(extents, 0));␊ |
411 | printf("block size 0x%x\n", (unsigned long)gBlockSize);␊ |
412 | printf("Allocation offset 0x%x\n", (unsigned long)gAllocationOffset);␊ |
413 | #endif␊ |
414 | *firstBlock = ((unsigned long long)GetExtentStart(extents, 0) * (unsigned long long) gBlockSize + gAllocationOffset) / 512ULL;␊ |
415 | return 0;␊ |
416 | }␊ |
417 | ␊ |
418 | long HFSGetUUID(CICell ih, char *uuidStr)␊ |
419 | {␊ |
420 | if (HFSInitPartition(ih) == -1) return -1;␊ |
421 | if (gVolID == 0LL) return -1;␊ |
422 | ␊ |
423 | return CreateUUIDString((uint8_t*)(&gVolID), sizeof(gVolID), uuidStr);␊ |
424 | }␊ |
425 | ␊ |
426 | // Private Functions␊ |
427 | ␊ |
428 | static long ReadFile(void * file, uint64_t * length, void * base, uint64_t offset)␊ |
429 | {␊ |
430 | void *extents;␊ |
431 | long fileID;␊ |
432 | uint64_t fileLength;␊ |
433 | HFSCatalogFile *hfsFile = file;␊ |
434 | HFSPlusCatalogFile *hfsPlusFile = file;␊ |
435 | ␊ |
436 | if (gIsHFSPlus) {␊ |
437 | fileID = SWAP_BE32(hfsPlusFile->fileID);␊ |
438 | fileLength = (uint64_t)SWAP_BE64(hfsPlusFile->dataFork.logicalSize);␊ |
439 | extents = &hfsPlusFile->dataFork.extents;␊ |
440 | } else {␊ |
441 | fileID = SWAP_BE32(hfsFile->fileID);␊ |
442 | fileLength = SWAP_BE32(hfsFile->dataLogicalSize);␊ |
443 | extents = &hfsFile->dataExtents;␊ |
444 | }␊ |
445 | ␊ |
446 | if (offset > fileLength) {␊ |
447 | printf("Offset is too large.\n");␊ |
448 | return -1;␊ |
449 | }␊ |
450 | ␊ |
451 | if ((*length == 0) || ((offset + *length) > fileLength)) {␊ |
452 | *length = fileLength - offset;␊ |
453 | }␊ |
454 | ␊ |
455 | /* if (*length > kLoadSize) {␊ |
456 | printf("File is too large.\n");␊ |
457 | return -1;␊ |
458 | }*/␊ |
459 | ␊ |
460 | *length = ReadExtent((char *)extents, fileLength, fileID,␊ |
461 | offset, *length, (char *)base, 0);␊ |
462 | ␊ |
463 | return 0;␊ |
464 | }␊ |
465 | ␊ |
466 | static long GetCatalogEntryInfo(void * entry, long * flags, long * time,␊ |
467 | FinderInfo * finderInfo, long * infoValid)␊ |
468 | {␊ |
469 | long tmpTime = 0;␊ |
470 | long valid = 0;␊ |
471 | ␊ |
472 | // Get information about the file.␊ |
473 | ␊ |
474 | switch ( SWAP_BE16(*(short *)entry) )␊ |
475 | {␊ |
476 | case kHFSFolderRecord :␊ |
477 | *flags = kFileTypeDirectory;␊ |
478 | tmpTime = SWAP_BE32(((HFSCatalogFolder *)entry)->modifyDate);␊ |
479 | break;␊ |
480 | ␊ |
481 | case kHFSPlusFolderRecord :␊ |
482 | *flags = kFileTypeDirectory |␊ |
483 | (SWAP_BE16(((HFSPlusCatalogFolder *)entry)->bsdInfo.fileMode) & kPermMask);␊ |
484 | if (SWAP_BE32(((HFSPlusCatalogFolder *)entry)->bsdInfo.ownerID) != 0)␊ |
485 | *flags |= kOwnerNotRoot;␊ |
486 | tmpTime = SWAP_BE32(((HFSPlusCatalogFolder *)entry)->contentModDate);␊ |
487 | break;␊ |
488 | ␊ |
489 | case kHFSFileRecord :␊ |
490 | *flags = kFileTypeFlat;␊ |
491 | tmpTime = SWAP_BE32(((HFSCatalogFile *)entry)->modifyDate);␊ |
492 | if (finderInfo) {␊ |
493 | SwapFinderInfo((FndrFileInfo *)finderInfo, &((HFSCatalogFile *)entry)->userInfo);␊ |
494 | valid = 1;␊ |
495 | }␊ |
496 | break;␊ |
497 | ␊ |
498 | case kHFSPlusFileRecord :␊ |
499 | *flags = kFileTypeFlat |␊ |
500 | (SWAP_BE16(((HFSPlusCatalogFile *)entry)->bsdInfo.fileMode) & kPermMask);␊ |
501 | if (SWAP_BE32(((HFSPlusCatalogFile *)entry)->bsdInfo.ownerID) != 0)␊ |
502 | *flags |= kOwnerNotRoot;␊ |
503 | tmpTime = SWAP_BE32(((HFSPlusCatalogFile *)entry)->contentModDate);␊ |
504 | if (finderInfo) {␊ |
505 | SwapFinderInfo((FndrFileInfo *)finderInfo, &((HFSPlusCatalogFile *)entry)->userInfo);␊ |
506 | valid = 1;␊ |
507 | }␊ |
508 | break;␊ |
509 | ␊ |
510 | case kHFSFileThreadRecord :␊ |
511 | case kHFSPlusFileThreadRecord :␊ |
512 | case kHFSFolderThreadRecord :␊ |
513 | case kHFSPlusFolderThreadRecord :␊ |
514 | *flags = kFileTypeUnknown;␊ |
515 | tmpTime = 0;␊ |
516 | break;␊ |
517 | }␊ |
518 | ␊ |
519 | if (time != 0) {␊ |
520 | // Convert base time from 1904 to 1970.␊ |
521 | *time = tmpTime - 2082844800;␊ |
522 | }␊ |
523 | if (infoValid) *infoValid = valid;␊ |
524 | ␊ |
525 | return 0;␊ |
526 | }␊ |
527 | ␊ |
528 | static long ResolvePathToCatalogEntry(char * filePath, long * flags,␊ |
529 | void * entry, long dirID, long long * dirIndex)␊ |
530 | {␊ |
531 | char *restPath;␊ |
532 | long result, cnt, subFolderID = 0;␊ |
533 | long long tmpDirIndex;␊ |
534 | HFSPlusCatalogFile *hfsPlusFile;␊ |
535 | ␊ |
536 | // Copy the file name to gTempStr␊ |
537 | cnt = 0;␊ |
538 | while ((filePath[cnt] != '/') && (filePath[cnt] != '\0')) cnt++;␊ |
539 | strlcpy(gTempStr, filePath, cnt+1);␊ |
540 | ␊ |
541 | // Move restPath to the right place.␊ |
542 | if (filePath[cnt] != '\0') cnt++;␊ |
543 | restPath = filePath + cnt;␊ |
544 | ␊ |
545 | // gTempStr is a name in the current Dir.␊ |
546 | // restPath is the rest of the path if any.␊ |
547 | ␊ |
548 | result = ReadCatalogEntry(gTempStr, dirID, entry, dirIndex);␊ |
549 | if (result == -1) {␊ |
550 | ␉return -1;␊ |
551 | }␊ |
552 | ␊ |
553 | GetCatalogEntryInfo(entry, flags, 0, 0, 0);␊ |
554 | ␊ |
555 | if ((*flags & kFileTypeMask) == kFileTypeDirectory) {␊ |
556 | if (gIsHFSPlus)␊ |
557 | subFolderID = SWAP_BE32(((HFSPlusCatalogFolder *)entry)->folderID);␊ |
558 | else␊ |
559 | subFolderID = SWAP_BE32(((HFSCatalogFolder *)entry)->folderID);␊ |
560 | }␊ |
561 | ␊ |
562 | if ((*flags & kFileTypeMask) == kFileTypeDirectory)␊ |
563 | result = ResolvePathToCatalogEntry(restPath, flags, entry,␊ |
564 | subFolderID, dirIndex);␊ |
565 | ␊ |
566 | if (gIsHFSPlus && ((*flags & kFileTypeMask) == kFileTypeFlat)) {␊ |
567 | hfsPlusFile = (HFSPlusCatalogFile *)entry;␊ |
568 | if ((SWAP_BE32(hfsPlusFile->userInfo.fdType) == kHardLinkFileType) &&␊ |
569 | (SWAP_BE32(hfsPlusFile->userInfo.fdCreator) == kHFSPlusCreator)) {␊ |
570 | sprintf(gLinkTemp, "%s/%s%ld", HFSPLUSMETADATAFOLDER,␊ |
571 | HFS_INODE_PREFIX, SWAP_BE32(hfsPlusFile->bsdInfo.special.iNodeNum));␊ |
572 | result = ResolvePathToCatalogEntry(gLinkTemp, flags, entry,␊ |
573 | kHFSRootFolderID, &tmpDirIndex);␊ |
574 | }␊ |
575 | }␊ |
576 | ␊ |
577 | return result;␊ |
578 | }␊ |
579 | ␊ |
580 | static long GetCatalogEntry(long long * dirIndex, char ** name,␊ |
581 | long * flags, long * time,␊ |
582 | FinderInfo * finderInfo, long * infoValid)␊ |
583 | {␊ |
584 | long extentSize, nodeSize, curNode, index;␊ |
585 | void *extent;␊ |
586 | char *nodeBuf, *testKey, *entry;␊ |
587 | BTNodeDescriptor *node;␊ |
588 | ␊ |
589 | if (gIsHFSPlus) {␊ |
590 | extent = &gHFSPlus->catalogFile.extents;␊ |
591 | extentSize = SWAP_BE64(gHFSPlus->catalogFile.logicalSize);␊ |
592 | } else {␊ |
593 | extent = (HFSExtentDescriptor *)&gHFSMDB->drCTExtRec;␊ |
594 | extentSize = SWAP_BE32(gHFSMDB->drCTFlSize);␊ |
595 | }␊ |
596 | ␊ |
597 | nodeSize = SWAP_BE16(gBTHeaders[kBTreeCatalog]->nodeSize);␊ |
598 | nodeBuf = (char *)malloc(nodeSize);␊ |
599 | node = (BTNodeDescriptor *)nodeBuf;␊ |
600 | ␊ |
601 | index = (long) (*dirIndex % nodeSize);␊ |
602 | curNode = (long) (*dirIndex / nodeSize);␊ |
603 | ␊ |
604 | // Read the BTree node and get the record for index.␊ |
605 | ReadExtent(extent, extentSize, kHFSCatalogFileID,␊ |
606 | (long long) curNode * nodeSize, nodeSize, nodeBuf, 1);␊ |
607 | GetBTreeRecord(index, nodeBuf, nodeSize, &testKey, &entry);␊ |
608 | ␊ |
609 | GetCatalogEntryInfo(entry, flags, time, finderInfo, infoValid);␊ |
610 | ␊ |
611 | // Get the file name.␊ |
612 | if (gIsHFSPlus) {␊ |
613 | utf_encodestr(((HFSPlusCatalogKey *)testKey)->nodeName.unicode,␊ |
614 | SWAP_BE16(((HFSPlusCatalogKey *)testKey)->nodeName.length),␊ |
615 | (u_int8_t *)gTempStr, 256, OSBigEndian);␊ |
616 | } else {␊ |
617 | strncpy(gTempStr,␊ |
618 | (const char *)&((HFSCatalogKey *)testKey)->nodeName[1],␊ |
619 | ((HFSCatalogKey *)testKey)->nodeName[0]);␊ |
620 | gTempStr[((HFSCatalogKey *)testKey)->nodeName[0]] = '\0';␊ |
621 | }␊ |
622 | *name = gTempStr;␊ |
623 | ␊ |
624 | // Update dirIndex.␊ |
625 | index++;␊ |
626 | if (index == SWAP_BE16(node->numRecords)) {␊ |
627 | index = 0;␊ |
628 | curNode = SWAP_BE32(node->fLink);␊ |
629 | }␊ |
630 | *dirIndex = (long long) curNode * nodeSize + index;␊ |
631 | ␊ |
632 | free(nodeBuf);␊ |
633 | ␊ |
634 | return 0;␊ |
635 | }␊ |
636 | ␊ |
637 | static long ReadCatalogEntry(char * fileName, long dirID,␊ |
638 | void * entry, long long * dirIndex)␊ |
639 | {␊ |
640 | long length;␊ |
641 | char key[sizeof(HFSPlusCatalogKey)];␊ |
642 | HFSCatalogKey *hfsKey = (HFSCatalogKey *)key;␊ |
643 | HFSPlusCatalogKey *hfsPlusKey = (HFSPlusCatalogKey *)key;␊ |
644 | ␊ |
645 | // Make the catalog key.␊ |
646 | if ( gIsHFSPlus )␊ |
647 | {␊ |
648 | hfsPlusKey->parentID = SWAP_BE32(dirID);␊ |
649 | length = strlen(fileName);␊ |
650 | if (length > 255) length = 255;␊ |
651 | utf_decodestr((u_int8_t *)fileName, hfsPlusKey->nodeName.unicode,␊ |
652 | &(hfsPlusKey->nodeName.length), 512, OSBigEndian);␊ |
653 | } else {␊ |
654 | hfsKey->parentID = SWAP_BE32(dirID);␊ |
655 | length = strlen(fileName);␊ |
656 | if (length > 31) length = 31;␊ |
657 | hfsKey->nodeName[0] = length;␊ |
658 | strncpy((char *)(hfsKey->nodeName + 1), fileName, length);␊ |
659 | }␊ |
660 | ␊ |
661 | return ReadBTreeEntry(kBTreeCatalog, &key, entry, dirIndex);␊ |
662 | }␊ |
663 | ␊ |
664 | static long ReadExtentsEntry(long fileID, long startBlock, void * entry)␊ |
665 | {␊ |
666 | char key[sizeof(HFSPlusExtentKey)];␊ |
667 | HFSExtentKey *hfsKey = (HFSExtentKey *)key;␊ |
668 | HFSPlusExtentKey *hfsPlusKey = (HFSPlusExtentKey *)key;␊ |
669 | ␊ |
670 | // Make the extents key.␊ |
671 | if (gIsHFSPlus) {␊ |
672 | hfsPlusKey->forkType = 0;␊ |
673 | hfsPlusKey->fileID = SWAP_BE32(fileID);␊ |
674 | hfsPlusKey->startBlock = SWAP_BE32(startBlock);␊ |
675 | } else {␊ |
676 | hfsKey->forkType = 0;␊ |
677 | hfsKey->fileID = SWAP_BE32(fileID);␊ |
678 | hfsKey->startBlock = SWAP_BE16(startBlock);␊ |
679 | }␊ |
680 | ␊ |
681 | return ReadBTreeEntry(kBTreeExtents, &key, entry, 0);␊ |
682 | }␊ |
683 | ␊ |
684 | static long ReadBTreeEntry(long btree, void * key, char * entry, long long * dirIndex)␊ |
685 | {␊ |
686 | long extentSize;␊ |
687 | void *extent;␊ |
688 | short extentFile;␊ |
689 | char *nodeBuf;␊ |
690 | BTNodeDescriptor *node;␊ |
691 | long nodeSize, result = 0, entrySize = 0;␊ |
692 | long curNode, index = 0, lowerBound, upperBound;␊ |
693 | char *testKey, *recordData;␊ |
694 | ␊ |
695 | // Figure out which tree is being looked at.␊ |
696 | if (btree == kBTreeCatalog) {␊ |
697 | if (gIsHFSPlus) {␊ |
698 | extent = &gHFSPlus->catalogFile.extents;␊ |
699 | extentSize = SWAP_BE64(gHFSPlus->catalogFile.logicalSize);␊ |
700 | } else {␊ |
701 | extent = (HFSExtentDescriptor *)&gHFSMDB->drCTExtRec;␊ |
702 | extentSize = SWAP_BE32(gHFSMDB->drCTFlSize);␊ |
703 | }␊ |
704 | extentFile = kHFSCatalogFileID;␊ |
705 | } else {␊ |
706 | if (gIsHFSPlus) {␊ |
707 | extent = &gHFSPlus->extentsFile.extents;␊ |
708 | extentSize = SWAP_BE64(gHFSPlus->extentsFile.logicalSize);␊ |
709 | } else {␊ |
710 | extent = (HFSExtentDescriptor *)&gHFSMDB->drXTExtRec;␊ |
711 | extentSize = SWAP_BE32(gHFSMDB->drXTFlSize);␊ |
712 | }␊ |
713 | extentFile = kHFSExtentsFileID;␊ |
714 | }␊ |
715 | ␊ |
716 | // Read the BTree Header if needed.␊ |
717 | if (gBTHeaders[btree] == 0) {␊ |
718 | ReadExtent(extent, extentSize, extentFile, 0, 256,␊ |
719 | gBTreeHeaderBuffer + btree * 256, 0);␊ |
720 | gBTHeaders[btree] = (BTHeaderRec *)(gBTreeHeaderBuffer + btree * 256 +␊ |
721 | sizeof(BTNodeDescriptor));␊ |
722 | if ((gIsHFSPlus && btree == kBTreeCatalog) &&␊ |
723 | (gBTHeaders[btree]->keyCompareType == kHFSBinaryCompare)) {␊ |
724 | gCaseSensitive = 1;␊ |
725 | }␊ |
726 | }␊ |
727 | ␊ |
728 | curNode = SWAP_BE32(gBTHeaders[btree]->rootNode);␊ |
729 | nodeSize = SWAP_BE16(gBTHeaders[btree]->nodeSize);␊ |
730 | nodeBuf = (char *)malloc(nodeSize);␊ |
731 | node = (BTNodeDescriptor *)nodeBuf;␊ |
732 | ␊ |
733 | while (1) {␊ |
734 | // Read the current node.␊ |
735 | ReadExtent(extent, extentSize, extentFile,␊ |
736 | (long long) curNode * nodeSize, nodeSize, nodeBuf, 1);␊ |
737 | ␊ |
738 | // Find the matching key.␊ |
739 | lowerBound = 0;␊ |
740 | upperBound = SWAP_BE16(node->numRecords) - 1;␊ |
741 | while (lowerBound <= upperBound) {␊ |
742 | index = (lowerBound + upperBound) / 2;␊ |
743 | ␊ |
744 | GetBTreeRecord(index, nodeBuf, nodeSize, &testKey, &recordData);␊ |
745 | ␊ |
746 | if (gIsHFSPlus) {␊ |
747 | if (btree == kBTreeCatalog) {␊ |
748 | result = CompareHFSPlusCatalogKeys(key, testKey);␊ |
749 | } else {␊ |
750 | result = CompareHFSPlusExtentsKeys(key, testKey);␊ |
751 | }␊ |
752 | } else {␊ |
753 | if (btree == kBTreeCatalog) {␊ |
754 | result = CompareHFSCatalogKeys(key, testKey);␊ |
755 | } else {␊ |
756 | result = CompareHFSExtentsKeys(key, testKey);␊ |
757 | }␊ |
758 | }␊ |
759 | ␊ |
760 | if (result < 0) upperBound = index - 1; // search < trial␊ |
761 | else if (result > 0) lowerBound = index + 1; // search > trial␊ |
762 | else break; // search = trial␊ |
763 | }␊ |
764 | ␊ |
765 | if (result < 0) {␊ |
766 | index = upperBound;␊ |
767 | GetBTreeRecord(index, nodeBuf, nodeSize, &testKey, &recordData);␊ |
768 | }␊ |
769 | ␊ |
770 | // Found the closest key... Recurse on it if this is an index node.␊ |
771 | if (node->kind == kBTIndexNode) {␊ |
772 | curNode = SWAP_BE32( *((long *)recordData) );␊ |
773 | } else break;␊ |
774 | }␊ |
775 | ␊ |
776 | // Return error if the file was not found.␊ |
777 | if (result != 0) { free(nodeBuf); return -1; }␊ |
778 | ␊ |
779 | if (btree == kBTreeCatalog) {␊ |
780 | switch (SWAP_BE16(*(short *)recordData)) {␊ |
781 | case kHFSFolderRecord : entrySize = 70; break;␊ |
782 | case kHFSFileRecord : entrySize = 102; break;␊ |
783 | case kHFSFolderThreadRecord : entrySize = 46; break;␊ |
784 | case kHFSFileThreadRecord : entrySize = 46; break;␊ |
785 | case kHFSPlusFolderRecord : entrySize = 88; break;␊ |
786 | case kHFSPlusFileRecord : entrySize = 248; break;␊ |
787 | case kHFSPlusFolderThreadRecord : entrySize = 264; break;␊ |
788 | case kHFSPlusFileThreadRecord : entrySize = 264; break;␊ |
789 | }␊ |
790 | } else {␊ |
791 | if (gIsHFSPlus) entrySize = sizeof(HFSPlusExtentRecord);␊ |
792 | else entrySize = sizeof(HFSExtentRecord);␊ |
793 | }␊ |
794 | ␊ |
795 | bcopy(recordData, entry, entrySize);␊ |
796 | ␊ |
797 | // Update dirIndex.␊ |
798 | if (dirIndex != 0) {␊ |
799 | index++;␊ |
800 | if (index == SWAP_BE16(node->numRecords)) {␊ |
801 | index = 0;␊ |
802 | curNode = SWAP_BE32(node->fLink);␊ |
803 | }␊ |
804 | *dirIndex = (long long) curNode * nodeSize + index;␊ |
805 | }␊ |
806 | ␊ |
807 | free(nodeBuf);␊ |
808 | ␊ |
809 | return 0;␊ |
810 | }␊ |
811 | ␊ |
812 | static void GetBTreeRecord(long index, char * nodeBuffer, long nodeSize,␊ |
813 | char ** key, char ** data)␊ |
814 | {␊ |
815 | long keySize;␊ |
816 | long recordOffset;␊ |
817 | ␊ |
818 | recordOffset = SWAP_BE16(*((short *)(nodeBuffer + (nodeSize - 2 * index - 2))));␊ |
819 | *key = nodeBuffer + recordOffset;␊ |
820 | if (gIsHFSPlus) {␊ |
821 | keySize = SWAP_BE16(*(short *)*key);␊ |
822 | *data = *key + 2 + keySize;␊ |
823 | } else {␊ |
824 | keySize = **key;␊ |
825 | *data = *key + 2 + keySize - (keySize & 1);␊ |
826 | }␊ |
827 | }␊ |
828 | ␊ |
829 | static long ReadExtent(char * extent, uint64_t extentSize,␊ |
830 | long extentFile, uint64_t offset, uint64_t size,␊ |
831 | void * buffer, long cache)␊ |
832 | {␊ |
833 | uint64_t lastOffset;␊ |
834 | ␉long long blockNumber, countedBlocks = 0;␊ |
835 | long long nextExtent = 0, sizeRead = 0, readSize;␊ |
836 | long long nextExtentBlock, currentExtentBlock = 0;␊ |
837 | long long readOffset;␊ |
838 | long long extentDensity, sizeofExtent, currentExtentSize;␊ |
839 | char *currentExtent, *extentBuffer = 0, *bufferPos = buffer;␊ |
840 | ␊ |
841 | if (offset >= extentSize) return 0;␊ |
842 | ␊ |
843 | if (gIsHFSPlus) {␊ |
844 | extentDensity = kHFSPlusExtentDensity;␊ |
845 | sizeofExtent = sizeof(HFSPlusExtentDescriptor);␊ |
846 | } else {␊ |
847 | extentDensity = kHFSExtentDensity;␊ |
848 | sizeofExtent = sizeof(HFSExtentDescriptor);␊ |
849 | }␊ |
850 | ␊ |
851 | lastOffset = offset + size;␊ |
852 | while (offset < lastOffset) {␊ |
853 | blockNumber = offset / gBlockSize;␊ |
854 | ␊ |
855 | // Find the extent for the offset.␊ |
856 | for (; ; nextExtent++) {␊ |
857 | if (nextExtent < extentDensity) {␊ |
858 | if ((countedBlocks+GetExtentSize(extent, nextExtent)-1)<blockNumber) {␊ |
859 | countedBlocks += GetExtentSize(extent, nextExtent);␊ |
860 | continue;␊ |
861 | }␊ |
862 | ␊ |
863 | currentExtent = extent + nextExtent * sizeofExtent;␊ |
864 | break;␊ |
865 | }␊ |
866 | ␊ |
867 | if (extentBuffer == 0) {␊ |
868 | extentBuffer = malloc(sizeofExtent * extentDensity);␊ |
869 | if (extentBuffer == 0) return -1;␊ |
870 | }␊ |
871 | ␊ |
872 | nextExtentBlock = nextExtent / extentDensity;␊ |
873 | if (currentExtentBlock != nextExtentBlock) {␊ |
874 | ReadExtentsEntry(extentFile, countedBlocks, extentBuffer);␊ |
875 | currentExtentBlock = nextExtentBlock;␊ |
876 | }␊ |
877 | ␊ |
878 | currentExtentSize = GetExtentSize(extentBuffer, nextExtent % extentDensity);␊ |
879 | ␊ |
880 | if ((countedBlocks + currentExtentSize - 1) >= blockNumber) {␊ |
881 | currentExtent = extentBuffer + sizeofExtent * (nextExtent % extentDensity);␊ |
882 | break;␊ |
883 | }␊ |
884 | ␊ |
885 | countedBlocks += currentExtentSize;␊ |
886 | }␊ |
887 | ␊ |
888 | readOffset = ((blockNumber - countedBlocks) * gBlockSize) +␊ |
889 | (offset % gBlockSize);␊ |
890 | ␊ |
891 | ␉␉// MacWen: fix overflow in multiplication by forcing 64bit multiplication␊ |
892 | readSize = (long long)GetExtentSize(currentExtent, 0) * gBlockSize - readOffset;␊ |
893 | if (readSize > (size - sizeRead)) readSize = size - sizeRead;␊ |
894 | ␊ |
895 | readOffset += (long long)GetExtentStart(currentExtent, 0) * gBlockSize;␊ |
896 | ␊ |
897 | CacheRead(gCurrentIH, bufferPos, gAllocationOffset + readOffset,␊ |
898 | readSize, cache);␊ |
899 | ␊ |
900 | sizeRead += readSize;␊ |
901 | offset += readSize;␊ |
902 | bufferPos += readSize;␊ |
903 | }␊ |
904 | ␊ |
905 | if (extentBuffer) free(extentBuffer);␊ |
906 | ␊ |
907 | return sizeRead;␊ |
908 | }␊ |
909 | ␊ |
910 | static long GetExtentStart(void * extents, long index)␊ |
911 | {␊ |
912 | long start;␊ |
913 | HFSExtentDescriptor *hfsExtents = extents;␊ |
914 | HFSPlusExtentDescriptor *hfsPlusExtents = extents;␊ |
915 | ␊ |
916 | if (gIsHFSPlus) start = SWAP_BE32(hfsPlusExtents[index].startBlock);␊ |
917 | else start = SWAP_BE16(hfsExtents[index].startBlock);␊ |
918 | ␊ |
919 | return start;␊ |
920 | }␊ |
921 | ␊ |
922 | static long GetExtentSize(void * extents, long index)␊ |
923 | {␊ |
924 | long size;␊ |
925 | HFSExtentDescriptor *hfsExtents = extents;␊ |
926 | HFSPlusExtentDescriptor *hfsPlusExtents = extents;␊ |
927 | ␊ |
928 | if (gIsHFSPlus) size = SWAP_BE32(hfsPlusExtents[index].blockCount);␊ |
929 | else size = SWAP_BE16(hfsExtents[index].blockCount);␊ |
930 | ␊ |
931 | return size;␊ |
932 | }␊ |
933 | ␊ |
934 | static long CompareHFSCatalogKeys(void * key, void * testKey)␊ |
935 | {␊ |
936 | HFSCatalogKey *searchKey, *trialKey;␊ |
937 | long result, searchParentID, trialParentID;␊ |
938 | ␊ |
939 | searchKey = key;␊ |
940 | trialKey = testKey;␊ |
941 | ␊ |
942 | searchParentID = SWAP_BE32(searchKey->parentID);␊ |
943 | trialParentID = SWAP_BE32(trialKey->parentID);␊ |
944 | ␊ |
945 | // parent dirID is unsigned␊ |
946 | if (searchParentID > trialParentID) result = 1;␊ |
947 | else if (searchParentID < trialParentID) result = -1;␊ |
948 | else {␊ |
949 | // parent dirID's are equal, compare names␊ |
950 | result = FastRelString(searchKey->nodeName, trialKey->nodeName);␊ |
951 | }␊ |
952 | ␊ |
953 | return result;␊ |
954 | }␊ |
955 | ␊ |
956 | static long CompareHFSPlusCatalogKeys(void * key, void * testKey)␊ |
957 | {␊ |
958 | HFSPlusCatalogKey *searchKey, *trialKey;␊ |
959 | long result, searchParentID, trialParentID;␊ |
960 | ␊ |
961 | searchKey = key;␊ |
962 | trialKey = testKey;␊ |
963 | ␊ |
964 | searchParentID = SWAP_BE32(searchKey->parentID);␊ |
965 | trialParentID = SWAP_BE32(trialKey->parentID);␊ |
966 | ␊ |
967 | // parent dirID is unsigned␊ |
968 | if (searchParentID > trialParentID) result = 1;␊ |
969 | else if (searchParentID < trialParentID) result = -1;␊ |
970 | else {␊ |
971 | // parent dirID's are equal, compare names␊ |
972 | if ((searchKey->nodeName.length == 0) || (trialKey->nodeName.length == 0))␊ |
973 | result = searchKey->nodeName.length - trialKey->nodeName.length;␊ |
974 | else␊ |
975 | if (gCaseSensitive) {␊ |
976 | result = BinaryUnicodeCompare(&searchKey->nodeName.unicode[0],␊ |
977 | SWAP_BE16(searchKey->nodeName.length),␊ |
978 | &trialKey->nodeName.unicode[0],␊ |
979 | SWAP_BE16(trialKey->nodeName.length));␊ |
980 | } else {␊ |
981 | result = FastUnicodeCompare(&searchKey->nodeName.unicode[0],␊ |
982 | SWAP_BE16(searchKey->nodeName.length),␊ |
983 | &trialKey->nodeName.unicode[0],␊ |
984 | SWAP_BE16(trialKey->nodeName.length), OSBigEndian);␊ |
985 | }␊ |
986 | }␊ |
987 | ␊ |
988 | return result;␊ |
989 | }␊ |
990 | ␊ |
991 | static long CompareHFSExtentsKeys(void * key, void * testKey)␊ |
992 | {␊ |
993 | HFSExtentKey *searchKey, *trialKey;␊ |
994 | long result;␊ |
995 | ␊ |
996 | searchKey = key;␊ |
997 | trialKey = testKey;␊ |
998 | ␊ |
999 | // assume searchKey < trialKey␊ |
1000 | result = -1; ␊ |
1001 | ␊ |
1002 | if (searchKey->fileID == trialKey->fileID) {␊ |
1003 | // FileNum's are equal; compare fork types␊ |
1004 | if (searchKey->forkType == trialKey->forkType) {␊ |
1005 | // Fork types are equal; compare allocation block number␊ |
1006 | if (searchKey->startBlock == trialKey->startBlock) {␊ |
1007 | // Everything is equal␊ |
1008 | result = 0;␊ |
1009 | } else {␊ |
1010 | // Allocation block numbers differ; determine sign␊ |
1011 | if (SWAP_BE16(searchKey->startBlock) > SWAP_BE16(trialKey->startBlock))␊ |
1012 | result = 1;␊ |
1013 | }␊ |
1014 | } else {␊ |
1015 | // Fork types differ; determine sign␊ |
1016 | if (searchKey->forkType > trialKey->forkType) result = 1;␊ |
1017 | }␊ |
1018 | } else {␊ |
1019 | // FileNums differ; determine sign␊ |
1020 | if (SWAP_BE32(searchKey->fileID) > SWAP_BE32(trialKey->fileID))␊ |
1021 | result = 1;␊ |
1022 | }␊ |
1023 | ␊ |
1024 | return result;␊ |
1025 | }␊ |
1026 | ␊ |
1027 | static long CompareHFSPlusExtentsKeys(void * key, void * testKey)␊ |
1028 | {␊ |
1029 | HFSPlusExtentKey *searchKey, *trialKey;␊ |
1030 | long result;␊ |
1031 | ␊ |
1032 | searchKey = key;␊ |
1033 | trialKey = testKey;␊ |
1034 | ␊ |
1035 | // assume searchKey < trialKey␊ |
1036 | result = -1; ␊ |
1037 | ␊ |
1038 | if (searchKey->fileID == trialKey->fileID) {␊ |
1039 | // FileNum's are equal; compare fork types␊ |
1040 | if (searchKey->forkType == trialKey->forkType) {␊ |
1041 | // Fork types are equal; compare allocation block number␊ |
1042 | if (searchKey->startBlock == trialKey->startBlock) {␊ |
1043 | // Everything is equal␊ |
1044 | result = 0;␊ |
1045 | } else {␊ |
1046 | // Allocation block numbers differ; determine sign␊ |
1047 | if (SWAP_BE32(searchKey->startBlock) > SWAP_BE32(trialKey->startBlock))␊ |
1048 | result = 1;␊ |
1049 | }␊ |
1050 | } else {␊ |
1051 | // Fork types differ; determine sign␊ |
1052 | if (searchKey->forkType > trialKey->forkType) result = 1;␊ |
1053 | }␊ |
1054 | } else {␊ |
1055 | // FileNums differ; determine sign␊ |
1056 | if (SWAP_BE32(searchKey->fileID) > SWAP_BE32(trialKey->fileID))␊ |
1057 | result = 1;␊ |
1058 | }␊ |
1059 | ␊ |
1060 | return result;␊ |
1061 | }␊ |
1062 | ␊ |
1063 | |