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