Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/libsaio/hfs.c

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

Archive Download this file

Revision: 1119