LLVM  8.0.1
MSFCommon.cpp
Go to the documentation of this file.
1 //===- MSFCommon.cpp - Common types and functions for MSF files -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
12 #include "llvm/Support/Endian.h"
13 #include "llvm/Support/Error.h"
14 #include <cstdint>
15 #include <cstring>
16 
17 using namespace llvm;
18 using namespace llvm::msf;
19 
21  // Check the magic bytes.
22  if (std::memcmp(SB.MagicBytes, Magic, sizeof(Magic)) != 0)
23  return make_error<MSFError>(msf_error_code::invalid_format,
24  "MSF magic header doesn't match");
25 
26  if (!isValidBlockSize(SB.BlockSize))
27  return make_error<MSFError>(msf_error_code::invalid_format,
28  "Unsupported block size.");
29 
30  // We don't support directories whose sizes aren't a multiple of four bytes.
31  if (SB.NumDirectoryBytes % sizeof(support::ulittle32_t) != 0)
32  return make_error<MSFError>(msf_error_code::invalid_format,
33  "Directory size is not multiple of 4.");
34 
35  // The number of blocks which comprise the directory is a simple function of
36  // the number of bytes it contains.
37  uint64_t NumDirectoryBlocks =
39 
40  // The directory, as we understand it, is a block which consists of a list of
41  // block numbers. It is unclear what would happen if the number of blocks
42  // couldn't fit on a single block.
43  if (NumDirectoryBlocks > SB.BlockSize / sizeof(support::ulittle32_t))
44  return make_error<MSFError>(msf_error_code::invalid_format,
45  "Too many directory blocks.");
46 
47  if (SB.BlockMapAddr == 0)
48  return make_error<MSFError>(msf_error_code::invalid_format,
49  "Block 0 is reserved");
50 
51  if (SB.BlockMapAddr >= SB.NumBlocks)
52  return make_error<MSFError>(msf_error_code::invalid_format,
53  "Block map address is invalid.");
54 
55  if (SB.FreeBlockMapBlock != 1 && SB.FreeBlockMapBlock != 2)
56  return make_error<MSFError>(
58  "The free block map isn't at block 1 or block 2.");
59 
60  return Error::success();
61 }
62 
64  bool IncludeUnusedFpmData,
65  bool AltFpm) {
66  MSFStreamLayout FL;
67  uint32_t NumFpmIntervals =
68  getNumFpmIntervals(Msf, IncludeUnusedFpmData, AltFpm);
69 
70  uint32_t FpmBlock = AltFpm ? Msf.alternateFpmBlock() : Msf.mainFpmBlock();
71 
72  for (uint32_t I = 0; I < NumFpmIntervals; ++I) {
73  FL.Blocks.push_back(support::ulittle32_t(FpmBlock));
74  FpmBlock += msf::getFpmIntervalLength(Msf);
75  }
76 
77  if (IncludeUnusedFpmData)
78  FL.Length = NumFpmIntervals * Msf.SB->BlockSize;
79  else
80  FL.Length = divideCeil(Msf.SB->NumBlocks, 8);
81 
82  return FL;
83 }
char MagicBytes[sizeof(Magic)]
Definition: MSFCommon.h:33
This class represents lattice values for constants.
Definition: AllocatorList.h:24
Error validateSuperBlock(const SuperBlock &SB)
Definition: MSFCommon.cpp:20
Describes the layout of a stream in an MSF layout.
Definition: MSFCommon.h:78
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:63
uint32_t getFpmIntervalLength(const MSFLayout &L)
Definition: MSFCommon.h:117
std::vector< support::ulittle32_t > Blocks
Definition: MSFCommon.h:81
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:133
support::ulittle32_t BlockSize
Definition: MSFCommon.h:37
support::ulittle32_t BlockMapAddr
Definition: MSFCommon.h:49
static const char Magic[]
Definition: MSFCommon.h:24
uint32_t alternateFpmBlock() const
Definition: MSFCommon.h:60
static ErrorSuccess success()
Create a success value.
Definition: Error.h:327
uint64_t bytesToBlocks(uint64_t NumBytes, uint64_t BlockSize)
Definition: MSFCommon.h:109
Merge contiguous icmps into a memcmp
Definition: MergeICmps.cpp:867
#define I(x, y, z)
Definition: MD5.cpp:58
uint32_t mainFpmBlock() const
Definition: MSFCommon.h:55
uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition: MathExtras.h:699
support::ulittle32_t FreeBlockMapBlock
Definition: MSFCommon.h:39
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
bool isValidBlockSize(uint32_t Size)
Definition: MSFCommon.h:91
const SuperBlock * SB
Definition: MSFCommon.h:65
support::ulittle32_t NumDirectoryBytes
Definition: MSFCommon.h:45
support::ulittle32_t NumBlocks
Definition: MSFCommon.h:43