File: BitmEncoder.h

package info (click to toggle)
p7zip 16.02%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 14,148 kB
  • sloc: cpp: 167,145; ansic: 14,992; python: 1,911; asm: 1,688; sh: 1,132; makefile: 703
file content (49 lines) | stat: -rw-r--r-- 1,108 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// BitmEncoder.h -- the Most Significant Bit of byte is First

#ifndef __BITM_ENCODER_H
#define __BITM_ENCODER_H

#include "../IStream.h"

template<class TOutByte>
class CBitmEncoder
{
  unsigned _bitPos;
  Byte _curByte;
  TOutByte _stream;
public:
  bool Create(UInt32 bufferSize) { return _stream.Create(bufferSize); }
  void SetStream(ISequentialOutStream *outStream) { _stream.SetStream(outStream);}
  UInt64 GetProcessedSize() const { return _stream.GetProcessedSize() + ((8 - _bitPos + 7) >> 3); }
  void Init()
  {
    _stream.Init();
    _bitPos = 8;
    _curByte = 0;
  }
  HRESULT Flush()
  {
    if (_bitPos < 8)
      WriteBits(0, _bitPos);
    return _stream.Flush();
  }
  void WriteBits(UInt32 value, unsigned numBits)
  {
    while (numBits > 0)
    {
      if (numBits < _bitPos)
      {
        _curByte |= ((Byte)value << (_bitPos -= numBits));
        return;
      }
      numBits -= _bitPos;
      UInt32 newBits = (value >> numBits);
      value -= (newBits << numBits);
      _stream.WriteByte((Byte)(_curByte | newBits));
      _bitPos = 8;
      _curByte = 0;
    }
  }
};

#endif