jlzw.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef JLZW_INCL
  14. #define JLZW_INCL
  15. #include "jiface.hpp"
  16. #include "jfile.hpp"
  17. #include <stdio.h>
  18. interface jlib_decl ICompressor : public IInterface
  19. {
  20. virtual void open(MemoryBuffer &mb, size32_t initialSize=0)=0; // variable internally sized buffer
  21. virtual void open(void *blk, size32_t blksize)=0; // fixed size output
  22. virtual void close()=0;
  23. virtual size32_t write(const void *buf,size32_t len)=0;
  24. virtual void * bufptr()=0;
  25. virtual size32_t buflen()=0;
  26. virtual void startblock()=0; // row based must call startblock/commitblock
  27. virtual void commitblock()=0;
  28. };
  29. interface jlib_decl IExpander : public IInterface
  30. {
  31. virtual size32_t init(const void *blk)=0; // returns size required
  32. virtual void expand(void *target)=0;
  33. virtual void * bufptr()=0;
  34. virtual size32_t buflen()=0;
  35. };
  36. interface jlib_decl IRandRowExpander : public IInterface
  37. {
  38. static inline bool isRand(const void *blk) { return *((const unsigned short *)blk+1)==0xffff; }
  39. virtual bool init(const void *blk,bool copy=true)=0;
  40. virtual size32_t rowSize() const = 0;
  41. virtual unsigned numRows() const = 0;
  42. virtual bool expandRow(void *target,unsigned index) const =0;
  43. virtual size32_t expandRow(void *target,unsigned index,size32_t ofs,size32_t sz) const =0;
  44. virtual const byte *firstRow() const = 0;
  45. virtual int cmpRow(const void *target,unsigned index,size32_t ofs=0,size32_t sz=(size32_t)-1) const =0;
  46. };
  47. extern jlib_decl ICompressor *createLZWCompressor(bool supportbigendian=false); // bigendiansupport required for cross platform with solaris
  48. extern jlib_decl IExpander *createLZWExpander(bool supportbigendian=false);
  49. #define RLEMAXOVERHEAD 2
  50. extern jlib_decl size32_t RLECompress(void *dst,const void *src,size32_t size); // maximum will write is 2+size
  51. extern jlib_decl size32_t RLEExpand(void *dst,const void *src,size32_t expsize); // returns amount read, expsize must be provided
  52. extern jlib_decl size32_t DiffCompressFirst(const void *src,void *dst,void *buff,size32_t rs); // compress first row (actually make bigger, but in same format as compression)
  53. // buf need not be initialized
  54. extern jlib_decl size32_t DiffCompress(const void *src,void *dst,void *buff,size32_t rs); // compress subsequent rows (bufs set by previous DiffFirstCompress or DiffCompress
  55. extern jlib_decl size32_t DiffCompress2(const void *src,void *dst,const void *prev,size32_t rs);// compress row (prev not updated)
  56. extern jlib_decl size32_t DiffExpand(const void *src,void *dst,const void *prev,size32_t rs); // expand row, prev must be passed previous expanded row
  57. extern jlib_decl size32_t DiffCompressedSize(const void *cmpressedsrc,size32_t rs); // calculate compressed row size - rs is expanded size
  58. inline size32_t MaxDiffCompressedRowSize (size32_t rowsize) { return rowsize+((rowsize+254)/255)*2; }
  59. extern jlib_decl ICompressor *createRDiffCompressor(); // NB only supports transaction mode one row per transaction and fixed row size
  60. extern jlib_decl IExpander *createRDiffExpander(); // NB only supports transaction mode one row per transaction and fixed row size
  61. extern jlib_decl ICompressor *createRandRDiffCompressor(); // similar to RDiffCompressor except rows can be expanded individually
  62. extern jlib_decl IRandRowExpander *createRandRDiffExpander(); // NB only supports fixed row size
  63. //Some helper functions to make it easy to compress/decompress to memorybuffers.
  64. extern jlib_decl void compressToBuffer(MemoryBuffer & out, size32_t len, const void * src);
  65. extern jlib_decl void decompressToBuffer(MemoryBuffer & out, const void * src);
  66. extern jlib_decl void decompressToBuffer(MemoryBuffer & out, MemoryBuffer & in);
  67. extern jlib_decl void decompressToAttr(MemoryAttr & out, const void * src);
  68. extern jlib_decl void decompressToBuffer(MemoryAttr & out, MemoryBuffer & in);
  69. extern jlib_decl void appendToBuffer(MemoryBuffer & out, size32_t len, const void * src); //format as failed compression
  70. #define COMPRESS_METHOD_ROWDIF 1
  71. #define COMPRESS_METHOD_LZW 2
  72. #define COMPRESS_METHOD_FASTLZ 3
  73. #define COMPRESS_METHOD_LZMA 4
  74. #define COMPRESS_METHOD_LZ4 5
  75. interface ICompressedFileIO: extends IFileIO
  76. {
  77. virtual unsigned dataCRC()=0; // CRC for data area (note total file CRC equals COMPRESSEDFILECRC)
  78. virtual size32_t recordSize()=0; // 0 for lzw/fastlz, otherwise record length for row difference compression
  79. virtual size32_t blockSize()=0; // block size used
  80. virtual void setBlockSize(size32_t size)=0; // only callable before any writes
  81. virtual bool readMode()=0; // true if created using createCompressedFileReader
  82. virtual unsigned method()=0;
  83. };
  84. extern jlib_decl bool isCompressedFile(const char *filename);
  85. extern jlib_decl bool isCompressedFile(IFile *file);
  86. extern jlib_decl ICompressedFileIO *createCompressedFileReader(IFile *file,IExpander *expander=NULL, bool memorymapped=false, IFEflags extraFlags=IFEnone);
  87. extern jlib_decl ICompressedFileIO *createCompressedFileReader(IFileIO *fileio,IExpander *expander=NULL);
  88. extern jlib_decl ICompressedFileIO *createCompressedFileWriter(IFile *file,size32_t recordsize,bool append=false,bool setcrc=true,ICompressor *compressor=NULL, unsigned compMethod=COMPRESS_METHOD_LZW, IFEflags extraFlags=IFEnone);
  89. extern jlib_decl ICompressedFileIO *createCompressedFileWriter(IFileIO *fileio,size32_t recordsize,bool setcrc=true,ICompressor *compressor=NULL, unsigned compMethod=COMPRESS_METHOD_LZW);
  90. #define COMPRESSEDFILECRC (~0U)
  91. extern jlib_decl ICompressor *createAESCompressor(const void *key, unsigned keylen=0); // keylen>0 must be 16, 24, or 32 Bytes, if keylen=0 key is padded to 32bytes
  92. extern jlib_decl IExpander *createAESExpander(const void *key, unsigned keylen=0); // keylen>0 must be 16, 24, or 32 Bytes, if keylen=0 key is padded to 32bytes
  93. extern jlib_decl ICompressor *createAESCompressor256(size32_t len, const void *key); // 256bit key
  94. extern jlib_decl IExpander *createAESExpander256(size32_t len, const void *key); // key is ascii and is padded
  95. interface IPropertyTree;
  96. extern jlib_decl IPropertyTree *getBlockedFileDetails(IFile *file);
  97. interface ICompressHandler : extends IInterface
  98. {
  99. virtual const char *queryType() const = 0;
  100. virtual ICompressor *getCompressor(const char *options=NULL) = 0;
  101. virtual IExpander *getExpander(const char *options=NULL) = 0;
  102. };
  103. extern jlib_decl void setDefaultCompressor(const char *type);
  104. extern jlib_decl ICompressHandler *queryCompressHandler(const char *type);
  105. extern jlib_decl ICompressHandler *queryDefaultCompressHandler();
  106. extern jlib_decl bool addCompressorHandler(ICompressHandler *handler); // returns true if added, false if already registered
  107. extern jlib_decl bool removeCompressorHandler(ICompressHandler *handler); // returns true if present and removed
  108. extern jlib_decl ICompressor *getCompressor(const char *type, const char *options=NULL);
  109. extern jlib_decl IExpander *getExpander(const char *type, const char *options=NULL);
  110. #define MIN_ROWCOMPRESS_RECSIZE 8
  111. #endif