| 229 | } |
| 230 | |
| 231 | std::int32_t ZstdStream::ReadInternal(void* ptr, std::int32_t size) |
| 232 | { |
| 233 | if (_state == State::Created) { |
| 234 | InitializeInternal(); |
| 235 | } |
| 236 | if (size <= 0 || _state == State::Finished) { |
| 237 | return 0; |
| 238 | } |
| 239 | if (_state == State::Unknown || _state >= State::Failed) { |
| 240 | return Stream::Invalid; |
| 241 | } |
| 242 | |
| 243 | std::int32_t n = (_outBufferLength - _outBufferPos); |
| 244 | while (n == 0) { |
| 245 | if (_inBufferPos >= _inBufferLength) { |
| 246 | std::int32_t bytesRead = _inputSize; |
| 247 | if (bytesRead < 0 || bytesRead > _inBufferCapacity) { |
| 248 | bytesRead = _inBufferCapacity; |
| 249 | } |
| 250 | |
| 251 | bytesRead = (std::int32_t)_inputStream->Read(&_buffer[0], bytesRead); |
| 252 | if (bytesRead <= 0) { |
| 253 | return Stream::Invalid; |
| 254 | } |
| 255 | |
| 256 | if (_inputSize > 0) { |
| 257 | _inputSize -= bytesRead; |
| 258 | } |
| 259 | |
| 260 | _inBufferPos = 0; |
| 261 | } |
| 262 | |
| 263 | ZSTD_inBuffer inBuffer = { &_buffer[0], _inBufferCapacity, _inBufferLength }; |
| 264 | ZSTD_outBuffer outBuffer = { &_buffer[_inBufferCapacity], _outBufferCapacity, _outBufferLength }; |
| 265 | std::size_t result = ZSTD_decompressStream(_strm, &outBuffer, &inBuffer); |
| 266 | if (ZSTD_isError(result)) { |
| 267 | #if defined(DEATH_TRACE_VERBOSE_IO) |
| 268 | LOGE("ZSTD_decompressStream() failed with error 0x{:x} ({})", result, ZSTD_getErrorName(result)); |
| 269 | #endif |
| 270 | _state = State::Failed; |
| 271 | return Stream::Invalid; |
| 272 | } |
| 273 | |
| 274 | _inBufferLength = inBuffer.pos; |
| 275 | _outBufferLength = outBuffer.pos; |
| 276 | |
| 277 | n = (_outBufferLength - _outBufferPos); |
| 278 | } |
| 279 | |
| 280 | if (n > size) { |
| 281 | n = size; |
| 282 | } |
| 283 | std::memcpy(ptr, &_buffer[_inBufferCapacity + _outBufferPos], n); |
| 284 | _outBufferPos += n; |
| 285 | return n; |
| 286 | } |
| 287 | |
| 288 | bool ZstdStream::CeaseReading() |