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

Archive Download this file

Revision: 698