summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/lzma/CPP/7zip/Common/InOutTempBuffer.cpp
blob: 3f7272ef8205fd12d2d65b36623f36048c019b01 (plain) (blame)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// InOutTempBuffer.cpp

#include "StdAfx.h"

#include "../../../C/Alloc.h"

#include "InOutTempBuffer.h"

#include "StreamUtils.h"

#ifdef USE_InOutTempBuffer_FILE

#include "../../../C/7zCrc.h"

#define kTempFilePrefixString FTEXT("7zt")
/*
  Total buffer size limit, if we use temp file scheme:
    32-bit:  16 MiB = 1 MiB *   16 buffers
    64-bit:   4 GiB = 1 MiB * 4096 buffers
*/
static const size_t kNumBufsMax = (size_t)1 << (sizeof(size_t) * 2 - 4);

#endif

static const size_t kBufSize = (size_t)1 << 20;


CInOutTempBuffer::CInOutTempBuffer():
    _size(0),
    _bufs(NULL),
    _numBufs(0),
    _numFilled(0)
{
 #ifdef USE_InOutTempBuffer_FILE
  _tempFile_Created = false;
  _useMemOnly = false;
  _crc = CRC_INIT_VAL;
 #endif
}

CInOutTempBuffer::~CInOutTempBuffer()
{
  for (size_t i = 0; i < _numBufs; i++)
    MyFree(_bufs[i]);
  MyFree(_bufs);
}


void *CInOutTempBuffer::GetBuf(size_t index)
{
  if (index >= _numBufs)
  {
    const size_t num = (_numBufs == 0 ? 16 : _numBufs * 2);
    void **p = (void **)MyRealloc(_bufs, num * sizeof(void *));
    if (!p)
      return NULL;
    _bufs = p;
    memset(p + _numBufs, 0, (num - _numBufs) * sizeof(void *));
    _numBufs = num;
  }
  
  void *buf = _bufs[index];
  if (!buf)
  {
    buf = MyAlloc(kBufSize);
    if (buf)
      _bufs[index] = buf;
  }
  return buf;
}


HRESULT CInOutTempBuffer::Write_HRESULT(const void *data, UInt32 size)
{
  if (size == 0)
    return S_OK;
  
 #ifdef USE_InOutTempBuffer_FILE
  if (!_tempFile_Created)
 #endif
  for (;;)  // loop for additional attemp to allocate memory after file creation error
  {
   #ifdef USE_InOutTempBuffer_FILE
    bool allocError = false;
   #endif
  
    for (;;)  // loop for writing to buffers
    {
      const size_t index = (size_t)(_size / kBufSize);
      
     #ifdef USE_InOutTempBuffer_FILE
      if (index >= kNumBufsMax && !_useMemOnly)
        break;
     #endif
    
      void *buf = GetBuf(index);
      if (!buf)
      {
       #ifdef USE_InOutTempBuffer_FILE
        if (!_useMemOnly)
        {
          allocError = true;
          break;
        }
       #endif
        return E_OUTOFMEMORY;
      }
      
      const size_t offset = (size_t)(_size) & (kBufSize - 1);
      size_t cur = kBufSize - offset;
      if (cur > size)
        cur = size;
      memcpy((Byte *)buf + offset, data, cur);
      _size += cur;
      if (index >= _numFilled)
        _numFilled = index + 1;
      data = (const void *)((const Byte *)data + cur);
      size -= (UInt32)cur;
      if (size == 0)
        return S_OK;
    }

   #ifdef USE_InOutTempBuffer_FILE
   #ifndef _WIN32
    _outFile.mode_for_Create = 0600;  // only owner will have the rights to access this file
   #endif
    if (_tempFile.CreateRandomInTempFolder(kTempFilePrefixString, &_outFile))
    {
      _tempFile_Created = true;
      break;
    }
    _useMemOnly = true;
    if (allocError)
      return GetLastError_noZero_HRESULT();
   #endif
  }

 #ifdef USE_InOutTempBuffer_FILE
  if (!_outFile.WriteFull(data, size))
    return GetLastError_noZero_HRESULT();
  _crc = CrcUpdate(_crc, data, size);
  _size += size;
  return S_OK;
 #endif
}


HRESULT CInOutTempBuffer::WriteToStream(ISequentialOutStream *stream)
{
  UInt64 rem = _size;
  // if (rem == 0) return S_OK;

  const size_t numFilled = _numFilled;
  _numFilled = 0;

  for (size_t i = 0; i < numFilled; i++)
  {
    if (rem == 0)
      return E_FAIL;
    size_t cur = kBufSize;
    if (cur > rem)
      cur = (size_t)rem;
    RINOK(WriteStream(stream, _bufs[i], cur))
    rem -= cur;
   #ifdef USE_InOutTempBuffer_FILE
    // we will use _bufs[0] later for writing from temp file
    if (i != 0 || !_tempFile_Created)
   #endif
    {
      MyFree(_bufs[i]);
      _bufs[i] = NULL;
    }
  }


 #ifdef USE_InOutTempBuffer_FILE

  if (rem == 0)
    return _tempFile_Created ? E_FAIL : S_OK;

  if (!_tempFile_Created)
    return E_FAIL;

  if (!_outFile.Close())
    return GetLastError_noZero_HRESULT();

  HRESULT hres;
  void *buf = GetBuf(0); // index
  if (!buf)
    hres = E_OUTOFMEMORY;
  else
  {
    NWindows::NFile::NIO::CInFile inFile;
    if (!inFile.Open(_tempFile.GetPath()))
      hres = GetLastError_noZero_HRESULT();
    else
    {
      UInt32 crc = CRC_INIT_VAL;
      for (;;)
      {
        size_t processed;
        if (!inFile.ReadFull(buf, kBufSize, processed))
        {
          hres = GetLastError_noZero_HRESULT();
          break;
        }
        if (processed == 0)
        {
          // we compare crc without CRC_GET_DIGEST
          hres = (_crc == crc ? S_OK : E_FAIL);
          break;
        }
        size_t n = processed;
        if (n > rem)
          n = (size_t)rem;
        hres = WriteStream(stream, buf, n);
        if (hres != S_OK)
          break;
        crc = CrcUpdate(crc, buf, n);
        rem -= n;
        if (n != processed)
        {
          hres = E_FAIL;
          break;
        }
      }
    }
  }

  // _tempFile.DisableDeleting(); // for debug
  _tempFile.Remove();
  RINOK(hres)

 #endif

  return rem == 0 ? S_OK : E_FAIL;
}