summaryrefslogtreecommitdiffstats
path: root/other-licenses/7zstub/src/CPP/7zip/Archive/7z/7zFolderInStream.cpp
blob: eee11a085542ed969e64f16d725db5cd1fc5f6e4 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// 7zFolderInStream.cpp

#include "StdAfx.h"

#include "7zFolderInStream.h"

namespace NArchive {
namespace N7z {

void CFolderInStream::Init(IArchiveUpdateCallback *updateCallback,
    const UInt32 *indexes, unsigned numFiles)
{
  _updateCallback = updateCallback;
  _indexes = indexes;
  _numFiles = numFiles;
  _index = 0;
  
  Processed.ClearAndReserve(numFiles);
  CRCs.ClearAndReserve(numFiles);
  Sizes.ClearAndReserve(numFiles);
  
  _pos = 0;
  _crc = CRC_INIT_VAL;
  _size_Defined = false;
  _size = 0;

  _stream.Release();
}

HRESULT CFolderInStream::OpenStream()
{
  _pos = 0;
  _crc = CRC_INIT_VAL;
  _size_Defined = false;
  _size = 0;

  while (_index < _numFiles)
  {
    CMyComPtr<ISequentialInStream> stream;
    HRESULT result = _updateCallback->GetStream(_indexes[_index], &stream);
    if (result != S_OK)
    {
      if (result != S_FALSE)
        return result;
    }

    _stream = stream;
    
    if (stream)
    {
      CMyComPtr<IStreamGetSize> streamGetSize;
      stream.QueryInterface(IID_IStreamGetSize, &streamGetSize);
      if (streamGetSize)
      {
        if (streamGetSize->GetSize(&_size) == S_OK)
          _size_Defined = true;
      }
      return S_OK;
    }
    
    _index++;
    RINOK(_updateCallback->SetOperationResult(NArchive::NUpdate::NOperationResult::kOK));
    AddFileInfo(result == S_OK);
  }
  return S_OK;
}

void CFolderInStream::AddFileInfo(bool isProcessed)
{
  Processed.Add(isProcessed);
  Sizes.Add(_pos);
  CRCs.Add(CRC_GET_DIGEST(_crc));
}

STDMETHODIMP CFolderInStream::Read(void *data, UInt32 size, UInt32 *processedSize)
{
  if (processedSize)
    *processedSize = 0;
  while (size != 0)
  {
    if (_stream)
    {
      UInt32 cur = size;
      const UInt32 kMax = (UInt32)1 << 20;
      if (cur > kMax)
        cur = kMax;
      RINOK(_stream->Read(data, cur, &cur));
      if (cur != 0)
      {
        _crc = CrcUpdate(_crc, data, cur);
        _pos += cur;
        if (processedSize)
          *processedSize = cur;
        return S_OK;
      }
      
      _stream.Release();
      _index++;
      AddFileInfo(true);

      _pos = 0;
      _crc = CRC_INIT_VAL;
      _size_Defined = false;
      _size = 0;

      RINOK(_updateCallback->SetOperationResult(NArchive::NUpdate::NOperationResult::kOK));
    }
    
    if (_index >= _numFiles)
      break;
    RINOK(OpenStream());
  }
  return S_OK;
}

STDMETHODIMP CFolderInStream::GetSubStreamSize(UInt64 subStream, UInt64 *value)
{
  *value = 0;
  if (subStream > Sizes.Size())
    return S_FALSE; // E_FAIL;
  
  unsigned index = (unsigned)subStream;
  if (index < Sizes.Size())
  {
    *value = Sizes[index];
    return S_OK;
  }
  
  if (!_size_Defined)
  {
    *value = _pos;
    return S_FALSE;
  }
  
  *value = (_pos > _size ? _pos : _size);
  return S_OK;
}

}}