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