ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
CheckedFile.h
Go to the documentation of this file.
1 /*
2  * Original work Copyright 2009 - 2010 Kevin Ackley (kackley@gwi.net)
3  * Modified work Copyright 2018 - 2020 Andy Maloney <asmaloney@gmail.com>
4  *
5  * Permission is hereby granted, free of charge, to any person or organization
6  * obtaining a copy of the software and accompanying documentation covered by
7  * this license (the "Software") to use, reproduce, display, distribute,
8  * execute, and transmit the Software, and to prepare derivative works of the
9  * Software, and to permit third-parties to whom the Software is furnished to
10  * do so, all subject to the following:
11  *
12  * The copyright notices in the Software and this entire statement, including
13  * the above license grant, this restriction and the following disclaimer,
14  * must be included in all copies of the Software, in whole or in part, and
15  * all derivative works of the Software, unless such copies or derivative
16  * works are solely in the form of machine-executable object code generated by
17  * a source language processor.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  * DEALINGS IN THE SOFTWARE.
26  */
27 
28 #pragma once
29 
30 #include <algorithm>
31 
32 #include "Common.h"
33 
34 namespace e57
35 {
40  class BufferView;
41 
43  {
44  public:
45  static constexpr size_t physicalPageSizeLog2 = 10; // physical page size is 2 raised to this power
46  static constexpr size_t physicalPageSize = 1 << physicalPageSizeLog2;
47  static constexpr uint64_t physicalPageSizeMask = physicalPageSize - 1;
48  static constexpr size_t logicalPageSize = physicalPageSize - 4;
49 
50  public:
51  enum Mode
52  {
56  };
57 
59  {
61  Physical
62  };
63 
65  CheckedFile( const char *input, uint64_t size, ReadChecksumPolicy policy );
66  ~CheckedFile();
67 
68  void read( char *buf, size_t nRead, size_t bufSize = 0 );
69  void write( const char *buf, size_t nWrite );
70  CheckedFile &operator<<( const e57::ustring &s );
71  CheckedFile &operator<<( int64_t i );
72  CheckedFile &operator<<( uint64_t i );
73  CheckedFile &operator<<( float f );
74  CheckedFile &operator<<( double d );
75  void seek( uint64_t offset, OffsetMode omode = Logical );
76  uint64_t position( OffsetMode omode = Logical );
77  uint64_t length( OffsetMode omode = Logical );
78  void extend( uint64_t newLength, OffsetMode omode = Logical );
80  {
81  return fileName_;
82  }
83  void close();
84  void unlink();
85 
86  static inline uint64_t logicalToPhysical( uint64_t logicalOffset );
87  static inline uint64_t physicalToLogical( uint64_t physicalOffset );
88 
89  private:
90  uint32_t checksum( char *buf, size_t size ) const;
91  void verifyChecksum( char *page_buffer, size_t page );
92 
93  template <class FTYPE> CheckedFile &writeFloatingPoint( FTYPE value, int precision );
94 
95  void getCurrentPageAndOffset( uint64_t &page, size_t &pageOffset, OffsetMode omode = Logical );
96  void readPhysicalPage( char *page_buffer, uint64_t page );
97  void writePhysicalPage( char *page_buffer, uint64_t page );
98  int open64( const e57::ustring &fileName, int flags, int mode );
99  uint64_t lseek64( int64_t offset, int whence );
100 
101  e57::ustring fileName_;
102  uint64_t logicalLength_ = 0;
103  uint64_t physicalLength_ = 0;
104 
105  ReadChecksumPolicy checkSumPolicy_ = CHECKSUM_POLICY_ALL;
106 
107  int fd_ = -1;
108  BufferView *bufView_ = nullptr;
109  bool readOnly_ = false;
110  };
111 
112  inline uint64_t CheckedFile::logicalToPhysical( uint64_t logicalOffset )
113  {
114  const uint64_t page = logicalOffset / logicalPageSize;
115  const uint64_t remainder = logicalOffset - page * logicalPageSize;
116 
117  return page * physicalPageSize + remainder;
118  }
119 
120  inline uint64_t CheckedFile::physicalToLogical( uint64_t physicalOffset )
121  {
122  const uint64_t page = physicalOffset >> physicalPageSizeLog2;
123  const size_t remainder = static_cast<size_t>( physicalOffset & physicalPageSizeMask );
124 
125  return page * logicalPageSize + std::min( remainder, logicalPageSize );
126  }
127 
128 }
int size
int offset
void write(const char *buf, size_t nWrite)
static constexpr uint64_t physicalPageSizeMask
Definition: CheckedFile.h:47
CheckedFile & operator<<(const e57::ustring &s)
static constexpr size_t physicalPageSize
Definition: CheckedFile.h:46
void read(char *buf, size_t nRead, size_t bufSize=0)
void extend(uint64_t newLength, OffsetMode omode=Logical)
uint64_t position(OffsetMode omode=Logical)
static uint64_t logicalToPhysical(uint64_t logicalOffset)
Definition: CheckedFile.h:112
static uint64_t physicalToLogical(uint64_t physicalOffset)
Definition: CheckedFile.h:120
CheckedFile(const e57::ustring &fileName, Mode mode, ReadChecksumPolicy policy)
static constexpr size_t physicalPageSizeLog2
Definition: CheckedFile.h:45
e57::ustring fileName() const
Definition: CheckedFile.h:79
static constexpr size_t logicalPageSize
Definition: CheckedFile.h:48
uint64_t length(OffsetMode omode=Logical)
void seek(uint64_t offset, OffsetMode omode=Logical)
int min(int a, int b)
Definition: cutil_math.h:53
constexpr ReadChecksumPolicy CHECKSUM_POLICY_ALL
Only verify 50% of the checksums. The last block is always verified.
Definition: E57Format.h:102
int ReadChecksumPolicy
Specifies the percentage of checksums which are verified when reading an ImageFile (0-100%).
Definition: E57Format.h:95
std::string ustring
UTF-8 encodeded Unicode string.
Definition: E57Format.h:54