Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LSBFEncoder.cpp « Common « 7zip - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 12a136bb91c3e8982d5b30c7cc88aa96227022ef (plain)
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
// LSBFEncoder.cpp

#include "StdAfx.h"

#include "LSBFEncoder.h"
#include "Common/Defs.h"

namespace NStream {
namespace NLSBF {

void CEncoder::WriteBits(UInt32 value, UInt32 numBits)
{
  while(numBits > 0)
  {
    UInt32 numNewBits = MyMin(numBits, m_BitPos);
    numBits -= numNewBits;

    UInt32 mask = (1 << numNewBits) - 1;
    m_CurByte |= (value & mask) << (8 - m_BitPos);
    value >>= numNewBits;

    m_BitPos -= numNewBits;

    if (m_BitPos == 0)
    {
      m_Stream.WriteByte(m_CurByte);
      m_BitPos = 8;
      m_CurByte = 0;
    }
  }
}


void CReverseEncoder::WriteBits(UInt32 value, UInt32 numBits)
{
  UInt32 reverseValue = 0;
  for(UInt32 i = 0; i < numBits; i++) 
  {
    reverseValue <<= 1;
    reverseValue |= value & 1;
    value >>= 1;
  } 
  m_Encoder->WriteBits(reverseValue, numBits);
}

}}