Fabcoin Core  0.16.2
P2P Digital Currency
zinflate.h
Go to the documentation of this file.
1 #ifndef CRYPTOPP_ZINFLATE_H
2 #define CRYPTOPP_ZINFLATE_H
3 
4 #include "cryptlib.h"
5 #include "secblock.h"
6 #include "filters.h"
7 #include "stdcpp.h"
8 
10 
14 {
15 public:
17  : m_store(store), m_buffer(0), m_bitsBuffered(0) {}
18  unsigned int BitsBuffered() const {return m_bitsBuffered;}
19  unsigned long PeekBuffer() const {return m_buffer;}
20  bool FillBuffer(unsigned int length);
21  unsigned long PeekBits(unsigned int length);
22  void SkipBits(unsigned int length);
23  unsigned long GetBits(unsigned int length);
24 
25 private:
27  unsigned long m_buffer;
28  unsigned int m_bitsBuffered;
29 };
30 
31 struct CodeLessThan;
32 
37 {
38 public:
39  typedef unsigned int code_t;
40  typedef unsigned int value_t;
41  enum {MAX_CODE_BITS = sizeof(code_t)*8};
42 
43  class Err : public Exception {public: Err(const std::string &what) : Exception(INVALID_DATA_FORMAT, "HuffmanDecoder: " + what) {}};
44 
46  HuffmanDecoder(const unsigned int *codeBitLengths, unsigned int nCodes)
48  {Initialize(codeBitLengths, nCodes);}
49 
50  void Initialize(const unsigned int *codeBitLengths, unsigned int nCodes);
51  unsigned int Decode(code_t code, /* out */ value_t &value) const;
52  bool Decode(LowFirstBitReader &reader, value_t &value) const;
53 
54 private:
55  friend struct CodeLessThan;
56 
57  struct CodeInfo
58  {
59  CodeInfo(code_t code=0, unsigned int len=0, value_t value=0) : code(code), len(len), value(value) {}
60  inline bool operator<(const CodeInfo &rhs) const {return code < rhs.code;}
61  code_t code;
62  unsigned int len;
63  value_t value;
64  };
65 
66  struct LookupEntry
67  {
68  unsigned int type;
69  union
70  {
71  value_t value;
72  const CodeInfo *begin;
73  };
74  union
75  {
76  unsigned int len;
77  const CodeInfo *end;
78  };
79  };
80 
81  static code_t NormalizeCode(code_t code, unsigned int codeBits);
82  void FillCacheEntry(LookupEntry &entry, code_t normalizedCode) const;
83 
85  std::vector<CodeInfo, AllocatorWithCleanup<CodeInfo> > m_codeToValue;
86  mutable std::vector<LookupEntry, AllocatorWithCleanup<LookupEntry> > m_cache;
87 };
88 
92 class Inflator : public AutoSignaling<Filter>
93 {
94 public:
95  class Err : public Exception
96  {
97  public:
98  Err(ErrorType e, const std::string &s)
99  : Exception(e, s) {}
100  };
101  class UnexpectedEndErr : public Err {public: UnexpectedEndErr() : Err(INVALID_DATA_FORMAT, "Inflator: unexpected end of compressed block") {}};
102  class BadBlockErr : public Err {public: BadBlockErr() : Err(INVALID_DATA_FORMAT, "Inflator: error in compressed block") {}};
103 
108  Inflator(BufferedTransformation *attachment = NULL, bool repeat = false, int autoSignalPropagation = -1);
109 
110  void IsolatedInitialize(const NameValuePairs &parameters);
111  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
112  bool IsolatedFlush(bool hardFlush, bool blocking);
113 
114  virtual unsigned int GetLog2WindowSize() const {return 15;}
115 
116 protected:
118 
119 private:
120  virtual unsigned int MaxPrestreamHeaderSize() const {return 0;}
121  virtual void ProcessPrestreamHeader() {}
122  virtual void ProcessDecompressedData(const byte *string, size_t length)
123  {AttachedTransformation()->Put(string, length);}
124  virtual unsigned int MaxPoststreamTailSize() const {return 0;}
125  virtual void ProcessPoststreamTail() {}
126 
127  void ProcessInput(bool flush);
128  void DecodeHeader();
129  bool DecodeBody();
130  void FlushOutput();
131  void OutputByte(byte b);
132  void OutputString(const byte *string, size_t length);
133  void OutputPast(unsigned int length, unsigned int distance);
134 
135  static const HuffmanDecoder *FixedLiteralDecoder();
136  static const HuffmanDecoder *FixedDistanceDecoder();
137 
138  const HuffmanDecoder& GetLiteralDecoder() const;
139  const HuffmanDecoder& GetDistanceDecoder() const;
140 
141  enum State {PRE_STREAM, WAIT_HEADER, DECODING_BODY, POST_STREAM, AFTER_END};
143  bool m_repeat, m_eof, m_wrappedAround;
146  enum NextDecode {LITERAL, LENGTH_BITS, DISTANCE, DISTANCE_BITS};
148  unsigned int m_literal, m_distance; // for LENGTH_BITS or DISTANCE_BITS
149  HuffmanDecoder m_dynamicLiteralDecoder, m_dynamicDistanceDecoder;
152  size_t m_current, m_lastFlush;
153 };
154 
156 
157 #endif
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:140
unsigned int m_bitsBuffered
Definition: zinflate.h:28
virtual void ProcessDecompressedData(const byte *string, size_t length)
Definition: zinflate.h:122
unsigned int type
Definition: zinflate.h:68
const char * what() const
Retrieves a C-string describing the exception.
Definition: cryptlib.h:168
ErrorType
Error types or categories.
Definition: cryptlib.h:145
uint8_t byte
Definition: Common.h:57
unsigned int m_cacheMask
Definition: zinflate.h:84
unsigned short word16
Definition: config.h:230
Err(const std::string &what)
Definition: zinflate.h:43
bool m_wrappedAround
Definition: zinflate.h:143
ByteQueue m_inQueue
Definition: zinflate.h:117
Exception(ErrorType errorType, const std::string &s)
Construct a new Exception.
Definition: cryptlib.h:165
std::vector< LookupEntry, AllocatorWithCleanup< LookupEntry > > m_cache
Definition: zinflate.h:86
size_t m_lastFlush
Definition: zinflate.h:152
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
Abstract base classes that provide a uniform interface to this library.
virtual unsigned int GetLog2WindowSize() const
Definition: zinflate.h:114
virtual void ProcessPrestreamHeader()
Definition: zinflate.h:121
static code_t NormalizeCode(code_t code, unsigned int codeBits)
Definition: zinflate.cpp:60
unsigned long m_buffer
Definition: zinflate.h:27
virtual void ProcessPoststreamTail()
Definition: zinflate.h:125
Interface for buffered transformations.
Definition: cryptlib.h:1352
bytes code
Definition: SmartVM.cpp:45
bool operator<(const CodeInfo &rhs) const
Definition: zinflate.h:60
State m_state
Definition: zinflate.h:142
Definition: zinflate.h:66
unsigned int m_maxCodeBits
Definition: zinflate.h:84
unsigned int code_t
Definition: zinflate.h:39
Classes and functions for secure memory allocations.
const CodeInfo * end
Definition: zinflate.h:77
Input data was received that did not conform to expected format.
Definition: cryptlib.h:155
virtual unsigned int MaxPrestreamHeaderSize() const
Definition: zinflate.h:120
HuffmanDecoder m_dynamicLiteralDecoder
Definition: zinflate.h:149
void Initialize(const unsigned int *codeBitLengths, unsigned int nCodes)
Definition: zinflate.cpp:65
byte m_blockType
Definition: zinflate.h:144
const CodeInfo * begin
Definition: zinflate.h:72
unsigned int m_normalizedCacheMask
Definition: zinflate.h:84
unsigned int m_literal
Definition: zinflate.h:148
Huffman Decoder.
Definition: zinflate.h:36
CodeInfo(code_t code=0, unsigned int len=0, value_t value=0)
Definition: zinflate.h:59
#define b(i, j)
HuffmanDecoder(const unsigned int *codeBitLengths, unsigned int nCodes)
Definition: zinflate.h:46
Data structure used to store byte strings.
Definition: queue.h:20
unsigned int value_t
Definition: zinflate.h:40
std::vector< CodeInfo, AllocatorWithCleanup< CodeInfo > > m_codeToValue
Definition: zinflate.h:85
void FillCacheEntry(LookupEntry &entry, code_t normalizedCode) const
Definition: zinflate.cpp:155
Implementation of BufferedTransformation&#39;s attachment interface.
unsigned int BitsBuffered() const
Definition: zinflate.h:18
DEFLATE decompressor (RFC 1951)
Definition: zinflate.h:92
Provides auto signaling support.
Definition: simple.h:280
word16 m_storedLen
Definition: zinflate.h:145
LowFirstBitReader(BufferedTransformation &store)
Definition: zinflate.h:16
#define NAMESPACE_END
Definition: config.h:201
std::vector< char * > parameters
Definition: boostTest.cpp:46
#define e(i)
Definition: sha.cpp:733
unsigned long PeekBuffer() const
Definition: zinflate.h:19
unsigned int len
Definition: zinflate.h:76
Err(ErrorType e, const std::string &s)
Definition: zinflate.h:98
unsigned int m_cacheBits
Definition: zinflate.h:84
BufferedTransformation & m_store
Definition: zinflate.h:26
virtual unsigned int MaxPoststreamTailSize() const
Definition: zinflate.h:124
LowFirstBitReader m_reader
Definition: zinflate.h:150
unsigned int len
Definition: zinflate.h:62
NextDecode m_nextDecode
Definition: zinflate.h:147
SecByteBlock m_window
Definition: zinflate.h:151
value_t value
Definition: zinflate.h:71
unsigned int Decode(code_t code, value_t &value) const
Definition: zinflate.cpp:182
Interface for retrieving values given their names.
Definition: cryptlib.h:279