libUTL++
Encoder.h
1 #pragma once
2 
4 
5 #include <libutl/CRC32.h>
6 #include <libutl/BufferedStream.h>
7 
9 
10 UTL_NS_BEGIN;
11 
13 
37 
39 class Encoder : public BufferedStream
40 {
43 
44 public:
51  Encoder(uint_t mode, Stream* stream, bool owner = true)
52  {
53  set(mode, stream, owner);
54  }
55 
57 
58  virtual void close();
60 
62 
63 
64  uint32_t
65  getCRC() const
66  {
67  return isCRC() ? _crc.get() : _lastCRC;
68  }
69 
71  uint32_t
72  getLastCRC() const
73  {
74  return _lastCRC;
75  }
76 
78  bool
79  isCRC() const
80  {
81  return getFlag(flg_crc);
82  }
83 
85  void
86  setCRC(bool crc)
87  {
88  setFlag(flg_crc, crc);
89  }
91 
98  virtual size_t decode(byte_t* block, size_t num) = 0;
99 
106  virtual size_t encode(const byte_t* block, size_t num) = 0;
107 
115  void set(uint_t mode, Stream* stream, bool owner = true, size_t bufSize = KB(16));
116 
117 protected:
118  virtual void clear();
119 
121 
122 
123  bool
124  isLastBlock() const
125  {
126  return getFlag(flg_lastBlock);
127  }
128 
130  void
131  setLastBlock(bool lastBlock)
132  {
133  setFlag(flg_lastBlock, lastBlock);
134  }
136 
138  virtual void overflow();
139 
141  virtual void underflow();
142 
144  virtual void finishEncoding();
145 
147  virtual size_t finishDecoding();
148 
149 private:
150  void
151  init()
152  {
153  _lastCRC = 0;
154  }
155  void
156  deInit()
157  {
158  }
159 
160 private:
161  enum flg_t
162  {
163  flg_lastBlock = 8,
164  flg_crc
165  };
166 
167 private:
168  CRC32 _crc;
169  uint32_t _lastCRC;
170 };
171 
173 
174 UTL_NS_END;
void deInit()
De-initialize UTL++.
Encoder/decoder abstraction.
Definition: Encoder.h:39
uint32_t getCRC() const
Get the CRC code.
Definition: Encoder.h:65
uint32_t getLastCRC() const
Get the last CRC code.
Definition: Encoder.h:72
unsigned char byte_t
Unsigned character.
Definition: types.h:31
void setLastBlock(bool lastBlock)
Set the lastBlock flag.
Definition: Encoder.h:131
unsigned int uint32_t
Unsigned 32-bit integer.
Definition: types.h:115
bool isLastBlock() const
Get the lastBlock flag.
Definition: Encoder.h:124
Encoder(uint_t mode, Stream *stream, bool owner=true)
Constructor.
Definition: Encoder.h:51
bool isCRC() const
Get the crc flag.
Definition: Encoder.h:79
#define UTL_CLASS_DECL_ABC(DC, BC)
Declaration of standard UTL++ functionality for an abstract base class (ABC).
Definition: macros.h:650
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
Stream I/O abstraction.
Definition: Stream.h:68
void setCRC(bool crc)
Set the crc flag.
Definition: Encoder.h:86
constexpr size_t KB(size_t n)
Convert size in kilobytes to size in bytes.
Definition: util_inl.h:1008
Generate CRC-32 checksums.
Definition: CRC32.h:25
#define UTL_CLASS_NO_COPY
Declare that a class cannot do copy().
Definition: macros.h:358
Buffered stream.
void init()
Initialize UTL++.