File: getbits.hpp

package info (click to toggle)
unrar-nonfree 1%3A7.1.8-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 1,952 kB
  • sloc: cpp: 26,394; makefile: 712; sh: 11
file content (81 lines) | stat: -rw-r--r-- 2,044 bytes parent folder | download | duplicates (2)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef _RAR_GETBITS_
#define _RAR_GETBITS_

class BitInput
{
  public:
    enum BufferSize {MAX_SIZE=0x8000}; // Size of input buffer.

    int InAddr; // Curent byte position in the buffer.
    int InBit;  // Current bit position in the current byte.

    bool ExternalBuffer;
  public:
    BitInput(bool AllocBuffer);
    ~BitInput();

    byte *InBuf; // Dynamically allocated input buffer.

    void InitBitInput()
    {
      InAddr=InBit=0;
    }
    
    // Move forward by 'Bits' bits.
    void addbits(uint Bits)
    {
      Bits+=InBit;
      InAddr+=Bits>>3;
      InBit=Bits&7;
    }

    // Return 16 bits from current position in the buffer.
    // Bit at (InAddr,InBit) has the highest position in returning data.
    uint getbits()
    {
#if defined(LITTLE_ENDIAN) && defined(ALLOW_MISALIGNED)
      uint32 BitField=RawGetBE4(InBuf+InAddr);
      BitField >>= (16-InBit);
#else
      uint BitField=(uint)InBuf[InAddr] << 16;
      BitField|=(uint)InBuf[InAddr+1] << 8;
      BitField|=(uint)InBuf[InAddr+2];
      BitField >>= (8-InBit);
#endif
      return BitField & 0xffff;
    }


    // Return 32 bits from current position in the buffer.
    // Bit at (InAddr,InBit) has the highest position in returning data.
    uint getbits32()
    {
      uint BitField=RawGetBE4(InBuf+InAddr);
      BitField <<= InBit;
      BitField|=(uint)InBuf[InAddr+4] >> (8-InBit);
      return BitField & 0xffffffff;
    }

    // Return 64 bits from current position in the buffer.
    // Bit at (InAddr,InBit) has the highest position in returning data.
    uint64 getbits64()
    {
      uint64 BitField=RawGetBE8(InBuf+InAddr);
      BitField <<= InBit;
      BitField|=(uint)InBuf[InAddr+8] >> (8-InBit);
      return BitField;
    }
    
    void faddbits(uint Bits);
    uint fgetbits();
    
    // Check if buffer has enough space for IncPtr bytes. Returns 'true'
    // if buffer will be overflown.
    bool Overflow(uint IncPtr) 
    {
      return InAddr+IncPtr>=MAX_SIZE;
    }

    void SetExternalBuffer(byte *Buf);
};
#endif