MCPcopy Create free account
hub / github.com/emoose/xbox-reversing / StfsGetLevelNHashEntry

Method StfsGetLevelNHashEntry

stfschk/StfsFileSystem.cs:530–607  ·  view source on GitHub ↗
(int BlockNumber, int Level, ref byte[] ExpectedHash, bool UseSecondaryBlock)

Source from the content-addressed store, hash-verified

528 }
529
530 STF_HASH_ENTRY StfsGetLevelNHashEntry(int BlockNumber, int Level, ref byte[] ExpectedHash, bool UseSecondaryBlock)
531 {
532 int record = BlockNumber;
533 if (Level > 0)
534 record /= kDataBlocksPerHashLevel[Level - 1];
535
536 record %= kDataBlocksPerHashLevel[0];
537
538 if(BlockNumber == 0x0007f404 && Level == 0)
539 {
540 record = record;
541 }
542
543 var backingBlock = StfsComputeLevelNBackingHashBlockNumber(BlockNumber, Level);
544 long hashOffset = StfsBackingBlockToOffset(backingBlock);
545
546 if (UseSecondaryBlock && !StfsVolumeDescriptor.ReadOnlyFormat)
547 hashOffset += kSectorSize;
548
549 bool isInvalidTable = InvalidTables.Contains(hashOffset);
550 if (!CachedTables.ContainsKey(hashOffset))
551 {
552 // Cache the table in memory, since it's likely to be needed again
553 byte[] block = new byte[kSectorSize];
554 if (Stream.Length > hashOffset)
555 {
556 lock (StreamLock)
557 {
558 Stream.Seek(hashOffset, SeekOrigin.Begin);
559 Stream.Read(block, 0, (int)kSectorSize);
560 }
561 }
562
563 var hashBlock = Utility.BytesToStruct<STF_HASH_BLOCK>(block);
564 hashBlock.EndianSwap();
565 CachedTables.Add(hashOffset, hashBlock);
566
567 if (!isInvalidTable)
568 {
569 if (hashOffset >= Stream.Length)
570 {
571 // hash offset outside file...
572 isInvalidTable = true;
573 InvalidTables.Add(hashOffset);
574 }
575 else if (!SkipHashChecks)
576 {
577 // It's not cached and not in the invalid table array yet... lets check it
578 byte[] hash;
579 lock (Sha1)
580 hash = Sha1.ComputeHash(block);
581
582 if (!hash.BytesMatch(ExpectedHash))
583 {
584 isInvalidTable = true;
585 InvalidTables.Add(hashOffset);
586 //Console.WriteLine($"Invalid hash table at 0x{hashOffset:X}!");
587 }

Callers

nothing calls this directly

Calls 4

ReadMethod · 0.80
AddMethod · 0.80
BytesMatchMethod · 0.80
EndianSwapMethod · 0.45

Tested by

no test coverage detected