1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// LimitedStreams.h
2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
3baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifndef __LIMITED_STREAMS_H
4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define __LIMITED_STREAMS_H
5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../Common/MyCom.h"
7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../Common/MyVector.h"
8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../IStream.h"
9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
10baa3858d3f5d128a5c8466b700098109edcad5f2repo syncclass CLimitedSequentialInStream:
11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  public ISequentialInStream,
12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  public CMyUnknownImp
13baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
14baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CMyComPtr<ISequentialInStream> _stream;
15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 _size;
16baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 _pos;
17baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool _wasFinished;
18baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpublic:
19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  void SetStream(ISequentialInStream *stream) { _stream = stream; }
20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  void ReleaseStream() { _stream.Release(); }
21baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  void Init(UInt64 streamSize)
22baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    _size = streamSize;
24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    _pos = 0;
25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    _wasFinished = false;
26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  MY_UNKNOWN_IMP
29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize);
31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 GetSize() const { return _pos; }
32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool WasFinished() const { return _wasFinished; }
33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync};
34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
35baa3858d3f5d128a5c8466b700098109edcad5f2repo syncclass CLimitedInStream:
36baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  public IInStream,
37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  public CMyUnknownImp
38baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CMyComPtr<IInStream> _stream;
40baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 _virtPos;
41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 _physPos;
42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 _size;
43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 _startOffset;
44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  HRESULT SeekToPhys() { return _stream->Seek(_physPos, STREAM_SEEK_SET, NULL); }
46baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpublic:
47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  void SetStream(IInStream *stream) { _stream = stream; }
48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  HRESULT InitAndSeek(UInt64 startOffset, UInt64 size)
49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    _startOffset = startOffset;
51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    _physPos = startOffset;
52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    _virtPos = 0;
53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    _size = size;
54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return SeekToPhys();
55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
56baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
57baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  MY_UNKNOWN_IMP1(IInStream)
58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
59baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize);
60baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition);
61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  HRESULT SeekToStart() { return Seek(0, STREAM_SEEK_SET, NULL); }
63baa3858d3f5d128a5c8466b700098109edcad5f2repo sync};
64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
65baa3858d3f5d128a5c8466b700098109edcad5f2repo syncclass CClusterInStream:
66baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  public IInStream,
67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  public CMyUnknownImp
68baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 _virtPos;
70baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 _physPos;
71baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 _curRem;
72baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpublic:
73baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CMyComPtr<IInStream> Stream;
74baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 StartOffset;
75baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 Size;
76baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int BlockSizeLog;
77baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CRecordVector<UInt32> Vector;
78baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
79baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  HRESULT SeekToPhys() { return Stream->Seek(_physPos, STREAM_SEEK_SET, NULL); }
80baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
81baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  HRESULT InitAndSeek()
82baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
83baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    _curRem = 0;
84baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    _virtPos = 0;
85baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    _physPos = StartOffset;
86baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (Vector.Size() > 0)
87baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
88baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      _physPos = StartOffset + (Vector[0] << BlockSizeLog);
89baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return SeekToPhys();
90baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
91baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return S_OK;
92baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
93baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
94baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  MY_UNKNOWN_IMP1(IInStream)
95baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
96baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize);
97baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition);
98baa3858d3f5d128a5c8466b700098109edcad5f2repo sync};
99baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
100baa3858d3f5d128a5c8466b700098109edcad5f2repo syncHRESULT CreateLimitedInStream(IInStream *inStream, UInt64 pos, UInt64 size, ISequentialInStream **resStream);
101baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
102baa3858d3f5d128a5c8466b700098109edcad5f2repo syncclass CLimitedSequentialOutStream:
103baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  public ISequentialOutStream,
104baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  public CMyUnknownImp
105baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
106baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CMyComPtr<ISequentialOutStream> _stream;
107baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 _size;
108baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool _overflow;
109baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool _overflowIsAllowed;
110baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpublic:
111baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  MY_UNKNOWN_IMP
112baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize);
113baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  void SetStream(ISequentialOutStream *stream) { _stream = stream; }
114baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  void ReleaseStream() { _stream.Release(); }
115baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  void Init(UInt64 size, bool overflowIsAllowed = false)
116baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
117baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    _size = size;
118baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    _overflow = false;
119baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    _overflowIsAllowed = overflowIsAllowed;
120baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
121baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool IsFinishedOK() const { return (_size == 0 && !_overflow); }
122baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 GetRem() const { return _size; }
123baa3858d3f5d128a5c8466b700098109edcad5f2repo sync};
124baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
125baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
126