ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
CheckedFile.cpp
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 #if defined( _WIN32 )
29 #if defined( _MSC_VER )
30 #include <codecvt>
31 #include <io.h>
32 #elif defined( __GNUC__ )
33 #define _LARGEFILE64_SOURCE
34 #define __LARGE64_FILES
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <unistd.h>
38 #else
39 #error "no supported compiler defined"
40 #endif
41 #elif defined( __linux__ )
42 #define _LARGEFILE64_SOURCE
43 #define __LARGE64_FILES
44 #include <sys/stat.h>
45 #include <sys/types.h>
46 #include <unistd.h>
47 #elif defined( __APPLE__ )
48 #include <sys/types.h>
49 #include <unistd.h>
50 #else
51 #error "no supported OS platform defined"
52 #endif
53 
54 #include <cmath>
55 #include <cstdio>
56 #include <cstring>
57 #include <fcntl.h>
58 
59 #include "CRC.h"
60 
61 #include "CheckedFile.h"
62 
63 // #define E57_CHECK_FILE_DEBUG
64 #ifdef E57_CHECK_FILE_DEBUG
65 #include <cassert>
66 #endif
67 
68 #ifndef O_BINARY
69 #define O_BINARY ( 0 )
70 #endif
71 
72 using namespace e57;
73 
74 // These extra definitions are required in C++11.
75 // In C++17, "static constexpr" is implicitly inline, so these are not required.
76 constexpr size_t CheckedFile::physicalPageSizeLog2;
77 constexpr size_t CheckedFile::physicalPageSize;
78 constexpr uint64_t CheckedFile::physicalPageSizeMask;
79 constexpr size_t CheckedFile::logicalPageSize;
80 
86 {
87 public:
90  BufferView( const char *input, uint64_t size ) : streamSize_( size ), stream_( input )
91  {
92  }
93 
94  uint64_t pos() const
95  {
96  return cursorStream_;
97  }
98 
99  bool seek( uint64_t offset, int whence )
100  {
101  if ( whence == SEEK_CUR )
102  {
103  cursorStream_ += offset;
104  }
105  else if ( whence == SEEK_SET )
106  {
107  cursorStream_ = offset;
108  }
109  else if ( whence == SEEK_END )
110  {
111  cursorStream_ = streamSize_ - offset;
112  }
113 
114  if ( cursorStream_ > streamSize_ )
115  {
116  cursorStream_ = streamSize_;
117  return false;
118  }
119 
120  return true;
121  }
122 
123  void read( char *buffer, uint64_t count )
124  {
125  const uint64_t start = cursorStream_;
126  for ( uint64_t i = 0; i < count; ++i )
127  {
128  buffer[i] = stream_[start + i];
129  ++cursorStream_;
130  }
131  }
132 
133 private:
134  const uint64_t streamSize_;
135  uint64_t cursorStream_ = 0;
136  const char *stream_;
137 };
138 
139 CheckedFile::CheckedFile( const ustring &fileName, Mode mode, ReadChecksumPolicy policy ) :
140  fileName_( fileName ), checkSumPolicy_( policy )
141 {
142  switch ( mode )
143  {
144  case ReadOnly:
145  fd_ = open64( fileName_, O_RDONLY | O_BINARY, 0 );
146 
147  readOnly_ = true;
148 
149  physicalLength_ = lseek64( 0LL, SEEK_END );
150  lseek64( 0, SEEK_SET );
151 
152  logicalLength_ = physicalToLogical( physicalLength_ );
153  break;
154 
155  case WriteCreate:
157  fd_ = open64( fileName_, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, S_IWRITE | S_IREAD );
158  break;
159 
160  case WriteExisting:
161  fd_ = open64( fileName_, O_RDWR | O_BINARY, 0 );
162 
163  logicalLength_ = physicalToLogical( length( Physical ) ); //???
164  break;
165  }
166 }
167 
168 CheckedFile::CheckedFile( const char *input, uint64_t size, ReadChecksumPolicy policy ) :
169  fileName_( "<StreamBuffer>" ), checkSumPolicy_( policy )
170 {
171  bufView_ = new BufferView( input, size );
172 
173  readOnly_ = true;
174 
175  physicalLength_ = lseek64( 0LL, SEEK_END );
176  lseek64( 0, SEEK_SET );
177 
178  logicalLength_ = physicalToLogical( physicalLength_ );
179 }
180 
181 int CheckedFile::open64( const ustring &fileName, int flags, int mode )
182 {
183 #if defined( _MSC_VER )
184  // Handle UTF-8 file names - Windows requires conversion to UTF-16
185  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
186  std::wstring widePath = converter.from_bytes( fileName );
187 
188  int handle;
189  int err = _wsopen_s( &handle, widePath.c_str(), flags, _SH_DENYNO, mode );
190  if ( handle < 0 )
191  {
192  throw E57_EXCEPTION2( E57_ERROR_OPEN_FAILED, "err=" + toString( err ) + " fileName=" + fileName +
193  " flags=" + toString( flags ) + " mode=" + toString( mode ) );
194  }
195  return handle;
196 #elif defined( __GNUC__ )
197  int result = open( fileName_.c_str(), flags, mode );
198  if ( result < 0 )
199  {
200  throw E57_EXCEPTION2( E57_ERROR_OPEN_FAILED, "result=" + toString( result ) + " fileName=" + fileName +
201  " flags=" + toString( flags ) + " mode=" + toString( mode ) );
202  }
203  return result;
204 #else
205 #error "no supported compiler defined"
206 #endif
207 }
208 
210 {
211  try
212  {
213  close();
214  }
215  catch ( ... )
216  {
217  //??? report?
218  }
219 }
220 
221 void CheckedFile::read( char *buf, size_t nRead, size_t /*bufSize*/ )
222 {
223  //??? what if read past logical end?, or physical end?
224  //??? need to keep track of logical length?
225  //??? check bufSize OK
226 
227  const uint64_t end = position( Logical ) + nRead;
228  const uint64_t logicalLength = length( Logical );
229 
230  if ( end > logicalLength )
231  {
232  throw E57_EXCEPTION2( E57_ERROR_INTERNAL, "fileName=" + fileName_ + " end=" + toString( end ) +
233  " length=" + toString( logicalLength ) );
234  }
235 
236  uint64_t page = 0;
237  size_t pageOffset = 0;
238 
239  getCurrentPageAndOffset( page, pageOffset );
240 
241  size_t n = std::min( nRead, logicalPageSize - pageOffset );
242 
244  std::vector<char> page_buffer_v( physicalPageSize );
245  char *page_buffer = &page_buffer_v[0];
246 
247  auto checksumMod = static_cast<const unsigned int>( std::nearbyint( 100.0 / checkSumPolicy_ ) );
248 
249  while ( nRead > 0 )
250  {
251  readPhysicalPage( page_buffer, page );
252 
253  switch ( checkSumPolicy_ )
254  {
256  break;
257 
258  case CHECKSUM_POLICY_ALL:
259  verifyChecksum( page_buffer, page );
260  break;
261 
262  default:
263  if ( !( page % checksumMod ) || ( nRead < physicalPageSize ) )
264  {
265  verifyChecksum( page_buffer, page );
266  }
267  break;
268  }
269 
270  memcpy( buf, page_buffer + pageOffset, n );
271 
272  buf += n;
273  nRead -= n;
274  pageOffset = 0;
275  ++page;
276 
277  n = std::min( nRead, logicalPageSize );
278  }
279 
281  seek( end, Logical );
282 }
283 
284 void CheckedFile::write( const char *buf, size_t nWrite )
285 {
286 #ifdef E57_MAX_VERBOSE
287  // cout << "write nWrite=" << nWrite << " position()="<< position() << std::endl;
288  // //???
289 #endif
290  if ( readOnly_ )
291  {
292  throw E57_EXCEPTION2( E57_ERROR_FILE_IS_READ_ONLY, "fileName=" + fileName_ );
293  }
294 
295  uint64_t end = position( Logical ) + nWrite;
296 
297  uint64_t page = 0;
298  size_t pageOffset = 0;
299 
300  getCurrentPageAndOffset( page, pageOffset );
301 
302  size_t n = std::min( nWrite, logicalPageSize - pageOffset );
303 
305  std::vector<char> page_buffer_v( physicalPageSize );
306  char *page_buffer = &page_buffer_v[0];
307 
308  while ( nWrite > 0 )
309  {
310  const uint64_t physicalLength = length( Physical );
311 
312  if ( page * physicalPageSize < physicalLength )
313  {
314  readPhysicalPage( page_buffer, page );
315  }
316 
317 #ifdef E57_MAX_VERBOSE
318  // cout << " page_buffer[0] read: '" << page_buffer[0] << "'" << std::endl;
319  // cout << "copy " << n << "bytes to page=" << page << " pageOffset=" <<
320  // pageOffset << " buf='"; //??? for (size_t i=0; i < n; i++) cout <<
321  // buf[i]; cout << "'" << std::endl;
322 #endif
323  memcpy( page_buffer + pageOffset, buf, n );
324  writePhysicalPage( page_buffer, page );
325 #ifdef E57_MAX_VERBOSE
326  // cout << " page_buffer[0] after write: '" << page_buffer[0] << "'" <<
327  // std::endl; //???
328 #endif
329  buf += n;
330  nWrite -= n;
331  pageOffset = 0;
332  page++;
333  n = std::min( nWrite, logicalPageSize );
334  }
335 
336  if ( end > logicalLength_ )
337  {
338  logicalLength_ = end;
339  }
340 
342  seek( end, Logical );
343 }
344 
346 {
347  write( s.c_str(), s.length() ); //??? should be times size of uchar?
348  return ( *this );
349 }
350 
352 {
353  std::stringstream ss;
354  ss << i;
355  return ( *this << ss.str() );
356 }
357 
359 {
360  std::stringstream ss;
361  ss << i;
362  return ( *this << ss.str() );
363 }
364 
366 {
367  //??? is 7 digits right number?
368  return writeFloatingPoint( f, 7 );
369 }
370 
372 {
373  //??? is 17 digits right number?
374  return writeFloatingPoint( d, 17 );
375 }
376 
377 template <class FTYPE> CheckedFile &CheckedFile::writeFloatingPoint( FTYPE value, int precision )
378 {
379 #ifdef E57_MAX_VERBOSE
380  std::cout << "CheckedFile::writeFloatingPoint, value=" << value << " precision=" << precision << std::endl;
381 #endif
382 
383  std::stringstream ss;
384  ss << std::scientific << std::setprecision( precision ) << value;
385 
389 
390  ustring s = ss.str();
391  const size_t len = s.length();
392 
393 #ifdef E57_MAX_DEBUG
394  ustring old_s = s;
395 #endif
396 
399  ustring mantissa = s.substr( 0, len - 5 );
400  ustring exponent = s.substr( len - 5, 5 );
401 
403  if ( exponent[0] == 'e' )
404  {
406  while ( mantissa[mantissa.length() - 1] == '0' )
407  {
408  mantissa = mantissa.substr( 0, mantissa.length() - 1 );
409  }
410 
412  if ( mantissa[mantissa.length() - 1] == '.' )
413  {
414  mantissa = mantissa.substr( 0, mantissa.length() - 1 );
415  }
416 
419  if ( exponent == "e+000" )
420  {
421  s = mantissa;
422  }
423  else
424  {
425  s = mantissa + exponent;
426  }
427  }
428 
429  // Disable these checks because they compare floats using "!=" which is
430  // invalid
431 #if 0 // E57_MAX_DEBUG
433  FTYPE old_value = static_cast<FTYPE>(atof(old_s.c_str()));
434  FTYPE new_value = static_cast<FTYPE>(atof(s.c_str()));
435  if (old_value != new_value)
436  throw E57_EXCEPTION2(E57_ERROR_INTERNAL, "fileName=" + fileName_ + " oldValue=" + toString(old_value) + " newValue=" + toString(new_value));
437  if (new_value != value)
438  throw E57_EXCEPTION2(E57_ERROR_INTERNAL, "fileName=" + fileName_ + " newValue=" + toString(new_value) + " value=" + toString(value));
439 #endif
440 
441  return ( *this << s );
442 }
443 
444 void CheckedFile::seek( uint64_t offset, OffsetMode omode )
445 {
446  //??? check for seek beyond logicalLength_
447  const auto pos = static_cast<int64_t>( omode == Physical ? offset : logicalToPhysical( offset ) );
448 
449 #ifdef E57_MAX_VERBOSE
450  // cout << "seek offset=" << offset << " omode=" << omode << " pos=" << pos
451  // << std::endl; //???
452 #endif
453  lseek64( pos, SEEK_SET );
454 }
455 
456 uint64_t CheckedFile::lseek64( int64_t offset, int whence )
457 {
458  if ( ( fd_ < 0 ) && ( bufView_ != nullptr ) )
459  {
460  const auto uoffset = static_cast<uint64_t>( offset );
461 
462  if ( bufView_->seek( uoffset, whence ) )
463  {
464  return bufView_->pos();
465  }
466 
467  throw E57_EXCEPTION2( E57_ERROR_LSEEK_FAILED, "fileName=" + fileName_ + " offset=" + toString( offset ) +
468  " whence=" + toString( whence ) );
469  }
470 
471 #if defined( _WIN32 )
472 #if defined( _MSC_VER ) || defined( __MINGW32__ ) //<rs 2010-06-16> mingw _is_ WIN32!
473  __int64 result = _lseeki64( fd_, offset, whence );
474 #elif defined( __GNUC__ ) //<rs 2010-06-16> this most likely will not get
475  // triggered (cygwin != WIN32)?
476 #ifdef E57_MAX_DEBUG
477  if ( sizeof( off_t ) != sizeof( offset ) )
478  throw E57_EXCEPTION2( E57_ERROR_INTERNAL, "sizeof(off_t)=" + toString( sizeof( off_t ) ) );
479 #endif
480  int64_t result = ::lseek( fd_, offset, whence );
481 #else
482 #error "no supported compiler defined"
483 #endif
484 #elif defined( __linux__ )
485  int64_t result = ::lseek64( fd_, offset, whence );
486 #elif defined( __APPLE__ )
487  int64_t result = ::lseek( fd_, offset, whence );
488 #else
489 #error "no supported OS platform defined"
490 #endif
491 
492  if ( result < 0 )
493  {
494  throw E57_EXCEPTION2( E57_ERROR_LSEEK_FAILED, "fileName=" + fileName_ + " offset=" + toString( offset ) +
495  " whence=" + toString( whence ) +
496  " result=" + toString( result ) );
497  }
498 
499  return static_cast<uint64_t>( result );
500 }
501 
503 {
505  const uint64_t pos = lseek64( 0LL, SEEK_CUR );
506 
507  if ( omode == Physical )
508  {
509  return pos;
510  }
511 
512  return physicalToLogical( pos );
513 }
514 
516 {
517  if ( omode == Physical )
518  {
519  if ( readOnly_ )
520  {
521  return physicalLength_;
522  }
523 
524  // Current file position
525  uint64_t original_pos = lseek64( 0LL, SEEK_CUR );
526 
527  // End file position
528  uint64_t end_pos = lseek64( 0LL, SEEK_END );
529 
530  // Restore original position
531  lseek64( original_pos, SEEK_SET );
532 
533  return end_pos;
534  }
535 
536  return logicalLength_;
537 }
538 
539 void CheckedFile::extend( uint64_t newLength, OffsetMode omode )
540 {
541 #ifdef E57_MAX_VERBOSE
542  // cout << "extend newLength=" << newLength << " omode="<< omode << std::endl;
543  // //???
544 #endif
545  if ( readOnly_ )
546  {
547  throw E57_EXCEPTION2( E57_ERROR_FILE_IS_READ_ONLY, "fileName=" + fileName_ );
548  }
549 
550  uint64_t newLogicalLength = 0;
551 
552  if ( omode == Physical )
553  {
554  newLogicalLength = physicalToLogical( newLength );
555  }
556  else
557  {
558  newLogicalLength = newLength;
559  }
560 
561  uint64_t currentLogicalLength = length( Logical );
562 
564  if ( newLogicalLength < currentLogicalLength )
565  {
566  throw E57_EXCEPTION2( E57_ERROR_INTERNAL, "fileName=" + fileName_ + " newLength=" + toString( newLogicalLength ) +
567  " currentLength=" + toString( currentLogicalLength ) );
568  }
569 
571  uint64_t nWrite = newLogicalLength - currentLogicalLength;
572 
574  seek( currentLogicalLength, Logical );
575 
576  uint64_t page = 0;
577  size_t pageOffset = 0;
578 
579  getCurrentPageAndOffset( page, pageOffset );
580 
583  size_t n = 0;
584 
585  if ( nWrite < logicalPageSize - pageOffset )
586  {
587  n = static_cast<size_t>( nWrite );
588  }
589  else
590  {
591  n = logicalPageSize - pageOffset;
592  }
593 
595  std::vector<char> page_buffer_v( physicalPageSize );
596  char *page_buffer = &page_buffer_v[0];
597 
598  while ( nWrite > 0 )
599  {
600  const uint64_t physicalLength = length( Physical );
601 
602  if ( page * physicalPageSize < physicalLength )
603  {
604  readPhysicalPage( page_buffer, page );
605  }
606 
607 #ifdef E57_MAX_VERBOSE
608  // cout << "extend " << n << "bytes on page=" << page << " pageOffset=" <<
609  // pageOffset << std::endl;
610  // //???
611 #endif
612  memset( page_buffer + pageOffset, 0, n );
613  writePhysicalPage( page_buffer, page );
614 
615  nWrite -= n;
616  pageOffset = 0;
617  ++page;
618 
619  if ( nWrite < logicalPageSize )
620  {
621  n = static_cast<size_t>( nWrite );
622  }
623  else
624  {
625  n = logicalPageSize;
626  }
627  }
628 
629  //??? what if loop above throws, logicalLength_ may be wrong
630  logicalLength_ = newLogicalLength;
631 
633  seek( newLogicalLength, Logical );
634 }
635 
637 {
638  if ( fd_ >= 0 )
639  {
640 #if defined( _MSC_VER )
641  int result = ::_close( fd_ );
642 #elif defined( __GNUC__ )
643  int result = ::close( fd_ );
644 #else
645 #error "no supported compiler defined"
646 #endif
647  if ( result < 0 )
648  {
649  throw E57_EXCEPTION2( E57_ERROR_CLOSE_FAILED, "fileName=" + fileName_ + " result=" + toString( result ) );
650  }
651 
652  fd_ = -1;
653  }
654 
655  if ( bufView_ != nullptr )
656  {
657  delete bufView_;
658  bufView_ = nullptr;
659 
660  // WARNING: do NOT delete buffer of bufView_ because
661  // pointer is handled by user !!
662  }
663 }
664 
666 {
667  close();
668 
670  int result = std::remove( fileName_.c_str() ); //??? unicode support here
671  (void)result; // this maybe unused
672 #ifdef E57_MAX_VERBOSE
673  if ( result < 0 )
674  {
675  std::cout << "std::remove() failed, result=" << result << std::endl;
676  }
677 #endif
678 }
679 
680 inline uint32_t swap_uint32( uint32_t val )
681 {
682  val = ( ( val << 8 ) & 0xFF00FF00 ) | ( ( val >> 8 ) & 0xFF00FF );
683 
684  return ( val << 16 ) | ( val >> 16 );
685 }
686 
688 uint32_t CheckedFile::checksum( char *buf, size_t size ) const
689 {
690  static const CRC::Parameters<crcpp_uint32, 32> sCRCParams{ 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true };
691 
692  static const CRC::Table<crcpp_uint32, 32> sCRCTable = sCRCParams.MakeTable();
693 
694  auto crc = CRC::Calculate<crcpp_uint32, 32>( buf, size, sCRCTable );
695 
696  // (Andy) I don't understand why we need to swap bytes here
697  crc = swap_uint32( crc );
698 
699  return crc;
700 }
701 
702 void CheckedFile::verifyChecksum( char *page_buffer, size_t page )
703 {
704  const uint32_t check_sum = checksum( page_buffer, logicalPageSize );
705  const uint32_t check_sum_in_page = *reinterpret_cast<uint32_t *>( &page_buffer[logicalPageSize] );
706 
707  if ( check_sum_in_page != check_sum )
708  {
709  const uint64_t physicalLength = length( Physical );
710 
712  "fileName=" + fileName_ + " computedChecksum=" + toString( check_sum ) +
713  " storedChecksum=" + toString( check_sum_in_page ) + " page=" + toString( page ) +
714  " length=" + toString( physicalLength ) );
715  }
716 }
717 
718 void CheckedFile::getCurrentPageAndOffset( uint64_t &page, size_t &pageOffset, OffsetMode omode )
719 {
720  const uint64_t pos = position( omode );
721 
722  if ( omode == Physical )
723  {
724  page = pos >> physicalPageSizeLog2;
725  pageOffset = static_cast<size_t>( pos & physicalPageSizeMask );
726  }
727  else
728  {
729  page = pos / logicalPageSize;
730  pageOffset = static_cast<size_t>( pos - page * logicalPageSize );
731  }
732 }
733 
734 void CheckedFile::readPhysicalPage( char *page_buffer, uint64_t page )
735 {
736 #ifdef E57_MAX_VERBOSE
737  // cout << "readPhysicalPage, page:" << page << std::endl;
738 #endif
739 
740 #ifdef E57_CHECK_FILE_DEBUG
741  const uint64_t physicalLength = length( Physical );
742 
743  assert( page * physicalPageSize < physicalLength );
744 #endif
745 
747  seek( page * physicalPageSize, Physical );
748 
749  if ( ( fd_ < 0 ) && ( bufView_ != nullptr ) )
750  {
751  bufView_->read( page_buffer, physicalPageSize );
752  return;
753  }
754 
755 #if defined( _MSC_VER )
756  int result = ::_read( fd_, page_buffer, physicalPageSize );
757 #elif defined( __GNUC__ )
758  ssize_t result = ::read( fd_, page_buffer, physicalPageSize );
759 #else
760 #error "no supported compiler defined"
761 #endif
762 
763  if ( result < 0 || static_cast<size_t>( result ) != physicalPageSize )
764  {
765  throw E57_EXCEPTION2( E57_ERROR_READ_FAILED, "fileName=" + fileName_ + " result=" + toString( result ) );
766  }
767 }
768 
769 void CheckedFile::writePhysicalPage( char *page_buffer, uint64_t page )
770 {
771 #ifdef E57_MAX_VERBOSE
772  // cout << "writePhysicalPage, page:" << page << std::endl;
773 #endif
774 
776  uint32_t check_sum = checksum( page_buffer, logicalPageSize );
777  *reinterpret_cast<uint32_t *>( &page_buffer[logicalPageSize] ) = check_sum; //??? little endian dependency
778 
780  seek( page * physicalPageSize, Physical );
781 
782 #if defined( _MSC_VER )
783  int result = ::_write( fd_, page_buffer, physicalPageSize );
784 #elif defined( __GNUC__ )
785  ssize_t result = ::write( fd_, page_buffer, physicalPageSize );
786 #else
787 #error "no supported compiler defined"
788 #endif
789 
790  if ( result < 0 )
791  {
792  throw E57_EXCEPTION2( E57_ERROR_WRITE_FAILED, "fileName=" + fileName_ + " result=" + toString( result ) );
793  }
794 }
#define O_BINARY
Definition: CheckedFile.cpp:69
uint32_t swap_uint32(uint32_t val)
int size
int count
int offset
#define E57_EXCEPTION2(ecode, context)
Definition: Common.h:68
core::Tensor result
Definition: VtkUtils.cpp:76
uint64_t pos() const
Definition: CheckedFile.cpp:94
void read(char *buffer, uint64_t count)
bool seek(uint64_t offset, int whence)
Definition: CheckedFile.cpp:99
BufferView(const char *input, uint64_t size)
Definition: CheckedFile.cpp:90
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
QTextStream & endl(QTextStream &stream)
Definition: QtCompat.h:718
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
@ E57_ERROR_FILE_IS_READ_ONLY
can't modify read only file
Definition: E57Exception.h:60
@ E57_ERROR_READ_FAILED
read() failed
Definition: E57Exception.h:64
@ E57_ERROR_INTERNAL
An unrecoverable inconsistent internal state was detected.
Definition: E57Exception.h:56
@ E57_ERROR_OPEN_FAILED
open() failed
Definition: E57Exception.h:62
@ E57_ERROR_WRITE_FAILED
write() failed
Definition: E57Exception.h:65
@ E57_ERROR_BAD_CHECKSUM
checksum mismatch, file is corrupted
Definition: E57Exception.h:61
@ E57_ERROR_CLOSE_FAILED
close() failed
Definition: E57Exception.h:63
@ E57_ERROR_LSEEK_FAILED
lseek() failed
Definition: E57Exception.h:66
std::string ustring
UTF-8 encodeded Unicode string.
Definition: E57Format.h:54
std::string toString(T x)
Definition: Common.h:80
constexpr ReadChecksumPolicy CHECKSUM_POLICY_NONE
Definition: E57Format.h:97
#define SEEK_SET
Definition: qioapi.cpp:38
#define SEEK_CUR
Definition: qioapi.cpp:30
#define SEEK_END
Definition: qioapi.cpp:34
CRC parameters.
Definition: CRC.h:152
CRC lookup table. After construction, the CRC parameters are fixed.
Definition: CRC.h:167