LLVM  14.0.0git
MSFCommon.cpp
Go to the documentation of this file.
1 //===- MSFCommon.cpp - Common types and functions for MSF files -----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
11 #include "llvm/Support/Endian.h"
12 #include "llvm/Support/Error.h"
13 #include <cstdint>
14 #include <cstring>
15 
16 using namespace llvm;
17 using namespace llvm::msf;
18 
20  // Check the magic bytes.
21  if (std::memcmp(SB.MagicBytes, Magic, sizeof(Magic)) != 0)
22  return make_error<MSFError>(msf_error_code::invalid_format,
23  "MSF magic header doesn't match");
24 
25  if (!isValidBlockSize(SB.BlockSize))
26  return make_error<MSFError>(msf_error_code::invalid_format,
27  "Unsupported block size.");
28 
29  // We don't support directories whose sizes aren't a multiple of four bytes.
30  if (SB.NumDirectoryBytes % sizeof(support::ulittle32_t) != 0)
31  return make_error<MSFError>(msf_error_code::invalid_format,
32  "Directory size is not multiple of 4.");
33 
34  // The number of blocks which comprise the directory is a simple function of
35  // the number of bytes it contains.
36  uint64_t NumDirectoryBlocks =
38 
39  // The directory, as we understand it, is a block which consists of a list of
40  // block numbers. It is unclear what would happen if the number of blocks
41  // couldn't fit on a single block.
42  if (NumDirectoryBlocks > SB.BlockSize / sizeof(support::ulittle32_t))
43  return make_error<MSFError>(msf_error_code::invalid_format,
44  "Too many directory blocks.");
45 
46  if (SB.BlockMapAddr == 0)
47  return make_error<MSFError>(msf_error_code::invalid_format,
48  "Block 0 is reserved");
49 
50  if (SB.BlockMapAddr >= SB.NumBlocks)
51  return make_error<MSFError>(msf_error_code::invalid_format,
52  "Block map address is invalid.");
53 
54  if (SB.FreeBlockMapBlock != 1 && SB.FreeBlockMapBlock != 2)
55  return make_error<MSFError>(
57  "The free block map isn't at block 1 or block 2.");
58 
59  return Error::success();
60 }
61 
63  bool IncludeUnusedFpmData,
64  bool AltFpm) {
65  MSFStreamLayout FL;
66  uint32_t NumFpmIntervals =
67  getNumFpmIntervals(Msf, IncludeUnusedFpmData, AltFpm);
68 
69  uint32_t FpmBlock = AltFpm ? Msf.alternateFpmBlock() : Msf.mainFpmBlock();
70 
71  for (uint32_t I = 0; I < NumFpmIntervals; ++I) {
72  FL.Blocks.push_back(support::ulittle32_t(FpmBlock));
73  FpmBlock += msf::getFpmIntervalLength(Msf);
74  }
75 
76  if (IncludeUnusedFpmData)
77  FL.Length = NumFpmIntervals * Msf.SB->BlockSize;
78  else
79  FL.Length = divideCeil(Msf.SB->NumBlocks, 8);
80 
81  return FL;
82 }
llvm::msf::MSFLayout::alternateFpmBlock
uint32_t alternateFpmBlock() const
Definition: MSFCommon.h:59
llvm::msf::MSFStreamLayout::Blocks
std::vector< support::ulittle32_t > Blocks
Definition: MSFCommon.h:80
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::msf::getNumFpmIntervals
uint32_t getNumFpmIntervals(uint32_t BlockSize, uint32_t NumBlocks, bool IncludeUnusedFpmData, int FpmNumber)
Given an MSF with the specified block size and number of blocks, determine how many pieces the specif...
Definition: MSFCommon.h:132
llvm::msf::bytesToBlocks
uint64_t bytesToBlocks(uint64_t NumBytes, uint64_t BlockSize)
Definition: MSFCommon.h:108
llvm::support::detail::packed_endian_specific_integral
Definition: Endian.h:206
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
llvm::msf::SuperBlock::NumDirectoryBytes
support::ulittle32_t NumDirectoryBytes
Definition: MSFCommon.h:44
memcmp
Merge contiguous icmps into a memcmp
Definition: MergeICmps.cpp:912
Error.h
llvm::msf::isValidBlockSize
bool isValidBlockSize(uint32_t Size)
Definition: MSFCommon.h:90
llvm::msf::getFpmStreamLayout
MSFStreamLayout getFpmStreamLayout(const MSFLayout &Msf, bool IncludeUnusedFpmData=false, bool AltFpm=false)
Determine the layout of the FPM stream, given the MSF layout.
Definition: MSFCommon.cpp:62
llvm::msf
Definition: IMSFFile.h:18
llvm::msf::validateSuperBlock
Error validateSuperBlock(const SuperBlock &SB)
Definition: MSFCommon.cpp:19
llvm::msf::MSFLayout
Definition: MSFCommon.h:51
llvm::msf::SuperBlock
Definition: MSFCommon.h:31
llvm::msf::SuperBlock::NumBlocks
support::ulittle32_t NumBlocks
Definition: MSFCommon.h:42
llvm::divideCeil
uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition: MathExtras.h:742
llvm::msf::getFpmIntervalLength
uint32_t getFpmIntervalLength(const MSFLayout &L)
Definition: MSFCommon.h:116
uint64_t
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::msf::SuperBlock::FreeBlockMapBlock
support::ulittle32_t FreeBlockMapBlock
Definition: MSFCommon.h:38
llvm::msf::Magic
static const char Magic[]
Definition: MSFCommon.h:23
uint32_t
llvm::msf::MSFLayout::mainFpmBlock
uint32_t mainFpmBlock() const
Definition: MSFCommon.h:54
MSFCommon.h
llvm::msf::SuperBlock::MagicBytes
char MagicBytes[sizeof(Magic)]
Definition: MSFCommon.h:32
llvm::msf::MSFStreamLayout::Length
uint32_t Length
Definition: MSFCommon.h:79
llvm::msf::SuperBlock::BlockSize
support::ulittle32_t BlockSize
Definition: MSFCommon.h:36
MSFError.h
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::msf::msf_error_code::invalid_format
@ invalid_format
llvm::msf::MSFLayout::SB
const SuperBlock * SB
Definition: MSFCommon.h:64
Endian.h
llvm::msf::SuperBlock::BlockMapAddr
support::ulittle32_t BlockMapAddr
Definition: MSFCommon.h:48
llvm::msf::MSFStreamLayout
Describes the layout of a stream in an MSF layout.
Definition: MSFCommon.h:77