summaryrefslogtreecommitdiffstats
path: root/hostTools/lzma/compress/InByte.h
blob: 49bf2f32edb4b8701dc2edeb91b25626a1a462ce (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
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef __STREAM_INBYTE_H
#define __STREAM_INBYTE_H

#include "IInOutStreams.h"

namespace NStream {

class CInByte
{
  UINT64 m_ProcessedSize;
  BYTE *m_BufferBase;
  UINT32 m_BufferSize;
  BYTE *m_Buffer;
  BYTE *m_BufferLimit;
  ISequentialInStream* m_Stream;
  bool m_StreamWasExhausted;

  bool ReadBlock();

public:
  CInByte(UINT32 aBufferSize = 0x100000);
  ~CInByte();
  
  void Init(ISequentialInStream *aStream);

  bool ReadByte(BYTE &aByte)
    {
      if(m_Buffer >= m_BufferLimit)
        if(!ReadBlock())
          return false;
      aByte = *m_Buffer++;
      return true;
    }
  BYTE ReadByte()
    {
      if(m_Buffer >= m_BufferLimit)
        if(!ReadBlock())
          return 0x0;
      return *m_Buffer++;
    }
  void ReadBytes(void *aData, UINT32 aSize, UINT32 &aProcessedSize)
    {
      for(aProcessedSize = 0; aProcessedSize < aSize; aProcessedSize++)
        if (!ReadByte(((BYTE *)aData)[aProcessedSize]))
          return;
    }
  bool ReadBytes(void *aData, UINT32 aSize)
    {
      UINT32 aProcessedSize;
      ReadBytes(aData, aSize, aProcessedSize);
      return (aProcessedSize == aSize);
    }
  UINT64 GetProcessedSize() const { return m_ProcessedSize + (m_Buffer - m_BufferBase); }
};

}

#endif