MCPcopy Create free account
hub / github.com/diasurgical/devilution / PartStream_LoadBitmap

Function PartStream_LoadBitmap

3rdParty/StormLib/src/FileStream.cpp:1477–1539  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1475}
1476
1477static bool PartStream_LoadBitmap(TBlockStream * pStream)
1478{
1479 PPART_FILE_MAP_ENTRY FileBitmap;
1480 PART_FILE_HEADER PartHdr;
1481 ULONGLONG ByteOffset = 0;
1482 ULONGLONG StreamSize = 0;
1483 DWORD BlockCount;
1484 DWORD BitmapSize;
1485
1486 // Only if the size is greater than size of the bitmap header
1487 if(pStream->Base.File.FileSize > sizeof(PART_FILE_HEADER))
1488 {
1489 // Attempt to read PART file header
1490 if(pStream->BaseRead(pStream, &ByteOffset, &PartHdr, sizeof(PART_FILE_HEADER)))
1491 {
1492 // We need to swap PART file header on big-endian platforms
1493 BSWAP_ARRAY32_UNSIGNED(&PartHdr, sizeof(PART_FILE_HEADER));
1494
1495 // Verify the PART file header
1496 if(IsPartHeader(&PartHdr))
1497 {
1498 // Get the number of blocks and size of one block
1499 StreamSize = MAKE_OFFSET64(PartHdr.FileSizeHi, PartHdr.FileSizeLo);
1500 ByteOffset = sizeof(PART_FILE_HEADER);
1501 BlockCount = (DWORD)((StreamSize + PartHdr.BlockSize - 1) / PartHdr.BlockSize);
1502 BitmapSize = BlockCount * sizeof(PART_FILE_MAP_ENTRY);
1503
1504 // Check if sizes match
1505 if((ByteOffset + BitmapSize) < pStream->Base.File.FileSize)
1506 {
1507 // Allocate space for the array of PART_FILE_MAP_ENTRY
1508 FileBitmap = STORM_ALLOC(PART_FILE_MAP_ENTRY, BlockCount);
1509 if(FileBitmap != NULL)
1510 {
1511 // Load the block map
1512 if(!pStream->BaseRead(pStream, &ByteOffset, FileBitmap, BitmapSize))
1513 {
1514 STORM_FREE(FileBitmap);
1515 return false;
1516 }
1517
1518 // Make sure that the byte order is correct
1519 BSWAP_ARRAY32_UNSIGNED(FileBitmap, BitmapSize);
1520
1521 // Update the stream size
1522 pStream->BuildNumber = StringToInt(PartHdr.GameBuildNumber);
1523 pStream->StreamSize = StreamSize;
1524
1525 // Fill the bitmap information
1526 pStream->FileBitmap = FileBitmap;
1527 pStream->BitmapSize = BitmapSize;
1528 pStream->BlockSize = PartHdr.BlockSize;
1529 pStream->BlockCount = BlockCount;
1530 pStream->IsComplete = PartStream_CheckFile(pStream);
1531 return true;
1532 }
1533 }
1534 }

Callers 2

PartStream_CreateMirrorFunction · 0.85
PartStream_OpenFunction · 0.85

Calls 3

IsPartHeaderFunction · 0.85
StringToIntFunction · 0.85
PartStream_CheckFileFunction · 0.85

Tested by

no test coverage detected