Run dos2unix on bayou and remove white space at the end of lines.
[coreboot.git] / payloads / bayou / util / pbuilder / lzma / C / 7zip / Compress / LZ / LZInWindow.h
1 // LZInWindow.h
2
3 #ifndef __LZ_IN_WINDOW_H
4 #define __LZ_IN_WINDOW_H
5
6 #include "../../IStream.h"
7
8 class CLZInWindow
9 {
10   Byte *_bufferBase; // pointer to buffer with data
11   ISequentialInStream *_stream;
12   UInt32 _posLimit;  // offset (from _buffer) when new block reading must be done
13   bool _streamEndWasReached; // if (true) then _streamPos shows real end of stream
14   const Byte *_pointerToLastSafePosition;
15 protected:
16   Byte  *_buffer;   // Pointer to virtual Buffer begin
17   UInt32 _blockSize;  // Size of Allocated memory block
18   UInt32 _pos;             // offset (from _buffer) of curent byte
19   UInt32 _keepSizeBefore;  // how many BYTEs must be kept in buffer before _pos
20   UInt32 _keepSizeAfter;   // how many BYTEs must be kept buffer after _pos
21   UInt32 _streamPos;   // offset (from _buffer) of first not read byte from Stream
22
23   void MoveBlock();
24   HRESULT ReadBlock();
25   void Free();
26 public:
27   CLZInWindow(): _bufferBase(0) {}
28   virtual ~CLZInWindow() { Free(); }
29
30   // keepSizeBefore + keepSizeAfter + keepSizeReserv < 4G)
31   bool Create(UInt32 keepSizeBefore, UInt32 keepSizeAfter, UInt32 keepSizeReserv = (1<<17));
32
33   void SetStream(ISequentialInStream *stream);
34   HRESULT Init();
35   // void ReleaseStream();
36
37   Byte *GetBuffer() const { return _buffer; }
38
39   const Byte *GetPointerToCurrentPos() const { return _buffer + _pos; }
40
41   HRESULT MovePos()
42   {
43     _pos++;
44     if (_pos > _posLimit)
45     {
46       const Byte *pointerToPostion = _buffer + _pos;
47       if(pointerToPostion > _pointerToLastSafePosition)
48         MoveBlock();
49       return ReadBlock();
50     }
51     else
52       return S_OK;
53   }
54   Byte GetIndexByte(Int32 index) const  {  return _buffer[(size_t)_pos + index]; }
55
56   // index + limit have not to exceed _keepSizeAfter;
57   // -2G <= index < 2G
58   UInt32 GetMatchLen(Int32 index, UInt32 distance, UInt32 limit) const
59   {
60     if(_streamEndWasReached)
61       if ((_pos + index) + limit > _streamPos)
62         limit = _streamPos - (_pos + index);
63     distance++;
64     const Byte *pby = _buffer + (size_t)_pos + index;
65     UInt32 i;
66     for(i = 0; i < limit && pby[i] == pby[(size_t)i - distance]; i++);
67     return i;
68   }
69
70   UInt32 GetNumAvailableBytes() const { return _streamPos - _pos; }
71
72   void ReduceOffsets(Int32 subValue)
73   {
74     _buffer += subValue;
75     _posLimit -= subValue;
76     _pos -= subValue;
77     _streamPos -= subValue;
78   }
79
80   bool NeedMove(UInt32 numCheckBytes)
81   {
82     UInt32 reserv = _pointerToLastSafePosition - (_buffer + _pos);
83     return (reserv <= numCheckBytes);
84   }
85 };
86
87 #endif