1370b324cSopenharmony_ci// StreamObjects.h
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#ifndef ZIP7_INC_STREAM_OBJECTS_H
4370b324cSopenharmony_ci#define ZIP7_INC_STREAM_OBJECTS_H
5370b324cSopenharmony_ci
6370b324cSopenharmony_ci#include "../../Common/MyBuffer.h"
7370b324cSopenharmony_ci#include "../../Common/MyCom.h"
8370b324cSopenharmony_ci#include "../../Common/MyVector.h"
9370b324cSopenharmony_ci
10370b324cSopenharmony_ci#include "../IStream.h"
11370b324cSopenharmony_ci
12370b324cSopenharmony_ciZ7_CLASS_IMP_IInStream(
13370b324cSopenharmony_ci  CBufferInStream
14370b324cSopenharmony_ci)
15370b324cSopenharmony_ci  UInt64 _pos;
16370b324cSopenharmony_cipublic:
17370b324cSopenharmony_ci  CByteBuffer Buf;
18370b324cSopenharmony_ci  void Init() { _pos = 0; }
19370b324cSopenharmony_ci};
20370b324cSopenharmony_ci
21370b324cSopenharmony_ci
22370b324cSopenharmony_ciZ7_CLASS_IMP_COM_0(
23370b324cSopenharmony_ci  CReferenceBuf
24370b324cSopenharmony_ci)
25370b324cSopenharmony_cipublic:
26370b324cSopenharmony_ci  CByteBuffer Buf;
27370b324cSopenharmony_ci};
28370b324cSopenharmony_ci
29370b324cSopenharmony_ci
30370b324cSopenharmony_ciZ7_CLASS_IMP_IInStream(
31370b324cSopenharmony_ci  CBufInStream
32370b324cSopenharmony_ci)
33370b324cSopenharmony_ci  const Byte *_data;
34370b324cSopenharmony_ci  UInt64 _pos;
35370b324cSopenharmony_ci  size_t _size;
36370b324cSopenharmony_ci  CMyComPtr<IUnknown> _ref;
37370b324cSopenharmony_cipublic:
38370b324cSopenharmony_ci  void Init(const Byte *data, size_t size, IUnknown *ref = NULL)
39370b324cSopenharmony_ci  {
40370b324cSopenharmony_ci    _data = data;
41370b324cSopenharmony_ci    _size = size;
42370b324cSopenharmony_ci    _pos = 0;
43370b324cSopenharmony_ci    _ref = ref;
44370b324cSopenharmony_ci  }
45370b324cSopenharmony_ci  void Init(CReferenceBuf *ref) { Init(ref->Buf, ref->Buf.Size(), ref); }
46370b324cSopenharmony_ci
47370b324cSopenharmony_ci  // Seek() is allowed here. So reading order could be changed
48370b324cSopenharmony_ci  bool WasFinished() const { return _pos == _size; }
49370b324cSopenharmony_ci};
50370b324cSopenharmony_ci
51370b324cSopenharmony_ci
52370b324cSopenharmony_civoid Create_BufInStream_WithReference(const void *data, size_t size, IUnknown *ref, ISequentialInStream **stream);
53370b324cSopenharmony_civoid Create_BufInStream_WithNewBuffer(const void *data, size_t size, ISequentialInStream **stream);
54370b324cSopenharmony_ciinline void Create_BufInStream_WithNewBuffer(const CByteBuffer &buf, ISequentialInStream **stream)
55370b324cSopenharmony_ci  { Create_BufInStream_WithNewBuffer(buf, buf.Size(), stream); }
56370b324cSopenharmony_ci
57370b324cSopenharmony_ci
58370b324cSopenharmony_ciclass CByteDynBuffer Z7_final
59370b324cSopenharmony_ci{
60370b324cSopenharmony_ci  size_t _capacity;
61370b324cSopenharmony_ci  Byte *_buf;
62370b324cSopenharmony_ci  Z7_CLASS_NO_COPY(CByteDynBuffer)
63370b324cSopenharmony_cipublic:
64370b324cSopenharmony_ci  CByteDynBuffer(): _capacity(0), _buf(NULL) {}
65370b324cSopenharmony_ci  // there is no copy constructor. So don't copy this object.
66370b324cSopenharmony_ci  ~CByteDynBuffer() { Free(); }
67370b324cSopenharmony_ci  void Free() throw();
68370b324cSopenharmony_ci  size_t GetCapacity() const { return _capacity; }
69370b324cSopenharmony_ci  operator Byte*() const { return _buf; }
70370b324cSopenharmony_ci  operator const Byte*() const { return _buf; }
71370b324cSopenharmony_ci  bool EnsureCapacity(size_t capacity) throw();
72370b324cSopenharmony_ci};
73370b324cSopenharmony_ci
74370b324cSopenharmony_ci
75370b324cSopenharmony_ciZ7_CLASS_IMP_COM_1(
76370b324cSopenharmony_ci  CDynBufSeqOutStream
77370b324cSopenharmony_ci  , ISequentialOutStream
78370b324cSopenharmony_ci)
79370b324cSopenharmony_ci  CByteDynBuffer _buffer;
80370b324cSopenharmony_ci  size_t _size;
81370b324cSopenharmony_cipublic:
82370b324cSopenharmony_ci  CDynBufSeqOutStream(): _size(0) {}
83370b324cSopenharmony_ci  void Init() { _size = 0;  }
84370b324cSopenharmony_ci  size_t GetSize() const { return _size; }
85370b324cSopenharmony_ci  const Byte *GetBuffer() const { return _buffer; }
86370b324cSopenharmony_ci  void CopyToBuffer(CByteBuffer &dest) const;
87370b324cSopenharmony_ci  Byte *GetBufPtrForWriting(size_t addSize);
88370b324cSopenharmony_ci  void UpdateSize(size_t addSize) { _size += addSize; }
89370b324cSopenharmony_ci};
90370b324cSopenharmony_ci
91370b324cSopenharmony_ci
92370b324cSopenharmony_ciZ7_CLASS_IMP_COM_1(
93370b324cSopenharmony_ci  CBufPtrSeqOutStream
94370b324cSopenharmony_ci  , ISequentialOutStream
95370b324cSopenharmony_ci)
96370b324cSopenharmony_ci  Byte *_buffer;
97370b324cSopenharmony_ci  size_t _size;
98370b324cSopenharmony_ci  size_t _pos;
99370b324cSopenharmony_cipublic:
100370b324cSopenharmony_ci  void Init(Byte *buffer, size_t size)
101370b324cSopenharmony_ci  {
102370b324cSopenharmony_ci    _buffer = buffer;
103370b324cSopenharmony_ci    _pos = 0;
104370b324cSopenharmony_ci    _size = size;
105370b324cSopenharmony_ci  }
106370b324cSopenharmony_ci  size_t GetPos() const { return _pos; }
107370b324cSopenharmony_ci};
108370b324cSopenharmony_ci
109370b324cSopenharmony_ci
110370b324cSopenharmony_ciZ7_CLASS_IMP_COM_1(
111370b324cSopenharmony_ci  CSequentialOutStreamSizeCount
112370b324cSopenharmony_ci  , ISequentialOutStream
113370b324cSopenharmony_ci)
114370b324cSopenharmony_ci  CMyComPtr<ISequentialOutStream> _stream;
115370b324cSopenharmony_ci  UInt64 _size;
116370b324cSopenharmony_cipublic:
117370b324cSopenharmony_ci  void SetStream(ISequentialOutStream *stream) { _stream = stream; }
118370b324cSopenharmony_ci  void Init() { _size = 0; }
119370b324cSopenharmony_ci  UInt64 GetSize() const { return _size; }
120370b324cSopenharmony_ci};
121370b324cSopenharmony_ci
122370b324cSopenharmony_ci
123370b324cSopenharmony_ciclass CCachedInStream:
124370b324cSopenharmony_ci  public IInStream,
125370b324cSopenharmony_ci  public CMyUnknownImp
126370b324cSopenharmony_ci{
127370b324cSopenharmony_ci  Z7_IFACES_IMP_UNK_2(ISequentialInStream, IInStream)
128370b324cSopenharmony_ci
129370b324cSopenharmony_ci  UInt64 *_tags;
130370b324cSopenharmony_ci  Byte *_data;
131370b324cSopenharmony_ci  size_t _dataSize;
132370b324cSopenharmony_ci  unsigned _blockSizeLog;
133370b324cSopenharmony_ci  unsigned _numBlocksLog;
134370b324cSopenharmony_ci  UInt64 _size;
135370b324cSopenharmony_ci  UInt64 _pos;
136370b324cSopenharmony_ciprotected:
137370b324cSopenharmony_ci  virtual HRESULT ReadBlock(UInt64 blockIndex, Byte *dest, size_t blockSize) = 0;
138370b324cSopenharmony_cipublic:
139370b324cSopenharmony_ci  CCachedInStream(): _tags(NULL), _data(NULL) {}
140370b324cSopenharmony_ci  virtual ~CCachedInStream() { Free(); } // the destructor must be virtual (Release() calls it) !!!
141370b324cSopenharmony_ci  void Free() throw();
142370b324cSopenharmony_ci  bool Alloc(unsigned blockSizeLog, unsigned numBlocksLog) throw();
143370b324cSopenharmony_ci  void Init(UInt64 size) throw();
144370b324cSopenharmony_ci};
145370b324cSopenharmony_ci
146370b324cSopenharmony_ci#endif
147