NCBI C++ ToolKit
compress.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef UTIL_COMPRESS__COMPRESS__HPP
2 #define UTIL_COMPRESS__COMPRESS__HPP
3 
4 /* $Id: compress.hpp 98408 2022-11-09 13:48:43Z ivanov $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Vladimir Ivanov
30  *
31  * File Description: The Compression API
32  *
33  */
34 
35 /// @file compress.hpp
36 ///
37 /// @warning
38 /// Compression API have support for some compression libraies. Toolkit checks
39 /// existing compressions libraries on a configuration stage. If system version
40 /// of some compression library is not found, for some libraries like BZIP22, ZLIB,
41 /// GZIP we have an embedded alternatives, that will be used automatically.
42 /// But for some others, like LZO and ZSTD, we dont have such options.
43 /// The configure define next macros, that can be used to put specific library
44 /// related code to corresponding #if/#endif block to avoid compilation errors
45 /// on some platforms that miss a specific compression library:
46 ///
47 /// - HAVE_LIBZ // for ZLIB and GZIP
48 /// - HAVE_LIBBZ2 // for BZIP2
49 /// - HAVE_LIBLZO // for LZO
50 /// - HAVE_LIBZSTD // for ZSTD
51 ///
52 /// #if defined(HAVE_LIB***)
53 /// // use selected library related code here
54 /// #else
55 /// // some backup code, or error reporting
56 /// #endif
57 ///
58 /// Theoretically, you need a check for LZO and ZSTD only.
59 ///
60 /// This is related to a library-specific classes only. Many other common compession
61 /// classes/interfaces can be used without such guards, like ICompression or streams,
62 /// for example.
63 
64 
65 #include <corelib/ncbistd.hpp>
66 #include <corelib/version_api.hpp>
67 
68 
69 /** @addtogroup Compression
70  *
71  * @{
72  */
73 
74 
76 
77 /// Macro to report errors in compression API.
78 ///
79 /// Error codes for ERR_COMPRESS and OMPRESS_HANDLE_EXCEPTIONS are really
80 /// a subcodes and current maximum value is defined in 'include/util/error_codes.hpp':
81 /// NCBI_DEFINE_ERRCODE_X(Util_Compress, 210, max);
82 /// For new values use 'max'+1 and update it there.
83 ///
84 #define ERR_COMPRESS(subcode, message) ERR_POST_X(subcode, Warning << message)
85 
86 /// Macro to catch and handle exceptions (from streams in the destructor)
87 #define COMPRESS_HANDLE_EXCEPTIONS(subcode, message) \
88  catch (CException& e) { \
89  try { \
90  NCBI_REPORT_EXCEPTION_X(subcode, message, e); \
91  } catch (...) { \
92  } \
93  } \
94  catch (exception& e) { \
95  try { \
96  ERR_POST_X(subcode, Error \
97  << "[" << message \
98  << "] Exception: " << e.what()); \
99  } catch (...) { \
100  } \
101  } \
102  catch (...) { \
103  try { \
104  ERR_POST_X(subcode, Error \
105  << "[" << message << "] Unknown exception"); \
106  } catch (...) { \
107  } \
108  } \
109 
110 /// Default compression I/O stream buffer size.
111 const streamsize kCompressionDefaultBufSize = 16*1024;
112 
113 
114 // Forward declaration
115 class CCompressionFile;
118 
119 
120 //////////////////////////////////////////////////////////////////////////////
121 //
122 // ICompression -- abstract interface class
123 //
124 
126 {
127 public:
128  /// Compression level.
129  ///
130  /// It is in range [0..9]. Increase of level might mean better compression
131  /// and usualy greater time of compression. Usualy 1 gives best speed,
132  /// 9 gives best compression, 0 gives no compression at all (if supported).
133  /// eDefault value requests a compromise between speed and compression
134  /// (according to developers of the corresponding compression algorithm).
135  /// @note
136  /// Each compression library/algorithm have its own set of compression levels,
137  /// than not usually fit into [0..9] range. But for convenience of use and
138  /// easier switching we prowide conversion between levels, whehe "lowest"
139  /// is a always a minimem allowed compression level, and "best" is a maximum.
140  /// All other approximately fits in between.
141  ///
142  enum ELevel {
143  eLevel_Default = -1, // default
144  eLevel_NoCompression = 0, // just store data (if supported, or eLevel_Lowest)
145  eLevel_Lowest = 1,
146  eLevel_VeryLow = 2,
147  eLevel_Low = 3,
148  eLevel_MediumLow = 4,
149  eLevel_Medium = 5,
150  eLevel_MediumHigh = 6,
151  eLevel_High = 7,
152  eLevel_VeryHigh = 8,
153  eLevel_Best = 9
154  };
155 
156  /// Compression flags. The flag selection depends from compression
157  /// algorithm implementation. For examples see the flags defined
158  /// in the derived classes: CBZip2Compression::EFlags,
159  /// CLZOCompression::EFlags, CZipCompression::EFlags, etc.
160  typedef unsigned int TFlags; ///< Bitwise OR of CXxxCompression::EFlags
161 
162 public:
163  /// Destructor
164  virtual ~ICompression(void) {}
165 
166  /// Return name and version of the compression library.
167  virtual CVersionInfo GetVersion(void) const = 0;
168 
169  // Get/set compression level.
170  virtual void SetLevel(ELevel level) = 0;
171  virtual ELevel GetLevel(void) const = 0;
172 
173  /// Return the default compression level for current compression algorithm.
174  virtual ELevel GetDefaultLevel(void) const = 0;
175 
176  // Get compressor's internal status/error code and description
177  // for the last operation.
178  virtual int GetErrorCode(void) const = 0;
179  virtual string GetErrorDescription(void) const = 0;
180 
181  // Get/set flags
182  virtual TFlags GetFlags(void) const = 0;
183  virtual void SetFlags(TFlags flags) = 0;
184 
185 
186  //=======================================================================
187  // Supported features
188  //=======================================================================
189 
190  /// Supported features
192  /// Check if current compression method have a real support for
193  /// eLevel_NoCompression, so it can store data without compression.
194  /// If this method of compression is not supported, the lowest
195  /// supported level will be used instead, automatically.
196  /// @sa GetLevel, ELevel
198 
199  /// Check if current compression have support for dictionaries.
200  /// Without support SetDictionary() always return FALSE.
201  /// @sa SetDictionary
203 
204  /// Check if current compression have implementation for
205  /// the EstimateCompressionBufferSize() method.
206  /// Without support it always return 0.
207  /// @sa EstimateCompressionBufferSize, CompressBuffer
209  };
210 
211  /// Check if compression have support for a specified feature
212  virtual bool HaveSupport(ESupportFeature feature) = 0;
213 
214 
215  //=======================================================================
216  // Utility functions
217  //=======================================================================
218 
219  /// (De)compress the source buffer into the destination buffer.
220  /// Return TRUE on success, FALSE on error.
221  /// The compressor error code can be acquired via GetErrorCode() call.
222  /// Notice that altogether the total size of the destination buffer must
223  /// be little more then size of the source buffer.
224  virtual bool CompressBuffer(
225  const void* src_buf, size_t src_len,
226  void* dst_buf, size_t dst_size,
227  /* out */ size_t* dst_len
228  ) = 0;
229 
230  virtual bool DecompressBuffer(
231  const void* src_buf, size_t src_len,
232  void* dst_buf, size_t dst_size,
233  /* out */ size_t* dst_len
234  ) = 0;
235 
236  /// Estimate buffer size for data compression (if supported).
237  virtual size_t EstimateCompressionBufferSize(size_t src_len) = 0;
238 
239  /// (De)compress file "src_file" and put result to file "dst_file".
240  /// Return TRUE on success, FALSE on error.
241  virtual bool CompressFile(
242  const string& src_file,
243  const string& dst_file,
244  size_t file_io_bufsize = kCompressionDefaultBufSize,
245  size_t compression_in_bufsize = kCompressionDefaultBufSize,
246  size_t compression_out_bufsize = kCompressionDefaultBufSize
247  ) = 0;
248  virtual bool DecompressFile(
249  const string& src_file,
250  const string& dst_file,
251  size_t file_io_bufsize = kCompressionDefaultBufSize,
252  size_t compression_in_bufsize = kCompressionDefaultBufSize,
253  size_t compression_out_bufsize = kCompressionDefaultBufSize
254  ) = 0;
255 
256  /// Set a dictionary for all compression/decompression operations (if supported).
257  virtual bool SetDictionary(
258  CCompressionDictionary& dict,
260  ) = 0;
261 };
262 
263 
264 //////////////////////////////////////////////////////////////////////////////
265 //
266 // CCompression -- abstract base class
267 //
268 
270 {
271 public:
272  // 'ctors
273  CCompression(ELevel level = eLevel_Default);
274  virtual ~CCompression(void);
275 
276  /// Return name and version of the compression library.
277  virtual CVersionInfo GetVersion(void) const = 0;
278 
279  /// Get/set compression level.
280  /// NOTE 1: Changing compression level after compression has begun will
281  /// be ignored.
282  /// NOTE 2: If the level is not supported by the underlying algorithm,
283  /// then it will be translated to the nearest supported value.
284  virtual void SetLevel(ELevel level);
285  virtual ELevel GetLevel(void) const;
286 
287  /// Get compressor's internal status/error code and description
288  /// for the last operation.
289  virtual int GetErrorCode(void) const;
290  virtual string GetErrorDescription(void) const;
291 
292  /// Get/set flags.
293  virtual TFlags GetFlags(void) const;
294  virtual void SetFlags(TFlags flags);
295 
296  /// Structure to get information about recommended buffer sizes for
297  /// file/stream I/O to tune up a (de)compression performance.
298  /// @sa GetRecommendedBufferSizes
300  size_t compression_in; ///< compression: recommended size for input buffer
301  size_t compression_out; ///< compression: recommended size for output buffer
302  size_t decompression_in; ///< decompression: recommended size for input buffer
303  size_t decompression_out; ///< decompression: recommended size for output buffer
304 
305  // Round up buffer size to a value divisible by 'precision'
306  size_t RoundUp(size_t value, size_t precision);
307  };
308 
309 protected:
310  /// Set last action error/status code and description
311  void SetError(int status, const char* description = 0);
312  void SetError(int status, const string& description);
313 
314  /// Universal file compression function.
315  /// Return TRUE on success, FALSE on error.
316  virtual bool x_CompressFile(
317  const string& src_file,
318  CCompressionFile& dst_file,
319  size_t file_io_bufsize = kCompressionDefaultBufSize
320  );
321  /// Universal file decompression function.
322  /// Return TRUE on success, FALSE on error.
323  virtual bool x_DecompressFile(
324  CCompressionFile& src_file,
325  const string& dst_file,
326  size_t file_io_bufsize = kCompressionDefaultBufSize
327  );
328 
329 protected:
330  /// Decompression mode (see fAllowTransparentRead flag).
332  eMode_Unknown, ///< Not known yet (decompress/transparent read)
333  eMode_Decompress, ///< Generic decompression
334  eMode_TransparentRead ///< Transparent read, the data is uncompressed
335  };
336  EDecompressMode m_DecompressMode; ///< Decompress mode (Decompress/TransparentRead/Unknown)
337 
338  // Dictionary
339  CCompressionDictionary* m_Dict; ///< Dictionary for compression/decompression
340  ENcbiOwnership m_DictOwn; ///< Dictionary ownership
341 
342 private:
343  ELevel m_Level; ///< Compression level
344  int m_ErrorCode; ///< Last compressor action error/status
345  string m_ErrorMsg; ///< Last compressor action error message
346  TFlags m_Flags; ///< Bitwise OR of flags
347 
348  // Friend classes
349  friend class CCompressionStreambuf;
350 };
351 
352 
353 
354 //////////////////////////////////////////////////////////////////////////////
355 //
356 // CCompressionFile -- abstract base class
357 //
358 
359 // Class for support work with compressed files.
360 // Assumed that file on hard disk is always compressed and data in memory
361 // is uncompressed.
362 //
363 
365 {
366 public:
367  /// Compression file handler
368  typedef void* TFile;
369 
370  /// File open mode
371  enum EMode {
372  eMode_Read, ///< Reading from compressed file
373  eMode_Write ///< Writing compressed data to file
374  };
375 
376  // 'ctors
377  CCompressionFile(void);
378  CCompressionFile(const string& path, EMode mode,
379  size_t compression_in_bufsize = 0,
380  size_t compression_out_bufsize = 0);
381  virtual ~CCompressionFile(void);
382 
383  /// Opens a compressed file for reading or writing.
384  /// Return NULL if error has been occurred.
385  virtual bool Open(const string& path, EMode mode,
386  size_t compression_in_bufsize = 0,
387  size_t compression_out_bufsize = 0) = 0;
388 
389  /// Read up to "len" uncompressed bytes from the compressed file "file"
390  /// into the buffer "buf". Return the number of bytes actually read
391  /// (0 for end of file, -1 for error)
392  virtual long Read(void* buf, size_t len) = 0;
393 
394  /// Writes the given number of uncompressed bytes into the compressed file.
395  /// Return the number of bytes actually written or -1 for error.
396  /// Returned value can be less than "len", especially if it exceed
397  /// numeric_limits<long>::max(), you should repeat writing for remaining portion.
398  virtual long Write(const void* buf, size_t len) = 0;
399 
400  /// Flushes all pending output if necessary, closes the compressed file.
401  /// Return TRUE on success, FALSE on error.
402  virtual bool Close(void) = 0;
403 
404 protected:
405  TFile m_File; ///< File handler.
406  EMode m_Mode; ///< File open mode.
407 };
408 
409 
410 
411 //////////////////////////////////////////////////////////////////////////////
412 //
413 // CCompressionDictionary -- class to load/keep/manage compression dictionary data
414 //
415 
417 {
418 public:
419  /// Use dictionary data from a memory buffer.
420  /// CCompressionDictionary can take an ownership of allocated memory,
421  /// so it can be automatically deallocated when not needed anymore.
422  CCompressionDictionary(const void* buf, size_t size, ENcbiOwnership own = eNoOwnership);
423 
424  /// Load a dictionary from file.
425  /// Throw an CCompressionException on file opening error.
426  CCompressionDictionary(const string& filename);
427 
428  /// Load a dictionary up to 'size' bytes from a stream 'is'.
429  /// Ideally, 'size' should be a real dictionary data size.
430  /// You can use bigger sizes, but it will be less memory efficient.
431  CCompressionDictionary(istream& is, size_t size);
432 
433  /// Destructor.
434  virtual ~CCompressionDictionary(void);
435 
436  /// Return pointer to the dictionary data.
437  const void* GetData(void) { return m_Data; };
438 
439  /// Return dictionary data size.
440  size_t GetSize(void) { return m_Size; };
441 
442  /// Free used memory
443  void Free(void);
444 
445 protected:
446  size_t LoadFromStream(istream& is, size_t size);
447 
448 private:
449  const void* m_Data; ///< Pointer to the dictionary data
450  size_t m_Size; ///< Size of the data
451  ENcbiOwnership m_Own; ///< Data ownership
452 };
453 
454 
455 
456 //////////////////////////////////////////////////////////////////////////////
457 //
458 // CCompressionProcessor -- abstract base class
459 //
460 // Contains a functions for service a compression/decompression session.
461 //
462 
464 {
465 public:
466  /// Type of the result of all basic functions
467  enum EStatus {
468  /// Everything is fine, no errors occurred
470  /// Special case of eStatus_Success.
471  /// Logical end of (compressed) stream is detected, no errors occurred.
472  /// All subsequent inquiries about data processing should be ignored.
474  /// Error has occurred. The error code can be acquired by GetErrorCode().
476  /// Output buffer overflow - not enough output space.
477  /// Buffer must be emptied and the last action repeated.
479  /// Special value. Just need to repeat last action.
481  /// Special value. Status is undefined.
483  };
484 
485  // 'ctors
486  CCompressionProcessor(void);
487  virtual ~CCompressionProcessor(void);
488 
489  /// Return compressor's busy flag. If returns value is true that
490  /// the current compression object already have being use in other
491  /// compression session.
492  bool IsBusy(void) const;
493 
494  /// Return TRUE if fAllowEmptyData flag is set for this compression.
495  /// @note
496  /// Used by stream buffer, that don't have access to specific
497  /// compression implementation flags. So this method should be
498  /// implemented in each processor.
499  virtual bool AllowEmptyData() const = 0;
500 
501  // Return number of processed/output bytes.
502  size_t GetProcessedSize(void);
503  size_t GetOutputSize(void);
504 
505 protected:
506  /// Initialize the internal stream state for compression/decompression.
507  /// It does not perform any compression, this will be done by Process().
508  virtual EStatus Init(void) = 0;
509 
510  /// Compress/decompress as much data as possible, and stops when the input
511  /// buffer becomes empty or the output buffer becomes full. It may
512  /// introduce some output latency (reading input without producing any
513  /// output).
514  virtual EStatus Process
515  (const char* in_buf, // [in] input buffer
516  size_t in_len, // [in] input data length
517  char* out_buf, // [in] output buffer
518  size_t out_size, // [in] output buffer size
519  size_t* in_avail, // [out] count unproc.bytes in input buffer
520  size_t* out_avail // [out] count bytes putted into out buffer
521  ) = 0;
522 
523  /// Flush compressed/decompressed data from the output buffer.
524  /// Flushing may degrade compression for some compression algorithms
525  /// and so it should be used only when necessary.
526  virtual EStatus Flush
527  (char* out_buf, // [in] output buffer
528  size_t out_size, // [in] output buffer size
529  size_t* out_avail // [out] count bytes putted into out buffer
530  ) = 0;
531 
532  /// Finish the compression/decompression process.
533  /// Process pending input, flush pending output.
534  /// This function slightly like to Flush(), but it must be called only
535  /// at the end of compression process just before End().
536  virtual EStatus Finish
537  (char* out_buf, // [in] output buffer
538  size_t out_size, // [in] output buffer size
539  size_t* out_avail // [out] count bytes putted into out buffer
540  ) = 0;
541 
542  /// Free all dynamically allocated data structures.
543  /// This function discards any unprocessed input and does not flush
544  /// any pending output.
545  /// @param abandon
546  /// If this parameter is not zero that skip all error checks,
547  /// always return eStatus_Success. Use it if Process/Flush/Finish where
548  /// not called to perform any compression/decompression after Init().
549  virtual EStatus End(int abandon = 0) = 0;
550 
551 protected:
552  /// Reset internal state
553  void Reset(void);
554 
555  /// Set/unset compressor busy flag
556  void SetBusy(bool busy = true);
557 
558  // Increase number of processed/output bytes.
559  void IncreaseProcessedSize(size_t n_bytes);
560  void IncreaseOutputSize(size_t n_bytes);
561 
562 private:
563  size_t m_ProcessedSize; //< The number of processed bytes
564  size_t m_OutputSize; //< The number of output bytes
565  bool m_Busy; //< TRUE if compressor is busy and not ready to begin next session
566  // Friend classes
567  friend class CCompressionStream;
568  friend class CCompressionStreambuf;
570 };
571 
572 
573 /////////////////////////////////////////////////////////////////////////////
574 //
575 // CCompressionException
576 //
577 // Exceptions generated by CCompresson and derived classes
578 //
579 
581 {
582 public:
583  enum EErrCode {
584  eCompression, ///< Compression/decompression error
585  eCompressionFile ///< Compression/decompression file error
586  };
587  virtual const char* GetErrCodeString(void) const override
588  {
589  switch (GetErrCode()) {
590  case eCompression : return "eCompression";
591  case eCompressionFile : return "eCompressionFile";
592  default : return CException::GetErrCodeString();
593  }
594  }
596 };
597 
598 
599 /////////////////////////////////////////////////////////////////////////////
600 //
601 // CCompressionUtil
602 //
603 // Utility functions
604 //
605 
607 {
608 public:
609  /// Store 4 bytes of value in the buffer.
610  static void StoreUI4(void* buf, unsigned long value);
611 
612  /// Read 4 bytes from buffer.
613  static Uint4 GetUI4(const void* buf);
614 
615  /// Store 2 bytes of value in the buffer.
616  static void StoreUI2(void* buf, unsigned long value);
617 
618  /// Read 2 bytes from buffer.
619  static Uint2 GetUI2(const void* buf);
620 };
621 
622 
623 //////////////////////////////////////////////////////////////////////////////
624 //
625 // IChunkHandler -- abstract interface class
626 //
627 
628 /// Interface class to scan data source for seekable data chunks.
629 ///
631 {
632 public:
633  typedef Uint8 TPosition; ///< Type to store stream positions
634 
635  /// Action types
636  enum EAction {
637  eAction_Continue, ///< Continue scanning to the next data chunk, if any.
638  eAction_Stop ///< Stop scanning.
639  };
640 
641  /// Destructor.
642  virtual ~IChunkHandler(void) {}
643 
644  /// Callback method, to be implemented by the end user.
645  /// @param raw_pos
646  /// Position of the chunk in the "raw" (undecoded) stream.
647  /// @param data_pos
648  /// Position of the chunk in the decoded stream data.
649  /// @return
650  /// Return a command for the scanning algorithm to continue or stop scanning.
651  virtual EAction OnChunk(TPosition raw_pos, TPosition data_pos) = 0;
652 };
653 
654 
655 /* @} */
656 
657 
658 //===========================================================================
659 //
660 // Inline
661 //
662 //===========================================================================
663 
664 inline
666 {
667  if ( precision <= 1 ) {
668  return value;
669  }
670  return size_t(value / precision) * precision + ((value % precision > 0) ? precision : 0);
671 }
672 
673 inline
675 {
676  m_ProcessedSize = 0;
677  m_OutputSize = 0;
678  m_Busy = false;
679 }
680 
681 inline
683 {
684  return m_Busy;
685 }
686 
687 inline
689 {
690  if ( busy && m_Busy ) {
691  NCBI_THROW(CCompressionException, eCompression,
692  "CCompression::SetBusy(): The compressor is busy now");
693  }
694  m_Busy = busy;
695 }
696 
697 inline
699 {
700  m_ProcessedSize += n_bytes;
701 }
702 
703 inline
705 {
706  m_OutputSize += n_bytes;
707 }
708 
709 inline
711 {
712  return m_ProcessedSize;
713 }
714 
715 inline
717 {
718  return m_OutputSize;
719 }
720 
721 
722 
724 
725 
726 #endif /* UTIL_COMPRESS__COMPRESS__HPP */
@ eStatus_Unknown
CCoreException –.
Definition: ncbiexpt.hpp:1476
CVersionInfo –.
Interface class to scan data source for seekable data chunks.
Definition: compress.hpp:631
char value[7]
Definition: config.c:431
Include a standard set of the NCBI C++ Toolkit most basic headers.
static uch flags
static char precision
Definition: genparams.c:28
@ eNoOwnership
No ownership is assumed.
Definition: ncbi_types.h:135
#define Free
Definition: ncbimem.hpp:122
virtual ~IChunkHandler(void)
Destructor.
Definition: compress.hpp:642
virtual bool HaveSupport(ESupportFeature feature)=0
Check if compression have support for a specified feature.
void IncreaseProcessedSize(size_t n_bytes)
Definition: compress.hpp:698
virtual ~ICompression(void)
Destructor.
Definition: compress.hpp:164
CCompressionFile(const string &path, EMode mode, size_t compression_in_bufsize=0, size_t compression_out_bufsize=0)
void IncreaseOutputSize(size_t n_bytes)
Definition: compress.hpp:704
ENcbiOwnership m_DictOwn
Dictionary ownership.
Definition: compress.hpp:340
TFile m_File
File handler.
Definition: compress.hpp:405
virtual const char * GetErrCodeString(void) const override
Translate from the error code value to its string representation.
Definition: compress.hpp:587
void Reset(void)
Reset internal state.
Definition: compress.hpp:674
ELevel m_Level
Compression level.
Definition: compress.hpp:343
size_t GetOutputSize(void)
Definition: compress.hpp:716
TFlags m_Flags
Bitwise OR of flags.
Definition: compress.hpp:346
virtual EStatus Finish(char *out_buf, size_t out_size, size_t *out_avail)=0
Finish the compression/decompression process.
void SetBusy(bool busy=true)
Set/unset compressor busy flag.
Definition: compress.hpp:688
size_t compression_out
compression: recommended size for output buffer
Definition: compress.hpp:301
virtual long Write(const void *buf, size_t len)=0
Writes the given number of uncompressed bytes into the compressed file.
bool IsBusy(void) const
Return compressor's busy flag.
Definition: compress.hpp:682
virtual void SetFlags(TFlags flags)=0
virtual EStatus Process(const char *in_buf, size_t in_len, char *out_buf, size_t out_size, size_t *in_avail, size_t *out_avail)=0
Compress/decompress as much data as possible, and stops when the input buffer becomes empty or the ou...
virtual CVersionInfo GetVersion(void) const =0
Return name and version of the compression library.
size_t RoundUp(size_t value, size_t precision)
Definition: compress.hpp:665
EAction
Action types.
Definition: compress.hpp:636
ELevel
Compression level.
Definition: compress.hpp:142
void * TFile
Compression file handler.
Definition: compress.hpp:368
virtual long Read(void *buf, size_t len)=0
Read up to "len" uncompressed bytes from the compressed file "file" into the buffer "buf".
virtual bool SetDictionary(CCompressionDictionary &dict, ENcbiOwnership own=eNoOwnership)=0
Set a dictionary for all compression/decompression operations (if supported).
NCBI_EXCEPTION_DEFAULT(CCompressionException, CCoreException)
string m_ErrorMsg
Last compressor action error message.
Definition: compress.hpp:345
virtual EAction OnChunk(TPosition raw_pos, TPosition data_pos)=0
Callback method, to be implemented by the end user.
virtual TFlags GetFlags(void) const =0
const streamsize kCompressionDefaultBufSize
Default compression I/O stream buffer size.
Definition: compress.hpp:111
virtual bool Close(void)=0
Flushes all pending output if necessary, closes the compressed file.
virtual int GetErrorCode(void) const =0
virtual bool Open(const string &path, EMode mode, size_t compression_in_bufsize=0, size_t compression_out_bufsize=0)=0
Opens a compressed file for reading or writing.
virtual CVersionInfo GetVersion(void) const =0
Return name and version of the compression library.
EDecompressMode m_DecompressMode
Decompress mode (Decompress/TransparentRead/Unknown)
Definition: compress.hpp:336
size_t decompression_out
decompression: recommended size for output buffer
Definition: compress.hpp:303
CCompressionDictionary * m_Dict
Dictionary for compression/decompression.
Definition: compress.hpp:339
virtual bool CompressBuffer(const void *src_buf, size_t src_len, void *dst_buf, size_t dst_size, size_t *dst_len)=0
(De)compress the source buffer into the destination buffer.
virtual EStatus Flush(char *out_buf, size_t out_size, size_t *out_avail)=0
Flush compressed/decompressed data from the output buffer.
size_t decompression_in
decompression: recommended size for input buffer
Definition: compress.hpp:302
const void * m_Data
Pointer to the dictionary data.
Definition: compress.hpp:449
virtual bool DecompressFile(const string &src_file, const string &dst_file, size_t file_io_bufsize=kCompressionDefaultBufSize, size_t compression_in_bufsize=kCompressionDefaultBufSize, size_t compression_out_bufsize=kCompressionDefaultBufSize)=0
int m_ErrorCode
Last compressor action error/status.
Definition: compress.hpp:344
virtual bool DecompressBuffer(const void *src_buf, size_t src_len, void *dst_buf, size_t dst_size, size_t *dst_len)=0
unsigned int TFlags
Compression flags.
Definition: compress.hpp:160
virtual void SetLevel(ELevel level)=0
virtual bool AllowEmptyData() const =0
Return TRUE if fAllowEmptyData flag is set for this compression.
virtual ELevel GetDefaultLevel(void) const =0
Return the default compression level for current compression algorithm.
EDecompressMode
Decompression mode (see fAllowTransparentRead flag).
Definition: compress.hpp:331
const void * GetData(void)
Return pointer to the dictionary data.
Definition: compress.hpp:437
size_t GetSize(void)
Return dictionary data size.
Definition: compress.hpp:440
size_t m_Size
Size of the data.
Definition: compress.hpp:450
size_t compression_in
compression: recommended size for input buffer
Definition: compress.hpp:300
EStatus
Type of the result of all basic functions.
Definition: compress.hpp:467
virtual string GetErrorDescription(void) const =0
ENcbiOwnership m_Own
Data ownership.
Definition: compress.hpp:451
virtual size_t EstimateCompressionBufferSize(size_t src_len)=0
Estimate buffer size for data compression (if supported).
ESupportFeature
Supported features.
Definition: compress.hpp:191
virtual ELevel GetLevel(void) const =0
size_t GetProcessedSize(void)
Definition: compress.hpp:710
Uint8 TPosition
Type to store stream positions.
Definition: compress.hpp:633
virtual bool CompressFile(const string &src_file, const string &dst_file, size_t file_io_bufsize=kCompressionDefaultBufSize, size_t compression_in_bufsize=kCompressionDefaultBufSize, size_t compression_out_bufsize=kCompressionDefaultBufSize)=0
(De)compress file "src_file" and put result to file "dst_file".
EMode
File open mode.
Definition: compress.hpp:371
EMode m_Mode
File open mode.
Definition: compress.hpp:406
virtual EStatus End(int abandon=0)=0
Free all dynamically allocated data structures.
virtual EStatus Init(void)=0
Initialize the internal stream state for compression/decompression.
@ eAction_Continue
Continue scanning to the next data chunk, if any.
Definition: compress.hpp:637
@ eMode_Decompress
Generic decompression.
Definition: compress.hpp:333
@ eMode_Unknown
Not known yet (decompress/transparent read)
Definition: compress.hpp:332
@ eStatus_Overflow
Output buffer overflow - not enough output space.
Definition: compress.hpp:478
@ eStatus_Error
Error has occurred. The error code can be acquired by GetErrorCode().
Definition: compress.hpp:475
@ eStatus_EndOfData
Special case of eStatus_Success.
Definition: compress.hpp:473
@ eStatus_Success
Everything is fine, no errors occurred.
Definition: compress.hpp:469
@ eStatus_Repeat
Special value. Just need to repeat last action.
Definition: compress.hpp:480
@ eCompression
Compression/decompression error.
Definition: compress.hpp:584
@ eFeature_Dictionary
Check if current compression have support for dictionaries.
Definition: compress.hpp:202
@ eFeature_NoCompression
Check if current compression method have a real support for eLevel_NoCompression, so it can store dat...
Definition: compress.hpp:197
@ eFeature_EstimateCompressionBufferSize
Check if current compression have implementation for the EstimateCompressionBufferSize() method.
Definition: compress.hpp:208
@ eMode_Read
Reading from compressed file.
Definition: compress.hpp:372
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
EErrCode
Error types that an application can generate.
Definition: ncbiexpt.hpp:884
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
Definition: ncbiexpt.cpp:444
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
uint16_t Uint2
2-byte (16-bit) unsigned integer
Definition: ncbitype.h:101
uint64_t Uint8
8-byte (64-bit) unsigned integer
Definition: ncbitype.h:105
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
ENcbiOwnership
Ownership relations between objects.
Definition: ncbi_types.h:134
char * buf
int len
mdb_mode_t mode
Definition: lmdb++.h:38
const struct ncbi::grid::netcache::search::fields::SIZE size
NCBI_XUTIL_EXPORT
Parameter to control printing diagnostic message about conversion of static array data from a differe...
Definition: static_set.hpp:72
Structure to get information about recommended buffer sizes for file/stream I/O to tune up a (de)comp...
Definition: compress.hpp:299
Modified on Fri Mar 01 10:07:07 2024 by modify_doxy.py rev. 669887