Chameleon

Chameleon Svn Source Tree

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

  • Property svn:executable set to
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
304 /*if (strstr(filePath, ".plist")) verbose("LoadConfig: [%s/%s] %d bytes.\n", devStr, filePath, (uint32_t)length);*/
305 if (strstr(filePath, "com.apple.Boot.plist")) verbose("Config: [%s/%s] %d bytes.\n", devStr, filePath, (uint32_t)length);
306 if (strstr(filePath, "theme.plist")) verbose("Theme: [%s].\n", devStr);
307 if (strstr(filePath, "smbios.plist")) verbose("SMBios: [%s/%s] %d bytes.\n", devStr, filePath, (uint32_t)length);
308 if (strstr(filePath, ".aml")) verbose("ACPI: [%s/%s] %d bytes.\n", devStr, filePath, (uint32_t)length);
309 return length;
310}
311
312long HFSGetDirEntry(CICell ih, char * dirPath, long long * dirIndex, char ** name,
313 long * flags, long * time,
314 FinderInfo * finderInfo, long * infoValid)
315{
316 char entry[512];
317 long dirID, dirFlags;
318
319 if (HFSInitPartition(ih) == -1) return -1;
320
321 if (*dirIndex == -1) return -1;
322
323 dirID = kHFSRootFolderID;
324 // Skip a lead '\'. Start in the system folder if there are two.
325 if (dirPath[0] == '/') {
326 if (dirPath[1] == '/') {
327 if (gIsHFSPlus) dirID = SWAP_BE32(((long *)gHFSPlus->finderInfo)[5]);
328 else dirID = SWAP_BE32(gHFSMDB->drFndrInfo[5]);
329 if (dirID == 0) return -1;
330 dirPath++;
331 }
332 dirPath++;
333 }
334
335 if (*dirIndex == 0) {
336 ResolvePathToCatalogEntry(dirPath, &dirFlags, entry, dirID, dirIndex);
337 if (*dirIndex == 0) *dirIndex = -1;
338 if ((dirFlags & kFileTypeMask) != kFileTypeUnknown) return -1;
339 }
340
341 GetCatalogEntry(dirIndex, name, flags, time, finderInfo, infoValid);
342 if (*dirIndex == 0) *dirIndex = -1;
343 if ((*flags & kFileTypeMask) == kFileTypeUnknown) return -1;
344
345 return 0;
346}
347
348void
349HFSGetDescription(CICell ih, char *str, long strMaxLen)
350{
351
352 UInt16 nodeSize;
353 UInt32 firstLeafNode;
354 long long dirIndex;
355 char *name;
356 long flags, time;
357
358 if (HFSInitPartition(ih) == -1) { return; }
359
360 /* Fill some crucial data structures by side effect. */
361 dirIndex = 0;
362 HFSGetDirEntry(ih, "/", &dirIndex, &name, &flags, &time, 0, 0);
363
364 /* Now we can loook up the volume name node. */
365 nodeSize = SWAP_BE16(gBTHeaders[kBTreeCatalog]->nodeSize);
366 firstLeafNode = SWAP_BE32(gBTHeaders[kBTreeCatalog]->firstLeafNode);
367
368 dirIndex = (long long) firstLeafNode * nodeSize;
369
370 GetCatalogEntry(&dirIndex, &name, &flags, &time, 0, 0);
371
372 strncpy(str, name, strMaxLen);
373 str[strMaxLen] = '\0';
374}
375
376
377long
378HFSGetFileBlock(CICell ih, char *filePath, unsigned long long *firstBlock)
379{
380 char entry[512];
381 long dirID, result, flags;
382 void *extents;
383 HFSCatalogFile *hfsFile = (void *)entry;
384 HFSPlusCatalogFile *hfsPlusFile = (void *)entry;
385
386 if (HFSInitPartition(ih) == -1) return -1;
387
388 dirID = kHFSRootFolderID;
389 // Skip a lead '\'. Start in the system folder if there are two.
390 if (filePath[0] == '/') {
391 if (filePath[1] == '/') {
392 if (gIsHFSPlus) dirID = SWAP_BE32(((long *)gHFSPlus->finderInfo)[5]);
393 else dirID = SWAP_BE32(gHFSMDB->drFndrInfo[5]);
394 if (dirID == 0) {
395return -1;
396 }
397 filePath++;
398 }
399 filePath++;
400 }
401
402 result = ResolvePathToCatalogEntry(filePath, &flags, entry, dirID, 0);
403 if ((result == -1) || ((flags & kFileTypeMask) != kFileTypeFlat)) {
404 printf("HFS: Resolve path %s failed\n", filePath);
405return -1;
406 }
407
408 if (gIsHFSPlus) {
409 extents = &hfsPlusFile->dataFork.extents;
410 } else {
411 extents = &hfsFile->dataExtents;
412 }
413
414#if DEBUG
415 printf("extent start 0x%x\n", (unsigned long)GetExtentStart(extents, 0));
416 printf("block size 0x%x\n", (unsigned long)gBlockSize);
417 printf("Allocation offset 0x%x\n", (unsigned long)gAllocationOffset);
418#endif
419 *firstBlock = ((unsigned long long)GetExtentStart(extents, 0) * (unsigned long long) gBlockSize + gAllocationOffset) / 512ULL;
420 return 0;
421}
422
423long HFSGetUUID(CICell ih, char *uuidStr)
424{
425 if (HFSInitPartition(ih) == -1) return -1;
426 if (gVolID == 0LL) return -1;
427
428 return CreateUUIDString((uint8_t*)(&gVolID), sizeof(gVolID), uuidStr);
429}
430
431// Private Functions
432
433static long ReadFile(void * file, uint64_t * length, void * base, uint64_t offset)
434{
435 void *extents;
436 long fileID;
437 uint64_t fileLength;
438 HFSCatalogFile *hfsFile = file;
439 HFSPlusCatalogFile *hfsPlusFile = file;
440
441 if (gIsHFSPlus) {
442 fileID = SWAP_BE32(hfsPlusFile->fileID);
443 fileLength = (uint64_t)SWAP_BE64(hfsPlusFile->dataFork.logicalSize);
444 extents = &hfsPlusFile->dataFork.extents;
445 } else {
446 fileID = SWAP_BE32(hfsFile->fileID);
447 fileLength = SWAP_BE32(hfsFile->dataLogicalSize);
448 extents = &hfsFile->dataExtents;
449 }
450
451 if (offset > fileLength) {
452 printf("Offset is too large.\n");
453 return -1;
454 }
455
456 if ((*length == 0) || ((offset + *length) > fileLength)) {
457 *length = fileLength - offset;
458 }
459
460/* if (*length > kLoadSize) {
461 printf("File is too large.\n");
462 return -1;
463 }*/
464
465 *length = ReadExtent((char *)extents, fileLength, fileID,
466 offset, *length, (char *)base, 0);
467
468 return 0;
469}
470
471static long GetCatalogEntryInfo(void * entry, long * flags, long * time,
472 FinderInfo * finderInfo, long * infoValid)
473{
474 long tmpTime = 0;
475 long valid = 0;
476
477 // Get information about the file.
478
479 switch ( SWAP_BE16(*(short *)entry) )
480 {
481 case kHFSFolderRecord :
482 *flags = kFileTypeDirectory;
483 tmpTime = SWAP_BE32(((HFSCatalogFolder *)entry)->modifyDate);
484 break;
485
486 case kHFSPlusFolderRecord :
487 *flags = kFileTypeDirectory |
488 (SWAP_BE16(((HFSPlusCatalogFolder *)entry)->bsdInfo.fileMode) & kPermMask);
489 if (SWAP_BE32(((HFSPlusCatalogFolder *)entry)->bsdInfo.ownerID) != 0)
490 *flags |= kOwnerNotRoot;
491 tmpTime = SWAP_BE32(((HFSPlusCatalogFolder *)entry)->contentModDate);
492 break;
493
494 case kHFSFileRecord :
495 *flags = kFileTypeFlat;
496 tmpTime = SWAP_BE32(((HFSCatalogFile *)entry)->modifyDate);
497 if (finderInfo) {
498 SwapFinderInfo((FndrFileInfo *)finderInfo, &((HFSCatalogFile *)entry)->userInfo);
499 valid = 1;
500 }
501 break;
502
503 case kHFSPlusFileRecord :
504 *flags = kFileTypeFlat |
505 (SWAP_BE16(((HFSPlusCatalogFile *)entry)->bsdInfo.fileMode) & kPermMask);
506 if (SWAP_BE32(((HFSPlusCatalogFile *)entry)->bsdInfo.ownerID) != 0)
507 *flags |= kOwnerNotRoot;
508 tmpTime = SWAP_BE32(((HFSPlusCatalogFile *)entry)->contentModDate);
509 if (finderInfo) {
510 SwapFinderInfo((FndrFileInfo *)finderInfo, &((HFSPlusCatalogFile *)entry)->userInfo);
511 valid = 1;
512 }
513 break;
514
515 case kHFSFileThreadRecord :
516 case kHFSPlusFileThreadRecord :
517 case kHFSFolderThreadRecord :
518 case kHFSPlusFolderThreadRecord :
519 *flags = kFileTypeUnknown;
520 tmpTime = 0;
521 break;
522 }
523
524 if (time != 0) {
525 // Convert base time from 1904 to 1970.
526 *time = tmpTime - 2082844800;
527 }
528 if (infoValid) *infoValid = valid;
529
530 return 0;
531}
532
533static long ResolvePathToCatalogEntry(char * filePath, long * flags,
534 void * entry, long dirID, long long * dirIndex)
535{
536 char *restPath;
537 long result, cnt, subFolderID = 0;
538 long long tmpDirIndex;
539 HFSPlusCatalogFile *hfsPlusFile;
540
541 // Copy the file name to gTempStr
542 cnt = 0;
543 while ((filePath[cnt] != '/') && (filePath[cnt] != '\0')) cnt++;
544 strlcpy(gTempStr, filePath, cnt+1);
545
546 // Move restPath to the right place.
547 if (filePath[cnt] != '\0') cnt++;
548 restPath = filePath + cnt;
549
550 // gTempStr is a name in the current Dir.
551 // restPath is the rest of the path if any.
552
553 result = ReadCatalogEntry(gTempStr, dirID, entry, dirIndex);
554 if (result == -1) {
555return -1;
556 }
557
558 GetCatalogEntryInfo(entry, flags, 0, 0, 0);
559
560 if ((*flags & kFileTypeMask) == kFileTypeDirectory) {
561 if (gIsHFSPlus)
562 subFolderID = SWAP_BE32(((HFSPlusCatalogFolder *)entry)->folderID);
563 else
564 subFolderID = SWAP_BE32(((HFSCatalogFolder *)entry)->folderID);
565 }
566
567 if ((*flags & kFileTypeMask) == kFileTypeDirectory)
568 result = ResolvePathToCatalogEntry(restPath, flags, entry,
569 subFolderID, dirIndex);
570
571 if (gIsHFSPlus && ((*flags & kFileTypeMask) == kFileTypeFlat)) {
572 hfsPlusFile = (HFSPlusCatalogFile *)entry;
573 if ((SWAP_BE32(hfsPlusFile->userInfo.fdType) == kHardLinkFileType) &&
574 (SWAP_BE32(hfsPlusFile->userInfo.fdCreator) == kHFSPlusCreator)) {
575 sprintf(gLinkTemp, "%s/%s%ld", HFSPLUSMETADATAFOLDER,
576 HFS_INODE_PREFIX, SWAP_BE32(hfsPlusFile->bsdInfo.special.iNodeNum));
577 result = ResolvePathToCatalogEntry(gLinkTemp, flags, entry,
578 kHFSRootFolderID, &tmpDirIndex);
579 }
580 }
581
582 return result;
583}
584
585static long GetCatalogEntry(long long * dirIndex, char ** name,
586 long * flags, long * time,
587 FinderInfo * finderInfo, long * infoValid)
588{
589 long extentSize, nodeSize, curNode, index;
590 void *extent;
591 char *nodeBuf, *testKey, *entry;
592 BTNodeDescriptor *node;
593
594 if (gIsHFSPlus) {
595 extent = &gHFSPlus->catalogFile.extents;
596 extentSize = SWAP_BE64(gHFSPlus->catalogFile.logicalSize);
597 } else {
598 extent = (HFSExtentDescriptor *)&gHFSMDB->drCTExtRec;
599 extentSize = SWAP_BE32(gHFSMDB->drCTFlSize);
600 }
601
602 nodeSize = SWAP_BE16(gBTHeaders[kBTreeCatalog]->nodeSize);
603 nodeBuf = (char *)malloc(nodeSize);
604 node = (BTNodeDescriptor *)nodeBuf;
605
606 index = (long) (*dirIndex % nodeSize);
607 curNode = (long) (*dirIndex / nodeSize);
608
609 // Read the BTree node and get the record for index.
610 ReadExtent(extent, extentSize, kHFSCatalogFileID,
611 (long long) curNode * nodeSize, nodeSize, nodeBuf, 1);
612 GetBTreeRecord(index, nodeBuf, nodeSize, &testKey, &entry);
613
614 GetCatalogEntryInfo(entry, flags, time, finderInfo, infoValid);
615
616 // Get the file name.
617 if (gIsHFSPlus) {
618 utf_encodestr(((HFSPlusCatalogKey *)testKey)->nodeName.unicode,
619 SWAP_BE16(((HFSPlusCatalogKey *)testKey)->nodeName.length),
620 (u_int8_t *)gTempStr, 256, OSBigEndian);
621 } else {
622 strncpy(gTempStr,
623 (const char *)&((HFSCatalogKey *)testKey)->nodeName[1],
624 ((HFSCatalogKey *)testKey)->nodeName[0]);
625 gTempStr[((HFSCatalogKey *)testKey)->nodeName[0]] = '\0';
626 }
627 *name = gTempStr;
628
629 // Update dirIndex.
630 index++;
631 if (index == SWAP_BE16(node->numRecords)) {
632 index = 0;
633 curNode = SWAP_BE32(node->fLink);
634 }
635 *dirIndex = (long long) curNode * nodeSize + index;
636
637 free(nodeBuf);
638
639 return 0;
640}
641
642static long ReadCatalogEntry(char * fileName, long dirID,
643 void * entry, long long * dirIndex)
644{
645 long length;
646 char key[sizeof(HFSPlusCatalogKey)];
647 HFSCatalogKey *hfsKey = (HFSCatalogKey *)key;
648 HFSPlusCatalogKey *hfsPlusKey = (HFSPlusCatalogKey *)key;
649
650 // Make the catalog key.
651 if ( gIsHFSPlus )
652 {
653 hfsPlusKey->parentID = SWAP_BE32(dirID);
654 length = strlen(fileName);
655 if (length > 255) length = 255;
656 utf_decodestr((u_int8_t *)fileName, hfsPlusKey->nodeName.unicode,
657 &(hfsPlusKey->nodeName.length), 512, OSBigEndian);
658 } else {
659 hfsKey->parentID = SWAP_BE32(dirID);
660 length = strlen(fileName);
661 if (length > 31) length = 31;
662 hfsKey->nodeName[0] = length;
663 strncpy((char *)(hfsKey->nodeName + 1), fileName, length);
664 }
665
666 return ReadBTreeEntry(kBTreeCatalog, &key, entry, dirIndex);
667}
668
669static long ReadExtentsEntry(long fileID, long startBlock, void * entry)
670{
671 char key[sizeof(HFSPlusExtentKey)];
672 HFSExtentKey *hfsKey = (HFSExtentKey *)key;
673 HFSPlusExtentKey *hfsPlusKey = (HFSPlusExtentKey *)key;
674
675 // Make the extents key.
676 if (gIsHFSPlus) {
677 hfsPlusKey->forkType = 0;
678 hfsPlusKey->fileID = SWAP_BE32(fileID);
679 hfsPlusKey->startBlock = SWAP_BE32(startBlock);
680 } else {
681 hfsKey->forkType = 0;
682 hfsKey->fileID = SWAP_BE32(fileID);
683 hfsKey->startBlock = SWAP_BE16(startBlock);
684 }
685
686 return ReadBTreeEntry(kBTreeExtents, &key, entry, 0);
687}
688
689static long ReadBTreeEntry(long btree, void * key, char * entry, long long * dirIndex)
690{
691 long extentSize;
692 void *extent;
693 short extentFile;
694 char *nodeBuf;
695 BTNodeDescriptor *node;
696 long nodeSize, result = 0, entrySize = 0;
697 long curNode, index = 0, lowerBound, upperBound;
698 char *testKey, *recordData;
699
700 // Figure out which tree is being looked at.
701 if (btree == kBTreeCatalog) {
702 if (gIsHFSPlus) {
703 extent = &gHFSPlus->catalogFile.extents;
704 extentSize = SWAP_BE64(gHFSPlus->catalogFile.logicalSize);
705 } else {
706 extent = (HFSExtentDescriptor *)&gHFSMDB->drCTExtRec;
707 extentSize = SWAP_BE32(gHFSMDB->drCTFlSize);
708 }
709 extentFile = kHFSCatalogFileID;
710 } else {
711 if (gIsHFSPlus) {
712 extent = &gHFSPlus->extentsFile.extents;
713 extentSize = SWAP_BE64(gHFSPlus->extentsFile.logicalSize);
714 } else {
715 extent = (HFSExtentDescriptor *)&gHFSMDB->drXTExtRec;
716 extentSize = SWAP_BE32(gHFSMDB->drXTFlSize);
717 }
718 extentFile = kHFSExtentsFileID;
719 }
720
721 // Read the BTree Header if needed.
722 if (gBTHeaders[btree] == 0) {
723 ReadExtent(extent, extentSize, extentFile, 0, 256,
724 gBTreeHeaderBuffer + btree * 256, 0);
725 gBTHeaders[btree] = (BTHeaderRec *)(gBTreeHeaderBuffer + btree * 256 +
726 sizeof(BTNodeDescriptor));
727 if ((gIsHFSPlus && btree == kBTreeCatalog) &&
728 (gBTHeaders[btree]->keyCompareType == kHFSBinaryCompare)) {
729 gCaseSensitive = 1;
730 }
731 }
732
733 curNode = SWAP_BE32(gBTHeaders[btree]->rootNode);
734 nodeSize = SWAP_BE16(gBTHeaders[btree]->nodeSize);
735 nodeBuf = (char *)malloc(nodeSize);
736 node = (BTNodeDescriptor *)nodeBuf;
737
738 while (1) {
739 // Read the current node.
740 ReadExtent(extent, extentSize, extentFile,
741 (long long) curNode * nodeSize, nodeSize, nodeBuf, 1);
742
743 // Find the matching key.
744 lowerBound = 0;
745 upperBound = SWAP_BE16(node->numRecords) - 1;
746 while (lowerBound <= upperBound) {
747 index = (lowerBound + upperBound) / 2;
748
749 GetBTreeRecord(index, nodeBuf, nodeSize, &testKey, &recordData);
750
751 if (gIsHFSPlus) {
752 if (btree == kBTreeCatalog) {
753 result = CompareHFSPlusCatalogKeys(key, testKey);
754 } else {
755 result = CompareHFSPlusExtentsKeys(key, testKey);
756 }
757 } else {
758 if (btree == kBTreeCatalog) {
759 result = CompareHFSCatalogKeys(key, testKey);
760 } else {
761 result = CompareHFSExtentsKeys(key, testKey);
762 }
763 }
764
765 if (result < 0) upperBound = index - 1; // search < trial
766 else if (result > 0) lowerBound = index + 1; // search > trial
767 else break; // search = trial
768 }
769
770 if (result < 0) {
771 index = upperBound;
772 GetBTreeRecord(index, nodeBuf, nodeSize, &testKey, &recordData);
773 }
774
775 // Found the closest key... Recurse on it if this is an index node.
776 if (node->kind == kBTIndexNode) {
777 curNode = SWAP_BE32( *((long *)recordData) );
778 } else break;
779 }
780
781 // Return error if the file was not found.
782 if (result != 0) { free(nodeBuf); return -1; }
783
784 if (btree == kBTreeCatalog) {
785 switch (SWAP_BE16(*(short *)recordData)) {
786 case kHFSFolderRecord : entrySize = 70; break;
787 case kHFSFileRecord : entrySize = 102; break;
788 case kHFSFolderThreadRecord : entrySize = 46; break;
789 case kHFSFileThreadRecord : entrySize = 46; break;
790 case kHFSPlusFolderRecord : entrySize = 88; break;
791 case kHFSPlusFileRecord : entrySize = 248; break;
792 case kHFSPlusFolderThreadRecord : entrySize = 264; break;
793 case kHFSPlusFileThreadRecord : entrySize = 264; 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 > (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: 665