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