AuroraRuntime/Source/Compression/BaseStream.cpp
Reece Wilson 8844e8fe64 [+] AuCrypto::BCrypt
> GetForcedMinRounds
> GenSalt
> HashPW
> HashPWEx
> CheckPassword
> CheckPasswordEx
[*] Refactor AuCompression APIs
[*] Clean up AuTryConstructs
[+] Internal compression API for compression based interceptors
[+] Root-level input stream arg check for all compression apis (harden)
[*] Clean up AuCompression code
[+] Solar Designer / OpenWall blowfish crypt
[*] BlowCrypt: accept length input parameter
[*] Split locale into 2 source files
[-] Ugly comment from Open.Win32.cpp. TODO: Readd later. Might warn on empty string bc it makes sense given, "." and "/" normalizes to nothing, and memory pre-idc-if-drops are dropped siliently.
2022-09-15 20:48:50 +01:00

125 lines
3.4 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: BaseStream.cpp
Date: 2022-2-14
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "Compression.hpp"
#include "IngestableReadBase.hpp"
#include "BaseStream.hpp"
namespace Aurora::Compression
{
AuStreamReadWrittenPair_t BaseStream::ReadEx(const Memory::MemoryViewWrite & /*opt*/ destination, bool ingestUntilEOS)
{
AU_LOCK_GUARD(this->_spinlock);
AuUInt32 read {}, len {};
if (!destination.length && !destination.ptr)
{
return {0, this->pOutputBuffer_->RemainingBytes()};
}
if (ingestUntilEOS)
{
while (this->pOutputBuffer_->RemainingBytes() < destination.length)
{
auto toRead = destination.length ? AuUInt32(destination.length - this->pOutputBuffer_->RemainingBytes()) : 10 * 1024;
if (Ingest_s(toRead).second == 0)
{
if (!this->pOutputBuffer_->RemainingBytes(true))
{
return {};
}
break;
}
read += toRead;
}
}
len = this->pOutputBuffer_->Read(destination.ptr, destination.length, destination.ptr == nullptr);
return {read, len};
}
AuUInt32 BaseStream::GetAvailableProcessedBytes()
{
AU_LOCK_GUARD(this->_spinlock);
return this->pOutputBuffer_->RemainingBytes(true);
}
AuUInt32 BaseStream::Read(const Memory::MemoryViewWrite & /*opt*/ destination)
{
AU_LOCK_GUARD(this->_spinlock);
if (!destination.length && !destination.ptr)
{
return this->pOutputBuffer_->RemainingBytes();
}
return this->pOutputBuffer_->Read(destination.ptr, destination.length, destination.ptr == nullptr);
}
bool BaseStream::GoBackByProcessedN(AuUInt32 offset)
{
AU_LOCK_GUARD(this->_spinlock);
return this->pOutputBuffer_->ReaderTryGoBack(offset);
}
bool BaseStream::GoForwardByProcessedN(AuUInt32 offset)
{
AU_LOCK_GUARD(this->_spinlock);
if (!offset)
{
return true;
}
return this->pOutputBuffer_->ReaderTryGoForward(offset);
}
AuStreamReadWrittenPair_t BaseStream::Ingest(AuUInt32 bytesFromUnprocessedInputSource)
{
AU_LOCK_GUARD(this->_spinlock);
if (!bytesFromUnprocessedInputSource)
{
return {};
}
return Ingest_s(bytesFromUnprocessedInputSource);
}
bool BaseStream::IsValid()
{
return this->uBufferSize_ ? (this->pOutputBuffer_ && this->pOutputBuffer_->IsValid()) : true;
}
AuSPtr<Memory::ByteBuffer> BaseStream::GetBuffer()
{
return this->pOutputBuffer_;
}
void BaseStream::SetBuffer(const AuSPtr<Memory::ByteBuffer> &pBuffer)
{
if (!pBuffer)
{
this->pOutputBuffer_ = AuUnsafeRaiiToShared(&this->_outbufferOwned);
}
else
{
this->pOutputBuffer_ = pBuffer;
}
}
bool BaseStream::Write(const void *a, AuUInt32 length)
{
return this->pOutputBuffer_->Write(reinterpret_cast<const AuUInt8 *>(a), length) == length;
}
AuUInt32 BaseStream::GetInternalBufferSize()
{
return this->pOutputBuffer_->allocSize;
}
}